Small fix for an enum
[wmaker-crm.git] / WINGs / wpanel.c
blob1e03090508bb704444e236ec6a78e3fd328965e9
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 && panel->defBtn) {
31 WMPerformButtonClick(panel->defBtn);
33 if (event->xkey.keycode == panel->escKey) {
34 if (panel->altBtn || panel->othBtn) {
35 WMPerformButtonClick(panel->othBtn ? panel->othBtn : panel->altBtn);
36 } else {
37 panel->result = WAPRDefault;
38 panel->done=1;
44 int
45 WMRunAlertPanel(WMScreen *scrPtr, WMWindow *owner,
46 char *title, char *msg, char *defaultButton,
47 char *alternateButton, char *otherButton)
49 WMAlertPanel *panel;
50 int tmp;
52 panel = WMCreateAlertPanel(scrPtr, owner, title, msg, defaultButton,
53 alternateButton, otherButton);
56 int px, py;
57 WMView *view = WMWidgetView(panel->win);
59 if (owner) {
60 WMView *oview = WMWidgetView(owner);
61 WMPoint pt = WMGetViewScreenPosition(oview);
63 px = (W_VIEW_WIDTH(oview)-W_VIEW_WIDTH(view))/2;
64 py = (W_VIEW_HEIGHT(oview)-W_VIEW_HEIGHT(view))/2;
66 px += pt.x;
67 py += pt.y;
68 } else {
69 px = (W_VIEW_WIDTH(scrPtr->rootView)-W_VIEW_WIDTH(view))/2;
70 py = (W_VIEW_HEIGHT(scrPtr->rootView)-W_VIEW_HEIGHT(view))/2;
72 WMSetWindowInitialPosition(panel->win, px, py);
76 scrPtr->modalView = W_VIEW(panel->win);
77 WMMapWidget(panel->win);
79 scrPtr->modal = 1;
80 while (!panel->done || WMScreenPending(scrPtr)) {
81 XEvent event;
83 WMNextEvent(scrPtr->display, &event);
84 WMHandleEvent(&event);
86 scrPtr->modal = 0;
88 tmp = panel->result;
90 WMDestroyAlertPanel(panel);
92 return tmp;
96 void
97 WMDestroyAlertPanel(WMAlertPanel *panel)
99 WMUnmapWidget(panel->win);
100 WMDestroyWidget(panel->win);
101 wfree(panel);
105 WMAlertPanel*
106 WMCreateAlertPanel(WMScreen *scrPtr, WMWindow *owner,
107 char *title, char *msg, char *defaultButton,
108 char *alternateButton, char *otherButton)
110 WMAlertPanel *panel;
111 int x, dw=0, aw=0, ow=0, w;
114 panel = wmalloc(sizeof(WMAlertPanel));
115 memset(panel, 0, sizeof(WMAlertPanel));
118 panel->retKey = XKeysymToKeycode(scrPtr->display, XK_Return);
119 panel->escKey = XKeysymToKeycode(scrPtr->display, XK_Escape);
121 if (owner) {
122 panel->win = WMCreatePanelWithStyleForWindow(owner, "alertPanel",
123 WMTitledWindowMask);
124 } else {
125 panel->win = WMCreateWindowWithStyle(scrPtr, "alertPanel",
126 WMTitledWindowMask);
129 WMSetWindowInitialPosition(panel->win,
130 (scrPtr->rootView->size.width - WMWidgetWidth(panel->win))/2,
131 (scrPtr->rootView->size.height - WMWidgetHeight(panel->win))/2);
133 WMSetWindowTitle(panel->win, "");
135 if (scrPtr->applicationIcon) {
136 panel->iLbl = WMCreateLabel(panel->win);
137 WMResizeWidget(panel->iLbl, scrPtr->applicationIcon->width,
138 scrPtr->applicationIcon->height);
139 WMMoveWidget(panel->iLbl, 8 + (64 - scrPtr->applicationIcon->width)/2,
140 (75 - scrPtr->applicationIcon->height)/2);
141 WMSetLabelImage(panel->iLbl, scrPtr->applicationIcon);
142 WMSetLabelImagePosition(panel->iLbl, WIPImageOnly);
145 if (title) {
146 WMFont *largeFont;
148 largeFont = WMBoldSystemFontOfSize(scrPtr, 24);
150 panel->tLbl = WMCreateLabel(panel->win);
151 WMMoveWidget(panel->tLbl, 80, (80 - WMFontHeight(largeFont))/2);
152 WMResizeWidget(panel->tLbl, 400 - 70, WMFontHeight(largeFont)+4);
153 WMSetLabelText(panel->tLbl, title);
154 WMSetLabelTextAlignment(panel->tLbl, WALeft);
155 WMSetLabelFont(panel->tLbl, largeFont);
157 WMReleaseFont(largeFont);
161 if (msg) {
162 panel->mLbl = WMCreateLabel(panel->win);
163 WMMoveWidget(panel->mLbl, 10, 83);
164 WMResizeWidget(panel->mLbl, 380, WMFontHeight(scrPtr->normalFont)*4);
165 WMSetLabelText(panel->mLbl, msg);
166 WMSetLabelTextAlignment(panel->mLbl, WACenter);
170 /* create divider line */
172 panel->line = WMCreateFrame(panel->win);
173 WMMoveWidget(panel->line, 0, 80);
174 WMResizeWidget(panel->line, 400, 2);
175 WMSetFrameRelief(panel->line, WRGroove);
177 /* create buttons */
178 if (otherButton)
179 ow = WMWidthOfString(scrPtr->normalFont, otherButton,
180 strlen(otherButton));
182 if (alternateButton)
183 aw = WMWidthOfString(scrPtr->normalFont, alternateButton,
184 strlen(alternateButton));
186 if (defaultButton)
187 dw = WMWidthOfString(scrPtr->normalFont, defaultButton,
188 strlen(defaultButton));
190 dw = dw + (scrPtr->buttonArrow ? scrPtr->buttonArrow->width : 0);
192 aw += 30;
193 ow += 30;
194 dw += 30;
196 w = WMAX(dw, WMAX(aw, ow));
197 if ((w+10)*3 < 400) {
198 aw = w;
199 ow = w;
200 dw = w;
201 } else {
202 int t;
204 t = 400 - 40 - aw - ow - dw;
205 aw += t/3;
206 ow += t/3;
207 dw += t/3;
210 x = 400;
212 if (defaultButton) {
213 x -= dw + 10;
215 panel->defBtn = WMCreateCommandButton(panel->win);
216 WMSetButtonAction(panel->defBtn, alertPanelOnClick, panel);
217 WMMoveWidget(panel->defBtn, x, 144);
218 WMResizeWidget(panel->defBtn, dw, 24);
219 WMSetButtonText(panel->defBtn, defaultButton);
220 WMSetButtonImage(panel->defBtn, scrPtr->buttonArrow);
221 WMSetButtonAltImage(panel->defBtn, scrPtr->pushedButtonArrow);
222 WMSetButtonImagePosition(panel->defBtn, WIPRight);
224 if (alternateButton) {
225 x -= aw + 10;
227 panel->altBtn = WMCreateCommandButton(panel->win);
228 WMMoveWidget(panel->altBtn, x, 144);
229 WMResizeWidget(panel->altBtn, aw, 24);
230 WMSetButtonAction(panel->altBtn, alertPanelOnClick, panel);
231 WMSetButtonText(panel->altBtn, alternateButton);
233 if (otherButton) {
234 x -= ow + 10;
236 panel->othBtn = WMCreateCommandButton(panel->win);
237 WMSetButtonAction(panel->othBtn, alertPanelOnClick, panel);
238 WMMoveWidget(panel->othBtn, x, 144);
239 WMResizeWidget(panel->othBtn, ow, 24);
240 WMSetButtonText(panel->othBtn, otherButton);
243 panel->done = 0;
245 WMCreateEventHandler(W_VIEW(panel->win), KeyPressMask,
246 handleKeyPress, panel);
248 WMRealizeWidget(panel->win);
249 WMMapSubwidgets(panel->win);
251 return panel;
258 static void
259 inputBoxOnClick(WMWidget *self, void *clientData)
261 WMInputPanel *panel = clientData;
263 panel->done = 1;
264 if (self == panel->defBtn) {
265 panel->result = WAPRDefault;
266 } else if (self == panel->altBtn) {
267 panel->result = WAPRAlternate;
273 static void
274 handleKeyPress2(XEvent *event, void *clientData)
276 WMInputPanel *panel = (WMInputPanel*)clientData;
278 if (event->xkey.keycode == panel->retKey && panel->defBtn) {
279 WMPerformButtonClick(panel->defBtn);
281 if (event->xkey.keycode == panel->escKey) {
282 if (panel->altBtn) {
283 WMPerformButtonClick(panel->altBtn);
284 } else {
285 /* printf("got esc\n");*/
286 panel->done = 1;
287 panel->result = WAPRDefault;
293 char*
294 WMRunInputPanel(WMScreen *scrPtr, WMWindow *owner, char *title,
295 char *msg, char *defaultText,
296 char *okButton, char *cancelButton)
298 WMInputPanel *panel;
299 char *tmp;
301 panel = WMCreateInputPanel(scrPtr, owner, title, msg, defaultText,
302 okButton, cancelButton);
306 int px, py;
307 WMView *view = WMWidgetView(panel->win);
309 if (owner) {
310 WMView *oview = WMWidgetView(owner);
311 WMPoint pt = WMGetViewScreenPosition(oview);
313 px = (W_VIEW_WIDTH(oview)-W_VIEW_WIDTH(view))/2;
314 py = (W_VIEW_HEIGHT(oview)-W_VIEW_HEIGHT(view))/2;
316 px += pt.x;
317 py += pt.y;
318 } else {
319 px = (W_VIEW_WIDTH(scrPtr->rootView)-W_VIEW_WIDTH(view))/2;
320 py = (W_VIEW_HEIGHT(scrPtr->rootView)-W_VIEW_HEIGHT(view))/2;
322 WMSetWindowInitialPosition(panel->win, px, py);
325 WMMapWidget(panel->win);
327 while (!panel->done || WMScreenPending(scrPtr)) {
328 XEvent event;
330 WMNextEvent(scrPtr->display, &event);
331 WMHandleEvent(&event);
335 if (panel->result == WAPRDefault)
336 tmp = WMGetTextFieldText(panel->text);
337 else
338 tmp = NULL;
340 WMDestroyInputPanel(panel);
342 return tmp;
346 void
347 WMDestroyInputPanel(WMInputPanel *panel)
349 WMRemoveNotificationObserver(panel);
350 WMUnmapWidget(panel->win);
351 WMDestroyWidget(panel->win);
352 wfree(panel);
357 static void
358 endedEditingObserver(void *observerData, WMNotification *notification)
360 WMInputPanel *panel = (WMInputPanel*)observerData;
362 switch ((int)WMGetNotificationClientData(notification)) {
363 case WMReturnTextMovement:
364 if (panel->defBtn)
365 WMPerformButtonClick(panel->defBtn);
366 break;
367 case WMEscapeTextMovement:
368 if (panel->altBtn)
369 WMPerformButtonClick(panel->altBtn);
370 else {
371 panel->done = 1;
372 panel->result = WAPRDefault;
374 break;
375 default:
376 break;
381 WMInputPanel*
382 WMCreateInputPanel(WMScreen *scrPtr, WMWindow *owner, char *title, char *msg,
383 char *defaultText, char *okButton, char *cancelButton)
385 WMInputPanel *panel;
386 int x, dw=0, aw=0, w;
389 panel = wmalloc(sizeof(WMInputPanel));
390 memset(panel, 0, sizeof(WMInputPanel));
392 panel->retKey = XKeysymToKeycode(scrPtr->display, XK_Return);
393 panel->escKey = XKeysymToKeycode(scrPtr->display, XK_Escape);
395 if (owner)
396 panel->win = WMCreatePanelWithStyleForWindow(owner, "inputPanel",
397 WMTitledWindowMask);
398 else
399 panel->win = WMCreateWindowWithStyle(scrPtr, "inputPanel",
400 WMTitledWindowMask);
401 WMSetWindowTitle(panel->win, "");
403 WMResizeWidget(panel->win, 320, 160);
405 if (title) {
406 WMFont *largeFont;
408 largeFont = WMBoldSystemFontOfSize(scrPtr, 24);
410 panel->tLbl = WMCreateLabel(panel->win);
411 WMMoveWidget(panel->tLbl, 20, 16);
412 WMResizeWidget(panel->tLbl, 320 - 40, WMFontHeight(largeFont)+4);
413 WMSetLabelText(panel->tLbl, title);
414 WMSetLabelTextAlignment(panel->tLbl, WALeft);
415 WMSetLabelFont(panel->tLbl, largeFont);
417 WMReleaseFont(largeFont);
421 if (msg) {
422 panel->mLbl = WMCreateLabel(panel->win);
423 WMMoveWidget(panel->mLbl, 20, 50);
424 WMResizeWidget(panel->mLbl, 320 - 40,
425 WMFontHeight(scrPtr->normalFont)*2);
426 WMSetLabelText(panel->mLbl, msg);
427 WMSetLabelTextAlignment(panel->mLbl, WALeft);
430 panel->text = WMCreateTextField(panel->win);
431 WMMoveWidget(panel->text, 20, 85);
432 WMResizeWidget(panel->text, 320 - 40, WMWidgetHeight(panel->text));
433 WMSetTextFieldText(panel->text, defaultText);
435 WMAddNotificationObserver(endedEditingObserver, panel,
436 WMTextDidEndEditingNotification, panel->text);
438 /* create buttons */
439 if (cancelButton)
440 aw = WMWidthOfString(scrPtr->normalFont, cancelButton,
441 strlen(cancelButton));
443 if (okButton)
444 dw = WMWidthOfString(scrPtr->normalFont, okButton,
445 strlen(okButton));
447 w = dw + (scrPtr->buttonArrow ? scrPtr->buttonArrow->width : 0);
448 if (aw > w)
449 w = aw;
451 w += 30;
452 x = 310;
454 if (okButton) {
455 x -= w + 10;
457 panel->defBtn = WMCreateCustomButton(panel->win, WBBPushInMask
458 |WBBPushChangeMask
459 |WBBPushLightMask);
460 WMSetButtonAction(panel->defBtn, inputBoxOnClick, panel);
461 WMMoveWidget(panel->defBtn, x, 124);
462 WMResizeWidget(panel->defBtn, w, 24);
463 WMSetButtonText(panel->defBtn, okButton);
464 WMSetButtonImage(panel->defBtn, scrPtr->buttonArrow);
465 WMSetButtonAltImage(panel->defBtn, scrPtr->pushedButtonArrow);
466 WMSetButtonImagePosition(panel->defBtn, WIPRight);
468 if (cancelButton) {
469 x -= w + 10;
471 panel->altBtn = WMCreateCommandButton(panel->win);
472 WMSetButtonAction(panel->altBtn, inputBoxOnClick, panel);
473 WMMoveWidget(panel->altBtn, x, 124);
474 WMResizeWidget(panel->altBtn, w, 24);
475 WMSetButtonText(panel->altBtn, cancelButton);
478 panel->done = 0;
480 WMCreateEventHandler(W_VIEW(panel->win), KeyPressMask,
481 handleKeyPress2, panel);
483 WMRealizeWidget(panel->win);
484 WMMapSubwidgets(panel->win);
486 WMSetFocusToWidget(panel->text);
488 return panel;