]> icculus.org git repositories - dana/openbox.git/blob - openbox/focus_cycle_popup.c
Use a_unfocused_label for all but the hilited items.
[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 HILITE_SIZE 40
34 #define HILITE_WIDTH 2
35 #define HILITE_MARGIN 1
36 #define HILITE_OFFSET (HILITE_WIDTH + HILITE_MARGIN)
37 #define ICON_SIZE (HILITE_SIZE - 2*HILITE_OFFSET)
38 #define OUTSIDE_BORDER 3
39 #define TEXT_BORDER 2
40
41 typedef struct _ObFocusCyclePopup       ObFocusCyclePopup;
42 typedef struct _ObFocusCyclePopupTarget ObFocusCyclePopupTarget;
43
44 struct _ObFocusCyclePopupTarget
45 {
46     ObClient *client;
47     gchar *text;
48     Window iconwin;
49     Window textwin;
50 };
51
52 struct _ObFocusCyclePopup
53 {
54     ObWindow obwin;
55     Window bg;
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_hilite_text;
67     RrAppearance *a_icon;
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     RrPixel32 *p;
99
100     single_popup = icon_popup_new();
101
102     popup.obwin.type = OB_WINDOW_CLASS_INTERNAL;
103     popup.a_bg = RrAppearanceCopy(ob_rr_theme->osd_hilite_bg);
104     popup.a_hilite_text = RrAppearanceCopy(ob_rr_theme->osd_hilite_label);
105     popup.a_text = RrAppearanceCopy(ob_rr_theme->a_unfocused_label);
106     popup.a_icon = RrAppearanceCopy(ob_rr_theme->a_clear);
107
108     popup.a_hilite_text->surface.parent = popup.a_bg;
109     popup.a_text->surface.parent = popup.a_bg;
110     popup.a_icon->surface.parent = popup.a_bg;
111
112     RrAppearanceAddTextures(popup.a_icon, 2);
113
114     popup.a_icon->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(obt_root(ob_screen), ob_rr_theme->obwidth,
119                              CWOverrideRedirect | CWBorderPixel, &attrib);
120
121     popup.targets = NULL;
122     popup.n_targets = 0;
123     popup.last_target = NULL;
124
125     /* set up the hilite texture for the icon */
126     popup.a_icon->texture[1].data.rgba.width = HILITE_SIZE;
127     popup.a_icon->texture[1].data.rgba.height = HILITE_SIZE;
128     popup.a_icon->texture[1].data.rgba.alpha = 0xff;
129     p = g_new(RrPixel32, HILITE_SIZE * HILITE_SIZE);
130     popup.a_icon->texture[1].data.rgba.data = p;
131
132     /* create the hilite under the target icon */
133     {
134         RrPixel32 color;
135         gint x, y, o;
136
137         color = ((ob_rr_theme->osd_color->r & 0xff) << RrDefaultRedOffset) +
138             ((ob_rr_theme->osd_color->g & 0xff) << RrDefaultGreenOffset) +
139             ((ob_rr_theme->osd_color->b & 0xff) << RrDefaultBlueOffset);
140
141         o = 0;
142         for (x = 0; x < HILITE_SIZE; x++)
143             for (y = 0; y < HILITE_SIZE; y++) {
144                 guchar a;
145
146                 if (x < HILITE_WIDTH ||
147                     x >= HILITE_SIZE - HILITE_WIDTH ||
148                     y < HILITE_WIDTH ||
149                     y >= HILITE_SIZE - HILITE_WIDTH)
150                 {
151                     /* the border of the target */
152                     a = 0x88;
153                 }
154                 else {
155                     /* the background of the target */
156                     a = 0x22;
157                 }
158
159                 p[o++] = color + (a << RrDefaultAlphaOffset);
160             }
161     }
162
163     stacking_add(INTERNAL_AS_WINDOW(&popup));
164     window_add(&popup.bg, INTERNAL_AS_WINDOW(&popup));
165 }
166
167 void focus_cycle_popup_shutdown(gboolean reconfig)
168 {
169     icon_popup_free(single_popup);
170
171     window_remove(popup.bg);
172     stacking_remove(INTERNAL_AS_WINDOW(&popup));
173
174     while(popup.targets) {
175         ObFocusCyclePopupTarget *t = popup.targets->data;
176
177         g_free(t->text);
178         XDestroyWindow(obt_display, t->iconwin);
179         XDestroyWindow(obt_display, t->textwin);
180
181         popup.targets = g_list_delete_link(popup.targets, popup.targets);
182     }
183
184     XDestroyWindow(obt_display, popup.bg);
185
186     RrAppearanceFree(popup.a_icon);
187     RrAppearanceFree(popup.a_hilite_text);
188     RrAppearanceFree(popup.a_text);
189     RrAppearanceFree(popup.a_bg);
190 }
191
192 static void popup_setup(ObFocusCyclePopup *p, gboolean create_targets,
193                         gboolean iconic_windows, gboolean all_desktops,
194                         gboolean dock_windows, gboolean desktop_windows)
195 {
196     gint maxwidth, n;
197     GList *it;
198
199     g_assert(p->targets == NULL);
200     g_assert(p->n_targets == 0);
201
202     /* make its width to be the width of all the possible titles */
203
204     /* build a list of all the valid focus targets and measure their strings,
205        and count them */
206     maxwidth = 0;
207     n = 0;
208     for (it = g_list_last(focus_order); it; it = g_list_previous(it)) {
209         ObClient *ft = it->data;
210
211         if (focus_valid_target(ft, TRUE,
212                                iconic_windows,
213                                all_desktops,
214                                dock_windows,
215                                desktop_windows))
216         {
217             gchar *text = popup_get_name(ft);
218
219             /* measure */
220             p->a_text->texture[0].data.text.string = text;
221             maxwidth = MAX(maxwidth, RrMinWidth(p->a_text));
222
223             if (!create_targets)
224                 g_free(text);
225             else {
226                 ObFocusCyclePopupTarget *t = g_new(ObFocusCyclePopupTarget, 1);
227
228                 t->client = ft;
229                 t->text = text;
230                 t->iconwin = create_window(p->bg, 0, 0, NULL);
231                 t->textwin = create_window(p->bg, 0, 0, NULL);
232
233                 XMapWindow(obt_display, t->iconwin);
234                 XMapWindow(obt_display, t->textwin);
235
236                 p->targets = g_list_prepend(p->targets, t);
237                 ++n;
238             }
239         }
240     }
241
242     p->n_targets = n;
243     p->maxtextw = maxwidth;
244 }
245
246 static gchar *popup_get_name(ObClient *c)
247 {
248     ObClient *p;
249     gchar *title;
250     const gchar *desk = NULL;
251     gchar *ret;
252
253     /* find our highest direct parent */
254     p = client_search_top_direct_parent(c);
255
256     if (c->desktop != DESKTOP_ALL && c->desktop != screen_desktop)
257         desk = screen_desktop_names[c->desktop];
258
259     title = c->iconic ? c->icon_title : c->title;
260
261     /* use the transient's parent's title/icon if we don't have one */
262     if (p != c && title[0] == '\0')
263         title = p->iconic ? p->icon_title : p->title;
264
265     if (desk)
266         ret = g_strdup_printf("%s [%s]", title, desk);
267     else
268         ret = g_strdup(title);
269
270     return ret;
271 }
272
273 static void popup_render(ObFocusCyclePopup *p, const ObClient *c)
274 {
275     gint ml, mt, mr, mb;
276     gint l, t, r, b;
277     gint x, y, w, h;
278     Rect *screen_area = NULL;
279     gint rgbax, rgbay, rgbaw, rgbah;
280     gint i;
281     GList *it;
282     const ObFocusCyclePopupTarget *newtarget;
283     gint newtargetx, newtargety;
284
285     screen_area = screen_physical_area_active();
286
287     /* get the outside margins */
288     RrMargins(p->a_bg, &ml, &mt, &mr, &mb);
289
290     /* get our outside borders */
291     l = ml + OUTSIDE_BORDER;
292     r = mr + OUTSIDE_BORDER;
293     t = mt + OUTSIDE_BORDER;
294     b = mb + OUTSIDE_BORDER;
295
296     /* get the width from the text and keep it within limits */
297     w = l + r + p->maxtextw;
298     w = MIN(w, MAX(screen_area->width/3, POPUP_WIDTH)); /* max width */
299     w = MAX(w, POPUP_WIDTH); /* min width */
300
301     /* find the height of the dialog */
302 #warning limit the height and scroll entries somehow
303     h = t + b + (p->n_targets * HILITE_SIZE);
304
305     /* find the position for the popup (include the outer borders) */
306     x = screen_area->x + (screen_area->width -
307                           (w + ob_rr_theme->obwidth * 2)) / 2;
308     y = screen_area->y + (screen_area->height -
309                           (h + ob_rr_theme->obwidth * 2)) / 2;
310
311     /* get the dimensions of the target hilite texture */
312     rgbax = ml;
313     rgbay = mt;
314     rgbaw = w - ml - mr;
315     rgbah = h - mt - mb;
316
317     if (!p->mapped) {
318         /* position the background but don't draw it*/
319         XMoveResizeWindow(obt_display, p->bg, x, y, w, h);
320     }
321
322     /* find the focused target */
323     for (i = 0, it = p->targets; it; ++i, it = g_list_next(it)) {
324         const ObFocusCyclePopupTarget *target = it->data;
325
326         if (target->client == c) {
327             /* save the target */
328             newtarget = target;
329             newtargetx = l;
330             newtargety = t + i * HILITE_SIZE;
331
332             if (!p->mapped)
333                 break; /* if we're not dimensioning, then we're done */
334         }
335     }
336
337     g_assert(newtarget != NULL);
338
339     /* * * draw everything * * */
340
341     /* draw the background */
342     if (!p->mapped)
343         RrPaint(p->a_bg, p->bg, w, h);
344
345     /* draw the icons and text */
346     for (i = 0, it = p->targets; it; ++i, it = g_list_next(it)) {
347         const ObFocusCyclePopupTarget *target = it->data;
348
349         /* have to redraw the targetted icon and last targetted icon
350          * to update the hilite */
351         if (!p->mapped || newtarget == target || p->last_target == target) {
352             const ObClientIcon *icon;
353             gint innerx, innery, textx, texty;
354             gint textw /* texth = ICON_SIZE */;
355             RrAppearance *text;
356
357             /* find the dimensions of the icon inside it */
358             innerx = l;
359             innery = t + i * HILITE_SIZE;
360
361             /* find the dimensions of the text box */
362             textx = innerx + HILITE_SIZE + TEXT_BORDER;
363             texty = innery + HILITE_OFFSET;
364             textw = w
365                     /* left edge */  - innerx - HILITE_SIZE - TEXT_BORDER
366                     /* right edge */ - OUTSIDE_BORDER - TEXT_BORDER;
367
368             /* move the icon */
369             XMoveResizeWindow(obt_display, target->iconwin,
370                               innerx, innery, HILITE_SIZE, HILITE_SIZE);
371
372             /* move the text */
373             XMoveResizeWindow(obt_display, target->textwin,
374                               textx, texty,
375                               textw, ICON_SIZE);
376
377             /* get the icon from the client */
378             icon = client_icon(target->client, ICON_SIZE, ICON_SIZE);
379             p->a_icon->texture[0].data.rgba.width = icon->width;
380             p->a_icon->texture[0].data.rgba.height = icon->height;
381             p->a_icon->texture[0].data.rgba.twidth = ICON_SIZE;
382             p->a_icon->texture[0].data.rgba.theight = ICON_SIZE;
383             p->a_icon->texture[0].data.rgba.tx = HILITE_OFFSET;
384             p->a_icon->texture[0].data.rgba.ty = HILITE_OFFSET;
385             p->a_icon->texture[0].data.rgba.alpha =
386                 target->client->iconic ? OB_ICONIC_ALPHA : 0xff;
387             p->a_icon->texture[0].data.rgba.data = icon->data;
388
389             /* Draw the hilite? */
390             p->a_icon->texture[1].type = (target == newtarget) ?
391                                          RR_TEXTURE_RGBA : RR_TEXTURE_NONE;
392
393             /* draw the icon */
394             p->a_icon->surface.parentx = innerx;
395             p->a_icon->surface.parenty = innery;
396             RrPaint(p->a_icon, target->iconwin, HILITE_SIZE, HILITE_SIZE);
397
398             /* draw the text */
399             text = (target == newtarget) ? p->a_hilite_text : p->a_text;
400             text->texture[0].data.text.string = target->text;
401             text->surface.parentx = textx;
402             text->surface.parenty = texty;
403             RrPaint(text, target->textwin, textw, ICON_SIZE);
404         }
405     }
406
407     p->last_target = newtarget;
408
409     g_free(screen_area);
410 }
411
412 void focus_cycle_popup_show(ObClient *c, gboolean iconic_windows,
413                             gboolean all_desktops, gboolean dock_windows,
414                             gboolean desktop_windows)
415 {
416     g_assert(c != NULL);
417
418     /* do this stuff only when the dialog is first showing */
419     if (!popup.mapped)
420         popup_setup(&popup, TRUE, iconic_windows, all_desktops,
421                     dock_windows, desktop_windows);
422     g_assert(popup.targets != NULL);
423
424     popup_render(&popup, c);
425
426     if (!popup.mapped) {
427         /* show the dialog */
428         XMapWindow(obt_display, popup.bg);
429         XFlush(obt_display);
430         popup.mapped = TRUE;
431         screen_hide_desktop_popup();
432     }
433 }
434
435 void focus_cycle_popup_hide(void)
436 {
437     gulong ignore_start;
438
439     ignore_start = event_start_ignore_all_enters();
440
441     XUnmapWindow(obt_display, popup.bg);
442     XFlush(obt_display);
443
444     event_end_ignore_all_enters(ignore_start);
445
446     popup.mapped = FALSE;
447
448     while(popup.targets) {
449         ObFocusCyclePopupTarget *t = popup.targets->data;
450
451         g_free(t->text);
452         XDestroyWindow(obt_display, t->iconwin);
453         XDestroyWindow(obt_display, t->textwin);
454         g_free(t);
455
456         popup.targets = g_list_delete_link(popup.targets, popup.targets);
457     }
458     popup.n_targets = 0;
459     popup.last_target = 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, HILITE_SIZE,
493                                                     HILITE_SIZE));
494     g_free(text);
495     screen_hide_desktop_popup();
496 }
497
498 void focus_cycle_popup_single_hide(void)
499 {
500     icon_popup_hide(single_popup);
501 }