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