Merge branch 'backport'
[dana/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 ObPopup *popup_new(void)
33 {
34     XSetWindowAttributes attrib;
35     ObPopup *self = g_new0(ObPopup, 1);
36
37     self->obwin.type = Window_Internal;
38     self->gravity = NorthWestGravity;
39     self->x = self->y = self->textw = self->h = 0;
40     self->a_bg = RrAppearanceCopy(ob_rr_theme->osd_hilite_bg);
41     self->a_text = RrAppearanceCopy(ob_rr_theme->osd_hilite_label);
42     self->iconwm = self->iconhm = 1;
43
44     attrib.override_redirect = True;
45     self->bg = XCreateWindow(ob_display, RootWindow(ob_display, ob_screen),
46                              0, 0, 1, 1, 0, RrDepth(ob_rr_inst),
47                              InputOutput, RrVisual(ob_rr_inst),
48                              CWOverrideRedirect, &attrib);
49
50     self->text = XCreateWindow(ob_display, self->bg,
51                                0, 0, 1, 1, 0, RrDepth(ob_rr_inst),
52                                InputOutput, RrVisual(ob_rr_inst), 0, NULL);
53
54     XSetWindowBorderWidth(ob_display, self->bg, ob_rr_theme->obwidth);
55     XSetWindowBorder(ob_display, self->bg,
56                      RrColorPixel(ob_rr_theme->osd_border_color));
57
58     XMapWindow(ob_display, self->text);
59
60     stacking_add(INTERNAL_AS_WINDOW(self));
61     return self;
62 }
63
64 void popup_free(ObPopup *self)
65 {
66     if (self) {
67         XDestroyWindow(ob_display, self->bg);
68         XDestroyWindow(ob_display, self->text);
69         RrAppearanceFree(self->a_bg);
70         RrAppearanceFree(self->a_text);
71         stacking_remove(self);
72         g_free(self);
73     }
74 }
75
76 void popup_position(ObPopup *self, gint gravity, gint x, gint y)
77 {
78     self->gravity = gravity;
79     self->x = x;
80     self->y = y;
81 }
82
83 void popup_text_width(ObPopup *self, gint w)
84 {
85     self->textw = w;
86 }
87
88 void popup_min_width(ObPopup *self, gint minw)
89 {
90     self->minw = minw;
91 }
92
93 void popup_max_width(ObPopup *self, gint maxw)
94 {
95     self->maxw = maxw;
96 }
97
98 void popup_height(ObPopup *self, gint h)
99 {
100     gint texth;
101
102     /* don't let the height be smaller than the text */
103     texth = RrMinHeight(self->a_text) + ob_rr_theme->paddingy * 2;
104     self->h = MAX(h, texth);
105 }
106
107 void popup_text_width_to_string(ObPopup *self, gchar *text)
108 {
109     if (text[0] != '\0') {
110         self->a_text->texture[0].data.text.string = text;
111         self->textw = RrMinWidth(self->a_text);
112     } else
113         self->textw = 0;
114 }
115
116 void popup_height_to_string(ObPopup *self, gchar *text)
117 {
118     self->h = RrMinHeight(self->a_text) + ob_rr_theme->paddingy * 2;
119 }
120
121 void popup_text_width_to_strings(ObPopup *self, gchar **strings, gint num)
122 {
123     gint i, maxw;
124
125     maxw = 0;
126     for (i = 0; i < num; ++i) {
127         popup_text_width_to_string(self, strings[i]);
128         maxw = MAX(maxw, self->textw);
129     }
130     self->textw = maxw;
131 }
132
133 void popup_set_text_align(ObPopup *self, RrJustify align)
134 {
135     self->a_text->texture[0].data.text.justify = align;
136 }
137
138 static gboolean popup_show_timeout(gpointer data)
139 {
140     ObPopup *self = data;
141
142     XMapWindow(ob_display, self->bg);
143     stacking_raise(INTERNAL_AS_WINDOW(self));
144     self->mapped = TRUE;
145     self->delay_mapped = FALSE;
146
147     return FALSE; /* don't repeat */
148 }
149
150 void popup_delay_show(ObPopup *self, gulong usec, gchar *text)
151 {
152     gint l, t, r, b;
153     gint x, y, w, h;
154     guint m;
155     gint emptyx, emptyy; /* empty space between elements */
156     gint textx, texty, textw, texth;
157     gint iconx, icony, iconw, iconh;
158     Rect *area, mon;
159
160     /* when there is no icon and the text is not parent relative, then
161        fill the whole dialog with the text appearance, don't use the bg at all
162     */
163     if (self->hasicon || self->a_text->surface.grad == RR_SURFACE_PARENTREL)
164         RrMargins(self->a_bg, &l, &t, &r, &b);
165     else
166         l = t = r = b = 0;
167
168     /* set up the textures */
169     self->a_text->texture[0].data.text.string = text;
170
171     /* measure the text out */
172     if (text[0] != '\0') {
173         RrMinSize(self->a_text, &textw, &texth);
174     } else {
175         textw = 0;
176         texth = RrMinHeight(self->a_text);
177     }
178
179     /* get the height, which is also used for the icon width */
180     emptyy = t + b + ob_rr_theme->paddingy * 2;
181     if (self->h)
182         texth = self->h - emptyy;
183     h = texth * self->iconhm + emptyy;
184
185     if (self->textw)
186         textw = self->textw;
187
188     iconx = textx = l + ob_rr_theme->paddingx;
189
190     emptyx = l + r + ob_rr_theme->paddingx * 2;
191     if (self->hasicon) {
192         iconw = texth * self->iconwm;
193         iconh = texth * self->iconhm;
194         textx += iconw + ob_rr_theme->paddingx;
195         if (textw)
196             emptyx += ob_rr_theme->paddingx; /* between the icon and text */
197     } else
198         iconw = 0;
199
200     texty = (h - texth - emptyy) / 2 + t + ob_rr_theme->paddingy;
201     icony = (h - iconh - emptyy) / 2 + t + ob_rr_theme->paddingy;
202
203     /* when there is no icon, then fill the whole dialog with the text
204        appearance
205     */
206     if (!self->hasicon)
207     {
208         textx = texty = 0;
209         texth += emptyy;
210         textw += emptyx;
211         emptyx = emptyy = 0;
212     }
213
214     w = textw + emptyx + iconw;
215     /* cap it at maxw/minw */
216     if (self->maxw) w = MIN(w, self->maxw);
217     if (self->minw) w = MAX(w, self->minw);
218     textw = w - emptyx - iconw;
219
220     /* sanity checks to avoid crashes! */
221     if (w < 1) w = 1;
222     if (h < 1) h = 1;
223     if (texth < 1) texth = 1;
224
225     /* set up the x coord */
226     x = self->x;
227     switch (self->gravity) {
228     case NorthGravity: case CenterGravity: case SouthGravity:
229         x -= w / 2;
230         break;
231     case NorthEastGravity: case EastGravity: case SouthEastGravity:
232         x -= w;
233         break;
234     }
235
236     /* set up the y coord */
237     y = self->y;
238     switch (self->gravity) {
239     case WestGravity: case CenterGravity: case EastGravity:
240         y -= h / 2;
241         break;
242     case SouthWestGravity: case SouthGravity: case SouthEastGravity:
243         y -= h;
244         break;
245     }
246
247     /* Find the monitor which contains the biggest part of the popup.
248      * If the popup is completely off screen, limit it to the intersection
249      * of all monitors and then try again. If it's still off screen, put it
250      * on monitor 0. */
251     RECT_SET(mon, x, y, w, h);
252     m = screen_find_monitor(&mon);
253     area = screen_physical_area_monitor(m);
254
255     x=MAX(MIN(x, area->x+area->width-w),area->x);
256     y=MAX(MIN(y, area->y+area->height-h),area->y);
257
258     if (m == screen_num_monitors) {
259         RECT_SET(mon, x, y, w, h);
260         m = screen_find_monitor(&mon);
261         if (m == screen_num_monitors)
262             m = 0;
263         area = screen_physical_area_monitor(m);
264
265         x=MAX(MIN(x, area->x+area->width-w),area->x);
266         y=MAX(MIN(y, area->y+area->height-h),area->y);
267     }
268
269     /* set the windows/appearances up */
270     XMoveResizeWindow(ob_display, self->bg, x, y, w, h);
271     /* when there is no icon and the text is not parent relative, then
272        fill the whole dialog with the text appearance, don't use the bg at all
273     */
274     if (self->hasicon || self->a_text->surface.grad == RR_SURFACE_PARENTREL)
275         RrPaint(self->a_bg, self->bg, w, h);
276
277     if (textw) {
278         self->a_text->surface.parent = self->a_bg;
279         self->a_text->surface.parentx = textx;
280         self->a_text->surface.parenty = texty;
281         XMoveResizeWindow(ob_display, self->text, textx, texty, textw, texth);
282         RrPaint(self->a_text, self->text, textw, texth);
283     }
284
285     if (self->hasicon)
286         self->draw_icon(iconx, icony, iconw, iconh, self->draw_icon_data);
287
288     /* do the actual showing */
289     if (!self->mapped) {
290         if (usec) {
291             /* don't kill previous show timers */
292             if (!self->delay_mapped) {
293                 ob_main_loop_timeout_add(ob_main_loop, usec,
294                                          popup_show_timeout, self,
295                                          g_direct_equal, NULL);
296                 self->delay_mapped = TRUE;
297             }
298         } else {
299             popup_show_timeout(self);
300         }
301     }
302
303     g_free(area);
304 }
305
306 void popup_hide(ObPopup *self)
307 {
308     if (self->mapped) {
309         gulong ignore_start;
310
311         /* kill enter events cause by this unmapping */
312         ignore_start = event_start_ignore_all_enters();
313
314         XUnmapWindow(ob_display, self->bg);
315         self->mapped = FALSE;
316
317         event_end_ignore_all_enters(ignore_start);
318     } else if (self->delay_mapped) {
319         ob_main_loop_timeout_remove(ob_main_loop, popup_show_timeout);
320         self->delay_mapped = FALSE;
321     }
322 }
323
324 static void icon_popup_draw_icon(gint x, gint y, gint w, gint h, gpointer data)
325 {
326     ObIconPopup *self = data;
327
328     self->a_icon->surface.parent = self->popup->a_bg;
329     self->a_icon->surface.parentx = x;
330     self->a_icon->surface.parenty = y;
331     XMoveResizeWindow(ob_display, self->icon, x, y, w, h);
332     RrPaint(self->a_icon, self->icon, w, h);
333 }
334
335 ObIconPopup *icon_popup_new(void)
336 {
337     ObIconPopup *self;
338
339     self = g_new0(ObIconPopup, 1);
340     self->popup = popup_new();
341     self->a_icon = RrAppearanceCopy(ob_rr_theme->a_clear_tex);
342     self->icon = XCreateWindow(ob_display, self->popup->bg,
343                                0, 0, 1, 1, 0,
344                                RrDepth(ob_rr_inst), InputOutput,
345                                RrVisual(ob_rr_inst), 0, NULL);
346     XMapWindow(ob_display, self->icon);
347
348     self->popup->hasicon = TRUE;
349     self->popup->draw_icon = icon_popup_draw_icon;
350     self->popup->draw_icon_data = self;
351
352     return self;
353 }
354
355 void icon_popup_free(ObIconPopup *self)
356 {
357     if (self) {
358         XDestroyWindow(ob_display, self->icon);
359         RrAppearanceFree(self->a_icon);
360         popup_free(self->popup);
361         g_free(self);
362     }
363 }
364
365 void icon_popup_delay_show(ObIconPopup *self, gulong usec,
366                            gchar *text, const ObClientIcon *icon)
367 {
368     if (icon) {
369         self->a_icon->texture[0].type = RR_TEXTURE_RGBA;
370         self->a_icon->texture[0].data.rgba.width = icon->width;
371         self->a_icon->texture[0].data.rgba.height = icon->height;
372         self->a_icon->texture[0].data.rgba.alpha = 0xff;
373         self->a_icon->texture[0].data.rgba.data = icon->data;
374     } else
375         self->a_icon->texture[0].type = RR_TEXTURE_NONE;
376
377     popup_delay_show(self->popup, usec, text);
378 }
379
380 void icon_popup_icon_size_multiplier(ObIconPopup *self, guint wm, guint hm)
381 {
382     /* cap them at 1 */
383     self->popup->iconwm = MAX(1, wm);
384     self->popup->iconhm = MAX(1, hm);
385 }
386
387 static void pager_popup_draw_icon(gint px, gint py, gint w, gint h,
388                                   gpointer data)
389 {
390     ObPagerPopup *self = data;
391     gint x, y;
392     guint rown, n;
393     guint horz_inc;
394     guint vert_inc;
395     guint r, c;
396     gint eachw, eachh;
397     const guint cols = screen_desktop_layout.columns;
398     const guint rows = screen_desktop_layout.rows;
399     const gint linewidth = ob_rr_theme->obwidth;
400
401     eachw = (w - ((cols + 1) * linewidth)) / cols;
402     eachh = (h - ((rows + 1) * linewidth)) / rows;
403     /* make them squares */
404     eachw = eachh = MIN(eachw, eachh);
405
406     /* center */
407     px += (w - (cols * (eachw + linewidth) + linewidth)) / 2;
408     py += (h - (rows * (eachh + linewidth) + linewidth)) / 2;
409
410     if (eachw <= 0 || eachh <= 0)
411         return;
412
413     switch (screen_desktop_layout.orientation) {
414     case OB_ORIENTATION_HORZ:
415         switch (screen_desktop_layout.start_corner) {
416         case OB_CORNER_TOPLEFT:
417             n = 0;
418             horz_inc = 1;
419             vert_inc = cols;
420             break;
421         case OB_CORNER_TOPRIGHT:
422             n = cols - 1;
423             horz_inc = -1;
424             vert_inc = cols;
425             break;
426         case OB_CORNER_BOTTOMRIGHT:
427             n = rows * cols - 1;
428             horz_inc = -1;
429             vert_inc = -screen_desktop_layout.columns;
430             break;
431         case OB_CORNER_BOTTOMLEFT:
432             n = (rows - 1) * cols;
433             horz_inc = 1;
434             vert_inc = -cols;
435             break;
436         default:
437             g_assert_not_reached();
438         }
439         break;
440     case OB_ORIENTATION_VERT:
441         switch (screen_desktop_layout.start_corner) {
442         case OB_CORNER_TOPLEFT:
443             n = 0;
444             horz_inc = rows;
445             vert_inc = 1;
446             break;
447         case OB_CORNER_TOPRIGHT:
448             n = rows * (cols - 1);
449             horz_inc = -rows;
450             vert_inc = 1;
451             break;
452         case OB_CORNER_BOTTOMRIGHT:
453             n = rows * cols - 1;
454             horz_inc = -rows;
455             vert_inc = -1;
456             break;
457         case OB_CORNER_BOTTOMLEFT:
458             n = rows - 1;
459             horz_inc = rows;
460             vert_inc = -1;
461             break;
462         default:
463             g_assert_not_reached();
464         }
465         break;
466     default:
467         g_assert_not_reached();
468     }
469
470     rown = n;
471     for (r = 0, y = 0; r < rows; ++r, y += eachh + linewidth)
472     {
473         for (c = 0, x = 0; c < cols; ++c, x += eachw + linewidth)
474         {
475             RrAppearance *a;
476
477             if (n < self->desks) {
478                 a = (n == self->curdesk ? self->hilight : self->unhilight);
479
480                 a->surface.parent = self->popup->a_bg;
481                 a->surface.parentx = x + px;
482                 a->surface.parenty = y + py;
483                 XMoveResizeWindow(ob_display, self->wins[n],
484                                   x + px, y + py, eachw, eachh);
485                 RrPaint(a, self->wins[n], eachw, eachh);
486             }
487             n += horz_inc;
488         }
489         n = rown += vert_inc;
490     }
491 }
492
493 ObPagerPopup *pager_popup_new(void)
494 {
495     ObPagerPopup *self;
496
497     self = g_new(ObPagerPopup, 1);
498     self->popup = popup_new();
499
500     self->desks = 0;
501     self->wins = g_new(Window, self->desks);
502     self->hilight = RrAppearanceCopy(ob_rr_theme->osd_hilite_fg);
503     self->unhilight = RrAppearanceCopy(ob_rr_theme->osd_unhilite_fg);
504
505     self->popup->hasicon = TRUE;
506     self->popup->draw_icon = pager_popup_draw_icon;
507     self->popup->draw_icon_data = self;
508
509     return self;
510 }
511
512 void pager_popup_free(ObPagerPopup *self)
513 {
514     if (self) {
515         guint i;
516
517         for (i = 0; i < self->desks; ++i)
518             XDestroyWindow(ob_display, self->wins[i]);
519         g_free(self->wins);
520         RrAppearanceFree(self->hilight);
521         RrAppearanceFree(self->unhilight);
522         popup_free(self->popup);
523         g_free(self);
524     }
525 }
526
527 void pager_popup_delay_show(ObPagerPopup *self, gulong usec,
528                             gchar *text, guint desk)
529 {
530     guint i;
531
532     if (screen_num_desktops < self->desks)
533         for (i = screen_num_desktops; i < self->desks; ++i)
534             XDestroyWindow(ob_display, self->wins[i]);
535
536     if (screen_num_desktops != self->desks)
537         self->wins = g_renew(Window, self->wins, screen_num_desktops);
538
539     if (screen_num_desktops > self->desks)
540         for (i = self->desks; i < screen_num_desktops; ++i) {
541             XSetWindowAttributes attr;
542
543             attr.border_pixel =
544                 RrColorPixel(ob_rr_theme->osd_border_color);
545             self->wins[i] = XCreateWindow(ob_display, self->popup->bg,
546                                           0, 0, 1, 1, ob_rr_theme->obwidth,
547                                           RrDepth(ob_rr_inst), InputOutput,
548                                           RrVisual(ob_rr_inst), CWBorderPixel,
549                                           &attr);
550             XMapWindow(ob_display, self->wins[i]);
551         }
552
553     self->desks = screen_num_desktops;
554     self->curdesk = desk;
555
556     popup_delay_show(self->popup, usec, text);
557 }
558
559 void pager_popup_icon_size_multiplier(ObPagerPopup *self, guint wm, guint hm)
560 {
561     /* cap them at 1 */
562     self->popup->iconwm = MAX(1, wm);
563     self->popup->iconhm = MAX(1, hm);
564 }