WINGs: Better presentation for example code in the tutorial
[whome.git] / WINGs_tutorial / FifthWindow.c
blob2ed67706afc7929b1b0b966012d67e89ae619a62
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;
16 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)
25 int i = 0;
26 WMOpenPanel *oPanel;
28 oPanel = WMGetOpenPanel(screen);
29 if (WMRunModalFilePanelForDirectory(oPanel, NULL, "/tmp",
30 "Search..", NULL) == True) {
31 char textbuf[40];
33 snprintf(textbuf, sizeof(textbuf), "%s\n-", WMGetFilePanelFileName(oPanel));
34 WMFreezeText(text);
35 WMAppendTextStream(text, textbuf);
36 WMThawText(text);
38 return;
41 static void handleEvents(XEvent *event, void *data)
43 char textbuf[40];
44 WMWidget *widget = (WMWidget*) data;
46 switch (event->type) {
47 case ButtonPress:
48 snprintf(textbuf, sizeof(textbuf), "Button down at (%i,%i) \n-",
49 event->xbutton.x, event->xbutton.y);
50 WMFreezeText(text);
51 WMAppendTextStream(text, textbuf);
52 WMThawText(text);
53 break;
57 static void resizeHandler(void *self, WMNotification *notif)
59 WMSize size = WMGetViewSize(WMWidgetView(win));
60 WMMoveWidget(controlframe, size.width-ButtonsetSize.width, size.height-ButtonsetSize.height);
61 WMResizeWidget(text, size.width - MARGIN - 10, size.height - 80);
65 int main(int argc, char **argv)
67 WMButton *Button;
69 WMInitializeApplication("FifthWindow", &argc, argv);
70 display = XOpenDisplay("");
71 if (!display) {
72 fprintf(stderr, "err: cannot open display");
73 exit(1);
75 screen = WMCreateScreen(display, DefaultScreen(display));
77 /* window */
78 win = WMCreateWindow(screen, "");
79 WMResizeWidget(win, WINWIDTH, WINHEIGHT);
80 WMSetWindowCloseAction(win, closeAll, NULL);
81 WMCreateEventHandler(WMWidgetView(win), ButtonPressMask, handleEvents, win);
82 color = WMCreateRGBColor(screen, 124<<9, 206<<8, 162<<8, False);
83 WMSetWidgetBackgroundColor((WMWidget *)win, color);
84 WMSetViewNotifySizeChanges(WMWidgetView(win), True);
85 WMAddNotificationObserver(resizeHandler, NULL, WMViewSizeDidChangeNotification, WMWidgetView(win));
87 /* Text area */
89 text = WMCreateText(win);
90 WMResizeWidget(text, WINWIDTH - MARGIN, WINHEIGHT - 80);
91 WMMoveWidget(text, 10, 10);
92 WMSetTextHasVerticalScroller(text, True);
93 WMSetTextEditable(text, False);
95 /* frame and two buttons */
97 controlframe = WMCreateFrame(win);
98 WMSetWidgetBackgroundColor((WMWidget *)controlframe, color);
99 WMSetFrameRelief(controlframe, WRFlat);
101 Button = WMCreateButton(controlframe, WBTMomentaryPush);
102 WMSetWidgetBackgroundColor((WMWidget *)Button, color);
103 WMSetButtonText(Button, "Files");
104 WMSetButtonAction(Button, selectFiles, NULL);
105 ButtonsetSize = WMGetViewSize(WMWidgetView(Button));
106 WMMoveWidget(Button, MARGIN, MARGIN);
108 Button = WMCreateButton(controlframe, WBTMomentaryPush);
109 WMSetWidgetBackgroundColor((WMWidget *)Button, color);
110 WMSetButtonText(Button, "Quit");
111 WMSetButtonAction(Button, closeAll, NULL);
112 WMMoveWidget(Button, 2*MARGIN + ButtonsetSize.width, MARGIN);
113 ButtonsetSize.width = 3*MARGIN + 2*ButtonsetSize.width;
114 ButtonsetSize.height = 2*MARGIN + ButtonsetSize.height;
115 WMResizeWidget(controlframe, ButtonsetSize.width, ButtonsetSize.height);
117 WMMapSubwidgets(controlframe);
118 resizeHandler(NULL,NULL);
120 /* end of frame and buttons setup */
122 WMMapSubwidgets(win);
123 WMMapWidget(win);
124 WMRealizeWidget(win);
126 WMScreenMainLoop(screen);
128 return 0;