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