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