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