WINGs: Better presentation for example code in the tutorial
[whome.git] / WINGs_tutorial / FourthWindow.c
blob93115fae4475d36324d4d433cde01e27e350838e
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 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 WMOpenPanel *oPanel;
26 oPanel = WMGetOpenPanel(screen);
27 if (WMRunModalFilePanelForDirectory(oPanel, NULL, "/tmp",
28 "Search..", NULL) == True) {
29 char textbuf[40];
31 snprintf(textbuf, sizeof(textbuf), "%s\n-", WMGetFilePanelFileName(oPanel));
32 WMFreezeText(text);
33 WMAppendTextStream(text, textbuf);
34 WMThawText(text);
36 return;
39 static void handleEvents(XEvent *event, void *data)
41 WMWidget *widget = (WMWidget*) data;
42 char textbuf[40];
44 switch (event->type) {
45 case ButtonPress:
46 snprintf(textbuf, sizeof(textbuf), "Button down at (%i,%i) \n-",
47 event->xbutton.x, event->xbutton.y);
48 WMFreezeText(text);
49 WMAppendTextStream(text, textbuf);
50 WMThawText(text);
51 break;
55 static void resizeHandler(void *self, WMNotification *notif)
57 WMSize size = WMGetViewSize(WMWidgetView(win));
58 WMMoveWidget(box, size.width - ButtonsetSize.width, size.height - ButtonsetSize.height);
59 WMResizeWidget(text, size.width - MARGIN - 10, size.height - 80);
63 int main(int argc, char **argv)
65 WMInitializeApplication("FourthWindow", &argc, argv);
66 if (!(display = XOpenDisplay(""))) {
67 fprintf(stderr, "err: cannot open display");
68 exit(-1);
70 screen = WMCreateScreen(display, DefaultScreen(display));
72 /* window */
73 win = WMCreateWindow(screen, "");
74 WMResizeWidget(win, WINWIDTH, WINHEIGHT);
75 WMSetWindowCloseAction(win, closeAll, NULL);
77 color = WMCreateRGBColor(screen, 124<<9, 206<<8, 162<<8, False);
78 WMSetWidgetBackgroundColor((WMWidget *)win, color);
80 WMCreateEventHandler(WMWidgetView(win), ButtonPressMask, handleEvents, win);
81 WMSetViewNotifySizeChanges(WMWidgetView(win), True);
82 WMAddNotificationObserver(resizeHandler, NULL, WMViewSizeDidChangeNotification, WMWidgetView(win));
84 /* Text area */
85 text = WMCreateText(win);
86 WMResizeWidget(text, WINWIDTH - MARGIN, WINHEIGHT - 80);
87 WMMoveWidget(text, 10, 10);
88 WMSetTextHasVerticalScroller(text, True);
89 WMSetTextEditable(text, False);
90 WMSetTextIgnoresNewline(text, False);
92 /* box with buttons */
93 box = WMCreateBox(win);
94 WMSetBoxBorderWidth(box, MARGIN);
95 WMSetWidgetBackgroundColor((WMWidget *)box, color);
96 WMSetBoxHorizontal(box, True);
98 Button = WMCreateButton(box, WBTMomentaryPush);
99 WMSetWidgetBackgroundColor((WMWidget *)Button, color);
100 WMSetButtonText(Button, "Files");
101 WMSetButtonAction(Button, selectFiles, NULL);
102 WMMapWidget(Button);
103 ButtonsetSize = WMGetViewSize(WMWidgetView(Button));
105 WMAddBoxSubview(box, WMWidgetView(Button), True, False, 60, 1000, MARGIN);
107 Button = WMCreateButton(box, WBTMomentaryPush);
108 WMSetWidgetBackgroundColor((WMWidget *)Button, color);
109 WMSetButtonText(Button, "Quit");
110 WMSetButtonAction(Button, closeAll, NULL);
111 WMMapWidget(Button);
113 WMAddBoxSubview(box, WMWidgetView(Button), True, False, 60, 1000, 0);
114 WMResizeWidget(box, 4 * MARGIN + 2 * ButtonsetSize.width,
115 2 * MARGIN + ButtonsetSize.height);
116 ButtonsetSize = WMGetViewSize(WMWidgetView(box));
117 resizeHandler(NULL, NULL);
118 /* end of box and buttons setup */
120 WMMapWidget(win);
122 WMMapSubwidgets(win);
123 WMRealizeWidget(win);
125 WMScreenMainLoop(screen);
127 return 0;