80 cols
[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 "window.h"
7 #include "stacking.h"
8 #include "render/render.h"
9 #include "render/theme.h"
10
11 struct _ObPopup
12 {
13     ObWindow obwin;
14     Window bg;
15
16     Window icon;
17     Window text;
18
19     gboolean hasicon;
20     RrAppearance *a_bg;
21     RrAppearance *a_icon;
22     RrAppearance *a_text;
23     gint gravity;
24     gint x;
25     gint y;
26     gint w;
27     gint h;
28     gboolean mapped;
29 };
30
31 Popup *popup_new(gboolean hasicon)
32 {
33     XSetWindowAttributes attrib;
34     Popup *self = g_new(Popup, 1);
35
36     self->obwin.type = Window_Internal;
37     self->hasicon = hasicon;
38     self->a_text = NULL;
39     self->gravity = NorthWestGravity;
40     self->x = self->y = self->w = self->h = 0;
41     self->mapped = FALSE;
42     self->a_bg = self->a_icon = self->a_text = NULL;
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     if (self->hasicon)
55         self->icon = XCreateWindow(ob_display, self->bg,
56                                    0, 0, 1, 1, 0,
57                                    RrDepth(ob_rr_inst), InputOutput,
58                                    RrVisual(ob_rr_inst), 0, NULL);
59
60     XMapWindow(ob_display, self->text);
61     XMapWindow(ob_display, self->icon);
62
63     stacking_add(INTERNAL_AS_WINDOW(self));
64     return self;
65 }
66
67 void popup_free(Popup *self)
68 {
69     if (self) {
70         XDestroyWindow(ob_display, self->bg);
71         XDestroyWindow(ob_display, self->text);
72         XDestroyWindow(ob_display, self->icon);
73         RrAppearanceFree(self->a_bg);
74         RrAppearanceFree(self->a_icon);
75         RrAppearanceFree(self->a_text);
76         stacking_remove(self);
77         g_free(self);
78     }
79 }
80
81 void popup_position(Popup *self, gint gravity, gint x, gint y)
82 {
83     self->gravity = gravity;
84     self->x = x;
85     self->y = y;
86 }
87
88 void popup_size(Popup *self, gint w, gint h)
89 {
90     self->w = w;
91     self->h = h;
92 }
93
94 void popup_size_to_string(Popup *self, gchar *text)
95 {
96     gint textw, texth;
97     gint iconw;
98
99     if (!self->a_text)
100         self->a_text = RrAppearanceCopy(ob_rr_theme->app_hilite_label);
101
102     self->a_text->texture[0].data.text.string = text;
103     RrMinsize(self->a_text, &textw, &texth);
104     /*XXX textw += ob_rr_theme->bevel * 2;*/
105     texth += ob_rr_theme->padding * 2;
106
107     self->h = texth + ob_rr_theme->padding * 2;
108     iconw = (self->hasicon ? texth : 0);
109     self->w = textw + iconw + ob_rr_theme->padding * (self->hasicon ? 3 : 2);
110 }
111
112 void popup_set_text_align(Popup *self, RrJustify align)
113 {
114     if (!self->a_text)
115         self->a_text = RrAppearanceCopy(ob_rr_theme->app_hilite_label);
116
117     self->a_text->texture[0].data.text.justify = align;
118 }
119
120 void popup_show(Popup *self, gchar *text, ObClientIcon *icon)
121 {
122     gint l, t, r, b;
123     gint x, y, w, h;
124     gint textw, texth;
125     gint iconw;
126
127     /* create the shit if needed */
128     if (!self->a_bg)
129         self->a_bg = RrAppearanceCopy(ob_rr_theme->app_hilite_bg);
130     if (self->hasicon && !self->a_icon)
131         self->a_icon = RrAppearanceCopy(ob_rr_theme->a_clear_tex);
132     if (!self->a_text)
133         self->a_text = RrAppearanceCopy(ob_rr_theme->app_hilite_label);
134
135     RrMargins(self->a_bg, &l, &t, &r, &b);
136
137     XSetWindowBorderWidth(ob_display, self->bg, ob_rr_theme->bwidth);
138     XSetWindowBorder(ob_display, self->bg, ob_rr_theme->b_color->pixel);
139
140     /* set up the textures */
141     self->a_text->texture[0].data.text.string = text;
142     if (self->hasicon) {
143         if (icon) {
144             self->a_icon->texture[0].type = RR_TEXTURE_RGBA;
145             self->a_icon->texture[0].data.rgba.width = icon->width;
146             self->a_icon->texture[0].data.rgba.height = icon->height;
147             self->a_icon->texture[0].data.rgba.data = icon->data;
148         } else
149             self->a_icon->texture[0].type = RR_TEXTURE_NONE;
150     }
151
152     /* measure the shit out */
153     RrMinsize(self->a_text, &textw, &texth);
154     /*XXX textw += ob_rr_theme->padding * 2;*/
155     texth += ob_rr_theme->padding * 2;
156
157     /* set the sizes up and reget the text sizes from the calculated
158        outer sizes */
159     if (self->h) {
160         h = self->h;
161         texth = h - (t+b + ob_rr_theme->padding * 2);
162     } else
163         h = t+b + texth + ob_rr_theme->padding * 2;
164     iconw = (self->hasicon ? texth : 0);
165     if (self->w) {
166         w = self->w;
167         textw = w - (l+r + iconw + ob_rr_theme->padding *
168                      (self->hasicon ? 3 : 2));
169     } else
170         w = l+r + textw + iconw + ob_rr_theme->padding *
171             (self->hasicon ? 3 : 2);
172     /* sanity checks to avoid crashes! */
173     if (w < 1) w = 1;
174     if (h < 1) h = 1;
175     if (textw < 1) textw = 1;
176     if (texth < 1) texth = 1;
177
178     /* set up the x coord */
179     x = self->x;
180     switch (self->gravity) {
181     case NorthGravity:
182     case CenterGravity:
183     case SouthGravity:
184         x -= w / 2;
185         break;
186     case NorthEastGravity:
187     case EastGravity:
188     case SouthEastGravity:
189         x -= w;
190         break;
191     }
192
193     /* set up the y coord */
194     y = self->y;
195     switch (self->gravity) {
196     case WestGravity:
197     case CenterGravity:
198     case EastGravity:
199         y -= h / 2;
200         break;
201     case SouthWestGravity:
202     case SouthGravity:
203     case SouthEastGravity:
204         y -= h;
205         break;
206     }
207
208     /* set the windows/appearances up */
209     XMoveResizeWindow(ob_display, self->bg, x, y, w, h);
210
211     self->a_text->surface.parent = self->a_bg;
212     self->a_text->surface.parentx = l + iconw +
213         ob_rr_theme->padding * (self->hasicon ? 2 : 1);
214     self->a_text->surface.parenty = t + ob_rr_theme->padding;
215     XMoveResizeWindow(ob_display, self->text,
216                       l + iconw + ob_rr_theme->padding * (self->hasicon ? 2 : 1),
217                       t + ob_rr_theme->padding, textw, texth);
218
219     if (self->hasicon) {
220         if (iconw < 1) iconw = 1; /* sanity check for crashes */
221         self->a_icon->surface.parent = self->a_bg;
222         self->a_icon->surface.parentx = l + ob_rr_theme->padding;
223         self->a_icon->surface.parenty = t + ob_rr_theme->padding;
224         XMoveResizeWindow(ob_display, self->icon,
225                           l + ob_rr_theme->padding, t + ob_rr_theme->padding,
226                           iconw, texth);
227     }
228
229     RrPaint(self->a_bg, self->bg, w, h);
230     RrPaint(self->a_text, self->text, textw, texth);
231     if (self->hasicon)
232         RrPaint(self->a_icon, self->icon, iconw, texth);
233
234     if (!self->mapped) {
235         XMapWindow(ob_display, self->bg);
236         stacking_raise(INTERNAL_AS_WINDOW(self));
237         self->mapped = TRUE;
238     }
239 }
240
241 void popup_hide(Popup *self)
242 {
243     if (self->mapped) {
244         XUnmapWindow(ob_display, self->bg);
245         self->mapped = FALSE;
246     }
247 }