home *** CD-ROM | disk | FTP | other *** search
/ BMUG PD-ROM 1995 Fall / PD-ROM F95.toast / Programming / Programming Languages / UCB Logo 3.0 ƒ / sources / standard source / xgraphics.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-08-04  |  6.4 KB  |  294 lines  |  [TEXT/ttxt]

  1. /* X window graphics for logo.  Nov. 1991. */
  2.  
  3. /* Note:  logo uses True and False as variables to hold references
  4.    to the nodes containing the values true and false.  X uses them
  5.    as macros serving the functions often served by the more standard
  6.    TRUE and FALSE.  To avoid a conflict, don't use True and False in
  7.    this file.
  8.  */
  9.  
  10. #include <stdio.h>
  11.  
  12. #include "logo.h"
  13. #include "xgraphics.h"
  14. #include "globals.h"
  15.  
  16.  
  17. int have_x = -1;
  18.  
  19. XWMHints xwmh = {
  20.   (InputHint|StateHint),    /* flags */
  21.   FALSE,            /* input */
  22.   NormalState,                  /* initial_state */
  23.   0,                /* icon pixmap */
  24.   0,                /* icon window */
  25.   0, 0,                         /* icon location */
  26.   0,                /* icon mask */
  27.   0,                /* Window group */
  28. };
  29.  
  30. int screen_height = DEFAULT_HEIGHT;
  31. int screen_width  = DEFAULT_WIDTH;
  32. int x_mouse_x, x_mouse_y;
  33.  
  34. int px, py;
  35.  
  36. /* We'll use 16 named colors for now (see xgraphics.h).
  37.    The ordering here corresponds to the zero-based argument
  38.    to setpencolor that gives that color -- pink is 12,
  39.    turquoise is 10 etc.
  40.  */
  41. char *colorname[NUMCOLORS] = {
  42.   "black", "blue", "green", "cyan", "red", "magenta", "yellow", "white",
  43.   "light grey", "purple", "turquoise", "lavender", "pink", "gold",
  44.   "orange", "brown"
  45. };
  46.  
  47.  
  48. XColor color[NUMCOLORS];
  49. XColor dummy;
  50.  
  51. void nop()
  52. {
  53. }
  54.  
  55. extern pen_info orig_pen;
  56. extern int turtle_shown;
  57.  
  58. extern int x_mouse_x, x_mouse_y;
  59.  
  60. Display    *dpy;        /* X server connection   */
  61. Window      win;        /* Window ID             */
  62. GC          draw_gc,            /* GC to draw with       */
  63.             up_gc,              /* Do nothing gc.        */
  64.             erase_gc,           /* Erase gc.             */
  65.             reverse_gc;         /* GC to reverse things. */
  66.  
  67. int         screen;
  68.  
  69. int save_argc;
  70. char ** save_argv;
  71.  
  72. void x_window_init(int argc, char **argv)
  73. {
  74.     save_argc = argc;
  75.     save_argv = argv;
  76. }
  77.  
  78. void real_window_init()
  79. {
  80.   unsigned long fth, pad;    /* Font size parameters */
  81.   unsigned long fg, bg, bd;    /* Pixel values */
  82.   unsigned long bw;        /* Border width */
  83.   XFontStruct *fontstruct;    /* Font descriptor */
  84.   XGCValues   gcv;        /* Struct for creating GC */
  85.   XEvent      event;        /* Event received */
  86.   XSizeHints  xsh;        /* Size hints for window manager */
  87.   char       *geomSpec;            /* Window geometry string */
  88.   XSetWindowAttributes xswa;    /* Temporary Set Window Attribute struct */
  89.   int n;
  90.  
  91.   /* Open X display. */
  92.   if ((dpy = XOpenDisplay(NULL)) == NULL) {
  93.     have_x = 0;
  94.     return;
  95.   } else have_x = 1;
  96.  
  97.   screen = DefaultScreen(dpy);
  98.  
  99.   /* Load default font. */
  100.   if ((fontstruct = XLoadQueryFont(dpy, FONT)) == NULL) {
  101.     have_x = 0;
  102.     return;
  103.   }
  104.  
  105.   fth = fontstruct->max_bounds.ascent + fontstruct->max_bounds.descent;
  106.  
  107.   /*
  108.    * Select colors.
  109.    */
  110.  
  111.   for(n = 0; n < NUMCOLORS; n++)
  112.     XAllocNamedColor(dpy, DefaultColormap(dpy, screen),
  113.              colorname[n], &color[n], &dummy);
  114.  
  115.  
  116.   /* Not much alternative to the following,
  117.      because of the use of the xor operation to erase the turtle.
  118.      The background HAS to be the zero pixel or the scheme won't work.
  119.    */
  120.   bg = 0;
  121.   fg = bd = 1;
  122.  
  123.   /*
  124.    * Set the border width of the window,  and the gap between the text
  125.    * and the edge of the window, "pad".
  126.    */
  127.   pad = BORDER;
  128.   bw = 1;
  129.  
  130.   /* Set up size/position hints. */
  131.   xsh.flags = (PPosition | PSize);
  132.   xsh.height = screen_height;
  133.   xsh.width = screen_width;
  134.   xsh.x = (DisplayWidth(dpy, DefaultScreen(dpy)) - xsh.width) / 2;
  135.   xsh.y = (DisplayHeight(dpy, DefaultScreen(dpy)) - xsh.height) / 2;
  136.  
  137.   win = XCreateSimpleWindow(dpy, DefaultRootWindow(dpy),
  138.                 xsh.x, xsh.y, xsh.width, xsh.height,
  139.                 bw, bd, bg);
  140.  
  141.   /* Label window. */
  142.   XSetStandardProperties(dpy, win, "BXLogo", "BXLogo", None,
  143.                 save_argv, save_argc, &xsh);
  144.   XSetWMHints(dpy, win, &xwmh);
  145.  
  146.   /* Set up default color map. */
  147.   xswa.colormap = DefaultColormap(dpy, DefaultScreen(dpy));
  148.   xswa.backing_store = Always;
  149.   XChangeWindowAttributes(dpy, win, CWColormap | CWBackingStore, &xswa);
  150.  
  151.  
  152.  /*
  153.   * Create GCs.
  154.   */
  155.  
  156.   /* Make the foreground and background fields in the GC
  157.      match the fg and bg variables that were passed to the window.
  158.    */
  159.  
  160.   gcv.font = fontstruct->fid;
  161.   gcv.background = 0;
  162.   gcv.foreground = 1;
  163.   gcv.plane_mask = AllPlanes;
  164.  
  165.   /* Normal drawing GC. */
  166.   draw_gc = XCreateGC(dpy, win,
  167.               (GCPlaneMask | GCFont | GCForeground | GCBackground),
  168.               &gcv);
  169.  
  170.   /* Create GC for erasing/drawing turtle. */
  171.   gcv.function = GXxor;
  172.   reverse_gc = XCreateGC(dpy, win, 
  173.              (GCPlaneMask | GCFont | GCFunction |
  174.               GCForeground | GCBackground), 
  175.              &gcv);
  176.  
  177.   /* Erase gc just draws the bacground color. */
  178.   gcv.foreground = bg;
  179.   gcv.function = GXcopy;
  180.   erase_gc = XCreateGC(dpy, win,
  181.                (GCPlaneMask | GCFont | GCForeground | GCBackground),
  182.                &gcv);
  183.  
  184.   gcv.function = GXnoop;
  185.   up_gc = XCreateGC(dpy, win,
  186.             (GCPlaneMask | GCFunction | GCForeground | GCBackground),
  187.             &gcv);
  188.  
  189.   orig_pen.pm = draw_gc;
  190.   orig_pen.vis = 0;
  191.  
  192.   XSelectInput(dpy, win, EVENT_MASK);
  193.  
  194.  /*
  195.   * Map the window to make it visible.  See Section 3.5.
  196.   */
  197.   XMapWindow(dpy, win);
  198.  
  199.  /*
  200.   * Loop,  examining each event.  Exit when we get mapped.
  201.   */
  202.   do {
  203.    /*
  204.     * Get the next event
  205.     */
  206.     XWindowEvent(dpy, win, StructureNotifyMask, &event);
  207.  
  208.     /*
  209.      * Wait for the map notify event.
  210.      */
  211.     if (event.type == MapNotify) {
  212.       XClearWindow(dpy, win);
  213.       break;
  214.     }
  215.   } while(1);
  216.  
  217.   if(turtle_shown)
  218.     draw_turtle();
  219. }
  220.  
  221. NODE *Get_node_pen_mode(GC mode)
  222. {
  223.   long cur_mode = (long)mode;
  224.  
  225.   checkX;
  226.  
  227.   if(cur_mode == (long)draw_gc)
  228.     return(make_static_strnode("paint"));
  229.  
  230.   if(cur_mode == (long)erase_gc)
  231.     return(make_static_strnode("erase"));
  232.  
  233.   if(cur_mode == (long)reverse_gc)
  234.     return(make_static_strnode("reverse"));
  235.  
  236.   return(make_static_strnode("unknown"));
  237. }
  238.  
  239.  
  240. void placate_x()
  241. {
  242.   XEvent           event;
  243.   XConfigureEvent *xce;
  244.   XMotionEvent    *xme;
  245.  
  246.     checkX;
  247.  
  248.   while(XCheckWindowEvent(dpy, win, EVENT_MASK, (XEvent *)&event))
  249.  
  250.     switch(event.type) {
  251.  
  252.     case ConfigureNotify:
  253.  
  254.       xce = (XConfigureEvent *)&event;
  255.       screen_height = xce->height;
  256.       screen_width  = xce->width;
  257.  
  258.       XClearWindow(dpy, win);
  259.       if(turtle_shown)
  260.     draw_turtle();
  261.       
  262.       break;
  263.  
  264.     case MotionNotify:
  265.  
  266.       xme = (XMotionEvent *)&event;
  267.       x_mouse_x = xme->x;
  268.       x_mouse_y = xme->y;
  269.       break;
  270.     }
  271. }
  272.  
  273. int get_mouse_x()
  274. {
  275.   checkX;
  276.  
  277.   placate_x();
  278.  
  279.   return( x_mouse_x - (screen_width / 2));
  280. }
  281.  
  282.  
  283. int get_mouse_y()
  284. {
  285.   checkX;
  286.  
  287.   placate_x();
  288.  
  289.   return((screen_height / 2) - x_mouse_y);
  290. }
  291.  
  292.  
  293. void logofill() {}
  294.