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