]> icculus.org git repositories - dana/openbox.git/blob - openbox/frame.c
dont make two animations conflict
[dana/openbox.git] / openbox / frame.c
1 /* -*- indent-tabs-mode: nil; tab-width: 4; c-basic-offset: 4; -*-
2
3    frame.c for the Openbox window manager
4    Copyright (c) 2006        Mikael Magnusson
5    Copyright (c) 2003-2007   Dana Jansens
6
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 2 of the License, or
10    (at your option) any later version.
11
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16
17    See the COPYING file for a copy of the GNU General Public License.
18 */
19
20 #include "frame.h"
21 #include "client.h"
22 #include "openbox.h"
23 #include "extensions.h"
24 #include "prop.h"
25 #include "config.h"
26 #include "framerender.h"
27 #include "mainloop.h"
28 #include "focus.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     if (win == client->window) {
842         /* conceptually, this is the desktop, as far as users are
843            concerned */
844         if (client->type == OB_CLIENT_TYPE_DESKTOP)
845             return OB_FRAME_CONTEXT_DESKTOP;
846         return OB_FRAME_CONTEXT_CLIENT;
847     }
848
849     self = client->frame;
850     if (win == self->inner || win == self->plate) {
851         /* conceptually, this is the desktop, as far as users are
852            concerned */
853         if (client->type == OB_CLIENT_TYPE_DESKTOP)
854             return OB_FRAME_CONTEXT_DESKTOP;
855         return OB_FRAME_CONTEXT_CLIENT;
856     }
857
858     if (win == self->window)    return OB_FRAME_CONTEXT_FRAME;
859     if (win == self->title)     return OB_FRAME_CONTEXT_TITLEBAR;
860     if (win == self->label)     return OB_FRAME_CONTEXT_TITLEBAR;
861     if (win == self->handle)    return OB_FRAME_CONTEXT_HANDLE;
862     if (win == self->lgrip)     return OB_FRAME_CONTEXT_BLCORNER;
863     if (win == self->rgrip)     return OB_FRAME_CONTEXT_BRCORNER;
864     if (win == self->tltresize) return OB_FRAME_CONTEXT_TLCORNER;
865     if (win == self->tllresize) return OB_FRAME_CONTEXT_TLCORNER;
866     if (win == self->trtresize) return OB_FRAME_CONTEXT_TRCORNER;
867     if (win == self->trrresize) return OB_FRAME_CONTEXT_TRCORNER;
868     if (win == self->max)       return OB_FRAME_CONTEXT_MAXIMIZE;
869     if (win == self->iconify)   return OB_FRAME_CONTEXT_ICONIFY;
870     if (win == self->close)     return OB_FRAME_CONTEXT_CLOSE;
871     if (win == self->icon)      return OB_FRAME_CONTEXT_ICON;
872     if (win == self->desk)      return OB_FRAME_CONTEXT_ALLDESKTOPS;
873     if (win == self->shade)     return OB_FRAME_CONTEXT_SHADE;
874
875     return OB_FRAME_CONTEXT_NONE;
876 }
877
878 void frame_client_gravity(ObFrame *self, gint *x, gint *y, gint w, gint h)
879 {
880     /* horizontal */
881     switch (self->client->gravity) {
882     default:
883     case NorthWestGravity:
884     case SouthWestGravity:
885     case WestGravity:
886         break;
887
888     case NorthGravity:
889     case SouthGravity:
890     case CenterGravity:
891         *x -= (self->size.left + w) / 2;
892         break;
893
894     case NorthEastGravity:
895     case SouthEastGravity:
896     case EastGravity:
897         *x -= (self->size.left + self->size.right + w) - 1;
898         break;
899
900     case ForgetGravity:
901     case StaticGravity:
902         *x -= self->size.left;
903         break;
904     }
905
906     /* vertical */
907     switch (self->client->gravity) {
908     default:
909     case NorthWestGravity:
910     case NorthEastGravity:
911     case NorthGravity:
912         break;
913
914     case CenterGravity:
915     case EastGravity:
916     case WestGravity:
917         *y -= (self->size.top + h) / 2;
918         break;
919
920     case SouthWestGravity:
921     case SouthEastGravity:
922     case SouthGravity:
923         *y -= (self->size.top + self->size.bottom + h) - 1;
924         break;
925
926     case ForgetGravity:
927     case StaticGravity:
928         *y -= self->size.top;
929         break;
930     }
931 }
932
933 void frame_frame_gravity(ObFrame *self, gint *x, gint *y, gint w, gint h)
934 {
935     /* horizontal */
936     switch (self->client->gravity) {
937     default:
938     case NorthWestGravity:
939     case WestGravity:
940     case SouthWestGravity:
941         break;
942     case NorthGravity:
943     case CenterGravity:
944     case SouthGravity:
945         *x += (self->size.left + w) / 2;
946         break;
947     case NorthEastGravity:
948     case EastGravity:
949     case SouthEastGravity:
950         *x += (self->size.left + self->size.right + w) - 1;
951         break;
952     case StaticGravity:
953     case ForgetGravity:
954         *x += self->size.left;
955         break;
956     }
957
958     /* vertical */
959     switch (self->client->gravity) {
960     default:
961     case NorthWestGravity:
962     case NorthGravity:
963     case NorthEastGravity:
964         break;
965     case WestGravity:
966     case CenterGravity:
967     case EastGravity:
968         *y += (self->size.top + h) / 2;
969         break;
970     case SouthWestGravity:
971     case SouthGravity:
972     case SouthEastGravity:
973         *y += (self->size.top + self->size.bottom + h) - 1;
974         break;
975     case StaticGravity:
976     case ForgetGravity:
977         *y += self->size.top;
978         break;
979     }
980 }
981
982 static void flash_done(gpointer data)
983 {
984     ObFrame *self = data;
985
986     if (self->focused != self->flash_on)
987         frame_adjust_focus(self, self->focused);
988 }
989
990 static gboolean flash_timeout(gpointer data)
991 {
992     ObFrame *self = data;
993     GTimeVal now;
994
995     g_get_current_time(&now);
996     if (now.tv_sec > self->flash_end.tv_sec ||
997         (now.tv_sec == self->flash_end.tv_sec &&
998          now.tv_usec >= self->flash_end.tv_usec))
999         self->flashing = FALSE;
1000
1001     if (!self->flashing)
1002         return FALSE; /* we are done */
1003
1004     self->flash_on = !self->flash_on;
1005     if (!self->focused) {
1006         frame_adjust_focus(self, self->flash_on);
1007         self->focused = FALSE;
1008     }
1009
1010     return TRUE; /* go again */
1011 }
1012
1013 void frame_flash_start(ObFrame *self)
1014 {
1015     self->flash_on = self->focused;
1016
1017     if (!self->flashing)
1018         ob_main_loop_timeout_add(ob_main_loop,
1019                                  G_USEC_PER_SEC * 0.6,
1020                                  flash_timeout,
1021                                  self,
1022                                  g_direct_equal,
1023                                  flash_done);
1024     g_get_current_time(&self->flash_end);
1025     g_time_val_add(&self->flash_end, G_USEC_PER_SEC * 5);
1026     
1027     self->flashing = TRUE;
1028 }
1029
1030 void frame_flash_stop(ObFrame *self)
1031 {
1032     self->flashing = FALSE;
1033 }
1034
1035 static gboolean frame_animate_iconify(gpointer p)
1036 {
1037     ObFrame *self = p;
1038     gint step = self->iconify_animation_step;
1039     gint absstep, nextstep;
1040     gint x, y, w, h;
1041     gint iconx, icony, iconw;
1042
1043     if (self->client->icon_geometry.width == 0) {
1044         /* there is no icon geometry set so just go straight down */
1045         Rect *a = screen_physical_area();
1046         iconx = self->area.x + self->area.width / 2 + 32;
1047         icony = a->y + a->width;
1048         iconw = 64;
1049     } else {
1050         iconx = self->client->icon_geometry.x;
1051         icony = self->client->icon_geometry.y;
1052         iconw = self->client->icon_geometry.width;
1053     }
1054
1055     if (step >= 0)
1056         absstep = FRAME_ANIMATE_ICONIFY_STEPS - step + 1;
1057     else
1058         absstep = FRAME_ANIMATE_ICONIFY_STEPS + step + 1;
1059
1060     if (step >= 0) {
1061         /* start where the frame is supposed to be */
1062         x = self->area.x;
1063         y = self->area.y;
1064         w = self->area.width - self->bwidth * 2;
1065         h = self->area.height - self->bwidth * 2;
1066     } else {
1067         /* start at the icon */
1068         x = iconx;
1069         y = icony;
1070         w = iconw;
1071         h = self->innersize.top; /* just the titlebar */
1072     }
1073
1074     if (step != 0) {
1075         gint dx, dy, dw;
1076         dx = self->area.x - iconx;
1077         dy = self->area.y - icony;
1078         dw = self->area.width - self->bwidth * 2 - iconw;
1079          /* if restoring, we move in the opposite direction */
1080         if (step < 0) { dx = -dx; dy = -dy; dw = -dw; }
1081         x = x - dx / FRAME_ANIMATE_ICONIFY_STEPS * absstep;
1082         y = y - dy / FRAME_ANIMATE_ICONIFY_STEPS * absstep;
1083         w = w - dw / FRAME_ANIMATE_ICONIFY_STEPS * absstep;
1084         h = self->innersize.top; /* just the titlebar */
1085     }
1086
1087     /* move one step forward */
1088     self->iconify_animation_step = step + (step < 0 ? 1 : (step > 0 ? -1 : 0));
1089
1090     /* call the callback when it's done */
1091     if (step == 0 && self->iconify_animation_cb)
1092             self->iconify_animation_cb(self->iconify_animation_data);
1093
1094     /* move to the next spot (after the callback for the animation ending) */
1095     XMoveResizeWindow(ob_display, self->window, x, y, w, h);
1096     XFlush(ob_display);
1097
1098     return step != 0; /* repeat until step is 0 */
1099 }
1100
1101 void frame_begin_iconify_animation(ObFrame *self, gboolean iconifying,
1102                                    ObFrameIconifyAnimateFunc callback,
1103                                    gpointer data)
1104 {
1105     if (iconifying) {
1106         if (self->iconify_animation_step == 0) /* wasnt doing anything */
1107             self->iconify_animation_step = FRAME_ANIMATE_ICONIFY_STEPS;
1108         else if (self->iconify_animation_step < 0) /* was deiconifying */
1109             self->iconify_animation_step =
1110                 FRAME_ANIMATE_ICONIFY_STEPS + self->iconify_animation_step;
1111     } else {
1112         if (self->iconify_animation_step == 0) /* wasnt doing anything */
1113             self->iconify_animation_step = -FRAME_ANIMATE_ICONIFY_STEPS;
1114         else if (self->iconify_animation_step > 0) /* was iconifying */
1115             self->iconify_animation_step =
1116                 -FRAME_ANIMATE_ICONIFY_STEPS + self->iconify_animation_step;
1117     }
1118     self->iconify_animation_cb = callback;
1119     self->iconify_animation_data = data;
1120
1121     if (self->iconify_animation_step == FRAME_ANIMATE_ICONIFY_STEPS ||
1122         self->iconify_animation_step == -FRAME_ANIMATE_ICONIFY_STEPS)
1123     {
1124         ob_main_loop_timeout_remove_data(ob_main_loop, frame_animate_iconify,
1125                                          self, FALSE);
1126         ob_main_loop_timeout_add(ob_main_loop,
1127                                  FRAME_ANIMATE_ICONIFY_TIME /
1128                                  FRAME_ANIMATE_ICONIFY_STEPS,
1129                                  frame_animate_iconify, self,
1130                                  g_direct_equal, NULL);
1131         /* do the first step */
1132         frame_animate_iconify(self);
1133     }
1134 }