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
;
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
= OB_WINDOW_CLASS_PROMPT
;
88 self
->super
.window
= XCreateWindow(obt_display
, obt_root(ob_screen
),
89 0, 0, 1, 1, ob_rr_theme
->obwidth
,
90 CopyFromParent
, InputOutput
,
92 CWOverrideRedirect
| CWBorderPixel
,
94 window_add(&self
->super
.window
, PROMPT_AS_WINDOW(self
));
96 self
->a_bg
= RrAppearanceCopy(ob_rr_theme
->osd_hilite_bg
);
98 self
->msg
.text
= g_strdup(msg
);
99 self
->msg
.window
= XCreateWindow(obt_display
, self
->super
.window
,
101 CopyFromParent
, InputOutput
,
102 CopyFromParent
, 0, NULL
);
103 XMapWindow(obt_display
, self
->msg
.window
);
106 for (c
= answers
; *c
!= NULL
; ++c
)
109 if (!self
->n_buttons
)
112 self
->button
= g_new(ObPromptElement
, self
->n_buttons
);
115 g_assert(self
->n_buttons
== 1); /* should be set to this above.. */
116 self
->button
[0].text
= g_strdup(_("OK"));
119 g_assert(self
->n_buttons
> 0);
120 for (i
= 0; i
< self
->n_buttons
; ++i
)
121 self
->button
[i
].text
= g_strdup(answers
[i
]);
124 for (i
= 0; i
< self
->n_buttons
; ++i
) {
125 self
->button
[i
].window
= XCreateWindow(obt_display
, self
->super
.window
,
127 CopyFromParent
, InputOutput
,
128 CopyFromParent
, 0, NULL
);
129 XMapWindow(obt_display
, self
->button
[i
].window
);
130 window_add(&self
->button
[i
].window
, PROMPT_AS_WINDOW(self
));
136 void prompt_ref(ObPrompt
*self
)
141 void prompt_unref(ObPrompt
*self
)
143 if (self
&& --self
->ref
== 0) {
146 for (i
= 0; i
< self
->n_buttons
; ++i
) {
147 window_remove(self
->button
[i
].window
);
148 XDestroyWindow(obt_display
, self
->button
[i
].window
);
151 XDestroyWindow(obt_display
, self
->msg
.window
);
153 RrAppearanceFree(self
->a_bg
);
155 window_remove(self
->super
.window
);
156 XDestroyWindow(obt_display
, self
->super
.window
);
161 static void prompt_layout(ObPrompt
*self
, const Rect
*area
)
165 gint allbuttonsw
, allbuttonsh
, buttonx
;
168 const gint OUTSIDE_MARGIN
= 4;
169 const gint MSG_BUTTON_SEPARATION
= 4;
170 const gint BUTTON_SEPARATION
= 4;
172 RrMargins(self
->a_bg
, &l
, &t
, &r
, &b
);
178 /* find the button sizes and how much space we need for them */
179 allbuttonsw
= allbuttonsh
= 0;
180 for (i
= 0; i
< self
->n_buttons
; ++i
) {
183 prompt_a_button
->texture
[0].data
.text
.string
= self
->button
[i
].text
;
184 prompt_a_hover
->texture
[0].data
.text
.string
= self
->button
[i
].text
;
185 prompt_a_press
->texture
[0].data
.text
.string
= self
->button
[i
].text
;
186 RrMinSize(prompt_a_button
, &bw
, &bh
);
187 self
->button
[i
].width
= bw
;
188 self
->button
[i
].height
= bh
;
189 RrMinSize(prompt_a_hover
, &bw
, &bh
);
190 self
->button
[i
].width
= MAX(self
->button
[i
].width
, bw
);
191 self
->button
[i
].height
= MAX(self
->button
[i
].height
, bh
);
192 RrMinSize(prompt_a_press
, &bw
, &bh
);
193 self
->button
[i
].width
= MAX(self
->button
[i
].width
, bw
);
194 self
->button
[i
].height
= MAX(self
->button
[i
].height
, bh
);
196 allbuttonsw
+= self
->button
[i
].width
+ (i
> 0 ? BUTTON_SEPARATION
: 0);
197 allbuttonsh
= MAX(allbuttonsh
, self
->button
[i
].height
);
200 self
->msg_wbound
= MAX(allbuttonsw
, area
->width
*3/5);
202 /* measure the text message area */
203 prompt_a_msg
->texture
[0].data
.text
.string
= self
->msg
.text
;
204 prompt_a_msg
->texture
[0].data
.text
.maxwidth
= self
->msg_wbound
;
205 RrMinSize(prompt_a_msg
, &self
->msg
.width
, &self
->msg
.height
);
207 g_print("height %d\n", self
->msg
.height
);
209 /* width and height inside the outer margins */
210 w
= MAX(self
->msg
.width
, allbuttonsw
);
211 h
= self
->msg
.height
+ MSG_BUTTON_SEPARATION
+ allbuttonsh
;
213 /* position the text message */
214 self
->msg
.x
= l
+ (w
- self
->msg
.width
) / 2;
217 /* position the button buttons */
218 buttonx
= l
+ (w
- allbuttonsw
) / 2;
219 for (i
= 0; i
< self
->n_buttons
; ++i
) {
220 self
->button
[i
].x
= buttonx
;
221 buttonx
+= self
->button
[i
].width
+ BUTTON_SEPARATION
;
222 self
->button
[i
].y
= t
+ h
- allbuttonsh
;
223 self
->button
[i
].y
+= (allbuttonsh
- self
->button
[i
].height
) / 2;
226 /* size and position the toplevel window */
227 self
->width
= w
+ l
+ r
;
228 self
->height
= h
+ t
+ b
;
229 self
->x
= (area
->width
- self
->width
) / 2;
230 self
->y
= (area
->height
- self
->height
) / 2;
232 /* move and resize the actual windows */
233 XMoveResizeWindow(obt_display
, self
->super
.window
,
234 self
->x
, self
->y
, self
->width
, self
->height
);
235 XMoveResizeWindow(obt_display
, self
->msg
.window
,
236 self
->msg
.x
, self
->msg
.y
,
237 self
->msg
.width
, self
->msg
.height
);
238 for (i
= 0; i
< self
->n_buttons
; ++i
)
239 XMoveResizeWindow(obt_display
, self
->button
[i
].window
,
240 self
->button
[i
].x
, self
->button
[i
].y
,
241 self
->button
[i
].width
, self
->button
[i
].height
);
244 static void render_button(ObPrompt
*self
, ObPromptElement
*e
)
246 prompt_a_button
->surface
.parent
= self
->a_bg
;
247 prompt_a_button
->surface
.parentx
= e
->x
;
248 prompt_a_button
->surface
.parentx
= e
->y
;
250 prompt_a_button
->texture
[0].data
.text
.string
= e
->text
;
251 RrPaint(prompt_a_button
, e
->window
, e
->width
, e
->height
);
254 static void render_all(ObPrompt
*self
)
258 RrPaint(self
->a_bg
, self
->super
.window
, self
->width
, self
->height
);
260 prompt_a_msg
->surface
.parent
= self
->a_bg
;
261 prompt_a_msg
->surface
.parentx
= self
->msg
.x
;
262 prompt_a_msg
->surface
.parentx
= self
->msg
.y
;
264 prompt_a_msg
->texture
[0].data
.text
.string
= self
->msg
.text
;
265 prompt_a_msg
->texture
[0].data
.text
.maxwidth
= self
->msg_wbound
;
266 RrPaint(prompt_a_msg
, self
->msg
.window
, self
->msg
.width
, self
->msg
.height
);
268 for (i
= 0; i
< self
->n_buttons
; ++i
)
269 render_button(self
, &self
->button
[i
]);
272 void prompt_show(ObPrompt
*self
, const Rect
*area
)
274 if (self
->mapped
) return;
276 prompt_layout(self
, area
);
278 XMapWindow(obt_display
, self
->super
.window
);
283 void prompt_hide(ObPrompt
*self
)
285 XUnmapWindow(obt_display
, self
->super
.window
);
286 self
->mapped
= FALSE
;