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