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