]> icculus.org git repositories - dana/openbox.git/blob - openbox/frame.c
merge r7212-7239 from trunk
[dana/openbox.git] / openbox / frame.c
1 /* -*- indent-tabs-mode: nil; tab-width: 4; c-basic-offset: 4; -*-
2
3    frame.c for the Openbox window manager
4    Copyright (c) 2006        Mikael Magnusson
5    Copyright (c) 2003-2007   Dana Jansens
6
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 2 of the License, or
10    (at your option) any later version.
11
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16
17    See the COPYING file for a copy of the GNU General Public License.
18 */
19
20 #include "frame.h"
21 #include "client.h"
22 #include "openbox.h"
23 #include "extensions.h"
24 #include "prop.h"
25 #include "config.h"
26 #include "framerender.h"
27 #include "mainloop.h"
28 #include "focus_cycle.h"
29 #include "focus_cycle_indicator.h"
30 #include "moveresize.h"
31 #include "screen.h"
32 #include "render/theme.h"
33
34 #define FRAME_EVENTMASK (EnterWindowMask | LeaveWindowMask | \
35                          ButtonPressMask | ButtonReleaseMask | \
36                          SubstructureRedirectMask | FocusChangeMask)
37 #define ELEMENT_EVENTMASK (ButtonPressMask | ButtonReleaseMask | \
38                            ButtonMotionMask | PointerMotionMask | \
39                            EnterWindowMask | LeaveWindowMask)
40
41 #define FRAME_ANIMATE_ICONIFY_TIME 150000 /* .15 seconds */
42 #define FRAME_ANIMATE_ICONIFY_STEP_TIME (G_USEC_PER_SEC / 60) /* 60 Hz */
43
44 #define FRAME_HANDLE_Y(f) (f->size.top + f->client->area.height + f->cbwidth_b)
45
46 static void flash_done(gpointer data);
47 static gboolean flash_timeout(gpointer data);
48
49 static void layout_title(ObFrame *self);
50 static void set_theme_statics(ObFrame *self);
51 static void free_theme_statics(ObFrame *self);
52 static gboolean frame_animate_iconify(gpointer self);
53 static void frame_adjust_cursors(ObFrame *self);
54
55 static Window createWindow(Window parent, Visual *visual,
56                            gulong mask, XSetWindowAttributes *attrib)
57 {
58     return XCreateWindow(ob_display, parent, 0, 0, 1, 1, 0,
59                          (visual ? 32 : RrDepth(ob_rr_inst)), InputOutput,
60                          (visual ? visual : RrVisual(ob_rr_inst)),
61                          mask, attrib);
62                        
63 }
64
65 static Visual *check_32bit_client(ObClient *c)
66 {
67     XWindowAttributes wattrib;
68     Status ret;
69
70     /* we're already running at 32 bit depth, yay. we don't need to use their
71        visual */
72     if (RrDepth(ob_rr_inst) == 32)
73         return NULL;
74
75     ret = XGetWindowAttributes(ob_display, c->window, &wattrib);
76     g_assert(ret != BadDrawable);
77     g_assert(ret != BadWindow);
78
79     if (wattrib.depth == 32)
80         return wattrib.visual;
81     return NULL;
82 }
83
84 ObFrame *frame_new(ObClient *client)
85 {
86     XSetWindowAttributes attrib;
87     gulong mask;
88     ObFrame *self;
89     Visual *visual;
90
91     self = g_new0(ObFrame, 1);
92     self->client = client;
93
94     visual = check_32bit_client(client);
95
96     /* create the non-visible decor windows */
97
98     mask = 0;
99     if (visual) {
100         /* client has a 32-bit visual */
101         mask |= CWColormap | CWBackPixel | CWBorderPixel;
102         /* create a colormap with the visual */
103         self->colormap = attrib.colormap =
104             XCreateColormap(ob_display,
105                             RootWindow(ob_display, ob_screen),
106                             visual, AllocNone);
107         attrib.background_pixel = BlackPixel(ob_display, ob_screen);
108         attrib.border_pixel = BlackPixel(ob_display, ob_screen);
109     }
110     self->window = createWindow(RootWindow(ob_display, ob_screen), visual,
111                                 mask, &attrib);
112
113     /* create the visible decor windows */
114
115     mask = 0;
116     if (visual) {
117         /* client has a 32-bit visual */
118         mask |= CWColormap | CWBackPixel | CWBorderPixel;
119         attrib.colormap = RrColormap(ob_rr_inst);
120     }
121
122     self->backback = createWindow(self->window, NULL, mask, &attrib);
123     self->backfront = createWindow(self->backback, NULL, mask, &attrib);
124
125     mask |= CWEventMask;
126     attrib.event_mask = ELEMENT_EVENTMASK;
127     self->title = createWindow(self->window, NULL, mask, &attrib);
128     self->titleleft = createWindow(self->window, NULL, mask, &attrib);
129     self->titletop = createWindow(self->window, NULL, mask, &attrib);
130     self->titletopleft = createWindow(self->window, NULL, mask, &attrib);
131     self->titletopright = createWindow(self->window, NULL, mask, &attrib);
132     self->titleright = createWindow(self->window, NULL, mask, &attrib);
133     self->titlebottom = createWindow(self->window, NULL, mask, &attrib);
134
135     self->topresize = createWindow(self->title, NULL, mask, &attrib);
136     self->tltresize = createWindow(self->title, NULL, mask, &attrib);
137     self->tllresize = createWindow(self->title, NULL, mask, &attrib);
138     self->trtresize = createWindow(self->title, NULL, mask, &attrib);
139     self->trrresize = createWindow(self->title, NULL, mask, &attrib);
140
141     self->left = createWindow(self->window, NULL, mask, &attrib);
142     self->right = createWindow(self->window, NULL, mask, &attrib);
143
144     self->innerleft = createWindow(self->window, NULL, mask, &attrib);
145     self->innertop = createWindow(self->window, NULL, mask, &attrib);
146     self->innerright = createWindow(self->window, NULL, mask, &attrib);
147     self->innerbottom = createWindow(self->window, NULL, mask, &attrib);
148
149     self->label = createWindow(self->title, NULL, mask, &attrib);
150     self->max = createWindow(self->title, NULL, mask, &attrib);
151     self->close = createWindow(self->title, NULL, mask, &attrib);
152     self->desk = createWindow(self->title, NULL, mask, &attrib);
153     self->shade = createWindow(self->title, NULL, mask, &attrib);
154     self->icon = createWindow(self->title, NULL, mask, &attrib);
155     self->iconify = createWindow(self->title, NULL, mask, &attrib);
156
157     self->handle = createWindow(self->window, NULL, mask, &attrib);
158     self->lgrip = createWindow(self->handle, NULL, mask, &attrib);
159     self->rgrip = createWindow(self->handle, NULL, mask, &attrib); 
160
161     self->handleleft = createWindow(self->handle, NULL, mask, &attrib);
162     self->handleright = createWindow(self->handle, NULL, mask, &attrib);
163
164     self->handletop = createWindow(self->window, NULL, mask, &attrib);
165     self->handlebottom = createWindow(self->window, NULL, mask, &attrib);
166     self->lgripleft = createWindow(self->window, NULL, mask, &attrib);
167     self->lgriptop = createWindow(self->window, NULL, mask, &attrib);
168     self->lgripbottom = createWindow(self->window, NULL, mask, &attrib);
169     self->rgripright = createWindow(self->window, NULL, mask, &attrib);
170     self->rgriptop = createWindow(self->window, NULL, mask, &attrib);
171     self->rgripbottom = createWindow(self->window, NULL, mask, &attrib);
172
173     self->focused = FALSE;
174
175     /* the other stuff is shown based on decor settings */
176     XMapWindow(ob_display, self->label);
177     XMapWindow(ob_display, self->backback);
178     XMapWindow(ob_display, self->backfront);
179
180     self->max_press = self->close_press = self->desk_press = 
181         self->iconify_press = self->shade_press = FALSE;
182     self->max_hover = self->close_hover = self->desk_hover = 
183         self->iconify_hover = self->shade_hover = FALSE;
184
185     set_theme_statics(self);
186
187     return (ObFrame*)self;
188 }
189
190 static void set_theme_statics(ObFrame *self)
191 {
192     /* set colors/appearance/sizes for stuff that doesn't change */
193     XResizeWindow(ob_display, self->max,
194                   ob_rr_theme->button_size, ob_rr_theme->button_size);
195     XResizeWindow(ob_display, self->iconify,
196                   ob_rr_theme->button_size, ob_rr_theme->button_size);
197     XResizeWindow(ob_display, self->icon,
198                   ob_rr_theme->button_size + 2, ob_rr_theme->button_size + 2);
199     XResizeWindow(ob_display, self->close,
200                   ob_rr_theme->button_size, ob_rr_theme->button_size);
201     XResizeWindow(ob_display, self->desk,
202                   ob_rr_theme->button_size, ob_rr_theme->button_size);
203     XResizeWindow(ob_display, self->shade,
204                   ob_rr_theme->button_size, ob_rr_theme->button_size);
205     XResizeWindow(ob_display, self->tltresize,
206                   ob_rr_theme->grip_width, ob_rr_theme->paddingy + 1);
207     XResizeWindow(ob_display, self->trtresize,
208                   ob_rr_theme->grip_width, ob_rr_theme->paddingy + 1);
209     XResizeWindow(ob_display, self->tllresize,
210                   ob_rr_theme->paddingx + 1, ob_rr_theme->title_height);
211     XResizeWindow(ob_display, self->trrresize,
212                   ob_rr_theme->paddingx + 1, ob_rr_theme->title_height);
213
214     /* set up the dynamic appearances */
215     self->a_unfocused_title = RrAppearanceCopy(ob_rr_theme->a_unfocused_title);
216     self->a_focused_title = RrAppearanceCopy(ob_rr_theme->a_focused_title);
217     self->a_unfocused_label = RrAppearanceCopy(ob_rr_theme->a_unfocused_label);
218     self->a_focused_label = RrAppearanceCopy(ob_rr_theme->a_focused_label);
219     self->a_unfocused_handle =
220         RrAppearanceCopy(ob_rr_theme->a_unfocused_handle);
221     self->a_focused_handle = RrAppearanceCopy(ob_rr_theme->a_focused_handle);
222     self->a_icon = RrAppearanceCopy(ob_rr_theme->a_icon);
223 }
224
225 static void free_theme_statics(ObFrame *self)
226 {
227     RrAppearanceFree(self->a_unfocused_title); 
228     RrAppearanceFree(self->a_focused_title);
229     RrAppearanceFree(self->a_unfocused_label);
230     RrAppearanceFree(self->a_focused_label);
231     RrAppearanceFree(self->a_unfocused_handle);
232     RrAppearanceFree(self->a_focused_handle);
233     RrAppearanceFree(self->a_icon);
234 }
235
236 void frame_free(ObFrame *self)
237 {
238     free_theme_statics(self);
239
240     XDestroyWindow(ob_display, self->window);
241     if (self->colormap)
242         XFreeColormap(ob_display, self->colormap);
243
244     g_free(self);
245 }
246
247 void frame_show(ObFrame *self)
248 {
249     if (!self->visible) {
250         self->visible = TRUE;
251         XMapWindow(ob_display, self->client->window);
252         XMapWindow(ob_display, self->window);
253     }
254 }
255
256 void frame_hide(ObFrame *self)
257 {
258     if (self->visible) {
259         self->visible = FALSE;
260         if (!frame_iconify_animating(self))
261             XUnmapWindow(ob_display, self->window);
262         /* we unmap the client itself so that we can get MapRequest
263            events, and because the ICCCM tells us to! */
264         XUnmapWindow(ob_display, self->client->window);
265         self->client->ignore_unmaps += 1;
266     }
267 }
268
269 void frame_adjust_theme(ObFrame *self)
270 {
271     free_theme_statics(self);
272     set_theme_statics(self);
273 }
274
275 void frame_adjust_shape(ObFrame *self)
276 {
277 #ifdef SHAPE
278     gint num;
279     XRectangle xrect[2];
280
281     if (!self->client->shaped) {
282         /* clear the shape on the frame window */
283         XShapeCombineMask(ob_display, self->window, ShapeBounding,
284                           self->size.left,
285                           self->size.top,
286                           None, ShapeSet);
287     } else {
288         /* make the frame's shape match the clients */
289         XShapeCombineShape(ob_display, self->window, ShapeBounding,
290                            self->size.left,
291                            self->size.top,
292                            self->client->window,
293                            ShapeBounding, ShapeSet);
294
295         num = 0;
296         if (self->decorations & OB_FRAME_DECOR_TITLEBAR) {
297             xrect[0].x = 0;
298             xrect[0].y = 0;
299             xrect[0].width = self->area.width;
300             xrect[0].height = ob_rr_theme->title_height +
301                 self->bwidth + self->rbwidth;
302             ++num;
303         }
304
305         if (self->decorations & OB_FRAME_DECOR_HANDLE &&
306             ob_rr_theme->handle_height > 0)
307         {
308             xrect[1].x = 0;
309             xrect[1].y = FRAME_HANDLE_Y(self);
310             xrect[1].width = self->area.width;
311             xrect[1].height = ob_rr_theme->handle_height +
312                 self->bwidth * 2;
313             ++num;
314         }
315
316         XShapeCombineRectangles(ob_display, self->window,
317                                 ShapeBounding, 0, 0, xrect, num,
318                                 ShapeUnion, Unsorted);
319     }
320 #endif
321 }
322
323 void frame_adjust_area(ObFrame *self, gboolean moved,
324                        gboolean resized, gboolean fake)
325 {
326     Strut oldsize;
327
328     oldsize = self->size;
329
330     if (resized) {
331         /* do this before changing the frame's status like max_horz max_vert */
332         frame_adjust_cursors(self);
333
334         self->functions = self->client->functions;
335         self->decorations = self->client->decorations;
336         self->max_horz = self->client->max_horz;
337         self->max_vert = self->client->max_vert;
338
339         if (self->decorations & OB_FRAME_DECOR_BORDER) {
340             self->bwidth = ob_rr_theme->fbwidth;
341             self->cbwidth_l = self->cbwidth_r = ob_rr_theme->cbwidthx;
342             self->cbwidth_t = self->cbwidth_b = ob_rr_theme->cbwidthy;
343         } else {
344             self->bwidth = self->cbwidth_l = self->cbwidth_t =
345                 self->cbwidth_r = self->cbwidth_b = 0;
346         }
347         self->rbwidth = self->bwidth;
348
349         if (self->max_horz) {
350             self->cbwidth_l = self->cbwidth_r = 0;
351             self->width = self->client->area.width - self->bwidth * 2;
352             if (self->max_vert)
353                 self->cbwidth_b = 0;
354         } else
355             self->width = self->client->area.width +
356                 self->cbwidth_l + self->cbwidth_r;
357
358         /* some elements are sized based of the width, so don't let them have
359            negative values */
360         self->width = MAX(self->width,
361                           (ob_rr_theme->grip_width + self->bwidth) * 2 + 1);
362
363         STRUT_SET(self->size,
364                   self->cbwidth_l + (!self->max_horz ? self->bwidth : 0),
365                   self->cbwidth_t + self->bwidth,
366                   self->cbwidth_r + (!self->max_horz ? self->bwidth : 0),
367                   self->cbwidth_b + (!self->max_horz || !self->max_vert ? self->bwidth : 0));
368
369         if (self->decorations & OB_FRAME_DECOR_TITLEBAR)
370             self->size.top += ob_rr_theme->title_height + self->rbwidth;
371         if (self->decorations & OB_FRAME_DECOR_HANDLE &&
372             ob_rr_theme->handle_height > 0)
373         {
374             self->size.bottom += ob_rr_theme->handle_height + self->bwidth;
375         }
376   
377         /* position/size and map/unmap all the windows */
378
379         if (!fake) {
380             if (self->cbwidth_l) {
381                 XMoveResizeWindow(ob_display, self->innerleft,
382                                   self->size.left - self->cbwidth_l,
383                                   self->size.top,
384                                   self->cbwidth_l, self->client->area.height);
385
386                 XMapWindow(ob_display, self->innerleft);
387             } else
388                 XUnmapWindow(ob_display, self->innerleft);
389
390             if (self->cbwidth_r) {
391                 XMoveResizeWindow(ob_display, self->innerright,
392                                   self->size.left + self->client->area.width,
393                                   self->size.top,
394                                   self->cbwidth_r, self->client->area.height);
395
396                 XMapWindow(ob_display, self->innerright);
397             } else
398                 XUnmapWindow(ob_display, self->innerright);
399
400             if (self->cbwidth_t) {
401                 XMoveResizeWindow(ob_display, self->innertop,
402                                   self->size.left - self->cbwidth_l,
403                                   self->size.top - self->cbwidth_t,
404                                   self->client->area.width +
405                                   self->cbwidth_l + self->cbwidth_r,
406                                   self->cbwidth_t);
407
408                 XMapWindow(ob_display, self->innertop);
409             } else
410                 XUnmapWindow(ob_display, self->innertop);
411
412             if (self->cbwidth_b) {
413                 XMoveResizeWindow(ob_display, self->innerbottom,
414                                   self->size.left - self->cbwidth_l,
415                                   self->size.top + self->client->area.height,
416                                   self->client->area.width +
417                                   self->cbwidth_l + self->cbwidth_r,
418                                   self->cbwidth_b);
419
420                 XMapWindow(ob_display, self->innerbottom);
421             } else
422                 XUnmapWindow(ob_display, self->innerbottom);
423
424             if (self->bwidth) {
425                 gint titlesides;
426
427                 /* height of titleleft and titleright */
428                 titlesides = (!self->max_horz ?
429                               ob_rr_theme->grip_width :
430                               self->size.top - self->bwidth);
431
432                 XMoveResizeWindow(ob_display, self->titletop,
433                                   ob_rr_theme->grip_width + self->bwidth, 0,
434                                   /* width + bwidth*2 - bwidth*2 - grips*2 */
435                                   self->width - ob_rr_theme->grip_width * 2,
436                                   self->bwidth);
437                 XMoveResizeWindow(ob_display, self->titletopleft,
438                                   0, 0,
439                                   ob_rr_theme->grip_width + self->bwidth,
440                                   self->bwidth);
441                 XMoveResizeWindow(ob_display, self->titletopright,
442                                   self->client->area.width +
443                                   self->size.left + self->size.right -
444                                   ob_rr_theme->grip_width - self->bwidth,
445                                   0,
446                                   ob_rr_theme->grip_width + self->bwidth,
447                                   self->bwidth);
448
449                 if (titlesides > 0) {
450                     XMoveResizeWindow(ob_display, self->titleleft,
451                                       0, self->bwidth,
452                                       self->bwidth,
453                                       titlesides);
454                     XMoveResizeWindow(ob_display, self->titleright,
455                                       self->client->area.width +
456                                       self->size.left + self->size.right -
457                                       self->bwidth,
458                                       self->bwidth,
459                                       self->bwidth,
460                                       titlesides);
461
462                     XMapWindow(ob_display, self->titleleft);
463                     XMapWindow(ob_display, self->titleright);
464                 } else {
465                     XUnmapWindow(ob_display, self->titleleft);
466                     XUnmapWindow(ob_display, self->titleright);
467                 }
468
469                 XMapWindow(ob_display, self->titletop);
470                 XMapWindow(ob_display, self->titletopleft);
471                 XMapWindow(ob_display, self->titletopright);
472
473                 if (self->decorations & OB_FRAME_DECOR_TITLEBAR &&
474                     self->rbwidth)
475                 {
476                     XMoveResizeWindow(ob_display, self->titlebottom,
477                                       self->bwidth,
478                                       ob_rr_theme->title_height + self->bwidth,
479                                       self->width,
480                                       self->rbwidth);
481
482                     XMapWindow(ob_display, self->titlebottom);
483                 } else
484                     XUnmapWindow(ob_display, self->titlebottom);
485             } else {
486                 XUnmapWindow(ob_display, self->titlebottom);
487
488                 XUnmapWindow(ob_display, self->titletop);
489                 XUnmapWindow(ob_display, self->titletopleft);
490                 XUnmapWindow(ob_display, self->titletopright);
491                 XUnmapWindow(ob_display, self->titleleft);
492                 XUnmapWindow(ob_display, self->titleright);
493             }
494
495             if (self->decorations & OB_FRAME_DECOR_TITLEBAR) {
496                 XMoveResizeWindow(ob_display, self->title,
497                                   self->bwidth, self->bwidth,
498                                   self->width, ob_rr_theme->title_height);
499
500                 XMapWindow(ob_display, self->title);
501
502                 if (self->decorations & OB_FRAME_DECOR_GRIPS) {
503                     XMoveResizeWindow(ob_display, self->topresize,
504                                       ob_rr_theme->grip_width,
505                                       0,
506                                       self->width - ob_rr_theme->grip_width *2,
507                                       ob_rr_theme->paddingy + 1);
508
509                     XMoveWindow(ob_display, self->tltresize, 0, 0);
510                     XMoveWindow(ob_display, self->tllresize, 0, 0);
511                     XMoveWindow(ob_display, self->trtresize,
512                                 self->width - ob_rr_theme->grip_width, 0);
513                     XMoveWindow(ob_display, self->trrresize,
514                                 self->width - ob_rr_theme->paddingx - 1, 0);
515
516                     XMapWindow(ob_display, self->topresize);
517                     XMapWindow(ob_display, self->tltresize);
518                     XMapWindow(ob_display, self->tllresize);
519                     XMapWindow(ob_display, self->trtresize);
520                     XMapWindow(ob_display, self->trrresize);
521                 } else {
522                     XUnmapWindow(ob_display, self->topresize);
523                     XUnmapWindow(ob_display, self->tltresize);
524                     XUnmapWindow(ob_display, self->tllresize);
525                     XUnmapWindow(ob_display, self->trtresize);
526                     XUnmapWindow(ob_display, self->trrresize);
527                 }
528             } else
529                 XUnmapWindow(ob_display, self->title);
530         }
531
532         if ((self->decorations & OB_FRAME_DECOR_TITLEBAR))
533             /* layout the title bar elements */
534             layout_title(self);
535
536         if (!fake) {
537             if (self->bwidth && self->size.bottom) {
538                 XMoveResizeWindow(ob_display, self->handlebottom,
539                                   ob_rr_theme->grip_width +
540                                   self->bwidth * 2,
541                                   self->size.top + self->client->area.height +
542                                   self->size.bottom - self->bwidth,
543                                   self->width - (ob_rr_theme->grip_width +
544                                                  self->bwidth) * 2,
545                                   self->bwidth);
546
547                 XMoveResizeWindow(ob_display, self->lgripleft,
548                                   0,
549                                   self->size.top + self->client->area.height +
550                                   self->size.bottom -
551                                   (!self->max_horz ?
552                                    ob_rr_theme->grip_width :
553                                    self->size.bottom - self->cbwidth_b),
554                                   self->bwidth,
555                                   (!self->max_horz ?
556                                    ob_rr_theme->grip_width :
557                                    self->size.bottom - self->cbwidth_b));
558                 XMoveResizeWindow(ob_display, self->rgripright,
559                                   self->size.left + self->client->area.width +
560                                   self->size.right - self->bwidth,
561                                   self->size.top + self->client->area.height +
562                                   self->size.bottom -
563                                   (!self->max_horz ?
564                                    ob_rr_theme->grip_width :
565                                    self->size.bottom - self->cbwidth_b),
566                                   self->bwidth,
567                                   (!self->max_horz ?
568                                    ob_rr_theme->grip_width :
569                                    self->size.bottom - self->cbwidth_b));
570
571                 XMoveResizeWindow(ob_display, self->lgripbottom,
572                                   self->bwidth,
573                                   self->size.top + self->client->area.height +
574                                   self->size.bottom - self->bwidth,
575                                   ob_rr_theme->grip_width + self->bwidth,
576                                   self->bwidth);
577                 XMoveResizeWindow(ob_display, self->rgripbottom,
578                                   self->size.left + self->client->area.width +
579                                   self->size.right - self->bwidth * 2 -
580                                   ob_rr_theme->grip_width,
581                                   self->size.top + self->client->area.height +
582                                   self->size.bottom - self->bwidth,
583                                   ob_rr_theme->grip_width + self->bwidth,
584                                   self->bwidth);
585
586                 XMapWindow(ob_display, self->handlebottom);
587                 XMapWindow(ob_display, self->lgripleft);
588                 XMapWindow(ob_display, self->rgripright);
589                 XMapWindow(ob_display, self->lgripbottom);
590                 XMapWindow(ob_display, self->rgripbottom);
591
592                 if (self->decorations & OB_FRAME_DECOR_HANDLE &&
593                     ob_rr_theme->handle_height > 0)
594                 {
595                     XMoveResizeWindow(ob_display, self->handletop,
596                                       ob_rr_theme->grip_width +
597                                       self->bwidth * 2,
598                                       FRAME_HANDLE_Y(self),
599                                       self->width - (ob_rr_theme->grip_width +
600                                                      self->bwidth) * 2,
601                                       self->bwidth);
602                     XMapWindow(ob_display, self->handletop);
603
604                     if (self->decorations & OB_FRAME_DECOR_GRIPS) {
605                         XMoveResizeWindow(ob_display, self->handleleft,
606                                           ob_rr_theme->grip_width,
607                                           0,
608                                           self->bwidth,
609                                           ob_rr_theme->handle_height);
610                         XMoveResizeWindow(ob_display, self->handleright,
611                                           self->width -
612                                           ob_rr_theme->grip_width -
613                                           self->bwidth,
614                                           0,
615                                           self->bwidth,
616                                           ob_rr_theme->handle_height);
617
618                         XMoveResizeWindow(ob_display, self->lgriptop,
619                                           self->bwidth,
620                                           FRAME_HANDLE_Y(self),
621                                           ob_rr_theme->grip_width +
622                                           self->bwidth,
623                                           self->bwidth);
624                         XMoveResizeWindow(ob_display, self->rgriptop,
625                                           self->size.left +
626                                           self->client->area.width +
627                                           self->size.right - self->bwidth * 2 -
628                                           ob_rr_theme->grip_width,
629                                           FRAME_HANDLE_Y(self),
630                                           ob_rr_theme->grip_width +
631                                           self->bwidth,
632                                           self->bwidth);
633
634                         XMapWindow(ob_display, self->handleleft);
635                         XMapWindow(ob_display, self->handleright);
636                         XMapWindow(ob_display, self->lgriptop);
637                         XMapWindow(ob_display, self->rgriptop);
638                     } else {
639                         XUnmapWindow(ob_display, self->handleleft);
640                         XUnmapWindow(ob_display, self->handleright);
641                         XUnmapWindow(ob_display, self->lgriptop);
642                         XUnmapWindow(ob_display, self->rgriptop);
643                     }
644                 } else {
645                     XUnmapWindow(ob_display, self->handleleft);
646                     XUnmapWindow(ob_display, self->handleright);
647                     XUnmapWindow(ob_display, self->lgriptop);
648                     XUnmapWindow(ob_display, self->rgriptop);
649
650                     XUnmapWindow(ob_display, self->handletop);
651                 }
652             } else {
653                 XUnmapWindow(ob_display, self->handleleft);
654                 XUnmapWindow(ob_display, self->handleright);
655                 XUnmapWindow(ob_display, self->lgriptop);
656                 XUnmapWindow(ob_display, self->rgriptop);
657
658                 XUnmapWindow(ob_display, self->handletop);
659
660                 XUnmapWindow(ob_display, self->handlebottom);
661                 XUnmapWindow(ob_display, self->lgripleft);
662                 XUnmapWindow(ob_display, self->rgripright);
663                 XUnmapWindow(ob_display, self->lgripbottom);
664                 XUnmapWindow(ob_display, self->rgripbottom);
665             }
666
667             if (self->decorations & OB_FRAME_DECOR_HANDLE &&
668                 ob_rr_theme->handle_height > 0)
669             {
670                 XMoveResizeWindow(ob_display, self->handle,
671                                   self->bwidth,
672                                   FRAME_HANDLE_Y(self) + self->bwidth,
673                                   self->width, ob_rr_theme->handle_height);
674                 XMapWindow(ob_display, self->handle);
675
676                 if (self->decorations & OB_FRAME_DECOR_GRIPS) {
677                     XMoveResizeWindow(ob_display, self->lgrip,
678                                       0, 0,
679                                       ob_rr_theme->grip_width,
680                                       ob_rr_theme->handle_height);
681                     XMoveResizeWindow(ob_display, self->rgrip,
682                                       self->width - ob_rr_theme->grip_width,
683                                       0,
684                                       ob_rr_theme->grip_width,
685                                       ob_rr_theme->handle_height);
686
687                     XMapWindow(ob_display, self->lgrip);
688                     XMapWindow(ob_display, self->rgrip);
689                 } else {
690                     XUnmapWindow(ob_display, self->lgrip);
691                     XUnmapWindow(ob_display, self->rgrip);
692                 }
693             } else {
694                 XUnmapWindow(ob_display, self->lgrip);
695                 XUnmapWindow(ob_display, self->rgrip);
696
697                 XUnmapWindow(ob_display, self->handle);
698             }
699
700             if (self->bwidth && !self->max_horz) {
701                 XMoveResizeWindow(ob_display, self->left,
702                                   0,
703                                   self->bwidth + ob_rr_theme->grip_width,
704                                   self->bwidth,
705                                   self->client->area.height +
706                                   self->size.top + self->size.bottom -
707                                   ob_rr_theme->grip_width * 2);
708
709                 XMapWindow(ob_display, self->left);
710             } else
711                 XUnmapWindow(ob_display, self->left);
712
713             if (self->bwidth && !self->max_horz) {
714                 XMoveResizeWindow(ob_display, self->right,
715                                   self->client->area.width +
716                                   self->cbwidth_l + self->cbwidth_r + self->bwidth,
717                                   self->bwidth + ob_rr_theme->grip_width,
718                                   self->bwidth,
719                                   self->client->area.height +
720                                   self->size.top + self->size.bottom -
721                                   ob_rr_theme->grip_width * 2);
722
723                 XMapWindow(ob_display, self->right);
724             } else
725                 XUnmapWindow(ob_display, self->right);
726
727             XMoveResizeWindow(ob_display, self->backback,
728                               self->size.left, self->size.top,
729                               self->client->area.width,
730                               self->client->area.height);
731         }
732     }
733
734     /* shading can change without being moved or resized */
735     RECT_SET_SIZE(self->area,
736                   self->client->area.width +
737                   self->size.left + self->size.right,
738                   (self->client->shaded ?
739                    ob_rr_theme->title_height + self->bwidth * 2:
740                    self->client->area.height +
741                    self->size.top + self->size.bottom));
742
743     if ((moved || resized) && !fake) {
744         /* find the new coordinates, done after setting the frame.size, for
745            frame_client_gravity. */
746         self->area.x = self->client->area.x;
747         self->area.y = self->client->area.y;
748         frame_client_gravity(self, &self->area.x, &self->area.y,
749                              self->client->area.width,
750                              self->client->area.height);
751     }
752
753     if (!fake) {
754         if (!frame_iconify_animating(self))
755             /* move and resize the top level frame.
756                shading can change without being moved or resized.
757                
758                but don't do this during an iconify animation. it will be
759                reflected afterwards.
760             */
761             XMoveResizeWindow(ob_display, self->window,
762                               self->area.x,
763                               self->area.y,
764                               self->area.width,
765                               self->area.height);
766
767         /* when the client has StaticGravity, it likes to move around.
768            also this correctly positions the client when it maps.
769            this also needs to be run when the frame's decorations sizes change!
770         */
771         XMoveWindow(ob_display, self->client->window,
772                     self->size.left, self->size.top);
773
774         if (resized) {
775             framerender_frame(self);
776             frame_adjust_shape(self);
777         }
778
779         if (!STRUT_EQUAL(self->size, oldsize)) {
780             gulong vals[4];
781             vals[0] = self->size.left;
782             vals[1] = self->size.right;
783             vals[2] = self->size.top;
784             vals[3] = self->size.bottom;
785             PROP_SETA32(self->client->window, net_frame_extents,
786                         cardinal, vals, 4);
787             PROP_SETA32(self->client->window, kde_net_wm_frame_strut,
788                         cardinal, vals, 4);
789         }
790
791         /* if this occurs while we are focus cycling, the indicator needs to
792            match the changes */
793         if (focus_cycle_target == self->client)
794             focus_cycle_draw_indicator(self->client);
795     }
796     if (resized && (self->decorations & OB_FRAME_DECOR_TITLEBAR))
797         XResizeWindow(ob_display, self->label, self->label_width,
798                       ob_rr_theme->label_height);
799
800 }
801
802 static void frame_adjust_cursors(ObFrame *self)
803 {
804     if ((self->functions & OB_CLIENT_FUNC_RESIZE) !=
805         (self->client->functions & OB_CLIENT_FUNC_RESIZE) ||
806         self->max_horz != self->client->max_horz ||
807         self->max_vert != self->client->max_vert)
808     {
809         gboolean r = (self->client->functions & OB_CLIENT_FUNC_RESIZE) &&
810             !(self->client->max_horz && self->client->max_vert);
811         gboolean topbot = !self->client->max_vert;
812         XSetWindowAttributes a;
813
814         /* these ones turn off when max vert */
815         a.cursor = ob_cursor(r && topbot ? OB_CURSOR_NORTH : OB_CURSOR_NONE);
816         XChangeWindowAttributes(ob_display, self->topresize, CWCursor, &a);
817         XChangeWindowAttributes(ob_display, self->titletop, CWCursor, &a);
818         a.cursor = ob_cursor(r && topbot ? OB_CURSOR_SOUTH : OB_CURSOR_NONE);
819         XChangeWindowAttributes(ob_display, self->handle, CWCursor, &a);
820         XChangeWindowAttributes(ob_display, self->handletop, CWCursor, &a);
821         XChangeWindowAttributes(ob_display, self->handlebottom, CWCursor, &a);
822         XChangeWindowAttributes(ob_display, self->innerbottom, CWCursor, &a);
823
824         /* these ones don't */
825         a.cursor = ob_cursor(r ? OB_CURSOR_NORTHWEST : OB_CURSOR_NONE);
826         XChangeWindowAttributes(ob_display, self->tltresize, CWCursor, &a);
827         XChangeWindowAttributes(ob_display, self->tllresize, CWCursor, &a);
828         XChangeWindowAttributes(ob_display, self->titletopleft, CWCursor, &a);
829         XChangeWindowAttributes(ob_display, self->titleleft, CWCursor, &a);
830         a.cursor = ob_cursor(r ? OB_CURSOR_NORTHEAST : OB_CURSOR_NONE);
831         XChangeWindowAttributes(ob_display, self->trtresize, CWCursor, &a);
832         XChangeWindowAttributes(ob_display, self->trrresize, CWCursor, &a);
833         XChangeWindowAttributes(ob_display, self->titletopright, CWCursor, &a);
834         XChangeWindowAttributes(ob_display, self->titleright, CWCursor, &a);
835         a.cursor = ob_cursor(r ? OB_CURSOR_WEST : OB_CURSOR_NONE);
836         XChangeWindowAttributes(ob_display, self->left, CWCursor, &a);
837         XChangeWindowAttributes(ob_display, self->innerleft, CWCursor, &a);
838         a.cursor = ob_cursor(r ? OB_CURSOR_EAST : OB_CURSOR_NONE);
839         XChangeWindowAttributes(ob_display, self->right, CWCursor, &a);
840         XChangeWindowAttributes(ob_display, self->innerright, CWCursor, &a);
841         a.cursor = ob_cursor(r ? OB_CURSOR_SOUTHWEST : OB_CURSOR_NONE);
842         XChangeWindowAttributes(ob_display, self->lgrip, CWCursor, &a);
843         XChangeWindowAttributes(ob_display, self->handleleft, CWCursor, &a);
844         XChangeWindowAttributes(ob_display, self->lgripleft, CWCursor, &a);
845         XChangeWindowAttributes(ob_display, self->lgriptop, CWCursor, &a);
846         XChangeWindowAttributes(ob_display, self->lgripbottom, CWCursor, &a);
847         a.cursor = ob_cursor(r ? OB_CURSOR_SOUTHEAST : OB_CURSOR_NONE);
848         XChangeWindowAttributes(ob_display, self->rgrip, CWCursor, &a);
849         XChangeWindowAttributes(ob_display, self->handleright, CWCursor, &a);
850         XChangeWindowAttributes(ob_display, self->rgripright, CWCursor, &a);
851         XChangeWindowAttributes(ob_display, self->rgriptop, CWCursor, &a);
852         XChangeWindowAttributes(ob_display, self->rgripbottom, CWCursor, &a);
853     }
854 }
855
856 void frame_adjust_client_area(ObFrame *self)
857 {
858     /* adjust the window which is there to prevent flashing on unmap */
859     XMoveResizeWindow(ob_display, self->backfront, 0, 0,
860                       self->client->area.width,
861                       self->client->area.height);
862 }
863
864 void frame_adjust_state(ObFrame *self)
865 {
866     framerender_frame(self);
867 }
868
869 void frame_adjust_focus(ObFrame *self, gboolean hilite)
870 {
871     self->focused = hilite;
872     framerender_frame(self);
873     XFlush(ob_display);
874 }
875
876 void frame_adjust_title(ObFrame *self)
877 {
878     framerender_frame(self);
879 }
880
881 void frame_adjust_icon(ObFrame *self)
882 {
883     framerender_frame(self);
884 }
885
886 void frame_grab_client(ObFrame *self)
887 {
888     /* DO NOT map the client window here. we used to do that, but it is bogus.
889        we need to set up the client's dimensions and everything before we
890        send a mapnotify or we create race conditions.
891     */
892
893     /* reparent the client to the frame */
894     XReparentWindow(ob_display, self->client->window, self->window, 0, 0);
895
896     /*
897       When reparenting the client window, it is usually not mapped yet, since
898       this occurs from a MapRequest. However, in the case where Openbox is
899       starting up, the window is already mapped, so we'll see an unmap event
900       for it.
901     */
902     if (ob_state() == OB_STATE_STARTING)
903         ++self->client->ignore_unmaps;
904
905     /* select the event mask on the client's parent (to receive config/map
906        req's) the ButtonPress is to catch clicks on the client border */
907     XSelectInput(ob_display, self->window, FRAME_EVENTMASK);
908
909     /* set all the windows for the frame in the window_map */
910     g_hash_table_insert(window_map, &self->window, self->client);
911     g_hash_table_insert(window_map, &self->backback, self->client);
912     g_hash_table_insert(window_map, &self->backfront, self->client);
913     g_hash_table_insert(window_map, &self->innerleft, self->client);
914     g_hash_table_insert(window_map, &self->innertop, self->client);
915     g_hash_table_insert(window_map, &self->innerright, self->client);
916     g_hash_table_insert(window_map, &self->innerbottom, self->client);
917     g_hash_table_insert(window_map, &self->title, self->client);
918     g_hash_table_insert(window_map, &self->label, self->client);
919     g_hash_table_insert(window_map, &self->max, self->client);
920     g_hash_table_insert(window_map, &self->close, self->client);
921     g_hash_table_insert(window_map, &self->desk, self->client);
922     g_hash_table_insert(window_map, &self->shade, self->client);
923     g_hash_table_insert(window_map, &self->icon, self->client);
924     g_hash_table_insert(window_map, &self->iconify, self->client);
925     g_hash_table_insert(window_map, &self->handle, self->client);
926     g_hash_table_insert(window_map, &self->lgrip, self->client);
927     g_hash_table_insert(window_map, &self->rgrip, self->client);
928     g_hash_table_insert(window_map, &self->topresize, self->client);
929     g_hash_table_insert(window_map, &self->tltresize, self->client);
930     g_hash_table_insert(window_map, &self->tllresize, self->client);
931     g_hash_table_insert(window_map, &self->trtresize, self->client);
932     g_hash_table_insert(window_map, &self->trrresize, self->client);
933     g_hash_table_insert(window_map, &self->left, self->client);
934     g_hash_table_insert(window_map, &self->right, self->client);
935     g_hash_table_insert(window_map, &self->titleleft, self->client);
936     g_hash_table_insert(window_map, &self->titletop, self->client);
937     g_hash_table_insert(window_map, &self->titletopleft, self->client);
938     g_hash_table_insert(window_map, &self->titletopright, self->client);
939     g_hash_table_insert(window_map, &self->titleright, self->client);
940     g_hash_table_insert(window_map, &self->titlebottom, self->client);
941     g_hash_table_insert(window_map, &self->handleleft, self->client);
942     g_hash_table_insert(window_map, &self->handletop, self->client);
943     g_hash_table_insert(window_map, &self->handleright, self->client);
944     g_hash_table_insert(window_map, &self->handlebottom, self->client);
945     g_hash_table_insert(window_map, &self->lgripleft, self->client);
946     g_hash_table_insert(window_map, &self->lgriptop, self->client);
947     g_hash_table_insert(window_map, &self->lgripbottom, self->client);
948     g_hash_table_insert(window_map, &self->rgripright, self->client);
949     g_hash_table_insert(window_map, &self->rgriptop, self->client);
950     g_hash_table_insert(window_map, &self->rgripbottom, self->client);
951 }
952
953 void frame_release_client(ObFrame *self)
954 {
955     XEvent ev;
956     gboolean reparent = TRUE;
957
958     /* if there was any animation going on, kill it */
959     ob_main_loop_timeout_remove_data(ob_main_loop, frame_animate_iconify,
960                                      self, FALSE);
961
962     /* check if the app has already reparented its window away */
963     while (XCheckTypedWindowEvent(ob_display, self->client->window,
964                                   ReparentNotify, &ev))
965     {
966         /* This check makes sure we don't catch our own reparent action to
967            our frame window. This doesn't count as the app reparenting itself
968            away of course.
969
970            Reparent events that are generated by us are just discarded here.
971            They are of no consequence to us anyhow.
972         */
973         if (ev.xreparent.parent != self->window) {
974             reparent = FALSE;
975             XPutBackEvent(ob_display, &ev);
976             break;
977         }
978     }
979
980     if (reparent) {
981         /* according to the ICCCM - if the client doesn't reparent itself,
982            then we will reparent the window to root for them */
983         XReparentWindow(ob_display, self->client->window,
984                         RootWindow(ob_display, ob_screen),
985                         self->client->area.x,
986                         self->client->area.y);
987     }
988
989     /* remove all the windows for the frame from the window_map */
990     g_hash_table_remove(window_map, &self->window);
991     g_hash_table_remove(window_map, &self->backback);
992     g_hash_table_remove(window_map, &self->backfront);
993     g_hash_table_remove(window_map, &self->innerleft);
994     g_hash_table_remove(window_map, &self->innertop);
995     g_hash_table_remove(window_map, &self->innerright);
996     g_hash_table_remove(window_map, &self->innerbottom);
997     g_hash_table_remove(window_map, &self->title);
998     g_hash_table_remove(window_map, &self->label);
999     g_hash_table_remove(window_map, &self->max);
1000     g_hash_table_remove(window_map, &self->close);
1001     g_hash_table_remove(window_map, &self->desk);
1002     g_hash_table_remove(window_map, &self->shade);
1003     g_hash_table_remove(window_map, &self->icon);
1004     g_hash_table_remove(window_map, &self->iconify);
1005     g_hash_table_remove(window_map, &self->handle);
1006     g_hash_table_remove(window_map, &self->lgrip);
1007     g_hash_table_remove(window_map, &self->rgrip);
1008     g_hash_table_remove(window_map, &self->topresize);
1009     g_hash_table_remove(window_map, &self->tltresize);
1010     g_hash_table_remove(window_map, &self->tllresize);
1011     g_hash_table_remove(window_map, &self->trtresize);
1012     g_hash_table_remove(window_map, &self->trrresize);
1013     g_hash_table_remove(window_map, &self->left);
1014     g_hash_table_remove(window_map, &self->right);
1015     g_hash_table_remove(window_map, &self->titleleft);
1016     g_hash_table_remove(window_map, &self->titletop);
1017     g_hash_table_remove(window_map, &self->titletopleft);
1018     g_hash_table_remove(window_map, &self->titletopright);
1019     g_hash_table_remove(window_map, &self->titleright);
1020     g_hash_table_remove(window_map, &self->titlebottom);
1021     g_hash_table_remove(window_map, &self->handleleft);
1022     g_hash_table_remove(window_map, &self->handletop);
1023     g_hash_table_remove(window_map, &self->handleright);
1024     g_hash_table_remove(window_map, &self->handlebottom);
1025     g_hash_table_remove(window_map, &self->lgripleft);
1026     g_hash_table_remove(window_map, &self->lgriptop);
1027     g_hash_table_remove(window_map, &self->lgripbottom);
1028     g_hash_table_remove(window_map, &self->rgripright);
1029     g_hash_table_remove(window_map, &self->rgriptop);
1030     g_hash_table_remove(window_map, &self->rgripbottom);
1031
1032     ob_main_loop_timeout_remove_data(ob_main_loop, flash_timeout, self, TRUE);
1033 }
1034
1035 /* is there anything present between us and the label? */
1036 static gboolean is_button_present(ObFrame *self, const gchar *lc, gint dir) {
1037     for (; *lc != '\0' && lc >= config_title_layout; lc += dir) {
1038         if (*lc == ' ') continue; /* it was invalid */
1039         if (*lc == 'N' && self->decorations & OB_FRAME_DECOR_ICON)
1040             return TRUE;
1041         if (*lc == 'D' && self->decorations & OB_FRAME_DECOR_ALLDESKTOPS)
1042             return TRUE;
1043         if (*lc == 'S' && self->decorations & OB_FRAME_DECOR_SHADE)
1044             return TRUE;
1045         if (*lc == 'I' && self->decorations & OB_FRAME_DECOR_ICONIFY)
1046             return TRUE;
1047         if (*lc == 'M' && self->decorations & OB_FRAME_DECOR_MAXIMIZE)
1048             return TRUE;
1049         if (*lc == 'C' && self->decorations & OB_FRAME_DECOR_CLOSE)
1050             return TRUE;
1051         if (*lc == 'L') return FALSE;
1052     }
1053     return FALSE;
1054 }
1055
1056 static void layout_title(ObFrame *self)
1057 {
1058     gchar *lc;
1059     gint i;
1060
1061     const gint bwidth = ob_rr_theme->button_size + ob_rr_theme->paddingx + 1;
1062     /* position of the left most button */
1063     const gint left = ob_rr_theme->paddingx + 1;
1064     /* position of the right most button */
1065     const gint right = self->width;
1066
1067     /* turn them all off */
1068     self->icon_on = self->desk_on = self->shade_on = self->iconify_on =
1069         self->max_on = self->close_on = self->label_on = FALSE;
1070     self->label_width = self->width - (ob_rr_theme->paddingx + 1) * 2;
1071     self->leftmost = self->rightmost = OB_FRAME_CONTEXT_NONE;
1072
1073     /* figure out what's being show, find each element's position, and the
1074        width of the label
1075
1076        do the ones before the label, then after the label,
1077        i will be +1 the first time through when working to the left,
1078        and -1 the second time through when working to the right */
1079     for (i = 1; i >= -1; i-=2) {
1080         gint x;
1081         ObFrameContext *firstcon;
1082
1083         if (i > 0) {
1084             x = left;
1085             lc = config_title_layout;
1086             firstcon = &self->leftmost;
1087         } else {
1088             x = right;
1089             lc = config_title_layout + strlen(config_title_layout)-1;
1090             firstcon = &self->rightmost;
1091         }
1092
1093         /* stop at the end of the string (or the label, which calls break) */
1094         for (; *lc != '\0' && lc >= config_title_layout; lc+=i) {
1095             if (*lc == 'L') {
1096                 if (i > 0) {
1097                     self->label_on = TRUE;
1098                     self->label_x = x;
1099                 }
1100                 break; /* break the for loop, do other side of label */
1101             } else if (*lc == 'N') {
1102                 if (firstcon) *firstcon = OB_FRAME_CONTEXT_ICON;
1103                 if ((self->icon_on = is_button_present(self, lc, i))) {
1104                     /* icon is bigger than buttons */
1105                     self->label_width -= bwidth + 2;
1106                     if (i > 0) self->icon_x = x;
1107                     x += i * (bwidth + 2);
1108                     if (i < 0) self->icon_x = x;
1109                 }
1110             } else if (*lc == 'D') {
1111                 if (firstcon) *firstcon = OB_FRAME_CONTEXT_ALLDESKTOPS;
1112                 if ((self->desk_on = is_button_present(self, lc, i))) {
1113                     self->label_width -= bwidth;
1114                     if (i > 0) self->desk_x = x;
1115                     x += i * bwidth;
1116                     if (i < 0) self->desk_x = x;
1117                 }
1118             } else if (*lc == 'S') {
1119                 if (firstcon) *firstcon = OB_FRAME_CONTEXT_SHADE;
1120                 if ((self->shade_on = is_button_present(self, lc, i))) {
1121                     self->label_width -= bwidth;
1122                     if (i > 0) self->shade_x = x;
1123                     x += i * bwidth;
1124                     if (i < 0) self->shade_x = x;
1125                 }
1126             } else if (*lc == 'I') {
1127                 if (firstcon) *firstcon = OB_FRAME_CONTEXT_ICONIFY;
1128                 if ((self->iconify_on = is_button_present(self, lc, i))) {
1129                     self->label_width -= bwidth;
1130                     if (i > 0) self->iconify_x = x;
1131                     x += i * bwidth;
1132                     if (i < 0) self->iconify_x = x;
1133                 }
1134             } else if (*lc == 'M') {
1135                 if (firstcon) *firstcon = OB_FRAME_CONTEXT_MAXIMIZE;
1136                 if ((self->max_on = is_button_present(self, lc, i))) {
1137                     self->label_width -= bwidth;
1138                     if (i > 0) self->max_x = x;
1139                     x += i * bwidth;
1140                     if (i < 0) self->max_x = x;
1141                 }
1142             } else if (*lc == 'C') {
1143                 if (firstcon) *firstcon = OB_FRAME_CONTEXT_CLOSE;
1144                 if ((self->close_on = is_button_present(self, lc, i))) {
1145                     self->label_width -= bwidth;
1146                     if (i > 0) self->close_x = x;
1147                     x += i * bwidth;
1148                     if (i < 0) self->close_x = x;
1149                 }
1150             } else
1151                 continue; /* don't set firstcon */
1152             firstcon = NULL;
1153         }
1154     }
1155
1156     /* position and map the elements */
1157     if (self->icon_on) {
1158         XMapWindow(ob_display, self->icon);
1159         XMoveWindow(ob_display, self->icon, self->icon_x,
1160                     ob_rr_theme->paddingy);
1161     } else
1162         XUnmapWindow(ob_display, self->icon);
1163
1164     if (self->desk_on) {
1165         XMapWindow(ob_display, self->desk);
1166         XMoveWindow(ob_display, self->desk, self->desk_x,
1167                     ob_rr_theme->paddingy + 1);
1168     } else
1169         XUnmapWindow(ob_display, self->desk);
1170
1171     if (self->shade_on) {
1172         XMapWindow(ob_display, self->shade);
1173         XMoveWindow(ob_display, self->shade, self->shade_x,
1174                     ob_rr_theme->paddingy + 1);
1175     } else
1176         XUnmapWindow(ob_display, self->shade);
1177
1178     if (self->iconify_on) {
1179         XMapWindow(ob_display, self->iconify);
1180         XMoveWindow(ob_display, self->iconify, self->iconify_x,
1181                     ob_rr_theme->paddingy + 1);
1182     } else
1183         XUnmapWindow(ob_display, self->iconify);
1184
1185     if (self->max_on) {
1186         XMapWindow(ob_display, self->max);
1187         XMoveWindow(ob_display, self->max, self->max_x,
1188                     ob_rr_theme->paddingy + 1);
1189     } else
1190         XUnmapWindow(ob_display, self->max);
1191
1192     if (self->close_on) {
1193         XMapWindow(ob_display, self->close);
1194         XMoveWindow(ob_display, self->close, self->close_x,
1195                     ob_rr_theme->paddingy + 1);
1196     } else
1197         XUnmapWindow(ob_display, self->close);
1198
1199     if (self->label_on) {
1200         self->label_width = MAX(1, self->label_width); /* no lower than 1 */
1201         XMapWindow(ob_display, self->label);
1202         XMoveWindow(ob_display, self->label, self->label_x,
1203                     ob_rr_theme->paddingy);
1204     } else
1205         XUnmapWindow(ob_display, self->label);
1206 }
1207
1208 ObFrameContext frame_context_from_string(const gchar *name)
1209 {
1210     if (!g_ascii_strcasecmp("Desktop", name))
1211         return OB_FRAME_CONTEXT_DESKTOP;
1212     else if (!g_ascii_strcasecmp("Root", name))
1213         return OB_FRAME_CONTEXT_ROOT;
1214     else if (!g_ascii_strcasecmp("Client", name))
1215         return OB_FRAME_CONTEXT_CLIENT;
1216     else if (!g_ascii_strcasecmp("Titlebar", name))
1217         return OB_FRAME_CONTEXT_TITLEBAR;
1218     else if (!g_ascii_strcasecmp("Frame", name))
1219         return OB_FRAME_CONTEXT_FRAME;
1220     else if (!g_ascii_strcasecmp("TLCorner", name))
1221         return OB_FRAME_CONTEXT_TLCORNER;
1222     else if (!g_ascii_strcasecmp("TRCorner", name))
1223         return OB_FRAME_CONTEXT_TRCORNER;
1224     else if (!g_ascii_strcasecmp("BLCorner", name))
1225         return OB_FRAME_CONTEXT_BLCORNER;
1226     else if (!g_ascii_strcasecmp("BRCorner", name))
1227         return OB_FRAME_CONTEXT_BRCORNER;
1228     else if (!g_ascii_strcasecmp("Top", name))
1229         return OB_FRAME_CONTEXT_TOP;
1230     else if (!g_ascii_strcasecmp("Handle", name))
1231         return OB_FRAME_CONTEXT_BOTTOM;
1232     else if (!g_ascii_strcasecmp("Bottom", name))
1233         return OB_FRAME_CONTEXT_BOTTOM;
1234     else if (!g_ascii_strcasecmp("Left", name))
1235         return OB_FRAME_CONTEXT_LEFT;
1236     else if (!g_ascii_strcasecmp("Right", name))
1237         return OB_FRAME_CONTEXT_RIGHT;
1238     else if (!g_ascii_strcasecmp("Maximize", name))
1239         return OB_FRAME_CONTEXT_MAXIMIZE;
1240     else if (!g_ascii_strcasecmp("AllDesktops", name))
1241         return OB_FRAME_CONTEXT_ALLDESKTOPS;
1242     else if (!g_ascii_strcasecmp("Shade", name))
1243         return OB_FRAME_CONTEXT_SHADE;
1244     else if (!g_ascii_strcasecmp("Iconify", name))
1245         return OB_FRAME_CONTEXT_ICONIFY;
1246     else if (!g_ascii_strcasecmp("Icon", name))
1247         return OB_FRAME_CONTEXT_ICON;
1248     else if (!g_ascii_strcasecmp("Close", name))
1249         return OB_FRAME_CONTEXT_CLOSE;
1250     else if (!g_ascii_strcasecmp("MoveResize", name))
1251         return OB_FRAME_CONTEXT_MOVE_RESIZE;
1252     return OB_FRAME_CONTEXT_NONE;
1253 }
1254
1255 ObFrameContext frame_context(ObClient *client, Window win, gint x, gint y)
1256 {
1257     ObFrame *self;
1258
1259     if (moveresize_in_progress)
1260         return OB_FRAME_CONTEXT_MOVE_RESIZE;
1261
1262     if (win == RootWindow(ob_display, ob_screen))
1263         return OB_FRAME_CONTEXT_ROOT ;
1264     if (client == NULL) return OB_FRAME_CONTEXT_NONE;
1265     if (win == client->window) {
1266         /* conceptually, this is the desktop, as far as users are
1267            concerned */
1268         if (client->type == OB_CLIENT_TYPE_DESKTOP)
1269             return OB_FRAME_CONTEXT_DESKTOP;
1270         return OB_FRAME_CONTEXT_CLIENT;
1271     }
1272
1273     self = client->frame;
1274
1275     /* when the user clicks in the corners of the titlebar and the client
1276        is fully maximized, then treat it like they clicked in the
1277        button that is there */
1278     if (self->max_horz && self->max_vert &&
1279         (win == self->title || win == self->titletop ||
1280          win == self->titleleft || win == self->titletopleft ||
1281          win == self->titleright || win == self->titletopright))
1282     {
1283         /* get the mouse coords in reference to the whole frame */
1284         gint fx = x;
1285         gint fy = y;
1286
1287         /* these windows are down a border width from the top of the frame */
1288         if (win == self->title ||
1289             win == self->titleleft || win == self->titleright)
1290             fy += self->bwidth;
1291
1292         /* title is a border width in from the edge */
1293         if (win == self->title)
1294             fx += self->bwidth;
1295         /* titletop is a bit to the right */
1296         else if (win == self->titletop)
1297             fx += ob_rr_theme->grip_width + self->bwidth;
1298         /* titletopright is way to the right edge */
1299         else if (win == self->titletopright)
1300             fx += self->area.width - (ob_rr_theme->grip_width + self->bwidth);
1301         /* titleright is even more way to the right edge */
1302         else if (win == self->titleright)
1303             fx += self->area.width - self->bwidth;
1304
1305         /* figure out if we're over the area that should be considered a
1306            button */
1307         if (fy < self->bwidth + ob_rr_theme->paddingy + 1 +
1308             ob_rr_theme->button_size)
1309         {
1310             if (fx < (self->bwidth + ob_rr_theme->paddingx + 1 +
1311                       ob_rr_theme->button_size))
1312             {
1313                 if (self->leftmost != OB_FRAME_CONTEXT_NONE)
1314                     return self->leftmost;
1315             }
1316             else if (fx >= (self->area.width -
1317                             (self->bwidth + ob_rr_theme->paddingx + 1 +
1318                              ob_rr_theme->button_size)))
1319             {
1320                 if (self->rightmost != OB_FRAME_CONTEXT_NONE)
1321                     return self->rightmost;
1322             }
1323         }
1324
1325         /* there is no resizing maximized windows so make them the titlebar
1326            context */
1327         return OB_FRAME_CONTEXT_TITLEBAR;
1328     }
1329     else if (self->max_vert &&
1330              (win == self->titletop || win == self->topresize))
1331         /* can't resize vertically when max vert */
1332         return OB_FRAME_CONTEXT_TITLEBAR;
1333
1334     if (win == self->window)            return OB_FRAME_CONTEXT_FRAME;
1335     if (win == self->label)             return OB_FRAME_CONTEXT_TITLEBAR;
1336     if (win == self->handle)            return OB_FRAME_CONTEXT_BOTTOM;
1337     if (win == self->handletop)         return OB_FRAME_CONTEXT_BOTTOM;
1338     if (win == self->handlebottom)      return OB_FRAME_CONTEXT_BOTTOM;
1339     if (win == self->handleleft)        return OB_FRAME_CONTEXT_BLCORNER;
1340     if (win == self->lgrip)             return OB_FRAME_CONTEXT_BLCORNER;
1341     if (win == self->lgripleft)         return OB_FRAME_CONTEXT_BLCORNER;
1342     if (win == self->lgriptop)          return OB_FRAME_CONTEXT_BLCORNER;
1343     if (win == self->lgripbottom)       return OB_FRAME_CONTEXT_BLCORNER;
1344     if (win == self->handleright)       return OB_FRAME_CONTEXT_BRCORNER;
1345     if (win == self->rgrip)             return OB_FRAME_CONTEXT_BRCORNER;
1346     if (win == self->rgripright)        return OB_FRAME_CONTEXT_BLCORNER;
1347     if (win == self->rgriptop)          return OB_FRAME_CONTEXT_BLCORNER;
1348     if (win == self->rgripbottom)       return OB_FRAME_CONTEXT_BLCORNER;
1349     if (win == self->title)             return OB_FRAME_CONTEXT_TITLEBAR;
1350     if (win == self->titlebottom)       return OB_FRAME_CONTEXT_TITLEBAR;
1351     if (win == self->titleleft)         return OB_FRAME_CONTEXT_TLCORNER;
1352     if (win == self->titletopleft)      return OB_FRAME_CONTEXT_TLCORNER;
1353     if (win == self->titleright)        return OB_FRAME_CONTEXT_TRCORNER;
1354     if (win == self->titletopright)     return OB_FRAME_CONTEXT_TRCORNER;
1355     if (win == self->titletop)          return OB_FRAME_CONTEXT_TOP;
1356     if (win == self->topresize)         return OB_FRAME_CONTEXT_TOP;
1357     if (win == self->tltresize)         return OB_FRAME_CONTEXT_TLCORNER;
1358     if (win == self->tllresize)         return OB_FRAME_CONTEXT_TLCORNER;
1359     if (win == self->trtresize)         return OB_FRAME_CONTEXT_TRCORNER;
1360     if (win == self->trrresize)         return OB_FRAME_CONTEXT_TRCORNER;
1361     if (win == self->left)              return OB_FRAME_CONTEXT_LEFT;
1362     if (win == self->right)             return OB_FRAME_CONTEXT_RIGHT;
1363     if (win == self->innertop)          return OB_FRAME_CONTEXT_TITLEBAR;
1364     if (win == self->innerleft)         return OB_FRAME_CONTEXT_LEFT;
1365     if (win == self->innerbottom)       return OB_FRAME_CONTEXT_BOTTOM;
1366     if (win == self->innerright)        return OB_FRAME_CONTEXT_RIGHT;
1367     if (win == self->max)               return OB_FRAME_CONTEXT_MAXIMIZE;
1368     if (win == self->iconify)           return OB_FRAME_CONTEXT_ICONIFY;
1369     if (win == self->close)             return OB_FRAME_CONTEXT_CLOSE;
1370     if (win == self->icon)              return OB_FRAME_CONTEXT_ICON;
1371     if (win == self->desk)              return OB_FRAME_CONTEXT_ALLDESKTOPS;
1372     if (win == self->shade)             return OB_FRAME_CONTEXT_SHADE;
1373
1374     return OB_FRAME_CONTEXT_NONE;
1375 }
1376
1377 void frame_client_gravity(ObFrame *self, gint *x, gint *y, gint w, gint h)
1378 {
1379     /* horizontal */
1380     switch (self->client->gravity) {
1381     default:
1382     case NorthWestGravity:
1383     case SouthWestGravity:
1384     case WestGravity:
1385         break;
1386
1387     case NorthGravity:
1388     case SouthGravity:
1389     case CenterGravity:
1390         /* the middle of the client will be the middle of the frame */
1391         *x -= (self->size.right - self->size.left) / 2;
1392         break;
1393
1394     case NorthEastGravity:
1395     case SouthEastGravity:
1396     case EastGravity:
1397         /* the right side of the client will be the right side of the frame */
1398         *x -= self->size.right + self->size.left -
1399             self->client->border_width * 2;
1400         break;
1401
1402     case ForgetGravity:
1403     case StaticGravity:
1404         /* the client's position won't move */
1405         *x -= self->size.left - self->client->border_width;
1406         break;
1407     }
1408
1409     /* vertical */
1410     switch (self->client->gravity) {
1411     default:
1412     case NorthWestGravity:
1413     case NorthEastGravity:
1414     case NorthGravity:
1415         break;
1416
1417     case CenterGravity:
1418     case EastGravity:
1419     case WestGravity:
1420         /* the middle of the client will be the middle of the frame */
1421         *y -= (self->size.bottom - self->size.top) / 2;
1422         break;
1423
1424     case SouthWestGravity:
1425     case SouthEastGravity:
1426     case SouthGravity:
1427         /* the bottom of the client will be the bottom of the frame */
1428         *y -= self->size.bottom + self->size.top -
1429             self->client->border_width * 2;
1430         break;
1431
1432     case ForgetGravity:
1433     case StaticGravity:
1434         /* the client's position won't move */
1435         *y -= self->size.top - self->client->border_width;
1436         break;
1437     }
1438 }
1439
1440 void frame_frame_gravity(ObFrame *self, gint *x, gint *y, gint w, gint h)
1441 {
1442     /* horizontal */
1443     switch (self->client->gravity) {
1444     default:
1445     case NorthWestGravity:
1446     case WestGravity:
1447     case SouthWestGravity:
1448         break;
1449     case NorthGravity:
1450     case CenterGravity:
1451     case SouthGravity:
1452         /* the middle of the client will be the middle of the frame */
1453         *x += (self->size.right - self->size.left) / 2;
1454         break;
1455     case NorthEastGravity:
1456     case EastGravity:
1457     case SouthEastGravity:
1458         /* the right side of the client will be the right side of the frame */
1459         *x += self->size.right + self->size.left -
1460             self->client->border_width * 2;
1461         break;
1462     case StaticGravity:
1463     case ForgetGravity:
1464         /* the client's position won't move */
1465         *x += self->size.left - self->client->border_width;
1466         break;
1467     }
1468
1469     /* vertical */
1470     switch (self->client->gravity) {
1471     default:
1472     case NorthWestGravity:
1473     case NorthGravity:
1474     case NorthEastGravity:
1475         break;
1476     case WestGravity:
1477     case CenterGravity:
1478     case EastGravity:
1479         /* the middle of the client will be the middle of the frame */
1480         *y += (self->size.bottom - self->size.top) / 2;
1481         break;
1482     case SouthWestGravity:
1483     case SouthGravity:
1484     case SouthEastGravity:
1485         /* the bottom of the client will be the bottom of the frame */
1486         *y += self->size.bottom + self->size.top -
1487             self->client->border_width * 2;
1488         break;
1489     case StaticGravity:
1490     case ForgetGravity:
1491         /* the client's position won't move */
1492         *y += self->size.top - self->client->border_width;
1493         break;
1494     }
1495 }
1496
1497 static void flash_done(gpointer data)
1498 {
1499     ObFrame *self = data;
1500
1501     if (self->focused != self->flash_on)
1502         frame_adjust_focus(self, self->focused);
1503 }
1504
1505 static gboolean flash_timeout(gpointer data)
1506 {
1507     ObFrame *self = data;
1508     GTimeVal now;
1509
1510     g_get_current_time(&now);
1511     if (now.tv_sec > self->flash_end.tv_sec ||
1512         (now.tv_sec == self->flash_end.tv_sec &&
1513          now.tv_usec >= self->flash_end.tv_usec))
1514         self->flashing = FALSE;
1515
1516     if (!self->flashing)
1517         return FALSE; /* we are done */
1518
1519     self->flash_on = !self->flash_on;
1520     if (!self->focused) {
1521         frame_adjust_focus(self, self->flash_on);
1522         self->focused = FALSE;
1523     }
1524
1525     return TRUE; /* go again */
1526 }
1527
1528 void frame_flash_start(ObFrame *self)
1529 {
1530     self->flash_on = self->focused;
1531
1532     if (!self->flashing)
1533         ob_main_loop_timeout_add(ob_main_loop,
1534                                  G_USEC_PER_SEC * 0.6,
1535                                  flash_timeout,
1536                                  self,
1537                                  g_direct_equal,
1538                                  flash_done);
1539     g_get_current_time(&self->flash_end);
1540     g_time_val_add(&self->flash_end, G_USEC_PER_SEC * 5);
1541     
1542     self->flashing = TRUE;
1543 }
1544
1545 void frame_flash_stop(ObFrame *self)
1546 {
1547     self->flashing = FALSE;
1548 }
1549
1550 static gulong frame_animate_iconify_time_left(ObFrame *self,
1551                                               const GTimeVal *now)
1552 {
1553     glong sec, usec;
1554     sec = self->iconify_animation_end.tv_sec - now->tv_sec;
1555     usec = self->iconify_animation_end.tv_usec - now->tv_usec;
1556     if (usec < 0) {
1557         usec += G_USEC_PER_SEC;
1558         sec--;
1559     }
1560     /* no negative values */
1561     return MAX(sec * G_USEC_PER_SEC + usec, 0);
1562 }
1563
1564 static gboolean frame_animate_iconify(gpointer p)
1565 {
1566     ObFrame *self = p;
1567     gint x, y, w, h;
1568     gint iconx, icony, iconw;
1569     GTimeVal now;
1570     gulong time;
1571     gboolean iconifying;
1572
1573     if (self->client->icon_geometry.width == 0) {
1574         /* there is no icon geometry set so just go straight down */
1575         Rect *a = screen_physical_area();
1576         iconx = self->area.x + self->area.width / 2 + 32;
1577         icony = a->y + a->width;
1578         iconw = 64;
1579     } else {
1580         iconx = self->client->icon_geometry.x;
1581         icony = self->client->icon_geometry.y;
1582         iconw = self->client->icon_geometry.width;
1583     }
1584
1585     iconifying = self->iconify_animation_going > 0;
1586
1587     /* how far do we have left to go ? */
1588     g_get_current_time(&now);
1589     time = frame_animate_iconify_time_left(self, &now);
1590     
1591     if (time == 0 || iconifying) {
1592         /* start where the frame is supposed to be */
1593         x = self->area.x;
1594         y = self->area.y;
1595         w = self->area.width;
1596         h = self->area.height;
1597     } else {
1598         /* start at the icon */
1599         x = iconx;
1600         y = icony;
1601         w = iconw;
1602         h = self->size.top; /* just the titlebar */
1603     }
1604
1605     if (time > 0) {
1606         glong dx, dy, dw;
1607         glong elapsed;
1608
1609         dx = self->area.x - iconx;
1610         dy = self->area.y - icony;
1611         dw = self->area.width - self->bwidth * 2 - iconw;
1612          /* if restoring, we move in the opposite direction */
1613         if (!iconifying) { dx = -dx; dy = -dy; dw = -dw; }
1614
1615         elapsed = FRAME_ANIMATE_ICONIFY_TIME - time;
1616         x = x - (dx * elapsed) / FRAME_ANIMATE_ICONIFY_TIME;
1617         y = y - (dy * elapsed) / FRAME_ANIMATE_ICONIFY_TIME;
1618         w = w - (dw * elapsed) / FRAME_ANIMATE_ICONIFY_TIME;
1619         h = self->size.top; /* just the titlebar */
1620     }
1621
1622     if (time == 0)
1623         frame_end_iconify_animation(self);
1624     else {
1625         XMoveResizeWindow(ob_display, self->window, x, y, w, h);
1626         XFlush(ob_display);
1627     }
1628
1629     return time > 0; /* repeat until we're out of time */
1630 }
1631
1632 void frame_end_iconify_animation(ObFrame *self)
1633 {
1634     /* see if there is an animation going */
1635     if (self->iconify_animation_going == 0) return;
1636
1637     if (!self->visible)
1638         XUnmapWindow(ob_display, self->window);
1639     else
1640         /* Send a ConfigureNotify when the animation is done, this fixes
1641            KDE's pager showing the window in the wrong place. */
1642         client_reconfigure(self->client);
1643
1644     /* we're not animating any more ! */
1645     self->iconify_animation_going = 0;
1646
1647     XMoveResizeWindow(ob_display, self->window,
1648                       self->area.x, self->area.y,
1649                       self->area.width, self->area.height);
1650     XFlush(ob_display);
1651 }
1652
1653 void frame_begin_iconify_animation(ObFrame *self, gboolean iconifying)
1654 {
1655     gulong time;
1656     gboolean new_anim = FALSE;
1657     gboolean set_end = TRUE;
1658     GTimeVal now;
1659
1660     /* if there is no titlebar, just don't animate for now
1661        XXX it would be nice tho.. */
1662     if (!(self->decorations & OB_FRAME_DECOR_TITLEBAR))
1663         return;
1664
1665     /* get the current time */
1666     g_get_current_time(&now);
1667
1668     /* get how long until the end */
1669     time = FRAME_ANIMATE_ICONIFY_TIME;
1670     if (self->iconify_animation_going) {
1671         if (!!iconifying != (self->iconify_animation_going > 0)) {
1672             /* animation was already going on in the opposite direction */
1673             time = time - frame_animate_iconify_time_left(self, &now);
1674         } else
1675             /* animation was already going in the same direction */
1676             set_end = FALSE;
1677     } else
1678         new_anim = TRUE;
1679     self->iconify_animation_going = iconifying ? 1 : -1;
1680
1681     /* set the ending time */
1682     if (set_end) {
1683         self->iconify_animation_end.tv_sec = now.tv_sec;
1684         self->iconify_animation_end.tv_usec = now.tv_usec;
1685         g_time_val_add(&self->iconify_animation_end, time);
1686     }
1687
1688     if (new_anim) {
1689         ob_main_loop_timeout_remove_data(ob_main_loop, frame_animate_iconify,
1690                                          self, FALSE);
1691         ob_main_loop_timeout_add(ob_main_loop,
1692                                  FRAME_ANIMATE_ICONIFY_STEP_TIME,
1693                                  frame_animate_iconify, self,
1694                                  g_direct_equal, NULL);
1695
1696         /* do the first step */
1697         frame_animate_iconify(self);
1698
1699         /* show it during the animation even if it is not "visible" */
1700         if (!self->visible)
1701             XMapWindow(ob_display, self->window);
1702     }
1703 }