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"
28 #include "render/render.h"
34 #define ICON_HILITE_WIDTH 2
35 #define ICON_HILITE_MARGIN 1
36 #define OUTSIDE_BORDER 3
39 typedef struct _ObFocusCyclePopup ObFocusCyclePopup;
40 typedef struct _ObFocusCyclePopupTarget ObFocusCyclePopupTarget;
42 struct _ObFocusCyclePopupTarget
50 struct _ObFocusCyclePopup
60 const ObFocusCyclePopupTarget *last_target;
68 RrPixel32 *hilite_rgba;
73 /*! This popup shows all possible windows */
74 static ObFocusCyclePopup popup;
75 /*! This popup shows a single window */
76 static ObIconPopup *single_popup;
78 static gchar *popup_get_name (ObClient *c);
79 static void popup_setup (ObFocusCyclePopup *p,
80 gboolean create_targets,
81 gboolean iconic_windows,
82 gboolean all_desktops,
83 gboolean dock_windows,
84 gboolean desktop_windows);
85 static void popup_render (ObFocusCyclePopup *p,
88 static Window create_window(Window parent, guint bwidth, gulong mask,
89 XSetWindowAttributes *attr)
91 return XCreateWindow(ob_display, parent, 0, 0, 1, 1, bwidth,
92 RrDepth(ob_rr_inst), InputOutput,
93 RrVisual(ob_rr_inst), mask, attr);
96 void focus_cycle_popup_startup(gboolean reconfig)
98 XSetWindowAttributes attrib;
100 single_popup = icon_popup_new();
102 popup.obwin.type = Window_Internal;
103 popup.a_bg = RrAppearanceCopy(ob_rr_theme->osd_hilite_bg);
104 popup.a_text = RrAppearanceCopy(ob_rr_theme->osd_hilite_label);
105 popup.a_icon = RrAppearanceCopy(ob_rr_theme->a_clear_tex);
107 popup.a_text->surface.parent = popup.a_bg;
108 popup.a_icon->surface.parent = popup.a_bg;
110 RrAppearanceClearTextures(popup.a_icon);
111 popup.a_icon->texture[0].type = RR_TEXTURE_IMAGE;
113 RrAppearanceAddTextures(popup.a_bg, 1);
114 popup.a_bg->texture[0].type = RR_TEXTURE_RGBA;
116 attrib.override_redirect = True;
117 attrib.border_pixel=RrColorPixel(ob_rr_theme->osd_border_color);
118 popup.bg = create_window(RootWindow(ob_display, ob_screen),
119 ob_rr_theme->obwidth,
120 CWOverrideRedirect | CWBorderPixel, &attrib);
122 popup.text = create_window(popup.bg, 0, 0, NULL);
124 popup.targets = NULL;
126 popup.last_target = NULL;
128 popup.hilite_rgba = NULL;
130 XMapWindow(ob_display, popup.text);
132 stacking_add(INTERNAL_AS_WINDOW(&popup));
133 g_hash_table_insert(window_map, &popup.bg, &popup);
136 void focus_cycle_popup_shutdown(gboolean reconfig)
138 icon_popup_free(single_popup);
140 g_hash_table_remove(window_map, &popup.bg);
141 stacking_remove(INTERNAL_AS_WINDOW(&popup));
143 while(popup.targets) {
144 ObFocusCyclePopupTarget *t = popup.targets->data;
146 RrImageUnref(t->icon);
148 XDestroyWindow(ob_display, t->win);
151 popup.targets = g_list_delete_link(popup.targets, popup.targets);
154 g_free(popup.hilite_rgba);
155 popup.hilite_rgba = NULL;
157 XDestroyWindow(ob_display, popup.text);
158 XDestroyWindow(ob_display, popup.bg);
160 RrAppearanceFree(popup.a_icon);
161 RrAppearanceFree(popup.a_text);
162 RrAppearanceFree(popup.a_bg);
165 static void popup_setup(ObFocusCyclePopup *p, gboolean create_targets,
166 gboolean iconic_windows, gboolean all_desktops,
167 gboolean dock_windows, gboolean desktop_windows)
172 g_assert(p->targets == NULL);
173 g_assert(p->n_targets == 0);
175 /* make its width to be the width of all the possible titles */
177 /* build a list of all the valid focus targets and measure their strings,
181 for (it = g_list_last(focus_order); it; it = g_list_previous(it)) {
182 ObClient *ft = it->data;
184 if (focus_valid_target(ft, TRUE,
191 gchar *text = popup_get_name(ft);
194 p->a_text->texture[0].data.text.string = text;
195 maxwidth = MAX(maxwidth, RrMinWidth(p->a_text));
200 ObFocusCyclePopupTarget *t = g_new(ObFocusCyclePopupTarget, 1);
204 t->icon = client_icon(t->client);
205 RrImageRef(t->icon); /* own the icon so it won't go away */
206 t->win = create_window(p->bg, 0, 0, NULL);
208 XMapWindow(ob_display, t->win);
210 p->targets = g_list_prepend(p->targets, t);
217 p->maxtextw = maxwidth;
220 static gchar *popup_get_name(ObClient *c)
224 const gchar *desk = NULL;
227 /* find our highest direct parent */
228 p = client_search_top_direct_parent(c);
230 if (c->desktop != DESKTOP_ALL && c->desktop != screen_desktop)
231 desk = screen_desktop_names[c->desktop];
233 title = c->iconic ? c->icon_title : c->title;
235 /* use the transient's parent's title/icon if we don't have one */
236 if (p != c && title[0] == '\0')
237 title = p->iconic ? p->icon_title : p->title;
240 ret = g_strdup_printf("%s [%s]", title, desk);
242 ret = g_strdup(title);
247 static void popup_render(ObFocusCyclePopup *p, const ObClient *c)
252 Rect *screen_area = NULL;
255 gint textx, texty, textw, texth;
256 gint rgbax, rgbay, rgbaw, rgbah;
261 const ObFocusCyclePopupTarget *newtarget;
262 gint newtargetx, newtargety;
264 screen_area = screen_physical_area_primary();
266 /* get the outside margins */
267 RrMargins(p->a_bg, &ml, &mt, &mr, &mb);
269 /* get our outside borders */
270 l = ml + OUTSIDE_BORDER;
271 r = mr + OUTSIDE_BORDER;
272 t = mt + OUTSIDE_BORDER;
273 b = mb + OUTSIDE_BORDER;
275 /* get the icon pictures' sizes */
276 innerw = ICON_SIZE - (ICON_HILITE_WIDTH + ICON_HILITE_MARGIN) * 2;
277 innerh = ICON_SIZE - (ICON_HILITE_WIDTH + ICON_HILITE_MARGIN) * 2;
279 /* get the width from the text and keep it within limits */
280 w = l + r + p->maxtextw;
281 w = MIN(w, MAX(screen_area->width/3, POPUP_WIDTH)); /* max width */
282 w = MAX(w, POPUP_WIDTH); /* min width */
284 /* how many icons will fit in that row? make the width fit that */
286 icons_per_row = (w + ICON_SIZE - 1) / ICON_SIZE;
287 w = icons_per_row * ICON_SIZE + l + r;
289 /* how many rows do we need? */
290 icon_rows = (p->n_targets-1) / icons_per_row + 1;
292 /* get the text dimensions */
294 texth = RrMinHeight(p->a_text) + TEXT_BORDER * 2;
296 /* find the height of the dialog */
297 h = t + b + (icon_rows * ICON_SIZE) + (OUTSIDE_BORDER + texth);
299 /* get the position of the text */
301 texty = h - texth - b;
303 /* find the position for the popup (include the outer borders) */
304 x = screen_area->x + (screen_area->width -
305 (w + ob_rr_theme->obwidth * 2)) / 2;
306 y = screen_area->y + (screen_area->height -
307 (h + ob_rr_theme->obwidth * 2)) / 2;
309 /* get the dimensions of the target hilite texture */
315 /* center the icons if there is less than one row */
317 icons_center_x = (w - p->n_targets * ICON_SIZE) / 2;
322 /* position the background but don't draw it*/
323 XMoveResizeWindow(ob_display, p->bg, x, y, w, h);
325 /* set up the hilite texture for the background */
326 p->a_bg->texture[0].data.rgba.width = rgbaw;
327 p->a_bg->texture[0].data.rgba.height = rgbah;
328 p->a_bg->texture[0].data.rgba.alpha = 0xff;
329 p->hilite_rgba = g_new(RrPixel32, rgbaw * rgbah);
330 p->a_bg->texture[0].data.rgba.data = p->hilite_rgba;
332 /* position the text, but don't draw it */
333 XMoveResizeWindow(ob_display, p->text, textx, texty, textw, texth);
334 p->a_text->surface.parentx = textx;
335 p->a_text->surface.parenty = texty;
338 /* find the focused target */
339 for (i = 0, it = p->targets; it; ++i, it = g_list_next(it)) {
340 const ObFocusCyclePopupTarget *target = it->data;
341 const gint row = i / icons_per_row; /* starting from 0 */
342 const gint col = i % icons_per_row; /* starting from 0 */
344 if (target->client == c) {
345 /* save the target */
347 newtargetx = icons_center_x + l + (col * ICON_SIZE);
348 newtargety = t + (row * ICON_SIZE);
351 break; /* if we're not dimensioning, then we're done */
355 g_assert(newtarget != NULL);
357 /* create the hilite under the target icon */
362 color = ((ob_rr_theme->osd_color->r & 0xff) << RrDefaultRedOffset) +
363 ((ob_rr_theme->osd_color->g & 0xff) << RrDefaultGreenOffset) +
364 ((ob_rr_theme->osd_color->b & 0xff) << RrDefaultBlueOffset);
367 for (i = 0; i < rgbah; ++i)
368 for (j = 0; j < rgbaw; ++j) {
370 const gint x = j + rgbax - newtargetx;
371 const gint y = i + rgbay - newtargety;
373 if (x < 0 || x >= ICON_SIZE ||
374 y < 0 || y >= ICON_SIZE)
376 /* outside the target */
379 else if (x < ICON_HILITE_WIDTH ||
380 x >= ICON_SIZE - ICON_HILITE_WIDTH ||
381 y < ICON_HILITE_WIDTH ||
382 y >= ICON_SIZE - ICON_HILITE_WIDTH)
384 /* the border of the target */
388 /* the background of the target */
392 p->hilite_rgba[o++] =
393 color + (a << RrDefaultAlphaOffset);
397 /* * * draw everything * * */
399 /* draw the background */
400 RrPaint(p->a_bg, p->bg, w, h);
403 for (i = 0, it = p->targets; it; ++i, it = g_list_next(it)) {
404 const ObFocusCyclePopupTarget *target = it->data;
406 /* have to redraw the targetted icon and last targetted icon,
407 they can pick up the hilite changes in the backgroud */
408 if (!p->mapped || newtarget == target || p->last_target == target) {
409 const gint row = i / icons_per_row; /* starting from 0 */
410 const gint col = i % icons_per_row; /* starting from 0 */
413 /* find the dimensions of the icon inside it */
414 innerx = icons_center_x + l + (col * ICON_SIZE);
415 innerx += ICON_HILITE_WIDTH + ICON_HILITE_MARGIN;
416 innery = t + (row * ICON_SIZE);
417 innery += ICON_HILITE_WIDTH + ICON_HILITE_MARGIN;
420 XMoveResizeWindow(ob_display, target->win,
421 innerx, innery, innerw, innerh);
423 /* get the icon from the client */
424 p->a_icon->texture[0].data.image.alpha =
425 target->client->iconic ? OB_ICONIC_ALPHA : 0xff;
426 p->a_icon->texture[0].data.image.image = target->icon;
429 p->a_icon->surface.parentx = innerx;
430 p->a_icon->surface.parenty = innery;
431 RrPaint(p->a_icon, target->win, innerw, innerh);
436 p->a_text->texture[0].data.text.string = newtarget->text;
437 p->a_text->surface.parentx = textx;
438 p->a_text->surface.parenty = texty;
439 RrPaint(p->a_text, p->text, textw, texth);
441 p->last_target = newtarget;
446 void focus_cycle_popup_show(ObClient *c, gboolean iconic_windows,
447 gboolean all_desktops, gboolean dock_windows,
448 gboolean desktop_windows)
452 /* do this stuff only when the dialog is first showing */
454 popup_setup(&popup, TRUE, iconic_windows, all_desktops,
455 dock_windows, desktop_windows);
456 g_assert(popup.targets != NULL);
458 popup_render(&popup, c);
461 /* show the dialog */
462 XMapWindow(ob_display, popup.bg);
465 screen_hide_desktop_popup();
469 void focus_cycle_popup_hide(void)
473 ignore_start = event_start_ignore_all_enters();
475 XUnmapWindow(ob_display, popup.bg);
478 event_end_ignore_all_enters(ignore_start);
480 popup.mapped = FALSE;
482 while(popup.targets) {
483 ObFocusCyclePopupTarget *t = popup.targets->data;
485 RrImageUnref(t->icon);
487 XDestroyWindow(ob_display, t->win);
490 popup.targets = g_list_delete_link(popup.targets, popup.targets);
493 popup.last_target = NULL;
495 g_free(popup.hilite_rgba);
496 popup.hilite_rgba = NULL;
499 void focus_cycle_popup_single_show(struct _ObClient *c,
500 gboolean iconic_windows,
501 gboolean all_desktops,
502 gboolean dock_windows,
503 gboolean desktop_windows)
509 /* do this stuff only when the dialog is first showing */
513 popup_setup(&popup, FALSE, iconic_windows, all_desktops,
514 dock_windows, desktop_windows);
515 g_assert(popup.targets == NULL);
517 /* position the popup */
518 a = screen_physical_area_primary();
519 icon_popup_position(single_popup, CenterGravity,
520 a->x + a->width / 2, a->y + a->height / 2);
521 icon_popup_height(single_popup, POPUP_HEIGHT);
522 icon_popup_min_width(single_popup, POPUP_WIDTH);
523 icon_popup_max_width(single_popup, MAX(a->width/3, POPUP_WIDTH));
524 icon_popup_text_width(single_popup, popup.maxtextw);
528 text = popup_get_name(c);
529 icon_popup_show(single_popup, text, client_icon(c));
531 screen_hide_desktop_popup();
534 void focus_cycle_popup_single_hide(void)
536 icon_popup_hide(single_popup);