Remove leftover config_four_corners from config.c.
[mikachu/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 ObPopup *popup_new(void)
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->obwidth);
55     XSetWindowBorder(ob_display, self->bg,
56                      RrColorPixel(ob_rr_theme->osd_border_color));
57
58     XMapWindow(ob_display, self->text);
59
60     stacking_add(INTERNAL_AS_WINDOW(self));
61     g_hash_table_insert(window_map, &self->bg, self);
62     return self;
63 }
64
65 void popup_free(ObPopup *self)
66 {
67     if (self) {
68         XDestroyWindow(ob_display, self->bg);
69         XDestroyWindow(ob_display, self->text);
70         RrAppearanceFree(self->a_bg);
71         RrAppearanceFree(self->a_text);
72         g_hash_table_remove(window_map, &self->bg);
73         stacking_remove(self);
74         g_free(self);
75     }
76 }
77
78 void popup_position(ObPopup *self, gint gravity, gint x, gint y)
79 {
80     self->gravity = gravity;
81     self->x = x;
82     self->y = y;
83 }
84
85 void popup_text_width(ObPopup *self, gint w)
86 {
87     self->textw = w;
88 }
89
90 void popup_min_width(ObPopup *self, gint minw)
91 {
92     self->minw = minw;
93 }
94
95 void popup_max_width(ObPopup *self, gint maxw)
96 {
97     self->maxw = maxw;
98 }
99
100 void popup_height(ObPopup *self, gint h)
101 {
102     gint texth;
103
104     /* don't let the height be smaller than the text */
105     texth = RrMinHeight(self->a_text) + ob_rr_theme->paddingy * 2;
106     self->h = MAX(h, texth);
107 }
108
109 void popup_text_width_to_string(ObPopup *self, gchar *text)
110 {
111     if (text[0] != '\0') {
112         self->a_text->texture[0].data.text.string = text;
113         self->textw = RrMinWidth(self->a_text);
114     } else
115         self->textw = 0;
116 }
117
118 void popup_height_to_string(ObPopup *self, gchar *text)
119 {
120     self->h = RrMinHeight(self->a_text) + ob_rr_theme->paddingy * 2;
121 }
122
123 void popup_text_width_to_strings(ObPopup *self, gchar **strings, gint num)
124 {
125     gint i, maxw;
126
127     maxw = 0;
128     for (i = 0; i < num; ++i) {
129         popup_text_width_to_string(self, strings[i]);
130         maxw = MAX(maxw, self->textw);
131     }
132     self->textw = maxw;
133 }
134
135 void popup_set_text_align(ObPopup *self, RrJustify align)
136 {
137     self->a_text->texture[0].data.text.justify = align;
138 }
139
140 static gboolean popup_show_timeout(gpointer data)
141 {
142     ObPopup *self = data;
143
144     XMapWindow(ob_display, self->bg);
145     stacking_raise(INTERNAL_AS_WINDOW(self));
146     self->mapped = TRUE;
147     self->delay_mapped = FALSE;
148
149     return FALSE; /* don't repeat */
150 }
151
152 void popup_delay_show(ObPopup *self, gulong usec, gchar *text)
153 {
154     gint l, t, r, b;
155     gint x, y, w, h;
156     guint m;
157     gint emptyx, emptyy; /* empty space between elements */
158     gint textx, texty, textw, texth;
159     gint iconx, icony, iconw, iconh;
160     Rect *area, mon;
161
162     /* when there is no icon and the text is not parent relative, then
163        fill the whole dialog with the text appearance, don't use the bg at all
164     */
165     if (self->hasicon || self->a_text->surface.grad == RR_SURFACE_PARENTREL)
166         RrMargins(self->a_bg, &l, &t, &r, &b);
167     else
168         l = t = r = b = 0;
169
170     /* set up the textures */
171     self->a_text->texture[0].data.text.string = text;
172
173     /* measure the text out */
174     if (text[0] != '\0') {
175         RrMinSize(self->a_text, &textw, &texth);
176     } else {
177         textw = 0;
178         texth = RrMinHeight(self->a_text);
179     }
180
181     /* get the height, which is also used for the icon width */
182     emptyy = t + b + ob_rr_theme->paddingy * 2;
183     if (self->h)
184         texth = self->h - emptyy;
185     h = texth * self->iconhm + emptyy;
186
187     if (self->textw)
188         textw = self->textw;
189
190     iconx = textx = l + ob_rr_theme->paddingx;
191
192     emptyx = l + r + ob_rr_theme->paddingx * 2;
193     if (self->hasicon) {
194         iconw = texth * self->iconwm;
195         iconh = texth * self->iconhm;
196         textx += iconw + ob_rr_theme->paddingx;
197         if (textw)
198             emptyx += ob_rr_theme->paddingx; /* between the icon and text */
199     } else
200         iconw = 0;
201
202     texty = (h - texth - emptyy) / 2 + t + ob_rr_theme->paddingy;
203     icony = (h - iconh - emptyy) / 2 + t + ob_rr_theme->paddingy;
204
205     /* when there is no icon, then fill the whole dialog with the text
206        appearance
207     */
208     if (!self->hasicon)
209     {
210         textx = texty = 0;
211         texth += emptyy;
212         textw += emptyx;
213         emptyx = emptyy = 0;
214     }
215
216     w = textw + emptyx + iconw;
217     /* cap it at maxw/minw */
218     if (self->maxw) w = MIN(w, self->maxw);
219     if (self->minw) w = MAX(w, self->minw);
220     textw = w - emptyx - iconw;
221
222     /* sanity checks to avoid crashes! */
223     if (w < 1) w = 1;
224     if (h < 1) h = 1;
225     if (texth < 1) texth = 1;
226
227     /* set up the x coord */
228     x = self->x;
229     switch (self->gravity) {
230     case NorthGravity: case CenterGravity: case SouthGravity:
231         x -= w / 2;
232         break;
233     case NorthEastGravity: case EastGravity: case SouthEastGravity:
234         x -= w;
235         break;
236     }
237
238     /* set up the y coord */
239     y = self->y;
240     switch (self->gravity) {
241     case WestGravity: case CenterGravity: case EastGravity:
242         y -= h / 2;
243         break;
244     case SouthWestGravity: case SouthGravity: case SouthEastGravity:
245         y -= h;
246         break;
247     }
248
249     /* Find the monitor which contains the biggest part of the popup.
250      * If the popup is completely off screen, limit it to the intersection
251      * of all monitors and then try again. If it's still off screen, put it
252      * on monitor 0. */
253     RECT_SET(mon, x, y, w, h);
254     m = screen_find_monitor(&mon);
255     area = screen_physical_area_monitor(m);
256
257     x=MAX(MIN(x, area->x+area->width-w),area->x);
258     y=MAX(MIN(y, area->y+area->height-h),area->y);
259
260     if (m == screen_num_monitors) {
261         RECT_SET(mon, x, y, w, h);
262         m = screen_find_monitor(&mon);
263         if (m == screen_num_monitors)
264             m = 0;
265         area = screen_physical_area_monitor(m);
266
267         x=MAX(MIN(x, area->x+area->width-w),area->x);
268         y=MAX(MIN(y, area->y+area->height-h),area->y);
269     }
270
271     /* set the windows/appearances up */
272     XMoveResizeWindow(ob_display, self->bg, x, y, w, h);
273     /* when there is no icon and the text is not parent relative, then
274        fill the whole dialog with the text appearance, don't use the bg at all
275     */
276     if (self->hasicon || self->a_text->surface.grad == RR_SURFACE_PARENTREL)
277         RrPaint(self->a_bg, self->bg, w, h);
278
279     if (textw) {
280         self->a_text->surface.parent = self->a_bg;
281         self->a_text->surface.parentx = textx;
282         self->a_text->surface.parenty = texty;
283         XMoveResizeWindow(ob_display, self->text, textx, texty, textw, texth);
284         RrPaint(self->a_text, self->text, textw, texth);
285     }
286
287     if (self->hasicon)
288         self->draw_icon(iconx, icony, iconw, iconh, self->draw_icon_data);
289
290     /* do the actual showing */
291     if (!self->mapped) {
292         if (usec) {
293             /* don't kill previous show timers */
294             if (!self->delay_mapped) {
295                 ob_main_loop_timeout_add(ob_main_loop, usec,
296                                          popup_show_timeout, self,
297                                          g_direct_equal, NULL);
298                 self->delay_mapped = TRUE;
299             }
300         } else {
301             popup_show_timeout(self);
302         }
303     }
304
305     g_free(area);
306 }
307
308 void popup_hide(ObPopup *self)
309 {
310     if (self->mapped) {
311         gulong ignore_start;
312
313         /* kill enter events cause by this unmapping */
314         ignore_start = event_start_ignore_all_enters();
315
316         XUnmapWindow(ob_display, self->bg);
317         self->mapped = FALSE;
318
319         event_end_ignore_all_enters(ignore_start);
320     } else if (self->delay_mapped) {
321         ob_main_loop_timeout_remove(ob_main_loop, popup_show_timeout);
322         self->delay_mapped = FALSE;
323     }
324 }
325
326 static void icon_popup_draw_icon(gint x, gint y, gint w, gint h, gpointer data)
327 {
328     ObIconPopup *self = data;
329
330     self->a_icon->surface.parent = self->popup->a_bg;
331     self->a_icon->surface.parentx = x;
332     self->a_icon->surface.parenty = y;
333     XMoveResizeWindow(ob_display, self->icon, x, y, w, h);
334     RrPaint(self->a_icon, self->icon, w, h);
335 }
336
337 ObIconPopup *icon_popup_new(void)
338 {
339     ObIconPopup *self;
340
341     self = g_new0(ObIconPopup, 1);
342     self->popup = popup_new();
343     self->a_icon = RrAppearanceCopy(ob_rr_theme->a_clear_tex);
344     self->icon = XCreateWindow(ob_display, self->popup->bg,
345                                0, 0, 1, 1, 0,
346                                RrDepth(ob_rr_inst), InputOutput,
347                                RrVisual(ob_rr_inst), 0, NULL);
348     XMapWindow(ob_display, self->icon);
349
350     self->popup->hasicon = TRUE;
351     self->popup->draw_icon = icon_popup_draw_icon;
352     self->popup->draw_icon_data = self;
353
354     return self;
355 }
356
357 void icon_popup_free(ObIconPopup *self)
358 {
359     if (self) {
360         XDestroyWindow(ob_display, self->icon);
361         RrAppearanceFree(self->a_icon);
362         popup_free(self->popup);
363         g_free(self);
364     }
365 }
366
367 void icon_popup_delay_show(ObIconPopup *self, gulong usec,
368                            gchar *text, const ObClientIcon *icon)
369 {
370     if (icon) {
371         self->a_icon->texture[0].type = RR_TEXTURE_RGBA;
372         self->a_icon->texture[0].data.rgba.width = icon->width;
373         self->a_icon->texture[0].data.rgba.height = icon->height;
374         self->a_icon->texture[0].data.rgba.alpha = 0xff;
375         self->a_icon->texture[0].data.rgba.data = icon->data;
376     } else
377         self->a_icon->texture[0].type = RR_TEXTURE_NONE;
378
379     popup_delay_show(self->popup, usec, text);
380 }
381
382 void icon_popup_icon_size_multiplier(ObIconPopup *self, guint wm, guint hm)
383 {
384     /* cap them at 1 */
385     self->popup->iconwm = MAX(1, wm);
386     self->popup->iconhm = MAX(1, hm);
387 }
388
389 static void pager_popup_draw_icon(gint px, gint py, gint w, gint h,
390                                   gpointer data)
391 {
392     ObPagerPopup *self = data;
393     gint x, y;
394     guint rown, n;
395     guint horz_inc;
396     guint vert_inc;
397     guint r, c;
398     gint eachw, eachh;
399     const guint cols = screen_desktop_layout.columns;
400     const guint rows = screen_desktop_layout.rows;
401     const gint linewidth = ob_rr_theme->obwidth;
402
403     eachw = (w - ((cols + 1) * linewidth)) / cols;
404     eachh = (h - ((rows + 1) * linewidth)) / rows;
405     /* make them squares */
406     eachw = eachh = MIN(eachw, eachh);
407
408     /* center */
409     px += (w - (cols * (eachw + linewidth) + linewidth)) / 2;
410     py += (h - (rows * (eachh + linewidth) + linewidth)) / 2;
411
412     if (eachw <= 0 || eachh <= 0)
413         return;
414
415     switch (screen_desktop_layout.orientation) {
416     case OB_ORIENTATION_HORZ:
417         switch (screen_desktop_layout.start_corner) {
418         case OB_CORNER_TOPLEFT:
419             n = 0;
420             horz_inc = 1;
421             vert_inc = cols;
422             break;
423         case OB_CORNER_TOPRIGHT:
424             n = cols - 1;
425             horz_inc = -1;
426             vert_inc = cols;
427             break;
428         case OB_CORNER_BOTTOMRIGHT:
429             n = rows * cols - 1;
430             horz_inc = -1;
431             vert_inc = -screen_desktop_layout.columns;
432             break;
433         case OB_CORNER_BOTTOMLEFT:
434             n = (rows - 1) * cols;
435             horz_inc = 1;
436             vert_inc = -cols;
437             break;
438         default:
439             g_assert_not_reached();
440         }
441         break;
442     case OB_ORIENTATION_VERT:
443         switch (screen_desktop_layout.start_corner) {
444         case OB_CORNER_TOPLEFT:
445             n = 0;
446             horz_inc = rows;
447             vert_inc = 1;
448             break;
449         case OB_CORNER_TOPRIGHT:
450             n = rows * (cols - 1);
451             horz_inc = -rows;
452             vert_inc = 1;
453             break;
454         case OB_CORNER_BOTTOMRIGHT:
455             n = rows * cols - 1;
456             horz_inc = -rows;
457             vert_inc = -1;
458             break;
459         case OB_CORNER_BOTTOMLEFT:
460             n = rows - 1;
461             horz_inc = rows;
462             vert_inc = -1;
463             break;
464         default:
465             g_assert_not_reached();
466         }
467         break;
468     default:
469         g_assert_not_reached();
470     }
471
472     rown = n;
473     for (r = 0, y = 0; r < rows; ++r, y += eachh + linewidth)
474     {
475         for (c = 0, x = 0; c < cols; ++c, x += eachw + linewidth)
476         {
477             RrAppearance *a;
478
479             if (n < self->desks) {
480                 a = (n == self->curdesk ? self->hilight : self->unhilight);
481
482                 a->surface.parent = self->popup->a_bg;
483                 a->surface.parentx = x + px;
484                 a->surface.parenty = y + py;
485                 XMoveResizeWindow(ob_display, self->wins[n],
486                                   x + px, y + py, eachw, eachh);
487                 RrPaint(a, self->wins[n], eachw, eachh);
488             }
489             n += horz_inc;
490         }
491         n = rown += vert_inc;
492     }
493 }
494
495 ObPagerPopup *pager_popup_new(void)
496 {
497     ObPagerPopup *self;
498
499     self = g_new(ObPagerPopup, 1);
500     self->popup = popup_new();
501
502     self->desks = 0;
503     self->wins = g_new(Window, self->desks);
504     self->hilight = RrAppearanceCopy(ob_rr_theme->osd_hilite_fg);
505     self->unhilight = RrAppearanceCopy(ob_rr_theme->osd_unhilite_fg);
506
507     self->popup->hasicon = TRUE;
508     self->popup->draw_icon = pager_popup_draw_icon;
509     self->popup->draw_icon_data = self;
510
511     return self;
512 }
513
514 void pager_popup_free(ObPagerPopup *self)
515 {
516     if (self) {
517         guint i;
518
519         for (i = 0; i < self->desks; ++i)
520             XDestroyWindow(ob_display, self->wins[i]);
521         g_free(self->wins);
522         RrAppearanceFree(self->hilight);
523         RrAppearanceFree(self->unhilight);
524         popup_free(self->popup);
525         g_free(self);
526     }
527 }
528
529 void pager_popup_delay_show(ObPagerPopup *self, gulong usec,
530                             gchar *text, guint desk)
531 {
532     guint i;
533
534     if (screen_num_desktops < self->desks)
535         for (i = screen_num_desktops; i < self->desks; ++i)
536             XDestroyWindow(ob_display, self->wins[i]);
537
538     if (screen_num_desktops != self->desks)
539         self->wins = g_renew(Window, self->wins, screen_num_desktops);
540
541     if (screen_num_desktops > self->desks)
542         for (i = self->desks; i < screen_num_desktops; ++i) {
543             XSetWindowAttributes attr;
544
545             attr.border_pixel =
546                 RrColorPixel(ob_rr_theme->osd_border_color);
547             self->wins[i] = XCreateWindow(ob_display, self->popup->bg,
548                                           0, 0, 1, 1, ob_rr_theme->obwidth,
549                                           RrDepth(ob_rr_inst), InputOutput,
550                                           RrVisual(ob_rr_inst), CWBorderPixel,
551                                           &attr);
552             XMapWindow(ob_display, self->wins[i]);
553         }
554
555     self->desks = screen_num_desktops;
556     self->curdesk = desk;
557
558     popup_delay_show(self->popup, usec, text);
559 }
560
561 void pager_popup_icon_size_multiplier(ObPagerPopup *self, guint wm, guint hm)
562 {
563     /* cap them at 1 */
564     self->popup->iconwm = MAX(1, wm);
565     self->popup->iconhm = MAX(1, hm);
566 }