2 * Copyright 1989 O'Reilly and Associates, Inc.
3 * See ../Copyright for complete rights and liability information.
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. */
22 static char *progname; /* name this program was invoked by */
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;
39 XFontStruct *font_info;
40 char *display_name = NULL;
41 int window_size = BIG_ENOUGH; /* or TOO_SMALL to display contents */
45 /* connect to X server */
46 if ((display = XOpenDisplay(display_name)) == NULL) {
47 (void)fprintf(stderr, "%s: cannot connect to X server %s\n",
48 progname, XDisplayName(display_name));
52 /* get screen size from display structure macro */
53 screen_num = DefaultScreen(display);
54 display_width = DisplayWidth(display, screen_num);
55 display_height = DisplayHeight(display, screen_num);
57 /* Note that in a real application, x and y would default to 0
58 * but would be settable from the command line or resource database.
62 /* size window with enough room for text */
63 width = display_width / 2, height = display_height / 3;
65 /* ------------------------------------------------------------ */
67 /* create top-level window */
68 topwin = XCreateSimpleWindow(display, RootWindow(display, screen_num),
69 x, y, width, 2 * height, border_width,
70 BlackPixel(display, screen_num),
71 WhitePixel(display, screen_num));
73 /* Set size hints for window manager. The window manager may
74 * override these settings. Note that in a real
75 * application if size or position were set by the user
76 * the flags would be UPosition and USize, and these would
77 * override the window manager's preferences for this window. */
79 /* x, y, width, and height hints are now taken from
80 * the actual settings of the window when mapped. Note
81 * that PPosition and PSize must be specified anyway. */
83 size_hints.flags = PPosition | PSize | PMinSize;
84 size_hints.min_width = 300;
85 size_hints.min_height = 200;
89 XClassHint class_hints;
91 /* format of the window name and icon name
92 * arguments has changed in R4 */
93 XTextProperty windowName, iconName;
95 /* These calls store window_name and icon_name into
96 * XTextProperty structures and set their other
98 if (XStringListToTextProperty(&window_name, 1, &windowName) ==
100 (void)fprintf(stderr,
101 "%s: structure allocation for windowName failed.\n",
106 if (XStringListToTextProperty(&icon_name, 1, &iconName) == 0) {
107 (void)fprintf(stderr,
108 "%s: structure allocation for iconName failed.\n",
113 wm_hints.initial_state = NormalState;
114 wm_hints.input = True;
115 wm_hints.flags = StateHint | InputHint;
117 class_hints.res_name = progname;
118 class_hints.res_class = "Basicwin";
120 XSetWMProperties(display, topwin, &windowName, &iconName,
121 argv, argc, &size_hints, &wm_hints,
125 /* ------------------------------------------------------------ */
127 /* create the window we're in charge of */
129 win = XCreateSimpleWindow(display, topwin,
130 x, y, width, height, border_width,
131 BlackPixel(display, screen_num),
132 WhitePixel(display, screen_num));
134 /* Select event types wanted */
135 XSelectInput(display, win, ExposureMask | KeyPressMask |
136 ButtonPressMask | StructureNotifyMask);
138 load_font(&font_info);
140 /* create GC for text and drawing */
141 getGC(win, &gc, font_info);
143 /* ------------------------------------------------------------ */
145 /* create the external-client window */
147 win2 = XCreateSimpleWindow(display, topwin,
148 x, y + height, width, height, border_width,
149 BlackPixel(display, screen_num),
150 WhitePixel(display, screen_num));
151 printf("external window: %d\n", win2);
152 ExternalClientInitialize(display, win2);
154 /* ------------------------------------------------------------ */
156 /* Display windows */
157 XMapWindow(display, topwin);
158 XMapWindow(display, win);
159 XMapWindow(display, win2);
161 /* get events, use first to display text and graphics */
163 XNextEvent(display, &report);
164 if (report.xany.window == win2)
165 ExternalClientEventHandler(display, win2, &report);
167 switch (report.type) {
169 /* unless this is the last contiguous expose,
170 * don't draw the window */
171 if (report.xexpose.count != 0)
174 /* if window too small to use */
175 if (window_size == TOO_SMALL)
176 TooSmall(win, gc, font_info);
178 /* place text in window */
179 draw_text(win, gc, font_info, width,
182 /* place graphics in window, */
183 draw_graphics(win, gc, width, height);
186 case ConfigureNotify:
187 /* window has been resized, change width and
188 * height to send to draw_text and draw_graphics
190 width = report.xconfigure.width;
191 height = report.xconfigure.height;
192 if ((width < size_hints.min_width) ||
193 (height < size_hints.min_height))
194 window_size = TOO_SMALL;
196 window_size = BIG_ENOUGH;
199 /* trickle down into KeyPress (no break) */
201 XUnloadFont(display, font_info->fid);
202 XFreeGC(display, gc);
203 XCloseDisplay(display);
206 /* all events selected by StructureNotifyMask
207 * except ConfigureNotify are thrown away here,
208 * since nothing is done with them */
214 getGC(win, gc, font_info)
217 XFontStruct *font_info;
219 unsigned long valuemask = 0; /* ignore XGCvalues and use defaults */
221 unsigned int line_width = 6;
222 int line_style = LineOnOffDash;
223 int cap_style = CapRound;
224 int join_style = JoinRound;
226 static char dash_list[] = { 12, 24 };
229 /* Create default Graphics Context */
230 *gc = XCreateGC(display, win, valuemask, &values);
233 XSetFont(display, *gc, font_info->fid);
235 /* specify black foreground since default window background is
236 * white and default foreground is undefined. */
237 XSetForeground(display, *gc, BlackPixel(display, screen_num));
239 /* set line attributes */
240 XSetLineAttributes(display, *gc, line_width, line_style,
241 cap_style, join_style);
244 XSetDashes(display, *gc, dash_offset, dash_list, list_length);
248 load_font(XFontStruct **font_info)
250 char *fontname = "9x15";
252 /* Load font and get font information structure. */
253 if ((*font_info = XLoadQueryFont(display, fontname)) == NULL) {
254 (void)fprintf(stderr, "%s: Cannot open 9x15 font\n", progname);
260 draw_text(Window win, GC gc, XFontStruct *font_info, unsigned int win_width, unsigned int win_height)
262 char *string1 = "Hi! I'm a window, who are you?";
263 char *string2 = "To terminate program; Press any key";
264 char *string3 = "or button while in this window.";
265 char *string4 = "Screen Dimensions:";
266 int len1, len2, len3, len4;
267 int width1, width2, width3;
268 char cd_height[50], cd_width[50], cd_depth[50];
270 int initial_y_offset, x_offset;
273 /* need length for both XTextWidth and XDrawString */
274 len1 = strlen(string1);
275 len2 = strlen(string2);
276 len3 = strlen(string3);
278 /* get string widths for centering */
279 width1 = XTextWidth(font_info, string1, len1);
280 width2 = XTextWidth(font_info, string2, len2);
281 width3 = XTextWidth(font_info, string3, len3);
283 font_height = font_info->ascent + font_info->descent;
285 /* output text, centered on each line */
286 XDrawString(display, win, gc, (win_width - width1) / 2,
287 font_height, string1, len1);
288 XDrawString(display, win, gc, (win_width - width2) / 2,
289 (int)(win_height - (2 * font_height)), string2, len2);
290 XDrawString(display, win, gc, (win_width - width3) / 2,
291 (int)(win_height - font_height), string3, len3);
293 /* copy numbers into string variables */
294 sz = snprintf(cd_height, sizeof(cd_height), " Height - %d pixels",
295 DisplayHeight(display, screen_num));
296 assert(sz>=0 && sz<sizeof(cd_height));
298 sz = snprintf(cd_width, sizeof(cd_width), " Width - %d pixels",
299 DisplayWidth(display, screen_num));
300 assert(sz>=0 && sz<sizeof(cd_width));
302 sz = snprintf(cd_depth, sizeof(cd_depth), " Depth - %d plane(s)",
303 DefaultDepth(display, screen_num));
304 assert(sz>=0 && sz<sizeof(cd_depth));
306 /* reuse these for same purpose */
307 len4 = strlen(string4);
308 len1 = strlen(cd_height);
309 len2 = strlen(cd_width);
310 len3 = strlen(cd_depth);
312 /* To center strings vertically, we place the first string
313 * so that the top of it is two font_heights above the center
314 * of the window. Since the baseline of the string is what we
315 * need to locate for XDrawString, and the baseline is one
316 * font_info->ascent below the top of the character,
317 * the final offset of the origin up from the center of the
318 * window is one font_height + one descent. */
320 initial_y_offset = win_height / 2 - font_height - font_info->descent;
321 x_offset = (int)win_width / 4;
322 XDrawString(display, win, gc, x_offset, (int)initial_y_offset,
325 XDrawString(display, win, gc, x_offset, (int)initial_y_offset +
326 font_height, cd_height, len1);
327 XDrawString(display, win, gc, x_offset, (int)initial_y_offset +
328 2 * font_height, cd_width, len2);
329 XDrawString(display, win, gc, x_offset, (int)initial_y_offset +
330 3 * font_height, cd_depth, len3);
333 draw_graphics(win, gc, window_width, window_height)
336 unsigned int window_width, window_height;
341 height = window_height / 2;
342 width = 3 * window_width / 4;
343 x = window_width / 2 - width / 2; /* center */
344 y = window_height / 2 - height / 2;
345 XDrawRectangle(display, win, gc, x, y, width, height);
348 TooSmall(win, gc, font_info)
351 XFontStruct *font_info;
353 char *string1 = "Too Small";
354 int y_offset, x_offset;
356 y_offset = font_info->ascent + 2;
359 /* output text, centered on each line */
360 XDrawString(display, win, gc, x_offset, y_offset, string1,