home *** CD-ROM | disk | FTP | other *** search
/ gondwana.ecr.mu.oz.au/pub/ / Graphics.tar / Graphics / simplex.c < prev    next >
C/C++ Source or Header  |  1990-12-10  |  3KB  |  128 lines

  1. /*
  2.  * Simple X program.....
  3.  */
  4. #include <stdio.h>
  5. #include <X11/Xlib.h>
  6. #include <X11/Xutil.h>
  7.  
  8. main(argc, argv)
  9.     int    argc;
  10.     char    *argv[];
  11. {
  12.     XFontStruct     *font_id;
  13.     GC        theGC;
  14.         XGCValues       xgcvals;
  15.     KeySym    key;
  16.     XEvent    event;
  17.     Window    window;
  18.     Display    *display;
  19.  
  20.     XSizeHints sizehints;
  21.     XWMHints   wmhints;
  22.     XGCValues  values;
  23.  
  24.     char     temp;
  25.     int     num, thescreen, cwidth, cheight;
  26.     unsigned long mask, foreground, background;
  27.  
  28.  
  29.     if ((display = XOpenDisplay((char *)NULL)) == NULL) {
  30.         fprintf(stderr,"Can't open display %s\n", XDisplayName((char *)NULL));
  31.         exit(1);
  32.     }
  33.  
  34.     thescreen  = DefaultScreen(display);
  35.     foreground = WhitePixel(display, thescreen);
  36.     background = BlackPixel(display, thescreen);
  37.  
  38.     sizehints.x      = 200;
  39.     sizehints.y      = 200;
  40.     sizehints.width  = 512;
  41.     sizehints.height = 400;
  42.     sizehints.flags  = PPosition | PSize;
  43.  
  44.  
  45.     window = XCreateSimpleWindow(display, 
  46.             DefaultRootWindow(display),
  47.             sizehints.x, sizehints.y,
  48.             sizehints.width, sizehints.height,
  49.             2, 
  50.             foreground, background
  51.         );
  52.  
  53.  
  54.     mask            = GCForeground | GCBackground;;
  55.     values.foreground = foreground;
  56.     values.background = background;
  57.  
  58.     theGC = XCreateGC(display, window, mask, &values);
  59.  
  60.     wmhints.input = True;
  61.     wmhints.flags = InputHint;
  62.     XSetWMHints(display, window, &wmhints);
  63.  
  64.     mask = KeyPressMask  | ExposureMask ;
  65.     XSelectInput(display, window, mask);
  66.  
  67.     XSetStandardProperties(display,
  68.             window, 
  69.             "Hello Demo", 
  70.             "Hello Icon",
  71.             None, 
  72.             " ", 0,
  73.             &sizehints
  74.     );
  75.  
  76.     if ((font_id = XLoadQueryFont(display, "fixed")) == (XFontStruct *)NULL) {
  77.         fprintf(stderr, "Can't open fixed font\n");
  78.         exit(1);
  79.     }
  80.  
  81.     cheight = font_id->max_bounds.ascent + font_id->max_bounds.descent;
  82.     cwidth = font_id->max_bounds.width;
  83.  
  84.         xgcvals.font = XLoadFont(display, "fixed");
  85.  
  86.         XChangeGC(display, theGC, GCFont, &xgcvals);
  87.  
  88.     XMapRaised(display, window);
  89.  
  90.         XFlush(display);
  91.  
  92.     while (True) {
  93.         XWindowEvent(display,window, mask,  &event);
  94.  
  95.         switch (event.type) {
  96.         case Expose:
  97.             if (event.xexpose.count == 0)
  98.                 XClearWindow(display, window);
  99.  
  100.                 XDrawString(display, 
  101.                     window, 
  102.                     theGC, 
  103.                     sizehints.width / 2 - 12 * cwidth / 2,
  104.                     sizehints.height / 2 - cheight / 2,
  105.                     "Hello, world", 
  106.                     12
  107.                 );
  108.             break;
  109.  
  110.         case MappingNotify:
  111.             XRefreshKeyboardMapping(&event);
  112.             break;
  113.  
  114.         case KeyPress:
  115.             printf("KeyPress\n");
  116.             num = XLookupString(&event, &temp, 1, &key, 0);
  117.             if (num == 1 && (temp == 'q' || temp == 'Q')) {
  118.                 XFreeGC(display, theGC);
  119.                 XDestroyWindow(display, window);
  120.                 XCloseDisplay(display);
  121.                 exit(0);
  122.             }
  123.             break;
  124.         }
  125.         XFlush(display);
  126.     }
  127. }
  128.