Merge branch 'backport'
[dana/openbox.git] / openbox / focus_cycle_popup.c
1 /* -*- indent-tabs-mode: nil; tab-width: 4; c-basic-offset: 4; -*-
2
3    focus_cycle_popup.c for the Openbox window manager
4    Copyright (c) 2006        Mikael Magnusson
5    Copyright (c) 2003-2007   Dana Jansens
6
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.
11
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.
16
17    See the COPYING file for a copy of the GNU General Public License.
18 */
19
20 #include "focus_cycle_popup.h"
21 #include "popup.h"
22 #include "client.h"
23 #include "screen.h"
24 #include "focus.h"
25 #include "openbox.h"
26 #include "window.h"
27 #include "event.h"
28 #include "render/render.h"
29
30 #include <X11/Xlib.h>
31 #include <glib.h>
32
33 /* Size of the hilite box around a window's icon */
34 #define HILITE_SIZE 40
35 /* Width of the outer ring around the hilite box */
36 #define HILITE_WIDTH 2
37 /* Space between the outer ring around the hilite box and the icon inside it */
38 #define HILITE_MARGIN 1
39 /* Total distance from the edge of the hilite box to the icon inside it */
40 #define HILITE_OFFSET (HILITE_WIDTH + HILITE_MARGIN)
41 /* Size of the icons, which can appear inside or outside of a hilite box */
42 #define ICON_SIZE (HILITE_SIZE - 2*HILITE_OFFSET)
43 /* Margin area around the outside of the dialog */
44 #define OUTSIDE_BORDER 3
45 /* Margin area around the text */
46 #define TEXT_BORDER 2
47 /* Scroll the list-mode list when the cursor gets within this many rows of the
48    top or bottom */
49 #define SCROLL_MARGIN 4
50
51 typedef struct _ObFocusCyclePopup       ObFocusCyclePopup;
52 typedef struct _ObFocusCyclePopupTarget ObFocusCyclePopupTarget;
53
54 struct _ObFocusCyclePopupTarget
55 {
56     ObClient *client;
57     gchar *text;
58     Window iconwin;
59     /* This is used when the popup is in list mode */
60     Window textwin;
61 };
62
63 struct _ObFocusCyclePopup
64 {
65     ObWindow obwin;
66     Window bg;
67
68     /* This is used when the popup is in icon mode */
69     Window icon_mode_text;
70
71     Window list_mode_up;
72     Window list_mode_down;
73
74     GList *targets;
75     gint n_targets;
76
77     const ObFocusCyclePopupTarget *last_target;
78
79     gint maxtextw;
80
81     /* How are the list is scrolled, in scroll mode */
82     gint scroll;
83
84     RrAppearance *a_bg;
85     RrAppearance *a_text;
86     RrAppearance *a_hilite_text;
87     RrAppearance *a_icon;
88     RrAppearance *a_arrow;
89
90     gboolean mapped;
91     ObFocusCyclePopupMode mode;
92 };
93
94 /*! This popup shows all possible windows */
95 static ObFocusCyclePopup popup;
96 /*! This popup shows a single window */
97 static ObIconPopup *single_popup;
98
99 static gchar *popup_get_name (ObClient *c);
100 static void   popup_setup    (ObFocusCyclePopup *p,
101                               gboolean create_targets,
102                               gboolean iconic_windows,
103                               gboolean all_desktops,
104                               gboolean dock_windows,
105                               gboolean desktop_windows);
106 static void   popup_render   (ObFocusCyclePopup *p,
107                               const ObClient *c);
108
109 static Window create_window(Window parent, guint bwidth, gulong mask,
110                             XSetWindowAttributes *attr)
111 {
112     return XCreateWindow(obt_display, parent, 0, 0, 1, 1, bwidth,
113                          RrDepth(ob_rr_inst), InputOutput,
114                          RrVisual(ob_rr_inst), mask, attr);
115 }
116
117 void focus_cycle_popup_startup(gboolean reconfig)
118 {
119     XSetWindowAttributes attrib;
120     RrPixel32 *p;
121
122     single_popup = icon_popup_new();
123
124     popup.obwin.type = OB_WINDOW_CLASS_INTERNAL;
125     popup.a_bg = RrAppearanceCopy(ob_rr_theme->osd_hilite_bg);
126     popup.a_hilite_text = RrAppearanceCopy(ob_rr_theme->osd_hilite_label);
127     popup.a_text = RrAppearanceCopy(ob_rr_theme->a_unfocused_label);
128     popup.a_icon = RrAppearanceCopy(ob_rr_theme->a_clear);
129     popup.a_arrow = RrAppearanceCopy(ob_rr_theme->a_clear_tex);
130
131     popup.a_hilite_text->surface.parent = popup.a_bg;
132     popup.a_text->surface.parent = popup.a_bg;
133     popup.a_icon->surface.parent = popup.a_bg;
134
135     popup.a_text->texture[0].data.text.justify = RR_JUSTIFY_LEFT;
136     popup.a_hilite_text->texture[0].data.text.justify = RR_JUSTIFY_LEFT;
137
138     /* 2 textures. texture[0] is the icon.  texture[1] is the hilight, and
139        may or may not be used */
140     RrAppearanceAddTextures(popup.a_icon, 2);
141
142     popup.a_icon->texture[0].type = RR_TEXTURE_RGBA;
143
144     popup.a_arrow->texture[0].type = RR_TEXTURE_MASK;
145     popup.a_arrow->texture[0].data.mask.color =
146         ob_rr_theme->osd_color;
147
148     attrib.override_redirect = True;
149     attrib.border_pixel=RrColorPixel(ob_rr_theme->osd_border_color);
150     popup.bg = create_window(obt_root(ob_screen), ob_rr_theme->obwidth,
151                              CWOverrideRedirect | CWBorderPixel, &attrib);
152
153     /* create the text window used for the icon-mode popup */
154     popup.icon_mode_text = create_window(popup.bg, 0, 0, NULL);
155
156     /* create the windows for the up and down arrows */
157     popup.list_mode_up = create_window(popup.bg, 0, 0, NULL);
158     popup.list_mode_down = create_window(popup.bg, 0, 0, NULL);
159
160     popup.targets = NULL;
161     popup.n_targets = 0;
162     popup.last_target = NULL;
163
164     /* set up the hilite texture for the icon */
165     popup.a_icon->texture[1].data.rgba.width = HILITE_SIZE;
166     popup.a_icon->texture[1].data.rgba.height = HILITE_SIZE;
167     popup.a_icon->texture[1].data.rgba.alpha = 0xff;
168     p = g_new(RrPixel32, HILITE_SIZE * HILITE_SIZE);
169     popup.a_icon->texture[1].data.rgba.data = p;
170
171     /* create the hilite under the target icon */
172     {
173         RrPixel32 color;
174         gint x, y, o;
175
176         color = ((ob_rr_theme->osd_color->r & 0xff) << RrDefaultRedOffset) +
177             ((ob_rr_theme->osd_color->g & 0xff) << RrDefaultGreenOffset) +
178             ((ob_rr_theme->osd_color->b & 0xff) << RrDefaultBlueOffset);
179
180         o = 0;
181         for (x = 0; x < HILITE_SIZE; x++)
182             for (y = 0; y < HILITE_SIZE; y++) {
183                 guchar a;
184
185                 if (x < HILITE_WIDTH ||
186                     x >= HILITE_SIZE - HILITE_WIDTH ||
187                     y < HILITE_WIDTH ||
188                     y >= HILITE_SIZE - HILITE_WIDTH)
189                 {
190                     /* the border of the target */
191                     a = 0x88;
192                 } else {
193                     /* the background of the target */
194                     a = 0x22;
195                 }
196
197                 p[o++] = color + (a << RrDefaultAlphaOffset);
198             }
199     }
200
201     stacking_add(INTERNAL_AS_WINDOW(&popup));
202     window_add(&popup.bg, INTERNAL_AS_WINDOW(&popup));
203 }
204
205 void focus_cycle_popup_shutdown(gboolean reconfig)
206 {
207     icon_popup_free(single_popup);
208
209     window_remove(popup.bg);
210     stacking_remove(INTERNAL_AS_WINDOW(&popup));
211
212     while(popup.targets) {
213         ObFocusCyclePopupTarget *t = popup.targets->data;
214
215         g_free(t->text);
216         XDestroyWindow(obt_display, t->iconwin);
217         XDestroyWindow(obt_display, t->textwin);
218
219         popup.targets = g_list_delete_link(popup.targets, popup.targets);
220     }
221
222     g_free(popup.a_icon->texture[1].data.rgba.data);
223     popup.a_icon->texture[1].data.rgba.data = NULL;
224
225     XDestroyWindow(obt_display, popup.list_mode_up);
226     XDestroyWindow(obt_display, popup.list_mode_down);
227     XDestroyWindow(obt_display, popup.icon_mode_text);
228     XDestroyWindow(obt_display, popup.bg);
229
230     RrAppearanceFree(popup.a_arrow);
231     RrAppearanceFree(popup.a_icon);
232     RrAppearanceFree(popup.a_hilite_text);
233     RrAppearanceFree(popup.a_text);
234     RrAppearanceFree(popup.a_bg);
235 }
236
237 static void popup_setup(ObFocusCyclePopup *p, gboolean create_targets,
238                         gboolean iconic_windows, gboolean all_desktops,
239                         gboolean dock_windows, gboolean desktop_windows)
240 {
241     gint maxwidth, n;
242     GList *it;
243
244     g_assert(p->targets == NULL);
245     g_assert(p->n_targets == 0);
246
247     /* make its width to be the width of all the possible titles */
248
249     /* build a list of all the valid focus targets and measure their strings,
250        and count them */
251     maxwidth = 0;
252     n = 0;
253     for (it = g_list_last(focus_order); it; it = g_list_previous(it)) {
254         ObClient *ft = it->data;
255
256         if (focus_valid_target(ft, TRUE,
257                                iconic_windows,
258                                all_desktops,
259                                dock_windows,
260                                desktop_windows))
261         {
262             gchar *text = popup_get_name(ft);
263
264             /* measure */
265             p->a_text->texture[0].data.text.string = text;
266             maxwidth = MAX(maxwidth, RrMinWidth(p->a_text));
267
268             if (!create_targets) {
269                 g_free(text);
270             } else {
271                 ObFocusCyclePopupTarget *t = g_new(ObFocusCyclePopupTarget, 1);
272
273                 t->client = ft;
274                 t->text = text;
275                 t->iconwin = create_window(p->bg, 0, 0, NULL);
276                 t->textwin = create_window(p->bg, 0, 0, NULL);
277
278                 p->targets = g_list_prepend(p->targets, t);
279                 ++n;
280             }
281         }
282     }
283
284     p->n_targets = n;
285     p->maxtextw = maxwidth;
286 }
287
288 static gchar *popup_get_name(ObClient *c)
289 {
290     ObClient *p;
291     gchar *title;
292     const gchar *desk = NULL;
293     gchar *ret;
294
295     /* find our highest direct parent */
296     p = client_search_top_direct_parent(c);
297
298     if (c->desktop != DESKTOP_ALL && c->desktop != screen_desktop)
299         desk = screen_desktop_names[c->desktop];
300
301     title = c->iconic ? c->icon_title : c->title;
302
303     /* use the transient's parent's title/icon if we don't have one */
304     if (p != c && title[0] == '\0')
305         title = p->iconic ? p->icon_title : p->title;
306
307     if (desk)
308         ret = g_strdup_printf("%s [%s]", title, desk);
309     else
310         ret = g_strdup(title);
311
312     return ret;
313 }
314
315 static void popup_render(ObFocusCyclePopup *p, const ObClient *c)
316 {
317     gint ml, mt, mr, mb;
318     gint l, t, r, b;
319     gint x, y, w, h;
320     Rect *screen_area = NULL;
321     gint rgbax, rgbay, rgbaw, rgbah;
322     gint i;
323     GList *it;
324     const ObFocusCyclePopupTarget *newtarget;
325     gint icons_per_row;
326     gint icon_rows;
327     gint textw, texth;
328     gint selected_pos;
329     gint last_scroll;
330
331     /* vars for icon mode */
332     gint icon_mode_textx;
333     gint icon_mode_texty;
334     gint icons_center_x;
335
336     /* vars for list mode */
337     gint list_mode_icon_column_w = HILITE_SIZE + OUTSIDE_BORDER;
338     gint up_arrow_x, down_arrow_x;
339     gint up_arrow_y, down_arrow_y;
340     gboolean showing_arrows = FALSE;
341
342     g_assert(p->mode == OB_FOCUS_CYCLE_POPUP_MODE_ICONS ||
343              p->mode == OB_FOCUS_CYCLE_POPUP_MODE_LIST);
344
345     screen_area = screen_physical_area_active();
346
347     /* get the outside margins */
348     RrMargins(p->a_bg, &ml, &mt, &mr, &mb);
349
350     /* get our outside borders */
351     l = ml + OUTSIDE_BORDER;
352     r = mr + OUTSIDE_BORDER;
353     t = mt + OUTSIDE_BORDER;
354     b = mb + OUTSIDE_BORDER;
355
356     /* get the width from the text and keep it within limits */
357     w = l + r + p->maxtextw;
358     if (p->mode == OB_FOCUS_CYCLE_POPUP_MODE_LIST)
359         /* when in list mode, there are icons down the side */
360         w += list_mode_icon_column_w;
361     w = MIN(w, MAX(screen_area->width/3, POPUP_WIDTH)); /* max width */
362     w = MAX(w, POPUP_WIDTH); /* min width */
363
364     /* get the text height */
365     texth = RrMinHeight(p->a_hilite_text);
366     if (p->mode == OB_FOCUS_CYCLE_POPUP_MODE_LIST)
367         texth = MAX(MAX(texth, RrMinHeight(p->a_text)), HILITE_SIZE);
368     else
369         texth += TEXT_BORDER * 2;
370
371     if (p->mode == OB_FOCUS_CYCLE_POPUP_MODE_ICONS) {
372         /* how many icons will fit in that row? make the width fit that */
373         w -= l + r;
374         icons_per_row = (w + HILITE_SIZE - 1) / HILITE_SIZE;
375         w = icons_per_row * HILITE_SIZE + l + r;
376
377         /* how many rows do we need? */
378         icon_rows = (p->n_targets-1) / icons_per_row + 1;
379     } else {
380         /* in list mode, there is one column of icons.. */
381         icons_per_row = 1;
382         /* maximum is 80% of the screen height */
383         icon_rows = MIN(p->n_targets,
384                         (4*screen_area->height/5) /* 80% of the screen */
385                         /
386                         MAX(HILITE_SIZE, texth)); /* height of each row */
387         /* but make sure there is always one */
388         icon_rows = MAX(icon_rows, 1);
389     }
390
391     /* get the text width */
392     textw = w - l - r;
393     if (p->mode == OB_FOCUS_CYCLE_POPUP_MODE_LIST)
394         /* leave space on the side for the icons */
395         textw -= list_mode_icon_column_w;
396
397     if (!p->mapped)
398         /* reset the scrolling when the dialog is first shown */
399         p->scroll = 0;
400
401     /* find the height of the dialog */
402     h = t + b + (icon_rows * MAX(HILITE_SIZE, texth));
403     if (p->mode == OB_FOCUS_CYCLE_POPUP_MODE_ICONS)
404         /* in icon mode the text sits below the icons, so make some space */
405         h += OUTSIDE_BORDER + texth;
406
407     /* find the focused target */
408     newtarget = NULL;
409     for (i = 0, it = p->targets; it; ++i, it = g_list_next(it)) {
410         const ObFocusCyclePopupTarget *target = it->data;
411         if (target->client == c) {
412             /* save the target */
413             newtarget = target;
414             break;
415         }
416     }
417     selected_pos = i;
418     g_assert(newtarget != NULL);
419
420     /* scroll the list if needed */
421     last_scroll = p->scroll;
422     if (p->mode == OB_FOCUS_CYCLE_POPUP_MODE_LIST) {
423         const gint top = p->scroll + SCROLL_MARGIN;
424         const gint bottom = p->scroll + icon_rows - SCROLL_MARGIN;
425         const gint min_scroll = 0;
426         const gint max_scroll = p->n_targets - icon_rows;
427
428         if (top - selected_pos >= 0) {
429             p->scroll -= top - selected_pos + 1;
430             p->scroll = MAX(p->scroll, min_scroll);
431         } else if (selected_pos - bottom >= 0) {
432             p->scroll += selected_pos - bottom + 1;
433             p->scroll = MIN(p->scroll, max_scroll);
434         }
435     }
436
437     /* show the scroll arrows when appropriate */
438     if (p->scroll && p->mode == OB_FOCUS_CYCLE_POPUP_MODE_LIST) {
439         XMapWindow(obt_display, p->list_mode_up);
440         showing_arrows = TRUE;
441     } else
442         XUnmapWindow(obt_display, p->list_mode_up);
443
444     if (p->scroll < p->n_targets - icon_rows &&
445         p->mode == OB_FOCUS_CYCLE_POPUP_MODE_LIST)
446     {
447         XMapWindow(obt_display, p->list_mode_down);
448         showing_arrows = TRUE;
449     } else
450         XUnmapWindow(obt_display, p->list_mode_down);
451
452     /* make space for the arrows */
453     if (showing_arrows)
454         h += ob_rr_theme->up_arrow_mask->height + OUTSIDE_BORDER
455             + ob_rr_theme->down_arrow_mask->height + OUTSIDE_BORDER;
456
457     /* center the icons if there is less than one row */
458     if (p->mode == OB_FOCUS_CYCLE_POPUP_MODE_ICONS && icon_rows == 1)
459         icons_center_x = (w - p->n_targets * HILITE_SIZE) / 2;
460     else
461         icons_center_x = 0;
462
463     if (p->mode == OB_FOCUS_CYCLE_POPUP_MODE_ICONS) {
464         /* get the position of the text */
465         icon_mode_textx = l;
466         icon_mode_texty = h - texth - b;
467     }
468
469     /* find the position for the popup (include the outer borders) */
470     x = screen_area->x + (screen_area->width -
471                           (w + ob_rr_theme->obwidth * 2)) / 2;
472     y = screen_area->y + (screen_area->height -
473                           (h + ob_rr_theme->obwidth * 2)) / 2;
474
475     /* get the dimensions of the target hilite texture */
476     rgbax = ml;
477     rgbay = mt;
478     rgbaw = w - ml - mr;
479     rgbah = h - mt - mb;
480
481     if (!p->mapped) {
482         /* position the background but don't draw it */
483         XMoveResizeWindow(obt_display, p->bg, x, y, w, h);
484
485         if (p->mode == OB_FOCUS_CYCLE_POPUP_MODE_ICONS) {
486             /* position the text */
487             XMoveResizeWindow(obt_display, p->icon_mode_text,
488                               icon_mode_textx, icon_mode_texty, textw, texth);
489             XMapWindow(obt_display, popup.icon_mode_text);
490         } else {
491             XUnmapWindow(obt_display, popup.icon_mode_text);
492
493             up_arrow_x = (w - ob_rr_theme->up_arrow_mask->width) / 2;
494             up_arrow_y = t;
495
496             down_arrow_x = (w - ob_rr_theme->down_arrow_mask->width) / 2;
497             down_arrow_y = h - b - ob_rr_theme->down_arrow_mask->height;
498
499             /* position the arrows */
500             XMoveResizeWindow(obt_display, p->list_mode_up,
501                               up_arrow_x, up_arrow_y,
502                               ob_rr_theme->up_arrow_mask->width,
503                               ob_rr_theme->up_arrow_mask->height);
504             XMoveResizeWindow(obt_display, p->list_mode_down,
505                               down_arrow_x, down_arrow_y,
506                               ob_rr_theme->down_arrow_mask->width,
507                               ob_rr_theme->down_arrow_mask->height);
508         }
509     }
510
511     /* * * draw everything * * */
512
513     /* draw the background */
514     if (!p->mapped)
515         RrPaint(p->a_bg, p->bg, w, h);
516
517     /* draw the scroll arrows */
518     if (!p->mapped && p->mode == OB_FOCUS_CYCLE_POPUP_MODE_LIST) {
519         p->a_arrow->texture[0].data.mask.mask =
520             ob_rr_theme->up_arrow_mask;
521         p->a_arrow->surface.parent = p->a_bg;
522         p->a_arrow->surface.parentx = up_arrow_x;
523         p->a_arrow->surface.parenty = up_arrow_y;
524         RrPaint(p->a_arrow, p->list_mode_up, 
525                 ob_rr_theme->up_arrow_mask->width,
526                 ob_rr_theme->up_arrow_mask->height);
527
528         p->a_arrow->texture[0].data.mask.mask =
529             ob_rr_theme->down_arrow_mask;
530         p->a_arrow->surface.parent = p->a_bg;
531         p->a_arrow->surface.parentx = down_arrow_x;
532         p->a_arrow->surface.parenty = down_arrow_y;
533         RrPaint(p->a_arrow, p->list_mode_down, 
534                 ob_rr_theme->down_arrow_mask->width,
535                 ob_rr_theme->down_arrow_mask->height);
536     }
537
538     /* draw the icons and text */
539     for (i = 0, it = p->targets; it; ++i, it = g_list_next(it)) {
540         const ObFocusCyclePopupTarget *target = it->data;
541
542         /* have to redraw the targetted icon and last targetted icon
543          * to update the hilite */
544         if (!p->mapped || newtarget == target || p->last_target == target ||
545             last_scroll != p->scroll)
546         {
547             /* row and column start from 0 */
548             const gint row = i / icons_per_row - p->scroll;
549             const gint col = i % icons_per_row;
550             const ObClientIcon *icon;
551             gint iconx, icony;
552             gint list_mode_textx, list_mode_texty;
553             RrAppearance *text;
554
555             /* find the coordinates for the icon */
556             iconx = icons_center_x + l + (col * HILITE_SIZE);
557             icony = t + (showing_arrows ? ob_rr_theme->up_arrow_mask->height
558                                           + OUTSIDE_BORDER
559                          : 0)
560                 + (row * MAX(texth, HILITE_SIZE))
561                 + MAX(texth - HILITE_SIZE, 0) / 2;
562
563             /* find the dimensions of the text box */
564             list_mode_textx = iconx + HILITE_SIZE + TEXT_BORDER;
565             list_mode_texty = icony;
566
567             /* position the icon */
568             XMoveResizeWindow(obt_display, target->iconwin,
569                               iconx, icony, HILITE_SIZE, HILITE_SIZE);
570
571             /* position the text */
572             if (p->mode == OB_FOCUS_CYCLE_POPUP_MODE_LIST)
573                 XMoveResizeWindow(obt_display, target->textwin,
574                                   list_mode_textx, list_mode_texty,
575                                   textw, texth);
576
577             /* show/hide the right windows */
578             if (row >= 0 && row < icon_rows) {
579                 XMapWindow(obt_display, target->iconwin);
580                 if (p->mode == OB_FOCUS_CYCLE_POPUP_MODE_LIST)
581                     XMapWindow(obt_display, target->textwin);
582                 else
583                     XUnmapWindow(obt_display, target->textwin);
584             } else {
585                 XUnmapWindow(obt_display, target->textwin);
586                 if (p->mode == OB_FOCUS_CYCLE_POPUP_MODE_LIST)
587                     XUnmapWindow(obt_display, target->iconwin);
588                 else
589                     XMapWindow(obt_display, target->iconwin);
590             }
591
592             /* get the icon from the client */
593             icon = client_icon(target->client, ICON_SIZE, ICON_SIZE);
594             p->a_icon->texture[0].data.rgba.width = icon->width;
595             p->a_icon->texture[0].data.rgba.height = icon->height;
596             p->a_icon->texture[0].data.rgba.twidth = ICON_SIZE;
597             p->a_icon->texture[0].data.rgba.theight = ICON_SIZE;
598             p->a_icon->texture[0].data.rgba.tx = HILITE_OFFSET;
599             p->a_icon->texture[0].data.rgba.ty = HILITE_OFFSET;
600             p->a_icon->texture[0].data.rgba.alpha =
601                 target->client->iconic ? OB_ICONIC_ALPHA : 0xff;
602             p->a_icon->texture[0].data.rgba.data = icon->data;
603
604             /* Draw the hilite? */
605             p->a_icon->texture[1].type = (target == newtarget) ?
606                 RR_TEXTURE_RGBA : RR_TEXTURE_NONE;
607
608             /* draw the icon */
609             p->a_icon->surface.parentx = iconx;
610             p->a_icon->surface.parenty = icony;
611             RrPaint(p->a_icon, target->iconwin, HILITE_SIZE, HILITE_SIZE);
612
613             /* draw the text */
614             if (p->mode == OB_FOCUS_CYCLE_POPUP_MODE_LIST ||
615                 target == newtarget)
616             {
617                 text = (target == newtarget) ? p->a_hilite_text : p->a_text;
618                 text->texture[0].data.text.string = target->text;
619                 text->surface.parentx =
620                     p->mode == OB_FOCUS_CYCLE_POPUP_MODE_ICONS ?
621                     icon_mode_textx : list_mode_textx;
622                 text->surface.parenty =
623                     p->mode == OB_FOCUS_CYCLE_POPUP_MODE_ICONS ?
624                     icon_mode_texty : list_mode_texty;
625                 RrPaint(text,
626                         (p->mode == OB_FOCUS_CYCLE_POPUP_MODE_ICONS ?
627                          p->icon_mode_text : target->textwin),
628                         textw, texth);
629             }
630         }
631     }
632
633     p->last_target = newtarget;
634
635     g_free(screen_area);
636
637     XFlush(obt_display);
638 }
639
640 void focus_cycle_popup_show(ObClient *c, gboolean iconic_windows,
641                             gboolean all_desktops, gboolean dock_windows,
642                             gboolean desktop_windows,
643                             ObFocusCyclePopupMode mode)
644 {
645     g_assert(c != NULL);
646
647     if (mode == OB_FOCUS_CYCLE_POPUP_MODE_NONE) {
648         focus_cycle_popup_hide();
649         return;
650     }
651
652     /* do this stuff only when the dialog is first showing */
653     if (!popup.mapped) {
654         popup_setup(&popup, TRUE, iconic_windows, all_desktops, 
655                     dock_windows, desktop_windows);
656         /* this is fixed once the dialog is shown */
657         popup.mode = mode;
658     }
659     g_assert(popup.targets != NULL);
660
661     popup_render(&popup, c);
662
663     if (!popup.mapped) {
664         /* show the dialog */
665         XMapWindow(obt_display, popup.bg);
666         XFlush(obt_display);
667         popup.mapped = TRUE;
668         screen_hide_desktop_popup();
669     }
670 }
671
672 void focus_cycle_popup_hide(void)
673 {
674     gulong ignore_start;
675
676     ignore_start = event_start_ignore_all_enters();
677
678     XUnmapWindow(obt_display, popup.bg);
679     XFlush(obt_display);
680
681     event_end_ignore_all_enters(ignore_start);
682
683     popup.mapped = FALSE;
684
685     while(popup.targets) {
686         ObFocusCyclePopupTarget *t = popup.targets->data;
687
688         g_free(t->text);
689         XDestroyWindow(obt_display, t->iconwin);
690         XDestroyWindow(obt_display, t->textwin);
691         g_free(t);
692
693         popup.targets = g_list_delete_link(popup.targets, popup.targets);
694     }
695     popup.n_targets = 0;
696     popup.last_target = NULL;
697 }
698
699 void focus_cycle_popup_single_show(struct _ObClient *c,
700                                    gboolean iconic_windows,
701                                    gboolean all_desktops,
702                                    gboolean dock_windows,
703                                    gboolean desktop_windows)
704 {
705     gchar *text;
706
707     g_assert(c != NULL);
708
709     /* do this stuff only when the dialog is first showing */
710     if (!popup.mapped) {
711         Rect *a;
712
713         popup_setup(&popup, FALSE, iconic_windows, all_desktops,
714                     dock_windows, desktop_windows);
715         g_assert(popup.targets == NULL);
716
717         /* position the popup */
718         a = screen_physical_area_active();
719         icon_popup_position(single_popup, CenterGravity,
720                             a->x + a->width / 2, a->y + a->height / 2);
721         icon_popup_height(single_popup, POPUP_HEIGHT);
722         icon_popup_min_width(single_popup, POPUP_WIDTH);
723         icon_popup_max_width(single_popup, MAX(a->width/3, POPUP_WIDTH));
724         icon_popup_text_width(single_popup, popup.maxtextw);
725         g_free(a);
726     }
727
728     text = popup_get_name(c);
729     icon_popup_show(single_popup, text, client_icon(c, HILITE_SIZE,
730                                                     HILITE_SIZE));
731     g_free(text);
732     screen_hide_desktop_popup();
733 }
734
735 void focus_cycle_popup_single_hide(void)
736 {
737     icon_popup_hide(single_popup);
738 }