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