]> icculus.org git repositories - mikachu/openbox.git/blob - openbox/popup.c
add a slight delay to the focus/desktop switch dialogs. so if you hit the key really...
[mikachu/openbox.git] / openbox / popup.c
1 /* -*- indent-tabs-mode: nil; tab-width: 4; c-basic-offset: 4; -*-
2
3    popup.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 "popup.h"
21
22 #include "openbox.h"
23 #include "frame.h"
24 #include "client.h"
25 #include "stacking.h"
26 #include "event.h"
27 #include "screen.h"
28 #include "mainloop.h"
29 #include "render/render.h"
30 #include "render/theme.h"
31
32 static gboolean popup_show_timeout(gpointer data)
33 {
34     ObPopup *self = data;
35     
36     XMapWindow(ob_display, self->bg);
37     stacking_raise(INTERNAL_AS_WINDOW(self));
38     self->mapped = TRUE;
39     self->delay_mapped = FALSE;
40
41     return FALSE; /* don't repeat */
42 }
43
44 ObPopup *popup_new(gboolean hasicon)
45 {
46     XSetWindowAttributes attrib;
47     ObPopup *self = g_new0(ObPopup, 1);
48
49     self->obwin.type = Window_Internal;
50     self->hasicon = hasicon;
51     self->gravity = NorthWestGravity;
52     self->x = self->y = self->w = self->h = 0;
53     self->a_bg = RrAppearanceCopy(ob_rr_theme->osd_hilite_bg);
54     self->a_text = RrAppearanceCopy(ob_rr_theme->osd_hilite_label);
55
56     attrib.override_redirect = True;
57     self->bg = XCreateWindow(ob_display, RootWindow(ob_display, ob_screen),
58                              0, 0, 1, 1, 0, RrDepth(ob_rr_inst),
59                              InputOutput, RrVisual(ob_rr_inst),
60                              CWOverrideRedirect, &attrib);
61     
62     self->text = XCreateWindow(ob_display, self->bg,
63                                0, 0, 1, 1, 0, RrDepth(ob_rr_inst),
64                                InputOutput, RrVisual(ob_rr_inst), 0, NULL);
65
66     XMapWindow(ob_display, self->text);
67
68     stacking_add(INTERNAL_AS_WINDOW(self));
69     return self;
70 }
71
72 void popup_free(ObPopup *self)
73 {
74     if (self) {
75         XDestroyWindow(ob_display, self->bg);
76         XDestroyWindow(ob_display, self->text);
77         RrAppearanceFree(self->a_bg);
78         RrAppearanceFree(self->a_text);
79         stacking_remove(self);
80         g_free(self);
81     }
82 }
83
84 void popup_position(ObPopup *self, gint gravity, gint x, gint y)
85 {
86     self->gravity = gravity;
87     self->x = x;
88     self->y = y;
89 }
90
91 void popup_width(ObPopup *self, gint w)
92 {
93     self->w = w;
94 }
95
96 void popup_height(ObPopup *self, gint h)
97 {
98     gint texth;
99
100     /* don't let the height be smaller than the text */
101     texth = RrMinHeight(self->a_text) + ob_rr_theme->paddingy * 2;
102     self->h = MAX(h, texth);
103 }
104
105 void popup_width_to_string(ObPopup *self, gchar *text, gint max)
106 {
107     gint textw, iconw;
108
109     self->a_text->texture[0].data.text.string = text;
110     textw = RrMinWidth(self->a_text);
111     if (self->hasicon) {
112         gint texth = RrMinHeight(self->a_text) + ob_rr_theme->paddingy * 2;
113         iconw = texth;
114     } else
115         iconw = 0;
116     self->w = textw + iconw + ob_rr_theme->paddingx * (self->hasicon ? 3 : 2);
117     /* cap it at "max" */
118     if (max > 0)
119         self->w = MIN(self->w, max);
120 }
121
122 void popup_height_to_string(ObPopup *self, gchar *text)
123 {
124     self->h = RrMinHeight(self->a_text) + ob_rr_theme->paddingy * 2;
125 }
126
127 void popup_width_to_strings(ObPopup *self, gchar **strings, gint max)
128 {
129     gint i, maxw;
130
131     maxw = 0;
132     for (i = 0; strings[i] != NULL; ++i) {
133         popup_width_to_string(self, strings[i], max);
134         maxw = MAX(maxw, self->w);
135     }
136     self->w = maxw;
137 }
138
139 void popup_set_text_align(ObPopup *self, RrJustify align)
140 {
141     self->a_text->texture[0].data.text.justify = align;
142 }
143
144 void popup_delay_show(ObPopup *self, gulong usec, gchar *text)
145 {
146     gint l, t, r, b;
147     gint x, y, w, h;
148     gint textw, texth;
149     gint iconw;
150     Rect *area; /* won't go outside this */
151
152     area = screen_physical_area();          /* XXX this should work quite
153                                                good, someone with xinerama,
154                                                and different resolutions on
155                                                screens? */
156
157     RrMargins(self->a_bg, &l, &t, &r, &b);
158
159     XSetWindowBorderWidth(ob_display, self->bg, ob_rr_theme->fbwidth);
160     XSetWindowBorder(ob_display, self->bg, ob_rr_theme->frame_b_color->pixel);
161
162     /* set up the textures */
163     self->a_text->texture[0].data.text.string = text;
164
165     /* measure the text out */
166     RrMinSize(self->a_text, &textw, &texth);
167     texth += ob_rr_theme->paddingy * 2;
168
169     /* set the sizes up and reget the text sizes from the calculated
170        outer sizes */
171     if (self->h) {
172         h = self->h;
173         texth = h - (t+b + ob_rr_theme->paddingy * 2);
174     } else
175         h = t+b + texth + ob_rr_theme->paddingy * 2;
176     iconw = (self->hasicon ? texth : 0);
177     if (self->w) {
178         w = self->w;
179         textw = w - (l+r + iconw + ob_rr_theme->paddingx *
180                      (self->hasicon ? 3 : 2));
181     } else
182         w = l+r + textw + iconw + ob_rr_theme->paddingx *
183             (self->hasicon ? 3 : 2);
184     /* sanity checks to avoid crashes! */
185     if (w < 1) w = 1;
186     if (h < 1) h = 1;
187     if (textw < 1) textw = 1;
188     if (texth < 1) texth = 1;
189
190     /* set up the x coord */
191     x = self->x;
192     switch (self->gravity) {
193     case NorthGravity:
194     case CenterGravity:
195     case SouthGravity:
196         x -= w / 2;
197         break;
198     case NorthEastGravity:
199     case EastGravity:
200     case SouthEastGravity:
201         x -= w;
202         break;
203     }
204
205     /* set up the y coord */
206     y = self->y;
207     switch (self->gravity) {
208     case WestGravity:
209     case CenterGravity:
210     case EastGravity:
211         y -= h / 2;
212         break;
213     case SouthWestGravity:
214     case SouthGravity:
215     case SouthEastGravity:
216         y -= h;
217         break;
218     }
219
220     x=MAX(MIN(x, area->width-w),0);
221     y=MAX(MIN(y, area->height-h),0);
222
223     /* set the windows/appearances up */
224     XMoveResizeWindow(ob_display, self->bg, x, y, w, h);
225
226     self->a_text->surface.parent = self->a_bg;
227     self->a_text->surface.parentx = l + iconw +
228         ob_rr_theme->paddingx * (self->hasicon ? 2 : 1);
229     self->a_text->surface.parenty = t + ob_rr_theme->paddingy;
230     XMoveResizeWindow(ob_display, self->text,
231                       l + iconw + ob_rr_theme->paddingx *
232                       (self->hasicon ? 2 : 1),
233                       t + ob_rr_theme->paddingy, textw, texth);
234
235     RrPaint(self->a_bg, self->bg, w, h);
236     RrPaint(self->a_text, self->text, textw, texth);
237
238     if (self->hasicon) {
239         if (iconw < 1) iconw = 1; /* sanity check for crashes */
240         if (self->draw_icon)
241             self->draw_icon(l + ob_rr_theme->paddingx,
242                             t + ob_rr_theme->paddingy,
243                             iconw, texth, self->draw_icon_data);
244     }
245
246     /* do the actual showing */
247     if (!self->mapped) {
248         if (usec) {
249             /* don't kill previous show timers */
250             if (!self->delay_mapped) {
251                 ob_main_loop_timeout_add(ob_main_loop, usec,
252                                          popup_show_timeout, self,
253                                          g_direct_equal, NULL);
254                 self->delay_mapped = TRUE;
255             }
256         } else {
257             popup_show_timeout(self);
258         }
259     }
260 }
261
262 void popup_hide(ObPopup *self)
263 {
264     if (self->mapped) {
265         XUnmapWindow(ob_display, self->bg);
266         self->mapped = FALSE;
267
268         /* kill enter events cause by this unmapping */
269         event_ignore_queued_enters();
270     } else if (self->delay_mapped) {
271         ob_main_loop_timeout_remove(ob_main_loop, popup_show_timeout);
272         self->delay_mapped = FALSE;
273     }
274 }
275
276 static void icon_popup_draw_icon(gint x, gint y, gint w, gint h, gpointer data)
277 {
278     ObIconPopup *self = data;
279
280     self->a_icon->surface.parent = self->popup->a_bg;
281     self->a_icon->surface.parentx = x;
282     self->a_icon->surface.parenty = y;
283     XMoveResizeWindow(ob_display, self->icon, x, y, w, h);
284     RrPaint(self->a_icon, self->icon, w, h);
285 }
286
287 ObIconPopup *icon_popup_new()
288 {
289     ObIconPopup *self;
290
291     self = g_new0(ObIconPopup, 1);
292     self->popup = popup_new(TRUE);
293     self->a_icon = RrAppearanceCopy(ob_rr_theme->a_clear_tex);
294     self->icon = XCreateWindow(ob_display, self->popup->bg,
295                                0, 0, 1, 1, 0,
296                                RrDepth(ob_rr_inst), InputOutput,
297                                RrVisual(ob_rr_inst), 0, NULL);
298     XMapWindow(ob_display, self->icon);
299
300     self->popup->draw_icon = icon_popup_draw_icon;
301     self->popup->draw_icon_data = self;
302
303     return self;
304 }
305
306 void icon_popup_free(ObIconPopup *self)
307 {
308     if (self) {
309         XDestroyWindow(ob_display, self->icon);
310         RrAppearanceFree(self->a_icon);
311         popup_free(self->popup);
312         g_free(self);
313     }
314 }
315
316 void icon_popup_delay_show(ObIconPopup *self, gulong usec,
317                            gchar *text, const ObClientIcon *icon)
318 {
319     if (icon) {
320         self->a_icon->texture[0].type = RR_TEXTURE_RGBA;
321         self->a_icon->texture[0].data.rgba.width = icon->width;
322         self->a_icon->texture[0].data.rgba.height = icon->height;
323         self->a_icon->texture[0].data.rgba.data = icon->data;
324     } else
325         self->a_icon->texture[0].type = RR_TEXTURE_NONE;
326
327     popup_delay_show(self->popup, usec, text);
328 }
329
330 static void pager_popup_draw_icon(gint px, gint py, gint w, gint h,
331                                   gpointer data)
332 {
333     ObPagerPopup *self = data;
334     gint x, y;
335     guint rown, n;
336     guint horz_inc;
337     guint vert_inc;
338     guint r, c;
339     gint eachw, eachh;
340
341     eachw = (w - ob_rr_theme->fbwidth -
342              (screen_desktop_layout.columns * ob_rr_theme->fbwidth))
343         / screen_desktop_layout.columns;
344     eachh = (h - ob_rr_theme->fbwidth -
345              (screen_desktop_layout.rows * ob_rr_theme->fbwidth))
346         / screen_desktop_layout.rows;
347     /* make them squares */
348     eachw = eachh = MIN(eachw, eachh);
349
350     /* center */
351     px += (w - (screen_desktop_layout.columns * (eachw + ob_rr_theme->fbwidth) +
352                 ob_rr_theme->fbwidth)) / 2;
353     py += (h - (screen_desktop_layout.rows * (eachh + ob_rr_theme->fbwidth) +
354                 ob_rr_theme->fbwidth)) / 2;
355
356     if (eachw <= 0 || eachh <= 0)
357         return;
358
359     switch (screen_desktop_layout.orientation) {
360     case OB_ORIENTATION_HORZ:
361         switch (screen_desktop_layout.start_corner) {
362         case OB_CORNER_TOPLEFT:
363             n = 0;
364             horz_inc = 1;
365             vert_inc = screen_desktop_layout.columns;
366             break;
367         case OB_CORNER_TOPRIGHT:
368             n = screen_desktop_layout.columns - 1;
369             horz_inc = -1;
370             vert_inc = screen_desktop_layout.columns;
371             break;
372         case OB_CORNER_BOTTOMRIGHT:
373             n = screen_desktop_layout.rows * screen_desktop_layout.columns - 1;
374             horz_inc = -1;
375             vert_inc = -screen_desktop_layout.columns;
376             break;
377         case OB_CORNER_BOTTOMLEFT:
378             n = (screen_desktop_layout.rows - 1)
379                 * screen_desktop_layout.columns;
380             horz_inc = 1;
381             vert_inc = -screen_desktop_layout.columns;
382             break;
383         default:
384             g_assert_not_reached();
385         }
386         break;
387     case OB_ORIENTATION_VERT:
388         switch (screen_desktop_layout.start_corner) {
389         case OB_CORNER_TOPLEFT:
390             n = 0;
391             horz_inc = screen_desktop_layout.rows;
392             vert_inc = 1;
393             break;
394         case OB_CORNER_TOPRIGHT:
395             n = screen_desktop_layout.rows
396                 * (screen_desktop_layout.columns - 1);
397             horz_inc = -screen_desktop_layout.rows;
398             vert_inc = 1;
399             break;
400         case OB_CORNER_BOTTOMRIGHT:
401             n = screen_desktop_layout.rows * screen_desktop_layout.columns - 1;
402             horz_inc = -screen_desktop_layout.rows;
403             vert_inc = -1;
404             break;
405         case OB_CORNER_BOTTOMLEFT:
406             n = screen_desktop_layout.rows - 1;
407             horz_inc = screen_desktop_layout.rows;
408             vert_inc = -1;
409             break;
410         default:
411             g_assert_not_reached();
412         }
413         break;
414     default:
415         g_assert_not_reached();
416     }
417
418     rown = n;
419     for (r = 0, y = 0; r < screen_desktop_layout.rows;
420          ++r, y += eachh + ob_rr_theme->fbwidth)
421     {
422         for (c = 0, x = 0; c < screen_desktop_layout.columns;
423              ++c, x += eachw + ob_rr_theme->fbwidth)
424         {
425             RrAppearance *a;
426
427             if (n < self->desks) {
428                 a = (n == self->curdesk ? self->hilight : self->unhilight);
429
430                 a->surface.parent = self->popup->a_bg;
431                 a->surface.parentx = x + px;
432                 a->surface.parenty = y + py;
433                 XMoveResizeWindow(ob_display, self->wins[n],
434                                   x + px, y + py, eachw, eachh);
435                 RrPaint(a, self->wins[n], eachw, eachh);
436             }
437             n += horz_inc;
438         }
439         n = rown += vert_inc;
440     }
441 }
442
443 ObPagerPopup *pager_popup_new()
444 {
445     ObPagerPopup *self;
446
447     self = g_new(ObPagerPopup, 1);
448     self->popup = popup_new(TRUE);
449
450     self->desks = 0;
451     self->wins = g_new(Window, self->desks);
452     self->hilight = RrAppearanceCopy(ob_rr_theme->osd_hilite_fg);
453     self->unhilight = RrAppearanceCopy(ob_rr_theme->osd_unhilite_fg);
454
455     self->popup->draw_icon = pager_popup_draw_icon;
456     self->popup->draw_icon_data = self;
457
458     return self;
459 }
460
461 void pager_popup_free(ObPagerPopup *self)
462 {
463     if (self) {
464         guint i;
465
466         for (i = 0; i < self->desks; ++i)
467             XDestroyWindow(ob_display, self->wins[i]);
468         g_free(self->wins);
469         RrAppearanceFree(self->hilight);
470         RrAppearanceFree(self->unhilight);
471         popup_free(self->popup);
472         g_free(self);
473     }
474 }
475
476 void pager_popup_delay_show(ObPagerPopup *self, gulong usec,
477                             gchar *text, guint desk)
478 {
479     guint i;
480
481     if (screen_num_desktops < self->desks)
482         for (i = screen_num_desktops; i < self->desks; ++i)
483             XDestroyWindow(ob_display, self->wins[i]);
484
485     if (screen_num_desktops != self->desks)
486         self->wins = g_renew(Window, self->wins, screen_num_desktops);
487
488     if (screen_num_desktops > self->desks)
489         for (i = self->desks; i < screen_num_desktops; ++i) {
490             XSetWindowAttributes attr;
491
492             attr.border_pixel = RrColorPixel(ob_rr_theme->frame_b_color);
493             self->wins[i] = XCreateWindow(ob_display, self->popup->bg,
494                                           0, 0, 1, 1, ob_rr_theme->fbwidth,
495                                           RrDepth(ob_rr_inst), InputOutput,
496                                           RrVisual(ob_rr_inst), CWBorderPixel,
497                                           &attr);
498             XMapWindow(ob_display, self->wins[i]);
499         }
500
501     self->desks = screen_num_desktops;
502     self->curdesk = desk;
503
504     popup_delay_show(self->popup, usec, text);
505 }