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 static GList
*prompt_list
= NULL
;
27 /* we construct these */
28 static RrAppearance
*prompt_a_button
;
29 static RrAppearance
*prompt_a_hover
;
30 static RrAppearance
*prompt_a_press
;
32 #define msg_appearance(self) (ob_rr_theme->osd_hilite_label)
34 void prompt_startup(gboolean reconfig
)
36 RrColor
*c_button
, *c_hover
, *c_press
;
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
);
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
;
46 RrAppearanceRemoveTextures(prompt_a_button
);
47 RrAppearanceRemoveTextures(prompt_a_hover
);
48 RrAppearanceRemoveTextures(prompt_a_press
);
50 RrAppearanceAddTextures(prompt_a_button
, 1);
51 RrAppearanceAddTextures(prompt_a_hover
, 1);
52 RrAppearanceAddTextures(prompt_a_press
, 1);
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];
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
;
64 void prompt_shutdown(gboolean reconfig
)
66 RrAppearanceFree(prompt_a_button
);
67 RrAppearanceFree(prompt_a_hover
);
68 RrAppearanceFree(prompt_a_press
);
71 ObPrompt
* prompt_new(const gchar
*msg
, const gchar
*const *answers
)
74 XSetWindowAttributes attrib
;
76 const gchar
*const *c
;
78 attrib
.override_redirect
= TRUE
;
80 self
= g_new0(ObPrompt
, 1);
82 self
->super
.type
= Window_Prompt
;
83 self
->super
.window
= XCreateWindow(ob_display
,
84 RootWindow(ob_display
, ob_screen
),
86 CopyFromParent
, InputOutput
,
88 CWOverrideRedirect
, &attrib
);
89 g_hash_table_insert(window_map
, &self
->super
.window
,
90 PROMPT_AS_WINDOW(self
));
92 self
->a_bg
= RrAppearanceCopy(ob_rr_theme
->osd_hilite_bg
);
94 self
->msg
.text
= g_strdup(msg
);
95 self
->msg
.window
= XCreateWindow(ob_display
, self
->super
.window
,
97 CopyFromParent
, InputOutput
,
98 CopyFromParent
, 0, NULL
);
99 XMapWindow(ob_display
, self
->msg
.window
);
102 for (c
= answers
; *c
!= NULL
; ++c
)
105 if (!self
->n_buttons
)
108 self
->button
= g_new(ObPromptElement
, self
->n_buttons
);
111 g_assert(self
->n_buttons
== 1); /* should be set to this above.. */
112 self
->button
[0].text
= g_strdup(_("OK"));
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
]);
120 for (i
= 0; i
< self
->n_buttons
; ++i
) {
121 self
->button
[i
].window
= XCreateWindow(ob_display
, self
->super
.window
,
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
));
133 void prompt_ref(ObPrompt
*self
)
138 void prompt_unref(ObPrompt
*self
)
140 if (self
&& --self
->ref
== 0) {
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
);
148 XDestroyWindow(ob_display
, self
->msg
.window
);
150 RrAppearanceFree(self
->a_bg
);
152 g_hash_table_remove(window_map
, &self
->super
.window
);
153 XDestroyWindow(ob_display
, self
->super
.window
);
158 static void prompt_layout(ObPrompt
*self
, const Rect
*area
)
160 RrAppearance
*a_msg
= msg_appearance(self
);
163 gint allbuttonsw
, allbuttonsh
, buttonx
;
166 const gint OUTSIDE_MARGIN
= 4;
167 const gint MSG_BUTTON_SEPARATION
= 4;
168 const gint BUTTON_SEPARATION
= 4;
170 RrMargins(self
->a_bg
, &l
, &t
, &r
, &b
);
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
) {
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
);
194 allbuttonsw
+= self
->button
[i
].width
+ (i
> 0 ? BUTTON_SEPARATION
: 0);
195 allbuttonsh
= MAX(allbuttonsh
, self
->button
[i
].height
);
198 self
->msg_wbound
= MAX(allbuttonsw
, area
->width
*3/5);
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;
206 /* width and height inside the outer margins */
207 w
= MAX(self
->msg
.width
, allbuttonsw
);
208 h
= self
->msg
.height
+ MSG_BUTTON_SEPARATION
+ allbuttonsh
;
210 /* position the text message */
211 self
->msg
.x
= l
+ (w
- self
->msg
.width
) / 2;
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;
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;
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
);
241 static void render_button(ObPrompt
*self
, ObPromptElement
*e
)
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
;
247 prompt_a_button
->texture
[0].data
.text
.string
= e
->text
;
248 RrPaint(prompt_a_button
, e
->window
, e
->width
, e
->height
);
251 static void render_all(ObPrompt
*self
)
255 RrPaint(self
->a_bg
, self
->super
.window
, self
->width
, self
->height
);
257 msg_appearance()->surface
.parent
= self
->a_bg
;
258 msg_appearance()->surface
.parentx
= self
->msg
.x
;
259 msg_appearance()->surface
.parentx
= self
->msg
.y
;
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;
267 for (i
= 0; i
< self
->n_buttons
; ++i
)
268 render_button(self
, &self
->button
[i
]);
271 void prompt_show(ObPrompt
*self
, const Rect
*area
)
273 if (self
->mapped
) return;
275 prompt_layout(self
, area
);
277 XMapWindow(ob_display
, self
->super
.window
);
282 void prompt_hide(ObPrompt
*self
)
284 XUnmapWindow(ob_display
, self
->super
.window
);
285 self
->mapped
= FALSE
;