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