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