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