Guess what? Yep, another wmtext update. Now this was all, please go to
[wmaker-crm.git] / WINGs / wpanel.c
blobc7149a86f88ea754fea428acde49d13418ca600c
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);
108 WMSetWindowInitialPosition(panel->win,
109 (scrPtr->rootView->size.width - WMWidgetWidth(panel->win))/2,
110 (scrPtr->rootView->size.height - WMWidgetHeight(panel->win))/2);
112 WMSetWindowTitle(panel->win, "");
114 if (scrPtr->applicationIcon) {
115 panel->iLbl = WMCreateLabel(panel->win);
116 WMResizeWidget(panel->iLbl, scrPtr->applicationIcon->width,
117 scrPtr->applicationIcon->height);
118 WMMoveWidget(panel->iLbl, 8 + (64 - scrPtr->applicationIcon->width)/2,
119 (75 - scrPtr->applicationIcon->height)/2);
120 WMSetLabelImage(panel->iLbl, scrPtr->applicationIcon);
121 WMSetLabelImagePosition(panel->iLbl, WIPImageOnly);
124 if (title) {
125 WMFont *largeFont;
127 largeFont = WMBoldSystemFontOfSize(scrPtr, 24);
129 panel->tLbl = WMCreateLabel(panel->win);
130 WMMoveWidget(panel->tLbl, 80, (80 - WMFontHeight(largeFont))/2);
131 WMResizeWidget(panel->tLbl, 400 - 70, WMFontHeight(largeFont)+4);
132 WMSetLabelText(panel->tLbl, title);
133 WMSetLabelTextAlignment(panel->tLbl, WALeft);
134 WMSetLabelFont(panel->tLbl, largeFont);
136 WMReleaseFont(largeFont);
140 if (msg) {
141 panel->mLbl = WMCreateLabel(panel->win);
142 WMMoveWidget(panel->mLbl, 10, 83);
143 WMResizeWidget(panel->mLbl, 380, WMFontHeight(scrPtr->normalFont)*4);
144 WMSetLabelText(panel->mLbl, msg);
145 WMSetLabelTextAlignment(panel->mLbl, WACenter);
149 /* create divider line */
151 panel->line = WMCreateFrame(panel->win);
152 WMMoveWidget(panel->line, 0, 80);
153 WMResizeWidget(panel->line, 400, 2);
154 WMSetFrameRelief(panel->line, WRGroove);
156 /* create buttons */
157 if (otherButton)
158 ow = WMWidthOfString(scrPtr->normalFont, otherButton,
159 strlen(otherButton));
161 if (alternateButton)
162 aw = WMWidthOfString(scrPtr->normalFont, alternateButton,
163 strlen(alternateButton));
165 if (defaultButton)
166 dw = WMWidthOfString(scrPtr->normalFont, defaultButton,
167 strlen(defaultButton));
169 w = dw + (scrPtr->buttonArrow ? scrPtr->buttonArrow->width : 0);
170 if (aw > w)
171 w = aw;
172 if (ow > w)
173 w = ow;
175 w += 30;
176 x = 400;
178 if (defaultButton) {
179 x -= w + 10;
181 panel->defBtn = WMCreateCommandButton(panel->win);
182 WMSetButtonAction(panel->defBtn, alertPanelOnClick, panel);
183 WMMoveWidget(panel->defBtn, x, 144);
184 WMResizeWidget(panel->defBtn, w, 24);
185 WMSetButtonText(panel->defBtn, defaultButton);
186 WMSetButtonImage(panel->defBtn, scrPtr->buttonArrow);
187 WMSetButtonAltImage(panel->defBtn, scrPtr->pushedButtonArrow);
188 WMSetButtonImagePosition(panel->defBtn, WIPRight);
190 if (alternateButton) {
191 x -= w + 10;
193 panel->altBtn = WMCreateCommandButton(panel->win);
194 WMMoveWidget(panel->altBtn, x, 144);
195 WMResizeWidget(panel->altBtn, w, 24);
196 WMSetButtonAction(panel->altBtn, alertPanelOnClick, panel);
197 WMSetButtonText(panel->altBtn, alternateButton);
199 if (otherButton) {
200 x -= w + 10;
202 panel->othBtn = WMCreateCommandButton(panel->win);
203 WMSetButtonAction(panel->othBtn, alertPanelOnClick, panel);
204 WMMoveWidget(panel->othBtn, x, 144);
205 WMResizeWidget(panel->othBtn, w, 24);
206 WMSetButtonText(panel->othBtn, otherButton);
209 panel->done = 0;
211 WMCreateEventHandler(W_VIEW(panel->win), KeyPressMask,
212 handleKeyPress, panel);
214 WMRealizeWidget(panel->win);
215 WMMapSubwidgets(panel->win);
217 return panel;
224 static void
225 inputBoxOnClick(WMWidget *self, void *clientData)
227 WMInputPanel *panel = clientData;
229 panel->done = 1;
230 if (self == panel->defBtn) {
231 panel->result = WAPRDefault;
232 } else if (self == panel->altBtn) {
233 panel->result = WAPRAlternate;
239 static void
240 handleKeyPress2(XEvent *event, void *clientData)
242 WMInputPanel *panel = (WMInputPanel*)clientData;
244 if (event->xkey.keycode == panel->retKey && panel->defBtn) {
245 WMPerformButtonClick(panel->defBtn);
247 if (event->xkey.keycode == panel->escKey) {
248 if (panel->altBtn) {
249 WMPerformButtonClick(panel->altBtn);
250 } else {
251 /* printf("got esc\n");*/
252 panel->done = 1;
253 panel->result = WAPRDefault;
259 char*
260 WMRunInputPanel(WMScreen *scrPtr, WMWindow *owner, char *title,
261 char *msg, char *defaultText,
262 char *okButton, char *cancelButton)
264 WMInputPanel *panel;
265 char *tmp;
267 panel = WMCreateInputPanel(scrPtr, owner, title, msg, defaultText,
268 okButton, cancelButton);
270 WMMapWidget(panel->win);
272 while (!panel->done || WMScreenPending(scrPtr)) {
273 XEvent event;
275 WMNextEvent(scrPtr->display, &event);
276 WMHandleEvent(&event);
280 if (panel->result == WAPRDefault)
281 tmp = WMGetTextFieldText(panel->text);
282 else
283 tmp = NULL;
285 WMDestroyInputPanel(panel);
287 return tmp;
291 void
292 WMDestroyInputPanel(WMInputPanel *panel)
294 WMRemoveNotificationObserver(panel);
295 WMUnmapWidget(panel->win);
296 WMDestroyWidget(panel->win);
297 wfree(panel);
302 static void
303 endedEditingObserver(void *observerData, WMNotification *notification)
305 WMInputPanel *panel = (WMInputPanel*)observerData;
307 switch ((int)WMGetNotificationClientData(notification)) {
308 case WMReturnTextMovement:
309 if (panel->defBtn)
310 WMPerformButtonClick(panel->defBtn);
311 break;
312 case WMEscapeTextMovement:
313 if (panel->altBtn)
314 WMPerformButtonClick(panel->altBtn);
315 else {
316 panel->done = 1;
317 panel->result = WAPRDefault;
319 break;
320 default:
321 break;
326 WMInputPanel*
327 WMCreateInputPanel(WMScreen *scrPtr, WMWindow *owner, char *title, char *msg,
328 char *defaultText, char *okButton, char *cancelButton)
330 WMInputPanel *panel;
331 int x, dw=0, aw=0, w;
334 panel = wmalloc(sizeof(WMInputPanel));
335 memset(panel, 0, sizeof(WMInputPanel));
337 panel->retKey = XKeysymToKeycode(scrPtr->display, XK_Return);
338 panel->escKey = XKeysymToKeycode(scrPtr->display, XK_Escape);
340 if (owner)
341 panel->win = WMCreatePanelWithStyleForWindow(owner, "inputPanel",
342 WMTitledWindowMask);
343 else
344 panel->win = WMCreateWindowWithStyle(scrPtr, "inputPanel",
345 WMTitledWindowMask);
346 WMSetWindowTitle(panel->win, "");
348 WMResizeWidget(panel->win, 320, 160);
350 if (title) {
351 WMFont *largeFont;
353 largeFont = WMBoldSystemFontOfSize(scrPtr, 24);
355 panel->tLbl = WMCreateLabel(panel->win);
356 WMMoveWidget(panel->tLbl, 20, 16);
357 WMResizeWidget(panel->tLbl, 320 - 40, WMFontHeight(largeFont)+4);
358 WMSetLabelText(panel->tLbl, title);
359 WMSetLabelTextAlignment(panel->tLbl, WALeft);
360 WMSetLabelFont(panel->tLbl, largeFont);
362 WMReleaseFont(largeFont);
366 if (msg) {
367 panel->mLbl = WMCreateLabel(panel->win);
368 WMMoveWidget(panel->mLbl, 20, 50);
369 WMResizeWidget(panel->mLbl, 320 - 40,
370 WMFontHeight(scrPtr->normalFont)*2);
371 WMSetLabelText(panel->mLbl, msg);
372 WMSetLabelTextAlignment(panel->mLbl, WALeft);
375 panel->text = WMCreateTextField(panel->win);
376 WMMoveWidget(panel->text, 20, 85);
377 WMResizeWidget(panel->text, 320 - 40, WMWidgetHeight(panel->text));
378 WMSetTextFieldText(panel->text, defaultText);
380 WMAddNotificationObserver(endedEditingObserver, panel,
381 WMTextDidEndEditingNotification, panel->text);
383 /* create buttons */
384 if (cancelButton)
385 aw = WMWidthOfString(scrPtr->normalFont, cancelButton,
386 strlen(cancelButton));
388 if (okButton)
389 dw = WMWidthOfString(scrPtr->normalFont, okButton,
390 strlen(okButton));
392 w = dw + (scrPtr->buttonArrow ? scrPtr->buttonArrow->width : 0);
393 if (aw > w)
394 w = aw;
396 w += 30;
397 x = 310;
399 if (okButton) {
400 x -= w + 10;
402 panel->defBtn = WMCreateCustomButton(panel->win, WBBPushInMask
403 |WBBPushChangeMask
404 |WBBPushLightMask);
405 WMSetButtonAction(panel->defBtn, inputBoxOnClick, panel);
406 WMMoveWidget(panel->defBtn, x, 124);
407 WMResizeWidget(panel->defBtn, w, 24);
408 WMSetButtonText(panel->defBtn, okButton);
409 WMSetButtonImage(panel->defBtn, scrPtr->buttonArrow);
410 WMSetButtonAltImage(panel->defBtn, scrPtr->pushedButtonArrow);
411 WMSetButtonImagePosition(panel->defBtn, WIPRight);
413 if (cancelButton) {
414 x -= w + 10;
416 panel->altBtn = WMCreateCommandButton(panel->win);
417 WMSetButtonAction(panel->altBtn, inputBoxOnClick, panel);
418 WMMoveWidget(panel->altBtn, x, 124);
419 WMResizeWidget(panel->altBtn, w, 24);
420 WMSetButtonText(panel->altBtn, cancelButton);
423 panel->done = 0;
425 WMCreateEventHandler(W_VIEW(panel->win), KeyPressMask,
426 handleKeyPress2, panel);
428 WMRealizeWidget(panel->win);
429 WMMapSubwidgets(panel->win);
431 WMSetFocusToWidget(panel->text);
433 return panel;