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