use g_slice_new() instead of g_new() part 2
[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 "focus_cycle.h"
22 #include "popup.h"
23 #include "client.h"
24 #include "screen.h"
25 #include "focus.h"
26 #include "openbox.h"
27 #include "config.h"
28 #include "window.h"
29 #include "event.h"
30 #include "obrender/render.h"
31
32 #include <X11/Xlib.h>
33 #include <glib.h>
34
35 /* Size of the icons, which can appear inside or outside of a hilite box */
36 #define ICON_SIZE (gint)config_theme_window_list_icon_size
37 /* Size of the hilite box around a window's icon */
38 #define HILITE_SIZE (ICON_SIZE + 2*HILITE_OFFSET)
39 /* Width of the outer ring around the hilite box */
40 #define HILITE_WIDTH 2
41 /* Space between the outer ring around the hilite box and the icon inside it */
42 #define HILITE_MARGIN 1
43 /* Total distance from the edge of the hilite box to the icon inside it */
44 #define HILITE_OFFSET (HILITE_WIDTH + HILITE_MARGIN)
45 /* Margin area around the outside of the dialog */
46 #define OUTSIDE_BORDER 3
47 /* Margin area around the text */
48 #define TEXT_BORDER 2
49 /* Scroll the list-mode list when the cursor gets within this many rows of the
50    top or bottom */
51 #define SCROLL_MARGIN 4
52
53 typedef struct _ObFocusCyclePopup       ObFocusCyclePopup;
54 typedef struct _ObFocusCyclePopupTarget ObFocusCyclePopupTarget;
55
56 struct _ObFocusCyclePopupTarget
57 {
58     ObClient *client;
59     RrImage *icon;
60     gchar *text;
61     Window iconwin;
62     /* This is used when the popup is in list mode */
63     Window textwin;
64 };
65
66 struct _ObFocusCyclePopup
67 {
68     ObWindow obwin;
69     Window bg;
70
71     /* This is used when the popup is in icon mode */
72     Window icon_mode_text;
73
74     Window list_mode_up;
75     Window list_mode_down;
76
77     GList *targets;
78     gint n_targets;
79
80     const ObFocusCyclePopupTarget *last_target;
81
82     gint maxtextw;
83
84     /* How are the list is scrolled, in scroll mode */
85     gint scroll;
86
87     RrAppearance *a_bg;
88     RrAppearance *a_text;
89     RrAppearance *a_hilite_text;
90     RrAppearance *a_icon;
91     RrAppearance *a_arrow;
92
93     gboolean mapped;
94     ObFocusCyclePopupMode mode;
95 };
96
97 /*! This popup shows all possible windows */
98 static ObFocusCyclePopup popup;
99 /*! This popup shows a single window */
100 static ObIconPopup *single_popup;
101
102 static gchar   *popup_get_name (ObClient *c);
103 static gboolean popup_setup    (ObFocusCyclePopup *p,
104                                 gboolean create_targets,
105                                 gboolean refresh_targets);
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_bg);
126     popup.a_hilite_text = RrAppearanceCopy(ob_rr_theme->osd_hilite_label);
127     popup.a_text = RrAppearanceCopy(ob_rr_theme->osd_unhilite_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     RrAppearanceClearTextures(popup.a_icon);
143     popup.a_icon->texture[0].type = RR_TEXTURE_IMAGE;
144
145     RrAppearanceClearTextures(popup.a_arrow);
146     popup.a_arrow->texture[0].type = RR_TEXTURE_MASK;
147     popup.a_arrow->texture[0].data.mask.color =
148         ob_rr_theme->osd_text_active_color;
149
150     attrib.override_redirect = True;
151     attrib.border_pixel=RrColorPixel(ob_rr_theme->osd_border_color);
152     popup.bg = create_window(obt_root(ob_screen), ob_rr_theme->obwidth,
153                              CWOverrideRedirect | CWBorderPixel, &attrib);
154
155     /* create the text window used for the icon-mode popup */
156     popup.icon_mode_text = create_window(popup.bg, 0, 0, NULL);
157
158     /* create the windows for the up and down arrows */
159     popup.list_mode_up = create_window(popup.bg, 0, 0, NULL);
160     popup.list_mode_down = create_window(popup.bg, 0, 0, NULL);
161
162     popup.targets = NULL;
163     popup.n_targets = 0;
164     popup.last_target = NULL;
165
166     /* set up the hilite texture for the icon */
167     popup.a_icon->texture[1].data.rgba.width = HILITE_SIZE;
168     popup.a_icon->texture[1].data.rgba.height = HILITE_SIZE;
169     popup.a_icon->texture[1].data.rgba.alpha = 0xff;
170     p = g_new(RrPixel32, HILITE_SIZE * HILITE_SIZE);
171     popup.a_icon->texture[1].data.rgba.data = p;
172
173     /* create the hilite under the target icon */
174     {
175         RrPixel32 color;
176         RrColor *tc;
177         gint x, y, o;
178
179         tc = ob_rr_theme->osd_text_active_color;
180         color = ((tc->r & 0xff) << RrDefaultRedOffset) +
181             ((tc->g & 0xff) << RrDefaultGreenOffset) +
182             ((tc->b & 0xff) << RrDefaultBlueOffset);
183
184         o = 0;
185         for (x = 0; x < HILITE_SIZE; x++)
186             for (y = 0; y < HILITE_SIZE; y++) {
187                 guchar a;
188
189                 if (x < HILITE_WIDTH ||
190                     x >= HILITE_SIZE - HILITE_WIDTH ||
191                     y < HILITE_WIDTH ||
192                     y >= HILITE_SIZE - HILITE_WIDTH)
193                 {
194                     /* the border of the target */
195                     a = 0x88;
196                 } else {
197                     /* the background of the target */
198                     a = 0x22;
199                 }
200
201                 p[o++] = color + (a << RrDefaultAlphaOffset);
202             }
203     }
204
205     stacking_add(INTERNAL_AS_WINDOW(&popup));
206     window_add(&popup.bg, INTERNAL_AS_WINDOW(&popup));
207 }
208
209 void focus_cycle_popup_shutdown(gboolean reconfig)
210 {
211     icon_popup_free(single_popup);
212
213     window_remove(popup.bg);
214     stacking_remove(INTERNAL_AS_WINDOW(&popup));
215
216     while(popup.targets) {
217         ObFocusCyclePopupTarget *t = popup.targets->data;
218
219         RrImageUnref(t->icon);
220         g_free(t->text);
221         XDestroyWindow(obt_display, t->iconwin);
222         XDestroyWindow(obt_display, t->textwin);
223         g_free(t);
224
225         popup.targets = g_list_delete_link(popup.targets, popup.targets);
226     }
227
228     g_free(popup.a_icon->texture[1].data.rgba.data);
229     popup.a_icon->texture[1].data.rgba.data = NULL;
230
231     XDestroyWindow(obt_display, popup.list_mode_up);
232     XDestroyWindow(obt_display, popup.list_mode_down);
233     XDestroyWindow(obt_display, popup.icon_mode_text);
234     XDestroyWindow(obt_display, popup.bg);
235
236     RrAppearanceFree(popup.a_arrow);
237     RrAppearanceFree(popup.a_icon);
238     RrAppearanceFree(popup.a_hilite_text);
239     RrAppearanceFree(popup.a_text);
240     RrAppearanceFree(popup.a_bg);
241 }
242
243 static void popup_target_free(ObFocusCyclePopupTarget *t)
244 {
245     RrImageUnref(t->icon);
246     g_free(t->text);
247     XDestroyWindow(obt_display, t->iconwin);
248     XDestroyWindow(obt_display, t->textwin);
249     g_slice_free(ObFocusCyclePopupTarget, t);
250 }
251
252 static gboolean popup_setup(ObFocusCyclePopup *p, gboolean create_targets,
253                             gboolean refresh_targets)
254 {
255     gint maxwidth, n;
256     GList *it;
257     GList *rtargets; /* old targets for refresh */
258     GList *rtlast;
259     gboolean change;
260
261     if (refresh_targets) {
262         rtargets = p->targets;
263         rtlast = g_list_last(rtargets);
264         p->targets = NULL;
265         p->n_targets = 0;
266         change = FALSE;
267     }
268     else {
269         rtargets = rtlast = NULL;
270         change = TRUE;
271     }
272
273     g_assert(p->targets == NULL);
274     g_assert(p->n_targets == 0);
275
276     /* make its width to be the width of all the possible titles */
277
278     /* build a list of all the valid focus targets and measure their strings,
279        and count them */
280     maxwidth = 0;
281     n = 0;
282     for (it = g_list_last(focus_order); it; it = g_list_previous(it)) {
283         ObClient *ft = it->data;
284
285         if (focus_cycle_valid(ft)) {
286             GList *rit;
287
288             /* reuse the target if possible during refresh */
289             for (rit = rtlast; rit; rit = g_list_previous(rit)) {
290                 ObFocusCyclePopupTarget *t = rit->data;
291                 if (t->client == ft) {
292                     if (rit == rtlast)
293                         rtlast = g_list_previous(rit);
294                     rtargets = g_list_remove_link(rtargets, rit);
295
296                     p->targets = g_list_concat(rit, p->targets);
297                     ++n;
298
299                     if (rit != rtlast)
300                         change = TRUE; /* order changed */
301                     break;
302                 }
303             }
304
305             if (!rit) {
306                 gchar *text = popup_get_name(ft);
307
308                 /* measure */
309                 p->a_text->texture[0].data.text.string = text;
310                 maxwidth = MAX(maxwidth, RrMinWidth(p->a_text));
311
312                 if (!create_targets) {
313                     g_free(text);
314                 } else {
315                     ObFocusCyclePopupTarget *t =
316                         g_slice_new(ObFocusCyclePopupTarget);
317
318                     t->client = ft;
319                     t->text = text;
320                     t->icon = client_icon(t->client);
321                     RrImageRef(t->icon); /* own the icon so it won't go away */
322                     t->iconwin = create_window(p->bg, 0, 0, NULL);
323                     t->textwin = create_window(p->bg, 0, 0, NULL);
324
325                     p->targets = g_list_prepend(p->targets, t);
326                     ++n;
327
328                     change = TRUE; /* added a window */
329                 }
330             }
331         }
332     }
333
334     if (rtargets) {
335         change = TRUE; /* removed a window */
336
337         while (rtargets) {
338             popup_target_free(rtargets->data);
339             rtargets = g_list_delete_link(rtargets, rtargets);
340         }
341     }
342
343     p->n_targets = n;
344     if (refresh_targets)
345         /* don't shrink when refreshing */
346         p->maxtextw = MAX(p->maxtextw, maxwidth);
347     else
348         p->maxtextw = maxwidth;
349
350     return change;
351 }
352
353 static void popup_cleanup(void)
354 {
355     while(popup.targets) {
356         popup_target_free(popup.targets->data);
357         popup.targets = g_list_delete_link(popup.targets, popup.targets);
358     }
359     popup.n_targets = 0;
360     popup.last_target = NULL;
361 }
362
363 static gchar *popup_get_name(ObClient *c)
364 {
365     ObClient *p;
366     gchar *title;
367     const gchar *desk = NULL;
368     gchar *ret;
369
370     /* find our highest direct parent */
371     p = client_search_top_direct_parent(c);
372
373     if (c->desktop != DESKTOP_ALL && c->desktop != screen_desktop)
374         desk = screen_desktop_names[c->desktop];
375
376     title = c->iconic ? c->icon_title : c->title;
377
378     /* use the transient's parent's title/icon if we don't have one */
379     if (p != c && title[0] == '\0')
380         title = p->iconic ? p->icon_title : p->title;
381
382     if (desk)
383         ret = g_strdup_printf("%s [%s]", title, desk);
384     else
385         ret = g_strdup(title);
386
387     return ret;
388 }
389
390 static void popup_render(ObFocusCyclePopup *p, const ObClient *c)
391 {
392     gint ml, mt, mr, mb;
393     gint l, t, r, b;
394     gint x, y, w, h;
395     Rect *screen_area = NULL;
396     gint i;
397     GList *it;
398     const ObFocusCyclePopupTarget *newtarget;
399     ObFocusCyclePopupMode mode = p->mode;
400     gint icons_per_row;
401     gint icon_rows;
402     gint textw, texth;
403     gint selected_pos;
404     gint last_scroll;
405
406     /* vars for icon mode */
407     gint icon_mode_textx;
408     gint icon_mode_texty;
409     gint icons_center_x;
410
411     /* vars for list mode */
412     gint list_mode_icon_column_w = HILITE_SIZE + OUTSIDE_BORDER;
413     gint up_arrow_x, down_arrow_x;
414     gint up_arrow_y, down_arrow_y;
415     gboolean showing_arrows = FALSE;
416
417     g_assert(mode == OB_FOCUS_CYCLE_POPUP_MODE_ICONS ||
418              mode == OB_FOCUS_CYCLE_POPUP_MODE_LIST);
419
420     screen_area = screen_physical_area_primary(FALSE);
421
422     /* get the outside margins */
423     RrMargins(p->a_bg, &ml, &mt, &mr, &mb);
424
425     /* get our outside borders */
426     l = ml + OUTSIDE_BORDER;
427     r = mr + OUTSIDE_BORDER;
428     t = mt + OUTSIDE_BORDER;
429     b = mb + OUTSIDE_BORDER;
430
431     /* get the width from the text and keep it within limits */
432     w = l + r + p->maxtextw;
433     if (mode == OB_FOCUS_CYCLE_POPUP_MODE_LIST)
434         /* when in list mode, there are icons down the side */
435         w += list_mode_icon_column_w;
436     w = MIN(w, MAX(screen_area->width/3, POPUP_WIDTH)); /* max width */
437     w = MAX(w, POPUP_WIDTH); /* min width */
438
439     /* get the text height */
440     texth = RrMinHeight(p->a_hilite_text);
441     if (mode == OB_FOCUS_CYCLE_POPUP_MODE_LIST)
442         texth = MAX(MAX(texth, RrMinHeight(p->a_text)), HILITE_SIZE);
443     else
444         texth += TEXT_BORDER * 2;
445
446     if (mode == OB_FOCUS_CYCLE_POPUP_MODE_ICONS) {
447         /* how many icons will fit in that row? make the width fit that */
448         w -= l + r;
449         icons_per_row = (w + HILITE_SIZE - 1) / HILITE_SIZE;
450         w = icons_per_row * HILITE_SIZE + l + r;
451
452         /* how many rows do we need? */
453         icon_rows = (p->n_targets-1) / icons_per_row + 1;
454     } else {
455         /* in list mode, there is one column of icons.. */
456         icons_per_row = 1;
457         /* maximum is 80% of the screen height */
458         icon_rows = MIN(p->n_targets,
459                         (4*screen_area->height/5) /* 80% of the screen */
460                         /
461                         MAX(HILITE_SIZE, texth)); /* height of each row */
462         /* but make sure there is always one */
463         icon_rows = MAX(icon_rows, 1);
464     }
465
466     /* get the text width */
467     textw = w - l - r;
468     if (mode == OB_FOCUS_CYCLE_POPUP_MODE_LIST)
469         /* leave space on the side for the icons */
470         textw -= list_mode_icon_column_w;
471
472     if (!p->mapped)
473         /* reset the scrolling when the dialog is first shown */
474         p->scroll = 0;
475
476     /* find the height of the dialog */
477     h = t + b + (icon_rows * MAX(HILITE_SIZE, texth));
478     if (mode == OB_FOCUS_CYCLE_POPUP_MODE_ICONS)
479         /* in icon mode the text sits below the icons, so make some space */
480         h += OUTSIDE_BORDER + texth;
481
482     /* find the focused target */
483     newtarget = NULL;
484     for (i = 0, it = p->targets; it; ++i, it = g_list_next(it)) {
485         const ObFocusCyclePopupTarget *target = it->data;
486         if (target->client == c) {
487             /* save the target */
488             newtarget = target;
489             break;
490         }
491     }
492     selected_pos = i;
493     g_assert(newtarget != NULL);
494
495     /* scroll the list if needed */
496     last_scroll = p->scroll;
497     if (mode == OB_FOCUS_CYCLE_POPUP_MODE_LIST) {
498         const gint top = p->scroll + SCROLL_MARGIN;
499         const gint bottom = p->scroll + icon_rows - SCROLL_MARGIN;
500         const gint min_scroll = 0;
501         const gint max_scroll = p->n_targets - icon_rows;
502
503         if (top - selected_pos >= 0) {
504             p->scroll -= top - selected_pos + 1;
505             p->scroll = MAX(p->scroll, min_scroll);
506         } else if (selected_pos - bottom >= 0) {
507             p->scroll += selected_pos - bottom + 1;
508             p->scroll = MIN(p->scroll, max_scroll);
509         }
510     }
511
512     /* show the scroll arrows when appropriate */
513     if (p->scroll && mode == OB_FOCUS_CYCLE_POPUP_MODE_LIST) {
514         XMapWindow(obt_display, p->list_mode_up);
515         showing_arrows = TRUE;
516     } else
517         XUnmapWindow(obt_display, p->list_mode_up);
518
519     if (p->scroll < p->n_targets - icon_rows &&
520         mode == OB_FOCUS_CYCLE_POPUP_MODE_LIST)
521     {
522         XMapWindow(obt_display, p->list_mode_down);
523         showing_arrows = TRUE;
524     } else
525         XUnmapWindow(obt_display, p->list_mode_down);
526
527     /* make space for the arrows */
528     if (showing_arrows)
529         h += ob_rr_theme->up_arrow_mask->height + OUTSIDE_BORDER
530             + ob_rr_theme->down_arrow_mask->height + OUTSIDE_BORDER;
531
532     /* center the icons if there is less than one row */
533     if (mode == OB_FOCUS_CYCLE_POPUP_MODE_ICONS && icon_rows == 1)
534         icons_center_x = (w - p->n_targets * HILITE_SIZE) / 2;
535     else
536         icons_center_x = 0;
537
538     if (mode == OB_FOCUS_CYCLE_POPUP_MODE_ICONS) {
539         /* get the position of the text */
540         icon_mode_textx = l;
541         icon_mode_texty = h - texth - b;
542     }
543
544     /* find the position for the popup (include the outer borders) */
545     x = screen_area->x + (screen_area->width -
546                           (w + ob_rr_theme->obwidth * 2)) / 2;
547     y = screen_area->y + (screen_area->height -
548                           (h + ob_rr_theme->obwidth * 2)) / 2;
549
550     if (!p->mapped) {
551         /* position the background but don't draw it */
552         XMoveResizeWindow(obt_display, p->bg, x, y, w, h);
553
554         if (mode == OB_FOCUS_CYCLE_POPUP_MODE_ICONS) {
555             /* position the text */
556             XMoveResizeWindow(obt_display, p->icon_mode_text,
557                               icon_mode_textx, icon_mode_texty, textw, texth);
558             XMapWindow(obt_display, popup.icon_mode_text);
559         } else {
560             XUnmapWindow(obt_display, popup.icon_mode_text);
561
562             up_arrow_x = (w - ob_rr_theme->up_arrow_mask->width) / 2;
563             up_arrow_y = t;
564
565             down_arrow_x = (w - ob_rr_theme->down_arrow_mask->width) / 2;
566             down_arrow_y = h - b - ob_rr_theme->down_arrow_mask->height;
567
568             /* position the arrows */
569             XMoveResizeWindow(obt_display, p->list_mode_up,
570                               up_arrow_x, up_arrow_y,
571                               ob_rr_theme->up_arrow_mask->width,
572                               ob_rr_theme->up_arrow_mask->height);
573             XMoveResizeWindow(obt_display, p->list_mode_down,
574                               down_arrow_x, down_arrow_y,
575                               ob_rr_theme->down_arrow_mask->width,
576                               ob_rr_theme->down_arrow_mask->height);
577         }
578     }
579
580     /* * * draw everything * * */
581
582     /* draw the background */
583     if (!p->mapped)
584         RrPaint(p->a_bg, p->bg, w, h);
585
586     /* draw the scroll arrows */
587     if (!p->mapped && mode == OB_FOCUS_CYCLE_POPUP_MODE_LIST) {
588         p->a_arrow->texture[0].data.mask.mask =
589             ob_rr_theme->up_arrow_mask;
590         p->a_arrow->surface.parent = p->a_bg;
591         p->a_arrow->surface.parentx = up_arrow_x;
592         p->a_arrow->surface.parenty = up_arrow_y;
593         RrPaint(p->a_arrow, p->list_mode_up, 
594                 ob_rr_theme->up_arrow_mask->width,
595                 ob_rr_theme->up_arrow_mask->height);
596
597         p->a_arrow->texture[0].data.mask.mask =
598             ob_rr_theme->down_arrow_mask;
599         p->a_arrow->surface.parent = p->a_bg;
600         p->a_arrow->surface.parentx = down_arrow_x;
601         p->a_arrow->surface.parenty = down_arrow_y;
602         RrPaint(p->a_arrow, p->list_mode_down, 
603                 ob_rr_theme->down_arrow_mask->width,
604                 ob_rr_theme->down_arrow_mask->height);
605     }
606
607     /* draw the icons and text */
608     for (i = 0, it = p->targets; it; ++i, it = g_list_next(it)) {
609         const ObFocusCyclePopupTarget *target = it->data;
610
611         /* have to redraw the targetted icon and last targetted icon
612          * to update the hilite */
613         if (!p->mapped || newtarget == target || p->last_target == target ||
614             last_scroll != p->scroll)
615         {
616             /* row and column start from 0 */
617             const gint row = i / icons_per_row - p->scroll;
618             const gint col = i % icons_per_row;
619             gint iconx, icony;
620             gint list_mode_textx, list_mode_texty;
621             RrAppearance *text;
622
623             /* find the coordinates for the icon */
624             iconx = icons_center_x + l + (col * HILITE_SIZE);
625             icony = t + (showing_arrows ? ob_rr_theme->up_arrow_mask->height
626                                           + OUTSIDE_BORDER
627                          : 0)
628                 + (row * MAX(texth, HILITE_SIZE))
629                 + MAX(texth - HILITE_SIZE, 0) / 2;
630
631             /* find the dimensions of the text box */
632             list_mode_textx = iconx + HILITE_SIZE + TEXT_BORDER;
633             list_mode_texty = icony;
634
635             /* position the icon */
636             XMoveResizeWindow(obt_display, target->iconwin,
637                               iconx, icony, HILITE_SIZE, HILITE_SIZE);
638
639             /* position the text */
640             if (mode == OB_FOCUS_CYCLE_POPUP_MODE_LIST)
641                 XMoveResizeWindow(obt_display, target->textwin,
642                                   list_mode_textx, list_mode_texty,
643                                   textw, texth);
644
645             /* show/hide the right windows */
646             if (row >= 0 && row < icon_rows) {
647                 XMapWindow(obt_display, target->iconwin);
648                 if (mode == OB_FOCUS_CYCLE_POPUP_MODE_LIST)
649                     XMapWindow(obt_display, target->textwin);
650                 else
651                     XUnmapWindow(obt_display, target->textwin);
652             } else {
653                 XUnmapWindow(obt_display, target->textwin);
654                 if (mode == OB_FOCUS_CYCLE_POPUP_MODE_LIST)
655                     XUnmapWindow(obt_display, target->iconwin);
656                 else
657                     XMapWindow(obt_display, target->iconwin);
658             }
659
660             /* get the icon from the client */
661             p->a_icon->texture[0].data.image.twidth = ICON_SIZE;
662             p->a_icon->texture[0].data.image.theight = ICON_SIZE;
663             p->a_icon->texture[0].data.image.tx = HILITE_OFFSET;
664             p->a_icon->texture[0].data.image.ty = HILITE_OFFSET;
665             p->a_icon->texture[0].data.image.alpha =
666                 target->client->iconic ? OB_ICONIC_ALPHA : 0xff;
667             p->a_icon->texture[0].data.image.image = target->icon;
668
669             /* Draw the hilite? */
670             p->a_icon->texture[1].type = (target == newtarget) ?
671                 RR_TEXTURE_RGBA : RR_TEXTURE_NONE;
672
673             /* draw the icon */
674             p->a_icon->surface.parentx = iconx;
675             p->a_icon->surface.parenty = icony;
676             RrPaint(p->a_icon, target->iconwin, HILITE_SIZE, HILITE_SIZE);
677
678             /* draw the text */
679             if (mode == OB_FOCUS_CYCLE_POPUP_MODE_LIST ||
680                 target == newtarget)
681             {
682                 text = (target == newtarget) ? p->a_hilite_text : p->a_text;
683                 text->texture[0].data.text.string = target->text;
684                 text->surface.parentx =
685                     mode == OB_FOCUS_CYCLE_POPUP_MODE_ICONS ?
686                     icon_mode_textx : list_mode_textx;
687                 text->surface.parenty =
688                     mode == OB_FOCUS_CYCLE_POPUP_MODE_ICONS ?
689                     icon_mode_texty : list_mode_texty;
690                 RrPaint(text,
691                         (mode == OB_FOCUS_CYCLE_POPUP_MODE_ICONS ?
692                          p->icon_mode_text : target->textwin),
693                         textw, texth);
694             }
695         }
696     }
697
698     p->last_target = newtarget;
699
700     g_free(screen_area);
701
702     XFlush(obt_display);
703 }
704
705 void focus_cycle_popup_show(ObClient *c, gboolean iconic_windows,
706                             gboolean all_desktops, gboolean dock_windows,
707                             gboolean desktop_windows,
708                             ObFocusCyclePopupMode mode)
709 {
710     g_assert(c != NULL);
711
712     if (mode == OB_FOCUS_CYCLE_POPUP_MODE_NONE) {
713         focus_cycle_popup_hide();
714         return;
715     }
716
717     /* do this stuff only when the dialog is first showing */
718     if (!popup.mapped) {
719         popup_setup(&popup, TRUE, FALSE);
720         /* this is fixed once the dialog is shown */
721         popup.mode = mode;
722     }
723     g_assert(popup.targets != NULL);
724
725     popup_render(&popup, c);
726
727     if (!popup.mapped) {
728         /* show the dialog */
729         XMapWindow(obt_display, popup.bg);
730         XFlush(obt_display);
731         popup.mapped = TRUE;
732         screen_hide_desktop_popup();
733     }
734 }
735
736 void focus_cycle_popup_hide(void)
737 {
738     gulong ignore_start;
739
740     ignore_start = event_start_ignore_all_enters();
741
742     XUnmapWindow(obt_display, popup.bg);
743     XFlush(obt_display);
744
745     event_end_ignore_all_enters(ignore_start);
746
747     popup.mapped = FALSE;
748
749     popup_cleanup();
750 }
751
752 void focus_cycle_popup_single_show(struct _ObClient *c,
753                                    gboolean iconic_windows,
754                                    gboolean all_desktops,
755                                    gboolean dock_windows,
756                                    gboolean desktop_windows)
757 {
758     gchar *text;
759
760     g_assert(c != NULL);
761
762     /* do this stuff only when the dialog is first showing */
763     if (!popup.mapped) {
764         Rect *a;
765
766         popup_setup(&popup, FALSE, FALSE);
767         g_assert(popup.targets == NULL);
768
769         /* position the popup */
770         a = screen_physical_area_primary(FALSE);
771         icon_popup_position(single_popup, CenterGravity,
772                             a->x + a->width / 2, a->y + a->height / 2);
773         icon_popup_height(single_popup, POPUP_HEIGHT);
774         icon_popup_min_width(single_popup, POPUP_WIDTH);
775         icon_popup_max_width(single_popup, MAX(a->width/3, POPUP_WIDTH));
776         icon_popup_text_width(single_popup, popup.maxtextw);
777         g_free(a);
778     }
779
780     text = popup_get_name(c);
781     icon_popup_show(single_popup, text, client_icon(c));
782     g_free(text);
783     screen_hide_desktop_popup();
784 }
785
786 void focus_cycle_popup_single_hide(void)
787 {
788     icon_popup_hide(single_popup);
789 }
790
791 gboolean focus_cycle_popup_is_showing(ObClient *c)
792 {
793     if (popup.mapped) {
794         GList *it;
795
796         for (it = popup.targets; it; it = g_list_next(it)) {
797             ObFocusCyclePopupTarget *t = it->data;
798             if (t->client == c)
799                 return TRUE;
800         }
801     }
802     return FALSE;
803 }
804
805 static ObClient* popup_revert(ObClient *target)
806 {
807     GList *it, *itt;
808
809     for (it = popup.targets; it; it = g_list_next(it)) {
810         ObFocusCyclePopupTarget *t = it->data;
811         if (t->client == target) {
812             /* move to a previous window if possible */
813             for (itt = it->prev; itt; itt = g_list_previous(itt)) {
814                 ObFocusCyclePopupTarget *t2 = itt->data;
815                 if (focus_cycle_valid(t2->client))
816                     return t2->client;
817             }
818
819             /* otherwise move to a following window if possible */
820             for (itt = it->next; itt; itt = g_list_next(itt)) {
821                 ObFocusCyclePopupTarget *t2 = itt->data;
822                 if (focus_cycle_valid(t2->client))
823                     return t2->client;
824             }
825
826             /* otherwise, we can't go anywhere there is nowhere valid to go */
827             return NULL;
828         }
829     }
830     return NULL;
831 }
832
833 ObClient* focus_cycle_popup_refresh(ObClient *target,
834                                     gboolean redraw)
835 {
836     if (!popup.mapped) return NULL;
837
838     if (!focus_cycle_valid(target))
839         target = popup_revert(target);
840
841     redraw = popup_setup(&popup, TRUE, TRUE) && redraw;
842
843     if (!target && popup.targets)
844         target = ((ObFocusCyclePopupTarget*)popup.targets->data)->client;
845
846     if (target && redraw) {
847         popup.mapped = FALSE;
848         popup_render(&popup, target);
849         XFlush(obt_display);
850         popup.mapped = TRUE;
851     }
852
853     return target;
854 }