]> icculus.org git repositories - mikachu/openbox.git/blob - openbox/prompt.c
backport the changes to render/ from the alttab branch (commit 3592046b2b26e05ee94c0d...
[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 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 = Window_Prompt;
148     self->super.window = XCreateWindow(ob_display,
149                                        RootWindow(ob_display, 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     PROP_SET32(self->super.window, net_wm_window_type, atom,
158                prop_atoms.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(ob_display, self->super.window,
166                                      0, 0, 1, 1, 0,
167                                      CopyFromParent, InputOutput,
168                                      CopyFromParent, 0, NULL);
169     XMapWindow(ob_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(ob_display, self->super.window,
193                                                0, 0, 1, 1, 0,
194                                                CopyFromParent, InputOutput,
195                                                CopyFromParent, 0, NULL);
196         XMapWindow(ob_display, self->button[i].window);
197         g_hash_table_insert(window_map, &self->button[i].window,
198                             PROMPT_AS_WINDOW(self));
199
200         /* listen for button presses on the buttons */
201         XSelectInput(ob_display, self->button[i].window,
202                      ButtonPressMask | ButtonReleaseMask | ButtonMotionMask);
203     }
204
205     prompt_list = g_list_prepend(prompt_list, self);
206
207     return self;
208 }
209
210 void prompt_ref(ObPrompt *self)
211 {
212     ++self->ref;
213 }
214
215 void prompt_unref(ObPrompt *self)
216 {
217     if (self && --self->ref == 0) {
218         gint i;
219
220         prompt_list = g_list_remove(prompt_list, self);
221
222         for (i = 0; i < self->n_buttons; ++i) {
223             g_hash_table_remove(window_map, &self->button[i].window);
224             XDestroyWindow(ob_display, self->button[i].window);
225         }
226
227         XDestroyWindow(ob_display, self->msg.window);
228         XDestroyWindow(ob_display, self->super.window);
229         g_free(self);
230     }
231 }
232
233 static void prompt_layout(ObPrompt *self)
234 {
235     gint l, r, t, b;
236     gint i;
237     gint allbuttonsw, allbuttonsh, buttonx;
238     gint w, h;
239     gint maxw;
240
241     RrMargins(prompt_a_bg, &l, &t, &r, &b);
242     l += OUTSIDE_MARGIN;
243     t += OUTSIDE_MARGIN;
244     r += OUTSIDE_MARGIN;
245     b += OUTSIDE_MARGIN;
246
247     {
248         Rect *area = screen_physical_area_all_monitors();
249         maxw = MIN(MAX_WIDTH, area->width*4/5);
250         g_free(area);
251     }
252
253     /* find the button sizes and how much space we need for them */
254     allbuttonsw = allbuttonsh = 0;
255     for (i = 0; i < self->n_buttons; ++i) {
256         gint bw, bh;
257
258         prompt_a_button->texture[0].data.text.string = self->button[i].text;
259         prompt_a_focus->texture[0].data.text.string = self->button[i].text;
260         prompt_a_press->texture[0].data.text.string = self->button[i].text;
261         prompt_a_pfocus->texture[0].data.text.string = self->button[i].text;
262         RrMinSize(prompt_a_button, &bw, &bh);
263         self->button[i].width = bw;
264         self->button[i].height = bh;
265         RrMinSize(prompt_a_focus, &bw, &bh);
266         g_print("button w %d h %d\n", bw, bh);
267         self->button[i].width = MAX(self->button[i].width, bw);
268         self->button[i].height = MAX(self->button[i].height, bh);
269         RrMinSize(prompt_a_press, &bw, &bh);
270         self->button[i].width = MAX(self->button[i].width, bw);
271         self->button[i].height = MAX(self->button[i].height, bh);
272         RrMinSize(prompt_a_pfocus, &bw, &bh);
273         self->button[i].width = MAX(self->button[i].width, bw);
274         self->button[i].height = MAX(self->button[i].height, bh);
275
276
277         self->button[i].width += BUTTON_HMARGIN * 2;
278         self->button[i].height += BUTTON_VMARGIN * 2;
279
280         allbuttonsw += self->button[i].width + (i > 0 ? BUTTON_SEPARATION : 0);
281         allbuttonsh = MAX(allbuttonsh, self->button[i].height);
282     }
283
284     self->msg_wbound = MAX(allbuttonsw, maxw);
285
286     /* measure the text message area */
287     prompt_a_msg->texture[0].data.text.string = self->msg.text;
288     prompt_a_msg->texture[0].data.text.maxwidth = self->msg_wbound;
289     RrMinSize(prompt_a_msg, &self->msg.width, &self->msg.height);
290
291     /* width and height inside the outer margins */
292     w = MAX(self->msg.width, allbuttonsw);
293     h = self->msg.height + MSG_BUTTON_SEPARATION + allbuttonsh;
294
295     /* position the text message */
296     self->msg.x = l + (w - self->msg.width) / 2;
297     self->msg.y = t;
298
299     /* position the button buttons on the right of the dialog */
300     buttonx = l + w;
301     for (i = self->n_buttons - 1; i >= 0; --i) {
302         self->button[i].x = buttonx - self->button[i].width;
303         buttonx -= self->button[i].width + BUTTON_SEPARATION;
304         self->button[i].y = t + h - allbuttonsh;
305         self->button[i].y += (allbuttonsh - self->button[i].height) / 2;
306     }
307
308     /* size and position the toplevel window */
309     prompt_resize(self, w + l + r, h + t + b);
310
311     /* move and resize the internal windows */
312     XMoveResizeWindow(ob_display, self->msg.window,
313                       self->msg.x, self->msg.y,
314                       self->msg.width, self->msg.height);
315     for (i = 0; i < self->n_buttons; ++i)
316         XMoveResizeWindow(ob_display, self->button[i].window,
317                           self->button[i].x, self->button[i].y,
318                           self->button[i].width, self->button[i].height);
319 }
320
321 static void prompt_resize(ObPrompt *self, gint w, gint h)
322 {
323     XConfigureRequestEvent req;
324     XSizeHints hints;
325
326     self->width = w;
327     self->height = h;
328
329     /* the user can't resize the prompt */
330     hints.flags = PMinSize | PMaxSize;
331     hints.min_width = hints.max_width = w;
332     hints.min_height = hints.max_height = h;
333     XSetWMNormalHints(ob_display, self->super.window, &hints);
334
335     if (self->mapped) {
336         /* send a configure request like a normal client would */
337         req.type = ConfigureRequest;
338         req.display = ob_display;
339         req.parent = RootWindow(ob_display, ob_screen);
340         req.window = self->super.window;
341         req.width = w;
342         req.height = h;
343         req.value_mask = CWWidth | CWHeight;
344         XSendEvent(req.display, req.window, FALSE, StructureNotifyMask,
345                    (XEvent*)&req);
346     }
347     else
348         XResizeWindow(ob_display, self->super.window, w, h);
349 }
350
351 static void setup_button_focus_tex(ObPromptElement *e, RrAppearance *a,
352                                    gboolean on)
353 {
354     gint l, r, t, b;
355
356     if (!on) {
357         gint i;
358
359         for (i = 1; i < 5; ++i)
360             a->texture[i].type = on ? RR_TEXTURE_LINE_ART : RR_TEXTURE_NONE;
361     }
362
363     if (!on) return;
364
365     RrMargins(a, &l, &t, &r, &b);
366     l += MIN(BUTTON_HMARGIN, BUTTON_VMARGIN) / 2;
367     r += MIN(BUTTON_HMARGIN, BUTTON_VMARGIN) / 2;
368     t += MIN(BUTTON_HMARGIN, BUTTON_VMARGIN) / 2;
369     b += MIN(BUTTON_HMARGIN, BUTTON_VMARGIN) / 2;
370
371     /* top line */
372     a->texture[1].data.lineart.x1 = l;
373     a->texture[1].data.lineart.x2 = e->width - r - 1;
374     a->texture[1].data.lineart.y1 = t;
375     a->texture[1].data.lineart.y2 = t;
376
377     /* bottom line */
378     a->texture[2].data.lineart.x1 = l;
379     a->texture[2].data.lineart.x2 = e->width - r - 1;
380     a->texture[2].data.lineart.y1 = e->height - b - 1;
381     a->texture[2].data.lineart.y2 = e->height - b - 1;
382
383     /* left line */
384     a->texture[3].data.lineart.x1 = l;
385     a->texture[3].data.lineart.x2 = l;
386     a->texture[3].data.lineart.y1 = t;
387     a->texture[3].data.lineart.y2 = e->height - b - 1;
388
389     /* right line */
390     a->texture[4].data.lineart.x1 = e->width - r - 1;
391     a->texture[4].data.lineart.x2 = e->width - r - 1;
392     a->texture[4].data.lineart.y1 = t;
393     a->texture[4].data.lineart.y2 = e->height - b - 1;
394
395     g_print("setting x2 %d\n", e->width - r - 1);
396 }
397
398 static void render_button(ObPrompt *self, ObPromptElement *e)
399 {
400     RrAppearance *a;
401
402     if (e->pressed && self->focus == e) a = prompt_a_pfocus;
403     else if (self->focus == e)          a = prompt_a_focus;
404     else if (e->pressed)                a = prompt_a_press;
405     else                                a = prompt_a_button;
406
407     a->surface.parent = prompt_a_bg;
408     a->surface.parentx = e->x;
409     a->surface.parenty = e->y;
410
411     /* draw the keyfocus line */
412     if (a == prompt_a_pfocus || a == prompt_a_focus)
413         setup_button_focus_tex(e, a, TRUE);
414
415     a->texture[0].data.text.string = e->text;
416     RrPaint(a, e->window, e->width, e->height);
417
418     /* turn off the keyfocus line so that it doesn't affect size calculations
419      */
420     if (a == prompt_a_pfocus || a == prompt_a_focus)
421         setup_button_focus_tex(e, a, FALSE);
422 }
423
424 static void render_all(ObPrompt *self)
425 {
426     gint i;
427
428     RrPaint(prompt_a_bg, self->super.window, self->width, self->height);
429
430     prompt_a_msg->surface.parent = prompt_a_bg;
431     prompt_a_msg->surface.parentx = self->msg.x;
432     prompt_a_msg->surface.parenty = self->msg.y;
433
434     prompt_a_msg->texture[0].data.text.string = self->msg.text;
435     prompt_a_msg->texture[0].data.text.maxwidth = self->msg_wbound;
436     RrPaint(prompt_a_msg, self->msg.window, self->msg.width, self->msg.height);
437
438     for (i = 0; i < self->n_buttons; ++i)
439         render_button(self, &self->button[i]);
440 }
441
442 void prompt_show(ObPrompt *self, ObClient *parent)
443 {
444     gint i;
445
446     if (self->mapped) {
447         /* activate the prompt */
448         PROP_MSG(self->super.window, net_active_window,
449                  1, /* from an application.. */
450                  event_curtime,
451                  0,
452                  0);
453         return;
454     }
455
456     /* set the focused button (if not found then the first button is used) */
457     self->focus = &self->button[0];
458     for (i = 0; i < self->n_buttons; ++i)
459         if (self->button[i].result == self->default_result) {
460             self->focus = &self->button[i];
461             break;
462         }
463
464     XSetTransientForHint(ob_display, self->super.window,
465                          (parent ? parent->window : 0));
466
467     /* set up the dialog and render it */
468     prompt_layout(self);
469     render_all(self);
470
471     client_manage(self->super.window, self);
472
473     self->mapped = TRUE;
474 }
475
476 void prompt_hide(ObPrompt *self)
477 {
478     XUnmapWindow(ob_display, self->super.window);
479     self->mapped = FALSE;
480 }
481
482 gboolean prompt_key_event(ObPrompt *self, XEvent *e)
483 {
484     gboolean shift;
485     guint shift_mask;
486
487     if (e->type != KeyPress) return FALSE;
488
489     g_print("key 0x%x 0x%x\n", e->xkey.keycode, ob_keycode(OB_KEY_TAB));
490
491     shift_mask = modkeys_key_to_mask(OB_MODKEY_KEY_SHIFT);
492     shift = !!(e->xkey.state & shift_mask);
493
494     /* only accept shift */
495     if (e->xkey.state != 0 && e->xkey.state != shift_mask)
496         return FALSE;
497
498     if (e->xkey.keycode == ob_keycode(OB_KEY_ESCAPE))
499         prompt_cancel(self);
500     else if (e->xkey.keycode == ob_keycode(OB_KEY_RETURN) ||
501              e->xkey.keycode == ob_keycode(OB_KEY_SPACE))
502     {
503         if (self->func) self->func(self, self->focus->result, self->data);
504         prompt_hide(self);
505     }
506     else if (e->xkey.keycode == ob_keycode(OB_KEY_TAB) ||
507              e->xkey.keycode == ob_keycode(OB_KEY_LEFT) ||
508              e->xkey.keycode == ob_keycode(OB_KEY_RIGHT))
509     {
510         gint i;
511         gboolean left;
512         ObPromptElement *oldfocus;
513
514         left = e->xkey.keycode == ob_keycode(OB_KEY_LEFT) ||
515             (e->xkey.keycode == ob_keycode(OB_KEY_TAB) && shift);
516         oldfocus = self->focus;
517
518         for (i = 0; i < self->n_buttons; ++i)
519             if (self->focus == &self->button[i]) break;
520         i += (left ? -1 : 1);
521         if (i < 0) i = self->n_buttons - 1;
522         else if (i >= self->n_buttons) i = 0;
523         self->focus = &self->button[i];
524
525         if (oldfocus != self->focus) render_button(self, oldfocus);
526         render_button(self, self->focus);
527     }
528     return TRUE;
529 }
530
531 gboolean prompt_mouse_event(ObPrompt *self, XEvent *e)
532 {
533     gint i;
534     ObPromptElement *but;
535
536     if (e->type != ButtonPress && e->type != ButtonRelease &&
537         e->type != MotionNotify) return FALSE;
538
539     /* find the button */
540     but = NULL;
541     for (i = 0; i < self->n_buttons; ++i)
542         if (self->button[i].window ==
543             (e->type == MotionNotify ? e->xmotion.window : e->xbutton.window))
544         {
545             but = &self->button[i];
546             break;
547         }
548     if (!but) return FALSE;
549
550     if (e->type == ButtonPress) {
551         ObPromptElement *oldfocus;
552
553         oldfocus = self->focus;
554
555         but->pressed = TRUE;
556         self->focus = but;
557
558         if (oldfocus != but) render_button(self, oldfocus);
559         render_button(self, but);
560     }
561     else if (e->type == ButtonRelease) {
562         if (but->pressed) {
563             if (self->func) self->func(self, but->result, self->data);
564             prompt_hide(self);
565         }
566     }
567     else if (e->type == MotionNotify) {
568         gboolean press;
569
570         press = (e->xmotion.x >= 0 && e->xmotion.y >= 0 &&
571                  e->xmotion.x < but->width && e->xmotion.y < but->height);
572
573         if (press != but->pressed) {
574             but->pressed = press;
575             render_button(self, but);
576         }
577     }
578     return TRUE;
579 }
580
581 void prompt_cancel(ObPrompt *self)
582 {
583     if (self->func) self->func(self, self->cancel_result, self->data);
584     prompt_hide(self);
585 }