]> icculus.org git repositories - mikachu/openbox.git/blob - openbox/focus_cycle_popup.c
new focus cycle popup of doom
[mikachu/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 = 0;
315         gint i, j;
316
317         memset(p->hilite_rgba, color, w * h * sizeof(RrPixel32));
318
319         for (i = 0; i < ICON_SIZE; ++i)
320             for (j = 0; j < ICON_SIZE; ++j) {
321                 guchar a;
322
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)
327                 {
328                     /* the border of the target */
329                     a = 0x88;
330                 }
331                 else {
332                     /* the background of the target */
333                     a = 0x33;
334                 }
335
336                 p->hilite_rgba[(i+newtargety) * w + (j+newtargetx)] =
337                     color + (a << RrDefaultAlphaOffset);
338             }
339     }
340
341     /* * * draw everything * * */
342
343     /* draw the background */
344     RrPaint(p->a_bg, p->bg, w, h);
345
346     /* draw the icons */
347     for (i = 0, it = p->targets; it; ++i, it = g_list_next(it)) {
348         const ObFocusCyclePopupTarget *target = it->data;
349
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 */
356             gint iconx, icony;
357
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;
363
364             /* move the icon */
365             XMoveResizeWindow(ob_display, target->win,
366                               iconx, icony, iconw, iconh);
367
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;
373
374             /* draw the icon */
375             p->a_icon->surface.parentx = iconx;
376             p->a_icon->surface.parenty = icony;
377             RrPaint(p->a_icon, target->win, iconw, iconh);
378         }
379     }
380
381     /* draw the text */
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);
386
387     p->last_target = newtarget;
388 }
389
390 void focus_cycle_popup_show(ObClient *c,
391                             gboolean all_desktops, gboolean dock_windows,
392                             gboolean desktop_windows)
393 {
394     g_assert(c != NULL);
395
396     /* do this stuff only when the dialog is first showing */
397     if (!popup.mapped)
398         popup_setup(&popup, all_desktops, dock_windows, desktop_windows);
399     g_assert(popup.targets != NULL);
400
401     popup_render(&popup, c);
402
403     if (!popup.mapped) {
404         /* show the dialog */
405         XMapWindow(ob_display, popup.bg);
406         XFlush(ob_display);
407         popup.mapped = TRUE;
408     }
409 }
410
411 void focus_cycle_popup_hide()
412 {
413     XUnmapWindow(ob_display, popup.bg);
414     XFlush(ob_display);
415
416     popup.mapped = FALSE;
417
418     while(popup.targets) {
419         ObFocusCyclePopupTarget *t = popup.targets->data;
420
421         g_free(t->text);
422         XDestroyWindow(ob_display, t->win);
423
424         popup.targets = g_list_delete_link(popup.targets, popup.targets);
425     }
426     popup.n_targets = 0;
427     popup.last_target = NULL;
428
429     g_free(popup.hilite_rgba);
430     popup.hilite_rgba = NULL;
431 }
432