1 /* -*- indent-tabs-mode: nil; tab-width: 4; c-basic-offset: 4; -*-
3 prompt.c for the Openbox window manager
4 Copyright (c) 2008 Dana Jansens
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.
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.
16 See the COPYING file for a copy of the GNU General Public License.
25 #include "obt/display.h"
26 #include "obt/keyboard.h"
30 static GList *prompt_list = NULL;
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;
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
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);
55 void prompt_startup(gboolean reconfig)
57 RrColor *c_button, *c_focus, *c_press, *c_pfocus;
59 /* note: this is not a copy, don't free it */
60 prompt_a_bg = ob_rr_theme->osd_bg;
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);
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;
72 RrAppearanceRemoveTextures(prompt_a_button);
73 RrAppearanceRemoveTextures(prompt_a_focus);
74 RrAppearanceRemoveTextures(prompt_a_press);
75 RrAppearanceRemoveTextures(prompt_a_pfocus);
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);
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];
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;
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_pfocus;
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;
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;
110 prompt_a_msg = RrAppearanceCopy(ob_rr_theme->osd_hilite_label);
111 prompt_a_msg->texture[0].data.text.flow = TRUE;
115 for (it = prompt_list; it; it = g_list_next(it)) {
116 ObPrompt *p = it->data;
123 void prompt_shutdown(gboolean 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);
133 g_assert(prompt_list == NULL);
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);
143 ObPrompt* prompt_new(const gchar *msg, const gchar *title,
144 const ObPromptAnswer *answers, gint n_answers,
145 gint default_result, gint cancel_result,
146 ObPromptCallback func, ObPromptCleanup cleanup,
150 XSetWindowAttributes attrib;
153 attrib.override_redirect = FALSE;
155 self = g_slice_new0(ObPrompt);
158 self->cleanup = cleanup;
160 self->default_result = default_result;
161 self->cancel_result = cancel_result;
162 self->super.type = OB_WINDOW_CLASS_PROMPT;
163 self->super.window = XCreateWindow(obt_display, obt_root(ob_screen),
165 CopyFromParent, InputOutput,
169 self->ic = obt_keyboard_context_new(self->super.window,
172 /* make it a dialog type window */
173 OBT_PROP_SET32(self->super.window, NET_WM_WINDOW_TYPE, ATOM,
174 OBT_PROP_ATOM(NET_WM_WINDOW_TYPE_DIALOG));
176 /* set the window's title */
178 OBT_PROP_SETS(self->super.window, NET_WM_NAME, utf8, title);
180 /* listen for key presses on the window */
181 self->event_mask = KeyPressMask;
183 /* set up the text message widow */
184 self->msg.text = g_strdup(msg);
185 self->msg.window = XCreateWindow(obt_display, self->super.window,
187 CopyFromParent, InputOutput,
188 CopyFromParent, 0, NULL);
189 XMapWindow(obt_display, self->msg.window);
191 /* set up the buttons from the answers */
193 self->n_buttons = n_answers;
194 if (!self->n_buttons)
197 self->button = g_new0(ObPromptElement, self->n_buttons);
199 if (n_answers == 0) {
200 g_assert(self->n_buttons == 1); /* should be set to this above.. */
201 self->button[0].text = g_strdup(_("OK"));
204 g_assert(self->n_buttons > 0);
205 for (i = 0; i < self->n_buttons; ++i) {
206 self->button[i].text = g_strdup(answers[i].text);
207 self->button[i].result = answers[i].result;
211 for (i = 0; i < self->n_buttons; ++i) {
212 self->button[i].window = XCreateWindow(obt_display, self->super.window,
214 CopyFromParent, InputOutput,
215 CopyFromParent, 0, NULL);
216 XMapWindow(obt_display, self->button[i].window);
217 window_add(&self->button[i].window, PROMPT_AS_WINDOW(self));
219 /* listen for button presses on the buttons */
220 XSelectInput(obt_display, self->button[i].window,
221 ButtonPressMask | ButtonReleaseMask | ButtonMotionMask);
224 prompt_list = g_list_prepend(prompt_list, self);
229 void prompt_ref(ObPrompt *self)
234 void prompt_unref(ObPrompt *self)
236 if (self && --self->ref == 0) {
242 prompt_list = g_list_remove(prompt_list, self);
244 obt_keyboard_context_unref(self->ic);
246 for (i = 0; i < self->n_buttons; ++i) {
247 window_remove(self->button[i].window);
248 XDestroyWindow(obt_display, self->button[i].window);
251 XDestroyWindow(obt_display, self->msg.window);
252 XDestroyWindow(obt_display, self->super.window);
253 g_slice_free(ObPrompt, self);
257 static void prompt_layout(ObPrompt *self)
261 gint allbuttonsw, allbuttonsh, buttonx;
265 RrMargins(prompt_a_bg, &l, &t, &r, &b);
272 const Rect *area = screen_physical_area_all_monitors();
273 maxw = MIN(MAX_WIDTH, area->width*4/5);
276 /* find the button sizes and how much space we need for them */
277 allbuttonsw = allbuttonsh = 0;
278 for (i = 0; i < self->n_buttons; ++i) {
281 prompt_a_button->texture[0].data.text.string = self->button[i].text;
282 prompt_a_focus->texture[0].data.text.string = self->button[i].text;
283 prompt_a_press->texture[0].data.text.string = self->button[i].text;
284 prompt_a_pfocus->texture[0].data.text.string = self->button[i].text;
285 RrMinSize(prompt_a_button, &bw, &bh);
286 self->button[i].width = bw;
287 self->button[i].height = bh;
288 RrMinSize(prompt_a_focus, &bw, &bh);
289 self->button[i].width = MAX(self->button[i].width, bw);
290 self->button[i].height = MAX(self->button[i].height, bh);
291 RrMinSize(prompt_a_press, &bw, &bh);
292 self->button[i].width = MAX(self->button[i].width, bw);
293 self->button[i].height = MAX(self->button[i].height, bh);
294 RrMinSize(prompt_a_pfocus, &bw, &bh);
295 self->button[i].width = MAX(self->button[i].width, bw);
296 self->button[i].height = MAX(self->button[i].height, bh);
298 self->button[i].width += BUTTON_HMARGIN * 2;
299 self->button[i].height += BUTTON_VMARGIN * 2;
301 allbuttonsw += self->button[i].width + (i > 0 ? BUTTON_SEPARATION : 0);
302 allbuttonsh = MAX(allbuttonsh, self->button[i].height);
305 self->msg_wbound = MAX(allbuttonsw, maxw);
307 /* measure the text message area */
308 prompt_a_msg->texture[0].data.text.string = self->msg.text;
309 prompt_a_msg->texture[0].data.text.maxwidth = self->msg_wbound;
310 RrMinSize(prompt_a_msg, &self->msg.width, &self->msg.height);
312 /* width and height inside the outer margins */
313 w = MAX(self->msg.width, allbuttonsw);
314 h = self->msg.height + MSG_BUTTON_SEPARATION + allbuttonsh;
316 /* position the text message */
317 self->msg.x = l + (w - self->msg.width) / 2;
320 /* position the button buttons on the right of the dialog */
322 for (i = self->n_buttons - 1; i >= 0; --i) {
323 self->button[i].x = buttonx - self->button[i].width;
324 buttonx -= self->button[i].width + BUTTON_SEPARATION;
325 self->button[i].y = t + h - allbuttonsh;
326 self->button[i].y += (allbuttonsh - self->button[i].height) / 2;
329 /* size and position the toplevel window */
330 prompt_resize(self, w + l + r, h + t + b);
332 /* move and resize the internal windows */
333 XMoveResizeWindow(obt_display, self->msg.window,
334 self->msg.x, self->msg.y,
335 self->msg.width, self->msg.height);
336 for (i = 0; i < self->n_buttons; ++i)
337 XMoveResizeWindow(obt_display, self->button[i].window,
338 self->button[i].x, self->button[i].y,
339 self->button[i].width, self->button[i].height);
342 static void prompt_resize(ObPrompt *self, gint w, gint h)
344 XConfigureRequestEvent req;
350 /* the user can't resize the prompt */
351 hints.flags = PMinSize | PMaxSize;
352 hints.min_width = hints.max_width = w;
353 hints.min_height = hints.max_height = h;
354 XSetWMNormalHints(obt_display, self->super.window, &hints);
357 /* send a configure request like a normal client would */
358 req.type = ConfigureRequest;
359 req.display = obt_display;
360 req.parent = obt_root(ob_screen);
361 req.window = self->super.window;
364 req.value_mask = CWWidth | CWHeight;
365 XSendEvent(req.display, req.window, FALSE, StructureNotifyMask,
369 XResizeWindow(obt_display, self->super.window, w, h);
372 static void setup_button_focus_tex(ObPromptElement *e, RrAppearance *a,
377 for (i = 1; i < 5; ++i)
378 a->texture[i].type = on ? RR_TEXTURE_LINE_ART : RR_TEXTURE_NONE;
382 RrMargins(a, &l, &t, &r, &b);
383 l += MIN(BUTTON_HMARGIN, BUTTON_VMARGIN) / 2;
384 r += MIN(BUTTON_HMARGIN, BUTTON_VMARGIN) / 2;
385 t += MIN(BUTTON_HMARGIN, BUTTON_VMARGIN) / 2;
386 b += MIN(BUTTON_HMARGIN, BUTTON_VMARGIN) / 2;
389 a->texture[1].data.lineart.x1 = l;
390 a->texture[1].data.lineart.x2 = e->width - r - 1;
391 a->texture[1].data.lineart.y1 = t;
392 a->texture[1].data.lineart.y2 = t;
395 a->texture[2].data.lineart.x1 = l;
396 a->texture[2].data.lineart.x2 = e->width - r - 1;
397 a->texture[2].data.lineart.y1 = e->height - b - 1;
398 a->texture[2].data.lineart.y2 = e->height - b - 1;
401 a->texture[3].data.lineart.x1 = l;
402 a->texture[3].data.lineart.x2 = l;
403 a->texture[3].data.lineart.y1 = t;
404 a->texture[3].data.lineart.y2 = e->height - b - 1;
407 a->texture[4].data.lineart.x1 = e->width - r - 1;
408 a->texture[4].data.lineart.x2 = e->width - r - 1;
409 a->texture[4].data.lineart.y1 = t;
410 a->texture[4].data.lineart.y2 = e->height - b - 1;
413 static void render_button(ObPrompt *self, ObPromptElement *e)
417 if (e->hover && self->focus == e) a = prompt_a_pfocus;
418 else if (self->focus == e) a = prompt_a_focus;
419 else if (e->pressed) a = prompt_a_press;
420 else a = prompt_a_button;
422 a->surface.parent = prompt_a_bg;
423 a->surface.parentx = e->x;
424 a->surface.parenty = e->y;
426 /* draw the keyfocus line */
427 if (a == prompt_a_pfocus || a == prompt_a_focus)
428 setup_button_focus_tex(e, a, TRUE);
430 a->texture[0].data.text.string = e->text;
431 RrPaint(a, e->window, e->width, e->height);
433 /* turn off the keyfocus line so that it doesn't affect size calculations
435 if (a == prompt_a_pfocus || a == prompt_a_focus)
436 setup_button_focus_tex(e, a, FALSE);
439 static void render_all(ObPrompt *self)
443 RrPaint(prompt_a_bg, self->super.window, self->width, self->height);
445 prompt_a_msg->surface.parent = prompt_a_bg;
446 prompt_a_msg->surface.parentx = self->msg.x;
447 prompt_a_msg->surface.parenty = self->msg.y;
449 prompt_a_msg->texture[0].data.text.string = self->msg.text;
450 prompt_a_msg->texture[0].data.text.maxwidth = self->msg_wbound;
451 RrPaint(prompt_a_msg, self->msg.window, self->msg.width, self->msg.height);
453 for (i = 0; i < self->n_buttons; ++i)
454 render_button(self, &self->button[i]);
457 void prompt_show(ObPrompt *self, ObClient *parent, gboolean modal)
462 /* activate the prompt */
463 OBT_PROP_MSG(ob_screen, self->super.window, NET_ACTIVE_WINDOW,
464 1, /* from an application.. */
471 /* set the focused button (if not found then the first button is used) */
472 self->focus = &self->button[0];
473 for (i = 0; i < self->n_buttons; ++i)
474 if (self->button[i].result == self->default_result) {
475 self->focus = &self->button[i];
486 /* make it transient for the window's group */
487 h.flags = WindowGroupHint;
488 h.window_group = parent->group->leader;
489 p = obt_root(ob_screen);
492 /* make it transient for the window directly */
497 XSetWMHints(obt_display, self->super.window, &h);
498 OBT_PROP_SET32(self->super.window, WM_TRANSIENT_FOR, WINDOW, p);
500 states[0] = OBT_PROP_ATOM(NET_WM_STATE_MODAL);
501 nstates = (modal ? 1 : 0);
502 OBT_PROP_SETA32(self->super.window, NET_WM_STATE, ATOM,
506 OBT_PROP_ERASE(self->super.window, WM_TRANSIENT_FOR);
508 /* set up the dialog and render it */
512 client_manage(self->super.window, self);
517 void prompt_hide(ObPrompt *self)
519 XUnmapWindow(obt_display, self->super.window);
520 self->mapped = FALSE;
523 gboolean prompt_key_event(ObPrompt *self, XEvent *e)
526 guint shift_mask, mods;
529 if (e->type != KeyPress) return FALSE;
531 shift_mask = obt_keyboard_modkey_to_modmask(OBT_KEYBOARD_MODKEY_SHIFT);
532 mods = obt_keyboard_only_modmasks(e->xkey.state);
533 shift = !!(mods & shift_mask);
535 /* only accept shift */
536 if (mods != 0 && mods != shift_mask)
539 sym = obt_keyboard_keypress_to_keysym(e);
541 if (sym == XK_Escape)
543 else if (sym == XK_Return || sym == XK_KP_Enter || sym == XK_space)
544 prompt_run_callback(self, self->focus->result);
545 else if (sym == XK_Tab || sym == XK_Left || sym == XK_Right) {
548 ObPromptElement *oldfocus;
550 left = (sym == XK_Left) || ((sym == XK_Tab) && shift);
551 oldfocus = self->focus;
553 for (i = 0; i < self->n_buttons; ++i)
554 if (self->focus == &self->button[i]) break;
555 i += (left ? -1 : 1);
556 if (i < 0) i = self->n_buttons - 1;
557 else if (i >= self->n_buttons) i = 0;
558 self->focus = &self->button[i];
560 if (oldfocus != self->focus) render_button(self, oldfocus);
561 render_button(self, self->focus);
566 gboolean prompt_mouse_event(ObPrompt *self, XEvent *e)
569 ObPromptElement *but;
571 if (e->type != ButtonPress && e->type != ButtonRelease &&
572 e->type != MotionNotify) return FALSE;
574 /* find the button */
576 for (i = 0; i < self->n_buttons; ++i)
577 if (self->button[i].window ==
578 (e->type == MotionNotify ? e->xmotion.window : e->xbutton.window))
580 but = &self->button[i];
583 if (!but) return FALSE;
585 if (e->type == ButtonPress) {
586 ObPromptElement *oldfocus;
588 oldfocus = self->focus;
590 but->pressed = but->hover = TRUE;
593 if (oldfocus != but) render_button(self, oldfocus);
594 render_button(self, but);
596 else if (e->type == ButtonRelease) {
598 prompt_run_callback(self, but->result);
599 but->pressed = FALSE;
601 else if (e->type == MotionNotify) {
605 hover = (e->xmotion.x >= 0 && e->xmotion.y >= 0 &&
606 e->xmotion.x < but->width && e->xmotion.y < but->height);
608 if (hover != but->hover) {
610 render_button(self, but);
617 void prompt_cancel(ObPrompt *self)
619 prompt_run_callback(self, self->cancel_result);
622 static gboolean prompt_show_message_cb(ObPrompt *p, int res, gpointer data)
624 return TRUE; /* call the cleanup func */
627 static void prompt_show_message_cleanup(ObPrompt *p, gpointer data)
632 ObPrompt* prompt_show_message(const gchar *msg, const gchar *title,
636 ObPromptAnswer ans[] = {
640 p = prompt_new(msg, title, ans, 1, 0, 0,
641 prompt_show_message_cb, prompt_show_message_cleanup, NULL);
642 prompt_show(p, NULL, FALSE);
646 static void prompt_run_callback(ObPrompt *self, gint result)
650 gboolean clean = self->func(self, self->focus->result, self->data);
651 if (clean && self->cleanup)
652 self->cleanup(self, self->data);