don't grab the pointer during interactive events. this allows you to alt-tab during...
[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        Ben 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 "screen.h"
27 #include "render/render.h"
28 #include "render/theme.h"
29
30 ObPopup *popup_new(gboolean hasicon)
31 {
32     XSetWindowAttributes attrib;
33     ObPopup *self = g_new0(ObPopup, 1);
34
35     self->obwin.type = Window_Internal;
36     self->hasicon = hasicon;
37     self->gravity = NorthWestGravity;
38     self->x = self->y = self->w = 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
42     attrib.override_redirect = True;
43     self->bg = XCreateWindow(ob_display, RootWindow(ob_display, ob_screen),
44                              0, 0, 1, 1, 0, RrDepth(ob_rr_inst),
45                              InputOutput, RrVisual(ob_rr_inst),
46                              CWOverrideRedirect, &attrib);
47     
48     self->text = XCreateWindow(ob_display, self->bg,
49                                0, 0, 1, 1, 0, RrDepth(ob_rr_inst),
50                                InputOutput, RrVisual(ob_rr_inst), 0, NULL);
51
52     XMapWindow(ob_display, self->text);
53
54     stacking_add(INTERNAL_AS_WINDOW(self));
55     return self;
56 }
57
58 void popup_free(ObPopup *self)
59 {
60     if (self) {
61         XDestroyWindow(ob_display, self->bg);
62         XDestroyWindow(ob_display, self->text);
63         RrAppearanceFree(self->a_bg);
64         RrAppearanceFree(self->a_text);
65         stacking_remove(self);
66         g_free(self);
67     }
68 }
69
70 void popup_position(ObPopup *self, gint gravity, gint x, gint y)
71 {
72     self->gravity = gravity;
73     self->x = x;
74     self->y = y;
75 }
76
77 void popup_size(ObPopup *self, gint w, gint h)
78 {
79     self->w = w;
80     self->h = h;
81 }
82
83 void popup_size_to_string(ObPopup *self, gchar *text)
84 {
85     gint textw, texth;
86     gint iconw;
87
88     self->a_text->texture[0].data.text.string = text;
89     RrMinsize(self->a_text, &textw, &texth);
90     /*XXX textw += ob_rr_theme->bevel * 2;*/
91     texth += ob_rr_theme->paddingy * 2;
92
93     self->h = texth + ob_rr_theme->paddingy * 2;
94     iconw = (self->hasicon ? texth : 0);
95     self->w = textw + iconw + ob_rr_theme->paddingx * (self->hasicon ? 3 : 2);
96 }
97
98 void popup_set_text_align(ObPopup *self, RrJustify align)
99 {
100     self->a_text->texture[0].data.text.justify = align;
101 }
102
103 void popup_show(ObPopup *self, gchar *text)
104 {
105     gint l, t, r, b;
106     gint x, y, w, h;
107     gint textw, texth;
108     gint iconw;
109     Rect *area; /* won't go outside this */
110
111     area = screen_physical_area();          /* XXX this should work quite
112                                                good, someone with xinerama,
113                                                and different resolutions on
114                                                screens? */
115
116     RrMargins(self->a_bg, &l, &t, &r, &b);
117
118     XSetWindowBorderWidth(ob_display, self->bg, ob_rr_theme->fbwidth);
119     XSetWindowBorder(ob_display, self->bg, ob_rr_theme->frame_b_color->pixel);
120
121     /* set up the textures */
122     self->a_text->texture[0].data.text.string = text;
123
124     /* measure the shit out */
125     RrMinsize(self->a_text, &textw, &texth);
126     /*XXX textw += ob_rr_theme->padding * 2;*/
127     texth += ob_rr_theme->paddingy * 2;
128
129     /* set the sizes up and reget the text sizes from the calculated
130        outer sizes */
131     if (self->h) {
132         h = self->h;
133         texth = h - (t+b + ob_rr_theme->paddingy * 2);
134     } else
135         h = t+b + texth + ob_rr_theme->paddingy * 2;
136     iconw = (self->hasicon ? texth : 0);
137     if (self->w) {
138         w = self->w;
139         textw = w - (l+r + iconw + ob_rr_theme->paddingx *
140                      (self->hasicon ? 3 : 2));
141     } else
142         w = l+r + textw + iconw + ob_rr_theme->paddingx *
143             (self->hasicon ? 3 : 2);
144     /* sanity checks to avoid crashes! */
145     if (w < 1) w = 1;
146     if (h < 1) h = 1;
147     if (textw < 1) textw = 1;
148     if (texth < 1) texth = 1;
149
150     /* set up the x coord */
151     x = self->x;
152     switch (self->gravity) {
153     case NorthGravity:
154     case CenterGravity:
155     case SouthGravity:
156         x -= w / 2;
157         break;
158     case NorthEastGravity:
159     case EastGravity:
160     case SouthEastGravity:
161         x -= w;
162         break;
163     }
164
165     /* set up the y coord */
166     y = self->y;
167     switch (self->gravity) {
168     case WestGravity:
169     case CenterGravity:
170     case EastGravity:
171         y -= h / 2;
172         break;
173     case SouthWestGravity:
174     case SouthGravity:
175     case SouthEastGravity:
176         y -= h;
177         break;
178     }
179
180     x=MAX(MIN(x, area->width-w),0);
181     y=MAX(MIN(y, area->height-h),0);
182
183     /* set the windows/appearances up */
184     XMoveResizeWindow(ob_display, self->bg, x, y, w, h);
185
186     self->a_text->surface.parent = self->a_bg;
187     self->a_text->surface.parentx = l + iconw +
188         ob_rr_theme->paddingx * (self->hasicon ? 2 : 1);
189     self->a_text->surface.parenty = t + ob_rr_theme->paddingy;
190     XMoveResizeWindow(ob_display, self->text,
191                       l + iconw + ob_rr_theme->paddingx *
192                       (self->hasicon ? 2 : 1),
193                       t + ob_rr_theme->paddingy, textw, texth);
194
195     RrPaint(self->a_bg, self->bg, w, h);
196     RrPaint(self->a_text, self->text, textw, texth);
197
198     if (self->hasicon) {
199         if (iconw < 1) iconw = 1; /* sanity check for crashes */
200         if (self->draw_icon)
201             self->draw_icon(l + ob_rr_theme->paddingx,
202                             t + ob_rr_theme->paddingy,
203                             iconw, texth, self->draw_icon_data);
204     }
205
206     if (!self->mapped) {
207         XMapWindow(ob_display, self->bg);
208         stacking_raise(INTERNAL_AS_WINDOW(self));
209         self->mapped = TRUE;
210     }
211 }
212
213 void popup_hide(ObPopup *self)
214 {
215     if (self->mapped) {
216         XEvent e;
217
218         XUnmapWindow(ob_display, self->bg);
219         self->mapped = FALSE;
220
221         /* kill enter events cause by this unmapping */
222         XSync(ob_display, FALSE);
223         while (XCheckTypedEvent(ob_display, EnterNotify, &e));
224     }
225 }
226
227 static void icon_popup_draw_icon(gint x, gint y, gint w, gint h, gpointer data)
228 {
229     ObIconPopup *self = data;
230
231     self->a_icon->surface.parent = self->popup->a_bg;
232     self->a_icon->surface.parentx = x;
233     self->a_icon->surface.parenty = y;
234     XMoveResizeWindow(ob_display, self->icon, x, y, w, h);
235     RrPaint(self->a_icon, self->icon, w, h);
236 }
237
238 ObIconPopup *icon_popup_new()
239 {
240     ObIconPopup *self;
241
242     self = g_new0(ObIconPopup, 1);
243     self->popup = popup_new(TRUE);
244     self->a_icon = RrAppearanceCopy(ob_rr_theme->a_clear_tex);
245     self->icon = XCreateWindow(ob_display, self->popup->bg,
246                                0, 0, 1, 1, 0,
247                                RrDepth(ob_rr_inst), InputOutput,
248                                RrVisual(ob_rr_inst), 0, NULL);
249     XMapWindow(ob_display, self->icon);
250
251     self->popup->draw_icon = icon_popup_draw_icon;
252     self->popup->draw_icon_data = self;
253
254     return self;
255 }
256
257 void icon_popup_free(ObIconPopup *self)
258 {
259     if (self) {
260         XDestroyWindow(ob_display, self->icon);
261         RrAppearanceFree(self->a_icon);
262         popup_free(self->popup);
263         g_free(self);
264     }
265 }
266
267 void icon_popup_show(ObIconPopup *self,
268                      gchar *text, const ObClientIcon *icon)
269 {
270     if (icon) {
271         self->a_icon->texture[0].type = RR_TEXTURE_RGBA;
272         self->a_icon->texture[0].data.rgba.width = icon->width;
273         self->a_icon->texture[0].data.rgba.height = icon->height;
274         self->a_icon->texture[0].data.rgba.data = icon->data;
275     } else
276         self->a_icon->texture[0].type = RR_TEXTURE_NONE;
277
278     popup_show(self->popup, text);
279 }
280
281 static void pager_popup_draw_icon(gint px, gint py, gint w, gint h,
282                                   gpointer data)
283 {
284     ObPagerPopup *self = data;
285     gint x, y;
286     guint rown, n;
287     guint horz_inc;
288     guint vert_inc;
289     guint r, c;
290     gint eachw, eachh;
291
292     eachw = (w - ob_rr_theme->fbwidth -
293              (screen_desktop_layout.columns * ob_rr_theme->fbwidth))
294         / screen_desktop_layout.columns;
295     eachh = (h - ob_rr_theme->fbwidth -
296              (screen_desktop_layout.rows * ob_rr_theme->fbwidth))
297         / screen_desktop_layout.rows;
298     /* make them squares */
299     eachw = eachh = MIN(eachw, eachh);
300
301     /* center */
302     px += (w - (screen_desktop_layout.columns * (eachw + ob_rr_theme->fbwidth) +
303                 ob_rr_theme->fbwidth)) / 2;
304     py += (h - (screen_desktop_layout.rows * (eachh + ob_rr_theme->fbwidth) +
305                 ob_rr_theme->fbwidth)) / 2;
306
307     if (eachw <= 0 || eachh <= 0)
308         return;
309
310     switch (screen_desktop_layout.orientation) {
311     case OB_ORIENTATION_HORZ:
312         switch (screen_desktop_layout.start_corner) {
313         case OB_CORNER_TOPLEFT:
314             n = 0;
315             horz_inc = 1;
316             vert_inc = screen_desktop_layout.columns;
317             break;
318         case OB_CORNER_TOPRIGHT:
319             n = screen_desktop_layout.columns - 1;
320             horz_inc = -1;
321             vert_inc = screen_desktop_layout.columns;
322             break;
323         case OB_CORNER_BOTTOMRIGHT:
324             n = screen_desktop_layout.rows * screen_desktop_layout.columns - 1;
325             horz_inc = -1;
326             vert_inc = -screen_desktop_layout.columns;
327             break;
328         case OB_CORNER_BOTTOMLEFT:
329             n = (screen_desktop_layout.rows - 1)
330                 * screen_desktop_layout.columns;
331             horz_inc = 1;
332             vert_inc = -screen_desktop_layout.columns;
333             break;
334         default:
335             g_assert_not_reached();
336         }
337         break;
338     case OB_ORIENTATION_VERT:
339         switch (screen_desktop_layout.start_corner) {
340         case OB_CORNER_TOPLEFT:
341             n = 0;
342             horz_inc = screen_desktop_layout.rows;
343             vert_inc = 1;
344             break;
345         case OB_CORNER_TOPRIGHT:
346             n = screen_desktop_layout.rows
347                 * (screen_desktop_layout.columns - 1);
348             horz_inc = -screen_desktop_layout.rows;
349             vert_inc = 1;
350             break;
351         case OB_CORNER_BOTTOMRIGHT:
352             n = screen_desktop_layout.rows * screen_desktop_layout.columns - 1;
353             horz_inc = -screen_desktop_layout.rows;
354             vert_inc = -1;
355             break;
356         case OB_CORNER_BOTTOMLEFT:
357             n = screen_desktop_layout.rows - 1;
358             horz_inc = screen_desktop_layout.rows;
359             vert_inc = -1;
360             break;
361         default:
362             g_assert_not_reached();
363         }
364         break;
365     default:
366         g_assert_not_reached();
367     }
368
369     rown = n;
370     for (r = 0, y = 0; r < screen_desktop_layout.rows;
371          ++r, y += eachh + ob_rr_theme->fbwidth)
372     {
373         for (c = 0, x = 0; c < screen_desktop_layout.columns;
374              ++c, x += eachw + ob_rr_theme->fbwidth)
375         {
376             RrAppearance *a;
377
378             if (n < self->desks) {
379                 a = (n == self->curdesk ? self->hilight : self->unhilight);
380
381                 a->surface.parent = self->popup->a_bg;
382                 a->surface.parentx = x + px;
383                 a->surface.parenty = y + py;
384                 XMoveResizeWindow(ob_display, self->wins[n],
385                                   x + px, y + py, eachw, eachh);
386                 RrPaint(a, self->wins[n], eachw, eachh);
387             }
388             n += horz_inc;
389         }
390         n = rown += vert_inc;
391     }
392 }
393
394 ObPagerPopup *pager_popup_new()
395 {
396     ObPagerPopup *self;
397
398     self = g_new(ObPagerPopup, 1);
399     self->popup = popup_new(TRUE);
400
401     self->desks = 0;
402     self->wins = g_new(Window, self->desks);
403     self->hilight = RrAppearanceCopy(ob_rr_theme->osd_hilite_fg);
404     self->unhilight = RrAppearanceCopy(ob_rr_theme->osd_unhilite_fg);
405
406     self->popup->draw_icon = pager_popup_draw_icon;
407     self->popup->draw_icon_data = self;
408
409     return self;
410 }
411
412 void pager_popup_free(ObPagerPopup *self)
413 {
414     if (self) {
415         guint i;
416
417         for (i = 0; i < self->desks; ++i)
418             XDestroyWindow(ob_display, self->wins[i]);
419         g_free(self->wins);
420         RrAppearanceFree(self->hilight);
421         RrAppearanceFree(self->unhilight);
422         popup_free(self->popup);
423         g_free(self);
424     }
425 }
426
427 void pager_popup_show(ObPagerPopup *self, gchar *text, guint desk)
428 {
429     guint i;
430
431     if (screen_num_desktops < self->desks)
432         for (i = screen_num_desktops; i < self->desks; ++i)
433             XDestroyWindow(ob_display, self->wins[i]);
434
435     if (screen_num_desktops != self->desks)
436         self->wins = g_renew(Window, self->wins, screen_num_desktops);
437
438     if (screen_num_desktops > self->desks)
439         for (i = self->desks; i < screen_num_desktops; ++i) {
440             XSetWindowAttributes attr;
441
442             attr.border_pixel = RrColorPixel(ob_rr_theme->frame_b_color);
443             self->wins[i] = XCreateWindow(ob_display, self->popup->bg,
444                                           0, 0, 1, 1, ob_rr_theme->fbwidth,
445                                           RrDepth(ob_rr_inst), InputOutput,
446                                           RrVisual(ob_rr_inst), CWBorderPixel,
447                                           &attr);
448             XMapWindow(ob_display, self->wins[i]);
449         }
450
451     self->desks = screen_num_desktops;
452     self->curdesk = desk;
453
454     popup_show(self->popup, text);
455 }