annotate etc/tests/external-widget/test-ew-xlib.c @ 4936:94bba904528c

(for main branch) Also reset dynarr->largest value during pdump. Avoids an assertion failure when working with dynarrs across dumping.
author Ben Wing <ben@xemacs.org>
date Tue, 19 Jan 2010 05:07:36 -0600
parents ac2d302a0011
children 03ab78e48ef6
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
2
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
1 /*
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
2 * Copyright 1989 O'Reilly and Associates, Inc.
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
3 * See ../Copyright for complete rights and liability information.
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
4 */
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
5 #include <X11/Xlib.h>
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
6 #include <X11/Xutil.h>
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
7 #include <X11/Xos.h>
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
8 #include <X11/Xatom.h>
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
9
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
10 #include <stdio.h>
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
11
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
12 #define BITMAPDEPTH 1
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
13 #define TOO_SMALL 0
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
14 #define BIG_ENOUGH 1
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
15
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
16 /* These are used as arguments to nearly every Xlib routine, so it saves
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
17 * routine arguments to declare them global. If there were
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
18 * additional source files, they would be declared extern there. */
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
19 Display *display;
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
20 int screen_num;
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
21
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
22 static char *progname; /* name this program was invoked by */
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
23
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
24 void main(argc, argv)
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
25 int argc;
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
26 char **argv;
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
27 {
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
28 Window topwin, win, win2;
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
29 unsigned int width, height; /* window size */
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
30 int x, y; /* window position */
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
31 unsigned int border_width = 4; /* four pixels */
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
32 unsigned int display_width, display_height;
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
33 char *window_name = "Basic Window Program";
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
34 char *icon_name = "basicwin";
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
35 XSizeHints size_hints;
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
36 int count;
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
37 XEvent report;
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
38 GC gc;
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
39 XFontStruct *font_info;
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
40 char *display_name = NULL;
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
41 int window_size = BIG_ENOUGH; /* or TOO_SMALL to display contents */
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
42
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
43 progname = argv[0];
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
44
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
45 /* connect to X server */
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
46 if ( (display=XOpenDisplay(display_name)) == NULL )
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
47 {
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
48 (void) fprintf( stderr, "%s: cannot connect to X server %s\n",
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
49 progname, XDisplayName(display_name));
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
50 exit( -1 );
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
51 }
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
52
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
53 /* get screen size from display structure macro */
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
54 screen_num = DefaultScreen(display);
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
55 display_width = DisplayWidth(display, screen_num);
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
56 display_height = DisplayHeight(display, screen_num);
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
57
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
58 /* Note that in a real application, x and y would default to 0
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
59 * but would be settable from the command line or resource database.
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
60 */
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
61 x = y = 0;
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
62
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
63 /* size window with enough room for text */
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
64 width = display_width/2, height = display_height/3;
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
65
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
66 /* ------------------------------------------------------------ */
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
67
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
68 /* create top-level window */
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
69 topwin = XCreateSimpleWindow(display, RootWindow(display,screen_num),
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
70 x, y, width, 2*height, border_width,
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
71 BlackPixel(display, screen_num),
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
72 WhitePixel(display,screen_num));
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
73
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
74 /* Set size hints for window manager. The window manager may
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
75 * override these settings. Note that in a real
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
76 * application if size or position were set by the user
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
77 * the flags would be UPosition and USize, and these would
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
78 * override the window manager's preferences for this window. */
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
79
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
80 /* x, y, width, and height hints are now taken from
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
81 * the actual settings of the window when mapped. Note
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
82 * that PPosition and PSize must be specified anyway. */
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
83
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
84 size_hints.flags = PPosition | PSize | PMinSize;
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
85 size_hints.min_width = 300;
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
86 size_hints.min_height = 200;
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
87
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
88 {
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
89 XWMHints wm_hints;
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
90 XClassHint class_hints;
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
91
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
92 /* format of the window name and icon name
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
93 * arguments has changed in R4 */
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
94 XTextProperty windowName, iconName;
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
95
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
96 /* These calls store window_name and icon_name into
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
97 * XTextProperty structures and set their other
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
98 * fields properly. */
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
99 if (XStringListToTextProperty(&window_name, 1, &windowName) == 0) {
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
100 (void) fprintf( stderr, "%s: structure allocation for windowName failed.\n",
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
101 progname);
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
102 exit(-1);
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
103 }
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
104
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
105 if (XStringListToTextProperty(&icon_name, 1, &iconName) == 0) {
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
106 (void) fprintf( stderr, "%s: structure allocation for iconName failed.\n",
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
107 progname);
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
108 exit(-1);
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
109 }
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
110
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
111 wm_hints.initial_state = NormalState;
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
112 wm_hints.input = True;
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
113 wm_hints.flags = StateHint | InputHint;
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
114
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
115 class_hints.res_name = progname;
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
116 class_hints.res_class = "Basicwin";
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
117
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
118 XSetWMProperties(display, topwin, &windowName, &iconName,
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
119 argv, argc, &size_hints, &wm_hints,
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
120 &class_hints);
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
121 }
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
122
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
123 /* ------------------------------------------------------------ */
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
124
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
125 /* create the window we're in charge of */
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
126
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
127 win = XCreateSimpleWindow(display, topwin,
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
128 x, y, width, height, border_width, BlackPixel(display,
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
129 screen_num), WhitePixel(display,screen_num));
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
130
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
131 /* Select event types wanted */
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
132 XSelectInput(display, win, ExposureMask | KeyPressMask |
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
133 ButtonPressMask | StructureNotifyMask);
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
134
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
135 load_font(&font_info);
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
136
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
137 /* create GC for text and drawing */
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
138 getGC(win, &gc, font_info);
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
139
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
140 /* ------------------------------------------------------------ */
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
141
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
142 /* create the external-client window */
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
143
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
144 win2 = XCreateSimpleWindow(display, topwin,
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
145 x, y+height, width, height, border_width,
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
146 BlackPixel(display, screen_num),
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
147 WhitePixel(display,screen_num));
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
148 printf("external window: %d\n", win2);
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
149 ExternalClientInitialize(display, win2);
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
150
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
151 /* ------------------------------------------------------------ */
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
152
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
153 /* Display windows */
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
154 XMapWindow(display, topwin);
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
155 XMapWindow(display, win);
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
156 XMapWindow(display, win2);
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
157
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
158 /* get events, use first to display text and graphics */
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
159 while (1) {
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
160 XNextEvent(display, &report);
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
161 if (report.xany.window == win2)
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
162 ExternalClientEventHandler(display, win2, &report);
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
163 else
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
164 switch (report.type) {
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
165 case Expose:
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
166 /* unless this is the last contiguous expose,
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
167 * don't draw the window */
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
168 if (report.xexpose.count != 0)
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
169 break;
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
170
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
171 /* if window too small to use */
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
172 if (window_size == TOO_SMALL)
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
173 TooSmall(win, gc, font_info);
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
174 else {
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
175 /* place text in window */
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
176 draw_text(win, gc, font_info, width, height);
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
177
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
178 /* place graphics in window, */
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
179 draw_graphics(win, gc, width, height);
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
180 }
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
181 break;
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
182 case ConfigureNotify:
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
183 /* window has been resized, change width and
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
184 * height to send to draw_text and draw_graphics
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
185 * in next Expose */
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
186 width = report.xconfigure.width;
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
187 height = report.xconfigure.height;
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
188 if ((width < size_hints.min_width) ||
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
189 (height < size_hints.min_height))
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
190 window_size = TOO_SMALL;
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
191 else
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
192 window_size = BIG_ENOUGH;
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
193 break;
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
194 case ButtonPress:
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
195 /* trickle down into KeyPress (no break) */
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
196 case KeyPress:
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
197 XUnloadFont(display, font_info->fid);
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
198 XFreeGC(display, gc);
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
199 XCloseDisplay(display);
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
200 exit(1);
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
201 default:
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
202 /* all events selected by StructureNotifyMask
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
203 * except ConfigureNotify are thrown away here,
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
204 * since nothing is done with them */
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
205 break;
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
206 } /* end switch */
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
207 } /* end while */
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
208 }
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
209
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
210 getGC(win, gc, font_info)
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
211 Window win;
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
212 GC *gc;
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
213 XFontStruct *font_info;
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
214 {
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
215 unsigned long valuemask = 0; /* ignore XGCvalues and use defaults */
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
216 XGCValues values;
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
217 unsigned int line_width = 6;
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
218 int line_style = LineOnOffDash;
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
219 int cap_style = CapRound;
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
220 int join_style = JoinRound;
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
221 int dash_offset = 0;
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
222 static char dash_list[] = {12, 24};
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
223 int list_length = 2;
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
224
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
225 /* Create default Graphics Context */
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
226 *gc = XCreateGC(display, win, valuemask, &values);
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
227
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
228 /* specify font */
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
229 XSetFont(display, *gc, font_info->fid);
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
230
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
231 /* specify black foreground since default window background is
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
232 * white and default foreground is undefined. */
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
233 XSetForeground(display, *gc, BlackPixel(display,screen_num));
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
234
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
235 /* set line attributes */
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
236 XSetLineAttributes(display, *gc, line_width, line_style,
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
237 cap_style, join_style);
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
238
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
239 /* set dashes */
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
240 XSetDashes(display, *gc, dash_offset, dash_list, list_length);
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
241 }
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
242
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
243 load_font(font_info)
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
244 XFontStruct **font_info;
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
245 {
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
246 char *fontname = "9x15";
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
247
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
248 /* Load font and get font information structure. */
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
249 if ((*font_info = XLoadQueryFont(display,fontname)) == NULL)
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
250 {
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
251 (void) fprintf( stderr, "%s: Cannot open 9x15 font\n",
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
252 progname);
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
253 exit( -1 );
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
254 }
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
255 }
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
256
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
257 draw_text(win, gc, font_info, win_width, win_height)
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
258 Window win;
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
259 GC gc;
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
260 XFontStruct *font_info;
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
261 unsigned int win_width, win_height;
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
262 {
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
263 char *string1 = "Hi! I'm a window, who are you?";
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
264 char *string2 = "To terminate program; Press any key";
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
265 char *string3 = "or button while in this window.";
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
266 char *string4 = "Screen Dimensions:";
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
267 int len1, len2, len3, len4;
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
268 int width1, width2, width3;
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
269 char cd_height[50], cd_width[50], cd_depth[50];
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
270 int font_height;
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
271 int initial_y_offset, x_offset;
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
272
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
273
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
274 /* need length for both XTextWidth and XDrawString */
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
275 len1 = strlen(string1);
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
276 len2 = strlen(string2);
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
277 len3 = strlen(string3);
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
278
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
279 /* get string widths for centering */
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
280 width1 = XTextWidth(font_info, string1, len1);
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
281 width2 = XTextWidth(font_info, string2, len2);
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
282 width3 = XTextWidth(font_info, string3, len3);
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
283
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
284 font_height = font_info->ascent + font_info->descent;
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
285
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
286 /* output text, centered on each line */
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
287 XDrawString(display, win, gc, (win_width - width1)/2,
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
288 font_height,
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
289 string1, len1);
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
290 XDrawString(display, win, gc, (win_width - width2)/2,
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
291 (int)(win_height - (2 * font_height)),
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
292 string2, len2);
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
293 XDrawString(display, win, gc, (win_width - width3)/2,
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
294 (int)(win_height - font_height),
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
295 string3, len3);
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
296
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
297 /* copy numbers into string variables */
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
298 (void) sprintf(cd_height, " Height - %d pixels",
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
299 DisplayHeight(display,screen_num));
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
300 (void) sprintf(cd_width, " Width - %d pixels",
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
301 DisplayWidth(display,screen_num));
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
302 (void) sprintf(cd_depth, " Depth - %d plane(s)",
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
303 DefaultDepth(display, screen_num));
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
304
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
305 /* reuse these for same purpose */
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
306 len4 = strlen(string4);
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
307 len1 = strlen(cd_height);
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
308 len2 = strlen(cd_width);
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
309 len3 = strlen(cd_depth);
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
310
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
311 /* To center strings vertically, we place the first string
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
312 * so that the top of it is two font_heights above the center
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
313 * of the window. Since the baseline of the string is what we
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
314 * need to locate for XDrawString, and the baseline is one
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
315 * font_info->ascent below the top of the character,
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
316 * the final offset of the origin up from the center of the
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
317 * window is one font_height + one descent. */
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
318
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
319 initial_y_offset = win_height/2 - font_height - font_info->descent;
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
320 x_offset = (int) win_width/4;
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
321 XDrawString(display, win, gc, x_offset, (int) initial_y_offset,
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
322 string4,len4);
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
323
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
324 XDrawString(display, win, gc, x_offset, (int) initial_y_offset +
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
325 font_height,cd_height,len1);
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
326 XDrawString(display, win, gc, x_offset, (int) initial_y_offset +
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
327 2 * font_height,cd_width,len2);
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
328 XDrawString(display, win, gc, x_offset, (int) initial_y_offset +
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
329 3 * font_height,cd_depth,len3);
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
330 }
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
331
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
332 draw_graphics(win, gc, window_width, window_height)
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
333 Window win;
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
334 GC gc;
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
335 unsigned int window_width, window_height;
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
336 {
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
337 int x, y;
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
338 int width, height;
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
339
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
340 height = window_height/2;
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
341 width = 3 * window_width/4;
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
342 x = window_width/2 - width/2; /* center */
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
343 y = window_height/2 - height/2;
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
344 XDrawRectangle(display, win, gc, x, y, width, height);
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
345 }
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
346
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
347 TooSmall(win, gc, font_info)
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
348 Window win;
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
349 GC gc;
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
350 XFontStruct *font_info;
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
351 {
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
352 char *string1 = "Too Small";
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
353 int y_offset, x_offset;
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
354
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
355 y_offset = font_info->ascent + 2;
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
356 x_offset = 2;
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
357
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
358 /* output text, centered on each line */
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
359 XDrawString(display, win, gc, x_offset, y_offset, string1,
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
360 strlen(string1));
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents:
diff changeset
361 }