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"
29 #include "render/render.h"
34 /* Size of the icons, which can appear inside or outside of a hilite box */
35 #define ICON_SIZE (gint)config_theme_window_list_icon_size
36 /* Size of the hilite box around a window's icon */
37 #define HILITE_SIZE (ICON_SIZE + 2*HILITE_OFFSET)
38 /* Width of the outer ring around the hilite box */
39 #define HILITE_WIDTH 2
40 /* Space between the outer ring around the hilite box and the icon inside it */
41 #define HILITE_MARGIN 1
42 /* Total distance from the edge of the hilite box to the icon inside it */
43 #define HILITE_OFFSET (HILITE_WIDTH + HILITE_MARGIN)
44 /* Margin area around the outside of the dialog */
45 #define OUTSIDE_BORDER 3
46 /* Margin area around the text */
48 /* Scroll the list-mode list when the cursor gets within this many rows of the
50 #define SCROLL_MARGIN 4
52 typedef struct _ObFocusCyclePopup ObFocusCyclePopup;
53 typedef struct _ObFocusCyclePopupTarget ObFocusCyclePopupTarget;
55 struct _ObFocusCyclePopupTarget
61 /* This is used when the popup is in list mode */
65 struct _ObFocusCyclePopup
70 /* This is used when the popup is in icon mode */
71 Window icon_mode_text;
74 Window list_mode_down;
79 const ObFocusCyclePopupTarget *last_target;
83 /* How are the list is scrolled, in scroll mode */
88 RrAppearance *a_hilite_text;
90 RrAppearance *a_arrow;
93 ObFocusCyclePopupMode mode;
96 /*! This popup shows all possible windows */
97 static ObFocusCyclePopup popup;
98 /*! This popup shows a single window */
99 static ObIconPopup *single_popup;
101 static gchar *popup_get_name (ObClient *c);
102 static void popup_setup (ObFocusCyclePopup *p,
103 gboolean create_targets,
104 gboolean iconic_windows,
105 gboolean all_desktops,
106 gboolean dock_windows,
107 gboolean desktop_windows);
108 static void popup_render (ObFocusCyclePopup *p,
111 static Window create_window(Window parent, guint bwidth, gulong mask,
112 XSetWindowAttributes *attr)
114 return XCreateWindow(obt_display, parent, 0, 0, 1, 1, bwidth,
115 RrDepth(ob_rr_inst), InputOutput,
116 RrVisual(ob_rr_inst), mask, attr);
119 void focus_cycle_popup_startup(gboolean reconfig)
121 XSetWindowAttributes attrib;
124 single_popup = icon_popup_new();
126 popup.obwin.type = OB_WINDOW_CLASS_INTERNAL;
127 popup.a_bg = RrAppearanceCopy(ob_rr_theme->osd_hilite_bg);
128 popup.a_hilite_text = RrAppearanceCopy(ob_rr_theme->osd_hilite_label);
129 popup.a_text = RrAppearanceCopy(ob_rr_theme->a_unfocused_label);
130 popup.a_icon = RrAppearanceCopy(ob_rr_theme->a_clear);
131 popup.a_arrow = RrAppearanceCopy(ob_rr_theme->a_clear_tex);
133 popup.a_hilite_text->surface.parent = popup.a_bg;
134 popup.a_text->surface.parent = popup.a_bg;
135 popup.a_icon->surface.parent = popup.a_bg;
137 popup.a_text->texture[0].data.text.justify = RR_JUSTIFY_LEFT;
138 popup.a_hilite_text->texture[0].data.text.justify = RR_JUSTIFY_LEFT;
140 /* 2 textures. texture[0] is the icon. texture[1] is the hilight, and
141 may or may not be used */
142 RrAppearanceAddTextures(popup.a_icon, 2);
144 RrAppearanceClearTextures(popup.a_icon);
145 popup.a_icon->texture[0].type = RR_TEXTURE_IMAGE;
147 RrAppearanceClearTextures(popup.a_arrow);
148 popup.a_arrow->texture[0].type = RR_TEXTURE_MASK;
149 popup.a_arrow->texture[0].data.mask.color =
150 ob_rr_theme->osd_color;
152 attrib.override_redirect = True;
153 attrib.border_pixel=RrColorPixel(ob_rr_theme->osd_border_color);
154 popup.bg = create_window(obt_root(ob_screen), ob_rr_theme->obwidth,
155 CWOverrideRedirect | CWBorderPixel, &attrib);
157 /* create the text window used for the icon-mode popup */
158 popup.icon_mode_text = create_window(popup.bg, 0, 0, NULL);
160 /* create the windows for the up and down arrows */
161 popup.list_mode_up = create_window(popup.bg, 0, 0, NULL);
162 popup.list_mode_down = create_window(popup.bg, 0, 0, NULL);
164 popup.targets = NULL;
166 popup.last_target = NULL;
168 /* set up the hilite texture for the icon */
169 popup.a_icon->texture[1].data.rgba.width = HILITE_SIZE;
170 popup.a_icon->texture[1].data.rgba.height = HILITE_SIZE;
171 popup.a_icon->texture[1].data.rgba.alpha = 0xff;
172 p = g_new(RrPixel32, HILITE_SIZE * HILITE_SIZE);
173 popup.a_icon->texture[1].data.rgba.data = p;
175 /* create the hilite under the target icon */
180 color = ((ob_rr_theme->osd_color->r & 0xff) << RrDefaultRedOffset) +
181 ((ob_rr_theme->osd_color->g & 0xff) << RrDefaultGreenOffset) +
182 ((ob_rr_theme->osd_color->b & 0xff) << RrDefaultBlueOffset);
185 for (x = 0; x < HILITE_SIZE; x++)
186 for (y = 0; y < HILITE_SIZE; y++) {
189 if (x < HILITE_WIDTH ||
190 x >= HILITE_SIZE - HILITE_WIDTH ||
192 y >= HILITE_SIZE - HILITE_WIDTH)
194 /* the border of the target */
197 /* the background of the target */
201 p[o++] = color + (a << RrDefaultAlphaOffset);
205 stacking_add(INTERNAL_AS_WINDOW(&popup));
206 window_add(&popup.bg, INTERNAL_AS_WINDOW(&popup));
209 void focus_cycle_popup_shutdown(gboolean reconfig)
211 icon_popup_free(single_popup);
213 window_remove(popup.bg);
214 stacking_remove(INTERNAL_AS_WINDOW(&popup));
216 while(popup.targets) {
217 ObFocusCyclePopupTarget *t = popup.targets->data;
219 RrImageUnref(t->icon);
221 XDestroyWindow(obt_display, t->iconwin);
222 XDestroyWindow(obt_display, t->textwin);
225 popup.targets = g_list_delete_link(popup.targets, popup.targets);
228 g_free(popup.a_icon->texture[1].data.rgba.data);
229 popup.a_icon->texture[1].data.rgba.data = NULL;
231 XDestroyWindow(obt_display, popup.list_mode_up);
232 XDestroyWindow(obt_display, popup.list_mode_down);
233 XDestroyWindow(obt_display, popup.icon_mode_text);
234 XDestroyWindow(obt_display, popup.bg);
236 RrAppearanceFree(popup.a_arrow);
237 RrAppearanceFree(popup.a_icon);
238 RrAppearanceFree(popup.a_hilite_text);
239 RrAppearanceFree(popup.a_text);
240 RrAppearanceFree(popup.a_bg);
243 static void popup_setup(ObFocusCyclePopup *p, gboolean create_targets,
244 gboolean iconic_windows, gboolean all_desktops,
245 gboolean dock_windows, gboolean desktop_windows)
250 g_assert(p->targets == NULL);
251 g_assert(p->n_targets == 0);
253 /* make its width to be the width of all the possible titles */
255 /* build a list of all the valid focus targets and measure their strings,
259 for (it = g_list_last(focus_order); it; it = g_list_previous(it)) {
260 ObClient *ft = it->data;
262 if (focus_valid_target(ft, TRUE,
269 gchar *text = popup_get_name(ft);
272 p->a_text->texture[0].data.text.string = text;
273 maxwidth = MAX(maxwidth, RrMinWidth(p->a_text));
275 if (!create_targets) {
278 ObFocusCyclePopupTarget *t = g_new(ObFocusCyclePopupTarget, 1);
282 t->icon = client_icon(t->client);
283 RrImageRef(t->icon); /* own the icon so it won't go away */
284 t->iconwin = create_window(p->bg, 0, 0, NULL);
285 t->textwin = create_window(p->bg, 0, 0, NULL);
287 p->targets = g_list_prepend(p->targets, t);
294 p->maxtextw = maxwidth;
297 static gchar *popup_get_name(ObClient *c)
301 const gchar *desk = NULL;
304 /* find our highest direct parent */
305 p = client_search_top_direct_parent(c);
307 if (c->desktop != DESKTOP_ALL && c->desktop != screen_desktop)
308 desk = screen_desktop_names[c->desktop];
310 title = c->iconic ? c->icon_title : c->title;
312 /* use the transient's parent's title/icon if we don't have one */
313 if (p != c && title[0] == '\0')
314 title = p->iconic ? p->icon_title : p->title;
317 ret = g_strdup_printf("%s [%s]", title, desk);
319 ret = g_strdup(title);
324 static void popup_render(ObFocusCyclePopup *p, const ObClient *c)
329 Rect *screen_area = NULL;
332 const ObFocusCyclePopupTarget *newtarget;
333 ObFocusCyclePopupMode mode = p->mode;
340 /* vars for icon mode */
341 gint icon_mode_textx;
342 gint icon_mode_texty;
345 /* vars for list mode */
346 gint list_mode_icon_column_w = HILITE_SIZE + OUTSIDE_BORDER;
347 gint up_arrow_x, down_arrow_x;
348 gint up_arrow_y, down_arrow_y;
349 gboolean showing_arrows = FALSE;
351 g_assert(mode == OB_FOCUS_CYCLE_POPUP_MODE_ICONS ||
352 mode == OB_FOCUS_CYCLE_POPUP_MODE_LIST);
354 screen_area = screen_physical_area_primary(FALSE);
356 /* get the outside margins */
357 RrMargins(p->a_bg, &ml, &mt, &mr, &mb);
359 /* get our outside borders */
360 l = ml + OUTSIDE_BORDER;
361 r = mr + OUTSIDE_BORDER;
362 t = mt + OUTSIDE_BORDER;
363 b = mb + OUTSIDE_BORDER;
365 /* get the width from the text and keep it within limits */
366 w = l + r + p->maxtextw;
367 if (mode == OB_FOCUS_CYCLE_POPUP_MODE_LIST)
368 /* when in list mode, there are icons down the side */
369 w += list_mode_icon_column_w;
370 w = MIN(w, MAX(screen_area->width/3, POPUP_WIDTH)); /* max width */
371 w = MAX(w, POPUP_WIDTH); /* min width */
373 /* get the text height */
374 texth = RrMinHeight(p->a_hilite_text);
375 if (mode == OB_FOCUS_CYCLE_POPUP_MODE_LIST)
376 texth = MAX(MAX(texth, RrMinHeight(p->a_text)), HILITE_SIZE);
378 texth += TEXT_BORDER * 2;
380 if (mode == OB_FOCUS_CYCLE_POPUP_MODE_ICONS) {
381 /* how many icons will fit in that row? make the width fit that */
383 icons_per_row = (w + HILITE_SIZE - 1) / HILITE_SIZE;
384 w = icons_per_row * HILITE_SIZE + l + r;
386 /* how many rows do we need? */
387 icon_rows = (p->n_targets-1) / icons_per_row + 1;
389 /* in list mode, there is one column of icons.. */
391 /* maximum is 80% of the screen height */
392 icon_rows = MIN(p->n_targets,
393 (4*screen_area->height/5) /* 80% of the screen */
395 MAX(HILITE_SIZE, texth)); /* height of each row */
396 /* but make sure there is always one */
397 icon_rows = MAX(icon_rows, 1);
400 /* get the text width */
402 if (mode == OB_FOCUS_CYCLE_POPUP_MODE_LIST)
403 /* leave space on the side for the icons */
404 textw -= list_mode_icon_column_w;
407 /* reset the scrolling when the dialog is first shown */
410 /* find the height of the dialog */
411 h = t + b + (icon_rows * MAX(HILITE_SIZE, texth));
412 if (mode == OB_FOCUS_CYCLE_POPUP_MODE_ICONS)
413 /* in icon mode the text sits below the icons, so make some space */
414 h += OUTSIDE_BORDER + texth;
416 /* find the focused target */
418 for (i = 0, it = p->targets; it; ++i, it = g_list_next(it)) {
419 const ObFocusCyclePopupTarget *target = it->data;
420 if (target->client == c) {
421 /* save the target */
427 g_assert(newtarget != NULL);
429 /* scroll the list if needed */
430 last_scroll = p->scroll;
431 if (mode == OB_FOCUS_CYCLE_POPUP_MODE_LIST) {
432 const gint top = p->scroll + SCROLL_MARGIN;
433 const gint bottom = p->scroll + icon_rows - SCROLL_MARGIN;
434 const gint min_scroll = 0;
435 const gint max_scroll = p->n_targets - icon_rows;
437 if (top - selected_pos >= 0) {
438 p->scroll -= top - selected_pos + 1;
439 p->scroll = MAX(p->scroll, min_scroll);
440 } else if (selected_pos - bottom >= 0) {
441 p->scroll += selected_pos - bottom + 1;
442 p->scroll = MIN(p->scroll, max_scroll);
446 /* show the scroll arrows when appropriate */
447 if (p->scroll && mode == OB_FOCUS_CYCLE_POPUP_MODE_LIST) {
448 XMapWindow(obt_display, p->list_mode_up);
449 showing_arrows = TRUE;
451 XUnmapWindow(obt_display, p->list_mode_up);
453 if (p->scroll < p->n_targets - icon_rows &&
454 mode == OB_FOCUS_CYCLE_POPUP_MODE_LIST)
456 XMapWindow(obt_display, p->list_mode_down);
457 showing_arrows = TRUE;
459 XUnmapWindow(obt_display, p->list_mode_down);
461 /* make space for the arrows */
463 h += ob_rr_theme->up_arrow_mask->height + OUTSIDE_BORDER
464 + ob_rr_theme->down_arrow_mask->height + OUTSIDE_BORDER;
466 /* center the icons if there is less than one row */
467 if (mode == OB_FOCUS_CYCLE_POPUP_MODE_ICONS && icon_rows == 1)
468 icons_center_x = (w - p->n_targets * HILITE_SIZE) / 2;
472 if (mode == OB_FOCUS_CYCLE_POPUP_MODE_ICONS) {
473 /* get the position of the text */
475 icon_mode_texty = h - texth - b;
478 /* find the position for the popup (include the outer borders) */
479 x = screen_area->x + (screen_area->width -
480 (w + ob_rr_theme->obwidth * 2)) / 2;
481 y = screen_area->y + (screen_area->height -
482 (h + ob_rr_theme->obwidth * 2)) / 2;
485 /* position the background but don't draw it */
486 XMoveResizeWindow(obt_display, p->bg, x, y, w, h);
488 if (mode == OB_FOCUS_CYCLE_POPUP_MODE_ICONS) {
489 /* position the text */
490 XMoveResizeWindow(obt_display, p->icon_mode_text,
491 icon_mode_textx, icon_mode_texty, textw, texth);
492 XMapWindow(obt_display, popup.icon_mode_text);
494 XUnmapWindow(obt_display, popup.icon_mode_text);
496 up_arrow_x = (w - ob_rr_theme->up_arrow_mask->width) / 2;
499 down_arrow_x = (w - ob_rr_theme->down_arrow_mask->width) / 2;
500 down_arrow_y = h - b - ob_rr_theme->down_arrow_mask->height;
502 /* position the arrows */
503 XMoveResizeWindow(obt_display, p->list_mode_up,
504 up_arrow_x, up_arrow_y,
505 ob_rr_theme->up_arrow_mask->width,
506 ob_rr_theme->up_arrow_mask->height);
507 XMoveResizeWindow(obt_display, p->list_mode_down,
508 down_arrow_x, down_arrow_y,
509 ob_rr_theme->down_arrow_mask->width,
510 ob_rr_theme->down_arrow_mask->height);
514 /* * * draw everything * * */
516 /* draw the background */
518 RrPaint(p->a_bg, p->bg, w, h);
520 /* draw the scroll arrows */
521 if (!p->mapped && mode == OB_FOCUS_CYCLE_POPUP_MODE_LIST) {
522 p->a_arrow->texture[0].data.mask.mask =
523 ob_rr_theme->up_arrow_mask;
524 p->a_arrow->surface.parent = p->a_bg;
525 p->a_arrow->surface.parentx = up_arrow_x;
526 p->a_arrow->surface.parenty = up_arrow_y;
527 RrPaint(p->a_arrow, p->list_mode_up,
528 ob_rr_theme->up_arrow_mask->width,
529 ob_rr_theme->up_arrow_mask->height);
531 p->a_arrow->texture[0].data.mask.mask =
532 ob_rr_theme->down_arrow_mask;
533 p->a_arrow->surface.parent = p->a_bg;
534 p->a_arrow->surface.parentx = down_arrow_x;
535 p->a_arrow->surface.parenty = down_arrow_y;
536 RrPaint(p->a_arrow, p->list_mode_down,
537 ob_rr_theme->down_arrow_mask->width,
538 ob_rr_theme->down_arrow_mask->height);
541 /* draw the icons and text */
542 for (i = 0, it = p->targets; it; ++i, it = g_list_next(it)) {
543 const ObFocusCyclePopupTarget *target = it->data;
545 /* have to redraw the targetted icon and last targetted icon
546 * to update the hilite */
547 if (!p->mapped || newtarget == target || p->last_target == target ||
548 last_scroll != p->scroll)
550 /* row and column start from 0 */
551 const gint row = i / icons_per_row - p->scroll;
552 const gint col = i % icons_per_row;
554 gint list_mode_textx, list_mode_texty;
557 /* find the coordinates for the icon */
558 iconx = icons_center_x + l + (col * HILITE_SIZE);
559 icony = t + (showing_arrows ? ob_rr_theme->up_arrow_mask->height
562 + (row * MAX(texth, HILITE_SIZE))
563 + MAX(texth - HILITE_SIZE, 0) / 2;
565 /* find the dimensions of the text box */
566 list_mode_textx = iconx + HILITE_SIZE + TEXT_BORDER;
567 list_mode_texty = icony;
569 /* position the icon */
570 XMoveResizeWindow(obt_display, target->iconwin,
571 iconx, icony, HILITE_SIZE, HILITE_SIZE);
573 /* position the text */
574 if (mode == OB_FOCUS_CYCLE_POPUP_MODE_LIST)
575 XMoveResizeWindow(obt_display, target->textwin,
576 list_mode_textx, list_mode_texty,
579 /* show/hide the right windows */
580 if (row >= 0 && row < icon_rows) {
581 XMapWindow(obt_display, target->iconwin);
582 if (mode == OB_FOCUS_CYCLE_POPUP_MODE_LIST)
583 XMapWindow(obt_display, target->textwin);
585 XUnmapWindow(obt_display, target->textwin);
587 XUnmapWindow(obt_display, target->textwin);
588 if (mode == OB_FOCUS_CYCLE_POPUP_MODE_LIST)
589 XUnmapWindow(obt_display, target->iconwin);
591 XMapWindow(obt_display, target->iconwin);
594 /* get the icon from the client */
595 p->a_icon->texture[0].data.image.twidth = ICON_SIZE;
596 p->a_icon->texture[0].data.image.theight = ICON_SIZE;
597 p->a_icon->texture[0].data.image.tx = HILITE_OFFSET;
598 p->a_icon->texture[0].data.image.ty = HILITE_OFFSET;
599 p->a_icon->texture[0].data.image.alpha =
600 target->client->iconic ? OB_ICONIC_ALPHA : 0xff;
601 p->a_icon->texture[0].data.image.image = target->icon;
603 /* Draw the hilite? */
604 p->a_icon->texture[1].type = (target == newtarget) ?
605 RR_TEXTURE_RGBA : RR_TEXTURE_NONE;
608 p->a_icon->surface.parentx = iconx;
609 p->a_icon->surface.parenty = icony;
610 RrPaint(p->a_icon, target->iconwin, HILITE_SIZE, HILITE_SIZE);
613 if (mode == OB_FOCUS_CYCLE_POPUP_MODE_LIST ||
616 text = (target == newtarget) ? p->a_hilite_text : p->a_text;
617 text->texture[0].data.text.string = target->text;
618 text->surface.parentx =
619 mode == OB_FOCUS_CYCLE_POPUP_MODE_ICONS ?
620 icon_mode_textx : list_mode_textx;
621 text->surface.parenty =
622 mode == OB_FOCUS_CYCLE_POPUP_MODE_ICONS ?
623 icon_mode_texty : list_mode_texty;
625 (mode == OB_FOCUS_CYCLE_POPUP_MODE_ICONS ?
626 p->icon_mode_text : target->textwin),
632 p->last_target = newtarget;
639 void focus_cycle_popup_show(ObClient *c, gboolean iconic_windows,
640 gboolean all_desktops, gboolean dock_windows,
641 gboolean desktop_windows,
642 ObFocusCyclePopupMode mode)
646 if (mode == OB_FOCUS_CYCLE_POPUP_MODE_NONE) {
647 focus_cycle_popup_hide();
651 /* do this stuff only when the dialog is first showing */
653 popup_setup(&popup, TRUE, iconic_windows, all_desktops,
654 dock_windows, desktop_windows);
655 /* this is fixed once the dialog is shown */
658 g_assert(popup.targets != NULL);
660 popup_render(&popup, c);
663 /* show the dialog */
664 XMapWindow(obt_display, popup.bg);
667 screen_hide_desktop_popup();
671 void focus_cycle_popup_hide(void)
675 ignore_start = event_start_ignore_all_enters();
677 XUnmapWindow(obt_display, popup.bg);
680 event_end_ignore_all_enters(ignore_start);
682 popup.mapped = FALSE;
684 while(popup.targets) {
685 ObFocusCyclePopupTarget *t = popup.targets->data;
687 RrImageUnref(t->icon);
689 XDestroyWindow(obt_display, t->iconwin);
690 XDestroyWindow(obt_display, t->textwin);
693 popup.targets = g_list_delete_link(popup.targets, popup.targets);
696 popup.last_target = NULL;
699 void focus_cycle_popup_single_show(struct _ObClient *c,
700 gboolean iconic_windows,
701 gboolean all_desktops,
702 gboolean dock_windows,
703 gboolean desktop_windows)
709 /* do this stuff only when the dialog is first showing */
713 popup_setup(&popup, FALSE, iconic_windows, all_desktops,
714 dock_windows, desktop_windows);
715 g_assert(popup.targets == NULL);
717 /* position the popup */
718 a = screen_physical_area_primary(FALSE);
719 icon_popup_position(single_popup, CenterGravity,
720 a->x + a->width / 2, a->y + a->height / 2);
721 icon_popup_height(single_popup, POPUP_HEIGHT);
722 icon_popup_min_width(single_popup, POPUP_WIDTH);
723 icon_popup_max_width(single_popup, MAX(a->width/3, POPUP_WIDTH));
724 icon_popup_text_width(single_popup, popup.maxtextw);
728 text = popup_get_name(c);
729 icon_popup_show(single_popup, text, client_icon(c));
731 screen_hide_desktop_popup();
734 void focus_cycle_popup_single_hide(void)
736 icon_popup_hide(single_popup);