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