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