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