]> icculus.org git repositories - dana/openbox.git/blob - openbox/popup.c
make popup windows saveunder
[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) 2004        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->app_hilite_bg);
40     self->a_text = RrAppearanceCopy(ob_rr_theme->app_hilite_label);
41
42     attrib.override_redirect = True;
43     attrib.save_under = 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 | CWSaveUnder, &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->padding * 2;
93
94     self->h = texth + ob_rr_theme->padding * 2;
95     iconw = (self->hasicon ? texth : 0);
96     self->w = textw + iconw + ob_rr_theme->padding * (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->bwidth);
120     XSetWindowBorder(ob_display, self->bg, ob_rr_theme->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->padding * 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->padding * 2);
135     } else
136         h = t+b + texth + ob_rr_theme->padding * 2;
137     iconw = (self->hasicon ? texth : 0);
138     if (self->w) {
139         w = self->w;
140         textw = w - (l+r + iconw + ob_rr_theme->padding *
141                      (self->hasicon ? 3 : 2));
142     } else
143         w = l+r + textw + iconw + ob_rr_theme->padding *
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->padding * (self->hasicon ? 2 : 1);
190     self->a_text->surface.parenty = t + ob_rr_theme->padding;
191     XMoveResizeWindow(ob_display, self->text,
192                       l + iconw + ob_rr_theme->padding *
193                       (self->hasicon ? 2 : 1),
194                       t + ob_rr_theme->padding, 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->padding, t + ob_rr_theme->padding,
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), FALSE);
209         self->mapped = TRUE;
210     }
211 }
212
213 void popup_hide(ObPopup *self)
214 {
215     if (self->mapped) {
216         XUnmapWindow(ob_display, self->bg);
217         self->mapped = FALSE;
218     }
219 }
220
221 static void icon_popup_draw_icon(gint x, gint y, gint w, gint h, gpointer data)
222 {
223     ObIconPopup *self = data;
224
225     self->a_icon->surface.parent = self->popup->a_bg;
226     self->a_icon->surface.parentx = x;
227     self->a_icon->surface.parenty = y;
228     XMoveResizeWindow(ob_display, self->icon, x, y, w, h);
229     RrPaint(self->a_icon, self->icon, w, h);
230 }
231
232 ObIconPopup *icon_popup_new()
233 {
234     ObIconPopup *self;
235
236     self = g_new0(ObIconPopup, 1);
237     self->popup = popup_new(TRUE);
238     self->a_icon = RrAppearanceCopy(ob_rr_theme->a_clear_tex);
239     self->icon = XCreateWindow(ob_display, self->popup->bg,
240                                0, 0, 1, 1, 0,
241                                RrDepth(ob_rr_inst), InputOutput,
242                                RrVisual(ob_rr_inst), 0, NULL);
243     XMapWindow(ob_display, self->icon);
244
245     self->popup->draw_icon = icon_popup_draw_icon;
246     self->popup->draw_icon_data = self;
247
248     return self;
249 }
250
251 void icon_popup_free(ObIconPopup *self)
252 {
253     if (self) {
254         XDestroyWindow(ob_display, self->icon);
255         RrAppearanceFree(self->a_icon);
256         popup_free(self->popup);
257         g_free(self);
258     }
259 }
260
261 void icon_popup_show(ObIconPopup *self,
262                      gchar *text, const ObClientIcon *icon)
263 {
264     if (icon) {
265         self->a_icon->texture[0].type = RR_TEXTURE_RGBA;
266         self->a_icon->texture[0].data.rgba.width = icon->width;
267         self->a_icon->texture[0].data.rgba.height = icon->height;
268         self->a_icon->texture[0].data.rgba.data = icon->data;
269     } else
270         self->a_icon->texture[0].type = RR_TEXTURE_NONE;
271
272     popup_show(self->popup, text);
273 }
274
275 static void pager_popup_draw_icon(gint px, gint py, gint w, gint h,
276                                   gpointer data)
277 {
278     ObPagerPopup *self = data;
279     gint x, y;
280     guint rown, n;
281     guint horz_inc;
282     guint vert_inc;
283     guint r, c;
284     gint eachw, eachh;
285
286     eachw = (w - ob_rr_theme->bwidth -
287              (screen_desktop_layout.columns * ob_rr_theme->bwidth))
288         / screen_desktop_layout.columns;
289     eachh = (h - ob_rr_theme->bwidth -
290              (screen_desktop_layout.rows * ob_rr_theme->bwidth))
291         / screen_desktop_layout.rows;
292     /* make them squares */
293     eachw = eachh = MIN(eachw, eachh);
294
295     /* center */
296     px += (w - (screen_desktop_layout.columns * (eachw + ob_rr_theme->bwidth) +
297                 ob_rr_theme->bwidth)) / 2;
298     py += (h - (screen_desktop_layout.rows * (eachh + ob_rr_theme->bwidth) +
299                 ob_rr_theme->bwidth)) / 2;
300
301     if (eachw <= 0 || eachh <= 0)
302         return;
303
304     switch (screen_desktop_layout.orientation) {
305     case OB_ORIENTATION_HORZ:
306         switch (screen_desktop_layout.start_corner) {
307         case OB_CORNER_TOPLEFT:
308             n = 0;
309             horz_inc = 1;
310             vert_inc = screen_desktop_layout.columns;
311             break;
312         case OB_CORNER_TOPRIGHT:
313             n = screen_desktop_layout.columns - 1;
314             horz_inc = -1;
315             vert_inc = screen_desktop_layout.columns;
316             break;
317         case OB_CORNER_BOTTOMRIGHT:
318             n = screen_desktop_layout.rows * screen_desktop_layout.columns - 1;
319             horz_inc = -1;
320             vert_inc = -screen_desktop_layout.columns;
321             break;
322         case OB_CORNER_BOTTOMLEFT:
323             n = (screen_desktop_layout.rows - 1) * screen_desktop_layout.columns;
324             horz_inc = 1;
325             vert_inc = -screen_desktop_layout.columns;
326             break;
327         default:
328             g_assert_not_reached();
329         }
330         break;
331     case OB_ORIENTATION_VERT:
332         switch (screen_desktop_layout.start_corner) {
333         case OB_CORNER_TOPLEFT:
334             n = 0;
335             horz_inc = screen_desktop_layout.rows;
336             vert_inc = 1;
337             break;
338         case OB_CORNER_TOPRIGHT:
339             n = screen_desktop_layout.rows * (screen_desktop_layout.columns - 1);
340             horz_inc = -screen_desktop_layout.rows;
341             vert_inc = 1;
342             break;
343         case OB_CORNER_BOTTOMRIGHT:
344             n = screen_desktop_layout.rows * screen_desktop_layout.columns - 1;
345             horz_inc = -screen_desktop_layout.rows;
346             vert_inc = -1;
347             break;
348         case OB_CORNER_BOTTOMLEFT:
349             n = screen_desktop_layout.rows - 1;
350             horz_inc = screen_desktop_layout.rows;
351             vert_inc = -1;
352             break;
353         default:
354             g_assert_not_reached();
355         }
356         break;
357     default:
358         g_assert_not_reached();
359     }
360
361     rown = n;
362     for (r = 0, y = 0; r < screen_desktop_layout.rows;
363          ++r, y += eachh + ob_rr_theme->bwidth)
364     {
365         for (c = 0, x = 0; c < screen_desktop_layout.columns;
366              ++c, x += eachw + ob_rr_theme->bwidth)
367         {
368             RrAppearance *a;
369
370             if (n < self->desks) {
371                 a = (n == self->curdesk ? self->hilight : self->unhilight);
372
373                 a->surface.parent = self->popup->a_bg;
374                 a->surface.parentx = x + px;
375                 a->surface.parenty = y + py;
376                 XMoveResizeWindow(ob_display, self->wins[n],
377                                   x + px, y + py, eachw, eachh);
378                 RrPaint(a, self->wins[n], eachw, eachh);
379             }
380             n += horz_inc;
381         }
382         n = rown += vert_inc;
383     }
384 }
385
386 ObPagerPopup *pager_popup_new()
387 {
388     ObPagerPopup *self;
389
390     self = g_new(ObPagerPopup, 1);
391     self->popup = popup_new(TRUE);
392
393     self->desks = 0;
394     self->wins = g_new(Window, self->desks);
395     self->hilight = RrAppearanceCopy(ob_rr_theme->app_hilite_fg);
396     self->unhilight = RrAppearanceCopy(ob_rr_theme->app_unhilite_fg);
397
398     self->popup->draw_icon = pager_popup_draw_icon;
399     self->popup->draw_icon_data = self;
400
401     return self;
402 }
403
404 void pager_popup_free(ObPagerPopup *self)
405 {
406     if (self) {
407         guint i;
408
409         for (i = 0; i < self->desks; ++i)
410             XDestroyWindow(ob_display, self->wins[i]);
411         g_free(self->wins);
412         RrAppearanceFree(self->hilight);
413         RrAppearanceFree(self->unhilight);
414         popup_free(self->popup);
415         g_free(self);
416     }
417 }
418
419 void pager_popup_show(ObPagerPopup *self, gchar *text, guint desk)
420 {
421     guint i;
422
423     if (screen_num_desktops < self->desks)
424         for (i = screen_num_desktops; i < self->desks; ++i)
425             XDestroyWindow(ob_display, self->wins[i]);
426
427     if (screen_num_desktops != self->desks)
428         self->wins = g_renew(Window, self->wins, screen_num_desktops);
429
430     if (screen_num_desktops > self->desks)
431         for (i = self->desks; i < screen_num_desktops; ++i) {
432             XSetWindowAttributes attr;
433
434             attr.border_pixel = RrColorPixel(ob_rr_theme->b_color);
435             self->wins[i] = XCreateWindow(ob_display, self->popup->bg,
436                                           0, 0, 1, 1, ob_rr_theme->bwidth,
437                                           RrDepth(ob_rr_inst), InputOutput,
438                                           RrVisual(ob_rr_inst), CWBorderPixel,
439                                           &attr);
440             XMapWindow(ob_display, self->wins[i]);
441         }
442
443     self->desks = screen_num_desktops;
444     self->curdesk = desk;
445
446     popup_show(self->popup, text);
447 }