]> icculus.org git repositories - dana/openbox.git/blob - openbox/prompt.c
properly place the msg texture
[dana/openbox.git] / openbox / prompt.c
1 /* -*- indent-tabs-mode: nil; tab-width: 4; c-basic-offset: 4; -*-
2
3    prompt.c for the Openbox window manager
4    Copyright (c) 2008        Dana Jansens
5
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 2 of the License, or
9    (at your option) any later version.
10
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15
16    See the COPYING file for a copy of the GNU General Public License.
17 */
18
19 #include "prompt.h"
20 #include "openbox.h"
21 #include "screen.h"
22 #include "openbox.h"
23 #include "client.h"
24 #include "prop.h"
25 #include "gettext.h"
26
27 static GList *prompt_list = NULL;
28
29 /* we construct these */
30 static RrAppearance *prompt_a_button;
31 static RrAppearance *prompt_a_hover;
32 static RrAppearance *prompt_a_press;
33 /* we change the max width which would screw with others */
34 static RrAppearance *prompt_a_msg;
35
36 static void prompt_layout(ObPrompt *self);
37 static void render_all(ObPrompt *self);
38 static void render_button(ObPrompt *self, ObPromptElement *e);
39
40 void prompt_startup(gboolean reconfig)
41 {
42     RrColor *c_button, *c_hover, *c_press;
43
44     prompt_a_button = RrAppearanceCopy(ob_rr_theme->a_focused_unpressed_close);
45     prompt_a_hover = RrAppearanceCopy(ob_rr_theme->a_hover_focused_close);
46     prompt_a_press = RrAppearanceCopy(ob_rr_theme->a_focused_pressed_close);
47
48     c_button = prompt_a_button->texture[0].data.mask.color;
49     c_hover = prompt_a_button->texture[0].data.mask.color;
50     c_press = prompt_a_button->texture[0].data.mask.color;
51
52     RrAppearanceRemoveTextures(prompt_a_button);
53     RrAppearanceRemoveTextures(prompt_a_hover);
54     RrAppearanceRemoveTextures(prompt_a_press);
55
56     RrAppearanceAddTextures(prompt_a_button, 1);
57     RrAppearanceAddTextures(prompt_a_hover, 1);
58     RrAppearanceAddTextures(prompt_a_press, 1);
59
60     /* totally cheating here.. */
61     prompt_a_button->texture[0] = ob_rr_theme->osd_hilite_label->texture[0];
62     prompt_a_hover->texture[0] = ob_rr_theme->osd_hilite_label->texture[0];
63     prompt_a_press->texture[0] = ob_rr_theme->osd_hilite_label->texture[0];
64
65     prompt_a_button->texture[0].data.text.color = c_button;
66     prompt_a_hover->texture[0].data.text.color = c_hover;
67     prompt_a_press->texture[0].data.text.color = c_press;
68
69     prompt_a_msg = RrAppearanceCopy(ob_rr_theme->osd_hilite_label);
70     prompt_a_msg->texture[0].data.text.flow = TRUE;
71
72     if (reconfig) {
73         GList *it;
74         for (it = prompt_list; it; it = g_list_next(it)) {
75             ObPrompt *p = it->data;
76             prompt_layout(p);
77             render_all(p);
78         }
79     }
80 }
81
82 void prompt_shutdown(gboolean reconfig)
83 {
84     RrAppearanceFree(prompt_a_button);
85     RrAppearanceFree(prompt_a_hover);
86     RrAppearanceFree(prompt_a_press);
87     RrAppearanceFree(prompt_a_msg);
88 }
89
90 ObPrompt* prompt_new(const gchar *msg, const gchar *const *answers)
91 {
92     ObPrompt *self;
93     XSetWindowAttributes attrib;
94     guint i;
95     const gchar *const *c;
96
97     attrib.override_redirect = FALSE;
98     attrib.border_pixel = RrColorPixel(ob_rr_theme->osd_border_color);
99
100     self = g_new0(ObPrompt, 1);
101     self->ref = 1;
102     self->super.type = Window_Prompt;
103     self->super.window = XCreateWindow(ob_display,
104                                        RootWindow(ob_display, ob_screen),
105                                        0, 0, 1, 1, ob_rr_theme->obwidth,
106                                        CopyFromParent, InputOutput,
107                                        CopyFromParent,
108                                        CWOverrideRedirect | CWBorderPixel,
109                                        &attrib);
110
111     PROP_SET32(self->super.window, net_wm_window_type, atom,
112                prop_atoms.net_wm_window_type_dialog);
113
114     self->a_bg = RrAppearanceCopy(ob_rr_theme->osd_hilite_bg);
115
116     self->msg.text = g_strdup(msg);
117     self->msg.window = XCreateWindow(ob_display, self->super.window,
118                                      0, 0, 1, 1, 0,
119                                      CopyFromParent, InputOutput,
120                                      CopyFromParent, 0, NULL);
121     XMapWindow(ob_display, self->msg.window);
122
123     self->n_buttons = 0;
124     for (c = answers; *c != NULL; ++c)
125         ++self->n_buttons;
126
127     if (!self->n_buttons)
128         self->n_buttons = 1;
129
130     self->button = g_new(ObPromptElement, self->n_buttons);
131
132     if (!answers) {
133         g_assert(self->n_buttons == 1); /* should be set to this above.. */
134         self->button[0].text = g_strdup(_("OK"));
135     }
136     else {
137         g_assert(self->n_buttons > 0);
138         for (i = 0; i < self->n_buttons; ++i)
139             self->button[i].text = g_strdup(answers[i]);
140     }
141
142     for (i = 0; i < self->n_buttons; ++i) {
143         self->button[i].window = XCreateWindow(ob_display, self->super.window,
144                                                0, 0, 1, 1, 0,
145                                                CopyFromParent, InputOutput,
146                                                CopyFromParent, 0, NULL);
147         XMapWindow(ob_display, self->button[i].window);
148         g_hash_table_insert(window_map, &self->button[i].window,
149                             PROMPT_AS_WINDOW(self));
150     }
151
152     prompt_list = g_list_prepend(prompt_list, self);
153
154     return self;
155 }
156
157 void prompt_ref(ObPrompt *self)
158 {
159     ++self->ref;
160 }
161
162 void prompt_unref(ObPrompt *self)
163 {
164     if (self && --self->ref == 0) {
165         guint i;
166
167         prompt_list = g_list_remove(prompt_list, self);
168
169         for (i = 0; i < self->n_buttons; ++i) {
170             g_hash_table_remove(window_map, &self->button[i].window);
171             XDestroyWindow(ob_display, self->button[i].window);
172         }
173
174         XDestroyWindow(ob_display, self->msg.window);
175
176         RrAppearanceFree(self->a_bg);
177
178         XDestroyWindow(ob_display, self->super.window);
179         g_free(self);
180     }
181 }
182
183 static void prompt_layout(ObPrompt *self)
184 {
185     gint l, r, t, b;
186     guint i;
187     gint allbuttonsw, allbuttonsh, buttonx;
188     gint w, h;
189     gint maxw;
190
191     const gint OUTSIDE_MARGIN = 4;
192     const gint MSG_BUTTON_SEPARATION = 4;
193     const gint BUTTON_SEPARATION = 4;
194     const gint MAX_WIDTH = 600;
195
196     RrMargins(self->a_bg, &l, &t, &r, &b);
197     l += OUTSIDE_MARGIN;
198     t += OUTSIDE_MARGIN;
199     r += OUTSIDE_MARGIN;
200     b += OUTSIDE_MARGIN;
201
202     {
203         Rect *area = screen_physical_area_all_monitors();
204         maxw = MIN(MAX_WIDTH, area->width*4/5);
205         g_free(area);
206     }
207
208     /* find the button sizes and how much space we need for them */
209     allbuttonsw = allbuttonsh = 0;
210     for (i = 0; i < self->n_buttons; ++i) {
211         gint bw, bh;
212
213         prompt_a_button->texture[0].data.text.string = self->button[i].text;
214         prompt_a_hover->texture[0].data.text.string = self->button[i].text;
215         prompt_a_press->texture[0].data.text.string = self->button[i].text;
216         RrMinSize(prompt_a_button, &bw, &bh);
217         self->button[i].width = bw;
218         self->button[i].height = bh;
219         RrMinSize(prompt_a_hover, &bw, &bh);
220         self->button[i].width = MAX(self->button[i].width, bw);
221         self->button[i].height = MAX(self->button[i].height, bh);
222         RrMinSize(prompt_a_press, &bw, &bh);
223         self->button[i].width = MAX(self->button[i].width, bw);
224         self->button[i].height = MAX(self->button[i].height, bh);
225
226         allbuttonsw += self->button[i].width + (i > 0 ? BUTTON_SEPARATION : 0);
227         allbuttonsh = MAX(allbuttonsh, self->button[i].height);
228     }
229
230     self->msg_wbound = MAX(allbuttonsw, maxw);
231
232     /* measure the text message area */
233     prompt_a_msg->texture[0].data.text.string = self->msg.text;
234     prompt_a_msg->texture[0].data.text.maxwidth = self->msg_wbound;
235     RrMinSize(prompt_a_msg, &self->msg.width, &self->msg.height);
236
237     /* width and height inside the outer margins */
238     w = MAX(self->msg.width, allbuttonsw);
239     h = self->msg.height + MSG_BUTTON_SEPARATION + allbuttonsh;
240
241     /* position the text message */
242     self->msg.x = l + (w - self->msg.width) / 2;
243     self->msg.y = t;
244
245     /* position the button buttons */
246     buttonx = l + (w - allbuttonsw) / 2;
247     for (i = 0; i < self->n_buttons; ++i) {
248         self->button[i].x = buttonx;
249         buttonx += self->button[i].width + BUTTON_SEPARATION;
250         self->button[i].y = t + h - allbuttonsh;
251         self->button[i].y += (allbuttonsh - self->button[i].height) / 2;
252     }
253
254     /* size and position the toplevel window */
255     self->width = w + l + r;
256     self->height = h + t + b;
257
258     /* move and resize the actual windows */
259     XResizeWindow(ob_display, self->super.window, self->width, self->height);
260     XMoveResizeWindow(ob_display, self->msg.window,
261                       self->msg.x, self->msg.y,
262                       self->msg.width, self->msg.height);
263     for (i = 0; i < self->n_buttons; ++i)
264         XMoveResizeWindow(ob_display, self->button[i].window,
265                           self->button[i].x, self->button[i].y,
266                           self->button[i].width, self->button[i].height);
267 }
268
269 static void render_button(ObPrompt *self, ObPromptElement *e)
270 {
271     prompt_a_button->surface.parent = self->a_bg;
272     prompt_a_button->surface.parentx = e->x;
273     prompt_a_button->surface.parentx = e->y;
274
275     prompt_a_button->texture[0].data.text.string = e->text;
276     RrPaint(prompt_a_button, e->window, e->width, e->height);
277 }
278
279 static void render_all(ObPrompt *self)
280 {
281     guint i;
282
283     RrPaint(self->a_bg, self->super.window, self->width, self->height);
284
285     prompt_a_msg->surface.parent = self->a_bg;
286     prompt_a_msg->surface.parentx = self->msg.x;
287     prompt_a_msg->surface.parenty = self->msg.y;
288
289     prompt_a_msg->texture[0].data.text.string = self->msg.text;
290     prompt_a_msg->texture[0].data.text.maxwidth = self->msg_wbound;
291     RrPaint(prompt_a_msg, self->msg.window, self->msg.width, self->msg.height);
292
293     for (i = 0; i < self->n_buttons; ++i)
294         render_button(self, &self->button[i]);
295 }
296
297 void prompt_show(ObPrompt *self, ObClient *parent)
298 {
299     XSizeHints hints;
300
301     if (self->mapped) return;
302
303     prompt_layout(self);
304     render_all(self);
305
306     /* you can't resize the prompt */
307     hints.flags = PMinSize | PMaxSize;
308     hints.min_width = hints.max_width = self->width;
309     hints.min_height = hints.max_height = self->height;
310     XSetWMNormalHints(ob_display, self->super.window, &hints);
311
312     XSetTransientForHint(ob_display, (parent ? parent->window : 0),
313                          self->super.window);
314
315     client_manage(self->super.window, self);
316
317     self->mapped = TRUE;
318 }
319
320 void prompt_hide(ObPrompt *self)
321 {
322     XUnmapWindow(ob_display, self->super.window);
323     self->mapped = FALSE;
324 }
325
326 void prompt_hide_window(Window window)
327 {
328     GList *it;
329     ObPrompt *p = NULL;
330
331     for (it = prompt_list; it; it = g_list_next(it)) {
332         p = it->data;
333         if (p->super.window == window) break;
334     }
335     g_assert(it != NULL);
336     prompt_hide(p);
337 }