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 / tiles / manytiles.c < prev    next >
C/C++ Source or Header  |  1990-08-08  |  9KB  |  385 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.  
  14. /* Display and screen are used as arguments to nearly every Xlib routine, 
  15.  * so it simplifies routine calls to declare them global.  If there were 
  16.  * additional source files, these variables would be declared extern in
  17.  * them. */
  18. Display *display;
  19. int screen;
  20.  
  21. char *ProgramName;
  22.  
  23. /* values for window_size in main, is window big enough to be useful? */
  24. #define SMALL 1
  25. #define OK 0
  26. #define NUMSTIPS  40
  27.  
  28. static char *filename[] = {
  29. "1x1",
  30. "2x2",
  31. "black",
  32. "boxes",
  33. "cntr_ptr",
  34. "cntr_ptrmsk",
  35. "cross_weave",
  36. "dimple1",
  37. "dimple3",
  38. "dot",
  39. "flagdown",
  40. "flagup",
  41. "flipped_gray",
  42. "gray",
  43. "gray1",
  44. "gray3",
  45. "icon",
  46. "left_ptr",
  47. "left_ptrmsk",
  48. "light_gray",
  49. "opendot",
  50. "opendotMask",
  51. "right_ptr",
  52. "right_ptrmsk",
  53. "root_weave",
  54. "scales",
  55. "sipb",
  56. "star",
  57. "starMask",
  58. "stipple",
  59. "target",
  60. "tie_fighter",
  61. "wide_weave",
  62. "weird_size",
  63. "wingdogs",
  64. "woman",
  65. "xfd_icon",
  66. "xlogo16",
  67. "xlogo32",
  68. "xlogo64"
  69. };
  70.  
  71. usage ()
  72. {
  73.     fprintf (stderr, 
  74.              "usage:  %s [-display host:server.screen] [-geometry geom]\n", ProgramName);
  75.     exit (1);
  76. }
  77.  
  78.  
  79. void main(argc, argv)
  80. int argc;
  81. char **argv;
  82. {
  83.     Window win;
  84.     unsigned int width, height, x, y;     /* window size and position */
  85.     unsigned int borderwidth = 4;          /* four pixels */
  86.     int i;   /* for counter */
  87.     unsigned int display_width, display_height;
  88.     unsigned int icon_width, icon_height;
  89.     char *window_name = "Basic Window Program";
  90.     char *icon_name = "basicwin";
  91.     Pixmap icon_pixmap;
  92.     Pixmap stipple[NUMSTIPS];
  93.     unsigned int stip_width[NUMSTIPS], stip_height[NUMSTIPS];
  94.     char fullfilename[256];
  95.     XSizeHints size_hints;
  96.     XEvent report;
  97.     GC gc;
  98.     XFontStruct *font_info;
  99.     char *display_name = NULL;
  100.     char *geom = NULL;
  101.  
  102.     int window_size = 0;    /* OK, or too SMALL to display contents */
  103.  
  104.     ProgramName = argv[0];
  105.     for (i = 1; i < argc; i++) {
  106.     char *arg = argv[i];
  107.     
  108.     if (arg[0] == '-') {
  109.     switch (arg[1]) {
  110.         case 'd':                 /* -display host:server.screen */
  111.             if (++i >= argc) usage ();
  112.             display_name = argv[i];
  113.             continue;
  114.         case 'g':                 /* -geometry geom */
  115.             if (++i >= argc) usage ();
  116.             geom = argv[i];
  117.             continue;
  118.         default:
  119.             usage ();
  120.         /* doesn't return */
  121.         }
  122.     } else
  123.         /* get bitmap file names here */
  124.         usage ();
  125.     }
  126.  
  127.     /* connect to X server */
  128.  
  129.     if ( (display=XOpenDisplay(display_name)) == NULL )
  130.     {
  131.         (void) fprintf( stderr, 
  132.                 "basicwin: cannot connect to X server %s\\n",
  133.                 XDisplayName(display_name));
  134.         exit( -1 );
  135.     }
  136.  
  137.  
  138.  
  139.     /* get screen size from display structure macro */
  140.     screen = DefaultScreen(display);
  141.     display_width = DisplayWidth(display, screen);
  142.     display_height = DisplayHeight(display, screen);
  143.  
  144.     /* place window */
  145.     x = 0, y = 0;
  146.  
  147.     /* set filenames */
  148.  
  149.     for (i = 0; i < NUMSTIPS; i++) {
  150.         strcpy(fullfilename, "/usr/include/X11/bitmaps/");
  151.         strcat(fullfilename, filename[i]);
  152.         if (create_read_stipple(&stipple[i], fullfilename, &stip_width[i], &stip_height[i]) != BitmapSuccess) {
  153.             fprintf(stderr, "basic: can't read bitmap %d\n",i);
  154.             exit(0);
  155.         }
  156.     }
  157.  
  158.     /* size window with enough room for text */
  159.     width = display_width/2, height = 4 * display_height/5;
  160.  
  161.     /* create opaque window */
  162.     win = XCreateSimpleWindow(display, RootWindow(display,screen), x, y, 
  163.             width, height, borderwidth, BlackPixel(display,
  164.                 screen), WhitePixel(display,screen));
  165.  
  166.  
  167.     /* Create pixmap of depth 1 (bitmap) for icon */
  168.     icon_pixmap = XCreateBitmapFromData(display, win, icon_bitmap_bits, 
  169.             icon_bitmap_width, icon_bitmap_height);
  170.  
  171.  
  172.     /* Set resize hints */
  173.     size_hints.flags = PPosition | PSize | PMinSize;
  174.     size_hints.x = x;
  175.     size_hints.y = y;
  176.     size_hints.width = width;
  177.     size_hints.height = height;
  178.     size_hints.min_width = 350;
  179.     size_hints.min_height = 250;
  180.  
  181.     /* set Properties for window manager (always before mapping) */
  182.     XSetStandardProperties(display, win, window_name, icon_name, 
  183.         icon_pixmap, argv, argc, &size_hints);
  184.  
  185.  
  186.     /* Select event types wanted */
  187.     XSelectInput(display, win, ExposureMask | KeyPressMask | 
  188.             ButtonPressMask | StructureNotifyMask);
  189.  
  190.     load_font(&font_info);
  191.  
  192.     /* create GC for text and drawing */
  193.     get_GC(win, &gc, font_info);
  194.  
  195.     /* Display window */
  196.     XMapWindow(display, win);
  197.  
  198.  
  199.     /* get events, use first to display text and graphics */
  200.     while (1)  {
  201.         XNextEvent(display, &report);
  202.         switch  (report.type) {
  203.         case Expose:
  204.             /* get all other Expose events on the queue */
  205.             while (XCheckTypedEvent(display, Expose, &report));
  206.             if (window_size == SMALL)
  207.                    TooSmall(win, gc, font_info);
  208.             else {
  209.                 /* place text in window */
  210.                    place_stips(win, gc, stipple, stip_width, stip_height, font_info, width, height);
  211.             }
  212.             break;
  213.         case ConfigureNotify:
  214.             /* window has been resized, change width and
  215.              * height to send to place_text and place_graphics
  216.              * in next Expose */
  217.             width = report.xconfigure.width;
  218.             height = report.xconfigure.height;
  219.             if ((width < size_hints.min_width) || 
  220.                     (height < size_hints.min_height))
  221.                 window_size = SMALL;
  222.             else
  223.                 window_size = OK;
  224.             break;
  225.         case ButtonPress:
  226.             /* trickle down into KeyPress (no break) */
  227.         case KeyPress:
  228.             XUnloadFont(display, font_info->fid);
  229.             XFreeGC(display, gc);
  230.             XCloseDisplay(display);
  231.             exit(1);
  232.         default:
  233.             /* all events selected by StructureNotifyMask
  234.              * except ConfigureNotify are thrown away here,
  235.              * since nothing is done with them */
  236.             break;
  237.         } /* end switch */
  238.     } /* end while */
  239. }
  240.  
  241. get_GC(win, gc, font_info)
  242. Window win;
  243. GC *gc;
  244. XFontStruct *font_info;
  245. {
  246.     unsigned long valuemask = 0; /* ignore XGCvalues and use defaults */
  247.     XGCValues values;
  248.     unsigned int line_width = 6;
  249.     int line_style = LineOnOffDash;
  250.     int cap_style = CapRound;
  251.     int join_style = JoinRound;
  252.     int dash_offset = 0;
  253.     static char dash_list[] = {
  254.         12, 24    };
  255.     int list_length = 2;
  256.  
  257.     /* Create default Graphics Context */
  258.     *gc = XCreateGC(display, win, valuemask, &values);
  259.  
  260.     /* specify font */
  261.     XSetFont(display, *gc, font_info->fid);
  262.  
  263.     /* specify black foreground since default may be white on white */
  264.     XSetForeground(display, *gc, BlackPixel(display,screen));
  265.     XSetBackground(display, *gc, WhitePixel(display,screen));
  266. }
  267.  
  268. load_font(font_info)
  269. XFontStruct **font_info;
  270. {
  271.     char *fontname = "9x15";
  272.  
  273.     /* Access font */
  274.     if ((*font_info = XLoadQueryFont(display,fontname)) == NULL)
  275.     {
  276.         (void) fprintf( stderr, "Basic: Cannot open 9x15 font\\n");
  277.         exit( -1 );
  278.     }
  279. }
  280.  
  281.  
  282. place_stips(win, gc, stipple, stip_width, stip_height, font_info, window_width, window_height)
  283. Window win;
  284. GC gc;
  285. Pixmap stipple[];
  286. unsigned int stip_width[], stip_height[];
  287. XFontStruct *font_info;
  288. unsigned int window_width, window_height;
  289. {
  290.     int i;
  291.     int dest_x, dest_y;
  292.     int string_x = 5, string_y = 12;
  293.     int text_height;
  294.     int direction, ascent, descent;
  295.     XCharStruct overall;
  296.  
  297.     text_height = font_info->max_bounds.ascent + font_info->max_bounds.descent;
  298.  
  299.     for (i = 120; i < window_width; i += 120 ) {
  300.         XDrawLine(display, win, gc, i, 0, i, window_height);
  301.     }
  302.  
  303.     for (i = 94; i < window_height; i += 94 ) {
  304.         XDrawLine(display, win, gc, 0, i, window_width, i);
  305.     }
  306.  
  307.     for (i = 0; i < NUMSTIPS; i++) {
  308.         if (string_x > window_width - 35)  {
  309.             string_x = 4;
  310.             string_y += 94;
  311.         }
  312.         XDrawString(display, win, gc, string_x, string_y, filename[i], strlen(filename[i]));
  313.         dest_y = string_y + font_info->max_bounds.descent + 4;
  314.         dest_x = string_x + 2;
  315.         XCopyPlane(display, stipple[i], win, gc, 0, 0, stip_width[i], stip_height[i], dest_x, dest_y, 1);
  316.         XTextExtents(font_info, filename[i], strlen(filename[i]),
  317.                 &direction, &ascent, &descent, &overall);
  318.         string_x += 120;
  319.     }
  320.     
  321. }
  322.  
  323. TooSmall(win, gc, font_info)
  324. Window win;
  325. GC gc;
  326. XFontStruct *font_info;
  327. {
  328.     char *string1 = "Too Small";
  329.     int y_offset, x_offset;
  330.  
  331.     y_offset = font_info->max_bounds.ascent + 2;
  332.     x_offset = 2;
  333.  
  334.     /* output text, centered on each line */
  335.     XDrawString(display, win, gc, x_offset, y_offset, string1, 
  336.             strlen(string1));
  337. }
  338.  
  339. #define name_width 16
  340. #define name_height 16
  341. #define name_x_hot    8
  342. #define name_y_hot    8
  343. static char name_bits[] =
  344.     {
  345.     0xf81f, 0xe3c7, 0xcff3, 0x9ff9,
  346.     0xbffd, 0x33cc, 0x7ffe, 0x7ffe,
  347.     0x7e7e, 0x7ffe, 0x37ec, 0xbbdd,
  348.     0x9c39, 0xcff3, 0xe3c7, 0xf81f
  349.     };
  350.  
  351.  
  352.  
  353. create_included_stipple(stip, width, height)
  354. Pixmap *stip; /* returned created stipple */
  355. unsigned int *width, *height;  /* returned */
  356. {
  357.     if (*stip = XCreateBitmapFromData(display, RootWindow(display, screen), 
  358.             name_bits, name_width, name_height) == NULL)
  359.         return(False);
  360.     *width = name_width;
  361.     *height = name_height;
  362.     return(True);
  363. }
  364.  
  365. create_read_stipple(stip, filename, width, height)
  366. Pixmap *stip;  /* returned created stipple */
  367. char *filename;
  368. unsigned int *width, *height;  /* returned */
  369. {
  370.     int depth = 1;
  371.     int value;
  372.     int x_hot, y_hot;  /* don't care about these unless for cursor */
  373.  
  374.     value = XReadBitmapFile(display, RootWindow(display, screen), 
  375.             filename, width, height, stip, &x_hot, &y_hot);
  376.     if (value == BitmapFileInvalid)
  377.         fprintf(stderr, "Filename %s contains invalid bitmap data\\n", filename);
  378.     else if (value == BitmapOpenFailed)
  379.         fprintf(stderr, "Filename %s could not be opened\\n", filename);
  380.     else if (value == BitmapNoMemory)
  381.         fprintf(stderr, "Not enough memory to allocate pixmap\\n");
  382.     return(value);
  383.     /* returns BitmapSuccess if everything worked */
  384. }
  385.