]> icculus.org git repositories - dana/openbox.git/blob - openbox/frame.c
animate iconify/reestore. yeah.
[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/10)
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(ob_main_loop, frame_animate_iconify);
605
606     /* check if the app has already reparented its window away */
607     while (XCheckTypedWindowEvent(ob_display, client->window,
608                                   ReparentNotify, &ev))
609     {
610         /* This check makes sure we don't catch our own reparent action to
611            our frame window. This doesn't count as the app reparenting itself
612            away of course.
613
614            Reparent events that are generated by us are just discarded here.
615            They are of no consequence to us anyhow.
616         */
617         if (ev.xreparent.parent != self->plate) {
618             reparent = FALSE;
619             XPutBackEvent(ob_display, &ev);
620             break;
621         }
622     }
623
624     if (reparent) {
625         /* according to the ICCCM - if the client doesn't reparent itself,
626            then we will reparent the window to root for them */
627         XReparentWindow(ob_display, client->window,
628                         RootWindow(ob_display, ob_screen),
629                         client->area.x,
630                         client->area.y);
631     }
632
633     /* remove all the windows for the frame from the window_map */
634     g_hash_table_remove(window_map, &self->window);
635     g_hash_table_remove(window_map, &self->plate);
636     g_hash_table_remove(window_map, &self->inner);
637     g_hash_table_remove(window_map, &self->title);
638     g_hash_table_remove(window_map, &self->label);
639     g_hash_table_remove(window_map, &self->max);
640     g_hash_table_remove(window_map, &self->close);
641     g_hash_table_remove(window_map, &self->desk);
642     g_hash_table_remove(window_map, &self->shade);
643     g_hash_table_remove(window_map, &self->icon);
644     g_hash_table_remove(window_map, &self->iconify);
645     g_hash_table_remove(window_map, &self->handle);
646     g_hash_table_remove(window_map, &self->lgrip);
647     g_hash_table_remove(window_map, &self->rgrip);
648     g_hash_table_remove(window_map, &self->tltresize);
649     g_hash_table_remove(window_map, &self->tllresize);
650     g_hash_table_remove(window_map, &self->trtresize);
651     g_hash_table_remove(window_map, &self->trrresize);
652
653     ob_main_loop_timeout_remove_data(ob_main_loop, flash_timeout, self, TRUE);
654
655     frame_free(self);
656 }
657
658 static void layout_title(ObFrame *self)
659 {
660     gchar *lc;
661     gint x;
662     gboolean n, d, i, l, m, c, s;
663
664     n = d = i = l = m = c = s = FALSE;
665
666     /* figure out whats being shown, and the width of the label */
667     self->label_width = self->width - (ob_rr_theme->paddingx + 1) * 2;
668     for (lc = config_title_layout; *lc != '\0'; ++lc) {
669         switch (*lc) {
670         case 'N':
671             if (n) { *lc = ' '; break; } /* rm duplicates */
672             n = TRUE;
673             self->label_width -= (ob_rr_theme->button_size + 2 +
674                                   ob_rr_theme->paddingx + 1);
675             break;
676         case 'D':
677             if (d) { *lc = ' '; break; }
678             if (!(self->decorations & OB_FRAME_DECOR_ALLDESKTOPS)
679                 && config_theme_hidedisabled)
680                 break;
681             d = TRUE;
682             self->label_width -= (ob_rr_theme->button_size +
683                                   ob_rr_theme->paddingx + 1);
684             break;
685         case 'S':
686             if (s) { *lc = ' '; break; }
687             if (!(self->decorations & OB_FRAME_DECOR_SHADE)
688                 && config_theme_hidedisabled)
689                 break;
690             s = TRUE;
691             self->label_width -= (ob_rr_theme->button_size +
692                                   ob_rr_theme->paddingx + 1);
693             break;
694         case 'I':
695             if (i) { *lc = ' '; break; }
696             if (!(self->decorations & OB_FRAME_DECOR_ICONIFY)
697                 && config_theme_hidedisabled)
698                 break;
699             i = TRUE;
700             self->label_width -= (ob_rr_theme->button_size +
701                                   ob_rr_theme->paddingx + 1);
702             break;
703         case 'L':
704             if (l) { *lc = ' '; break; }
705             l = TRUE;
706             break;
707         case 'M':
708             if (m) { *lc = ' '; break; }
709             if (!(self->decorations & OB_FRAME_DECOR_MAXIMIZE)
710                 && config_theme_hidedisabled)
711                 break;
712             m = TRUE;
713             self->label_width -= (ob_rr_theme->button_size +
714                                   ob_rr_theme->paddingx + 1);
715             break;
716         case 'C':
717             if (c) { *lc = ' '; break; }
718             if (!(self->decorations & OB_FRAME_DECOR_CLOSE)
719                 && config_theme_hidedisabled)
720                 break;
721             c = TRUE;
722             self->label_width -= (ob_rr_theme->button_size +
723                                   ob_rr_theme->paddingx + 1);
724             break;
725         }
726     }
727     if (self->label_width < 1) self->label_width = 1;
728
729     if (!n) XUnmapWindow(ob_display, self->icon);
730     if (!d) XUnmapWindow(ob_display, self->desk);
731     if (!s) XUnmapWindow(ob_display, self->shade);
732     if (!i) XUnmapWindow(ob_display, self->iconify);
733     if (!l) XUnmapWindow(ob_display, self->label);
734     if (!m) XUnmapWindow(ob_display, self->max);
735     if (!c) XUnmapWindow(ob_display, self->close);
736
737     x = ob_rr_theme->paddingx + 1;
738     for (lc = config_title_layout; *lc != '\0'; ++lc) {
739         switch (*lc) {
740         case 'N':
741             if (!n) break;
742             self->icon_x = x;
743             XMapWindow(ob_display, self->icon);
744             XMoveWindow(ob_display, self->icon, x, ob_rr_theme->paddingy);
745             x += ob_rr_theme->button_size + 2 + ob_rr_theme->paddingx + 1;
746             break;
747         case 'D':
748             if (!d) break;
749             self->desk_x = x;
750             XMapWindow(ob_display, self->desk);
751             XMoveWindow(ob_display, self->desk, x, ob_rr_theme->paddingy + 1);
752             x += ob_rr_theme->button_size + ob_rr_theme->paddingx + 1;
753             break;
754         case 'S':
755             if (!s) break;
756             self->shade_x = x;
757             XMapWindow(ob_display, self->shade);
758             XMoveWindow(ob_display, self->shade, x, ob_rr_theme->paddingy + 1);
759             x += ob_rr_theme->button_size + ob_rr_theme->paddingx + 1;
760             break;
761         case 'I':
762             if (!i) break;
763             self->iconify_x = x;
764             XMapWindow(ob_display, self->iconify);
765             XMoveWindow(ob_display,self->iconify, x, ob_rr_theme->paddingy + 1);
766             x += ob_rr_theme->button_size + ob_rr_theme->paddingx + 1;
767             break;
768         case 'L':
769             if (!l) break;
770             self->label_x = x;
771             XMapWindow(ob_display, self->label);
772             XMoveWindow(ob_display, self->label, x, ob_rr_theme->paddingy);
773             x += self->label_width + ob_rr_theme->paddingx + 1;
774             break;
775         case 'M':
776             if (!m) break;
777             self->max_x = x;
778             XMapWindow(ob_display, self->max);
779             XMoveWindow(ob_display, self->max, x, ob_rr_theme->paddingy + 1);
780             x += ob_rr_theme->button_size + ob_rr_theme->paddingx + 1;
781             break;
782         case 'C':
783             if (!c) break;
784             self->close_x = x;
785             XMapWindow(ob_display, self->close);
786             XMoveWindow(ob_display, self->close, x, ob_rr_theme->paddingy + 1);
787             x += ob_rr_theme->button_size + ob_rr_theme->paddingx + 1;
788             break;
789         }
790     }
791 }
792
793 ObFrameContext frame_context_from_string(const gchar *name)
794 {
795     if (!g_ascii_strcasecmp("Desktop", name))
796         return OB_FRAME_CONTEXT_DESKTOP;
797     else if (!g_ascii_strcasecmp("Client", name))
798         return OB_FRAME_CONTEXT_CLIENT;
799     else if (!g_ascii_strcasecmp("Titlebar", name))
800         return OB_FRAME_CONTEXT_TITLEBAR;
801     else if (!g_ascii_strcasecmp("Handle", name))
802         return OB_FRAME_CONTEXT_HANDLE;
803     else if (!g_ascii_strcasecmp("Frame", name))
804         return OB_FRAME_CONTEXT_FRAME;
805     else if (!g_ascii_strcasecmp("TLCorner", name))
806         return OB_FRAME_CONTEXT_TLCORNER;
807     else if (!g_ascii_strcasecmp("TRCorner", name))
808         return OB_FRAME_CONTEXT_TRCORNER;
809     else if (!g_ascii_strcasecmp("BLCorner", name))
810         return OB_FRAME_CONTEXT_BLCORNER;
811     else if (!g_ascii_strcasecmp("BRCorner", name))
812         return OB_FRAME_CONTEXT_BRCORNER;
813     else if (!g_ascii_strcasecmp("Maximize", name))
814         return OB_FRAME_CONTEXT_MAXIMIZE;
815     else if (!g_ascii_strcasecmp("AllDesktops", name))
816         return OB_FRAME_CONTEXT_ALLDESKTOPS;
817     else if (!g_ascii_strcasecmp("Shade", name))
818         return OB_FRAME_CONTEXT_SHADE;
819     else if (!g_ascii_strcasecmp("Iconify", name))
820         return OB_FRAME_CONTEXT_ICONIFY;
821     else if (!g_ascii_strcasecmp("Icon", name))
822         return OB_FRAME_CONTEXT_ICON;
823     else if (!g_ascii_strcasecmp("Close", name))
824         return OB_FRAME_CONTEXT_CLOSE;
825     else if (!g_ascii_strcasecmp("MoveResize", name))
826         return OB_FRAME_CONTEXT_MOVE_RESIZE;
827     return OB_FRAME_CONTEXT_NONE;
828 }
829
830 ObFrameContext frame_context(ObClient *client, Window win)
831 {
832     ObFrame *self;
833
834     if (moveresize_in_progress)
835         return OB_FRAME_CONTEXT_MOVE_RESIZE;
836
837     if (win == RootWindow(ob_display, ob_screen))
838         return OB_FRAME_CONTEXT_DESKTOP;
839     if (client == NULL) return OB_FRAME_CONTEXT_NONE;
840     if (win == client->window) {
841         /* conceptually, this is the desktop, as far as users are
842            concerned */
843         if (client->type == OB_CLIENT_TYPE_DESKTOP)
844             return OB_FRAME_CONTEXT_DESKTOP;
845         return OB_FRAME_CONTEXT_CLIENT;
846     }
847
848     self = client->frame;
849     if (win == self->inner || win == self->plate) {
850         /* conceptually, this is the desktop, as far as users are
851            concerned */
852         if (client->type == OB_CLIENT_TYPE_DESKTOP)
853             return OB_FRAME_CONTEXT_DESKTOP;
854         return OB_FRAME_CONTEXT_CLIENT;
855     }
856
857     if (win == self->window)    return OB_FRAME_CONTEXT_FRAME;
858     if (win == self->title)     return OB_FRAME_CONTEXT_TITLEBAR;
859     if (win == self->label)     return OB_FRAME_CONTEXT_TITLEBAR;
860     if (win == self->handle)    return OB_FRAME_CONTEXT_HANDLE;
861     if (win == self->lgrip)     return OB_FRAME_CONTEXT_BLCORNER;
862     if (win == self->rgrip)     return OB_FRAME_CONTEXT_BRCORNER;
863     if (win == self->tltresize) return OB_FRAME_CONTEXT_TLCORNER;
864     if (win == self->tllresize) return OB_FRAME_CONTEXT_TLCORNER;
865     if (win == self->trtresize) return OB_FRAME_CONTEXT_TRCORNER;
866     if (win == self->trrresize) return OB_FRAME_CONTEXT_TRCORNER;
867     if (win == self->max)       return OB_FRAME_CONTEXT_MAXIMIZE;
868     if (win == self->iconify)   return OB_FRAME_CONTEXT_ICONIFY;
869     if (win == self->close)     return OB_FRAME_CONTEXT_CLOSE;
870     if (win == self->icon)      return OB_FRAME_CONTEXT_ICON;
871     if (win == self->desk)      return OB_FRAME_CONTEXT_ALLDESKTOPS;
872     if (win == self->shade)     return OB_FRAME_CONTEXT_SHADE;
873
874     return OB_FRAME_CONTEXT_NONE;
875 }
876
877 void frame_client_gravity(ObFrame *self, gint *x, gint *y, gint w, gint h)
878 {
879     /* horizontal */
880     switch (self->client->gravity) {
881     default:
882     case NorthWestGravity:
883     case SouthWestGravity:
884     case WestGravity:
885         break;
886
887     case NorthGravity:
888     case SouthGravity:
889     case CenterGravity:
890         *x -= (self->size.left + w) / 2;
891         break;
892
893     case NorthEastGravity:
894     case SouthEastGravity:
895     case EastGravity:
896         *x -= (self->size.left + self->size.right + w) - 1;
897         break;
898
899     case ForgetGravity:
900     case StaticGravity:
901         *x -= self->size.left;
902         break;
903     }
904
905     /* vertical */
906     switch (self->client->gravity) {
907     default:
908     case NorthWestGravity:
909     case NorthEastGravity:
910     case NorthGravity:
911         break;
912
913     case CenterGravity:
914     case EastGravity:
915     case WestGravity:
916         *y -= (self->size.top + h) / 2;
917         break;
918
919     case SouthWestGravity:
920     case SouthEastGravity:
921     case SouthGravity:
922         *y -= (self->size.top + self->size.bottom + h) - 1;
923         break;
924
925     case ForgetGravity:
926     case StaticGravity:
927         *y -= self->size.top;
928         break;
929     }
930 }
931
932 void frame_frame_gravity(ObFrame *self, gint *x, gint *y, gint w, gint h)
933 {
934     /* horizontal */
935     switch (self->client->gravity) {
936     default:
937     case NorthWestGravity:
938     case WestGravity:
939     case SouthWestGravity:
940         break;
941     case NorthGravity:
942     case CenterGravity:
943     case SouthGravity:
944         *x += (self->size.left + w) / 2;
945         break;
946     case NorthEastGravity:
947     case EastGravity:
948     case SouthEastGravity:
949         *x += (self->size.left + self->size.right + w) - 1;
950         break;
951     case StaticGravity:
952     case ForgetGravity:
953         *x += self->size.left;
954         break;
955     }
956
957     /* vertical */
958     switch (self->client->gravity) {
959     default:
960     case NorthWestGravity:
961     case NorthGravity:
962     case NorthEastGravity:
963         break;
964     case WestGravity:
965     case CenterGravity:
966     case EastGravity:
967         *y += (self->size.top + h) / 2;
968         break;
969     case SouthWestGravity:
970     case SouthGravity:
971     case SouthEastGravity:
972         *y += (self->size.top + self->size.bottom + h) - 1;
973         break;
974     case StaticGravity:
975     case ForgetGravity:
976         *y += self->size.top;
977         break;
978     }
979 }
980
981 static void flash_done(gpointer data)
982 {
983     ObFrame *self = data;
984
985     if (self->focused != self->flash_on)
986         frame_adjust_focus(self, self->focused);
987 }
988
989 static gboolean flash_timeout(gpointer data)
990 {
991     ObFrame *self = data;
992     GTimeVal now;
993
994     g_get_current_time(&now);
995     if (now.tv_sec > self->flash_end.tv_sec ||
996         (now.tv_sec == self->flash_end.tv_sec &&
997          now.tv_usec >= self->flash_end.tv_usec))
998         self->flashing = FALSE;
999
1000     if (!self->flashing)
1001         return FALSE; /* we are done */
1002
1003     self->flash_on = !self->flash_on;
1004     if (!self->focused) {
1005         frame_adjust_focus(self, self->flash_on);
1006         self->focused = FALSE;
1007     }
1008
1009     return TRUE; /* go again */
1010 }
1011
1012 void frame_flash_start(ObFrame *self)
1013 {
1014     self->flash_on = self->focused;
1015
1016     if (!self->flashing)
1017         ob_main_loop_timeout_add(ob_main_loop,
1018                                  G_USEC_PER_SEC * 0.6,
1019                                  flash_timeout,
1020                                  self,
1021                                  g_direct_equal,
1022                                  flash_done);
1023     g_get_current_time(&self->flash_end);
1024     g_time_val_add(&self->flash_end, G_USEC_PER_SEC * 5);
1025     
1026     self->flashing = TRUE;
1027 }
1028
1029 void frame_flash_stop(ObFrame *self)
1030 {
1031     self->flashing = FALSE;
1032 }
1033
1034 static gboolean frame_animate_iconify(gpointer p)
1035 {
1036     ObFrame *self = p;
1037     gint step = self->iconify_animation_step;
1038     gint absstep, nextstep;
1039     gint x, y, w, h;
1040     gint iconx, icony, iconw;
1041
1042     if (self->client->icon_geometry.width == 0) {
1043         /* there is no icon geometry set so just go straight down */
1044         Rect *a = screen_physical_area();
1045         iconx = self->area.x + self->area.width / 2 + 32;
1046         icony = a->y + a->width;
1047         iconw = 64;
1048     } else {
1049         iconx = self->client->icon_geometry.x;
1050         icony = self->client->icon_geometry.y;
1051         iconw = self->client->icon_geometry.width;
1052     }
1053
1054     if (step >= 0)
1055         absstep = FRAME_ANIMATE_ICONIFY_STEPS - step + 1;
1056     else
1057         absstep = FRAME_ANIMATE_ICONIFY_STEPS + step + 1;
1058
1059     if (step >= 0) {
1060         /* start where the frame is supposed to be */
1061         x = self->area.x;
1062         y = self->area.y;
1063         w = self->area.width - self->bwidth * 2;
1064         h = self->area.height - self->bwidth * 2;
1065     } else {
1066         /* start at the icon */
1067         x = iconx;
1068         y = icony;
1069         w = iconw;
1070         h = self->innersize.top; /* just the titlebar */
1071     }
1072
1073     if (step != 0) {
1074         gint dx, dy, dw;
1075         dx = self->area.x - iconx;
1076         dy = self->area.y - icony;
1077         dw = self->area.width - self->bwidth * 2 - iconw;
1078          /* if restoring, we move in the opposite direction */
1079         if (step < 0) { dx = -dx; dy = -dy; dw = -dw; }
1080         x = x - dx / FRAME_ANIMATE_ICONIFY_STEPS * absstep;
1081         y = y - dy / FRAME_ANIMATE_ICONIFY_STEPS * absstep;
1082         w = w - dw / FRAME_ANIMATE_ICONIFY_STEPS * absstep;
1083         h = self->innersize.top; /* just the titlebar */
1084     }
1085
1086     /* move one step forward */
1087     self->iconify_animation_step = step + (step < 0 ? 1 : (step > 0 ? -1 : 0));
1088
1089     /* call the callback when it's done */
1090     if (step == 0 && self->iconify_animation_cb)
1091             self->iconify_animation_cb(self->iconify_animation_data);
1092
1093     /* move to the next spot (after the callback for the animation ending) */
1094     XMoveResizeWindow(ob_display, self->window, x, y, w, h);
1095     XFlush(ob_display);
1096
1097     return step != 0; /* repeat until step is 0 */
1098 }
1099
1100 void frame_begin_iconify_animation(ObFrame *self, gboolean iconifying,
1101                                    ObFrameIconifyAnimateFunc callback,
1102                                    gpointer data)
1103 {
1104     if (iconifying) {
1105         if (self->iconify_animation_step == 0) /* wasnt doing anything */
1106             self->iconify_animation_step = FRAME_ANIMATE_ICONIFY_STEPS;
1107         else if (self->iconify_animation_step < 0) /* was deiconifying */
1108             self->iconify_animation_step =
1109                 FRAME_ANIMATE_ICONIFY_STEPS + self->iconify_animation_step;
1110     } else {
1111         if (self->iconify_animation_step == 0) /* wasnt doing anything */
1112             self->iconify_animation_step = -FRAME_ANIMATE_ICONIFY_STEPS;
1113         else if (self->iconify_animation_step > 0) /* was iconifying */
1114             self->iconify_animation_step =
1115                 -FRAME_ANIMATE_ICONIFY_STEPS + self->iconify_animation_step;
1116     }
1117     self->iconify_animation_cb = callback;
1118     self->iconify_animation_data = data;
1119
1120     if (self->iconify_animation_step == FRAME_ANIMATE_ICONIFY_STEPS ||
1121         self->iconify_animation_step == -FRAME_ANIMATE_ICONIFY_STEPS)
1122     {
1123         ob_main_loop_timeout_remove(ob_main_loop, frame_animate_iconify);
1124         ob_main_loop_timeout_add(ob_main_loop,
1125                                  FRAME_ANIMATE_ICONIFY_TIME /
1126                                  FRAME_ANIMATE_ICONIFY_STEPS,
1127                                  frame_animate_iconify, self,
1128                                  g_direct_equal, NULL);
1129         /* do the first step */
1130         frame_animate_iconify(self);
1131     }
1132 }