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,
56 RrColorPixel(ob_rr_theme->frame_focused_border_color));
58 XMapWindow(ob_display, self->text);
60 stacking_add(INTERNAL_AS_WINDOW(self));
64 void popup_free(ObPopup *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);
76 void popup_position(ObPopup *self, gint gravity, gint x, gint y)
78 self->gravity = gravity;
83 void popup_text_width(ObPopup *self, gint w)
88 void popup_min_width(ObPopup *self, gint minw)
93 void popup_max_width(ObPopup *self, gint maxw)
98 void popup_height(ObPopup *self, gint h)
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);
107 void popup_text_width_to_string(ObPopup *self, gchar *text)
109 if (text[0] != '\0') {
110 self->a_text->texture[0].data.text.string = text;
111 self->textw = RrMinWidth(self->a_text);
116 void popup_height_to_string(ObPopup *self, gchar *text)
118 self->h = RrMinHeight(self->a_text) + ob_rr_theme->paddingy * 2;
121 void popup_text_width_to_strings(ObPopup *self, gchar **strings, gint num)
126 for (i = 0; i < num; ++i) {
127 popup_text_width_to_string(self, strings[i]);
128 maxw = MAX(maxw, self->textw);
133 void popup_set_text_align(ObPopup *self, RrJustify align)
135 self->a_text->texture[0].data.text.justify = align;
138 static gboolean popup_show_timeout(gpointer data)
140 ObPopup *self = data;
142 XMapWindow(ob_display, self->bg);
143 stacking_raise(INTERNAL_AS_WINDOW(self));
145 self->delay_mapped = FALSE;
147 return FALSE; /* don't repeat */
150 void popup_delay_show(ObPopup *self, gulong usec, gchar *text)
154 gint emptyx, emptyy; /* empty space between elements */
155 gint textx, texty, textw, texth;
156 gint iconx, icony, iconw, iconh;
159 area = screen_physical_area();
161 RrMargins(self->a_bg, &l, &t, &r, &b);
163 /* set up the textures */
164 self->a_text->texture[0].data.text.string = text;
166 /* measure the text out */
167 if (text[0] != '\0') {
168 RrMinSize(self->a_text, &textw, &texth);
171 texth = RrMinHeight(self->a_text);
174 /* get the height, which is also used for the icon width */
175 emptyy = t + b + ob_rr_theme->paddingy * 2;
177 texth = self->h - emptyy;
178 h = texth * self->iconhm + emptyy;
183 iconx = textx = l + ob_rr_theme->paddingx;
185 emptyx = l + r + ob_rr_theme->paddingx * 2;
187 iconw = texth * self->iconwm;
188 iconh = texth * self->iconhm;
189 textx += iconw + ob_rr_theme->paddingx;
191 emptyx += ob_rr_theme->paddingx; /* between the icon and text */
195 texty = (h - texth - emptyy) / 2 + t + ob_rr_theme->paddingy;
196 icony = (h - iconh - emptyy) / 2 + t + ob_rr_theme->paddingy;
198 w = textw + emptyx + iconw;
199 /* cap it at maxw/minw */
200 if (self->maxw) w = MIN(w, self->maxw);
201 if (self->minw) w = MAX(w, self->minw);
202 textw = w - emptyx - iconw;
204 /* sanity checks to avoid crashes! */
207 if (texth < 1) texth = 1;
209 /* set up the x coord */
211 switch (self->gravity) {
212 case NorthGravity: case CenterGravity: case SouthGravity:
215 case NorthEastGravity: case EastGravity: case SouthEastGravity:
220 /* set up the y coord */
222 switch (self->gravity) {
223 case WestGravity: case CenterGravity: case EastGravity:
226 case SouthWestGravity: case SouthGravity: case SouthEastGravity:
231 x=MAX(MIN(x, area->width-w),0);
232 y=MAX(MIN(y, area->height-h),0);
234 /* set the windows/appearances up */
235 XMoveResizeWindow(ob_display, self->bg, x, y, w, h);
236 RrPaint(self->a_bg, self->bg, w, h);
239 self->a_text->surface.parent = self->a_bg;
240 self->a_text->surface.parentx = textx;
241 self->a_text->surface.parenty = texty;
242 XMoveResizeWindow(ob_display, self->text, textx, texty, textw, texth);
243 RrPaint(self->a_text, self->text, textw, texth);
247 self->draw_icon(iconx, icony, iconw, iconh, self->draw_icon_data);
249 /* do the actual showing */
252 /* don't kill previous show timers */
253 if (!self->delay_mapped) {
254 ob_main_loop_timeout_add(ob_main_loop, usec,
255 popup_show_timeout, self,
256 g_direct_equal, NULL);
257 self->delay_mapped = TRUE;
260 popup_show_timeout(self);
265 void popup_hide(ObPopup *self)
270 /* kill enter events cause by this unmapping */
271 ignore_start = event_start_ignore_all_enters();
273 XUnmapWindow(ob_display, self->bg);
274 self->mapped = FALSE;
276 event_end_ignore_all_enters(ignore_start);
277 } else if (self->delay_mapped) {
278 ob_main_loop_timeout_remove(ob_main_loop, popup_show_timeout);
279 self->delay_mapped = FALSE;
283 static void icon_popup_draw_icon(gint x, gint y, gint w, gint h, gpointer data)
285 ObIconPopup *self = data;
287 self->a_icon->surface.parent = self->popup->a_bg;
288 self->a_icon->surface.parentx = x;
289 self->a_icon->surface.parenty = y;
290 XMoveResizeWindow(ob_display, self->icon, x, y, w, h);
291 RrPaint(self->a_icon, self->icon, w, h);
294 ObIconPopup *icon_popup_new()
298 self = g_new0(ObIconPopup, 1);
299 self->popup = popup_new(TRUE);
300 self->a_icon = RrAppearanceCopy(ob_rr_theme->a_clear_tex);
301 self->icon = XCreateWindow(ob_display, self->popup->bg,
303 RrDepth(ob_rr_inst), InputOutput,
304 RrVisual(ob_rr_inst), 0, NULL);
305 XMapWindow(ob_display, self->icon);
307 self->popup->hasicon = TRUE;
308 self->popup->draw_icon = icon_popup_draw_icon;
309 self->popup->draw_icon_data = self;
314 void icon_popup_free(ObIconPopup *self)
317 XDestroyWindow(ob_display, self->icon);
318 RrAppearanceFree(self->a_icon);
319 popup_free(self->popup);
324 void icon_popup_delay_show(ObIconPopup *self, gulong usec,
325 gchar *text, const ObClientIcon *icon)
328 self->a_icon->texture[0].type = RR_TEXTURE_RGBA;
329 self->a_icon->texture[0].data.rgba.width = icon->width;
330 self->a_icon->texture[0].data.rgba.height = icon->height;
331 self->a_icon->texture[0].data.rgba.alpha = 0xff;
332 self->a_icon->texture[0].data.rgba.data = icon->data;
334 self->a_icon->texture[0].type = RR_TEXTURE_NONE;
336 popup_delay_show(self->popup, usec, text);
339 void icon_popup_icon_size_multiplier(ObIconPopup *self, guint wm, guint hm)
342 self->popup->iconwm = MAX(1, wm);
343 self->popup->iconhm = MAX(1, hm);
346 static void pager_popup_draw_icon(gint px, gint py, gint w, gint h,
349 ObPagerPopup *self = data;
356 const guint cols = screen_desktop_layout.columns;
357 const guint rows = screen_desktop_layout.rows;
358 const gint linewidth = ob_rr_theme->fbwidth;
360 eachw = (w - ((cols + 1) * linewidth)) / cols;
361 eachh = (h - ((rows + 1) * linewidth)) / rows;
362 /* make them squares */
363 eachw = eachh = MIN(eachw, eachh);
366 px += (w - (cols * (eachw + linewidth) + linewidth)) / 2;
367 py += (h - (rows * (eachh + linewidth) + linewidth)) / 2;
369 if (eachw <= 0 || eachh <= 0)
372 switch (screen_desktop_layout.orientation) {
373 case OB_ORIENTATION_HORZ:
374 switch (screen_desktop_layout.start_corner) {
375 case OB_CORNER_TOPLEFT:
380 case OB_CORNER_TOPRIGHT:
385 case OB_CORNER_BOTTOMRIGHT:
388 vert_inc = -screen_desktop_layout.columns;
390 case OB_CORNER_BOTTOMLEFT:
391 n = (rows - 1) * cols;
396 g_assert_not_reached();
399 case OB_ORIENTATION_VERT:
400 switch (screen_desktop_layout.start_corner) {
401 case OB_CORNER_TOPLEFT:
406 case OB_CORNER_TOPRIGHT:
407 n = rows * (cols - 1);
411 case OB_CORNER_BOTTOMRIGHT:
416 case OB_CORNER_BOTTOMLEFT:
422 g_assert_not_reached();
426 g_assert_not_reached();
430 for (r = 0, y = 0; r < rows; ++r, y += eachh + linewidth)
432 for (c = 0, x = 0; c < cols; ++c, x += eachw + linewidth)
436 if (n < self->desks) {
437 a = (n == self->curdesk ? self->hilight : self->unhilight);
439 a->surface.parent = self->popup->a_bg;
440 a->surface.parentx = x + px;
441 a->surface.parenty = y + py;
442 XMoveResizeWindow(ob_display, self->wins[n],
443 x + px, y + py, eachw, eachh);
444 RrPaint(a, self->wins[n], eachw, eachh);
448 n = rown += vert_inc;
452 ObPagerPopup *pager_popup_new()
456 self = g_new(ObPagerPopup, 1);
457 self->popup = popup_new(TRUE);
460 self->wins = g_new(Window, self->desks);
461 self->hilight = RrAppearanceCopy(ob_rr_theme->osd_hilite_fg);
462 self->unhilight = RrAppearanceCopy(ob_rr_theme->osd_unhilite_fg);
464 self->popup->hasicon = TRUE;
465 self->popup->draw_icon = pager_popup_draw_icon;
466 self->popup->draw_icon_data = self;
471 void pager_popup_free(ObPagerPopup *self)
476 for (i = 0; i < self->desks; ++i)
477 XDestroyWindow(ob_display, self->wins[i]);
479 RrAppearanceFree(self->hilight);
480 RrAppearanceFree(self->unhilight);
481 popup_free(self->popup);
486 void pager_popup_delay_show(ObPagerPopup *self, gulong usec,
487 gchar *text, guint desk)
491 if (screen_num_desktops < self->desks)
492 for (i = screen_num_desktops; i < self->desks; ++i)
493 XDestroyWindow(ob_display, self->wins[i]);
495 if (screen_num_desktops != self->desks)
496 self->wins = g_renew(Window, self->wins, screen_num_desktops);
498 if (screen_num_desktops > self->desks)
499 for (i = self->desks; i < screen_num_desktops; ++i) {
500 XSetWindowAttributes attr;
503 RrColorPixel(ob_rr_theme->frame_focused_border_color);
504 self->wins[i] = XCreateWindow(ob_display, self->popup->bg,
505 0, 0, 1, 1, ob_rr_theme->fbwidth,
506 RrDepth(ob_rr_inst), InputOutput,
507 RrVisual(ob_rr_inst), CWBorderPixel,
509 XMapWindow(ob_display, self->wins[i]);
512 self->desks = screen_num_desktops;
513 self->curdesk = desk;
515 popup_delay_show(self->popup, usec, text);
518 void pager_popup_icon_size_multiplier(ObPagerPopup *self, guint wm, guint hm)
521 self->popup->iconwm = MAX(1, wm);
522 self->popup->iconhm = MAX(1, hm);