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
,
75 gboolean iconic_windows
,
76 gboolean all_desktops
,
77 gboolean dock_windows
,
78 gboolean desktop_windows
);
79 static void popup_render (ObFocusCyclePopup
*p
,
82 static Window
create_window(Window parent
, guint bwidth
, gulong mask
,
83 XSetWindowAttributes
*attr
)
85 return XCreateWindow(ob_display
, parent
, 0, 0, 1, 1, bwidth
,
86 RrDepth(ob_rr_inst
), InputOutput
,
87 RrVisual(ob_rr_inst
), mask
, attr
);
90 void focus_cycle_popup_startup(gboolean reconfig
)
92 XSetWindowAttributes attrib
;
94 popup
.obwin
.type
= Window_Internal
;
95 popup
.a_bg
= RrAppearanceCopy(ob_rr_theme
->osd_hilite_bg
);
96 popup
.a_text
= RrAppearanceCopy(ob_rr_theme
->osd_hilite_label
);
97 popup
.a_icon
= RrAppearanceCopy(ob_rr_theme
->a_clear_tex
);
99 popup
.a_text
->surface
.parent
= popup
.a_bg
;
100 popup
.a_icon
->surface
.parent
= popup
.a_bg
;
102 popup
.a_icon
->texture
[0].type
= RR_TEXTURE_RGBA
;
104 RrAppearanceAddTextures(popup
.a_bg
, 1);
105 popup
.a_bg
->texture
[0].type
= RR_TEXTURE_RGBA
;
107 attrib
.override_redirect
= True
;
108 attrib
.border_pixel
=RrColorPixel(ob_rr_theme
->frame_focused_border_color
);
109 popup
.bg
= create_window(RootWindow(ob_display
, ob_screen
),
110 ob_rr_theme
->fbwidth
,
111 CWOverrideRedirect
| CWBorderPixel
, &attrib
);
113 popup
.text
= create_window(popup
.bg
, 0, 0, NULL
);
115 popup
.targets
= NULL
;
117 popup
.last_target
= NULL
;
119 popup
.hilite_rgba
= NULL
;
121 XMapWindow(ob_display
, popup
.text
);
123 stacking_add(INTERNAL_AS_WINDOW(&popup
));
126 void focus_cycle_popup_shutdown(gboolean reconfig
)
128 stacking_remove(INTERNAL_AS_WINDOW(&popup
));
130 while(popup
.targets
) {
131 ObFocusCyclePopupTarget
*t
= popup
.targets
->data
;
134 XDestroyWindow(ob_display
, t
->win
);
136 popup
.targets
= g_list_delete_link(popup
.targets
, popup
.targets
);
139 g_free(popup
.hilite_rgba
);
141 XDestroyWindow(ob_display
, popup
.text
);
142 XDestroyWindow(ob_display
, popup
.bg
);
144 RrAppearanceFree(popup
.a_icon
);
145 RrAppearanceFree(popup
.a_text
);
146 RrAppearanceFree(popup
.a_bg
);
149 static void popup_setup(ObFocusCyclePopup
*p
,
150 gboolean iconic_windows
, gboolean all_desktops
,
151 gboolean dock_windows
, gboolean desktop_windows
)
156 g_assert(p
->targets
== NULL
);
157 g_assert(p
->n_targets
== 0);
159 /* make its width to be the width of all the possible titles */
161 /* build a list of all the valid focus targets and measure their strings,
165 for (it
= g_list_last(focus_order
); it
; it
= g_list_previous(it
)) {
166 ObClient
*ft
= it
->data
;
168 if (focus_cycle_target_valid(ft
,
174 ObFocusCyclePopupTarget
*t
= g_new(ObFocusCyclePopupTarget
, 1);
177 t
->text
= popup_get_name(ft
);
178 t
->win
= create_window(p
->bg
, 0, 0, NULL
);
180 XMapWindow(ob_display
, t
->win
);
183 p
->a_text
->texture
[0].data
.text
.string
= t
->text
;
184 maxwidth
= MAX(maxwidth
, RrMinWidth(p
->a_text
));
186 p
->targets
= g_list_prepend(p
->targets
, t
);
192 p
->maxtextw
= maxwidth
;
195 static gchar
*popup_get_name(ObClient
*c
)
199 const gchar
*desk
= NULL
;
202 /* find our highest direct parent, including non-normal windows */
203 for (p
= c
; p
->transient_for
&& p
->transient_for
!= OB_TRAN_GROUP
;
204 p
= p
->transient_for
);
206 if (c
->desktop
!= DESKTOP_ALL
&& c
->desktop
!= screen_desktop
)
207 desk
= screen_desktop_names
[c
->desktop
];
209 /* use the transient's parent's title/icon if we don't have one */
210 if (p
!= c
&& !strcmp("", (c
->iconic
? c
->icon_title
: c
->title
)))
211 title
= g_strdup(p
->iconic
? p
->icon_title
: p
->title
);
214 title
= g_strdup(c
->iconic
? c
->icon_title
: c
->title
);
217 ret
= g_strdup_printf("%s [%s]", title
, desk
);
227 static void popup_render(ObFocusCyclePopup
*p
, const ObClient
*c
)
235 gint textx
, texty
, textw
, texth
;
236 gint rgbax
, rgbay
, rgbaw
, rgbah
;
241 const ObFocusCyclePopupTarget
*newtarget
;
242 gint newtargetx
, newtargety
;
244 /* XXX find the middle monitor? */
245 screen_area
= screen_physical_area_monitor(0);
247 /* get the outside margins */
248 RrMargins(p
->a_bg
, &ml
, &mt
, &mr
, &mb
);
250 /* get our outside borders */
251 l
= ml
+ OUTSIDE_BORDER
;
252 r
= mr
+ OUTSIDE_BORDER
;
253 t
= mt
+ OUTSIDE_BORDER
;
254 b
= mb
+ OUTSIDE_BORDER
;
256 /* get the icon pictures' sizes */
257 innerw
= ICON_SIZE
- (ICON_HILITE_WIDTH
+ ICON_HILITE_MARGIN
) * 2;
258 innerh
= ICON_SIZE
- (ICON_HILITE_WIDTH
+ ICON_HILITE_MARGIN
) * 2;
260 /* get the width from the text and keep it within limits */
261 w
= l
+ r
+ p
->maxtextw
;
262 w
= MIN(w
, MAX(screen_area
->width
/3, POPUP_WIDTH
)); /* max width */
263 w
= MAX(w
, POPUP_WIDTH
); /* min width */
265 /* how many icons will fit in that row? make the width fit that */
267 icons_per_row
= (w
+ ICON_SIZE
- 1) / ICON_SIZE
;
268 w
= icons_per_row
* ICON_SIZE
+ l
+ r
;
270 /* how many rows do we need? */
271 icon_rows
= (p
->n_targets
-1) / icons_per_row
+ 1;
273 /* get the text dimensions */
275 texth
= RrMinHeight(p
->a_text
);
277 /* find the height of the dialog */
278 h
= t
+ b
+ (icon_rows
* ICON_SIZE
) + (OUTSIDE_BORDER
+ texth
);
280 /* get the position of the text */
282 texty
= h
- texth
- b
;
284 /* find the position for the popup (include the outer borders) */
285 x
= screen_area
->x
+ (screen_area
->width
-
286 (w
+ ob_rr_theme
->fbwidth
* 2)) / 2;
287 y
= screen_area
->y
+ (screen_area
->height
-
288 (h
+ ob_rr_theme
->fbwidth
* 2)) / 2;
290 /* get the dimensions of the target hilite texture */
296 /* center the icons if there is less than one row */
298 icons_center_x
= (w
- p
->n_targets
* ICON_SIZE
) / 2;
303 /* position the background but don't draw it*/
304 XMoveResizeWindow(ob_display
, p
->bg
, x
, y
, w
, h
);
306 /* set up the hilite texture for the background */
307 p
->a_bg
->texture
[0].data
.rgba
.width
= rgbaw
;
308 p
->a_bg
->texture
[0].data
.rgba
.height
= rgbah
;
309 p
->hilite_rgba
= g_new(RrPixel32
, rgbaw
* rgbah
);
310 p
->a_bg
->texture
[0].data
.rgba
.data
= p
->hilite_rgba
;
312 /* position the text, but don't draw it */
313 XMoveResizeWindow(ob_display
, p
->text
, textx
, texty
, textw
, texth
);
314 p
->a_text
->surface
.parentx
= textx
;
315 p
->a_text
->surface
.parenty
= texty
;
318 /* find the focused target */
319 for (i
= 0, it
= p
->targets
; it
; ++i
, it
= g_list_next(it
)) {
320 const ObFocusCyclePopupTarget
*target
= it
->data
;
321 const gint row
= i
/ icons_per_row
; /* starting from 0 */
322 const gint col
= i
% icons_per_row
; /* starting from 0 */
324 if (target
->client
== c
) {
325 /* save the target */
327 newtargetx
= icons_center_x
+ l
+ (col
* ICON_SIZE
);
328 newtargety
= t
+ (row
* ICON_SIZE
);
331 break; /* if we're not dimensioning, then we're done */
335 g_assert(newtarget
!= NULL
);
337 /* create the hilite under the target icon */
342 color
= ((ob_rr_theme
->osd_color
->r
& 0xff) << RrDefaultRedOffset
) +
343 ((ob_rr_theme
->osd_color
->g
& 0xff) << RrDefaultGreenOffset
) +
344 ((ob_rr_theme
->osd_color
->b
& 0xff) << RrDefaultBlueOffset
);
347 for (i
= 0; i
< rgbah
; ++i
)
348 for (j
= 0; j
< rgbaw
; ++j
) {
350 const gint x
= j
+ rgbax
- newtargetx
;
351 const gint y
= i
+ rgbay
- newtargety
;
353 if (x
< 0 || x
>= ICON_SIZE
||
354 y
< 0 || y
>= ICON_SIZE
)
356 /* outside the target */
359 else if (x
< ICON_HILITE_WIDTH
||
360 x
>= ICON_SIZE
- ICON_HILITE_WIDTH
||
361 y
< ICON_HILITE_WIDTH
||
362 y
>= ICON_SIZE
- ICON_HILITE_WIDTH
)
364 /* the border of the target */
368 /* the background of the target */
372 p
->hilite_rgba
[o
++] =
373 color
+ (a
<< RrDefaultAlphaOffset
);
377 /* * * draw everything * * */
379 /* draw the background */
380 RrPaint(p
->a_bg
, p
->bg
, w
, h
);
383 for (i
= 0, it
= p
->targets
; it
; ++i
, it
= g_list_next(it
)) {
384 const ObFocusCyclePopupTarget
*target
= it
->data
;
386 /* have to redraw the targetted icon and last targetted icon,
387 they can pick up the hilite changes in the backgroud */
388 if (!p
->mapped
|| newtarget
== target
|| p
->last_target
== target
) {
389 const ObClientIcon
*icon
;
390 const gint row
= i
/ icons_per_row
; /* starting from 0 */
391 const gint col
= i
% icons_per_row
; /* starting from 0 */
394 /* find the dimensions of the icon inside it */
395 innerx
= icons_center_x
+ l
+ (col
* ICON_SIZE
);
396 innerx
+= ICON_HILITE_WIDTH
+ ICON_HILITE_MARGIN
;
397 innery
= t
+ (row
* ICON_SIZE
);
398 innery
+= ICON_HILITE_WIDTH
+ ICON_HILITE_MARGIN
;
401 XMoveResizeWindow(ob_display
, target
->win
,
402 innerx
, innery
, innerw
, innerh
);
404 /* get the icon from the client */
405 icon
= client_icon(target
->client
, innerw
, innerh
);
406 p
->a_icon
->texture
[0].data
.rgba
.width
= icon
->width
;
407 p
->a_icon
->texture
[0].data
.rgba
.height
= icon
->height
;
408 p
->a_icon
->texture
[0].data
.rgba
.data
= icon
->data
;
411 p
->a_icon
->surface
.parentx
= innerx
;
412 p
->a_icon
->surface
.parenty
= innery
;
413 RrPaint(p
->a_icon
, target
->win
, innerw
, innerh
);
418 p
->a_text
->texture
[0].data
.text
.string
= newtarget
->text
;
419 p
->a_text
->surface
.parentx
= textx
;
420 p
->a_text
->surface
.parenty
= texty
;
421 RrPaint(p
->a_text
, p
->text
, textw
, texth
);
423 p
->last_target
= newtarget
;
426 void focus_cycle_popup_show(ObClient
*c
, gboolean iconic_windows
,
427 gboolean all_desktops
, gboolean dock_windows
,
428 gboolean desktop_windows
)
432 /* do this stuff only when the dialog is first showing */
434 popup_setup(&popup
, iconic_windows
, all_desktops
,
435 dock_windows
, desktop_windows
);
436 g_assert(popup
.targets
!= NULL
);
438 popup_render(&popup
, c
);
441 /* show the dialog */
442 XMapWindow(ob_display
, popup
.bg
);
448 void focus_cycle_popup_hide()
450 XUnmapWindow(ob_display
, popup
.bg
);
453 popup
.mapped
= FALSE
;
455 while(popup
.targets
) {
456 ObFocusCyclePopupTarget
*t
= popup
.targets
->data
;
459 XDestroyWindow(ob_display
, t
->win
);
461 popup
.targets
= g_list_delete_link(popup
.targets
, popup
.targets
);
464 popup
.last_target
= NULL
;
466 g_free(popup
.hilite_rgba
);
467 popup
.hilite_rgba
= NULL
;