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_size(ObPopup *self, gint w, gint h)
84 void popup_size_to_string(ObPopup *self, gchar *text)
89 self->a_text->texture[0].data.text.string = text;
90 RrMinsize(self->a_text, &textw, &texth);
91 /*XXX textw += ob_rr_theme->bevel * 2;*/
92 texth += ob_rr_theme->paddingy * 2;
94 self->h = texth + ob_rr_theme->paddingy * 2;
95 iconw = (self->hasicon ? texth : 0);
96 self->w = textw + iconw + ob_rr_theme->paddingx * (self->hasicon ? 3 : 2);
99 void popup_set_text_align(ObPopup *self, RrJustify align)
101 self->a_text->texture[0].data.text.justify = align;
104 void popup_show(ObPopup *self, gchar *text)
110 Rect *area; /* won't go outside this */
112 area = screen_physical_area(); /* XXX this should work quite
113 good, someone with xinerama,
114 and different resolutions on
117 RrMargins(self->a_bg, &l, &t, &r, &b);
119 XSetWindowBorderWidth(ob_display, self->bg, ob_rr_theme->fbwidth);
120 XSetWindowBorder(ob_display, self->bg, ob_rr_theme->frame_b_color->pixel);
122 /* set up the textures */
123 self->a_text->texture[0].data.text.string = text;
125 /* measure the shit out */
126 RrMinsize(self->a_text, &textw, &texth);
127 /*XXX textw += ob_rr_theme->padding * 2;*/
128 texth += ob_rr_theme->paddingy * 2;
130 /* set the sizes up and reget the text sizes from the calculated
134 texth = h - (t+b + ob_rr_theme->paddingy * 2);
136 h = t+b + texth + ob_rr_theme->paddingy * 2;
137 iconw = (self->hasicon ? texth : 0);
140 textw = w - (l+r + iconw + ob_rr_theme->paddingx *
141 (self->hasicon ? 3 : 2));
143 w = l+r + textw + iconw + ob_rr_theme->paddingx *
144 (self->hasicon ? 3 : 2);
145 /* sanity checks to avoid crashes! */
148 if (textw < 1) textw = 1;
149 if (texth < 1) texth = 1;
151 /* set up the x coord */
153 switch (self->gravity) {
159 case NorthEastGravity:
161 case SouthEastGravity:
166 /* set up the y coord */
168 switch (self->gravity) {
174 case SouthWestGravity:
176 case SouthEastGravity:
181 x=MAX(MIN(x, area->width-w),0);
182 y=MAX(MIN(y, area->height-h),0);
184 /* set the windows/appearances up */
185 XMoveResizeWindow(ob_display, self->bg, x, y, w, h);
187 self->a_text->surface.parent = self->a_bg;
188 self->a_text->surface.parentx = l + iconw +
189 ob_rr_theme->paddingx * (self->hasicon ? 2 : 1);
190 self->a_text->surface.parenty = t + ob_rr_theme->paddingy;
191 XMoveResizeWindow(ob_display, self->text,
192 l + iconw + ob_rr_theme->paddingx *
193 (self->hasicon ? 2 : 1),
194 t + ob_rr_theme->paddingy, textw, texth);
196 RrPaint(self->a_bg, self->bg, w, h);
197 RrPaint(self->a_text, self->text, textw, texth);
200 if (iconw < 1) iconw = 1; /* sanity check for crashes */
202 self->draw_icon(l + ob_rr_theme->paddingx,
203 t + ob_rr_theme->paddingy,
204 iconw, texth, self->draw_icon_data);
208 XMapWindow(ob_display, self->bg);
209 stacking_raise(INTERNAL_AS_WINDOW(self));
214 void popup_hide(ObPopup *self)
217 XUnmapWindow(ob_display, self->bg);
218 self->mapped = FALSE;
220 /* kill enter events cause by this unmapping */
221 event_ignore_queued_enters();
225 static void icon_popup_draw_icon(gint x, gint y, gint w, gint h, gpointer data)
227 ObIconPopup *self = data;
229 self->a_icon->surface.parent = self->popup->a_bg;
230 self->a_icon->surface.parentx = x;
231 self->a_icon->surface.parenty = y;
232 XMoveResizeWindow(ob_display, self->icon, x, y, w, h);
233 RrPaint(self->a_icon, self->icon, w, h);
236 ObIconPopup *icon_popup_new()
240 self = g_new0(ObIconPopup, 1);
241 self->popup = popup_new(TRUE);
242 self->a_icon = RrAppearanceCopy(ob_rr_theme->a_clear_tex);
243 self->icon = XCreateWindow(ob_display, self->popup->bg,
245 RrDepth(ob_rr_inst), InputOutput,
246 RrVisual(ob_rr_inst), 0, NULL);
247 XMapWindow(ob_display, self->icon);
249 self->popup->draw_icon = icon_popup_draw_icon;
250 self->popup->draw_icon_data = self;
255 void icon_popup_free(ObIconPopup *self)
258 XDestroyWindow(ob_display, self->icon);
259 RrAppearanceFree(self->a_icon);
260 popup_free(self->popup);
265 void icon_popup_show(ObIconPopup *self,
266 gchar *text, const ObClientIcon *icon)
269 self->a_icon->texture[0].type = RR_TEXTURE_RGBA;
270 self->a_icon->texture[0].data.rgba.width = icon->width;
271 self->a_icon->texture[0].data.rgba.height = icon->height;
272 self->a_icon->texture[0].data.rgba.data = icon->data;
274 self->a_icon->texture[0].type = RR_TEXTURE_NONE;
276 popup_show(self->popup, text);
279 static void pager_popup_draw_icon(gint px, gint py, gint w, gint h,
282 ObPagerPopup *self = data;
290 eachw = (w - ob_rr_theme->fbwidth -
291 (screen_desktop_layout.columns * ob_rr_theme->fbwidth))
292 / screen_desktop_layout.columns;
293 eachh = (h - ob_rr_theme->fbwidth -
294 (screen_desktop_layout.rows * ob_rr_theme->fbwidth))
295 / screen_desktop_layout.rows;
296 /* make them squares */
297 eachw = eachh = MIN(eachw, eachh);
300 px += (w - (screen_desktop_layout.columns * (eachw + ob_rr_theme->fbwidth) +
301 ob_rr_theme->fbwidth)) / 2;
302 py += (h - (screen_desktop_layout.rows * (eachh + ob_rr_theme->fbwidth) +
303 ob_rr_theme->fbwidth)) / 2;
305 if (eachw <= 0 || eachh <= 0)
308 switch (screen_desktop_layout.orientation) {
309 case OB_ORIENTATION_HORZ:
310 switch (screen_desktop_layout.start_corner) {
311 case OB_CORNER_TOPLEFT:
314 vert_inc = screen_desktop_layout.columns;
316 case OB_CORNER_TOPRIGHT:
317 n = screen_desktop_layout.columns - 1;
319 vert_inc = screen_desktop_layout.columns;
321 case OB_CORNER_BOTTOMRIGHT:
322 n = screen_desktop_layout.rows * screen_desktop_layout.columns - 1;
324 vert_inc = -screen_desktop_layout.columns;
326 case OB_CORNER_BOTTOMLEFT:
327 n = (screen_desktop_layout.rows - 1)
328 * screen_desktop_layout.columns;
330 vert_inc = -screen_desktop_layout.columns;
333 g_assert_not_reached();
336 case OB_ORIENTATION_VERT:
337 switch (screen_desktop_layout.start_corner) {
338 case OB_CORNER_TOPLEFT:
340 horz_inc = screen_desktop_layout.rows;
343 case OB_CORNER_TOPRIGHT:
344 n = screen_desktop_layout.rows
345 * (screen_desktop_layout.columns - 1);
346 horz_inc = -screen_desktop_layout.rows;
349 case OB_CORNER_BOTTOMRIGHT:
350 n = screen_desktop_layout.rows * screen_desktop_layout.columns - 1;
351 horz_inc = -screen_desktop_layout.rows;
354 case OB_CORNER_BOTTOMLEFT:
355 n = screen_desktop_layout.rows - 1;
356 horz_inc = screen_desktop_layout.rows;
360 g_assert_not_reached();
364 g_assert_not_reached();
368 for (r = 0, y = 0; r < screen_desktop_layout.rows;
369 ++r, y += eachh + ob_rr_theme->fbwidth)
371 for (c = 0, x = 0; c < screen_desktop_layout.columns;
372 ++c, x += eachw + ob_rr_theme->fbwidth)
376 if (n < self->desks) {
377 a = (n == self->curdesk ? self->hilight : self->unhilight);
379 a->surface.parent = self->popup->a_bg;
380 a->surface.parentx = x + px;
381 a->surface.parenty = y + py;
382 XMoveResizeWindow(ob_display, self->wins[n],
383 x + px, y + py, eachw, eachh);
384 RrPaint(a, self->wins[n], eachw, eachh);
388 n = rown += vert_inc;
392 ObPagerPopup *pager_popup_new()
396 self = g_new(ObPagerPopup, 1);
397 self->popup = popup_new(TRUE);
400 self->wins = g_new(Window, self->desks);
401 self->hilight = RrAppearanceCopy(ob_rr_theme->osd_hilite_fg);
402 self->unhilight = RrAppearanceCopy(ob_rr_theme->osd_unhilite_fg);
404 self->popup->draw_icon = pager_popup_draw_icon;
405 self->popup->draw_icon_data = self;
410 void pager_popup_free(ObPagerPopup *self)
415 for (i = 0; i < self->desks; ++i)
416 XDestroyWindow(ob_display, self->wins[i]);
418 RrAppearanceFree(self->hilight);
419 RrAppearanceFree(self->unhilight);
420 popup_free(self->popup);
425 void pager_popup_show(ObPagerPopup *self, gchar *text, guint desk)
429 if (screen_num_desktops < self->desks)
430 for (i = screen_num_desktops; i < self->desks; ++i)
431 XDestroyWindow(ob_display, self->wins[i]);
433 if (screen_num_desktops != self->desks)
434 self->wins = g_renew(Window, self->wins, screen_num_desktops);
436 if (screen_num_desktops > self->desks)
437 for (i = self->desks; i < screen_num_desktops; ++i) {
438 XSetWindowAttributes attr;
440 attr.border_pixel = RrColorPixel(ob_rr_theme->frame_b_color);
441 self->wins[i] = XCreateWindow(ob_display, self->popup->bg,
442 0, 0, 1, 1, ob_rr_theme->fbwidth,
443 RrDepth(ob_rr_inst), InputOutput,
444 RrVisual(ob_rr_inst), CWBorderPixel,
446 XMapWindow(ob_display, self->wins[i]);
449 self->desks = screen_num_desktops;
450 self->curdesk = desk;
452 popup_show(self->popup, text);