]> icculus.org git repositories - mikachu/openbox.git/blob - openbox/frame.c
lots of fixes for the iconify animation. i think it should all work perfectly now ?
[mikachu/openbox.git] / openbox / frame.c
1 /* -*- indent-tabs-mode: nil; tab-width: 4; c-basic-offset: 4; -*-
2
3    frame.c for the Openbox window manager
4    Copyright (c) 2006        Mikael Magnusson
5    Copyright (c) 2003-2007   Dana Jansens
6
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 2 of the License, or
10    (at your option) any later version.
11
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16
17    See the COPYING file for a copy of the GNU General Public License.
18 */
19
20 #include "frame.h"
21 #include "client.h"
22 #include "openbox.h"
23 #include "extensions.h"
24 #include "prop.h"
25 #include "config.h"
26 #include "framerender.h"
27 #include "mainloop.h"
28 #include "focus.h"
29 #include "moveresize.h"
30 #include "screen.h"
31 #include "render/theme.h"
32
33 #define PLATE_EVENTMASK (SubstructureRedirectMask | FocusChangeMask)
34 #define FRAME_EVENTMASK (EnterWindowMask | LeaveWindowMask | \
35                          ButtonPressMask | ButtonReleaseMask)
36 #define ELEMENT_EVENTMASK (ButtonPressMask | ButtonReleaseMask | \
37                            ButtonMotionMask | \
38                            EnterWindowMask | LeaveWindowMask)
39 /* The inner window does not need enter/leave events.
40    If it does get them, then it needs its own context for enter events
41    because sloppy focus will focus the window when you enter the inner window
42    from the frame. */
43 #define INNER_EVENTMASK (ButtonPressMask)
44
45 //#define FRAME_ANIMATE_ICONIFY_TIME 150000 /* .15 seconds */
46 #define FRAME_ANIMATE_ICONIFY_TIME 2000000
47 #define FRAME_ANIMATE_ICONIFY_STEP_TIME (G_USEC_PER_SEC / 30) /* 30 Hz */
48
49 #define FRAME_HANDLE_Y(f) (f->innersize.top + f->client->area.height + \
50                            f->cbwidth_y)
51
52 static void layout_title(ObFrame *self);
53 static void flash_done(gpointer data);
54 static gboolean flash_timeout(gpointer data);
55
56 static void set_theme_statics(ObFrame *self);
57 static void free_theme_statics(ObFrame *self);
58 static gboolean frame_animate_iconify(gpointer self);
59
60 static Window createWindow(Window parent, Visual *visual,
61                            gulong mask, XSetWindowAttributes *attrib)
62 {
63     return XCreateWindow(ob_display, parent, 0, 0, 1, 1, 0,
64                          (visual ? 32 : RrDepth(ob_rr_inst)), InputOutput,
65                          (visual ? visual : RrVisual(ob_rr_inst)),
66                          mask, attrib);
67                        
68 }
69
70 static Visual *check_32bit_client(ObClient *c)
71 {
72     XWindowAttributes wattrib;
73     Status ret;
74
75     ret = XGetWindowAttributes(ob_display, c->window, &wattrib);
76     g_assert(ret != BadDrawable);
77     g_assert(ret != BadWindow);
78
79     if (wattrib.depth == 32)
80         return wattrib.visual;
81     return NULL;
82 }
83
84 ObFrame *frame_new(ObClient *client)
85 {
86     XSetWindowAttributes attrib;
87     gulong mask;
88     ObFrame *self;
89     Visual *visual;
90
91     self = g_new0(ObFrame, 1);
92
93     visual = check_32bit_client(client);
94
95     /* create the non-visible decor windows */
96
97     mask = CWEventMask;
98     if (visual) {
99         /* client has a 32-bit visual */
100         mask |= CWColormap | CWBackPixel | CWBorderPixel;
101         /* create a colormap with the visual */
102         self->colormap = attrib.colormap =
103             XCreateColormap(ob_display,
104                             RootWindow(ob_display, ob_screen),
105                             visual, AllocNone);
106         attrib.background_pixel = BlackPixel(ob_display, 0);
107         attrib.border_pixel = BlackPixel(ob_display, 0);
108     }
109     attrib.event_mask = FRAME_EVENTMASK;
110     self->window = createWindow(RootWindow(ob_display, ob_screen), visual,
111                                 mask, &attrib);
112
113     attrib.event_mask = INNER_EVENTMASK;
114     self->inner = createWindow(self->window, visual, mask, &attrib);
115
116     mask &= ~CWEventMask;
117     self->plate = createWindow(self->inner, visual, mask, &attrib);
118
119     /* create the visible decor windows */
120
121     mask = CWEventMask;
122     if (visual) {
123         /* client has a 32-bit visual */
124         mask |= CWColormap | CWBackPixel | CWBorderPixel;
125         attrib.colormap = RrColormap(ob_rr_inst);
126     }
127     attrib.event_mask = ELEMENT_EVENTMASK;
128     self->title = createWindow(self->window, NULL, mask, &attrib);
129
130     mask |= CWCursor;
131     attrib.cursor = ob_cursor(OB_CURSOR_NORTHWEST);
132     self->tltresize = createWindow(self->title, NULL, mask, &attrib);
133     self->tllresize = createWindow(self->title, NULL, mask, &attrib);
134     attrib.cursor = ob_cursor(OB_CURSOR_NORTHEAST);
135     self->trtresize = createWindow(self->title, NULL, mask, &attrib);
136     self->trrresize = createWindow(self->title, NULL, mask, &attrib);
137
138     mask &= ~CWCursor;
139     self->label = createWindow(self->title, NULL, mask, &attrib);
140     self->max = createWindow(self->title, NULL, mask, &attrib);
141     self->close = createWindow(self->title, NULL, mask, &attrib);
142     self->desk = createWindow(self->title, NULL, mask, &attrib);
143     self->shade = createWindow(self->title, NULL, mask, &attrib);
144     self->icon = createWindow(self->title, NULL, mask, &attrib);
145     self->iconify = createWindow(self->title, NULL, mask, &attrib);
146     self->handle = createWindow(self->window, NULL, mask, &attrib);
147
148     mask |= CWCursor;
149     attrib.cursor = ob_cursor(OB_CURSOR_SOUTHWEST);
150     self->lgrip = createWindow(self->handle, NULL, mask, &attrib);
151     attrib.cursor = ob_cursor(OB_CURSOR_SOUTHEAST);
152     self->rgrip = createWindow(self->handle, NULL, mask, &attrib); 
153
154     self->focused = FALSE;
155
156     /* the other stuff is shown based on decor settings */
157     XMapWindow(ob_display, self->plate);
158     XMapWindow(ob_display, self->inner);
159     XMapWindow(ob_display, self->lgrip);
160     XMapWindow(ob_display, self->rgrip);
161     XMapWindow(ob_display, self->label);
162
163     self->max_press = self->close_press = self->desk_press = 
164         self->iconify_press = self->shade_press = FALSE;
165     self->max_hover = self->close_hover = self->desk_hover = 
166         self->iconify_hover = self->shade_hover = FALSE;
167
168     set_theme_statics(self);
169
170     return (ObFrame*)self;
171 }
172
173 static void set_theme_statics(ObFrame *self)
174 {
175     /* set colors/appearance/sizes for stuff that doesn't change */
176     XSetWindowBorder(ob_display, self->window,
177                      RrColorPixel(ob_rr_theme->frame_b_color));
178     XSetWindowBorder(ob_display, self->inner,
179                      RrColorPixel(ob_rr_theme->frame_b_color));
180     XSetWindowBorder(ob_display, self->title,
181                      RrColorPixel(ob_rr_theme->frame_b_color));
182     XSetWindowBorder(ob_display, self->handle,
183                      RrColorPixel(ob_rr_theme->frame_b_color));
184     XSetWindowBorder(ob_display, self->rgrip,
185                      RrColorPixel(ob_rr_theme->frame_b_color));
186     XSetWindowBorder(ob_display, self->lgrip,
187                      RrColorPixel(ob_rr_theme->frame_b_color));
188
189     XResizeWindow(ob_display, self->max,
190                   ob_rr_theme->button_size, ob_rr_theme->button_size);
191     XResizeWindow(ob_display, self->iconify,
192                   ob_rr_theme->button_size, ob_rr_theme->button_size);
193     XResizeWindow(ob_display, self->icon,
194                   ob_rr_theme->button_size + 2, ob_rr_theme->button_size + 2);
195     XResizeWindow(ob_display, self->close,
196                   ob_rr_theme->button_size, ob_rr_theme->button_size);
197     XResizeWindow(ob_display, self->desk,
198                   ob_rr_theme->button_size, ob_rr_theme->button_size);
199     XResizeWindow(ob_display, self->shade,
200                   ob_rr_theme->button_size, ob_rr_theme->button_size);
201     if (ob_rr_theme->handle_height > 0) {
202         XResizeWindow(ob_display, self->lgrip,
203                       ob_rr_theme->grip_width, ob_rr_theme->handle_height);
204         XResizeWindow(ob_display, self->rgrip,
205                       ob_rr_theme->grip_width, ob_rr_theme->handle_height);
206     }
207     XResizeWindow(ob_display, self->tltresize,
208                   ob_rr_theme->grip_width, ob_rr_theme->paddingy + 1);
209     XResizeWindow(ob_display, self->trtresize,
210                   ob_rr_theme->grip_width, ob_rr_theme->paddingy + 1);
211     XResizeWindow(ob_display, self->tllresize,
212                   ob_rr_theme->paddingx + 1, ob_rr_theme->title_height);
213     XResizeWindow(ob_display, self->trrresize,
214                   ob_rr_theme->paddingx + 1, ob_rr_theme->title_height);
215
216     /* set up the dynamic appearances */
217     self->a_unfocused_title = RrAppearanceCopy(ob_rr_theme->a_unfocused_title);
218     self->a_focused_title = RrAppearanceCopy(ob_rr_theme->a_focused_title);
219     self->a_unfocused_label = RrAppearanceCopy(ob_rr_theme->a_unfocused_label);
220     self->a_focused_label = RrAppearanceCopy(ob_rr_theme->a_focused_label);
221     self->a_unfocused_handle =
222         RrAppearanceCopy(ob_rr_theme->a_unfocused_handle);
223     self->a_focused_handle = RrAppearanceCopy(ob_rr_theme->a_focused_handle);
224     self->a_icon = RrAppearanceCopy(ob_rr_theme->a_icon);
225 }
226
227 static void free_theme_statics(ObFrame *self)
228 {
229     RrAppearanceFree(self->a_unfocused_title); 
230     RrAppearanceFree(self->a_focused_title);
231     RrAppearanceFree(self->a_unfocused_label);
232     RrAppearanceFree(self->a_focused_label);
233     RrAppearanceFree(self->a_unfocused_handle);
234     RrAppearanceFree(self->a_focused_handle);
235     RrAppearanceFree(self->a_icon);
236 }
237
238 static void frame_free(ObFrame *self)
239 {
240     free_theme_statics(self);
241
242     XDestroyWindow(ob_display, self->window);
243     if (self->colormap)
244         XFreeColormap(ob_display, self->colormap);
245
246     g_free(self);
247 }
248
249 void frame_show(ObFrame *self)
250 {
251     if (!self->visible) {
252         self->visible = TRUE;
253         XMapWindow(ob_display, self->client->window);
254         XMapWindow(ob_display, self->window);
255     }
256 }
257
258 void frame_hide(ObFrame *self)
259 {
260     if (self->visible) {
261         self->visible = FALSE;
262         self->client->ignore_unmaps += 1;
263         /* we unmap the client itself so that we can get MapRequest
264            events, and because the ICCCM tells us to! */
265         XUnmapWindow(ob_display, self->window);
266         XUnmapWindow(ob_display, self->client->window);
267     }
268 }
269
270 void frame_adjust_theme(ObFrame *self)
271 {
272     free_theme_statics(self);
273     set_theme_statics(self);
274 }
275
276 void frame_adjust_shape(ObFrame *self)
277 {
278 #ifdef SHAPE
279     gint num;
280     XRectangle xrect[2];
281
282     if (!self->client->shaped) {
283         /* clear the shape on the frame window */
284         XShapeCombineMask(ob_display, self->window, ShapeBounding,
285                           self->innersize.left,
286                           self->innersize.top,
287                           None, ShapeSet);
288     } else {
289         /* make the frame's shape match the clients */
290         XShapeCombineShape(ob_display, self->window, ShapeBounding,
291                            self->innersize.left,
292                            self->innersize.top,
293                            self->client->window,
294                            ShapeBounding, ShapeSet);
295
296         num = 0;
297         if (self->decorations & OB_FRAME_DECOR_TITLEBAR) {
298             xrect[0].x = -ob_rr_theme->fbwidth;
299             xrect[0].y = -ob_rr_theme->fbwidth;
300             xrect[0].width = self->width + self->rbwidth * 2;
301             xrect[0].height = ob_rr_theme->title_height +
302                 self->bwidth * 2;
303             ++num;
304         }
305
306         if (self->decorations & OB_FRAME_DECOR_HANDLE) {
307             xrect[1].x = -ob_rr_theme->fbwidth;
308             xrect[1].y = FRAME_HANDLE_Y(self);
309             xrect[1].width = self->width + self->rbwidth * 2;
310             xrect[1].height = ob_rr_theme->handle_height +
311                 self->bwidth * 2;
312             ++num;
313         }
314
315         XShapeCombineRectangles(ob_display, self->window,
316                                 ShapeBounding, 0, 0, xrect, num,
317                                 ShapeUnion, Unsorted);
318     }
319 #endif
320 }
321
322 void frame_adjust_area(ObFrame *self, gboolean moved,
323                        gboolean resized, gboolean fake)
324 {
325     Strut oldsize;
326
327     oldsize = self->size;
328
329     if (resized) {
330         self->decorations = self->client->decorations;
331         self->max_horz = self->client->max_horz;
332
333         if (self->decorations & OB_FRAME_DECOR_BORDER) {
334             self->bwidth = ob_rr_theme->fbwidth;
335             self->cbwidth_x = ob_rr_theme->cbwidthx;
336             self->cbwidth_y = ob_rr_theme->cbwidthy;
337         } else {
338             self->bwidth = self->cbwidth_x = self->cbwidth_y = 0;
339         }
340         self->rbwidth = self->bwidth;
341
342         if (self->max_horz)
343             self->bwidth = self->cbwidth_x = 0;
344
345         STRUT_SET(self->innersize,
346                   self->cbwidth_x,
347                   self->cbwidth_y,
348                   self->cbwidth_x,
349                   self->cbwidth_y);
350         self->width = self->client->area.width + self->cbwidth_x * 2 -
351             (self->max_horz ? self->rbwidth * 2 : 0);
352         self->width = MAX(self->width, 1); /* no lower than 1 */
353
354         /* set border widths */
355         if (!fake) {
356             XSetWindowBorderWidth(ob_display, self->window, self->bwidth);
357             XSetWindowBorderWidth(ob_display, self->inner, self->bwidth);
358             XSetWindowBorderWidth(ob_display, self->title,  self->rbwidth);
359             XSetWindowBorderWidth(ob_display, self->handle, self->rbwidth);
360             XSetWindowBorderWidth(ob_display, self->lgrip,  self->rbwidth);
361             XSetWindowBorderWidth(ob_display, self->rgrip,  self->rbwidth);
362         }
363
364         if (self->decorations & OB_FRAME_DECOR_TITLEBAR)
365             self->innersize.top += ob_rr_theme->title_height + self->rbwidth +
366                 (self->rbwidth - self->bwidth);
367         if (self->decorations & OB_FRAME_DECOR_HANDLE &&
368             ob_rr_theme->handle_height > 0)
369             self->innersize.bottom += ob_rr_theme->handle_height +
370                 self->rbwidth + (self->rbwidth - self->bwidth);
371   
372         /* they all default off, they're turned on in layout_title */
373         self->icon_x = -1;
374         self->desk_x = -1;
375         self->shade_x = -1;
376         self->iconify_x = -1;
377         self->label_x = -1;
378         self->max_x = -1;
379         self->close_x = -1;
380
381         /* position/size and map/unmap all the windows */
382
383         if (!fake) {
384             if (self->decorations & OB_FRAME_DECOR_TITLEBAR) {
385                 XMoveResizeWindow(ob_display, self->title,
386                                   -self->bwidth, -self->bwidth,
387                                   self->width, ob_rr_theme->title_height);
388                 XMapWindow(ob_display, self->title);
389
390                 if (self->decorations & OB_FRAME_DECOR_GRIPS) {
391                     XMoveWindow(ob_display, self->tltresize, 0, 0);
392                     XMoveWindow(ob_display, self->tllresize, 0, 0);
393                     XMoveWindow(ob_display, self->trtresize,
394                                 self->width - ob_rr_theme->grip_width, 0);
395                     XMoveWindow(ob_display, self->trrresize,
396                                 self->width - ob_rr_theme->paddingx - 1, 0);
397                     XMapWindow(ob_display, self->tltresize);
398                     XMapWindow(ob_display, self->tllresize);
399                     XMapWindow(ob_display, self->trtresize);
400                     XMapWindow(ob_display, self->trrresize);
401                 } else {
402                     XUnmapWindow(ob_display, self->tltresize);
403                     XUnmapWindow(ob_display, self->tllresize);
404                     XUnmapWindow(ob_display, self->trtresize);
405                     XUnmapWindow(ob_display, self->trrresize);
406                 }
407             } else
408                 XUnmapWindow(ob_display, self->title);
409         }
410
411         if (self->decorations & OB_FRAME_DECOR_TITLEBAR)
412             /* layout the title bar elements */
413             layout_title(self);
414
415         if (!fake) {
416             if (self->decorations & OB_FRAME_DECOR_HANDLE &&
417                 ob_rr_theme->handle_height > 0)
418             {
419                 XMoveResizeWindow(ob_display, self->handle,
420                                   -self->bwidth, FRAME_HANDLE_Y(self),
421                                   self->width, ob_rr_theme->handle_height);
422                 XMapWindow(ob_display, self->handle);
423
424                 if (self->decorations & OB_FRAME_DECOR_GRIPS) {
425                     XMoveWindow(ob_display, self->lgrip,
426                                 -self->rbwidth, -self->rbwidth);
427                     XMoveWindow(ob_display, self->rgrip,
428                                 -self->rbwidth + self->width -
429                                 ob_rr_theme->grip_width, -self->rbwidth);
430                     XMapWindow(ob_display, self->lgrip);
431                     XMapWindow(ob_display, self->rgrip);
432                 } else {
433                     XUnmapWindow(ob_display, self->lgrip);
434                     XUnmapWindow(ob_display, self->rgrip);
435                 }
436             } else
437                 XUnmapWindow(ob_display, self->handle);
438
439             /* move and resize the inner border window which contains the plate
440              */
441             XMoveResizeWindow(ob_display, self->inner,
442                               self->innersize.left - self->cbwidth_x -
443                               self->bwidth,
444                               self->innersize.top - self->cbwidth_y -
445                               self->bwidth,
446                               self->client->area.width +
447                               self->cbwidth_x * 2,
448                               self->client->area.height +
449                               self->cbwidth_y * 2);
450
451             /* move the plate */
452             XMoveWindow(ob_display, self->plate,
453                         self->cbwidth_x, self->cbwidth_y);
454
455             /* when the client has StaticGravity, it likes to move around. */
456             XMoveWindow(ob_display, self->client->window, 0, 0);
457         }
458
459         STRUT_SET(self->size,
460                   self->innersize.left + self->bwidth,
461                   self->innersize.top + self->bwidth,
462                   self->innersize.right + self->bwidth,
463                   self->innersize.bottom + self->bwidth);
464     }
465
466     /* shading can change without being moved or resized */
467     RECT_SET_SIZE(self->area,
468                   self->client->area.width +
469                   self->size.left + self->size.right,
470                   (self->client->shaded ?
471                    ob_rr_theme->title_height + self->rbwidth * 2:
472                    self->client->area.height +
473                    self->size.top + self->size.bottom));
474
475     if (moved || resized) {
476         /* find the new coordinates, done after setting the frame.size, for
477            frame_client_gravity. */
478         self->area.x = self->client->area.x;
479         self->area.y = self->client->area.y;
480         frame_client_gravity(self, &self->area.x, &self->area.y,
481                              self->client->area.width,
482                              self->client->area.height);
483     }
484
485     if (!fake) {
486         if (!frame_iconify_animating(self))
487             /* move and resize the top level frame.
488                shading can change without being moved or resized.
489                
490                but don't do this during an iconify animation. it will be
491                reflected afterwards.
492             */
493             XMoveResizeWindow(ob_display, self->window,
494                               self->area.x, self->area.y,
495                               self->area.width - self->bwidth * 2,
496                               self->area.height - self->bwidth * 2);
497
498         if (resized) {
499             framerender_frame(self);
500             frame_adjust_shape(self);
501         }
502
503         if (!STRUT_EQUAL(self->size, oldsize)) {
504             gulong vals[4];
505             vals[0] = self->size.left;
506             vals[1] = self->size.right;
507             vals[2] = self->size.top;
508             vals[3] = self->size.bottom;
509             PROP_SETA32(self->client->window, net_frame_extents,
510                         cardinal, vals, 4);
511         }
512
513         /* if this occurs while we are focus cycling, the indicator needs to
514            match the changes */
515         if (focus_cycle_target == self->client)
516             focus_cycle_draw_indicator();
517     }
518     if (resized && (self->decorations & OB_FRAME_DECOR_TITLEBAR))
519         XResizeWindow(ob_display, self->label, self->label_width,
520                       ob_rr_theme->label_height);
521 }
522
523 void frame_adjust_client_area(ObFrame *self)
524 {
525     /* resize the plate */
526     XResizeWindow(ob_display, self->plate,
527                   self->client->area.width, self->client->area.height);
528 }
529
530 void frame_adjust_state(ObFrame *self)
531 {
532     framerender_frame(self);
533 }
534
535 void frame_adjust_focus(ObFrame *self, gboolean hilite)
536 {
537     self->focused = hilite;
538     framerender_frame(self);
539     XFlush(ob_display);
540 }
541
542 void frame_adjust_title(ObFrame *self)
543 {
544     framerender_frame(self);
545 }
546
547 void frame_adjust_icon(ObFrame *self)
548 {
549     framerender_frame(self);
550 }
551
552 void frame_grab_client(ObFrame *self, ObClient *client)
553 {
554     self->client = client;
555
556     /* reparent the client to the frame */
557     XReparentWindow(ob_display, client->window, self->plate, 0, 0);
558     /*
559       When reparenting the client window, it is usually not mapped yet, since
560       this occurs from a MapRequest. However, in the case where Openbox is
561       starting up, the window is already mapped, so we'll see unmap events for
562       it. There are 2 unmap events generated that we see, one with the 'event'
563       member set the root window, and one set to the client, but both get
564       handled and need to be ignored.
565     */
566     if (ob_state() == OB_STATE_STARTING)
567         client->ignore_unmaps += 2;
568
569     /* select the event mask on the client's parent (to receive config/map
570        req's) the ButtonPress is to catch clicks on the client border */
571     XSelectInput(ob_display, self->plate, PLATE_EVENTMASK);
572
573     frame_adjust_area(self, TRUE, TRUE, FALSE);
574
575     /* map the client so it maps when the frame does */
576     XMapWindow(ob_display, client->window);
577
578     /* set all the windows for the frame in the window_map */
579     g_hash_table_insert(window_map, &self->window, client);
580     g_hash_table_insert(window_map, &self->plate, client);
581     g_hash_table_insert(window_map, &self->inner, client);
582     g_hash_table_insert(window_map, &self->title, client);
583     g_hash_table_insert(window_map, &self->label, client);
584     g_hash_table_insert(window_map, &self->max, client);
585     g_hash_table_insert(window_map, &self->close, client);
586     g_hash_table_insert(window_map, &self->desk, client);
587     g_hash_table_insert(window_map, &self->shade, client);
588     g_hash_table_insert(window_map, &self->icon, client);
589     g_hash_table_insert(window_map, &self->iconify, client);
590     g_hash_table_insert(window_map, &self->handle, client);
591     g_hash_table_insert(window_map, &self->lgrip, client);
592     g_hash_table_insert(window_map, &self->rgrip, client);
593     g_hash_table_insert(window_map, &self->tltresize, client);
594     g_hash_table_insert(window_map, &self->tllresize, client);
595     g_hash_table_insert(window_map, &self->trtresize, client);
596     g_hash_table_insert(window_map, &self->trrresize, client);
597 }
598
599 void frame_release_client(ObFrame *self, ObClient *client)
600 {
601     XEvent ev;
602     gboolean reparent = TRUE;
603
604     g_assert(self->client == client);
605
606     /* if there was any animation going on, kill it */
607     ob_main_loop_timeout_remove_data(ob_main_loop, frame_animate_iconify,
608                                      self, FALSE);
609
610     /* check if the app has already reparented its window away */
611     while (XCheckTypedWindowEvent(ob_display, client->window,
612                                   ReparentNotify, &ev))
613     {
614         /* This check makes sure we don't catch our own reparent action to
615            our frame window. This doesn't count as the app reparenting itself
616            away of course.
617
618            Reparent events that are generated by us are just discarded here.
619            They are of no consequence to us anyhow.
620         */
621         if (ev.xreparent.parent != self->plate) {
622             reparent = FALSE;
623             XPutBackEvent(ob_display, &ev);
624             break;
625         }
626     }
627
628     if (reparent) {
629         /* according to the ICCCM - if the client doesn't reparent itself,
630            then we will reparent the window to root for them */
631         XReparentWindow(ob_display, client->window,
632                         RootWindow(ob_display, ob_screen),
633                         client->area.x,
634                         client->area.y);
635     }
636
637     /* remove all the windows for the frame from the window_map */
638     g_hash_table_remove(window_map, &self->window);
639     g_hash_table_remove(window_map, &self->plate);
640     g_hash_table_remove(window_map, &self->inner);
641     g_hash_table_remove(window_map, &self->title);
642     g_hash_table_remove(window_map, &self->label);
643     g_hash_table_remove(window_map, &self->max);
644     g_hash_table_remove(window_map, &self->close);
645     g_hash_table_remove(window_map, &self->desk);
646     g_hash_table_remove(window_map, &self->shade);
647     g_hash_table_remove(window_map, &self->icon);
648     g_hash_table_remove(window_map, &self->iconify);
649     g_hash_table_remove(window_map, &self->handle);
650     g_hash_table_remove(window_map, &self->lgrip);
651     g_hash_table_remove(window_map, &self->rgrip);
652     g_hash_table_remove(window_map, &self->tltresize);
653     g_hash_table_remove(window_map, &self->tllresize);
654     g_hash_table_remove(window_map, &self->trtresize);
655     g_hash_table_remove(window_map, &self->trrresize);
656
657     ob_main_loop_timeout_remove_data(ob_main_loop, flash_timeout, self, TRUE);
658
659     frame_free(self);
660 }
661
662 static void layout_title(ObFrame *self)
663 {
664     gchar *lc;
665     gint x;
666     gboolean n, d, i, l, m, c, s;
667
668     n = d = i = l = m = c = s = FALSE;
669
670     /* figure out whats being shown, and the width of the label */
671     self->label_width = self->width - (ob_rr_theme->paddingx + 1) * 2;
672     for (lc = config_title_layout; *lc != '\0'; ++lc) {
673         switch (*lc) {
674         case 'N':
675             if (n) { *lc = ' '; break; } /* rm duplicates */
676             n = TRUE;
677             self->label_width -= (ob_rr_theme->button_size + 2 +
678                                   ob_rr_theme->paddingx + 1);
679             break;
680         case 'D':
681             if (d) { *lc = ' '; break; }
682             if (!(self->decorations & OB_FRAME_DECOR_ALLDESKTOPS)
683                 && config_theme_hidedisabled)
684                 break;
685             d = TRUE;
686             self->label_width -= (ob_rr_theme->button_size +
687                                   ob_rr_theme->paddingx + 1);
688             break;
689         case 'S':
690             if (s) { *lc = ' '; break; }
691             if (!(self->decorations & OB_FRAME_DECOR_SHADE)
692                 && config_theme_hidedisabled)
693                 break;
694             s = TRUE;
695             self->label_width -= (ob_rr_theme->button_size +
696                                   ob_rr_theme->paddingx + 1);
697             break;
698         case 'I':
699             if (i) { *lc = ' '; break; }
700             if (!(self->decorations & OB_FRAME_DECOR_ICONIFY)
701                 && config_theme_hidedisabled)
702                 break;
703             i = TRUE;
704             self->label_width -= (ob_rr_theme->button_size +
705                                   ob_rr_theme->paddingx + 1);
706             break;
707         case 'L':
708             if (l) { *lc = ' '; break; }
709             l = TRUE;
710             break;
711         case 'M':
712             if (m) { *lc = ' '; break; }
713             if (!(self->decorations & OB_FRAME_DECOR_MAXIMIZE)
714                 && config_theme_hidedisabled)
715                 break;
716             m = TRUE;
717             self->label_width -= (ob_rr_theme->button_size +
718                                   ob_rr_theme->paddingx + 1);
719             break;
720         case 'C':
721             if (c) { *lc = ' '; break; }
722             if (!(self->decorations & OB_FRAME_DECOR_CLOSE)
723                 && config_theme_hidedisabled)
724                 break;
725             c = TRUE;
726             self->label_width -= (ob_rr_theme->button_size +
727                                   ob_rr_theme->paddingx + 1);
728             break;
729         }
730     }
731     if (self->label_width < 1) self->label_width = 1;
732
733     if (!n) XUnmapWindow(ob_display, self->icon);
734     if (!d) XUnmapWindow(ob_display, self->desk);
735     if (!s) XUnmapWindow(ob_display, self->shade);
736     if (!i) XUnmapWindow(ob_display, self->iconify);
737     if (!l) XUnmapWindow(ob_display, self->label);
738     if (!m) XUnmapWindow(ob_display, self->max);
739     if (!c) XUnmapWindow(ob_display, self->close);
740
741     x = ob_rr_theme->paddingx + 1;
742     for (lc = config_title_layout; *lc != '\0'; ++lc) {
743         switch (*lc) {
744         case 'N':
745             if (!n) break;
746             self->icon_x = x;
747             XMapWindow(ob_display, self->icon);
748             XMoveWindow(ob_display, self->icon, x, ob_rr_theme->paddingy);
749             x += ob_rr_theme->button_size + 2 + ob_rr_theme->paddingx + 1;
750             break;
751         case 'D':
752             if (!d) break;
753             self->desk_x = x;
754             XMapWindow(ob_display, self->desk);
755             XMoveWindow(ob_display, self->desk, x, ob_rr_theme->paddingy + 1);
756             x += ob_rr_theme->button_size + ob_rr_theme->paddingx + 1;
757             break;
758         case 'S':
759             if (!s) break;
760             self->shade_x = x;
761             XMapWindow(ob_display, self->shade);
762             XMoveWindow(ob_display, self->shade, x, ob_rr_theme->paddingy + 1);
763             x += ob_rr_theme->button_size + ob_rr_theme->paddingx + 1;
764             break;
765         case 'I':
766             if (!i) break;
767             self->iconify_x = x;
768             XMapWindow(ob_display, self->iconify);
769             XMoveWindow(ob_display,self->iconify, x, ob_rr_theme->paddingy + 1);
770             x += ob_rr_theme->button_size + ob_rr_theme->paddingx + 1;
771             break;
772         case 'L':
773             if (!l) break;
774             self->label_x = x;
775             XMapWindow(ob_display, self->label);
776             XMoveWindow(ob_display, self->label, x, ob_rr_theme->paddingy);
777             x += self->label_width + ob_rr_theme->paddingx + 1;
778             break;
779         case 'M':
780             if (!m) break;
781             self->max_x = x;
782             XMapWindow(ob_display, self->max);
783             XMoveWindow(ob_display, self->max, x, ob_rr_theme->paddingy + 1);
784             x += ob_rr_theme->button_size + ob_rr_theme->paddingx + 1;
785             break;
786         case 'C':
787             if (!c) break;
788             self->close_x = x;
789             XMapWindow(ob_display, self->close);
790             XMoveWindow(ob_display, self->close, x, ob_rr_theme->paddingy + 1);
791             x += ob_rr_theme->button_size + ob_rr_theme->paddingx + 1;
792             break;
793         }
794     }
795 }
796
797 ObFrameContext frame_context_from_string(const gchar *name)
798 {
799     if (!g_ascii_strcasecmp("Desktop", name))
800         return OB_FRAME_CONTEXT_DESKTOP;
801     else if (!g_ascii_strcasecmp("Client", name))
802         return OB_FRAME_CONTEXT_CLIENT;
803     else if (!g_ascii_strcasecmp("Titlebar", name))
804         return OB_FRAME_CONTEXT_TITLEBAR;
805     else if (!g_ascii_strcasecmp("Handle", name))
806         return OB_FRAME_CONTEXT_HANDLE;
807     else if (!g_ascii_strcasecmp("Frame", name))
808         return OB_FRAME_CONTEXT_FRAME;
809     else if (!g_ascii_strcasecmp("TLCorner", name))
810         return OB_FRAME_CONTEXT_TLCORNER;
811     else if (!g_ascii_strcasecmp("TRCorner", name))
812         return OB_FRAME_CONTEXT_TRCORNER;
813     else if (!g_ascii_strcasecmp("BLCorner", name))
814         return OB_FRAME_CONTEXT_BLCORNER;
815     else if (!g_ascii_strcasecmp("BRCorner", name))
816         return OB_FRAME_CONTEXT_BRCORNER;
817     else if (!g_ascii_strcasecmp("Maximize", name))
818         return OB_FRAME_CONTEXT_MAXIMIZE;
819     else if (!g_ascii_strcasecmp("AllDesktops", name))
820         return OB_FRAME_CONTEXT_ALLDESKTOPS;
821     else if (!g_ascii_strcasecmp("Shade", name))
822         return OB_FRAME_CONTEXT_SHADE;
823     else if (!g_ascii_strcasecmp("Iconify", name))
824         return OB_FRAME_CONTEXT_ICONIFY;
825     else if (!g_ascii_strcasecmp("Icon", name))
826         return OB_FRAME_CONTEXT_ICON;
827     else if (!g_ascii_strcasecmp("Close", name))
828         return OB_FRAME_CONTEXT_CLOSE;
829     else if (!g_ascii_strcasecmp("MoveResize", name))
830         return OB_FRAME_CONTEXT_MOVE_RESIZE;
831     return OB_FRAME_CONTEXT_NONE;
832 }
833
834 ObFrameContext frame_context(ObClient *client, Window win)
835 {
836     ObFrame *self;
837
838     if (moveresize_in_progress)
839         return OB_FRAME_CONTEXT_MOVE_RESIZE;
840
841     if (win == RootWindow(ob_display, ob_screen))
842         return OB_FRAME_CONTEXT_DESKTOP;
843     if (client == NULL) return OB_FRAME_CONTEXT_NONE;
844     if (win == client->window) {
845         /* conceptually, this is the desktop, as far as users are
846            concerned */
847         if (client->type == OB_CLIENT_TYPE_DESKTOP)
848             return OB_FRAME_CONTEXT_DESKTOP;
849         return OB_FRAME_CONTEXT_CLIENT;
850     }
851
852     self = client->frame;
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 gulong frame_animate_iconify_time_left(ObFrame *self,
1039                                               const GTimeVal *now)
1040 {
1041     glong sec, usec;
1042     sec = self->iconify_animation_end.tv_sec - now->tv_sec;
1043     usec = self->iconify_animation_end.tv_usec - now->tv_usec;
1044     if (usec < 0) {
1045         usec += G_USEC_PER_SEC;
1046         sec--;
1047     }
1048     /* no negative values */
1049     return MAX(sec * G_USEC_PER_SEC + usec, 0);
1050 }
1051
1052 static gboolean frame_animate_iconify(gpointer p)
1053 {
1054     ObFrame *self = p;
1055     gint x, y, w, h;
1056     gint iconx, icony, iconw;
1057     GTimeVal now;
1058     gulong time;
1059     gboolean iconifying;
1060
1061     if (self->client->icon_geometry.width == 0) {
1062         /* there is no icon geometry set so just go straight down */
1063         Rect *a = screen_physical_area();
1064         iconx = self->area.x + self->area.width / 2 + 32;
1065         icony = a->y + a->width;
1066         iconw = 64;
1067     } else {
1068         iconx = self->client->icon_geometry.x;
1069         icony = self->client->icon_geometry.y;
1070         iconw = self->client->icon_geometry.width;
1071     }
1072
1073     iconifying = self->iconify_animation_going > 0;
1074
1075     /* how far do we have left to go ? */
1076     g_get_current_time(&now);
1077     time = frame_animate_iconify_time_left(self, &now);
1078     
1079     if (time == 0 || iconifying) {
1080         /* start where the frame is supposed to be */
1081         x = self->area.x;
1082         y = self->area.y;
1083         w = self->area.width - self->bwidth * 2;
1084         h = self->area.height - self->bwidth * 2;
1085     } else {
1086         /* start at the icon */
1087         x = iconx;
1088         y = icony;
1089         w = iconw;
1090         h = self->innersize.top; /* just the titlebar */
1091     }
1092
1093     if (time > 0) {
1094         glong dx, dy, dw;
1095         glong elapsed;
1096
1097         dx = self->area.x - iconx;
1098         dy = self->area.y - icony;
1099         dw = self->area.width - self->bwidth * 2 - iconw;
1100          /* if restoring, we move in the opposite direction */
1101         if (!iconifying) { dx = -dx; dy = -dy; dw = -dw; }
1102
1103         elapsed = FRAME_ANIMATE_ICONIFY_TIME - time;
1104         x = x - (dx * elapsed) / FRAME_ANIMATE_ICONIFY_TIME;
1105         y = y - (dy * elapsed) / FRAME_ANIMATE_ICONIFY_TIME;
1106         w = w - (dw * elapsed) / FRAME_ANIMATE_ICONIFY_TIME;
1107         h = self->innersize.top; /* just the titlebar */
1108     }
1109
1110     if (time == 0)
1111         frame_end_iconify_animation(self);
1112     else {
1113         XMoveResizeWindow(ob_display, self->window, x, y, w, h);
1114         XFlush(ob_display);
1115     }
1116
1117     return time > 0; /* repeat until we're out of time */
1118 }
1119
1120 void frame_end_iconify_animation(ObFrame *self)
1121 {
1122     /* see if there is an animation going */
1123     if (self->iconify_animation_going == 0) return;
1124
1125     /* call the callback when it's done */
1126     if (self->iconify_animation_cb)
1127         self->iconify_animation_cb(self->iconify_animation_data);
1128     /* we're not animating any more ! */
1129     self->iconify_animation_going = 0;
1130
1131     /* move after the callback for the animation ending */
1132     XMoveResizeWindow(ob_display, self->window,
1133                       self->area.x, self->area.y,
1134                       self->area.width - self->bwidth * 2,
1135                       self->area.height - self->bwidth * 2);
1136     XFlush(ob_display);
1137 }
1138
1139 void frame_begin_iconify_animation(ObFrame *self, gboolean iconifying,
1140                                    ObFrameIconifyAnimateFunc callback,
1141                                    gpointer data)
1142 {
1143     gulong time;
1144     gboolean new_anim = FALSE;
1145     gboolean set_end = TRUE;
1146     GTimeVal now;
1147
1148     /* if there is no titlebar, just don't animate for now
1149        XXX it would be nice tho.. */
1150     if (!(self->decorations & OB_FRAME_DECOR_TITLEBAR)) {
1151         if (callback) callback(data);
1152         return;
1153     }
1154
1155     /* get the current time */
1156     g_get_current_time(&now);
1157
1158     /* get how long until the end */
1159     time = FRAME_ANIMATE_ICONIFY_TIME;
1160     if (self->iconify_animation_going) {
1161         if (!!iconifying != (self->iconify_animation_going > 0)) {
1162             /* animation was already going on in the opposite direction */
1163             time = time - frame_animate_iconify_time_left(self, &now);
1164         } else
1165             /* animation was already going in the same direction */
1166             set_end = FALSE;
1167     } else
1168         new_anim = TRUE;
1169     self->iconify_animation_going = iconifying ? 1 : -1;
1170
1171     self->iconify_animation_cb = callback;
1172     self->iconify_animation_data = data;
1173
1174     /* set the ending time */
1175     if (set_end) {
1176         self->iconify_animation_end.tv_sec = now.tv_sec;
1177         self->iconify_animation_end.tv_usec = now.tv_usec;
1178         g_time_val_add(&self->iconify_animation_end, time);
1179     }
1180
1181     if (new_anim) {
1182         ob_main_loop_timeout_remove_data(ob_main_loop, frame_animate_iconify,
1183                                          self, FALSE);
1184         ob_main_loop_timeout_add(ob_main_loop,
1185                                  FRAME_ANIMATE_ICONIFY_STEP_TIME,
1186                                  frame_animate_iconify, self,
1187                                  g_direct_equal, NULL);
1188
1189         /* do the first step */
1190         frame_animate_iconify(self);
1191
1192         if (!self->visible)
1193             frame_show(self);
1194     }
1195 }
1196
1197 gboolean frame_visible(ObFrame *self)
1198 {
1199     /* if it is animating back from iconic state then it is considered
1200        visible. but if it is iconifying then it is not visible. */
1201     return self->visible && self->iconify_animation_going <= 0;
1202 }