]> Dogcows Code - chaz/openbox/blob - openbox/prompt.c
add a cleanup callback to the prompt interface. when the prompt's callback returns...
[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 "group.h"
25 #include "prop.h"
26 #include "modkeys.h"
27 #include "event.h"
28 #include "gettext.h"
29
30 static GList *prompt_list = NULL;
31
32 /* we construct these */
33 static RrAppearance *prompt_a_bg;
34 static RrAppearance *prompt_a_button;
35 static RrAppearance *prompt_a_focus;
36 static RrAppearance *prompt_a_press;
37 static RrAppearance *prompt_a_pfocus;
38 /* we change the max width which would screw with others */
39 static RrAppearance *prompt_a_msg;
40
41 /* sizing stuff */
42 #define OUTSIDE_MARGIN 4
43 #define MSG_BUTTON_SEPARATION 4
44 #define BUTTON_SEPARATION 4
45 #define BUTTON_VMARGIN 4
46 #define BUTTON_HMARGIN 12
47 #define MAX_WIDTH 400
48
49 static void prompt_layout(ObPrompt *self);
50 static void render_all(ObPrompt *self);
51 static void render_button(ObPrompt *self, ObPromptElement *e);
52 static void prompt_resize(ObPrompt *self, gint w, gint h);
53 static void prompt_run_callback(ObPrompt *self, gint result);
54
55 void prompt_startup(gboolean reconfig)
56 {
57 RrColor *c_button, *c_focus, *c_press, *c_pfocus;
58
59 /* note: this is not a copy, don't free it */
60 prompt_a_bg = ob_rr_theme->osd_hilite_bg;
61
62 prompt_a_button = RrAppearanceCopy(ob_rr_theme->a_focused_unpressed_close);
63 prompt_a_focus = RrAppearanceCopy(ob_rr_theme->a_hover_focused_close);
64 prompt_a_press = RrAppearanceCopy(ob_rr_theme->a_focused_pressed_close);
65 prompt_a_pfocus = RrAppearanceCopy(ob_rr_theme->a_focused_pressed_close);
66
67 c_button = prompt_a_button->texture[0].data.mask.color;
68 c_focus = prompt_a_focus->texture[0].data.mask.color;
69 c_press = prompt_a_press->texture[0].data.mask.color;
70 c_pfocus = prompt_a_press->texture[0].data.mask.color;
71
72 RrAppearanceRemoveTextures(prompt_a_button);
73 RrAppearanceRemoveTextures(prompt_a_focus);
74 RrAppearanceRemoveTextures(prompt_a_press);
75 RrAppearanceRemoveTextures(prompt_a_pfocus);
76
77 /* texture[0] is the text and texture[1-4] (for prompt_a_focus and
78 prompt_a_pfocus) is lineart to show where keyboard focus is */
79 RrAppearanceAddTextures(prompt_a_button, 1);
80 RrAppearanceAddTextures(prompt_a_focus, 5);
81 RrAppearanceAddTextures(prompt_a_press, 1);
82 RrAppearanceAddTextures(prompt_a_pfocus, 5);
83
84 /* totally cheating here.. */
85 prompt_a_button->texture[0] = ob_rr_theme->osd_hilite_label->texture[0];
86 prompt_a_focus->texture[0] = ob_rr_theme->osd_hilite_label->texture[0];
87 prompt_a_press->texture[0] = ob_rr_theme->osd_hilite_label->texture[0];
88 prompt_a_pfocus->texture[0] = ob_rr_theme->osd_hilite_label->texture[0];
89
90 prompt_a_button->texture[0].data.text.justify = RR_JUSTIFY_CENTER;
91 prompt_a_focus->texture[0].data.text.justify = RR_JUSTIFY_CENTER;
92 prompt_a_press->texture[0].data.text.justify = RR_JUSTIFY_CENTER;
93 prompt_a_pfocus->texture[0].data.text.justify = RR_JUSTIFY_CENTER;
94
95 prompt_a_button->texture[0].data.text.color = c_button;
96 prompt_a_focus->texture[0].data.text.color = c_focus;
97 prompt_a_press->texture[0].data.text.color = c_press;
98 prompt_a_pfocus->texture[0].data.text.color = c_press;
99
100 prompt_a_focus->texture[1].data.lineart.color = c_focus;
101 prompt_a_focus->texture[2].data.lineart.color = c_focus;
102 prompt_a_focus->texture[3].data.lineart.color = c_focus;
103 prompt_a_focus->texture[4].data.lineart.color = c_focus;
104
105 prompt_a_pfocus->texture[1].data.lineart.color = c_press;
106 prompt_a_pfocus->texture[2].data.lineart.color = c_press;
107 prompt_a_pfocus->texture[3].data.lineart.color = c_press;
108 prompt_a_pfocus->texture[4].data.lineart.color = c_press;
109
110 prompt_a_msg = RrAppearanceCopy(ob_rr_theme->osd_hilite_label);
111 prompt_a_msg->texture[0].data.text.flow = TRUE;
112
113 if (reconfig) {
114 GList *it;
115 for (it = prompt_list; it; it = g_list_next(it)) {
116 ObPrompt *p = it->data;
117 prompt_layout(p);
118 render_all(p);
119 }
120 }
121 }
122
123 void prompt_shutdown(gboolean reconfig)
124 {
125 GList *it;
126
127 if (!reconfig) {
128 for (it = prompt_list; it; it = g_list_next(it)) {
129 ObPrompt *p = it->data;
130 if (p->cleanup) p->cleanup(p, p->data);
131 }
132
133 g_assert(prompt_list == NULL);
134 }
135
136 RrAppearanceFree(prompt_a_button);
137 RrAppearanceFree(prompt_a_focus);
138 RrAppearanceFree(prompt_a_press);
139 RrAppearanceFree(prompt_a_pfocus);
140 RrAppearanceFree(prompt_a_msg);
141 }
142
143 ObPrompt* prompt_new(const gchar *msg,
144 const ObPromptAnswer *answers, gint n_answers,
145 gint default_result, gint cancel_result,
146 ObPromptCallback func, ObPromptCleanup cleanup,
147 gpointer data)
148 {
149 ObPrompt *self;
150 XSetWindowAttributes attrib;
151 gint i;
152
153 attrib.override_redirect = FALSE;
154
155 self = g_new0(ObPrompt, 1);
156 self->ref = 1;
157 self->func = func;
158 self->cleanup = cleanup;
159 self->data = data;
160 self->default_result = default_result;
161 self->cancel_result = cancel_result;
162 self->super.type = Window_Prompt;
163 self->super.window = XCreateWindow(ob_display,
164 RootWindow(ob_display, ob_screen),
165 0, 0, 1, 1, 0,
166 CopyFromParent, InputOutput,
167 CopyFromParent,
168 CWOverrideRedirect,
169 &attrib);
170
171 /* make it a dialog type window */
172 PROP_SET32(self->super.window, net_wm_window_type, atom,
173 prop_atoms.net_wm_window_type_dialog);
174
175 /* listen for key presses on the window */
176 self->event_mask = KeyPressMask;
177
178 /* set up the text message widow */
179 self->msg.text = g_strdup(msg);
180 self->msg.window = XCreateWindow(ob_display, self->super.window,
181 0, 0, 1, 1, 0,
182 CopyFromParent, InputOutput,
183 CopyFromParent, 0, NULL);
184 XMapWindow(ob_display, self->msg.window);
185
186 /* set up the buttons from the answers */
187
188 self->n_buttons = n_answers;
189 if (!self->n_buttons)
190 self->n_buttons = 1;
191
192 self->button = g_new0(ObPromptElement, self->n_buttons);
193
194 if (n_answers == 0) {
195 g_assert(self->n_buttons == 1); /* should be set to this above.. */
196 self->button[0].text = g_strdup(_("OK"));
197 }
198 else {
199 g_assert(self->n_buttons > 0);
200 for (i = 0; i < self->n_buttons; ++i) {
201 self->button[i].text = g_strdup(answers[i].text);
202 self->button[i].result = answers[i].result;
203 }
204 }
205
206 for (i = 0; i < self->n_buttons; ++i) {
207 self->button[i].window = XCreateWindow(ob_display, self->super.window,
208 0, 0, 1, 1, 0,
209 CopyFromParent, InputOutput,
210 CopyFromParent, 0, NULL);
211 XMapWindow(ob_display, self->button[i].window);
212 g_hash_table_insert(window_map, &self->button[i].window,
213 PROMPT_AS_WINDOW(self));
214
215 /* listen for button presses on the buttons */
216 XSelectInput(ob_display, self->button[i].window,
217 ButtonPressMask | ButtonReleaseMask | ButtonMotionMask);
218 }
219
220 prompt_list = g_list_prepend(prompt_list, self);
221
222 return self;
223 }
224
225 void prompt_ref(ObPrompt *self)
226 {
227 ++self->ref;
228 }
229
230 void prompt_unref(ObPrompt *self)
231 {
232 if (self && --self->ref == 0) {
233 gint i;
234
235 if (self->mapped)
236 prompt_hide(self);
237
238 prompt_list = g_list_remove(prompt_list, self);
239
240 for (i = 0; i < self->n_buttons; ++i) {
241 g_hash_table_remove(window_map, &self->button[i].window);
242 XDestroyWindow(ob_display, self->button[i].window);
243 }
244
245 XDestroyWindow(ob_display, self->msg.window);
246 XDestroyWindow(ob_display, self->super.window);
247 g_free(self);
248 }
249 }
250
251 static void prompt_layout(ObPrompt *self)
252 {
253 gint l, r, t, b;
254 gint i;
255 gint allbuttonsw, allbuttonsh, buttonx;
256 gint w, h;
257 gint maxw;
258
259 RrMargins(prompt_a_bg, &l, &t, &r, &b);
260 l += OUTSIDE_MARGIN;
261 t += OUTSIDE_MARGIN;
262 r += OUTSIDE_MARGIN;
263 b += OUTSIDE_MARGIN;
264
265 {
266 Rect *area = screen_physical_area_all_monitors();
267 maxw = MIN(MAX_WIDTH, area->width*4/5);
268 g_free(area);
269 }
270
271 /* find the button sizes and how much space we need for them */
272 allbuttonsw = allbuttonsh = 0;
273 for (i = 0; i < self->n_buttons; ++i) {
274 gint bw, bh;
275
276 prompt_a_button->texture[0].data.text.string = self->button[i].text;
277 prompt_a_focus->texture[0].data.text.string = self->button[i].text;
278 prompt_a_press->texture[0].data.text.string = self->button[i].text;
279 prompt_a_pfocus->texture[0].data.text.string = self->button[i].text;
280 RrMinSize(prompt_a_button, &bw, &bh);
281 self->button[i].width = bw;
282 self->button[i].height = bh;
283 RrMinSize(prompt_a_focus, &bw, &bh);
284 self->button[i].width = MAX(self->button[i].width, bw);
285 self->button[i].height = MAX(self->button[i].height, bh);
286 RrMinSize(prompt_a_press, &bw, &bh);
287 self->button[i].width = MAX(self->button[i].width, bw);
288 self->button[i].height = MAX(self->button[i].height, bh);
289 RrMinSize(prompt_a_pfocus, &bw, &bh);
290 self->button[i].width = MAX(self->button[i].width, bw);
291 self->button[i].height = MAX(self->button[i].height, bh);
292
293
294 self->button[i].width += BUTTON_HMARGIN * 2;
295 self->button[i].height += BUTTON_VMARGIN * 2;
296
297 allbuttonsw += self->button[i].width + (i > 0 ? BUTTON_SEPARATION : 0);
298 allbuttonsh = MAX(allbuttonsh, self->button[i].height);
299 }
300
301 self->msg_wbound = MAX(allbuttonsw, maxw);
302
303 /* measure the text message area */
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 RrMinSize(prompt_a_msg, &self->msg.width, &self->msg.height);
307
308 /* width and height inside the outer margins */
309 w = MAX(self->msg.width, allbuttonsw);
310 h = self->msg.height + MSG_BUTTON_SEPARATION + allbuttonsh;
311
312 /* position the text message */
313 self->msg.x = l + (w - self->msg.width) / 2;
314 self->msg.y = t;
315
316 /* position the button buttons on the right of the dialog */
317 buttonx = l + w;
318 for (i = self->n_buttons - 1; i >= 0; --i) {
319 self->button[i].x = buttonx - self->button[i].width;
320 buttonx -= self->button[i].width + BUTTON_SEPARATION;
321 self->button[i].y = t + h - allbuttonsh;
322 self->button[i].y += (allbuttonsh - self->button[i].height) / 2;
323 }
324
325 /* size and position the toplevel window */
326 prompt_resize(self, w + l + r, h + t + b);
327
328 /* move and resize the internal windows */
329 XMoveResizeWindow(ob_display, self->msg.window,
330 self->msg.x, self->msg.y,
331 self->msg.width, self->msg.height);
332 for (i = 0; i < self->n_buttons; ++i)
333 XMoveResizeWindow(ob_display, self->button[i].window,
334 self->button[i].x, self->button[i].y,
335 self->button[i].width, self->button[i].height);
336 }
337
338 static void prompt_resize(ObPrompt *self, gint w, gint h)
339 {
340 XConfigureRequestEvent req;
341 XSizeHints hints;
342
343 self->width = w;
344 self->height = h;
345
346 /* the user can't resize the prompt */
347 hints.flags = PMinSize | PMaxSize;
348 hints.min_width = hints.max_width = w;
349 hints.min_height = hints.max_height = h;
350 XSetWMNormalHints(ob_display, self->super.window, &hints);
351
352 if (self->mapped) {
353 /* send a configure request like a normal client would */
354 req.type = ConfigureRequest;
355 req.display = ob_display;
356 req.parent = RootWindow(ob_display, ob_screen);
357 req.window = self->super.window;
358 req.width = w;
359 req.height = h;
360 req.value_mask = CWWidth | CWHeight;
361 XSendEvent(req.display, req.window, FALSE, StructureNotifyMask,
362 (XEvent*)&req);
363 }
364 else
365 XResizeWindow(ob_display, self->super.window, w, h);
366 }
367
368 static void setup_button_focus_tex(ObPromptElement *e, RrAppearance *a,
369 gboolean on)
370 {
371 gint i, l, r, t, b;
372
373 for (i = 1; i < 5; ++i)
374 a->texture[i].type = on ? RR_TEXTURE_LINE_ART : RR_TEXTURE_NONE;
375
376 if (!on) return;
377
378 RrMargins(a, &l, &t, &r, &b);
379 l += MIN(BUTTON_HMARGIN, BUTTON_VMARGIN) / 2;
380 r += MIN(BUTTON_HMARGIN, BUTTON_VMARGIN) / 2;
381 t += MIN(BUTTON_HMARGIN, BUTTON_VMARGIN) / 2;
382 b += MIN(BUTTON_HMARGIN, BUTTON_VMARGIN) / 2;
383
384 /* top line */
385 a->texture[1].data.lineart.x1 = l;
386 a->texture[1].data.lineart.x2 = e->width - r - 1;
387 a->texture[1].data.lineart.y1 = t;
388 a->texture[1].data.lineart.y2 = t;
389
390 /* bottom line */
391 a->texture[2].data.lineart.x1 = l;
392 a->texture[2].data.lineart.x2 = e->width - r - 1;
393 a->texture[2].data.lineart.y1 = e->height - b - 1;
394 a->texture[2].data.lineart.y2 = e->height - b - 1;
395
396 /* left line */
397 a->texture[3].data.lineart.x1 = l;
398 a->texture[3].data.lineart.x2 = l;
399 a->texture[3].data.lineart.y1 = t;
400 a->texture[3].data.lineart.y2 = e->height - b - 1;
401
402 /* right line */
403 a->texture[4].data.lineart.x1 = e->width - r - 1;
404 a->texture[4].data.lineart.x2 = e->width - r - 1;
405 a->texture[4].data.lineart.y1 = t;
406 a->texture[4].data.lineart.y2 = e->height - b - 1;
407 }
408
409 static void render_button(ObPrompt *self, ObPromptElement *e)
410 {
411 RrAppearance *a;
412
413 if (e->pressed && self->focus == e) a = prompt_a_pfocus;
414 else if (self->focus == e) a = prompt_a_focus;
415 else if (e->pressed) a = prompt_a_press;
416 else a = prompt_a_button;
417
418 a->surface.parent = prompt_a_bg;
419 a->surface.parentx = e->x;
420 a->surface.parenty = e->y;
421
422 /* draw the keyfocus line */
423 if (a == prompt_a_pfocus || a == prompt_a_focus)
424 setup_button_focus_tex(e, a, TRUE);
425
426 a->texture[0].data.text.string = e->text;
427 RrPaint(a, e->window, e->width, e->height);
428
429 /* turn off the keyfocus line so that it doesn't affect size calculations
430 */
431 if (a == prompt_a_pfocus || a == prompt_a_focus)
432 setup_button_focus_tex(e, a, FALSE);
433 }
434
435 static void render_all(ObPrompt *self)
436 {
437 gint i;
438
439 RrPaint(prompt_a_bg, self->super.window, self->width, self->height);
440
441 prompt_a_msg->surface.parent = prompt_a_bg;
442 prompt_a_msg->surface.parentx = self->msg.x;
443 prompt_a_msg->surface.parenty = self->msg.y;
444
445 prompt_a_msg->texture[0].data.text.string = self->msg.text;
446 prompt_a_msg->texture[0].data.text.maxwidth = self->msg_wbound;
447 RrPaint(prompt_a_msg, self->msg.window, self->msg.width, self->msg.height);
448
449 for (i = 0; i < self->n_buttons; ++i)
450 render_button(self, &self->button[i]);
451 }
452
453 void prompt_show(ObPrompt *self, ObClient *parent, gboolean modal)
454 {
455 gint i;
456
457 if (self->mapped) {
458 /* activate the prompt */
459 PROP_MSG(self->super.window, net_active_window,
460 1, /* from an application.. */
461 event_curtime,
462 0,
463 0);
464 return;
465 }
466
467 /* set the focused button (if not found then the first button is used) */
468 self->focus = &self->button[0];
469 for (i = 0; i < self->n_buttons; ++i)
470 if (self->button[i].result == self->default_result) {
471 self->focus = &self->button[i];
472 break;
473 }
474
475 if (parent) {
476 Atom states[1];
477 gint nstates;
478 Window p;
479 XWMHints h;
480
481 if (parent->group) {
482 /* make it transient for the window's group */
483 h.flags = WindowGroupHint;
484 h.window_group = parent->group->leader;
485 p = RootWindow(ob_display, ob_screen);
486 }
487 else {
488 /* make it transient for the window directly */
489 h.flags = 0;
490 p = parent->window;
491 }
492
493 XSetWMHints(ob_display, self->super.window, &h);
494 PROP_SET32(self->super.window, wm_transient_for, window, p);
495
496 states[0] = prop_atoms.net_wm_state_modal;
497 nstates = (modal ? 1 : 0);
498 PROP_SETA32(self->super.window, net_wm_state, atom, states, nstates);
499 }
500 else
501 PROP_ERASE(self->super.window, wm_transient_for);
502
503 /* set up the dialog and render it */
504 prompt_layout(self);
505 render_all(self);
506
507 client_manage(self->super.window, self);
508
509 self->mapped = TRUE;
510 }
511
512 void prompt_hide(ObPrompt *self)
513 {
514 XUnmapWindow(ob_display, self->super.window);
515 self->mapped = FALSE;
516 }
517
518 gboolean prompt_key_event(ObPrompt *self, XEvent *e)
519 {
520 gboolean shift;
521 guint shift_mask;
522
523 if (e->type != KeyPress) return FALSE;
524
525 shift_mask = modkeys_key_to_mask(OB_MODKEY_KEY_SHIFT);
526 shift = !!(e->xkey.state & shift_mask);
527
528 /* only accept shift */
529 if (e->xkey.state != 0 && e->xkey.state != shift_mask)
530 return FALSE;
531
532 if (e->xkey.keycode == ob_keycode(OB_KEY_ESCAPE))
533 prompt_cancel(self);
534 else if (e->xkey.keycode == ob_keycode(OB_KEY_RETURN) ||
535 e->xkey.keycode == ob_keycode(OB_KEY_SPACE))
536 {
537 prompt_run_callback(self, self->focus->result);
538 }
539 else if (e->xkey.keycode == ob_keycode(OB_KEY_TAB) ||
540 e->xkey.keycode == ob_keycode(OB_KEY_LEFT) ||
541 e->xkey.keycode == ob_keycode(OB_KEY_RIGHT))
542 {
543 gint i;
544 gboolean left;
545 ObPromptElement *oldfocus;
546
547 left = e->xkey.keycode == ob_keycode(OB_KEY_LEFT) ||
548 (e->xkey.keycode == ob_keycode(OB_KEY_TAB) && shift);
549 oldfocus = self->focus;
550
551 for (i = 0; i < self->n_buttons; ++i)
552 if (self->focus == &self->button[i]) break;
553 i += (left ? -1 : 1);
554 if (i < 0) i = self->n_buttons - 1;
555 else if (i >= self->n_buttons) i = 0;
556 self->focus = &self->button[i];
557
558 if (oldfocus != self->focus) render_button(self, oldfocus);
559 render_button(self, self->focus);
560 }
561 return TRUE;
562 }
563
564 gboolean prompt_mouse_event(ObPrompt *self, XEvent *e)
565 {
566 gint i;
567 ObPromptElement *but;
568
569 if (e->type != ButtonPress && e->type != ButtonRelease &&
570 e->type != MotionNotify) return FALSE;
571
572 /* find the button */
573 but = NULL;
574 for (i = 0; i < self->n_buttons; ++i)
575 if (self->button[i].window ==
576 (e->type == MotionNotify ? e->xmotion.window : e->xbutton.window))
577 {
578 but = &self->button[i];
579 break;
580 }
581 if (!but) return FALSE;
582
583 if (e->type == ButtonPress) {
584 ObPromptElement *oldfocus;
585
586 oldfocus = self->focus;
587
588 but->pressed = TRUE;
589 self->focus = but;
590
591 if (oldfocus != but) render_button(self, oldfocus);
592 render_button(self, but);
593 }
594 else if (e->type == ButtonRelease) {
595 if (but->pressed)
596 prompt_run_callback(self, but->result);
597 }
598 else if (e->type == MotionNotify) {
599 gboolean press;
600
601 press = (e->xmotion.x >= 0 && e->xmotion.y >= 0 &&
602 e->xmotion.x < but->width && e->xmotion.y < but->height);
603
604 if (press != but->pressed) {
605 but->pressed = press;
606 render_button(self, but);
607 }
608 }
609 return TRUE;
610 }
611
612 void prompt_cancel(ObPrompt *self)
613 {
614 prompt_run_callback(self, self->cancel_result);
615 }
616
617 static gboolean prompt_show_message_cb(ObPrompt *p, int res, gpointer data)
618 {
619 return TRUE; /* call the cleanup func */
620 }
621
622 static void prompt_show_message_cleanup(ObPrompt *p, gpointer data)
623 {
624 prompt_unref(p);
625 }
626
627 void prompt_show_message(const gchar *msg, const gchar *answer)
628 {
629 ObPrompt *p;
630 ObPromptAnswer ans[] = {
631 { answer, 0 }
632 };
633
634 p = prompt_new(msg, ans, 1, 0, 0,
635 prompt_show_message_cb, prompt_show_message_cleanup, NULL);
636 prompt_show(p, NULL, FALSE);
637 }
638
639 static void prompt_run_callback(ObPrompt *self, gint result)
640 {
641 prompt_ref(self);
642 if (self->func) {
643 gboolean clean = self->func(self, self->focus->result, self->data);
644 if (clean && self->cleanup)
645 self->cleanup(self, self->data);
646 }
647 prompt_hide(self);
648 prompt_unref(self);
649 }
This page took 0.064896 seconds and 5 git commands to generate.