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 )
48 (void) fprintf( stderr, "%s: cannot connect to X server %s\n",
49 progname, XDisplayName(display_name));
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);
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.
63 /* size window with enough room for text */
64 width = display_width/2, height = display_height/3;
66 /* ------------------------------------------------------------ */
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));
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. */
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. */
84 size_hints.flags = PPosition | PSize | PMinSize;
85 size_hints.min_width = 300;
86 size_hints.min_height = 200;
90 XClassHint class_hints;
92 /* format of the window name and icon name
93 * arguments has changed in R4 */
94 XTextProperty windowName, iconName;
96 /* These calls store window_name and icon_name into
97 * XTextProperty structures and set their other
99 if (XStringListToTextProperty(&window_name, 1, &windowName) == 0) {
100 (void) fprintf( stderr, "%s: structure allocation for windowName failed.\n",
105 if (XStringListToTextProperty(&icon_name, 1, &iconName) == 0) {
106 (void) fprintf( stderr, "%s: structure allocation for iconName failed.\n",
111 wm_hints.initial_state = NormalState;
112 wm_hints.input = True;
113 wm_hints.flags = StateHint | InputHint;
115 class_hints.res_name = progname;
116 class_hints.res_class = "Basicwin";
118 XSetWMProperties(display, topwin, &windowName, &iconName,
119 argv, argc, &size_hints, &wm_hints,
123 /* ------------------------------------------------------------ */
125 /* create the window we're in charge of */
127 win = XCreateSimpleWindow(display, topwin,
128 x, y, width, height, border_width, BlackPixel(display,
129 screen_num), WhitePixel(display,screen_num));
131 /* Select event types wanted */
132 XSelectInput(display, win, ExposureMask | KeyPressMask |
133 ButtonPressMask | StructureNotifyMask);
135 load_font(&font_info);
137 /* create GC for text and drawing */
138 getGC(win, &gc, font_info);
140 /* ------------------------------------------------------------ */
142 /* create the external-client window */
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);
151 /* ------------------------------------------------------------ */
153 /* Display windows */
154 XMapWindow(display, topwin);
155 XMapWindow(display, win);
156 XMapWindow(display, win2);
158 /* get events, use first to display text and graphics */
160 XNextEvent(display, &report);
161 if (report.xany.window == win2)
162 ExternalClientEventHandler(display, win2, &report);
164 switch (report.type) {
166 /* unless this is the last contiguous expose,
167 * don't draw the window */
168 if (report.xexpose.count != 0)
171 /* if window too small to use */
172 if (window_size == TOO_SMALL)
173 TooSmall(win, gc, font_info);
175 /* place text in window */
176 draw_text(win, gc, font_info, width, height);
178 /* place graphics in window, */
179 draw_graphics(win, gc, width, height);
182 case ConfigureNotify:
183 /* window has been resized, change width and
184 * height to send to draw_text and draw_graphics
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;
192 window_size = BIG_ENOUGH;
195 /* trickle down into KeyPress (no break) */
197 XUnloadFont(display, font_info->fid);
198 XFreeGC(display, gc);
199 XCloseDisplay(display);
202 /* all events selected by StructureNotifyMask
203 * except ConfigureNotify are thrown away here,
204 * since nothing is done with them */
210 getGC(win, gc, font_info)
213 XFontStruct *font_info;
215 unsigned long valuemask = 0; /* ignore XGCvalues and use defaults */
217 unsigned int line_width = 6;
218 int line_style = LineOnOffDash;
219 int cap_style = CapRound;
220 int join_style = JoinRound;
222 static char dash_list[] = {12, 24};
225 /* Create default Graphics Context */
226 *gc = XCreateGC(display, win, valuemask, &values);
229 XSetFont(display, *gc, font_info->fid);
231 /* specify black foreground since default window background is
232 * white and default foreground is undefined. */
233 XSetForeground(display, *gc, BlackPixel(display,screen_num));
235 /* set line attributes */
236 XSetLineAttributes(display, *gc, line_width, line_style,
237 cap_style, join_style);
240 XSetDashes(display, *gc, dash_offset, dash_list, list_length);
244 XFontStruct **font_info;
246 char *fontname = "9x15";
248 /* Load font and get font information structure. */
249 if ((*font_info = XLoadQueryFont(display,fontname)) == NULL)
251 (void) fprintf( stderr, "%s: Cannot open 9x15 font\n",
257 draw_text(win, gc, font_info, win_width, win_height)
260 XFontStruct *font_info;
261 unsigned int win_width, win_height;
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];
271 int initial_y_offset, x_offset;
274 /* need length for both XTextWidth and XDrawString */
275 len1 = strlen(string1);
276 len2 = strlen(string2);
277 len3 = strlen(string3);
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);
284 font_height = font_info->ascent + font_info->descent;
286 /* output text, centered on each line */
287 XDrawString(display, win, gc, (win_width - width1)/2,
290 XDrawString(display, win, gc, (win_width - width2)/2,
291 (int)(win_height - (2 * font_height)),
293 XDrawString(display, win, gc, (win_width - width3)/2,
294 (int)(win_height - font_height),
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));
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);
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. */
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,
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);
332 draw_graphics(win, gc, window_width, window_height)
335 unsigned int window_width, window_height;
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);
347 TooSmall(win, gc, font_info)
350 XFontStruct *font_info;
352 char *string1 = "Too Small";
353 int y_offset, x_offset;
355 y_offset = font_info->ascent + 2;
358 /* output text, centered on each line */
359 XDrawString(display, win, gc, x_offset, y_offset, string1,