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