d58402bd78da00e9f82a893291dd89e122ba6809
[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 "openbox.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 = Window_Prompt;
88     self->super.window = XCreateWindow(ob_display,
89                                        RootWindow(ob_display, ob_screen),
90                                        0, 0, 1, 1, 0,
91                                        0, 0, 1, 1, ob_rr_theme->obwidth,
92                                        CopyFromParent, InputOutput,
93                                        CopyFromParent,
94                                        CWOverrideRedirect | CWBorderPixel,
95                                        &attrib);
96     g_hash_table_insert(window_map, &self->super.window,
97                         PROMPT_AS_WINDOW(self));
98
99     self->a_bg = RrAppearanceCopy(ob_rr_theme->osd_hilite_bg);
100
101     self->msg.text = g_strdup(msg);
102     self->msg.window = XCreateWindow(ob_display, self->super.window,
103                                      0, 0, 1, 1, 0,
104                                      CopyFromParent, InputOutput,
105                                      CopyFromParent, 0, NULL);
106     XMapWindow(ob_display, self->msg.window);
107
108     self->n_buttons = 0;
109     for (c = answers; *c != NULL; ++c)
110         ++self->n_buttons;
111
112     if (!self->n_buttons)
113         self->n_buttons = 1;
114
115     self->button = g_new(ObPromptElement, self->n_buttons);
116
117     if (!answers) {
118         g_assert(self->n_buttons == 1); /* should be set to this above.. */
119         self->button[0].text = g_strdup(_("OK"));
120     }
121     else {
122         g_assert(self->n_buttons > 0);
123         for (i = 0; i < self->n_buttons; ++i)
124             self->button[i].text = g_strdup(answers[i]);
125     }
126
127     for (i = 0; i < self->n_buttons; ++i) {
128         self->button[i].window = XCreateWindow(ob_display, self->super.window,
129                                                0, 0, 1, 1, 0,
130                                                CopyFromParent, InputOutput,
131                                                CopyFromParent, 0, NULL);
132         XMapWindow(ob_display, self->button[i].window);
133         g_hash_table_insert(window_map, &self->button[i].window,
134                             PROMPT_AS_WINDOW(self));
135     }
136
137     return self;
138 }
139
140 void prompt_ref(ObPrompt *self)
141 {
142     ++self->ref;
143 }
144
145 void prompt_unref(ObPrompt *self)
146 {
147     if (self && --self->ref == 0) {
148         guint i;
149
150         for (i = 0; i < self->n_buttons; ++i) {
151             g_hash_table_remove(window_map, &self->button[i].window);
152             XDestroyWindow(ob_display, self->button[i].window);
153         }
154
155         XDestroyWindow(ob_display, self->msg.window);
156
157         RrAppearanceFree(self->a_bg);
158
159         g_hash_table_remove(window_map, &self->super.window);
160         XDestroyWindow(ob_display, self->super.window);
161         g_free(self);
162     }
163 }
164
165 static void prompt_layout(ObPrompt *self, const Rect *area)
166 {
167     gint l, r, t, b;
168     guint i;
169     gint allbuttonsw, allbuttonsh, buttonx;
170     gint w, h;
171
172     const gint OUTSIDE_MARGIN = 4;
173     const gint MSG_BUTTON_SEPARATION = 4;
174     const gint BUTTON_SEPARATION = 4;
175
176     RrMargins(self->a_bg, &l, &t, &r, &b);
177     l += OUTSIDE_MARGIN;
178     t += OUTSIDE_MARGIN;
179     r += OUTSIDE_MARGIN;
180     b += OUTSIDE_MARGIN;
181
182     /* find the button sizes and how much space we need for them */
183     allbuttonsw = allbuttonsh = 0;
184     for (i = 0; i < self->n_buttons; ++i) {
185         gint bw, bh;
186
187         prompt_a_button->texture[0].data.text.string = self->button[i].text;
188         prompt_a_hover->texture[0].data.text.string = self->button[i].text;
189         prompt_a_press->texture[0].data.text.string = self->button[i].text;
190         RrMinSize(prompt_a_button, &bw, &bh);
191         self->button[i].width = bw;
192         self->button[i].height = bh;
193         RrMinSize(prompt_a_hover, &bw, &bh);
194         self->button[i].width = MAX(self->button[i].width, bw);
195         self->button[i].height = MAX(self->button[i].height, bh);
196         RrMinSize(prompt_a_press, &bw, &bh);
197         self->button[i].width = MAX(self->button[i].width, bw);
198         self->button[i].height = MAX(self->button[i].height, bh);
199
200         allbuttonsw += self->button[i].width + (i > 0 ? BUTTON_SEPARATION : 0);
201         allbuttonsh = MAX(allbuttonsh, self->button[i].height);
202     }
203
204     self->msg_wbound = MAX(allbuttonsw, area->width*3/5);
205
206     /* measure the text message area */
207     prompt_a_msg->texture[0].data.text.string = self->msg.text;
208     prompt_a_msg->texture[0].data.text.maxwidth = self->msg_wbound;
209     RrMinSize(prompt_a_msg, &self->msg.width, &self->msg.height);
210
211     g_print("height %d\n", self->msg.height);
212
213     /* width and height inside the outer margins */
214     w = MAX(self->msg.width, allbuttonsw);
215     h = self->msg.height + MSG_BUTTON_SEPARATION + allbuttonsh;
216
217     /* position the text message */
218     self->msg.x = l + (w - self->msg.width) / 2;
219     self->msg.y = t;
220
221     /* position the button buttons */
222     buttonx = l + (w - allbuttonsw) / 2;
223     for (i = 0; i < self->n_buttons; ++i) {
224         self->button[i].x = buttonx;
225         buttonx += self->button[i].width + BUTTON_SEPARATION;
226         self->button[i].y = t + h - allbuttonsh;
227         self->button[i].y += (allbuttonsh - self->button[i].height) / 2;
228     }
229
230     /* size and position the toplevel window */
231     self->width = w + l + r;
232     self->height = h + t + b;
233     self->x = (area->width - self->width) / 2;
234     self->y = (area->height - self->height) / 2;
235
236     /* move and resize the actual windows */
237     XMoveResizeWindow(ob_display, self->super.window,
238                       self->x, self->y, self->width, self->height);
239     XMoveResizeWindow(ob_display, self->msg.window,
240                       self->msg.x, self->msg.y,
241                       self->msg.width, self->msg.height);
242     for (i = 0; i < self->n_buttons; ++i)
243         XMoveResizeWindow(ob_display, self->button[i].window,
244                           self->button[i].x, self->button[i].y,
245                           self->button[i].width, self->button[i].height);
246 }
247
248 static void render_button(ObPrompt *self, ObPromptElement *e)
249 {
250     prompt_a_button->surface.parent = self->a_bg;
251     prompt_a_button->surface.parentx = e->x;
252     prompt_a_button->surface.parentx = e->y;
253
254     prompt_a_button->texture[0].data.text.string = e->text;
255     RrPaint(prompt_a_button, e->window, e->width, e->height);
256 }
257
258 static void render_all(ObPrompt *self)
259 {
260     guint i;
261
262     RrPaint(self->a_bg, self->super.window, self->width, self->height);
263
264     prompt_a_msg->surface.parent = self->a_bg;
265     prompt_a_msg->surface.parentx = self->msg.x;
266     prompt_a_msg->surface.parentx = self->msg.y;
267
268     prompt_a_msg->texture[0].data.text.string = self->msg.text;
269     prompt_a_msg->texture[0].data.text.maxwidth = self->msg_wbound;
270     RrPaint(prompt_a_msg, self->msg.window, self->msg.width, self->msg.height);
271
272     for (i = 0; i < self->n_buttons; ++i)
273         render_button(self, &self->button[i]);
274 }
275
276 void prompt_show(ObPrompt *self, const Rect *area)
277 {
278     if (self->mapped) return;
279
280     prompt_layout(self, area);
281     render_all(self);
282     XMapWindow(ob_display, self->super.window);
283
284     self->mapped = TRUE;
285 }
286
287 void prompt_hide(ObPrompt *self)
288 {
289     XUnmapWindow(ob_display, self->super.window);
290     self->mapped = FALSE;
291 }