716d23ada421fc82b0b8e64bad19ee07d9bfe856
[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.h"
29 #include "moveresize.h"
30 #include "screen.h"
31 #include "render/theme.h"
32
33 #define PLATE_EVENTMASK (SubstructureRedirectMask | FocusChangeMask)
34 #define FRAME_EVENTMASK (EnterWindowMask | LeaveWindowMask | \
35                          ButtonPressMask | ButtonReleaseMask)
36 #define ELEMENT_EVENTMASK (ButtonPressMask | ButtonReleaseMask | \
37                            ButtonMotionMask | PointerMotionMask | \
38                            EnterWindowMask | LeaveWindowMask)
39 /* The inner window does not need enter/leave events.
40    If it does get them, then it needs its own context for enter events
41    because sloppy focus will focus the window when you enter the inner window
42    from the frame. */
43 #define INNER_EVENTMASK (ButtonPressMask)
44
45 #define FRAME_ANIMATE_ICONIFY_TIME 150000 /* .15 seconds */
46 #define FRAME_ANIMATE_ICONIFY_STEP_TIME (G_USEC_PER_SEC / 60) /* 60 Hz */
47
48 #define FRAME_HANDLE_Y(f) (f->innersize.top + f->client->area.height + \
49                            f->cbwidth_y)
50
51 /* the offsets for the titlebar elements from the edge of the titlebar.
52    negative means from the right edge. */
53 gint icon_off;
54 gint label_off;
55 gint iconify_off;
56 gint desk_off;
57 gint shade_off;
58 gint max_off;
59 gint close_off;
60
61
62 static void flash_done(gpointer data);
63 static gboolean flash_timeout(gpointer data);
64
65 static void layout_title(ObFrame *self);
66 static void set_theme_statics(ObFrame *self);
67 static void free_theme_statics(ObFrame *self);
68 static gboolean frame_animate_iconify(gpointer self);
69
70 static Window createWindow(Window parent, Visual *visual,
71                            gulong mask, XSetWindowAttributes *attrib)
72 {
73     return XCreateWindow(ob_display, parent, 0, 0, 1, 1, 0,
74                          (visual ? 32 : RrDepth(ob_rr_inst)), InputOutput,
75                          (visual ? visual : RrVisual(ob_rr_inst)),
76                          mask, attrib);
77                        
78 }
79
80 static Visual *check_32bit_client(ObClient *c)
81 {
82     XWindowAttributes wattrib;
83     Status ret;
84
85     ret = XGetWindowAttributes(ob_display, c->window, &wattrib);
86     g_assert(ret != BadDrawable);
87     g_assert(ret != BadWindow);
88
89     if (wattrib.depth == 32)
90         return wattrib.visual;
91     return NULL;
92 }
93
94 ObFrame *frame_new(ObClient *client)
95 {
96     XSetWindowAttributes attrib;
97     gulong mask;
98     ObFrame *self;
99     Visual *visual;
100
101     self = g_new0(ObFrame, 1);
102     self->client = client;
103
104     visual = check_32bit_client(client);
105
106     /* create the non-visible decor windows */
107
108     mask = CWEventMask;
109     if (visual) {
110         /* client has a 32-bit visual */
111         mask |= CWColormap | CWBackPixel | CWBorderPixel;
112         /* create a colormap with the visual */
113         self->colormap = attrib.colormap =
114             XCreateColormap(ob_display,
115                             RootWindow(ob_display, ob_screen),
116                             visual, AllocNone);
117         attrib.background_pixel = BlackPixel(ob_display, 0);
118         attrib.border_pixel = BlackPixel(ob_display, 0);
119     }
120     attrib.event_mask = FRAME_EVENTMASK;
121     self->window = createWindow(RootWindow(ob_display, ob_screen), visual,
122                                 mask, &attrib);
123
124     attrib.event_mask = INNER_EVENTMASK;
125     self->inner = createWindow(self->window, visual, mask, &attrib);
126
127     mask &= ~CWEventMask;
128     self->plate = createWindow(self->inner, visual, mask, &attrib);
129
130     /* create the visible decor windows */
131
132     mask = CWEventMask;
133     if (visual) {
134         /* client has a 32-bit visual */
135         mask |= CWColormap | CWBackPixel | CWBorderPixel;
136         attrib.colormap = RrColormap(ob_rr_inst);
137     }
138     attrib.event_mask = ELEMENT_EVENTMASK;
139     self->title = createWindow(self->window, NULL, mask, &attrib);
140
141     mask |= CWCursor;
142     attrib.cursor = ob_cursor(OB_CURSOR_NORTHWEST);
143     self->tltresize = createWindow(self->title, NULL, mask, &attrib);
144     self->tllresize = createWindow(self->title, NULL, mask, &attrib);
145     attrib.cursor = ob_cursor(OB_CURSOR_NORTHEAST);
146     self->trtresize = createWindow(self->title, NULL, mask, &attrib);
147     self->trrresize = createWindow(self->title, NULL, mask, &attrib);
148
149     mask &= ~CWCursor;
150     self->label = createWindow(self->title, NULL, mask, &attrib);
151     self->max = createWindow(self->title, NULL, mask, &attrib);
152     self->close = createWindow(self->title, NULL, mask, &attrib);
153     self->desk = createWindow(self->title, NULL, mask, &attrib);
154     self->shade = createWindow(self->title, NULL, mask, &attrib);
155     self->icon = createWindow(self->title, NULL, mask, &attrib);
156     self->iconify = createWindow(self->title, NULL, mask, &attrib);
157
158     mask |= CWCursor;
159     attrib.cursor = ob_cursor(OB_CURSOR_SOUTH);
160     self->handle = createWindow(self->window, NULL, mask, &attrib);
161     attrib.cursor = ob_cursor(OB_CURSOR_SOUTHWEST);
162     self->lgrip = createWindow(self->handle, NULL, mask, &attrib);
163     attrib.cursor = ob_cursor(OB_CURSOR_SOUTHEAST);
164     self->rgrip = createWindow(self->handle, NULL, mask, &attrib); 
165
166     self->focused = FALSE;
167
168     /* the other stuff is shown based on decor settings */
169     XMapWindow(ob_display, self->plate);
170     XMapWindow(ob_display, self->inner);
171     XMapWindow(ob_display, self->lgrip);
172     XMapWindow(ob_display, self->rgrip);
173     XMapWindow(ob_display, self->label);
174
175     self->max_press = self->close_press = self->desk_press = 
176         self->iconify_press = self->shade_press = FALSE;
177     self->max_hover = self->close_hover = self->desk_hover = 
178         self->iconify_hover = self->shade_hover = FALSE;
179
180     set_theme_statics(self);
181
182     return (ObFrame*)self;
183 }
184
185 static void set_theme_statics(ObFrame *self)
186 {
187     /* set colors/appearance/sizes for stuff that doesn't change */
188     XResizeWindow(ob_display, self->max,
189                   ob_rr_theme->button_size, ob_rr_theme->button_size);
190     XResizeWindow(ob_display, self->iconify,
191                   ob_rr_theme->button_size, ob_rr_theme->button_size);
192     XResizeWindow(ob_display, self->icon,
193                   ob_rr_theme->button_size + 2, ob_rr_theme->button_size + 2);
194     XResizeWindow(ob_display, self->close,
195                   ob_rr_theme->button_size, ob_rr_theme->button_size);
196     XResizeWindow(ob_display, self->desk,
197                   ob_rr_theme->button_size, ob_rr_theme->button_size);
198     XResizeWindow(ob_display, self->shade,
199                   ob_rr_theme->button_size, ob_rr_theme->button_size);
200     if (ob_rr_theme->handle_height > 0) {
201         XResizeWindow(ob_display, self->lgrip,
202                       ob_rr_theme->grip_width, ob_rr_theme->handle_height);
203         XResizeWindow(ob_display, self->rgrip,
204                       ob_rr_theme->grip_width, ob_rr_theme->handle_height);
205     }
206     XResizeWindow(ob_display, self->tltresize,
207                   ob_rr_theme->grip_width, ob_rr_theme->paddingy + 1);
208     XResizeWindow(ob_display, self->trtresize,
209                   ob_rr_theme->grip_width, ob_rr_theme->paddingy + 1);
210     XResizeWindow(ob_display, self->tllresize,
211                   ob_rr_theme->paddingx + 1, ob_rr_theme->title_height);
212     XResizeWindow(ob_display, self->trrresize,
213                   ob_rr_theme->paddingx + 1, ob_rr_theme->title_height);
214
215     /* set up the dynamic appearances */
216     self->a_unfocused_title = RrAppearanceCopy(ob_rr_theme->a_unfocused_title);
217     self->a_focused_title = RrAppearanceCopy(ob_rr_theme->a_focused_title);
218     self->a_unfocused_label = RrAppearanceCopy(ob_rr_theme->a_unfocused_label);
219     self->a_focused_label = RrAppearanceCopy(ob_rr_theme->a_focused_label);
220     self->a_unfocused_handle =
221         RrAppearanceCopy(ob_rr_theme->a_unfocused_handle);
222     self->a_focused_handle = RrAppearanceCopy(ob_rr_theme->a_focused_handle);
223     self->a_icon = RrAppearanceCopy(ob_rr_theme->a_icon);
224 }
225
226 static void free_theme_statics(ObFrame *self)
227 {
228     RrAppearanceFree(self->a_unfocused_title); 
229     RrAppearanceFree(self->a_focused_title);
230     RrAppearanceFree(self->a_unfocused_label);
231     RrAppearanceFree(self->a_focused_label);
232     RrAppearanceFree(self->a_unfocused_handle);
233     RrAppearanceFree(self->a_focused_handle);
234     RrAppearanceFree(self->a_icon);
235 }
236
237 void frame_free(ObFrame *self)
238 {
239     free_theme_statics(self);
240
241     XDestroyWindow(ob_display, self->window);
242     if (self->colormap)
243         XFreeColormap(ob_display, self->colormap);
244
245     g_free(self);
246 }
247
248 void frame_show(ObFrame *self)
249 {
250     if (!self->visible) {
251         self->visible = TRUE;
252         XMapWindow(ob_display, self->client->window);
253         XMapWindow(ob_display, self->window);
254     }
255 }
256
257 void frame_hide(ObFrame *self)
258 {
259     if (self->visible) {
260         self->visible = FALSE;
261         if (!frame_iconify_animating(self))
262             XUnmapWindow(ob_display, self->window);
263         /* we unmap the client itself so that we can get MapRequest
264            events, and because the ICCCM tells us to! */
265         XUnmapWindow(ob_display, self->client->window);
266         self->client->ignore_unmaps += 1;
267     }
268 }
269
270 void frame_adjust_theme(ObFrame *self)
271 {
272     free_theme_statics(self);
273     set_theme_statics(self);
274 }
275
276 void frame_adjust_shape(ObFrame *self)
277 {
278 #ifdef SHAPE
279     gint num;
280     XRectangle xrect[2];
281
282     if (!self->client->shaped) {
283         /* clear the shape on the frame window */
284         XShapeCombineMask(ob_display, self->window, ShapeBounding,
285                           self->innersize.left,
286                           self->innersize.top,
287                           None, ShapeSet);
288     } else {
289         /* make the frame's shape match the clients */
290         XShapeCombineShape(ob_display, self->window, ShapeBounding,
291                            self->innersize.left,
292                            self->innersize.top,
293                            self->client->window,
294                            ShapeBounding, ShapeSet);
295
296         num = 0;
297         if (self->decorations & OB_FRAME_DECOR_TITLEBAR) {
298             xrect[0].x = -ob_rr_theme->fbwidth;
299             xrect[0].y = -ob_rr_theme->fbwidth;
300             xrect[0].width = self->width + self->rbwidth * 2;
301             xrect[0].height = ob_rr_theme->title_height +
302                 self->bwidth * 2;
303             ++num;
304         }
305
306         if (self->decorations & OB_FRAME_DECOR_HANDLE) {
307             xrect[1].x = -ob_rr_theme->fbwidth;
308             xrect[1].y = FRAME_HANDLE_Y(self);
309             xrect[1].width = self->width + self->rbwidth * 2;
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         self->decorations = self->client->decorations;
331         self->max_horz = self->client->max_horz;
332
333         if (self->decorations & OB_FRAME_DECOR_BORDER) {
334             self->bwidth = ob_rr_theme->fbwidth;
335             self->cbwidth_x = ob_rr_theme->cbwidthx;
336             self->cbwidth_y = ob_rr_theme->cbwidthy;
337         } else {
338             self->bwidth = self->cbwidth_x = self->cbwidth_y = 0;
339         }
340         self->rbwidth = self->bwidth;
341
342         if (self->max_horz)
343             self->bwidth = self->cbwidth_x = 0;
344
345         STRUT_SET(self->innersize,
346                   self->cbwidth_x,
347                   self->cbwidth_y,
348                   self->cbwidth_x,
349                   self->cbwidth_y);
350         self->width = self->client->area.width + self->cbwidth_x * 2 -
351             (self->max_horz ? self->rbwidth * 2 : 0);
352         self->width = MAX(self->width, 1); /* no lower than 1 */
353
354         /* set border widths */
355         if (!fake) {
356             XSetWindowBorderWidth(ob_display, self->window, self->bwidth);
357             XSetWindowBorderWidth(ob_display, self->inner, self->bwidth);
358             XSetWindowBorderWidth(ob_display, self->title,  self->rbwidth);
359             XSetWindowBorderWidth(ob_display, self->handle, self->rbwidth);
360             XSetWindowBorderWidth(ob_display, self->lgrip,  self->rbwidth);
361             XSetWindowBorderWidth(ob_display, self->rgrip,  self->rbwidth);
362         }
363
364         if (self->decorations & OB_FRAME_DECOR_TITLEBAR)
365             self->innersize.top += ob_rr_theme->title_height + self->rbwidth +
366                 (self->rbwidth - self->bwidth);
367         if (self->decorations & OB_FRAME_DECOR_HANDLE &&
368             ob_rr_theme->handle_height > 0)
369             self->innersize.bottom += ob_rr_theme->handle_height +
370                 self->rbwidth + (self->rbwidth - self->bwidth);
371   
372         /* position/size and map/unmap all the windows */
373
374         if (!fake) {
375             if (self->decorations & OB_FRAME_DECOR_TITLEBAR) {
376                 XMoveResizeWindow(ob_display, self->title,
377                                   -self->bwidth, -self->bwidth,
378                                   self->width, ob_rr_theme->title_height);
379                 XMapWindow(ob_display, self->title);
380
381                 if (self->decorations & OB_FRAME_DECOR_GRIPS) {
382                     XMoveWindow(ob_display, self->tltresize, 0, 0);
383                     XMoveWindow(ob_display, self->tllresize, 0, 0);
384                     XMoveWindow(ob_display, self->trtresize,
385                                 self->width - ob_rr_theme->grip_width, 0);
386                     XMoveWindow(ob_display, self->trrresize,
387                                 self->width - ob_rr_theme->paddingx - 1, 0);
388                     XMapWindow(ob_display, self->tltresize);
389                     XMapWindow(ob_display, self->tllresize);
390                     XMapWindow(ob_display, self->trtresize);
391                     XMapWindow(ob_display, self->trrresize);
392                 } else {
393                     XUnmapWindow(ob_display, self->tltresize);
394                     XUnmapWindow(ob_display, self->tllresize);
395                     XUnmapWindow(ob_display, self->trtresize);
396                     XUnmapWindow(ob_display, self->trrresize);
397                 }
398             } else
399                 XUnmapWindow(ob_display, self->title);
400         }
401
402         if ((self->decorations & OB_FRAME_DECOR_TITLEBAR))
403             /* layout the title bar elements */
404             layout_title(self);
405
406         if (!fake) {
407             if (self->decorations & OB_FRAME_DECOR_HANDLE &&
408                 ob_rr_theme->handle_height > 0)
409             {
410                 XMoveResizeWindow(ob_display, self->handle,
411                                   -self->bwidth, FRAME_HANDLE_Y(self),
412                                   self->width, ob_rr_theme->handle_height);
413                 XMapWindow(ob_display, self->handle);
414
415                 if (self->decorations & OB_FRAME_DECOR_GRIPS) {
416                     XMoveWindow(ob_display, self->lgrip,
417                                 -self->rbwidth, -self->rbwidth);
418                     XMoveWindow(ob_display, self->rgrip,
419                                 -self->rbwidth + self->width -
420                                 ob_rr_theme->grip_width, -self->rbwidth);
421                     XMapWindow(ob_display, self->lgrip);
422                     XMapWindow(ob_display, self->rgrip);
423                 } else {
424                     XUnmapWindow(ob_display, self->lgrip);
425                     XUnmapWindow(ob_display, self->rgrip);
426                 }
427             } else
428                 XUnmapWindow(ob_display, self->handle);
429
430             /* move and resize the inner border window which contains the plate
431              */
432             XMoveResizeWindow(ob_display, self->inner,
433                               self->innersize.left - self->cbwidth_x -
434                               self->bwidth,
435                               self->innersize.top - self->cbwidth_y -
436                               self->bwidth,
437                               self->client->area.width +
438                               self->cbwidth_x * 2,
439                               self->client->area.height +
440                               self->cbwidth_y * 2);
441
442             /* move the plate */
443             XMoveWindow(ob_display, self->plate,
444                         self->cbwidth_x, self->cbwidth_y);
445
446             /* when the client has StaticGravity, it likes to move around. */
447             XMoveWindow(ob_display, self->client->window, 0, 0);
448         }
449
450         STRUT_SET(self->size,
451                   self->innersize.left + self->bwidth,
452                   self->innersize.top + self->bwidth,
453                   self->innersize.right + self->bwidth,
454                   self->innersize.bottom + self->bwidth);
455     }
456
457     /* shading can change without being moved or resized */
458     RECT_SET_SIZE(self->area,
459                   self->client->area.width +
460                   self->size.left + self->size.right,
461                   (self->client->shaded ?
462                    ob_rr_theme->title_height + self->rbwidth * 2:
463                    self->client->area.height +
464                    self->size.top + self->size.bottom));
465
466     if (moved || resized) {
467         /* find the new coordinates, done after setting the frame.size, for
468            frame_client_gravity. */
469         self->area.x = self->client->area.x;
470         self->area.y = self->client->area.y;
471         frame_client_gravity(self, &self->area.x, &self->area.y,
472                              self->client->area.width,
473                              self->client->area.height);
474     }
475
476     if (!fake) {
477         if (!frame_iconify_animating(self))
478             /* move and resize the top level frame.
479                shading can change without being moved or resized.
480                
481                but don't do this during an iconify animation. it will be
482                reflected afterwards.
483             */
484             XMoveResizeWindow(ob_display, self->window,
485                               self->area.x, self->area.y,
486                               self->area.width - self->bwidth * 2,
487                               self->area.height - self->bwidth * 2);
488
489         if (resized) {
490             framerender_frame(self);
491             frame_adjust_shape(self);
492         }
493
494         if (!STRUT_EQUAL(self->size, oldsize)) {
495             gulong vals[4];
496             vals[0] = self->size.left;
497             vals[1] = self->size.right;
498             vals[2] = self->size.top;
499             vals[3] = self->size.bottom;
500             PROP_SETA32(self->client->window, net_frame_extents,
501                         cardinal, vals, 4);
502             PROP_SETA32(self->client->window, kde_net_wm_frame_strut,
503                         cardinal, vals, 4);
504         }
505
506         /* if this occurs while we are focus cycling, the indicator needs to
507            match the changes */
508         if (focus_cycle_target == self->client)
509             focus_cycle_draw_indicator();
510     }
511     if (resized && (self->decorations & OB_FRAME_DECOR_TITLEBAR))
512         XResizeWindow(ob_display, self->label, self->label_width,
513                       ob_rr_theme->label_height);
514 }
515
516 void frame_adjust_client_area(ObFrame *self)
517 {
518     /* resize the plate */
519     XResizeWindow(ob_display, self->plate,
520                   self->client->area.width, self->client->area.height);
521 }
522
523 void frame_adjust_state(ObFrame *self)
524 {
525     framerender_frame(self);
526 }
527
528 void frame_adjust_focus(ObFrame *self, gboolean hilite)
529 {
530     self->focused = hilite;
531     framerender_frame(self);
532     XFlush(ob_display);
533 }
534
535 void frame_adjust_title(ObFrame *self)
536 {
537     framerender_frame(self);
538 }
539
540 void frame_adjust_icon(ObFrame *self)
541 {
542     framerender_frame(self);
543 }
544
545 void frame_grab_client(ObFrame *self)
546 {
547     /* reparent the client to the frame */
548     XReparentWindow(ob_display, self->client->window, self->plate, 0, 0);
549     /*
550       When reparenting the client window, it is usually not mapped yet, since
551       this occurs from a MapRequest. However, in the case where Openbox is
552       starting up, the window is already mapped, so we'll see unmap events for
553       it. There are 2 unmap events generated that we see, one with the 'event'
554       member set the root window, and one set to the client, but both get
555       handled and need to be ignored.
556     */
557     if (ob_state() == OB_STATE_STARTING)
558         self->client->ignore_unmaps += 2;
559
560     /* select the event mask on the client's parent (to receive config/map
561        req's) the ButtonPress is to catch clicks on the client border */
562     XSelectInput(ob_display, self->plate, PLATE_EVENTMASK);
563
564     /* map the client so it maps when the frame does */
565     XMapWindow(ob_display, self->client->window);
566
567     /* set all the windows for the frame in the window_map */
568     g_hash_table_insert(window_map, &self->window, self->client);
569     g_hash_table_insert(window_map, &self->plate, self->client);
570     g_hash_table_insert(window_map, &self->inner, self->client);
571     g_hash_table_insert(window_map, &self->title, self->client);
572     g_hash_table_insert(window_map, &self->label, self->client);
573     g_hash_table_insert(window_map, &self->max, self->client);
574     g_hash_table_insert(window_map, &self->close, self->client);
575     g_hash_table_insert(window_map, &self->desk, self->client);
576     g_hash_table_insert(window_map, &self->shade, self->client);
577     g_hash_table_insert(window_map, &self->icon, self->client);
578     g_hash_table_insert(window_map, &self->iconify, self->client);
579     g_hash_table_insert(window_map, &self->handle, self->client);
580     g_hash_table_insert(window_map, &self->lgrip, self->client);
581     g_hash_table_insert(window_map, &self->rgrip, self->client);
582     g_hash_table_insert(window_map, &self->tltresize, self->client);
583     g_hash_table_insert(window_map, &self->tllresize, self->client);
584     g_hash_table_insert(window_map, &self->trtresize, self->client);
585     g_hash_table_insert(window_map, &self->trrresize, self->client);
586 }
587
588 void frame_release_client(ObFrame *self)
589 {
590     XEvent ev;
591     gboolean reparent = TRUE;
592
593     /* if there was any animation going on, kill it */
594     ob_main_loop_timeout_remove_data(ob_main_loop, frame_animate_iconify,
595                                      self, FALSE);
596
597     /* check if the app has already reparented its window away */
598     while (XCheckTypedWindowEvent(ob_display, self->client->window,
599                                   ReparentNotify, &ev))
600     {
601         /* This check makes sure we don't catch our own reparent action to
602            our frame window. This doesn't count as the app reparenting itself
603            away of course.
604
605            Reparent events that are generated by us are just discarded here.
606            They are of no consequence to us anyhow.
607         */
608         if (ev.xreparent.parent != self->plate) {
609             reparent = FALSE;
610             XPutBackEvent(ob_display, &ev);
611             break;
612         }
613     }
614
615     if (reparent) {
616         /* according to the ICCCM - if the client doesn't reparent itself,
617            then we will reparent the window to root for them */
618         XReparentWindow(ob_display, self->client->window,
619                         RootWindow(ob_display, ob_screen),
620                         self->client->area.x,
621                         self->client->area.y);
622     }
623
624     /* remove all the windows for the frame from the window_map */
625     g_hash_table_remove(window_map, &self->window);
626     g_hash_table_remove(window_map, &self->plate);
627     g_hash_table_remove(window_map, &self->inner);
628     g_hash_table_remove(window_map, &self->title);
629     g_hash_table_remove(window_map, &self->label);
630     g_hash_table_remove(window_map, &self->max);
631     g_hash_table_remove(window_map, &self->close);
632     g_hash_table_remove(window_map, &self->desk);
633     g_hash_table_remove(window_map, &self->shade);
634     g_hash_table_remove(window_map, &self->icon);
635     g_hash_table_remove(window_map, &self->iconify);
636     g_hash_table_remove(window_map, &self->handle);
637     g_hash_table_remove(window_map, &self->lgrip);
638     g_hash_table_remove(window_map, &self->rgrip);
639     g_hash_table_remove(window_map, &self->tltresize);
640     g_hash_table_remove(window_map, &self->tllresize);
641     g_hash_table_remove(window_map, &self->trtresize);
642     g_hash_table_remove(window_map, &self->trrresize);
643
644     ob_main_loop_timeout_remove_data(ob_main_loop, flash_timeout, self, TRUE);
645 }
646
647 /* is there anything present between us and the label? */
648 static gboolean is_button_present(ObFrame *self, const gchar *lc, gint dir) {
649     for (; *lc != '\0' && lc >= config_title_layout; lc += dir) {
650         if (*lc == ' ') continue; /* it was invalid */
651         if (*lc == 'N' && self->decorations & OB_FRAME_DECOR_ICON)
652             return TRUE;
653         if (*lc == 'D' && self->decorations & OB_FRAME_DECOR_ALLDESKTOPS)
654             return TRUE;
655         if (*lc == 'S' && self->decorations & OB_FRAME_DECOR_SHADE)
656             return TRUE;
657         if (*lc == 'I' && self->decorations & OB_FRAME_DECOR_ICONIFY)
658             return TRUE;
659         if (*lc == 'M' && self->decorations & OB_FRAME_DECOR_MAXIMIZE)
660             return TRUE;
661         if (*lc == 'C' && self->decorations & OB_FRAME_DECOR_CLOSE)
662             return TRUE;
663         if (*lc == 'L') return FALSE;
664     }
665     return FALSE;
666 }
667
668 static void layout_title(ObFrame *self)
669 {
670     gchar *lc;
671     gint i;
672
673     const gint bwidth = ob_rr_theme->button_size + ob_rr_theme->paddingx + 1;
674     /* position of the left most button */
675     const gint left = ob_rr_theme->paddingx + 1;
676     /* position of the right most button */
677     const gint right = self->width - bwidth;
678
679     /* turn them all off */
680     self->icon_on = self->desk_on = self->shade_on = self->iconify_on =
681         self->max_on = self->close_on = self->label_on = FALSE;
682     self->label_width = self->width - (ob_rr_theme->paddingx + 1) * 2;
683     self->leftmost = self->rightmost = OB_FRAME_CONTEXT_NONE;
684
685     /* figure out what's being show, find each element's position, and the
686        width of the label
687
688        do the ones before the label, then after the label,
689        i will be +1 the first time through when working to the left,
690        and -1 the second time through when working to the right */
691     for (i = 1; i >= -1; i-=2) {
692         gint x;
693         ObFrameContext *firstcon;
694
695         if (i > 0) {
696             x = left;
697             lc = config_title_layout;
698             firstcon = &self->leftmost;
699         } else {
700             x = right;
701             lc = config_title_layout + strlen(config_title_layout)-1;
702             firstcon = &self->rightmost;
703         }
704
705         /* stop at the end of the string (or the label, which calls break) */
706         for (; *lc != '\0' && lc >= config_title_layout; lc+=i) {
707             if (*lc == 'L') {
708                 if (i > 0) {
709                     self->label_on = TRUE;
710                     self->label_x = x;
711                 }
712                 break; /* break the for loop, do other side of label */
713             } else if (*lc == 'N') {
714                 if (firstcon) *firstcon = OB_FRAME_CONTEXT_ICON;
715                 if ((self->icon_on = is_button_present(self, lc, i))) {
716                     /* icon is bigger than buttons */
717                     self->label_width -= bwidth + 2;
718                     self->icon_x = x;
719                     x += i * (bwidth + 2);
720                 }
721             } else if (*lc == 'D') {
722                 if (firstcon) *firstcon = OB_FRAME_CONTEXT_ALLDESKTOPS;
723                 if ((self->desk_on = is_button_present(self, lc, i))) {
724                     self->label_width -= bwidth;
725                     self->desk_x = x;
726                     x += i * bwidth;
727                 }
728             } else if (*lc == 'S') {
729                 if (firstcon) *firstcon = OB_FRAME_CONTEXT_SHADE;
730                 if ((self->shade_on = is_button_present(self, lc, i))) {
731                     self->label_width -= bwidth;
732                     self->shade_x = x;
733                     x += i * bwidth;
734                 }
735             } else if (*lc == 'I') {
736                 if (firstcon) *firstcon = OB_FRAME_CONTEXT_ICONIFY;
737                 if ((self->iconify_on = is_button_present(self, lc, i))) {
738                     self->label_width -= bwidth;
739                     self->iconify_x = x;
740                     x += i * bwidth;
741                 }
742             } else if (*lc == 'M') {
743                 if (firstcon) *firstcon = OB_FRAME_CONTEXT_MAXIMIZE;
744                 if ((self->max_on = is_button_present(self, lc, i))) {
745                     self->label_width -= bwidth;
746                     self->max_x = x;
747                     x += i * bwidth;
748                 }
749             } else if (*lc == 'C') {
750                 if (firstcon) *firstcon = OB_FRAME_CONTEXT_CLOSE;
751                 if ((self->close_on = is_button_present(self, lc, i))) {
752                     self->label_width -= bwidth;
753                     self->close_x = x;
754                     x += i * bwidth;
755                 }
756             } else
757                 continue; /* don't set firstcon */
758             firstcon = NULL;
759         }
760     }
761
762     /* position and map the elements */
763     if (self->icon_on) {
764         XMapWindow(ob_display, self->icon);
765         XMoveWindow(ob_display, self->icon, self->icon_x,
766                     ob_rr_theme->paddingy);
767     } else
768         XUnmapWindow(ob_display, self->icon);
769
770     if (self->desk_on) {
771         XMapWindow(ob_display, self->desk);
772         XMoveWindow(ob_display, self->desk, self->desk_x,
773                     ob_rr_theme->paddingy + 1);
774     } else
775         XUnmapWindow(ob_display, self->desk);
776
777     if (self->shade_on) {
778         XMapWindow(ob_display, self->shade);
779         XMoveWindow(ob_display, self->shade, self->shade_x,
780                     ob_rr_theme->paddingy + 1);
781     } else
782         XUnmapWindow(ob_display, self->shade);
783
784     if (self->iconify_on) {
785         XMapWindow(ob_display, self->iconify);
786         XMoveWindow(ob_display, self->iconify, self->iconify_x,
787                     ob_rr_theme->paddingy + 1);
788     } else
789         XUnmapWindow(ob_display, self->iconify);
790
791     if (self->max_on) {
792         XMapWindow(ob_display, self->max);
793         XMoveWindow(ob_display, self->max, self->max_x,
794                     ob_rr_theme->paddingy + 1);
795     } else
796         XUnmapWindow(ob_display, self->max);
797
798     if (self->close_on) {
799         XMapWindow(ob_display, self->close);
800         XMoveWindow(ob_display, self->close, self->close_x,
801                     ob_rr_theme->paddingy + 1);
802     } else
803         XUnmapWindow(ob_display, self->close);
804
805     if (self->label_on) {
806         self->label_width = MAX(1, self->label_width); /* no lower than 1 */
807         XMapWindow(ob_display, self->label);
808         XMoveWindow(ob_display, self->label, self->label_x,
809                     ob_rr_theme->paddingy);
810     } else
811         XUnmapWindow(ob_display, self->label);
812 }
813
814 ObFrameContext frame_context_from_string(const gchar *name)
815 {
816     if (!g_ascii_strcasecmp("Desktop", name))
817         return OB_FRAME_CONTEXT_DESKTOP;
818     else if (!g_ascii_strcasecmp("Client", name))
819         return OB_FRAME_CONTEXT_CLIENT;
820     else if (!g_ascii_strcasecmp("Titlebar", name))
821         return OB_FRAME_CONTEXT_TITLEBAR;
822     else if (!g_ascii_strcasecmp("Handle", name))
823         return OB_FRAME_CONTEXT_HANDLE;
824     else if (!g_ascii_strcasecmp("Frame", name))
825         return OB_FRAME_CONTEXT_FRAME;
826     else if (!g_ascii_strcasecmp("TLCorner", name))
827         return OB_FRAME_CONTEXT_TLCORNER;
828     else if (!g_ascii_strcasecmp("TRCorner", name))
829         return OB_FRAME_CONTEXT_TRCORNER;
830     else if (!g_ascii_strcasecmp("BLCorner", name))
831         return OB_FRAME_CONTEXT_BLCORNER;
832     else if (!g_ascii_strcasecmp("BRCorner", name))
833         return OB_FRAME_CONTEXT_BRCORNER;
834     else if (!g_ascii_strcasecmp("Maximize", name))
835         return OB_FRAME_CONTEXT_MAXIMIZE;
836     else if (!g_ascii_strcasecmp("AllDesktops", name))
837         return OB_FRAME_CONTEXT_ALLDESKTOPS;
838     else if (!g_ascii_strcasecmp("Shade", name))
839         return OB_FRAME_CONTEXT_SHADE;
840     else if (!g_ascii_strcasecmp("Iconify", name))
841         return OB_FRAME_CONTEXT_ICONIFY;
842     else if (!g_ascii_strcasecmp("Icon", name))
843         return OB_FRAME_CONTEXT_ICON;
844     else if (!g_ascii_strcasecmp("Close", name))
845         return OB_FRAME_CONTEXT_CLOSE;
846     else if (!g_ascii_strcasecmp("MoveResize", name))
847         return OB_FRAME_CONTEXT_MOVE_RESIZE;
848     return OB_FRAME_CONTEXT_NONE;
849 }
850
851 ObFrameContext frame_context(ObClient *client, Window win, gint x, gint y)
852 {
853     ObFrame *self;
854
855     if (moveresize_in_progress)
856         return OB_FRAME_CONTEXT_MOVE_RESIZE;
857
858     if (win == RootWindow(ob_display, ob_screen))
859         return OB_FRAME_CONTEXT_DESKTOP;
860     if (client == NULL) return OB_FRAME_CONTEXT_NONE;
861     if (win == client->window) {
862         /* conceptually, this is the desktop, as far as users are
863            concerned */
864         if (client->type == OB_CLIENT_TYPE_DESKTOP)
865             return OB_FRAME_CONTEXT_DESKTOP;
866         return OB_FRAME_CONTEXT_CLIENT;
867     }
868
869     self = client->frame;
870     if (win == self->inner || win == self->plate) {
871         /* conceptually, this is the desktop, as far as users are
872            concerned */
873         if (client->type == OB_CLIENT_TYPE_DESKTOP)
874             return OB_FRAME_CONTEXT_DESKTOP;
875         return OB_FRAME_CONTEXT_CLIENT;
876     }
877
878     if (win == self->title) {
879         /* when the user clicks in the corners of the titlebar and the client
880            is fully maximized, then treat it like they clicked in the
881            button that is there */
882         if (self->client->max_horz && self->client->max_vert &&
883             y < ob_rr_theme->paddingy + 1 + ob_rr_theme->button_size)
884         {
885             if (x < ((ob_rr_theme->paddingx + 1) * 2 +
886                      ob_rr_theme->button_size)) {
887                 if (self->leftmost != OB_FRAME_CONTEXT_NONE)
888                     return self->leftmost;
889             }
890             else if (x > (self->width -
891                           (ob_rr_theme->paddingx + 1 +
892                            ob_rr_theme->button_size)))
893             {
894                 if (self->rightmost != OB_FRAME_CONTEXT_NONE)
895                     return self->rightmost;
896             }
897         }
898         return OB_FRAME_CONTEXT_TITLEBAR;
899     }
900
901     if (win == self->window)    return OB_FRAME_CONTEXT_FRAME;
902     if (win == self->label)     return OB_FRAME_CONTEXT_TITLEBAR;
903     if (win == self->handle)    return OB_FRAME_CONTEXT_HANDLE;
904     if (win == self->lgrip)     return OB_FRAME_CONTEXT_BLCORNER;
905     if (win == self->rgrip)     return OB_FRAME_CONTEXT_BRCORNER;
906     if (win == self->tltresize) return OB_FRAME_CONTEXT_TLCORNER;
907     if (win == self->tllresize) return OB_FRAME_CONTEXT_TLCORNER;
908     if (win == self->trtresize) return OB_FRAME_CONTEXT_TRCORNER;
909     if (win == self->trrresize) return OB_FRAME_CONTEXT_TRCORNER;
910     if (win == self->max)       return OB_FRAME_CONTEXT_MAXIMIZE;
911     if (win == self->iconify)   return OB_FRAME_CONTEXT_ICONIFY;
912     if (win == self->close)     return OB_FRAME_CONTEXT_CLOSE;
913     if (win == self->icon)      return OB_FRAME_CONTEXT_ICON;
914     if (win == self->desk)      return OB_FRAME_CONTEXT_ALLDESKTOPS;
915     if (win == self->shade)     return OB_FRAME_CONTEXT_SHADE;
916
917     return OB_FRAME_CONTEXT_NONE;
918 }
919
920 void frame_client_gravity(ObFrame *self, gint *x, gint *y, gint w, gint h)
921 {
922     /* horizontal */
923     switch (self->client->gravity) {
924     default:
925     case NorthWestGravity:
926     case SouthWestGravity:
927     case WestGravity:
928         break;
929
930     case NorthGravity:
931     case SouthGravity:
932     case CenterGravity:
933         *x -= (self->size.left + w) / 2;
934         break;
935
936     case NorthEastGravity:
937     case SouthEastGravity:
938     case EastGravity:
939         *x -= (self->size.left + self->size.right + w) - 1;
940         break;
941
942     case ForgetGravity:
943     case StaticGravity:
944         *x -= self->size.left;
945         break;
946     }
947
948     /* vertical */
949     switch (self->client->gravity) {
950     default:
951     case NorthWestGravity:
952     case NorthEastGravity:
953     case NorthGravity:
954         break;
955
956     case CenterGravity:
957     case EastGravity:
958     case WestGravity:
959         *y -= (self->size.top + h) / 2;
960         break;
961
962     case SouthWestGravity:
963     case SouthEastGravity:
964     case SouthGravity:
965         *y -= (self->size.top + self->size.bottom + h) - 1;
966         break;
967
968     case ForgetGravity:
969     case StaticGravity:
970         *y -= self->size.top;
971         break;
972     }
973 }
974
975 void frame_frame_gravity(ObFrame *self, gint *x, gint *y, gint w, gint h)
976 {
977     /* horizontal */
978     switch (self->client->gravity) {
979     default:
980     case NorthWestGravity:
981     case WestGravity:
982     case SouthWestGravity:
983         break;
984     case NorthGravity:
985     case CenterGravity:
986     case SouthGravity:
987         *x += (self->size.left + w) / 2;
988         break;
989     case NorthEastGravity:
990     case EastGravity:
991     case SouthEastGravity:
992         *x += (self->size.left + self->size.right + w) - 1;
993         break;
994     case StaticGravity:
995     case ForgetGravity:
996         *x += self->size.left;
997         break;
998     }
999
1000     /* vertical */
1001     switch (self->client->gravity) {
1002     default:
1003     case NorthWestGravity:
1004     case NorthGravity:
1005     case NorthEastGravity:
1006         break;
1007     case WestGravity:
1008     case CenterGravity:
1009     case EastGravity:
1010         *y += (self->size.top + h) / 2;
1011         break;
1012     case SouthWestGravity:
1013     case SouthGravity:
1014     case SouthEastGravity:
1015         *y += (self->size.top + self->size.bottom + h) - 1;
1016         break;
1017     case StaticGravity:
1018     case ForgetGravity:
1019         *y += self->size.top;
1020         break;
1021     }
1022 }
1023
1024 static void flash_done(gpointer data)
1025 {
1026     ObFrame *self = data;
1027
1028     if (self->focused != self->flash_on)
1029         frame_adjust_focus(self, self->focused);
1030 }
1031
1032 static gboolean flash_timeout(gpointer data)
1033 {
1034     ObFrame *self = data;
1035     GTimeVal now;
1036
1037     g_get_current_time(&now);
1038     if (now.tv_sec > self->flash_end.tv_sec ||
1039         (now.tv_sec == self->flash_end.tv_sec &&
1040          now.tv_usec >= self->flash_end.tv_usec))
1041         self->flashing = FALSE;
1042
1043     if (!self->flashing)
1044         return FALSE; /* we are done */
1045
1046     self->flash_on = !self->flash_on;
1047     if (!self->focused) {
1048         frame_adjust_focus(self, self->flash_on);
1049         self->focused = FALSE;
1050     }
1051
1052     return TRUE; /* go again */
1053 }
1054
1055 void frame_flash_start(ObFrame *self)
1056 {
1057     self->flash_on = self->focused;
1058
1059     if (!self->flashing)
1060         ob_main_loop_timeout_add(ob_main_loop,
1061                                  G_USEC_PER_SEC * 0.6,
1062                                  flash_timeout,
1063                                  self,
1064                                  g_direct_equal,
1065                                  flash_done);
1066     g_get_current_time(&self->flash_end);
1067     g_time_val_add(&self->flash_end, G_USEC_PER_SEC * 5);
1068     
1069     self->flashing = TRUE;
1070 }
1071
1072 void frame_flash_stop(ObFrame *self)
1073 {
1074     self->flashing = FALSE;
1075 }
1076
1077 static gulong frame_animate_iconify_time_left(ObFrame *self,
1078                                               const GTimeVal *now)
1079 {
1080     glong sec, usec;
1081     sec = self->iconify_animation_end.tv_sec - now->tv_sec;
1082     usec = self->iconify_animation_end.tv_usec - now->tv_usec;
1083     if (usec < 0) {
1084         usec += G_USEC_PER_SEC;
1085         sec--;
1086     }
1087     /* no negative values */
1088     return MAX(sec * G_USEC_PER_SEC + usec, 0);
1089 }
1090
1091 static gboolean frame_animate_iconify(gpointer p)
1092 {
1093     ObFrame *self = p;
1094     gint x, y, w, h;
1095     gint iconx, icony, iconw;
1096     GTimeVal now;
1097     gulong time;
1098     gboolean iconifying;
1099
1100     if (self->client->icon_geometry.width == 0) {
1101         /* there is no icon geometry set so just go straight down */
1102         Rect *a = screen_physical_area();
1103         iconx = self->area.x + self->area.width / 2 + 32;
1104         icony = a->y + a->width;
1105         iconw = 64;
1106     } else {
1107         iconx = self->client->icon_geometry.x;
1108         icony = self->client->icon_geometry.y;
1109         iconw = self->client->icon_geometry.width;
1110     }
1111
1112     iconifying = self->iconify_animation_going > 0;
1113
1114     /* how far do we have left to go ? */
1115     g_get_current_time(&now);
1116     time = frame_animate_iconify_time_left(self, &now);
1117     
1118     if (time == 0 || iconifying) {
1119         /* start where the frame is supposed to be */
1120         x = self->area.x;
1121         y = self->area.y;
1122         w = self->area.width - self->bwidth * 2;
1123         h = self->area.height - self->bwidth * 2;
1124     } else {
1125         /* start at the icon */
1126         x = iconx;
1127         y = icony;
1128         w = iconw;
1129         h = self->innersize.top; /* just the titlebar */
1130     }
1131
1132     if (time > 0) {
1133         glong dx, dy, dw;
1134         glong elapsed;
1135
1136         dx = self->area.x - iconx;
1137         dy = self->area.y - icony;
1138         dw = self->area.width - self->bwidth * 2 - iconw;
1139          /* if restoring, we move in the opposite direction */
1140         if (!iconifying) { dx = -dx; dy = -dy; dw = -dw; }
1141
1142         elapsed = FRAME_ANIMATE_ICONIFY_TIME - time;
1143         x = x - (dx * elapsed) / FRAME_ANIMATE_ICONIFY_TIME;
1144         y = y - (dy * elapsed) / FRAME_ANIMATE_ICONIFY_TIME;
1145         w = w - (dw * elapsed) / FRAME_ANIMATE_ICONIFY_TIME;
1146         h = self->innersize.top; /* just the titlebar */
1147     }
1148
1149     if (time == 0)
1150         frame_end_iconify_animation(self);
1151     else {
1152         XMoveResizeWindow(ob_display, self->window, x, y, w, h);
1153         XFlush(ob_display);
1154     }
1155
1156     return time > 0; /* repeat until we're out of time */
1157 }
1158
1159 void frame_end_iconify_animation(ObFrame *self)
1160 {
1161     /* see if there is an animation going */
1162     if (self->iconify_animation_going == 0) return;
1163
1164     if (!self->visible)
1165         XUnmapWindow(ob_display, self->window);
1166
1167     /* we're not animating any more ! */
1168     self->iconify_animation_going = 0;
1169
1170     XMoveResizeWindow(ob_display, self->window,
1171                       self->area.x, self->area.y,
1172                       self->area.width - self->bwidth * 2,
1173                       self->area.height - self->bwidth * 2);
1174     XFlush(ob_display);
1175 }
1176
1177 void frame_begin_iconify_animation(ObFrame *self, gboolean iconifying)
1178 {
1179     gulong time;
1180     gboolean new_anim = FALSE;
1181     gboolean set_end = TRUE;
1182     GTimeVal now;
1183
1184     /* if there is no titlebar, just don't animate for now
1185        XXX it would be nice tho.. */
1186     if (!(self->decorations & OB_FRAME_DECOR_TITLEBAR))
1187         return;
1188
1189     /* get the current time */
1190     g_get_current_time(&now);
1191
1192     /* get how long until the end */
1193     time = FRAME_ANIMATE_ICONIFY_TIME;
1194     if (self->iconify_animation_going) {
1195         if (!!iconifying != (self->iconify_animation_going > 0)) {
1196             /* animation was already going on in the opposite direction */
1197             time = time - frame_animate_iconify_time_left(self, &now);
1198         } else
1199             /* animation was already going in the same direction */
1200             set_end = FALSE;
1201     } else
1202         new_anim = TRUE;
1203     self->iconify_animation_going = iconifying ? 1 : -1;
1204
1205     /* set the ending time */
1206     if (set_end) {
1207         self->iconify_animation_end.tv_sec = now.tv_sec;
1208         self->iconify_animation_end.tv_usec = now.tv_usec;
1209         g_time_val_add(&self->iconify_animation_end, time);
1210     }
1211
1212     if (new_anim) {
1213         ob_main_loop_timeout_remove_data(ob_main_loop, frame_animate_iconify,
1214                                          self, FALSE);
1215         ob_main_loop_timeout_add(ob_main_loop,
1216                                  FRAME_ANIMATE_ICONIFY_STEP_TIME,
1217                                  frame_animate_iconify, self,
1218                                  g_direct_equal, NULL);
1219
1220         /* do the first step */
1221         frame_animate_iconify(self);
1222
1223         /* show it during the animation even if it is not "visible" */
1224         if (!self->visible)
1225             XMapWindow(ob_display, self->window);
1226     }
1227 }