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 "openbox.h"
23 #include "client.h"
24 #include "prop.h"
25 #include "modkeys.h"
26 #include "event.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 = Window_Prompt;
119     self->super.window = XCreateWindow(ob_display,
120                                        RootWindow(ob_display, ob_screen),
121                                        0, 0, 1, 1, 0,
122                                        CopyFromParent, InputOutput,
123                                        CopyFromParent,
124                                        CWOverrideRedirect,
125                                        &attrib);
126
127     /* make it a dialog type window */
128     PROP_SET32(self->super.window, net_wm_window_type, atom,
129                prop_atoms.net_wm_window_type_dialog);
130
131     /* listen for key presses on the window */
132     self->event_mask = KeyPressMask;
133
134     /* set up the text message widow */
135     self->msg.text = g_strdup(msg);
136     self->msg.window = XCreateWindow(ob_display, self->super.window,
137                                      0, 0, 1, 1, 0,
138                                      CopyFromParent, InputOutput,
139                                      CopyFromParent, 0, NULL);
140     XMapWindow(ob_display, self->msg.window);
141
142     /* set up the buttons from the answers */
143
144     self->n_buttons = n_answers;
145     if (!self->n_buttons)
146         self->n_buttons = 1;
147
148     self->button = g_new0(ObPromptElement, self->n_buttons);
149
150     if (n_answers == 0) {
151         g_assert(self->n_buttons == 1); /* should be set to this above.. */
152         self->button[0].text = g_strdup(_("OK"));
153     }
154     else {
155         g_assert(self->n_buttons > 0);
156         for (i = 0; i < self->n_buttons; ++i) {
157             self->button[i].text = g_strdup(answers[i].text);
158             self->button[i].result = answers[i].result;
159         }
160     }
161
162     for (i = 0; i < self->n_buttons; ++i) {
163         self->button[i].window = XCreateWindow(ob_display, self->super.window,
164                                                0, 0, 1, 1, 0,
165                                                CopyFromParent, InputOutput,
166                                                CopyFromParent, 0, NULL);
167         XMapWindow(ob_display, self->button[i].window);
168         g_hash_table_insert(window_map, &self->button[i].window,
169                             PROMPT_AS_WINDOW(self));
170
171         /* listen for button presses on the buttons */
172         XSelectInput(ob_display, self->button[i].window,
173                      ButtonPressMask | ButtonReleaseMask | ButtonMotionMask);
174     }
175
176     prompt_list = g_list_prepend(prompt_list, self);
177
178     return self;
179 }
180
181 void prompt_ref(ObPrompt *self)
182 {
183     ++self->ref;
184 }
185
186 void prompt_unref(ObPrompt *self)
187 {
188     if (self && --self->ref == 0) {
189         gint i;
190
191         prompt_list = g_list_remove(prompt_list, self);
192
193         for (i = 0; i < self->n_buttons; ++i) {
194             g_hash_table_remove(window_map, &self->button[i].window);
195             XDestroyWindow(ob_display, self->button[i].window);
196         }
197
198         XDestroyWindow(ob_display, self->msg.window);
199         XDestroyWindow(ob_display, self->super.window);
200         g_free(self);
201     }
202 }
203
204 static void prompt_layout(ObPrompt *self)
205 {
206     gint l, r, t, b;
207     gint i;
208     gint allbuttonsw, allbuttonsh, buttonx;
209     gint w, h;
210     gint maxw;
211
212     const gint OUTSIDE_MARGIN = 4;
213     const gint MSG_BUTTON_SEPARATION = 4;
214     const gint BUTTON_SEPARATION = 4;
215     const gint BUTTON_VMARGIN = 4;
216     const gint BUTTON_HMARGIN = 12;
217     const gint MAX_WIDTH = 600;
218
219     RrMargins(prompt_a_bg, &l, &t, &r, &b);
220     l += OUTSIDE_MARGIN;
221     t += OUTSIDE_MARGIN;
222     r += OUTSIDE_MARGIN;
223     b += OUTSIDE_MARGIN;
224
225     {
226         Rect *area = screen_physical_area_all_monitors();
227         maxw = MIN(MAX_WIDTH, area->width*4/5);
228         g_free(area);
229     }
230
231     /* find the button sizes and how much space we need for them */
232     allbuttonsw = allbuttonsh = 0;
233     for (i = 0; i < self->n_buttons; ++i) {
234         gint bw, bh;
235
236         prompt_a_button->texture[0].data.text.string = self->button[i].text;
237         prompt_a_focus->texture[0].data.text.string = self->button[i].text;
238         prompt_a_press->texture[0].data.text.string = self->button[i].text;
239         RrMinSize(prompt_a_button, &bw, &bh);
240         self->button[i].width = bw;
241         self->button[i].height = bh;
242         RrMinSize(prompt_a_focus, &bw, &bh);
243         self->button[i].width = MAX(self->button[i].width, bw);
244         self->button[i].height = MAX(self->button[i].height, bh);
245         RrMinSize(prompt_a_press, &bw, &bh);
246         self->button[i].width = MAX(self->button[i].width, bw);
247         self->button[i].height = MAX(self->button[i].height, bh);
248
249         self->button[i].width += BUTTON_HMARGIN * 2;
250         self->button[i].height += BUTTON_VMARGIN * 2;
251
252         allbuttonsw += self->button[i].width + (i > 0 ? BUTTON_SEPARATION : 0);
253         allbuttonsh = MAX(allbuttonsh, self->button[i].height);
254     }
255
256     self->msg_wbound = MAX(allbuttonsw, maxw);
257
258     /* measure the text message area */
259     prompt_a_msg->texture[0].data.text.string = self->msg.text;
260     prompt_a_msg->texture[0].data.text.maxwidth = self->msg_wbound;
261     RrMinSize(prompt_a_msg, &self->msg.width, &self->msg.height);
262
263     /* width and height inside the outer margins */
264     w = MAX(self->msg.width, allbuttonsw);
265     h = self->msg.height + MSG_BUTTON_SEPARATION + allbuttonsh;
266
267     /* position the text message */
268     self->msg.x = l + (w - self->msg.width) / 2;
269     self->msg.y = t;
270
271     /* position the button buttons on the right of the dialog */
272     buttonx = l + w;
273     for (i = self->n_buttons - 1; i >= 0; --i) {
274         self->button[i].x = buttonx - self->button[i].width;
275         buttonx -= self->button[i].width + BUTTON_SEPARATION;
276         self->button[i].y = t + h - allbuttonsh;
277         self->button[i].y += (allbuttonsh - self->button[i].height) / 2;
278     }
279
280     /* size and position the toplevel window */
281     prompt_resize(self, w + l + r, h + t + b);
282
283     /* move and resize the internal windows */
284     XMoveResizeWindow(ob_display, self->msg.window,
285                       self->msg.x, self->msg.y,
286                       self->msg.width, self->msg.height);
287     for (i = 0; i < self->n_buttons; ++i)
288         XMoveResizeWindow(ob_display, self->button[i].window,
289                           self->button[i].x, self->button[i].y,
290                           self->button[i].width, self->button[i].height);
291 }
292
293 static void prompt_resize(ObPrompt *self, gint w, gint h)
294 {
295     XConfigureRequestEvent req;
296     XSizeHints hints;
297
298     self->width = w;
299     self->height = h;
300
301     /* the user can't resize the prompt */
302     hints.flags = PMinSize | PMaxSize;
303     hints.min_width = hints.max_width = w;
304     hints.min_height = hints.max_height = h;
305     XSetWMNormalHints(ob_display, self->super.window, &hints);
306
307     if (self->mapped) {
308         /* send a configure request like a normal client would */
309         req.type = ConfigureRequest;
310         req.display = ob_display;
311         req.parent = RootWindow(ob_display, ob_screen);
312         req.window = self->super.window;
313         req.width = w;
314         req.height = h;
315         req.value_mask = CWWidth | CWHeight;
316         XSendEvent(req.display, req.window, FALSE, StructureNotifyMask,
317                    (XEvent*)&req);
318     }
319     else
320         XResizeWindow(ob_display, self->super.window, w, h);
321 }
322
323 static void render_button(ObPrompt *self, ObPromptElement *e)
324 {
325     RrAppearance *a;
326
327     if (e->pressed) a = prompt_a_press;
328     else if (self->focus == e) a = prompt_a_focus;
329     else a = prompt_a_button;
330
331     a->surface.parent = prompt_a_bg;
332     a->surface.parentx = e->x;
333     a->surface.parentx = e->y;
334
335     a->texture[0].data.text.string = e->text;
336     RrPaint(a, e->window, e->width, e->height);
337 }
338
339 static void render_all(ObPrompt *self)
340 {
341     gint i;
342
343     RrPaint(prompt_a_bg, self->super.window, self->width, self->height);
344
345     prompt_a_msg->surface.parent = prompt_a_bg;
346     prompt_a_msg->surface.parentx = self->msg.x;
347     prompt_a_msg->surface.parenty = self->msg.y;
348
349     prompt_a_msg->texture[0].data.text.string = self->msg.text;
350     prompt_a_msg->texture[0].data.text.maxwidth = self->msg_wbound;
351     RrPaint(prompt_a_msg, self->msg.window, self->msg.width, self->msg.height);
352
353     for (i = 0; i < self->n_buttons; ++i)
354         render_button(self, &self->button[i]);
355 }
356
357 void prompt_show(ObPrompt *self, ObClient *parent)
358 {
359     gint i;
360
361     if (self->mapped) {
362         /* activate the prompt */
363         PROP_MSG(self->super.window, net_active_window,
364                  1, /* from an application.. */
365                  event_curtime,
366                  0,
367                  0);
368         return;
369     }
370
371     /* set the focused button (if not found then the first button is used) */
372     self->focus = &self->button[0];
373     for (i = 0; i < self->n_buttons; ++i)
374         if (self->button[i].result == self->default_result) {
375             self->focus = &self->button[i];
376             break;
377         }
378
379     XSetTransientForHint(ob_display, self->super.window,
380                          (parent ? parent->window : 0));
381
382     /* set up the dialog and render it */
383     prompt_layout(self);
384     render_all(self);
385
386     client_manage(self->super.window, self);
387
388     self->mapped = TRUE;
389 }
390
391 void prompt_hide(ObPrompt *self)
392 {
393     XUnmapWindow(ob_display, self->super.window);
394     self->mapped = FALSE;
395 }
396
397 gboolean prompt_key_event(ObPrompt *self, XEvent *e)
398 {
399     gboolean shift;
400     guint shift_mask;
401
402     if (e->type != KeyPress) return FALSE;
403
404     g_print("key 0x%x 0x%x\n", e->xkey.keycode, ob_keycode(OB_KEY_TAB));
405
406     shift_mask = modkeys_key_to_mask(OB_MODKEY_KEY_SHIFT);
407     shift = !!(e->xkey.state & shift_mask);
408
409     /* only accept shift */
410     if (e->xkey.state != 0 && e->xkey.state != shift_mask)
411         return FALSE;
412
413     if (e->xkey.keycode == ob_keycode(OB_KEY_ESCAPE))
414         prompt_cancel(self);
415     else if (e->xkey.keycode == ob_keycode(OB_KEY_RETURN) ||
416              e->xkey.keycode == ob_keycode(OB_KEY_SPACE))
417     {
418         if (self->func) self->func(self, self->focus->result, self->data);
419         prompt_hide(self);
420     }
421     else if (e->xkey.keycode == ob_keycode(OB_KEY_TAB) ||
422              e->xkey.keycode == ob_keycode(OB_KEY_LEFT) ||
423              e->xkey.keycode == ob_keycode(OB_KEY_RIGHT))
424     {
425         gint i;
426         gboolean left;
427         ObPromptElement *oldfocus;
428
429         left = e->xkey.keycode == ob_keycode(OB_KEY_LEFT) ||
430             (e->xkey.keycode == ob_keycode(OB_KEY_TAB) && shift);
431         oldfocus = self->focus;
432
433         for (i = 0; i < self->n_buttons; ++i)
434             if (self->focus == &self->button[i]) break;
435         i += (left ? -1 : 1);
436         if (i < 0) i = self->n_buttons - 1;
437         else if (i >= self->n_buttons) i = 0;
438         self->focus = &self->button[i];
439
440         if (oldfocus != self->focus) render_button(self, oldfocus);
441         render_button(self, self->focus);
442     }
443     return TRUE;
444 }
445
446 gboolean prompt_mouse_event(ObPrompt *self, XEvent *e)
447 {
448     gint i;
449     ObPromptElement *but;
450
451     if (e->type != ButtonPress && e->type != ButtonRelease &&
452         e->type != MotionNotify) return FALSE;
453
454     /* find the button */
455     but = NULL;
456     for (i = 0; i < self->n_buttons; ++i)
457         if (self->button[i].window ==
458             (e->type == MotionNotify ? e->xmotion.window : e->xbutton.window))
459         {
460             but = &self->button[i];
461             break;
462         }
463     if (!but) return FALSE;
464
465     if (e->type == ButtonPress) {
466         ObPromptElement *oldfocus;
467
468         oldfocus = self->focus;
469
470         but->pressed = TRUE;
471         self->focus = but;
472
473         if (oldfocus != but) render_button(self, oldfocus);
474         render_button(self, but);
475     }
476     else if (e->type == ButtonRelease) {
477         if (but->pressed) {
478             if (self->func) self->func(self, but->result, self->data);
479             prompt_hide(self);
480         }
481     }
482     else if (e->type == MotionNotify) {
483         gboolean press;
484
485         press = (e->xmotion.x >= 0 && e->xmotion.y >= 0 &&
486                  e->xmotion.x < but->width && e->xmotion.y < but->height);
487
488         if (press != but->pressed) {
489             but->pressed = press;
490             render_button(self, but);
491         }
492     }
493     return TRUE;
494 }
495
496 void prompt_cancel(ObPrompt *self)
497 {
498     if (self->func) self->func(self, self->cancel_result, self->data);
499     prompt_hide(self);
500 }