1 /* -*- indent-tabs-mode: nil; tab-width: 4; c-basic-offset: 4; -*-
3 popup.c for the Openbox window manager
4 Copyright (c) 2006 Mikael Magnusson
5 Copyright (c) 2003-2007 Dana Jansens
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.
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.
17 See the COPYING file for a copy of the GNU General Public License.
29 #include "render/render.h"
30 #include "render/theme.h"
32 static gboolean popup_show_timeout(gpointer data)
36 XMapWindow(ob_display, self->bg);
37 stacking_raise(INTERNAL_AS_WINDOW(self));
39 self->delay_mapped = FALSE;
41 return FALSE; /* don't repeat */
44 ObPopup *popup_new(gboolean hasicon)
46 XSetWindowAttributes attrib;
47 ObPopup *self = g_new0(ObPopup, 1);
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);
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);
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);
66 XMapWindow(ob_display, self->text);
68 stacking_add(INTERNAL_AS_WINDOW(self));
72 void popup_free(ObPopup *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);
84 void popup_position(ObPopup *self, gint gravity, gint x, gint y)
86 self->gravity = gravity;
91 void popup_width(ObPopup *self, gint w)
96 void popup_height(ObPopup *self, gint h)
100 /* don't let the height be smaller than the text */
101 texth = RrMinHeight(self->a_text) + ob_rr_theme->paddingy * 2;
102 self->h = MAX(h, texth);
105 void popup_width_to_string(ObPopup *self, gchar *text, gint max)
109 self->a_text->texture[0].data.text.string = text;
110 textw = RrMinWidth(self->a_text);
112 gint texth = RrMinHeight(self->a_text) + ob_rr_theme->paddingy * 2;
116 self->w = textw + iconw + ob_rr_theme->paddingx * (self->hasicon ? 3 : 2);
117 /* cap it at "max" */
119 self->w = MIN(self->w, max);
122 void popup_height_to_string(ObPopup *self, gchar *text)
124 self->h = RrMinHeight(self->a_text) + ob_rr_theme->paddingy * 2;
127 void popup_width_to_strings(ObPopup *self, gchar **strings, gint max)
132 for (i = 0; strings[i] != NULL; ++i) {
133 popup_width_to_string(self, strings[i], max);
134 maxw = MAX(maxw, self->w);
139 void popup_set_text_align(ObPopup *self, RrJustify align)
141 self->a_text->texture[0].data.text.justify = align;
144 void popup_delay_show(ObPopup *self, gulong usec, gchar *text)
150 Rect *area; /* won't go outside this */
152 area = screen_physical_area(); /* XXX this should work quite
153 good, someone with xinerama,
154 and different resolutions on
157 RrMargins(self->a_bg, &l, &t, &r, &b);
159 XSetWindowBorderWidth(ob_display, self->bg, ob_rr_theme->fbwidth);
160 XSetWindowBorder(ob_display, self->bg, ob_rr_theme->frame_b_color->pixel);
162 /* set up the textures */
163 self->a_text->texture[0].data.text.string = text;
165 /* measure the text out */
166 RrMinSize(self->a_text, &textw, &texth);
167 texth += ob_rr_theme->paddingy * 2;
169 /* set the sizes up and reget the text sizes from the calculated
173 texth = h - (t+b + ob_rr_theme->paddingy * 2);
175 h = t+b + texth + ob_rr_theme->paddingy * 2;
176 iconw = (self->hasicon ? texth : 0);
179 textw = w - (l+r + iconw + ob_rr_theme->paddingx *
180 (self->hasicon ? 3 : 2));
182 w = l+r + textw + iconw + ob_rr_theme->paddingx *
183 (self->hasicon ? 3 : 2);
184 /* sanity checks to avoid crashes! */
187 if (textw < 1) textw = 1;
188 if (texth < 1) texth = 1;
190 /* set up the x coord */
192 switch (self->gravity) {
198 case NorthEastGravity:
200 case SouthEastGravity:
205 /* set up the y coord */
207 switch (self->gravity) {
213 case SouthWestGravity:
215 case SouthEastGravity:
220 x=MAX(MIN(x, area->width-w),0);
221 y=MAX(MIN(y, area->height-h),0);
223 /* set the windows/appearances up */
224 XMoveResizeWindow(ob_display, self->bg, x, y, w, h);
226 self->a_text->surface.parent = self->a_bg;
227 self->a_text->surface.parentx = l + iconw +
228 ob_rr_theme->paddingx * (self->hasicon ? 2 : 1);
229 self->a_text->surface.parenty = t + ob_rr_theme->paddingy;
230 XMoveResizeWindow(ob_display, self->text,
231 l + iconw + ob_rr_theme->paddingx *
232 (self->hasicon ? 2 : 1),
233 t + ob_rr_theme->paddingy, textw, texth);
235 RrPaint(self->a_bg, self->bg, w, h);
236 RrPaint(self->a_text, self->text, textw, texth);
239 if (iconw < 1) iconw = 1; /* sanity check for crashes */
241 self->draw_icon(l + ob_rr_theme->paddingx,
242 t + ob_rr_theme->paddingy,
243 iconw, texth, self->draw_icon_data);
246 /* do the actual showing */
249 /* don't kill previous show timers */
250 if (!self->delay_mapped) {
251 ob_main_loop_timeout_add(ob_main_loop, usec,
252 popup_show_timeout, self,
253 g_direct_equal, NULL);
254 self->delay_mapped = TRUE;
257 popup_show_timeout(self);
262 void popup_hide(ObPopup *self)
265 XUnmapWindow(ob_display, self->bg);
266 self->mapped = FALSE;
268 /* kill enter events cause by this unmapping */
269 event_ignore_queued_enters();
270 } else if (self->delay_mapped) {
271 ob_main_loop_timeout_remove(ob_main_loop, popup_show_timeout);
272 self->delay_mapped = FALSE;
276 static void icon_popup_draw_icon(gint x, gint y, gint w, gint h, gpointer data)
278 ObIconPopup *self = data;
280 self->a_icon->surface.parent = self->popup->a_bg;
281 self->a_icon->surface.parentx = x;
282 self->a_icon->surface.parenty = y;
283 XMoveResizeWindow(ob_display, self->icon, x, y, w, h);
284 RrPaint(self->a_icon, self->icon, w, h);
287 ObIconPopup *icon_popup_new()
291 self = g_new0(ObIconPopup, 1);
292 self->popup = popup_new(TRUE);
293 self->a_icon = RrAppearanceCopy(ob_rr_theme->a_clear_tex);
294 self->icon = XCreateWindow(ob_display, self->popup->bg,
296 RrDepth(ob_rr_inst), InputOutput,
297 RrVisual(ob_rr_inst), 0, NULL);
298 XMapWindow(ob_display, self->icon);
300 self->popup->draw_icon = icon_popup_draw_icon;
301 self->popup->draw_icon_data = self;
306 void icon_popup_free(ObIconPopup *self)
309 XDestroyWindow(ob_display, self->icon);
310 RrAppearanceFree(self->a_icon);
311 popup_free(self->popup);
316 void icon_popup_delay_show(ObIconPopup *self, gulong usec,
317 gchar *text, const ObClientIcon *icon)
320 self->a_icon->texture[0].type = RR_TEXTURE_RGBA;
321 self->a_icon->texture[0].data.rgba.width = icon->width;
322 self->a_icon->texture[0].data.rgba.height = icon->height;
323 self->a_icon->texture[0].data.rgba.data = icon->data;
325 self->a_icon->texture[0].type = RR_TEXTURE_NONE;
327 popup_delay_show(self->popup, usec, text);
330 static void pager_popup_draw_icon(gint px, gint py, gint w, gint h,
333 ObPagerPopup *self = data;
341 eachw = (w - ob_rr_theme->fbwidth -
342 (screen_desktop_layout.columns * ob_rr_theme->fbwidth))
343 / screen_desktop_layout.columns;
344 eachh = (h - ob_rr_theme->fbwidth -
345 (screen_desktop_layout.rows * ob_rr_theme->fbwidth))
346 / screen_desktop_layout.rows;
347 /* make them squares */
348 eachw = eachh = MIN(eachw, eachh);
351 px += (w - (screen_desktop_layout.columns * (eachw + ob_rr_theme->fbwidth) +
352 ob_rr_theme->fbwidth)) / 2;
353 py += (h - (screen_desktop_layout.rows * (eachh + ob_rr_theme->fbwidth) +
354 ob_rr_theme->fbwidth)) / 2;
356 if (eachw <= 0 || eachh <= 0)
359 switch (screen_desktop_layout.orientation) {
360 case OB_ORIENTATION_HORZ:
361 switch (screen_desktop_layout.start_corner) {
362 case OB_CORNER_TOPLEFT:
365 vert_inc = screen_desktop_layout.columns;
367 case OB_CORNER_TOPRIGHT:
368 n = screen_desktop_layout.columns - 1;
370 vert_inc = screen_desktop_layout.columns;
372 case OB_CORNER_BOTTOMRIGHT:
373 n = screen_desktop_layout.rows * screen_desktop_layout.columns - 1;
375 vert_inc = -screen_desktop_layout.columns;
377 case OB_CORNER_BOTTOMLEFT:
378 n = (screen_desktop_layout.rows - 1)
379 * screen_desktop_layout.columns;
381 vert_inc = -screen_desktop_layout.columns;
384 g_assert_not_reached();
387 case OB_ORIENTATION_VERT:
388 switch (screen_desktop_layout.start_corner) {
389 case OB_CORNER_TOPLEFT:
391 horz_inc = screen_desktop_layout.rows;
394 case OB_CORNER_TOPRIGHT:
395 n = screen_desktop_layout.rows
396 * (screen_desktop_layout.columns - 1);
397 horz_inc = -screen_desktop_layout.rows;
400 case OB_CORNER_BOTTOMRIGHT:
401 n = screen_desktop_layout.rows * screen_desktop_layout.columns - 1;
402 horz_inc = -screen_desktop_layout.rows;
405 case OB_CORNER_BOTTOMLEFT:
406 n = screen_desktop_layout.rows - 1;
407 horz_inc = screen_desktop_layout.rows;
411 g_assert_not_reached();
415 g_assert_not_reached();
419 for (r = 0, y = 0; r < screen_desktop_layout.rows;
420 ++r, y += eachh + ob_rr_theme->fbwidth)
422 for (c = 0, x = 0; c < screen_desktop_layout.columns;
423 ++c, x += eachw + ob_rr_theme->fbwidth)
427 if (n < self->desks) {
428 a = (n == self->curdesk ? self->hilight : self->unhilight);
430 a->surface.parent = self->popup->a_bg;
431 a->surface.parentx = x + px;
432 a->surface.parenty = y + py;
433 XMoveResizeWindow(ob_display, self->wins[n],
434 x + px, y + py, eachw, eachh);
435 RrPaint(a, self->wins[n], eachw, eachh);
439 n = rown += vert_inc;
443 ObPagerPopup *pager_popup_new()
447 self = g_new(ObPagerPopup, 1);
448 self->popup = popup_new(TRUE);
451 self->wins = g_new(Window, self->desks);
452 self->hilight = RrAppearanceCopy(ob_rr_theme->osd_hilite_fg);
453 self->unhilight = RrAppearanceCopy(ob_rr_theme->osd_unhilite_fg);
455 self->popup->draw_icon = pager_popup_draw_icon;
456 self->popup->draw_icon_data = self;
461 void pager_popup_free(ObPagerPopup *self)
466 for (i = 0; i < self->desks; ++i)
467 XDestroyWindow(ob_display, self->wins[i]);
469 RrAppearanceFree(self->hilight);
470 RrAppearanceFree(self->unhilight);
471 popup_free(self->popup);
476 void pager_popup_delay_show(ObPagerPopup *self, gulong usec,
477 gchar *text, guint desk)
481 if (screen_num_desktops < self->desks)
482 for (i = screen_num_desktops; i < self->desks; ++i)
483 XDestroyWindow(ob_display, self->wins[i]);
485 if (screen_num_desktops != self->desks)
486 self->wins = g_renew(Window, self->wins, screen_num_desktops);
488 if (screen_num_desktops > self->desks)
489 for (i = self->desks; i < screen_num_desktops; ++i) {
490 XSetWindowAttributes attr;
492 attr.border_pixel = RrColorPixel(ob_rr_theme->frame_b_color);
493 self->wins[i] = XCreateWindow(ob_display, self->popup->bg,
494 0, 0, 1, 1, ob_rr_theme->fbwidth,
495 RrDepth(ob_rr_inst), InputOutput,
496 RrVisual(ob_rr_inst), CWBorderPixel,
498 XMapWindow(ob_display, self->wins[i]);
501 self->desks = screen_num_desktops;
502 self->curdesk = desk;
504 popup_delay_show(self->popup, usec, text);