added menu item objetc
[wmaker-crm.git] / WINGs / wpanel.c
blobd6ed093940da9ec30eaefa36812ba8567e0bcd54
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);
55 scrPtr->modalView = W_VIEW(panel->win);
56 WMMapWidget(panel->win);
58 scrPtr->modal = 1;
59 while (!panel->done || WMScreenPending(scrPtr)) {
60 XEvent event;
62 WMNextEvent(scrPtr->display, &event);
63 WMHandleEvent(&event);
65 scrPtr->modal = 0;
67 tmp = panel->result;
69 WMDestroyAlertPanel(panel);
71 return tmp;
75 void
76 WMDestroyAlertPanel(WMAlertPanel *panel)
78 WMUnmapWidget(panel->win);
79 WMDestroyWidget(panel->win);
80 wfree(panel);
84 WMAlertPanel*
85 WMCreateAlertPanel(WMScreen *scrPtr, WMWindow *owner,
86 char *title, char *msg, char *defaultButton,
87 char *alternateButton, char *otherButton)
89 WMAlertPanel *panel;
90 int x, dw=0, aw=0, ow=0, w;
93 panel = wmalloc(sizeof(WMAlertPanel));
94 memset(panel, 0, sizeof(WMAlertPanel));
97 panel->retKey = XKeysymToKeycode(scrPtr->display, XK_Return);
98 panel->escKey = XKeysymToKeycode(scrPtr->display, XK_Escape);
100 if (owner)
101 panel->win = WMCreatePanelWithStyleForWindow(owner, "alertPanel",
102 WMTitledWindowMask);
103 else
104 panel->win = WMCreateWindowWithStyle(scrPtr, "alertPanel",
105 WMTitledWindowMask);
107 WMSetWindowInitialPosition(panel->win,
108 (scrPtr->rootView->size.width - WMWidgetWidth(panel->win))/2,
109 (scrPtr->rootView->size.height - WMWidgetHeight(panel->win))/2);
111 WMSetWindowTitle(panel->win, "");
113 if (scrPtr->applicationIcon) {
114 panel->iLbl = WMCreateLabel(panel->win);
115 WMResizeWidget(panel->iLbl, scrPtr->applicationIcon->width,
116 scrPtr->applicationIcon->height);
117 WMMoveWidget(panel->iLbl, 8 + (64 - scrPtr->applicationIcon->width)/2,
118 (75 - scrPtr->applicationIcon->height)/2);
119 WMSetLabelImage(panel->iLbl, scrPtr->applicationIcon);
120 WMSetLabelImagePosition(panel->iLbl, WIPImageOnly);
123 if (title) {
124 WMFont *largeFont;
126 largeFont = WMBoldSystemFontOfSize(scrPtr, 24);
128 panel->tLbl = WMCreateLabel(panel->win);
129 WMMoveWidget(panel->tLbl, 80, (80 - WMFontHeight(largeFont))/2);
130 WMResizeWidget(panel->tLbl, 400 - 70, WMFontHeight(largeFont)+4);
131 WMSetLabelText(panel->tLbl, title);
132 WMSetLabelTextAlignment(panel->tLbl, WALeft);
133 WMSetLabelFont(panel->tLbl, largeFont);
135 WMReleaseFont(largeFont);
139 if (msg) {
140 panel->mLbl = WMCreateLabel(panel->win);
141 WMMoveWidget(panel->mLbl, 10, 83);
142 WMResizeWidget(panel->mLbl, 380, WMFontHeight(scrPtr->normalFont)*4);
143 WMSetLabelText(panel->mLbl, msg);
144 WMSetLabelTextAlignment(panel->mLbl, WACenter);
148 /* create divider line */
150 panel->line = WMCreateFrame(panel->win);
151 WMMoveWidget(panel->line, 0, 80);
152 WMResizeWidget(panel->line, 400, 2);
153 WMSetFrameRelief(panel->line, WRGroove);
155 /* create buttons */
156 if (otherButton)
157 ow = WMWidthOfString(scrPtr->normalFont, otherButton,
158 strlen(otherButton));
160 if (alternateButton)
161 aw = WMWidthOfString(scrPtr->normalFont, alternateButton,
162 strlen(alternateButton));
164 if (defaultButton)
165 dw = WMWidthOfString(scrPtr->normalFont, defaultButton,
166 strlen(defaultButton));
168 w = dw + (scrPtr->buttonArrow ? scrPtr->buttonArrow->width : 0);
169 if (aw > w)
170 w = aw;
171 if (ow > w)
172 w = ow;
174 w += 30;
175 x = 400;
177 if (defaultButton) {
178 x -= w + 10;
180 panel->defBtn = WMCreateCommandButton(panel->win);
181 WMSetButtonAction(panel->defBtn, alertPanelOnClick, panel);
182 WMMoveWidget(panel->defBtn, x, 144);
183 WMResizeWidget(panel->defBtn, w, 24);
184 WMSetButtonText(panel->defBtn, defaultButton);
185 WMSetButtonImage(panel->defBtn, scrPtr->buttonArrow);
186 WMSetButtonAltImage(panel->defBtn, scrPtr->pushedButtonArrow);
187 WMSetButtonImagePosition(panel->defBtn, WIPRight);
189 if (alternateButton) {
190 x -= w + 10;
192 panel->altBtn = WMCreateCommandButton(panel->win);
193 WMMoveWidget(panel->altBtn, x, 144);
194 WMResizeWidget(panel->altBtn, w, 24);
195 WMSetButtonAction(panel->altBtn, alertPanelOnClick, panel);
196 WMSetButtonText(panel->altBtn, alternateButton);
198 if (otherButton) {
199 x -= w + 10;
201 panel->othBtn = WMCreateCommandButton(panel->win);
202 WMSetButtonAction(panel->othBtn, alertPanelOnClick, panel);
203 WMMoveWidget(panel->othBtn, x, 144);
204 WMResizeWidget(panel->othBtn, w, 24);
205 WMSetButtonText(panel->othBtn, otherButton);
208 panel->done = 0;
210 WMCreateEventHandler(W_VIEW(panel->win), KeyPressMask,
211 handleKeyPress, panel);
213 WMRealizeWidget(panel->win);
214 WMMapSubwidgets(panel->win);
216 return panel;
223 static void
224 inputBoxOnClick(WMWidget *self, void *clientData)
226 WMInputPanel *panel = clientData;
228 panel->done = 1;
229 if (self == panel->defBtn) {
230 panel->result = WAPRDefault;
231 } else if (self == panel->altBtn) {
232 panel->result = WAPRAlternate;
238 static void
239 handleKeyPress2(XEvent *event, void *clientData)
241 WMInputPanel *panel = (WMInputPanel*)clientData;
243 if (event->xkey.keycode == panel->retKey && panel->defBtn) {
244 WMPerformButtonClick(panel->defBtn);
246 if (event->xkey.keycode == panel->escKey) {
247 if (panel->altBtn) {
248 WMPerformButtonClick(panel->altBtn);
249 } else {
250 /* printf("got esc\n");*/
251 panel->done = 1;
252 panel->result = WAPRDefault;
258 char*
259 WMRunInputPanel(WMScreen *scrPtr, WMWindow *owner, char *title,
260 char *msg, char *defaultText,
261 char *okButton, char *cancelButton)
263 WMInputPanel *panel;
264 char *tmp;
266 panel = WMCreateInputPanel(scrPtr, owner, title, msg, defaultText,
267 okButton, cancelButton);
269 WMMapWidget(panel->win);
271 while (!panel->done || WMScreenPending(scrPtr)) {
272 XEvent event;
274 WMNextEvent(scrPtr->display, &event);
275 WMHandleEvent(&event);
279 if (panel->result == WAPRDefault)
280 tmp = WMGetTextFieldText(panel->text);
281 else
282 tmp = NULL;
284 WMDestroyInputPanel(panel);
286 return tmp;
290 void
291 WMDestroyInputPanel(WMInputPanel *panel)
293 WMRemoveNotificationObserver(panel);
294 WMUnmapWidget(panel->win);
295 WMDestroyWidget(panel->win);
296 wfree(panel);
301 static void
302 endedEditingObserver(void *observerData, WMNotification *notification)
304 WMInputPanel *panel = (WMInputPanel*)observerData;
306 switch ((int)WMGetNotificationClientData(notification)) {
307 case WMReturnTextMovement:
308 if (panel->defBtn)
309 WMPerformButtonClick(panel->defBtn);
310 break;
311 case WMEscapeTextMovement:
312 if (panel->altBtn)
313 WMPerformButtonClick(panel->altBtn);
314 else {
315 panel->done = 1;
316 panel->result = WAPRDefault;
318 break;
319 default:
320 break;
325 WMInputPanel*
326 WMCreateInputPanel(WMScreen *scrPtr, WMWindow *owner, char *title, char *msg,
327 char *defaultText, char *okButton, char *cancelButton)
329 WMInputPanel *panel;
330 int x, dw=0, aw=0, w;
333 panel = wmalloc(sizeof(WMInputPanel));
334 memset(panel, 0, sizeof(WMInputPanel));
336 panel->retKey = XKeysymToKeycode(scrPtr->display, XK_Return);
337 panel->escKey = XKeysymToKeycode(scrPtr->display, XK_Escape);
339 if (owner)
340 panel->win = WMCreatePanelWithStyleForWindow(owner, "inputPanel",
341 WMTitledWindowMask);
342 else
343 panel->win = WMCreateWindowWithStyle(scrPtr, "inputPanel",
344 WMTitledWindowMask);
345 WMSetWindowTitle(panel->win, "");
347 WMResizeWidget(panel->win, 320, 160);
349 if (title) {
350 WMFont *largeFont;
352 largeFont = WMBoldSystemFontOfSize(scrPtr, 24);
354 panel->tLbl = WMCreateLabel(panel->win);
355 WMMoveWidget(panel->tLbl, 20, 16);
356 WMResizeWidget(panel->tLbl, 320 - 40, WMFontHeight(largeFont)+4);
357 WMSetLabelText(panel->tLbl, title);
358 WMSetLabelTextAlignment(panel->tLbl, WALeft);
359 WMSetLabelFont(panel->tLbl, largeFont);
361 WMReleaseFont(largeFont);
365 if (msg) {
366 panel->mLbl = WMCreateLabel(panel->win);
367 WMMoveWidget(panel->mLbl, 20, 50);
368 WMResizeWidget(panel->mLbl, 320 - 40,
369 WMFontHeight(scrPtr->normalFont)*2);
370 WMSetLabelText(panel->mLbl, msg);
371 WMSetLabelTextAlignment(panel->mLbl, WALeft);
374 panel->text = WMCreateTextField(panel->win);
375 WMMoveWidget(panel->text, 20, 85);
376 WMResizeWidget(panel->text, 320 - 40, WMWidgetHeight(panel->text));
377 WMSetTextFieldText(panel->text, defaultText);
379 WMAddNotificationObserver(endedEditingObserver, panel,
380 WMTextDidEndEditingNotification, panel->text);
382 /* create buttons */
383 if (cancelButton)
384 aw = WMWidthOfString(scrPtr->normalFont, cancelButton,
385 strlen(cancelButton));
387 if (okButton)
388 dw = WMWidthOfString(scrPtr->normalFont, okButton,
389 strlen(okButton));
391 w = dw + (scrPtr->buttonArrow ? scrPtr->buttonArrow->width : 0);
392 if (aw > w)
393 w = aw;
395 w += 30;
396 x = 310;
398 if (okButton) {
399 x -= w + 10;
401 panel->defBtn = WMCreateCustomButton(panel->win, WBBPushInMask
402 |WBBPushChangeMask
403 |WBBPushLightMask);
404 WMSetButtonAction(panel->defBtn, inputBoxOnClick, panel);
405 WMMoveWidget(panel->defBtn, x, 124);
406 WMResizeWidget(panel->defBtn, w, 24);
407 WMSetButtonText(panel->defBtn, okButton);
408 WMSetButtonImage(panel->defBtn, scrPtr->buttonArrow);
409 WMSetButtonAltImage(panel->defBtn, scrPtr->pushedButtonArrow);
410 WMSetButtonImagePosition(panel->defBtn, WIPRight);
412 if (cancelButton) {
413 x -= w + 10;
415 panel->altBtn = WMCreateCommandButton(panel->win);
416 WMSetButtonAction(panel->altBtn, inputBoxOnClick, panel);
417 WMMoveWidget(panel->altBtn, x, 124);
418 WMResizeWidget(panel->altBtn, w, 24);
419 WMSetButtonText(panel->altBtn, cancelButton);
422 panel->done = 0;
424 WMCreateEventHandler(W_VIEW(panel->win), KeyPressMask,
425 handleKeyPress2, panel);
427 WMRealizeWidget(panel->win);
428 WMMapSubwidgets(panel->win);
430 WMSetFocusToWidget(panel->text);
432 return panel;