]> Dogcows Code - chaz/openbox/blob - openbox/focus_cycle_popup.c
add some padding inside the text box
[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 "focus_cycle.h"
26 #include "openbox.h"
27 #include "window.h"
28 #include "event.h"
29 #include "render/render.h"
30
31 #include <X11/Xlib.h>
32 #include <glib.h>
33
34 #define ICON_SIZE 40
35 #define ICON_HILITE_WIDTH 2
36 #define ICON_HILITE_MARGIN 1
37 #define OUTSIDE_BORDER 3
38 #define TEXT_BORDER 2
39
40 typedef struct _ObFocusCyclePopup ObFocusCyclePopup;
41 typedef struct _ObFocusCyclePopupTarget ObFocusCyclePopupTarget;
42
43 struct _ObFocusCyclePopupTarget
44 {
45 ObClient *client;
46 gchar *text;
47 Window win;
48 };
49
50 struct _ObFocusCyclePopup
51 {
52 ObWindow obwin;
53 Window bg;
54
55 Window text;
56
57 GList *targets;
58 gint n_targets;
59
60 const ObFocusCyclePopupTarget *last_target;
61
62 gint maxtextw;
63
64 RrAppearance *a_bg;
65 RrAppearance *a_text;
66 RrAppearance *a_icon;
67
68 RrPixel32 *hilite_rgba;
69
70 gboolean mapped;
71 };
72
73 /*! This popup shows all possible windows */
74 static ObFocusCyclePopup popup;
75 /*! This popup shows a single window */
76 static ObIconPopup *single_popup;
77
78 static gchar *popup_get_name (ObClient *c);
79 static void popup_setup (ObFocusCyclePopup *p,
80 gboolean create_targets,
81 gboolean iconic_windows,
82 gboolean all_desktops,
83 gboolean dock_windows,
84 gboolean desktop_windows);
85 static void popup_render (ObFocusCyclePopup *p,
86 const ObClient *c);
87
88 static Window create_window(Window parent, guint bwidth, gulong mask,
89 XSetWindowAttributes *attr)
90 {
91 return XCreateWindow(ob_display, parent, 0, 0, 1, 1, bwidth,
92 RrDepth(ob_rr_inst), InputOutput,
93 RrVisual(ob_rr_inst), mask, attr);
94 }
95
96 void focus_cycle_popup_startup(gboolean reconfig)
97 {
98 XSetWindowAttributes attrib;
99
100 single_popup = icon_popup_new(TRUE);
101
102 popup.obwin.type = Window_Internal;
103 popup.a_bg = RrAppearanceCopy(ob_rr_theme->osd_hilite_bg);
104 popup.a_text = RrAppearanceCopy(ob_rr_theme->osd_hilite_label);
105 popup.a_icon = RrAppearanceCopy(ob_rr_theme->a_clear_tex);
106
107 popup.a_text->surface.parent = popup.a_bg;
108 popup.a_icon->surface.parent = popup.a_bg;
109
110 popup.a_icon->texture[0].type = RR_TEXTURE_RGBA;
111
112 RrAppearanceAddTextures(popup.a_bg, 1);
113 popup.a_bg->texture[0].type = RR_TEXTURE_RGBA;
114
115 attrib.override_redirect = True;
116 attrib.border_pixel=RrColorPixel(ob_rr_theme->osd_border_color);
117 popup.bg = create_window(RootWindow(ob_display, ob_screen),
118 ob_rr_theme->obwidth,
119 CWOverrideRedirect | CWBorderPixel, &attrib);
120
121 popup.text = create_window(popup.bg, 0, 0, NULL);
122
123 popup.targets = NULL;
124 popup.n_targets = 0;
125 popup.last_target = NULL;
126
127 popup.hilite_rgba = NULL;
128
129 XMapWindow(ob_display, popup.text);
130
131 stacking_add(INTERNAL_AS_WINDOW(&popup));
132 }
133
134 void focus_cycle_popup_shutdown(gboolean reconfig)
135 {
136 icon_popup_free(single_popup);
137
138 stacking_remove(INTERNAL_AS_WINDOW(&popup));
139
140 while(popup.targets) {
141 ObFocusCyclePopupTarget *t = popup.targets->data;
142
143 g_free(t->text);
144 XDestroyWindow(ob_display, t->win);
145
146 popup.targets = g_list_delete_link(popup.targets, popup.targets);
147 }
148
149 g_free(popup.hilite_rgba);
150
151 XDestroyWindow(ob_display, popup.text);
152 XDestroyWindow(ob_display, popup.bg);
153
154 RrAppearanceFree(popup.a_icon);
155 RrAppearanceFree(popup.a_text);
156 RrAppearanceFree(popup.a_bg);
157 }
158
159 static void popup_setup(ObFocusCyclePopup *p, gboolean create_targets,
160 gboolean iconic_windows, gboolean all_desktops,
161 gboolean dock_windows, gboolean desktop_windows)
162 {
163 gint maxwidth, n;
164 GList *it;
165
166 g_assert(p->targets == NULL);
167 g_assert(p->n_targets == 0);
168
169 /* make its width to be the width of all the possible titles */
170
171 /* build a list of all the valid focus targets and measure their strings,
172 and count them */
173 maxwidth = 0;
174 n = 0;
175 for (it = g_list_last(focus_order); it; it = g_list_previous(it)) {
176 ObClient *ft = it->data;
177
178 if (focus_cycle_target_valid(ft,
179 iconic_windows,
180 all_desktops,
181 dock_windows,
182 desktop_windows))
183 {
184 gchar *text = popup_get_name(ft);
185
186 /* measure */
187 p->a_text->texture[0].data.text.string = text;
188 maxwidth = MAX(maxwidth, RrMinWidth(p->a_text));
189
190 if (!create_targets)
191 g_free(text);
192 else {
193 ObFocusCyclePopupTarget *t = g_new(ObFocusCyclePopupTarget, 1);
194
195 t->client = ft;
196 t->text = text;
197 t->win = create_window(p->bg, 0, 0, NULL);
198
199 XMapWindow(ob_display, t->win);
200
201 p->targets = g_list_prepend(p->targets, t);
202 ++n;
203 }
204 }
205 }
206
207 p->n_targets = n;
208 p->maxtextw = maxwidth;
209 }
210
211 static gchar *popup_get_name(ObClient *c)
212 {
213 ObClient *p;
214 gchar *title = NULL;
215 const gchar *desk = NULL;
216 gchar *ret;
217
218 /* find our highest direct parent, including non-normal windows */
219 for (p = c; p->transient_for && p->transient_for != OB_TRAN_GROUP;
220 p = p->transient_for);
221
222 if (c->desktop != DESKTOP_ALL && c->desktop != screen_desktop)
223 desk = screen_desktop_names[c->desktop];
224
225 /* use the transient's parent's title/icon if we don't have one */
226 if (p != c && !strcmp("", (c->iconic ? c->icon_title : c->title)))
227 title = g_strdup(p->iconic ? p->icon_title : p->title);
228
229 if (title == NULL)
230 title = g_strdup(c->iconic ? c->icon_title : c->title);
231
232 if (desk)
233 ret = g_strdup_printf("%s [%s]", title, desk);
234 else {
235 ret = title;
236 title = NULL;
237 }
238 g_free(title);
239
240 return ret;
241 }
242
243 static void popup_render(ObFocusCyclePopup *p, const ObClient *c)
244 {
245 gint ml, mt, mr, mb;
246 gint l, t, r, b;
247 gint x, y, w, h;
248 Rect *screen_area;
249 gint icons_per_row;
250 gint icon_rows;
251 gint textx, texty, textw, texth;
252 gint rgbax, rgbay, rgbaw, rgbah;
253 gint icons_center_x;
254 gint innerw, innerh;
255 gint i;
256 GList *it;
257 const ObFocusCyclePopupTarget *newtarget;
258 gint newtargetx, newtargety;
259
260 /* XXX find the middle monitor? */
261 screen_area = screen_physical_area_monitor(0);
262
263 /* get the outside margins */
264 RrMargins(p->a_bg, &ml, &mt, &mr, &mb);
265
266 /* get our outside borders */
267 l = ml + OUTSIDE_BORDER;
268 r = mr + OUTSIDE_BORDER;
269 t = mt + OUTSIDE_BORDER;
270 b = mb + OUTSIDE_BORDER;
271
272 /* get the icon pictures' sizes */
273 innerw = ICON_SIZE - (ICON_HILITE_WIDTH + ICON_HILITE_MARGIN) * 2;
274 innerh = ICON_SIZE - (ICON_HILITE_WIDTH + ICON_HILITE_MARGIN) * 2;
275
276 /* get the width from the text and keep it within limits */
277 w = l + r + p->maxtextw;
278 w = MIN(w, MAX(screen_area->width/3, POPUP_WIDTH)); /* max width */
279 w = MAX(w, POPUP_WIDTH); /* min width */
280
281 /* how many icons will fit in that row? make the width fit that */
282 w -= l + r;
283 icons_per_row = (w + ICON_SIZE - 1) / ICON_SIZE;
284 w = icons_per_row * ICON_SIZE + l + r;
285
286 /* how many rows do we need? */
287 icon_rows = (p->n_targets-1) / icons_per_row + 1;
288
289 /* get the text dimensions */
290 textw = w - l - r;
291 texth = RrMinHeight(p->a_text) + TEXT_BORDER * 2;
292
293 /* find the height of the dialog */
294 h = t + b + (icon_rows * ICON_SIZE) + (OUTSIDE_BORDER + texth);
295
296 /* get the position of the text */
297 textx = l;
298 texty = h - texth - b;
299
300 /* find the position for the popup (include the outer borders) */
301 x = screen_area->x + (screen_area->width -
302 (w + ob_rr_theme->obwidth * 2)) / 2;
303 y = screen_area->y + (screen_area->height -
304 (h + ob_rr_theme->obwidth * 2)) / 2;
305
306 /* get the dimensions of the target hilite texture */
307 rgbax = ml;
308 rgbay = mt;
309 rgbaw = w - ml - mr;
310 rgbah = h - mt - mb;
311
312 /* center the icons if there is less than one row */
313 if (icon_rows == 1)
314 icons_center_x = (w - p->n_targets * ICON_SIZE) / 2;
315 else
316 icons_center_x = 0;
317
318 if (!p->mapped) {
319 /* position the background but don't draw it*/
320 XMoveResizeWindow(ob_display, p->bg, x, y, w, h);
321
322 /* set up the hilite texture for the background */
323 p->a_bg->texture[0].data.rgba.width = rgbaw;
324 p->a_bg->texture[0].data.rgba.height = rgbah;
325 p->a_bg->texture[0].data.rgba.alpha = 0xff;
326 p->hilite_rgba = g_new(RrPixel32, rgbaw * rgbah);
327 p->a_bg->texture[0].data.rgba.data = p->hilite_rgba;
328
329 /* position the text, but don't draw it */
330 XMoveResizeWindow(ob_display, p->text, textx, texty, textw, texth);
331 p->a_text->surface.parentx = textx;
332 p->a_text->surface.parenty = texty;
333 }
334
335 /* find the focused target */
336 for (i = 0, it = p->targets; it; ++i, it = g_list_next(it)) {
337 const ObFocusCyclePopupTarget *target = it->data;
338 const gint row = i / icons_per_row; /* starting from 0 */
339 const gint col = i % icons_per_row; /* starting from 0 */
340
341 if (target->client == c) {
342 /* save the target */
343 newtarget = target;
344 newtargetx = icons_center_x + l + (col * ICON_SIZE);
345 newtargety = t + (row * ICON_SIZE);
346
347 if (!p->mapped)
348 break; /* if we're not dimensioning, then we're done */
349 }
350 }
351
352 g_assert(newtarget != NULL);
353
354 /* create the hilite under the target icon */
355 {
356 RrPixel32 color;
357 gint i, j, o;
358
359 color = ((ob_rr_theme->osd_color->r & 0xff) << RrDefaultRedOffset) +
360 ((ob_rr_theme->osd_color->g & 0xff) << RrDefaultGreenOffset) +
361 ((ob_rr_theme->osd_color->b & 0xff) << RrDefaultBlueOffset);
362
363 o = 0;
364 for (i = 0; i < rgbah; ++i)
365 for (j = 0; j < rgbaw; ++j) {
366 guchar a;
367 const gint x = j + rgbax - newtargetx;
368 const gint y = i + rgbay - newtargety;
369
370 if (x < 0 || x >= ICON_SIZE ||
371 y < 0 || y >= ICON_SIZE)
372 {
373 /* outside the target */
374 a = 0x00;
375 }
376 else if (x < ICON_HILITE_WIDTH ||
377 x >= ICON_SIZE - ICON_HILITE_WIDTH ||
378 y < ICON_HILITE_WIDTH ||
379 y >= ICON_SIZE - ICON_HILITE_WIDTH)
380 {
381 /* the border of the target */
382 a = 0x88;
383 }
384 else {
385 /* the background of the target */
386 a = 0x22;
387 }
388
389 p->hilite_rgba[o++] =
390 color + (a << RrDefaultAlphaOffset);
391 }
392 }
393
394 /* * * draw everything * * */
395
396 /* draw the background */
397 RrPaint(p->a_bg, p->bg, w, h);
398
399 /* draw the icons */
400 for (i = 0, it = p->targets; it; ++i, it = g_list_next(it)) {
401 const ObFocusCyclePopupTarget *target = it->data;
402
403 /* have to redraw the targetted icon and last targetted icon,
404 they can pick up the hilite changes in the backgroud */
405 if (!p->mapped || newtarget == target || p->last_target == target) {
406 const ObClientIcon *icon;
407 const gint row = i / icons_per_row; /* starting from 0 */
408 const gint col = i % icons_per_row; /* starting from 0 */
409 gint innerx, innery;
410
411 /* find the dimensions of the icon inside it */
412 innerx = icons_center_x + l + (col * ICON_SIZE);
413 innerx += ICON_HILITE_WIDTH + ICON_HILITE_MARGIN;
414 innery = t + (row * ICON_SIZE);
415 innery += ICON_HILITE_WIDTH + ICON_HILITE_MARGIN;
416
417 /* move the icon */
418 XMoveResizeWindow(ob_display, target->win,
419 innerx, innery, innerw, innerh);
420
421 /* get the icon from the client */
422 icon = client_icon(target->client, innerw, innerh);
423 p->a_icon->texture[0].data.rgba.width = icon->width;
424 p->a_icon->texture[0].data.rgba.height = icon->height;
425 p->a_icon->texture[0].data.rgba.alpha =
426 target->client->iconic ? OB_ICONIC_ALPHA : 0xff;
427 p->a_icon->texture[0].data.rgba.data = icon->data;
428
429 /* draw the icon */
430 p->a_icon->surface.parentx = innerx;
431 p->a_icon->surface.parenty = innery;
432 RrPaint(p->a_icon, target->win, innerw, innerh);
433 }
434 }
435
436 /* draw the text */
437 p->a_text->texture[0].data.text.string = newtarget->text;
438 p->a_text->surface.parentx = textx;
439 p->a_text->surface.parenty = texty;
440 RrPaint(p->a_text, p->text, textw, texth);
441
442 p->last_target = newtarget;
443 }
444
445 void focus_cycle_popup_show(ObClient *c, gboolean iconic_windows,
446 gboolean all_desktops, gboolean dock_windows,
447 gboolean desktop_windows)
448 {
449 g_assert(c != NULL);
450
451 /* do this stuff only when the dialog is first showing */
452 if (!popup.mapped)
453 popup_setup(&popup, TRUE, iconic_windows, all_desktops,
454 dock_windows, desktop_windows);
455 g_assert(popup.targets != NULL);
456
457 popup_render(&popup, c);
458
459 if (!popup.mapped) {
460 /* show the dialog */
461 XMapWindow(ob_display, popup.bg);
462 XFlush(ob_display);
463 popup.mapped = TRUE;
464 }
465 }
466
467 void focus_cycle_popup_hide()
468 {
469 gulong ignore_start;
470
471 ignore_start = event_start_ignore_all_enters();
472
473 XUnmapWindow(ob_display, popup.bg);
474 XFlush(ob_display);
475
476 event_end_ignore_all_enters(ignore_start);
477
478 popup.mapped = FALSE;
479
480 while(popup.targets) {
481 ObFocusCyclePopupTarget *t = popup.targets->data;
482
483 g_free(t->text);
484 XDestroyWindow(ob_display, t->win);
485
486 popup.targets = g_list_delete_link(popup.targets, popup.targets);
487 }
488 popup.n_targets = 0;
489 popup.last_target = NULL;
490
491 g_free(popup.hilite_rgba);
492 popup.hilite_rgba = NULL;
493 }
494
495 void focus_cycle_popup_single_show(struct _ObClient *c,
496 gboolean iconic_windows,
497 gboolean all_desktops,
498 gboolean dock_windows,
499 gboolean desktop_windows)
500 {
501 gchar *text;
502
503 g_assert(c != NULL);
504
505 /* do this stuff only when the dialog is first showing */
506 if (!popup.mapped) {
507 Rect *a;
508
509 popup_setup(&popup, FALSE, iconic_windows, all_desktops,
510 dock_windows, desktop_windows);
511 g_assert(popup.targets == NULL);
512
513 /* position the popup */
514 a = screen_physical_area_monitor(0);
515 icon_popup_position(single_popup, CenterGravity,
516 a->x + a->width / 2, a->y + a->height / 2);
517 icon_popup_height(single_popup, POPUP_HEIGHT);
518 icon_popup_min_width(single_popup, POPUP_WIDTH);
519 icon_popup_max_width(single_popup, MAX(a->width/3, POPUP_WIDTH));
520 icon_popup_text_width(single_popup, popup.maxtextw);
521 }
522
523 text = popup_get_name(c);
524 icon_popup_show(single_popup, text, client_icon(c, ICON_SIZE, ICON_SIZE));
525 g_free(text);
526 }
527
528 void focus_cycle_popup_single_hide()
529 {
530 icon_popup_hide(single_popup);
531 }
This page took 0.062186 seconds and 5 git commands to generate.