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