remove the ob_root var, its redundant of what Xlib already provides
[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     textw += ob_rr_theme->bevel * 2;
105     texth += ob_rr_theme->bevel * 2;
106
107     self->h = texth + ob_rr_theme->bevel * 2;
108     iconw = (self->hasicon ? texth : 0);
109     self->w = textw + iconw + ob_rr_theme->bevel * (self->hasicon ? 3 : 2);
110 }
111
112 void popup_show(Popup *self, gchar *text, ObClientIcon *icon)
113 {
114     gint x, y, w, h;
115     gint textw, texth;
116     gint iconw;
117
118     /* create the shit if needed */
119     if (!self->a_bg)
120         self->a_bg = RrAppearanceCopy(ob_rr_theme->app_hilite_bg);
121     if (self->hasicon && !self->a_icon)
122         self->a_icon = RrAppearanceCopy(ob_rr_theme->app_icon);
123     if (!self->a_text)
124         self->a_text = RrAppearanceCopy(ob_rr_theme->app_hilite_label);
125
126     XSetWindowBorderWidth(ob_display, self->bg, ob_rr_theme->bwidth);
127     XSetWindowBorder(ob_display, self->bg, ob_rr_theme->b_color->pixel);
128
129     /* set up the textures */
130     self->a_text->texture[0].data.text.string = text;
131     if (self->hasicon) {
132         if (icon) {
133             self->a_icon->texture[0].type = RR_TEXTURE_RGBA;
134             self->a_icon->texture[0].data.rgba.width = icon->width;
135             self->a_icon->texture[0].data.rgba.height = icon->height;
136             self->a_icon->texture[0].data.rgba.data = icon->data;
137         } else
138             self->a_icon->texture[0].type = RR_TEXTURE_NONE;
139     }
140
141     /* measure the shit out */
142     RrMinsize(self->a_text, &textw, &texth);
143     textw += ob_rr_theme->bevel * 2;
144     texth += ob_rr_theme->bevel * 2;
145
146     /* set the sizes up and reget the text sizes from the calculated
147        outer sizes */
148     if (self->h) {
149         h = self->h;
150         texth = h - (ob_rr_theme->bevel * 2);
151     } else
152         h = texth + ob_rr_theme->bevel * 2;
153     iconw = (self->hasicon ? texth : 0);
154     if (self->w) {
155         w = self->w;
156         textw = w - (iconw + ob_rr_theme->bevel * (self->hasicon ? 3 : 2));
157     } else
158         w = textw + iconw + ob_rr_theme->bevel * (self->hasicon ? 3 : 2);
159     /* sanity checks to avoid crashes! */
160     if (w < 1) w = 1;
161     if (h < 1) h = 1;
162     if (textw < 1) textw = 1;
163     if (texth < 1) texth = 1;
164
165     /* set up the x coord */
166     x = self->x;
167     switch (self->gravity) {
168     case NorthGravity:
169     case CenterGravity:
170     case SouthGravity:
171         x -= w / 2;
172         break;
173     case NorthEastGravity:
174     case EastGravity:
175     case SouthEastGravity:
176         x -= w;
177         break;
178     }
179
180     /* set up the y coord */
181     y = self->y;
182     switch (self->gravity) {
183     case WestGravity:
184     case CenterGravity:
185     case EastGravity:
186         y -= h / 2;
187         break;
188     case SouthWestGravity:
189     case SouthGravity:
190     case SouthEastGravity:
191         y -= h;
192         break;
193     }
194
195     /* set the windows/appearances up */
196     XMoveResizeWindow(ob_display, self->bg, x, y, w, h);
197
198     self->a_text->surface.parent = self->a_bg;
199     self->a_text->surface.parentx = iconw +
200         ob_rr_theme->bevel * (self->hasicon ? 2 : 1);
201     self->a_text->surface.parenty = ob_rr_theme->bevel;
202     XMoveResizeWindow(ob_display, self->text,
203                       iconw + ob_rr_theme->bevel * (self->hasicon ? 2 : 1),
204                       ob_rr_theme->bevel, textw, texth);
205
206     if (self->hasicon) {
207         if (iconw < 1) iconw = 1; /* sanity check for crashes */
208         self->a_icon->surface.parent = self->a_bg;
209         self->a_icon->surface.parentx = ob_rr_theme->bevel;
210         self->a_icon->surface.parenty = ob_rr_theme->bevel;
211         XMoveResizeWindow(ob_display, self->icon,
212                           ob_rr_theme->bevel, ob_rr_theme->bevel,
213                           iconw, texth);
214     }
215
216     RrPaint(self->a_bg, self->bg, w, h);
217     RrPaint(self->a_text, self->text, textw, texth);
218     if (self->hasicon)
219         RrPaint(self->a_icon, self->icon, iconw, texth);
220
221     if (!self->mapped) {
222         XMapWindow(ob_display, self->bg);
223         stacking_raise(INTERNAL_AS_WINDOW(self));
224         self->mapped = TRUE;
225     }
226 }
227
228 void popup_hide(Popup *self)
229 {
230     if (self->mapped) {
231         XUnmapWindow(ob_display, self->bg);
232         self->mapped = FALSE;
233     }
234 }