home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / listings / v_09_10 / 9n10106a < prev    next >
Text File  |  1991-01-22  |  7KB  |  304 lines

  1.  
  2.  
  3. /*
  4.  *      shape1.c
  5.  *      An Xlib program using the SHAPE
  6.  *    extension from X11 Release 4.
  7.  *      Written for the C Users Journal.
  8.  *
  9.  *      Link with the X library, e.g.,
  10.  *
  11.  *              cc -o shape1 shape1.c -lXext -lX11
  12.  *
  13.  *      Define SYSV if you have malloc()
  14.  *      declared in stdlib.h.
  15.  *
  16.  *    14 Dec 90
  17.  */
  18.  
  19. #include <stdio.h>
  20.  
  21. #ifdef SYSV
  22. #include <stdlib.h>
  23. #endif
  24.  
  25. #include  <X11/Xlib.h>
  26. #include  <X11/Xutil.h>
  27. #include  <X11/keysymdef.h>
  28. #include  <X11/keysym.h>
  29. #include  <X11/extensions/shape.h>
  30.  
  31.  
  32. /*
  33.  *      We have a hard-coded size and location
  34.  */
  35. #define X_LOCATION              10
  36. #define Y_LOCATION              20
  37. #define WIDTH                   400
  38. #define HEIGHT                  300
  39. #define MAX_STRING_LENGTH       400
  40.  
  41. #define    MAX_RECTS        100
  42.  
  43. /*
  44.  *      Use xlsfonts to find a font name
  45.  *      that is available on your system.
  46.  */
  47. /* #define FONT_NAME               "fixed"  */
  48. #define FONT_NAME               "variable"
  49.  
  50.  
  51. main( argc, argv )
  52.  
  53. int     argc;
  54. char    *argv[];
  55.  
  56. {       /* main */
  57.         Display         *display;
  58.         int             screen;
  59.         Window          rootwindow;
  60.         Window          window;
  61.         XSizeHints      *sizehints;
  62.         XWMHints        *wmhints;
  63.         GC              gc;
  64.         XEvent          event;
  65.         int             done;
  66.         XFontStruct     *font;
  67.     int        shape_status, major_version, minor_version;
  68.     XRectangle    rectangles[ MAX_RECTS + 1 ];
  69.     int        i, number_rectangles, x, y;
  70.     char        message[ MAX_STRING_LENGTH + 4 ];
  71.  
  72.  
  73.         /*
  74.          * Connect to an X server.
  75.          */
  76.         display = XOpenDisplay( (char *) NULL );
  77.  
  78.         if ( display == (Display *) NULL )
  79.                 {
  80.                 fprintf( stderr, "Error opeing in display\n" );
  81.                 exit( 1 );
  82.                 }
  83.  
  84.         screen     = DefaultScreen( display );
  85.         rootwindow = RootWindow( display, screen );
  86.  
  87.         /*
  88.          * Create a window, note reversed
  89.      * order of colors
  90.          */
  91.         window = XCreateSimpleWindow( display,
  92.                         rootwindow,     /* parent */
  93.                         X_LOCATION, Y_LOCATION,
  94.                         WIDTH, HEIGHT,
  95.                         1,              /* border width */
  96.                         WhitePixel( display, screen ),
  97.                         BlackPixel( display, screen ) );
  98.  
  99.  
  100.         /*
  101.          * Set up Window Manager Hints for keyboard input
  102.          */
  103.         wmhints = (XWMHints *) malloc( sizeof(XWMHints) );
  104.  
  105.         wmhints->flags = InputHint;
  106.         wmhints->input = True;
  107.  
  108.         XSetWMHints(display, window, wmhints );
  109.  
  110.         free( wmhints );
  111.  
  112.         /*
  113.          * Set up hints about the window
  114.          */
  115.         sizehints = (XSizeHints *) malloc( sizeof(XSizeHints) );
  116.  
  117.         sizehints->x      = X_LOCATION;
  118.         sizehints->y      = Y_LOCATION;
  119.         sizehints->width  = WIDTH;
  120.         sizehints->height = HEIGHT;
  121.         sizehints->flags  = PPosition | PSize;
  122.  
  123.         /* 
  124.          * Use XSetWMProperties() in R4
  125.          *      
  126.          */
  127.         XSetStandardProperties( display, window,
  128.                 "Shape1",        /* window name */
  129.                 "Shape1",        /* icon name */
  130.                 (Pixmap) None,   /* Icon pixmap */
  131.                 argv, argc,
  132.                 sizehints );
  133.  
  134.         free( sizehints );
  135.  
  136.         /*
  137.          * Ask for Expose, mouse (Button) and keyboard input
  138.          */
  139.         XSelectInput( display, window,
  140.                 ButtonPressMask | ExposureMask | KeyPressMask );
  141.  
  142.  
  143.         /*
  144.          * Load up a font. 
  145.          */
  146.         font = XLoadQueryFont( display, FONT_NAME );
  147.  
  148.  
  149.         if ( font == (XFontStruct *) NULL )
  150.                 {
  151.                 fprintf( stderr, "Error in loading %s font.\n",
  152.                         FONT_NAME );
  153.  
  154.                 XCloseDisplay( display );
  155.                 exit( 1 );
  156.                 }
  157.  
  158.  
  159.         /*
  160.          * Create a Graphics Context (GC) for drawing text
  161.          */
  162.         gc = XCreateGC( display, window,
  163.                 0L, (XGCValues *) NULL );
  164.  
  165.         XSetForeground( display, gc, 
  166.                 WhitePixel( display, screen ) );
  167.  
  168.         /*
  169.          * Set the background color, to, 
  170.      * this time to black, since the 
  171.      * foreground is white.
  172.          */     
  173.         XSetBackground( display, gc, 
  174.                 BlackPixel( display, screen ) );
  175.  
  176.         /*
  177.          * Set the GC to draw in the given font.
  178.          */
  179.         XSetFont( display, gc, font->fid );
  180.  
  181.  
  182.     /*
  183.      * Check if we have the SHAPE extension
  184.      */
  185.     shape_status = XShapeQueryVersion( display, 
  186.         &major_version,
  187.         &minor_version );
  188.  
  189.     if( shape_status )
  190.         {
  191.         sprintf( message,
  192.             "SHAPE extension supported at version %d.%d\n",
  193.             major_version, minor_version );
  194.  
  195.         /*
  196.          * Set up some rectangles
  197.          */
  198.         i = 0;
  199.             for ( x = 0; x < WIDTH; x += 50 )
  200.                     {
  201.                     for( y = 0; y < HEIGHT; y += 50 )
  202.                 {
  203.                 i++;
  204.  
  205.                 if ( i >= MAX_RECTS )
  206.                     {
  207.                     i = MAX_RECTS - 1;
  208.                     }
  209.  
  210.                 rectangles[i].width  = 45;
  211.                 rectangles[i].height = 45;
  212.  
  213.                 rectangles[i].x = x;
  214.                 rectangles[i].y = y;
  215.                 }
  216.             }
  217.  
  218.  
  219.         /*
  220.          * Make our window's shape be the set of rectangles
  221.          */
  222.         number_rectangles = i;
  223.         XShapeCombineRectangles( display, 
  224.             window,
  225.             ShapeBounding,
  226.             0, 0,         /* x, y, offsets */
  227.             rectangles,
  228.             number_rectangles,
  229.             ShapeSet,
  230.             Unsorted );  /* we haven't sorted the rects */
  231.         }
  232.     else
  233.         {
  234.         sprintf( message, 
  235.             "Sorry, the SHAPE extension is not available" );
  236.         }
  237.  
  238.     
  239.     
  240.         /*
  241.          * Make Window appear on the screen
  242.          */
  243.         XMapWindow( display, window );
  244.         XFlush( display );
  245.  
  246.         done = False;
  247.  
  248.         while( !done )
  249.                 {
  250.                 XNextEvent( display, &event );
  251.  
  252.                 switch( event.type )
  253.                         {
  254.                         case ButtonPress:
  255.                                 XCloseDisplay( display );
  256.  
  257.                                 exit( 0 );
  258.                                 break;
  259.                         case Expose:
  260.                                 /*
  261.                                  * Only redraw when all
  262.                                  * Expose events are in
  263.                                  */
  264.                                 if ( event.xexpose.count == 0 )
  265.                                         {
  266.                                         RedrawText( display, window, 
  267.                                                 gc, message );
  268.                                         }
  269.                                 break;
  270.                         }
  271.  
  272.                 }
  273.  
  274.  
  275. }       /* main */
  276.  
  277. RedrawText( display, window, gc, string )
  278.  
  279. Display *display;
  280. Window  window;
  281. GC      gc;
  282. char    string[];
  283.  
  284. {       /* RedrawText */
  285.  
  286.         XDrawImageString( display, window, gc,
  287.                 5, 20,          /* location */
  288.                 string,
  289.                 strlen( string ) );
  290.  
  291. #define MESSAGE "Click a Mouse Button to Exit"
  292.  
  293.         XDrawImageString( display, window, gc,
  294.                 5, 40,          /* location */
  295.                 MESSAGE,
  296.                 strlen( MESSAGE ) );
  297.  
  298.         XFlush( display );
  299.  
  300. }       /* RedrawText */
  301.  
  302. /* end of file */
  303.  
  304.