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