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