remove trailing whitespace
[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->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     gint emptyx, emptyy; /* empty space between elements */
155     gint textx, texty, textw, texth;
156     gint iconx, icony, iconw, iconh;
157     Rect *area, mon;
158
159     RECT_SET(mon, self->x, self->y, 1, 1);
160     area = screen_physical_area_monitor(screen_find_monitor(&mon));
161
162     /* when there is no icon and the text is not parent relative, then
163        fill the whole dialog with the text appearance, don't use the bg at all
164     */
165     if (self->hasicon || self->a_text->surface.grad == RR_SURFACE_PARENTREL)
166         RrMargins(self->a_bg, &l, &t, &r, &b);
167     else
168         l = t = r = b = 0;
169
170     /* set up the textures */
171     self->a_text->texture[0].data.text.string = text;
172
173     /* measure the text out */
174     if (text[0] != '\0') {
175         RrMinSize(self->a_text, &textw, &texth);
176     } else {
177         textw = 0;
178         texth = RrMinHeight(self->a_text);
179     }
180
181     /* get the height, which is also used for the icon width */
182     emptyy = t + b + ob_rr_theme->paddingy * 2;
183     if (self->h)
184         texth = self->h - emptyy;
185     h = texth * self->iconhm + emptyy;
186
187     if (self->textw)
188         textw = self->textw;
189
190     iconx = textx = l + ob_rr_theme->paddingx;
191
192     emptyx = l + r + ob_rr_theme->paddingx * 2;
193     if (self->hasicon) {
194         iconw = texth * self->iconwm;
195         iconh = texth * self->iconhm;
196         textx += iconw + ob_rr_theme->paddingx;
197         if (textw)
198             emptyx += ob_rr_theme->paddingx; /* between the icon and text */
199     } else
200         iconw = 0;
201
202     texty = (h - texth - emptyy) / 2 + t + ob_rr_theme->paddingy;
203     icony = (h - iconh - emptyy) / 2 + t + ob_rr_theme->paddingy;
204
205     /* when there is no icon, then fill the whole dialog with the text
206        appearance
207     */
208     if (!self->hasicon)
209     {
210         textx = texty = 0;
211         texth += emptyy;
212         textw += emptyx;
213         emptyx = emptyy = 0;
214     }
215
216     w = textw + emptyx + iconw;
217     /* cap it at maxw/minw */
218     if (self->maxw) w = MIN(w, self->maxw);
219     if (self->minw) w = MAX(w, self->minw);
220     textw = w - emptyx - iconw;
221
222     /* sanity checks to avoid crashes! */
223     if (w < 1) w = 1;
224     if (h < 1) h = 1;
225     if (texth < 1) texth = 1;
226
227     /* set up the x coord */
228     x = self->x;
229     switch (self->gravity) {
230     case NorthGravity: case CenterGravity: case SouthGravity:
231         x -= w / 2;
232         break;
233     case NorthEastGravity: case EastGravity: case SouthEastGravity:
234         x -= w;
235         break;
236     }
237
238     /* set up the y coord */
239     y = self->y;
240     switch (self->gravity) {
241     case WestGravity: case CenterGravity: case EastGravity:
242         y -= h / 2;
243         break;
244     case SouthWestGravity: case SouthGravity: case SouthEastGravity:
245         y -= h;
246         break;
247     }
248
249     x=MAX(MIN(x, area->width-w),0);
250     y=MAX(MIN(y, area->height-h),0);
251
252     /* set the windows/appearances up */
253     XMoveResizeWindow(ob_display, self->bg, x, y, w, h);
254     /* when there is no icon and the text is not parent relative, then
255        fill the whole dialog with the text appearance, don't use the bg at all
256     */
257     if (self->hasicon || self->a_text->surface.grad == RR_SURFACE_PARENTREL)
258         RrPaint(self->a_bg, self->bg, w, h);
259
260     if (textw) {
261         self->a_text->surface.parent = self->a_bg;
262         self->a_text->surface.parentx = textx;
263         self->a_text->surface.parenty = texty;
264         XMoveResizeWindow(ob_display, self->text, textx, texty, textw, texth);
265         RrPaint(self->a_text, self->text, textw, texth);
266     }
267
268     if (self->hasicon)
269         self->draw_icon(iconx, icony, iconw, iconh, self->draw_icon_data);
270
271     /* do the actual showing */
272     if (!self->mapped) {
273         if (usec) {
274             /* don't kill previous show timers */
275             if (!self->delay_mapped) {
276                 ob_main_loop_timeout_add(ob_main_loop, usec,
277                                          popup_show_timeout, self,
278                                          g_direct_equal, NULL);
279                 self->delay_mapped = TRUE;
280             }
281         } else {
282             popup_show_timeout(self);
283         }
284     }
285
286     g_free(area);
287 }
288
289 void popup_hide(ObPopup *self)
290 {
291     if (self->mapped) {
292         gulong ignore_start;
293
294         /* kill enter events cause by this unmapping */
295         ignore_start = event_start_ignore_all_enters();
296
297         XUnmapWindow(ob_display, self->bg);
298         self->mapped = FALSE;
299
300         event_end_ignore_all_enters(ignore_start);
301     } else if (self->delay_mapped) {
302         ob_main_loop_timeout_remove(ob_main_loop, popup_show_timeout);
303         self->delay_mapped = FALSE;
304     }
305 }
306
307 static void icon_popup_draw_icon(gint x, gint y, gint w, gint h, gpointer data)
308 {
309     ObIconPopup *self = data;
310
311     self->a_icon->surface.parent = self->popup->a_bg;
312     self->a_icon->surface.parentx = x;
313     self->a_icon->surface.parenty = y;
314     XMoveResizeWindow(ob_display, self->icon, x, y, w, h);
315     RrPaint(self->a_icon, self->icon, w, h);
316 }
317
318 ObIconPopup *icon_popup_new()
319 {
320     ObIconPopup *self;
321
322     self = g_new0(ObIconPopup, 1);
323     self->popup = popup_new(TRUE);
324     self->a_icon = RrAppearanceCopy(ob_rr_theme->a_clear_tex);
325     self->icon = XCreateWindow(ob_display, self->popup->bg,
326                                0, 0, 1, 1, 0,
327                                RrDepth(ob_rr_inst), InputOutput,
328                                RrVisual(ob_rr_inst), 0, NULL);
329     XMapWindow(ob_display, self->icon);
330
331     self->popup->hasicon = TRUE;
332     self->popup->draw_icon = icon_popup_draw_icon;
333     self->popup->draw_icon_data = self;
334
335     return self;
336 }
337
338 void icon_popup_free(ObIconPopup *self)
339 {
340     if (self) {
341         XDestroyWindow(ob_display, self->icon);
342         RrAppearanceFree(self->a_icon);
343         popup_free(self->popup);
344         g_free(self);
345     }
346 }
347
348 void icon_popup_delay_show(ObIconPopup *self, gulong usec,
349                            gchar *text, const ObClientIcon *icon)
350 {
351     if (icon) {
352         self->a_icon->texture[0].type = RR_TEXTURE_RGBA;
353         self->a_icon->texture[0].data.rgba.width = icon->width;
354         self->a_icon->texture[0].data.rgba.height = icon->height;
355         self->a_icon->texture[0].data.rgba.alpha = 0xff;
356         self->a_icon->texture[0].data.rgba.data = icon->data;
357     } else
358         self->a_icon->texture[0].type = RR_TEXTURE_NONE;
359
360     popup_delay_show(self->popup, usec, text);
361 }
362
363 void icon_popup_icon_size_multiplier(ObIconPopup *self, guint wm, guint hm)
364 {
365     /* cap them at 1 */
366     self->popup->iconwm = MAX(1, wm);
367     self->popup->iconhm = MAX(1, hm);
368 }
369
370 static void pager_popup_draw_icon(gint px, gint py, gint w, gint h,
371                                   gpointer data)
372 {
373     ObPagerPopup *self = data;
374     gint x, y;
375     guint rown, n;
376     guint horz_inc;
377     guint vert_inc;
378     guint r, c;
379     gint eachw, eachh;
380     const guint cols = screen_desktop_layout.columns;
381     const guint rows = screen_desktop_layout.rows;
382     const gint linewidth = ob_rr_theme->obwidth;
383
384     eachw = (w - ((cols + 1) * linewidth)) / cols;
385     eachh = (h - ((rows + 1) * linewidth)) / rows;
386     /* make them squares */
387     eachw = eachh = MIN(eachw, eachh);
388
389     /* center */
390     px += (w - (cols * (eachw + linewidth) + linewidth)) / 2;
391     py += (h - (rows * (eachh + linewidth) + linewidth)) / 2;
392
393     if (eachw <= 0 || eachh <= 0)
394         return;
395
396     switch (screen_desktop_layout.orientation) {
397     case OB_ORIENTATION_HORZ:
398         switch (screen_desktop_layout.start_corner) {
399         case OB_CORNER_TOPLEFT:
400             n = 0;
401             horz_inc = 1;
402             vert_inc = cols;
403             break;
404         case OB_CORNER_TOPRIGHT:
405             n = cols - 1;
406             horz_inc = -1;
407             vert_inc = cols;
408             break;
409         case OB_CORNER_BOTTOMRIGHT:
410             n = rows * cols - 1;
411             horz_inc = -1;
412             vert_inc = -screen_desktop_layout.columns;
413             break;
414         case OB_CORNER_BOTTOMLEFT:
415             n = (rows - 1) * cols;
416             horz_inc = 1;
417             vert_inc = -cols;
418             break;
419         default:
420             g_assert_not_reached();
421         }
422         break;
423     case OB_ORIENTATION_VERT:
424         switch (screen_desktop_layout.start_corner) {
425         case OB_CORNER_TOPLEFT:
426             n = 0;
427             horz_inc = rows;
428             vert_inc = 1;
429             break;
430         case OB_CORNER_TOPRIGHT:
431             n = rows * (cols - 1);
432             horz_inc = -rows;
433             vert_inc = 1;
434             break;
435         case OB_CORNER_BOTTOMRIGHT:
436             n = rows * cols - 1;
437             horz_inc = -rows;
438             vert_inc = -1;
439             break;
440         case OB_CORNER_BOTTOMLEFT:
441             n = rows - 1;
442             horz_inc = rows;
443             vert_inc = -1;
444             break;
445         default:
446             g_assert_not_reached();
447         }
448         break;
449     default:
450         g_assert_not_reached();
451     }
452
453     rown = n;
454     for (r = 0, y = 0; r < rows; ++r, y += eachh + linewidth)
455     {
456         for (c = 0, x = 0; c < cols; ++c, x += eachw + linewidth)
457         {
458             RrAppearance *a;
459
460             if (n < self->desks) {
461                 a = (n == self->curdesk ? self->hilight : self->unhilight);
462
463                 a->surface.parent = self->popup->a_bg;
464                 a->surface.parentx = x + px;
465                 a->surface.parenty = y + py;
466                 XMoveResizeWindow(ob_display, self->wins[n],
467                                   x + px, y + py, eachw, eachh);
468                 RrPaint(a, self->wins[n], eachw, eachh);
469             }
470             n += horz_inc;
471         }
472         n = rown += vert_inc;
473     }
474 }
475
476 ObPagerPopup *pager_popup_new()
477 {
478     ObPagerPopup *self;
479
480     self = g_new(ObPagerPopup, 1);
481     self->popup = popup_new(TRUE);
482
483     self->desks = 0;
484     self->wins = g_new(Window, self->desks);
485     self->hilight = RrAppearanceCopy(ob_rr_theme->osd_hilite_fg);
486     self->unhilight = RrAppearanceCopy(ob_rr_theme->osd_unhilite_fg);
487
488     self->popup->hasicon = TRUE;
489     self->popup->draw_icon = pager_popup_draw_icon;
490     self->popup->draw_icon_data = self;
491
492     return self;
493 }
494
495 void pager_popup_free(ObPagerPopup *self)
496 {
497     if (self) {
498         guint i;
499
500         for (i = 0; i < self->desks; ++i)
501             XDestroyWindow(ob_display, self->wins[i]);
502         g_free(self->wins);
503         RrAppearanceFree(self->hilight);
504         RrAppearanceFree(self->unhilight);
505         popup_free(self->popup);
506         g_free(self);
507     }
508 }
509
510 void pager_popup_delay_show(ObPagerPopup *self, gulong usec,
511                             gchar *text, guint desk)
512 {
513     guint i;
514
515     if (screen_num_desktops < self->desks)
516         for (i = screen_num_desktops; i < self->desks; ++i)
517             XDestroyWindow(ob_display, self->wins[i]);
518
519     if (screen_num_desktops != self->desks)
520         self->wins = g_renew(Window, self->wins, screen_num_desktops);
521
522     if (screen_num_desktops > self->desks)
523         for (i = self->desks; i < screen_num_desktops; ++i) {
524             XSetWindowAttributes attr;
525
526             attr.border_pixel =
527                 RrColorPixel(ob_rr_theme->osd_border_color);
528             self->wins[i] = XCreateWindow(ob_display, self->popup->bg,
529                                           0, 0, 1, 1, ob_rr_theme->obwidth,
530                                           RrDepth(ob_rr_inst), InputOutput,
531                                           RrVisual(ob_rr_inst), CWBorderPixel,
532                                           &attr);
533             XMapWindow(ob_display, self->wins[i]);
534         }
535
536     self->desks = screen_num_desktops;
537     self->curdesk = desk;
538
539     popup_delay_show(self->popup, usec, text);
540 }
541
542 void pager_popup_icon_size_multiplier(ObPagerPopup *self, guint wm, guint hm)
543 {
544     /* cap them at 1 */
545     self->popup->iconwm = MAX(1, wm);
546     self->popup->iconhm = MAX(1, hm);
547 }