Update tutorial link
[whome.git] / WINGs_tutorial / FifthWindow.c
blob3260b7b454c1b3005c03c61a65689674171c561c
1 #define MARGIN 14
2 #define WINWIDTH 300
3 #define WINHEIGHT 400
5 Display *display;
6 WMScreen *screen;
8 WMWindow *win;
9 WMSize ButtonsetSize;
11 WMText *text;
12 WMColor *color;
13 WMFrame *controlframe;
15 char textbuf[40];
17 void closeAll(WMWidget *self,void *data){
18 WMDestroyWidget(self);
19 fprintf(stderr,"I've been used!\n");
20 exit(0);
23 static void selectFiles(void *self, void *data){
24 int i=0;
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(controlframe, 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 WMButton *Button;
60 WMInitializeApplication("FifthWindow", &argc, argv);
61 if (!(display = XOpenDisplay(""))){
62 fprintf(stderr,"err: cannot open display");
63 exit(1);
65 screen = WMCreateScreen(display, DefaultScreen(display));
67 /* window */
68 win = WMCreateWindow(screen, "");
69 WMResizeWidget(win, WINWIDTH, WINHEIGHT);
70 WMSetWindowCloseAction(win, closeAll, NULL);
71 WMCreateEventHandler(WMWidgetView(win), ButtonPressMask,handleEvents, win);
72 color = WMCreateRGBColor(screen, 124<<9,206<<8,162<<8, False);
73 WMSetWidgetBackgroundColor((WMWidget *)win, color);
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);
85 /* frame and two buttons */
87 controlframe=WMCreateFrame(win);
88 WMSetWidgetBackgroundColor((WMWidget *)controlframe, color);
89 WMSetFrameRelief(controlframe,WRFlat);
91 Button =WMCreateButton(controlframe,WBTMomentaryPush);
92 WMSetWidgetBackgroundColor((WMWidget *)Button, color);
93 WMSetButtonText (Button, "Files");
94 WMSetButtonAction (Button, selectFiles, NULL);
95 ButtonsetSize = WMGetViewSize(WMWidgetView(Button));
96 WMMoveWidget(Button, MARGIN, MARGIN);
98 Button =WMCreateButton(controlframe,WBTMomentaryPush);
99 WMSetWidgetBackgroundColor((WMWidget *)Button, color);
100 WMSetButtonText (Button, "Quit");
101 WMSetButtonAction (Button, closeAll, NULL);
102 WMMoveWidget(Button,2*MARGIN+ButtonsetSize.width, MARGIN);
103 ButtonsetSize.width = 3*MARGIN+2*ButtonsetSize.width;
104 ButtonsetSize.height=2*MARGIN+ButtonsetSize.height;
105 WMResizeWidget(controlframe,ButtonsetSize.width,ButtonsetSize.height);
107 WMMapSubwidgets(controlframe);
108 resizeHandler(NULL,NULL);
109 /* end of frame and buttons setup */
112 WMMapSubwidgets(win);
113 WMMapWidget(win);
114 WMRealizeWidget(win);
116 WMScreenMainLoop(screen);
118 return 0;