tell what signal is going to be sent to the client when killing through the dialog
[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_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 = Window_Prompt;
115     self->super.window = XCreateWindow(ob_display,
116                                        RootWindow(ob_display, ob_screen),
117                                        0, 0, 1, 1, ob_rr_theme->obwidth,
118                                        CopyFromParent, InputOutput,
119                                        CopyFromParent,
120                                        CWOverrideRedirect | CWBorderPixel,
121                                        &attrib);
122
123     /* make it a dialog type window */
124     PROP_SET32(self->super.window, net_wm_window_type, atom,
125                prop_atoms.net_wm_window_type_dialog);
126
127     /* listen for key presses on the window */
128     self->event_mask = KeyPressMask;
129
130     /* we make a copy of this appearance for each prompt */
131     self->a_bg = RrAppearanceCopy(ob_rr_theme->osd_hilite_bg);
132
133     /* set up the text message widow */
134     self->msg.text = g_strdup(msg);
135     self->msg.window = XCreateWindow(ob_display, self->super.window,
136                                      0, 0, 1, 1, 0,
137                                      CopyFromParent, InputOutput,
138                                      CopyFromParent, 0, NULL);
139     XMapWindow(ob_display, self->msg.window);
140
141     /* set up the buttons from the answers */
142
143     self->n_buttons = n_answers;
144     if (!self->n_buttons)
145         self->n_buttons = 1;
146
147     self->button = g_new0(ObPromptElement, self->n_buttons);
148
149     if (n_answers == 0) {
150         g_assert(self->n_buttons == 1); /* should be set to this above.. */
151         self->button[0].text = g_strdup(_("OK"));
152     }
153     else {
154         g_assert(self->n_buttons > 0);
155         for (i = 0; i < self->n_buttons; ++i) {
156             self->button[i].text = g_strdup(answers[i].text);
157             self->button[i].result = answers[i].result;
158         }
159     }
160
161     for (i = 0; i < self->n_buttons; ++i) {
162         self->button[i].window = XCreateWindow(ob_display, self->super.window,
163                                                0, 0, 1, 1, 0,
164                                                CopyFromParent, InputOutput,
165                                                CopyFromParent, 0, NULL);
166         XMapWindow(ob_display, self->button[i].window);
167         g_hash_table_insert(window_map, &self->button[i].window,
168                             PROMPT_AS_WINDOW(self));
169
170         /* listen for button presses on the buttons */
171         XSelectInput(ob_display, self->button[i].window,
172                      ButtonPressMask | ButtonReleaseMask | ButtonMotionMask);
173     }
174
175     prompt_list = g_list_prepend(prompt_list, self);
176
177     return self;
178 }
179
180 void prompt_ref(ObPrompt *self)
181 {
182     ++self->ref;
183 }
184
185 void prompt_unref(ObPrompt *self)
186 {
187     if (self && --self->ref == 0) {
188         gint i;
189
190         prompt_list = g_list_remove(prompt_list, self);
191
192         for (i = 0; i < self->n_buttons; ++i) {
193             g_hash_table_remove(window_map, &self->button[i].window);
194             XDestroyWindow(ob_display, self->button[i].window);
195         }
196
197         XDestroyWindow(ob_display, self->msg.window);
198
199         RrAppearanceFree(self->a_bg);
200
201         XDestroyWindow(ob_display, self->super.window);
202         g_free(self);
203     }
204 }
205
206 static void prompt_layout(ObPrompt *self)
207 {
208     gint l, r, t, b;
209     gint i;
210     gint allbuttonsw, allbuttonsh, buttonx;
211     gint w, h;
212     gint maxw;
213
214     const gint OUTSIDE_MARGIN = 4;
215     const gint MSG_BUTTON_SEPARATION = 4;
216     const gint BUTTON_SEPARATION = 4;
217     const gint BUTTON_VMARGIN = 4;
218     const gint BUTTON_HMARGIN = 12;
219     const gint MAX_WIDTH = 600;
220
221     RrMargins(self->a_bg, &l, &t, &r, &b);
222     l += OUTSIDE_MARGIN;
223     t += OUTSIDE_MARGIN;
224     r += OUTSIDE_MARGIN;
225     b += OUTSIDE_MARGIN;
226
227     {
228         Rect *area = screen_physical_area_all_monitors();
229         maxw = MIN(MAX_WIDTH, area->width*4/5);
230         g_free(area);
231     }
232
233     /* find the button sizes and how much space we need for them */
234     allbuttonsw = allbuttonsh = 0;
235     for (i = 0; i < self->n_buttons; ++i) {
236         gint bw, bh;
237
238         prompt_a_button->texture[0].data.text.string = self->button[i].text;
239         prompt_a_focus->texture[0].data.text.string = self->button[i].text;
240         prompt_a_press->texture[0].data.text.string = self->button[i].text;
241         RrMinSize(prompt_a_button, &bw, &bh);
242         self->button[i].width = bw;
243         self->button[i].height = bh;
244         RrMinSize(prompt_a_focus, &bw, &bh);
245         self->button[i].width = MAX(self->button[i].width, bw);
246         self->button[i].height = MAX(self->button[i].height, bh);
247         RrMinSize(prompt_a_press, &bw, &bh);
248         self->button[i].width = MAX(self->button[i].width, bw);
249         self->button[i].height = MAX(self->button[i].height, bh);
250
251         self->button[i].width += BUTTON_HMARGIN * 2;
252         self->button[i].height += BUTTON_VMARGIN * 2;
253
254         allbuttonsw += self->button[i].width + (i > 0 ? BUTTON_SEPARATION : 0);
255         allbuttonsh = MAX(allbuttonsh, self->button[i].height);
256     }
257
258     self->msg_wbound = MAX(allbuttonsw, maxw);
259
260     /* measure the text message area */
261     prompt_a_msg->texture[0].data.text.string = self->msg.text;
262     prompt_a_msg->texture[0].data.text.maxwidth = self->msg_wbound;
263     RrMinSize(prompt_a_msg, &self->msg.width, &self->msg.height);
264
265     /* width and height inside the outer margins */
266     w = MAX(self->msg.width, allbuttonsw);
267     h = self->msg.height + MSG_BUTTON_SEPARATION + allbuttonsh;
268
269     /* position the text message */
270     self->msg.x = l + (w - self->msg.width) / 2;
271     self->msg.y = t;
272
273     /* position the button buttons on the right of the dialog */
274     buttonx = l + w;
275     for (i = self->n_buttons - 1; i >= 0; --i) {
276         self->button[i].x = buttonx - self->button[i].width;
277         buttonx -= self->button[i].width + BUTTON_SEPARATION;
278         self->button[i].y = t + h - allbuttonsh;
279         self->button[i].y += (allbuttonsh - self->button[i].height) / 2;
280     }
281
282     /* size and position the toplevel window */
283     self->width = w + l + r;
284     self->height = h + t + b;
285
286     /* move and resize the actual windows */
287     XResizeWindow(ob_display, self->super.window, self->width, self->height);
288     XMoveResizeWindow(ob_display, self->msg.window,
289                       self->msg.x, self->msg.y,
290                       self->msg.width, self->msg.height);
291     for (i = 0; i < self->n_buttons; ++i)
292         XMoveResizeWindow(ob_display, self->button[i].window,
293                           self->button[i].x, self->button[i].y,
294                           self->button[i].width, self->button[i].height);
295 }
296
297 static void render_button(ObPrompt *self, ObPromptElement *e)
298 {
299     RrAppearance *a;
300
301     if (e->pressed) a = prompt_a_press;
302     else if (self->focus == e) a = prompt_a_focus;
303     else a = prompt_a_button;
304
305     a->surface.parent = self->a_bg;
306     a->surface.parentx = e->x;
307     a->surface.parentx = e->y;
308
309     a->texture[0].data.text.string = e->text;
310     RrPaint(a, e->window, e->width, e->height);
311 }
312
313 static void render_all(ObPrompt *self)
314 {
315     gint i;
316
317     RrPaint(self->a_bg, self->super.window, self->width, self->height);
318
319     prompt_a_msg->surface.parent = self->a_bg;
320     prompt_a_msg->surface.parentx = self->msg.x;
321     prompt_a_msg->surface.parenty = self->msg.y;
322
323     prompt_a_msg->texture[0].data.text.string = self->msg.text;
324     prompt_a_msg->texture[0].data.text.maxwidth = self->msg_wbound;
325     RrPaint(prompt_a_msg, self->msg.window, self->msg.width, self->msg.height);
326
327     for (i = 0; i < self->n_buttons; ++i)
328         render_button(self, &self->button[i]);
329 }
330
331 void prompt_show(ObPrompt *self, ObClient *parent)
332 {
333     XSizeHints hints;
334     gint i;
335
336     if (self->mapped) {
337         /* activate the prompt */
338         PROP_MSG(self->super.window, net_active_window,
339                  1, /* from an application.. */
340                  event_curtime,
341                  0,
342                  0);
343         return;
344     }
345
346     /* set the focused button (if not found then the first button is used) */
347     self->focus = &self->button[0];
348     for (i = 0; i < self->n_buttons; ++i)
349         if (self->button[i].result == self->default_result) {
350             self->focus = &self->button[i];
351             break;
352         }
353
354     /* you can't resize the prompt */
355     hints.flags = PMinSize | PMaxSize;
356     hints.min_width = hints.max_width = self->width;
357     hints.min_height = hints.max_height = self->height;
358     XSetWMNormalHints(ob_display, self->super.window, &hints);
359
360     XSetTransientForHint(ob_display, self->super.window,
361                          (parent ? parent->window : 0));
362
363     /* set up the dialog and render it */
364     prompt_layout(self);
365     render_all(self);
366
367     client_manage(self->super.window, self);
368
369     self->mapped = TRUE;
370 }
371
372 void prompt_hide(ObPrompt *self)
373 {
374     XUnmapWindow(ob_display, self->super.window);
375     self->mapped = FALSE;
376 }
377
378 gboolean prompt_key_event(ObPrompt *self, XEvent *e)
379 {
380     gboolean shift;
381     guint shift_mask;
382
383     if (e->type != KeyPress) return FALSE;
384
385     g_print("key 0x%x 0x%x\n", e->xkey.keycode, ob_keycode(OB_KEY_TAB));
386
387     shift_mask = modkeys_key_to_mask(OB_MODKEY_KEY_SHIFT);
388     shift = !!(e->xkey.state & shift_mask);
389
390     /* only accept shift */
391     if (e->xkey.state != 0 && e->xkey.state != shift_mask)
392         return FALSE;
393
394     if (e->xkey.keycode == ob_keycode(OB_KEY_ESCAPE))
395         prompt_cancel(self);
396     else if (e->xkey.keycode == ob_keycode(OB_KEY_RETURN) ||
397              e->xkey.keycode == ob_keycode(OB_KEY_SPACE))
398     {
399         if (self->func) self->func(self, self->focus->result, self->data);
400         prompt_hide(self);
401     }
402     else if (e->xkey.keycode == ob_keycode(OB_KEY_TAB) ||
403              e->xkey.keycode == ob_keycode(OB_KEY_LEFT) ||
404              e->xkey.keycode == ob_keycode(OB_KEY_RIGHT))
405     {
406         gint i;
407         gboolean left;
408         ObPromptElement *oldfocus;
409
410         left = e->xkey.keycode == ob_keycode(OB_KEY_LEFT) ||
411             (e->xkey.keycode == ob_keycode(OB_KEY_TAB) && shift);
412         oldfocus = self->focus;
413
414         for (i = 0; i < self->n_buttons; ++i)
415             if (self->focus == &self->button[i]) break;
416         i += (left ? -1 : 1);
417         if (i < 0) i = self->n_buttons - 1;
418         else if (i >= self->n_buttons) i = 0;
419         self->focus = &self->button[i];
420
421         if (oldfocus != self->focus) render_button(self, oldfocus);
422         render_button(self, self->focus);
423     }
424     return TRUE;
425 }
426
427 gboolean prompt_mouse_event(ObPrompt *self, XEvent *e)
428 {
429     gint i;
430     ObPromptElement *but;
431
432     if (e->type != ButtonPress && e->type != ButtonRelease &&
433         e->type != MotionNotify) return FALSE;
434
435     /* find the button */
436     but = NULL;
437     for (i = 0; i < self->n_buttons; ++i)
438         if (self->button[i].window ==
439             (e->type == MotionNotify ? e->xmotion.window : e->xbutton.window))
440         {
441             but = &self->button[i];
442             break;
443         }
444     if (!but) return FALSE;
445
446     if (e->type == ButtonPress) {
447         ObPromptElement *oldfocus;
448
449         oldfocus = self->focus;
450
451         but->pressed = TRUE;
452         self->focus = but;
453
454         if (oldfocus != but) render_button(self, oldfocus);
455         render_button(self, but);
456     }
457     else if (e->type == ButtonRelease) {
458         if (but->pressed) {
459             if (self->func) self->func(self, but->result, self->data);
460             prompt_hide(self);
461         }
462     }
463     else if (e->type == MotionNotify) {
464         gboolean press;
465
466         press = (e->xmotion.x >= 0 && e->xmotion.y >= 0 &&
467                  e->xmotion.x < but->width && e->xmotion.y < but->height);
468
469         if (press != but->pressed) {
470             but->pressed = press;
471             render_button(self, but);
472         }
473     }
474     return TRUE;
475 }
476
477 void prompt_cancel(ObPrompt *self)
478 {
479     if (self->func) self->func(self, self->cancel_result, self->data);
480     prompt_hide(self);
481 }