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