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