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"
25 #include "focus_cycle.h"
28 #include "render/render.h"
34 #define ICON_HILITE_WIDTH 2
35 #define ICON_HILITE_MARGIN 1
36 #define OUTSIDE_BORDER 3
38 typedef struct _ObFocusCyclePopup ObFocusCyclePopup
;
39 typedef struct _ObFocusCyclePopupTarget ObFocusCyclePopupTarget
;
41 struct _ObFocusCyclePopupTarget
48 struct _ObFocusCyclePopup
58 const ObFocusCyclePopupTarget
*last_target
;
66 RrPixel32
*hilite_rgba
;
71 static ObFocusCyclePopup popup
;
73 static gchar
*popup_get_name (ObClient
*c
);
74 static void popup_setup (ObFocusCyclePopup
*p
,gboolean all_desktops
,
75 gboolean dock_windows
, gboolean desktop_windows
);
76 static void popup_render (ObFocusCyclePopup
*p
, const ObClient
*c
);
78 static Window
create_window(Window parent
, guint bwidth
, gulong mask
,
79 XSetWindowAttributes
*attr
)
81 return XCreateWindow(ob_display
, parent
, 0, 0, 1, 1, bwidth
,
82 RrDepth(ob_rr_inst
), InputOutput
,
83 RrVisual(ob_rr_inst
), mask
, attr
);
86 void focus_cycle_popup_startup(gboolean reconfig
)
88 XSetWindowAttributes attrib
;
90 popup
.obwin
.type
= Window_Internal
;
91 popup
.a_bg
= RrAppearanceCopy(ob_rr_theme
->osd_hilite_bg
);
92 popup
.a_text
= RrAppearanceCopy(ob_rr_theme
->osd_hilite_label
);
93 popup
.a_icon
= RrAppearanceCopy(ob_rr_theme
->a_clear_tex
);
95 popup
.a_text
->surface
.parent
= popup
.a_bg
;
96 popup
.a_icon
->surface
.parent
= popup
.a_bg
;
98 popup
.a_icon
->texture
[0].type
= RR_TEXTURE_RGBA
;
100 RrAppearanceAddTextures(popup
.a_bg
, 1);
101 popup
.a_bg
->texture
[0].type
= RR_TEXTURE_RGBA
;
103 attrib
.override_redirect
= True
;
104 attrib
.border_pixel
=RrColorPixel(ob_rr_theme
->frame_focused_border_color
);
105 popup
.bg
= create_window(RootWindow(ob_display
, ob_screen
),
106 ob_rr_theme
->fbwidth
,
107 CWOverrideRedirect
| CWBorderPixel
, &attrib
);
109 popup
.text
= create_window(popup
.bg
, 0, 0, NULL
);
111 popup
.targets
= NULL
;
113 popup
.last_target
= NULL
;
115 popup
.hilite_rgba
= NULL
;
117 XMapWindow(ob_display
, popup
.text
);
119 stacking_add(INTERNAL_AS_WINDOW(&popup
));
122 void focus_cycle_popup_shutdown(gboolean reconfig
)
124 stacking_remove(INTERNAL_AS_WINDOW(&popup
));
126 while(popup
.targets
) {
127 ObFocusCyclePopupTarget
*t
= popup
.targets
->data
;
130 XDestroyWindow(ob_display
, t
->win
);
132 popup
.targets
= g_list_delete_link(popup
.targets
, popup
.targets
);
135 g_free(popup
.hilite_rgba
);
137 XDestroyWindow(ob_display
, popup
.text
);
138 XDestroyWindow(ob_display
, popup
.bg
);
140 RrAppearanceFree(popup
.a_icon
);
141 RrAppearanceFree(popup
.a_text
);
142 RrAppearanceFree(popup
.a_bg
);
145 static void popup_setup(ObFocusCyclePopup
*p
,gboolean all_desktops
,
146 gboolean dock_windows
, gboolean desktop_windows
)
151 g_assert(p
->targets
== NULL
);
152 g_assert(p
->n_targets
== 0);
154 /* make its width to be the width of all the possible titles */
156 /* build a list of all the valid focus targets and measure their strings,
160 for (it
= g_list_last(focus_order
); it
; it
= g_list_previous(it
)) {
161 ObClient
*ft
= it
->data
;
163 if (focus_cycle_target_valid(ft
,
168 ObFocusCyclePopupTarget
*t
= g_new(ObFocusCyclePopupTarget
, 1);
171 t
->text
= popup_get_name(ft
);
172 t
->win
= create_window(p
->bg
, 0, 0, NULL
);
174 XMapWindow(ob_display
, t
->win
);
177 p
->a_text
->texture
[0].data
.text
.string
= t
->text
;
178 maxwidth
= MAX(maxwidth
, RrMinWidth(p
->a_text
));
180 p
->targets
= g_list_prepend(p
->targets
, t
);
186 p
->maxtextw
= maxwidth
;
189 static gchar
*popup_get_name(ObClient
*c
)
193 const gchar
*desk
= NULL
;
196 /* find our highest direct parent, including non-normal windows */
197 for (p
= c
; p
->transient_for
&& p
->transient_for
!= OB_TRAN_GROUP
;
198 p
= p
->transient_for
);
200 if (c
->desktop
!= DESKTOP_ALL
&& c
->desktop
!= screen_desktop
)
201 desk
= screen_desktop_names
[c
->desktop
];
203 /* use the transient's parent's title/icon if we don't have one */
204 if (p
!= c
&& !strcmp("", (c
->iconic
? c
->icon_title
: c
->title
)))
205 title
= g_strdup(p
->iconic
? p
->icon_title
: p
->title
);
208 title
= g_strdup(c
->iconic
? c
->icon_title
: c
->title
);
211 ret
= g_strdup_printf("%s [%s]", title
, desk
);
221 static void popup_render(ObFocusCyclePopup
*p
, const ObClient
*c
)
229 gint textx
, texty
, textw
, texth
;
230 gint rgbax
, rgbay
, rgbaw
, rgbah
;
234 const ObFocusCyclePopupTarget
*newtarget
;
235 gint newtargetx
, newtargety
;
237 /* XXX find the middle monitor? */
238 screen_area
= screen_physical_area_monitor(0);
240 /* get the outside margins */
241 RrMargins(p
->a_bg
, &ml
, &mt
, &mr
, &mb
);
243 /* get our outside borders */
244 l
= ml
+ OUTSIDE_BORDER
;
245 r
= mr
+ OUTSIDE_BORDER
;
246 t
= mt
+ OUTSIDE_BORDER
;
247 b
= mb
+ OUTSIDE_BORDER
;
249 /* get the icon pictures' sizes */
250 innerw
= ICON_SIZE
- (ICON_HILITE_WIDTH
+ ICON_HILITE_MARGIN
) * 2;
251 innerh
= ICON_SIZE
- (ICON_HILITE_WIDTH
+ ICON_HILITE_MARGIN
) * 2;
253 /* get the width from the text and keep it within limits */
254 w
= l
+ r
+ p
->maxtextw
;
255 w
= MIN(w
, MAX(screen_area
->width
/3, POPUP_WIDTH
)); /* max width */
256 w
= MAX(w
, POPUP_WIDTH
); /* min width */
258 /* how many icons will fit in that row? make the width fit that */
260 icons_per_row
= w
/ ICON_SIZE
;
261 w
= icons_per_row
* ICON_SIZE
+ l
+ r
;
263 /* how many rows do we need? */
264 icon_rows
= (p
->n_targets
-1) / icons_per_row
+ 1;
266 /* get the text dimensions */
268 texth
= RrMinHeight(p
->a_text
);
270 /* find the height of the dialog */
271 h
= t
+ b
+ (icon_rows
* ICON_SIZE
) + (OUTSIDE_BORDER
+ texth
);
273 /* get the position of the text */
275 texty
= h
- texth
- b
;
277 /* find the position for the popup (include the outer borders) */
278 x
= screen_area
->x
+ (screen_area
->width
-
279 (w
+ ob_rr_theme
->fbwidth
* 2)) / 2;
280 y
= screen_area
->y
+ (screen_area
->height
-
281 (h
+ ob_rr_theme
->fbwidth
* 2)) / 2;
283 /* get the dimensions of the target hilite texture */
290 /* position the background but don't draw it*/
291 XMoveResizeWindow(ob_display
, p
->bg
, x
, y
, w
, h
);
293 /* set up the hilite texture for the background */
294 p
->a_bg
->texture
[0].data
.rgba
.width
= rgbaw
;
295 p
->a_bg
->texture
[0].data
.rgba
.height
= rgbah
;
296 p
->hilite_rgba
= g_new(RrPixel32
, rgbaw
* rgbah
);
297 p
->a_bg
->texture
[0].data
.rgba
.data
= p
->hilite_rgba
;
299 /* position the text, but don't draw it */
300 XMoveResizeWindow(ob_display
, p
->text
, textx
, texty
, textw
, texth
);
301 p
->a_text
->surface
.parentx
= textx
;
302 p
->a_text
->surface
.parenty
= texty
;
305 /* find the focused target */
306 for (i
= 0, it
= p
->targets
; it
; ++i
, it
= g_list_next(it
)) {
307 const ObFocusCyclePopupTarget
*target
= it
->data
;
308 const gint row
= i
/ icons_per_row
; /* starting from 0 */
309 const gint col
= i
% icons_per_row
; /* starting from 0 */
311 if (target
->client
== c
) {
312 /* save the target */
314 newtargetx
= l
+ (col
* ICON_SIZE
);
315 newtargety
= t
+ (row
* ICON_SIZE
);
318 break; /* if we're not dimensioning, then we're done */
322 g_assert(newtarget
!= NULL
);
324 /* create the hilite under the target icon */
329 color
= ((ob_rr_theme
->osd_color
->r
& 0xff) << RrDefaultRedOffset
) +
330 ((ob_rr_theme
->osd_color
->g
& 0xff) << RrDefaultGreenOffset
) +
331 ((ob_rr_theme
->osd_color
->b
& 0xff) << RrDefaultBlueOffset
);
334 for (i
= 0; i
< rgbah
; ++i
)
335 for (j
= 0; j
< rgbaw
; ++j
) {
337 const gint x
= j
+ rgbax
- newtargetx
;
338 const gint y
= i
+ rgbay
- newtargety
;
340 if (x
< 0 || x
>= ICON_SIZE
||
341 y
< 0 || y
>= ICON_SIZE
)
343 /* outside the target */
346 else if (x
< ICON_HILITE_WIDTH
||
347 x
>= ICON_SIZE
- ICON_HILITE_WIDTH
||
348 y
< ICON_HILITE_WIDTH
||
349 y
>= ICON_SIZE
- ICON_HILITE_WIDTH
)
351 /* the border of the target */
355 /* the background of the target */
359 p
->hilite_rgba
[o
++] =
360 color
+ (a
<< RrDefaultAlphaOffset
);
364 /* * * draw everything * * */
366 /* draw the background */
367 RrPaint(p
->a_bg
, p
->bg
, w
, h
);
370 for (i
= 0, it
= p
->targets
; it
; ++i
, it
= g_list_next(it
)) {
371 const ObFocusCyclePopupTarget
*target
= it
->data
;
373 /* have to redraw the targetted icon and last targetted icon,
374 they can pick up the hilite changes in the backgroud */
375 if (!p
->mapped
|| newtarget
== target
|| p
->last_target
== target
) {
376 const ObClientIcon
*icon
;
377 const gint row
= i
/ icons_per_row
; /* starting from 0 */
378 const gint col
= i
% icons_per_row
; /* starting from 0 */
381 /* find the dimensions of the icon inside it */
382 innerx
= l
+ (col
* ICON_SIZE
);
383 innerx
+= ICON_HILITE_WIDTH
+ ICON_HILITE_MARGIN
;
384 innery
= t
+ (row
* ICON_SIZE
);
385 innery
+= ICON_HILITE_WIDTH
+ ICON_HILITE_MARGIN
;
388 XMoveResizeWindow(ob_display
, target
->win
,
389 innerx
, innery
, innerw
, innerh
);
391 /* get the icon from the client */
392 icon
= client_icon(target
->client
, innerw
, innerh
);
393 p
->a_icon
->texture
[0].data
.rgba
.width
= icon
->width
;
394 p
->a_icon
->texture
[0].data
.rgba
.height
= icon
->height
;
395 p
->a_icon
->texture
[0].data
.rgba
.data
= icon
->data
;
398 p
->a_icon
->surface
.parentx
= innerx
;
399 p
->a_icon
->surface
.parenty
= innery
;
400 RrPaint(p
->a_icon
, target
->win
, innerw
, innerh
);
405 p
->a_text
->texture
[0].data
.text
.string
= newtarget
->text
;
406 p
->a_text
->surface
.parentx
= textx
;
407 p
->a_text
->surface
.parenty
= texty
;
408 RrPaint(p
->a_text
, p
->text
, textw
, texth
);
410 p
->last_target
= newtarget
;
413 void focus_cycle_popup_show(ObClient
*c
,
414 gboolean all_desktops
, gboolean dock_windows
,
415 gboolean desktop_windows
)
419 /* do this stuff only when the dialog is first showing */
421 popup_setup(&popup
, all_desktops
, dock_windows
, desktop_windows
);
422 g_assert(popup
.targets
!= NULL
);
424 popup_render(&popup
, c
);
427 /* show the dialog */
428 XMapWindow(ob_display
, popup
.bg
);
434 void focus_cycle_popup_hide()
436 XUnmapWindow(ob_display
, popup
.bg
);
439 popup
.mapped
= FALSE
;
441 while(popup
.targets
) {
442 ObFocusCyclePopupTarget
*t
= popup
.targets
->data
;
445 XDestroyWindow(ob_display
, t
->win
);
447 popup
.targets
= g_list_delete_link(popup
.targets
, popup
.targets
);
450 popup
.last_target
= NULL
;
452 g_free(popup
.hilite_rgba
);
453 popup
.hilite_rgba
= NULL
;