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