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"
34 XSetWindowAttributes attrib;
35 ObPopup *self = g_new0(ObPopup, 1);
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;
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);
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);
54 XSetWindowBorderWidth(ob_display, self->bg, ob_rr_theme->fbwidth);
55 XSetWindowBorder(ob_display, self->bg, ob_rr_theme->frame_b_color->pixel);
57 XMapWindow(ob_display, self->text);
59 stacking_add(INTERNAL_AS_WINDOW(self));
63 void popup_free(ObPopup *self)
66 XDestroyWindow(ob_display, self->bg);
67 XDestroyWindow(ob_display, self->text);
68 RrAppearanceFree(self->a_bg);
69 RrAppearanceFree(self->a_text);
70 stacking_remove(self);
75 void popup_position(ObPopup *self, gint gravity, gint x, gint y)
77 self->gravity = gravity;
82 void popup_text_width(ObPopup *self, gint w)
87 void popup_min_width(ObPopup *self, gint minw)
92 void popup_max_width(ObPopup *self, gint maxw)
97 void popup_height(ObPopup *self, gint h)
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);
106 void popup_text_width_to_string(ObPopup *self, gchar *text)
108 if (text[0] != '\0') {
109 self->a_text->texture[0].data.text.string = text;
110 self->textw = RrMinWidth(self->a_text);
115 void popup_height_to_string(ObPopup *self, gchar *text)
117 self->h = RrMinHeight(self->a_text) + ob_rr_theme->paddingy * 2;
120 void popup_text_width_to_strings(ObPopup *self, gchar **strings, gint num)
125 for (i = 0; i < num; ++i) {
126 popup_text_width_to_string(self, strings[i]);
127 maxw = MAX(maxw, self->textw);
132 void popup_set_text_align(ObPopup *self, RrJustify align)
134 self->a_text->texture[0].data.text.justify = align;
137 static gboolean popup_show_timeout(gpointer data)
139 ObPopup *self = data;
141 XMapWindow(ob_display, self->bg);
142 stacking_raise(INTERNAL_AS_WINDOW(self));
144 self->delay_mapped = FALSE;
146 return FALSE; /* don't repeat */
149 void popup_delay_show(ObPopup *self, gulong usec, gchar *text)
153 gint emptyx, emptyy; /* empty space between elements */
154 gint textx, texty, textw, texth;
155 gint iconx, icony, iconw, iconh;
157 RrMargins(self->a_bg, &l, &t, &r, &b);
159 /* set up the textures */
160 self->a_text->texture[0].data.text.string = text;
162 /* measure the text out */
163 if (text[0] != '\0') {
164 RrMinSize(self->a_text, &textw, &texth);
167 texth = RrMinHeight(self->a_text);
170 /* get the height, which is also used for the icon width */
171 emptyy = t + b + ob_rr_theme->paddingy * 2;
173 texth = self->h - emptyy;
174 h = texth * self->iconhm + emptyy;
179 iconx = textx = l + ob_rr_theme->paddingx;
180 icony = texty = t + ob_rr_theme->paddingy;
182 emptyx = l + r + ob_rr_theme->paddingx * 2;
184 iconw = texth * self->iconwm;
185 iconh = texth * self->iconhm;
186 textx += iconw + ob_rr_theme->paddingx;
188 emptyx += ob_rr_theme->paddingx; /* between the icon and text */
192 w = textw + emptyx + iconw;
193 /* cap it at maxw/minw */
194 if (self->maxw) w = MIN(w, self->maxw);
195 if (self->minw) w = MAX(w, self->minw);
196 textw = w - emptyx - iconw;
198 /* sanity checks to avoid crashes! */
201 if (texth < 1) texth = 1;
203 /* set up the x coord */
205 switch (self->gravity) {
206 case NorthGravity: case CenterGravity: case SouthGravity:
209 case NorthEastGravity: case EastGravity: case SouthEastGravity:
214 /* set up the y coord */
216 switch (self->gravity) {
217 case WestGravity: case CenterGravity: case EastGravity:
220 case SouthWestGravity: case SouthGravity: case SouthEastGravity:
225 /* set the windows/appearances up */
226 XMoveResizeWindow(ob_display, self->bg, x, y, w, h);
227 RrPaint(self->a_bg, self->bg, w, h);
230 self->a_text->surface.parent = self->a_bg;
231 self->a_text->surface.parentx = textx;
232 self->a_text->surface.parenty = texty;
233 XMoveResizeWindow(ob_display, self->text, textx, texty, textw, texth);
234 RrPaint(self->a_text, self->text, textw, texth);
238 self->draw_icon(iconx, icony, iconw, iconh, self->draw_icon_data);
240 /* do the actual showing */
243 /* don't kill previous show timers */
244 if (!self->delay_mapped) {
245 ob_main_loop_timeout_add(ob_main_loop, usec,
246 popup_show_timeout, self,
247 g_direct_equal, NULL);
248 self->delay_mapped = TRUE;
251 popup_show_timeout(self);
256 void popup_hide(ObPopup *self)
259 XUnmapWindow(ob_display, self->bg);
260 self->mapped = FALSE;
262 /* kill enter events cause by this unmapping */
263 event_ignore_queued_enters();
264 } else if (self->delay_mapped) {
265 ob_main_loop_timeout_remove(ob_main_loop, popup_show_timeout);
266 self->delay_mapped = FALSE;
270 static void icon_popup_draw_icon(gint x, gint y, gint w, gint h, gpointer data)
272 ObIconPopup *self = data;
274 self->a_icon->surface.parent = self->popup->a_bg;
275 self->a_icon->surface.parentx = x;
276 self->a_icon->surface.parenty = y;
277 XMoveResizeWindow(ob_display, self->icon, x, y, w, h);
278 RrPaint(self->a_icon, self->icon, w, h);
281 ObIconPopup *icon_popup_new()
285 self = g_new0(ObIconPopup, 1);
286 self->popup = popup_new(TRUE);
287 self->a_icon = RrAppearanceCopy(ob_rr_theme->a_clear_tex);
288 self->icon = XCreateWindow(ob_display, self->popup->bg,
290 RrDepth(ob_rr_inst), InputOutput,
291 RrVisual(ob_rr_inst), 0, NULL);
292 XMapWindow(ob_display, self->icon);
294 self->popup->hasicon = TRUE;
295 self->popup->draw_icon = icon_popup_draw_icon;
296 self->popup->draw_icon_data = self;
301 void icon_popup_free(ObIconPopup *self)
304 XDestroyWindow(ob_display, self->icon);
305 RrAppearanceFree(self->a_icon);
306 popup_free(self->popup);
311 void icon_popup_delay_show(ObIconPopup *self, gulong usec,
312 gchar *text, const ObClientIcon *icon)
315 self->a_icon->texture[0].type = RR_TEXTURE_RGBA;
316 self->a_icon->texture[0].data.rgba.width = icon->width;
317 self->a_icon->texture[0].data.rgba.height = icon->height;
318 self->a_icon->texture[0].data.rgba.data = icon->data;
320 self->a_icon->texture[0].type = RR_TEXTURE_NONE;
322 popup_delay_show(self->popup, usec, text);
325 void icon_popup_icon_size_multiplier(ObIconPopup *self, guint wm, guint hm)
327 if (wm != 0) self->popup->iconwm = wm;
328 if (hm != 0) self->popup->iconhm = hm;
331 static void pager_popup_draw_icon(gint px, gint py, gint w, gint h,
334 ObPagerPopup *self = data;
341 const guint cols = screen_desktop_layout.columns;
342 const guint rows = screen_desktop_layout.rows;
343 const gint linewidth = ob_rr_theme->fbwidth;
345 eachw = (w - ((cols + 1) * linewidth)) / cols;
346 eachh = (h - ((rows + 1) * linewidth)) / rows;
347 /* make them squares */
348 eachw = eachh = MIN(eachw, eachh);
351 px += (w - (cols * (eachw + linewidth) + linewidth)) / 2;
352 py += (h - (rows * (eachh + linewidth) + linewidth)) / 2;
354 if (eachw <= 0 || eachh <= 0)
357 switch (screen_desktop_layout.orientation) {
358 case OB_ORIENTATION_HORZ:
359 switch (screen_desktop_layout.start_corner) {
360 case OB_CORNER_TOPLEFT:
365 case OB_CORNER_TOPRIGHT:
370 case OB_CORNER_BOTTOMRIGHT:
373 vert_inc = -screen_desktop_layout.columns;
375 case OB_CORNER_BOTTOMLEFT:
376 n = (rows - 1) * cols;
381 g_assert_not_reached();
384 case OB_ORIENTATION_VERT:
385 switch (screen_desktop_layout.start_corner) {
386 case OB_CORNER_TOPLEFT:
391 case OB_CORNER_TOPRIGHT:
392 n = rows * (cols - 1);
396 case OB_CORNER_BOTTOMRIGHT:
401 case OB_CORNER_BOTTOMLEFT:
407 g_assert_not_reached();
411 g_assert_not_reached();
415 for (r = 0, y = 0; r < rows; ++r, y += eachh + linewidth)
417 for (c = 0, x = 0; c < cols; ++c, x += eachw + linewidth)
421 if (n < self->desks) {
422 a = (n == self->curdesk ? self->hilight : self->unhilight);
424 a->surface.parent = self->popup->a_bg;
425 a->surface.parentx = x + px;
426 a->surface.parenty = y + py;
427 XMoveResizeWindow(ob_display, self->wins[n],
428 x + px, y + py, eachw, eachh);
429 RrPaint(a, self->wins[n], eachw, eachh);
433 n = rown += vert_inc;
437 ObPagerPopup *pager_popup_new()
441 self = g_new(ObPagerPopup, 1);
442 self->popup = popup_new(TRUE);
445 self->wins = g_new(Window, self->desks);
446 self->hilight = RrAppearanceCopy(ob_rr_theme->osd_hilite_fg);
447 self->unhilight = RrAppearanceCopy(ob_rr_theme->osd_unhilite_fg);
449 self->popup->hasicon = TRUE;
450 self->popup->draw_icon = pager_popup_draw_icon;
451 self->popup->draw_icon_data = self;
456 void pager_popup_free(ObPagerPopup *self)
461 for (i = 0; i < self->desks; ++i)
462 XDestroyWindow(ob_display, self->wins[i]);
464 RrAppearanceFree(self->hilight);
465 RrAppearanceFree(self->unhilight);
466 popup_free(self->popup);
471 void pager_popup_delay_show(ObPagerPopup *self, gulong usec,
472 gchar *text, guint desk)
476 if (screen_num_desktops < self->desks)
477 for (i = screen_num_desktops; i < self->desks; ++i)
478 XDestroyWindow(ob_display, self->wins[i]);
480 if (screen_num_desktops != self->desks)
481 self->wins = g_renew(Window, self->wins, screen_num_desktops);
483 if (screen_num_desktops > self->desks)
484 for (i = self->desks; i < screen_num_desktops; ++i) {
485 XSetWindowAttributes attr;
487 attr.border_pixel = RrColorPixel(ob_rr_theme->frame_b_color);
488 self->wins[i] = XCreateWindow(ob_display, self->popup->bg,
489 0, 0, 1, 1, ob_rr_theme->fbwidth,
490 RrDepth(ob_rr_inst), InputOutput,
491 RrVisual(ob_rr_inst), CWBorderPixel,
493 XMapWindow(ob_display, self->wins[i]);
496 self->desks = screen_num_desktops;
497 self->curdesk = desk;
499 popup_delay_show(self->popup, usec, text);
502 void pager_popup_icon_size_multiplier(ObPagerPopup *self, guint wm, guint hm)
504 if (wm != 0) self->popup->iconwm = wm;
505 if (hm != 0) self->popup->iconhm = hm;