Yet another try to get 0.51.2-pre2 in cvs
[wmaker-crm.git] / WINGs / wpanel.c
blobb164eb7d01df881dbe5b66eb522122969cd3f9cb
3 #include "WINGsP.h"
5 #include <X11/keysym.h>
9 static void
10 alertPanelOnClick(WMWidget *self, void *clientData)
12 WMAlertPanel *panel = clientData;
14 panel->done = 1;
15 if (self == panel->defBtn) {
16 panel->result = WAPRDefault;
17 } else if (self == panel->othBtn) {
18 panel->result = WAPROther;
19 } else if (self == panel->altBtn) {
20 panel->result = WAPRAlternate;
25 static void
26 handleKeyPress(XEvent *event, void *clientData)
28 WMAlertPanel *panel = (WMAlertPanel*)clientData;
30 if (event->xkey.keycode == panel->retKey) {
31 WMPerformButtonClick(panel->defBtn);
36 int
37 WMRunAlertPanel(WMScreen *scrPtr, WMWindow *owner,
38 char *title, char *msg, char *defaultButton,
39 char *alternateButton, char *otherButton)
41 WMAlertPanel *panel;
42 int tmp;
44 panel = WMCreateAlertPanel(scrPtr, owner, title, msg, defaultButton,
45 alternateButton, otherButton);
47 scrPtr->modalView = W_VIEW(panel->win);
48 WMMapWidget(panel->win);
50 scrPtr->modal = 1;
51 while (!panel->done || WMScreenPending(scrPtr)) {
52 XEvent event;
54 WMNextEvent(scrPtr->display, &event);
55 WMHandleEvent(&event);
57 scrPtr->modal = 0;
59 tmp = panel->result;
61 WMDestroyAlertPanel(panel);
63 return tmp;
67 void
68 WMDestroyAlertPanel(WMAlertPanel *panel)
70 WMUnmapWidget(panel->win);
71 WMDestroyWidget(panel->win);
72 free(panel);
76 WMAlertPanel*
77 WMCreateAlertPanel(WMScreen *scrPtr, WMWindow *owner,
78 char *title, char *msg, char *defaultButton,
79 char *alternateButton, char *otherButton)
81 WMAlertPanel *panel;
82 int x, dw=0, aw=0, ow=0, w;
85 panel = wmalloc(sizeof(WMAlertPanel));
86 memset(panel, 0, sizeof(WMAlertPanel));
89 panel->retKey = XKeysymToKeycode(scrPtr->display, XK_Return);
91 if (owner)
92 panel->win = WMCreatePanelWithStyleForWindow(owner, "alertPanel",
93 WMTitledWindowMask);
94 else
95 panel->win = WMCreateWindowWithStyle(scrPtr, "alertPanel",
96 WMTitledWindowMask);
98 WMSetWindowTitle(panel->win, "");
100 if (scrPtr->applicationIcon) {
101 panel->iLbl = WMCreateLabel(panel->win);
102 WMResizeWidget(panel->iLbl, scrPtr->applicationIcon->width,
103 scrPtr->applicationIcon->height);
104 WMMoveWidget(panel->iLbl, 8 + (64 - scrPtr->applicationIcon->width)/2,
105 (75 - scrPtr->applicationIcon->height)/2);
106 WMSetLabelImage(panel->iLbl, scrPtr->applicationIcon);
107 WMSetLabelImagePosition(panel->iLbl, WIPImageOnly);
110 if (title) {
111 WMFont *largeFont;
113 largeFont = WMBoldSystemFontOfSize(scrPtr, 24);
115 panel->tLbl = WMCreateLabel(panel->win);
116 WMMoveWidget(panel->tLbl, 80, (80 - WMFontHeight(largeFont))/2);
117 WMResizeWidget(panel->tLbl, 400 - 70, WMFontHeight(largeFont)+4);
118 WMSetLabelText(panel->tLbl, title);
119 WMSetLabelTextAlignment(panel->tLbl, WALeft);
120 WMSetLabelFont(panel->tLbl, largeFont);
122 WMReleaseFont(largeFont);
126 if (msg) {
127 panel->mLbl = WMCreateLabel(panel->win);
128 WMMoveWidget(panel->mLbl, 10, 83);
129 WMResizeWidget(panel->mLbl, 380, WMFontHeight(scrPtr->normalFont)*4);
130 WMSetLabelText(panel->mLbl, msg);
131 WMSetLabelTextAlignment(panel->mLbl, WACenter);
135 /* create divider line */
137 panel->line = WMCreateFrame(panel->win);
138 WMMoveWidget(panel->line, 0, 80);
139 WMResizeWidget(panel->line, 400, 2);
140 WMSetFrameRelief(panel->line, WRGroove);
142 /* create buttons */
143 if (otherButton)
144 ow = WMWidthOfString(scrPtr->normalFont, otherButton,
145 strlen(otherButton));
147 if (alternateButton)
148 aw = WMWidthOfString(scrPtr->normalFont, alternateButton,
149 strlen(alternateButton));
151 if (defaultButton)
152 dw = WMWidthOfString(scrPtr->normalFont, defaultButton,
153 strlen(defaultButton));
155 w = dw + (scrPtr->buttonArrow ? scrPtr->buttonArrow->width : 0);
156 if (aw > w)
157 w = aw;
158 if (ow > w)
159 w = ow;
161 w += 30;
162 x = 400;
164 if (defaultButton) {
165 x -= w + 10;
167 panel->defBtn = WMCreateCommandButton(panel->win);
168 WMSetButtonAction(panel->defBtn, alertPanelOnClick, panel);
169 WMMoveWidget(panel->defBtn, x, 144);
170 WMResizeWidget(panel->defBtn, w, 24);
171 WMSetButtonText(panel->defBtn, defaultButton);
172 WMSetButtonImage(panel->defBtn, scrPtr->buttonArrow);
173 WMSetButtonAltImage(panel->defBtn, scrPtr->pushedButtonArrow);
174 WMSetButtonImagePosition(panel->defBtn, WIPRight);
176 if (alternateButton) {
177 x -= w + 10;
179 panel->altBtn = WMCreateCommandButton(panel->win);
180 WMMoveWidget(panel->altBtn, x, 144);
181 WMResizeWidget(panel->altBtn, w, 24);
182 WMSetButtonAction(panel->altBtn, alertPanelOnClick, panel);
183 WMSetButtonText(panel->altBtn, alternateButton);
185 if (otherButton) {
186 x -= w + 10;
188 panel->othBtn = WMCreateCommandButton(panel->win);
189 WMSetButtonAction(panel->othBtn, alertPanelOnClick, panel);
190 WMMoveWidget(panel->othBtn, x, 144);
191 WMResizeWidget(panel->othBtn, w, 24);
192 WMSetButtonText(panel->othBtn, otherButton);
195 panel->done = 0;
197 WMCreateEventHandler(W_VIEW(panel->win), KeyPressMask,
198 handleKeyPress, panel);
200 WMRealizeWidget(panel->win);
201 WMMapSubwidgets(panel->win);
203 return panel;
210 static void
211 inputBoxOnClick(WMWidget *self, void *clientData)
213 WMInputPanel *panel = clientData;
215 panel->done = 1;
216 if (self == panel->defBtn) {
217 panel->result = WAPRDefault;
218 } else if (self == panel->altBtn) {
219 panel->result = WAPRAlternate;
225 static void
226 handleKeyPress2(XEvent *event, void *clientData)
228 WMInputPanel *panel = (WMInputPanel*)clientData;
230 if (event->xkey.keycode == panel->retKey) {
231 WMPerformButtonClick(panel->defBtn);
236 char*
237 WMRunInputPanel(WMScreen *scrPtr, WMWindow *owner, char *title,
238 char *msg, char *defaultText,
239 char *okButton, char *cancelButton)
241 WMInputPanel *panel;
242 char *tmp;
244 panel = WMCreateInputPanel(scrPtr, owner, title, msg, defaultText,
245 okButton, cancelButton);
247 WMMapWidget(panel->win);
249 while (!panel->done || WMScreenPending(scrPtr)) {
250 XEvent event;
252 WMNextEvent(scrPtr->display, &event);
253 WMHandleEvent(&event);
257 if (panel->result == WAPRDefault)
258 tmp = WMGetTextFieldText(panel->text);
259 else
260 tmp = NULL;
262 WMDestroyInputPanel(panel);
264 return tmp;
268 void
269 WMDestroyInputPanel(WMInputPanel *panel)
271 WMRemoveNotificationObserver(panel);
272 WMUnmapWidget(panel->win);
273 WMDestroyWidget(panel->win);
274 free(panel);
279 static void
280 endedEditingObserver(void *observerData, WMNotification *notification)
282 WMInputPanel *panel = (WMInputPanel*)observerData;
284 if ((int)WMGetNotificationClientData(notification) == WMReturnTextMovement) {
285 WMPerformButtonClick(panel->defBtn);
290 WMInputPanel*
291 WMCreateInputPanel(WMScreen *scrPtr, WMWindow *owner, char *title, char *msg,
292 char *defaultText, char *okButton, char *cancelButton)
294 WMInputPanel *panel;
295 int x, dw=0, aw=0, w;
298 panel = wmalloc(sizeof(WMInputPanel));
299 memset(panel, 0, sizeof(WMInputPanel));
301 panel->retKey = XKeysymToKeycode(scrPtr->display, XK_Return);
303 if (owner)
304 panel->win = WMCreatePanelWithStyleForWindow(owner, "inputPanel",
305 WMTitledWindowMask);
306 else
307 panel->win = WMCreateWindowWithStyle(scrPtr, "inputPanel",
308 WMTitledWindowMask);
309 WMSetWindowTitle(panel->win, "");
311 WMResizeWidget(panel->win, 320, 160);
313 if (title) {
314 WMFont *largeFont;
316 largeFont = WMBoldSystemFontOfSize(scrPtr, 24);
318 panel->tLbl = WMCreateLabel(panel->win);
319 WMMoveWidget(panel->tLbl, 20, 16);
320 WMResizeWidget(panel->tLbl, 320 - 40, WMFontHeight(largeFont)+4);
321 WMSetLabelText(panel->tLbl, title);
322 WMSetLabelTextAlignment(panel->tLbl, WALeft);
323 WMSetLabelFont(panel->tLbl, largeFont);
325 WMReleaseFont(largeFont);
329 if (msg) {
330 panel->mLbl = WMCreateLabel(panel->win);
331 WMMoveWidget(panel->mLbl, 20, 50);
332 WMResizeWidget(panel->mLbl, 320 - 40,
333 WMFontHeight(scrPtr->normalFont)*2);
334 WMSetLabelText(panel->mLbl, msg);
335 WMSetLabelTextAlignment(panel->mLbl, WALeft);
338 panel->text = WMCreateTextField(panel->win);
339 WMMoveWidget(panel->text, 20, 85);
340 WMResizeWidget(panel->text, 320 - 40, WMWidgetHeight(panel->text));
341 WMSetTextFieldText(panel->text, defaultText);
343 WMAddNotificationObserver(endedEditingObserver, panel,
344 WMTextDidEndEditingNotification, panel->text);
346 /* create buttons */
347 if (cancelButton)
348 aw = WMWidthOfString(scrPtr->normalFont, cancelButton,
349 strlen(cancelButton));
351 if (okButton)
352 dw = WMWidthOfString(scrPtr->normalFont, okButton,
353 strlen(okButton));
355 w = dw + (scrPtr->buttonArrow ? scrPtr->buttonArrow->width : 0);
356 if (aw > w)
357 w = aw;
359 w += 30;
360 x = 310;
362 if (okButton) {
363 x -= w + 10;
365 panel->defBtn = WMCreateCustomButton(panel->win, WBBPushInMask
366 |WBBPushChangeMask
367 |WBBPushLightMask);
368 WMSetButtonAction(panel->defBtn, inputBoxOnClick, panel);
369 WMMoveWidget(panel->defBtn, x, 124);
370 WMResizeWidget(panel->defBtn, w, 24);
371 WMSetButtonText(panel->defBtn, okButton);
372 WMSetButtonImage(panel->defBtn, scrPtr->buttonArrow);
373 WMSetButtonAltImage(panel->defBtn, scrPtr->pushedButtonArrow);
374 WMSetButtonImagePosition(panel->defBtn, WIPRight);
376 if (cancelButton) {
377 x -= w + 10;
379 panel->altBtn = WMCreateCommandButton(panel->win);
380 WMSetButtonAction(panel->altBtn, inputBoxOnClick, panel);
381 WMMoveWidget(panel->altBtn, x, 124);
382 WMResizeWidget(panel->altBtn, w, 24);
383 WMSetButtonText(panel->altBtn, cancelButton);
386 panel->done = 0;
388 WMCreateEventHandler(W_VIEW(panel->win), KeyPressMask,
389 handleKeyPress2, panel);
391 WMRealizeWidget(panel->win);
392 WMMapSubwidgets(panel->win);
394 WMSetFocusToWidget(panel->text);
396 return panel;