File: wtest.c

package info (click to toggle)
wmaker 0.96.0-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 15,332 kB
  • sloc: ansic: 99,482; sh: 7,068; perl: 3,423; makefile: 1,647; lisp: 219; python: 34
file content (184 lines) | stat: -rw-r--r-- 4,738 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
/* quick and dirty test application that demonstrates: application hiding,
 *	application defined titlebar button images, application defined
 *	titlebar button actions, application menus, docking and
 *	window manager commands
 *
 * Note that the windows don't have a window command menu.
 *
 * TODO: remake
 */

#include "config.h"

#include <stdio.h>
#include <stdlib.h>
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/Xproto.h>
#include <WMaker.h>

#ifdef HAVE_STDNORETURN
#include <stdnoreturn.h>
#endif


static char bits[] = {
	0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
	0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff
};

static char mbits[] = {
	0xff, 0x03, 0xff, 0x01, 0xff, 0x00, 0x7f, 0x00, 0x3f, 0x00, 0x1f, 0x00,
	0x0f, 0x00, 0x07, 0x00, 0x03, 0x00, 0x01, 0x00
};

Display *dpy;
Window leader;
WMAppContext *app;

static void callback(int item)
{
	printf("pushed item %i\n", item);
}

static noreturn void quit(int item)
{
	/*
	 * This parameter is not used, but because we're a call-back we have a fixed
	 * prototype, so we tell the compiler it is ok to avoid a spurious unused
	 * variable warning
	 */
	(void) item;

	exit(0);
}

static void hide(int item)
{
	/*
	 * This parameter is not used, but because we're a call-back we have a fixed
	 * prototype, so we tell the compiler it is ok to avoid a spurious unused
	 * variable warning
	 */
	(void) item;

	WMHideApplication(app);
}

Atom delete_win, miniaturize_win;
Atom prots[6];
GNUstepWMAttributes attr;
XWMHints *hints;
WMMenu *menu;
WMMenu *submenu;
int wincount = 0;

static void newwin(int item)
{
	Window win;
	XClassHint classhint;
	char title[100];

	/*
	 * This parameter is not used, but because we're a call-back we have a fixed
	 * prototype, so we tell the compiler it is ok to avoid a spurious unused
	 * variable warning
	 */
	(void) item;

	wincount++;
	win = XCreateSimpleWindow(dpy, DefaultRootWindow(dpy), 10 * wincount, 10 * wincount, 200, 100, 0, 0, 0);
	prots[0] = delete_win;
	prots[1] = miniaturize_win;
	XSetWMProtocols(dpy, win, prots, 2);
	sprintf(title, "Test Window %i", wincount);
	XStoreName(dpy, win, title);

	/* set class hint */
	classhint.res_name = "test";
	classhint.res_class = "Test";
	XSetClassHint(dpy, win, &classhint);

	/* set WindowMaker hints */
	attr.flags = GSMiniaturizePixmapAttr | GSMiniaturizeMaskAttr;
	attr.miniaturize_pixmap = XCreateBitmapFromData(dpy, DefaultRootWindow(dpy), bits, 10, 10);

	attr.miniaturize_mask = XCreateBitmapFromData(dpy, DefaultRootWindow(dpy), mbits, 10, 10);

	WMSetWindowAttributes(dpy, win, &attr);

	hints = XAllocWMHints();
	/* set window group leader */
	hints->window_group = leader;
	hints->flags = WindowGroupHint;
	XSetWMHints(dpy, win, hints);

	WMAppAddWindow(app, win);
	XMapWindow(dpy, win);
}

int main(int argc, char **argv)
{
	XClassHint classhint;

	dpy = XOpenDisplay("");
	if (!dpy) {
		puts("could not open display!");
		exit(1);
	}
	delete_win = XInternAtom(dpy, "WM_DELETE_WINDOW", False);
	miniaturize_win = XInternAtom(dpy, "_GNUSTEP_WM_MINIATURIZE_WINDOW", False);

	leader = XCreateSimpleWindow(dpy, DefaultRootWindow(dpy), 10, 10, 10, 10, 0, 0, 0);

	/* set class hint */
	classhint.res_name = "test";
	classhint.res_class = "Test";
	XSetClassHint(dpy, leader, &classhint);

	/* set window group leader to self */
	hints = XAllocWMHints();
	hints->window_group = leader;
	hints->flags = WindowGroupHint;
	XSetWMHints(dpy, leader, hints);

	/* create app context */
	app = WMAppCreateWithMain(dpy, DefaultScreen(dpy), leader);
	menu = WMMenuCreate(app, "Test Menu");
	submenu = WMMenuCreate(app, "File");
	WMMenuAddSubmenu(menu, "File", submenu);

	WMMenuAddItem(menu, "Hide", (WMMenuAction) hide, NULL, NULL, NULL);
	WMMenuAddItem(menu, "Quit", (WMMenuAction) quit, NULL, NULL, NULL);
	WMMenuAddItem(submenu, "New", (WMMenuAction) newwin, NULL, NULL, NULL);
	WMMenuAddItem(submenu, "Open", (WMMenuAction) callback, NULL, NULL, NULL);
	WMMenuAddItem(submenu, "Save", (WMMenuAction) callback, NULL, NULL, NULL);
	WMMenuAddItem(submenu, "Save As...", (WMMenuAction) callback, NULL, NULL, NULL);

	WMAppSetMainMenu(app, menu);

	WMRealizeMenus(app);

	/* set command to use to startup this */
	XSetCommand(dpy, leader, argv, argc);

	/* create first window */
	newwin(0);

	XFlush(dpy);
	puts("Run xprop on the test window to see the properties defined");
	while (wincount > 0) {
		XEvent ev;
		XNextEvent(dpy, &ev);
		if (ev.type == ClientMessage) {
			if (ev.xclient.data.l[0] == delete_win) {
				XDestroyWindow(dpy, ev.xclient.window);
				wincount--;
			} else if (ev.xclient.data.l[0] == miniaturize_win) {
				puts("You've pushed the maximize window button");
			}
		}
		WMProcessEvent(app, &ev);
	}
	exit(0);
}