- Fixed function naming problem in WINGs.
[wmaker-crm.git] / WINGs / wpanel.c
blob9ffe1ed9f6e88e898303d30887fe958670ec358b
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 WMSetWindowInitialPosition(panel->win,
99 (scrPtr->rootView->size.width - WMWidgetWidth(panel->win))/2,
100 (scrPtr->rootView->size.height - WMWidgetHeight(panel->win))/2);
101 WMSetWindowTitle(panel->win, "");
103 if (scrPtr->applicationIcon) {
104 panel->iLbl = WMCreateLabel(panel->win);
105 WMResizeWidget(panel->iLbl, scrPtr->applicationIcon->width,
106 scrPtr->applicationIcon->height);
107 WMMoveWidget(panel->iLbl, 8 + (64 - scrPtr->applicationIcon->width)/2,
108 (75 - scrPtr->applicationIcon->height)/2);
109 WMSetLabelImage(panel->iLbl, scrPtr->applicationIcon);
110 WMSetLabelImagePosition(panel->iLbl, WIPImageOnly);
113 if (title) {
114 WMFont *largeFont;
116 largeFont = WMBoldSystemFontOfSize(scrPtr, 24);
118 panel->tLbl = WMCreateLabel(panel->win);
119 WMMoveWidget(panel->tLbl, 80, (80 - WMFontHeight(largeFont))/2);
120 WMResizeWidget(panel->tLbl, 400 - 70, WMFontHeight(largeFont)+4);
121 WMSetLabelText(panel->tLbl, title);
122 WMSetLabelTextAlignment(panel->tLbl, WALeft);
123 WMSetLabelFont(panel->tLbl, largeFont);
125 WMReleaseFont(largeFont);
129 if (msg) {
130 panel->mLbl = WMCreateLabel(panel->win);
131 WMMoveWidget(panel->mLbl, 10, 83);
132 WMResizeWidget(panel->mLbl, 380, WMFontHeight(scrPtr->normalFont)*4);
133 WMSetLabelText(panel->mLbl, msg);
134 WMSetLabelTextAlignment(panel->mLbl, WACenter);
138 /* create divider line */
140 panel->line = WMCreateFrame(panel->win);
141 WMMoveWidget(panel->line, 0, 80);
142 WMResizeWidget(panel->line, 400, 2);
143 WMSetFrameRelief(panel->line, WRGroove);
145 /* create buttons */
146 if (otherButton)
147 ow = WMWidthOfString(scrPtr->normalFont, otherButton,
148 strlen(otherButton));
150 if (alternateButton)
151 aw = WMWidthOfString(scrPtr->normalFont, alternateButton,
152 strlen(alternateButton));
154 if (defaultButton)
155 dw = WMWidthOfString(scrPtr->normalFont, defaultButton,
156 strlen(defaultButton));
158 w = dw + (scrPtr->buttonArrow ? scrPtr->buttonArrow->width : 0);
159 if (aw > w)
160 w = aw;
161 if (ow > w)
162 w = ow;
164 w += 30;
165 x = 400;
167 if (defaultButton) {
168 x -= w + 10;
170 panel->defBtn = WMCreateCommandButton(panel->win);
171 WMSetButtonAction(panel->defBtn, alertPanelOnClick, panel);
172 WMMoveWidget(panel->defBtn, x, 144);
173 WMResizeWidget(panel->defBtn, w, 24);
174 WMSetButtonText(panel->defBtn, defaultButton);
175 WMSetButtonImage(panel->defBtn, scrPtr->buttonArrow);
176 WMSetButtonAltImage(panel->defBtn, scrPtr->pushedButtonArrow);
177 WMSetButtonImagePosition(panel->defBtn, WIPRight);
179 if (alternateButton) {
180 x -= w + 10;
182 panel->altBtn = WMCreateCommandButton(panel->win);
183 WMMoveWidget(panel->altBtn, x, 144);
184 WMResizeWidget(panel->altBtn, w, 24);
185 WMSetButtonAction(panel->altBtn, alertPanelOnClick, panel);
186 WMSetButtonText(panel->altBtn, alternateButton);
188 if (otherButton) {
189 x -= w + 10;
191 panel->othBtn = WMCreateCommandButton(panel->win);
192 WMSetButtonAction(panel->othBtn, alertPanelOnClick, panel);
193 WMMoveWidget(panel->othBtn, x, 144);
194 WMResizeWidget(panel->othBtn, w, 24);
195 WMSetButtonText(panel->othBtn, otherButton);
198 panel->done = 0;
200 WMCreateEventHandler(W_VIEW(panel->win), KeyPressMask,
201 handleKeyPress, panel);
203 WMRealizeWidget(panel->win);
204 WMMapSubwidgets(panel->win);
206 return panel;
213 static void
214 inputBoxOnClick(WMWidget *self, void *clientData)
216 WMInputPanel *panel = clientData;
218 panel->done = 1;
219 if (self == panel->defBtn) {
220 panel->result = WAPRDefault;
221 } else if (self == panel->altBtn) {
222 panel->result = WAPRAlternate;
228 static void
229 handleKeyPress2(XEvent *event, void *clientData)
231 WMInputPanel *panel = (WMInputPanel*)clientData;
233 if (event->xkey.keycode == panel->retKey) {
234 WMPerformButtonClick(panel->defBtn);
239 char*
240 WMRunInputPanel(WMScreen *scrPtr, WMWindow *owner, char *title,
241 char *msg, char *defaultText,
242 char *okButton, char *cancelButton)
244 WMInputPanel *panel;
245 char *tmp;
247 panel = WMCreateInputPanel(scrPtr, owner, title, msg, defaultText,
248 okButton, cancelButton);
250 WMMapWidget(panel->win);
252 while (!panel->done || WMScreenPending(scrPtr)) {
253 XEvent event;
255 WMNextEvent(scrPtr->display, &event);
256 WMHandleEvent(&event);
260 if (panel->result == WAPRDefault)
261 tmp = WMGetTextFieldText(panel->text);
262 else
263 tmp = NULL;
265 WMDestroyInputPanel(panel);
267 return tmp;
271 void
272 WMDestroyInputPanel(WMInputPanel *panel)
274 WMRemoveNotificationObserver(panel);
275 WMUnmapWidget(panel->win);
276 WMDestroyWidget(panel->win);
277 free(panel);
282 static void
283 endedEditingObserver(void *observerData, WMNotification *notification)
285 WMInputPanel *panel = (WMInputPanel*)observerData;
287 if ((int)WMGetNotificationClientData(notification) == WMReturnTextMovement) {
288 WMPerformButtonClick(panel->defBtn);
293 WMInputPanel*
294 WMCreateInputPanel(WMScreen *scrPtr, WMWindow *owner, char *title, char *msg,
295 char *defaultText, char *okButton, char *cancelButton)
297 WMInputPanel *panel;
298 int x, dw=0, aw=0, w;
301 panel = wmalloc(sizeof(WMInputPanel));
302 memset(panel, 0, sizeof(WMInputPanel));
304 panel->retKey = XKeysymToKeycode(scrPtr->display, XK_Return);
306 if (owner)
307 panel->win = WMCreatePanelWithStyleForWindow(owner, "inputPanel",
308 WMTitledWindowMask);
309 else
310 panel->win = WMCreateWindowWithStyle(scrPtr, "inputPanel",
311 WMTitledWindowMask);
312 WMSetWindowTitle(panel->win, "");
314 WMResizeWidget(panel->win, 320, 160);
316 if (title) {
317 WMFont *largeFont;
319 largeFont = WMBoldSystemFontOfSize(scrPtr, 24);
321 panel->tLbl = WMCreateLabel(panel->win);
322 WMMoveWidget(panel->tLbl, 20, 16);
323 WMResizeWidget(panel->tLbl, 320 - 40, WMFontHeight(largeFont)+4);
324 WMSetLabelText(panel->tLbl, title);
325 WMSetLabelTextAlignment(panel->tLbl, WALeft);
326 WMSetLabelFont(panel->tLbl, largeFont);
328 WMReleaseFont(largeFont);
332 if (msg) {
333 panel->mLbl = WMCreateLabel(panel->win);
334 WMMoveWidget(panel->mLbl, 20, 50);
335 WMResizeWidget(panel->mLbl, 320 - 40,
336 WMFontHeight(scrPtr->normalFont)*2);
337 WMSetLabelText(panel->mLbl, msg);
338 WMSetLabelTextAlignment(panel->mLbl, WALeft);
341 panel->text = WMCreateTextField(panel->win);
342 WMMoveWidget(panel->text, 20, 85);
343 WMResizeWidget(panel->text, 320 - 40, WMWidgetHeight(panel->text));
344 WMSetTextFieldText(panel->text, defaultText);
346 WMAddNotificationObserver(endedEditingObserver, panel,
347 WMTextDidEndEditingNotification, panel->text);
349 /* create buttons */
350 if (cancelButton)
351 aw = WMWidthOfString(scrPtr->normalFont, cancelButton,
352 strlen(cancelButton));
354 if (okButton)
355 dw = WMWidthOfString(scrPtr->normalFont, okButton,
356 strlen(okButton));
358 w = dw + (scrPtr->buttonArrow ? scrPtr->buttonArrow->width : 0);
359 if (aw > w)
360 w = aw;
362 w += 30;
363 x = 310;
365 if (okButton) {
366 x -= w + 10;
368 panel->defBtn = WMCreateCustomButton(panel->win, WBBPushInMask
369 |WBBPushChangeMask
370 |WBBPushLightMask);
371 WMSetButtonAction(panel->defBtn, inputBoxOnClick, panel);
372 WMMoveWidget(panel->defBtn, x, 124);
373 WMResizeWidget(panel->defBtn, w, 24);
374 WMSetButtonText(panel->defBtn, okButton);
375 WMSetButtonImage(panel->defBtn, scrPtr->buttonArrow);
376 WMSetButtonAltImage(panel->defBtn, scrPtr->pushedButtonArrow);
377 WMSetButtonImagePosition(panel->defBtn, WIPRight);
379 if (cancelButton) {
380 x -= w + 10;
382 panel->altBtn = WMCreateCommandButton(panel->win);
383 WMSetButtonAction(panel->altBtn, inputBoxOnClick, panel);
384 WMMoveWidget(panel->altBtn, x, 124);
385 WMResizeWidget(panel->altBtn, w, 24);
386 WMSetButtonText(panel->altBtn, cancelButton);
389 panel->done = 0;
391 WMCreateEventHandler(W_VIEW(panel->win), KeyPressMask,
392 handleKeyPress2, panel);
394 WMRealizeWidget(panel->win);
395 WMMapSubwidgets(panel->win);
397 WMSetFocusToWidget(panel->text);
399 return panel;