- Fixed a bug in popup button code.
[wmaker-crm.git] / WINGs / wtest.c
blob375281409c8c4c5000c96d9b6922be6375cf5a85
1 /*
2 * WINGs test application
3 */
5 #include "WINGs.h"
7 #include <stdio.h>
12 * You need to define this function to link any program to WINGs.
13 * This will be called when the application will be terminated because
14 * on a fatal error.
16 void
17 wAbort()
19 exit(1);
25 Display *dpy;
27 int windowCount = 0;
29 void
30 closeAction(WMWidget *self, void *data)
32 WMDestroyWidget(self);
33 windowCount--;
34 if (windowCount < 1)
35 exit(0);
39 void
40 testOpenFilePanel(WMScreen *scr)
42 WMOpenPanel *panel;
44 windowCount++;
46 /* get the shared Open File panel */
47 panel = WMGetOpenPanel(scr);
49 WMRunModalFilePanelForDirectory(panel, NULL, "/usr/local", NULL, NULL);
51 /* free the panel to save some memory. Not needed otherwise. */
52 WMFreeFilePanel(WMGetOpenPanel(scr));
56 void
57 testFontPanel(WMScreen *scr)
59 WMFontPanel *panel;
61 windowCount++;
63 panel = WMGetFontPanel(scr);
65 WMShowFontPanel(panel);
67 /* WMFreeFontPanel(panel);*/
72 void
73 testList(WMScreen *scr)
75 WMWindow *win;
76 WMList *list;
77 char text[100];
78 int i;
80 windowCount++;
82 win = WMCreateWindow(scr, "testList");
83 WMSetWindowTitle(win, "List");
84 WMSetWindowCloseAction(win, closeAction, NULL);
86 list = WMCreateList(win);
87 for (i=0; i<50; i++) {
88 sprintf(text, "Item %i", i);
89 WMAddListItem(list, text);
91 WMRealizeWidget(win);
92 WMMapSubwidgets(win);
93 WMMapWidget(win);
99 void
100 testGradientButtons(WMScreen *scr)
102 WMWindow *win;
103 WMButton *btn;
104 WMPixmap *pix1, *pix2;
105 RImage *back;
106 RColor light, dark;
107 WMColor *color, *altColor;
109 windowCount++;
111 /* creates the top-level window */
112 win = WMCreateWindow(scr, "testGradientButtons");
113 WMSetWindowTitle(win, "Gradiented Button Demo");
114 WMResizeWidget(win, 300, 200);
116 light.red = 0x90;
117 light.green = 0x85;
118 light.blue = 0x90;
119 dark.red = 0x35;
120 dark.green = 0x30;
121 dark.blue = 0x35;
123 color = WMCreateRGBColor(scr, 0x5900, 0x5100, 0x5900, True);
124 WMSetWidgetBackgroundColor(win, color);
125 WMReleaseColor(color);
127 back = RRenderGradient(60, 24, &dark, &light, RGRD_DIAGONAL);
128 RBevelImage(back, RBEV_RAISED2);
129 pix1 = WMCreatePixmapFromRImage(scr, back, 0);
130 RDestroyImage(back);
132 back = RRenderGradient(60, 24, &dark, &light, RGRD_DIAGONAL);
133 RBevelImage(back, RBEV_SUNKEN);
134 pix2 = WMCreatePixmapFromRImage(scr, back, 0);
135 RDestroyImage(back);
137 color = WMWhiteColor(scr);
138 altColor = WMCreateNamedColor(scr, "red", True);
140 btn = WMCreateButton(win, WBTMomentaryChange);
141 WMResizeWidget(btn, 60, 24);
142 WMMoveWidget(btn, 20, 100);
143 WMSetButtonBordered(btn, False);
144 WMSetButtonImagePosition(btn, WIPOverlaps);
145 WMSetButtonImage(btn, pix1);
146 WMSetButtonAltImage(btn, pix2);
147 WMSetButtonText(btn, "Cool");
148 WMSetButtonTextColor(btn, color);
149 WMSetButtonAltTextColor(btn, altColor);
151 WMSetBalloonTextForView("This is a cool button", WMWidgetView(btn));
153 btn = WMCreateButton(win, WBTMomentaryChange);
154 WMResizeWidget(btn, 60, 24);
155 WMMoveWidget(btn, 90, 100);
156 WMSetButtonBordered(btn, False);
157 WMSetButtonImagePosition(btn, WIPOverlaps);
158 WMSetButtonImage(btn, pix1);
159 WMSetButtonAltImage(btn, pix2);
160 WMSetButtonText(btn, "Button");
161 WMSetButtonTextColor(btn, color);
163 WMSetBalloonTextForView("Este é outro balão.", WMWidgetView(btn));
165 WMReleaseColor(color);
166 color = WMCreateNamedColor(scr, "orange", True);
168 btn = WMCreateButton(win, WBTMomentaryChange);
169 WMResizeWidget(btn, 60, 24);
170 WMMoveWidget(btn, 160, 100);
171 WMSetButtonBordered(btn, False);
172 WMSetButtonImagePosition(btn, WIPOverlaps);
173 WMSetButtonImage(btn, pix1);
174 WMSetButtonAltImage(btn, pix2);
175 WMSetButtonText(btn, "Test");
176 WMSetButtonTextColor(btn, color);
178 WMSetBalloonTextForView("This is yet another button.\nBut the balloon has 3 lines.\nYay!",
179 WMWidgetView(btn));
181 WMReleaseColor(color);
182 WMReleaseColor(altColor);
184 WMRealizeWidget(win);
185 WMMapSubwidgets(win);
186 WMMapWidget(win);
190 void
191 testScrollView(WMScreen *scr)
193 WMWindow *win;
194 WMScrollView *sview;
195 WMFrame *f;
196 WMLabel *l;
197 char buffer[128];
198 int i;
200 windowCount++;
202 /* creates the top-level window */
203 win = WMCreateWindow(scr, "testScroll");
204 WMSetWindowTitle(win, "Scrollable View");
206 WMSetWindowCloseAction(win, closeAction, NULL);
208 /* set the window size */
209 WMResizeWidget(win, 300, 300);
211 /* creates a scrollable view inside the top-level window */
212 sview = WMCreateScrollView(win);
213 WMResizeWidget(sview, 200, 200);
214 WMMoveWidget(sview, 30, 30);
215 WMSetScrollViewRelief(sview, WRSunken);
216 WMSetScrollViewHasVerticalScroller(sview, True);
217 WMSetScrollViewHasHorizontalScroller(sview, True);
219 /* create a frame with a bunch of labels */
220 f = WMCreateFrame(win);
221 WMResizeWidget(f, 400, 400);
222 WMSetFrameRelief(f, WRFlat);
224 for (i=0; i<20; i++) {
225 l = WMCreateLabel(f);
226 WMResizeWidget(l, 50, 18);
227 WMMoveWidget(l, 10, 20*i);
228 sprintf(buffer, "Label %i", i);
229 WMSetLabelText(l, buffer);
230 WMSetLabelRelief(l, WRSimple);
232 WMMapSubwidgets(f);
233 WMMapWidget(f);
235 WMSetScrollViewContentView(sview, WMWidgetView(f));
237 /* make the windows of the widgets be actually created */
238 WMRealizeWidget(win);
240 /* Map all child widgets of the top-level be mapped.
241 * You must call this for each container widget (like frames),
242 * even if they are childs of the top-level window.
244 WMMapSubwidgets(win);
246 /* map the top-level window */
247 WMMapWidget(win);
251 void
252 testColorWell(WMScreen *scr)
254 WMWindow *win;
255 WMColorWell *well1, *well2;
257 windowCount++;
259 win = WMCreateWindow(scr, "testColor");
260 WMResizeWidget(win, 300, 300);
262 WMSetWindowCloseAction(win, closeAction, NULL);
264 well1 = WMCreateColorWell(win);
265 WMResizeWidget(well1, 60, 40);
266 WMMoveWidget(well1, 100, 100);
267 WMSetColorWellColor(well1, WMCreateRGBColor(scr, 0x8888, 0, 0x1111, True));
268 well2 = WMCreateColorWell(win);
269 WMResizeWidget(well2, 60, 40);
270 WMMoveWidget(well2, 200, 100);
271 WMSetColorWellColor(well2, WMCreateRGBColor(scr, 0, 0, 0x8888, True));
273 WMRealizeWidget(win);
274 WMMapSubwidgets(win);
275 WMMapWidget(win);
278 void
279 sliderCallback(WMWidget *w, void *data)
281 printf("SLIEDER == %i\n", WMGetSliderValue(w));
285 void
286 testSlider(WMScreen *scr)
288 WMWindow *win;
289 WMSlider *s;
291 windowCount++;
293 win = WMCreateWindow(scr, "testSlider");
294 WMResizeWidget(win, 300, 300);
295 WMSetWindowTitle(win, "Sliders");
297 WMSetWindowCloseAction(win, closeAction, NULL);
299 s = WMCreateSlider(win);
300 WMResizeWidget(s, 16, 100);
301 WMMoveWidget(s, 100, 100);
302 WMSetSliderKnobThickness(s, 8);
303 WMSetSliderContinuous(s, False);
304 WMSetSliderAction(s, sliderCallback, s);
306 s = WMCreateSlider(win);
307 WMResizeWidget(s, 100, 16);
308 WMMoveWidget(s, 100, 10);
309 WMSetSliderKnobThickness(s, 8);
311 WMRealizeWidget(win);
312 WMMapSubwidgets(win);
313 WMMapWidget(win);
317 void
318 testTextField(WMScreen *scr)
320 WMWindow *win;
321 WMTextField *field, *field2;
323 windowCount++;
325 win = WMCreateWindow(scr, "testText");
326 WMResizeWidget(win, 400, 300);
328 WMSetWindowCloseAction(win, closeAction, NULL);
330 field = WMCreateTextField(win);
331 WMResizeWidget(field, 200, 20);
332 WMMoveWidget(field, 20, 20);
334 field2 = WMCreateTextField(win);
335 WMResizeWidget(field2, 200, 20);
336 WMMoveWidget(field2, 20, 50);
337 WMSetTextFieldAlignment(field2, WARight);
339 WMRealizeWidget(win);
340 WMMapSubwidgets(win);
341 WMMapWidget(win);
346 void
347 testProgressIndicator(WMScreen *scr)
349 WMWindow *win;
350 WMProgressIndicator *pPtr;
352 windowCount++;
354 win = WMCreateWindow(scr, "testProgressIndicator");
355 WMResizeWidget(win, 292, 32);
357 WMSetWindowCloseAction(win, closeAction, NULL);
359 pPtr = WMCreateProgressIndicator(win);
360 WMMoveWidget(pPtr, 8, 8);
361 WMSetProgressIndicatorValue(pPtr, 75);
363 WMRealizeWidget(win);
364 WMMapSubwidgets(win);
365 WMMapWidget(win);
370 void
371 testPullDown(WMScreen *scr)
373 WMWindow *win;
374 WMPopUpButton *pop, *pop2;
376 windowCount++;
378 win = WMCreateWindow(scr, "pullDown");
379 WMResizeWidget(win, 400, 300);
381 WMSetWindowCloseAction(win, closeAction, NULL);
383 pop = WMCreatePopUpButton(win);
384 WMResizeWidget(pop, 100, 20);
385 WMMoveWidget(pop, 50, 60);
386 WMSetPopUpButtonPullsDown(pop, True);
387 WMSetPopUpButtonText(pop, "Commands");
388 WMAddPopUpButtonItem(pop, "Add");
389 WMAddPopUpButtonItem(pop, "Remove");
390 WMAddPopUpButtonItem(pop, "Check");
391 WMAddPopUpButtonItem(pop, "Eat");
393 pop2 = WMCreatePopUpButton(win);
394 WMResizeWidget(pop2, 100, 20);
395 WMMoveWidget(pop2, 200, 60);
396 WMSetPopUpButtonText(pop2, "Select");
397 WMAddPopUpButtonItem(pop2, "Apples");
398 WMAddPopUpButtonItem(pop2, "Bananas");
399 WMAddPopUpButtonItem(pop2, "Strawberries");
400 WMAddPopUpButtonItem(pop2, "Blueberries");
402 WMRealizeWidget(win);
403 WMMapSubwidgets(win);
404 WMMapWidget(win);
409 void
410 testTabView(WMScreen *scr)
412 WMWindow *win;
413 WMTabView *tabv;
414 WMTabViewItem *tab;
415 WMFrame *frame;
416 WMLabel *label;
418 windowCount++;
420 win = WMCreateWindow(scr, "testTabs");
421 WMResizeWidget(win, 400, 300);
423 WMSetWindowCloseAction(win, closeAction, NULL);
425 tabv = WMCreateTabView(win);
426 WMMoveWidget(tabv, 50, 50);
427 WMResizeWidget(tabv, 300, 200);
429 frame = WMCreateFrame(win);
430 WMSetFrameRelief(frame, WRFlat);
431 label = WMCreateLabel(frame);
432 WMResizeWidget(label, 100, 100);
433 WMSetLabelText(label, "Label 1");
434 WMMapWidget(label);
437 tab = WMCreateTabViewItemWithIdentifier(0);
438 WMSetTabViewItemView(tab, WMWidgetView(frame));
439 WMAddItemInTabView(tabv, tab);
440 WMSetTabViewItemLabel(tab, "Instances");
442 frame = WMCreateFrame(win);
443 WMSetFrameRelief(frame, WRFlat);
444 label = WMCreateLabel(frame);
445 WMResizeWidget(label, 40, 50);
446 WMSetLabelText(label, "Label 2");
447 WMMapWidget(label);
450 tab = WMCreateTabViewItemWithIdentifier(0);
451 WMSetTabViewItemView(tab, WMWidgetView(frame));
452 WMAddItemInTabView(tabv, tab);
453 WMSetTabViewItemLabel(tab, "Classes");
456 frame = WMCreateFrame(win);
457 WMSetFrameRelief(frame, WRFlat);
458 label = WMCreateLabel(frame);
459 WMResizeWidget(label, 100, 100);
460 WMMoveWidget(label, 60, 40);
461 WMSetLabelText(label, "Label 3");
462 WMMapWidget(label);
464 tab = WMCreateTabViewItemWithIdentifier(0);
465 WMSetTabViewItemView(tab, WMWidgetView(frame));
466 WMAddItemInTabView(tabv, tab);
467 WMSetTabViewItemLabel(tab, "Something");
470 WMRealizeWidget(win);
471 WMMapSubwidgets(win);
472 WMMapWidget(win);
476 void
477 splitViewConstrainProc(WMSplitView *sPtr, int indView,
478 int *minSize, int *maxSize)
480 switch (indView) {
481 case 0:
482 *minSize = 20;
483 break;
484 case 1:
485 *minSize = 40;
486 *maxSize = 80;
487 break;
488 case 2:
489 *maxSize = 60;
490 break;
491 default:
492 break;
497 static void
498 resizeSplitView(XEvent *event, void *data)
500 WMSplitView *sPtr = (WMSplitView*)data;
502 if (event->type == ConfigureNotify) {
503 int width = event->xconfigure.width - 10;
505 if (width < WMGetSplitViewDividerThickness(sPtr))
506 width = WMGetSplitViewDividerThickness(sPtr);
508 if (width != WMWidgetWidth(sPtr) ||
509 event->xconfigure.height != WMWidgetHeight(sPtr))
510 WMResizeWidget(sPtr, width, event->xconfigure.height - 55);
514 void
515 appendSubviewButtonAction(WMWidget *self, void *data)
517 WMSplitView *sPtr = (WMSplitView*)data;
518 char buf[64];
519 WMLabel *label = WMCreateLabel(sPtr);
521 sprintf(buf, "Subview %d", WMGetSplitViewSubviewsCount(sPtr) + 1);
522 WMSetLabelText(label, buf);
523 WMSetLabelRelief(label, WRSunken);
524 WMAddSplitViewSubview(sPtr, WMWidgetView(label));
525 WMRealizeWidget(label);
526 WMMapWidget(label);
529 void
530 removeSubviewButtonAction(WMWidget *self, void *data)
532 WMSplitView *sPtr = (WMSplitView*)data;
533 int count = WMGetSplitViewSubviewsCount(sPtr);
535 if (count > 2) {
536 WMView *view = WMGetSplitViewSubviewAt(sPtr, count-1);
537 WMDestroyWidget(WMWidgetOfView(view));
538 WMRemoveSplitViewSubviewAt(sPtr, count-1);
542 void
543 orientationButtonAction(WMWidget *self, void *data)
545 WMSplitView *sPtr = (WMSplitView*)data;
546 WMSetSplitViewVertical(sPtr, !WMGetSplitViewVertical(sPtr));
549 void
550 adjustSubviewsButtonAction(WMWidget *self, void *data)
552 WMAdjustSplitViewSubviews((WMSplitView*)data);
555 void
556 testSplitView(WMScreen *scr)
558 WMWindow *win;
559 WMSplitView *splitv1, *splitv2;
560 WMFrame *frame;
561 WMLabel *label;
562 WMButton *button;
564 windowCount++;
566 win = WMCreateWindow(scr, "testTabs");
567 WMResizeWidget(win, 300, 400);
568 WMSetWindowCloseAction(win, closeAction, NULL);
570 frame = WMCreateFrame(win);
571 WMSetFrameRelief(frame, WRSunken);
572 WMMoveWidget(frame, 5, 5);
573 WMResizeWidget(frame, 290, 40);
575 splitv1 = WMCreateSplitView(win);
576 WMMoveWidget(splitv1, 5, 50);
577 WMResizeWidget(splitv1, 290, 345);
578 WMSetSplitViewConstrainProc(splitv1, splitViewConstrainProc);
579 WMCreateEventHandler(WMWidgetView(win), StructureNotifyMask,
580 resizeSplitView, splitv1);
582 button = WMCreateCommandButton(frame);
583 WMSetButtonText(button, "+");
584 WMSetButtonAction(button, appendSubviewButtonAction, splitv1);
585 WMMoveWidget(button, 10, 8);
586 WMMapWidget(button);
588 button = WMCreateCommandButton(frame);
589 WMSetButtonText(button, "-");
590 WMSetButtonAction(button, removeSubviewButtonAction, splitv1);
591 WMMoveWidget(button, 80, 8);
592 WMMapWidget(button);
594 button = WMCreateCommandButton(frame);
595 WMSetButtonText(button, "=");
596 WMMoveWidget(button, 150, 8);
597 WMSetButtonAction(button, adjustSubviewsButtonAction, splitv1);
598 WMMapWidget(button);
600 button = WMCreateCommandButton(frame);
601 WMSetButtonText(button, "#");
602 WMMoveWidget(button, 220, 8);
603 WMSetButtonAction(button, orientationButtonAction, splitv1);
604 WMMapWidget(button);
606 label = WMCreateLabel(splitv1);
607 WMSetLabelText(label, "Subview 1");
608 WMSetLabelRelief(label, WRSunken);
609 WMMapWidget(label);
610 WMAddSplitViewSubview(splitv1, WMWidgetView(label));
612 splitv2 = WMCreateSplitView(splitv1);
613 WMResizeWidget(splitv2, 150, 150);
614 WMSetSplitViewVertical(splitv2, True);
616 label = WMCreateLabel(splitv2);
617 WMSetLabelText(label, "Subview 2.1");
618 WMSetLabelRelief(label, WRSunken);
619 WMMapWidget(label);
620 WMAddSplitViewSubview(splitv2, WMWidgetView(label));
622 label = WMCreateLabel(splitv2);
623 WMSetLabelText(label, "Subview 2.2");
624 WMSetLabelRelief(label, WRSunken);
625 WMMapWidget(label);
626 WMAddSplitViewSubview(splitv2, WMWidgetView(label));
628 label = WMCreateLabel(splitv2);
629 WMSetLabelText(label, "Subview 2.3");
630 WMSetLabelRelief(label, WRSunken);
631 WMMapWidget(label);
632 WMAddSplitViewSubview(splitv2, WMWidgetView(label));
634 WMMapWidget(splitv2);
635 WMAddSplitViewSubview(splitv1, WMWidgetView(splitv2));
637 WMRealizeWidget(win);
638 WMMapSubwidgets(win);
639 WMMapWidget(win);
642 #include "WUtil.h"
646 int main(int argc, char **argv)
648 WMScreen *scr;
649 WMPixmap *pixmap;
652 /* Initialize the application */
653 WMInitializeApplication("Test", &argc, argv);
656 * Open connection to the X display.
658 dpy = XOpenDisplay("");
660 if (!dpy) {
661 puts("could not open display");
662 exit(1);
665 /* This is used to disable buffering of X protocol requests.
666 * Do NOT use it unless when debugging. It will cause a major
667 * slowdown in your application
669 #ifdef DEBUG
670 XSynchronize(dpy, True);
671 #endif
673 * Create screen descriptor.
675 scr = WMCreateScreen(dpy, DefaultScreen(dpy));
678 * Loads the logo of the application.
680 pixmap = WMCreatePixmapFromFile(scr, "logo.xpm");
683 * Makes the logo be used in standard dialog panels.
685 WMSetApplicationIconImage(scr, pixmap); WMReleasePixmap(pixmap);
688 * Do some test stuff.
690 * Put the testSomething() function you want to test here.
694 testSplitView(scr);
695 testFontPanel(scr);
696 testGradientButtons(scr);
697 #if 0
699 testProgressIndicator(scr);
700 testColorWell(scr);
702 testTabView(scr);
703 testTextField(scr);
705 testOpenFilePanel(scr);
706 testList(scr);
707 testScrollView(scr);
710 testSlider(scr);
711 testPullDown(scr);
712 #endif
714 * The main event loop.
717 WMScreenMainLoop(scr);
719 return 0;