]> icculus.org git repositories - dana/openbox.git/blob - openbox/prompt.c
Make ObPrompt windows get managed as clients, and make them able to reconfigure as...
[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 "client.h"
23 #include "obt/display.h"
24 #include "obt/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 = OB_WINDOW_CLASS_PROMPT;
103     self->super.window = XCreateWindow(obt_display, obt_root(ob_screen),
104                                        0, 0, 1, 1, ob_rr_theme->obwidth,
105                                        CopyFromParent, InputOutput,
106                                        CopyFromParent,
107                                        CWOverrideRedirect | CWBorderPixel,
108                                        &attrib);
109
110     OBT_PROP_SET32(self->super.window, NET_WM_WINDOW_TYPE, ATOM,
111                    OBT_PROP_ATOM(NET_WM_WINDOW_TYPE_DIALOG));
112
113     self->a_bg = RrAppearanceCopy(ob_rr_theme->osd_hilite_bg);
114
115     self->msg.text = g_strdup(msg);
116     self->msg.window = XCreateWindow(obt_display, self->super.window,
117                                      0, 0, 1, 1, 0,
118                                      CopyFromParent, InputOutput,
119                                      CopyFromParent, 0, NULL);
120     XMapWindow(obt_display, self->msg.window);
121
122     self->n_buttons = 0;
123     for (c = answers; *c != NULL; ++c)
124         ++self->n_buttons;
125
126     if (!self->n_buttons)
127         self->n_buttons = 1;
128
129     self->button = g_new(ObPromptElement, self->n_buttons);
130
131     if (!answers) {
132         g_assert(self->n_buttons == 1); /* should be set to this above.. */
133         self->button[0].text = g_strdup(_("OK"));
134     }
135     else {
136         g_assert(self->n_buttons > 0);
137         for (i = 0; i < self->n_buttons; ++i)
138             self->button[i].text = g_strdup(answers[i]);
139     }
140
141     for (i = 0; i < self->n_buttons; ++i) {
142         self->button[i].window = XCreateWindow(obt_display, self->super.window,
143                                                0, 0, 1, 1, 0,
144                                                CopyFromParent, InputOutput,
145                                                CopyFromParent, 0, NULL);
146         XMapWindow(obt_display, self->button[i].window);
147         window_add(&self->button[i].window, PROMPT_AS_WINDOW(self));
148     }
149
150     prompt_list = g_list_prepend(prompt_list, self);
151
152     return self;
153 }
154
155 void prompt_ref(ObPrompt *self)
156 {
157     ++self->ref;
158 }
159
160 void prompt_unref(ObPrompt *self)
161 {
162     if (self && --self->ref == 0) {
163         guint i;
164
165         prompt_list = g_list_remove(prompt_list, self);
166
167         for (i = 0; i < self->n_buttons; ++i) {
168             window_remove(self->button[i].window);
169             XDestroyWindow(obt_display, self->button[i].window);
170         }
171
172         XDestroyWindow(obt_display, self->msg.window);
173
174         RrAppearanceFree(self->a_bg);
175
176         XDestroyWindow(obt_display, self->super.window);
177         g_free(self);
178     }
179 }
180
181 static void prompt_layout(ObPrompt *self)
182 {
183     gint l, r, t, b;
184     guint i;
185     gint allbuttonsw, allbuttonsh, buttonx;
186     gint w, h;
187     gint maxw;
188
189     const gint OUTSIDE_MARGIN = 4;
190     const gint MSG_BUTTON_SEPARATION = 4;
191     const gint BUTTON_SEPARATION = 4;
192     const gint MAX_WIDTH = 600;
193
194     RrMargins(self->a_bg, &l, &t, &r, &b);
195     l += OUTSIDE_MARGIN;
196     t += OUTSIDE_MARGIN;
197     r += OUTSIDE_MARGIN;
198     b += OUTSIDE_MARGIN;
199
200     {
201         Rect *area = screen_physical_area_all_monitors();
202         maxw = MIN(MAX_WIDTH, area->width*4/5);
203         g_free(area);
204     }
205
206     /* find the button sizes and how much space we need for them */
207     allbuttonsw = allbuttonsh = 0;
208     for (i = 0; i < self->n_buttons; ++i) {
209         gint bw, bh;
210
211         prompt_a_button->texture[0].data.text.string = self->button[i].text;
212         prompt_a_hover->texture[0].data.text.string = self->button[i].text;
213         prompt_a_press->texture[0].data.text.string = self->button[i].text;
214         RrMinSize(prompt_a_button, &bw, &bh);
215         self->button[i].width = bw;
216         self->button[i].height = bh;
217         RrMinSize(prompt_a_hover, &bw, &bh);
218         self->button[i].width = MAX(self->button[i].width, bw);
219         self->button[i].height = MAX(self->button[i].height, bh);
220         RrMinSize(prompt_a_press, &bw, &bh);
221         self->button[i].width = MAX(self->button[i].width, bw);
222         self->button[i].height = MAX(self->button[i].height, bh);
223
224         allbuttonsw += self->button[i].width + (i > 0 ? BUTTON_SEPARATION : 0);
225         allbuttonsh = MAX(allbuttonsh, self->button[i].height);
226     }
227
228     self->msg_wbound = MAX(allbuttonsw, maxw);
229
230     /* measure the text message area */
231     prompt_a_msg->texture[0].data.text.string = self->msg.text;
232     prompt_a_msg->texture[0].data.text.maxwidth = self->msg_wbound;
233     RrMinSize(prompt_a_msg, &self->msg.width, &self->msg.height);
234
235     /* width and height inside the outer margins */
236     w = MAX(self->msg.width, allbuttonsw);
237     h = self->msg.height + MSG_BUTTON_SEPARATION + allbuttonsh;
238
239     /* position the text message */
240     self->msg.x = l + (w - self->msg.width) / 2;
241     self->msg.y = t;
242
243     /* position the button buttons */
244     buttonx = l + (w - allbuttonsw) / 2;
245     for (i = 0; i < self->n_buttons; ++i) {
246         self->button[i].x = buttonx;
247         buttonx += self->button[i].width + BUTTON_SEPARATION;
248         self->button[i].y = t + h - allbuttonsh;
249         self->button[i].y += (allbuttonsh - self->button[i].height) / 2;
250     }
251
252     /* size and position the toplevel window */
253     self->width = w + l + r;
254     self->height = h + t + b;
255
256     /* move and resize the actual windows */
257     XResizeWindow(obt_display, self->super.window, self->width, self->height);
258     XMoveResizeWindow(obt_display, self->msg.window,
259                       self->msg.x, self->msg.y,
260                       self->msg.width, self->msg.height);
261     for (i = 0; i < self->n_buttons; ++i)
262         XMoveResizeWindow(obt_display, self->button[i].window,
263                           self->button[i].x, self->button[i].y,
264                           self->button[i].width, self->button[i].height);
265 }
266
267 static void render_button(ObPrompt *self, ObPromptElement *e)
268 {
269     prompt_a_button->surface.parent = self->a_bg;
270     prompt_a_button->surface.parentx = e->x;
271     prompt_a_button->surface.parentx = e->y;
272
273     prompt_a_button->texture[0].data.text.string = e->text;
274     RrPaint(prompt_a_button, e->window, e->width, e->height);
275 }
276
277 static void render_all(ObPrompt *self)
278 {
279     guint i;
280
281     RrPaint(self->a_bg, self->super.window, self->width, self->height);
282
283     prompt_a_msg->surface.parent = self->a_bg;
284     prompt_a_msg->surface.parentx = self->msg.x;
285     prompt_a_msg->surface.parentx = self->msg.y;
286
287     prompt_a_msg->texture[0].data.text.string = self->msg.text;
288     prompt_a_msg->texture[0].data.text.maxwidth = self->msg_wbound;
289     RrPaint(prompt_a_msg, self->msg.window, self->msg.width, self->msg.height);
290
291     for (i = 0; i < self->n_buttons; ++i)
292         render_button(self, &self->button[i]);
293 }
294
295 void prompt_show(ObPrompt *self, ObClient *parent)
296 {
297     XSizeHints hints;
298
299     if (self->mapped) return;
300
301     prompt_layout(self);
302     render_all(self);
303
304     /* you can't resize the prompt */
305     hints.flags = PMinSize | PMaxSize;
306     hints.min_width = hints.max_width = self->width;
307     hints.min_height = hints.max_height = self->height;
308     XSetWMNormalHints(obt_display, self->super.window, &hints);
309
310     XSetTransientForHint(obt_display, (parent ? parent->window : 0),
311                          self->super.window);
312
313     client_manage(self->super.window, self);
314
315     self->mapped = TRUE;
316 }
317
318 void prompt_hide(ObPrompt *self)
319 {
320     XUnmapWindow(obt_display, self->super.window);
321     self->mapped = FALSE;
322 }
323
324 void prompt_hide_window(Window window)
325 {
326     GList *it;
327     ObPrompt *p = NULL;
328
329     for (it = prompt_list; it; it = g_list_next(it)) {
330         p = it->data;
331         if (p->super.window == window) break;
332     }
333     g_assert(it != NULL);
334     prompt_hide(p);
335 }