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