Initial revision
[wmaker-crm.git] / WINGs / wpanel.c
bloba9d180dc711d8a0d556fa7ec765adcdcc33777ae
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) {
31 WMPerformButtonClick(panel->defBtn);
36 int
37 WMRunAlertPanel(WMScreen *scrPtr, WMWindow *owner,
38 char *title, char *msg, char *defaultButton,
39 char *alternateButton, char *otherButton)
41 WMAlertPanel *panel;
42 int tmp;
44 panel = WMCreateAlertPanel(scrPtr, owner, title, msg, defaultButton,
45 alternateButton, otherButton);
47 scrPtr->modalView = W_VIEW(panel->win);
48 WMMapWidget(panel->win);
50 scrPtr->modal = 1;
51 while (!panel->done || WMScreenPending(scrPtr)) {
52 XEvent event;
54 WMNextEvent(scrPtr->display, &event);
55 WMHandleEvent(&event);
57 scrPtr->modal = 0;
59 tmp = panel->result;
61 WMDestroyAlertPanel(panel);
63 return tmp;
67 void
68 WMDestroyAlertPanel(WMAlertPanel *panel)
70 WMUnmapWidget(panel->win);
71 WMDestroyWidget(panel->win);
72 free(panel);
76 WMAlertPanel*
77 WMCreateAlertPanel(WMScreen *scrPtr, WMWindow *owner,
78 char *title, char *msg, char *defaultButton,
79 char *alternateButton, char *otherButton)
81 WMAlertPanel *panel;
82 int x, dw=0, aw=0, ow=0, w;
85 panel = wmalloc(sizeof(WMAlertPanel));
86 memset(panel, 0, sizeof(WMAlertPanel));
89 panel->retKey = XKeysymToKeycode(scrPtr->display, XK_Return);
91 if (owner)
92 panel->win = WMCreatePanelWithStyleForWindow(owner, "alertPanel",
93 WMTitledWindowMask);
94 else
95 panel->win = WMCreateWindowWithStyle(scrPtr, "alertPanel",
96 WMTitledWindowMask);
98 WMSetWindowTitle(panel->win, "");
100 if (scrPtr->applicationIcon) {
101 panel->iLbl = WMCreateLabel(panel->win);
102 WMResizeWidget(panel->iLbl, scrPtr->applicationIcon->width,
103 scrPtr->applicationIcon->height);
104 WMMoveWidget(panel->iLbl, 8 + (64 - scrPtr->applicationIcon->width)/2,
105 (75 - scrPtr->applicationIcon->height)/2);
106 WMSetLabelImage(panel->iLbl, scrPtr->applicationIcon);
107 WMSetLabelImagePosition(panel->iLbl, WIPImageOnly);
110 if (title) {
111 WMFont *largeFont;
113 largeFont = WMBoldSystemFontOfSize(scrPtr, 24);
115 panel->tLbl = WMCreateLabel(panel->win);
116 WMMoveWidget(panel->tLbl, 80, (80 - largeFont->height)/2);
117 WMResizeWidget(panel->tLbl, 400 - 70, largeFont->height+4);
118 WMSetLabelText(panel->tLbl, title);
119 WMSetLabelTextAlignment(panel->tLbl, WALeft);
120 WMSetLabelFont(panel->tLbl, largeFont);
122 WMReleaseFont(largeFont);
126 if (msg) {
127 panel->mLbl = WMCreateLabel(panel->win);
128 WMMoveWidget(panel->mLbl, 10, 83);
129 WMResizeWidget(panel->mLbl, 380, scrPtr->normalFont->height*4);
130 WMSetLabelText(panel->mLbl, msg);
131 WMSetLabelTextAlignment(panel->mLbl, WACenter);
135 /* create divider line */
137 panel->line = WMCreateFrame(panel->win);
138 WMMoveWidget(panel->line, 0, 80);
139 WMResizeWidget(panel->line, 400, 2);
140 WMSetFrameRelief(panel->line, WRGroove);
142 /* create buttons */
143 if (otherButton)
144 ow = WMWidthOfString(scrPtr->normalFont, otherButton,
145 strlen(otherButton));
147 if (alternateButton)
148 aw = WMWidthOfString(scrPtr->normalFont, alternateButton,
149 strlen(alternateButton));
151 if (defaultButton)
152 dw = WMWidthOfString(scrPtr->normalFont, defaultButton,
153 strlen(defaultButton));
155 w = dw + (scrPtr->buttonArrow ? scrPtr->buttonArrow->width : 0);
156 if (aw > w)
157 w = aw;
158 if (ow > w)
159 w = ow;
161 w += 30;
162 x = 400;
164 if (defaultButton) {
165 x -= w + 10;
167 panel->defBtn = WMCreateCommandButton(panel->win);
168 WMSetButtonAction(panel->defBtn, alertPanelOnClick, panel);
169 WMMoveWidget(panel->defBtn, x, 144);
170 WMResizeWidget(panel->defBtn, w, 24);
171 WMSetButtonText(panel->defBtn, defaultButton);
172 WMSetButtonImage(panel->defBtn, scrPtr->buttonArrow);
173 WMSetButtonAltImage(panel->defBtn, scrPtr->pushedButtonArrow);
174 WMSetButtonImagePosition(panel->defBtn, WIPRight);
176 if (alternateButton) {
177 x -= w + 10;
179 panel->altBtn = WMCreateCommandButton(panel->win);
180 WMMoveWidget(panel->altBtn, x, 144);
181 WMResizeWidget(panel->altBtn, w, 24);
182 WMSetButtonAction(panel->altBtn, alertPanelOnClick, panel);
183 WMSetButtonText(panel->altBtn, alternateButton);
185 if (otherButton) {
186 x -= w + 10;
188 panel->othBtn = WMCreateCommandButton(panel->win);
189 WMSetButtonAction(panel->othBtn, alertPanelOnClick, panel);
190 WMMoveWidget(panel->othBtn, x, 144);
191 WMResizeWidget(panel->othBtn, w, 24);
192 WMSetButtonText(panel->othBtn, otherButton);
195 panel->done = 0;
197 WMCreateEventHandler(W_VIEW(panel->win), KeyPressMask,
198 handleKeyPress, panel);
200 WMRealizeWidget(panel->win);
201 WMMapSubwidgets(panel->win);
203 return panel;
210 static void
211 inputBoxOnClick(WMWidget *self, void *clientData)
213 WMInputPanel *panel = clientData;
215 panel->done = 1;
216 if (self == panel->defBtn) {
217 panel->result = WAPRDefault;
218 } else if (self == panel->altBtn) {
219 panel->result = WAPRAlternate;
225 static void
226 handleKeyPress2(XEvent *event, void *clientData)
228 WMInputPanel *panel = (WMInputPanel*)clientData;
230 if (event->xkey.keycode == panel->retKey) {
231 WMPerformButtonClick(panel->defBtn);
236 char*
237 WMRunInputPanel(WMScreen *scrPtr, WMWindow *owner, char *title,
238 char *msg, char *defaultText,
239 char *okButton, char *cancelButton)
241 WMInputPanel *panel;
242 char *tmp;
244 panel = WMCreateInputPanel(scrPtr, owner, title, msg, defaultText,
245 okButton, cancelButton);
247 WMMapWidget(panel->win);
249 while (!panel->done || WMScreenPending(scrPtr)) {
250 XEvent event;
252 WMNextEvent(scrPtr->display, &event);
253 WMHandleEvent(&event);
257 if (panel->result == WAPRDefault)
258 tmp = WMGetTextFieldText(panel->text);
259 else
260 tmp = NULL;
262 WMDestroyInputPanel(panel);
264 return tmp;
268 void
269 WMDestroyInputPanel(WMInputPanel *panel)
271 WMRemoveNotificationObserver(panel);
272 WMUnmapWidget(panel->win);
273 WMDestroyWidget(panel->win);
274 free(panel);
279 static void
280 endedEditingObserver(void *observerData, WMNotification *notification)
282 WMInputPanel *panel = (WMInputPanel*)observerData;
284 if ((int)WMGetNotificationClientData(notification) == WMReturnTextMovement) {
285 WMPerformButtonClick(panel->defBtn);
290 WMInputPanel*
291 WMCreateInputPanel(WMScreen *scrPtr, WMWindow *owner, char *title, char *msg,
292 char *defaultText, char *okButton, char *cancelButton)
294 WMInputPanel *panel;
295 int x, dw=0, aw=0, w;
298 panel = wmalloc(sizeof(WMInputPanel));
299 memset(panel, 0, sizeof(WMInputPanel));
301 panel->retKey = XKeysymToKeycode(scrPtr->display, XK_Return);
303 if (owner)
304 panel->win = WMCreatePanelWithStyleForWindow(owner, "inputPanel",
305 WMTitledWindowMask);
306 else
307 panel->win = WMCreateWindowWithStyle(scrPtr, "inputPanel",
308 WMTitledWindowMask);
309 WMSetWindowTitle(panel->win, "");
311 WMResizeWidget(panel->win, 320, 160);
313 if (title) {
314 WMFont *largeFont;
316 largeFont = WMBoldSystemFontOfSize(scrPtr, 24);
318 panel->tLbl = WMCreateLabel(panel->win);
319 WMMoveWidget(panel->tLbl, 20, 16);
320 WMResizeWidget(panel->tLbl, 320 - 40, largeFont->height+4);
321 WMSetLabelText(panel->tLbl, title);
322 WMSetLabelTextAlignment(panel->tLbl, WALeft);
323 WMSetLabelFont(panel->tLbl, largeFont);
325 WMReleaseFont(largeFont);
329 if (msg) {
330 panel->mLbl = WMCreateLabel(panel->win);
331 WMMoveWidget(panel->mLbl, 20, 50);
332 WMResizeWidget(panel->mLbl, 320 - 40,scrPtr->normalFont->height*2);
333 WMSetLabelText(panel->mLbl, msg);
334 WMSetLabelTextAlignment(panel->mLbl, WALeft);
337 panel->text = WMCreateTextField(panel->win);
338 WMMoveWidget(panel->text, 20, 85);
339 WMResizeWidget(panel->text, 320 - 40, WMWidgetHeight(panel->text));
340 WMSetTextFieldText(panel->text, defaultText);
342 WMAddNotificationObserver(endedEditingObserver, panel,
343 WMTextDidEndEditingNotification, panel->text);
345 /* create buttons */
346 if (cancelButton)
347 aw = WMWidthOfString(scrPtr->normalFont, cancelButton,
348 strlen(cancelButton));
350 if (okButton)
351 dw = WMWidthOfString(scrPtr->normalFont, okButton,
352 strlen(okButton));
354 w = dw + (scrPtr->buttonArrow ? scrPtr->buttonArrow->width : 0);
355 if (aw > w)
356 w = aw;
358 w += 30;
359 x = 310;
361 if (okButton) {
362 x -= w + 10;
364 panel->defBtn = WMCreateCustomButton(panel->win, WBBPushInMask
365 |WBBPushChangeMask
366 |WBBPushLightMask);
367 WMSetButtonAction(panel->defBtn, inputBoxOnClick, panel);
368 WMMoveWidget(panel->defBtn, x, 124);
369 WMResizeWidget(panel->defBtn, w, 24);
370 WMSetButtonText(panel->defBtn, okButton);
371 WMSetButtonImage(panel->defBtn, scrPtr->buttonArrow);
372 WMSetButtonAltImage(panel->defBtn, scrPtr->pushedButtonArrow);
373 WMSetButtonImagePosition(panel->defBtn, WIPRight);
375 if (cancelButton) {
376 x -= w + 10;
378 panel->altBtn = WMCreateCommandButton(panel->win);
379 WMSetButtonAction(panel->altBtn, inputBoxOnClick, panel);
380 WMMoveWidget(panel->altBtn, x, 124);
381 WMResizeWidget(panel->altBtn, w, 24);
382 WMSetButtonText(panel->altBtn, cancelButton);
385 panel->done = 0;
387 WMCreateEventHandler(W_VIEW(panel->win), KeyPressMask,
388 handleKeyPress2, panel);
390 WMRealizeWidget(panel->win);
391 WMMapSubwidgets(panel->win);
393 WMSetFocusToWidget(panel->text);
395 return panel;