1 /* -*- indent-tabs-mode: nil; tab-width: 4; c-basic-offset: 4; -*-
3 focus_cycle_popup.c for the Openbox window manager
4 Copyright (c) 2006 Mikael Magnusson
5 Copyright (c) 2003-2007 Dana Jansens
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.
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.
17 See the COPYING file for a copy of the GNU General Public License.
20 #include "focus_cycle_popup.h"
28 #include "render/render.h"
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 */
47 /* Scroll the list-mode list when the cursor gets within this many rows of the
49 #define SCROLL_MARGIN 4
51 typedef struct _ObFocusCyclePopup ObFocusCyclePopup;
52 typedef struct _ObFocusCyclePopupTarget ObFocusCyclePopupTarget;
54 struct _ObFocusCyclePopupTarget
59 /* This is used when the popup is in list mode */
63 struct _ObFocusCyclePopup
68 /* This is used when the popup is in icon mode */
69 Window icon_mode_text;
74 const ObFocusCyclePopupTarget *last_target;
78 /* How are the list is scrolled, in scroll mode */
83 RrAppearance *a_hilite_text;
87 ObFocusCyclePopupMode mode;
90 /*! This popup shows all possible windows */
91 static ObFocusCyclePopup popup;
92 /*! This popup shows a single window */
93 static ObIconPopup *single_popup;
95 static gchar *popup_get_name (ObClient *c);
96 static void popup_setup (ObFocusCyclePopup *p,
97 gboolean create_targets,
98 gboolean iconic_windows,
99 gboolean all_desktops,
100 gboolean dock_windows,
101 gboolean desktop_windows);
102 static void popup_render (ObFocusCyclePopup *p,
105 static Window create_window(Window parent, guint bwidth, gulong mask,
106 XSetWindowAttributes *attr)
108 return XCreateWindow(obt_display, parent, 0, 0, 1, 1, bwidth,
109 RrDepth(ob_rr_inst), InputOutput,
110 RrVisual(ob_rr_inst), mask, attr);
113 void focus_cycle_popup_startup(gboolean reconfig)
115 XSetWindowAttributes attrib;
118 single_popup = icon_popup_new();
120 popup.obwin.type = OB_WINDOW_CLASS_INTERNAL;
121 popup.a_bg = RrAppearanceCopy(ob_rr_theme->osd_hilite_bg);
122 popup.a_hilite_text = RrAppearanceCopy(ob_rr_theme->osd_hilite_label);
123 popup.a_text = RrAppearanceCopy(ob_rr_theme->a_unfocused_label);
124 popup.a_icon = RrAppearanceCopy(ob_rr_theme->a_clear);
126 popup.a_hilite_text->surface.parent = popup.a_bg;
127 popup.a_text->surface.parent = popup.a_bg;
128 popup.a_icon->surface.parent = popup.a_bg;
130 popup.a_text->texture[0].data.text.justify = RR_JUSTIFY_LEFT;
131 popup.a_hilite_text->texture[0].data.text.justify = RR_JUSTIFY_LEFT;
133 /* 2 textures. texture[0] is the icon. texture[1] is the hilight, and
134 may or may not be used */
135 RrAppearanceAddTextures(popup.a_icon, 2);
137 popup.a_icon->texture[0].type = RR_TEXTURE_RGBA;
139 attrib.override_redirect = True;
140 attrib.border_pixel=RrColorPixel(ob_rr_theme->osd_border_color);
141 popup.bg = create_window(obt_root(ob_screen), ob_rr_theme->obwidth,
142 CWOverrideRedirect | CWBorderPixel, &attrib);
144 /* create the text window used for the icon-mode popup */
145 popup.icon_mode_text = create_window(popup.bg, 0, 0, NULL);
147 popup.targets = NULL;
149 popup.last_target = NULL;
151 /* set up the hilite texture for the icon */
152 popup.a_icon->texture[1].data.rgba.width = HILITE_SIZE;
153 popup.a_icon->texture[1].data.rgba.height = HILITE_SIZE;
154 popup.a_icon->texture[1].data.rgba.alpha = 0xff;
155 p = g_new(RrPixel32, HILITE_SIZE * HILITE_SIZE);
156 popup.a_icon->texture[1].data.rgba.data = p;
158 /* create the hilite under the target icon */
163 color = ((ob_rr_theme->osd_color->r & 0xff) << RrDefaultRedOffset) +
164 ((ob_rr_theme->osd_color->g & 0xff) << RrDefaultGreenOffset) +
165 ((ob_rr_theme->osd_color->b & 0xff) << RrDefaultBlueOffset);
168 for (x = 0; x < HILITE_SIZE; x++)
169 for (y = 0; y < HILITE_SIZE; y++) {
172 if (x < HILITE_WIDTH ||
173 x >= HILITE_SIZE - HILITE_WIDTH ||
175 y >= HILITE_SIZE - HILITE_WIDTH)
177 /* the border of the target */
181 /* the background of the target */
185 p[o++] = color + (a << RrDefaultAlphaOffset);
189 stacking_add(INTERNAL_AS_WINDOW(&popup));
190 window_add(&popup.bg, INTERNAL_AS_WINDOW(&popup));
193 void focus_cycle_popup_shutdown(gboolean reconfig)
195 icon_popup_free(single_popup);
197 window_remove(popup.bg);
198 stacking_remove(INTERNAL_AS_WINDOW(&popup));
200 while(popup.targets) {
201 ObFocusCyclePopupTarget *t = popup.targets->data;
204 XDestroyWindow(obt_display, t->iconwin);
205 XDestroyWindow(obt_display, t->textwin);
207 popup.targets = g_list_delete_link(popup.targets, popup.targets);
210 g_free(popup.a_icon->texture[1].data.rgba.data);
211 popup.a_icon->texture[1].data.rgba.data = NULL;
213 XDestroyWindow(obt_display, popup.icon_mode_text);
214 XDestroyWindow(obt_display, popup.bg);
216 RrAppearanceFree(popup.a_icon);
217 RrAppearanceFree(popup.a_hilite_text);
218 RrAppearanceFree(popup.a_text);
219 RrAppearanceFree(popup.a_bg);
222 static void popup_setup(ObFocusCyclePopup *p, gboolean create_targets,
223 gboolean iconic_windows, gboolean all_desktops,
224 gboolean dock_windows, gboolean desktop_windows)
229 g_assert(p->targets == NULL);
230 g_assert(p->n_targets == 0);
232 /* make its width to be the width of all the possible titles */
234 /* build a list of all the valid focus targets and measure their strings,
238 for (it = g_list_last(focus_order); it; it = g_list_previous(it)) {
239 ObClient *ft = it->data;
241 if (focus_valid_target(ft, TRUE,
247 gchar *text = popup_get_name(ft);
250 p->a_text->texture[0].data.text.string = text;
251 maxwidth = MAX(maxwidth, RrMinWidth(p->a_text));
256 ObFocusCyclePopupTarget *t = g_new(ObFocusCyclePopupTarget, 1);
260 t->iconwin = create_window(p->bg, 0, 0, NULL);
261 t->textwin = create_window(p->bg, 0, 0, NULL);
263 p->targets = g_list_prepend(p->targets, t);
270 p->maxtextw = maxwidth;
273 static gchar *popup_get_name(ObClient *c)
277 const gchar *desk = NULL;
280 /* find our highest direct parent */
281 p = client_search_top_direct_parent(c);
283 if (c->desktop != DESKTOP_ALL && c->desktop != screen_desktop)
284 desk = screen_desktop_names[c->desktop];
286 title = c->iconic ? c->icon_title : c->title;
288 /* use the transient's parent's title/icon if we don't have one */
289 if (p != c && title[0] == '\0')
290 title = p->iconic ? p->icon_title : p->title;
293 ret = g_strdup_printf("%s [%s]", title, desk);
295 ret = g_strdup(title);
300 static void popup_render(ObFocusCyclePopup *p, const ObClient *c)
305 Rect *screen_area = NULL;
306 gint rgbax, rgbay, rgbaw, rgbah;
309 const ObFocusCyclePopupTarget *newtarget;
316 /* vars for icon mode */
317 gint icon_mode_textx;
318 gint icon_mode_texty;
321 /* vars for list mode */
322 gint list_mode_icon_column_w = HILITE_SIZE + OUTSIDE_BORDER;
324 g_assert(p->mode == OB_FOCUS_CYCLE_POPUP_MODE_ICONS ||
325 p->mode == OB_FOCUS_CYCLE_POPUP_MODE_LIST);
327 screen_area = screen_physical_area_active();
329 /* get the outside margins */
330 RrMargins(p->a_bg, &ml, &mt, &mr, &mb);
332 /* get our outside borders */
333 l = ml + OUTSIDE_BORDER;
334 r = mr + OUTSIDE_BORDER;
335 t = mt + OUTSIDE_BORDER;
336 b = mb + OUTSIDE_BORDER;
338 /* get the width from the text and keep it within limits */
339 w = l + r + p->maxtextw;
340 if (p->mode == OB_FOCUS_CYCLE_POPUP_MODE_LIST)
341 /* when in list mode, there are icons down the side */
342 w += list_mode_icon_column_w;
343 w = MIN(w, MAX(screen_area->width/3, POPUP_WIDTH)); /* max width */
344 w = MAX(w, POPUP_WIDTH); /* min width */
346 /* get the text height */
347 texth = RrMinHeight(p->a_hilite_text);
348 if (p->mode == OB_FOCUS_CYCLE_POPUP_MODE_LIST)
349 texth = MAX(MAX(texth, RrMinHeight(p->a_text)), ICON_SIZE);
351 texth += TEXT_BORDER * 2;
353 if (p->mode == OB_FOCUS_CYCLE_POPUP_MODE_ICONS) {
354 /* how many icons will fit in that row? make the width fit that */
356 icons_per_row = (w + HILITE_SIZE - 1) / HILITE_SIZE;
357 w = icons_per_row * HILITE_SIZE + l + r;
359 /* how many rows do we need? */
360 icon_rows = (p->n_targets-1) / icons_per_row + 1;
364 /* in list mode, there is one column of icons.. */
366 /* maximum is 80% of the screen height */
367 icon_rows = MIN(p->n_targets,
368 (4*screen_area->height/5) /* 80% of the screen */
370 MAX(HILITE_SIZE, texth)); /* height of each row */
373 /* get the text width */
375 if (p->mode == OB_FOCUS_CYCLE_POPUP_MODE_LIST)
376 /* leave space on the side for the icons */
377 textw -= list_mode_icon_column_w;
379 /* find the height of the dialog */
380 h = t + b + (icon_rows * MAX(HILITE_SIZE, texth));
381 if (p->mode == OB_FOCUS_CYCLE_POPUP_MODE_ICONS)
382 /* in icon mode the text sits below the icons, so make some space */
383 h += OUTSIDE_BORDER + texth;
385 /* center the icons if there is less than one row */
387 icons_center_x = (w - p->n_targets * HILITE_SIZE) / 2;
391 if (p->mode == OB_FOCUS_CYCLE_POPUP_MODE_ICONS) {
392 /* get the position of the text */
394 icon_mode_texty = h - texth - b;
397 /* find the position for the popup (include the outer borders) */
398 x = screen_area->x + (screen_area->width -
399 (w + ob_rr_theme->obwidth * 2)) / 2;
400 y = screen_area->y + (screen_area->height -
401 (h + ob_rr_theme->obwidth * 2)) / 2;
403 /* get the dimensions of the target hilite texture */
410 /* position the background but don't draw it */
411 XMoveResizeWindow(obt_display, p->bg, x, y, w, h);
413 if (p->mode == OB_FOCUS_CYCLE_POPUP_MODE_ICONS) {
414 /* position the text */
415 XMoveResizeWindow(obt_display, p->icon_mode_text,
416 icon_mode_textx, icon_mode_texty, textw, texth);
417 XMapWindow(obt_display, popup.icon_mode_text);
420 XUnmapWindow(obt_display, popup.icon_mode_text);
422 /* reset the scrolling when the dialog is first shown */
426 /* find the focused target */
428 for (i = 0, it = p->targets; it; ++i, it = g_list_next(it)) {
429 const ObFocusCyclePopupTarget *target = it->data;
430 if (target->client == c) {
431 /* save the target */
438 /* scroll the list if needed */
439 last_scroll = p->scroll;
440 if (p->mode == OB_FOCUS_CYCLE_POPUP_MODE_LIST) {
441 const gint top = p->scroll + SCROLL_MARGIN;
442 const gint bottom = p->scroll + icon_rows - SCROLL_MARGIN;
443 const gint min_scroll = 0;
444 const gint max_scroll = p->n_targets - icon_rows;
446 if (top - selected_pos >= 0) {
447 p->scroll -= top - selected_pos + 1;
448 p->scroll = MAX(p->scroll, min_scroll);
450 else if (selected_pos - bottom >= 0) {
451 p->scroll += selected_pos - bottom + 1;
452 p->scroll = MIN(p->scroll, max_scroll);
456 g_assert(newtarget != NULL);
458 /* * * draw everything * * */
460 /* draw the background */
462 RrPaint(p->a_bg, p->bg, w, h);
464 /* draw the icons and text */
465 for (i = 0, it = p->targets; it; ++i, it = g_list_next(it)) {
466 const ObFocusCyclePopupTarget *target = it->data;
468 /* have to redraw the targetted icon and last targetted icon
469 * to update the hilite */
470 if (!p->mapped || newtarget == target || p->last_target == target ||
471 last_scroll != p->scroll)
473 /* row and column start from 0 */
474 const gint row = i / icons_per_row - p->scroll;
475 const gint col = i % icons_per_row;
476 const ObClientIcon *icon;
478 gint list_mode_textx, list_mode_texty;
481 /* find the coordinates for the icon */
482 iconx = icons_center_x + l + (col * HILITE_SIZE);
483 icony = t + (row * MAX(texth, HILITE_SIZE))
484 + MAX(texth - HILITE_SIZE, 0) / 2;
486 /* find the dimensions of the text box */
487 list_mode_textx = iconx + HILITE_SIZE + TEXT_BORDER;
488 list_mode_texty = icony + HILITE_OFFSET;
490 /* position the icon */
491 XMoveResizeWindow(obt_display, target->iconwin,
492 iconx, icony, HILITE_SIZE, HILITE_SIZE);
494 /* position the text */
495 if (p->mode == OB_FOCUS_CYCLE_POPUP_MODE_LIST)
496 XMoveResizeWindow(obt_display, target->textwin,
497 list_mode_textx, list_mode_texty,
500 /* show/hide the right windows */
501 if (row >= 0 && row < icon_rows) {
502 XMapWindow(obt_display, target->iconwin);
503 if (p->mode == OB_FOCUS_CYCLE_POPUP_MODE_LIST)
504 XMapWindow(obt_display, target->textwin);
506 XUnmapWindow(obt_display, target->textwin);
509 XUnmapWindow(obt_display, target->textwin);
510 if (p->mode == OB_FOCUS_CYCLE_POPUP_MODE_LIST)
511 XUnmapWindow(obt_display, target->iconwin);
513 XMapWindow(obt_display, target->iconwin);
516 /* only draw if we have to */
518 newtarget == target || p->last_target == target ||
519 /* wasn't visible before... */
520 ((row + p->scroll < last_scroll ||
521 row + p->scroll >= last_scroll + icon_rows) &&
522 /* ...and is visible now */
523 (row >= 0 && row < icon_rows)))
525 /* get the icon from the client */
526 icon = client_icon(target->client, ICON_SIZE, ICON_SIZE);
527 p->a_icon->texture[0].data.rgba.width = icon->width;
528 p->a_icon->texture[0].data.rgba.height = icon->height;
529 p->a_icon->texture[0].data.rgba.twidth = ICON_SIZE;
530 p->a_icon->texture[0].data.rgba.theight = ICON_SIZE;
531 p->a_icon->texture[0].data.rgba.tx = HILITE_OFFSET;
532 p->a_icon->texture[0].data.rgba.ty = HILITE_OFFSET;
533 p->a_icon->texture[0].data.rgba.alpha =
534 target->client->iconic ? OB_ICONIC_ALPHA : 0xff;
535 p->a_icon->texture[0].data.rgba.data = icon->data;
537 /* Draw the hilite? */
538 p->a_icon->texture[1].type = (target == newtarget) ?
539 RR_TEXTURE_RGBA : RR_TEXTURE_NONE;
542 p->a_icon->surface.parentx = iconx;
543 p->a_icon->surface.parenty = icony;
544 RrPaint(p->a_icon, target->iconwin, HILITE_SIZE, HILITE_SIZE);
547 if (p->mode == OB_FOCUS_CYCLE_POPUP_MODE_LIST ||
550 text = (target == newtarget) ? p->a_hilite_text : p->a_text;
551 text->texture[0].data.text.string = target->text;
552 text->surface.parentx =
553 p->mode == OB_FOCUS_CYCLE_POPUP_MODE_ICONS ?
554 icon_mode_textx : list_mode_textx;
555 text->surface.parenty =
556 p->mode == OB_FOCUS_CYCLE_POPUP_MODE_ICONS ?
557 icon_mode_texty : list_mode_texty;
559 (p->mode == OB_FOCUS_CYCLE_POPUP_MODE_ICONS ?
560 p->icon_mode_text : target->textwin),
567 p->last_target = newtarget;
574 void focus_cycle_popup_show(ObClient *c, gboolean iconic_windows,
575 gboolean all_desktops, gboolean dock_windows,
576 gboolean desktop_windows,
577 ObFocusCyclePopupMode mode)
581 if (mode == OB_FOCUS_CYCLE_POPUP_MODE_NONE) {
582 focus_cycle_popup_hide();
586 /* do this stuff only when the dialog is first showing */
588 popup_setup(&popup, TRUE, iconic_windows, all_desktops,
589 dock_windows, desktop_windows);
590 /* this is fixed once the dialog is shown */
593 g_assert(popup.targets != NULL);
595 popup_render(&popup, c);
598 /* show the dialog */
599 XMapWindow(obt_display, popup.bg);
602 screen_hide_desktop_popup();
606 void focus_cycle_popup_hide(void)
610 ignore_start = event_start_ignore_all_enters();
612 XUnmapWindow(obt_display, popup.bg);
615 event_end_ignore_all_enters(ignore_start);
617 popup.mapped = FALSE;
619 while(popup.targets) {
620 ObFocusCyclePopupTarget *t = popup.targets->data;
623 XDestroyWindow(obt_display, t->iconwin);
624 XDestroyWindow(obt_display, t->textwin);
627 popup.targets = g_list_delete_link(popup.targets, popup.targets);
630 popup.last_target = NULL;
633 void focus_cycle_popup_single_show(struct _ObClient *c,
634 gboolean iconic_windows,
635 gboolean all_desktops,
636 gboolean dock_windows,
637 gboolean desktop_windows)
643 /* do this stuff only when the dialog is first showing */
647 popup_setup(&popup, FALSE, iconic_windows, all_desktops,
648 dock_windows, desktop_windows);
649 g_assert(popup.targets == NULL);
651 /* position the popup */
652 a = screen_physical_area_active();
653 icon_popup_position(single_popup, CenterGravity,
654 a->x + a->width / 2, a->y + a->height / 2);
655 icon_popup_height(single_popup, POPUP_HEIGHT);
656 icon_popup_min_width(single_popup, POPUP_WIDTH);
657 icon_popup_max_width(single_popup, MAX(a->width/3, POPUP_WIDTH));
658 icon_popup_text_width(single_popup, popup.maxtextw);
662 text = popup_get_name(c);
663 icon_popup_show(single_popup, text, client_icon(c, HILITE_SIZE,
666 screen_hide_desktop_popup();
669 void focus_cycle_popup_single_hide(void)
671 icon_popup_hide(single_popup);