Fixed issue with GNUstep applications losing focus when all their windows
[wmaker-crm.git] / test / wtest.c
blob71feeb1fa603d9cdea22a7d4202dee102b639099
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
6 * Note that the windows don't have a window command menu.
8 * TODO: remake
9 */
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>
18 static unsigned char bits[] = {
19 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
20 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
22 static unsigned char mbits[] = {
23 0xff, 0x03, 0xff, 0x01, 0xff, 0x00, 0x7f, 0x00, 0x3f, 0x00, 0x1f, 0x00,
24 0x0f, 0x00, 0x07, 0x00, 0x03, 0x00, 0x01, 0x00};
27 Display *dpy;
28 Window leader;
29 WMAppContext *app;
32 static void
33 callback(void *foo, int item, Time time)
35 printf("pushed item %i\n", item);
38 static void
39 quit(void *foo, int item, Time time)
41 exit(0);
46 static void
47 hide(void *foo, int item, Time time)
49 WMHideApplication(app);
53 Atom delete_win, miniaturize_win;
54 Atom prots[6];
55 GNUstepWMAttributes attr;
56 XWMHints *hints;
57 WMMenu *menu;
58 WMMenu *submenu;
59 int wincount=0;
61 static void
62 newwin(void *foo, int item, Time time)
64 Window win;
65 XClassHint classhint;
66 char title[100];
68 wincount++;
69 win = XCreateSimpleWindow(dpy, DefaultRootWindow(dpy),
70 10*wincount, 10*wincount, 200, 100, 0, 0, 0);
71 prots[0] = delete_win;
72 prots[1] = miniaturize_win;
73 XSetWMProtocols(dpy, win, prots, 2);
74 sprintf(title, "Test Window %i", wincount);
75 XStoreName(dpy, win, title);
77 /* set class hint */
78 classhint.res_name = "test";
79 classhint.res_class = "Test";
80 XSetClassHint(dpy, win, &classhint);
82 /* set WindowMaker hints */
83 attr.flags = GSMiniaturizePixmapAttr|GSMiniaturizeMaskAttr;
84 attr.miniaturize_pixmap =
85 XCreateBitmapFromData(dpy, DefaultRootWindow(dpy), bits, 10, 10);
87 attr.miniaturize_mask =
88 XCreateBitmapFromData(dpy, DefaultRootWindow(dpy), mbits, 10, 10);
90 attr.flags |= GSWindowStyleAttr;
91 attr.window_style = NSTitledWindowMask|NSClosableWindowMask;
94 WMSetWindowAttributes(dpy, win, &attr);
96 hints = XAllocWMHints();
97 /* set window group leader */
98 hints->window_group = leader;
99 hints->flags = WindowGroupHint;
100 XSetWMHints(dpy, win, hints);
102 WMAppAddWindow(app, win);
103 XMapWindow(dpy, win);
106 int main(int argc, char **argv)
108 XClassHint classhint;
110 dpy = XOpenDisplay("");
111 if (!dpy) {
112 puts("could not open display!");
113 exit(1);
115 delete_win = XInternAtom(dpy, "WM_DELETE_WINDOW", False);
116 miniaturize_win = XInternAtom(dpy, "_GNUSTEP_WM_MINIATURIZE_WINDOW",
117 False);
119 leader = XCreateSimpleWindow(dpy, DefaultRootWindow(dpy), 10, 10, 10, 10,
120 0, 0, 0);
121 /* set class hint */
122 classhint.res_name = "test";
123 classhint.res_class = "Test";
124 XSetClassHint(dpy, leader, &classhint);
126 /* set window group leader to self */
127 hints = XAllocWMHints();
128 hints->window_group = leader;
129 hints->flags = WindowGroupHint;
130 XSetWMHints(dpy, leader, hints);
132 /* create app context */
133 app = WMAppCreateWithMain(dpy, DefaultScreen(dpy), leader);
134 menu = WMMenuCreate(app, "Test Menu");
135 submenu = WMMenuCreate(app, "File");
136 WMMenuAddSubmenu(menu, "File", submenu);
138 WMMenuAddItem(menu, "Hide", (WMMenuAction)hide, NULL, NULL, NULL);
139 WMMenuAddItem(menu, "Quit", (WMMenuAction)quit, NULL, NULL, NULL);
140 WMMenuAddItem(submenu, "New", (WMMenuAction)newwin, NULL, NULL, NULL);
141 WMMenuAddItem(submenu, "Open", (WMMenuAction)callback, NULL, NULL, NULL);
142 WMMenuAddItem(submenu, "Save", (WMMenuAction)callback, NULL, NULL, NULL);
143 WMMenuAddItem(submenu, "Save As...", (WMMenuAction)callback, NULL, NULL, NULL);
145 WMAppSetMainMenu(app, menu);
147 WMRealizeMenus(app);
149 /* set command to use to startup this */
150 XSetCommand(dpy, leader, argv, argc);
152 /* create first window */
153 newwin(NULL, 0, 0);
156 XFlush(dpy);
157 puts("Run xprop on the test window to see the properties defined");
158 while (wincount>0) {
159 XEvent ev;
160 XNextEvent(dpy, &ev);
161 if (ev.type==ClientMessage) {
162 if (ev.xclient.data.l[0]==delete_win) {
163 XDestroyWindow(dpy,ev.xclient.window);
164 wincount--;
165 } else if (ev.xclient.data.l[0]==miniaturize_win) {
166 puts("You've pushed the maximize window button");
169 WMProcessEvent(app, &ev);
171 exit(0);