]> icculus.org git repositories - mikachu/openbox.git/blob - openbox/focus_cycle_popup.c
center the icons when there is only 1 row
[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 icons_center_x;
238     gint innerw, innerh;
239     gint i;
240     GList *it;
241     const ObFocusCyclePopupTarget *newtarget;
242     gint newtargetx, newtargety;
243
244     /* XXX find the middle monitor? */
245     screen_area = screen_physical_area_monitor(0);
246
247     /* get the outside margins */
248     RrMargins(p->a_bg, &ml, &mt, &mr, &mb);
249
250     /* get our outside borders */
251     l = ml + OUTSIDE_BORDER;
252     r = mr + OUTSIDE_BORDER;
253     t = mt + OUTSIDE_BORDER;
254     b = mb + OUTSIDE_BORDER;
255
256     /* get the icon pictures' sizes */
257     innerw = ICON_SIZE - (ICON_HILITE_WIDTH + ICON_HILITE_MARGIN) * 2;
258     innerh = ICON_SIZE - (ICON_HILITE_WIDTH + ICON_HILITE_MARGIN) * 2;
259
260     /* get the width from the text and keep it within limits */
261     w = l + r + p->maxtextw;
262     w = MIN(w, MAX(screen_area->width/3, POPUP_WIDTH)); /* max width */
263     w = MAX(w, POPUP_WIDTH); /* min width */
264
265     /* how many icons will fit in that row? make the width fit that */
266     w -= l + r;
267     icons_per_row = w / ICON_SIZE;
268     w = icons_per_row * ICON_SIZE + l + r;
269
270     /* how many rows do we need? */
271     icon_rows = (p->n_targets-1) / icons_per_row + 1;
272
273     /* get the text dimensions */
274     textw = w - l - r;
275     texth = RrMinHeight(p->a_text);
276
277     /* find the height of the dialog */
278     h = t + b + (icon_rows * ICON_SIZE) + (OUTSIDE_BORDER + texth);
279
280     /* get the position of the text */
281     textx = l;
282     texty = h - texth - b;
283
284     /* find the position for the popup (include the outer borders) */
285     x = screen_area->x + (screen_area->width -
286                           (w + ob_rr_theme->fbwidth * 2)) / 2;
287     y = screen_area->y + (screen_area->height -
288                           (h + ob_rr_theme->fbwidth * 2)) / 2;
289
290     /* get the dimensions of the target hilite texture */
291     rgbax = ml;
292     rgbay = mt;
293     rgbaw = w - ml - mr;
294     rgbah = h - mt - mb;
295
296     /* center the icons if there is less than one row */
297     if (icon_rows == 1)
298         icons_center_x = (w - p->n_targets * ICON_SIZE) / 2;
299     else
300         icons_center_x;
301
302     if (!p->mapped) {
303         /* position the background but don't draw it*/
304         XMoveResizeWindow(ob_display, p->bg, x, y, w, h);
305
306         /* set up the hilite texture for the background */
307         p->a_bg->texture[0].data.rgba.width = rgbaw;
308         p->a_bg->texture[0].data.rgba.height = rgbah;
309         p->hilite_rgba = g_new(RrPixel32, rgbaw * rgbah);
310         p->a_bg->texture[0].data.rgba.data = p->hilite_rgba;
311
312         /* position the text, but don't draw it */
313         XMoveResizeWindow(ob_display, p->text, textx, texty, textw, texth);
314         p->a_text->surface.parentx = textx;
315         p->a_text->surface.parenty = texty;
316     }
317
318     /* find the focused target */
319     for (i = 0, it = p->targets; it; ++i, it = g_list_next(it)) {
320         const ObFocusCyclePopupTarget *target = it->data;
321         const gint row = i / icons_per_row; /* starting from 0 */
322         const gint col = i % icons_per_row; /* starting from 0 */
323
324         if (target->client == c) {
325             /* save the target */
326             newtarget = target;
327             newtargetx = icons_center_x + l + (col * ICON_SIZE);
328             newtargety = t + (row * ICON_SIZE);
329
330             if (!p->mapped)
331                 break; /* if we're not dimensioning, then we're done */
332         }
333     }
334
335     g_assert(newtarget != NULL);
336
337     /* create the hilite under the target icon */
338     {
339         RrPixel32 color;
340         gint i, j, o;
341
342         color = ((ob_rr_theme->osd_color->r & 0xff) << RrDefaultRedOffset) +
343             ((ob_rr_theme->osd_color->g & 0xff) << RrDefaultGreenOffset) +
344             ((ob_rr_theme->osd_color->b & 0xff) << RrDefaultBlueOffset);
345
346         o = 0;
347         for (i = 0; i < rgbah; ++i)
348             for (j = 0; j < rgbaw; ++j) {
349                 guchar a;
350                 const gint x = j + rgbax - newtargetx;
351                 const gint y = i + rgbay - newtargety;
352
353                 if (x < 0 || x >= ICON_SIZE ||
354                     y < 0 || y >= ICON_SIZE)
355                 {
356                     /* outside the target */
357                     a = 0x00;
358                 }
359                 else if (x < ICON_HILITE_WIDTH ||
360                          x >= ICON_SIZE - ICON_HILITE_WIDTH ||
361                          y < ICON_HILITE_WIDTH ||
362                          y >= ICON_SIZE - ICON_HILITE_WIDTH)
363                 {
364                     /* the border of the target */
365                     a = 0x88;
366                 }
367                 else {
368                     /* the background of the target */
369                     a = 0x22;
370                 }
371
372                 p->hilite_rgba[o++] =
373                     color + (a << RrDefaultAlphaOffset);
374             }
375     }
376
377     /* * * draw everything * * */
378
379     /* draw the background */
380     RrPaint(p->a_bg, p->bg, w, h);
381
382     /* draw the icons */
383     for (i = 0, it = p->targets; it; ++i, it = g_list_next(it)) {
384         const ObFocusCyclePopupTarget *target = it->data;
385
386         /* have to redraw the targetted icon and last targetted icon,
387            they can pick up the hilite changes in the backgroud */
388         if (!p->mapped || newtarget == target || p->last_target == target) {
389             const ObClientIcon *icon;
390             const gint row = i / icons_per_row; /* starting from 0 */
391             const gint col = i % icons_per_row; /* starting from 0 */
392             gint innerx, innery;
393
394             /* find the dimensions of the icon inside it */
395             innerx = icons_center_x + l + (col * ICON_SIZE);
396             innerx += ICON_HILITE_WIDTH + ICON_HILITE_MARGIN;
397             innery = t + (row * ICON_SIZE);
398             innery += ICON_HILITE_WIDTH + ICON_HILITE_MARGIN;
399
400             /* move the icon */
401             XMoveResizeWindow(ob_display, target->win,
402                               innerx, innery, innerw, innerh);
403
404             /* get the icon from the client */
405             icon = client_icon(target->client, innerw, innerh);
406             p->a_icon->texture[0].data.rgba.width = icon->width;
407             p->a_icon->texture[0].data.rgba.height = icon->height;
408             p->a_icon->texture[0].data.rgba.data = icon->data;
409
410             /* draw the icon */
411             p->a_icon->surface.parentx = innerx;
412             p->a_icon->surface.parenty = innery;
413             RrPaint(p->a_icon, target->win, innerw, innerh);
414         }
415     }
416
417     /* draw the text */
418     p->a_text->texture[0].data.text.string = newtarget->text;
419     p->a_text->surface.parentx = textx;
420     p->a_text->surface.parenty = texty;
421     RrPaint(p->a_text, p->text, textw, texth);
422
423     p->last_target = newtarget;
424 }
425
426 void focus_cycle_popup_show(ObClient *c, gboolean iconic_windows,
427                             gboolean all_desktops, gboolean dock_windows,
428                             gboolean desktop_windows)
429 {
430     g_assert(c != NULL);
431
432     /* do this stuff only when the dialog is first showing */
433     if (!popup.mapped)
434         popup_setup(&popup, iconic_windows, all_desktops,
435                     dock_windows, desktop_windows);
436     g_assert(popup.targets != NULL);
437
438     popup_render(&popup, c);
439
440     if (!popup.mapped) {
441         /* show the dialog */
442         XMapWindow(ob_display, popup.bg);
443         XFlush(ob_display);
444         popup.mapped = TRUE;
445     }
446 }
447
448 void focus_cycle_popup_hide()
449 {
450     XUnmapWindow(ob_display, popup.bg);
451     XFlush(ob_display);
452
453     popup.mapped = FALSE;
454
455     while(popup.targets) {
456         ObFocusCyclePopupTarget *t = popup.targets->data;
457
458         g_free(t->text);
459         XDestroyWindow(ob_display, t->win);
460
461         popup.targets = g_list_delete_link(popup.targets, popup.targets);
462     }
463     popup.n_targets = 0;
464     popup.last_target = NULL;
465
466     g_free(popup.hilite_rgba);
467     popup.hilite_rgba = NULL;
468 }
469