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