]> icculus.org git repositories - dana/openbox.git/blob - openbox/focus_cycle_popup.c
better coloring for the cycling hilite
[dana/openbox.git] / openbox / focus_cycle_popup.c
1 /* -*- indent-tabs-mode: nil; tab-width: 4; c-basic-offset: 4; -*-
2
3    focus_cycle_popup.c for the Openbox window manager
4    Copyright (c) 2006        Mikael Magnusson
5    Copyright (c) 2003-2007   Dana Jansens
6
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.
11
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.
16
17    See the COPYING file for a copy of the GNU General Public License.
18 */
19
20 #include "focus_cycle_popup.h"
21 #include "popup.h"
22 #include "client.h"
23 #include "screen.h"
24 #include "focus.h"
25 #include "focus_cycle.h"
26 #include "openbox.h"
27 #include "window.h"
28 #include "render/render.h"
29
30 #include <X11/Xlib.h>
31 #include <glib.h>
32
33 #define ICON_SIZE 48
34 #define ICON_HILITE_WIDTH 2
35
36 typedef struct _ObFocusCyclePopup       ObFocusCyclePopup;
37 typedef struct _ObFocusCyclePopupTarget ObFocusCyclePopupTarget;
38
39 struct _ObFocusCyclePopupTarget
40 {
41     ObClient *client;
42     gchar *text;
43     Window win;
44 };
45
46 struct _ObFocusCyclePopup
47 {
48     ObWindow obwin;
49     Window bg;
50
51     Window text;
52
53     GList *targets;
54     gint n_targets;
55
56     ObFocusCyclePopupTarget *last_target;
57
58     gint maxtextw;
59
60     RrAppearance *a_bg;
61     RrAppearance *a_text;
62     RrAppearance *a_icon;
63
64     RrPixel32 *hilite_rgba;
65
66     gboolean mapped;
67 };
68
69 static ObFocusCyclePopup popup;
70
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);
75
76 static Window create_window(Window parent, guint bwidth, gulong mask,
77                             XSetWindowAttributes *attr)
78 {
79     return XCreateWindow(ob_display, parent, 0, 0, 1, 1, bwidth,
80                          RrDepth(ob_rr_inst), InputOutput,
81                          RrVisual(ob_rr_inst), mask, attr);
82 }
83
84 void focus_cycle_popup_startup(gboolean reconfig)
85 {
86     XSetWindowAttributes attrib;
87
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);
92
93     popup.a_text->surface.parent = popup.a_bg;
94     popup.a_icon->surface.parent = popup.a_bg;
95
96     popup.a_icon->texture[0].type = RR_TEXTURE_RGBA;
97
98     RrAppearanceAddTextures(popup.a_bg, 1);
99     popup.a_bg->texture[0].type = RR_TEXTURE_RGBA;
100
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);
106
107     popup.text = create_window(popup.bg, 0, 0, NULL);
108
109     popup.targets = NULL;
110     popup.n_targets = 0;
111     popup.last_target = NULL;
112
113     popup.hilite_rgba = NULL;
114
115     XMapWindow(ob_display, popup.text);
116
117     stacking_add(INTERNAL_AS_WINDOW(&popup));
118 }
119
120 void focus_cycle_popup_shutdown(gboolean reconfig)
121 {
122     stacking_remove(INTERNAL_AS_WINDOW(&popup));
123
124     while(popup.targets) {
125         ObFocusCyclePopupTarget *t = popup.targets->data;
126
127         g_free(t->text);
128         XDestroyWindow(ob_display, t->win);
129
130         popup.targets = g_list_delete_link(popup.targets, popup.targets);
131     }
132
133     g_free(popup.hilite_rgba);
134
135     XDestroyWindow(ob_display, popup.text);
136     XDestroyWindow(ob_display, popup.bg);
137
138     RrAppearanceFree(popup.a_icon);
139     RrAppearanceFree(popup.a_text);
140     RrAppearanceFree(popup.a_bg);
141 }
142
143 static void popup_setup(ObFocusCyclePopup *p,gboolean all_desktops,
144                         gboolean dock_windows, gboolean desktop_windows)
145 {
146     gint maxwidth, n;
147     GList *it;
148
149     g_assert(p->targets == NULL);
150     g_assert(p->n_targets == 0);
151
152     /* make its width to be the width of all the possible titles */
153
154     /* build a list of all the valid focus targets and measure their strings,
155        and count them */
156     maxwidth = 0;
157     n = 0;
158     for (it = g_list_last(focus_order); it; it = g_list_previous(it)) {
159         ObClient *ft = it->data;
160
161         if (focus_cycle_target_valid(ft,
162                                      all_desktops,
163                                      dock_windows,
164                                      desktop_windows))
165         {
166             ObFocusCyclePopupTarget *t = g_new(ObFocusCyclePopupTarget, 1);
167
168             t->client = ft;
169             t->text = popup_get_name(ft);
170             t->win = create_window(p->bg, 0, 0, NULL);
171
172             XMapWindow(ob_display, t->win);
173
174             /* measure */
175             p->a_text->texture[0].data.text.string = t->text;
176             maxwidth = MAX(maxwidth, RrMinWidth(p->a_text));
177
178             p->targets = g_list_prepend(p->targets, t);
179             ++n;
180         }
181     }
182
183     p->n_targets = n;
184     p->maxtextw = maxwidth;
185 }
186
187 static gchar *popup_get_name(ObClient *c)
188 {
189     ObClient *p;
190     gchar *title = NULL;
191     const gchar *desk = NULL;
192     gchar *ret;
193
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);
197
198     if (c->desktop != DESKTOP_ALL && c->desktop != screen_desktop)
199         desk = screen_desktop_names[c->desktop];
200
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);
204
205     if (title == NULL)
206         title = g_strdup(c->iconic ? c->icon_title : c->title);
207
208     if (desk)
209         ret = g_strdup_printf("%s [%s]", title, desk);
210     else {
211         ret = title;
212         title = NULL;
213     }
214     g_free(title);
215
216     return ret;
217 }
218
219 static void popup_render(ObFocusCyclePopup *p, const ObClient *c)
220 {
221     gint l, t, r, b;
222     gint x, y, w, h;
223     Rect *screen_area;
224     gint icons_per_row;
225     gint icon_rows;
226     gint textx, texty, textw, texth;
227     gint iconw, iconh;
228     gint i;
229     GList *it;
230     const ObFocusCyclePopupTarget *newtarget;
231     gint newtargetx, newtargety;
232
233     /* XXX find the middle monitor? */
234     screen_area = screen_physical_area_monitor(0);
235
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;
242
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;
246
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 */
251
252     /* how many icons will fit in that row? make the width fit that */
253     w -= l + r;
254     icons_per_row = w / ICON_SIZE;
255     w = icons_per_row * ICON_SIZE + l + r;
256
257     /* how many rows do we need? */
258     icon_rows = (p->n_targets-1) / icons_per_row + 1;
259
260     /* get the text dimensions */
261     textw = w - l - r;
262     texth = RrMinHeight(p->a_text);
263
264     /* find the height of the dialog */
265     h = t + b + (icon_rows * ICON_SIZE) + (ob_rr_theme->paddingy + texth);
266
267     /* get the position of the text */
268     textx = l;
269     texty = h - texth;
270
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;
276
277     if (!p->mapped) {
278         /* position the background but don't draw it*/
279         XMoveResizeWindow(ob_display, p->bg, x, y, w, h);
280
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;
286
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;
291     }
292
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 */
298
299         if (target->client == c) {
300             /* save the target */
301             newtarget = target;
302             newtargetx = l + (col * ICON_SIZE);
303             newtargety = t + (row * ICON_SIZE);
304
305             if (!p->mapped)
306                 break; /* if we're not dimensioning, then we're done */
307         }
308     }
309
310     g_assert(newtarget != NULL);
311
312     /* create the hilite under the target icon */
313     {
314         RrPixel32 color;
315         gint i, j;
316
317         color = ((ob_rr_theme->osd_color->r & 0xff) << RrDefaultRedOffset) +
318             ((ob_rr_theme->osd_color->g & 0xff) << RrDefaultGreenOffset) +
319             ((ob_rr_theme->osd_color->b & 0xff) << RrDefaultBlueOffset);
320
321         for (i = 0; i < h; ++i)
322             for (j = 0; j < w; ++j) {
323                 guchar a;
324                 const gint x = j - newtargetx;
325                 const gint y = i - newtargety;
326
327                 if (x < 0 || x >= ICON_SIZE ||
328                     y < 0 || y >= ICON_SIZE)
329                 {
330                     /* outside the target */
331                     a = 0x00;
332                 }
333                 else if (x < ICON_HILITE_WIDTH ||
334                          x >= ICON_SIZE-ICON_HILITE_WIDTH ||
335                          y < ICON_HILITE_WIDTH ||
336                          y >= ICON_SIZE-ICON_HILITE_WIDTH)
337                 {
338                     /* the border of the target */
339                     a = 0x88;
340                 }
341                 else {
342                     /* the background of the target */
343                     a = 0x22;
344                 }
345
346                 p->hilite_rgba[i * w + j] =
347                     color + (a << RrDefaultAlphaOffset);
348             }
349     }
350
351     /* * * draw everything * * */
352
353     /* draw the background */
354     RrPaint(p->a_bg, p->bg, w, h);
355
356     /* draw the icons */
357     for (i = 0, it = p->targets; it; ++i, it = g_list_next(it)) {
358         const ObFocusCyclePopupTarget *target = it->data;
359
360         /* have to redraw the targetted icon and last targetted icon,
361            they can pick up the hilite changes in the backgroud */
362         if (!p->mapped || newtarget == target || p->last_target == target) {
363             const ObClientIcon *icon;
364             const gint row = i / icons_per_row; /* starting from 0 */
365             const gint col = i % icons_per_row; /* starting from 0 */
366             gint iconx, icony;
367
368             /* find the dimensions of the icon inside it */
369             iconx = l + (col * ICON_SIZE) + ICON_HILITE_WIDTH +
370                 ob_rr_theme->paddingx;
371             icony = t + (row * ICON_SIZE) + ICON_HILITE_WIDTH +
372                 ob_rr_theme->paddingy;
373
374             /* move the icon */
375             XMoveResizeWindow(ob_display, target->win,
376                               iconx, icony, iconw, iconh);
377
378             /* get the icon from the client */
379             icon = client_icon(target->client, iconw, iconh);
380             p->a_icon->texture[0].data.rgba.width = icon->width;
381             p->a_icon->texture[0].data.rgba.height = icon->height;
382             p->a_icon->texture[0].data.rgba.data = icon->data;
383
384             /* draw the icon */
385             p->a_icon->surface.parentx = iconx;
386             p->a_icon->surface.parenty = icony;
387             RrPaint(p->a_icon, target->win, iconw, iconh);
388         }
389     }
390
391     /* draw the text */
392     p->a_text->texture[0].data.text.string = newtarget->text;
393     p->a_text->surface.parentx = textx;
394     p->a_text->surface.parenty = texty;
395     RrPaint(p->a_text, p->text, textw, texth);
396
397     p->last_target = newtarget;
398 }
399
400 void focus_cycle_popup_show(ObClient *c,
401                             gboolean all_desktops, gboolean dock_windows,
402                             gboolean desktop_windows)
403 {
404     g_assert(c != NULL);
405
406     /* do this stuff only when the dialog is first showing */
407     if (!popup.mapped)
408         popup_setup(&popup, all_desktops, dock_windows, desktop_windows);
409     g_assert(popup.targets != NULL);
410
411     popup_render(&popup, c);
412
413     if (!popup.mapped) {
414         /* show the dialog */
415         XMapWindow(ob_display, popup.bg);
416         XFlush(ob_display);
417         popup.mapped = TRUE;
418     }
419 }
420
421 void focus_cycle_popup_hide()
422 {
423     XUnmapWindow(ob_display, popup.bg);
424     XFlush(ob_display);
425
426     popup.mapped = FALSE;
427
428     while(popup.targets) {
429         ObFocusCyclePopupTarget *t = popup.targets->data;
430
431         g_free(t->text);
432         XDestroyWindow(ob_display, t->win);
433
434         popup.targets = g_list_delete_link(popup.targets, popup.targets);
435     }
436     popup.n_targets = 0;
437     popup.last_target = NULL;
438
439     g_free(popup.hilite_rgba);
440     popup.hilite_rgba = NULL;
441 }
442