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 Ben 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.
27 #include "render/render.h"
28 #include "render/theme.h"
30 ObPopup *popup_new(gboolean hasicon)
32 XSetWindowAttributes attrib;
33 ObPopup *self = g_new0(ObPopup, 1);
35 self->obwin.type = Window_Internal;
36 self->hasicon = hasicon;
37 self->gravity = NorthWestGravity;
38 self->x = self->y = self->w = self->h = 0;
39 self->a_bg = RrAppearanceCopy(ob_rr_theme->osd_hilite_bg);
40 self->a_text = RrAppearanceCopy(ob_rr_theme->osd_hilite_label);
42 attrib.override_redirect = True;
43 self->bg = XCreateWindow(ob_display, RootWindow(ob_display, ob_screen),
44 0, 0, 1, 1, 0, RrDepth(ob_rr_inst),
45 InputOutput, RrVisual(ob_rr_inst),
46 CWOverrideRedirect, &attrib);
48 self->text = XCreateWindow(ob_display, self->bg,
49 0, 0, 1, 1, 0, RrDepth(ob_rr_inst),
50 InputOutput, RrVisual(ob_rr_inst), 0, NULL);
52 XMapWindow(ob_display, self->text);
54 stacking_add(INTERNAL_AS_WINDOW(self));
58 void popup_free(ObPopup *self)
61 XDestroyWindow(ob_display, self->bg);
62 XDestroyWindow(ob_display, self->text);
63 RrAppearanceFree(self->a_bg);
64 RrAppearanceFree(self->a_text);
65 stacking_remove(self);
70 void popup_position(ObPopup *self, gint gravity, gint x, gint y)
72 self->gravity = gravity;
77 void popup_size(ObPopup *self, gint w, gint h)
83 void popup_size_to_string(ObPopup *self, gchar *text)
88 self->a_text->texture[0].data.text.string = text;
89 RrMinsize(self->a_text, &textw, &texth);
90 /*XXX textw += ob_rr_theme->bevel * 2;*/
91 texth += ob_rr_theme->paddingy * 2;
93 self->h = texth + ob_rr_theme->paddingy * 2;
94 iconw = (self->hasicon ? texth : 0);
95 self->w = textw + iconw + ob_rr_theme->paddingx * (self->hasicon ? 3 : 2);
98 void popup_set_text_align(ObPopup *self, RrJustify align)
100 self->a_text->texture[0].data.text.justify = align;
103 void popup_show(ObPopup *self, gchar *text)
109 Rect *area; /* won't go outside this */
111 area = screen_physical_area(); /* XXX this should work quite
112 good, someone with xinerama,
113 and different resolutions on
116 RrMargins(self->a_bg, &l, &t, &r, &b);
118 XSetWindowBorderWidth(ob_display, self->bg, ob_rr_theme->fbwidth);
119 XSetWindowBorder(ob_display, self->bg, ob_rr_theme->frame_b_color->pixel);
121 /* set up the textures */
122 self->a_text->texture[0].data.text.string = text;
124 /* measure the shit out */
125 RrMinsize(self->a_text, &textw, &texth);
126 /*XXX textw += ob_rr_theme->padding * 2;*/
127 texth += ob_rr_theme->paddingy * 2;
129 /* set the sizes up and reget the text sizes from the calculated
133 texth = h - (t+b + ob_rr_theme->paddingy * 2);
135 h = t+b + texth + ob_rr_theme->paddingy * 2;
136 iconw = (self->hasicon ? texth : 0);
139 textw = w - (l+r + iconw + ob_rr_theme->paddingx *
140 (self->hasicon ? 3 : 2));
142 w = l+r + textw + iconw + ob_rr_theme->paddingx *
143 (self->hasicon ? 3 : 2);
144 /* sanity checks to avoid crashes! */
147 if (textw < 1) textw = 1;
148 if (texth < 1) texth = 1;
150 /* set up the x coord */
152 switch (self->gravity) {
158 case NorthEastGravity:
160 case SouthEastGravity:
165 /* set up the y coord */
167 switch (self->gravity) {
173 case SouthWestGravity:
175 case SouthEastGravity:
180 x=MAX(MIN(x, area->width-w),0);
181 y=MAX(MIN(y, area->height-h),0);
183 /* set the windows/appearances up */
184 XMoveResizeWindow(ob_display, self->bg, x, y, w, h);
186 self->a_text->surface.parent = self->a_bg;
187 self->a_text->surface.parentx = l + iconw +
188 ob_rr_theme->paddingx * (self->hasicon ? 2 : 1);
189 self->a_text->surface.parenty = t + ob_rr_theme->paddingy;
190 XMoveResizeWindow(ob_display, self->text,
191 l + iconw + ob_rr_theme->paddingx *
192 (self->hasicon ? 2 : 1),
193 t + ob_rr_theme->paddingy, textw, texth);
195 RrPaint(self->a_bg, self->bg, w, h);
196 RrPaint(self->a_text, self->text, textw, texth);
199 if (iconw < 1) iconw = 1; /* sanity check for crashes */
201 self->draw_icon(l + ob_rr_theme->paddingx,
202 t + ob_rr_theme->paddingy,
203 iconw, texth, self->draw_icon_data);
207 XMapWindow(ob_display, self->bg);
208 stacking_raise(INTERNAL_AS_WINDOW(self));
213 void popup_hide(ObPopup *self)
216 XUnmapWindow(ob_display, self->bg);
217 self->mapped = FALSE;
219 /* kill enter events cause by this unmapping */
220 event_ignore_queued_enters();
224 static void icon_popup_draw_icon(gint x, gint y, gint w, gint h, gpointer data)
226 ObIconPopup *self = data;
228 self->a_icon->surface.parent = self->popup->a_bg;
229 self->a_icon->surface.parentx = x;
230 self->a_icon->surface.parenty = y;
231 XMoveResizeWindow(ob_display, self->icon, x, y, w, h);
232 RrPaint(self->a_icon, self->icon, w, h);
235 ObIconPopup *icon_popup_new()
239 self = g_new0(ObIconPopup, 1);
240 self->popup = popup_new(TRUE);
241 self->a_icon = RrAppearanceCopy(ob_rr_theme->a_clear_tex);
242 self->icon = XCreateWindow(ob_display, self->popup->bg,
244 RrDepth(ob_rr_inst), InputOutput,
245 RrVisual(ob_rr_inst), 0, NULL);
246 XMapWindow(ob_display, self->icon);
248 self->popup->draw_icon = icon_popup_draw_icon;
249 self->popup->draw_icon_data = self;
254 void icon_popup_free(ObIconPopup *self)
257 XDestroyWindow(ob_display, self->icon);
258 RrAppearanceFree(self->a_icon);
259 popup_free(self->popup);
264 void icon_popup_show(ObIconPopup *self,
265 gchar *text, const ObClientIcon *icon)
268 self->a_icon->texture[0].type = RR_TEXTURE_RGBA;
269 self->a_icon->texture[0].data.rgba.width = icon->width;
270 self->a_icon->texture[0].data.rgba.height = icon->height;
271 self->a_icon->texture[0].data.rgba.data = icon->data;
273 self->a_icon->texture[0].type = RR_TEXTURE_NONE;
275 popup_show(self->popup, text);
278 static void pager_popup_draw_icon(gint px, gint py, gint w, gint h,
281 ObPagerPopup *self = data;
289 eachw = (w - ob_rr_theme->fbwidth -
290 (screen_desktop_layout.columns * ob_rr_theme->fbwidth))
291 / screen_desktop_layout.columns;
292 eachh = (h - ob_rr_theme->fbwidth -
293 (screen_desktop_layout.rows * ob_rr_theme->fbwidth))
294 / screen_desktop_layout.rows;
295 /* make them squares */
296 eachw = eachh = MIN(eachw, eachh);
299 px += (w - (screen_desktop_layout.columns * (eachw + ob_rr_theme->fbwidth) +
300 ob_rr_theme->fbwidth)) / 2;
301 py += (h - (screen_desktop_layout.rows * (eachh + ob_rr_theme->fbwidth) +
302 ob_rr_theme->fbwidth)) / 2;
304 if (eachw <= 0 || eachh <= 0)
307 switch (screen_desktop_layout.orientation) {
308 case OB_ORIENTATION_HORZ:
309 switch (screen_desktop_layout.start_corner) {
310 case OB_CORNER_TOPLEFT:
313 vert_inc = screen_desktop_layout.columns;
315 case OB_CORNER_TOPRIGHT:
316 n = screen_desktop_layout.columns - 1;
318 vert_inc = screen_desktop_layout.columns;
320 case OB_CORNER_BOTTOMRIGHT:
321 n = screen_desktop_layout.rows * screen_desktop_layout.columns - 1;
323 vert_inc = -screen_desktop_layout.columns;
325 case OB_CORNER_BOTTOMLEFT:
326 n = (screen_desktop_layout.rows - 1)
327 * screen_desktop_layout.columns;
329 vert_inc = -screen_desktop_layout.columns;
332 g_assert_not_reached();
335 case OB_ORIENTATION_VERT:
336 switch (screen_desktop_layout.start_corner) {
337 case OB_CORNER_TOPLEFT:
339 horz_inc = screen_desktop_layout.rows;
342 case OB_CORNER_TOPRIGHT:
343 n = screen_desktop_layout.rows
344 * (screen_desktop_layout.columns - 1);
345 horz_inc = -screen_desktop_layout.rows;
348 case OB_CORNER_BOTTOMRIGHT:
349 n = screen_desktop_layout.rows * screen_desktop_layout.columns - 1;
350 horz_inc = -screen_desktop_layout.rows;
353 case OB_CORNER_BOTTOMLEFT:
354 n = screen_desktop_layout.rows - 1;
355 horz_inc = screen_desktop_layout.rows;
359 g_assert_not_reached();
363 g_assert_not_reached();
367 for (r = 0, y = 0; r < screen_desktop_layout.rows;
368 ++r, y += eachh + ob_rr_theme->fbwidth)
370 for (c = 0, x = 0; c < screen_desktop_layout.columns;
371 ++c, x += eachw + ob_rr_theme->fbwidth)
375 if (n < self->desks) {
376 a = (n == self->curdesk ? self->hilight : self->unhilight);
378 a->surface.parent = self->popup->a_bg;
379 a->surface.parentx = x + px;
380 a->surface.parenty = y + py;
381 XMoveResizeWindow(ob_display, self->wins[n],
382 x + px, y + py, eachw, eachh);
383 RrPaint(a, self->wins[n], eachw, eachh);
387 n = rown += vert_inc;
391 ObPagerPopup *pager_popup_new()
395 self = g_new(ObPagerPopup, 1);
396 self->popup = popup_new(TRUE);
399 self->wins = g_new(Window, self->desks);
400 self->hilight = RrAppearanceCopy(ob_rr_theme->osd_hilite_fg);
401 self->unhilight = RrAppearanceCopy(ob_rr_theme->osd_unhilite_fg);
403 self->popup->draw_icon = pager_popup_draw_icon;
404 self->popup->draw_icon_data = self;
409 void pager_popup_free(ObPagerPopup *self)
414 for (i = 0; i < self->desks; ++i)
415 XDestroyWindow(ob_display, self->wins[i]);
417 RrAppearanceFree(self->hilight);
418 RrAppearanceFree(self->unhilight);
419 popup_free(self->popup);
424 void pager_popup_show(ObPagerPopup *self, gchar *text, guint desk)
428 if (screen_num_desktops < self->desks)
429 for (i = screen_num_desktops; i < self->desks; ++i)
430 XDestroyWindow(ob_display, self->wins[i]);
432 if (screen_num_desktops != self->desks)
433 self->wins = g_renew(Window, self->wins, screen_num_desktops);
435 if (screen_num_desktops > self->desks)
436 for (i = self->desks; i < screen_num_desktops; ++i) {
437 XSetWindowAttributes attr;
439 attr.border_pixel = RrColorPixel(ob_rr_theme->frame_b_color);
440 self->wins[i] = XCreateWindow(ob_display, self->popup->bg,
441 0, 0, 1, 1, ob_rr_theme->fbwidth,
442 RrDepth(ob_rr_inst), InputOutput,
443 RrVisual(ob_rr_inst), CWBorderPixel,
445 XMapWindow(ob_display, self->wins[i]);
448 self->desks = screen_num_desktops;
449 self->curdesk = desk;
451 popup_show(self->popup, text);