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