]> icculus.org git repositories - dana/openbox.git/blob - openbox/popup.c
update swedish translation (bork bork bork)
[dana/openbox.git] / 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 static gboolean popup_show_timeout(gpointer data)
33 {
34     ObPopup *self = data;
35     
36     XMapWindow(ob_display, self->bg);
37     stacking_raise(INTERNAL_AS_WINDOW(self));
38     self->mapped = TRUE;
39     self->delay_mapped = FALSE;
40
41     return FALSE; /* don't repeat */
42 }
43
44 ObPopup *popup_new(gboolean hasicon)
45 {
46     XSetWindowAttributes attrib;
47     ObPopup *self = g_new0(ObPopup, 1);
48
49     self->obwin.type = Window_Internal;
50     self->hasicon = hasicon;
51     self->gravity = NorthWestGravity;
52     self->x = self->y = self->w = self->h = 0;
53     self->a_bg = RrAppearanceCopy(ob_rr_theme->osd_hilite_bg);
54     self->a_text = RrAppearanceCopy(ob_rr_theme->osd_hilite_label);
55
56     attrib.override_redirect = True;
57     self->bg = XCreateWindow(ob_display, RootWindow(ob_display, ob_screen),
58                              0, 0, 1, 1, 0, RrDepth(ob_rr_inst),
59                              InputOutput, RrVisual(ob_rr_inst),
60                              CWOverrideRedirect, &attrib);
61     
62     self->text = XCreateWindow(ob_display, self->bg,
63                                0, 0, 1, 1, 0, RrDepth(ob_rr_inst),
64                                InputOutput, RrVisual(ob_rr_inst), 0, NULL);
65
66     XMapWindow(ob_display, self->text);
67
68     stacking_add(INTERNAL_AS_WINDOW(self));
69     return self;
70 }
71
72 void popup_free(ObPopup *self)
73 {
74     if (self) {
75         XDestroyWindow(ob_display, self->bg);
76         XDestroyWindow(ob_display, self->text);
77         RrAppearanceFree(self->a_bg);
78         RrAppearanceFree(self->a_text);
79         stacking_remove(self);
80         g_free(self);
81     }
82 }
83
84 void popup_position(ObPopup *self, gint gravity, gint x, gint y)
85 {
86     self->gravity = gravity;
87     self->x = x;
88     self->y = y;
89 }
90
91 void popup_width(ObPopup *self, gint w)
92 {
93     self->w = w;
94     self->maxw = w;
95 }
96
97 void popup_height(ObPopup *self, gint h)
98 {
99     gint texth;
100
101     /* don't let the height be smaller than the text */
102     texth = RrMinHeight(self->a_text) + ob_rr_theme->paddingy * 2;
103     self->h = MAX(h, texth);
104 }
105
106 void popup_width_to_string(ObPopup *self, gchar *text, gint max)
107 {
108     self->a_text->texture[0].data.text.string = text;
109     self->w = RrMinWidth(self->a_text);
110     self->maxw = max;
111 }
112
113 void popup_height_to_string(ObPopup *self, gchar *text)
114 {
115     self->h = RrMinHeight(self->a_text) + ob_rr_theme->paddingy * 2;
116 }
117
118 void popup_width_to_strings(ObPopup *self, gchar **strings, gint num, gint max)
119 {
120     gint i, maxw;
121
122     maxw = 0;
123     for (i = 0; i < num; ++i) {
124         popup_width_to_string(self, strings[i], max);
125         maxw = MAX(maxw, self->w);
126     }
127     self->w = maxw;
128 }
129
130 void popup_set_text_align(ObPopup *self, RrJustify align)
131 {
132     self->a_text->texture[0].data.text.justify = align;
133 }
134
135 void popup_delay_show(ObPopup *self, gulong usec, gchar *text)
136 {
137     gint l, t, r, b;
138     gint x, y, w, h;
139     gint xpadding, ypadding;
140     gint textw, texth;
141     gint iconw;
142     Rect *area; /* won't go outside this */
143
144     area = screen_physical_area();          /* XXX this should work quite
145                                                good, someone with xinerama,
146                                                and different resolutions on
147                                                screens? */
148
149     RrMargins(self->a_bg, &l, &t, &r, &b);
150
151     XSetWindowBorderWidth(ob_display, self->bg, ob_rr_theme->fbwidth);
152     XSetWindowBorder(ob_display, self->bg, ob_rr_theme->frame_b_color->pixel);
153
154     /* set up the textures */
155     self->a_text->texture[0].data.text.string = text;
156
157     /* measure the text out */
158     RrMinSize(self->a_text, &textw, &texth);
159     texth += ob_rr_theme->paddingy * 2;
160
161     ypadding = (t+b + ob_rr_theme->paddingy * 2);
162
163     /* set the sizes up and reget the text sizes from the calculated
164        outer sizes */
165     if (self->h) {
166         h = self->h;
167         texth = h - ypadding;
168     } else
169         h = texth + ypadding;
170
171     iconw = (self->hasicon ? texth : 0);
172     xpadding = l+r + iconw + ob_rr_theme->paddingx *
173         (self->hasicon ? 3 : 2);
174
175     if (self->w)
176         textw = self->w;
177     w = textw + xpadding;
178     /* cap it at "maxw" */
179     if (self->maxw) w = MIN(w, self->maxw);
180     textw = w - xpadding;
181
182     /* sanity checks to avoid crashes! */
183     if (w < 1) w = 1;
184     if (h < 1) h = 1;
185     if (textw < 1) textw = 1;
186     if (texth < 1) texth = 1;
187
188     /* set up the x coord */
189     x = self->x;
190     switch (self->gravity) {
191     case NorthGravity:
192     case CenterGravity:
193     case SouthGravity:
194         x -= w / 2;
195         break;
196     case NorthEastGravity:
197     case EastGravity:
198     case SouthEastGravity:
199         x -= w;
200         break;
201     }
202
203     /* set up the y coord */
204     y = self->y;
205     switch (self->gravity) {
206     case WestGravity:
207     case CenterGravity:
208     case EastGravity:
209         y -= h / 2;
210         break;
211     case SouthWestGravity:
212     case SouthGravity:
213     case SouthEastGravity:
214         y -= h;
215         break;
216     }
217
218     x=MAX(MIN(x, area->width-w),0);
219     y=MAX(MIN(y, area->height-h),0);
220
221     /* set the windows/appearances up */
222     XMoveResizeWindow(ob_display, self->bg, x, y, w, h);
223
224     self->a_text->surface.parent = self->a_bg;
225     self->a_text->surface.parentx = l + iconw +
226         ob_rr_theme->paddingx * (self->hasicon ? 2 : 1);
227     self->a_text->surface.parenty = t + ob_rr_theme->paddingy;
228     XMoveResizeWindow(ob_display, self->text,
229                       l + iconw + ob_rr_theme->paddingx *
230                       (self->hasicon ? 2 : 1),
231                       t + ob_rr_theme->paddingy, textw, texth);
232
233     RrPaint(self->a_bg, self->bg, w, h);
234     RrPaint(self->a_text, self->text, textw, texth);
235
236     if (self->hasicon) {
237         if (iconw < 1) iconw = 1; /* sanity check for crashes */
238         if (self->draw_icon)
239             self->draw_icon(l + ob_rr_theme->paddingx,
240                             t + ob_rr_theme->paddingy,
241                             iconw, texth, self->draw_icon_data);
242     }
243
244     /* do the actual showing */
245     if (!self->mapped) {
246         if (usec) {
247             /* don't kill previous show timers */
248             if (!self->delay_mapped) {
249                 ob_main_loop_timeout_add(ob_main_loop, usec,
250                                          popup_show_timeout, self,
251                                          g_direct_equal, NULL);
252                 self->delay_mapped = TRUE;
253             }
254         } else {
255             popup_show_timeout(self);
256         }
257     }
258 }
259
260 void popup_hide(ObPopup *self)
261 {
262     if (self->mapped) {
263         XUnmapWindow(ob_display, self->bg);
264         self->mapped = FALSE;
265
266         /* kill enter events cause by this unmapping */
267         event_ignore_queued_enters();
268     } else if (self->delay_mapped) {
269         ob_main_loop_timeout_remove(ob_main_loop, popup_show_timeout);
270         self->delay_mapped = FALSE;
271     }
272 }
273
274 static void icon_popup_draw_icon(gint x, gint y, gint w, gint h, gpointer data)
275 {
276     ObIconPopup *self = data;
277
278     self->a_icon->surface.parent = self->popup->a_bg;
279     self->a_icon->surface.parentx = x;
280     self->a_icon->surface.parenty = y;
281     XMoveResizeWindow(ob_display, self->icon, x, y, w, h);
282     RrPaint(self->a_icon, self->icon, w, h);
283 }
284
285 ObIconPopup *icon_popup_new()
286 {
287     ObIconPopup *self;
288
289     self = g_new0(ObIconPopup, 1);
290     self->popup = popup_new(TRUE);
291     self->a_icon = RrAppearanceCopy(ob_rr_theme->a_clear_tex);
292     self->icon = XCreateWindow(ob_display, self->popup->bg,
293                                0, 0, 1, 1, 0,
294                                RrDepth(ob_rr_inst), InputOutput,
295                                RrVisual(ob_rr_inst), 0, NULL);
296     XMapWindow(ob_display, self->icon);
297
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 static void pager_popup_draw_icon(gint px, gint py, gint w, gint h,
329                                   gpointer data)
330 {
331     ObPagerPopup *self = data;
332     gint x, y;
333     guint rown, n;
334     guint horz_inc;
335     guint vert_inc;
336     guint r, c;
337     gint eachw, eachh;
338
339     eachw = (w - ob_rr_theme->fbwidth -
340              (screen_desktop_layout.columns * ob_rr_theme->fbwidth))
341         / screen_desktop_layout.columns;
342     eachh = (h - ob_rr_theme->fbwidth -
343              (screen_desktop_layout.rows * ob_rr_theme->fbwidth))
344         / screen_desktop_layout.rows;
345     /* make them squares */
346     eachw = eachh = MIN(eachw, eachh);
347
348     /* center */
349     px += (w - (screen_desktop_layout.columns * (eachw + ob_rr_theme->fbwidth) +
350                 ob_rr_theme->fbwidth)) / 2;
351     py += (h - (screen_desktop_layout.rows * (eachh + ob_rr_theme->fbwidth) +
352                 ob_rr_theme->fbwidth)) / 2;
353
354     if (eachw <= 0 || eachh <= 0)
355         return;
356
357     switch (screen_desktop_layout.orientation) {
358     case OB_ORIENTATION_HORZ:
359         switch (screen_desktop_layout.start_corner) {
360         case OB_CORNER_TOPLEFT:
361             n = 0;
362             horz_inc = 1;
363             vert_inc = screen_desktop_layout.columns;
364             break;
365         case OB_CORNER_TOPRIGHT:
366             n = screen_desktop_layout.columns - 1;
367             horz_inc = -1;
368             vert_inc = screen_desktop_layout.columns;
369             break;
370         case OB_CORNER_BOTTOMRIGHT:
371             n = screen_desktop_layout.rows * screen_desktop_layout.columns - 1;
372             horz_inc = -1;
373             vert_inc = -screen_desktop_layout.columns;
374             break;
375         case OB_CORNER_BOTTOMLEFT:
376             n = (screen_desktop_layout.rows - 1)
377                 * screen_desktop_layout.columns;
378             horz_inc = 1;
379             vert_inc = -screen_desktop_layout.columns;
380             break;
381         default:
382             g_assert_not_reached();
383         }
384         break;
385     case OB_ORIENTATION_VERT:
386         switch (screen_desktop_layout.start_corner) {
387         case OB_CORNER_TOPLEFT:
388             n = 0;
389             horz_inc = screen_desktop_layout.rows;
390             vert_inc = 1;
391             break;
392         case OB_CORNER_TOPRIGHT:
393             n = screen_desktop_layout.rows
394                 * (screen_desktop_layout.columns - 1);
395             horz_inc = -screen_desktop_layout.rows;
396             vert_inc = 1;
397             break;
398         case OB_CORNER_BOTTOMRIGHT:
399             n = screen_desktop_layout.rows * screen_desktop_layout.columns - 1;
400             horz_inc = -screen_desktop_layout.rows;
401             vert_inc = -1;
402             break;
403         case OB_CORNER_BOTTOMLEFT:
404             n = screen_desktop_layout.rows - 1;
405             horz_inc = screen_desktop_layout.rows;
406             vert_inc = -1;
407             break;
408         default:
409             g_assert_not_reached();
410         }
411         break;
412     default:
413         g_assert_not_reached();
414     }
415
416     rown = n;
417     for (r = 0, y = 0; r < screen_desktop_layout.rows;
418          ++r, y += eachh + ob_rr_theme->fbwidth)
419     {
420         for (c = 0, x = 0; c < screen_desktop_layout.columns;
421              ++c, x += eachw + ob_rr_theme->fbwidth)
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 = RrColorPixel(ob_rr_theme->frame_b_color);
492             self->wins[i] = XCreateWindow(ob_display, self->popup->bg,
493                                           0, 0, 1, 1, ob_rr_theme->fbwidth,
494                                           RrDepth(ob_rr_inst), InputOutput,
495                                           RrVisual(ob_rr_inst), CWBorderPixel,
496                                           &attr);
497             XMapWindow(ob_display, self->wins[i]);
498         }
499
500     self->desks = screen_num_desktops;
501     self->curdesk = desk;
502
503     popup_delay_show(self->popup, usec, text);
504 }