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