give prompts a border, and fix how they are laid out. and make them use the multi...
[mikachu/openbox.git] / 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 "obt/display.h"
23 #include "gettext.h"
24
25 static GList *prompt_list = NULL;
26
27 /* we construct these */
28 static RrAppearance *prompt_a_button;
29 static RrAppearance *prompt_a_hover;
30 static RrAppearance *prompt_a_press;
31 /* we change the max width which would screw with others */
32 static RrAppearance *prompt_a_msg;
33
34 void prompt_startup(gboolean reconfig)
35 {
36     RrColor *c_button, *c_hover, *c_press;
37
38     prompt_a_button = RrAppearanceCopy(ob_rr_theme->a_focused_unpressed_close);
39     prompt_a_hover = RrAppearanceCopy(ob_rr_theme->a_hover_focused_close);
40     prompt_a_press = RrAppearanceCopy(ob_rr_theme->a_focused_pressed_close);
41
42     c_button = prompt_a_button->texture[0].data.mask.color;
43     c_hover = prompt_a_button->texture[0].data.mask.color;
44     c_press = prompt_a_button->texture[0].data.mask.color;
45
46     RrAppearanceRemoveTextures(prompt_a_button);
47     RrAppearanceRemoveTextures(prompt_a_hover);
48     RrAppearanceRemoveTextures(prompt_a_press);
49
50     RrAppearanceAddTextures(prompt_a_button, 1);
51     RrAppearanceAddTextures(prompt_a_hover, 1);
52     RrAppearanceAddTextures(prompt_a_press, 1);
53
54     /* totally cheating here.. */
55     prompt_a_button->texture[0] = ob_rr_theme->osd_hilite_label->texture[0];
56     prompt_a_hover->texture[0] = ob_rr_theme->osd_hilite_label->texture[0];
57     prompt_a_press->texture[0] = ob_rr_theme->osd_hilite_label->texture[0];
58
59     prompt_a_button->texture[0].data.text.color = c_button;
60     prompt_a_hover->texture[0].data.text.color = c_hover;
61     prompt_a_press->texture[0].data.text.color = c_press;
62
63     prompt_a_msg = RrAppearanceCopy(ob_rr_theme->osd_hilite_label);
64     prompt_a_msg->texture[0].data.text.flow = TRUE;
65 }
66
67 void prompt_shutdown(gboolean reconfig)
68 {
69     RrAppearanceFree(prompt_a_button);
70     RrAppearanceFree(prompt_a_hover);
71     RrAppearanceFree(prompt_a_press);
72     RrAppearanceFree(prompt_a_msg);
73 }
74
75 ObPrompt* prompt_new(const gchar *msg, const gchar *const *answers)
76 {
77     ObPrompt *self;
78     XSetWindowAttributes attrib;
79     guint i;
80     const gchar *const *c;
81
82     attrib.override_redirect = TRUE;
83     attrib.border_pixel = RrColorPixel(ob_rr_theme->osd_border_color);
84
85     self = g_new0(ObPrompt, 1);
86     self->ref = 1;
87     self->super.type = OB_WINDOW_CLASS_PROMPT;
88     self->super.window = XCreateWindow(obt_display, obt_root(ob_screen),
89                                        0, 0, 1, 1, ob_rr_theme->obwidth,
90                                        CopyFromParent, InputOutput,
91                                        CopyFromParent,
92                                        CWOverrideRedirect | CWBorderPixel,
93                                        &attrib);
94     window_add(&self->super.window, PROMPT_AS_WINDOW(self));
95
96     self->a_bg = RrAppearanceCopy(ob_rr_theme->osd_hilite_bg);
97
98     self->msg.text = g_strdup(msg);
99     self->msg.window = XCreateWindow(obt_display, self->super.window,
100                                      0, 0, 1, 1, 0,
101                                      CopyFromParent, InputOutput,
102                                      CopyFromParent, 0, NULL);
103     XMapWindow(obt_display, self->msg.window);
104
105     self->n_buttons = 0;
106     for (c = answers; *c != NULL; ++c)
107         ++self->n_buttons;
108
109     if (!self->n_buttons)
110         self->n_buttons = 1;
111
112     self->button = g_new(ObPromptElement, self->n_buttons);
113
114     if (!answers) {
115         g_assert(self->n_buttons == 1); /* should be set to this above.. */
116         self->button[0].text = g_strdup(_("OK"));
117     }
118     else {
119         g_assert(self->n_buttons > 0);
120         for (i = 0; i < self->n_buttons; ++i)
121             self->button[i].text = g_strdup(answers[i]);
122     }
123
124     for (i = 0; i < self->n_buttons; ++i) {
125         self->button[i].window = XCreateWindow(obt_display, self->super.window,
126                                                0, 0, 1, 1, 0,
127                                                CopyFromParent, InputOutput,
128                                                CopyFromParent, 0, NULL);
129         XMapWindow(obt_display, self->button[i].window);
130         window_add(&self->button[i].window, PROMPT_AS_WINDOW(self));
131     }
132
133     return self;
134 }
135
136 void prompt_ref(ObPrompt *self)
137 {
138     ++self->ref;
139 }
140
141 void prompt_unref(ObPrompt *self)
142 {
143     if (self && --self->ref == 0) {
144         guint i;
145
146         for (i = 0; i < self->n_buttons; ++i) {
147             window_remove(self->button[i].window);
148             XDestroyWindow(obt_display, self->button[i].window);
149         }
150
151         XDestroyWindow(obt_display, self->msg.window);
152
153         RrAppearanceFree(self->a_bg);
154
155         window_remove(self->super.window);
156         XDestroyWindow(obt_display, self->super.window);
157         g_free(self);
158     }
159 }
160
161 static void prompt_layout(ObPrompt *self, const Rect *area)
162 {
163     gint l, r, t, b;
164     guint i;
165     gint allbuttonsw, allbuttonsh, buttonx;
166     gint w, h;
167
168     const gint OUTSIDE_MARGIN = 4;
169     const gint MSG_BUTTON_SEPARATION = 4;
170     const gint BUTTON_SEPARATION = 4;
171
172     RrMargins(self->a_bg, &l, &t, &r, &b);
173     l += OUTSIDE_MARGIN;
174     t += OUTSIDE_MARGIN;
175     r += OUTSIDE_MARGIN;
176     b += OUTSIDE_MARGIN;
177
178     /* find the button sizes and how much space we need for them */
179     allbuttonsw = allbuttonsh = 0;
180     for (i = 0; i < self->n_buttons; ++i) {
181         gint bw, bh;
182
183         prompt_a_button->texture[0].data.text.string = self->button[i].text;
184         prompt_a_hover->texture[0].data.text.string = self->button[i].text;
185         prompt_a_press->texture[0].data.text.string = self->button[i].text;
186         RrMinSize(prompt_a_button, &bw, &bh);
187         self->button[i].width = bw;
188         self->button[i].height = bh;
189         RrMinSize(prompt_a_hover, &bw, &bh);
190         self->button[i].width = MAX(self->button[i].width, bw);
191         self->button[i].height = MAX(self->button[i].height, bh);
192         RrMinSize(prompt_a_press, &bw, &bh);
193         self->button[i].width = MAX(self->button[i].width, bw);
194         self->button[i].height = MAX(self->button[i].height, bh);
195
196         allbuttonsw += self->button[i].width + (i > 0 ? BUTTON_SEPARATION : 0);
197         allbuttonsh = MAX(allbuttonsh, self->button[i].height);
198     }
199
200     self->msg_wbound = MAX(allbuttonsw, area->width*3/5);
201
202     /* measure the text message area */
203     prompt_a_msg->texture[0].data.text.string = self->msg.text;
204     prompt_a_msg->texture[0].data.text.maxwidth = self->msg_wbound;
205     RrMinSize(prompt_a_msg, &self->msg.width, &self->msg.height);
206
207     g_print("height %d\n", self->msg.height);
208
209     /* width and height inside the outer margins */
210     w = MAX(self->msg.width, allbuttonsw);
211     h = self->msg.height + MSG_BUTTON_SEPARATION + allbuttonsh;
212
213     /* position the text message */
214     self->msg.x = l + (w - self->msg.width) / 2;
215     self->msg.y = t;
216
217     /* position the button buttons */
218     buttonx = l + (w - allbuttonsw) / 2;
219     for (i = 0; i < self->n_buttons; ++i) {
220         self->button[i].x = buttonx;
221         buttonx += self->button[i].width + BUTTON_SEPARATION;
222         self->button[i].y = t + h - allbuttonsh;
223         self->button[i].y += (allbuttonsh - self->button[i].height) / 2;
224     }
225
226     /* size and position the toplevel window */
227     self->width = w + l + r;
228     self->height = h + t + b;
229     self->x = (area->width - self->width) / 2;
230     self->y = (area->height - self->height) / 2;
231
232     /* move and resize the actual windows */
233     XMoveResizeWindow(obt_display, self->super.window,
234                       self->x, self->y, self->width, self->height);
235     XMoveResizeWindow(obt_display, self->msg.window,
236                       self->msg.x, self->msg.y,
237                       self->msg.width, self->msg.height);
238     for (i = 0; i < self->n_buttons; ++i)
239         XMoveResizeWindow(obt_display, self->button[i].window,
240                           self->button[i].x, self->button[i].y,
241                           self->button[i].width, self->button[i].height);
242 }
243
244 static void render_button(ObPrompt *self, ObPromptElement *e)
245 {
246     prompt_a_button->surface.parent = self->a_bg;
247     prompt_a_button->surface.parentx = e->x;
248     prompt_a_button->surface.parentx = e->y;
249
250     prompt_a_button->texture[0].data.text.string = e->text;
251     RrPaint(prompt_a_button, e->window, e->width, e->height);
252 }
253
254 static void render_all(ObPrompt *self)
255 {
256     guint i;
257
258     RrPaint(self->a_bg, self->super.window, self->width, self->height);
259
260     prompt_a_msg->surface.parent = self->a_bg;
261     prompt_a_msg->surface.parentx = self->msg.x;
262     prompt_a_msg->surface.parentx = self->msg.y;
263
264     prompt_a_msg->texture[0].data.text.string = self->msg.text;
265     prompt_a_msg->texture[0].data.text.maxwidth = self->msg_wbound;
266     RrPaint(prompt_a_msg, self->msg.window, self->msg.width, self->msg.height);
267
268     for (i = 0; i < self->n_buttons; ++i)
269         render_button(self, &self->button[i]);
270 }
271
272 void prompt_show(ObPrompt *self, const Rect *area)
273 {
274     if (self->mapped) return;
275
276     prompt_layout(self, area);
277     render_all(self);
278     XMapWindow(obt_display, self->super.window);
279
280     self->mapped = TRUE;
281 }
282
283 void prompt_hide(ObPrompt *self)
284 {
285     XUnmapWindow(obt_display, self->super.window);
286     self->mapped = FALSE;
287 }