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