54ea469d921f38912e23c4a01b301f435d8600f0
[mikachu/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/keyboard.h"
25 #include "obt/prop.h"
26 #include "gettext.h"
27
28 static GList *prompt_list = NULL;
29
30 /* we construct these */
31 static RrAppearance *prompt_a_button;
32 static RrAppearance *prompt_a_focus;
33 static RrAppearance *prompt_a_press;
34 /* we change the max width which would screw with others */
35 static RrAppearance *prompt_a_msg;
36
37 static void prompt_layout(ObPrompt *self);
38 static void render_all(ObPrompt *self);
39 static void render_button(ObPrompt *self, ObPromptElement *e);
40
41 void prompt_startup(gboolean reconfig)
42 {
43     RrColor *c_button, *c_focus, *c_press;
44
45     prompt_a_button = RrAppearanceCopy(ob_rr_theme->a_focused_unpressed_close);
46     prompt_a_focus = RrAppearanceCopy(ob_rr_theme->a_hover_focused_close);
47     prompt_a_press = RrAppearanceCopy(ob_rr_theme->a_focused_pressed_close);
48
49     c_button = prompt_a_button->texture[0].data.mask.color;
50     c_focus = prompt_a_button->texture[0].data.mask.color;
51     c_press = prompt_a_button->texture[0].data.mask.color;
52
53     RrAppearanceRemoveTextures(prompt_a_button);
54     RrAppearanceRemoveTextures(prompt_a_focus);
55     RrAppearanceRemoveTextures(prompt_a_press);
56
57     RrAppearanceAddTextures(prompt_a_button, 1);
58     RrAppearanceAddTextures(prompt_a_focus, 1);
59     RrAppearanceAddTextures(prompt_a_press, 1);
60
61     /* totally cheating here.. */
62     prompt_a_button->texture[0] = ob_rr_theme->osd_hilite_label->texture[0];
63     prompt_a_focus->texture[0] = ob_rr_theme->osd_hilite_label->texture[0];
64     prompt_a_press->texture[0] = ob_rr_theme->osd_hilite_label->texture[0];
65
66     prompt_a_button->texture[0].data.text.justify = RR_JUSTIFY_CENTER;
67     prompt_a_focus->texture[0].data.text.justify = RR_JUSTIFY_CENTER;
68     prompt_a_press->texture[0].data.text.justify = RR_JUSTIFY_CENTER;
69
70     prompt_a_button->texture[0].data.text.color = c_button;
71     prompt_a_focus->texture[0].data.text.color = c_focus;
72     prompt_a_press->texture[0].data.text.color = c_press;
73
74     prompt_a_msg = RrAppearanceCopy(ob_rr_theme->osd_hilite_label);
75     prompt_a_msg->texture[0].data.text.flow = TRUE;
76
77     if (reconfig) {
78         GList *it;
79         for (it = prompt_list; it; it = g_list_next(it)) {
80             ObPrompt *p = it->data;
81             prompt_layout(p);
82             render_all(p);
83         }
84     }
85 }
86
87 void prompt_shutdown(gboolean reconfig)
88 {
89     RrAppearanceFree(prompt_a_button);
90     RrAppearanceFree(prompt_a_focus);
91     RrAppearanceFree(prompt_a_press);
92     RrAppearanceFree(prompt_a_msg);
93 }
94
95 ObPrompt* prompt_new(const gchar *msg,
96                      const ObPromptAnswer *answers, gint n_answers,
97                      gint default_result, gint cancel_result,
98                      ObPromptCallback func, gpointer data)
99 {
100     ObPrompt *self;
101     XSetWindowAttributes attrib;
102     gint i;
103
104     attrib.override_redirect = FALSE;
105     attrib.border_pixel = RrColorPixel(ob_rr_theme->osd_border_color);
106
107     self = g_new0(ObPrompt, 1);
108     self->ref = 1;
109     self->func = func;
110     self->data = data;
111     self->default_result = default_result;
112     self->cancel_result = cancel_result;
113     self->super.type = OB_WINDOW_CLASS_PROMPT;
114     self->super.window = XCreateWindow(obt_display, obt_root(ob_screen),
115                                        0, 0, 1, 1, ob_rr_theme->obwidth,
116                                        CopyFromParent, InputOutput,
117                                        CopyFromParent,
118                                        CWOverrideRedirect | CWBorderPixel,
119                                        &attrib);
120
121     /* make it a dialog type window */
122     OBT_PROP_SET32(self->super.window, NET_WM_WINDOW_TYPE, ATOM,
123                    OBT_PROP_ATOM(NET_WM_WINDOW_TYPE_DIALOG));
124
125     /* listen for key presses on the window */
126     self->event_mask = KeyPressMask;
127
128     /* we make a copy of this appearance for each prompt */
129     self->a_bg = RrAppearanceCopy(ob_rr_theme->osd_hilite_bg);
130
131     /* set up the text message widow */
132     self->msg.text = g_strdup(msg);
133     self->msg.window = XCreateWindow(obt_display, self->super.window,
134                                      0, 0, 1, 1, 0,
135                                      CopyFromParent, InputOutput,
136                                      CopyFromParent, 0, NULL);
137     XMapWindow(obt_display, self->msg.window);
138
139     /* set up the buttons from the answers */
140
141     self->n_buttons = n_answers;
142     if (!self->n_buttons)
143         self->n_buttons = 1;
144
145     self->button = g_new0(ObPromptElement, self->n_buttons);
146
147     if (n_answers == 0) {
148         g_assert(self->n_buttons == 1); /* should be set to this above.. */
149         self->button[0].text = g_strdup(_("OK"));
150     }
151     else {
152         g_assert(self->n_buttons > 0);
153         for (i = 0; i < self->n_buttons; ++i) {
154             self->button[i].text = g_strdup(answers[i].text);
155             self->button[i].result = answers[i].result;
156         }
157     }
158
159     for (i = 0; i < self->n_buttons; ++i) {
160         self->button[i].window = XCreateWindow(obt_display, self->super.window,
161                                                0, 0, 1, 1, 0,
162                                                CopyFromParent, InputOutput,
163                                                CopyFromParent, 0, NULL);
164         XMapWindow(obt_display, self->button[i].window);
165         window_add(&self->button[i].window, PROMPT_AS_WINDOW(self));
166
167         /* listen for button presses on the buttons */
168         XSelectInput(obt_display, self->button[i].window,
169                      ButtonPressMask | ButtonReleaseMask | ButtonMotionMask);
170     }
171
172     prompt_list = g_list_prepend(prompt_list, self);
173
174     return self;
175 }
176
177 void prompt_ref(ObPrompt *self)
178 {
179     ++self->ref;
180 }
181
182 void prompt_unref(ObPrompt *self)
183 {
184     if (self && --self->ref == 0) {
185         gint i;
186
187         prompt_list = g_list_remove(prompt_list, self);
188
189         for (i = 0; i < self->n_buttons; ++i) {
190             window_remove(self->button[i].window);
191             XDestroyWindow(obt_display, self->button[i].window);
192         }
193
194         XDestroyWindow(obt_display, self->msg.window);
195
196         RrAppearanceFree(self->a_bg);
197
198         XDestroyWindow(obt_display, self->super.window);
199         g_free(self);
200     }
201 }
202
203 static void prompt_layout(ObPrompt *self)
204 {
205     gint l, r, t, b;
206     gint i;
207     gint allbuttonsw, allbuttonsh, buttonx;
208     gint w, h;
209     gint maxw;
210
211     const gint OUTSIDE_MARGIN = 4;
212     const gint MSG_BUTTON_SEPARATION = 4;
213     const gint BUTTON_SEPARATION = 4;
214     const gint BUTTON_VMARGIN = 4;
215     const gint BUTTON_HMARGIN = 12;
216     const gint MAX_WIDTH = 600;
217
218     RrMargins(self->a_bg, &l, &t, &r, &b);
219     l += OUTSIDE_MARGIN;
220     t += OUTSIDE_MARGIN;
221     r += OUTSIDE_MARGIN;
222     b += OUTSIDE_MARGIN;
223
224     {
225         Rect *area = screen_physical_area_all_monitors();
226         maxw = MIN(MAX_WIDTH, area->width*4/5);
227         g_free(area);
228     }
229
230     /* find the button sizes and how much space we need for them */
231     allbuttonsw = allbuttonsh = 0;
232     for (i = 0; i < self->n_buttons; ++i) {
233         gint bw, bh;
234
235         prompt_a_button->texture[0].data.text.string = self->button[i].text;
236         prompt_a_focus->texture[0].data.text.string = self->button[i].text;
237         prompt_a_press->texture[0].data.text.string = self->button[i].text;
238         RrMinSize(prompt_a_button, &bw, &bh);
239         self->button[i].width = bw;
240         self->button[i].height = bh;
241         RrMinSize(prompt_a_focus, &bw, &bh);
242         self->button[i].width = MAX(self->button[i].width, bw);
243         self->button[i].height = MAX(self->button[i].height, bh);
244         RrMinSize(prompt_a_press, &bw, &bh);
245         self->button[i].width = MAX(self->button[i].width, bw);
246         self->button[i].height = MAX(self->button[i].height, bh);
247
248         self->button[i].width += BUTTON_HMARGIN * 2;
249         self->button[i].height += BUTTON_VMARGIN * 2;
250
251         allbuttonsw += self->button[i].width + (i > 0 ? BUTTON_SEPARATION : 0);
252         allbuttonsh = MAX(allbuttonsh, self->button[i].height);
253     }
254
255     self->msg_wbound = MAX(allbuttonsw, maxw);
256
257     /* measure the text message area */
258     prompt_a_msg->texture[0].data.text.string = self->msg.text;
259     prompt_a_msg->texture[0].data.text.maxwidth = self->msg_wbound;
260     RrMinSize(prompt_a_msg, &self->msg.width, &self->msg.height);
261
262     /* width and height inside the outer margins */
263     w = MAX(self->msg.width, allbuttonsw);
264     h = self->msg.height + MSG_BUTTON_SEPARATION + allbuttonsh;
265
266     /* position the text message */
267     self->msg.x = l + (w - self->msg.width) / 2;
268     self->msg.y = t;
269
270     /* position the button buttons on the right of the dialog */
271     buttonx = l + w;
272     for (i = self->n_buttons - 1; i >= 0; --i) {
273         self->button[i].x = buttonx - self->button[i].width;
274         buttonx -= self->button[i].width + BUTTON_SEPARATION;
275         self->button[i].y = t + h - allbuttonsh;
276         self->button[i].y += (allbuttonsh - self->button[i].height) / 2;
277     }
278
279     /* size and position the toplevel window */
280     self->width = w + l + r;
281     self->height = h + t + b;
282
283     /* move and resize the actual windows */
284     XResizeWindow(obt_display, self->super.window, self->width, self->height);
285     XMoveResizeWindow(obt_display, self->msg.window,
286                       self->msg.x, self->msg.y,
287                       self->msg.width, self->msg.height);
288     for (i = 0; i < self->n_buttons; ++i)
289         XMoveResizeWindow(obt_display, self->button[i].window,
290                           self->button[i].x, self->button[i].y,
291                           self->button[i].width, self->button[i].height);
292 }
293
294 static void render_button(ObPrompt *self, ObPromptElement *e)
295 {
296     RrAppearance *a;
297
298     if (e->pressed) a = prompt_a_press;
299     else if (self->focus == e) a = prompt_a_focus;
300     else a = prompt_a_button;
301
302     a->surface.parent = self->a_bg;
303     a->surface.parentx = e->x;
304     a->surface.parentx = e->y;
305
306     a->texture[0].data.text.string = e->text;
307     RrPaint(a, e->window, e->width, e->height);
308 }
309
310 static void render_all(ObPrompt *self)
311 {
312     gint i;
313
314     RrPaint(self->a_bg, self->super.window, self->width, self->height);
315
316     prompt_a_msg->surface.parent = self->a_bg;
317     prompt_a_msg->surface.parentx = self->msg.x;
318     prompt_a_msg->surface.parenty = self->msg.y;
319
320     prompt_a_msg->texture[0].data.text.string = self->msg.text;
321     prompt_a_msg->texture[0].data.text.maxwidth = self->msg_wbound;
322     RrPaint(prompt_a_msg, self->msg.window, self->msg.width, self->msg.height);
323
324     for (i = 0; i < self->n_buttons; ++i)
325         render_button(self, &self->button[i]);
326 }
327
328 void prompt_show(ObPrompt *self, ObClient *parent)
329 {
330     XSizeHints hints;
331     gint i;
332
333     if (self->mapped) return;
334
335     /* set the focused button (if not found then the first button is used) */
336     self->focus = &self->button[0];
337     for (i = 0; i < self->n_buttons; ++i)
338         if (self->button[i].result == self->default_result) {
339             self->focus = &self->button[i];
340             break;
341         }
342
343     /* you can't resize the prompt */
344     hints.flags = PMinSize | PMaxSize;
345     hints.min_width = hints.max_width = self->width;
346     hints.min_height = hints.max_height = self->height;
347     XSetWMNormalHints(obt_display, self->super.window, &hints);
348
349     XSetTransientForHint(obt_display, (parent ? parent->window : 0),
350                          self->super.window);
351
352     /* set up the dialog and render it */
353     prompt_layout(self);
354     render_all(self);
355
356     client_manage(self->super.window, self);
357
358     self->mapped = TRUE;
359 }
360
361 void prompt_hide(ObPrompt *self)
362 {
363     XUnmapWindow(obt_display, self->super.window);
364     self->mapped = FALSE;
365 }
366
367 void prompt_key_event(ObPrompt *self, XEvent *e)
368 {
369     gboolean shift;
370     guint shift_mask;
371
372     if (e->type != KeyPress) return;
373
374     shift_mask = obt_keyboard_modkey_to_modmask(OBT_KEYBOARD_MODKEY_SHIFT);
375     shift = !!(e->xkey.state & shift_mask);
376
377     /* only accept shift */
378     if (e->xkey.state != 0 && e->xkey.state != shift_mask)
379         return;
380
381     if (e->xkey.keycode == ob_keycode(OB_KEY_ESCAPE))
382         prompt_cancel(self);
383     else if (e->xkey.keycode == ob_keycode(OB_KEY_RETURN)) {
384         if (self->func) self->func(self, self->focus->result, self->data);
385         prompt_hide(self);
386     }
387     else if (e->xkey.keycode == ob_keycode(OB_KEY_TAB)) {
388         gint i;
389         ObPromptElement *oldfocus;
390
391         oldfocus = self->focus;
392
393         for (i = 0; i < self->n_buttons; ++i)
394             if (self->focus == &self->button[i]) break;
395         i += (shift ? -1 : 1);
396         if (i < 0) i = self->n_buttons - 1;
397         else if (i >= self->n_buttons) i = 0;
398         self->focus = &self->button[i];
399
400         if (oldfocus != self->focus) render_button(self, oldfocus);
401         render_button(self, self->focus);
402     }
403 }
404
405 void prompt_mouse_event(ObPrompt *self, XEvent *e)
406 {
407     gint i;
408     ObPromptElement *but;
409
410     if (e->type != ButtonPress && e->type != ButtonRelease &&
411         e->type != MotionNotify) return;        
412
413     /* find the button */
414     for (i = 0; i < self->n_buttons; ++i)
415         if (self->button[i].window ==
416             (e->type == MotionNotify ? e->xmotion.window : e->xbutton.window))
417         {
418             but = &self->button[i];
419             break;
420         }
421     g_assert(but != NULL);
422
423     if (e->type == ButtonPress) {
424         ObPromptElement *oldfocus;
425
426         oldfocus = self->focus;
427
428         but->pressed = TRUE;
429         self->focus = but;
430
431         if (oldfocus != but) render_button(self, oldfocus);
432         render_button(self, but);
433     }
434     else if (e->type == ButtonRelease) {
435         if (but->pressed) {
436             if (self->func) self->func(self, but->result, self->data);
437             prompt_hide(self);
438         }
439     }
440     else if (e->type == MotionNotify) {
441         gboolean press;
442
443         press = (e->xmotion.x >= 0 && e->xmotion.y >= 0 &&
444                  e->xmotion.x < but->width && e->xmotion.y < but->height);
445
446         if (press != but->pressed) {
447             but->pressed = press;
448             render_button(self, but);
449         }
450     }
451 }
452
453 void prompt_cancel(ObPrompt *self)
454 {
455     if (self->func) self->func(self, self->cancel_result, self->data);
456     prompt_hide(self);
457 }