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