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