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