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
35 #define OUTSIDE_BORDER 2
37 typedef struct _ObFocusCyclePopup ObFocusCyclePopup;
38 typedef struct _ObFocusCyclePopupTarget ObFocusCyclePopupTarget;
40 struct _ObFocusCyclePopupTarget
47 struct _ObFocusCyclePopup
57 ObFocusCyclePopupTarget *last_target;
65 RrPixel32 *hilite_rgba;
70 static ObFocusCyclePopup popup;
72 static gchar *popup_get_name (ObClient *c);
73 static void popup_setup (ObFocusCyclePopup *p,gboolean all_desktops,
74 gboolean dock_windows, gboolean desktop_windows);
75 static void popup_render (ObFocusCyclePopup *p, const ObClient *c);
77 static Window create_window(Window parent, guint bwidth, gulong mask,
78 XSetWindowAttributes *attr)
80 return XCreateWindow(ob_display, parent, 0, 0, 1, 1, bwidth,
81 RrDepth(ob_rr_inst), InputOutput,
82 RrVisual(ob_rr_inst), mask, attr);
85 void focus_cycle_popup_startup(gboolean reconfig)
87 XSetWindowAttributes attrib;
89 popup.obwin.type = Window_Internal;
90 popup.a_bg = RrAppearanceCopy(ob_rr_theme->osd_hilite_bg);
91 popup.a_text = RrAppearanceCopy(ob_rr_theme->osd_hilite_label);
92 popup.a_icon = RrAppearanceCopy(ob_rr_theme->a_clear_tex);
94 popup.a_text->surface.parent = popup.a_bg;
95 popup.a_icon->surface.parent = popup.a_bg;
97 popup.a_icon->texture[0].type = RR_TEXTURE_RGBA;
99 RrAppearanceAddTextures(popup.a_bg, 1);
100 popup.a_bg->texture[0].type = RR_TEXTURE_RGBA;
102 attrib.override_redirect = True;
103 attrib.border_pixel=RrColorPixel(ob_rr_theme->frame_focused_border_color);
104 popup.bg = create_window(RootWindow(ob_display, ob_screen),
105 ob_rr_theme->fbwidth,
106 CWOverrideRedirect | CWBorderPixel, &attrib);
108 popup.text = create_window(popup.bg, 0, 0, NULL);
110 popup.targets = NULL;
112 popup.last_target = NULL;
114 popup.hilite_rgba = NULL;
116 XMapWindow(ob_display, popup.text);
118 stacking_add(INTERNAL_AS_WINDOW(&popup));
121 void focus_cycle_popup_shutdown(gboolean reconfig)
123 stacking_remove(INTERNAL_AS_WINDOW(&popup));
125 while(popup.targets) {
126 ObFocusCyclePopupTarget *t = popup.targets->data;
129 XDestroyWindow(ob_display, t->win);
131 popup.targets = g_list_delete_link(popup.targets, popup.targets);
134 g_free(popup.hilite_rgba);
136 XDestroyWindow(ob_display, popup.text);
137 XDestroyWindow(ob_display, popup.bg);
139 RrAppearanceFree(popup.a_icon);
140 RrAppearanceFree(popup.a_text);
141 RrAppearanceFree(popup.a_bg);
144 static void popup_setup(ObFocusCyclePopup *p,gboolean all_desktops,
145 gboolean dock_windows, gboolean desktop_windows)
150 g_assert(p->targets == NULL);
151 g_assert(p->n_targets == 0);
153 /* make its width to be the width of all the possible titles */
155 /* build a list of all the valid focus targets and measure their strings,
159 for (it = g_list_last(focus_order); it; it = g_list_previous(it)) {
160 ObClient *ft = it->data;
162 if (focus_cycle_target_valid(ft,
167 ObFocusCyclePopupTarget *t = g_new(ObFocusCyclePopupTarget, 1);
170 t->text = popup_get_name(ft);
171 t->win = create_window(p->bg, 0, 0, NULL);
173 XMapWindow(ob_display, t->win);
176 p->a_text->texture[0].data.text.string = t->text;
177 maxwidth = MAX(maxwidth, RrMinWidth(p->a_text));
179 p->targets = g_list_prepend(p->targets, t);
185 p->maxtextw = maxwidth;
188 static gchar *popup_get_name(ObClient *c)
192 const gchar *desk = NULL;
195 /* find our highest direct parent, including non-normal windows */
196 for (p = c; p->transient_for && p->transient_for != OB_TRAN_GROUP;
197 p = p->transient_for);
199 if (c->desktop != DESKTOP_ALL && c->desktop != screen_desktop)
200 desk = screen_desktop_names[c->desktop];
202 /* use the transient's parent's title/icon if we don't have one */
203 if (p != c && !strcmp("", (c->iconic ? c->icon_title : c->title)))
204 title = g_strdup(p->iconic ? p->icon_title : p->title);
207 title = g_strdup(c->iconic ? c->icon_title : c->title);
210 ret = g_strdup_printf("%s [%s]", title, desk);
220 static void popup_render(ObFocusCyclePopup *p, const ObClient *c)
227 gint textx, texty, textw, texth;
231 const ObFocusCyclePopupTarget *newtarget;
232 gint newtargetx, newtargety;
234 /* XXX find the middle monitor? */
235 screen_area = screen_physical_area_monitor(0);
237 /* get the outside margins */
238 RrMargins(p->a_bg, &l, &t, &r, &b);
239 l += ob_rr_theme->paddingx + OUTSIDE_BORDER;
240 r += ob_rr_theme->paddingx + OUTSIDE_BORDER;
241 t += ob_rr_theme->paddingy + OUTSIDE_BORDER;
242 b += ob_rr_theme->paddingy + OUTSIDE_BORDER;
244 /* get the icons sizes */
245 iconw = ICON_SIZE - (ICON_HILITE_WIDTH + ob_rr_theme->paddingx) * 2;
246 iconh = ICON_SIZE - (ICON_HILITE_WIDTH + ob_rr_theme->paddingy) * 2;
248 /* get the width from the text and keep it within limits */
249 w = l + r + p->maxtextw;
250 w = MIN(w, MAX(screen_area->width/3, POPUP_WIDTH)); /* max width */
251 w = MAX(w, POPUP_WIDTH); /* min width */
253 /* how many icons will fit in that row? make the width fit that */
255 icons_per_row = w / ICON_SIZE;
256 w = icons_per_row * ICON_SIZE + l + r;
258 /* how many rows do we need? */
259 icon_rows = (p->n_targets-1) / icons_per_row + 1;
261 /* get the text dimensions */
263 texth = RrMinHeight(p->a_text);
265 /* find the height of the dialog */
266 h = t + b + (icon_rows * ICON_SIZE) +
267 (ob_rr_theme->paddingy * 2 + OUTSIDE_BORDER + texth);
269 /* get the position of the text */
271 texty = h - texth - b;
273 /* find the position for the popup (include the outer borders) */
274 x = screen_area->x + (screen_area->width -
275 (w + ob_rr_theme->fbwidth * 2)) / 2;
276 y = screen_area->y + (screen_area->height -
277 (h + ob_rr_theme->fbwidth * 2)) / 2;
280 /* position the background but don't draw it*/
281 XMoveResizeWindow(ob_display, p->bg, x, y, w, h);
283 /* set up the hilite texture for the background */
284 p->a_bg->texture[0].data.rgba.width = w;
285 p->a_bg->texture[0].data.rgba.height = h;
286 p->hilite_rgba = g_new(RrPixel32, w * h);
287 p->a_bg->texture[0].data.rgba.data = p->hilite_rgba;
289 /* position the text, but don't draw it */
290 XMoveResizeWindow(ob_display, p->text, textx, texty, textw, texth);
291 p->a_text->surface.parentx = textx;
292 p->a_text->surface.parenty = texty;
295 /* find the focused target */
296 for (i = 0, it = p->targets; it; ++i, it = g_list_next(it)) {
297 const ObFocusCyclePopupTarget *target = it->data;
298 const gint row = i / icons_per_row; /* starting from 0 */
299 const gint col = i % icons_per_row; /* starting from 0 */
301 if (target->client == c) {
302 /* save the target */
304 newtargetx = l + (col * ICON_SIZE);
305 newtargety = t + (row * ICON_SIZE);
308 break; /* if we're not dimensioning, then we're done */
312 g_assert(newtarget != NULL);
314 /* create the hilite under the target icon */
319 color = ((ob_rr_theme->osd_color->r & 0xff) << RrDefaultRedOffset) +
320 ((ob_rr_theme->osd_color->g & 0xff) << RrDefaultGreenOffset) +
321 ((ob_rr_theme->osd_color->b & 0xff) << RrDefaultBlueOffset);
323 for (i = 0; i < h; ++i)
324 for (j = 0; j < w; ++j) {
326 const gint x = j - newtargetx;
327 const gint y = i - newtargety;
329 if (x < 0 || x >= ICON_SIZE ||
330 y < 0 || y >= ICON_SIZE)
332 /* outside the target */
335 else if (x < ICON_HILITE_WIDTH ||
336 x >= ICON_SIZE-ICON_HILITE_WIDTH ||
337 y < ICON_HILITE_WIDTH ||
338 y >= ICON_SIZE-ICON_HILITE_WIDTH)
340 /* the border of the target */
344 /* the background of the target */
348 p->hilite_rgba[i * w + j] =
349 color + (a << RrDefaultAlphaOffset);
353 /* * * draw everything * * */
355 /* draw the background */
356 RrPaint(p->a_bg, p->bg, w, h);
359 for (i = 0, it = p->targets; it; ++i, it = g_list_next(it)) {
360 const ObFocusCyclePopupTarget *target = it->data;
362 /* have to redraw the targetted icon and last targetted icon,
363 they can pick up the hilite changes in the backgroud */
364 if (!p->mapped || newtarget == target || p->last_target == target) {
365 const ObClientIcon *icon;
366 const gint row = i / icons_per_row; /* starting from 0 */
367 const gint col = i % icons_per_row; /* starting from 0 */
370 /* find the dimensions of the icon inside it */
371 iconx = l + (col * ICON_SIZE) + ICON_HILITE_WIDTH +
372 ob_rr_theme->paddingx;
373 icony = t + (row * ICON_SIZE) + ICON_HILITE_WIDTH +
374 ob_rr_theme->paddingy;
377 XMoveResizeWindow(ob_display, target->win,
378 iconx, icony, iconw, iconh);
380 /* get the icon from the client */
381 icon = client_icon(target->client, iconw, iconh);
382 p->a_icon->texture[0].data.rgba.width = icon->width;
383 p->a_icon->texture[0].data.rgba.height = icon->height;
384 p->a_icon->texture[0].data.rgba.data = icon->data;
387 p->a_icon->surface.parentx = iconx;
388 p->a_icon->surface.parenty = icony;
389 RrPaint(p->a_icon, target->win, iconw, iconh);
394 p->a_text->texture[0].data.text.string = newtarget->text;
395 p->a_text->surface.parentx = textx;
396 p->a_text->surface.parenty = texty;
397 RrPaint(p->a_text, p->text, textw, texth);
399 p->last_target = newtarget;
402 void focus_cycle_popup_show(ObClient *c,
403 gboolean all_desktops, gboolean dock_windows,
404 gboolean desktop_windows)
408 /* do this stuff only when the dialog is first showing */
410 popup_setup(&popup, all_desktops, dock_windows, desktop_windows);
411 g_assert(popup.targets != NULL);
413 popup_render(&popup, c);
416 /* show the dialog */
417 XMapWindow(ob_display, popup.bg);
423 void focus_cycle_popup_hide()
425 XUnmapWindow(ob_display, popup.bg);
428 popup.mapped = FALSE;
430 while(popup.targets) {
431 ObFocusCyclePopupTarget *t = popup.targets->data;
434 XDestroyWindow(ob_display, t->win);
436 popup.targets = g_list_delete_link(popup.targets, popup.targets);
439 popup.last_target = NULL;
441 g_free(popup.hilite_rgba);
442 popup.hilite_rgba = NULL;