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.ovl.c < prev    next >
C/C++ Source or Header  |  1989-12-08  |  10KB  |  369 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. #define CANNOT_OVERLAY 0
  15. #define CAN_OVERLAY 1
  16.  
  17. /* Display and screen_num are used as arguments to nearly every Xlib routine, 
  18.  * so it simplifies routine calls to declare them global.  If there were 
  19.  * additional source files, these variables would be declared extern in
  20.  * them. */
  21. Display *display;
  22. int screen_num;
  23. extern int get_colors();
  24.  
  25. /* pixel values */
  26. unsigned long foreground, background_pixel, overlay_pixel_1, overlay_pixel_2;
  27. /* passed back from get_color */
  28. unsigned long overlay_plane_mask;
  29.  
  30. /* values for window_size in main, is window big enough to be useful? */
  31. #define SMALL 1
  32. #define OK 0
  33.  
  34. void main(argc, argv)
  35. int argc;
  36. char **argv;
  37. {
  38.     Window win;
  39.     unsigned int width, height, x, y;     /* window size and position */
  40.     unsigned int borderwidth = 4;          /* four pixels */
  41.     unsigned int display_width, display_height;
  42.     char *window_name = "Basic Window Program";
  43.     char *icon_name = "basicwin";
  44.     Pixmap icon_pixmap;
  45.     XSizeHints size_hints;
  46.     XEvent report;
  47.     GC draw_gc, overlay_gc, clear_gc;
  48.     XFontStruct *font_info;
  49.     char *display_name = NULL;
  50.     int window_size = 0;    /* OK, or too SMALL to display contents */
  51.  
  52.     /* connect to X server */
  53.  
  54.     if ( (display=XOpenDisplay(display_name)) == NULL )
  55.     {
  56.         (void) fprintf( stderr, 
  57.             "basicwin: cannot connect to X server %s\\n",
  58.             XDisplayName(display_name));
  59.         exit( -1 );
  60.     }
  61.  
  62.     XSynchronize(display, True);
  63.  
  64.     /* get screen_num size from display structure macro */
  65.     screen_num = DefaultScreen(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.     printf("going to getting color\n");
  76.     if (get_colors() == CANNOT_OVERLAY) {
  77.         fprintf(stderr, "basic: can't allocate colors for overlay.\n");
  78.         exit(1);
  79.     }
  80.     printf("back from getting color\n");
  81.  
  82.     /* create opaque window */
  83.     win = XCreateSimpleWindow(display, RootWindow(display,screen_num), x, y, 
  84.             width, height, borderwidth, BlackPixel(display, screen_num),
  85.                 background_pixel);
  86.  
  87.     /* Create pixmap of depth 1 (bitmap) for icon */
  88.     icon_pixmap = XCreateBitmapFromData(display, win, icon_bitmap_bits, 
  89.             icon_bitmap_width, icon_bitmap_height);
  90.  
  91.     /* Set resize hints */
  92.     size_hints.flags = PPosition | PSize | PMinSize;
  93.     size_hints.x = x;
  94.     size_hints.y = y;
  95.     size_hints.width = width;
  96.     size_hints.height = height;
  97.     size_hints.min_width = 350;
  98.     size_hints.min_height = 250;
  99.  
  100.     /* set Properties for window manager (always before mapping) */
  101.     XSetStandardProperties(display, win, window_name, icon_name, 
  102.         icon_pixmap, argv, argc, &size_hints);
  103.  
  104.     /* Select event types wanted */
  105.     XSelectInput(display, win, ExposureMask | KeyPressMask | 
  106.             ButtonPressMask | ButtonReleaseMask | StructureNotifyMask);
  107.  
  108.     load_font(&font_info);
  109.  
  110.     /* create GC for text and drawing */
  111.     get_GC(win, &draw_gc, font_info);
  112.  
  113.     /* create GC for overlays */
  114.     get_GC2(win, &overlay_gc);
  115.     get_GC3(win, &clear_gc);
  116.  
  117.     /* Display window */
  118.     XMapWindow(display, win);
  119.  
  120.     /* get events, use first to display text and graphics */
  121.     while (1)  {
  122.         XNextEvent(display, &report);
  123.         switch  (report.type) {
  124.         case Expose:
  125.             /* get all other Expose events on the queue */
  126.             while (XCheckTypedEvent(display, Expose, &report));
  127.             if (window_size == SMALL)
  128.                    TooSmall(win, draw_gc, font_info);
  129.             else {
  130.                 /* place text in window */
  131.                    place_text(win, draw_gc, font_info, width, height);
  132.  
  133.                 /* place graphics in window, */
  134.                    place_graphics(win, draw_gc, width, height);
  135.  
  136.                 /* overlay rectangle over whole window */
  137.             }
  138.             break;
  139.         case ConfigureNotify:
  140.             /* window has been resized, change width and
  141.              * height to send to place_text and place_graphics
  142.              * in next Expose */
  143.             width = report.xconfigure.width;
  144.             height = report.xconfigure.height;
  145.             if ((width < size_hints.min_width) || 
  146.                     (height < size_hints.min_height))
  147.                 window_size = SMALL;
  148.             else
  149.                 window_size = OK;
  150.             break;
  151.         case ButtonPress:
  152.             place_overlay(win, overlay_gc, width, height);
  153.             break;
  154.         case ButtonRelease:
  155.             remove_overlay(win, clear_gc, width, height);
  156.             break;
  157.         case KeyPress:
  158.             XUnloadFont(display, font_info->fid);
  159.             XFreeGC(display, draw_gc);
  160.             XFreeGC(display, overlay_gc);
  161.             XFreeGC(display, clear_gc);
  162.             XCloseDisplay(display);
  163.             exit(1);
  164.         default:
  165.             /* all events selected by StructureNotifyMask
  166.              * except ConfigureNotify are thrown away here,
  167.              * since nothing is done with them */
  168.             break;
  169.         } /* end switch */
  170.     } /* end while */
  171. }
  172.  
  173. get_GC3(win, gc)
  174. Window win;
  175. GC *gc;
  176. {
  177.     /* Create default Graphics Context */
  178.     *gc = XCreateGC(display, win, 0, NULL);
  179.  
  180.     XSetFunction(display, *gc, GXclear);
  181.     XSetPlaneMask(display, *gc, overlay_plane_mask);
  182. }
  183.  
  184. get_GC2(win, gc)
  185. Window win;
  186. GC *gc;
  187. {
  188.     /* Create default Graphics Context */
  189.     *gc = XCreateGC(display, win, 0, NULL);
  190.  
  191.     XSetForeground(display, *gc, overlay_pixel_1);
  192.     XSetPlaneMask(display, *gc, overlay_plane_mask);
  193. }
  194.  
  195. get_GC(win, gc, font_info)
  196. Window win;
  197. GC *gc;
  198. XFontStruct *font_info;
  199. {
  200.     unsigned int line_width = 6;
  201.     int line_style = LineOnOffDash;
  202.     int cap_style = CapRound;
  203.     int join_style = JoinRound;
  204.     int dash_offset = 0;
  205.     static char dash_list[] = {
  206.         12, 24    };
  207.     int list_length = 2;
  208.  
  209.     /* Create default Graphics Context */
  210.     *gc = XCreateGC(display, win, 0, NULL);
  211.  
  212.     /* specify font */
  213.     XSetFont(display, *gc, font_info->fid);
  214.  
  215.     XSetForeground(display, *gc, foreground);
  216.  
  217.     /* set line attributes */
  218.     XSetLineAttributes(display, *gc, line_width, line_style, cap_style, 
  219.             join_style);
  220.  
  221.     /* set dashes to be line_width in length */
  222.     XSetDashes(display, *gc, dash_offset, dash_list, list_length);
  223. }
  224.  
  225. load_font(font_info)
  226. XFontStruct **font_info;
  227. {
  228.     char *fontname = "9x15";
  229.  
  230.     /* Access font */
  231.     if ((*font_info = XLoadQueryFont(display,fontname)) == NULL)
  232.     {
  233.         (void) fprintf( stderr, "Basic: Cannot open 9x15 font\\n");
  234.         exit( -1 );
  235.     }
  236. }
  237.  
  238. place_text(win, gc, font_info, win_width, win_height)
  239. Window win;
  240. GC gc;
  241. XFontStruct *font_info;
  242. unsigned int win_width, win_height;
  243. {
  244.     int y = 20;     /* offset from corner of window*/
  245.     char *string1 = "Hi! I'm a window, who are you?";
  246.     char *string2 = "To terminate program; Press any key";
  247.     char *string3 = "or button while in this window.";
  248.     char *string4 = "Screen Dimensions:";
  249.     int len1, len2, len3, len4;
  250.     int width1, width2, width3;
  251.     char cd_height[50], cd_width[50], cd_depth[50];
  252.     int font_height;
  253.     int initial_y_offset, x_offset;
  254.  
  255.  
  256.     /* need length for both XTextWidth and XDrawString */
  257.     len1 = strlen(string1);
  258.     len2 = strlen(string2);
  259.     len3 = strlen(string3);
  260.  
  261.     /* get string widths for centering */
  262.     width1 = XTextWidth(font_info, string1, len1);
  263.     width2 = XTextWidth(font_info, string2, len2);
  264.     width3 = XTextWidth(font_info, string3, len3);
  265.  
  266.     /* output text, centered on each line */
  267.     XDrawString(display,win,gc,(win_width - width1)/2,y,string1,len1);
  268.     XDrawString(display,win,gc,(win_width - width2)/2, 
  269.             (int)(win_height - 35),string2,len2);
  270.     XDrawString(display,win,gc,(win_width - width3)/2, 
  271.             (int)(win_height - 15),string3,len3);
  272.  
  273.     /* copy numbers into string variables */
  274.     (void) sprintf(cd_height, " Height - %d pixels", 
  275.             DisplayHeight(display,screen_num));
  276.     (void) sprintf(cd_width, " Width  - %d pixels", 
  277.             DisplayWidth(display,screen_num));
  278.     (void) sprintf(cd_depth, " Depth  - %d plane(s)", 
  279.             DefaultDepth(display, screen_num));
  280.  
  281.     /* reuse these for same purpose */
  282.     len4 = strlen(string4);
  283.     len1 = strlen(cd_height);
  284.     len2 = strlen(cd_width);
  285.     len3 = strlen(cd_depth);
  286.  
  287.     font_height = font_info->max_bounds.ascent + 
  288.             font_info->max_bounds.descent;
  289.  
  290.     /* To center strings vertically, we place the first string
  291.      * so that the top of it is two font_heights above the center
  292.      * of the window.  Since the baseline of the string is what we
  293.      * need to locate for XDrawString, and the baseline is one
  294.      * font_info->max_bounds.ascent below the top of the chacter,
  295.      * the final offset of the origin up from the center of the 
  296.      * window is one font_height + one descent. */
  297.  
  298.     initial_y_offset = win_height/2 - font_height - 
  299.             font_info->max_bounds.descent;
  300.     x_offset = (int) win_width/4;
  301.     XDrawString(display, win, gc, x_offset, (int) initial_y_offset, 
  302.             string4,len4);
  303.  
  304.     XDrawString(display, win, gc, x_offset, (int) initial_y_offset + 
  305.             font_height,cd_height,len1);
  306.     XDrawString(display, win, gc, x_offset, (int) initial_y_offset + 
  307.             2 * font_height,cd_width,len2);
  308.     XDrawString(display, win, gc, x_offset, (int) initial_y_offset + 
  309.             3 * font_height,cd_depth,len3);
  310. }
  311.  
  312.  
  313. place_graphics(win, gc, window_width, window_height)
  314. Window win;
  315. GC gc;
  316. unsigned int window_width, window_height;
  317. {
  318.     int x, y;
  319.     int width, height;
  320.  
  321.     height = window_height/2;
  322.     width = 3 * window_width/4;
  323.     x = window_width/2 - width/2;  /* center */
  324.     y = window_height/2 - height/2;
  325.     XDrawRectangle(display, win, gc, x, y, width, height);
  326. }
  327.  
  328. TooSmall(win, gc, font_info)
  329. Window win;
  330. GC gc;
  331. XFontStruct *font_info;
  332. {
  333.     char *string1 = "Too Small";
  334.     int y_offset, x_offset;
  335.  
  336.     y_offset = font_info->max_bounds.ascent + 2;
  337.     x_offset = 2;
  338.  
  339.     /* output text, centered on each line */
  340.     XDrawString(display, win, gc, x_offset, y_offset, string1, 
  341.             strlen(string1));
  342. }
  343.  
  344. place_overlay(win, gc, window_width, window_height)
  345. Window win;
  346. GC gc;
  347. unsigned int window_width, window_height;
  348. {
  349.     int x, y;
  350.     int width, height;
  351.  
  352.     printf("placing overlay\n");
  353.     height = window_height/2;
  354.     width = 3 * window_width/4;
  355.     x = window_width/2 - width/2;  /* center */
  356.     y = window_height/2 - height/2;
  357.     XFillRectangle(display, win, gc, x, y, width, height);
  358. }
  359.  
  360. remove_overlay(win, gc, window_width, window_height)
  361. Window win;
  362. GC gc;
  363. unsigned int window_width, window_height;
  364. {
  365.     printf("clearing overlay\n");
  366.     /* this clears the overlay plane set in the gc */
  367.     XFillRectangle(display, win, gc, 0, 0, 10000, 10000);
  368. }
  369.