]> Dogcows Code - chaz/openbox/blob - openbox/focus_cycle_popup.c
combine the old focus cycle popup code with mika's new list-mode popup, and make...
[chaz/openbox] / 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
48 typedef struct _ObFocusCyclePopup ObFocusCyclePopup;
49 typedef struct _ObFocusCyclePopupTarget ObFocusCyclePopupTarget;
50
51 struct _ObFocusCyclePopupTarget
52 {
53 ObClient *client;
54 gchar *text;
55 Window iconwin;
56 /* This is used when the popup is in list mode */
57 Window textwin;
58 };
59
60 struct _ObFocusCyclePopup
61 {
62 ObWindow obwin;
63 Window bg;
64
65 /* This is used when the popup is in icon mode */
66 Window icon_mode_text;
67
68 GList *targets;
69 gint n_targets;
70
71 const ObFocusCyclePopupTarget *last_target;
72
73 gint maxtextw;
74
75 RrAppearance *a_bg;
76 RrAppearance *a_text;
77 RrAppearance *a_hilite_text;
78 RrAppearance *a_icon;
79
80 gboolean mapped;
81 };
82
83 /*! This popup shows all possible windows */
84 static ObFocusCyclePopup popup;
85 /*! This popup shows a single window */
86 static ObIconPopup *single_popup;
87
88 static gchar *popup_get_name (ObClient *c);
89 static void popup_setup (ObFocusCyclePopup *p,
90 gboolean create_targets,
91 gboolean iconic_windows,
92 gboolean all_desktops,
93 gboolean dock_windows,
94 gboolean desktop_windows);
95 static void popup_render (ObFocusCyclePopup *p,
96 const ObClient *c,
97 ObFocusCyclePopupMode mode);
98
99 static Window create_window(Window parent, guint bwidth, gulong mask,
100 XSetWindowAttributes *attr)
101 {
102 return XCreateWindow(obt_display, parent, 0, 0, 1, 1, bwidth,
103 RrDepth(ob_rr_inst), InputOutput,
104 RrVisual(ob_rr_inst), mask, attr);
105 }
106
107 void focus_cycle_popup_startup(gboolean reconfig)
108 {
109 XSetWindowAttributes attrib;
110 RrPixel32 *p;
111
112 single_popup = icon_popup_new();
113
114 popup.obwin.type = OB_WINDOW_CLASS_INTERNAL;
115 popup.a_bg = RrAppearanceCopy(ob_rr_theme->osd_hilite_bg);
116 popup.a_hilite_text = RrAppearanceCopy(ob_rr_theme->osd_hilite_label);
117 popup.a_text = RrAppearanceCopy(ob_rr_theme->a_unfocused_label);
118 popup.a_icon = RrAppearanceCopy(ob_rr_theme->a_clear);
119
120 popup.a_hilite_text->surface.parent = popup.a_bg;
121 popup.a_text->surface.parent = popup.a_bg;
122 popup.a_icon->surface.parent = popup.a_bg;
123
124 popup.a_text->texture[0].data.text.justify = RR_JUSTIFY_LEFT;
125 popup.a_hilite_text->texture[0].data.text.justify = RR_JUSTIFY_LEFT;
126
127 /* 2 textures. texture[0] is the icon. texture[1] is the hilight, and
128 may or may not be used */
129 RrAppearanceAddTextures(popup.a_icon, 2);
130
131 popup.a_icon->texture[0].type = RR_TEXTURE_RGBA;
132
133 attrib.override_redirect = True;
134 attrib.border_pixel=RrColorPixel(ob_rr_theme->osd_border_color);
135 popup.bg = create_window(obt_root(ob_screen), ob_rr_theme->obwidth,
136 CWOverrideRedirect | CWBorderPixel, &attrib);
137
138 /* create the text window used for the icon-mode popup */
139 popup.icon_mode_text = create_window(popup.bg, 0, 0, NULL);
140 XMapWindow(obt_display, popup.icon_mode_text);
141
142 popup.targets = NULL;
143 popup.n_targets = 0;
144 popup.last_target = NULL;
145
146 /* set up the hilite texture for the icon */
147 popup.a_icon->texture[1].data.rgba.width = HILITE_SIZE;
148 popup.a_icon->texture[1].data.rgba.height = HILITE_SIZE;
149 popup.a_icon->texture[1].data.rgba.alpha = 0xff;
150 p = g_new(RrPixel32, HILITE_SIZE * HILITE_SIZE);
151 popup.a_icon->texture[1].data.rgba.data = p;
152
153 /* create the hilite under the target icon */
154 {
155 RrPixel32 color;
156 gint x, y, o;
157
158 color = ((ob_rr_theme->osd_color->r & 0xff) << RrDefaultRedOffset) +
159 ((ob_rr_theme->osd_color->g & 0xff) << RrDefaultGreenOffset) +
160 ((ob_rr_theme->osd_color->b & 0xff) << RrDefaultBlueOffset);
161
162 o = 0;
163 for (x = 0; x < HILITE_SIZE; x++)
164 for (y = 0; y < HILITE_SIZE; y++) {
165 guchar a;
166
167 if (x < HILITE_WIDTH ||
168 x >= HILITE_SIZE - HILITE_WIDTH ||
169 y < HILITE_WIDTH ||
170 y >= HILITE_SIZE - HILITE_WIDTH)
171 {
172 /* the border of the target */
173 a = 0x88;
174 }
175 else {
176 /* the background of the target */
177 a = 0x22;
178 }
179
180 p[o++] = color + (a << RrDefaultAlphaOffset);
181 }
182 }
183
184 stacking_add(INTERNAL_AS_WINDOW(&popup));
185 window_add(&popup.bg, INTERNAL_AS_WINDOW(&popup));
186 }
187
188 void focus_cycle_popup_shutdown(gboolean reconfig)
189 {
190 icon_popup_free(single_popup);
191
192 window_remove(popup.bg);
193 stacking_remove(INTERNAL_AS_WINDOW(&popup));
194
195 while(popup.targets) {
196 ObFocusCyclePopupTarget *t = popup.targets->data;
197
198 g_free(t->text);
199 XDestroyWindow(obt_display, t->iconwin);
200 XDestroyWindow(obt_display, t->textwin);
201
202 popup.targets = g_list_delete_link(popup.targets, popup.targets);
203 }
204
205 g_free(popup.a_icon->texture[1].data.rgba.data);
206 popup.a_icon->texture[1].data.rgba.data = NULL;
207
208 XDestroyWindow(obt_display, popup.icon_mode_text);
209 XDestroyWindow(obt_display, popup.bg);
210
211 RrAppearanceFree(popup.a_icon);
212 RrAppearanceFree(popup.a_hilite_text);
213 RrAppearanceFree(popup.a_text);
214 RrAppearanceFree(popup.a_bg);
215 }
216
217 static void popup_setup(ObFocusCyclePopup *p, gboolean create_targets,
218 gboolean iconic_windows, gboolean all_desktops,
219 gboolean dock_windows, gboolean desktop_windows)
220 {
221 gint maxwidth, n;
222 GList *it;
223
224 g_assert(p->targets == NULL);
225 g_assert(p->n_targets == 0);
226
227 /* make its width to be the width of all the possible titles */
228
229 /* build a list of all the valid focus targets and measure their strings,
230 and count them */
231 maxwidth = 0;
232 n = 0;
233 for (it = g_list_last(focus_order); it; it = g_list_previous(it)) {
234 ObClient *ft = it->data;
235
236 if (focus_valid_target(ft, TRUE,
237 iconic_windows,
238 all_desktops,
239 dock_windows,
240 desktop_windows))
241 {
242 gchar *text = popup_get_name(ft);
243
244 /* measure */
245 p->a_text->texture[0].data.text.string = text;
246 maxwidth = MAX(maxwidth, RrMinWidth(p->a_text));
247
248 if (!create_targets)
249 g_free(text);
250 else {
251 ObFocusCyclePopupTarget *t = g_new(ObFocusCyclePopupTarget, 1);
252
253 t->client = ft;
254 t->text = text;
255 t->iconwin = create_window(p->bg, 0, 0, NULL);
256 t->textwin = create_window(p->bg, 0, 0, NULL);
257
258 XMapWindow(obt_display, t->iconwin);
259 XMapWindow(obt_display, t->textwin);
260
261 p->targets = g_list_prepend(p->targets, t);
262 ++n;
263 }
264 }
265 }
266
267 p->n_targets = n;
268 p->maxtextw = maxwidth;
269 }
270
271 static gchar *popup_get_name(ObClient *c)
272 {
273 ObClient *p;
274 gchar *title;
275 const gchar *desk = NULL;
276 gchar *ret;
277
278 /* find our highest direct parent */
279 p = client_search_top_direct_parent(c);
280
281 if (c->desktop != DESKTOP_ALL && c->desktop != screen_desktop)
282 desk = screen_desktop_names[c->desktop];
283
284 title = c->iconic ? c->icon_title : c->title;
285
286 /* use the transient's parent's title/icon if we don't have one */
287 if (p != c && title[0] == '\0')
288 title = p->iconic ? p->icon_title : p->title;
289
290 if (desk)
291 ret = g_strdup_printf("%s [%s]", title, desk);
292 else
293 ret = g_strdup(title);
294
295 return ret;
296 }
297
298 static void popup_render(ObFocusCyclePopup *p, const ObClient *c,
299 ObFocusCyclePopupMode mode)
300 {
301 gint ml, mt, mr, mb;
302 gint l, t, r, b;
303 gint x, y, w, h;
304 Rect *screen_area = NULL;
305 gint rgbax, rgbay, rgbaw, rgbah;
306 gint i;
307 GList *it;
308 const ObFocusCyclePopupTarget *newtarget;
309 gint icons_per_row;
310 gint icon_rows;
311 gint textw, texth;
312
313 /* vars for icon mode */
314 gint icon_mode_textx;
315 gint icon_mode_texty;
316 gint icons_center_x;
317
318 /* vars for list mode */
319 gint list_mode_icon_column_w = HILITE_SIZE + OUTSIDE_BORDER;
320
321 g_assert(mode == OB_FOCUS_CYCLE_POPUP_MODE_ICONS ||
322 mode == OB_FOCUS_CYCLE_POPUP_MODE_LIST);
323
324 screen_area = screen_physical_area_active();
325
326 /* get the outside margins */
327 RrMargins(p->a_bg, &ml, &mt, &mr, &mb);
328
329 /* get our outside borders */
330 l = ml + OUTSIDE_BORDER;
331 r = mr + OUTSIDE_BORDER;
332 t = mt + OUTSIDE_BORDER;
333 b = mb + OUTSIDE_BORDER;
334
335 /* get the width from the text and keep it within limits */
336 w = l + r + p->maxtextw;
337 if (mode == OB_FOCUS_CYCLE_POPUP_MODE_LIST)
338 /* when in list mode, there are icons down the side */
339 w += list_mode_icon_column_w;
340 w = MIN(w, MAX(screen_area->width/3, POPUP_WIDTH)); /* max width */
341 w = MAX(w, POPUP_WIDTH); /* min width */
342
343 if (mode == OB_FOCUS_CYCLE_POPUP_MODE_ICONS) {
344 /* how many icons will fit in that row? make the width fit that */
345 w -= l + r;
346 icons_per_row = (w + ICON_SIZE - 1) / ICON_SIZE;
347 w = icons_per_row * ICON_SIZE + l + r;
348
349 /* how many rows do we need? */
350 icon_rows = (p->n_targets-1) / icons_per_row + 1;
351
352 }
353 else {
354 /* in list mode, there is one column of icons.. */
355 icons_per_row = 1;
356 icon_rows = p->n_targets;
357 }
358
359 /* get the text dimensions */
360 textw = w - l - r;
361 if (mode == OB_FOCUS_CYCLE_POPUP_MODE_LIST)
362 /* leave space on the side for the icons */
363 textw -= list_mode_icon_column_w;
364 texth = RrMinHeight(p->a_text) + TEXT_BORDER * 2;
365
366 /* find the height of the dialog */
367 #warning limit the height and scroll entries somehow
368 h = t + b + (icon_rows * MAX(HILITE_SIZE, texth));
369 if (mode == OB_FOCUS_CYCLE_POPUP_MODE_ICONS)
370 /* in icon mode the text sits below the icons, so make some space */
371 h += OUTSIDE_BORDER + texth;
372
373 /* center the icons if there is less than one row */
374 if (icon_rows == 1)
375 icons_center_x = (w - p->n_targets * ICON_SIZE) / 2;
376 else
377 icons_center_x = 0;
378
379 if (mode == OB_FOCUS_CYCLE_POPUP_MODE_ICONS) {
380 /* get the position of the text */
381 icon_mode_textx = l;
382 icon_mode_texty = h - texth - b;
383 }
384
385 /* find the position for the popup (include the outer borders) */
386 x = screen_area->x + (screen_area->width -
387 (w + ob_rr_theme->obwidth * 2)) / 2;
388 y = screen_area->y + (screen_area->height -
389 (h + ob_rr_theme->obwidth * 2)) / 2;
390
391 /* get the dimensions of the target hilite texture */
392 rgbax = ml;
393 rgbay = mt;
394 rgbaw = w - ml - mr;
395 rgbah = h - mt - mb;
396
397 if (!p->mapped) {
398 /* position the background but don't draw it */
399 XMoveResizeWindow(obt_display, p->bg, x, y, w, h);
400
401 if (mode == OB_FOCUS_CYCLE_POPUP_MODE_ICONS)
402 /* position the text */
403 XMoveResizeWindow(obt_display, p->icon_mode_text,
404 icon_mode_textx, icon_mode_texty, textw, texth);
405 }
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
418 g_assert(newtarget != NULL);
419
420 /* * * draw everything * * */
421
422 /* draw the background */
423 if (!p->mapped)
424 RrPaint(p->a_bg, p->bg, w, h);
425
426 /* draw the icons and text */
427 for (i = 0, it = p->targets; it; ++i, it = g_list_next(it)) {
428 const ObFocusCyclePopupTarget *target = it->data;
429
430 /* have to redraw the targetted icon and last targetted icon
431 * to update the hilite */
432 if (!p->mapped || newtarget == target || p->last_target == target) {
433 const gint row = i / icons_per_row; /* starting from 0 */
434 const gint col = i % icons_per_row; /* starting from 0 */
435 const ObClientIcon *icon;
436 gint iconx, icony;
437 gint list_mode_textx, list_mode_texty;
438 RrAppearance *text;
439
440 /* find the coordinates for the icon */
441 iconx = icons_center_x + l + (col * HILITE_SIZE);
442 icony = t + (row * MAX(texth, HILITE_SIZE))
443 + MAX(texth - HILITE_SIZE, 0) / 2;
444
445 /* find the dimensions of the text box */
446 list_mode_textx = iconx + HILITE_SIZE + TEXT_BORDER;
447 list_mode_texty = icony + HILITE_OFFSET;
448 // textw = w
449 // /* left edge */ - innerx - HILITE_SIZE - TEXT_BORDER
450 // /* right edge */ - OUTSIDE_BORDER - TEXT_BORDER;
451
452 /* position the icon */
453 XMoveResizeWindow(obt_display, target->iconwin,
454 iconx, icony, HILITE_SIZE, HILITE_SIZE);
455
456 /* position the text */
457 if (mode == OB_FOCUS_CYCLE_POPUP_MODE_LIST)
458 XMoveResizeWindow(obt_display, target->textwin,
459 list_mode_textx, list_mode_texty,
460 textw, texth);
461
462 /* get the icon from the client */
463 icon = client_icon(target->client, ICON_SIZE, ICON_SIZE);
464 p->a_icon->texture[0].data.rgba.width = icon->width;
465 p->a_icon->texture[0].data.rgba.height = icon->height;
466 p->a_icon->texture[0].data.rgba.twidth = ICON_SIZE;
467 p->a_icon->texture[0].data.rgba.theight = ICON_SIZE;
468 p->a_icon->texture[0].data.rgba.tx = HILITE_OFFSET;
469 p->a_icon->texture[0].data.rgba.ty = HILITE_OFFSET;
470 p->a_icon->texture[0].data.rgba.alpha =
471 target->client->iconic ? OB_ICONIC_ALPHA : 0xff;
472 p->a_icon->texture[0].data.rgba.data = icon->data;
473
474 /* Draw the hilite? */
475 p->a_icon->texture[1].type = (target == newtarget) ?
476 RR_TEXTURE_RGBA : RR_TEXTURE_NONE;
477
478 /* draw the icon */
479 p->a_icon->surface.parentx = iconx;
480 p->a_icon->surface.parenty = icony;
481 RrPaint(p->a_icon, target->iconwin, HILITE_SIZE, HILITE_SIZE);
482
483 /* draw the text */
484 if (mode == OB_FOCUS_CYCLE_POPUP_MODE_LIST || target == newtarget)
485 {
486 text = (target == newtarget) ? p->a_hilite_text : p->a_text;
487 text->texture[0].data.text.string = target->text;
488 text->surface.parentx =
489 mode == OB_FOCUS_CYCLE_POPUP_MODE_ICONS ?
490 icon_mode_textx : list_mode_textx;
491 text->surface.parenty =
492 mode == OB_FOCUS_CYCLE_POPUP_MODE_ICONS ?
493 icon_mode_texty : list_mode_texty;
494 RrPaint(text,
495 (mode == OB_FOCUS_CYCLE_POPUP_MODE_ICONS ?
496 p->icon_mode_text : target->textwin),
497 textw, texth);
498 }
499 }
500 }
501
502 p->last_target = newtarget;
503
504 g_free(screen_area);
505 }
506
507 void focus_cycle_popup_show(ObClient *c, gboolean iconic_windows,
508 gboolean all_desktops, gboolean dock_windows,
509 gboolean desktop_windows,
510 ObFocusCyclePopupMode mode)
511 {
512 g_assert(c != NULL);
513
514 if (mode == OB_FOCUS_CYCLE_POPUP_MODE_NONE) {
515 focus_cycle_popup_hide();
516 return;
517 }
518
519 /* do this stuff only when the dialog is first showing */
520 if (!popup.mapped)
521 popup_setup(&popup, TRUE, iconic_windows, all_desktops,
522 dock_windows, desktop_windows);
523 g_assert(popup.targets != NULL);
524
525 popup_render(&popup, c, mode);
526
527 if (!popup.mapped) {
528 /* show the dialog */
529 XMapWindow(obt_display, popup.bg);
530 XFlush(obt_display);
531 popup.mapped = TRUE;
532 screen_hide_desktop_popup();
533 }
534 }
535
536 void focus_cycle_popup_hide(void)
537 {
538 gulong ignore_start;
539
540 ignore_start = event_start_ignore_all_enters();
541
542 XUnmapWindow(obt_display, popup.bg);
543 XFlush(obt_display);
544
545 event_end_ignore_all_enters(ignore_start);
546
547 popup.mapped = FALSE;
548
549 while(popup.targets) {
550 ObFocusCyclePopupTarget *t = popup.targets->data;
551
552 g_free(t->text);
553 XDestroyWindow(obt_display, t->iconwin);
554 XDestroyWindow(obt_display, t->textwin);
555 g_free(t);
556
557 popup.targets = g_list_delete_link(popup.targets, popup.targets);
558 }
559 popup.n_targets = 0;
560 popup.last_target = NULL;
561 }
562
563 void focus_cycle_popup_single_show(struct _ObClient *c,
564 gboolean iconic_windows,
565 gboolean all_desktops,
566 gboolean dock_windows,
567 gboolean desktop_windows)
568 {
569 gchar *text;
570
571 g_assert(c != NULL);
572
573 /* do this stuff only when the dialog is first showing */
574 if (!popup.mapped) {
575 Rect *a;
576
577 popup_setup(&popup, FALSE, iconic_windows, all_desktops,
578 dock_windows, desktop_windows);
579 g_assert(popup.targets == NULL);
580
581 /* position the popup */
582 a = screen_physical_area_active();
583 icon_popup_position(single_popup, CenterGravity,
584 a->x + a->width / 2, a->y + a->height / 2);
585 icon_popup_height(single_popup, POPUP_HEIGHT);
586 icon_popup_min_width(single_popup, POPUP_WIDTH);
587 icon_popup_max_width(single_popup, MAX(a->width/3, POPUP_WIDTH));
588 icon_popup_text_width(single_popup, popup.maxtextw);
589 g_free(a);
590 }
591
592 text = popup_get_name(c);
593 icon_popup_show(single_popup, text, client_icon(c, HILITE_SIZE,
594 HILITE_SIZE));
595 g_free(text);
596 screen_hide_desktop_popup();
597 }
598
599 void focus_cycle_popup_single_hide(void)
600 {
601 icon_popup_hide(single_popup);
602 }
This page took 0.059532 seconds and 5 git commands to generate.