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