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
50 struct _ObFocusCyclePopup
58 const ObFocusCyclePopupTarget
*last_target
;
69 /*! This popup shows all possible windows */
70 static ObFocusCyclePopup popup
;
71 /*! This popup shows a single window */
72 static ObIconPopup
*single_popup
;
74 static gchar
*popup_get_name (ObClient
*c
);
75 static void popup_setup (ObFocusCyclePopup
*p
,
76 gboolean create_targets
,
77 gboolean iconic_windows
,
78 gboolean all_desktops
,
79 gboolean dock_windows
,
80 gboolean desktop_windows
);
81 static void popup_render (ObFocusCyclePopup
*p
,
84 static Window
create_window(Window parent
, guint bwidth
, gulong mask
,
85 XSetWindowAttributes
*attr
)
87 return XCreateWindow(obt_display
, parent
, 0, 0, 1, 1, bwidth
,
88 RrDepth(ob_rr_inst
), InputOutput
,
89 RrVisual(ob_rr_inst
), mask
, attr
);
92 void focus_cycle_popup_startup(gboolean reconfig
)
94 XSetWindowAttributes attrib
;
97 single_popup
= icon_popup_new();
99 popup
.obwin
.type
= OB_WINDOW_CLASS_INTERNAL
;
100 popup
.a_bg
= RrAppearanceCopy(ob_rr_theme
->osd_hilite_bg
);
101 popup
.a_text
= RrAppearanceCopy(ob_rr_theme
->osd_hilite_label
);
102 popup
.a_icon
= RrAppearanceCopy(ob_rr_theme
->a_clear
);
104 popup
.a_text
->surface
.parent
= popup
.a_bg
;
105 popup
.a_icon
->surface
.parent
= popup
.a_bg
;
107 RrAppearanceAddTextures(popup
.a_icon
, 2);
109 popup
.a_icon
->texture
[0].type
= RR_TEXTURE_RGBA
;
111 attrib
.override_redirect
= True
;
112 attrib
.border_pixel
=RrColorPixel(ob_rr_theme
->osd_border_color
);
113 popup
.bg
= create_window(obt_root(ob_screen
), ob_rr_theme
->obwidth
,
114 CWOverrideRedirect
| CWBorderPixel
, &attrib
);
116 popup
.targets
= NULL
;
118 popup
.last_target
= NULL
;
120 /* set up the hilite texture for the icon */
121 popup
.a_icon
->texture
[1].data
.rgba
.width
= ICON_SIZE
;
122 popup
.a_icon
->texture
[1].data
.rgba
.height
= ICON_SIZE
;
123 popup
.a_icon
->texture
[1].data
.rgba
.alpha
= 0xff;
124 p
= g_new(RrPixel32
, ICON_SIZE
* ICON_SIZE
);
125 popup
.a_icon
->texture
[1].data
.rgba
.data
= p
;
127 /* create the hilite under the target icon */
132 color
= ((ob_rr_theme
->osd_color
->r
& 0xff) << RrDefaultRedOffset
) +
133 ((ob_rr_theme
->osd_color
->g
& 0xff) << RrDefaultGreenOffset
) +
134 ((ob_rr_theme
->osd_color
->b
& 0xff) << RrDefaultBlueOffset
);
137 for (x
= 0; x
< ICON_SIZE
; x
++)
138 for (y
= 0; y
< ICON_SIZE
; y
++) {
141 if (x
< ICON_HILITE_WIDTH
||
142 x
>= ICON_SIZE
- ICON_HILITE_WIDTH
||
143 y
< ICON_HILITE_WIDTH
||
144 y
>= ICON_SIZE
- ICON_HILITE_WIDTH
)
146 /* the border of the target */
150 /* the background of the target */
154 p
[o
++] = color
+ (a
<< RrDefaultAlphaOffset
);
158 stacking_add(INTERNAL_AS_WINDOW(&popup
));
159 window_add(&popup
.bg
, INTERNAL_AS_WINDOW(&popup
));
162 void focus_cycle_popup_shutdown(gboolean reconfig
)
164 icon_popup_free(single_popup
);
166 window_remove(popup
.bg
);
167 stacking_remove(INTERNAL_AS_WINDOW(&popup
));
169 while(popup
.targets
) {
170 ObFocusCyclePopupTarget
*t
= popup
.targets
->data
;
173 XDestroyWindow(obt_display
, t
->iconwin
);
174 XDestroyWindow(obt_display
, t
->textwin
);
176 popup
.targets
= g_list_delete_link(popup
.targets
, popup
.targets
);
179 XDestroyWindow(obt_display
, popup
.bg
);
181 RrAppearanceFree(popup
.a_icon
);
182 RrAppearanceFree(popup
.a_text
);
183 RrAppearanceFree(popup
.a_bg
);
186 static void popup_setup(ObFocusCyclePopup
*p
, gboolean create_targets
,
187 gboolean iconic_windows
, gboolean all_desktops
,
188 gboolean dock_windows
, gboolean desktop_windows
)
193 g_assert(p
->targets
== NULL
);
194 g_assert(p
->n_targets
== 0);
196 /* make its width to be the width of all the possible titles */
198 /* build a list of all the valid focus targets and measure their strings,
202 for (it
= g_list_last(focus_order
); it
; it
= g_list_previous(it
)) {
203 ObClient
*ft
= it
->data
;
205 if (focus_valid_target(ft
, TRUE
,
211 gchar
*text
= popup_get_name(ft
);
214 p
->a_text
->texture
[0].data
.text
.string
= text
;
215 maxwidth
= MAX(maxwidth
, RrMinWidth(p
->a_text
));
220 ObFocusCyclePopupTarget
*t
= g_new(ObFocusCyclePopupTarget
, 1);
224 t
->iconwin
= create_window(p
->bg
, 0, 0, NULL
);
225 t
->textwin
= create_window(p
->bg
, 0, 0, NULL
);
227 XMapWindow(obt_display
, t
->iconwin
);
228 XMapWindow(obt_display
, t
->textwin
);
230 p
->targets
= g_list_prepend(p
->targets
, t
);
237 p
->maxtextw
= maxwidth
;
240 static gchar
*popup_get_name(ObClient
*c
)
244 const gchar
*desk
= NULL
;
247 /* find our highest direct parent */
248 p
= client_search_top_direct_parent(c
);
250 if (c
->desktop
!= DESKTOP_ALL
&& c
->desktop
!= screen_desktop
)
251 desk
= screen_desktop_names
[c
->desktop
];
253 title
= c
->iconic
? c
->icon_title
: c
->title
;
255 /* use the transient's parent's title/icon if we don't have one */
256 if (p
!= c
&& title
[0] == '\0')
257 title
= p
->iconic
? p
->icon_title
: p
->title
;
260 ret
= g_strdup_printf("%s [%s]", title
, desk
);
262 ret
= g_strdup(title
);
267 static void popup_render(ObFocusCyclePopup
*p
, const ObClient
*c
)
272 Rect
*screen_area
= NULL
;
273 gint rgbax
, rgbay
, rgbaw
, rgbah
;
277 const ObFocusCyclePopupTarget
*newtarget
;
278 gint newtargetx
, newtargety
;
280 screen_area
= screen_physical_area_active();
282 /* get the outside margins */
283 RrMargins(p
->a_bg
, &ml
, &mt
, &mr
, &mb
);
285 /* get our outside borders */
286 l
= ml
+ OUTSIDE_BORDER
;
287 r
= mr
+ OUTSIDE_BORDER
;
288 t
= mt
+ OUTSIDE_BORDER
;
289 b
= mb
+ OUTSIDE_BORDER
;
291 /* get the icon pictures' sizes */
292 innerw
= ICON_SIZE
- (ICON_HILITE_WIDTH
+ ICON_HILITE_MARGIN
) * 2;
293 innerh
= ICON_SIZE
- (ICON_HILITE_WIDTH
+ ICON_HILITE_MARGIN
) * 2;
295 /* get the width from the text and keep it within limits */
296 w
= l
+ r
+ p
->maxtextw
;
297 w
= MIN(w
, MAX(screen_area
->width
/3, POPUP_WIDTH
)); /* max width */
298 w
= MAX(w
, POPUP_WIDTH
); /* min width */
300 /* find the height of the dialog */
301 #warning limit the height and scroll entries somehow
302 h
= t
+ b
+ (p
->n_targets
* ICON_SIZE
) + OUTSIDE_BORDER
;
304 /* find the position for the popup (include the outer borders) */
305 x
= screen_area
->x
+ (screen_area
->width
-
306 (w
+ ob_rr_theme
->obwidth
* 2)) / 2;
307 y
= screen_area
->y
+ (screen_area
->height
-
308 (h
+ ob_rr_theme
->obwidth
* 2)) / 2;
310 /* get the dimensions of the target hilite texture */
317 /* position the background but don't draw it*/
318 XMoveResizeWindow(obt_display
, p
->bg
, x
, y
, w
, h
);
321 /* find the focused target */
322 for (i
= 0, it
= p
->targets
; it
; ++i
, it
= g_list_next(it
)) {
323 const ObFocusCyclePopupTarget
*target
= it
->data
;
325 if (target
->client
== c
) {
326 /* save the target */
329 newtargety
= t
+ i
* ICON_SIZE
;
332 break; /* if we're not dimensioning, then we're done */
336 g_assert(newtarget
!= NULL
);
338 /* * * draw everything * * */
340 /* draw the background */
342 RrPaint(p
->a_bg
, p
->bg
, w
, h
);
344 /* draw the icons and text */
345 for (i
= 0, it
= p
->targets
; it
; ++i
, it
= g_list_next(it
)) {
346 const ObFocusCyclePopupTarget
*target
= it
->data
;
348 /* have to redraw the targetted icon and last targetted icon,
349 they can pick up the hilite changes in the backgroud */
350 if (!p
->mapped
|| newtarget
== target
|| p
->last_target
== target
) {
351 const ObClientIcon
*icon
;
354 /* find the dimensions of the icon inside it */
356 innerx
+= ICON_HILITE_WIDTH
+ ICON_HILITE_MARGIN
;
357 innery
= t
+ i
* ICON_SIZE
;
358 innery
+= ICON_HILITE_WIDTH
+ ICON_HILITE_MARGIN
;
361 XMoveResizeWindow(obt_display
, target
->iconwin
,
362 innerx
, innery
, innerw
, innerh
);
365 XMoveResizeWindow(obt_display
, target
->textwin
,
366 innerx
+ ICON_SIZE
, innery
,
367 w
- innerx
- ICON_SIZE
- OUTSIDE_BORDER
, innerh
);
369 /* get the icon from the client */
370 icon
= client_icon(target
->client
, innerw
, innerh
);
371 p
->a_icon
->texture
[0].data
.rgba
.width
= icon
->width
;
372 p
->a_icon
->texture
[0].data
.rgba
.height
= icon
->height
;
373 p
->a_icon
->texture
[0].data
.rgba
.alpha
=
374 target
->client
->iconic
? OB_ICONIC_ALPHA
: 0xff;
375 p
->a_icon
->texture
[0].data
.rgba
.data
= icon
->data
;
377 /* Draw the hilite? */
378 #warning do i have to add more obrender interface thingers to get it to draw the icon inside the hilight? sigh
379 p
->a_icon
->texture
[1].type
= (target
== newtarget
) ?
380 RR_TEXTURE_RGBA
: RR_TEXTURE_NONE
;
383 p
->a_icon
->surface
.parentx
= innerx
;
384 p
->a_icon
->surface
.parenty
= innery
;
385 RrPaint(p
->a_icon
, target
->iconwin
, innerw
, innerh
);
388 p
->a_text
->texture
[0].data
.text
.string
= target
->text
;
389 p
->a_text
->surface
.parentx
= innerx
+ ICON_SIZE
;
390 p
->a_text
->surface
.parenty
= innery
;
391 RrPaint(p
->a_text
, target
->textwin
, w
- innerx
- ICON_SIZE
- OUTSIDE_BORDER
, innerh
);
395 p
->last_target
= newtarget
;
400 void focus_cycle_popup_show(ObClient
*c
, gboolean iconic_windows
,
401 gboolean all_desktops
, gboolean dock_windows
,
402 gboolean desktop_windows
)
406 /* do this stuff only when the dialog is first showing */
408 popup_setup(&popup
, TRUE
, iconic_windows
, all_desktops
,
409 dock_windows
, desktop_windows
);
410 g_assert(popup
.targets
!= NULL
);
412 popup_render(&popup
, c
);
415 /* show the dialog */
416 XMapWindow(obt_display
, popup
.bg
);
419 screen_hide_desktop_popup();
423 void focus_cycle_popup_hide(void)
427 ignore_start
= event_start_ignore_all_enters();
429 XUnmapWindow(obt_display
, popup
.bg
);
432 event_end_ignore_all_enters(ignore_start
);
434 popup
.mapped
= FALSE
;
436 while(popup
.targets
) {
437 ObFocusCyclePopupTarget
*t
= popup
.targets
->data
;
440 XDestroyWindow(obt_display
, t
->iconwin
);
441 XDestroyWindow(obt_display
, t
->textwin
);
444 popup
.targets
= g_list_delete_link(popup
.targets
, popup
.targets
);
447 popup
.last_target
= NULL
;
450 void focus_cycle_popup_single_show(struct _ObClient
*c
,
451 gboolean iconic_windows
,
452 gboolean all_desktops
,
453 gboolean dock_windows
,
454 gboolean desktop_windows
)
460 /* do this stuff only when the dialog is first showing */
464 popup_setup(&popup
, FALSE
, iconic_windows
, all_desktops
,
465 dock_windows
, desktop_windows
);
466 g_assert(popup
.targets
== NULL
);
468 /* position the popup */
469 a
= screen_physical_area_active();
470 icon_popup_position(single_popup
, CenterGravity
,
471 a
->x
+ a
->width
/ 2, a
->y
+ a
->height
/ 2);
472 icon_popup_height(single_popup
, POPUP_HEIGHT
);
473 icon_popup_min_width(single_popup
, POPUP_WIDTH
);
474 icon_popup_max_width(single_popup
, MAX(a
->width
/3, POPUP_WIDTH
));
475 icon_popup_text_width(single_popup
, popup
.maxtextw
);
479 text
= popup_get_name(c
);
480 icon_popup_show(single_popup
, text
, client_icon(c
, ICON_SIZE
, ICON_SIZE
));
482 screen_hide_desktop_popup();
485 void focus_cycle_popup_single_hide(void)
487 icon_popup_hide(single_popup
);