limit the size popup to the screen, maybe this doesnt work with xinerama though?
[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) 2003        Ben Jansens
5
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 2 of the License, or
9    (at your option) any later version.
10
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15
16    See the COPYING file for a copy of the GNU General Public License.
17 */
18
19 #include "popup.h"
20
21 #include "openbox.h"
22 #include "frame.h"
23 #include "client.h"
24 #include "stacking.h"
25 #include "screen.h"
26 #include "render/render.h"
27 #include "render/theme.h"
28
29 ObPopup *popup_new(gboolean hasicon)
30 {
31     XSetWindowAttributes attrib;
32     ObPopup *self = g_new0(ObPopup, 1);
33
34     self->obwin.type = Window_Internal;
35     self->hasicon = hasicon;
36     self->gravity = NorthWestGravity;
37     self->x = self->y = self->w = self->h = 0;
38     self->a_bg = RrAppearanceCopy(ob_rr_theme->app_hilite_bg);
39     self->a_text = RrAppearanceCopy(ob_rr_theme->app_hilite_label);
40
41     attrib.override_redirect = True;
42     self->bg = XCreateWindow(ob_display, RootWindow(ob_display, ob_screen),
43                              0, 0, 1, 1, 0, RrDepth(ob_rr_inst),
44                              InputOutput, RrVisual(ob_rr_inst),
45                              CWOverrideRedirect, &attrib);
46     
47     self->text = XCreateWindow(ob_display, self->bg,
48                                0, 0, 1, 1, 0, RrDepth(ob_rr_inst),
49                                InputOutput, RrVisual(ob_rr_inst), 0, NULL);
50
51     XMapWindow(ob_display, self->text);
52
53     stacking_add(INTERNAL_AS_WINDOW(self));
54     return self;
55 }
56
57 void popup_free(ObPopup *self)
58 {
59     if (self) {
60         XDestroyWindow(ob_display, self->bg);
61         XDestroyWindow(ob_display, self->text);
62         RrAppearanceFree(self->a_bg);
63         RrAppearanceFree(self->a_text);
64         stacking_remove(self);
65         g_free(self);
66     }
67 }
68
69 void popup_position(ObPopup *self, gint gravity, gint x, gint y)
70 {
71     self->gravity = gravity;
72     self->x = x;
73     self->y = y;
74 }
75
76 void popup_size(ObPopup *self, gint w, gint h)
77 {
78     self->w = w;
79     self->h = h;
80 }
81
82 void popup_size_to_string(ObPopup *self, gchar *text)
83 {
84     gint textw, texth;
85     gint iconw;
86
87     self->a_text->texture[0].data.text.string = text;
88     RrMinsize(self->a_text, &textw, &texth);
89     /*XXX textw += ob_rr_theme->bevel * 2;*/
90     texth += ob_rr_theme->padding * 2;
91
92     self->h = texth + ob_rr_theme->padding * 2;
93     iconw = (self->hasicon ? texth : 0);
94     self->w = textw + iconw + ob_rr_theme->padding * (self->hasicon ? 3 : 2);
95 }
96
97 void popup_set_text_align(ObPopup *self, RrJustify align)
98 {
99     self->a_text->texture[0].data.text.justify = align;
100 }
101
102 void popup_show(ObPopup *self, gchar *text)
103 {
104     gint l, t, r, b;
105     gint x, y, w, h;
106     gint textw, texth;
107     gint iconw;
108     Rect *area; /* won't go outside this */
109
110     area = screen_physical_area_monitor(0); /* XXX i'm guessing this
111                                                is wrong for xinerama? */
112
113     RrMargins(self->a_bg, &l, &t, &r, &b);
114
115     XSetWindowBorderWidth(ob_display, self->bg, ob_rr_theme->bwidth);
116     XSetWindowBorder(ob_display, self->bg, ob_rr_theme->b_color->pixel);
117
118     /* set up the textures */
119     self->a_text->texture[0].data.text.string = text;
120
121     /* measure the shit out */
122     RrMinsize(self->a_text, &textw, &texth);
123     /*XXX textw += ob_rr_theme->padding * 2;*/
124     texth += ob_rr_theme->padding * 2;
125
126     /* set the sizes up and reget the text sizes from the calculated
127        outer sizes */
128     if (self->h) {
129         h = self->h;
130         texth = h - (t+b + ob_rr_theme->padding * 2);
131     } else
132         h = t+b + texth + ob_rr_theme->padding * 2;
133     iconw = (self->hasicon ? texth : 0);
134     if (self->w) {
135         w = self->w;
136         textw = w - (l+r + iconw + ob_rr_theme->padding *
137                      (self->hasicon ? 3 : 2));
138     } else
139         w = l+r + textw + iconw + ob_rr_theme->padding *
140             (self->hasicon ? 3 : 2);
141     /* sanity checks to avoid crashes! */
142     if (w < 1) w = 1;
143     if (h < 1) h = 1;
144     if (textw < 1) textw = 1;
145     if (texth < 1) texth = 1;
146
147     /* set up the x coord */
148     x = self->x;
149     switch (self->gravity) {
150     case NorthGravity:
151     case CenterGravity:
152     case SouthGravity:
153         x -= w / 2;
154         break;
155     case NorthEastGravity:
156     case EastGravity:
157     case SouthEastGravity:
158         x -= w;
159         break;
160     }
161
162     /* set up the y coord */
163     y = self->y;
164     switch (self->gravity) {
165     case WestGravity:
166     case CenterGravity:
167     case EastGravity:
168         y -= h / 2;
169         break;
170     case SouthWestGravity:
171     case SouthGravity:
172     case SouthEastGravity:
173         y -= h;
174         break;
175     }
176
177     x=MAX(MIN(x, area->width-w),0);
178     y=MAX(MIN(y, area->height-h),0);
179
180     /* set the windows/appearances up */
181     XMoveResizeWindow(ob_display, self->bg, x, y, w, h);
182
183     self->a_text->surface.parent = self->a_bg;
184     self->a_text->surface.parentx = l + iconw +
185         ob_rr_theme->padding * (self->hasicon ? 2 : 1);
186     self->a_text->surface.parenty = t + ob_rr_theme->padding;
187     XMoveResizeWindow(ob_display, self->text,
188                       l + iconw + ob_rr_theme->padding *
189                       (self->hasicon ? 2 : 1),
190                       t + ob_rr_theme->padding, textw, texth);
191
192     RrPaint(self->a_bg, self->bg, w, h);
193     RrPaint(self->a_text, self->text, textw, texth);
194
195     if (self->hasicon) {
196         if (iconw < 1) iconw = 1; /* sanity check for crashes */
197         if (self->draw_icon)
198             self->draw_icon(l + ob_rr_theme->padding, t + ob_rr_theme->padding,
199                             iconw, texth, self->draw_icon_data);
200     }
201
202     if (!self->mapped) {
203         XMapWindow(ob_display, self->bg);
204         stacking_raise(INTERNAL_AS_WINDOW(self));
205         self->mapped = TRUE;
206     }
207 }
208
209 void popup_hide(ObPopup *self)
210 {
211     if (self->mapped) {
212         XUnmapWindow(ob_display, self->bg);
213         self->mapped = FALSE;
214     }
215 }
216
217 static void icon_popup_draw_icon(gint x, gint y, gint w, gint h, gpointer data)
218 {
219     ObIconPopup *self = data;
220
221     self->a_icon->surface.parent = self->popup->a_bg;
222     self->a_icon->surface.parentx = x;
223     self->a_icon->surface.parenty = y;
224     XMoveResizeWindow(ob_display, self->icon, x, y, w, h);
225     RrPaint(self->a_icon, self->icon, w, h);
226 }
227
228 ObIconPopup *icon_popup_new()
229 {
230     ObIconPopup *self;
231
232     self = g_new0(ObIconPopup, 1);
233     self->popup = popup_new(TRUE);
234     self->a_icon = RrAppearanceCopy(ob_rr_theme->a_clear_tex);
235     self->icon = XCreateWindow(ob_display, self->popup->bg,
236                                0, 0, 1, 1, 0,
237                                RrDepth(ob_rr_inst), InputOutput,
238                                RrVisual(ob_rr_inst), 0, NULL);
239     XMapWindow(ob_display, self->icon);
240
241     self->popup->draw_icon = icon_popup_draw_icon;
242     self->popup->draw_icon_data = self;
243
244     return self;
245 }
246
247 void icon_popup_free(ObIconPopup *self)
248 {
249     if (self) {
250         XDestroyWindow(ob_display, self->icon);
251         RrAppearanceFree(self->a_icon);
252         popup_free(self->popup);
253         g_free(self);
254     }
255 }
256
257 void icon_popup_show(ObIconPopup *self,
258                      gchar *text, const ObClientIcon *icon)
259 {
260     if (icon) {
261         self->a_icon->texture[0].type = RR_TEXTURE_RGBA;
262         self->a_icon->texture[0].data.rgba.width = icon->width;
263         self->a_icon->texture[0].data.rgba.height = icon->height;
264         self->a_icon->texture[0].data.rgba.data = icon->data;
265     } else
266         self->a_icon->texture[0].type = RR_TEXTURE_NONE;
267
268     popup_show(self->popup, text);
269 }
270
271 static void pager_popup_draw_icon(gint px, gint py, gint w, gint h,
272                                   gpointer data)
273 {
274     ObPagerPopup *self = data;
275     gint x, y;
276     guint i;
277     guint rown, n;
278     guint horz_inc;
279     guint vert_inc;
280     guint r, c;
281     gint eachw, eachh;
282
283     eachw = (w - ob_rr_theme->bwidth -
284              (screen_desktop_layout.columns * ob_rr_theme->bwidth))
285         / screen_desktop_layout.columns;
286     eachh = (h - ob_rr_theme->bwidth -
287              (screen_desktop_layout.rows * ob_rr_theme->bwidth))
288         / screen_desktop_layout.rows;
289     /* make them squares */
290     eachw = eachh = MIN(eachw, eachh);
291
292     /* center */
293     px += (w - (screen_desktop_layout.columns * (eachw + ob_rr_theme->bwidth) +
294                 ob_rr_theme->bwidth)) / 2;
295     py += (h - (screen_desktop_layout.rows * (eachh + ob_rr_theme->bwidth) +
296                 ob_rr_theme->bwidth)) / 2;
297
298     if (eachw <= 0 || eachh <= 0)
299         return;
300
301     switch (screen_desktop_layout.start_corner) {
302     case OB_CORNER_TOPLEFT:
303         n = 0;
304         switch (screen_desktop_layout.orientation) {
305         case OB_ORIENTATION_HORZ:
306             horz_inc = 1;
307             vert_inc = screen_desktop_layout.columns;
308             break;
309         case OB_ORIENTATION_VERT:
310             horz_inc = screen_desktop_layout.rows;
311             vert_inc = 1;
312             break;
313         }
314         break;
315     case OB_CORNER_TOPRIGHT:
316         n = screen_desktop_layout.columns;
317         switch (screen_desktop_layout.orientation) {
318         case OB_ORIENTATION_HORZ:
319             horz_inc = -1;
320             vert_inc = screen_desktop_layout.columns;
321             break;
322         case OB_ORIENTATION_VERT:
323             horz_inc = -screen_desktop_layout.rows;
324             vert_inc = 1;
325             break;
326         }
327         break;
328     case OB_CORNER_BOTTOMLEFT:
329         n = screen_desktop_layout.rows;
330         switch (screen_desktop_layout.orientation) {
331         case OB_ORIENTATION_HORZ:
332             horz_inc = 1;
333             vert_inc = -screen_desktop_layout.columns;
334             break;
335         case OB_ORIENTATION_VERT:
336             horz_inc = screen_desktop_layout.rows;
337             vert_inc = -1;
338             break;
339         }
340         break;
341     case OB_CORNER_BOTTOMRIGHT:
342         n = MAX(self->desks,
343                 screen_desktop_layout.rows * screen_desktop_layout.columns);
344         switch (screen_desktop_layout.orientation) {
345         case OB_ORIENTATION_HORZ:
346             horz_inc = -1;
347             vert_inc = -screen_desktop_layout.columns;
348             break;
349         case OB_ORIENTATION_VERT:
350             horz_inc = -screen_desktop_layout.rows;
351             vert_inc = -1;
352             break;
353         }
354         break;
355     }
356
357     rown = n;
358     i = 0;
359     for (r = 0, y = 0; r < screen_desktop_layout.rows;
360          ++r, y += eachh + ob_rr_theme->bwidth)
361     {
362         for (c = 0, x = 0; c < screen_desktop_layout.columns;
363              ++c, x += eachw + ob_rr_theme->bwidth)
364         {
365             RrAppearance *a;
366
367             if (i >= self->desks)
368                 break;
369
370             a = (n == self->curdesk ? self->hilight : self->unhilight);
371
372             a->surface.parent = self->popup->a_bg;
373             a->surface.parentx = x + px;
374             a->surface.parenty = y + py;
375             XMoveResizeWindow(ob_display, self->wins[i],
376                               x + px, y + py, eachw, eachh);
377             RrPaint(a, self->wins[i], eachw, eachh);
378
379             n += horz_inc;
380
381             ++i;
382         }
383         n = rown += vert_inc;
384     }
385 }
386
387 ObPagerPopup *pager_popup_new()
388 {
389     ObPagerPopup *self;
390
391     self = g_new(ObPagerPopup, 1);
392     self->popup = popup_new(TRUE);
393
394     self->desks = 0;
395     self->wins = g_new(Window, self->desks);
396     self->hilight = RrAppearanceCopy(ob_rr_theme->app_hilite_fg);
397     self->unhilight = RrAppearanceCopy(ob_rr_theme->app_unhilite_fg);
398
399     self->popup->draw_icon = pager_popup_draw_icon;
400     self->popup->draw_icon_data = self;
401
402     return self;
403 }
404
405 void pager_popup_free(ObPagerPopup *self)
406 {
407     if (self) {
408         guint i;
409
410         for (i = 0; i < self->desks; ++i)
411             XDestroyWindow(ob_display, self->wins[i]);
412         g_free(self->wins);
413         RrAppearanceFree(self->hilight);
414         RrAppearanceFree(self->unhilight);
415         popup_free(self->popup);
416         g_free(self);
417     }
418 }
419
420 void pager_popup_show(ObPagerPopup *self, gchar *text, guint desk)
421 {
422     guint i;
423
424     if (screen_num_desktops < self->desks)
425         for (i = screen_num_desktops; i < self->desks; ++i)
426             XDestroyWindow(ob_display, self->wins[i]);
427
428     if (screen_num_desktops != self->desks)
429         self->wins = g_renew(Window, self->wins, screen_num_desktops);
430
431     if (screen_num_desktops > self->desks)
432         for (i = self->desks; i < screen_num_desktops; ++i) {
433             XSetWindowAttributes attr;
434
435             attr.border_pixel = RrColorPixel(ob_rr_theme->b_color);
436             self->wins[i] = XCreateWindow(ob_display, self->popup->bg,
437                                           0, 0, 1, 1, ob_rr_theme->bwidth,
438                                           RrDepth(ob_rr_inst), InputOutput,
439                                           RrVisual(ob_rr_inst), CWBorderPixel,
440                                           &attr);
441             XMapWindow(ob_display, self->wins[i]);
442         }
443
444     self->desks = screen_num_desktops;
445     self->curdesk = desk;
446
447     popup_show(self->popup, text);
448 }