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.
22 #include "obt/display.h"
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
= OB_WINDOW_CLASS_PROMPT
;
83 self
->super
.window
= XCreateWindow(obt_display
, obt_root(ob_screen
),
85 CopyFromParent
, InputOutput
,
87 CWOverrideRedirect
, &attrib
);
88 window_add(&self
->super
.window
, PROMPT_AS_WINDOW(self
));
90 self
->a_bg
= RrAppearanceCopy(ob_rr_theme
->osd_hilite_bg
);
92 self
->msg
.text
= g_strdup(msg
);
93 self
->msg
.window
= XCreateWindow(obt_display
, self
->super
.window
,
95 CopyFromParent
, InputOutput
,
96 CopyFromParent
, 0, NULL
);
97 XMapWindow(obt_display
, self
->msg
.window
);
100 for (c
= answers
; *c
!= NULL
; ++c
)
103 if (!self
->n_buttons
)
106 self
->button
= g_new(ObPromptElement
, self
->n_buttons
);
109 g_assert(self
->n_buttons
== 1); /* should be set to this above.. */
110 self
->button
[0].text
= g_strdup(_("OK"));
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
]);
118 for (i
= 0; i
< self
->n_buttons
; ++i
) {
119 self
->button
[i
].window
= XCreateWindow(obt_display
, self
->super
.window
,
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
));
130 void prompt_ref(ObPrompt
*self
)
135 void prompt_unref(ObPrompt
*self
)
137 if (self
&& --self
->ref
== 0) {
140 for (i
= 0; i
< self
->n_buttons
; ++i
) {
141 window_remove(self
->button
[i
].window
);
142 XDestroyWindow(obt_display
, self
->button
[i
].window
);
145 XDestroyWindow(obt_display
, self
->msg
.window
);
147 RrAppearanceFree(self
->a_bg
);
149 window_remove(self
->super
.window
);
150 XDestroyWindow(obt_display
, self
->super
.window
);
155 static void prompt_layout(ObPrompt
*self
, const Rect
*area
)
157 RrAppearance
*a_msg
= msg_appearance(self
);
160 gint allbuttonsw
, allbuttonsh
, buttonx
;
163 const gint OUTSIDE_MARGIN
= 4;
164 const gint MSG_BUTTON_SEPARATION
= 4;
165 const gint BUTTON_SEPARATION
= 4;
167 RrMargins(self
->a_bg
, &l
, &t
, &r
, &b
);
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
) {
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
);
191 allbuttonsw
+= self
->button
[i
].width
+ (i
> 0 ? BUTTON_SEPARATION
: 0);
192 allbuttonsh
= MAX(allbuttonsh
, self
->button
[i
].height
);
195 self
->msg_wbound
= MAX(allbuttonsw
, area
->width
*3/5);
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;
203 /* width and height inside the outer margins */
204 w
= MAX(self
->msg
.width
, allbuttonsw
);
205 h
= self
->msg
.height
+ MSG_BUTTON_SEPARATION
+ allbuttonsh
;
207 /* position the text message */
208 self
->msg
.x
= l
+ (w
- self
->msg
.width
) / 2;
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;
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;
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
);
238 static void render_button(ObPrompt
*self
, ObPromptElement
*e
)
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
;
244 prompt_a_button
->texture
[0].data
.text
.string
= e
->text
;
245 RrPaint(prompt_a_button
, e
->window
, e
->width
, e
->height
);
248 static void render_all(ObPrompt
*self
)
252 RrPaint(self
->a_bg
, self
->super
.window
, self
->width
, self
->height
);
254 msg_appearance()->surface
.parent
= self
->a_bg
;
255 msg_appearance()->surface
.parentx
= self
->msg
.x
;
256 msg_appearance()->surface
.parentx
= self
->msg
.y
;
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;
264 for (i
= 0; i
< self
->n_buttons
; ++i
)
265 render_button(self
, &self
->button
[i
]);
268 void prompt_show(ObPrompt
*self
, const Rect
*area
)
270 if (self
->mapped
) return;
272 prompt_layout(self
, area
);
274 XMapWindow(obt_display
, self
->super
.window
);
279 void prompt_hide(ObPrompt
*self
)
281 XUnmapWindow(obt_display
, self
->super
.window
);
282 self
->mapped
= FALSE
;