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