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