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