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