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