]> Dogcows Code - chaz/openbox/blob - openbox/popup.c
split the move and resize functions.
[chaz/openbox] / openbox / popup.c
1 /* -*- indent-tabs-mode: nil; tab-width: 4; c-basic-offset: 4; -*-
2
3 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 "popup.h"
21
22 #include "openbox.h"
23 #include "frame.h"
24 #include "client.h"
25 #include "stacking.h"
26 #include "event.h"
27 #include "screen.h"
28 #include "mainloop.h"
29 #include "render/render.h"
30 #include "render/theme.h"
31
32 ObPopup *popup_new()
33 {
34 XSetWindowAttributes attrib;
35 ObPopup *self = g_new0(ObPopup, 1);
36
37 self->obwin.type = Window_Internal;
38 self->gravity = NorthWestGravity;
39 self->x = self->y = self->textw = self->h = 0;
40 self->a_bg = RrAppearanceCopy(ob_rr_theme->osd_hilite_bg);
41 self->a_text = RrAppearanceCopy(ob_rr_theme->osd_hilite_label);
42 self->iconwm = self->iconhm = 1;
43
44 attrib.override_redirect = True;
45 self->bg = XCreateWindow(ob_display, RootWindow(ob_display, ob_screen),
46 0, 0, 1, 1, 0, RrDepth(ob_rr_inst),
47 InputOutput, RrVisual(ob_rr_inst),
48 CWOverrideRedirect, &attrib);
49
50 self->text = XCreateWindow(ob_display, self->bg,
51 0, 0, 1, 1, 0, RrDepth(ob_rr_inst),
52 InputOutput, RrVisual(ob_rr_inst), 0, NULL);
53
54 XSetWindowBorderWidth(ob_display, self->bg, ob_rr_theme->fbwidth);
55 XSetWindowBorder(ob_display, self->bg,
56 RrColorPixel(ob_rr_theme->frame_focused_border_color));
57
58 XMapWindow(ob_display, self->text);
59
60 stacking_add(INTERNAL_AS_WINDOW(self));
61 return self;
62 }
63
64 void popup_free(ObPopup *self)
65 {
66 if (self) {
67 XDestroyWindow(ob_display, self->bg);
68 XDestroyWindow(ob_display, self->text);
69 RrAppearanceFree(self->a_bg);
70 RrAppearanceFree(self->a_text);
71 stacking_remove(self);
72 g_free(self);
73 }
74 }
75
76 void popup_position(ObPopup *self, gint gravity, gint x, gint y)
77 {
78 self->gravity = gravity;
79 self->x = x;
80 self->y = y;
81 }
82
83 void popup_text_width(ObPopup *self, gint w)
84 {
85 self->textw = w;
86 }
87
88 void popup_min_width(ObPopup *self, gint minw)
89 {
90 self->minw = minw;
91 }
92
93 void popup_max_width(ObPopup *self, gint maxw)
94 {
95 self->maxw = maxw;
96 }
97
98 void popup_height(ObPopup *self, gint h)
99 {
100 gint texth;
101
102 /* don't let the height be smaller than the text */
103 texth = RrMinHeight(self->a_text) + ob_rr_theme->paddingy * 2;
104 self->h = MAX(h, texth);
105 }
106
107 void popup_text_width_to_string(ObPopup *self, gchar *text)
108 {
109 if (text[0] != '\0') {
110 self->a_text->texture[0].data.text.string = text;
111 self->textw = RrMinWidth(self->a_text);
112 } else
113 self->textw = 0;
114 }
115
116 void popup_height_to_string(ObPopup *self, gchar *text)
117 {
118 self->h = RrMinHeight(self->a_text) + ob_rr_theme->paddingy * 2;
119 }
120
121 void popup_text_width_to_strings(ObPopup *self, gchar **strings, gint num)
122 {
123 gint i, maxw;
124
125 maxw = 0;
126 for (i = 0; i < num; ++i) {
127 popup_text_width_to_string(self, strings[i]);
128 maxw = MAX(maxw, self->textw);
129 }
130 self->textw = maxw;
131 }
132
133 void popup_set_text_align(ObPopup *self, RrJustify align)
134 {
135 self->a_text->texture[0].data.text.justify = align;
136 }
137
138 static gboolean popup_show_timeout(gpointer data)
139 {
140 ObPopup *self = data;
141
142 XMapWindow(ob_display, self->bg);
143 stacking_raise(INTERNAL_AS_WINDOW(self));
144 self->mapped = TRUE;
145 self->delay_mapped = FALSE;
146
147 return FALSE; /* don't repeat */
148 }
149
150 void popup_delay_show(ObPopup *self, gulong usec, gchar *text)
151 {
152 gint l, t, r, b;
153 gint x, y, w, h;
154 gint emptyx, emptyy; /* empty space between elements */
155 gint textx, texty, textw, texth;
156 gint iconx, icony, iconw, iconh;
157
158 RrMargins(self->a_bg, &l, &t, &r, &b);
159
160 /* set up the textures */
161 self->a_text->texture[0].data.text.string = text;
162
163 /* measure the text out */
164 if (text[0] != '\0') {
165 RrMinSize(self->a_text, &textw, &texth);
166 } else {
167 textw = 0;
168 texth = RrMinHeight(self->a_text);
169 }
170
171 /* get the height, which is also used for the icon width */
172 emptyy = t + b + ob_rr_theme->paddingy * 2;
173 if (self->h)
174 texth = self->h - emptyy;
175 h = texth * self->iconhm + emptyy;
176
177 if (self->textw)
178 textw = self->textw;
179
180 iconx = textx = l + ob_rr_theme->paddingx;
181
182 emptyx = l + r + ob_rr_theme->paddingx * 2;
183 if (self->hasicon) {
184 iconw = texth * self->iconwm;
185 iconh = texth * self->iconhm;
186 textx += iconw + ob_rr_theme->paddingx;
187 if (textw)
188 emptyx += ob_rr_theme->paddingx; /* between the icon and text */
189 } else
190 iconw = 0;
191
192 texty = (h - texth - emptyy) / 2 + t + ob_rr_theme->paddingy;
193 icony = (h - iconh - emptyy) / 2 + t + ob_rr_theme->paddingy;
194
195 w = textw + emptyx + iconw;
196 /* cap it at maxw/minw */
197 if (self->maxw) w = MIN(w, self->maxw);
198 if (self->minw) w = MAX(w, self->minw);
199 textw = w - emptyx - iconw;
200
201 /* sanity checks to avoid crashes! */
202 if (w < 1) w = 1;
203 if (h < 1) h = 1;
204 if (texth < 1) texth = 1;
205
206 /* set up the x coord */
207 x = self->x;
208 switch (self->gravity) {
209 case NorthGravity: case CenterGravity: case SouthGravity:
210 x -= w / 2;
211 break;
212 case NorthEastGravity: case EastGravity: case SouthEastGravity:
213 x -= w;
214 break;
215 }
216
217 /* set up the y coord */
218 y = self->y;
219 switch (self->gravity) {
220 case WestGravity: case CenterGravity: case EastGravity:
221 y -= h / 2;
222 break;
223 case SouthWestGravity: case SouthGravity: case SouthEastGravity:
224 y -= h;
225 break;
226 }
227
228 /* set the windows/appearances up */
229 XMoveResizeWindow(ob_display, self->bg, x, y, w, h);
230 RrPaint(self->a_bg, self->bg, w, h);
231
232 if (textw) {
233 self->a_text->surface.parent = self->a_bg;
234 self->a_text->surface.parentx = textx;
235 self->a_text->surface.parenty = texty;
236 XMoveResizeWindow(ob_display, self->text, textx, texty, textw, texth);
237 RrPaint(self->a_text, self->text, textw, texth);
238 }
239
240 if (self->hasicon)
241 self->draw_icon(iconx, icony, iconw, iconh, self->draw_icon_data);
242
243 /* do the actual showing */
244 if (!self->mapped) {
245 if (usec) {
246 /* don't kill previous show timers */
247 if (!self->delay_mapped) {
248 ob_main_loop_timeout_add(ob_main_loop, usec,
249 popup_show_timeout, self,
250 g_direct_equal, NULL);
251 self->delay_mapped = TRUE;
252 }
253 } else {
254 popup_show_timeout(self);
255 }
256 }
257 }
258
259 void popup_hide(ObPopup *self)
260 {
261 if (self->mapped) {
262 XUnmapWindow(ob_display, self->bg);
263 self->mapped = FALSE;
264
265 /* kill enter events cause by this unmapping */
266 event_ignore_queued_enters();
267 } else if (self->delay_mapped) {
268 ob_main_loop_timeout_remove(ob_main_loop, popup_show_timeout);
269 self->delay_mapped = FALSE;
270 }
271 }
272
273 static void icon_popup_draw_icon(gint x, gint y, gint w, gint h, gpointer data)
274 {
275 ObIconPopup *self = data;
276
277 self->a_icon->surface.parent = self->popup->a_bg;
278 self->a_icon->surface.parentx = x;
279 self->a_icon->surface.parenty = y;
280 XMoveResizeWindow(ob_display, self->icon, x, y, w, h);
281 RrPaint(self->a_icon, self->icon, w, h);
282 }
283
284 ObIconPopup *icon_popup_new()
285 {
286 ObIconPopup *self;
287
288 self = g_new0(ObIconPopup, 1);
289 self->popup = popup_new(TRUE);
290 self->a_icon = RrAppearanceCopy(ob_rr_theme->a_clear_tex);
291 self->icon = XCreateWindow(ob_display, self->popup->bg,
292 0, 0, 1, 1, 0,
293 RrDepth(ob_rr_inst), InputOutput,
294 RrVisual(ob_rr_inst), 0, NULL);
295 XMapWindow(ob_display, self->icon);
296
297 self->popup->hasicon = TRUE;
298 self->popup->draw_icon = icon_popup_draw_icon;
299 self->popup->draw_icon_data = self;
300
301 return self;
302 }
303
304 void icon_popup_free(ObIconPopup *self)
305 {
306 if (self) {
307 XDestroyWindow(ob_display, self->icon);
308 RrAppearanceFree(self->a_icon);
309 popup_free(self->popup);
310 g_free(self);
311 }
312 }
313
314 void icon_popup_delay_show(ObIconPopup *self, gulong usec,
315 gchar *text, const ObClientIcon *icon)
316 {
317 if (icon) {
318 self->a_icon->texture[0].type = RR_TEXTURE_RGBA;
319 self->a_icon->texture[0].data.rgba.width = icon->width;
320 self->a_icon->texture[0].data.rgba.height = icon->height;
321 self->a_icon->texture[0].data.rgba.data = icon->data;
322 } else
323 self->a_icon->texture[0].type = RR_TEXTURE_NONE;
324
325 popup_delay_show(self->popup, usec, text);
326 }
327
328 void icon_popup_icon_size_multiplier(ObIconPopup *self, guint wm, guint hm)
329 {
330 /* cap them at 1 */
331 self->popup->iconwm = MAX(1, wm);
332 self->popup->iconhm = MAX(1, hm);
333 }
334
335 static void pager_popup_draw_icon(gint px, gint py, gint w, gint h,
336 gpointer data)
337 {
338 ObPagerPopup *self = data;
339 gint x, y;
340 guint rown, n;
341 guint horz_inc;
342 guint vert_inc;
343 guint r, c;
344 gint eachw, eachh;
345 const guint cols = screen_desktop_layout.columns;
346 const guint rows = screen_desktop_layout.rows;
347 const gint linewidth = ob_rr_theme->fbwidth;
348
349 eachw = (w - ((cols + 1) * linewidth)) / cols;
350 eachh = (h - ((rows + 1) * linewidth)) / rows;
351 /* make them squares */
352 eachw = eachh = MIN(eachw, eachh);
353
354 /* center */
355 px += (w - (cols * (eachw + linewidth) + linewidth)) / 2;
356 py += (h - (rows * (eachh + linewidth) + linewidth)) / 2;
357
358 if (eachw <= 0 || eachh <= 0)
359 return;
360
361 switch (screen_desktop_layout.orientation) {
362 case OB_ORIENTATION_HORZ:
363 switch (screen_desktop_layout.start_corner) {
364 case OB_CORNER_TOPLEFT:
365 n = 0;
366 horz_inc = 1;
367 vert_inc = cols;
368 break;
369 case OB_CORNER_TOPRIGHT:
370 n = cols - 1;
371 horz_inc = -1;
372 vert_inc = cols;
373 break;
374 case OB_CORNER_BOTTOMRIGHT:
375 n = rows * cols - 1;
376 horz_inc = -1;
377 vert_inc = -screen_desktop_layout.columns;
378 break;
379 case OB_CORNER_BOTTOMLEFT:
380 n = (rows - 1) * cols;
381 horz_inc = 1;
382 vert_inc = -cols;
383 break;
384 default:
385 g_assert_not_reached();
386 }
387 break;
388 case OB_ORIENTATION_VERT:
389 switch (screen_desktop_layout.start_corner) {
390 case OB_CORNER_TOPLEFT:
391 n = 0;
392 horz_inc = rows;
393 vert_inc = 1;
394 break;
395 case OB_CORNER_TOPRIGHT:
396 n = rows * (cols - 1);
397 horz_inc = -rows;
398 vert_inc = 1;
399 break;
400 case OB_CORNER_BOTTOMRIGHT:
401 n = rows * cols - 1;
402 horz_inc = -rows;
403 vert_inc = -1;
404 break;
405 case OB_CORNER_BOTTOMLEFT:
406 n = rows - 1;
407 horz_inc = rows;
408 vert_inc = -1;
409 break;
410 default:
411 g_assert_not_reached();
412 }
413 break;
414 default:
415 g_assert_not_reached();
416 }
417
418 rown = n;
419 for (r = 0, y = 0; r < rows; ++r, y += eachh + linewidth)
420 {
421 for (c = 0, x = 0; c < cols; ++c, x += eachw + linewidth)
422 {
423 RrAppearance *a;
424
425 if (n < self->desks) {
426 a = (n == self->curdesk ? self->hilight : self->unhilight);
427
428 a->surface.parent = self->popup->a_bg;
429 a->surface.parentx = x + px;
430 a->surface.parenty = y + py;
431 XMoveResizeWindow(ob_display, self->wins[n],
432 x + px, y + py, eachw, eachh);
433 RrPaint(a, self->wins[n], eachw, eachh);
434 }
435 n += horz_inc;
436 }
437 n = rown += vert_inc;
438 }
439 }
440
441 ObPagerPopup *pager_popup_new()
442 {
443 ObPagerPopup *self;
444
445 self = g_new(ObPagerPopup, 1);
446 self->popup = popup_new(TRUE);
447
448 self->desks = 0;
449 self->wins = g_new(Window, self->desks);
450 self->hilight = RrAppearanceCopy(ob_rr_theme->osd_hilite_fg);
451 self->unhilight = RrAppearanceCopy(ob_rr_theme->osd_unhilite_fg);
452
453 self->popup->hasicon = TRUE;
454 self->popup->draw_icon = pager_popup_draw_icon;
455 self->popup->draw_icon_data = self;
456
457 return self;
458 }
459
460 void pager_popup_free(ObPagerPopup *self)
461 {
462 if (self) {
463 guint i;
464
465 for (i = 0; i < self->desks; ++i)
466 XDestroyWindow(ob_display, self->wins[i]);
467 g_free(self->wins);
468 RrAppearanceFree(self->hilight);
469 RrAppearanceFree(self->unhilight);
470 popup_free(self->popup);
471 g_free(self);
472 }
473 }
474
475 void pager_popup_delay_show(ObPagerPopup *self, gulong usec,
476 gchar *text, guint desk)
477 {
478 guint i;
479
480 if (screen_num_desktops < self->desks)
481 for (i = screen_num_desktops; i < self->desks; ++i)
482 XDestroyWindow(ob_display, self->wins[i]);
483
484 if (screen_num_desktops != self->desks)
485 self->wins = g_renew(Window, self->wins, screen_num_desktops);
486
487 if (screen_num_desktops > self->desks)
488 for (i = self->desks; i < screen_num_desktops; ++i) {
489 XSetWindowAttributes attr;
490
491 attr.border_pixel =
492 RrColorPixel(ob_rr_theme->frame_focused_border_color);
493 self->wins[i] = XCreateWindow(ob_display, self->popup->bg,
494 0, 0, 1, 1, ob_rr_theme->fbwidth,
495 RrDepth(ob_rr_inst), InputOutput,
496 RrVisual(ob_rr_inst), CWBorderPixel,
497 &attr);
498 XMapWindow(ob_display, self->wins[i]);
499 }
500
501 self->desks = screen_num_desktops;
502 self->curdesk = desk;
503
504 popup_delay_show(self->popup, usec, text);
505 }
506
507 void pager_popup_icon_size_multiplier(ObPagerPopup *self, guint wm, guint hm)
508 {
509 /* cap them at 1 */
510 self->popup->iconwm = MAX(1, wm);
511 self->popup->iconhm = MAX(1, hm);
512 }
This page took 0.05529 seconds and 4 git commands to generate.