small update to list selection code.
[wmaker-crm.git] / WINGs / Tests / wtest.c
blobfa0c1aa2c3079cec67f4660827755343e5bb634c
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 WMUnmapWidget(self);
33 WMDestroyWidget(self);
34 windowCount--;
35 printf("window closed, window count = %d\n", windowCount);
36 if (windowCount < 1)
37 exit(0);
41 void
42 testOpenFilePanel(WMScreen *scr)
44 WMOpenPanel *panel;
46 /* windowCount++; */
48 /* get the shared Open File panel */
49 panel = WMGetOpenPanel(scr);
51 WMRunModalFilePanelForDirectory(panel, NULL, "/usr/local", NULL, NULL);
53 /* free the panel to save some memory. Not needed otherwise. */
54 WMFreeFilePanel(WMGetOpenPanel(scr));
58 void
59 testFontPanel(WMScreen *scr)
61 WMFontPanel *panel;
63 /*windowCount++;*/
65 panel = WMGetFontPanel(scr);
67 WMShowFontPanel(panel);
69 /* WMFreeFontPanel(panel);*/
74 void
75 testFrame(WMScreen *scr)
77 WMWindow *win;
78 WMFrame *frame;
79 int i;
80 static char* titles[] = {
81 "AboveTop",
82 "AtTop",
83 "BelowTop",
84 "AboveBottom",
85 "AtBottom",
86 "BelowBottom"
88 static WMTitlePosition pos[] = {
89 WTPAboveTop,
90 WTPAtTop,
91 WTPBelowTop,
92 WTPAboveBottom,
93 WTPAtBottom,
94 WTPBelowBottom
97 windowCount++;
99 win = WMCreateWindow(scr, "testFrame");
100 WMSetWindowTitle(win, "Frame");
101 WMSetWindowCloseAction(win, closeAction, NULL);
102 WMResizeWidget(win, 400, 300);
104 for (i = 0; i < 6; i++) {
105 frame = WMCreateFrame(win);
106 WMMoveWidget(frame, 8+(i%3)*130, 8+(i/3)*130);
107 WMResizeWidget(frame, 120, 120);
108 WMSetFrameTitle(frame, titles[i]);
109 WMSetFrameTitlePosition(frame, pos[i]);
112 WMRealizeWidget(win);
113 WMMapSubwidgets(win);
114 WMMapWidget(win);
118 void
119 testList(WMScreen *scr)
121 WMWindow *win;
122 WMList *list;
123 WMList *mlist;
124 WMLabel *label;
125 WMLabel *mlabel;
126 char text[100];
127 int i;
129 windowCount++;
131 win = WMCreateWindow(scr, "testList");
132 WMSetWindowTitle(win, "List");
133 WMSetWindowCloseAction(win, closeAction, NULL);
135 list = WMCreateList(win);
136 for (i=0; i<50; i++) {
137 sprintf(text, "Item %i", i);
138 WMAddListItem(list, text);
140 mlist = WMCreateList(win);
141 WMSetListAllowMultipleSelection(mlist, True);
142 WMMoveWidget(mlist, 220, 0);
143 for (i=0; i<50; i++) {
144 sprintf(text, "Item %i", i);
145 WMAddListItem(mlist, text);
147 WMRealizeWidget(win);
148 WMMapSubwidgets(win);
149 WMMapWidget(win);
153 void
154 testButton(WMScreen *scr)
156 WMWindow *win;
157 int i;
158 char *types[] = {
159 "MomentaryPush",
160 "PushOnPushOff",
161 "Toggle",
162 "Switch",
163 "Radio",
164 "MomentaryChange",
165 "OnOff",
166 "MomentaryLigh"
169 windowCount++;
171 win = WMCreateWindow(scr, "testButton");
172 WMResizeWidget(win, 300, 300);
173 WMSetWindowTitle(win, "Buttons");
175 WMSetWindowCloseAction(win, closeAction, NULL);
177 for (i = 1; i < 9; i++) {
178 WMButton *b;
179 b = WMCreateButton(win, i);
180 WMResizeWidget(b, 150, 24);
181 WMMoveWidget(b, 20, i*30);
182 WMSetButtonText(b, types[i-1]);
185 WMRealizeWidget(win);
186 WMMapSubwidgets(win);
187 WMMapWidget(win);
191 void
192 testGradientButtons(WMScreen *scr)
194 WMWindow *win;
195 WMButton *btn;
196 WMPixmap *pix1, *pix2;
197 RImage *back;
198 RColor light, dark;
199 WMColor *color, *altColor;
201 windowCount++;
203 /* creates the top-level window */
204 win = WMCreateWindow(scr, "testGradientButtons");
205 WMSetWindowTitle(win, "Gradiented Button Demo");
206 WMResizeWidget(win, 300, 200);
208 light.red = 0x90;
209 light.green = 0x85;
210 light.blue = 0x90;
211 dark.red = 0x35;
212 dark.green = 0x30;
213 dark.blue = 0x35;
215 color = WMCreateRGBColor(scr, 0x5900, 0x5100, 0x5900, True);
216 WMSetWidgetBackgroundColor(win, color);
217 WMReleaseColor(color);
219 back = RRenderGradient(60, 24, &dark, &light, RGRD_DIAGONAL);
220 RBevelImage(back, RBEV_RAISED2);
221 pix1 = WMCreatePixmapFromRImage(scr, back, 0);
222 RDestroyImage(back);
224 back = RRenderGradient(60, 24, &dark, &light, RGRD_DIAGONAL);
225 RBevelImage(back, RBEV_SUNKEN);
226 pix2 = WMCreatePixmapFromRImage(scr, back, 0);
227 RDestroyImage(back);
229 color = WMWhiteColor(scr);
230 altColor = WMCreateNamedColor(scr, "red", True);
232 btn = WMCreateButton(win, WBTMomentaryChange);
233 WMResizeWidget(btn, 60, 24);
234 WMMoveWidget(btn, 20, 100);
235 WMSetButtonBordered(btn, False);
236 WMSetButtonImagePosition(btn, WIPOverlaps);
237 WMSetButtonImage(btn, pix1);
238 WMSetButtonAltImage(btn, pix2);
239 WMSetButtonText(btn, "Cool");
240 WMSetButtonTextColor(btn, color);
241 WMSetButtonAltTextColor(btn, altColor);
243 WMSetBalloonTextForView("This is a cool button", WMWidgetView(btn));
245 btn = WMCreateButton(win, WBTMomentaryChange);
246 WMResizeWidget(btn, 60, 24);
247 WMMoveWidget(btn, 90, 100);
248 WMSetButtonBordered(btn, False);
249 WMSetButtonImagePosition(btn, WIPOverlaps);
250 WMSetButtonImage(btn, pix1);
251 WMSetButtonAltImage(btn, pix2);
252 WMSetButtonText(btn, "Button");
253 WMSetButtonTextColor(btn, color);
255 WMSetBalloonTextForView("Este é outro balão.", WMWidgetView(btn));
257 WMReleaseColor(color);
258 color = WMCreateNamedColor(scr, "orange", True);
260 btn = WMCreateButton(win, WBTMomentaryChange);
261 WMResizeWidget(btn, 60, 24);
262 WMMoveWidget(btn, 160, 100);
263 WMSetButtonBordered(btn, False);
264 WMSetButtonImagePosition(btn, WIPOverlaps);
265 WMSetButtonImage(btn, pix1);
266 WMSetButtonAltImage(btn, pix2);
267 WMSetButtonText(btn, "Test");
268 WMSetButtonTextColor(btn, color);
270 WMSetBalloonTextForView("This is yet another button.\nBut the balloon has 3 lines.\nYay!",
271 WMWidgetView(btn));
273 WMReleaseColor(color);
274 WMReleaseColor(altColor);
276 WMRealizeWidget(win);
277 WMMapSubwidgets(win);
278 WMMapWidget(win);
282 void
283 testScrollView(WMScreen *scr)
285 WMWindow *win;
286 WMScrollView *sview;
287 WMFrame *f;
288 WMLabel *l;
289 char buffer[128];
290 int i;
292 windowCount++;
294 /* creates the top-level window */
295 win = WMCreateWindow(scr, "testScroll");
296 WMSetWindowTitle(win, "Scrollable View");
298 WMSetWindowCloseAction(win, closeAction, NULL);
300 /* set the window size */
301 WMResizeWidget(win, 300, 300);
303 /* creates a scrollable view inside the top-level window */
304 sview = WMCreateScrollView(win);
305 WMResizeWidget(sview, 200, 200);
306 WMMoveWidget(sview, 30, 30);
307 WMSetScrollViewRelief(sview, WRSunken);
308 WMSetScrollViewHasVerticalScroller(sview, True);
309 WMSetScrollViewHasHorizontalScroller(sview, True);
311 /* create a frame with a bunch of labels */
312 f = WMCreateFrame(win);
313 WMResizeWidget(f, 400, 400);
314 WMSetFrameRelief(f, WRFlat);
316 for (i=0; i<20; i++) {
317 l = WMCreateLabel(f);
318 WMResizeWidget(l, 50, 18);
319 WMMoveWidget(l, 10, 20*i);
320 sprintf(buffer, "Label %i", i);
321 WMSetLabelText(l, buffer);
322 WMSetLabelRelief(l, WRSimple);
324 WMMapSubwidgets(f);
325 WMMapWidget(f);
327 WMSetScrollViewContentView(sview, WMWidgetView(f));
329 /* make the windows of the widgets be actually created */
330 WMRealizeWidget(win);
332 /* Map all child widgets of the top-level be mapped.
333 * You must call this for each container widget (like frames),
334 * even if they are childs of the top-level window.
336 WMMapSubwidgets(win);
338 /* map the top-level window */
339 WMMapWidget(win);
343 void
344 testColorWell(WMScreen *scr)
346 WMWindow *win;
347 WMColorWell *well1, *well2;
349 windowCount++;
351 win = WMCreateWindow(scr, "testColor");
352 WMResizeWidget(win, 300, 300);
353 WMSetWindowCloseAction(win, closeAction, NULL);
355 well1 = WMCreateColorWell(win);
356 WMResizeWidget(well1, 60, 40);
357 WMMoveWidget(well1, 100, 100);
358 WMSetColorWellColor(well1, WMCreateRGBColor(scr, 0x8888, 0, 0x1111, True));
359 well2 = WMCreateColorWell(win);
360 WMResizeWidget(well2, 60, 40);
361 WMMoveWidget(well2, 200, 100);
362 WMSetColorWellColor(well2, WMCreateRGBColor(scr, 0, 0, 0x8888, True));
364 WMRealizeWidget(win);
365 WMMapSubwidgets(win);
366 WMMapWidget(win);
369 void
370 sliderCallback(WMWidget *w, void *data)
372 printf("SLIDER == %i\n", WMGetSliderValue(w));
376 void
377 testSlider(WMScreen *scr)
379 WMWindow *win;
380 WMSlider *s;
382 windowCount++;
384 win = WMCreateWindow(scr, "testSlider");
385 WMResizeWidget(win, 300, 300);
386 WMSetWindowTitle(win, "Sliders");
388 WMSetWindowCloseAction(win, closeAction, NULL);
390 s = WMCreateSlider(win);
391 WMResizeWidget(s, 16, 100);
392 WMMoveWidget(s, 100, 100);
393 WMSetSliderKnobThickness(s, 8);
394 WMSetSliderContinuous(s, False);
395 WMSetSliderAction(s, sliderCallback, s);
397 s = WMCreateSlider(win);
398 WMResizeWidget(s, 100, 16);
399 WMMoveWidget(s, 100, 10);
400 WMSetSliderKnobThickness(s, 8);
402 WMRealizeWidget(win);
403 WMMapSubwidgets(win);
404 WMMapWidget(win);
407 void
408 testTextField(WMScreen *scr)
410 WMWindow *win;
411 WMTextField *field, *field2;
413 windowCount++;
415 win = WMCreateWindow(scr, "testTextField");
416 WMResizeWidget(win, 400, 300);
418 WMSetWindowCloseAction(win, closeAction, NULL);
420 field = WMCreateTextField(win);
421 WMResizeWidget(field, 200, 20);
422 WMMoveWidget(field, 20, 20);
424 field2 = WMCreateTextField(win);
425 WMResizeWidget(field2, 200, 20);
426 WMMoveWidget(field2, 20, 50);
427 WMSetTextFieldAlignment(field2, WARight);
429 WMRealizeWidget(win);
430 WMMapSubwidgets(win);
431 WMMapWidget(win);
435 void
436 testText(WMScreen *scr)
438 WMWindow *win;
439 WMText *text;
440 FILE *file = fopen("../README", "r");
442 windowCount++;
444 win = WMCreateWindow(scr, "testText");
445 WMResizeWidget(win, 500, 300);
447 WMSetWindowCloseAction(win, closeAction, NULL);
449 text = WMCreateText(win);
450 WMResizeWidget(text, 480, 280);
451 WMMoveWidget(text, 10, 10);
452 WMSetTextHasVerticalScroller(text, True);
453 WMSetTextEditable(text, False);
455 if(file) {
456 char buf[1024];
458 WMFreezeText(text);
459 while(fgets(buf, 1023, file))
460 WMAppendTextStream(text, buf);
462 fclose(file);
463 WMThawText(text);
464 WMRefreshText(text, 0, 0);
465 } else {
466 WMAppendTextStream(text, "<HTML><i>Where's</i> the <b>README</b>?");
469 WMRealizeWidget(win);
470 WMMapSubwidgets(win);
471 WMMapWidget(win);
476 void
477 testProgressIndicator(WMScreen *scr)
479 WMWindow *win;
480 WMProgressIndicator *pPtr;
482 windowCount++;
484 win = WMCreateWindow(scr, "testProgressIndicator");
485 WMResizeWidget(win, 292, 32);
487 WMSetWindowCloseAction(win, closeAction, NULL);
489 pPtr = WMCreateProgressIndicator(win);
490 WMMoveWidget(pPtr, 8, 8);
491 WMSetProgressIndicatorValue(pPtr, 75);
493 WMRealizeWidget(win);
494 WMMapSubwidgets(win);
495 WMMapWidget(win);
500 void
501 testPullDown(WMScreen *scr)
503 WMWindow *win;
504 WMPopUpButton *pop, *pop2;
506 windowCount++;
508 win = WMCreateWindow(scr, "pullDown");
509 WMResizeWidget(win, 400, 300);
511 WMSetWindowCloseAction(win, closeAction, NULL);
513 pop = WMCreatePopUpButton(win);
514 WMResizeWidget(pop, 100, 20);
515 WMMoveWidget(pop, 50, 60);
516 WMSetPopUpButtonPullsDown(pop, True);
517 WMSetPopUpButtonText(pop, "Commands");
518 WMAddPopUpButtonItem(pop, "Add");
519 WMAddPopUpButtonItem(pop, "Remove");
520 WMAddPopUpButtonItem(pop, "Check");
521 WMAddPopUpButtonItem(pop, "Eat");
523 pop2 = WMCreatePopUpButton(win);
524 WMResizeWidget(pop2, 100, 20);
525 WMMoveWidget(pop2, 200, 60);
526 WMSetPopUpButtonText(pop2, "Select");
527 WMAddPopUpButtonItem(pop2, "Apples");
528 WMAddPopUpButtonItem(pop2, "Bananas");
529 WMAddPopUpButtonItem(pop2, "Strawberries");
530 WMAddPopUpButtonItem(pop2, "Blueberries");
532 WMRealizeWidget(win);
533 WMMapSubwidgets(win);
534 WMMapWidget(win);
539 void
540 testTabView(WMScreen *scr)
542 WMWindow *win;
543 WMTabView *tabv;
544 WMTabViewItem *tab;
545 WMFrame *frame;
546 WMLabel *label;
548 windowCount++;
550 win = WMCreateWindow(scr, "testTabs");
551 WMResizeWidget(win, 400, 300);
553 WMSetWindowCloseAction(win, closeAction, NULL);
555 tabv = WMCreateTabView(win);
556 WMMoveWidget(tabv, 50, 50);
557 WMResizeWidget(tabv, 300, 200);
559 frame = WMCreateFrame(win);
560 WMSetFrameRelief(frame, WRFlat);
561 label = WMCreateLabel(frame);
562 WMResizeWidget(label, 100, 100);
563 WMSetLabelText(label, "Label 1");
564 WMMapWidget(label);
567 tab = WMCreateTabViewItemWithIdentifier(0);
568 WMSetTabViewItemView(tab, WMWidgetView(frame));
569 WMAddItemInTabView(tabv, tab);
570 WMSetTabViewItemLabel(tab, "Instances");
572 frame = WMCreateFrame(win);
573 WMSetFrameRelief(frame, WRFlat);
574 label = WMCreateLabel(frame);
575 WMResizeWidget(label, 40, 50);
576 WMSetLabelText(label, "Label 2");
577 WMMapWidget(label);
580 tab = WMCreateTabViewItemWithIdentifier(0);
581 WMSetTabViewItemView(tab, WMWidgetView(frame));
582 WMAddItemInTabView(tabv, tab);
583 WMSetTabViewItemLabel(tab, "Classes");
586 frame = WMCreateFrame(win);
587 WMSetFrameRelief(frame, WRFlat);
588 label = WMCreateLabel(frame);
589 WMResizeWidget(label, 100, 100);
590 WMMoveWidget(label, 60, 40);
591 WMSetLabelText(label, "Label 3");
592 WMMapWidget(label);
594 tab = WMCreateTabViewItemWithIdentifier(0);
595 WMSetTabViewItemView(tab, WMWidgetView(frame));
596 WMAddItemInTabView(tabv, tab);
597 WMSetTabViewItemLabel(tab, "Something");
600 frame = WMCreateFrame(win);
601 WMSetFrameRelief(frame, WRFlat);
602 label = WMCreateLabel(frame);
603 WMResizeWidget(label, 100, 100);
604 WMMoveWidget(label, 160, 40);
605 WMSetLabelText(label, "Label 4");
606 WMMapWidget(label);
608 tab = WMCreateTabViewItemWithIdentifier(0);
609 WMSetTabViewItemView(tab, WMWidgetView(frame));
610 WMAddItemInTabView(tabv, tab);
611 WMSetTabViewItemLabel(tab, "Bla!");
615 frame = WMCreateFrame(win);
616 WMSetFrameRelief(frame, WRFlat);
617 label = WMCreateLabel(frame);
618 WMResizeWidget(label, 100, 100);
619 WMMoveWidget(label, 160, 40);
620 WMSetLabelText(label, "Label fjweqklrj qwl");
621 WMMapWidget(label);
622 tab = WMCreateTabViewItemWithIdentifier(0);
623 WMSetTabViewItemView(tab, WMWidgetView(frame));
624 WMAddItemInTabView(tabv, tab);
625 WMSetTabViewItemLabel(tab, "Weee!");
628 WMRealizeWidget(win);
629 WMMapSubwidgets(win);
630 WMMapWidget(win);
634 void
635 splitViewConstrainProc(WMSplitView *sPtr, int indView,
636 int *minSize, int *maxSize)
638 switch (indView) {
639 case 0:
640 *minSize = 20;
641 break;
642 case 1:
643 *minSize = 40;
644 *maxSize = 80;
645 break;
646 case 2:
647 *maxSize = 60;
648 break;
649 default:
650 break;
655 static void
656 resizeSplitView(XEvent *event, void *data)
658 WMSplitView *sPtr = (WMSplitView*)data;
660 if (event->type == ConfigureNotify) {
661 int width = event->xconfigure.width - 10;
663 if (width < WMGetSplitViewDividerThickness(sPtr))
664 width = WMGetSplitViewDividerThickness(sPtr);
666 if (width != WMWidgetWidth(sPtr) ||
667 event->xconfigure.height != WMWidgetHeight(sPtr))
668 WMResizeWidget(sPtr, width, event->xconfigure.height - 55);
672 void
673 appendSubviewButtonAction(WMWidget *self, void *data)
675 WMSplitView *sPtr = (WMSplitView*)data;
676 char buf[64];
677 WMLabel *label = WMCreateLabel(sPtr);
679 sprintf(buf, "Subview %d", WMGetSplitViewSubviewsCount(sPtr) + 1);
680 WMSetLabelText(label, buf);
681 WMSetLabelRelief(label, WRSunken);
682 WMAddSplitViewSubview(sPtr, WMWidgetView(label));
683 WMRealizeWidget(label);
684 WMMapWidget(label);
687 void
688 removeSubviewButtonAction(WMWidget *self, void *data)
690 WMSplitView *sPtr = (WMSplitView*)data;
691 int count = WMGetSplitViewSubviewsCount(sPtr);
693 if (count > 2) {
694 WMView *view = WMGetSplitViewSubviewAt(sPtr, count-1);
695 WMDestroyWidget(WMWidgetOfView(view));
696 WMRemoveSplitViewSubviewAt(sPtr, count-1);
700 void
701 orientationButtonAction(WMWidget *self, void *data)
703 WMSplitView *sPtr = (WMSplitView*)data;
704 WMSetSplitViewVertical(sPtr, !WMGetSplitViewVertical(sPtr));
707 void
708 adjustSubviewsButtonAction(WMWidget *self, void *data)
710 WMAdjustSplitViewSubviews((WMSplitView*)data);
713 void
714 testSplitView(WMScreen *scr)
716 WMWindow *win;
717 WMSplitView *splitv1, *splitv2;
718 WMFrame *frame;
719 WMLabel *label;
720 WMButton *button;
722 windowCount++;
724 win = WMCreateWindow(scr, "testTabs");
725 WMResizeWidget(win, 300, 400);
726 WMSetWindowCloseAction(win, closeAction, NULL);
728 frame = WMCreateFrame(win);
729 WMSetFrameRelief(frame, WRSunken);
730 WMMoveWidget(frame, 5, 5);
731 WMResizeWidget(frame, 290, 40);
733 splitv1 = WMCreateSplitView(win);
734 WMMoveWidget(splitv1, 5, 50);
735 WMResizeWidget(splitv1, 290, 345);
736 WMSetSplitViewConstrainProc(splitv1, splitViewConstrainProc);
737 WMCreateEventHandler(WMWidgetView(win), StructureNotifyMask,
738 resizeSplitView, splitv1);
740 button = WMCreateCommandButton(frame);
741 WMSetButtonText(button, "+");
742 WMSetButtonAction(button, appendSubviewButtonAction, splitv1);
743 WMMoveWidget(button, 10, 8);
744 WMMapWidget(button);
746 button = WMCreateCommandButton(frame);
747 WMSetButtonText(button, "-");
748 WMSetButtonAction(button, removeSubviewButtonAction, splitv1);
749 WMMoveWidget(button, 80, 8);
750 WMMapWidget(button);
752 button = WMCreateCommandButton(frame);
753 WMSetButtonText(button, "=");
754 WMMoveWidget(button, 150, 8);
755 WMSetButtonAction(button, adjustSubviewsButtonAction, splitv1);
756 WMMapWidget(button);
758 button = WMCreateCommandButton(frame);
759 WMSetButtonText(button, "#");
760 WMMoveWidget(button, 220, 8);
761 WMSetButtonAction(button, orientationButtonAction, splitv1);
762 WMMapWidget(button);
764 label = WMCreateLabel(splitv1);
765 WMSetLabelText(label, "Subview 1");
766 WMSetLabelRelief(label, WRSunken);
767 WMMapWidget(label);
768 WMAddSplitViewSubview(splitv1, WMWidgetView(label));
770 splitv2 = WMCreateSplitView(splitv1);
771 WMResizeWidget(splitv2, 150, 150);
772 WMSetSplitViewVertical(splitv2, True);
774 label = WMCreateLabel(splitv2);
775 WMSetLabelText(label, "Subview 2.1");
776 WMSetLabelRelief(label, WRSunken);
777 WMMapWidget(label);
778 WMAddSplitViewSubview(splitv2, WMWidgetView(label));
780 label = WMCreateLabel(splitv2);
781 WMSetLabelText(label, "Subview 2.2");
782 WMSetLabelRelief(label, WRSunken);
783 WMMapWidget(label);
784 WMAddSplitViewSubview(splitv2, WMWidgetView(label));
786 label = WMCreateLabel(splitv2);
787 WMSetLabelText(label, "Subview 2.3");
788 WMSetLabelRelief(label, WRSunken);
789 WMMapWidget(label);
790 WMAddSplitViewSubview(splitv2, WMWidgetView(label));
792 WMMapWidget(splitv2);
793 WMAddSplitViewSubview(splitv1, WMWidgetView(splitv2));
795 WMRealizeWidget(win);
796 WMMapSubwidgets(win);
797 WMMapWidget(win);
801 /*******************************************************************/
803 #include <sys/types.h>
804 #include <dirent.h>
805 #include <string.h>
808 typedef struct {
809 int x, y;
810 Bool mouseDown;
811 char *filename;
812 } DNDStuff;
814 WMPixmap*
815 getImage(WMScreen *scr, char *file)
817 char buffer[1000];
818 WMPixmap *pix;
820 sprintf(buffer, "../../WindowMaker/Icons/%s", file);
821 pix = WMCreatePixmapFromFile(scr, buffer);
823 return pix;
829 static void
830 iconMouseStuff(XEvent *event, void *cdata)
832 WMLabel *label = (WMLabel*)cdata;
833 DNDStuff *stuff = WMGetHangedData(label);
834 WMPoint where;
836 switch (event->type) {
837 case ButtonPress:
838 stuff->x = event->xbutton.x;
839 stuff->y = event->xbutton.y;
840 stuff->mouseDown = True;
841 break;
842 case ButtonRelease:
843 stuff->mouseDown = False;
844 break;
845 case MotionNotify:
846 if (!stuff->mouseDown)
847 break;
849 if (abs(stuff->x - event->xmotion.x)>4
850 || abs(stuff->y - event->xmotion.y)>4) {
852 where = WMGetViewScreenPosition(WMWidgetView(label));
854 WMDragImageFromView(WMWidgetView(label),
855 WMGetLabelImage(label),
856 NULL, /* XXX */
857 where,
858 wmksize(stuff->x, stuff->y),
859 event, True);
861 break;
866 static void
867 endedDragImage(WMView *self, WMPixmap *image, WMPoint point, Bool deposited)
869 DNDStuff *stuff = WMGetHangedData(WMWidgetOfView(self));
871 if (deposited) {
872 WMDestroyWidget(WMWidgetOfView(self));
875 stuff->mouseDown = False;
879 static WMData*
880 fetchDragData(WMView *self, char *type)
882 DNDStuff *stuff = WMGetHangedData(WMWidgetOfView(self));
884 return WMCreateDataWithBytes(stuff->filename, strlen(stuff->filename)+1);
888 WMDragSourceProcs dragSourceProcs = {
889 NULL,
890 NULL,
891 endedDragImage,
892 fetchDragData
896 /************************/
899 unsigned
900 draggingEntered(WMView *self, WMDraggingInfo *info)
902 return WDOperationCopy;
906 unsigned
907 draggingUpdated(WMView *self, WMDraggingInfo *info)
909 return WDOperationCopy;
913 void (*draggingExited)(WMView *self, WMDraggingInfo *info);
915 char*
916 prepareForDragOperation(WMView *self, WMDraggingInfo *info)
918 return "application/X-WINGs-Bla";
922 WMLabel* makeDraggableLabel(WMWidget *w, char *file, int x, int y);
924 Bool
925 performDragOperation(WMView *self, WMDraggingInfo *info, WMData *data)
927 char *file = (char*)WMDataBytes(data);
928 WMPoint pos;
930 pos = WMGetDraggingInfoImageLocation(info);
932 if (file!=NULL) {
933 WMLabel *label;
934 WMPoint pos2 = WMGetViewScreenPosition(self);
937 label = makeDraggableLabel(WMWidgetOfView(self), file,
938 pos.x-pos2.x, pos.y-pos2.y);
939 WMRealizeWidget(label);
940 WMMapWidget(label);
944 return True;
948 void
949 concludeDragOperation(WMView *self, WMDraggingInfo *info)
956 WMDragDestinationProcs dragDestProcs = {
957 draggingEntered,
958 draggingUpdated,
959 NULL,
960 prepareForDragOperation,
961 performDragOperation,
962 concludeDragOperation
968 WMLabel*
969 makeDraggableLabel(WMWidget *w, char *file, int x, int y)
971 DNDStuff *stuff;
972 WMLabel *label;
973 WMPixmap *image = getImage(WMWidgetScreen(w), file);
975 stuff = wmalloc(sizeof(DNDStuff));
976 stuff->mouseDown = False;
978 stuff->filename = wstrdup(file);
980 label = WMCreateLabel(w);
981 WMResizeWidget(label, 48, 48);
982 WMMoveWidget(label, x, y);
984 WMSetViewDragSourceProcs(WMWidgetView(label), &dragSourceProcs);
986 WMHangData(label, stuff);
988 WMCreateEventHandler(WMWidgetView(label),
989 ButtonPressMask|ButtonReleaseMask|ButtonMotionMask,
990 iconMouseStuff, label);
993 if (image != NULL) {
994 WMSetLabelImagePosition(label, WIPImageOnly);
995 WMSetLabelImage(label, image);
996 WMReleasePixmap(image);
997 } else puts(file);
999 return label;
1004 void
1005 testDragAndDrop(WMScreen *scr)
1007 WMWindow *win;
1008 WMFrame *frame;
1009 WMLabel *label;
1010 int i, j;
1011 DIR *dir;
1012 struct dirent *ent;
1013 char *types[] = {
1014 "application/X-WINGs-Bla",
1015 NULL
1018 windowCount++;
1020 win = WMCreateWindow(scr, "dragDrop");
1021 WMResizeWidget(win, 300, 300);
1022 WMSetWindowCloseAction(win, closeAction, NULL);
1023 WMSetWindowTitle(win, "Drag and Drop");
1026 frame = WMCreateFrame(win);
1027 WMSetFrameRelief(frame, WRSunken);
1028 WMResizeWidget(frame, 250, 250);
1029 WMMoveWidget(frame, 25, 25);
1031 WMRegisterViewForDraggedTypes(WMWidgetView(frame), types);
1032 WMSetViewDragDestinationProcs(WMWidgetView(frame), &dragDestProcs);
1034 dir = opendir("../../WindowMaker/Icons");
1035 if (!dir) {
1036 perror("../../WindowMaker/Icons");
1037 return;
1040 for (i = 0, j=0; j < 8; i++) {
1041 ent = readdir(dir);
1042 if (!ent)
1043 break;
1045 if (strstr(ent->d_name, ".xpm")==NULL) {
1046 continue;
1049 label = makeDraggableLabel(frame, ent->d_name,4+(j/4)*64, 4+(j%4)*64);
1051 j++;
1054 closedir(dir);
1056 WMMapSubwidgets(frame);
1058 WMMapSubwidgets(win);
1059 WMRealizeWidget(win);
1060 WMMapWidget(win);
1067 /*******************************************************************/
1069 #include "WUtil.h"
1071 void
1072 testUD()
1074 WMUserDefaults *defs;
1075 char str[32];
1077 defs = WMGetStandardUserDefaults();
1079 sprintf(str, "TEST DATA");
1080 puts(str);
1081 WMSetUDStringForKey(defs, str, "testKey");
1082 puts(str);
1087 main(int argc, char **argv)
1089 WMScreen *scr;
1090 WMPixmap *pixmap;
1093 /* Initialize the application */
1094 WMInitializeApplication("Test", &argc, argv);
1096 testUD();
1099 * Open connection to the X display.
1101 dpy = XOpenDisplay("");
1103 if (!dpy) {
1104 puts("could not open display");
1105 exit(1);
1108 /* This is used to disable buffering of X protocol requests.
1109 * Do NOT use it unless when debugging. It will cause a major
1110 * slowdown in your application
1112 #if 1
1113 XSynchronize(dpy, True);
1114 #endif
1116 * Create screen descriptor.
1118 scr = WMCreateScreen(dpy, DefaultScreen(dpy));
1121 * Loads the logo of the application.
1123 pixmap = WMCreatePixmapFromFile(scr, "logo.xpm");
1126 * Makes the logo be used in standard dialog panels.
1128 WMSetApplicationIconImage(scr, pixmap); WMReleasePixmap(pixmap);
1131 * Do some test stuff.
1133 * Put the testSomething() function you want to test here.
1136 testList(scr);
1138 #if 0
1139 testColorWell(scr);
1141 testTextField(scr);
1142 testText(scr);
1144 testDragAndDrop(scr);
1145 testDragAndDrop(scr);
1146 testFontPanel(scr);
1148 testScrollView(scr);
1150 testButton(scr);
1152 testFrame(scr);
1155 testTabView(scr);
1159 testSplitView(scr);
1161 testGradientButtons(scr);
1162 testProgressIndicator(scr);
1165 testOpenFilePanel(scr);
1167 testSlider(scr);
1168 testPullDown(scr);
1169 #endif
1171 * The main event loop.
1174 WMScreenMainLoop(scr);
1176 return 0;