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