changed panel to use boxes
[wmaker-crm.git] / WINGs / wpanel.c
blobedf8072880d8dbad853eb3498a1eae2e71dac55a
3 #include "WINGsP.h"
5 #include <X11/keysym.h>
9 static void
10 alertPanelOnClick(WMWidget *self, void *clientData)
12 WMAlertPanel *panel = clientData;
14 WMBreakModalLoop(WMWidgetScreen(panel->othBtn));
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 WMBreakModalLoop(WMWidgetScreen(panel->othBtn));
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);
75 WMMapWidget(panel->win);
77 WMRunModalLoop(scrPtr, W_VIEW(panel->win));
79 tmp = panel->result;
81 WMDestroyAlertPanel(panel);
83 return tmp;
87 void
88 WMDestroyAlertPanel(WMAlertPanel *panel)
90 WMUnmapWidget(panel->win);
91 WMDestroyWidget(panel->win);
92 wfree(panel);
96 WMAlertPanel*
97 WMCreateAlertPanel(WMScreen *scrPtr, WMWindow *owner,
98 char *title, char *msg, char *defaultButton,
99 char *alternateButton, char *otherButton)
101 WMAlertPanel *panel;
102 int x, dw=0, aw=0, ow=0, w;
103 WMBox *hbox;
106 panel = wmalloc(sizeof(WMAlertPanel));
107 memset(panel, 0, sizeof(WMAlertPanel));
110 panel->retKey = XKeysymToKeycode(scrPtr->display, XK_Return);
111 panel->escKey = XKeysymToKeycode(scrPtr->display, XK_Escape);
113 if (owner) {
114 panel->win = WMCreatePanelWithStyleForWindow(owner, "alertPanel",
115 WMTitledWindowMask);
116 } else {
117 panel->win = WMCreateWindowWithStyle(scrPtr, "alertPanel",
118 WMTitledWindowMask);
121 WMSetWindowInitialPosition(panel->win,
122 (scrPtr->rootView->size.width - WMWidgetWidth(panel->win))/2,
123 (scrPtr->rootView->size.height - WMWidgetHeight(panel->win))/2);
125 WMSetWindowTitle(panel->win, "");
127 panel->vbox = WMCreateBox(panel->win);
128 WMSetBoxExpandsToParent(panel->vbox);
129 WMSetBoxHorizontal(panel->vbox, False);
130 WMMapWidget(panel->vbox);
132 hbox = WMCreateBox(panel->vbox);
133 WSetBoxHorizontal(hbox, True);
134 WMMapWidget(hbox);
135 WMAddBoxSubview(panel->vbox, WMWidgetView(hbox), False, True, 80, 0, 0, 5);
137 panel->iLbl = WMCreateLabel(hbox);
138 WMSetLabelImagePosition(panel->iLbl, WIPImageOnly);
139 WMMapWidget(panel->iLbl);
140 WMAddBoxSubview(hbox, WMWidgetView(panel->iLbl), False, True, 64, 0, 0, 10);
142 if (scrPtr->applicationIcon) {
143 WMSetLabelImage(panel->iLbl, scrPtr->applicationIcon);
146 if (title) {
147 WMFont *largeFont;
149 largeFont = WMBoldSystemFontOfSize(scrPtr, 24);
151 panel->tLbl = WMCreateLabel(hbox);
152 WMMapWidget(panel->tLbl);
153 WMAddBoxSubview(hbox, WMWidgetView(panel->tLbl), True, True,
154 64, 0, 0, 0);
155 WMSetLabelText(panel->tLbl, title);
156 WMSetLabelTextAlignment(panel->tLbl, WALeft);
157 WMSetLabelFont(panel->tLbl, largeFont);
159 WMReleaseFont(largeFont);
162 /* create divider line */
164 panel->line = WMCreateFrame(panel->win);
165 WMMapWidget(panel->line);
166 WMAddBoxSubview(panel->vbox, WMWidgetView(panel->line), False, True,
167 2, 2, 5);
168 WMSetFrameRelief(panel->line, WRGroove);
171 if (msg) {
172 panel->mLbl = WMCreateLabel(panel->win);
173 WMMapWidget(panel->mLbl);
174 WMAddBoxSubview(panel->vbox, WMWidgetView(panel->mLbl), True, True,
175 WMFontHeight(scrPtr->normalFont)*4, 0, 0, 5);
176 WMSetLabelText(panel->mLbl, msg);
177 WMSetLabelTextAlignment(panel->mLbl, WACenter);
180 hbox = WMCreateBox(panel->vbox);
181 WMSetBoxHorizontal(hbox, True);
182 WMMapWidget(hbox);
183 WMAddBoxSubview(panel->vbox, hbox, False, True, 24, 0, 0, 0);
185 /* create buttons */
186 if (otherButton)
187 ow = WMWidthOfString(scrPtr->normalFont, otherButton,
188 strlen(otherButton));
190 if (alternateButton)
191 aw = WMWidthOfString(scrPtr->normalFont, alternateButton,
192 strlen(alternateButton));
194 if (defaultButton)
195 dw = WMWidthOfString(scrPtr->normalFont, defaultButton,
196 strlen(defaultButton));
198 dw = dw + (scrPtr->buttonArrow ? scrPtr->buttonArrow->width : 0);
200 aw += 30;
201 ow += 30;
202 dw += 30;
204 w = WMAX(dw, WMAX(aw, ow));
205 if ((w+10)*3 < 400) {
206 aw = w;
207 ow = w;
208 dw = w;
209 } else {
210 int t;
212 t = 400 - 40 - aw - ow - dw;
213 aw += t/3;
214 ow += t/3;
215 dw += t/3;
218 x = 400;
220 if (defaultButton) {
221 x -= dw + 10;
223 panel->defBtn = WMCreateCommandButton(panel->win);
224 WMSetButtonAction(panel->defBtn, alertPanelOnClick, panel);
225 WMAddBoxSubviewAtEnd(hbox, WMWidgetView(panel->defBtn),
226 False, True, dw, 0, 5);
227 WMSetButtonText(panel->defBtn, defaultButton);
228 WMSetButtonImage(panel->defBtn, scrPtr->buttonArrow);
229 WMSetButtonAltImage(panel->defBtn, scrPtr->pushedButtonArrow);
230 WMSetButtonImagePosition(panel->defBtn, WIPRight);
232 if (alternateButton) {
233 x -= aw + 10;
235 panel->altBtn = WMCreateCommandButton(panel->win);
236 WMAddBoxSubviewAtEnd(hbox, WMWidgetView(panel->altBtn),
237 False, True, aw, 0, 5);
238 WMSetButtonAction(panel->altBtn, alertPanelOnClick, panel);
239 WMSetButtonText(panel->altBtn, alternateButton);
241 if (otherButton) {
242 x -= ow + 10;
244 panel->othBtn = WMCreateCommandButton(panel->win);
245 WMSetButtonAction(panel->othBtn, alertPanelOnClick, panel);
246 WMAddBoxSubviewAtEnd(hbox, WMWidgetView(panel->othBtn),
247 False, True, ow, 0, 5);
248 WMSetButtonText(panel->othBtn, otherButton);
251 WMMapSubwidgets(hbox);
253 WMCreateEventHandler(W_VIEW(panel->win), KeyPressMask,
254 handleKeyPress, panel);
256 WMRealizeWidget(panel->win);
257 WMMapSubwidgets(panel->win);
259 return panel;
266 static void
267 inputBoxOnClick(WMWidget *self, void *clientData)
269 WMInputPanel *panel = clientData;
271 WMBreakModalLoop(WMWidgetScreen(self));
272 if (self == panel->defBtn) {
273 panel->result = WAPRDefault;
274 } else if (self == panel->altBtn) {
275 panel->result = WAPRAlternate;
281 static void
282 handleKeyPress2(XEvent *event, void *clientData)
284 WMInputPanel *panel = (WMInputPanel*)clientData;
286 if (event->xkey.keycode == panel->retKey && panel->defBtn) {
287 WMPerformButtonClick(panel->defBtn);
289 if (event->xkey.keycode == panel->escKey) {
290 if (panel->altBtn) {
291 WMPerformButtonClick(panel->altBtn);
292 } else {
293 /* printf("got esc\n");*/
294 WMBreakModalLoop(WMWidgetScreen(panel->altBtn));
295 panel->result = WAPRDefault;
301 char*
302 WMRunInputPanel(WMScreen *scrPtr, WMWindow *owner, char *title,
303 char *msg, char *defaultText,
304 char *okButton, char *cancelButton)
306 WMInputPanel *panel;
307 char *tmp;
309 panel = WMCreateInputPanel(scrPtr, owner, title, msg, defaultText,
310 okButton, cancelButton);
314 int px, py;
315 WMView *view = WMWidgetView(panel->win);
317 if (owner) {
318 WMView *oview = WMWidgetView(owner);
319 WMPoint pt = WMGetViewScreenPosition(oview);
321 px = (W_VIEW_WIDTH(oview)-W_VIEW_WIDTH(view))/2;
322 py = (W_VIEW_HEIGHT(oview)-W_VIEW_HEIGHT(view))/2;
324 px += pt.x;
325 py += pt.y;
326 } else {
327 px = (W_VIEW_WIDTH(scrPtr->rootView)-W_VIEW_WIDTH(view))/2;
328 py = (W_VIEW_HEIGHT(scrPtr->rootView)-W_VIEW_HEIGHT(view))/2;
330 WMSetWindowInitialPosition(panel->win, px, py);
333 WMMapWidget(panel->win);
335 WMRunModalLoop(scrPtr, W_VIEW(panel->win));
337 if (panel->result == WAPRDefault)
338 tmp = WMGetTextFieldText(panel->text);
339 else
340 tmp = NULL;
342 WMDestroyInputPanel(panel);
344 return tmp;
348 void
349 WMDestroyInputPanel(WMInputPanel *panel)
351 WMRemoveNotificationObserver(panel);
352 WMUnmapWidget(panel->win);
353 WMDestroyWidget(panel->win);
354 wfree(panel);
359 static void
360 endedEditingObserver(void *observerData, WMNotification *notification)
362 WMInputPanel *panel = (WMInputPanel*)observerData;
364 switch ((int)WMGetNotificationClientData(notification)) {
365 case WMReturnTextMovement:
366 if (panel->defBtn)
367 WMPerformButtonClick(panel->defBtn);
368 break;
369 case WMEscapeTextMovement:
370 if (panel->altBtn)
371 WMPerformButtonClick(panel->altBtn);
372 else {
373 WMBreakModalLoop(WMWidgetScreen(panel->defBtn));
374 panel->result = WAPRDefault;
376 break;
377 default:
378 break;
383 WMInputPanel*
384 WMCreateInputPanel(WMScreen *scrPtr, WMWindow *owner, char *title, char *msg,
385 char *defaultText, char *okButton, char *cancelButton)
387 WMInputPanel *panel;
388 int x, dw=0, aw=0, w;
391 panel = wmalloc(sizeof(WMInputPanel));
392 memset(panel, 0, sizeof(WMInputPanel));
394 panel->retKey = XKeysymToKeycode(scrPtr->display, XK_Return);
395 panel->escKey = XKeysymToKeycode(scrPtr->display, XK_Escape);
397 if (owner)
398 panel->win = WMCreatePanelWithStyleForWindow(owner, "inputPanel",
399 WMTitledWindowMask);
400 else
401 panel->win = WMCreateWindowWithStyle(scrPtr, "inputPanel",
402 WMTitledWindowMask);
403 WMSetWindowTitle(panel->win, "");
405 WMResizeWidget(panel->win, 320, 160);
407 if (title) {
408 WMFont *largeFont;
410 largeFont = WMBoldSystemFontOfSize(scrPtr, 24);
412 panel->tLbl = WMCreateLabel(panel->win);
413 WMMoveWidget(panel->tLbl, 20, 16);
414 WMResizeWidget(panel->tLbl, 320 - 40, WMFontHeight(largeFont)+4);
415 WMSetLabelText(panel->tLbl, title);
416 WMSetLabelTextAlignment(panel->tLbl, WALeft);
417 WMSetLabelFont(panel->tLbl, largeFont);
419 WMReleaseFont(largeFont);
423 if (msg) {
424 panel->mLbl = WMCreateLabel(panel->win);
425 WMMoveWidget(panel->mLbl, 20, 50);
426 WMResizeWidget(panel->mLbl, 320 - 40,
427 WMFontHeight(scrPtr->normalFont)*2);
428 WMSetLabelText(panel->mLbl, msg);
429 WMSetLabelTextAlignment(panel->mLbl, WALeft);
432 panel->text = WMCreateTextField(panel->win);
433 WMMoveWidget(panel->text, 20, 85);
434 WMResizeWidget(panel->text, 320 - 40, WMWidgetHeight(panel->text));
435 WMSetTextFieldText(panel->text, defaultText);
437 WMAddNotificationObserver(endedEditingObserver, panel,
438 WMTextDidEndEditingNotification, panel->text);
440 /* create buttons */
441 if (cancelButton)
442 aw = WMWidthOfString(scrPtr->normalFont, cancelButton,
443 strlen(cancelButton));
445 if (okButton)
446 dw = WMWidthOfString(scrPtr->normalFont, okButton,
447 strlen(okButton));
449 w = dw + (scrPtr->buttonArrow ? scrPtr->buttonArrow->width : 0);
450 if (aw > w)
451 w = aw;
453 w += 30;
454 x = 310;
456 if (okButton) {
457 x -= w + 10;
459 panel->defBtn = WMCreateCustomButton(panel->win, WBBPushInMask
460 |WBBPushChangeMask
461 |WBBPushLightMask);
462 WMSetButtonAction(panel->defBtn, inputBoxOnClick, panel);
463 WMMoveWidget(panel->defBtn, x, 124);
464 WMResizeWidget(panel->defBtn, w, 24);
465 WMSetButtonText(panel->defBtn, okButton);
466 WMSetButtonImage(panel->defBtn, scrPtr->buttonArrow);
467 WMSetButtonAltImage(panel->defBtn, scrPtr->pushedButtonArrow);
468 WMSetButtonImagePosition(panel->defBtn, WIPRight);
470 if (cancelButton) {
471 x -= w + 10;
473 panel->altBtn = WMCreateCommandButton(panel->win);
474 WMSetButtonAction(panel->altBtn, inputBoxOnClick, panel);
475 WMMoveWidget(panel->altBtn, x, 124);
476 WMResizeWidget(panel->altBtn, w, 24);
477 WMSetButtonText(panel->altBtn, cancelButton);
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;