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