1 /* -*- indent-tabs-mode: nil; tab-width: 4; c-basic-offset: 4; -*-
3 popup.c for the Openbox window manager
4 Copyright (c) 2003 Ben Jansens
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 See the COPYING file for a copy of the GNU General Public License.
26 #include "render/render.h"
27 #include "render/theme.h"
29 ObPopup *popup_new(gboolean hasicon)
31 XSetWindowAttributes attrib;
32 ObPopup *self = g_new0(ObPopup, 1);
34 self->obwin.type = Window_Internal;
35 self->hasicon = hasicon;
36 self->gravity = NorthWestGravity;
37 self->x = self->y = self->w = self->h = 0;
38 self->a_bg = RrAppearanceCopy(ob_rr_theme->app_hilite_bg);
39 self->a_text = RrAppearanceCopy(ob_rr_theme->app_hilite_label);
41 attrib.override_redirect = True;
42 self->bg = XCreateWindow(ob_display, RootWindow(ob_display, ob_screen),
43 0, 0, 1, 1, 0, RrDepth(ob_rr_inst),
44 InputOutput, RrVisual(ob_rr_inst),
45 CWOverrideRedirect, &attrib);
47 self->text = XCreateWindow(ob_display, self->bg,
48 0, 0, 1, 1, 0, RrDepth(ob_rr_inst),
49 InputOutput, RrVisual(ob_rr_inst), 0, NULL);
51 XMapWindow(ob_display, self->text);
53 stacking_add(INTERNAL_AS_WINDOW(self));
57 void popup_free(ObPopup *self)
60 XDestroyWindow(ob_display, self->bg);
61 XDestroyWindow(ob_display, self->text);
62 RrAppearanceFree(self->a_bg);
63 RrAppearanceFree(self->a_text);
64 stacking_remove(self);
69 void popup_position(ObPopup *self, gint gravity, gint x, gint y)
71 self->gravity = gravity;
76 void popup_size(ObPopup *self, gint w, gint h)
82 void popup_size_to_string(ObPopup *self, gchar *text)
87 self->a_text->texture[0].data.text.string = text;
88 RrMinsize(self->a_text, &textw, &texth);
89 /*XXX textw += ob_rr_theme->bevel * 2;*/
90 texth += ob_rr_theme->padding * 2;
92 self->h = texth + ob_rr_theme->padding * 2;
93 iconw = (self->hasicon ? texth : 0);
94 self->w = textw + iconw + ob_rr_theme->padding * (self->hasicon ? 3 : 2);
97 void popup_set_text_align(ObPopup *self, RrJustify align)
99 self->a_text->texture[0].data.text.justify = align;
102 void popup_show(ObPopup *self, gchar *text)
108 Rect *area; /* won't go outside this */
110 area = screen_physical_area_monitor(0); /* XXX i'm guessing this
111 is wrong for xinerama? */
113 RrMargins(self->a_bg, &l, &t, &r, &b);
115 XSetWindowBorderWidth(ob_display, self->bg, ob_rr_theme->bwidth);
116 XSetWindowBorder(ob_display, self->bg, ob_rr_theme->b_color->pixel);
118 /* set up the textures */
119 self->a_text->texture[0].data.text.string = text;
121 /* measure the shit out */
122 RrMinsize(self->a_text, &textw, &texth);
123 /*XXX textw += ob_rr_theme->padding * 2;*/
124 texth += ob_rr_theme->padding * 2;
126 /* set the sizes up and reget the text sizes from the calculated
130 texth = h - (t+b + ob_rr_theme->padding * 2);
132 h = t+b + texth + ob_rr_theme->padding * 2;
133 iconw = (self->hasicon ? texth : 0);
136 textw = w - (l+r + iconw + ob_rr_theme->padding *
137 (self->hasicon ? 3 : 2));
139 w = l+r + textw + iconw + ob_rr_theme->padding *
140 (self->hasicon ? 3 : 2);
141 /* sanity checks to avoid crashes! */
144 if (textw < 1) textw = 1;
145 if (texth < 1) texth = 1;
147 /* set up the x coord */
149 switch (self->gravity) {
155 case NorthEastGravity:
157 case SouthEastGravity:
162 /* set up the y coord */
164 switch (self->gravity) {
170 case SouthWestGravity:
172 case SouthEastGravity:
177 x=MAX(MIN(x, area->width-w),0);
178 y=MAX(MIN(y, area->height-h),0);
180 /* set the windows/appearances up */
181 XMoveResizeWindow(ob_display, self->bg, x, y, w, h);
183 self->a_text->surface.parent = self->a_bg;
184 self->a_text->surface.parentx = l + iconw +
185 ob_rr_theme->padding * (self->hasicon ? 2 : 1);
186 self->a_text->surface.parenty = t + ob_rr_theme->padding;
187 XMoveResizeWindow(ob_display, self->text,
188 l + iconw + ob_rr_theme->padding *
189 (self->hasicon ? 2 : 1),
190 t + ob_rr_theme->padding, textw, texth);
192 RrPaint(self->a_bg, self->bg, w, h);
193 RrPaint(self->a_text, self->text, textw, texth);
196 if (iconw < 1) iconw = 1; /* sanity check for crashes */
198 self->draw_icon(l + ob_rr_theme->padding, t + ob_rr_theme->padding,
199 iconw, texth, self->draw_icon_data);
203 XMapWindow(ob_display, self->bg);
204 stacking_raise(INTERNAL_AS_WINDOW(self));
209 void popup_hide(ObPopup *self)
212 XUnmapWindow(ob_display, self->bg);
213 self->mapped = FALSE;
217 static void icon_popup_draw_icon(gint x, gint y, gint w, gint h, gpointer data)
219 ObIconPopup *self = data;
221 self->a_icon->surface.parent = self->popup->a_bg;
222 self->a_icon->surface.parentx = x;
223 self->a_icon->surface.parenty = y;
224 XMoveResizeWindow(ob_display, self->icon, x, y, w, h);
225 RrPaint(self->a_icon, self->icon, w, h);
228 ObIconPopup *icon_popup_new()
232 self = g_new0(ObIconPopup, 1);
233 self->popup = popup_new(TRUE);
234 self->a_icon = RrAppearanceCopy(ob_rr_theme->a_clear_tex);
235 self->icon = XCreateWindow(ob_display, self->popup->bg,
237 RrDepth(ob_rr_inst), InputOutput,
238 RrVisual(ob_rr_inst), 0, NULL);
239 XMapWindow(ob_display, self->icon);
241 self->popup->draw_icon = icon_popup_draw_icon;
242 self->popup->draw_icon_data = self;
247 void icon_popup_free(ObIconPopup *self)
250 XDestroyWindow(ob_display, self->icon);
251 RrAppearanceFree(self->a_icon);
252 popup_free(self->popup);
257 void icon_popup_show(ObIconPopup *self,
258 gchar *text, const ObClientIcon *icon)
261 self->a_icon->texture[0].type = RR_TEXTURE_RGBA;
262 self->a_icon->texture[0].data.rgba.width = icon->width;
263 self->a_icon->texture[0].data.rgba.height = icon->height;
264 self->a_icon->texture[0].data.rgba.data = icon->data;
266 self->a_icon->texture[0].type = RR_TEXTURE_NONE;
268 popup_show(self->popup, text);
271 static void pager_popup_draw_icon(gint px, gint py, gint w, gint h,
274 ObPagerPopup *self = data;
283 eachw = (w - ob_rr_theme->bwidth -
284 (screen_desktop_layout.columns * ob_rr_theme->bwidth))
285 / screen_desktop_layout.columns;
286 eachh = (h - ob_rr_theme->bwidth -
287 (screen_desktop_layout.rows * ob_rr_theme->bwidth))
288 / screen_desktop_layout.rows;
289 /* make them squares */
290 eachw = eachh = MIN(eachw, eachh);
293 px += (w - (screen_desktop_layout.columns * (eachw + ob_rr_theme->bwidth) +
294 ob_rr_theme->bwidth)) / 2;
295 py += (h - (screen_desktop_layout.rows * (eachh + ob_rr_theme->bwidth) +
296 ob_rr_theme->bwidth)) / 2;
298 if (eachw <= 0 || eachh <= 0)
301 switch (screen_desktop_layout.start_corner) {
302 case OB_CORNER_TOPLEFT:
304 switch (screen_desktop_layout.orientation) {
305 case OB_ORIENTATION_HORZ:
307 vert_inc = screen_desktop_layout.columns;
309 case OB_ORIENTATION_VERT:
310 horz_inc = screen_desktop_layout.rows;
315 case OB_CORNER_TOPRIGHT:
316 n = screen_desktop_layout.columns;
317 switch (screen_desktop_layout.orientation) {
318 case OB_ORIENTATION_HORZ:
320 vert_inc = screen_desktop_layout.columns;
322 case OB_ORIENTATION_VERT:
323 horz_inc = -screen_desktop_layout.rows;
328 case OB_CORNER_BOTTOMLEFT:
329 n = screen_desktop_layout.rows;
330 switch (screen_desktop_layout.orientation) {
331 case OB_ORIENTATION_HORZ:
333 vert_inc = -screen_desktop_layout.columns;
335 case OB_ORIENTATION_VERT:
336 horz_inc = screen_desktop_layout.rows;
341 case OB_CORNER_BOTTOMRIGHT:
343 screen_desktop_layout.rows * screen_desktop_layout.columns);
344 switch (screen_desktop_layout.orientation) {
345 case OB_ORIENTATION_HORZ:
347 vert_inc = -screen_desktop_layout.columns;
349 case OB_ORIENTATION_VERT:
350 horz_inc = -screen_desktop_layout.rows;
359 for (r = 0, y = 0; r < screen_desktop_layout.rows;
360 ++r, y += eachh + ob_rr_theme->bwidth)
362 for (c = 0, x = 0; c < screen_desktop_layout.columns;
363 ++c, x += eachw + ob_rr_theme->bwidth)
367 if (i >= self->desks)
370 a = (n == self->curdesk ? self->hilight : self->unhilight);
372 a->surface.parent = self->popup->a_bg;
373 a->surface.parentx = x + px;
374 a->surface.parenty = y + py;
375 XMoveResizeWindow(ob_display, self->wins[i],
376 x + px, y + py, eachw, eachh);
377 RrPaint(a, self->wins[i], eachw, eachh);
383 n = rown += vert_inc;
387 ObPagerPopup *pager_popup_new()
391 self = g_new(ObPagerPopup, 1);
392 self->popup = popup_new(TRUE);
395 self->wins = g_new(Window, self->desks);
396 self->hilight = RrAppearanceCopy(ob_rr_theme->app_hilite_fg);
397 self->unhilight = RrAppearanceCopy(ob_rr_theme->app_unhilite_fg);
399 self->popup->draw_icon = pager_popup_draw_icon;
400 self->popup->draw_icon_data = self;
405 void pager_popup_free(ObPagerPopup *self)
410 for (i = 0; i < self->desks; ++i)
411 XDestroyWindow(ob_display, self->wins[i]);
413 RrAppearanceFree(self->hilight);
414 RrAppearanceFree(self->unhilight);
415 popup_free(self->popup);
420 void pager_popup_show(ObPagerPopup *self, gchar *text, guint desk)
424 if (screen_num_desktops < self->desks)
425 for (i = screen_num_desktops; i < self->desks; ++i)
426 XDestroyWindow(ob_display, self->wins[i]);
428 if (screen_num_desktops != self->desks)
429 self->wins = g_renew(Window, self->wins, screen_num_desktops);
431 if (screen_num_desktops > self->desks)
432 for (i = self->desks; i < screen_num_desktops; ++i) {
433 XSetWindowAttributes attr;
435 attr.border_pixel = RrColorPixel(ob_rr_theme->b_color);
436 self->wins[i] = XCreateWindow(ob_display, self->popup->bg,
437 0, 0, 1, 1, ob_rr_theme->bwidth,
438 RrDepth(ob_rr_inst), InputOutput,
439 RrVisual(ob_rr_inst), CWBorderPixel,
441 XMapWindow(ob_display, self->wins[i]);
444 self->desks = screen_num_desktops;
445 self->curdesk = desk;
447 popup_show(self->popup, text);