40757b5b700d645212982989a53b36e41dce1c3e
[mikachu/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 * (self->hasicon ? 3 : 2);
171     /* sanity checks to avoid crashes! */
172     if (w < 1) w = 1;
173     if (h < 1) h = 1;
174     if (textw < 1) textw = 1;
175     if (texth < 1) texth = 1;
176
177     /* set up the x coord */
178     x = self->x;
179     switch (self->gravity) {
180     case NorthGravity:
181     case CenterGravity:
182     case SouthGravity:
183         x -= w / 2;
184         break;
185     case NorthEastGravity:
186     case EastGravity:
187     case SouthEastGravity:
188         x -= w;
189         break;
190     }
191
192     /* set up the y coord */
193     y = self->y;
194     switch (self->gravity) {
195     case WestGravity:
196     case CenterGravity:
197     case EastGravity:
198         y -= h / 2;
199         break;
200     case SouthWestGravity:
201     case SouthGravity:
202     case SouthEastGravity:
203         y -= h;
204         break;
205     }
206
207     /* set the windows/appearances up */
208     XMoveResizeWindow(ob_display, self->bg, x, y, w, h);
209
210     self->a_text->surface.parent = self->a_bg;
211     self->a_text->surface.parentx = l + iconw +
212         ob_rr_theme->padding * (self->hasicon ? 2 : 1);
213     self->a_text->surface.parenty = t + ob_rr_theme->padding;
214     XMoveResizeWindow(ob_display, self->text,
215                       l + iconw + ob_rr_theme->padding * (self->hasicon ? 2 : 1),
216                       t + ob_rr_theme->padding, textw, texth);
217
218     if (self->hasicon) {
219         if (iconw < 1) iconw = 1; /* sanity check for crashes */
220         self->a_icon->surface.parent = self->a_bg;
221         self->a_icon->surface.parentx = l + ob_rr_theme->padding;
222         self->a_icon->surface.parenty = t + ob_rr_theme->padding;
223         XMoveResizeWindow(ob_display, self->icon,
224                           l + ob_rr_theme->padding, t + ob_rr_theme->padding,
225                           iconw, texth);
226     }
227
228     RrPaint(self->a_bg, self->bg, w, h);
229     RrPaint(self->a_text, self->text, textw, texth);
230     if (self->hasicon)
231         RrPaint(self->a_icon, self->icon, iconw, texth);
232
233     if (!self->mapped) {
234         XMapWindow(ob_display, self->bg);
235         stacking_raise(INTERNAL_AS_WINDOW(self));
236         self->mapped = TRUE;
237     }
238 }
239
240 void popup_hide(Popup *self)
241 {
242     if (self->mapped) {
243         XUnmapWindow(ob_display, self->bg);
244         self->mapped = FALSE;
245     }
246 }