]> Dogcows Code - chaz/openbox/blob - openbox/focus_cycle_popup.c
b97bc97bb4088d3a5860b8426a8256bb5257b7ce
[chaz/openbox] / 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 RrImage *icon;
46 gchar *text;
47 Window win;
48 };
49
50 struct _ObFocusCyclePopup
51 {
52 ObWindow obwin;
53 Window bg;
54
55 Window text;
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 RrPixel32 *hilite_rgba;
69
70 gboolean mapped;
71 };
72
73 /*! This popup shows all possible windows */
74 static ObFocusCyclePopup popup;
75 /*! This popup shows a single window */
76 static ObIconPopup *single_popup;
77
78 static gchar *popup_get_name (ObClient *c);
79 static gboolean popup_setup (ObFocusCyclePopup *p,
80 gboolean create_targets,
81 gboolean refresh_targets,
82 gboolean iconic_windows,
83 gboolean all_desktops,
84 gboolean dock_windows,
85 gboolean desktop_windows);
86 static void popup_render (ObFocusCyclePopup *p,
87 const ObClient *c);
88
89 static Window create_window(Window parent, guint bwidth, gulong mask,
90 XSetWindowAttributes *attr)
91 {
92 return XCreateWindow(ob_display, parent, 0, 0, 1, 1, bwidth,
93 RrDepth(ob_rr_inst), InputOutput,
94 RrVisual(ob_rr_inst), mask, attr);
95 }
96
97 void focus_cycle_popup_startup(gboolean reconfig)
98 {
99 XSetWindowAttributes attrib;
100
101 single_popup = icon_popup_new();
102
103 popup.obwin.type = Window_Internal;
104 popup.a_bg = RrAppearanceCopy(ob_rr_theme->osd_hilite_bg);
105 popup.a_text = RrAppearanceCopy(ob_rr_theme->osd_hilite_label);
106 popup.a_icon = RrAppearanceCopy(ob_rr_theme->a_clear_tex);
107
108 popup.a_text->surface.parent = popup.a_bg;
109 popup.a_icon->surface.parent = popup.a_bg;
110
111 RrAppearanceClearTextures(popup.a_icon);
112 popup.a_icon->texture[0].type = RR_TEXTURE_IMAGE;
113
114 RrAppearanceAddTextures(popup.a_bg, 1);
115 popup.a_bg->texture[0].type = RR_TEXTURE_RGBA;
116
117 attrib.override_redirect = True;
118 attrib.border_pixel=RrColorPixel(ob_rr_theme->osd_border_color);
119 popup.bg = create_window(RootWindow(ob_display, ob_screen),
120 ob_rr_theme->obwidth,
121 CWOverrideRedirect | CWBorderPixel, &attrib);
122
123 popup.text = create_window(popup.bg, 0, 0, NULL);
124
125 popup.targets = NULL;
126 popup.n_targets = 0;
127 popup.last_target = NULL;
128
129 popup.hilite_rgba = NULL;
130
131 XMapWindow(ob_display, popup.text);
132
133 stacking_add(INTERNAL_AS_WINDOW(&popup));
134 g_hash_table_insert(window_map, &popup.bg, &popup);
135 }
136
137 void focus_cycle_popup_shutdown(gboolean reconfig)
138 {
139 icon_popup_free(single_popup);
140
141 g_hash_table_remove(window_map, &popup.bg);
142 stacking_remove(INTERNAL_AS_WINDOW(&popup));
143
144 while(popup.targets) {
145 ObFocusCyclePopupTarget *t = popup.targets->data;
146
147 RrImageUnref(t->icon);
148 g_free(t->text);
149 XDestroyWindow(ob_display, t->win);
150 g_free(t);
151
152 popup.targets = g_list_delete_link(popup.targets, popup.targets);
153 }
154
155 g_free(popup.hilite_rgba);
156 popup.hilite_rgba = NULL;
157
158 XDestroyWindow(ob_display, popup.text);
159 XDestroyWindow(ob_display, popup.bg);
160
161 RrAppearanceFree(popup.a_icon);
162 RrAppearanceFree(popup.a_text);
163 RrAppearanceFree(popup.a_bg);
164 }
165
166 static void popup_target_free(ObFocusCyclePopupTarget *t)
167 {
168 RrImageUnref(t->icon);
169 g_free(t->text);
170 XDestroyWindow(ob_display, t->win);
171 g_free(t);
172 }
173
174 static gboolean popup_setup(ObFocusCyclePopup *p, gboolean create_targets,
175 gboolean refresh_targets,
176 gboolean iconic_windows, gboolean all_desktops,
177 gboolean dock_windows, gboolean desktop_windows)
178 {
179 gint maxwidth, n;
180 GList *it;
181 GList *rtargets; /* old targets for refresh */
182 GList *rtlast;
183 gboolean change;
184
185 if (refresh_targets) {
186 rtargets = p->targets;
187 rtlast = g_list_last(rtargets);
188 p->targets = NULL;
189 p->n_targets = 0;
190 change = FALSE;
191 }
192 else {
193 rtargets = rtlast = NULL;
194 change = TRUE;
195 }
196
197 g_assert(p->targets == NULL);
198 g_assert(p->n_targets == 0);
199
200 /* make its width to be the width of all the possible titles */
201
202 /* build a list of all the valid focus targets and measure their strings,
203 and count them */
204 maxwidth = 0;
205 n = 0;
206 for (it = g_list_last(focus_order); it; it = g_list_previous(it)) {
207 ObClient *ft = it->data;
208
209 if (focus_valid_target(ft, TRUE,
210 iconic_windows,
211 all_desktops,
212 dock_windows,
213 desktop_windows,
214 FALSE))
215 {
216 GList *rit;
217
218 /* reuse the target if possible during refresh */
219 for (rit = rtlast; rit; rit = g_list_previous(rit)) {
220 ObFocusCyclePopupTarget *t = rit->data;
221 if (t->client == ft) {
222 if (rit == rtlast)
223 rtlast = g_list_previous(rit);
224 rtargets = g_list_remove_link(rtargets, rit);
225
226 p->targets = g_list_concat(rit, p->targets);
227 ++n;
228
229 if (rit != rtlast)
230 change = TRUE; /* order changed */
231 break;
232 }
233 }
234
235 if (!rit) {
236 gchar *text = popup_get_name(ft);
237
238 /* measure */
239 p->a_text->texture[0].data.text.string = text;
240 maxwidth = MAX(maxwidth, RrMinWidth(p->a_text));
241
242 if (!create_targets)
243 g_free(text);
244 else {
245 ObFocusCyclePopupTarget *t =
246 g_new(ObFocusCyclePopupTarget, 1);
247
248 t->client = ft;
249 t->text = text;
250 t->icon = client_icon(t->client);
251 RrImageRef(t->icon); /* own the icon so it won't go away */
252 t->win = create_window(p->bg, 0, 0, NULL);
253
254 XMapWindow(ob_display, t->win);
255
256 p->targets = g_list_prepend(p->targets, t);
257 ++n;
258
259 change = TRUE; /* added a window */
260 }
261 }
262 }
263 }
264
265 if (rtargets) {
266 change = TRUE; /* removed a window */
267
268 while (rtargets) {
269 popup_target_free(rtargets->data);
270 rtargets = g_list_delete_link(rtargets, rtargets);
271
272 popup.targets = g_list_delete_link(popup.targets, popup.targets);
273 }
274 }
275
276 p->n_targets = n;
277 p->maxtextw = maxwidth;
278
279 return change;
280 }
281
282 static void popup_cleanup(void)
283 {
284 while(popup.targets) {
285 popup_target_free(popup.targets->data);
286 popup.targets = g_list_delete_link(popup.targets, popup.targets);
287 }
288 popup.n_targets = 0;
289 popup.last_target = NULL;
290 }
291
292 static gchar *popup_get_name(ObClient *c)
293 {
294 ObClient *p;
295 gchar *title;
296 const gchar *desk = NULL;
297 gchar *ret;
298
299 /* find our highest direct parent */
300 p = client_search_top_direct_parent(c);
301
302 if (c->desktop != DESKTOP_ALL && c->desktop != screen_desktop)
303 desk = screen_desktop_names[c->desktop];
304
305 title = c->iconic ? c->icon_title : c->title;
306
307 /* use the transient's parent's title/icon if we don't have one */
308 if (p != c && title[0] == '\0')
309 title = p->iconic ? p->icon_title : p->title;
310
311 if (desk)
312 ret = g_strdup_printf("%s [%s]", title, desk);
313 else
314 ret = g_strdup(title);
315
316 return ret;
317 }
318
319 static void popup_render(ObFocusCyclePopup *p, const ObClient *c)
320 {
321 gint ml, mt, mr, mb;
322 gint l, t, r, b;
323 gint x, y, w, h;
324 Rect *screen_area = NULL;
325 gint icons_per_row;
326 gint icon_rows;
327 gint textx, texty, textw, texth;
328 gint rgbax, rgbay, rgbaw, rgbah;
329 gint icons_center_x;
330 gint innerw, innerh;
331 gint i;
332 GList *it;
333 const ObFocusCyclePopupTarget *newtarget;
334 gint newtargetx, newtargety;
335
336 screen_area = screen_physical_area_primary(FALSE);
337
338 /* get the outside margins */
339 RrMargins(p->a_bg, &ml, &mt, &mr, &mb);
340
341 /* get our outside borders */
342 l = ml + OUTSIDE_BORDER;
343 r = mr + OUTSIDE_BORDER;
344 t = mt + OUTSIDE_BORDER;
345 b = mb + OUTSIDE_BORDER;
346
347 /* get the icon pictures' sizes */
348 innerw = ICON_SIZE - (ICON_HILITE_WIDTH + ICON_HILITE_MARGIN) * 2;
349 innerh = ICON_SIZE - (ICON_HILITE_WIDTH + ICON_HILITE_MARGIN) * 2;
350
351 /* get the width from the text and keep it within limits */
352 w = l + r + p->maxtextw;
353 w = MIN(w, MAX(screen_area->width/3, POPUP_WIDTH)); /* max width */
354 w = MAX(w, POPUP_WIDTH); /* min width */
355
356 /* how many icons will fit in that row? make the width fit that */
357 w -= l + r;
358 icons_per_row = (w + ICON_SIZE - 1) / ICON_SIZE;
359 w = icons_per_row * ICON_SIZE + l + r;
360
361 /* how many rows do we need? */
362 icon_rows = (p->n_targets-1) / icons_per_row + 1;
363
364 /* get the text dimensions */
365 textw = w - l - r;
366 texth = RrMinHeight(p->a_text) + TEXT_BORDER * 2;
367
368 /* find the height of the dialog */
369 h = t + b + (icon_rows * ICON_SIZE) + (OUTSIDE_BORDER + texth);
370
371 /* get the position of the text */
372 textx = l;
373 texty = h - texth - b;
374
375 /* find the position for the popup (include the outer borders) */
376 x = screen_area->x + (screen_area->width -
377 (w + ob_rr_theme->obwidth * 2)) / 2;
378 y = screen_area->y + (screen_area->height -
379 (h + ob_rr_theme->obwidth * 2)) / 2;
380
381 /* get the dimensions of the target hilite texture */
382 rgbax = ml;
383 rgbay = mt;
384 rgbaw = w - ml - mr;
385 rgbah = h - mt - mb;
386
387 /* center the icons if there is less than one row */
388 if (icon_rows == 1)
389 icons_center_x = (w - p->n_targets * ICON_SIZE) / 2;
390 else
391 icons_center_x = 0;
392
393 if (!p->mapped) {
394 /* position the background but don't draw it*/
395 XMoveResizeWindow(ob_display, p->bg, x, y, w, h);
396
397 /* set up the hilite texture for the background */
398 p->a_bg->texture[0].data.rgba.width = rgbaw;
399 p->a_bg->texture[0].data.rgba.height = rgbah;
400 p->a_bg->texture[0].data.rgba.alpha = 0xff;
401 p->hilite_rgba = g_new(RrPixel32, rgbaw * rgbah);
402 p->a_bg->texture[0].data.rgba.data = p->hilite_rgba;
403
404 /* position the text, but don't draw it */
405 XMoveResizeWindow(ob_display, p->text, textx, texty, textw, texth);
406 p->a_text->surface.parentx = textx;
407 p->a_text->surface.parenty = texty;
408 }
409
410 /* find the focused target */
411 for (i = 0, it = p->targets; it; ++i, it = g_list_next(it)) {
412 const ObFocusCyclePopupTarget *target = it->data;
413 const gint row = i / icons_per_row; /* starting from 0 */
414 const gint col = i % icons_per_row; /* starting from 0 */
415
416 if (target->client == c) {
417 /* save the target */
418 newtarget = target;
419 newtargetx = icons_center_x + l + (col * ICON_SIZE);
420 newtargety = t + (row * ICON_SIZE);
421
422 if (!p->mapped)
423 break; /* if we're not dimensioning, then we're done */
424 }
425 }
426
427 g_assert(newtarget != NULL);
428
429 /* create the hilite under the target icon */
430 {
431 RrPixel32 color;
432 gint i, j, o;
433
434 color = ((ob_rr_theme->osd_color->r & 0xff) << RrDefaultRedOffset) +
435 ((ob_rr_theme->osd_color->g & 0xff) << RrDefaultGreenOffset) +
436 ((ob_rr_theme->osd_color->b & 0xff) << RrDefaultBlueOffset);
437
438 o = 0;
439 for (i = 0; i < rgbah; ++i)
440 for (j = 0; j < rgbaw; ++j) {
441 guchar a;
442 const gint x = j + rgbax - newtargetx;
443 const gint y = i + rgbay - newtargety;
444
445 if (x < 0 || x >= ICON_SIZE ||
446 y < 0 || y >= ICON_SIZE)
447 {
448 /* outside the target */
449 a = 0x00;
450 }
451 else if (x < ICON_HILITE_WIDTH ||
452 x >= ICON_SIZE - ICON_HILITE_WIDTH ||
453 y < ICON_HILITE_WIDTH ||
454 y >= ICON_SIZE - ICON_HILITE_WIDTH)
455 {
456 /* the border of the target */
457 a = 0x88;
458 }
459 else {
460 /* the background of the target */
461 a = 0x22;
462 }
463
464 p->hilite_rgba[o++] =
465 color + (a << RrDefaultAlphaOffset);
466 }
467 }
468
469 /* * * draw everything * * */
470
471 /* draw the background */
472 RrPaint(p->a_bg, p->bg, w, h);
473
474 /* draw the icons */
475 for (i = 0, it = p->targets; it; ++i, it = g_list_next(it)) {
476 const ObFocusCyclePopupTarget *target = it->data;
477
478 /* have to redraw the targetted icon and last targetted icon,
479 they can pick up the hilite changes in the backgroud */
480 if (!p->mapped || newtarget == target || p->last_target == target) {
481 const gint row = i / icons_per_row; /* starting from 0 */
482 const gint col = i % icons_per_row; /* starting from 0 */
483 gint innerx, innery;
484
485 /* find the dimensions of the icon inside it */
486 innerx = icons_center_x + l + (col * ICON_SIZE);
487 innerx += ICON_HILITE_WIDTH + ICON_HILITE_MARGIN;
488 innery = t + (row * ICON_SIZE);
489 innery += ICON_HILITE_WIDTH + ICON_HILITE_MARGIN;
490
491 /* move the icon */
492 XMoveResizeWindow(ob_display, target->win,
493 innerx, innery, innerw, innerh);
494
495 /* get the icon from the client */
496 p->a_icon->texture[0].data.image.alpha =
497 target->client->iconic ? OB_ICONIC_ALPHA : 0xff;
498 p->a_icon->texture[0].data.image.image = target->icon;
499
500 /* draw the icon */
501 p->a_icon->surface.parentx = innerx;
502 p->a_icon->surface.parenty = innery;
503 RrPaint(p->a_icon, target->win, innerw, innerh);
504 }
505 }
506
507 /* draw the text */
508 p->a_text->texture[0].data.text.string = newtarget->text;
509 p->a_text->surface.parentx = textx;
510 p->a_text->surface.parenty = texty;
511 RrPaint(p->a_text, p->text, textw, texth);
512
513 p->last_target = newtarget;
514
515 g_free(screen_area);
516 }
517
518 void focus_cycle_popup_show(ObClient *c, gboolean iconic_windows,
519 gboolean all_desktops, gboolean dock_windows,
520 gboolean desktop_windows)
521 {
522 g_assert(c != NULL);
523
524 /* do this stuff only when the dialog is first showing */
525 if (!popup.mapped)
526 popup_setup(&popup, TRUE, FALSE, iconic_windows, all_desktops,
527 dock_windows, desktop_windows);
528 g_assert(popup.targets != NULL);
529
530 popup_render(&popup, c);
531
532 if (!popup.mapped) {
533 /* show the dialog */
534 XMapWindow(ob_display, popup.bg);
535 XFlush(ob_display);
536 popup.mapped = TRUE;
537 screen_hide_desktop_popup();
538 }
539 }
540
541 void focus_cycle_popup_hide(void)
542 {
543 gulong ignore_start;
544
545 ignore_start = event_start_ignore_all_enters();
546
547 XUnmapWindow(ob_display, popup.bg);
548 XFlush(ob_display);
549
550 event_end_ignore_all_enters(ignore_start);
551
552 popup.mapped = FALSE;
553
554 popup_cleanup();
555
556 g_free(popup.hilite_rgba);
557 popup.hilite_rgba = NULL;
558 }
559
560 void focus_cycle_popup_single_show(struct _ObClient *c,
561 gboolean iconic_windows,
562 gboolean all_desktops,
563 gboolean dock_windows,
564 gboolean desktop_windows)
565 {
566 gchar *text;
567
568 g_assert(c != NULL);
569
570 /* do this stuff only when the dialog is first showing */
571 if (!popup.mapped) {
572 Rect *a;
573
574 popup_setup(&popup, FALSE, FALSE, iconic_windows, all_desktops,
575 dock_windows, desktop_windows);
576 g_assert(popup.targets == NULL);
577
578 /* position the popup */
579 a = screen_physical_area_primary(FALSE);
580 icon_popup_position(single_popup, CenterGravity,
581 a->x + a->width / 2, a->y + a->height / 2);
582 icon_popup_height(single_popup, POPUP_HEIGHT);
583 icon_popup_min_width(single_popup, POPUP_WIDTH);
584 icon_popup_max_width(single_popup, MAX(a->width/3, POPUP_WIDTH));
585 icon_popup_text_width(single_popup, popup.maxtextw);
586 g_free(a);
587 }
588
589 text = popup_get_name(c);
590 icon_popup_show(single_popup, text, client_icon(c));
591 g_free(text);
592 screen_hide_desktop_popup();
593 }
594
595 void focus_cycle_popup_single_hide(void)
596 {
597 icon_popup_hide(single_popup);
598 }
599
600 GList* focus_cycle_popup_is_showing(ObClient *client)
601 {
602 if (popup.mapped) {
603 GList *it;
604
605 for (it = popup.targets; it; it = g_list_next(it)) {
606 ObFocusCyclePopupTarget *t = it->data;
607 if (t->client == client)
608 return it;
609 }
610 }
611 return NULL;
612 }
613
614 ObClient* focus_cycle_popup_revert(ObClient *target)
615 {
616 GList *it;
617
618 if (!popup.mapped) return NULL;
619
620 for (it = popup.targets; it; it = g_list_next(it)) {
621 ObFocusCyclePopupTarget *t = it->data;
622 if (t->client == target) {
623 if (it->prev)
624 return ((ObFocusCyclePopupTarget*)it->prev->data)->client;
625 else if (it->next)
626 return ((ObFocusCyclePopupTarget*)it->next->data)->client;
627 else
628 return NULL;
629 }
630 }
631 g_assert_not_reached();
632 }
633
634 void focus_cycle_popup_refresh(ObClient *target,
635 gboolean iconic_windows,
636 gboolean all_desktops,
637 gboolean dock_windows,
638 gboolean desktop_windows)
639 {
640 if (!popup.mapped) return;
641
642 if (popup_setup(&popup, TRUE, TRUE, iconic_windows, all_desktops,
643 dock_windows, desktop_windows))
644 {
645 popup.mapped = FALSE;
646 popup_render(&popup, target);
647 XFlush(ob_display);
648 popup.mapped = TRUE;
649 }
650 }
This page took 0.073043 seconds and 4 git commands to generate.