]> icculus.org git repositories - dana/openbox.git/blob - openbox/prompt.c
Merge branch 'wip/alttab'
[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 "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_button;
33 static RrAppearance *prompt_a_focus;
34 static RrAppearance *prompt_a_press;
35 /* we change the max width which would screw with others */
36 static RrAppearance *prompt_a_msg;
37
38 static void prompt_layout(ObPrompt *self);
39 static void render_all(ObPrompt *self);
40 static void render_button(ObPrompt *self, ObPromptElement *e);
41
42 void prompt_startup(gboolean reconfig)
43 {
44     RrColor *c_button, *c_focus, *c_press;
45
46     prompt_a_button = RrAppearanceCopy(ob_rr_theme->a_focused_unpressed_close);
47     prompt_a_focus = RrAppearanceCopy(ob_rr_theme->a_hover_focused_close);
48     prompt_a_press = RrAppearanceCopy(ob_rr_theme->a_focused_pressed_close);
49
50     c_button = prompt_a_button->texture[0].data.mask.color;
51     c_focus = prompt_a_focus->texture[0].data.mask.color;
52     c_press = prompt_a_press->texture[0].data.mask.color;
53
54     RrAppearanceRemoveTextures(prompt_a_button);
55     RrAppearanceRemoveTextures(prompt_a_focus);
56     RrAppearanceRemoveTextures(prompt_a_press);
57
58     RrAppearanceAddTextures(prompt_a_button, 1);
59     RrAppearanceAddTextures(prompt_a_focus, 1);
60     RrAppearanceAddTextures(prompt_a_press, 1);
61
62     /* totally cheating here.. */
63     prompt_a_button->texture[0] = ob_rr_theme->osd_hilite_label->texture[0];
64     prompt_a_focus->texture[0] = ob_rr_theme->osd_hilite_label->texture[0];
65     prompt_a_press->texture[0] = ob_rr_theme->osd_hilite_label->texture[0];
66
67     prompt_a_button->texture[0].data.text.justify = RR_JUSTIFY_CENTER;
68     prompt_a_focus->texture[0].data.text.justify = RR_JUSTIFY_CENTER;
69     prompt_a_press->texture[0].data.text.justify = RR_JUSTIFY_CENTER;
70
71     prompt_a_button->texture[0].data.text.color = c_button;
72     prompt_a_focus->texture[0].data.text.color = c_focus;
73     prompt_a_press->texture[0].data.text.color = c_press;
74
75     prompt_a_msg = RrAppearanceCopy(ob_rr_theme->osd_hilite_label);
76     prompt_a_msg->texture[0].data.text.flow = TRUE;
77
78     if (reconfig) {
79         GList *it;
80         for (it = prompt_list; it; it = g_list_next(it)) {
81             ObPrompt *p = it->data;
82             prompt_layout(p);
83             render_all(p);
84         }
85     }
86 }
87
88 void prompt_shutdown(gboolean reconfig)
89 {
90     RrAppearanceFree(prompt_a_button);
91     RrAppearanceFree(prompt_a_focus);
92     RrAppearanceFree(prompt_a_press);
93     RrAppearanceFree(prompt_a_msg);
94 }
95
96 ObPrompt* prompt_new(const gchar *msg,
97                      const ObPromptAnswer *answers, gint n_answers,
98                      gint default_result, gint cancel_result,
99                      ObPromptCallback func, gpointer data)
100 {
101     ObPrompt *self;
102     XSetWindowAttributes attrib;
103     gint i;
104
105     attrib.override_redirect = FALSE;
106     attrib.border_pixel = RrColorPixel(ob_rr_theme->osd_border_color);
107
108     self = g_new0(ObPrompt, 1);
109     self->ref = 1;
110     self->func = func;
111     self->data = data;
112     self->default_result = default_result;
113     self->cancel_result = cancel_result;
114     self->super.type = OB_WINDOW_CLASS_PROMPT;
115     self->super.window = XCreateWindow(obt_display, obt_root(ob_screen),
116                                        0, 0, 1, 1, ob_rr_theme->obwidth,
117                                        CopyFromParent, InputOutput,
118                                        CopyFromParent,
119                                        CWOverrideRedirect | CWBorderPixel,
120                                        &attrib);
121
122     /* make it a dialog type window */
123     OBT_PROP_SET32(self->super.window, NET_WM_WINDOW_TYPE, ATOM,
124                    OBT_PROP_ATOM(NET_WM_WINDOW_TYPE_DIALOG));
125
126     /* listen for key presses on the window */
127     self->event_mask = KeyPressMask;
128
129     /* we make a copy of this appearance for each prompt */
130     self->a_bg = RrAppearanceCopy(ob_rr_theme->osd_hilite_bg);
131
132     /* set up the text message widow */
133     self->msg.text = g_strdup(msg);
134     self->msg.window = XCreateWindow(obt_display, self->super.window,
135                                      0, 0, 1, 1, 0,
136                                      CopyFromParent, InputOutput,
137                                      CopyFromParent, 0, NULL);
138     XMapWindow(obt_display, self->msg.window);
139
140     /* set up the buttons from the answers */
141
142     self->n_buttons = n_answers;
143     if (!self->n_buttons)
144         self->n_buttons = 1;
145
146     self->button = g_new0(ObPromptElement, self->n_buttons);
147
148     if (n_answers == 0) {
149         g_assert(self->n_buttons == 1); /* should be set to this above.. */
150         self->button[0].text = g_strdup(_("OK"));
151     }
152     else {
153         g_assert(self->n_buttons > 0);
154         for (i = 0; i < self->n_buttons; ++i) {
155             self->button[i].text = g_strdup(answers[i].text);
156             self->button[i].result = answers[i].result;
157         }
158     }
159
160     for (i = 0; i < self->n_buttons; ++i) {
161         self->button[i].window = XCreateWindow(obt_display, self->super.window,
162                                                0, 0, 1, 1, 0,
163                                                CopyFromParent, InputOutput,
164                                                CopyFromParent, 0, NULL);
165         XMapWindow(obt_display, self->button[i].window);
166         window_add(&self->button[i].window, PROMPT_AS_WINDOW(self));
167
168         /* listen for button presses on the buttons */
169         XSelectInput(obt_display, self->button[i].window,
170                      ButtonPressMask | ButtonReleaseMask | ButtonMotionMask);
171     }
172
173     prompt_list = g_list_prepend(prompt_list, self);
174
175     return self;
176 }
177
178 void prompt_ref(ObPrompt *self)
179 {
180     ++self->ref;
181 }
182
183 void prompt_unref(ObPrompt *self)
184 {
185     if (self && --self->ref == 0) {
186         gint i;
187
188         prompt_list = g_list_remove(prompt_list, self);
189
190         for (i = 0; i < self->n_buttons; ++i) {
191             window_remove(self->button[i].window);
192             XDestroyWindow(obt_display, self->button[i].window);
193         }
194
195         XDestroyWindow(obt_display, self->msg.window);
196
197         RrAppearanceFree(self->a_bg);
198
199         XDestroyWindow(obt_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(self->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     self->width = w + l + r;
282     self->height = h + t + b;
283
284     /* move and resize the actual windows */
285     XResizeWindow(obt_display, self->super.window, self->width, self->height);
286     XMoveResizeWindow(obt_display, self->msg.window,
287                       self->msg.x, self->msg.y,
288                       self->msg.width, self->msg.height);
289     for (i = 0; i < self->n_buttons; ++i)
290         XMoveResizeWindow(obt_display, self->button[i].window,
291                           self->button[i].x, self->button[i].y,
292                           self->button[i].width, self->button[i].height);
293 }
294
295 static void render_button(ObPrompt *self, ObPromptElement *e)
296 {
297     RrAppearance *a;
298
299     if (e->pressed) a = prompt_a_press;
300     else if (self->focus == e) a = prompt_a_focus;
301     else a = prompt_a_button;
302
303     a->surface.parent = self->a_bg;
304     a->surface.parentx = e->x;
305     a->surface.parentx = e->y;
306
307     a->texture[0].data.text.string = e->text;
308     RrPaint(a, e->window, e->width, e->height);
309 }
310
311 static void render_all(ObPrompt *self)
312 {
313     gint i;
314
315     RrPaint(self->a_bg, self->super.window, self->width, self->height);
316
317     prompt_a_msg->surface.parent = self->a_bg;
318     prompt_a_msg->surface.parentx = self->msg.x;
319     prompt_a_msg->surface.parenty = self->msg.y;
320
321     prompt_a_msg->texture[0].data.text.string = self->msg.text;
322     prompt_a_msg->texture[0].data.text.maxwidth = self->msg_wbound;
323     RrPaint(prompt_a_msg, self->msg.window, self->msg.width, self->msg.height);
324
325     for (i = 0; i < self->n_buttons; ++i)
326         render_button(self, &self->button[i]);
327 }
328
329 void prompt_show(ObPrompt *self, ObClient *parent)
330 {
331     XSizeHints hints;
332     gint i;
333
334     if (self->mapped) {
335         /* activate the prompt */
336         OBT_PROP_MSG(ob_screen, self->super.window, NET_ACTIVE_WINDOW,
337                      1, /* from an application.. */
338                      event_curtime,
339                      0,
340                      0, 0);
341         return;
342     }
343
344     /* set the focused button (if not found then the first button is used) */
345     self->focus = &self->button[0];
346     for (i = 0; i < self->n_buttons; ++i)
347         if (self->button[i].result == self->default_result) {
348             self->focus = &self->button[i];
349             break;
350         }
351
352     /* you can't resize the prompt */
353     hints.flags = PMinSize | PMaxSize;
354     hints.min_width = hints.max_width = self->width;
355     hints.min_height = hints.max_height = self->height;
356     XSetWMNormalHints(obt_display, self->super.window, &hints);
357
358     XSetTransientForHint(obt_display, self->super.window,
359                          (parent ? parent->window : 0));
360
361     /* set up the dialog and render it */
362     prompt_layout(self);
363     render_all(self);
364
365     client_manage(self->super.window, self);
366
367     self->mapped = TRUE;
368 }
369
370 void prompt_hide(ObPrompt *self)
371 {
372     XUnmapWindow(obt_display, self->super.window);
373     self->mapped = FALSE;
374 }
375
376 gboolean prompt_key_event(ObPrompt *self, XEvent *e)
377 {
378     gboolean shift;
379     guint shift_mask;
380
381     if (e->type != KeyPress) return FALSE;
382
383     shift_mask = obt_keyboard_modkey_to_modmask(OBT_KEYBOARD_MODKEY_SHIFT);
384     shift = !!(e->xkey.state & shift_mask);
385
386     /* only accept shift */
387     if (e->xkey.state != 0 && e->xkey.state != shift_mask)
388         return FALSE;
389
390     if (e->xkey.keycode == ob_keycode(OB_KEY_ESCAPE))
391         prompt_cancel(self);
392     else if (e->xkey.keycode == ob_keycode(OB_KEY_RETURN) ||
393              e->xkey.keycode == ob_keycode(OB_KEY_SPACE))
394     {
395         if (self->func) self->func(self, self->focus->result, self->data);
396         prompt_hide(self);
397     }
398     else if (e->xkey.keycode == ob_keycode(OB_KEY_TAB) ||
399              e->xkey.keycode == ob_keycode(OB_KEY_LEFT) ||
400              e->xkey.keycode == ob_keycode(OB_KEY_RIGHT))
401     {
402         gint i;
403         gboolean left;
404         ObPromptElement *oldfocus;
405
406         left = e->xkey.keycode == ob_keycode(OB_KEY_LEFT) ||
407             (e->xkey.keycode == ob_keycode(OB_KEY_TAB) && shift);
408         oldfocus = self->focus;
409
410         for (i = 0; i < self->n_buttons; ++i)
411             if (self->focus == &self->button[i]) break;
412         i += (left ? -1 : 1);
413         if (i < 0) i = self->n_buttons - 1;
414         else if (i >= self->n_buttons) i = 0;
415         self->focus = &self->button[i];
416
417         if (oldfocus != self->focus) render_button(self, oldfocus);
418         render_button(self, self->focus);
419     }
420     return TRUE;
421 }
422
423 gboolean prompt_mouse_event(ObPrompt *self, XEvent *e)
424 {
425     gint i;
426     ObPromptElement *but;
427
428     if (e->type != ButtonPress && e->type != ButtonRelease &&
429         e->type != MotionNotify) return FALSE;
430
431     /* find the button */
432     but = NULL;
433     for (i = 0; i < self->n_buttons; ++i)
434         if (self->button[i].window ==
435             (e->type == MotionNotify ? e->xmotion.window : e->xbutton.window))
436         {
437             but = &self->button[i];
438             break;
439         }
440     if (!but) return FALSE;
441
442     if (e->type == ButtonPress) {
443         ObPromptElement *oldfocus;
444
445         oldfocus = self->focus;
446
447         but->pressed = TRUE;
448         self->focus = but;
449
450         if (oldfocus != but) render_button(self, oldfocus);
451         render_button(self, but);
452     }
453     else if (e->type == ButtonRelease) {
454         if (but->pressed) {
455             if (self->func) self->func(self, but->result, self->data);
456             prompt_hide(self);
457         }
458     }
459     else if (e->type == MotionNotify) {
460         gboolean press;
461
462         press = (e->xmotion.x >= 0 && e->xmotion.y >= 0 &&
463                  e->xmotion.x < but->width && e->xmotion.y < but->height);
464
465         if (press != but->pressed) {
466             but->pressed = press;
467             render_button(self, but);
468         }
469     }
470     return TRUE;
471 }
472
473 void prompt_cancel(ObPrompt *self)
474 {
475     if (self->func) self->func(self, self->cancel_result, self->data);
476     prompt_hide(self);
477 }