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