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