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
58 const ObFocusCyclePopupTarget *last_target;
66 RrPixel32 *hilite_rgba;
71 /*! This popup shows all possible windows */
72 static ObFocusCyclePopup popup;
73 /*! This popup shows a single window */
74 static ObIconPopup *single_popup;
76 static gchar *popup_get_name (ObClient *c);
77 static void popup_setup (ObFocusCyclePopup *p,
78 gboolean create_targets,
79 gboolean iconic_windows,
80 gboolean all_desktops,
81 gboolean dock_windows,
82 gboolean desktop_windows);
83 static void popup_render (ObFocusCyclePopup *p,
86 static Window create_window(Window parent, guint bwidth, gulong mask,
87 XSetWindowAttributes *attr)
89 return XCreateWindow(obt_display, parent, 0, 0, 1, 1, bwidth,
90 RrDepth(ob_rr_inst), InputOutput,
91 RrVisual(ob_rr_inst), mask, attr);
94 void focus_cycle_popup_startup(gboolean reconfig)
96 XSetWindowAttributes attrib;
98 single_popup = icon_popup_new();
100 popup.obwin.type = OB_WINDOW_CLASS_INTERNAL;
101 popup.a_bg = RrAppearanceCopy(ob_rr_theme->osd_hilite_bg);
102 popup.a_text = RrAppearanceCopy(ob_rr_theme->osd_hilite_label);
103 popup.a_icon = RrAppearanceCopy(ob_rr_theme->a_clear);
105 popup.a_text->surface.parent = popup.a_bg;
106 popup.a_icon->surface.parent = popup.a_bg;
108 RrAppearanceAddTextures(popup.a_icon, 2);
110 popup.a_icon->texture[0].type = RR_TEXTURE_RGBA;
112 attrib.override_redirect = True;
113 attrib.border_pixel=RrColorPixel(ob_rr_theme->osd_border_color);
114 popup.bg = create_window(obt_root(ob_screen), ob_rr_theme->obwidth,
115 CWOverrideRedirect | CWBorderPixel, &attrib);
117 popup.targets = NULL;
119 popup.last_target = NULL;
121 /* set up the hilite texture for the icon */
122 popup.a_icon->texture[1].data.rgba.width = ICON_SIZE;
123 popup.a_icon->texture[1].data.rgba.height = ICON_SIZE;
124 popup.a_icon->texture[1].data.rgba.alpha = 0xff;
125 popup.hilite_rgba = g_new(RrPixel32, ICON_SIZE * ICON_SIZE);
126 popup.a_icon->texture[1].data.rgba.data = popup.hilite_rgba;
128 /* create the hilite under the target icon */
133 color = ((ob_rr_theme->osd_color->r & 0xff) << RrDefaultRedOffset) +
134 ((ob_rr_theme->osd_color->g & 0xff) << RrDefaultGreenOffset) +
135 ((ob_rr_theme->osd_color->b & 0xff) << RrDefaultBlueOffset);
138 for (x = 0; x < ICON_SIZE; x++)
139 for (y = 0; y < ICON_SIZE; y++) {
142 if (x < ICON_HILITE_WIDTH ||
143 x >= ICON_SIZE - ICON_HILITE_WIDTH ||
144 y < ICON_HILITE_WIDTH ||
145 y >= ICON_SIZE - ICON_HILITE_WIDTH)
147 /* the border of the target */
151 /* the background of the target */
155 popup.hilite_rgba[o++] =
156 color + (a << RrDefaultAlphaOffset);
161 stacking_add(INTERNAL_AS_WINDOW(&popup));
162 window_add(&popup.bg, INTERNAL_AS_WINDOW(&popup));
165 void focus_cycle_popup_shutdown(gboolean reconfig)
167 icon_popup_free(single_popup);
169 window_remove(popup.bg);
170 stacking_remove(INTERNAL_AS_WINDOW(&popup));
172 while(popup.targets) {
173 ObFocusCyclePopupTarget *t = popup.targets->data;
176 XDestroyWindow(obt_display, t->iconwin);
177 XDestroyWindow(obt_display, t->textwin);
179 popup.targets = g_list_delete_link(popup.targets, popup.targets);
182 g_free(popup.hilite_rgba);
183 popup.hilite_rgba = NULL;
185 XDestroyWindow(obt_display, popup.bg);
187 RrAppearanceFree(popup.a_icon);
188 RrAppearanceFree(popup.a_text);
189 RrAppearanceFree(popup.a_bg);
192 static void popup_setup(ObFocusCyclePopup *p, gboolean create_targets,
193 gboolean iconic_windows, gboolean all_desktops,
194 gboolean dock_windows, gboolean desktop_windows)
199 g_assert(p->targets == NULL);
200 g_assert(p->n_targets == 0);
202 /* make its width to be the width of all the possible titles */
204 /* build a list of all the valid focus targets and measure their strings,
208 for (it = g_list_last(focus_order); it; it = g_list_previous(it)) {
209 ObClient *ft = it->data;
211 if (focus_valid_target(ft, TRUE,
217 gchar *text = popup_get_name(ft);
220 p->a_text->texture[0].data.text.string = text;
221 maxwidth = MAX(maxwidth, RrMinWidth(p->a_text));
226 ObFocusCyclePopupTarget *t = g_new(ObFocusCyclePopupTarget, 1);
230 t->iconwin = create_window(p->bg, 0, 0, NULL);
231 t->textwin = create_window(p->bg, 0, 0, NULL);
233 XMapWindow(obt_display, t->iconwin);
234 XMapWindow(obt_display, t->textwin);
236 p->targets = g_list_prepend(p->targets, t);
243 p->maxtextw = maxwidth;
246 static gchar *popup_get_name(ObClient *c)
250 const gchar *desk = NULL;
253 /* find our highest direct parent */
254 p = client_search_top_direct_parent(c);
256 if (c->desktop != DESKTOP_ALL && c->desktop != screen_desktop)
257 desk = screen_desktop_names[c->desktop];
259 title = c->iconic ? c->icon_title : c->title;
261 /* use the transient's parent's title/icon if we don't have one */
262 if (p != c && title[0] == '\0')
263 title = p->iconic ? p->icon_title : p->title;
266 ret = g_strdup_printf("%s [%s]", title, desk);
268 ret = g_strdup(title);
273 static void popup_render(ObFocusCyclePopup *p, const ObClient *c)
278 Rect *screen_area = NULL;
279 gint rgbax, rgbay, rgbaw, rgbah;
283 const ObFocusCyclePopupTarget *newtarget;
284 gint newtargetx, newtargety;
286 screen_area = screen_physical_area_active();
288 /* get the outside margins */
289 RrMargins(p->a_bg, &ml, &mt, &mr, &mb);
291 /* get our outside borders */
292 l = ml + OUTSIDE_BORDER;
293 r = mr + OUTSIDE_BORDER;
294 t = mt + OUTSIDE_BORDER;
295 b = mb + OUTSIDE_BORDER;
297 /* get the icon pictures' sizes */
298 innerw = ICON_SIZE - (ICON_HILITE_WIDTH + ICON_HILITE_MARGIN) * 2;
299 innerh = ICON_SIZE - (ICON_HILITE_WIDTH + ICON_HILITE_MARGIN) * 2;
301 /* get the width from the text and keep it within limits */
302 w = l + r + p->maxtextw;
303 w = MIN(w, MAX(screen_area->width/3, POPUP_WIDTH)); /* max width */
304 w = MAX(w, POPUP_WIDTH); /* min width */
306 /* find the height of the dialog */
307 #warning limit the height and scroll entries somehow
308 h = t + b + (p->n_targets * ICON_SIZE) + OUTSIDE_BORDER;
310 /* find the position for the popup (include the outer borders) */
311 x = screen_area->x + (screen_area->width -
312 (w + ob_rr_theme->obwidth * 2)) / 2;
313 y = screen_area->y + (screen_area->height -
314 (h + ob_rr_theme->obwidth * 2)) / 2;
316 /* get the dimensions of the target hilite texture */
323 /* position the background but don't draw it*/
324 XMoveResizeWindow(obt_display, p->bg, x, y, w, h);
327 /* find the focused target */
328 for (i = 0, it = p->targets; it; ++i, it = g_list_next(it)) {
329 const ObFocusCyclePopupTarget *target = it->data;
331 if (target->client == c) {
332 /* save the target */
335 newtargety = t + i * ICON_SIZE;
338 break; /* if we're not dimensioning, then we're done */
342 g_assert(newtarget != NULL);
344 /* * * draw everything * * */
346 /* draw the background */
348 RrPaint(p->a_bg, p->bg, w, h);
350 /* draw the icons and text */
351 for (i = 0, it = p->targets; it; ++i, it = g_list_next(it)) {
352 const ObFocusCyclePopupTarget *target = it->data;
354 /* have to redraw the targetted icon and last targetted icon,
355 they can pick up the hilite changes in the backgroud */
356 if (!p->mapped || newtarget == target || p->last_target == target) {
357 const ObClientIcon *icon;
360 /* find the dimensions of the icon inside it */
362 innerx += ICON_HILITE_WIDTH + ICON_HILITE_MARGIN;
363 innery = t + i * ICON_SIZE;
364 innery += ICON_HILITE_WIDTH + ICON_HILITE_MARGIN;
367 XMoveResizeWindow(obt_display, target->iconwin,
368 innerx, innery, innerw, innerh);
371 XMoveResizeWindow(obt_display, target->textwin,
372 innerx + ICON_SIZE, innery,
373 w - innerx - ICON_SIZE - OUTSIDE_BORDER, innerh);
375 /* get the icon from the client */
376 icon = client_icon(target->client, innerw, innerh);
377 p->a_icon->texture[0].data.rgba.width = icon->width;
378 p->a_icon->texture[0].data.rgba.height = icon->height;
379 p->a_icon->texture[0].data.rgba.alpha =
380 target->client->iconic ? OB_ICONIC_ALPHA : 0xff;
381 p->a_icon->texture[0].data.rgba.data = icon->data;
383 /* Draw the hilite? */
384 #warning do i have to add more obrender interface thingers to get it to draw the icon inside the hilight? sigh
385 p->a_icon->texture[1].type = (target == newtarget) ?
386 RR_TEXTURE_RGBA : RR_TEXTURE_NONE;
389 p->a_icon->surface.parentx = innerx;
390 p->a_icon->surface.parenty = innery;
391 RrPaint(p->a_icon, target->iconwin, innerw, innerh);
394 p->a_text->texture[0].data.text.string = target->text;
395 p->a_text->surface.parentx = innerx + ICON_SIZE;
396 p->a_text->surface.parenty = innery;
397 RrPaint(p->a_text, target->textwin, w - innerx - ICON_SIZE - OUTSIDE_BORDER, innerh);
401 p->last_target = newtarget;
406 void focus_cycle_popup_show(ObClient *c, gboolean iconic_windows,
407 gboolean all_desktops, gboolean dock_windows,
408 gboolean desktop_windows)
412 /* do this stuff only when the dialog is first showing */
414 popup_setup(&popup, TRUE, iconic_windows, all_desktops,
415 dock_windows, desktop_windows);
416 g_assert(popup.targets != NULL);
418 popup_render(&popup, c);
421 /* show the dialog */
422 XMapWindow(obt_display, popup.bg);
425 screen_hide_desktop_popup();
429 void focus_cycle_popup_hide(void)
433 ignore_start = event_start_ignore_all_enters();
435 XUnmapWindow(obt_display, popup.bg);
438 event_end_ignore_all_enters(ignore_start);
440 popup.mapped = FALSE;
442 while(popup.targets) {
443 ObFocusCyclePopupTarget *t = popup.targets->data;
446 XDestroyWindow(obt_display, t->iconwin);
447 XDestroyWindow(obt_display, t->textwin);
450 popup.targets = g_list_delete_link(popup.targets, popup.targets);
453 popup.last_target = NULL;
456 void focus_cycle_popup_single_show(struct _ObClient *c,
457 gboolean iconic_windows,
458 gboolean all_desktops,
459 gboolean dock_windows,
460 gboolean desktop_windows)
466 /* do this stuff only when the dialog is first showing */
470 popup_setup(&popup, FALSE, iconic_windows, all_desktops,
471 dock_windows, desktop_windows);
472 g_assert(popup.targets == NULL);
474 /* position the popup */
475 a = screen_physical_area_active();
476 icon_popup_position(single_popup, CenterGravity,
477 a->x + a->width / 2, a->y + a->height / 2);
478 icon_popup_height(single_popup, POPUP_HEIGHT);
479 icon_popup_min_width(single_popup, POPUP_WIDTH);
480 icon_popup_max_width(single_popup, MAX(a->width/3, POPUP_WIDTH));
481 icon_popup_text_width(single_popup, popup.maxtextw);
485 text = popup_get_name(c);
486 icon_popup_show(single_popup, text, client_icon(c, ICON_SIZE, ICON_SIZE));
488 screen_hide_desktop_popup();
491 void focus_cycle_popup_single_hide(void)
493 icon_popup_hide(single_popup);