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
;
31 /* we change the max width which would screw with others */
32 static RrAppearance
*prompt_a_msg
;
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
;
63 prompt_a_msg
= RrAppearanceCopy(ob_rr_theme
->osd_hilite_label
);
64 prompt_a_msg
->texture
[0].data
.text
.flow
= TRUE
;
67 void prompt_shutdown(gboolean reconfig
)
69 RrAppearanceFree(prompt_a_button
);
70 RrAppearanceFree(prompt_a_hover
);
71 RrAppearanceFree(prompt_a_press
);
72 RrAppearanceFree(prompt_a_msg
);
75 ObPrompt
* prompt_new(const gchar
*msg
, const gchar
*const *answers
)
78 XSetWindowAttributes attrib
;
80 const gchar
*const *c
;
82 attrib
.override_redirect
= TRUE
;
83 attrib
.border_pixel
= RrColorPixel(ob_rr_theme
->osd_border_color
);
85 self
= g_new0(ObPrompt
, 1);
87 self
->super
.type
= Window_Prompt
;
88 self
->super
.window
= XCreateWindow(ob_display
,
89 RootWindow(ob_display
, ob_screen
),
91 0, 0, 1, 1, ob_rr_theme
->obwidth
,
92 CopyFromParent
, InputOutput
,
94 CWOverrideRedirect
| CWBorderPixel
,
96 g_hash_table_insert(window_map
, &self
->super
.window
,
97 PROMPT_AS_WINDOW(self
));
99 self
->a_bg
= RrAppearanceCopy(ob_rr_theme
->osd_hilite_bg
);
101 self
->msg
.text
= g_strdup(msg
);
102 self
->msg
.window
= XCreateWindow(ob_display
, self
->super
.window
,
104 CopyFromParent
, InputOutput
,
105 CopyFromParent
, 0, NULL
);
106 XMapWindow(ob_display
, self
->msg
.window
);
109 for (c
= answers
; *c
!= NULL
; ++c
)
112 if (!self
->n_buttons
)
115 self
->button
= g_new(ObPromptElement
, self
->n_buttons
);
118 g_assert(self
->n_buttons
== 1); /* should be set to this above.. */
119 self
->button
[0].text
= g_strdup(_("OK"));
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
]);
127 for (i
= 0; i
< self
->n_buttons
; ++i
) {
128 self
->button
[i
].window
= XCreateWindow(ob_display
, self
->super
.window
,
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
));
140 void prompt_ref(ObPrompt
*self
)
145 void prompt_unref(ObPrompt
*self
)
147 if (self
&& --self
->ref
== 0) {
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
);
155 XDestroyWindow(ob_display
, self
->msg
.window
);
157 RrAppearanceFree(self
->a_bg
);
159 g_hash_table_remove(window_map
, &self
->super
.window
);
160 XDestroyWindow(ob_display
, self
->super
.window
);
165 static void prompt_layout(ObPrompt
*self
, const Rect
*area
)
169 gint allbuttonsw
, allbuttonsh
, buttonx
;
172 const gint OUTSIDE_MARGIN
= 4;
173 const gint MSG_BUTTON_SEPARATION
= 4;
174 const gint BUTTON_SEPARATION
= 4;
176 RrMargins(self
->a_bg
, &l
, &t
, &r
, &b
);
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
) {
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
);
200 allbuttonsw
+= self
->button
[i
].width
+ (i
> 0 ? BUTTON_SEPARATION
: 0);
201 allbuttonsh
= MAX(allbuttonsh
, self
->button
[i
].height
);
204 self
->msg_wbound
= MAX(allbuttonsw
, area
->width
*3/5);
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
);
211 g_print("height %d\n", self
->msg
.height
);
213 /* width and height inside the outer margins */
214 w
= MAX(self
->msg
.width
, allbuttonsw
);
215 h
= self
->msg
.height
+ MSG_BUTTON_SEPARATION
+ allbuttonsh
;
217 /* position the text message */
218 self
->msg
.x
= l
+ (w
- self
->msg
.width
) / 2;
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;
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;
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
);
248 static void render_button(ObPrompt
*self
, ObPromptElement
*e
)
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
;
254 prompt_a_button
->texture
[0].data
.text
.string
= e
->text
;
255 RrPaint(prompt_a_button
, e
->window
, e
->width
, e
->height
);
258 static void render_all(ObPrompt
*self
)
262 RrPaint(self
->a_bg
, self
->super
.window
, self
->width
, self
->height
);
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
;
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
);
272 for (i
= 0; i
< self
->n_buttons
; ++i
)
273 render_button(self
, &self
->button
[i
]);
276 void prompt_show(ObPrompt
*self
, const Rect
*area
)
278 if (self
->mapped
) return;
280 prompt_layout(self
, area
);
282 XMapWindow(ob_display
, self
->super
.window
);
287 void prompt_hide(ObPrompt
*self
)
289 XUnmapWindow(ob_display
, self
->super
.window
);
290 self
->mapped
= FALSE
;