]> icculus.org git repositories - mikachu/openbox.git/blob - openbox/focus_cycle_popup.c
Draw the hilite texture with the icon appearance instead of the background.
[mikachu/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);
104
105     popup.a_text->surface.parent = popup.a_bg;
106     popup.a_icon->surface.parent = popup.a_bg;
107
108     RrAppearanceAddTextures(popup.a_icon, 2);
109
110     popup.a_icon->texture[1].type = RR_TEXTURE_RGBA;
111
112     attrib.override_redirect = True;
113     attrib.border_pixel=RrColorPixel(ob_rr_theme->osd_border_color);
114     popup.bg = create_window(obt_root(ob_screen), ob_rr_theme->obwidth,
115                              CWOverrideRedirect | CWBorderPixel, &attrib);
116
117     popup.targets = NULL;
118     popup.n_targets = 0;
119     popup.last_target = NULL;
120
121     popup.hilite_rgba = NULL;
122
123     stacking_add(INTERNAL_AS_WINDOW(&popup));
124     window_add(&popup.bg, INTERNAL_AS_WINDOW(&popup));
125 }
126
127 void focus_cycle_popup_shutdown(gboolean reconfig)
128 {
129     icon_popup_free(single_popup);
130
131     window_remove(popup.bg);
132     stacking_remove(INTERNAL_AS_WINDOW(&popup));
133
134     while(popup.targets) {
135         ObFocusCyclePopupTarget *t = popup.targets->data;
136
137         g_free(t->text);
138         XDestroyWindow(obt_display, t->iconwin);
139         XDestroyWindow(obt_display, t->textwin);
140
141         popup.targets = g_list_delete_link(popup.targets, popup.targets);
142     }
143
144     g_free(popup.hilite_rgba);
145     popup.hilite_rgba = NULL;
146
147     XDestroyWindow(obt_display, popup.bg);
148
149     RrAppearanceFree(popup.a_icon);
150     RrAppearanceFree(popup.a_text);
151     RrAppearanceFree(popup.a_bg);
152 }
153
154 static void popup_setup(ObFocusCyclePopup *p, gboolean create_targets,
155                         gboolean iconic_windows, gboolean all_desktops,
156                         gboolean dock_windows, gboolean desktop_windows)
157 {
158     gint maxwidth, n;
159     GList *it;
160
161     g_assert(p->targets == NULL);
162     g_assert(p->n_targets == 0);
163
164     /* make its width to be the width of all the possible titles */
165
166     /* build a list of all the valid focus targets and measure their strings,
167        and count them */
168     maxwidth = 0;
169     n = 0;
170     for (it = g_list_last(focus_order); it; it = g_list_previous(it)) {
171         ObClient *ft = it->data;
172
173         if (focus_valid_target(ft, TRUE,
174                                iconic_windows,
175                                all_desktops,
176                                dock_windows,
177                                desktop_windows))
178         {
179             gchar *text = popup_get_name(ft);
180
181             /* measure */
182             p->a_text->texture[0].data.text.string = text;
183             maxwidth = MAX(maxwidth, RrMinWidth(p->a_text));
184
185             if (!create_targets)
186                 g_free(text);
187             else {
188                 ObFocusCyclePopupTarget *t = g_new(ObFocusCyclePopupTarget, 1);
189
190                 t->client = ft;
191                 t->text = text;
192                 t->iconwin = create_window(p->bg, 0, 0, NULL);
193                 t->textwin = create_window(p->bg, 0, 0, NULL);
194
195                 XMapWindow(obt_display, t->iconwin);
196                 XMapWindow(obt_display, t->textwin);
197
198                 p->targets = g_list_prepend(p->targets, t);
199                 ++n;
200             }
201         }
202     }
203
204     p->n_targets = n;
205     p->maxtextw = maxwidth;
206 }
207
208 static gchar *popup_get_name(ObClient *c)
209 {
210     ObClient *p;
211     gchar *title;
212     const gchar *desk = NULL;
213     gchar *ret;
214
215     /* find our highest direct parent */
216     p = client_search_top_direct_parent(c);
217
218     if (c->desktop != DESKTOP_ALL && c->desktop != screen_desktop)
219         desk = screen_desktop_names[c->desktop];
220
221     title = c->iconic ? c->icon_title : c->title;
222
223     /* use the transient's parent's title/icon if we don't have one */
224     if (p != c && title[0] == '\0')
225         title = p->iconic ? p->icon_title : p->title;
226
227     if (desk)
228         ret = g_strdup_printf("%s [%s]", title, desk);
229     else
230         ret = g_strdup(title);
231
232     return ret;
233 }
234
235 static void popup_render(ObFocusCyclePopup *p, const ObClient *c)
236 {
237     gint ml, mt, mr, mb;
238     gint l, t, r, b;
239     gint x, y, w, h;
240     Rect *screen_area = NULL;
241     gint rgbax, rgbay, rgbaw, rgbah;
242     gint innerw, innerh;
243     gint i;
244     GList *it;
245     const ObFocusCyclePopupTarget *newtarget;
246     gint newtargetx, newtargety;
247
248     screen_area = screen_physical_area_active();
249
250     /* get the outside margins */
251     RrMargins(p->a_bg, &ml, &mt, &mr, &mb);
252
253     /* get our outside borders */
254     l = ml + OUTSIDE_BORDER;
255     r = mr + OUTSIDE_BORDER;
256     t = mt + OUTSIDE_BORDER;
257     b = mb + OUTSIDE_BORDER;
258
259     /* get the icon pictures' sizes */
260     innerw = ICON_SIZE - (ICON_HILITE_WIDTH + ICON_HILITE_MARGIN) * 2;
261     innerh = ICON_SIZE - (ICON_HILITE_WIDTH + ICON_HILITE_MARGIN) * 2;
262
263     /* get the width from the text and keep it within limits */
264     w = l + r + p->maxtextw;
265     w = MIN(w, MAX(screen_area->width/3, POPUP_WIDTH)); /* max width */
266     w = MAX(w, POPUP_WIDTH); /* min width */
267
268     /* find the height of the dialog */
269 #warning limit the height and scroll entries somehow
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 icon */
289         p->a_icon->texture[0].data.rgba.width = ICON_SIZE;
290         p->a_icon->texture[0].data.rgba.height = ICON_SIZE;
291         p->a_icon->texture[0].data.rgba.alpha = 0xff;
292         p->hilite_rgba = g_new(RrPixel32, ICON_SIZE * ICON_SIZE);
293         p->a_icon->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 (x = 0; x < ICON_SIZE; x++)
324             for (y = 0; y < ICON_SIZE; y++) {
325                 guchar a;
326
327                 if (x < ICON_HILITE_WIDTH ||
328                     x >= ICON_SIZE - ICON_HILITE_WIDTH ||
329                     y < ICON_HILITE_WIDTH ||
330                     y >= ICON_SIZE - ICON_HILITE_WIDTH)
331                 {
332                     /* the border of the target */
333                     a = 0x88;
334                 }
335                 else {
336                     /* the background of the target */
337                     a = 0x22;
338                 }
339
340                 p->hilite_rgba[o++] =
341                     color + (a << RrDefaultAlphaOffset);
342             }
343     }
344
345     /* * * draw everything * * */
346
347     /* draw the background */
348     if (!p->mapped)
349         RrPaint(p->a_bg, p->bg, w, h);
350
351     /* draw the icons and text */
352     for (i = 0, it = p->targets; it; ++i, it = g_list_next(it)) {
353         const ObFocusCyclePopupTarget *target = it->data;
354
355         /* have to redraw the targetted icon and last targetted icon,
356            they can pick up the hilite changes in the backgroud */
357         if (!p->mapped || newtarget == target || p->last_target == target) {
358             const ObClientIcon *icon;
359             gint innerx, innery;
360
361             /* find the dimensions of the icon inside it */
362             innerx = l;
363             innerx += ICON_HILITE_WIDTH + ICON_HILITE_MARGIN;
364             innery = t + i * ICON_SIZE;
365             innery += ICON_HILITE_WIDTH + ICON_HILITE_MARGIN;
366
367             /* move the icon */
368             XMoveResizeWindow(obt_display, target->iconwin,
369                               innerx, innery, innerw, innerh);
370
371             /* move the text */
372             XMoveResizeWindow(obt_display, target->textwin,
373                               innerx + ICON_SIZE, innery,
374                               w - innerx - ICON_SIZE - OUTSIDE_BORDER, innerh);
375
376             /* get the icon from the client */
377             icon = client_icon(target->client, innerw, innerh);
378             p->a_icon->texture[1].data.rgba.width = icon->width;
379             p->a_icon->texture[1].data.rgba.height = icon->height;
380             p->a_icon->texture[1].data.rgba.alpha =
381                 target->client->iconic ? OB_ICONIC_ALPHA : 0xff;
382             p->a_icon->texture[1].data.rgba.data = icon->data;
383
384             /* Draw the hilite? */
385 #warning do i have to add more obrender interface thingers to get it to draw the icon inside the hilight? sigh
386             p->a_icon->texture[0].type = (target == newtarget) ?
387                                          RR_TEXTURE_RGBA : RR_TEXTURE_NONE;
388
389             /* draw the icon */
390             p->a_icon->surface.parentx = innerx;
391             p->a_icon->surface.parenty = innery;
392             RrPaint(p->a_icon, target->iconwin, innerw, innerh);
393
394             /* draw the text */
395             p->a_text->texture[0].data.text.string = target->text;
396             p->a_text->surface.parentx = innerx + ICON_SIZE;
397             p->a_text->surface.parenty = innery;
398             RrPaint(p->a_text, target->textwin, w - innerx - ICON_SIZE - OUTSIDE_BORDER, innerh);
399         }
400     }
401
402     p->last_target = newtarget;
403
404     g_free(screen_area);
405 }
406
407 void focus_cycle_popup_show(ObClient *c, gboolean iconic_windows,
408                             gboolean all_desktops, gboolean dock_windows,
409                             gboolean desktop_windows)
410 {
411     g_assert(c != NULL);
412
413     /* do this stuff only when the dialog is first showing */
414     if (!popup.mapped)
415         popup_setup(&popup, TRUE, iconic_windows, all_desktops,
416                     dock_windows, desktop_windows);
417     g_assert(popup.targets != NULL);
418
419     popup_render(&popup, c);
420
421     if (!popup.mapped) {
422         /* show the dialog */
423         XMapWindow(obt_display, popup.bg);
424         XFlush(obt_display);
425         popup.mapped = TRUE;
426         screen_hide_desktop_popup();
427     }
428 }
429
430 void focus_cycle_popup_hide(void)
431 {
432     gulong ignore_start;
433
434     ignore_start = event_start_ignore_all_enters();
435
436     XUnmapWindow(obt_display, popup.bg);
437     XFlush(obt_display);
438
439     event_end_ignore_all_enters(ignore_start);
440
441     popup.mapped = FALSE;
442
443     while(popup.targets) {
444         ObFocusCyclePopupTarget *t = popup.targets->data;
445
446         g_free(t->text);
447         XDestroyWindow(obt_display, t->iconwin);
448         XDestroyWindow(obt_display, t->textwin);
449         g_free(t);
450
451         popup.targets = g_list_delete_link(popup.targets, popup.targets);
452     }
453     popup.n_targets = 0;
454     popup.last_target = NULL;
455
456     g_free(popup.hilite_rgba);
457     popup.hilite_rgba = NULL;
458 }
459
460 void focus_cycle_popup_single_show(struct _ObClient *c,
461                                    gboolean iconic_windows,
462                                    gboolean all_desktops,
463                                    gboolean dock_windows,
464                                    gboolean desktop_windows)
465 {
466     gchar *text;
467
468     g_assert(c != NULL);
469
470     /* do this stuff only when the dialog is first showing */
471     if (!popup.mapped) {
472         Rect *a;
473
474         popup_setup(&popup, FALSE, iconic_windows, all_desktops,
475                     dock_windows, desktop_windows);
476         g_assert(popup.targets == NULL);
477
478         /* position the popup */
479         a = screen_physical_area_active();
480         icon_popup_position(single_popup, CenterGravity,
481                             a->x + a->width / 2, a->y + a->height / 2);
482         icon_popup_height(single_popup, POPUP_HEIGHT);
483         icon_popup_min_width(single_popup, POPUP_WIDTH);
484         icon_popup_max_width(single_popup, MAX(a->width/3, POPUP_WIDTH));
485         icon_popup_text_width(single_popup, popup.maxtextw);
486         g_free(a);
487     }
488
489     text = popup_get_name(c);
490     icon_popup_show(single_popup, text, client_icon(c, ICON_SIZE, ICON_SIZE));
491     g_free(text);
492     screen_hide_desktop_popup();
493 }
494
495 void focus_cycle_popup_single_hide(void)
496 {
497     icon_popup_hide(single_popup);
498 }