Update Serbian translation from master branch
[wmaker-crm.git] / WINGs / wpanel.c
bloba0220487d3025842df9fbf02074ed362a3a0c46e
2 #include "WINGsP.h"
4 #include <X11/keysym.h>
5 #include <stdint.h>
7 static void alertPanelOnClick(WMWidget * self, void *clientData)
9 WMAlertPanel *panel = clientData;
11 WMBreakModalLoop(WMWidgetScreen(self));
12 if (self == panel->defBtn) {
13 panel->result = WAPRDefault;
14 } else if (self == panel->othBtn) {
15 panel->result = WAPROther;
16 } else if (self == panel->altBtn) {
17 panel->result = WAPRAlternate;
21 static void handleKeyPress(XEvent * event, void *clientData)
23 WMAlertPanel *panel = (WMAlertPanel *) clientData;
24 KeySym ksym;
26 XLookupString(&event->xkey, NULL, 0, &ksym, NULL);
28 if ((ksym == XK_Return || ksym == XK_KP_Enter) && panel->defBtn) {
29 WMPerformButtonClick(panel->defBtn);
30 } else if (ksym == XK_Escape) {
31 if (panel->altBtn || panel->othBtn) {
32 WMPerformButtonClick(panel->othBtn ? panel->othBtn : panel->altBtn);
33 } else {
34 panel->result = WAPRDefault;
35 WMBreakModalLoop(WMWidgetScreen(panel->win));
40 int
41 WMRunAlertPanel(WMScreen * scrPtr, WMWindow * owner,
42 const char *title, const char *msg, const char *defaultButton, const char *alternateButton, const char *otherButton)
44 WMAlertPanel *panel;
45 int tmp;
47 panel = WMCreateAlertPanel(scrPtr, owner, title, msg, defaultButton, alternateButton, otherButton);
50 int px, py;
51 WMView *view = WMWidgetView(panel->win);
53 if (owner) {
54 WMView *oview = WMWidgetView(owner);
55 WMPoint pt = WMGetViewScreenPosition(oview);
57 px = (W_VIEW_WIDTH(oview) - W_VIEW_WIDTH(view)) / 2;
58 py = (W_VIEW_HEIGHT(oview) - W_VIEW_HEIGHT(view)) / 2;
60 px += pt.x;
61 py += pt.y;
62 } else {
63 px = (W_VIEW_WIDTH(scrPtr->rootView) - W_VIEW_WIDTH(view)) / 2;
64 py = (W_VIEW_HEIGHT(scrPtr->rootView) - W_VIEW_HEIGHT(view)) / 2;
66 WMSetWindowInitialPosition(panel->win, px, py);
69 WMMapWidget(panel->win);
71 WMRunModalLoop(scrPtr, W_VIEW(panel->win));
73 tmp = panel->result;
75 WMDestroyAlertPanel(panel);
77 return tmp;
80 void WMDestroyAlertPanel(WMAlertPanel * panel)
82 WMUnmapWidget(panel->win);
83 WMDestroyWidget(panel->win);
84 wfree(panel);
87 WMAlertPanel *WMCreateAlertPanel(WMScreen * scrPtr, WMWindow * owner,
88 const char *title, const char *msg, const char *defaultButton,
89 const char *alternateButton, const char *otherButton)
91 WMAlertPanel *panel;
92 WMFont *defaultFont;
93 int dw = 0, aw = 0, ow = 0, w;
94 WMBox *hbox;
95 WMPixmap *icon;
97 defaultFont = WMSystemFontOfSize(scrPtr, 12);
98 panel = wmalloc(sizeof(WMAlertPanel));
100 if (owner) {
101 panel->win = WMCreatePanelWithStyleForWindow(owner, "alertPanel", WMTitledWindowMask);
102 } else {
103 panel->win = WMCreateWindowWithStyle(scrPtr, "alertPanel", WMTitledWindowMask);
106 WMSetWindowInitialPosition(panel->win,
107 (scrPtr->rootView->size.width - WMWidgetWidth(panel->win)) / 2,
108 (scrPtr->rootView->size.height - WMWidgetHeight(panel->win)) / 2);
110 WMSetWindowTitle(panel->win, "");
112 panel->vbox = WMCreateBox(panel->win);
113 WMSetViewExpandsToParent(WMWidgetView(panel->vbox), 0, 0, 0, 0);
114 WMSetBoxHorizontal(panel->vbox, False);
115 WMMapWidget(panel->vbox);
117 hbox = WMCreateBox(panel->vbox);
118 WMSetBoxBorderWidth(hbox, 5);
119 WMSetBoxHorizontal(hbox, True);
120 WMMapWidget(hbox);
121 WMAddBoxSubview(panel->vbox, WMWidgetView(hbox), False, True, 74, 0, 5);
123 panel->iLbl = WMCreateLabel(hbox);
124 WMSetLabelImagePosition(panel->iLbl, WIPImageOnly);
125 WMMapWidget(panel->iLbl);
126 WMAddBoxSubview(hbox, WMWidgetView(panel->iLbl), False, True, 64, 0, 10);
127 icon = WMCreateApplicationIconBlendedPixmap(scrPtr, (RColor *) NULL);
128 if (icon) {
129 WMSetLabelImage(panel->iLbl, icon);
130 WMReleasePixmap(icon);
131 } else {
132 WMSetLabelImage(panel->iLbl, scrPtr->applicationIconPixmap);
135 if (title) {
136 WMFont *largeFont;
138 largeFont = WMBoldSystemFontOfSize(scrPtr, 24);
140 panel->tLbl = WMCreateLabel(hbox);
141 WMMapWidget(panel->tLbl);
142 WMAddBoxSubview(hbox, WMWidgetView(panel->tLbl), True, True, 64, 0, 0);
143 WMSetLabelText(panel->tLbl, title);
144 WMSetLabelTextAlignment(panel->tLbl, WALeft);
145 WMSetLabelFont(panel->tLbl, largeFont);
147 WMReleaseFont(largeFont);
150 /* create divider line */
152 panel->line = WMCreateFrame(panel->win);
153 WMMapWidget(panel->line);
154 WMAddBoxSubview(panel->vbox, WMWidgetView(panel->line), False, True, 2, 2, 5);
155 WMSetFrameRelief(panel->line, WRGroove);
157 if (msg) {
158 panel->mLbl = WMCreateLabel(panel->vbox);
159 WMSetLabelWraps(panel->mLbl, True);
160 WMMapWidget(panel->mLbl);
161 WMAddBoxSubview(panel->vbox, WMWidgetView(panel->mLbl), True, True,
162 WMFontHeight(scrPtr->normalFont) * 4, 0, 5);
163 WMSetLabelText(panel->mLbl, msg);
164 WMSetLabelTextAlignment(panel->mLbl, WACenter);
165 WMSetLabelFont(panel->mLbl, defaultFont);
168 panel->hbox = WMCreateBox(panel->vbox);
169 WMSetBoxBorderWidth(panel->hbox, 10);
170 WMSetBoxHorizontal(panel->hbox, True);
171 WMMapWidget(panel->hbox);
172 WMAddBoxSubview(panel->vbox, WMWidgetView(panel->hbox), False, True, 44, 0, 0);
174 /* create buttons */
175 if (otherButton)
176 ow = WMWidthOfString(defaultFont, otherButton, strlen(otherButton));
178 if (alternateButton)
179 aw = WMWidthOfString(defaultFont, alternateButton, strlen(alternateButton));
181 if (defaultButton)
182 dw = WMWidthOfString(defaultFont, defaultButton, strlen(defaultButton));
184 dw = dw + (scrPtr->buttonArrow ? scrPtr->buttonArrow->width : 0);
186 aw += 30;
187 ow += 30;
188 dw += 30;
190 w = WMAX(dw, WMAX(aw, ow));
191 if ((w + 10) * 3 < 400) {
192 aw = w;
193 ow = w;
194 dw = w;
195 } else {
196 int t;
198 t = 400 - 40 - aw - ow - dw;
199 aw += t / 3;
200 ow += t / 3;
201 dw += t / 3;
204 if (defaultButton) {
205 panel->defBtn = WMCreateCommandButton(panel->hbox);
206 WMSetButtonAction(panel->defBtn, alertPanelOnClick, panel);
207 WMAddBoxSubviewAtEnd(panel->hbox, WMWidgetView(panel->defBtn), False, True, dw, 0, 0);
208 WMSetButtonText(panel->defBtn, defaultButton);
209 WMSetButtonImage(panel->defBtn, scrPtr->buttonArrow);
210 WMSetButtonAltImage(panel->defBtn, scrPtr->pushedButtonArrow);
211 WMSetButtonImagePosition(panel->defBtn, WIPRight);
212 WMSetButtonFont(panel->defBtn, defaultFont);
214 if (alternateButton) {
215 panel->altBtn = WMCreateCommandButton(panel->hbox);
216 WMAddBoxSubviewAtEnd(panel->hbox, WMWidgetView(panel->altBtn), False, True, aw, 0, 5);
217 WMSetButtonAction(panel->altBtn, alertPanelOnClick, panel);
218 WMSetButtonText(panel->altBtn, alternateButton);
219 WMSetButtonFont(panel->altBtn, defaultFont);
221 if (otherButton) {
222 panel->othBtn = WMCreateCommandButton(panel->hbox);
223 WMSetButtonAction(panel->othBtn, alertPanelOnClick, panel);
224 WMAddBoxSubviewAtEnd(panel->hbox, WMWidgetView(panel->othBtn), False, True, ow, 0, 5);
225 WMSetButtonText(panel->othBtn, otherButton);
226 WMSetButtonFont(panel->othBtn, defaultFont);
229 WMMapSubwidgets(panel->hbox);
231 WMCreateEventHandler(W_VIEW(panel->win), KeyPressMask, handleKeyPress, panel);
233 WMRealizeWidget(panel->win);
234 WMMapSubwidgets(panel->win);
236 WMReleaseFont(defaultFont);
238 return panel;
241 WMAlertPanel *WMCreateScaledAlertPanel(WMScreen * scrPtr, WMWindow * owner,
242 const char *title, const char *msg, const char *defaultButton,
243 const char *alternateButton, const char *otherButton)
245 WMAlertPanel *panel;
246 int dw = 0, aw = 0, ow = 0, w;
247 WMBox *hbox;
248 WMPixmap *icon;
249 int wmScaleWidth, wmScaleHeight;
250 int pwidth, pheight;
252 panel = wmalloc(sizeof(WMAlertPanel));
254 if (owner) {
255 panel->win = WMCreatePanelWithStyleForWindow(owner, "alertPanel", WMTitledWindowMask);
256 } else {
257 panel->win = WMCreateWindowWithStyle(scrPtr, "alertPanel", WMTitledWindowMask);
260 /* calculate and set the panel's size */
261 WMGetScaleBaseFromSystemFont(scrPtr, &wmScaleWidth, &wmScaleHeight);
262 pwidth = WMScaleX(400);
263 pheight = WMScaleY(5) /* upper margin */
264 + 64 /* icon size */
265 + WMScaleY(5) /* space between icon and divider line */
266 + 2 /* divider line */
267 + WMScaleY(5); /* space between divider line and message */
268 if (msg)
269 pheight += WMFontHeight(scrPtr->normalFont) * 4 + WMScaleY(5);
270 pheight += WMScaleY(44);
271 WMResizeWidget(panel->win, pwidth, pheight);
273 WMSetWindowInitialPosition(panel->win,
274 (scrPtr->rootView->size.width - pwidth) / 2,
275 (scrPtr->rootView->size.height - pheight) / 2);
277 WMSetWindowTitle(panel->win, "");
279 panel->vbox = WMCreateBox(panel->win);
280 WMSetViewExpandsToParent(WMWidgetView(panel->vbox), 0, 0, 0, 0);
281 WMSetBoxHorizontal(panel->vbox, False);
282 WMMapWidget(panel->vbox);
284 hbox = WMCreateBox(panel->vbox);
285 WMSetBoxBorderWidth(hbox, WMScaleX(5));
286 WMSetBoxHorizontal(hbox, True);
287 WMMapWidget(hbox);
288 WMAddBoxSubview(panel->vbox, WMWidgetView(hbox), False, True, 64 + 2 * WMScaleY(5), 0, WMScaleY(5));
290 panel->iLbl = WMCreateLabel(hbox);
291 WMSetLabelImagePosition(panel->iLbl, WIPImageOnly);
292 WMMapWidget(panel->iLbl);
293 WMAddBoxSubview(hbox, WMWidgetView(panel->iLbl), False, True, 64, 0, 10);
294 icon = WMCreateApplicationIconBlendedPixmap(scrPtr, (RColor *) NULL);
295 if (icon) {
296 WMSetLabelImage(panel->iLbl, icon);
297 WMReleasePixmap(icon);
298 } else {
299 WMSetLabelImage(panel->iLbl, scrPtr->applicationIconPixmap);
302 if (title) {
303 WMFont *largeFont;
305 largeFont = WMBoldSystemFontOfSize(scrPtr, 24);
307 panel->tLbl = WMCreateLabel(hbox);
308 WMMapWidget(panel->tLbl);
309 WMAddBoxSubview(hbox, WMWidgetView(panel->tLbl), True, True, 64, 0, 0);
310 WMSetLabelText(panel->tLbl, title);
311 WMSetLabelTextAlignment(panel->tLbl, WALeft);
312 WMSetLabelFont(panel->tLbl, largeFont);
314 WMReleaseFont(largeFont);
317 /* create divider line */
319 panel->line = WMCreateFrame(panel->win);
320 WMMapWidget(panel->line);
321 WMAddBoxSubview(panel->vbox, WMWidgetView(panel->line), False, True, 2, 2, WMScaleY(5));
322 WMSetFrameRelief(panel->line, WRGroove);
324 if (msg) {
325 panel->mLbl = WMCreateLabel(panel->vbox);
326 WMSetLabelWraps(panel->mLbl, True);
327 WMMapWidget(panel->mLbl);
328 WMAddBoxSubview(panel->vbox, WMWidgetView(panel->mLbl), True, True,
329 WMFontHeight(scrPtr->normalFont) * 4, 0, WMScaleY(5));
330 WMSetLabelText(panel->mLbl, msg);
331 WMSetLabelTextAlignment(panel->mLbl, WACenter);
334 panel->hbox = WMCreateBox(panel->vbox);
335 WMSetBoxBorderWidth(panel->hbox, WMScaleX(10));
336 WMSetBoxHorizontal(panel->hbox, True);
337 WMMapWidget(panel->hbox);
338 WMAddBoxSubview(panel->vbox, WMWidgetView(panel->hbox), False, True, WMScaleY(44), 0, 0);
340 /* create buttons */
341 if (otherButton)
342 ow = WMWidthOfString(scrPtr->normalFont, otherButton, strlen(otherButton));
344 if (alternateButton)
345 aw = WMWidthOfString(scrPtr->normalFont, alternateButton, strlen(alternateButton));
347 if (defaultButton)
348 dw = WMWidthOfString(scrPtr->normalFont, defaultButton, strlen(defaultButton));
350 dw = dw + (scrPtr->buttonArrow ? scrPtr->buttonArrow->width : 0);
352 aw += WMScaleX(30);
353 ow += WMScaleX(30);
354 dw += WMScaleX(30);
356 w = WMAX(dw, WMAX(aw, ow));
357 if ((w + WMScaleX(10)) * 3 < pwidth) {
358 aw = w;
359 ow = w;
360 dw = w;
361 } else {
362 int t;
364 t = pwidth - 4 * WMScaleX(10) - aw - ow - dw;
365 aw += t / 3;
366 ow += t / 3;
367 dw += t / 3;
370 if (defaultButton) {
371 panel->defBtn = WMCreateCommandButton(panel->hbox);
372 WMResizeWidget(panel->defBtn, dw, WMScaleY(24));
373 WMSetButtonAction(panel->defBtn, alertPanelOnClick, panel);
374 WMAddBoxSubviewAtEnd(panel->hbox, WMWidgetView(panel->defBtn), False, True, dw, 0, 0);
375 WMSetButtonText(panel->defBtn, defaultButton);
376 WMSetButtonImage(panel->defBtn, scrPtr->buttonArrow);
377 WMSetButtonAltImage(panel->defBtn, scrPtr->pushedButtonArrow);
378 WMSetButtonImagePosition(panel->defBtn, WIPRight);
380 if (alternateButton) {
381 panel->altBtn = WMCreateCommandButton(panel->hbox);
382 WMResizeWidget(panel->altBtn, aw, WMScaleY(24));
383 WMAddBoxSubviewAtEnd(panel->hbox, WMWidgetView(panel->altBtn), False, True, aw, 0, WMScaleX(5));
384 WMSetButtonAction(panel->altBtn, alertPanelOnClick, panel);
385 WMSetButtonText(panel->altBtn, alternateButton);
387 if (otherButton) {
388 panel->othBtn = WMCreateCommandButton(panel->hbox);
389 WMResizeWidget(panel->othBtn, ow, WMScaleY(24));
390 WMSetButtonAction(panel->othBtn, alertPanelOnClick, panel);
391 WMAddBoxSubviewAtEnd(panel->hbox, WMWidgetView(panel->othBtn), False, True, ow, 0, WMScaleX(5));
392 WMSetButtonText(panel->othBtn, otherButton);
395 WMMapSubwidgets(panel->hbox);
397 WMCreateEventHandler(W_VIEW(panel->win), KeyPressMask, handleKeyPress, panel);
399 WMRealizeWidget(panel->win);
400 WMMapSubwidgets(panel->win);
402 return panel;
405 static void inputBoxOnClick(WMWidget * self, void *clientData)
407 WMInputPanel *panel = clientData;
409 WMBreakModalLoop(WMWidgetScreen(self));
410 if (self == panel->defBtn) {
411 panel->result = WAPRDefault;
412 } else if (self == panel->altBtn) {
413 panel->result = WAPRAlternate;
417 static void handleKeyPress2(XEvent * event, void *clientData)
419 WMInputPanel *panel = (WMInputPanel *) clientData;
420 KeySym ksym;
422 XLookupString(&event->xkey, NULL, 0, &ksym, NULL);
424 if ((ksym == XK_Return || ksym == XK_KP_Enter) && panel->defBtn) {
425 WMPerformButtonClick(panel->defBtn);
426 } else if (ksym == XK_Escape) {
427 if (panel->altBtn) {
428 WMPerformButtonClick(panel->altBtn);
429 } else {
430 /* printf("got esc\n"); */
431 WMBreakModalLoop(WMWidgetScreen(panel->win));
432 panel->result = WAPRDefault;
437 char *WMRunInputPanel(WMScreen * scrPtr, WMWindow * owner, const char *title,
438 const char *msg, const char *defaultText, const char *okButton, const char *cancelButton)
440 WMInputPanel *panel;
441 char *tmp;
443 panel = WMCreateInputPanel(scrPtr, owner, title, msg, defaultText, okButton, cancelButton);
446 int px, py;
447 WMView *view = WMWidgetView(panel->win);
449 if (owner) {
450 WMView *oview = WMWidgetView(owner);
451 WMPoint pt = WMGetViewScreenPosition(oview);
453 px = (W_VIEW_WIDTH(oview) - W_VIEW_WIDTH(view)) / 2;
454 py = (W_VIEW_HEIGHT(oview) - W_VIEW_HEIGHT(view)) / 2;
456 px += pt.x;
457 py += pt.y;
458 } else {
459 px = (W_VIEW_WIDTH(scrPtr->rootView) - W_VIEW_WIDTH(view)) / 2;
460 py = (W_VIEW_HEIGHT(scrPtr->rootView) - W_VIEW_HEIGHT(view)) / 2;
462 WMSetWindowInitialPosition(panel->win, px, py);
465 WMMapWidget(panel->win);
467 WMRunModalLoop(scrPtr, W_VIEW(panel->win));
469 if (panel->result == WAPRDefault)
470 tmp = WMGetTextFieldText(panel->text);
471 else
472 tmp = NULL;
474 WMDestroyInputPanel(panel);
476 return tmp;
479 void WMDestroyInputPanel(WMInputPanel * panel)
481 WMRemoveNotificationObserver(panel);
482 WMUnmapWidget(panel->win);
483 WMDestroyWidget(panel->win);
484 wfree(panel);
487 static void endedEditingObserver(void *observerData, WMNotification * notification)
489 WMInputPanel *panel = (WMInputPanel *) observerData;
491 switch ((uintptr_t)WMGetNotificationClientData(notification)) {
492 case WMReturnTextMovement:
493 if (panel->defBtn)
494 WMPerformButtonClick(panel->defBtn);
495 break;
496 case WMEscapeTextMovement:
497 if (panel->altBtn)
498 WMPerformButtonClick(panel->altBtn);
499 else {
500 WMBreakModalLoop(WMWidgetScreen(panel->win));
501 panel->result = WAPRDefault;
503 break;
504 default:
505 break;
509 WMInputPanel *WMCreateInputPanel(WMScreen * scrPtr, WMWindow * owner, const char *title, const char *msg,
510 const char *defaultText, const char *okButton, const char *cancelButton)
512 WMInputPanel *panel;
513 WMFont *defaultFont;
514 int x, dw = 0, aw = 0, w;
516 defaultFont = WMSystemFontOfSize(scrPtr, 12);
517 panel = wmalloc(sizeof(WMInputPanel));
519 if (owner)
520 panel->win = WMCreatePanelWithStyleForWindow(owner, "inputPanel", WMTitledWindowMask);
521 else
522 panel->win = WMCreateWindowWithStyle(scrPtr, "inputPanel", WMTitledWindowMask);
523 WMSetWindowTitle(panel->win, "");
525 WMResizeWidget(panel->win, 320, 160);
527 if (title) {
528 WMFont *largeFont;
530 largeFont = WMBoldSystemFontOfSize(scrPtr, 24);
532 panel->tLbl = WMCreateLabel(panel->win);
533 WMMoveWidget(panel->tLbl, 20, 16);
534 WMResizeWidget(panel->tLbl, 320 - 40, WMFontHeight(largeFont) + 4);
535 WMSetLabelText(panel->tLbl, title);
536 WMSetLabelTextAlignment(panel->tLbl, WALeft);
537 WMSetLabelFont(panel->tLbl, largeFont);
539 WMReleaseFont(largeFont);
542 if (msg) {
543 panel->mLbl = WMCreateLabel(panel->win);
544 WMMoveWidget(panel->mLbl, 20, 50);
545 WMResizeWidget(panel->mLbl, 320 - 40, WMFontHeight(scrPtr->normalFont) * 2);
546 WMSetLabelText(panel->mLbl, msg);
547 WMSetLabelTextAlignment(panel->mLbl, WALeft);
548 WMSetLabelFont(panel->mLbl, defaultFont);
551 panel->text = WMCreateTextField(panel->win);
552 WMMoveWidget(panel->text, 20, 85);
553 WMResizeWidget(panel->text, 320 - 40, WMWidgetHeight(panel->text));
554 WMSetTextFieldText(panel->text, defaultText);
555 WMSetTextFieldFont(panel->text, defaultFont);
557 WMAddNotificationObserver(endedEditingObserver, panel, WMTextDidEndEditingNotification, panel->text);
559 /* create buttons */
560 if (cancelButton)
561 aw = WMWidthOfString(defaultFont, cancelButton, strlen(cancelButton));
563 if (okButton)
564 dw = WMWidthOfString(defaultFont, okButton, strlen(okButton));
566 w = dw + (scrPtr->buttonArrow ? scrPtr->buttonArrow->width : 0);
567 if (aw > w)
568 w = aw;
570 w += 30;
571 x = 310;
573 if (okButton) {
574 x -= w + 10;
576 panel->defBtn = WMCreateCustomButton(panel->win, WBBPushInMask
577 | WBBPushChangeMask | WBBPushLightMask);
578 WMSetButtonAction(panel->defBtn, inputBoxOnClick, panel);
579 WMMoveWidget(panel->defBtn, x, 124);
580 WMResizeWidget(panel->defBtn, w, 24);
581 WMSetButtonText(panel->defBtn, okButton);
582 WMSetButtonImage(panel->defBtn, scrPtr->buttonArrow);
583 WMSetButtonAltImage(panel->defBtn, scrPtr->pushedButtonArrow);
584 WMSetButtonImagePosition(panel->defBtn, WIPRight);
585 WMSetButtonFont(panel->defBtn, defaultFont);
587 if (cancelButton) {
588 x -= w + 10;
590 panel->altBtn = WMCreateCommandButton(panel->win);
591 WMSetButtonAction(panel->altBtn, inputBoxOnClick, panel);
592 WMMoveWidget(panel->altBtn, x, 124);
593 WMResizeWidget(panel->altBtn, w, 24);
594 WMSetButtonText(panel->altBtn, cancelButton);
595 WMSetButtonFont(panel->altBtn, defaultFont);
598 WMCreateEventHandler(W_VIEW(panel->win), KeyPressMask, handleKeyPress2, panel);
600 WMRealizeWidget(panel->win);
601 WMMapSubwidgets(panel->win);
603 WMSetFocusToWidget(panel->text);
605 WMReleaseFont(defaultFont);
607 return panel;
610 WMInputPanel *WMCreateScaledInputPanel(WMScreen * scrPtr, WMWindow * owner, const char *title, const char *msg,
611 const char *defaultText, const char *okButton, const char *cancelButton)
613 WMInputPanel *panel;
614 int x, dw = 0, aw = 0, w;
615 int wmScaleWidth, wmScaleHeight;
617 panel = wmalloc(sizeof(WMInputPanel));
619 if (owner)
620 panel->win = WMCreatePanelWithStyleForWindow(owner, "inputPanel", WMTitledWindowMask);
621 else
622 panel->win = WMCreateWindowWithStyle(scrPtr, "inputPanel", WMTitledWindowMask);
623 WMSetWindowTitle(panel->win, "");
625 WMGetScaleBaseFromSystemFont(scrPtr, &wmScaleWidth, &wmScaleHeight);
626 WMResizeWidget(panel->win, WMScaleX(320), WMScaleY(160));
628 if (title) {
629 WMFont *largeFont;
631 largeFont = WMBoldSystemFontOfSize(scrPtr, WMScaleY(24));
633 panel->tLbl = WMCreateLabel(panel->win);
634 WMMoveWidget(panel->tLbl, WMScaleX(20), WMScaleY(16));
635 WMResizeWidget(panel->tLbl, WMScaleX(320) - 2 * WMScaleX(20), WMFontHeight(largeFont) + WMScaleY(4));
636 WMSetLabelText(panel->tLbl, title);
637 WMSetLabelTextAlignment(panel->tLbl, WALeft);
638 WMSetLabelFont(panel->tLbl, largeFont);
640 WMReleaseFont(largeFont);
643 if (msg) {
644 panel->mLbl = WMCreateLabel(panel->win);
645 WMMoveWidget(panel->mLbl, WMScaleX(20), WMScaleY(50));
646 WMResizeWidget(panel->mLbl, WMScaleX(320) - 2 * WMScaleX(20), WMFontHeight(scrPtr->normalFont) * 2);
647 WMSetLabelText(panel->mLbl, msg);
648 WMSetLabelTextAlignment(panel->mLbl, WALeft);
651 panel->text = WMCreateTextField(panel->win);
652 WMMoveWidget(panel->text, WMScaleX(20), WMScaleY(85));
653 WMResizeWidget(panel->text, WMScaleX(320) - 2 * WMScaleX(20), WMScaleY(20));
654 WMSetTextFieldText(panel->text, defaultText);
656 WMAddNotificationObserver(endedEditingObserver, panel, WMTextDidEndEditingNotification, panel->text);
658 /* create buttons */
659 if (cancelButton)
660 aw = WMWidthOfString(scrPtr->normalFont, cancelButton, strlen(cancelButton));
662 if (okButton)
663 dw = WMWidthOfString(scrPtr->normalFont, okButton, strlen(okButton));
665 w = dw + (scrPtr->buttonArrow ? scrPtr->buttonArrow->width : 0);
666 if (aw > w)
667 w = aw;
669 w += WMScaleX(30);
670 x = WMScaleX(310);
672 if (okButton) {
673 x -= w + WMScaleX(10);
675 panel->defBtn = WMCreateCustomButton(panel->win, WBBPushInMask
676 | WBBPushChangeMask | WBBPushLightMask);
677 WMSetButtonAction(panel->defBtn, inputBoxOnClick, panel);
678 WMMoveWidget(panel->defBtn, x, WMScaleY(124));
679 WMResizeWidget(panel->defBtn, w, WMScaleY(24));
680 WMSetButtonText(panel->defBtn, okButton);
681 WMSetButtonImage(panel->defBtn, scrPtr->buttonArrow);
682 WMSetButtonAltImage(panel->defBtn, scrPtr->pushedButtonArrow);
683 WMSetButtonImagePosition(panel->defBtn, WIPRight);
685 if (cancelButton) {
686 x -= w + WMScaleX(10);
688 panel->altBtn = WMCreateCommandButton(panel->win);
689 WMSetButtonAction(panel->altBtn, inputBoxOnClick, panel);
690 WMMoveWidget(panel->altBtn, x, WMScaleY(124));
691 WMResizeWidget(panel->altBtn, w, WMScaleY(24));
692 WMSetButtonText(panel->altBtn, cancelButton);
695 WMCreateEventHandler(W_VIEW(panel->win), KeyPressMask, handleKeyPress2, panel);
697 WMRealizeWidget(panel->win);
698 WMMapSubwidgets(panel->win);
700 WMSetFocusToWidget(panel->text);
702 return panel;
705 static void handleKeyPress3(XEvent * event, void *clientData)
707 WMGenericPanel *panel = (WMGenericPanel *) clientData;
708 KeySym ksym;
710 XLookupString(&event->xkey, NULL, 0, &ksym, NULL);
712 if ((ksym == XK_Return || ksym == XK_KP_Enter) && panel->defBtn) {
713 WMPerformButtonClick(panel->defBtn);
714 } else if (ksym == XK_Escape) {
715 if (panel->altBtn) {
716 WMPerformButtonClick(panel->altBtn);
717 } else {
718 panel->result = WAPRDefault;
719 WMBreakModalLoop(WMWidgetScreen(panel->win));
724 void WMDestroyGenericPanel(WMGenericPanel * panel)
726 WMUnmapWidget(panel->win);
727 WMDestroyWidget(panel->win);
728 wfree(panel);
731 WMGenericPanel *WMCreateGenericPanel(WMScreen * scrPtr, WMWindow * owner,
732 const char *title, const char *defaultButton, const char *alternateButton)
734 WMGenericPanel *panel;
735 WMFont *defaultFont;
736 int dw = 0, aw = 0, w;
737 WMBox *hbox;
738 WMPixmap *icon;
740 defaultFont = WMSystemFontOfSize(scrPtr, 12);
741 panel = wmalloc(sizeof(WMGenericPanel));
743 if (owner) {
744 panel->win = WMCreatePanelWithStyleForWindow(owner, "genericPanel", WMTitledWindowMask);
745 } else {
746 panel->win = WMCreateWindowWithStyle(scrPtr, "genericPanel", WMTitledWindowMask);
749 WMSetWindowInitialPosition(panel->win,
750 (scrPtr->rootView->size.width - WMWidgetWidth(panel->win)) / 2,
751 (scrPtr->rootView->size.height - WMWidgetHeight(panel->win)) / 2);
753 WMSetWindowTitle(panel->win, "");
755 panel->vbox = WMCreateBox(panel->win);
756 WMSetViewExpandsToParent(WMWidgetView(panel->vbox), 0, 0, 0, 0);
757 WMSetBoxHorizontal(panel->vbox, False);
758 WMMapWidget(panel->vbox);
760 hbox = WMCreateBox(panel->vbox);
761 WMSetBoxBorderWidth(hbox, 5);
762 WMSetBoxHorizontal(hbox, True);
763 WMMapWidget(hbox);
764 WMAddBoxSubview(panel->vbox, WMWidgetView(hbox), False, True, 74, 0, 5);
766 panel->iLbl = WMCreateLabel(hbox);
767 WMSetLabelImagePosition(panel->iLbl, WIPImageOnly);
768 WMMapWidget(panel->iLbl);
769 WMAddBoxSubview(hbox, WMWidgetView(panel->iLbl), False, True, 64, 0, 10);
770 icon = WMCreateApplicationIconBlendedPixmap(scrPtr, (RColor *) NULL);
771 if (icon) {
772 WMSetLabelImage(panel->iLbl, icon);
773 WMReleasePixmap(icon);
774 } else {
775 WMSetLabelImage(panel->iLbl, scrPtr->applicationIconPixmap);
778 if (title) {
779 WMFont *largeFont;
781 largeFont = WMBoldSystemFontOfSize(scrPtr, 24);
783 panel->tLbl = WMCreateLabel(hbox);
784 WMMapWidget(panel->tLbl);
785 WMAddBoxSubview(hbox, WMWidgetView(panel->tLbl), True, True, 64, 0, 0);
786 WMSetLabelText(panel->tLbl, title);
787 WMSetLabelTextAlignment(panel->tLbl, WALeft);
788 WMSetLabelFont(panel->tLbl, largeFont);
790 WMReleaseFont(largeFont);
793 /* create divider line */
795 panel->line = WMCreateFrame(panel->vbox);
796 WMMapWidget(panel->line);
797 WMAddBoxSubview(panel->vbox, WMWidgetView(panel->line), False, True, 2, 2, 5);
798 WMSetFrameRelief(panel->line, WRGroove);
800 panel->content = WMCreateFrame(panel->vbox);
801 WMMapWidget(panel->content);
802 WMAddBoxSubview(panel->vbox, WMWidgetView(panel->content), True, True, 50, 0, 5);
803 WMSetFrameRelief(panel->content, WRFlat);
805 hbox = WMCreateBox(panel->vbox);
806 WMSetBoxBorderWidth(hbox, 10);
807 WMSetBoxHorizontal(hbox, True);
808 WMMapWidget(hbox);
809 WMAddBoxSubview(panel->vbox, WMWidgetView(hbox), False, True, 44, 0, 0);
811 /* create buttons */
812 if (defaultButton)
813 dw = WMWidthOfString(defaultFont, defaultButton, strlen(defaultButton));
815 if (alternateButton)
816 aw = WMWidthOfString(defaultFont, alternateButton, strlen(alternateButton));
818 dw = dw + (scrPtr->buttonArrow ? scrPtr->buttonArrow->width : 0);
820 aw += 30;
821 dw += 30;
823 w = WMAX(dw, aw);
824 if ((w + 10) * 2 < 400) {
825 dw = w;
826 } else {
827 int t;
829 t = 400 - 40 - aw - dw;
830 dw += t / 2;
833 if (defaultButton) {
834 panel->defBtn = WMCreateCommandButton(hbox);
835 WMSetButtonAction(panel->defBtn, alertPanelOnClick, panel);
836 WMAddBoxSubviewAtEnd(hbox, WMWidgetView(panel->defBtn), False, True, dw, 0, 0);
837 WMSetButtonText(panel->defBtn, defaultButton);
838 WMSetButtonImage(panel->defBtn, scrPtr->buttonArrow);
839 WMSetButtonAltImage(panel->defBtn, scrPtr->pushedButtonArrow);
840 WMSetButtonImagePosition(panel->defBtn, WIPRight);
841 WMSetButtonFont(panel->defBtn, defaultFont);
844 WMMapSubwidgets(hbox);
846 WMCreateEventHandler(W_VIEW(panel->win), KeyPressMask, handleKeyPress3, panel);
848 WMRealizeWidget(panel->win);
849 WMMapSubwidgets(panel->win);
851 WMReleaseFont(defaultFont);
853 return panel;