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