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
60 /* This is used when the popup is in list mode */
64 struct _ObFocusCyclePopup
69 /* This is used when the popup is in icon mode */
70 Window icon_mode_text;
73 Window list_mode_down;
78 const ObFocusCyclePopupTarget *last_target;
82 /* How are the list is scrolled, in scroll mode */
87 RrAppearance *a_hilite_text;
89 RrAppearance *a_arrow;
92 ObFocusCyclePopupMode mode;
95 /*! This popup shows all possible windows */
96 static ObFocusCyclePopup popup;
97 /*! This popup shows a single window */
98 static ObIconPopup *single_popup;
100 static gchar *popup_get_name (ObClient *c);
101 static void popup_setup (ObFocusCyclePopup *p,
102 gboolean create_targets,
103 gboolean iconic_windows,
104 gboolean all_desktops,
105 gboolean dock_windows,
106 gboolean desktop_windows);
107 static void popup_render (ObFocusCyclePopup *p,
110 static Window create_window(Window parent, guint bwidth, gulong mask,
111 XSetWindowAttributes *attr)
113 return XCreateWindow(obt_display, parent, 0, 0, 1, 1, bwidth,
114 RrDepth(ob_rr_inst), InputOutput,
115 RrVisual(ob_rr_inst), mask, attr);
118 void focus_cycle_popup_startup(gboolean reconfig)
120 XSetWindowAttributes attrib;
123 single_popup = icon_popup_new();
125 popup.obwin.type = OB_WINDOW_CLASS_INTERNAL;
126 popup.a_bg = RrAppearanceCopy(ob_rr_theme->osd_hilite_bg);
127 popup.a_hilite_text = RrAppearanceCopy(ob_rr_theme->osd_hilite_label);
128 popup.a_text = RrAppearanceCopy(ob_rr_theme->a_unfocused_label);
129 popup.a_icon = RrAppearanceCopy(ob_rr_theme->a_clear);
130 popup.a_arrow = RrAppearanceCopy(ob_rr_theme->a_clear_tex);
132 popup.a_hilite_text->surface.parent = popup.a_bg;
133 popup.a_text->surface.parent = popup.a_bg;
134 popup.a_icon->surface.parent = popup.a_bg;
136 popup.a_text->texture[0].data.text.justify = RR_JUSTIFY_LEFT;
137 popup.a_hilite_text->texture[0].data.text.justify = RR_JUSTIFY_LEFT;
139 /* 2 textures. texture[0] is the icon. texture[1] is the hilight, and
140 may or may not be used */
141 RrAppearanceAddTextures(popup.a_icon, 2);
143 RrAppearanceClearTextures(popup.a_icon);
144 popup.a_icon->texture[0].type = RR_TEXTURE_IMAGE;
146 RrAppearanceClearTextures(popup.a_arrow);
147 popup.a_arrow->texture[0].type = RR_TEXTURE_MASK;
148 popup.a_arrow->texture[0].data.mask.color =
149 ob_rr_theme->osd_color;
151 attrib.override_redirect = True;
152 attrib.border_pixel=RrColorPixel(ob_rr_theme->osd_border_color);
153 popup.bg = create_window(obt_root(ob_screen), ob_rr_theme->obwidth,
154 CWOverrideRedirect | CWBorderPixel, &attrib);
156 /* create the text window used for the icon-mode popup */
157 popup.icon_mode_text = create_window(popup.bg, 0, 0, NULL);
159 /* create the windows for the up and down arrows */
160 popup.list_mode_up = create_window(popup.bg, 0, 0, NULL);
161 popup.list_mode_down = create_window(popup.bg, 0, 0, NULL);
163 popup.targets = NULL;
165 popup.last_target = NULL;
167 /* set up the hilite texture for the icon */
168 popup.a_icon->texture[1].data.rgba.width = HILITE_SIZE;
169 popup.a_icon->texture[1].data.rgba.height = HILITE_SIZE;
170 popup.a_icon->texture[1].data.rgba.alpha = 0xff;
171 p = g_new(RrPixel32, HILITE_SIZE * HILITE_SIZE);
172 popup.a_icon->texture[1].data.rgba.data = p;
174 /* create the hilite under the target icon */
179 color = ((ob_rr_theme->osd_color->r & 0xff) << RrDefaultRedOffset) +
180 ((ob_rr_theme->osd_color->g & 0xff) << RrDefaultGreenOffset) +
181 ((ob_rr_theme->osd_color->b & 0xff) << RrDefaultBlueOffset);
184 for (x = 0; x < HILITE_SIZE; x++)
185 for (y = 0; y < HILITE_SIZE; y++) {
188 if (x < HILITE_WIDTH ||
189 x >= HILITE_SIZE - HILITE_WIDTH ||
191 y >= HILITE_SIZE - HILITE_WIDTH)
193 /* the border of the target */
196 /* the background of the target */
200 p[o++] = color + (a << RrDefaultAlphaOffset);
204 stacking_add(INTERNAL_AS_WINDOW(&popup));
205 window_add(&popup.bg, INTERNAL_AS_WINDOW(&popup));
208 void focus_cycle_popup_shutdown(gboolean reconfig)
210 icon_popup_free(single_popup);
212 window_remove(popup.bg);
213 stacking_remove(INTERNAL_AS_WINDOW(&popup));
215 while(popup.targets) {
216 ObFocusCyclePopupTarget *t = popup.targets->data;
218 RrImageUnref(t->icon);
220 XDestroyWindow(obt_display, t->iconwin);
221 XDestroyWindow(obt_display, t->textwin);
224 popup.targets = g_list_delete_link(popup.targets, popup.targets);
227 g_free(popup.a_icon->texture[1].data.rgba.data);
228 popup.a_icon->texture[1].data.rgba.data = NULL;
230 XDestroyWindow(obt_display, popup.list_mode_up);
231 XDestroyWindow(obt_display, popup.list_mode_down);
232 XDestroyWindow(obt_display, popup.icon_mode_text);
233 XDestroyWindow(obt_display, popup.bg);
235 RrAppearanceFree(popup.a_arrow);
236 RrAppearanceFree(popup.a_icon);
237 RrAppearanceFree(popup.a_hilite_text);
238 RrAppearanceFree(popup.a_text);
239 RrAppearanceFree(popup.a_bg);
242 static void popup_setup(ObFocusCyclePopup *p, gboolean create_targets,
243 gboolean iconic_windows, gboolean all_desktops,
244 gboolean dock_windows, gboolean desktop_windows)
249 g_assert(p->targets == NULL);
250 g_assert(p->n_targets == 0);
252 /* make its width to be the width of all the possible titles */
254 /* build a list of all the valid focus targets and measure their strings,
258 for (it = g_list_last(focus_order); it; it = g_list_previous(it)) {
259 ObClient *ft = it->data;
261 if (focus_valid_target(ft, TRUE,
267 gchar *text = popup_get_name(ft);
270 p->a_text->texture[0].data.text.string = text;
271 maxwidth = MAX(maxwidth, RrMinWidth(p->a_text));
273 if (!create_targets) {
276 ObFocusCyclePopupTarget *t = g_new(ObFocusCyclePopupTarget, 1);
280 t->icon = client_icon(t->client);
281 RrImageRef(t->icon); /* own the icon so it won't go away */
282 t->iconwin = create_window(p->bg, 0, 0, NULL);
283 t->textwin = create_window(p->bg, 0, 0, NULL);
285 p->targets = g_list_prepend(p->targets, t);
292 p->maxtextw = maxwidth;
295 static gchar *popup_get_name(ObClient *c)
299 const gchar *desk = NULL;
302 /* find our highest direct parent */
303 p = client_search_top_direct_parent(c);
305 if (c->desktop != DESKTOP_ALL && c->desktop != screen_desktop)
306 desk = screen_desktop_names[c->desktop];
308 title = c->iconic ? c->icon_title : c->title;
310 /* use the transient's parent's title/icon if we don't have one */
311 if (p != c && title[0] == '\0')
312 title = p->iconic ? p->icon_title : p->title;
315 ret = g_strdup_printf("%s [%s]", title, desk);
317 ret = g_strdup(title);
322 static void popup_render(ObFocusCyclePopup *p, const ObClient *c)
327 Rect *screen_area = NULL;
328 gint rgbax, rgbay, rgbaw, rgbah;
331 const ObFocusCyclePopupTarget *newtarget;
338 /* vars for icon mode */
339 gint icon_mode_textx;
340 gint icon_mode_texty;
343 /* vars for list mode */
344 gint list_mode_icon_column_w = HILITE_SIZE + OUTSIDE_BORDER;
345 gint up_arrow_x, down_arrow_x;
346 gint up_arrow_y, down_arrow_y;
347 gboolean showing_arrows = FALSE;
349 g_assert(p->mode == OB_FOCUS_CYCLE_POPUP_MODE_ICONS ||
350 p->mode == OB_FOCUS_CYCLE_POPUP_MODE_LIST);
352 screen_area = screen_physical_area_active();
354 /* get the outside margins */
355 RrMargins(p->a_bg, &ml, &mt, &mr, &mb);
357 /* get our outside borders */
358 l = ml + OUTSIDE_BORDER;
359 r = mr + OUTSIDE_BORDER;
360 t = mt + OUTSIDE_BORDER;
361 b = mb + OUTSIDE_BORDER;
363 /* get the width from the text and keep it within limits */
364 w = l + r + p->maxtextw;
365 if (p->mode == OB_FOCUS_CYCLE_POPUP_MODE_LIST)
366 /* when in list mode, there are icons down the side */
367 w += list_mode_icon_column_w;
368 w = MIN(w, MAX(screen_area->width/3, POPUP_WIDTH)); /* max width */
369 w = MAX(w, POPUP_WIDTH); /* min width */
371 /* get the text height */
372 texth = RrMinHeight(p->a_hilite_text);
373 if (p->mode == OB_FOCUS_CYCLE_POPUP_MODE_LIST)
374 texth = MAX(MAX(texth, RrMinHeight(p->a_text)), HILITE_SIZE);
376 texth += TEXT_BORDER * 2;
378 if (p->mode == OB_FOCUS_CYCLE_POPUP_MODE_ICONS) {
379 /* how many icons will fit in that row? make the width fit that */
381 icons_per_row = (w + HILITE_SIZE - 1) / HILITE_SIZE;
382 w = icons_per_row * HILITE_SIZE + l + r;
384 /* how many rows do we need? */
385 icon_rows = (p->n_targets-1) / icons_per_row + 1;
387 /* in list mode, there is one column of icons.. */
389 /* maximum is 80% of the screen height */
390 icon_rows = MIN(p->n_targets,
391 (4*screen_area->height/5) /* 80% of the screen */
393 MAX(HILITE_SIZE, texth)); /* height of each row */
394 /* but make sure there is always one */
395 icon_rows = MAX(icon_rows, 1);
398 /* get the text width */
400 if (p->mode == OB_FOCUS_CYCLE_POPUP_MODE_LIST)
401 /* leave space on the side for the icons */
402 textw -= list_mode_icon_column_w;
405 /* reset the scrolling when the dialog is first shown */
408 /* find the height of the dialog */
409 h = t + b + (icon_rows * MAX(HILITE_SIZE, texth));
410 if (p->mode == OB_FOCUS_CYCLE_POPUP_MODE_ICONS)
411 /* in icon mode the text sits below the icons, so make some space */
412 h += OUTSIDE_BORDER + texth;
414 /* find the focused target */
416 for (i = 0, it = p->targets; it; ++i, it = g_list_next(it)) {
417 const ObFocusCyclePopupTarget *target = it->data;
418 if (target->client == c) {
419 /* save the target */
425 g_assert(newtarget != NULL);
427 /* scroll the list if needed */
428 last_scroll = p->scroll;
429 if (p->mode == OB_FOCUS_CYCLE_POPUP_MODE_LIST) {
430 const gint top = p->scroll + SCROLL_MARGIN;
431 const gint bottom = p->scroll + icon_rows - SCROLL_MARGIN;
432 const gint min_scroll = 0;
433 const gint max_scroll = p->n_targets - icon_rows;
435 if (top - selected_pos >= 0) {
436 p->scroll -= top - selected_pos + 1;
437 p->scroll = MAX(p->scroll, min_scroll);
438 } else if (selected_pos - bottom >= 0) {
439 p->scroll += selected_pos - bottom + 1;
440 p->scroll = MIN(p->scroll, max_scroll);
444 /* show the scroll arrows when appropriate */
445 if (p->scroll && p->mode == OB_FOCUS_CYCLE_POPUP_MODE_LIST) {
446 XMapWindow(obt_display, p->list_mode_up);
447 showing_arrows = TRUE;
449 XUnmapWindow(obt_display, p->list_mode_up);
451 if (p->scroll < p->n_targets - icon_rows &&
452 p->mode == OB_FOCUS_CYCLE_POPUP_MODE_LIST)
454 XMapWindow(obt_display, p->list_mode_down);
455 showing_arrows = TRUE;
457 XUnmapWindow(obt_display, p->list_mode_down);
459 /* make space for the arrows */
461 h += ob_rr_theme->up_arrow_mask->height + OUTSIDE_BORDER
462 + ob_rr_theme->down_arrow_mask->height + OUTSIDE_BORDER;
464 /* center the icons if there is less than one row */
465 if (p->mode == OB_FOCUS_CYCLE_POPUP_MODE_ICONS && icon_rows == 1)
466 icons_center_x = (w - p->n_targets * HILITE_SIZE) / 2;
470 if (p->mode == OB_FOCUS_CYCLE_POPUP_MODE_ICONS) {
471 /* get the position of the text */
473 icon_mode_texty = h - texth - b;
476 /* find the position for the popup (include the outer borders) */
477 x = screen_area->x + (screen_area->width -
478 (w + ob_rr_theme->obwidth * 2)) / 2;
479 y = screen_area->y + (screen_area->height -
480 (h + ob_rr_theme->obwidth * 2)) / 2;
482 /* get the dimensions of the target hilite texture */
489 /* position the background but don't draw it */
490 XMoveResizeWindow(obt_display, p->bg, x, y, w, h);
492 if (p->mode == OB_FOCUS_CYCLE_POPUP_MODE_ICONS) {
493 /* position the text */
494 XMoveResizeWindow(obt_display, p->icon_mode_text,
495 icon_mode_textx, icon_mode_texty, textw, texth);
496 XMapWindow(obt_display, popup.icon_mode_text);
498 XUnmapWindow(obt_display, popup.icon_mode_text);
500 up_arrow_x = (w - ob_rr_theme->up_arrow_mask->width) / 2;
503 down_arrow_x = (w - ob_rr_theme->down_arrow_mask->width) / 2;
504 down_arrow_y = h - b - ob_rr_theme->down_arrow_mask->height;
506 /* position the arrows */
507 XMoveResizeWindow(obt_display, p->list_mode_up,
508 up_arrow_x, up_arrow_y,
509 ob_rr_theme->up_arrow_mask->width,
510 ob_rr_theme->up_arrow_mask->height);
511 XMoveResizeWindow(obt_display, p->list_mode_down,
512 down_arrow_x, down_arrow_y,
513 ob_rr_theme->down_arrow_mask->width,
514 ob_rr_theme->down_arrow_mask->height);
518 /* * * draw everything * * */
520 /* draw the background */
522 RrPaint(p->a_bg, p->bg, w, h);
524 /* draw the scroll arrows */
525 if (!p->mapped && p->mode == OB_FOCUS_CYCLE_POPUP_MODE_LIST) {
526 p->a_arrow->texture[0].data.mask.mask =
527 ob_rr_theme->up_arrow_mask;
528 p->a_arrow->surface.parent = p->a_bg;
529 p->a_arrow->surface.parentx = up_arrow_x;
530 p->a_arrow->surface.parenty = up_arrow_y;
531 RrPaint(p->a_arrow, p->list_mode_up,
532 ob_rr_theme->up_arrow_mask->width,
533 ob_rr_theme->up_arrow_mask->height);
535 p->a_arrow->texture[0].data.mask.mask =
536 ob_rr_theme->down_arrow_mask;
537 p->a_arrow->surface.parent = p->a_bg;
538 p->a_arrow->surface.parentx = down_arrow_x;
539 p->a_arrow->surface.parenty = down_arrow_y;
540 RrPaint(p->a_arrow, p->list_mode_down,
541 ob_rr_theme->down_arrow_mask->width,
542 ob_rr_theme->down_arrow_mask->height);
545 /* draw the icons and text */
546 for (i = 0, it = p->targets; it; ++i, it = g_list_next(it)) {
547 const ObFocusCyclePopupTarget *target = it->data;
549 /* have to redraw the targetted icon and last targetted icon
550 * to update the hilite */
551 if (!p->mapped || newtarget == target || p->last_target == target ||
552 last_scroll != p->scroll)
554 /* row and column start from 0 */
555 const gint row = i / icons_per_row - p->scroll;
556 const gint col = i % icons_per_row;
558 gint list_mode_textx, list_mode_texty;
561 /* find the coordinates for the icon */
562 iconx = icons_center_x + l + (col * HILITE_SIZE);
563 icony = t + (showing_arrows ? ob_rr_theme->up_arrow_mask->height
566 + (row * MAX(texth, HILITE_SIZE))
567 + MAX(texth - HILITE_SIZE, 0) / 2;
569 /* find the dimensions of the text box */
570 list_mode_textx = iconx + HILITE_SIZE + TEXT_BORDER;
571 list_mode_texty = icony;
573 /* position the icon */
574 XMoveResizeWindow(obt_display, target->iconwin,
575 iconx, icony, HILITE_SIZE, HILITE_SIZE);
577 /* position the text */
578 if (p->mode == OB_FOCUS_CYCLE_POPUP_MODE_LIST)
579 XMoveResizeWindow(obt_display, target->textwin,
580 list_mode_textx, list_mode_texty,
583 /* show/hide the right windows */
584 if (row >= 0 && row < icon_rows) {
585 XMapWindow(obt_display, target->iconwin);
586 if (p->mode == OB_FOCUS_CYCLE_POPUP_MODE_LIST)
587 XMapWindow(obt_display, target->textwin);
589 XUnmapWindow(obt_display, target->textwin);
591 XUnmapWindow(obt_display, target->textwin);
592 if (p->mode == OB_FOCUS_CYCLE_POPUP_MODE_LIST)
593 XUnmapWindow(obt_display, target->iconwin);
595 XMapWindow(obt_display, target->iconwin);
598 /* get the icon from the client */
599 p->a_icon->texture[0].data.image.twidth = ICON_SIZE;
600 p->a_icon->texture[0].data.image.theight = ICON_SIZE;
601 p->a_icon->texture[0].data.image.tx = HILITE_OFFSET;
602 p->a_icon->texture[0].data.image.ty = HILITE_OFFSET;
603 p->a_icon->texture[0].data.image.alpha =
604 target->client->iconic ? OB_ICONIC_ALPHA : 0xff;
605 p->a_icon->texture[0].data.image.image = target->icon;
607 /* Draw the hilite? */
608 p->a_icon->texture[1].type = (target == newtarget) ?
609 RR_TEXTURE_RGBA : RR_TEXTURE_NONE;
612 p->a_icon->surface.parentx = iconx;
613 p->a_icon->surface.parenty = icony;
614 RrPaint(p->a_icon, target->iconwin, HILITE_SIZE, HILITE_SIZE);
617 if (p->mode == OB_FOCUS_CYCLE_POPUP_MODE_LIST ||
620 text = (target == newtarget) ? p->a_hilite_text : p->a_text;
621 text->texture[0].data.text.string = target->text;
622 text->surface.parentx =
623 p->mode == OB_FOCUS_CYCLE_POPUP_MODE_ICONS ?
624 icon_mode_textx : list_mode_textx;
625 text->surface.parenty =
626 p->mode == OB_FOCUS_CYCLE_POPUP_MODE_ICONS ?
627 icon_mode_texty : list_mode_texty;
629 (p->mode == OB_FOCUS_CYCLE_POPUP_MODE_ICONS ?
630 p->icon_mode_text : target->textwin),
636 p->last_target = newtarget;
643 void focus_cycle_popup_show(ObClient *c, gboolean iconic_windows,
644 gboolean all_desktops, gboolean dock_windows,
645 gboolean desktop_windows,
646 ObFocusCyclePopupMode mode)
650 if (mode == OB_FOCUS_CYCLE_POPUP_MODE_NONE) {
651 focus_cycle_popup_hide();
655 /* do this stuff only when the dialog is first showing */
657 popup_setup(&popup, TRUE, iconic_windows, all_desktops,
658 dock_windows, desktop_windows);
659 /* this is fixed once the dialog is shown */
662 g_assert(popup.targets != NULL);
664 popup_render(&popup, c);
667 /* show the dialog */
668 XMapWindow(obt_display, popup.bg);
671 screen_hide_desktop_popup();
675 void focus_cycle_popup_hide(void)
679 ignore_start = event_start_ignore_all_enters();
681 XUnmapWindow(obt_display, popup.bg);
684 event_end_ignore_all_enters(ignore_start);
686 popup.mapped = FALSE;
688 while(popup.targets) {
689 ObFocusCyclePopupTarget *t = popup.targets->data;
691 RrImageUnref(t->icon);
693 XDestroyWindow(obt_display, t->iconwin);
694 XDestroyWindow(obt_display, t->textwin);
697 popup.targets = g_list_delete_link(popup.targets, popup.targets);
700 popup.last_target = NULL;
703 void focus_cycle_popup_single_show(struct _ObClient *c,
704 gboolean iconic_windows,
705 gboolean all_desktops,
706 gboolean dock_windows,
707 gboolean desktop_windows)
713 /* do this stuff only when the dialog is first showing */
717 popup_setup(&popup, FALSE, iconic_windows, all_desktops,
718 dock_windows, desktop_windows);
719 g_assert(popup.targets == NULL);
721 /* position the popup */
722 a = screen_physical_area_active();
723 icon_popup_position(single_popup, CenterGravity,
724 a->x + a->width / 2, a->y + a->height / 2);
725 icon_popup_height(single_popup, POPUP_HEIGHT);
726 icon_popup_min_width(single_popup, POPUP_WIDTH);
727 icon_popup_max_width(single_popup, MAX(a->width/3, POPUP_WIDTH));
728 icon_popup_text_width(single_popup, popup.maxtextw);
732 text = popup_get_name(c);
733 icon_popup_show(single_popup, text, client_icon(c));
735 screen_hide_desktop_popup();
738 void focus_cycle_popup_single_hide(void)
740 icon_popup_hide(single_popup);