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