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"
34 #define ICON_HILITE_WIDTH 2
35 #define ICON_HILITE_MARGIN 1
36 #define OUTSIDE_BORDER 3
39 typedef struct _ObFocusCyclePopup ObFocusCyclePopup
;
40 typedef struct _ObFocusCyclePopupTarget ObFocusCyclePopupTarget
;
42 struct _ObFocusCyclePopupTarget
49 struct _ObFocusCyclePopup
59 const ObFocusCyclePopupTarget
*last_target
;
67 RrPixel32
*hilite_rgba
;
72 /*! This popup shows all possible windows */
73 static ObFocusCyclePopup popup
;
74 /*! This popup shows a single window */
75 static ObIconPopup
*single_popup
;
77 static gchar
*popup_get_name (ObClient
*c
);
78 static void popup_setup (ObFocusCyclePopup
*p
,
79 gboolean create_targets
,
80 gboolean iconic_windows
,
81 gboolean all_desktops
,
82 gboolean dock_windows
,
83 gboolean desktop_windows
);
84 static void popup_render (ObFocusCyclePopup
*p
,
87 static Window
create_window(Window parent
, guint bwidth
, gulong mask
,
88 XSetWindowAttributes
*attr
)
90 return XCreateWindow(ob_display
, parent
, 0, 0, 1, 1, bwidth
,
91 RrDepth(ob_rr_inst
), InputOutput
,
92 RrVisual(ob_rr_inst
), mask
, attr
);
95 void focus_cycle_popup_startup(gboolean reconfig
)
97 XSetWindowAttributes attrib
;
99 single_popup
= icon_popup_new();
101 popup
.obwin
.type
= Window_Internal
;
102 popup
.a_bg
= RrAppearanceCopy(ob_rr_theme
->osd_hilite_bg
);
103 popup
.a_text
= RrAppearanceCopy(ob_rr_theme
->osd_hilite_label
);
104 popup
.a_icon
= RrAppearanceCopy(ob_rr_theme
->a_clear_tex
);
106 popup
.a_text
->surface
.parent
= popup
.a_bg
;
107 popup
.a_icon
->surface
.parent
= popup
.a_bg
;
109 popup
.a_icon
->texture
[0].type
= RR_TEXTURE_RGBA
;
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
;
145 XDestroyWindow(ob_display
, t
->win
);
147 popup
.targets
= g_list_delete_link(popup
.targets
, popup
.targets
);
150 g_free(popup
.hilite_rgba
);
151 popup
.hilite_rgba
= NULL
;
153 XDestroyWindow(ob_display
, popup
.text
);
154 XDestroyWindow(ob_display
, popup
.bg
);
156 RrAppearanceFree(popup
.a_icon
);
157 RrAppearanceFree(popup
.a_text
);
158 RrAppearanceFree(popup
.a_bg
);
161 static void popup_setup(ObFocusCyclePopup
*p
, gboolean create_targets
,
162 gboolean iconic_windows
, gboolean all_desktops
,
163 gboolean dock_windows
, gboolean desktop_windows
)
168 g_assert(p
->targets
== NULL
);
169 g_assert(p
->n_targets
== 0);
171 /* make its width to be the width of all the possible titles */
173 /* build a list of all the valid focus targets and measure their strings,
177 for (it
= g_list_last(focus_order
); it
; it
= g_list_previous(it
)) {
178 ObClient
*ft
= it
->data
;
180 if (focus_valid_target(ft
, TRUE
,
186 gchar
*text
= popup_get_name(ft
);
189 p
->a_text
->texture
[0].data
.text
.string
= text
;
190 maxwidth
= MAX(maxwidth
, RrMinWidth(p
->a_text
));
195 ObFocusCyclePopupTarget
*t
= g_new(ObFocusCyclePopupTarget
, 1);
199 t
->win
= create_window(p
->bg
, 0, 0, NULL
);
201 XMapWindow(ob_display
, t
->win
);
203 p
->targets
= g_list_prepend(p
->targets
, t
);
210 p
->maxtextw
= maxwidth
;
213 static gchar
*popup_get_name(ObClient
*c
)
217 const gchar
*desk
= NULL
;
220 /* find our highest direct parent */
221 p
= client_search_top_direct_parent(c
);
223 if (c
->desktop
!= DESKTOP_ALL
&& c
->desktop
!= screen_desktop
)
224 desk
= screen_desktop_names
[c
->desktop
];
226 title
= c
->iconic
? c
->icon_title
: c
->title
;
228 /* use the transient's parent's title/icon if we don't have one */
229 if (p
!= c
&& title
[0] == '\0')
230 title
= p
->iconic
? p
->icon_title
: p
->title
;
233 ret
= g_strdup_printf("%s [%s]", title
, desk
);
235 ret
= g_strdup(title
);
240 static void popup_render(ObFocusCyclePopup
*p
, const ObClient
*c
)
245 Rect
*screen_area
= NULL
;
248 gint textx
, texty
, textw
, texth
;
249 gint rgbax
, rgbay
, rgbaw
, rgbah
;
254 const ObFocusCyclePopupTarget
*newtarget
;
255 gint newtargetx
, newtargety
;
257 screen_area
= screen_physical_area_active();
259 /* get the outside margins */
260 RrMargins(p
->a_bg
, &ml
, &mt
, &mr
, &mb
);
262 /* get our outside borders */
263 l
= ml
+ OUTSIDE_BORDER
;
264 r
= mr
+ OUTSIDE_BORDER
;
265 t
= mt
+ OUTSIDE_BORDER
;
266 b
= mb
+ OUTSIDE_BORDER
;
268 /* get the icon pictures' sizes */
269 innerw
= ICON_SIZE
- (ICON_HILITE_WIDTH
+ ICON_HILITE_MARGIN
) * 2;
270 innerh
= ICON_SIZE
- (ICON_HILITE_WIDTH
+ ICON_HILITE_MARGIN
) * 2;
272 /* get the width from the text and keep it within limits */
273 w
= l
+ r
+ p
->maxtextw
;
274 w
= MIN(w
, MAX(screen_area
->width
/3, POPUP_WIDTH
)); /* max width */
275 w
= MAX(w
, POPUP_WIDTH
); /* min width */
277 /* how many icons will fit in that row? make the width fit that */
279 icons_per_row
= (w
+ ICON_SIZE
- 1) / ICON_SIZE
;
280 w
= icons_per_row
* ICON_SIZE
+ l
+ r
;
282 /* how many rows do we need? */
283 icon_rows
= (p
->n_targets
-1) / icons_per_row
+ 1;
285 /* get the text dimensions */
287 texth
= RrMinHeight(p
->a_text
) + TEXT_BORDER
* 2;
289 /* find the height of the dialog */
290 h
= t
+ b
+ (icon_rows
* ICON_SIZE
) + (OUTSIDE_BORDER
+ texth
);
292 /* get the position of the text */
294 texty
= h
- texth
- b
;
296 /* find the position for the popup (include the outer borders) */
297 x
= screen_area
->x
+ (screen_area
->width
-
298 (w
+ ob_rr_theme
->obwidth
* 2)) / 2;
299 y
= screen_area
->y
+ (screen_area
->height
-
300 (h
+ ob_rr_theme
->obwidth
* 2)) / 2;
302 /* get the dimensions of the target hilite texture */
308 /* center the icons if there is less than one row */
310 icons_center_x
= (w
- p
->n_targets
* ICON_SIZE
) / 2;
315 /* position the background but don't draw it*/
316 XMoveResizeWindow(ob_display
, p
->bg
, x
, y
, w
, h
);
318 /* set up the hilite texture for the background */
319 p
->a_bg
->texture
[0].data
.rgba
.width
= rgbaw
;
320 p
->a_bg
->texture
[0].data
.rgba
.height
= rgbah
;
321 p
->a_bg
->texture
[0].data
.rgba
.alpha
= 0xff;
322 p
->hilite_rgba
= g_new(RrPixel32
, rgbaw
* rgbah
);
323 p
->a_bg
->texture
[0].data
.rgba
.data
= p
->hilite_rgba
;
325 /* position the text, but don't draw it */
326 XMoveResizeWindow(ob_display
, p
->text
, textx
, texty
, textw
, texth
);
327 p
->a_text
->surface
.parentx
= textx
;
328 p
->a_text
->surface
.parenty
= texty
;
331 /* find the focused target */
332 for (i
= 0, it
= p
->targets
; it
; ++i
, it
= g_list_next(it
)) {
333 const ObFocusCyclePopupTarget
*target
= it
->data
;
334 const gint row
= i
/ icons_per_row
; /* starting from 0 */
335 const gint col
= i
% icons_per_row
; /* starting from 0 */
337 if (target
->client
== c
) {
338 /* save the target */
340 newtargetx
= icons_center_x
+ l
+ (col
* ICON_SIZE
);
341 newtargety
= t
+ (row
* ICON_SIZE
);
344 break; /* if we're not dimensioning, then we're done */
348 g_assert(newtarget
!= NULL
);
350 /* create the hilite under the target icon */
355 color
= ((ob_rr_theme
->osd_color
->r
& 0xff) << RrDefaultRedOffset
) +
356 ((ob_rr_theme
->osd_color
->g
& 0xff) << RrDefaultGreenOffset
) +
357 ((ob_rr_theme
->osd_color
->b
& 0xff) << RrDefaultBlueOffset
);
360 for (i
= 0; i
< rgbah
; ++i
)
361 for (j
= 0; j
< rgbaw
; ++j
) {
363 const gint x
= j
+ rgbax
- newtargetx
;
364 const gint y
= i
+ rgbay
- newtargety
;
366 if (x
< 0 || x
>= ICON_SIZE
||
367 y
< 0 || y
>= ICON_SIZE
)
369 /* outside the target */
372 else if (x
< ICON_HILITE_WIDTH
||
373 x
>= ICON_SIZE
- ICON_HILITE_WIDTH
||
374 y
< ICON_HILITE_WIDTH
||
375 y
>= ICON_SIZE
- ICON_HILITE_WIDTH
)
377 /* the border of the target */
381 /* the background of the target */
385 p
->hilite_rgba
[o
++] =
386 color
+ (a
<< RrDefaultAlphaOffset
);
390 /* * * draw everything * * */
392 /* draw the background */
393 RrPaint(p
->a_bg
, p
->bg
, w
, h
);
396 for (i
= 0, it
= p
->targets
; it
; ++i
, it
= g_list_next(it
)) {
397 const ObFocusCyclePopupTarget
*target
= it
->data
;
399 /* have to redraw the targetted icon and last targetted icon,
400 they can pick up the hilite changes in the backgroud */
401 if (!p
->mapped
|| newtarget
== target
|| p
->last_target
== target
) {
402 const ObClientIcon
*icon
;
403 const gint row
= i
/ icons_per_row
; /* starting from 0 */
404 const gint col
= i
% icons_per_row
; /* starting from 0 */
407 /* find the dimensions of the icon inside it */
408 innerx
= icons_center_x
+ l
+ (col
* ICON_SIZE
);
409 innerx
+= ICON_HILITE_WIDTH
+ ICON_HILITE_MARGIN
;
410 innery
= t
+ (row
* ICON_SIZE
);
411 innery
+= ICON_HILITE_WIDTH
+ ICON_HILITE_MARGIN
;
414 XMoveResizeWindow(ob_display
, target
->win
,
415 innerx
, innery
, innerw
, innerh
);
417 /* get the icon from the client */
418 icon
= client_icon(target
->client
, innerw
, innerh
);
419 p
->a_icon
->texture
[0].data
.rgba
.width
= icon
->width
;
420 p
->a_icon
->texture
[0].data
.rgba
.height
= icon
->height
;
421 p
->a_icon
->texture
[0].data
.rgba
.alpha
=
422 target
->client
->iconic
? OB_ICONIC_ALPHA
: 0xff;
423 p
->a_icon
->texture
[0].data
.rgba
.data
= icon
->data
;
426 p
->a_icon
->surface
.parentx
= innerx
;
427 p
->a_icon
->surface
.parenty
= innery
;
428 RrPaint(p
->a_icon
, target
->win
, innerw
, innerh
);
433 p
->a_text
->texture
[0].data
.text
.string
= newtarget
->text
;
434 p
->a_text
->surface
.parentx
= textx
;
435 p
->a_text
->surface
.parenty
= texty
;
436 RrPaint(p
->a_text
, p
->text
, textw
, texth
);
438 p
->last_target
= newtarget
;
443 void focus_cycle_popup_show(ObClient
*c
, gboolean iconic_windows
,
444 gboolean all_desktops
, gboolean dock_windows
,
445 gboolean desktop_windows
)
449 /* do this stuff only when the dialog is first showing */
451 popup_setup(&popup
, TRUE
, iconic_windows
, all_desktops
,
452 dock_windows
, desktop_windows
);
453 g_assert(popup
.targets
!= NULL
);
455 popup_render(&popup
, c
);
458 /* show the dialog */
459 XMapWindow(ob_display
, popup
.bg
);
462 screen_hide_desktop_popup();
466 void focus_cycle_popup_hide(void)
470 ignore_start
= event_start_ignore_all_enters();
472 XUnmapWindow(ob_display
, popup
.bg
);
475 event_end_ignore_all_enters(ignore_start
);
477 popup
.mapped
= FALSE
;
479 while(popup
.targets
) {
480 ObFocusCyclePopupTarget
*t
= popup
.targets
->data
;
483 XDestroyWindow(ob_display
, t
->win
);
485 popup
.targets
= g_list_delete_link(popup
.targets
, popup
.targets
);
488 popup
.last_target
= NULL
;
490 g_free(popup
.hilite_rgba
);
491 popup
.hilite_rgba
= NULL
;
494 void focus_cycle_popup_single_show(struct _ObClient
*c
,
495 gboolean iconic_windows
,
496 gboolean all_desktops
,
497 gboolean dock_windows
,
498 gboolean desktop_windows
)
504 /* do this stuff only when the dialog is first showing */
508 popup_setup(&popup
, FALSE
, iconic_windows
, all_desktops
,
509 dock_windows
, desktop_windows
);
510 g_assert(popup
.targets
== NULL
);
512 /* position the popup */
513 a
= screen_physical_area_active();
514 icon_popup_position(single_popup
, CenterGravity
,
515 a
->x
+ a
->width
/ 2, a
->y
+ a
->height
/ 2);
516 icon_popup_height(single_popup
, POPUP_HEIGHT
);
517 icon_popup_min_width(single_popup
, POPUP_WIDTH
);
518 icon_popup_max_width(single_popup
, MAX(a
->width
/3, POPUP_WIDTH
));
519 icon_popup_text_width(single_popup
, popup
.maxtextw
);
523 text
= popup_get_name(c
);
524 icon_popup_show(single_popup
, text
, client_icon(c
, ICON_SIZE
, ICON_SIZE
));
526 screen_hide_desktop_popup();
529 void focus_cycle_popup_single_hide(void)
531 icon_popup_hide(single_popup
);