]> icculus.org git repositories - dana/openbox.git/blob - openbox/prompt.c
position the buttons in the bottom right instead of centering them
[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/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, const gchar *const *answers)
96 {
97     ObPrompt *self;
98     XSetWindowAttributes attrib;
99     guint i;
100     const gchar *const *c;
101
102     attrib.override_redirect = FALSE;
103     attrib.border_pixel = RrColorPixel(ob_rr_theme->osd_border_color);
104
105     self = g_new0(ObPrompt, 1);
106     self->ref = 1;
107     self->super.type = OB_WINDOW_CLASS_PROMPT;
108     self->super.window = XCreateWindow(obt_display, obt_root(ob_screen),
109                                        0, 0, 1, 1, ob_rr_theme->obwidth,
110                                        CopyFromParent, InputOutput,
111                                        CopyFromParent,
112                                        CWOverrideRedirect | CWBorderPixel,
113                                        &attrib);
114
115     /* make it a dialog type window */
116     OBT_PROP_SET32(self->super.window, NET_WM_WINDOW_TYPE, ATOM,
117                    OBT_PROP_ATOM(NET_WM_WINDOW_TYPE_DIALOG));
118
119     /* listen for key presses on the window */
120     self->event_mask = KeyPressMask;
121
122     self->a_bg = RrAppearanceCopy(ob_rr_theme->osd_hilite_bg);
123
124     self->msg.text = g_strdup(msg);
125     self->msg.window = XCreateWindow(obt_display, self->super.window,
126                                      0, 0, 1, 1, 0,
127                                      CopyFromParent, InputOutput,
128                                      CopyFromParent, 0, NULL);
129     XMapWindow(obt_display, self->msg.window);
130
131     self->n_buttons = 0;
132     for (c = answers; *c != NULL; ++c)
133         ++self->n_buttons;
134
135     if (!self->n_buttons)
136         self->n_buttons = 1;
137
138     self->button = g_new(ObPromptElement, self->n_buttons);
139
140     if (!answers) {
141         g_assert(self->n_buttons == 1); /* should be set to this above.. */
142         self->button[0].text = g_strdup(_("OK"));
143     }
144     else {
145         g_assert(self->n_buttons > 0);
146         for (i = 0; i < self->n_buttons; ++i)
147             self->button[i].text = g_strdup(answers[i]);
148     }
149
150     for (i = 0; i < self->n_buttons; ++i) {
151         self->button[i].window = XCreateWindow(obt_display, self->super.window,
152                                                0, 0, 1, 1, 0,
153                                                CopyFromParent, InputOutput,
154                                                CopyFromParent, 0, NULL);
155         XMapWindow(obt_display, self->button[i].window);
156         window_add(&self->button[i].window, PROMPT_AS_WINDOW(self));
157
158         /* listen for button presses on the buttons */
159         XSelectInput(obt_display, self->button[i].window,
160                      ButtonPressMask | ButtonReleaseMask | ButtonMotionMask);
161     }
162
163     /* set the focus to the first button */
164     self->focus = &self->button[0];
165
166     prompt_list = g_list_prepend(prompt_list, self);
167
168     return self;
169 }
170
171 void prompt_ref(ObPrompt *self)
172 {
173     ++self->ref;
174 }
175
176 void prompt_unref(ObPrompt *self)
177 {
178     if (self && --self->ref == 0) {
179         guint i;
180
181         prompt_list = g_list_remove(prompt_list, self);
182
183         for (i = 0; i < self->n_buttons; ++i) {
184             window_remove(self->button[i].window);
185             XDestroyWindow(obt_display, self->button[i].window);
186         }
187
188         XDestroyWindow(obt_display, self->msg.window);
189
190         RrAppearanceFree(self->a_bg);
191
192         XDestroyWindow(obt_display, self->super.window);
193         g_free(self);
194     }
195 }
196
197 static void prompt_layout(ObPrompt *self)
198 {
199     gint l, r, t, b;
200     guint i;
201     gint allbuttonsw, allbuttonsh, buttonx;
202     gint w, h;
203     gint maxw;
204
205     const gint OUTSIDE_MARGIN = 4;
206     const gint MSG_BUTTON_SEPARATION = 4;
207     const gint BUTTON_SEPARATION = 4;
208     const gint BUTTON_VMARGIN = 4;
209     const gint BUTTON_HMARGIN = 12;
210     const gint MAX_WIDTH = 600;
211
212     RrMargins(self->a_bg, &l, &t, &r, &b);
213     l += OUTSIDE_MARGIN;
214     t += OUTSIDE_MARGIN;
215     r += OUTSIDE_MARGIN;
216     b += OUTSIDE_MARGIN;
217
218     {
219         Rect *area = screen_physical_area_all_monitors();
220         maxw = MIN(MAX_WIDTH, area->width*4/5);
221         g_free(area);
222     }
223
224     /* find the button sizes and how much space we need for them */
225     allbuttonsw = allbuttonsh = 0;
226     for (i = 0; i < self->n_buttons; ++i) {
227         gint bw, bh;
228
229         prompt_a_button->texture[0].data.text.string = self->button[i].text;
230         prompt_a_focus->texture[0].data.text.string = self->button[i].text;
231         prompt_a_press->texture[0].data.text.string = self->button[i].text;
232         RrMinSize(prompt_a_button, &bw, &bh);
233         self->button[i].width = bw;
234         self->button[i].height = bh;
235         RrMinSize(prompt_a_focus, &bw, &bh);
236         self->button[i].width = MAX(self->button[i].width, bw);
237         self->button[i].height = MAX(self->button[i].height, bh);
238         RrMinSize(prompt_a_press, &bw, &bh);
239         self->button[i].width = MAX(self->button[i].width, bw);
240         self->button[i].height = MAX(self->button[i].height, bh);
241
242         self->button[i].width += BUTTON_HMARGIN * 2;
243         self->button[i].height += BUTTON_VMARGIN * 2;
244
245         allbuttonsw += self->button[i].width + (i > 0 ? BUTTON_SEPARATION : 0);
246         allbuttonsh = MAX(allbuttonsh, self->button[i].height);
247     }
248
249     self->msg_wbound = MAX(allbuttonsw, maxw);
250
251     /* measure the text message area */
252     prompt_a_msg->texture[0].data.text.string = self->msg.text;
253     prompt_a_msg->texture[0].data.text.maxwidth = self->msg_wbound;
254     RrMinSize(prompt_a_msg, &self->msg.width, &self->msg.height);
255
256     /* width and height inside the outer margins */
257     w = MAX(self->msg.width, allbuttonsw);
258     h = self->msg.height + MSG_BUTTON_SEPARATION + allbuttonsh;
259
260     /* position the text message */
261     self->msg.x = l + (w - self->msg.width) / 2;
262     self->msg.y = t;
263
264     /* position the button buttons on the right of the dialog */
265     buttonx = l + w;
266     for (i = self->n_buttons; i > 0; --i) {
267         self->button[i-1].x = buttonx - self->button[i-1].width;
268         buttonx -= self->button[i-1].width + BUTTON_SEPARATION;
269         self->button[i-1].y = t + h - allbuttonsh;
270         self->button[i-1].y += (allbuttonsh - self->button[i-1].height) / 2;
271     }
272
273     /* size and position the toplevel window */
274     self->width = w + l + r;
275     self->height = h + t + b;
276
277     /* move and resize the actual windows */
278     XResizeWindow(obt_display, self->super.window, self->width, self->height);
279     XMoveResizeWindow(obt_display, self->msg.window,
280                       self->msg.x, self->msg.y,
281                       self->msg.width, self->msg.height);
282     for (i = 0; i < self->n_buttons; ++i)
283         XMoveResizeWindow(obt_display, self->button[i].window,
284                           self->button[i].x, self->button[i].y,
285                           self->button[i].width, self->button[i].height);
286 }
287
288 static void render_button(ObPrompt *self, ObPromptElement *e)
289 {
290     RrAppearance *a;
291
292     if (e->pressed) a = prompt_a_press;
293     else if (self->focus == e) a = prompt_a_focus;
294     else a = prompt_a_button;
295
296     a->surface.parent = self->a_bg;
297     a->surface.parentx = e->x;
298     a->surface.parentx = e->y;
299
300     a->texture[0].data.text.string = e->text;
301     RrPaint(a, e->window, e->width, e->height);
302 }
303
304 static void render_all(ObPrompt *self)
305 {
306     guint i;
307
308     RrPaint(self->a_bg, self->super.window, self->width, self->height);
309
310     prompt_a_msg->surface.parent = self->a_bg;
311     prompt_a_msg->surface.parentx = self->msg.x;
312     prompt_a_msg->surface.parenty = self->msg.y;
313
314     prompt_a_msg->texture[0].data.text.string = self->msg.text;
315     prompt_a_msg->texture[0].data.text.maxwidth = self->msg_wbound;
316     RrPaint(prompt_a_msg, self->msg.window, self->msg.width, self->msg.height);
317
318     for (i = 0; i < self->n_buttons; ++i)
319         render_button(self, &self->button[i]);
320 }
321
322 void prompt_show(ObPrompt *self, ObClient *parent)
323 {
324     XSizeHints hints;
325
326     if (self->mapped) return;
327
328     prompt_layout(self);
329     render_all(self);
330
331     /* you can't resize the prompt */
332     hints.flags = PMinSize | PMaxSize;
333     hints.min_width = hints.max_width = self->width;
334     hints.min_height = hints.max_height = self->height;
335     XSetWMNormalHints(obt_display, self->super.window, &hints);
336
337     XSetTransientForHint(obt_display, (parent ? parent->window : 0),
338                          self->super.window);
339
340     client_manage(self->super.window, self);
341
342     self->mapped = TRUE;
343 }
344
345 void prompt_hide(ObPrompt *self)
346 {
347     XUnmapWindow(obt_display, self->super.window);
348     self->mapped = FALSE;
349 }
350
351 void prompt_key_event(ObPrompt *self, XEvent *e)
352 {
353     gboolean shift;
354     guint shift_mask;
355
356     if (e->type != KeyPress) return;
357
358     shift_mask = obt_keyboard_modkey_to_modmask(OBT_KEYBOARD_MODKEY_SHIFT);
359     shift = !!(e->xkey.state & shift_mask);
360
361     /* only accept shift */
362     if (e->xkey.state != 0 && e->xkey.state != shift_mask)
363         return;
364
365     if (e->xkey.keycode == ob_keycode(OB_KEY_ESCAPE))
366         prompt_hide(self);
367     else if (e->xkey.keycode == ob_keycode(OB_KEY_RETURN)) {
368         /* XXX run stuff */
369         prompt_hide(self);
370     }
371     else if (e->xkey.keycode == ob_keycode(OB_KEY_TAB)) {
372         guint i;
373         ObPromptElement *oldfocus;
374
375         oldfocus = self->focus;
376
377         for (i = 0; i < self->n_buttons; ++i)
378             if (self->focus == &self->button[i]) break;
379         i += (shift ? -1 : 1);
380         if (i < 0) i = self->n_buttons - 1;
381         else if (i >= self->n_buttons) i = 0;
382         self->focus = &self->button[i];
383
384         if (oldfocus != self->focus) render_button(self, oldfocus);
385         render_button(self, self->focus);
386     }
387 }
388
389 void prompt_mouse_event(ObPrompt *self, XEvent *e)
390 {
391     guint i;
392     ObPromptElement *but;
393
394     if (e->type != ButtonPress && e->type != ButtonRelease &&
395         e->type != MotionNotify) return;        
396
397     /* find the button */
398     for (i = 0; i < self->n_buttons; ++i)
399         if (self->button[i].window ==
400             (e->type == MotionNotify ? e->xmotion.window : e->xbutton.window))
401         {
402             but = &self->button[i];
403             break;
404         }
405     g_assert(but != NULL);
406
407     if (e->type == ButtonPress) {
408         ObPromptElement *oldfocus;
409
410         oldfocus = self->focus;
411
412         but->pressed = TRUE;
413         self->focus = but;
414
415         if (oldfocus != but) render_button(self, oldfocus);
416         render_button(self, but);
417     }
418     else if (e->type == ButtonRelease) {
419         if (but->pressed) {
420             /* XXX run stuff */
421             prompt_hide(self);
422         }
423     }
424     else if (e->type == MotionNotify) {
425         gboolean press;
426
427         press = (e->xmotion.x >= 0 && e->xmotion.y >= 0 &&
428                  e->xmotion.x < but->width && e->xmotion.y < but->height);
429
430         if (press != but->pressed) {
431             but->pressed = press;
432             render_button(self, but);
433         }
434     }
435 }