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.
28 #include "render/render.h"
29 #include "render/theme.h"
31 ObPopup *popup_new(gboolean hasicon)
33 XSetWindowAttributes attrib;
34 ObPopup *self = g_new0(ObPopup, 1);
36 self->obwin.type = Window_Internal;
37 self->hasicon = hasicon;
38 self->gravity = NorthWestGravity;
39 self->x = self->y = self->w = 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);
43 attrib.override_redirect = True;
44 self->bg = XCreateWindow(ob_display, RootWindow(ob_display, ob_screen),
45 0, 0, 1, 1, 0, RrDepth(ob_rr_inst),
46 InputOutput, RrVisual(ob_rr_inst),
47 CWOverrideRedirect, &attrib);
49 self->text = XCreateWindow(ob_display, self->bg,
50 0, 0, 1, 1, 0, RrDepth(ob_rr_inst),
51 InputOutput, RrVisual(ob_rr_inst), 0, NULL);
53 XMapWindow(ob_display, self->text);
55 stacking_add(INTERNAL_AS_WINDOW(self));
59 void popup_free(ObPopup *self)
62 XDestroyWindow(ob_display, self->bg);
63 XDestroyWindow(ob_display, self->text);
64 RrAppearanceFree(self->a_bg);
65 RrAppearanceFree(self->a_text);
66 stacking_remove(self);
71 void popup_position(ObPopup *self, gint gravity, gint x, gint y)
73 self->gravity = gravity;
78 void popup_width(ObPopup *self, gint w)
83 void popup_height(ObPopup *self, gint h)
87 /* don't let the height be smaller than the text */
88 texth = RrMinHeight(self->a_text) + ob_rr_theme->paddingy * 2;
89 self->h = MAX(h, texth);
92 void popup_width_to_string(ObPopup *self, gchar *text, gint max)
96 self->a_text->texture[0].data.text.string = text;
97 textw = RrMinWidth(self->a_text);
99 gint texth = RrMinHeight(self->a_text) + ob_rr_theme->paddingy * 2;
103 self->w = textw + iconw + ob_rr_theme->paddingx * (self->hasicon ? 3 : 2);
104 /* cap it at "max" */
106 self->w = MIN(self->w, max);
109 void popup_height_to_string(ObPopup *self, gchar *text)
111 self->h = RrMinHeight(self->a_text) + ob_rr_theme->paddingy * 2;
114 void popup_width_to_strings(ObPopup *self, gchar **strings, gint max)
119 for (i = 0; strings[i] != NULL; ++i) {
120 popup_width_to_string(self, strings[i], max);
121 maxw = MAX(maxw, self->w);
126 void popup_set_text_align(ObPopup *self, RrJustify align)
128 self->a_text->texture[0].data.text.justify = align;
131 void popup_show(ObPopup *self, gchar *text)
137 Rect *area; /* won't go outside this */
139 area = screen_physical_area(); /* XXX this should work quite
140 good, someone with xinerama,
141 and different resolutions on
144 RrMargins(self->a_bg, &l, &t, &r, &b);
146 XSetWindowBorderWidth(ob_display, self->bg, ob_rr_theme->fbwidth);
147 XSetWindowBorder(ob_display, self->bg, ob_rr_theme->frame_b_color->pixel);
149 /* set up the textures */
150 self->a_text->texture[0].data.text.string = text;
152 /* measure the text out */
153 RrMinSize(self->a_text, &textw, &texth);
154 texth += ob_rr_theme->paddingy * 2;
156 /* set the sizes up and reget the text sizes from the calculated
160 texth = h - (t+b + ob_rr_theme->paddingy * 2);
162 h = t+b + texth + ob_rr_theme->paddingy * 2;
163 iconw = (self->hasicon ? texth : 0);
166 textw = w - (l+r + iconw + ob_rr_theme->paddingx *
167 (self->hasicon ? 3 : 2));
169 w = l+r + textw + iconw + ob_rr_theme->paddingx *
170 (self->hasicon ? 3 : 2);
171 /* sanity checks to avoid crashes! */
174 if (textw < 1) textw = 1;
175 if (texth < 1) texth = 1;
177 /* set up the x coord */
179 switch (self->gravity) {
185 case NorthEastGravity:
187 case SouthEastGravity:
192 /* set up the y coord */
194 switch (self->gravity) {
200 case SouthWestGravity:
202 case SouthEastGravity:
207 x=MAX(MIN(x, area->width-w),0);
208 y=MAX(MIN(y, area->height-h),0);
210 /* set the windows/appearances up */
211 XMoveResizeWindow(ob_display, self->bg, x, y, w, h);
213 self->a_text->surface.parent = self->a_bg;
214 self->a_text->surface.parentx = l + iconw +
215 ob_rr_theme->paddingx * (self->hasicon ? 2 : 1);
216 self->a_text->surface.parenty = t + ob_rr_theme->paddingy;
217 XMoveResizeWindow(ob_display, self->text,
218 l + iconw + ob_rr_theme->paddingx *
219 (self->hasicon ? 2 : 1),
220 t + ob_rr_theme->paddingy, textw, texth);
222 RrPaint(self->a_bg, self->bg, w, h);
223 RrPaint(self->a_text, self->text, textw, texth);
226 if (iconw < 1) iconw = 1; /* sanity check for crashes */
228 self->draw_icon(l + ob_rr_theme->paddingx,
229 t + ob_rr_theme->paddingy,
230 iconw, texth, self->draw_icon_data);
234 XMapWindow(ob_display, self->bg);
235 stacking_raise(INTERNAL_AS_WINDOW(self));
240 void popup_hide(ObPopup *self)
243 XUnmapWindow(ob_display, self->bg);
244 self->mapped = FALSE;
246 /* kill enter events cause by this unmapping */
247 event_ignore_queued_enters();
251 static void icon_popup_draw_icon(gint x, gint y, gint w, gint h, gpointer data)
253 ObIconPopup *self = data;
255 self->a_icon->surface.parent = self->popup->a_bg;
256 self->a_icon->surface.parentx = x;
257 self->a_icon->surface.parenty = y;
258 XMoveResizeWindow(ob_display, self->icon, x, y, w, h);
259 RrPaint(self->a_icon, self->icon, w, h);
262 ObIconPopup *icon_popup_new()
266 self = g_new0(ObIconPopup, 1);
267 self->popup = popup_new(TRUE);
268 self->a_icon = RrAppearanceCopy(ob_rr_theme->a_clear_tex);
269 self->icon = XCreateWindow(ob_display, self->popup->bg,
271 RrDepth(ob_rr_inst), InputOutput,
272 RrVisual(ob_rr_inst), 0, NULL);
273 XMapWindow(ob_display, self->icon);
275 self->popup->draw_icon = icon_popup_draw_icon;
276 self->popup->draw_icon_data = self;
281 void icon_popup_free(ObIconPopup *self)
284 XDestroyWindow(ob_display, self->icon);
285 RrAppearanceFree(self->a_icon);
286 popup_free(self->popup);
291 void icon_popup_show(ObIconPopup *self,
292 gchar *text, const ObClientIcon *icon)
295 self->a_icon->texture[0].type = RR_TEXTURE_RGBA;
296 self->a_icon->texture[0].data.rgba.width = icon->width;
297 self->a_icon->texture[0].data.rgba.height = icon->height;
298 self->a_icon->texture[0].data.rgba.data = icon->data;
300 self->a_icon->texture[0].type = RR_TEXTURE_NONE;
302 popup_show(self->popup, text);
305 static void pager_popup_draw_icon(gint px, gint py, gint w, gint h,
308 ObPagerPopup *self = data;
316 eachw = (w - ob_rr_theme->fbwidth -
317 (screen_desktop_layout.columns * ob_rr_theme->fbwidth))
318 / screen_desktop_layout.columns;
319 eachh = (h - ob_rr_theme->fbwidth -
320 (screen_desktop_layout.rows * ob_rr_theme->fbwidth))
321 / screen_desktop_layout.rows;
322 /* make them squares */
323 eachw = eachh = MIN(eachw, eachh);
326 px += (w - (screen_desktop_layout.columns * (eachw + ob_rr_theme->fbwidth) +
327 ob_rr_theme->fbwidth)) / 2;
328 py += (h - (screen_desktop_layout.rows * (eachh + ob_rr_theme->fbwidth) +
329 ob_rr_theme->fbwidth)) / 2;
331 if (eachw <= 0 || eachh <= 0)
334 switch (screen_desktop_layout.orientation) {
335 case OB_ORIENTATION_HORZ:
336 switch (screen_desktop_layout.start_corner) {
337 case OB_CORNER_TOPLEFT:
340 vert_inc = screen_desktop_layout.columns;
342 case OB_CORNER_TOPRIGHT:
343 n = screen_desktop_layout.columns - 1;
345 vert_inc = screen_desktop_layout.columns;
347 case OB_CORNER_BOTTOMRIGHT:
348 n = screen_desktop_layout.rows * screen_desktop_layout.columns - 1;
350 vert_inc = -screen_desktop_layout.columns;
352 case OB_CORNER_BOTTOMLEFT:
353 n = (screen_desktop_layout.rows - 1)
354 * screen_desktop_layout.columns;
356 vert_inc = -screen_desktop_layout.columns;
359 g_assert_not_reached();
362 case OB_ORIENTATION_VERT:
363 switch (screen_desktop_layout.start_corner) {
364 case OB_CORNER_TOPLEFT:
366 horz_inc = screen_desktop_layout.rows;
369 case OB_CORNER_TOPRIGHT:
370 n = screen_desktop_layout.rows
371 * (screen_desktop_layout.columns - 1);
372 horz_inc = -screen_desktop_layout.rows;
375 case OB_CORNER_BOTTOMRIGHT:
376 n = screen_desktop_layout.rows * screen_desktop_layout.columns - 1;
377 horz_inc = -screen_desktop_layout.rows;
380 case OB_CORNER_BOTTOMLEFT:
381 n = screen_desktop_layout.rows - 1;
382 horz_inc = screen_desktop_layout.rows;
386 g_assert_not_reached();
390 g_assert_not_reached();
394 for (r = 0, y = 0; r < screen_desktop_layout.rows;
395 ++r, y += eachh + ob_rr_theme->fbwidth)
397 for (c = 0, x = 0; c < screen_desktop_layout.columns;
398 ++c, x += eachw + ob_rr_theme->fbwidth)
402 if (n < self->desks) {
403 a = (n == self->curdesk ? self->hilight : self->unhilight);
405 a->surface.parent = self->popup->a_bg;
406 a->surface.parentx = x + px;
407 a->surface.parenty = y + py;
408 XMoveResizeWindow(ob_display, self->wins[n],
409 x + px, y + py, eachw, eachh);
410 RrPaint(a, self->wins[n], eachw, eachh);
414 n = rown += vert_inc;
418 ObPagerPopup *pager_popup_new()
422 self = g_new(ObPagerPopup, 1);
423 self->popup = popup_new(TRUE);
426 self->wins = g_new(Window, self->desks);
427 self->hilight = RrAppearanceCopy(ob_rr_theme->osd_hilite_fg);
428 self->unhilight = RrAppearanceCopy(ob_rr_theme->osd_unhilite_fg);
430 self->popup->draw_icon = pager_popup_draw_icon;
431 self->popup->draw_icon_data = self;
436 void pager_popup_free(ObPagerPopup *self)
441 for (i = 0; i < self->desks; ++i)
442 XDestroyWindow(ob_display, self->wins[i]);
444 RrAppearanceFree(self->hilight);
445 RrAppearanceFree(self->unhilight);
446 popup_free(self->popup);
451 void pager_popup_show(ObPagerPopup *self, gchar *text, guint desk)
455 if (screen_num_desktops < self->desks)
456 for (i = screen_num_desktops; i < self->desks; ++i)
457 XDestroyWindow(ob_display, self->wins[i]);
459 if (screen_num_desktops != self->desks)
460 self->wins = g_renew(Window, self->wins, screen_num_desktops);
462 if (screen_num_desktops > self->desks)
463 for (i = self->desks; i < screen_num_desktops; ++i) {
464 XSetWindowAttributes attr;
466 attr.border_pixel = RrColorPixel(ob_rr_theme->frame_b_color);
467 self->wins[i] = XCreateWindow(ob_display, self->popup->bg,
468 0, 0, 1, 1, ob_rr_theme->fbwidth,
469 RrDepth(ob_rr_inst), InputOutput,
470 RrVisual(ob_rr_inst), CWBorderPixel,
472 XMapWindow(ob_display, self->wins[i]);
475 self->desks = screen_num_desktops;
476 self->curdesk = desk;
478 popup_show(self->popup, text);