]> icculus.org git repositories - dana/openbox.git/blob - openbox/focus_cycle_popup.c
Redraw the focus cycle popup when the list of focusable windows changes, rather than...
[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     RrImage *icon;
46     gchar *text;
47     Window win;
48 };
49
50 struct _ObFocusCyclePopup
51 {
52     ObWindow obwin;
53     Window bg;
54
55     Window text;
56
57     GList *targets;
58     gint n_targets;
59
60     const ObFocusCyclePopupTarget *last_target;
61
62     gint maxtextw;
63
64     RrAppearance *a_bg;
65     RrAppearance *a_text;
66     RrAppearance *a_icon;
67
68     RrPixel32 *hilite_rgba;
69
70     gboolean mapped;
71 };
72
73 /*! This popup shows all possible windows */
74 static ObFocusCyclePopup popup;
75 /*! This popup shows a single window */
76 static ObIconPopup *single_popup;
77
78 static gchar *popup_get_name (ObClient *c);
79 static void   popup_setup    (ObFocusCyclePopup *p,
80                               gboolean create_targets,
81                               gboolean iconic_windows,
82                               gboolean all_desktops,
83                               gboolean dock_windows,
84                               gboolean desktop_windows);
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();
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     RrAppearanceClearTextures(popup.a_icon);
111     popup.a_icon->texture[0].type = RR_TEXTURE_IMAGE;
112
113     RrAppearanceAddTextures(popup.a_bg, 1);
114     popup.a_bg->texture[0].type = RR_TEXTURE_RGBA;
115
116     attrib.override_redirect = True;
117     attrib.border_pixel=RrColorPixel(ob_rr_theme->osd_border_color);
118     popup.bg = create_window(RootWindow(ob_display, ob_screen),
119                              ob_rr_theme->obwidth,
120                              CWOverrideRedirect | CWBorderPixel, &attrib);
121
122     popup.text = create_window(popup.bg, 0, 0, NULL);
123
124     popup.targets = NULL;
125     popup.n_targets = 0;
126     popup.last_target = NULL;
127
128     popup.hilite_rgba = NULL;
129
130     XMapWindow(ob_display, popup.text);
131
132     stacking_add(INTERNAL_AS_WINDOW(&popup));
133     g_hash_table_insert(window_map, &popup.bg, &popup);
134 }
135
136 void focus_cycle_popup_shutdown(gboolean reconfig)
137 {
138     icon_popup_free(single_popup);
139
140     g_hash_table_remove(window_map, &popup.bg);
141     stacking_remove(INTERNAL_AS_WINDOW(&popup));
142
143     while(popup.targets) {
144         ObFocusCyclePopupTarget *t = popup.targets->data;
145
146         RrImageUnref(t->icon);
147         g_free(t->text);
148         XDestroyWindow(ob_display, t->win);
149         g_free(t);
150
151         popup.targets = g_list_delete_link(popup.targets, popup.targets);
152     }
153
154     g_free(popup.hilite_rgba);
155     popup.hilite_rgba = NULL;
156
157     XDestroyWindow(ob_display, popup.text);
158     XDestroyWindow(ob_display, popup.bg);
159
160     RrAppearanceFree(popup.a_icon);
161     RrAppearanceFree(popup.a_text);
162     RrAppearanceFree(popup.a_bg);
163 }
164
165 static void popup_setup(ObFocusCyclePopup *p, gboolean create_targets,
166                         gboolean iconic_windows, gboolean all_desktops,
167                         gboolean dock_windows, gboolean desktop_windows)
168 {
169     gint maxwidth, n;
170     GList *it;
171
172     g_assert(p->targets == NULL);
173     g_assert(p->n_targets == 0);
174
175     /* make its width to be the width of all the possible titles */
176
177     /* build a list of all the valid focus targets and measure their strings,
178        and count them */
179     maxwidth = 0;
180     n = 0;
181     for (it = g_list_last(focus_order); it; it = g_list_previous(it)) {
182         ObClient *ft = it->data;
183
184         if (focus_valid_target(ft, TRUE,
185                                iconic_windows,
186                                all_desktops,
187                                dock_windows,
188                                desktop_windows,
189                                FALSE))
190         {
191             gchar *text = popup_get_name(ft);
192
193             /* measure */
194             p->a_text->texture[0].data.text.string = text;
195             maxwidth = MAX(maxwidth, RrMinWidth(p->a_text));
196
197             if (!create_targets)
198                 g_free(text);
199             else {
200                 ObFocusCyclePopupTarget *t = g_new(ObFocusCyclePopupTarget, 1);
201
202                 t->client = ft;
203                 t->text = text;
204                 t->icon = client_icon(t->client);
205                 RrImageRef(t->icon); /* own the icon so it won't go away */
206                 t->win = create_window(p->bg, 0, 0, NULL);
207
208                 XMapWindow(ob_display, t->win);
209
210                 p->targets = g_list_prepend(p->targets, t);
211                 ++n;
212             }
213         }
214     }
215
216     p->n_targets = n;
217     p->maxtextw = maxwidth;
218 }
219
220 static void popup_cleanup(void)
221 {
222     while(popup.targets) {
223         ObFocusCyclePopupTarget *t = popup.targets->data;
224
225         RrImageUnref(t->icon);
226         g_free(t->text);
227         XDestroyWindow(ob_display, t->win);
228         g_free(t);
229
230         popup.targets = g_list_delete_link(popup.targets, popup.targets);
231     }
232     popup.n_targets = 0;
233     popup.last_target = NULL;
234 }
235
236 static gchar *popup_get_name(ObClient *c)
237 {
238     ObClient *p;
239     gchar *title;
240     const gchar *desk = NULL;
241     gchar *ret;
242
243     /* find our highest direct parent */
244     p = client_search_top_direct_parent(c);
245
246     if (c->desktop != DESKTOP_ALL && c->desktop != screen_desktop)
247         desk = screen_desktop_names[c->desktop];
248
249     title = c->iconic ? c->icon_title : c->title;
250
251     /* use the transient's parent's title/icon if we don't have one */
252     if (p != c && title[0] == '\0')
253         title = p->iconic ? p->icon_title : p->title;
254
255     if (desk)
256         ret = g_strdup_printf("%s [%s]", title, desk);
257     else
258         ret = g_strdup(title);
259
260     return ret;
261 }
262
263 static void popup_render(ObFocusCyclePopup *p, const ObClient *c)
264 {
265     gint ml, mt, mr, mb;
266     gint l, t, r, b;
267     gint x, y, w, h;
268     Rect *screen_area = NULL;
269     gint icons_per_row;
270     gint icon_rows;
271     gint textx, texty, textw, texth;
272     gint rgbax, rgbay, rgbaw, rgbah;
273     gint icons_center_x;
274     gint innerw, innerh;
275     gint i;
276     GList *it;
277     const ObFocusCyclePopupTarget *newtarget;
278     gint newtargetx, newtargety;
279
280     screen_area = screen_physical_area_primary(FALSE);
281
282     /* get the outside margins */
283     RrMargins(p->a_bg, &ml, &mt, &mr, &mb);
284
285     /* get our outside borders */
286     l = ml + OUTSIDE_BORDER;
287     r = mr + OUTSIDE_BORDER;
288     t = mt + OUTSIDE_BORDER;
289     b = mb + OUTSIDE_BORDER;
290
291     /* get the icon pictures' sizes */
292     innerw = ICON_SIZE - (ICON_HILITE_WIDTH + ICON_HILITE_MARGIN) * 2;
293     innerh = ICON_SIZE - (ICON_HILITE_WIDTH + ICON_HILITE_MARGIN) * 2;
294
295     /* get the width from the text and keep it within limits */
296     w = l + r + p->maxtextw;
297     w = MIN(w, MAX(screen_area->width/3, POPUP_WIDTH)); /* max width */
298     w = MAX(w, POPUP_WIDTH); /* min width */
299
300     /* how many icons will fit in that row? make the width fit that */
301     w -= l + r;
302     icons_per_row = (w + ICON_SIZE - 1) / ICON_SIZE;
303     w = icons_per_row * ICON_SIZE + l + r;
304
305     /* how many rows do we need? */
306     icon_rows = (p->n_targets-1) / icons_per_row + 1;
307
308     /* get the text dimensions */
309     textw = w - l - r;
310     texth = RrMinHeight(p->a_text) + TEXT_BORDER * 2;
311
312     /* find the height of the dialog */
313     h = t + b + (icon_rows * ICON_SIZE) + (OUTSIDE_BORDER + texth);
314
315     /* get the position of the text */
316     textx = l;
317     texty = h - texth - b;
318
319     /* find the position for the popup (include the outer borders) */
320     x = screen_area->x + (screen_area->width -
321                           (w + ob_rr_theme->obwidth * 2)) / 2;
322     y = screen_area->y + (screen_area->height -
323                           (h + ob_rr_theme->obwidth * 2)) / 2;
324
325     /* get the dimensions of the target hilite texture */
326     rgbax = ml;
327     rgbay = mt;
328     rgbaw = w - ml - mr;
329     rgbah = h - mt - mb;
330
331     /* center the icons if there is less than one row */
332     if (icon_rows == 1)
333         icons_center_x = (w - p->n_targets * ICON_SIZE) / 2;
334     else
335         icons_center_x = 0;
336
337     if (!p->mapped) {
338         /* position the background but don't draw it*/
339         XMoveResizeWindow(ob_display, p->bg, x, y, w, h);
340
341         /* set up the hilite texture for the background */
342         p->a_bg->texture[0].data.rgba.width = rgbaw;
343         p->a_bg->texture[0].data.rgba.height = rgbah;
344         p->a_bg->texture[0].data.rgba.alpha = 0xff;
345         p->hilite_rgba = g_new(RrPixel32, rgbaw * rgbah);
346         p->a_bg->texture[0].data.rgba.data = p->hilite_rgba;
347
348         /* position the text, but don't draw it */
349         XMoveResizeWindow(ob_display, p->text, textx, texty, textw, texth);
350         p->a_text->surface.parentx = textx;
351         p->a_text->surface.parenty = texty;
352     }
353
354     /* find the focused target */
355     for (i = 0, it = p->targets; it; ++i, it = g_list_next(it)) {
356         const ObFocusCyclePopupTarget *target = it->data;
357         const gint row = i / icons_per_row; /* starting from 0 */
358         const gint col = i % icons_per_row; /* starting from 0 */
359
360         if (target->client == c) {
361             /* save the target */
362             newtarget = target;
363             newtargetx = icons_center_x + l + (col * ICON_SIZE);
364             newtargety = t + (row * ICON_SIZE);
365
366             if (!p->mapped)
367                 break; /* if we're not dimensioning, then we're done */
368         }
369     }
370
371     g_assert(newtarget != NULL);
372
373     /* create the hilite under the target icon */
374     {
375         RrPixel32 color;
376         gint i, j, o;
377
378         color = ((ob_rr_theme->osd_color->r & 0xff) << RrDefaultRedOffset) +
379             ((ob_rr_theme->osd_color->g & 0xff) << RrDefaultGreenOffset) +
380             ((ob_rr_theme->osd_color->b & 0xff) << RrDefaultBlueOffset);
381
382         o = 0;
383         for (i = 0; i < rgbah; ++i)
384             for (j = 0; j < rgbaw; ++j) {
385                 guchar a;
386                 const gint x = j + rgbax - newtargetx;
387                 const gint y = i + rgbay - newtargety;
388
389                 if (x < 0 || x >= ICON_SIZE ||
390                     y < 0 || y >= ICON_SIZE)
391                 {
392                     /* outside the target */
393                     a = 0x00;
394                 }
395                 else if (x < ICON_HILITE_WIDTH ||
396                          x >= ICON_SIZE - ICON_HILITE_WIDTH ||
397                          y < ICON_HILITE_WIDTH ||
398                          y >= ICON_SIZE - ICON_HILITE_WIDTH)
399                 {
400                     /* the border of the target */
401                     a = 0x88;
402                 }
403                 else {
404                     /* the background of the target */
405                     a = 0x22;
406                 }
407
408                 p->hilite_rgba[o++] =
409                     color + (a << RrDefaultAlphaOffset);
410             }
411     }
412
413     /* * * draw everything * * */
414
415     /* draw the background */
416     RrPaint(p->a_bg, p->bg, w, h);
417
418     /* draw the icons */
419     for (i = 0, it = p->targets; it; ++i, it = g_list_next(it)) {
420         const ObFocusCyclePopupTarget *target = it->data;
421
422         /* have to redraw the targetted icon and last targetted icon,
423            they can pick up the hilite changes in the backgroud */
424         if (!p->mapped || newtarget == target || p->last_target == target) {
425             const gint row = i / icons_per_row; /* starting from 0 */
426             const gint col = i % icons_per_row; /* starting from 0 */
427             gint innerx, innery;
428
429             /* find the dimensions of the icon inside it */
430             innerx = icons_center_x + l + (col * ICON_SIZE);
431             innerx += ICON_HILITE_WIDTH + ICON_HILITE_MARGIN;
432             innery = t + (row * ICON_SIZE);
433             innery += ICON_HILITE_WIDTH + ICON_HILITE_MARGIN;
434
435             /* move the icon */
436             XMoveResizeWindow(ob_display, target->win,
437                               innerx, innery, innerw, innerh);
438
439             /* get the icon from the client */
440             p->a_icon->texture[0].data.image.alpha =
441                 target->client->iconic ? OB_ICONIC_ALPHA : 0xff;
442             p->a_icon->texture[0].data.image.image = target->icon;
443
444             /* draw the icon */
445             p->a_icon->surface.parentx = innerx;
446             p->a_icon->surface.parenty = innery;
447             RrPaint(p->a_icon, target->win, innerw, innerh);
448         }
449     }
450
451     /* draw the text */
452     p->a_text->texture[0].data.text.string = newtarget->text;
453     p->a_text->surface.parentx = textx;
454     p->a_text->surface.parenty = texty;
455     RrPaint(p->a_text, p->text, textw, texth);
456
457     p->last_target = newtarget;
458
459     g_free(screen_area);
460 }
461
462 void focus_cycle_popup_show(ObClient *c, gboolean iconic_windows,
463                             gboolean all_desktops, gboolean dock_windows,
464                             gboolean desktop_windows)
465 {
466     g_assert(c != NULL);
467
468     /* do this stuff only when the dialog is first showing */
469     if (!popup.mapped)
470         popup_setup(&popup, TRUE, iconic_windows, all_desktops,
471                     dock_windows, desktop_windows);
472     g_assert(popup.targets != NULL);
473
474     popup_render(&popup, c);
475
476     if (!popup.mapped) {
477         /* show the dialog */
478         XMapWindow(ob_display, popup.bg);
479         XFlush(ob_display);
480         popup.mapped = TRUE;
481         screen_hide_desktop_popup();
482     }
483 }
484
485 void focus_cycle_popup_hide(void)
486 {
487     gulong ignore_start;
488
489     ignore_start = event_start_ignore_all_enters();
490
491     XUnmapWindow(ob_display, popup.bg);
492     XFlush(ob_display);
493
494     event_end_ignore_all_enters(ignore_start);
495
496     popup.mapped = FALSE;
497
498     popup_cleanup();
499
500     g_free(popup.hilite_rgba);
501     popup.hilite_rgba = NULL;
502 }
503
504 void focus_cycle_popup_single_show(struct _ObClient *c,
505                                    gboolean iconic_windows,
506                                    gboolean all_desktops,
507                                    gboolean dock_windows,
508                                    gboolean desktop_windows)
509 {
510     gchar *text;
511
512     g_assert(c != NULL);
513
514     /* do this stuff only when the dialog is first showing */
515     if (!popup.mapped) {
516         Rect *a;
517
518         popup_setup(&popup, FALSE, iconic_windows, all_desktops,
519                     dock_windows, desktop_windows);
520         g_assert(popup.targets == NULL);
521
522         /* position the popup */
523         a = screen_physical_area_primary(FALSE);
524         icon_popup_position(single_popup, CenterGravity,
525                             a->x + a->width / 2, a->y + a->height / 2);
526         icon_popup_height(single_popup, POPUP_HEIGHT);
527         icon_popup_min_width(single_popup, POPUP_WIDTH);
528         icon_popup_max_width(single_popup, MAX(a->width/3, POPUP_WIDTH));
529         icon_popup_text_width(single_popup, popup.maxtextw);
530         g_free(a);
531     }
532
533     text = popup_get_name(c);
534     icon_popup_show(single_popup, text, client_icon(c));
535     g_free(text);
536     screen_hide_desktop_popup();
537 }
538
539 void focus_cycle_popup_single_hide(void)
540 {
541     icon_popup_hide(single_popup);
542 }
543
544 GList* focus_cycle_popup_is_showing(ObClient *client)
545 {
546     if (popup.mapped) {
547         GList *it;
548
549         for (it = popup.targets; it; it = g_list_next(it)) {
550             ObFocusCyclePopupTarget *t = it->data;
551             if (t->client == client)
552                 return it;
553         }
554     }
555     return NULL;
556 }
557
558 ObClient* focus_cycle_popup_revert(ObClient *target)
559 {
560     GList *it;
561
562     if (!popup.mapped) return NULL;
563
564     for (it = popup.targets; it; it = g_list_next(it)) {
565         ObFocusCyclePopupTarget *t = it->data;
566         if (t->client == target) {
567             if (it->prev)
568                 return ((ObFocusCyclePopupTarget*)it->prev->data)->client;
569             else if (it->next)
570                 return ((ObFocusCyclePopupTarget*)it->next->data)->client;
571             else
572                 return NULL;
573         }
574     }
575     g_assert_not_reached();
576 }
577
578 void focus_cycle_popup_refresh(ObClient *target,
579                                gboolean iconic_windows,
580                                gboolean all_desktops,
581                                gboolean dock_windows,
582                                gboolean desktop_windows)
583 {
584     if (!popup.mapped) return;
585
586     popup_cleanup();
587     popup_setup(&popup, TRUE, iconic_windows, all_desktops,
588                 dock_windows, desktop_windows);
589
590     popup.mapped = FALSE;
591     popup_render(&popup, target);
592     XFlush(ob_display);
593     popup.mapped = TRUE;
594 }