1 /* -*- indent-tabs-mode: nil; tab-width: 4; c-basic-offset: 4; -*-
3 focus_cycle_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.
20 #include "focus_cycle_popup.h"
25 #include "focus_cycle.h"
28 #include "render/render.h"
34 #define ICON_HILITE_WIDTH 2
36 typedef struct _ObFocusCyclePopup ObFocusCyclePopup;
37 typedef struct _ObFocusCyclePopupTarget ObFocusCyclePopupTarget;
39 struct _ObFocusCyclePopupTarget
46 struct _ObFocusCyclePopup
56 ObFocusCyclePopupTarget *last_target;
64 RrPixel32 *hilite_rgba;
69 static ObFocusCyclePopup popup;
71 static gchar *popup_get_name (ObClient *c);
72 static void popup_setup (ObFocusCyclePopup *p,gboolean all_desktops,
73 gboolean dock_windows, gboolean desktop_windows);
74 static void popup_render (ObFocusCyclePopup *p, const ObClient *c);
76 static Window create_window(Window parent, guint bwidth, gulong mask,
77 XSetWindowAttributes *attr)
79 return XCreateWindow(ob_display, parent, 0, 0, 1, 1, bwidth,
80 RrDepth(ob_rr_inst), InputOutput,
81 RrVisual(ob_rr_inst), mask, attr);
84 void focus_cycle_popup_startup(gboolean reconfig)
86 XSetWindowAttributes attrib;
88 popup.obwin.type = Window_Internal;
89 popup.a_bg = RrAppearanceCopy(ob_rr_theme->osd_hilite_bg);
90 popup.a_text = RrAppearanceCopy(ob_rr_theme->osd_hilite_label);
91 popup.a_icon = RrAppearanceCopy(ob_rr_theme->a_clear_tex);
93 popup.a_text->surface.parent = popup.a_bg;
94 popup.a_icon->surface.parent = popup.a_bg;
96 popup.a_icon->texture[0].type = RR_TEXTURE_RGBA;
98 RrAppearanceAddTextures(popup.a_bg, 1);
99 popup.a_bg->texture[0].type = RR_TEXTURE_RGBA;
101 attrib.override_redirect = True;
102 attrib.border_pixel=RrColorPixel(ob_rr_theme->frame_focused_border_color);
103 popup.bg = create_window(RootWindow(ob_display, ob_screen),
104 ob_rr_theme->fbwidth,
105 CWOverrideRedirect | CWBorderPixel, &attrib);
107 popup.text = create_window(popup.bg, 0, 0, NULL);
109 popup.targets = NULL;
111 popup.last_target = NULL;
113 popup.hilite_rgba = NULL;
115 XMapWindow(ob_display, popup.text);
117 stacking_add(INTERNAL_AS_WINDOW(&popup));
120 void focus_cycle_popup_shutdown(gboolean reconfig)
122 stacking_remove(INTERNAL_AS_WINDOW(&popup));
124 while(popup.targets) {
125 ObFocusCyclePopupTarget *t = popup.targets->data;
128 XDestroyWindow(ob_display, t->win);
130 popup.targets = g_list_delete_link(popup.targets, popup.targets);
133 g_free(popup.hilite_rgba);
135 XDestroyWindow(ob_display, popup.text);
136 XDestroyWindow(ob_display, popup.bg);
138 RrAppearanceFree(popup.a_icon);
139 RrAppearanceFree(popup.a_text);
140 RrAppearanceFree(popup.a_bg);
143 static void popup_setup(ObFocusCyclePopup *p,gboolean all_desktops,
144 gboolean dock_windows, gboolean desktop_windows)
149 g_assert(p->targets == NULL);
150 g_assert(p->n_targets == 0);
152 /* make its width to be the width of all the possible titles */
154 /* build a list of all the valid focus targets and measure their strings,
158 for (it = g_list_last(focus_order); it; it = g_list_previous(it)) {
159 ObClient *ft = it->data;
161 if (focus_cycle_target_valid(ft,
166 ObFocusCyclePopupTarget *t = g_new(ObFocusCyclePopupTarget, 1);
169 t->text = popup_get_name(ft);
170 t->win = create_window(p->bg, 0, 0, NULL);
172 XMapWindow(ob_display, t->win);
175 p->a_text->texture[0].data.text.string = t->text;
176 maxwidth = MAX(maxwidth, RrMinWidth(p->a_text));
178 p->targets = g_list_prepend(p->targets, t);
184 p->maxtextw = maxwidth;
187 static gchar *popup_get_name(ObClient *c)
191 const gchar *desk = NULL;
194 /* find our highest direct parent, including non-normal windows */
195 for (p = c; p->transient_for && p->transient_for != OB_TRAN_GROUP;
196 p = p->transient_for);
198 if (c->desktop != DESKTOP_ALL && c->desktop != screen_desktop)
199 desk = screen_desktop_names[c->desktop];
201 /* use the transient's parent's title/icon if we don't have one */
202 if (p != c && !strcmp("", (c->iconic ? c->icon_title : c->title)))
203 title = g_strdup(p->iconic ? p->icon_title : p->title);
206 title = g_strdup(c->iconic ? c->icon_title : c->title);
209 ret = g_strdup_printf("%s [%s]", title, desk);
219 static void popup_render(ObFocusCyclePopup *p, const ObClient *c)
226 gint textx, texty, textw, texth;
230 const ObFocusCyclePopupTarget *newtarget;
231 gint newtargetx, newtargety;
233 /* XXX find the middle monitor? */
234 screen_area = screen_physical_area_monitor(0);
236 /* get the outside margins */
237 RrMargins(p->a_bg, &l, &t, &r, &b);
238 l += ob_rr_theme->paddingx;
239 r += ob_rr_theme->paddingx;
240 t += ob_rr_theme->paddingy;
241 b += ob_rr_theme->paddingy;
243 /* get the icons sizes */
244 iconw = ICON_SIZE - (ICON_HILITE_WIDTH + ob_rr_theme->paddingx) * 2;
245 iconh = ICON_SIZE - (ICON_HILITE_WIDTH + ob_rr_theme->paddingy) * 2;
247 /* get the width from the text and keep it within limits */
248 w = l + r + p->maxtextw;
249 w = MIN(w, MAX(screen_area->width/3, POPUP_WIDTH)); /* max width */
250 w = MAX(w, POPUP_WIDTH); /* min width */
252 /* how many icons will fit in that row? make the width fit that */
254 icons_per_row = w / ICON_SIZE;
255 w = icons_per_row * ICON_SIZE + l + r;
257 /* how many rows do we need? */
258 icon_rows = (p->n_targets-1) / icons_per_row + 1;
260 /* get the text dimensions */
262 texth = RrMinHeight(p->a_text);
264 /* find the height of the dialog */
265 h = t + b + (icon_rows * ICON_SIZE) + (ob_rr_theme->paddingy + texth);
267 /* get the position of the text */
271 /* find the position for the popup (include the outer borders) */
272 x = screen_area->x + (screen_area->width -
273 (w + ob_rr_theme->fbwidth * 2)) / 2;
274 y = screen_area->y + (screen_area->height -
275 (h + ob_rr_theme->fbwidth * 2)) / 2;
278 /* position the background but don't draw it*/
279 XMoveResizeWindow(ob_display, p->bg, x, y, w, h);
281 /* set up the hilite texture for the background */
282 p->a_bg->texture[0].data.rgba.width = w;
283 p->a_bg->texture[0].data.rgba.height = h;
284 p->hilite_rgba = g_new(RrPixel32, w * h);
285 p->a_bg->texture[0].data.rgba.data = p->hilite_rgba;
287 /* position the text, but don't draw it */
288 XMoveResizeWindow(ob_display, p->text, textx, texty, textw, texth);
289 p->a_text->surface.parentx = textx;
290 p->a_text->surface.parenty = texty;
293 /* find the focused target */
294 for (i = 0, it = p->targets; it; ++i, it = g_list_next(it)) {
295 const ObFocusCyclePopupTarget *target = it->data;
296 const gint row = i / icons_per_row; /* starting from 0 */
297 const gint col = i % icons_per_row; /* starting from 0 */
299 if (target->client == c) {
300 /* save the target */
302 newtargetx = l + (col * ICON_SIZE);
303 newtargety = t + (row * ICON_SIZE);
306 break; /* if we're not dimensioning, then we're done */
310 g_assert(newtarget != NULL);
312 /* create the hilite under the target icon */
317 memset(p->hilite_rgba, color, w * h * sizeof(RrPixel32));
319 for (i = 0; i < ICON_SIZE; ++i)
320 for (j = 0; j < ICON_SIZE; ++j) {
323 if (i < ICON_HILITE_WIDTH ||
324 i >= ICON_SIZE-ICON_HILITE_WIDTH ||
325 j < ICON_HILITE_WIDTH ||
326 j >= ICON_SIZE-ICON_HILITE_WIDTH)
328 /* the border of the target */
332 /* the background of the target */
336 p->hilite_rgba[(i+newtargety) * w + (j+newtargetx)] =
337 color + (a << RrDefaultAlphaOffset);
341 /* * * draw everything * * */
343 /* draw the background */
344 RrPaint(p->a_bg, p->bg, w, h);
347 for (i = 0, it = p->targets; it; ++i, it = g_list_next(it)) {
348 const ObFocusCyclePopupTarget *target = it->data;
350 /* have to redraw the targetted icon and last targetted icon,
351 they can pick up the hilite changes in the backgroud */
352 if (!p->mapped || newtarget == target || p->last_target == target) {
353 const ObClientIcon *icon;
354 const gint row = i / icons_per_row; /* starting from 0 */
355 const gint col = i % icons_per_row; /* starting from 0 */
358 /* find the dimensions of the icon inside it */
359 iconx = l + (col * ICON_SIZE) + ICON_HILITE_WIDTH +
360 ob_rr_theme->paddingx;
361 icony = t + (row * ICON_SIZE) + ICON_HILITE_WIDTH +
362 ob_rr_theme->paddingy;
365 XMoveResizeWindow(ob_display, target->win,
366 iconx, icony, iconw, iconh);
368 /* get the icon from the client */
369 icon = client_icon(target->client, iconw, iconh);
370 p->a_icon->texture[0].data.rgba.width = icon->width;
371 p->a_icon->texture[0].data.rgba.height = icon->height;
372 p->a_icon->texture[0].data.rgba.data = icon->data;
375 p->a_icon->surface.parentx = iconx;
376 p->a_icon->surface.parenty = icony;
377 RrPaint(p->a_icon, target->win, iconw, iconh);
382 p->a_text->texture[0].data.text.string = newtarget->text;
383 p->a_text->surface.parentx = textx;
384 p->a_text->surface.parenty = texty;
385 RrPaint(p->a_text, p->text, textw, texth);
387 p->last_target = newtarget;
390 void focus_cycle_popup_show(ObClient *c,
391 gboolean all_desktops, gboolean dock_windows,
392 gboolean desktop_windows)
396 /* do this stuff only when the dialog is first showing */
398 popup_setup(&popup, all_desktops, dock_windows, desktop_windows);
399 g_assert(popup.targets != NULL);
401 popup_render(&popup, c);
404 /* show the dialog */
405 XMapWindow(ob_display, popup.bg);
411 void focus_cycle_popup_hide()
413 XUnmapWindow(ob_display, popup.bg);
416 popup.mapped = FALSE;
418 while(popup.targets) {
419 ObFocusCyclePopupTarget *t = popup.targets->data;
422 XDestroyWindow(ob_display, t->win);
424 popup.targets = g_list_delete_link(popup.targets, popup.targets);
427 popup.last_target = NULL;
429 g_free(popup.hilite_rgba);
430 popup.hilite_rgba = NULL;