home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / x / volume8 / xfig2.8 / part05 / canvas.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-07-02  |  5.4 KB  |  222 lines

  1. /* 
  2.  *    FIG : Facility for Interactive Generation of figures
  3.  *
  4.  *    Copyright (c) 1985 by Supoj Sutanthavibul (supoj@sally.UTEXAS.EDU)
  5.  *    January 1985.
  6.  *    1st revision : Aug 1985.
  7.  *
  8.  *    %W%    %G%
  9. */
  10. #include "fig.h"
  11. #include "resources.h"
  12. #include "const.h"
  13. #include "paintop.h"
  14.  
  15. /***************** import global variables and routines *******************/
  16.  
  17. extern int        cur_command;
  18. extern int        errno;
  19. extern int        action_on;     /* tracker.c */
  20. extern int        magnet_mode;
  21.  
  22. extern int        CANVAS_WIDTH, CANVAS_HEIGHT;
  23. extern            null_proc();
  24. extern            (*canvas_kbd_proc)();
  25. extern            (*canvas_locmove_proc)();
  26. extern            (*canvas_leftbut_proc)();
  27. extern            (*canvas_middlebut_proc)();
  28. extern            (*canvas_rightbut_proc)();
  29. extern            null_proc();
  30. extern            set_popupmenu();
  31.  
  32.  
  33. /*********************** local variables and routines ************************/
  34.  
  35. static            canvas_selected();
  36. static            canvas_sighandler();
  37.  
  38. static Arg      canvas_args[] =
  39. {
  40.     /* 0 */ { XtNlabel, (XtArgVal)"" },
  41.     /* 1 */ { XtNwidth, (XtArgVal) 0 },
  42.     /* 2 */ { XtNheight, (XtArgVal) 0 },
  43.     /* 3 */ { XtNfromHoriz, (XtArgVal)NULL },
  44.     /* 4 */ { XtNhorizDistance, (XtArgVal) 0 },
  45.     /* 5 */ { XtNfromVert, (XtArgVal)NULL },
  46.     /* 6 */ { XtNvertDistance, (XtArgVal) 0 },
  47.     /* 7 */ { XtNtop,  (XtArgVal)XtChainTop  },
  48.     /* 8 */ { XtNleft, (XtArgVal)XtChainLeft },
  49. };
  50.  
  51. static void 
  52.  
  53. canvas_exposed(tool, event, params, nparams)
  54.     TOOL        tool;
  55.     INPUTEVENT    *event;
  56.     String        *params;
  57.     Cardinal    *nparams;
  58. {
  59.     if (((XExposeEvent *)event)->count > 0)
  60.         return;
  61.     redisplay_canvas();
  62.     redisplay_rulers();
  63. }
  64.  
  65. static void 
  66.  
  67. set_pos(tool, event, params, nparams)
  68.     TOOL        tool;
  69.     XEvent        *event;
  70.     String        *params;
  71.     Cardinal    *nparams;
  72. {
  73.     int        x, y;    /* fixed 3/12/89  from patch 3 */
  74.     Window        w;
  75.     extern TOOL    menu;
  76.  
  77.     XtGeometryResult result;
  78.     XtWidgetGeometry request, reply_return;
  79.     Dimension ww, hh, dw, dh;
  80.  
  81.     XTranslateCoordinates(tool_d, canvas_win, XDefaultRootWindow(tool_d),
  82.                   event->xbutton.x, event->xbutton.y, &x, &y, &w);
  83.     request.request_mode = CWWidth | CWHeight;
  84.     result = XtQueryGeometry((Widget)menu, &request, &reply_return);
  85.  
  86.     ww = reply_return.width;
  87.     hh = reply_return.height;
  88.     dw = DisplayWidth(tool_d, tool_sn);
  89.     dh = DisplayHeight(tool_d, tool_sn);
  90.     x = (x + ww > dw + 10) ? dw - ww + 10 : x;
  91.     y = (y + hh > dh + 10) ? dh - hh + 10 : y;
  92.  
  93.     XtMoveWidget(menu, x-10, y-10);
  94.     XWarpPointer(tool_d, None, DefaultRootWindow(tool_d),
  95.              0, 0, 0, 0, x, y);
  96.     set_temp_cursor(&arrow_cursor);    /* set cursor to arrow for popup menu */
  97. }
  98.  
  99. XtActionsRec canvas_actions[] =
  100. {
  101.     { "Event", (XtActionProc)canvas_selected },
  102.     { "Expose", (XtActionProc)canvas_exposed },
  103.     { "set_pos", (XtActionProc)set_pos },
  104. };
  105.  
  106. static String canvas_translations =
  107.     "<Motion>:Event()\n\
  108.     <Btn1Down>:Event()\n\
  109.     <Btn2Down>:Event()\n\
  110.     <Btn3Down>:set_pos(popup_menu)MenuPopup(popup_menu)\n\
  111.     <Key>:Event()\n\
  112.     <Expose>:Expose()\n";
  113.  
  114. static    Arg    canvas_color_args[] = {
  115.     { XtNforeground, (XtArgVal) &x_fg_color.pixel },
  116.     { XtNbackground, (XtArgVal) &x_bg_color.pixel },
  117. };
  118.  
  119. init_canvas(tool)
  120.     TOOL        tool;
  121. {
  122.     XColor    fixcolors[2];
  123.     
  124.     canvas_args[1].value = CANVAS_WIDTH;
  125.     canvas_args[2].value = CANVAS_HEIGHT;
  126.     canvas_args[3].value = (XtArgVal)panel_sw;    /* to the right of the panel */
  127.     canvas_args[5].value = (XtArgVal)topruler_sw;    /* below the top ruler */
  128.     canvas_sw = XtCreateWidget("canvas", labelWidgetClass, tool,
  129.         canvas_args, XtNumber(canvas_args));
  130.     XtGetValues(canvas_sw, canvas_color_args, XtNumber(canvas_color_args));
  131.  
  132.     /* get the RGB values for recolor cursor use -- may want to have
  133.        cursor color resource */
  134.     fixcolors[0] = x_fg_color;
  135.     fixcolors[1] = x_bg_color;
  136.     XQueryColors(tool_d, DefaultColormapOfScreen(tool_s), fixcolors, 2);
  137.     x_fg_color = fixcolors[0];
  138.     x_bg_color = fixcolors[1];
  139.     
  140.     /* now fix the global GC */
  141.     XSetState(tool_d, gc, x_fg_color.pixel, x_bg_color.pixel, GXcopy,
  142.           AllPlanes);
  143.     
  144.     /* and recolor the cursors */
  145.     recolor_cursors();
  146.     
  147.     canvas_leftbut_proc = null_proc;
  148.     canvas_middlebut_proc = null_proc;
  149.     canvas_rightbut_proc = null_proc;
  150.     canvas_kbd_proc = canvas_locmove_proc = null_proc;
  151.     XtAddActions(canvas_actions, XtNumber(canvas_actions));
  152.     XtOverrideTranslations(canvas_sw,
  153.         XtParseTranslationTable(canvas_translations));
  154.     return (1);
  155. }
  156.  
  157. setup_canvas()
  158. {
  159.     canvas_rightbut_proc = set_popupmenu;
  160.     canvas_win = XtWindow(canvas_sw);
  161.     init_grid();
  162. }
  163.  
  164. static 
  165.  
  166. canvas_selected(tool, event, params, nparams)
  167.     TOOL        tool;
  168.     INPUTEVENT    *event;
  169.     String        *params;
  170.     Cardinal    *nparams;
  171. {
  172.     register int    x, y, t;
  173.     char        buf[1];
  174.     XButtonPressedEvent    *be = (XButtonPressedEvent *)event;
  175.     XKeyPressedEvent    *ke = (XKeyPressedEvent *)event;
  176.  
  177.     x = event->x;
  178.     y = event->y;
  179.     if (magnet_mode)
  180.     {
  181.         x = ((t = x % 5) < 3) ? x - t - 1 : x + 5 - t - 1;
  182.         y = ((t = y % 5) < 3) ? y - t - 1 : y + 5 - t - 1;
  183.     }
  184.     switch (event->type)
  185.     {
  186.     case MotionNotify:
  187.         {
  188.             Window rw, cw;
  189.             static int sx = -10000, sy = -10000;
  190.             int rx, ry, cx, cy;
  191.             unsigned int mask;
  192.  
  193.             XQueryPointer(event->display, event->window,
  194.                       &rw, &cw,
  195.                       &rx, &ry,
  196.                       &cx, &cy,
  197.                       &mask);
  198.             if(cx == sx && cy == sy) break;
  199.             x = sx = cx;
  200.             y = sy = cy;
  201.         }
  202.         set_rulermark(x, y);
  203.         (*canvas_locmove_proc)(x, y);
  204.         break;
  205.     case ButtonPress:
  206.         if (be->button & Button1)
  207.             (*canvas_leftbut_proc)(x, y);
  208.         else if (be->button & Button2)
  209.             (*canvas_middlebut_proc)(x, y);
  210.         break;
  211.     case KeyPress:
  212.         if (XLookupString(ke, buf, sizeof(buf), NULL, NULL) > 0)
  213.             (*canvas_kbd_proc)((unsigned char)buf[0]);
  214.         break;
  215.     }
  216. }
  217.  
  218. clear_canvas()
  219. {
  220.     XClearArea(tool_d, canvas_win, 0, 0, 0, 0, False);
  221. }
  222.