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.
23 #include "obt/display.h"
27 static GList
*prompt_list
= NULL
;
29 /* we construct these */
30 static RrAppearance
*prompt_a_button
;
31 static RrAppearance
*prompt_a_hover
;
32 static RrAppearance
*prompt_a_press
;
33 /* we change the max width which would screw with others */
34 static RrAppearance
*prompt_a_msg
;
36 static void prompt_layout(ObPrompt
*self
);
37 static void render_all(ObPrompt
*self
);
38 static void render_button(ObPrompt
*self
, ObPromptElement
*e
);
40 void prompt_startup(gboolean reconfig
)
42 RrColor
*c_button
, *c_hover
, *c_press
;
44 prompt_a_button
= RrAppearanceCopy(ob_rr_theme
->a_focused_unpressed_close
);
45 prompt_a_hover
= RrAppearanceCopy(ob_rr_theme
->a_hover_focused_close
);
46 prompt_a_press
= RrAppearanceCopy(ob_rr_theme
->a_focused_pressed_close
);
48 c_button
= prompt_a_button
->texture
[0].data
.mask
.color
;
49 c_hover
= prompt_a_button
->texture
[0].data
.mask
.color
;
50 c_press
= prompt_a_button
->texture
[0].data
.mask
.color
;
52 RrAppearanceRemoveTextures(prompt_a_button
);
53 RrAppearanceRemoveTextures(prompt_a_hover
);
54 RrAppearanceRemoveTextures(prompt_a_press
);
56 RrAppearanceAddTextures(prompt_a_button
, 1);
57 RrAppearanceAddTextures(prompt_a_hover
, 1);
58 RrAppearanceAddTextures(prompt_a_press
, 1);
60 /* totally cheating here.. */
61 prompt_a_button
->texture
[0] = ob_rr_theme
->osd_hilite_label
->texture
[0];
62 prompt_a_hover
->texture
[0] = ob_rr_theme
->osd_hilite_label
->texture
[0];
63 prompt_a_press
->texture
[0] = ob_rr_theme
->osd_hilite_label
->texture
[0];
65 prompt_a_button
->texture
[0].data
.text
.color
= c_button
;
66 prompt_a_hover
->texture
[0].data
.text
.color
= c_hover
;
67 prompt_a_press
->texture
[0].data
.text
.color
= c_press
;
69 prompt_a_msg
= RrAppearanceCopy(ob_rr_theme
->osd_hilite_label
);
70 prompt_a_msg
->texture
[0].data
.text
.flow
= TRUE
;
74 for (it
= prompt_list
; it
; it
= g_list_next(it
)) {
75 ObPrompt
*p
= it
->data
;
82 void prompt_shutdown(gboolean reconfig
)
84 RrAppearanceFree(prompt_a_button
);
85 RrAppearanceFree(prompt_a_hover
);
86 RrAppearanceFree(prompt_a_press
);
87 RrAppearanceFree(prompt_a_msg
);
90 ObPrompt
* prompt_new(const gchar
*msg
, const gchar
*const *answers
)
93 XSetWindowAttributes attrib
;
95 const gchar
*const *c
;
97 attrib
.override_redirect
= FALSE
;
98 attrib
.border_pixel
= RrColorPixel(ob_rr_theme
->osd_border_color
);
100 self
= g_new0(ObPrompt
, 1);
102 self
->super
.type
= OB_WINDOW_CLASS_PROMPT
;
103 self
->super
.window
= XCreateWindow(obt_display
, obt_root(ob_screen
),
104 0, 0, 1, 1, ob_rr_theme
->obwidth
,
105 CopyFromParent
, InputOutput
,
107 CWOverrideRedirect
| CWBorderPixel
,
110 OBT_PROP_SET32(self
->super
.window
, NET_WM_WINDOW_TYPE
, ATOM
,
111 OBT_PROP_ATOM(NET_WM_WINDOW_TYPE_DIALOG
));
113 self
->a_bg
= RrAppearanceCopy(ob_rr_theme
->osd_hilite_bg
);
115 self
->msg
.text
= g_strdup(msg
);
116 self
->msg
.window
= XCreateWindow(obt_display
, self
->super
.window
,
118 CopyFromParent
, InputOutput
,
119 CopyFromParent
, 0, NULL
);
120 XMapWindow(obt_display
, self
->msg
.window
);
123 for (c
= answers
; *c
!= NULL
; ++c
)
126 if (!self
->n_buttons
)
129 self
->button
= g_new(ObPromptElement
, self
->n_buttons
);
132 g_assert(self
->n_buttons
== 1); /* should be set to this above.. */
133 self
->button
[0].text
= g_strdup(_("OK"));
136 g_assert(self
->n_buttons
> 0);
137 for (i
= 0; i
< self
->n_buttons
; ++i
)
138 self
->button
[i
].text
= g_strdup(answers
[i
]);
141 for (i
= 0; i
< self
->n_buttons
; ++i
) {
142 self
->button
[i
].window
= XCreateWindow(obt_display
, self
->super
.window
,
144 CopyFromParent
, InputOutput
,
145 CopyFromParent
, 0, NULL
);
146 XMapWindow(obt_display
, self
->button
[i
].window
);
147 window_add(&self
->button
[i
].window
, PROMPT_AS_WINDOW(self
));
150 prompt_list
= g_list_prepend(prompt_list
, self
);
155 void prompt_ref(ObPrompt
*self
)
160 void prompt_unref(ObPrompt
*self
)
162 if (self
&& --self
->ref
== 0) {
165 prompt_list
= g_list_remove(prompt_list
, self
);
167 for (i
= 0; i
< self
->n_buttons
; ++i
) {
168 window_remove(self
->button
[i
].window
);
169 XDestroyWindow(obt_display
, self
->button
[i
].window
);
172 XDestroyWindow(obt_display
, self
->msg
.window
);
174 RrAppearanceFree(self
->a_bg
);
176 XDestroyWindow(obt_display
, self
->super
.window
);
181 static void prompt_layout(ObPrompt
*self
)
185 gint allbuttonsw
, allbuttonsh
, buttonx
;
189 const gint OUTSIDE_MARGIN
= 4;
190 const gint MSG_BUTTON_SEPARATION
= 4;
191 const gint BUTTON_SEPARATION
= 4;
192 const gint MAX_WIDTH
= 600;
194 RrMargins(self
->a_bg
, &l
, &t
, &r
, &b
);
201 Rect
*area
= screen_physical_area_all_monitors();
202 maxw
= MIN(MAX_WIDTH
, area
->width
*4/5);
206 /* find the button sizes and how much space we need for them */
207 allbuttonsw
= allbuttonsh
= 0;
208 for (i
= 0; i
< self
->n_buttons
; ++i
) {
211 prompt_a_button
->texture
[0].data
.text
.string
= self
->button
[i
].text
;
212 prompt_a_hover
->texture
[0].data
.text
.string
= self
->button
[i
].text
;
213 prompt_a_press
->texture
[0].data
.text
.string
= self
->button
[i
].text
;
214 RrMinSize(prompt_a_button
, &bw
, &bh
);
215 self
->button
[i
].width
= bw
;
216 self
->button
[i
].height
= bh
;
217 RrMinSize(prompt_a_hover
, &bw
, &bh
);
218 self
->button
[i
].width
= MAX(self
->button
[i
].width
, bw
);
219 self
->button
[i
].height
= MAX(self
->button
[i
].height
, bh
);
220 RrMinSize(prompt_a_press
, &bw
, &bh
);
221 self
->button
[i
].width
= MAX(self
->button
[i
].width
, bw
);
222 self
->button
[i
].height
= MAX(self
->button
[i
].height
, bh
);
224 allbuttonsw
+= self
->button
[i
].width
+ (i
> 0 ? BUTTON_SEPARATION
: 0);
225 allbuttonsh
= MAX(allbuttonsh
, self
->button
[i
].height
);
228 self
->msg_wbound
= MAX(allbuttonsw
, maxw
);
230 /* measure the text message area */
231 prompt_a_msg
->texture
[0].data
.text
.string
= self
->msg
.text
;
232 prompt_a_msg
->texture
[0].data
.text
.maxwidth
= self
->msg_wbound
;
233 RrMinSize(prompt_a_msg
, &self
->msg
.width
, &self
->msg
.height
);
235 /* width and height inside the outer margins */
236 w
= MAX(self
->msg
.width
, allbuttonsw
);
237 h
= self
->msg
.height
+ MSG_BUTTON_SEPARATION
+ allbuttonsh
;
239 /* position the text message */
240 self
->msg
.x
= l
+ (w
- self
->msg
.width
) / 2;
243 /* position the button buttons */
244 buttonx
= l
+ (w
- allbuttonsw
) / 2;
245 for (i
= 0; i
< self
->n_buttons
; ++i
) {
246 self
->button
[i
].x
= buttonx
;
247 buttonx
+= self
->button
[i
].width
+ BUTTON_SEPARATION
;
248 self
->button
[i
].y
= t
+ h
- allbuttonsh
;
249 self
->button
[i
].y
+= (allbuttonsh
- self
->button
[i
].height
) / 2;
252 /* size and position the toplevel window */
253 self
->width
= w
+ l
+ r
;
254 self
->height
= h
+ t
+ b
;
256 /* move and resize the actual windows */
257 XResizeWindow(obt_display
, self
->super
.window
, self
->width
, self
->height
);
258 XMoveResizeWindow(obt_display
, self
->msg
.window
,
259 self
->msg
.x
, self
->msg
.y
,
260 self
->msg
.width
, self
->msg
.height
);
261 for (i
= 0; i
< self
->n_buttons
; ++i
)
262 XMoveResizeWindow(obt_display
, self
->button
[i
].window
,
263 self
->button
[i
].x
, self
->button
[i
].y
,
264 self
->button
[i
].width
, self
->button
[i
].height
);
267 static void render_button(ObPrompt
*self
, ObPromptElement
*e
)
269 prompt_a_button
->surface
.parent
= self
->a_bg
;
270 prompt_a_button
->surface
.parentx
= e
->x
;
271 prompt_a_button
->surface
.parentx
= e
->y
;
273 prompt_a_button
->texture
[0].data
.text
.string
= e
->text
;
274 RrPaint(prompt_a_button
, e
->window
, e
->width
, e
->height
);
277 static void render_all(ObPrompt
*self
)
281 RrPaint(self
->a_bg
, self
->super
.window
, self
->width
, self
->height
);
283 prompt_a_msg
->surface
.parent
= self
->a_bg
;
284 prompt_a_msg
->surface
.parentx
= self
->msg
.x
;
285 prompt_a_msg
->surface
.parenty
= self
->msg
.y
;
287 prompt_a_msg
->texture
[0].data
.text
.string
= self
->msg
.text
;
288 prompt_a_msg
->texture
[0].data
.text
.maxwidth
= self
->msg_wbound
;
289 RrPaint(prompt_a_msg
, self
->msg
.window
, self
->msg
.width
, self
->msg
.height
);
291 for (i
= 0; i
< self
->n_buttons
; ++i
)
292 render_button(self
, &self
->button
[i
]);
295 void prompt_show(ObPrompt
*self
, ObClient
*parent
)
299 if (self
->mapped
) return;
304 /* you can't resize the prompt */
305 hints
.flags
= PMinSize
| PMaxSize
;
306 hints
.min_width
= hints
.max_width
= self
->width
;
307 hints
.min_height
= hints
.max_height
= self
->height
;
308 XSetWMNormalHints(obt_display
, self
->super
.window
, &hints
);
310 XSetTransientForHint(obt_display
, (parent
? parent
->window
: 0),
313 client_manage(self
->super
.window
, self
);
318 void prompt_hide(ObPrompt
*self
)
320 XUnmapWindow(obt_display
, self
->super
.window
);
321 self
->mapped
= FALSE
;
324 void prompt_hide_window(Window window
)
329 for (it
= prompt_list
; it
; it
= g_list_next(it
)) {
331 if (p
->super
.window
== window
) break;
333 g_assert(it
!= NULL
);