Change to the linux kernel coding style
[wmaker-crm.git] / test / wtest.c
1 /* quick and dirty test application that demonstrates: application hiding,
2 * application defined titlebar button images, application defined
3 * titlebar button actions, application menus, docking and
4 * window manager commands
5 *
6 * Note that the windows don't have a window command menu.
7 *
8 * TODO: remake
9 */
10
11 #include <stdio.h>
12 #include <stdlib.h>
13 #include <X11/Xlib.h>
14 #include <X11/Xutil.h>
15 #include <X11/Xproto.h>
16 #include <WMaker.h>
17
18 static char bits[] = {
19 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
20 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff
21 };
22
23 static char mbits[] = {
24 0xff, 0x03, 0xff, 0x01, 0xff, 0x00, 0x7f, 0x00, 0x3f, 0x00, 0x1f, 0x00,
25 0x0f, 0x00, 0x07, 0x00, 0x03, 0x00, 0x01, 0x00
26 };
27
28 Display *dpy;
29 Window leader;
30 WMAppContext *app;
31
32 static void callback(void *foo, int item, Time time)
33 {
34 printf("pushed item %i\n", item);
35 }
36
37 static void quit(void *foo, int item, Time time)
38 {
39 exit(0);
40 }
41
42 static void hide(void *foo, int item, Time time)
43 {
44 WMHideApplication(app);
45 }
46
47 Atom delete_win, miniaturize_win;
48 Atom prots[6];
49 GNUstepWMAttributes attr;
50 XWMHints *hints;
51 WMMenu *menu;
52 WMMenu *submenu;
53 int wincount = 0;
54
55 static void newwin(void *foo, int item, Time time)
56 {
57 Window win;
58 XClassHint classhint;
59 char title[100];
60
61 wincount++;
62 win = XCreateSimpleWindow(dpy, DefaultRootWindow(dpy), 10 * wincount, 10 * wincount, 200, 100, 0, 0, 0);
63 prots[0] = delete_win;
64 prots[1] = miniaturize_win;
65 XSetWMProtocols(dpy, win, prots, 2);
66 sprintf(title, "Test Window %i", wincount);
67 XStoreName(dpy, win, title);
68
69 /* set class hint */
70 classhint.res_name = "test";
71 classhint.res_class = "Test";
72 XSetClassHint(dpy, win, &classhint);
73
74 /* set WindowMaker hints */
75 attr.flags = GSMiniaturizePixmapAttr | GSMiniaturizeMaskAttr;
76 attr.miniaturize_pixmap = XCreateBitmapFromData(dpy, DefaultRootWindow(dpy), bits, 10, 10);
77
78 attr.miniaturize_mask = XCreateBitmapFromData(dpy, DefaultRootWindow(dpy), mbits, 10, 10);
79 /*
80 attr.flags |= GSWindowStyleAttr;
81 attr.window_style = NSTitledWindowMask|NSClosableWindowMask;
82 */
83
84 WMSetWindowAttributes(dpy, win, &attr);
85
86 hints = XAllocWMHints();
87 /* set window group leader */
88 hints->window_group = leader;
89 hints->flags = WindowGroupHint;
90 XSetWMHints(dpy, win, hints);
91
92 WMAppAddWindow(app, win);
93 XMapWindow(dpy, win);
94 }
95
96 int main(int argc, char **argv)
97 {
98 XClassHint classhint;
99
100 dpy = XOpenDisplay("");
101 if (!dpy) {
102 puts("could not open display!");
103 exit(1);
104 }
105 delete_win = XInternAtom(dpy, "WM_DELETE_WINDOW", False);
106 miniaturize_win = XInternAtom(dpy, "_GNUSTEP_WM_MINIATURIZE_WINDOW", False);
107
108 leader = XCreateSimpleWindow(dpy, DefaultRootWindow(dpy), 10, 10, 10, 10, 0, 0, 0);
109 /* set class hint */
110 classhint.res_name = "test";
111 classhint.res_class = "Test";
112 XSetClassHint(dpy, leader, &classhint);
113
114 /* set window group leader to self */
115 hints = XAllocWMHints();
116 hints->window_group = leader;
117 hints->flags = WindowGroupHint;
118 XSetWMHints(dpy, leader, hints);
119
120 /* create app context */
121 app = WMAppCreateWithMain(dpy, DefaultScreen(dpy), leader);
122 menu = WMMenuCreate(app, "Test Menu");
123 submenu = WMMenuCreate(app, "File");
124 WMMenuAddSubmenu(menu, "File", submenu);
125
126 WMMenuAddItem(menu, "Hide", (WMMenuAction) hide, NULL, NULL, NULL);
127 WMMenuAddItem(menu, "Quit", (WMMenuAction) quit, NULL, NULL, NULL);
128 WMMenuAddItem(submenu, "New", (WMMenuAction) newwin, NULL, NULL, NULL);
129 WMMenuAddItem(submenu, "Open", (WMMenuAction) callback, NULL, NULL, NULL);
130 WMMenuAddItem(submenu, "Save", (WMMenuAction) callback, NULL, NULL, NULL);
131 WMMenuAddItem(submenu, "Save As...", (WMMenuAction) callback, NULL, NULL, NULL);
132
133 WMAppSetMainMenu(app, menu);
134
135 WMRealizeMenus(app);
136
137 /* set command to use to startup this */
138 XSetCommand(dpy, leader, argv, argc);
139
140 /* create first window */
141 newwin(NULL, 0, 0);
142
143 XFlush(dpy);
144 puts("Run xprop on the test window to see the properties defined");
145 while (wincount > 0) {
146 XEvent ev;
147 XNextEvent(dpy, &ev);
148 if (ev.type == ClientMessage) {
149 if (ev.xclient.data.l[0] == delete_win) {
150 XDestroyWindow(dpy, ev.xclient.window);
151 wincount--;
152 } else if (ev.xclient.data.l[0] == miniaturize_win) {
153 puts("You've pushed the maximize window button");
154 }
155 }
156 WMProcessEvent(app, &ev);
157 }
158 exit(0);
159 }