]> icculus.org git repositories - dana/openbox.git/blob - openbox/focus_cycle_popup.c
fix positioning of stuff in the cycling popup. oh my god. the image texture is drawn...
[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 #define ICON_HILITE_MARGIN 1
36 #define OUTSIDE_BORDER 3
37
38 typedef struct _ObFocusCyclePopup       ObFocusCyclePopup;
39 typedef struct _ObFocusCyclePopupTarget ObFocusCyclePopupTarget;
40
41 struct _ObFocusCyclePopupTarget
42 {
43     ObClient *client;
44     gchar *text;
45     Window win;
46 };
47
48 struct _ObFocusCyclePopup
49 {
50     ObWindow obwin;
51     Window bg;
52
53     Window text;
54
55     GList *targets;
56     gint n_targets;
57
58     const ObFocusCyclePopupTarget *last_target;
59
60     gint maxtextw;
61
62     RrAppearance *a_bg;
63     RrAppearance *a_text;
64     RrAppearance *a_icon;
65
66     RrPixel32 *hilite_rgba;
67
68     gboolean mapped;
69 };
70
71 static ObFocusCyclePopup popup;
72
73 static gchar *popup_get_name (ObClient *c);
74 static void   popup_setup    (ObFocusCyclePopup *p,gboolean all_desktops,
75                               gboolean dock_windows, gboolean desktop_windows);
76 static void   popup_render   (ObFocusCyclePopup *p, const ObClient *c);
77
78 static Window create_window(Window parent, guint bwidth, gulong mask,
79                             XSetWindowAttributes *attr)
80 {
81     return XCreateWindow(ob_display, parent, 0, 0, 1, 1, bwidth,
82                          RrDepth(ob_rr_inst), InputOutput,
83                          RrVisual(ob_rr_inst), mask, attr);
84 }
85
86 void focus_cycle_popup_startup(gboolean reconfig)
87 {
88     XSetWindowAttributes attrib;
89
90     popup.obwin.type = Window_Internal;
91     popup.a_bg = RrAppearanceCopy(ob_rr_theme->osd_hilite_bg);
92     popup.a_text = RrAppearanceCopy(ob_rr_theme->osd_hilite_label);
93     popup.a_icon = RrAppearanceCopy(ob_rr_theme->a_clear_tex);
94
95     popup.a_text->surface.parent = popup.a_bg;
96     popup.a_icon->surface.parent = popup.a_bg;
97
98     popup.a_icon->texture[0].type = RR_TEXTURE_RGBA;
99
100     RrAppearanceAddTextures(popup.a_bg, 1);
101     popup.a_bg->texture[0].type = RR_TEXTURE_RGBA;
102
103     attrib.override_redirect = True;
104     attrib.border_pixel=RrColorPixel(ob_rr_theme->frame_focused_border_color);
105     popup.bg = create_window(RootWindow(ob_display, ob_screen),
106                              ob_rr_theme->fbwidth,
107                              CWOverrideRedirect | CWBorderPixel, &attrib);
108
109     popup.text = create_window(popup.bg, 0, 0, NULL);
110
111     popup.targets = NULL;
112     popup.n_targets = 0;
113     popup.last_target = NULL;
114
115     popup.hilite_rgba = NULL;
116
117     XMapWindow(ob_display, popup.text);
118
119     stacking_add(INTERNAL_AS_WINDOW(&popup));
120 }
121
122 void focus_cycle_popup_shutdown(gboolean reconfig)
123 {
124     stacking_remove(INTERNAL_AS_WINDOW(&popup));
125
126     while(popup.targets) {
127         ObFocusCyclePopupTarget *t = popup.targets->data;
128
129         g_free(t->text);
130         XDestroyWindow(ob_display, t->win);
131
132         popup.targets = g_list_delete_link(popup.targets, popup.targets);
133     }
134
135     g_free(popup.hilite_rgba);
136
137     XDestroyWindow(ob_display, popup.text);
138     XDestroyWindow(ob_display, popup.bg);
139
140     RrAppearanceFree(popup.a_icon);
141     RrAppearanceFree(popup.a_text);
142     RrAppearanceFree(popup.a_bg);
143 }
144
145 static void popup_setup(ObFocusCyclePopup *p,gboolean all_desktops,
146                         gboolean dock_windows, gboolean desktop_windows)
147 {
148     gint maxwidth, n;
149     GList *it;
150
151     g_assert(p->targets == NULL);
152     g_assert(p->n_targets == 0);
153
154     /* make its width to be the width of all the possible titles */
155
156     /* build a list of all the valid focus targets and measure their strings,
157        and count them */
158     maxwidth = 0;
159     n = 0;
160     for (it = g_list_last(focus_order); it; it = g_list_previous(it)) {
161         ObClient *ft = it->data;
162
163         if (focus_cycle_target_valid(ft,
164                                      all_desktops,
165                                      dock_windows,
166                                      desktop_windows))
167         {
168             ObFocusCyclePopupTarget *t = g_new(ObFocusCyclePopupTarget, 1);
169
170             t->client = ft;
171             t->text = popup_get_name(ft);
172             t->win = create_window(p->bg, 0, 0, NULL);
173
174             XMapWindow(ob_display, t->win);
175
176             /* measure */
177             p->a_text->texture[0].data.text.string = t->text;
178             maxwidth = MAX(maxwidth, RrMinWidth(p->a_text));
179
180             p->targets = g_list_prepend(p->targets, t);
181             ++n;
182         }
183     }
184
185     p->n_targets = n;
186     p->maxtextw = maxwidth;
187 }
188
189 static gchar *popup_get_name(ObClient *c)
190 {
191     ObClient *p;
192     gchar *title = NULL;
193     const gchar *desk = NULL;
194     gchar *ret;
195
196     /* find our highest direct parent, including non-normal windows */
197     for (p = c; p->transient_for && p->transient_for != OB_TRAN_GROUP;
198          p = p->transient_for);
199
200     if (c->desktop != DESKTOP_ALL && c->desktop != screen_desktop)
201         desk = screen_desktop_names[c->desktop];
202
203     /* use the transient's parent's title/icon if we don't have one */
204     if (p != c && !strcmp("", (c->iconic ? c->icon_title : c->title)))
205         title = g_strdup(p->iconic ? p->icon_title : p->title);
206
207     if (title == NULL)
208         title = g_strdup(c->iconic ? c->icon_title : c->title);
209
210     if (desk)
211         ret = g_strdup_printf("%s [%s]", title, desk);
212     else {
213         ret = title;
214         title = NULL;
215     }
216     g_free(title);
217
218     return ret;
219 }
220
221 static void popup_render(ObFocusCyclePopup *p, const ObClient *c)
222 {
223     gint ml, mt, mr, mb;
224     gint l, t, r, b;
225     gint x, y, w, h;
226     Rect *screen_area;
227     gint icons_per_row;
228     gint icon_rows;
229     gint textx, texty, textw, texth;
230     gint rgbax, rgbay, rgbaw, rgbah;
231     gint innerw, innerh;
232     gint i;
233     GList *it;
234     const ObFocusCyclePopupTarget *newtarget;
235     gint newtargetx, newtargety;
236
237     /* XXX find the middle monitor? */
238     screen_area = screen_physical_area_monitor(0);
239
240     /* get the outside margins */
241     RrMargins(p->a_bg, &ml, &mt, &mr, &mb);
242
243     /* get our outside borders */
244     l = ml + OUTSIDE_BORDER;
245     r = mr + OUTSIDE_BORDER;
246     t = mt + OUTSIDE_BORDER;
247     b = mb + OUTSIDE_BORDER;
248
249     /* get the icon pictures' sizes */
250     innerw = ICON_SIZE - (ICON_HILITE_WIDTH + ICON_HILITE_MARGIN) * 2;
251     innerh = ICON_SIZE - (ICON_HILITE_WIDTH + ICON_HILITE_MARGIN) * 2;
252
253     /* get the width from the text and keep it within limits */
254     w = l + r + p->maxtextw;
255     w = MIN(w, MAX(screen_area->width/3, POPUP_WIDTH)); /* max width */
256     w = MAX(w, POPUP_WIDTH); /* min width */
257
258     /* how many icons will fit in that row? make the width fit that */
259     w -= l + r;
260     icons_per_row = w / ICON_SIZE;
261     w = icons_per_row * ICON_SIZE + l + r;
262
263     /* how many rows do we need? */
264     icon_rows = (p->n_targets-1) / icons_per_row + 1;
265
266     /* get the text dimensions */
267     textw = w - l - r;
268     texth = RrMinHeight(p->a_text);
269
270     /* find the height of the dialog */
271     h = t + b + (icon_rows * ICON_SIZE) + (OUTSIDE_BORDER + texth);
272
273     /* get the position of the text */
274     textx = l;
275     texty = h - texth - b;
276
277     /* find the position for the popup (include the outer borders) */
278     x = screen_area->x + (screen_area->width -
279                           (w + ob_rr_theme->fbwidth * 2)) / 2;
280     y = screen_area->y + (screen_area->height -
281                           (h + ob_rr_theme->fbwidth * 2)) / 2;
282
283     /* get the dimensions of the target hilite texture */
284     rgbax = ml;
285     rgbay = mt;
286     rgbaw = w - ml - mr;
287     rgbah = h - mt - mb;
288
289     if (!p->mapped) {
290         /* position the background but don't draw it*/
291         XMoveResizeWindow(ob_display, p->bg, x, y, w, h);
292
293         /* set up the hilite texture for the background */
294         p->a_bg->texture[0].data.rgba.width = rgbaw;
295         p->a_bg->texture[0].data.rgba.height = rgbah;
296         p->hilite_rgba = g_new(RrPixel32, rgbaw * rgbah);
297         p->a_bg->texture[0].data.rgba.data = p->hilite_rgba;
298
299         /* position the text, but don't draw it */
300         XMoveResizeWindow(ob_display, p->text, textx, texty, textw, texth);
301         p->a_text->surface.parentx = textx;
302         p->a_text->surface.parenty = texty;
303     }
304
305     /* find the focused target */
306     for (i = 0, it = p->targets; it; ++i, it = g_list_next(it)) {
307         const ObFocusCyclePopupTarget *target = it->data;
308         const gint row = i / icons_per_row; /* starting from 0 */
309         const gint col = i % icons_per_row; /* starting from 0 */
310
311         if (target->client == c) {
312             /* save the target */
313             newtarget = target;
314             newtargetx = l + (col * ICON_SIZE);
315             newtargety = t + (row * ICON_SIZE);
316
317             if (!p->mapped)
318                 break; /* if we're not dimensioning, then we're done */
319         }
320     }
321
322     g_assert(newtarget != NULL);
323
324     /* create the hilite under the target icon */
325     {
326         RrPixel32 color;
327         gint i, j, o;
328
329         color = ((ob_rr_theme->osd_color->r & 0xff) << RrDefaultRedOffset) +
330             ((ob_rr_theme->osd_color->g & 0xff) << RrDefaultGreenOffset) +
331             ((ob_rr_theme->osd_color->b & 0xff) << RrDefaultBlueOffset);
332
333         o = 0;
334         for (i = 0; i < rgbah; ++i)
335             for (j = 0; j < rgbaw; ++j) {
336                 guchar a;
337                 const gint x = j + rgbax - newtargetx;
338                 const gint y = i + rgbay - newtargety;
339
340                 if (x < 0 || x >= ICON_SIZE ||
341                     y < 0 || y >= ICON_SIZE)
342                 {
343                     /* outside the target */
344                     a = 0x00;
345                 }
346                 else if (x < ICON_HILITE_WIDTH ||
347                          x >= ICON_SIZE - ICON_HILITE_WIDTH ||
348                          y < ICON_HILITE_WIDTH ||
349                          y >= ICON_SIZE - ICON_HILITE_WIDTH)
350                 {
351                     /* the border of the target */
352                     a = 0x88;
353                 }
354                 else {
355                     /* the background of the target */
356                     a = 0x22;
357                 }
358
359                 p->hilite_rgba[o++] =
360                     color + (a << RrDefaultAlphaOffset);
361             }
362     }
363
364     /* * * draw everything * * */
365
366     /* draw the background */
367     RrPaint(p->a_bg, p->bg, w, h);
368
369     /* draw the icons */
370     for (i = 0, it = p->targets; it; ++i, it = g_list_next(it)) {
371         const ObFocusCyclePopupTarget *target = it->data;
372
373         /* have to redraw the targetted icon and last targetted icon,
374            they can pick up the hilite changes in the backgroud */
375         if (!p->mapped || newtarget == target || p->last_target == target) {
376             const ObClientIcon *icon;
377             const gint row = i / icons_per_row; /* starting from 0 */
378             const gint col = i % icons_per_row; /* starting from 0 */
379             gint innerx, innery;
380
381             /* find the dimensions of the icon inside it */
382             innerx = l + (col * ICON_SIZE);
383             innerx += ICON_HILITE_WIDTH + ICON_HILITE_MARGIN;
384             innery = t + (row * ICON_SIZE);
385             innery += ICON_HILITE_WIDTH + ICON_HILITE_MARGIN;
386
387             /* move the icon */
388             XMoveResizeWindow(ob_display, target->win,
389                               innerx, innery, innerw, innerh);
390
391             /* get the icon from the client */
392             icon = client_icon(target->client, innerw, innerh);
393             p->a_icon->texture[0].data.rgba.width = icon->width;
394             p->a_icon->texture[0].data.rgba.height = icon->height;
395             p->a_icon->texture[0].data.rgba.data = icon->data;
396
397             /* draw the icon */
398             p->a_icon->surface.parentx = innerx;
399             p->a_icon->surface.parenty = innery;
400             RrPaint(p->a_icon, target->win, innerw, innerh);
401         }
402     }
403
404     /* draw the text */
405     p->a_text->texture[0].data.text.string = newtarget->text;
406     p->a_text->surface.parentx = textx;
407     p->a_text->surface.parenty = texty;
408     RrPaint(p->a_text, p->text, textw, texth);
409
410     p->last_target = newtarget;
411 }
412
413 void focus_cycle_popup_show(ObClient *c,
414                             gboolean all_desktops, gboolean dock_windows,
415                             gboolean desktop_windows)
416 {
417     g_assert(c != NULL);
418
419     /* do this stuff only when the dialog is first showing */
420     if (!popup.mapped)
421         popup_setup(&popup, all_desktops, dock_windows, desktop_windows);
422     g_assert(popup.targets != NULL);
423
424     popup_render(&popup, c);
425
426     if (!popup.mapped) {
427         /* show the dialog */
428         XMapWindow(ob_display, popup.bg);
429         XFlush(ob_display);
430         popup.mapped = TRUE;
431     }
432 }
433
434 void focus_cycle_popup_hide()
435 {
436     XUnmapWindow(ob_display, popup.bg);
437     XFlush(ob_display);
438
439     popup.mapped = FALSE;
440
441     while(popup.targets) {
442         ObFocusCyclePopupTarget *t = popup.targets->data;
443
444         g_free(t->text);
445         XDestroyWindow(ob_display, t->win);
446
447         popup.targets = g_list_delete_link(popup.targets, popup.targets);
448     }
449     popup.n_targets = 0;
450     popup.last_target = NULL;
451
452     g_free(popup.hilite_rgba);
453     popup.hilite_rgba = NULL;
454 }
455