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"
21 #include "focus_cycle.h"
29 #include "render/render.h"
35 #define ICON_HILITE_WIDTH 2
36 #define ICON_HILITE_MARGIN 1
37 #define OUTSIDE_BORDER 3
40 typedef struct _ObFocusCyclePopup ObFocusCyclePopup
;
41 typedef struct _ObFocusCyclePopupTarget ObFocusCyclePopupTarget
;
43 struct _ObFocusCyclePopupTarget
51 struct _ObFocusCyclePopup
61 const ObFocusCyclePopupTarget
*last_target
;
69 RrPixel32
*hilite_rgba
;
74 /*! This popup shows all possible windows */
75 static ObFocusCyclePopup popup
;
76 /*! This popup shows a single window */
77 static ObIconPopup
*single_popup
;
79 static gchar
*popup_get_name (ObClient
*c
);
80 static gboolean
popup_setup (ObFocusCyclePopup
*p
,
81 gboolean create_targets
,
82 gboolean refresh_targets
);
83 static void popup_render (ObFocusCyclePopup
*p
,
86 static Window
create_window(Window parent
, guint bwidth
, gulong mask
,
87 XSetWindowAttributes
*attr
)
89 return XCreateWindow(ob_display
, parent
, 0, 0, 1, 1, bwidth
,
90 RrDepth(ob_rr_inst
), InputOutput
,
91 RrVisual(ob_rr_inst
), mask
, attr
);
94 void focus_cycle_popup_startup(gboolean reconfig
)
96 XSetWindowAttributes attrib
;
98 single_popup
= icon_popup_new();
100 popup
.obwin
.type
= Window_Internal
;
101 popup
.a_bg
= RrAppearanceCopy(ob_rr_theme
->osd_hilite_bg
);
102 popup
.a_text
= RrAppearanceCopy(ob_rr_theme
->osd_hilite_label
);
103 popup
.a_icon
= RrAppearanceCopy(ob_rr_theme
->a_clear_tex
);
105 popup
.a_text
->surface
.parent
= popup
.a_bg
;
106 popup
.a_icon
->surface
.parent
= popup
.a_bg
;
108 RrAppearanceClearTextures(popup
.a_icon
);
109 popup
.a_icon
->texture
[0].type
= RR_TEXTURE_IMAGE
;
111 RrAppearanceAddTextures(popup
.a_bg
, 1);
112 popup
.a_bg
->texture
[0].type
= RR_TEXTURE_RGBA
;
114 attrib
.override_redirect
= True
;
115 attrib
.border_pixel
=RrColorPixel(ob_rr_theme
->osd_border_color
);
116 popup
.bg
= create_window(RootWindow(ob_display
, ob_screen
),
117 ob_rr_theme
->obwidth
,
118 CWOverrideRedirect
| CWBorderPixel
, &attrib
);
120 popup
.text
= create_window(popup
.bg
, 0, 0, NULL
);
122 popup
.targets
= NULL
;
124 popup
.last_target
= NULL
;
126 popup
.hilite_rgba
= NULL
;
128 XMapWindow(ob_display
, popup
.text
);
130 stacking_add(INTERNAL_AS_WINDOW(&popup
));
131 g_hash_table_insert(window_map
, &popup
.bg
, &popup
);
134 void focus_cycle_popup_shutdown(gboolean reconfig
)
136 icon_popup_free(single_popup
);
138 g_hash_table_remove(window_map
, &popup
.bg
);
139 stacking_remove(INTERNAL_AS_WINDOW(&popup
));
141 while(popup
.targets
) {
142 ObFocusCyclePopupTarget
*t
= popup
.targets
->data
;
144 RrImageUnref(t
->icon
);
146 XDestroyWindow(ob_display
, t
->win
);
149 popup
.targets
= g_list_delete_link(popup
.targets
, popup
.targets
);
152 g_free(popup
.hilite_rgba
);
153 popup
.hilite_rgba
= NULL
;
155 XDestroyWindow(ob_display
, popup
.text
);
156 XDestroyWindow(ob_display
, popup
.bg
);
158 RrAppearanceFree(popup
.a_icon
);
159 RrAppearanceFree(popup
.a_text
);
160 RrAppearanceFree(popup
.a_bg
);
163 static void popup_target_free(ObFocusCyclePopupTarget
*t
)
165 RrImageUnref(t
->icon
);
167 XDestroyWindow(ob_display
, t
->win
);
171 static gboolean
popup_setup(ObFocusCyclePopup
*p
, gboolean create_targets
,
172 gboolean refresh_targets
)
176 GList
*rtargets
; /* old targets for refresh */
180 if (refresh_targets
) {
181 rtargets
= p
->targets
;
182 rtlast
= g_list_last(rtargets
);
188 rtargets
= rtlast
= NULL
;
192 g_assert(p
->targets
== NULL
);
193 g_assert(p
->n_targets
== 0);
195 /* make its width to be the width of all the possible titles */
197 /* build a list of all the valid focus targets and measure their strings,
201 for (it
= g_list_last(focus_order
); it
; it
= g_list_previous(it
)) {
202 ObClient
*ft
= it
->data
;
204 if (focus_cycle_valid(ft
)) {
207 /* reuse the target if possible during refresh */
208 for (rit
= rtlast
; rit
; rit
= g_list_previous(rit
)) {
209 ObFocusCyclePopupTarget
*t
= rit
->data
;
210 if (t
->client
== ft
) {
212 rtlast
= g_list_previous(rit
);
213 rtargets
= g_list_remove_link(rtargets
, rit
);
215 p
->targets
= g_list_concat(rit
, p
->targets
);
219 change
= TRUE
; /* order changed */
225 gchar
*text
= popup_get_name(ft
);
228 p
->a_text
->texture
[0].data
.text
.string
= text
;
229 maxwidth
= MAX(maxwidth
, RrMinWidth(p
->a_text
));
234 ObFocusCyclePopupTarget
*t
=
235 g_new(ObFocusCyclePopupTarget
, 1);
239 t
->icon
= client_icon(t
->client
);
240 RrImageRef(t
->icon
); /* own the icon so it won't go away */
241 t
->win
= create_window(p
->bg
, 0, 0, NULL
);
243 XMapWindow(ob_display
, t
->win
);
245 p
->targets
= g_list_prepend(p
->targets
, t
);
248 change
= TRUE
; /* added a window */
255 change
= TRUE
; /* removed a window */
258 popup_target_free(rtargets
->data
);
259 rtargets
= g_list_delete_link(rtargets
, rtargets
);
264 p
->maxtextw
= maxwidth
;
269 static void popup_cleanup(void)
271 while(popup
.targets
) {
272 popup_target_free(popup
.targets
->data
);
273 popup
.targets
= g_list_delete_link(popup
.targets
, popup
.targets
);
276 popup
.last_target
= NULL
;
279 static gchar
*popup_get_name(ObClient
*c
)
283 const gchar
*desk
= NULL
;
286 /* find our highest direct parent */
287 p
= client_search_top_direct_parent(c
);
289 if (c
->desktop
!= DESKTOP_ALL
&& c
->desktop
!= screen_desktop
)
290 desk
= screen_desktop_names
[c
->desktop
];
292 title
= c
->iconic
? c
->icon_title
: c
->title
;
294 /* use the transient's parent's title/icon if we don't have one */
295 if (p
!= c
&& title
[0] == '\0')
296 title
= p
->iconic
? p
->icon_title
: p
->title
;
299 ret
= g_strdup_printf("%s [%s]", title
, desk
);
301 ret
= g_strdup(title
);
306 static void popup_render(ObFocusCyclePopup
*p
, const ObClient
*c
)
311 Rect
*screen_area
= NULL
;
314 gint textx
, texty
, textw
, texth
;
315 gint rgbax
, rgbay
, rgbaw
, rgbah
;
320 const ObFocusCyclePopupTarget
*newtarget
;
321 gint newtargetx
, newtargety
;
323 screen_area
= screen_physical_area_primary(FALSE
);
325 /* get the outside margins */
326 RrMargins(p
->a_bg
, &ml
, &mt
, &mr
, &mb
);
328 /* get our outside borders */
329 l
= ml
+ OUTSIDE_BORDER
;
330 r
= mr
+ OUTSIDE_BORDER
;
331 t
= mt
+ OUTSIDE_BORDER
;
332 b
= mb
+ OUTSIDE_BORDER
;
334 /* get the icon pictures' sizes */
335 innerw
= ICON_SIZE
- (ICON_HILITE_WIDTH
+ ICON_HILITE_MARGIN
) * 2;
336 innerh
= ICON_SIZE
- (ICON_HILITE_WIDTH
+ ICON_HILITE_MARGIN
) * 2;
338 /* get the width from the text and keep it within limits */
339 w
= l
+ r
+ p
->maxtextw
;
340 w
= MIN(w
, MAX(screen_area
->width
/3, POPUP_WIDTH
)); /* max width */
341 w
= MAX(w
, POPUP_WIDTH
); /* min width */
343 /* how many icons will fit in that row? make the width fit that */
345 icons_per_row
= (w
+ ICON_SIZE
- 1) / ICON_SIZE
;
346 w
= icons_per_row
* ICON_SIZE
+ l
+ r
;
348 /* how many rows do we need? */
349 icon_rows
= (p
->n_targets
-1) / icons_per_row
+ 1;
351 /* get the text dimensions */
353 texth
= RrMinHeight(p
->a_text
) + TEXT_BORDER
* 2;
355 /* find the height of the dialog */
356 h
= t
+ b
+ (icon_rows
* ICON_SIZE
) + (OUTSIDE_BORDER
+ texth
);
358 /* get the position of the text */
360 texty
= h
- texth
- b
;
362 /* find the position for the popup (include the outer borders) */
363 x
= screen_area
->x
+ (screen_area
->width
-
364 (w
+ ob_rr_theme
->obwidth
* 2)) / 2;
365 y
= screen_area
->y
+ (screen_area
->height
-
366 (h
+ ob_rr_theme
->obwidth
* 2)) / 2;
368 /* get the dimensions of the target hilite texture */
374 /* center the icons if there is less than one row */
376 icons_center_x
= (w
- p
->n_targets
* ICON_SIZE
) / 2;
381 /* position the background but don't draw it*/
382 XMoveResizeWindow(ob_display
, p
->bg
, x
, y
, w
, h
);
384 /* set up the hilite texture for the background */
385 p
->a_bg
->texture
[0].data
.rgba
.width
= rgbaw
;
386 p
->a_bg
->texture
[0].data
.rgba
.height
= rgbah
;
387 p
->a_bg
->texture
[0].data
.rgba
.alpha
= 0xff;
388 p
->hilite_rgba
= g_new(RrPixel32
, rgbaw
* rgbah
);
389 p
->a_bg
->texture
[0].data
.rgba
.data
= p
->hilite_rgba
;
391 /* position the text, but don't draw it */
392 XMoveResizeWindow(ob_display
, p
->text
, textx
, texty
, textw
, texth
);
393 p
->a_text
->surface
.parentx
= textx
;
394 p
->a_text
->surface
.parenty
= texty
;
397 /* find the focused target */
398 for (i
= 0, it
= p
->targets
; it
; ++i
, it
= g_list_next(it
)) {
399 const ObFocusCyclePopupTarget
*target
= it
->data
;
400 const gint row
= i
/ icons_per_row
; /* starting from 0 */
401 const gint col
= i
% icons_per_row
; /* starting from 0 */
403 if (target
->client
== c
) {
404 /* save the target */
406 newtargetx
= icons_center_x
+ l
+ (col
* ICON_SIZE
);
407 newtargety
= t
+ (row
* ICON_SIZE
);
410 break; /* if we're not dimensioning, then we're done */
414 g_assert(newtarget
!= NULL
);
416 /* create the hilite under the target icon */
421 color
= ((ob_rr_theme
->osd_color
->r
& 0xff) << RrDefaultRedOffset
) +
422 ((ob_rr_theme
->osd_color
->g
& 0xff) << RrDefaultGreenOffset
) +
423 ((ob_rr_theme
->osd_color
->b
& 0xff) << RrDefaultBlueOffset
);
426 for (i
= 0; i
< rgbah
; ++i
)
427 for (j
= 0; j
< rgbaw
; ++j
) {
429 const gint x
= j
+ rgbax
- newtargetx
;
430 const gint y
= i
+ rgbay
- newtargety
;
432 if (x
< 0 || x
>= ICON_SIZE
||
433 y
< 0 || y
>= ICON_SIZE
)
435 /* outside the target */
438 else if (x
< ICON_HILITE_WIDTH
||
439 x
>= ICON_SIZE
- ICON_HILITE_WIDTH
||
440 y
< ICON_HILITE_WIDTH
||
441 y
>= ICON_SIZE
- ICON_HILITE_WIDTH
)
443 /* the border of the target */
447 /* the background of the target */
451 p
->hilite_rgba
[o
++] =
452 color
+ (a
<< RrDefaultAlphaOffset
);
456 /* * * draw everything * * */
458 /* draw the background */
459 RrPaint(p
->a_bg
, p
->bg
, w
, h
);
462 for (i
= 0, it
= p
->targets
; it
; ++i
, it
= g_list_next(it
)) {
463 const ObFocusCyclePopupTarget
*target
= it
->data
;
465 /* have to redraw the targetted icon and last targetted icon,
466 they can pick up the hilite changes in the backgroud */
467 if (!p
->mapped
|| newtarget
== target
|| p
->last_target
== target
) {
468 const gint row
= i
/ icons_per_row
; /* starting from 0 */
469 const gint col
= i
% icons_per_row
; /* starting from 0 */
472 /* find the dimensions of the icon inside it */
473 innerx
= icons_center_x
+ l
+ (col
* ICON_SIZE
);
474 innerx
+= ICON_HILITE_WIDTH
+ ICON_HILITE_MARGIN
;
475 innery
= t
+ (row
* ICON_SIZE
);
476 innery
+= ICON_HILITE_WIDTH
+ ICON_HILITE_MARGIN
;
479 XMoveResizeWindow(ob_display
, target
->win
,
480 innerx
, innery
, innerw
, innerh
);
482 /* get the icon from the client */
483 p
->a_icon
->texture
[0].data
.image
.alpha
=
484 target
->client
->iconic
? OB_ICONIC_ALPHA
: 0xff;
485 p
->a_icon
->texture
[0].data
.image
.image
= target
->icon
;
488 p
->a_icon
->surface
.parentx
= innerx
;
489 p
->a_icon
->surface
.parenty
= innery
;
490 RrPaint(p
->a_icon
, target
->win
, innerw
, innerh
);
495 p
->a_text
->texture
[0].data
.text
.string
= newtarget
->text
;
496 p
->a_text
->surface
.parentx
= textx
;
497 p
->a_text
->surface
.parenty
= texty
;
498 RrPaint(p
->a_text
, p
->text
, textw
, texth
);
500 p
->last_target
= newtarget
;
505 void focus_cycle_popup_show(ObClient
*c
, gboolean iconic_windows
,
506 gboolean all_desktops
, gboolean dock_windows
,
507 gboolean desktop_windows
)
511 /* do this stuff only when the dialog is first showing */
513 popup_setup(&popup
, TRUE
, FALSE
);
514 g_assert(popup
.targets
!= NULL
);
516 popup_render(&popup
, c
);
519 /* show the dialog */
520 XMapWindow(ob_display
, popup
.bg
);
523 screen_hide_desktop_popup();
527 void focus_cycle_popup_hide(void)
531 ignore_start
= event_start_ignore_all_enters();
533 XUnmapWindow(ob_display
, popup
.bg
);
536 event_end_ignore_all_enters(ignore_start
);
538 popup
.mapped
= FALSE
;
542 g_free(popup
.hilite_rgba
);
543 popup
.hilite_rgba
= NULL
;
546 void focus_cycle_popup_single_show(struct _ObClient
*c
,
547 gboolean iconic_windows
,
548 gboolean all_desktops
,
549 gboolean dock_windows
,
550 gboolean desktop_windows
)
556 /* do this stuff only when the dialog is first showing */
560 popup_setup(&popup
, FALSE
, FALSE
);
561 g_assert(popup
.targets
== NULL
);
563 /* position the popup */
564 a
= screen_physical_area_primary(FALSE
);
565 icon_popup_position(single_popup
, CenterGravity
,
566 a
->x
+ a
->width
/ 2, a
->y
+ a
->height
/ 2);
567 icon_popup_height(single_popup
, POPUP_HEIGHT
);
568 icon_popup_min_width(single_popup
, POPUP_WIDTH
);
569 icon_popup_max_width(single_popup
, MAX(a
->width
/3, POPUP_WIDTH
));
570 icon_popup_text_width(single_popup
, popup
.maxtextw
);
574 text
= popup_get_name(c
);
575 icon_popup_show(single_popup
, text
, client_icon(c
));
577 screen_hide_desktop_popup();
580 void focus_cycle_popup_single_hide(void)
582 icon_popup_hide(single_popup
);
585 gboolean
focus_cycle_popup_is_showing(ObClient
*c
)
590 for (it
= popup
.targets
; it
; it
= g_list_next(it
)) {
591 ObFocusCyclePopupTarget
*t
= it
->data
;
599 static ObClient
* popup_revert(ObClient
*target
)
603 for (it
= popup
.targets
; it
; it
= g_list_next(it
)) {
604 ObFocusCyclePopupTarget
*t
= it
->data
;
605 if (t
->client
== target
) {
606 /* move to a previous window if possible */
607 for (itt
= it
->prev
; itt
; itt
= g_list_previous(itt
)) {
608 ObFocusCyclePopupTarget
*t2
= it
->data
;
609 if (focus_cycle_valid(t2
->client
))
613 /* otherwise move to a following window if possible */
614 for (itt
= it
->next
; itt
; itt
= g_list_next(itt
)) {
615 ObFocusCyclePopupTarget
*t2
= it
->data
;
616 if (focus_cycle_valid(t2
->client
))
620 /* otherwise, we can't go anywhere there is nowhere valid to go */
627 ObClient
* focus_cycle_popup_refresh(ObClient
*target
,
630 if (!popup
.mapped
) return NULL
;
632 if (!focus_cycle_valid(target
))
633 target
= popup_revert(target
);
635 redraw
= popup_setup(&popup
, TRUE
, TRUE
) && redraw
;
637 if (!target
&& popup
.targets
)
638 target
= ((ObFocusCyclePopupTarget
*)popup
.targets
->data
)->client
;
640 if (target
&& redraw
) {
641 popup
.mapped
= FALSE
;
642 popup_render(&popup
, target
);