home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-385-Vol-1of3.iso / x / xlibpr3.zip / basicwin / getvis / basicwin.c next >
C/C++ Source or Header  |  1989-11-27  |  10KB  |  329 lines

  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. #include "../bitmaps/icon_bitmap"
  13. #define BITMAPDEPTH 1
  14.  
  15. /* Display and screen are used as arguments to nearly every Xlib routine, 
  16.  * so it simplifies routine calls to declare them global.  If there were 
  17.  * additional source files, these variables would be declared extern in
  18.  * them. */
  19. Display *display;
  20. int screen;
  21.  
  22. /* values for window_size in main, is window big enough to be useful? */
  23. #define SMALL 1
  24. #define OK 0
  25.  
  26. static char *vistype[] = {
  27. "StaticGray",
  28. "GrayScale",
  29. "StaticColor",
  30. "PseudoColor",
  31. "TrueColor",
  32. "DirectColor"
  33. };
  34.  
  35.  
  36. void main(argc, argv)
  37. int argc;
  38. char **argv;
  39. {
  40.     Window win;
  41.     unsigned int width, height, x, y;     /* window size and position */
  42.     unsigned int borderwidth = 4;          /* four pixels */
  43.     unsigned int display_width, display_height;
  44.     unsigned int icon_width, icon_height;
  45.     char *window_name = "Basic Window Program";
  46.     char *icon_name = "basicwin";
  47.     Pixmap icon_pixmap;
  48.     XSizeHints size_hints;
  49.     XEvent report;
  50.     GC gc;
  51.     XFontStruct *font_info;
  52.     char *display_name = NULL;
  53.     int window_size = 0;    /* OK, or too SMALL to display contents */
  54.     Visual *visual;
  55.     XStandardColormap stndcmap;
  56.  
  57.     /* connect to X server */
  58.  
  59.     if ( (display=XOpenDisplay(display_name)) == NULL )
  60.     {
  61.         (void) fprintf( stderr, 
  62.                 "basicwin: cannot connect to X server %s\\n",
  63.                 XDisplayName(display_name));
  64.         exit( -1 );
  65.     }
  66.  
  67.     /* get screen size from display structure macro */
  68.     screen = DefaultScreen(display);
  69.     display_width = DisplayWidth(display, screen);
  70.     display_height = DisplayHeight(display, screen);
  71.  
  72.     visual = DefaultVisual(display, screen);
  73.     printf("red mask: %d\ngreen mask: %d\nblue mask: %d\n", visual->red_mask, visual->blue_mask, visual->green_mask);
  74.     printf("default visual class: %s\n", vistype[visual->class]);
  75.     printf("bits per rgb: %d\n", visual->bits_per_rgb);
  76.     printf("number of colormap entries: %d\n", visual->map_entries);
  77.     
  78.     if (!XGetStandardColormap(display, RootWindow(display, screen), &stndcmap, XA_RGB_DEFAULT_MAP))
  79.         printf("no RGB_DEFAULT_MAP standard colormap defined.\n");
  80.     else {
  81.         printf("stnd colormap ID: %d\n", stndcmap.colormap);
  82.         printf("red_max: %d\n", stndcmap.red_max);
  83.         printf("blue_max: %d\n", stndcmap.blue_max);
  84.         printf("green_max: %d\n", stndcmap.green_max);
  85.         printf("red_mult: %d\n", stndcmap.red_mult);
  86.         printf("blue_mult: %d\n", stndcmap.blue_mult);
  87.         printf("green_mult: %d\n", stndcmap.green_mult);
  88.         printf("base pixel: %d\n", stndcmap.base_pixel);
  89.     }
  90.  
  91.     printf("default colormap ID %d\n", DefaultColormap(display, screen));
  92.  
  93.     /* place window */
  94.     x = display_width/3, y = display_height/3;
  95.  
  96.     /* size window with enough room for text */
  97.     width = display_width/3, height = display_height/4;
  98.  
  99.     /* create opaque window */
  100.     win = XCreateSimpleWindow(display, RootWindow(display,screen), x, y, 
  101.             width, height, borderwidth, BlackPixel(display,
  102.                 screen), WhitePixel(display,screen));
  103.  
  104.     /* Create pixmap of depth 1 (bitmap) for icon */
  105.     icon_pixmap = XCreateBitmapFromData(display, win, icon_bitmap_bits, 
  106.             icon_bitmap_width, icon_bitmap_height);
  107.  
  108.     /* Set resize hints */
  109.     size_hints.flags = PPosition | PSize | PMinSize;
  110.     size_hints.x = x;
  111.     size_hints.y = y;
  112.     size_hints.width = width;
  113.     size_hints.height = height;
  114.     size_hints.min_width = 350;
  115.     size_hints.min_height = 250;
  116.  
  117.     /* set Properties for window manager (always before mapping) */
  118.     XSetStandardProperties(display, win, window_name, icon_name, 
  119.         icon_pixmap, argv, argc, &size_hints);
  120.  
  121.     /* Select event types wanted */
  122.     XSelectInput(display, win, ExposureMask | KeyPressMask | 
  123.             ButtonPressMask | StructureNotifyMask);
  124.  
  125.     load_font(&font_info);
  126.  
  127.     /* create GC for text and drawing */
  128.     get_GC(win, &gc, font_info);
  129.  
  130.     /* Display window */
  131.     XMapWindow(display, win);
  132.  
  133.     /* get events, use first to display text and graphics */
  134.     while (1)  {
  135.         XNextEvent(display, &report);
  136.         switch  (report.type) {
  137.         case Expose:
  138.             /* get all other Expose events on the queue */
  139.             while (XCheckTypedEvent(display, Expose, &report));
  140.             if (window_size == SMALL)
  141.                    TooSmall(win, gc, font_info);
  142.             else {
  143.                 /* place text in window */
  144.                    place_text(win, gc, font_info, width, height);
  145.  
  146.                 /* place graphics in window, */
  147.                    place_graphics(win, gc, width, height);
  148.             }
  149.             break;
  150.         case ConfigureNotify:
  151.             /* window has been resized, change width and
  152.              * height to send to place_text and place_graphics
  153.              * in next Expose */
  154.             width = report.xconfigure.width;
  155.             height = report.xconfigure.height;
  156.             if ((width < size_hints.min_width) || 
  157.                     (height < size_hints.min_height))
  158.                 window_size = SMALL;
  159.             else
  160.                 window_size = OK;
  161.             break;
  162.         case ButtonPress:
  163.             /* trickle down into KeyPress (no break) */
  164.         case KeyPress:
  165.             XUnloadFont(display, font_info->fid);
  166.             XFreeGC(display, gc);
  167.             XCloseDisplay(display);
  168.             exit(1);
  169.         default:
  170.             /* all events selected by StructureNotifyMask
  171.              * except ConfigureNotify are thrown away here,
  172.              * since nothing is done with them */
  173.             break;
  174.         } /* end switch */
  175.     } /* end while */
  176. }
  177.  
  178. get_GC(win, gc, font_info)
  179. Window win;
  180. GC *gc;
  181. XFontStruct *font_info;
  182. {
  183.     unsigned long valuemask = 0; /* ignore XGCvalues and use defaults */
  184.     XGCValues values;
  185.     unsigned int line_width = 6;
  186.     int line_style = LineOnOffDash;
  187.     int cap_style = CapRound;
  188.     int join_style = JoinRound;
  189.     int dash_offset = 0;
  190.     static char dash_list[] = {
  191.         12, 24    };
  192.     int list_length = 2;
  193.  
  194.     /* Create default Graphics Context */
  195.     *gc = XCreateGC(display, win, valuemask, &values);
  196.  
  197.     /* specify font */
  198.     XSetFont(display, *gc, font_info->fid);
  199.  
  200.     /* specify black foreground since default may be white on white */
  201.     XSetForeground(display, *gc, BlackPixel(display,screen));
  202.  
  203.     /* set line attributes */
  204.     XSetLineAttributes(display, *gc, line_width, line_style, cap_style, 
  205.             join_style);
  206.  
  207.     /* set dashes to be line_width in length */
  208.     XSetDashes(display, *gc, dash_offset, dash_list, list_length);
  209. }
  210.  
  211. load_font(font_info)
  212. XFontStruct **font_info;
  213. {
  214.     char *fontname = "9x15";
  215.  
  216.     /* Access font */
  217.     if ((*font_info = XLoadQueryFont(display,fontname)) == NULL)
  218.     {
  219.         (void) fprintf( stderr, "Basic: Cannot open 9x15 font\\n");
  220.         exit( -1 );
  221.     }
  222. }
  223.  
  224. place_text(win, gc, font_info, win_width, win_height)
  225. Window win;
  226. GC gc;
  227. XFontStruct *font_info;
  228. unsigned int win_width, win_height;
  229. {
  230.     int y = 20;     /* offset from corner of window*/
  231.     char *string1 = "Hi! I'm a window, who are you?";
  232.     char *string2 = "To terminate program; Press any key";
  233.     char *string3 = "or button while in this window.";
  234.     char *string4 = "Screen Dimensions:";
  235.     int len1, len2, len3, len4;
  236.     int width1, width2, width3;
  237.     char cd_height[50], cd_width[50], cd_depth[50];
  238.     int font_height;
  239.     int initial_y_offset, x_offset;
  240.  
  241.  
  242.     /* need length for both XTextWidth and XDrawString */
  243.     len1 = strlen(string1);
  244.     len2 = strlen(string2);
  245.     len3 = strlen(string3);
  246.  
  247.     /* get string widths for centering */
  248.     width1 = XTextWidth(font_info, string1, len1);
  249.     width2 = XTextWidth(font_info, string2, len2);
  250.     width3 = XTextWidth(font_info, string3, len3);
  251.  
  252.     /* output text, centered on each line */
  253.     XDrawString(display,win,gc,(win_width - width1)/2,y,string1,len1);
  254.     XDrawString(display,win,gc,(win_width - width2)/2, 
  255.             (int)(win_height - 35),string2,len2);
  256.     XDrawString(display,win,gc,(win_width - width3)/2, 
  257.             (int)(win_height - 15),string3,len3);
  258.  
  259.     /* copy numbers into string variables */
  260.     (void) sprintf(cd_height, " Height - %d pixels", 
  261.             DisplayHeight(display,screen));
  262.     (void) sprintf(cd_width, " Width  - %d pixels", 
  263.             DisplayWidth(display,screen));
  264.     (void) sprintf(cd_depth, " Depth  - %d plane(s)", 
  265.             DefaultDepth(display, screen));
  266.  
  267.     /* reuse these for same purpose */
  268.     len4 = strlen(string4);
  269.     len1 = strlen(cd_height);
  270.     len2 = strlen(cd_width);
  271.     len3 = strlen(cd_depth);
  272.  
  273.     font_height = font_info->max_bounds.ascent + 
  274.             font_info->max_bounds.descent;
  275.  
  276.     /* To center strings vertically, we place the first string
  277.      * so that the top of it is two font_heights above the center
  278.      * of the window.  Since the baseline of the string is what we
  279.      * need to locate for XDrawString, and the baseline is one
  280.      * font_info->max_bounds.ascent below the top of the chacter,
  281.      * the final offset of the origin up from the center of the 
  282.      * window is one font_height + one descent. */
  283.  
  284.     initial_y_offset = win_height/2 - font_height - 
  285.             font_info->max_bounds.descent;
  286.     x_offset = (int) win_width/4;
  287.     XDrawString(display, win, gc, x_offset, (int) initial_y_offset, 
  288.             string4,len4);
  289.  
  290.     XDrawString(display, win, gc, x_offset, (int) initial_y_offset + 
  291.             font_height,cd_height,len1);
  292.     XDrawString(display, win, gc, x_offset, (int) initial_y_offset + 
  293.             2 * font_height,cd_width,len2);
  294.     XDrawString(display, win, gc, x_offset, (int) initial_y_offset + 
  295.             3 * font_height,cd_depth,len3);
  296. }
  297.  
  298.  
  299. place_graphics(win, gc, window_width, window_height)
  300. Window win;
  301. GC gc;
  302. unsigned int window_width, window_height;
  303. {
  304.     int x, y;
  305.     int width, height;
  306.  
  307.     height = window_height/2;
  308.     width = 3 * window_width/4;
  309.     x = window_width/2 - width/2;  /* center */
  310.     y = window_height/2 - height/2;
  311.     XDrawRectangle(display, win, gc, x, y, width, height);
  312. }
  313.  
  314. TooSmall(win, gc, font_info)
  315. Window win;
  316. GC gc;
  317. XFontStruct *font_info;
  318. {
  319.     char *string1 = "Too Small";
  320.     int y_offset, x_offset;
  321.  
  322.     y_offset = font_info->max_bounds.ascent + 2;
  323.     x_offset = 2;
  324.  
  325.     /* output text, centered on each line */
  326.     XDrawString(display, win, gc, x_offset, y_offset, string1, 
  327.             strlen(string1));
  328. }
  329.