5 #include <X11/keysym.h>
10 alertPanelOnClick(WMWidget
*self
, void *clientData
)
12 WMAlertPanel
*panel
= clientData
;
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
;
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
);
37 panel
->result
= WAPRDefault
;
45 WMRunAlertPanel(WMScreen
*scrPtr
, WMWindow
*owner
,
46 char *title
, char *msg
, char *defaultButton
,
47 char *alternateButton
, char *otherButton
)
52 panel
= WMCreateAlertPanel(scrPtr
, owner
, title
, msg
, defaultButton
,
53 alternateButton
, otherButton
);
57 WMView
*view
= WMWidgetView(panel
->win
);
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;
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
);
80 while (!panel
->done
|| WMScreenPending(scrPtr
)) {
83 WMNextEvent(scrPtr
->display
, &event
);
84 WMHandleEvent(&event
);
90 WMDestroyAlertPanel(panel
);
97 WMDestroyAlertPanel(WMAlertPanel
*panel
)
99 WMUnmapWidget(panel
->win
);
100 WMDestroyWidget(panel
->win
);
106 WMCreateAlertPanel(WMScreen
*scrPtr
, WMWindow
*owner
,
107 char *title
, char *msg
, char *defaultButton
,
108 char *alternateButton
, char *otherButton
)
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
);
122 panel
->win
= WMCreatePanelWithStyleForWindow(owner
, "alertPanel",
125 panel
->win
= WMCreateWindowWithStyle(scrPtr
, "alertPanel",
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
);
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
);
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
);
179 ow
= WMWidthOfString(scrPtr
->normalFont
, otherButton
,
180 strlen(otherButton
));
183 aw
= WMWidthOfString(scrPtr
->normalFont
, alternateButton
,
184 strlen(alternateButton
));
187 dw
= WMWidthOfString(scrPtr
->normalFont
, defaultButton
,
188 strlen(defaultButton
));
190 dw
= dw
+ (scrPtr
->buttonArrow
? scrPtr
->buttonArrow
->width
: 0);
196 w
= WMAX(dw
, WMAX(aw
, ow
));
197 if ((w
+10)*3 < 400) {
204 t
= 400 - 40 - aw
- ow
- dw
;
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
) {
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
);
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
);
245 WMCreateEventHandler(W_VIEW(panel
->win
), KeyPressMask
,
246 handleKeyPress
, panel
);
248 WMRealizeWidget(panel
->win
);
249 WMMapSubwidgets(panel
->win
);
259 inputBoxOnClick(WMWidget
*self
, void *clientData
)
261 WMInputPanel
*panel
= clientData
;
264 if (self
== panel
->defBtn
) {
265 panel
->result
= WAPRDefault
;
266 } else if (self
== panel
->altBtn
) {
267 panel
->result
= WAPRAlternate
;
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
) {
283 WMPerformButtonClick(panel
->altBtn
);
285 /* printf("got esc\n");*/
287 panel
->result
= WAPRDefault
;
294 WMRunInputPanel(WMScreen
*scrPtr
, WMWindow
*owner
, char *title
,
295 char *msg
, char *defaultText
,
296 char *okButton
, char *cancelButton
)
301 panel
= WMCreateInputPanel(scrPtr
, owner
, title
, msg
, defaultText
,
302 okButton
, cancelButton
);
307 WMView
*view
= WMWidgetView(panel
->win
);
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;
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
)) {
330 WMNextEvent(scrPtr
->display
, &event
);
331 WMHandleEvent(&event
);
335 if (panel
->result
== WAPRDefault
)
336 tmp
= WMGetTextFieldText(panel
->text
);
340 WMDestroyInputPanel(panel
);
347 WMDestroyInputPanel(WMInputPanel
*panel
)
349 WMRemoveNotificationObserver(panel
);
350 WMUnmapWidget(panel
->win
);
351 WMDestroyWidget(panel
->win
);
358 endedEditingObserver(void *observerData
, WMNotification
*notification
)
360 WMInputPanel
*panel
= (WMInputPanel
*)observerData
;
362 switch ((int)WMGetNotificationClientData(notification
)) {
363 case WMReturnTextMovement
:
365 WMPerformButtonClick(panel
->defBtn
);
367 case WMEscapeTextMovement
:
369 WMPerformButtonClick(panel
->altBtn
);
372 panel
->result
= WAPRDefault
;
382 WMCreateInputPanel(WMScreen
*scrPtr
, WMWindow
*owner
, char *title
, char *msg
,
383 char *defaultText
, char *okButton
, char *cancelButton
)
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
);
396 panel
->win
= WMCreatePanelWithStyleForWindow(owner
, "inputPanel",
399 panel
->win
= WMCreateWindowWithStyle(scrPtr
, "inputPanel",
401 WMSetWindowTitle(panel
->win
, "");
403 WMResizeWidget(panel
->win
, 320, 160);
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
);
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
);
440 aw
= WMWidthOfString(scrPtr
->normalFont
, cancelButton
,
441 strlen(cancelButton
));
444 dw
= WMWidthOfString(scrPtr
->normalFont
, okButton
,
447 w
= dw
+ (scrPtr
->buttonArrow
? scrPtr
->buttonArrow
->width
: 0);
457 panel
->defBtn
= WMCreateCustomButton(panel
->win
, WBBPushInMask
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
);
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
);
480 WMCreateEventHandler(W_VIEW(panel
->win
), KeyPressMask
,
481 handleKeyPress2
, panel
);
483 WMRealizeWidget(panel
->win
);
484 WMMapSubwidgets(panel
->win
);
486 WMSetFocusToWidget(panel
->text
);