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