]> Dogcows Code - chaz/openbox/blob - openbox/popup.c
add XFlush to g_timeout callbacks
[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 "obrender/render.h"
29 #include "obrender/theme.h"
30
31 ObPopup *popup_new(void)
32 {
33 XSetWindowAttributes attrib;
34 ObPopup *self = g_slice_new0(ObPopup);
35
36 self->obwin.type = OB_WINDOW_CLASS_INTERNAL;
37 self->gravity = NorthWestGravity;
38 self->x = self->y = self->textw = self->h = 0;
39 self->a_bg = RrAppearanceCopy(ob_rr_theme->osd_bg);
40 self->a_text = RrAppearanceCopy(ob_rr_theme->osd_hilite_label);
41 self->iconwm = self->iconhm = 1;
42
43 attrib.override_redirect = True;
44 self->bg = XCreateWindow(obt_display, obt_root(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(obt_display, self->bg,
50 0, 0, 1, 1, 0, RrDepth(ob_rr_inst),
51 InputOutput, RrVisual(ob_rr_inst), 0, NULL);
52
53 XSetWindowBorderWidth(obt_display, self->bg, ob_rr_theme->obwidth);
54 XSetWindowBorder(obt_display, self->bg,
55 RrColorPixel(ob_rr_theme->osd_border_color));
56
57 XMapWindow(obt_display, self->text);
58
59 stacking_add(INTERNAL_AS_WINDOW(self));
60 window_add(&self->bg, INTERNAL_AS_WINDOW(self));
61 return self;
62 }
63
64 void popup_free(ObPopup *self)
65 {
66 if (self) {
67 popup_hide(self); /* make sure it's not showing or is being delayed and
68 will be shown */
69 XDestroyWindow(obt_display, self->bg);
70 XDestroyWindow(obt_display, self->text);
71 RrAppearanceFree(self->a_bg);
72 RrAppearanceFree(self->a_text);
73 window_remove(self->bg);
74 stacking_remove(self);
75 g_slice_free(ObPopup, self);
76 }
77 }
78
79 void popup_position(ObPopup *self, gint gravity, gint x, gint y)
80 {
81 self->gravity = gravity;
82 self->x = x;
83 self->y = y;
84 }
85
86 void popup_text_width(ObPopup *self, gint w)
87 {
88 self->textw = w;
89 }
90
91 void popup_min_width(ObPopup *self, gint minw)
92 {
93 self->minw = minw;
94 }
95
96 void popup_max_width(ObPopup *self, gint maxw)
97 {
98 self->maxw = maxw;
99 }
100
101 void popup_height(ObPopup *self, gint h)
102 {
103 gint texth;
104
105 /* don't let the height be smaller than the text */
106 texth = RrMinHeight(self->a_text) + ob_rr_theme->paddingy * 2;
107 self->h = MAX(h, texth);
108 }
109
110 void popup_text_width_to_string(ObPopup *self, gchar *text)
111 {
112 if (text[0] != '\0') {
113 self->a_text->texture[0].data.text.string = text;
114 self->textw = RrMinWidth(self->a_text);
115 } else
116 self->textw = 0;
117 }
118
119 void popup_height_to_string(ObPopup *self, gchar *text)
120 {
121 self->h = RrMinHeight(self->a_text) + ob_rr_theme->paddingy * 2;
122 }
123
124 void popup_text_width_to_strings(ObPopup *self, gchar **strings, gint num)
125 {
126 gint i, maxw;
127
128 maxw = 0;
129 for (i = 0; i < num; ++i) {
130 popup_text_width_to_string(self, strings[i]);
131 maxw = MAX(maxw, self->textw);
132 }
133 self->textw = maxw;
134 }
135
136 void popup_set_text_align(ObPopup *self, RrJustify align)
137 {
138 self->a_text->texture[0].data.text.justify = align;
139 }
140
141 static gboolean popup_show_timeout(gpointer data)
142 {
143 ObPopup *self = data;
144
145 XMapWindow(obt_display, self->bg);
146 stacking_raise(INTERNAL_AS_WINDOW(self));
147 self->mapped = TRUE;
148 self->delay_mapped = FALSE;
149 self->delay_timer = 0;
150
151 XFlush(obt_display);
152 return FALSE; /* don't repeat */
153 }
154
155 void popup_delay_show(ObPopup *self, gulong msec, gchar *text)
156 {
157 gint l, t, r, b;
158 gint x, y, w, h;
159 guint m;
160 gint emptyx, emptyy; /* empty space between elements */
161 gint textx, texty, textw, texth;
162 gint iconx, icony, iconw, iconh;
163 const Rect *area;
164 Rect mon;
165 gboolean hasicon = self->hasicon;
166
167 /* when there is no icon and the text is not parent relative, then
168 fill the whole dialog with the text appearance, don't use the bg at all
169 */
170 if (hasicon || self->a_text->surface.grad == RR_SURFACE_PARENTREL)
171 RrMargins(self->a_bg, &l, &t, &r, &b);
172 else
173 l = t = r = b = 0;
174
175 /* set up the textures */
176 self->a_text->texture[0].data.text.string = text;
177
178 /* measure the text out */
179 if (text[0] != '\0') {
180 RrMinSize(self->a_text, &textw, &texth);
181 } else {
182 textw = 0;
183 texth = RrMinHeight(self->a_text);
184 }
185
186 /* get the height, which is also used for the icon width */
187 emptyy = t + b + ob_rr_theme->paddingy * 2;
188 if (self->h)
189 texth = self->h - emptyy;
190 h = texth * self->iconhm + emptyy;
191
192 if (self->textw)
193 textw = self->textw;
194
195 iconx = textx = l + ob_rr_theme->paddingx;
196
197 emptyx = l + r + ob_rr_theme->paddingx * 2;
198 if (hasicon) {
199 iconw = texth * self->iconwm;
200 iconh = texth * self->iconhm;
201 textx += iconw + ob_rr_theme->paddingx;
202 if (textw)
203 emptyx += ob_rr_theme->paddingx; /* between the icon and text */
204 } else
205 iconw = 0;
206
207 texty = (h - texth - emptyy) / 2 + t + ob_rr_theme->paddingy;
208 icony = (h - iconh - emptyy) / 2 + t + ob_rr_theme->paddingy;
209
210 /* when there is no icon, then fill the whole dialog with the text
211 appearance
212 */
213 if (!hasicon)
214 {
215 textx = texty = 0;
216 texth += emptyy;
217 textw += emptyx;
218 emptyx = emptyy = 0;
219 }
220
221 w = textw + emptyx + iconw;
222 /* cap it at maxw/minw */
223 if (self->maxw) w = MIN(w, self->maxw);
224 if (self->minw) w = MAX(w, self->minw);
225 textw = w - emptyx - iconw;
226
227 /* sanity checks to avoid crashes! */
228 if (w < 1) w = 1;
229 if (h < 1) h = 1;
230 if (texth < 1) texth = 1;
231
232 /* set up the x coord */
233 x = self->x;
234 switch (self->gravity) {
235 case NorthGravity: case CenterGravity: case SouthGravity:
236 x -= w / 2;
237 break;
238 case NorthEastGravity: case EastGravity: case SouthEastGravity:
239 x -= w;
240 break;
241 }
242
243 /* set up the y coord */
244 y = self->y;
245 switch (self->gravity) {
246 case WestGravity: case CenterGravity: case EastGravity:
247 y -= h / 2;
248 break;
249 case SouthWestGravity: case SouthGravity: case SouthEastGravity:
250 y -= h;
251 break;
252 }
253
254 /* Find the monitor which contains the biggest part of the popup.
255 * If the popup is completely off screen, limit it to the intersection
256 * of all monitors and then try again. If it's still off screen, put it
257 * on monitor 0. */
258 RECT_SET(mon, x, y, w, h);
259 m = screen_find_monitor(&mon);
260 area = screen_physical_area_monitor(m);
261
262 x=MAX(MIN(x, area->x+area->width-w),area->x);
263 y=MAX(MIN(y, area->y+area->height-h),area->y);
264
265 if (m == screen_num_monitors) {
266 RECT_SET(mon, x, y, w, h);
267 m = screen_find_monitor(&mon);
268 if (m == screen_num_monitors)
269 m = 0;
270 area = screen_physical_area_monitor(m);
271
272 x=MAX(MIN(x, area->x+area->width-w),area->x);
273 y=MAX(MIN(y, area->y+area->height-h),area->y);
274 }
275
276 /* set the windows/appearances up */
277 XMoveResizeWindow(obt_display, self->bg, x, y, w, h);
278 /* when there is no icon and the text is not parent relative, then
279 fill the whole dialog with the text appearance, don't use the bg at all
280 */
281 if (hasicon || self->a_text->surface.grad == RR_SURFACE_PARENTREL)
282 RrPaint(self->a_bg, self->bg, w, h);
283
284 if (textw) {
285 self->a_text->surface.parent = self->a_bg;
286 self->a_text->surface.parentx = textx;
287 self->a_text->surface.parenty = texty;
288 XMoveResizeWindow(obt_display, self->text, textx, texty, textw, texth);
289 RrPaint(self->a_text, self->text, textw, texth);
290 }
291
292 if (hasicon)
293 self->draw_icon(iconx, icony, iconw, iconh, self->draw_icon_data);
294
295 /* do the actual showing */
296 if (!self->mapped) {
297 if (msec) {
298 /* don't kill previous show timers */
299 if (!self->delay_mapped) {
300 self->delay_timer =
301 g_timeout_add(msec, popup_show_timeout, self);
302 self->delay_mapped = TRUE;
303 }
304 } else {
305 popup_show_timeout(self);
306 }
307 }
308 }
309
310 void popup_hide(ObPopup *self)
311 {
312 if (self->mapped) {
313 gulong ignore_start;
314
315 /* kill enter events cause by this unmapping */
316 ignore_start = event_start_ignore_all_enters();
317
318 XUnmapWindow(obt_display, self->bg);
319 self->mapped = FALSE;
320
321 event_end_ignore_all_enters(ignore_start);
322 } else if (self->delay_mapped) {
323 g_source_remove(self->delay_timer);
324 self->delay_timer = 0;
325 self->delay_mapped = FALSE;
326 }
327 }
328
329 static void icon_popup_draw_icon(gint x, gint y, gint w, gint h, gpointer data)
330 {
331 ObIconPopup *self = data;
332
333 self->a_icon->surface.parent = self->popup->a_bg;
334 self->a_icon->surface.parentx = x;
335 self->a_icon->surface.parenty = y;
336 XMoveResizeWindow(obt_display, self->icon, x, y, w, h);
337 RrPaint(self->a_icon, self->icon, w, h);
338 }
339
340 ObIconPopup *icon_popup_new(void)
341 {
342 ObIconPopup *self;
343
344 self = g_slice_new0(ObIconPopup);
345 self->popup = popup_new();
346 self->a_icon = RrAppearanceCopy(ob_rr_theme->a_clear_tex);
347 self->icon = XCreateWindow(obt_display, self->popup->bg,
348 0, 0, 1, 1, 0,
349 RrDepth(ob_rr_inst), InputOutput,
350 RrVisual(ob_rr_inst), 0, NULL);
351 XMapWindow(obt_display, self->icon);
352
353 self->popup->hasicon = TRUE;
354 self->popup->draw_icon = icon_popup_draw_icon;
355 self->popup->draw_icon_data = self;
356
357 return self;
358 }
359
360 void icon_popup_free(ObIconPopup *self)
361 {
362 if (self) {
363 XDestroyWindow(obt_display, self->icon);
364 RrAppearanceFree(self->a_icon);
365 popup_free(self->popup);
366 g_slice_free(ObIconPopup, self);
367 }
368 }
369
370 void icon_popup_delay_show(ObIconPopup *self, gulong msec,
371 gchar *text, RrImage *icon)
372 {
373 if (icon) {
374 RrAppearanceClearTextures(self->a_icon);
375 self->a_icon->texture[0].type = RR_TEXTURE_IMAGE;
376 self->a_icon->texture[0].data.image.alpha = 0xff;
377 self->a_icon->texture[0].data.image.image = icon;
378 } else {
379 RrAppearanceClearTextures(self->a_icon);
380 self->a_icon->texture[0].type = RR_TEXTURE_NONE;
381 }
382
383 popup_delay_show(self->popup, msec, text);
384 }
385
386 void icon_popup_icon_size_multiplier(ObIconPopup *self, guint wm, guint hm)
387 {
388 /* cap them at 1 */
389 self->popup->iconwm = MAX(1, wm);
390 self->popup->iconhm = MAX(1, hm);
391 }
392
393 static void pager_popup_draw_icon(gint px, gint py, gint w, gint h,
394 gpointer data)
395 {
396 ObPagerPopup *self = data;
397 gint x, y;
398 guint rown, n;
399 guint horz_inc;
400 guint vert_inc;
401 guint r, c;
402 gint eachw, eachh;
403 const guint cols = screen_desktop_layout.columns;
404 const guint rows = screen_desktop_layout.rows;
405 const gint linewidth = ob_rr_theme->obwidth;
406
407 eachw = (w - ((cols + 1) * linewidth)) / cols;
408 eachh = (h - ((rows + 1) * linewidth)) / rows;
409 /* make them squares */
410 eachw = eachh = MIN(eachw, eachh);
411
412 /* center */
413 px += (w - (cols * (eachw + linewidth) + linewidth)) / 2;
414 py += (h - (rows * (eachh + linewidth) + linewidth)) / 2;
415
416 if (eachw <= 0 || eachh <= 0)
417 return;
418
419 switch (screen_desktop_layout.orientation) {
420 case OB_ORIENTATION_HORZ:
421 switch (screen_desktop_layout.start_corner) {
422 case OB_CORNER_TOPLEFT:
423 n = 0;
424 horz_inc = 1;
425 vert_inc = cols;
426 break;
427 case OB_CORNER_TOPRIGHT:
428 n = cols - 1;
429 horz_inc = -1;
430 vert_inc = cols;
431 break;
432 case OB_CORNER_BOTTOMRIGHT:
433 n = rows * cols - 1;
434 horz_inc = -1;
435 vert_inc = -screen_desktop_layout.columns;
436 break;
437 case OB_CORNER_BOTTOMLEFT:
438 n = (rows - 1) * cols;
439 horz_inc = 1;
440 vert_inc = -cols;
441 break;
442 default:
443 g_assert_not_reached();
444 }
445 break;
446 case OB_ORIENTATION_VERT:
447 switch (screen_desktop_layout.start_corner) {
448 case OB_CORNER_TOPLEFT:
449 n = 0;
450 horz_inc = rows;
451 vert_inc = 1;
452 break;
453 case OB_CORNER_TOPRIGHT:
454 n = rows * (cols - 1);
455 horz_inc = -rows;
456 vert_inc = 1;
457 break;
458 case OB_CORNER_BOTTOMRIGHT:
459 n = rows * cols - 1;
460 horz_inc = -rows;
461 vert_inc = -1;
462 break;
463 case OB_CORNER_BOTTOMLEFT:
464 n = rows - 1;
465 horz_inc = rows;
466 vert_inc = -1;
467 break;
468 default:
469 g_assert_not_reached();
470 }
471 break;
472 default:
473 g_assert_not_reached();
474 }
475
476 rown = n;
477 for (r = 0, y = 0; r < rows; ++r, y += eachh + linewidth)
478 {
479 for (c = 0, x = 0; c < cols; ++c, x += eachw + linewidth)
480 {
481 RrAppearance *a;
482
483 if (n < self->desks) {
484 a = (n == self->curdesk ? self->hilight : self->unhilight);
485
486 a->surface.parent = self->popup->a_bg;
487 a->surface.parentx = x + px;
488 a->surface.parenty = y + py;
489 XMoveResizeWindow(obt_display, self->wins[n],
490 x + px, y + py, eachw, eachh);
491 RrPaint(a, self->wins[n], eachw, eachh);
492 }
493 n += horz_inc;
494 }
495 n = rown += vert_inc;
496 }
497 }
498
499 ObPagerPopup *pager_popup_new(void)
500 {
501 ObPagerPopup *self;
502
503 self = g_slice_new(ObPagerPopup);
504 self->popup = popup_new();
505
506 self->desks = 0;
507 self->wins = g_new(Window, self->desks);
508 self->hilight = RrAppearanceCopy(ob_rr_theme->osd_hilite_bg);
509 self->unhilight = RrAppearanceCopy(ob_rr_theme->osd_unhilite_bg);
510
511 self->popup->hasicon = TRUE;
512 self->popup->draw_icon = pager_popup_draw_icon;
513 self->popup->draw_icon_data = self;
514
515 return self;
516 }
517
518 void pager_popup_free(ObPagerPopup *self)
519 {
520 if (self) {
521 guint i;
522
523 for (i = 0; i < self->desks; ++i)
524 XDestroyWindow(obt_display, self->wins[i]);
525 g_free(self->wins);
526 RrAppearanceFree(self->hilight);
527 RrAppearanceFree(self->unhilight);
528 popup_free(self->popup);
529 g_slice_free(ObPagerPopup, self);
530 }
531 }
532
533 void pager_popup_delay_show(ObPagerPopup *self, gulong msec,
534 gchar *text, guint desk)
535 {
536 guint i;
537
538 if (screen_num_desktops < self->desks)
539 for (i = screen_num_desktops; i < self->desks; ++i)
540 XDestroyWindow(obt_display, self->wins[i]);
541
542 if (screen_num_desktops != self->desks)
543 self->wins = g_renew(Window, self->wins, screen_num_desktops);
544
545 if (screen_num_desktops > self->desks)
546 for (i = self->desks; i < screen_num_desktops; ++i) {
547 XSetWindowAttributes attr;
548
549 attr.border_pixel =
550 RrColorPixel(ob_rr_theme->osd_border_color);
551 self->wins[i] = XCreateWindow(obt_display, self->popup->bg,
552 0, 0, 1, 1, ob_rr_theme->obwidth,
553 RrDepth(ob_rr_inst), InputOutput,
554 RrVisual(ob_rr_inst), CWBorderPixel,
555 &attr);
556 XMapWindow(obt_display, self->wins[i]);
557 }
558
559 self->desks = screen_num_desktops;
560 self->curdesk = desk;
561
562 popup_delay_show(self->popup, msec, text);
563 }
564
565 void pager_popup_icon_size_multiplier(ObPagerPopup *self, guint wm, guint hm)
566 {
567 /* cap them at 1 */
568 self->popup->iconwm = MAX(1, wm);
569 self->popup->iconhm = MAX(1, hm);
570 }
This page took 0.065764 seconds and 5 git commands to generate.