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.
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
= Window_Prompt
;
103 self
->super
.window
= XCreateWindow(ob_display
,
104 RootWindow(ob_display
, ob_screen
),
105 0, 0, 1, 1, ob_rr_theme
->obwidth
,
106 CopyFromParent
, InputOutput
,
108 CWOverrideRedirect
| CWBorderPixel
,
111 PROP_SET32(self
->super
.window
, net_wm_window_type
, atom
,
112 prop_atoms
.net_wm_window_type_dialog
);
114 self
->a_bg
= RrAppearanceCopy(ob_rr_theme
->osd_hilite_bg
);
116 self
->msg
.text
= g_strdup(msg
);
117 self
->msg
.window
= XCreateWindow(ob_display
, self
->super
.window
,
119 CopyFromParent
, InputOutput
,
120 CopyFromParent
, 0, NULL
);
121 XMapWindow(ob_display
, self
->msg
.window
);
124 for (c
= answers
; *c
!= NULL
; ++c
)
127 if (!self
->n_buttons
)
130 self
->button
= g_new(ObPromptElement
, self
->n_buttons
);
133 g_assert(self
->n_buttons
== 1); /* should be set to this above.. */
134 self
->button
[0].text
= g_strdup(_("OK"));
137 g_assert(self
->n_buttons
> 0);
138 for (i
= 0; i
< self
->n_buttons
; ++i
)
139 self
->button
[i
].text
= g_strdup(answers
[i
]);
142 for (i
= 0; i
< self
->n_buttons
; ++i
) {
143 self
->button
[i
].window
= XCreateWindow(ob_display
, self
->super
.window
,
145 CopyFromParent
, InputOutput
,
146 CopyFromParent
, 0, NULL
);
147 XMapWindow(ob_display
, self
->button
[i
].window
);
148 g_hash_table_insert(window_map
, &self
->button
[i
].window
,
149 PROMPT_AS_WINDOW(self
));
152 prompt_list
= g_list_prepend(prompt_list
, self
);
157 void prompt_ref(ObPrompt
*self
)
162 void prompt_unref(ObPrompt
*self
)
164 if (self
&& --self
->ref
== 0) {
167 prompt_list
= g_list_remove(prompt_list
, self
);
169 for (i
= 0; i
< self
->n_buttons
; ++i
) {
170 g_hash_table_remove(window_map
, &self
->button
[i
].window
);
171 XDestroyWindow(ob_display
, self
->button
[i
].window
);
174 XDestroyWindow(ob_display
, self
->msg
.window
);
176 RrAppearanceFree(self
->a_bg
);
178 XDestroyWindow(ob_display
, self
->super
.window
);
183 static void prompt_layout(ObPrompt
*self
)
187 gint allbuttonsw
, allbuttonsh
, buttonx
;
191 const gint OUTSIDE_MARGIN
= 4;
192 const gint MSG_BUTTON_SEPARATION
= 4;
193 const gint BUTTON_SEPARATION
= 4;
194 const gint MAX_WIDTH
= 600;
196 RrMargins(self
->a_bg
, &l
, &t
, &r
, &b
);
203 Rect
*area
= screen_physical_area_all_monitors();
204 maxw
= MIN(MAX_WIDTH
, area
->width
*4/5);
208 /* find the button sizes and how much space we need for them */
209 allbuttonsw
= allbuttonsh
= 0;
210 for (i
= 0; i
< self
->n_buttons
; ++i
) {
213 prompt_a_button
->texture
[0].data
.text
.string
= self
->button
[i
].text
;
214 prompt_a_hover
->texture
[0].data
.text
.string
= self
->button
[i
].text
;
215 prompt_a_press
->texture
[0].data
.text
.string
= self
->button
[i
].text
;
216 RrMinSize(prompt_a_button
, &bw
, &bh
);
217 self
->button
[i
].width
= bw
;
218 self
->button
[i
].height
= bh
;
219 RrMinSize(prompt_a_hover
, &bw
, &bh
);
220 self
->button
[i
].width
= MAX(self
->button
[i
].width
, bw
);
221 self
->button
[i
].height
= MAX(self
->button
[i
].height
, bh
);
222 RrMinSize(prompt_a_press
, &bw
, &bh
);
223 self
->button
[i
].width
= MAX(self
->button
[i
].width
, bw
);
224 self
->button
[i
].height
= MAX(self
->button
[i
].height
, bh
);
226 allbuttonsw
+= self
->button
[i
].width
+ (i
> 0 ? BUTTON_SEPARATION
: 0);
227 allbuttonsh
= MAX(allbuttonsh
, self
->button
[i
].height
);
230 self
->msg_wbound
= MAX(allbuttonsw
, maxw
);
232 /* measure the text message area */
233 prompt_a_msg
->texture
[0].data
.text
.string
= self
->msg
.text
;
234 prompt_a_msg
->texture
[0].data
.text
.maxwidth
= self
->msg_wbound
;
235 RrMinSize(prompt_a_msg
, &self
->msg
.width
, &self
->msg
.height
);
237 /* width and height inside the outer margins */
238 w
= MAX(self
->msg
.width
, allbuttonsw
);
239 h
= self
->msg
.height
+ MSG_BUTTON_SEPARATION
+ allbuttonsh
;
241 /* position the text message */
242 self
->msg
.x
= l
+ (w
- self
->msg
.width
) / 2;
245 /* position the button buttons */
246 buttonx
= l
+ (w
- allbuttonsw
) / 2;
247 for (i
= 0; i
< self
->n_buttons
; ++i
) {
248 self
->button
[i
].x
= buttonx
;
249 buttonx
+= self
->button
[i
].width
+ BUTTON_SEPARATION
;
250 self
->button
[i
].y
= t
+ h
- allbuttonsh
;
251 self
->button
[i
].y
+= (allbuttonsh
- self
->button
[i
].height
) / 2;
254 /* size and position the toplevel window */
255 self
->width
= w
+ l
+ r
;
256 self
->height
= h
+ t
+ b
;
258 /* move and resize the actual windows */
259 XResizeWindow(ob_display
, self
->super
.window
, self
->width
, self
->height
);
260 XMoveResizeWindow(ob_display
, self
->msg
.window
,
261 self
->msg
.x
, self
->msg
.y
,
262 self
->msg
.width
, self
->msg
.height
);
263 for (i
= 0; i
< self
->n_buttons
; ++i
)
264 XMoveResizeWindow(ob_display
, self
->button
[i
].window
,
265 self
->button
[i
].x
, self
->button
[i
].y
,
266 self
->button
[i
].width
, self
->button
[i
].height
);
269 static void render_button(ObPrompt
*self
, ObPromptElement
*e
)
271 prompt_a_button
->surface
.parent
= self
->a_bg
;
272 prompt_a_button
->surface
.parentx
= e
->x
;
273 prompt_a_button
->surface
.parentx
= e
->y
;
275 prompt_a_button
->texture
[0].data
.text
.string
= e
->text
;
276 RrPaint(prompt_a_button
, e
->window
, e
->width
, e
->height
);
279 static void render_all(ObPrompt
*self
)
283 RrPaint(self
->a_bg
, self
->super
.window
, self
->width
, self
->height
);
285 prompt_a_msg
->surface
.parent
= self
->a_bg
;
286 prompt_a_msg
->surface
.parentx
= self
->msg
.x
;
287 prompt_a_msg
->surface
.parentx
= self
->msg
.y
;
289 prompt_a_msg
->texture
[0].data
.text
.string
= self
->msg
.text
;
290 prompt_a_msg
->texture
[0].data
.text
.maxwidth
= self
->msg_wbound
;
291 RrPaint(prompt_a_msg
, self
->msg
.window
, self
->msg
.width
, self
->msg
.height
);
293 for (i
= 0; i
< self
->n_buttons
; ++i
)
294 render_button(self
, &self
->button
[i
]);
297 void prompt_show(ObPrompt
*self
, ObClient
*parent
)
301 if (self
->mapped
) return;
306 /* you can't resize the prompt */
307 hints
.flags
= PMinSize
| PMaxSize
;
308 hints
.min_width
= hints
.max_width
= self
->width
;
309 hints
.min_height
= hints
.max_height
= self
->height
;
310 XSetWMNormalHints(ob_display
, self
->super
.window
, &hints
);
312 XSetTransientForHint(ob_display
, (parent
? parent
->window
: 0),
315 client_manage(self
->super
.window
, self
);
320 void prompt_hide(ObPrompt
*self
)
322 XUnmapWindow(ob_display
, self
->super
.window
);
323 self
->mapped
= FALSE
;
326 void prompt_hide_window(Window window
)
331 for (it
= prompt_list
; it
; it
= g_list_next(it
)) {
333 if (p
->super
.window
== window
) break;
335 g_assert(it
!= NULL
);