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