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