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
;
240 const ObFocusCyclePopupTarget
*newtarget
;
241 gint newtargetx
, newtargety
;
243 /* XXX find the middle monitor? */
244 screen_area
= screen_physical_area_monitor(0);
246 /* get the outside margins */
247 RrMargins(p
->a_bg
, &ml
, &mt
, &mr
, &mb
);
249 /* get our outside borders */
250 l
= ml
+ OUTSIDE_BORDER
;
251 r
= mr
+ OUTSIDE_BORDER
;
252 t
= mt
+ OUTSIDE_BORDER
;
253 b
= mb
+ OUTSIDE_BORDER
;
255 /* get the icon pictures' sizes */
256 innerw
= ICON_SIZE
- (ICON_HILITE_WIDTH
+ ICON_HILITE_MARGIN
) * 2;
257 innerh
= ICON_SIZE
- (ICON_HILITE_WIDTH
+ ICON_HILITE_MARGIN
) * 2;
259 /* get the width from the text and keep it within limits */
260 w
= l
+ r
+ p
->maxtextw
;
261 w
= MIN(w
, MAX(screen_area
->width
/3, POPUP_WIDTH
)); /* max width */
262 w
= MAX(w
, POPUP_WIDTH
); /* min width */
264 /* how many icons will fit in that row? make the width fit that */
266 icons_per_row
= w
/ ICON_SIZE
;
267 w
= icons_per_row
* ICON_SIZE
+ l
+ r
;
269 /* how many rows do we need? */
270 icon_rows
= (p
->n_targets
-1) / icons_per_row
+ 1;
272 /* get the text dimensions */
274 texth
= RrMinHeight(p
->a_text
);
276 /* find the height of the dialog */
277 h
= t
+ b
+ (icon_rows
* ICON_SIZE
) + (OUTSIDE_BORDER
+ texth
);
279 /* get the position of the text */
281 texty
= h
- texth
- b
;
283 /* find the position for the popup (include the outer borders) */
284 x
= screen_area
->x
+ (screen_area
->width
-
285 (w
+ ob_rr_theme
->fbwidth
* 2)) / 2;
286 y
= screen_area
->y
+ (screen_area
->height
-
287 (h
+ ob_rr_theme
->fbwidth
* 2)) / 2;
289 /* get the dimensions of the target hilite texture */
296 /* position the background but don't draw it*/
297 XMoveResizeWindow(ob_display
, p
->bg
, x
, y
, w
, h
);
299 /* set up the hilite texture for the background */
300 p
->a_bg
->texture
[0].data
.rgba
.width
= rgbaw
;
301 p
->a_bg
->texture
[0].data
.rgba
.height
= rgbah
;
302 p
->hilite_rgba
= g_new(RrPixel32
, rgbaw
* rgbah
);
303 p
->a_bg
->texture
[0].data
.rgba
.data
= p
->hilite_rgba
;
305 /* position the text, but don't draw it */
306 XMoveResizeWindow(ob_display
, p
->text
, textx
, texty
, textw
, texth
);
307 p
->a_text
->surface
.parentx
= textx
;
308 p
->a_text
->surface
.parenty
= texty
;
311 /* find the focused target */
312 for (i
= 0, it
= p
->targets
; it
; ++i
, it
= g_list_next(it
)) {
313 const ObFocusCyclePopupTarget
*target
= it
->data
;
314 const gint row
= i
/ icons_per_row
; /* starting from 0 */
315 const gint col
= i
% icons_per_row
; /* starting from 0 */
317 if (target
->client
== c
) {
318 /* save the target */
320 newtargetx
= l
+ (col
* ICON_SIZE
);
321 newtargety
= t
+ (row
* ICON_SIZE
);
324 break; /* if we're not dimensioning, then we're done */
328 g_assert(newtarget
!= NULL
);
330 /* create the hilite under the target icon */
335 color
= ((ob_rr_theme
->osd_color
->r
& 0xff) << RrDefaultRedOffset
) +
336 ((ob_rr_theme
->osd_color
->g
& 0xff) << RrDefaultGreenOffset
) +
337 ((ob_rr_theme
->osd_color
->b
& 0xff) << RrDefaultBlueOffset
);
340 for (i
= 0; i
< rgbah
; ++i
)
341 for (j
= 0; j
< rgbaw
; ++j
) {
343 const gint x
= j
+ rgbax
- newtargetx
;
344 const gint y
= i
+ rgbay
- newtargety
;
346 if (x
< 0 || x
>= ICON_SIZE
||
347 y
< 0 || y
>= ICON_SIZE
)
349 /* outside the target */
352 else if (x
< ICON_HILITE_WIDTH
||
353 x
>= ICON_SIZE
- ICON_HILITE_WIDTH
||
354 y
< ICON_HILITE_WIDTH
||
355 y
>= ICON_SIZE
- ICON_HILITE_WIDTH
)
357 /* the border of the target */
361 /* the background of the target */
365 p
->hilite_rgba
[o
++] =
366 color
+ (a
<< RrDefaultAlphaOffset
);
370 /* * * draw everything * * */
372 /* draw the background */
373 RrPaint(p
->a_bg
, p
->bg
, w
, h
);
376 for (i
= 0, it
= p
->targets
; it
; ++i
, it
= g_list_next(it
)) {
377 const ObFocusCyclePopupTarget
*target
= it
->data
;
379 /* have to redraw the targetted icon and last targetted icon,
380 they can pick up the hilite changes in the backgroud */
381 if (!p
->mapped
|| newtarget
== target
|| p
->last_target
== target
) {
382 const ObClientIcon
*icon
;
383 const gint row
= i
/ icons_per_row
; /* starting from 0 */
384 const gint col
= i
% icons_per_row
; /* starting from 0 */
387 /* find the dimensions of the icon inside it */
388 innerx
= l
+ (col
* ICON_SIZE
);
389 innerx
+= ICON_HILITE_WIDTH
+ ICON_HILITE_MARGIN
;
390 innery
= t
+ (row
* ICON_SIZE
);
391 innery
+= ICON_HILITE_WIDTH
+ ICON_HILITE_MARGIN
;
394 XMoveResizeWindow(ob_display
, target
->win
,
395 innerx
, innery
, innerw
, innerh
);
397 /* get the icon from the client */
398 icon
= client_icon(target
->client
, innerw
, innerh
);
399 p
->a_icon
->texture
[0].data
.rgba
.width
= icon
->width
;
400 p
->a_icon
->texture
[0].data
.rgba
.height
= icon
->height
;
401 p
->a_icon
->texture
[0].data
.rgba
.data
= icon
->data
;
404 p
->a_icon
->surface
.parentx
= innerx
;
405 p
->a_icon
->surface
.parenty
= innery
;
406 RrPaint(p
->a_icon
, target
->win
, innerw
, innerh
);
411 p
->a_text
->texture
[0].data
.text
.string
= newtarget
->text
;
412 p
->a_text
->surface
.parentx
= textx
;
413 p
->a_text
->surface
.parenty
= texty
;
414 RrPaint(p
->a_text
, p
->text
, textw
, texth
);
416 p
->last_target
= newtarget
;
419 void focus_cycle_popup_show(ObClient
*c
, gboolean iconic_windows
,
420 gboolean all_desktops
, gboolean dock_windows
,
421 gboolean desktop_windows
)
425 /* do this stuff only when the dialog is first showing */
427 popup_setup(&popup
, iconic_windows
, all_desktops
,
428 dock_windows
, desktop_windows
);
429 g_assert(popup
.targets
!= NULL
);
431 popup_render(&popup
, c
);
434 /* show the dialog */
435 XMapWindow(ob_display
, popup
.bg
);
441 void focus_cycle_popup_hide()
443 XUnmapWindow(ob_display
, popup
.bg
);
446 popup
.mapped
= FALSE
;
448 while(popup
.targets
) {
449 ObFocusCyclePopupTarget
*t
= popup
.targets
->data
;
452 XDestroyWindow(ob_display
, t
->win
);
454 popup
.targets
= g_list_delete_link(popup
.targets
, popup
.targets
);
457 popup
.last_target
= NULL
;
459 g_free(popup
.hilite_rgba
);
460 popup
.hilite_rgba
= NULL
;