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 /*! This popup shows all possible windows */
72 static ObFocusCyclePopup popup
;
73 /*! This popup shows a single window */
74 static ObIconPopup
*single_popup
;
76 static gchar
*popup_get_name (ObClient
*c
);
77 static void popup_setup (ObFocusCyclePopup
*p
,
78 gboolean create_targets
,
79 gboolean iconic_windows
,
80 gboolean all_desktops
,
81 gboolean dock_windows
,
82 gboolean desktop_windows
);
83 static void popup_render (ObFocusCyclePopup
*p
,
86 static Window
create_window(Window parent
, guint bwidth
, gulong mask
,
87 XSetWindowAttributes
*attr
)
89 return XCreateWindow(ob_display
, parent
, 0, 0, 1, 1, bwidth
,
90 RrDepth(ob_rr_inst
), InputOutput
,
91 RrVisual(ob_rr_inst
), mask
, attr
);
94 void focus_cycle_popup_startup(gboolean reconfig
)
96 XSetWindowAttributes attrib
;
98 single_popup
= icon_popup_new(TRUE
);
100 popup
.obwin
.type
= Window_Internal
;
101 popup
.a_bg
= RrAppearanceCopy(ob_rr_theme
->osd_hilite_bg
);
102 popup
.a_text
= RrAppearanceCopy(ob_rr_theme
->osd_hilite_label
);
103 popup
.a_icon
= RrAppearanceCopy(ob_rr_theme
->a_clear_tex
);
105 popup
.a_text
->surface
.parent
= popup
.a_bg
;
106 popup
.a_icon
->surface
.parent
= popup
.a_bg
;
108 popup
.a_icon
->texture
[0].type
= RR_TEXTURE_RGBA
;
110 RrAppearanceAddTextures(popup
.a_bg
, 1);
111 popup
.a_bg
->texture
[0].type
= RR_TEXTURE_RGBA
;
113 attrib
.override_redirect
= True
;
114 attrib
.border_pixel
=RrColorPixel(ob_rr_theme
->frame_focused_border_color
);
115 popup
.bg
= create_window(RootWindow(ob_display
, ob_screen
),
116 ob_rr_theme
->fbwidth
,
117 CWOverrideRedirect
| CWBorderPixel
, &attrib
);
119 popup
.text
= create_window(popup
.bg
, 0, 0, NULL
);
121 popup
.targets
= NULL
;
123 popup
.last_target
= NULL
;
125 popup
.hilite_rgba
= NULL
;
127 XMapWindow(ob_display
, popup
.text
);
129 stacking_add(INTERNAL_AS_WINDOW(&popup
));
132 void focus_cycle_popup_shutdown(gboolean reconfig
)
134 icon_popup_free(single_popup
);
136 stacking_remove(INTERNAL_AS_WINDOW(&popup
));
138 while(popup
.targets
) {
139 ObFocusCyclePopupTarget
*t
= popup
.targets
->data
;
142 XDestroyWindow(ob_display
, t
->win
);
144 popup
.targets
= g_list_delete_link(popup
.targets
, popup
.targets
);
147 g_free(popup
.hilite_rgba
);
149 XDestroyWindow(ob_display
, popup
.text
);
150 XDestroyWindow(ob_display
, popup
.bg
);
152 RrAppearanceFree(popup
.a_icon
);
153 RrAppearanceFree(popup
.a_text
);
154 RrAppearanceFree(popup
.a_bg
);
157 static void popup_setup(ObFocusCyclePopup
*p
, gboolean create_targets
,
158 gboolean iconic_windows
, gboolean all_desktops
,
159 gboolean dock_windows
, gboolean desktop_windows
)
164 g_assert(p
->targets
== NULL
);
165 g_assert(p
->n_targets
== 0);
167 /* make its width to be the width of all the possible titles */
169 /* build a list of all the valid focus targets and measure their strings,
173 for (it
= g_list_last(focus_order
); it
; it
= g_list_previous(it
)) {
174 ObClient
*ft
= it
->data
;
176 if (focus_cycle_target_valid(ft
,
182 gchar
*text
= popup_get_name(ft
);
185 p
->a_text
->texture
[0].data
.text
.string
= text
;
186 maxwidth
= MAX(maxwidth
, RrMinWidth(p
->a_text
));
191 ObFocusCyclePopupTarget
*t
= g_new(ObFocusCyclePopupTarget
, 1);
195 t
->win
= create_window(p
->bg
, 0, 0, NULL
);
197 XMapWindow(ob_display
, t
->win
);
199 p
->targets
= g_list_prepend(p
->targets
, t
);
206 p
->maxtextw
= maxwidth
;
209 static gchar
*popup_get_name(ObClient
*c
)
213 const gchar
*desk
= NULL
;
216 /* find our highest direct parent, including non-normal windows */
217 for (p
= c
; p
->transient_for
&& p
->transient_for
!= OB_TRAN_GROUP
;
218 p
= p
->transient_for
);
220 if (c
->desktop
!= DESKTOP_ALL
&& c
->desktop
!= screen_desktop
)
221 desk
= screen_desktop_names
[c
->desktop
];
223 /* use the transient's parent's title/icon if we don't have one */
224 if (p
!= c
&& !strcmp("", (c
->iconic
? c
->icon_title
: c
->title
)))
225 title
= g_strdup(p
->iconic
? p
->icon_title
: p
->title
);
228 title
= g_strdup(c
->iconic
? c
->icon_title
: c
->title
);
231 ret
= g_strdup_printf("%s [%s]", title
, desk
);
241 static void popup_render(ObFocusCyclePopup
*p
, const ObClient
*c
)
249 gint textx
, texty
, textw
, texth
;
250 gint rgbax
, rgbay
, rgbaw
, rgbah
;
255 const ObFocusCyclePopupTarget
*newtarget
;
256 gint newtargetx
, newtargety
;
258 /* XXX find the middle monitor? */
259 screen_area
= screen_physical_area_monitor(0);
261 /* get the outside margins */
262 RrMargins(p
->a_bg
, &ml
, &mt
, &mr
, &mb
);
264 /* get our outside borders */
265 l
= ml
+ OUTSIDE_BORDER
;
266 r
= mr
+ OUTSIDE_BORDER
;
267 t
= mt
+ OUTSIDE_BORDER
;
268 b
= mb
+ OUTSIDE_BORDER
;
270 /* get the icon pictures' sizes */
271 innerw
= ICON_SIZE
- (ICON_HILITE_WIDTH
+ ICON_HILITE_MARGIN
) * 2;
272 innerh
= ICON_SIZE
- (ICON_HILITE_WIDTH
+ ICON_HILITE_MARGIN
) * 2;
274 /* get the width from the text and keep it within limits */
275 w
= l
+ r
+ p
->maxtextw
;
276 w
= MIN(w
, MAX(screen_area
->width
/3, POPUP_WIDTH
)); /* max width */
277 w
= MAX(w
, POPUP_WIDTH
); /* min width */
279 /* how many icons will fit in that row? make the width fit that */
281 icons_per_row
= (w
+ ICON_SIZE
- 1) / ICON_SIZE
;
282 w
= icons_per_row
* ICON_SIZE
+ l
+ r
;
284 /* how many rows do we need? */
285 icon_rows
= (p
->n_targets
-1) / icons_per_row
+ 1;
287 /* get the text dimensions */
289 texth
= RrMinHeight(p
->a_text
);
291 /* find the height of the dialog */
292 h
= t
+ b
+ (icon_rows
* ICON_SIZE
) + (OUTSIDE_BORDER
+ texth
);
294 /* get the position of the text */
296 texty
= h
- texth
- b
;
298 /* find the position for the popup (include the outer borders) */
299 x
= screen_area
->x
+ (screen_area
->width
-
300 (w
+ ob_rr_theme
->fbwidth
* 2)) / 2;
301 y
= screen_area
->y
+ (screen_area
->height
-
302 (h
+ ob_rr_theme
->fbwidth
* 2)) / 2;
304 /* get the dimensions of the target hilite texture */
310 /* center the icons if there is less than one row */
312 icons_center_x
= (w
- p
->n_targets
* ICON_SIZE
) / 2;
317 /* position the background but don't draw it*/
318 XMoveResizeWindow(ob_display
, p
->bg
, x
, y
, w
, h
);
320 /* set up the hilite texture for the background */
321 p
->a_bg
->texture
[0].data
.rgba
.width
= rgbaw
;
322 p
->a_bg
->texture
[0].data
.rgba
.height
= rgbah
;
323 p
->hilite_rgba
= g_new(RrPixel32
, rgbaw
* rgbah
);
324 p
->a_bg
->texture
[0].data
.rgba
.data
= p
->hilite_rgba
;
326 /* position the text, but don't draw it */
327 XMoveResizeWindow(ob_display
, p
->text
, textx
, texty
, textw
, texth
);
328 p
->a_text
->surface
.parentx
= textx
;
329 p
->a_text
->surface
.parenty
= texty
;
332 /* find the focused target */
333 for (i
= 0, it
= p
->targets
; it
; ++i
, it
= g_list_next(it
)) {
334 const ObFocusCyclePopupTarget
*target
= it
->data
;
335 const gint row
= i
/ icons_per_row
; /* starting from 0 */
336 const gint col
= i
% icons_per_row
; /* starting from 0 */
338 if (target
->client
== c
) {
339 /* save the target */
341 newtargetx
= icons_center_x
+ l
+ (col
* ICON_SIZE
);
342 newtargety
= t
+ (row
* ICON_SIZE
);
345 break; /* if we're not dimensioning, then we're done */
349 g_assert(newtarget
!= NULL
);
351 /* create the hilite under the target icon */
356 color
= ((ob_rr_theme
->osd_color
->r
& 0xff) << RrDefaultRedOffset
) +
357 ((ob_rr_theme
->osd_color
->g
& 0xff) << RrDefaultGreenOffset
) +
358 ((ob_rr_theme
->osd_color
->b
& 0xff) << RrDefaultBlueOffset
);
361 for (i
= 0; i
< rgbah
; ++i
)
362 for (j
= 0; j
< rgbaw
; ++j
) {
364 const gint x
= j
+ rgbax
- newtargetx
;
365 const gint y
= i
+ rgbay
- newtargety
;
367 if (x
< 0 || x
>= ICON_SIZE
||
368 y
< 0 || y
>= ICON_SIZE
)
370 /* outside the target */
373 else if (x
< ICON_HILITE_WIDTH
||
374 x
>= ICON_SIZE
- ICON_HILITE_WIDTH
||
375 y
< ICON_HILITE_WIDTH
||
376 y
>= ICON_SIZE
- ICON_HILITE_WIDTH
)
378 /* the border of the target */
382 /* the background of the target */
386 p
->hilite_rgba
[o
++] =
387 color
+ (a
<< RrDefaultAlphaOffset
);
391 /* * * draw everything * * */
393 /* draw the background */
394 RrPaint(p
->a_bg
, p
->bg
, w
, h
);
397 for (i
= 0, it
= p
->targets
; it
; ++i
, it
= g_list_next(it
)) {
398 const ObFocusCyclePopupTarget
*target
= it
->data
;
400 /* have to redraw the targetted icon and last targetted icon,
401 they can pick up the hilite changes in the backgroud */
402 if (!p
->mapped
|| newtarget
== target
|| p
->last_target
== target
) {
403 const ObClientIcon
*icon
;
404 const gint row
= i
/ icons_per_row
; /* starting from 0 */
405 const gint col
= i
% icons_per_row
; /* starting from 0 */
408 /* find the dimensions of the icon inside it */
409 innerx
= icons_center_x
+ l
+ (col
* ICON_SIZE
);
410 innerx
+= ICON_HILITE_WIDTH
+ ICON_HILITE_MARGIN
;
411 innery
= t
+ (row
* ICON_SIZE
);
412 innery
+= ICON_HILITE_WIDTH
+ ICON_HILITE_MARGIN
;
415 XMoveResizeWindow(ob_display
, target
->win
,
416 innerx
, innery
, innerw
, innerh
);
418 /* get the icon from the client */
419 icon
= client_icon(target
->client
, innerw
, innerh
);
420 p
->a_icon
->texture
[0].data
.rgba
.width
= icon
->width
;
421 p
->a_icon
->texture
[0].data
.rgba
.height
= icon
->height
;
422 p
->a_icon
->texture
[0].data
.rgba
.data
= icon
->data
;
425 p
->a_icon
->surface
.parentx
= innerx
;
426 p
->a_icon
->surface
.parenty
= innery
;
427 RrPaint(p
->a_icon
, target
->win
, innerw
, innerh
);
432 p
->a_text
->texture
[0].data
.text
.justify
= RR_JUSTIFY_CENTER
;
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
;
441 void focus_cycle_popup_show(ObClient
*c
, gboolean iconic_windows
,
442 gboolean all_desktops
, gboolean dock_windows
,
443 gboolean desktop_windows
)
447 /* do this stuff only when the dialog is first showing */
449 popup_setup(&popup
, TRUE
, iconic_windows
, all_desktops
,
450 dock_windows
, desktop_windows
);
451 g_assert(popup
.targets
!= NULL
);
453 popup_render(&popup
, c
);
456 /* show the dialog */
457 XMapWindow(ob_display
, popup
.bg
);
463 void focus_cycle_popup_hide()
465 XUnmapWindow(ob_display
, popup
.bg
);
468 popup
.mapped
= FALSE
;
470 while(popup
.targets
) {
471 ObFocusCyclePopupTarget
*t
= popup
.targets
->data
;
474 XDestroyWindow(ob_display
, t
->win
);
476 popup
.targets
= g_list_delete_link(popup
.targets
, popup
.targets
);
479 popup
.last_target
= NULL
;
481 g_free(popup
.hilite_rgba
);
482 popup
.hilite_rgba
= NULL
;
485 void focus_cycle_popup_single_show(struct _ObClient
*c
,
486 gboolean iconic_windows
,
487 gboolean all_desktops
,
488 gboolean dock_windows
,
489 gboolean desktop_windows
)
495 /* do this stuff only when the dialog is first showing */
499 popup_setup(&popup
, FALSE
, iconic_windows
, all_desktops
,
500 dock_windows
, desktop_windows
);
501 g_assert(popup
.targets
== NULL
);
503 /* position the popup */
504 a
= screen_physical_area_monitor(0);
505 icon_popup_position(single_popup
, CenterGravity
,
506 a
->x
+ a
->width
/ 2, a
->y
+ a
->height
/ 2);
507 icon_popup_height(single_popup
, POPUP_HEIGHT
);
508 icon_popup_min_width(single_popup
, POPUP_WIDTH
);
509 icon_popup_max_width(single_popup
, MAX(a
->width
/3, POPUP_WIDTH
));
510 icon_popup_text_width(single_popup
, popup
.maxtextw
);
513 text
= popup_get_name(c
);
514 icon_popup_show(single_popup
, text
, client_icon(c
, ICON_SIZE
, ICON_SIZE
));
518 void focus_cycle_popup_single_hide()
520 icon_popup_hide(single_popup
);