Add WINGs tutorial
[whome.git] / WINGs_tutorial / FourthWindow.c
blob66b98a1971f959f1d5e1fcaad4e84c5231d52b75
1 #define MARGIN 14
2 #define WINWIDTH 300
3 #define WINHEIGHT 400
5 Display *display;
6 WMScreen *screen;
8 WMButton *Button;
9 WMWindow *win;
10 WMSize ButtonsetSize;
12 WMBox *box;
13 WMText *text;
14 WMColor *color;
16 char textbuf[40];
18 void closeAll(WMWidget *self,void *data){
19 WMDestroyWidget(self);
20 fprintf(stderr,"I've been used!\n");
21 exit(0);
24 static void selectFiles(void *self, void *data){
25 WMOpenPanel *oPanel;
26 oPanel = WMGetOpenPanel(screen);
27 if (WMRunModalFilePanelForDirectory(oPanel, NULL, "/tmp",
28 "Search..", NULL) == True){
29 snprintf(textbuf,39,"%s\n-", WMGetFilePanelFileName(oPanel));
30 WMFreezeText(text);
31 WMAppendTextStream(text,textbuf);
32 WMThawText(text);
34 return ;
37 static void handleEvents(XEvent *event, void *data){
38 WMWidget *widget = (WMWidget*)data;
39 switch (event->type) {
40 case ButtonPress:
41 snprintf(textbuf,39,"Button down at (%i,%i) \n-",event->xbutton.x,event->xbutton.y);
42 WMFreezeText(text);
43 WMAppendTextStream(text,textbuf);
44 WMThawText(text);
45 break;
49 static void resizeHandler(void *self, WMNotification *notif){
50 WMSize size = WMGetViewSize(WMWidgetView(win));
51 WMMoveWidget(box, size.width-ButtonsetSize.width, size.height-ButtonsetSize.height);
52 WMResizeWidget(text, size.width-MARGIN -10, size.height-80);
56 int main (int argc, char **argv){
58 WMInitializeApplication("FourthWindow", &argc, argv);
59 if (!(display = XOpenDisplay(""))){
60 fprintf(stderr,"err: cannot open display");
61 exit(-1);
63 screen = WMCreateScreen(display, DefaultScreen(display));
65 /* window */
66 win = WMCreateWindow(screen, "");
67 WMResizeWidget(win, WINWIDTH, WINHEIGHT);
68 WMSetWindowCloseAction(win, closeAll, NULL);
70 color = WMCreateRGBColor(screen, 124<<9,206<<8,162<<8, False);
71 WMSetWidgetBackgroundColor((WMWidget *)win, color);
73 WMCreateEventHandler(WMWidgetView(win), ButtonPressMask,handleEvents, win);
74 WMSetViewNotifySizeChanges(WMWidgetView(win), True);
75 WMAddNotificationObserver(resizeHandler, NULL, WMViewSizeDidChangeNotification, WMWidgetView(win));
77 /* Text area */
79 text = WMCreateText(win);
80 WMResizeWidget(text, WINWIDTH-MARGIN, WINHEIGHT -80);
81 WMMoveWidget(text, 10, 10);
82 WMSetTextHasVerticalScroller(text, True);
83 WMSetTextEditable(text, False);
84 WMSetTextIgnoresNewline(text, False);
86 /* box with buttons */
87 box=WMCreateBox(win);
88 WMSetBoxBorderWidth(box, MARGIN);
89 WMSetWidgetBackgroundColor((WMWidget *)box, color);
90 WMSetBoxHorizontal(box, True);
93 Button =WMCreateButton(box,WBTMomentaryPush);
94 WMSetWidgetBackgroundColor((WMWidget *)Button, color);
95 WMSetButtonText (Button, "Files");
96 WMSetButtonAction (Button, selectFiles, NULL);
97 WMMapWidget(Button);
98 ButtonsetSize = WMGetViewSize(WMWidgetView(Button));
100 WMAddBoxSubview(box, WMWidgetView(Button), True, False, 60, 1000, MARGIN);
102 Button =WMCreateButton(box,WBTMomentaryPush);
103 WMSetWidgetBackgroundColor((WMWidget *)Button, color);
104 WMSetButtonText (Button, "Quit");
105 WMSetButtonAction (Button, closeAll, NULL);
106 WMMapWidget(Button);
108 WMAddBoxSubview(box, WMWidgetView(Button), True,False, 60, 1000, 0);
109 WMResizeWidget(box, 4*MARGIN+2*ButtonsetSize.width,2*MARGIN+ButtonsetSize.height);
110 ButtonsetSize =WMGetViewSize(WMWidgetView(box));
111 resizeHandler(NULL,NULL);
112 /* end of box and buttons setup */
114 WMMapWidget(win);
116 WMMapSubwidgets(win);
117 WMRealizeWidget(win);
119 WMScreenMainLoop(screen);
121 return 0;