home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / x / volume19 / xfig / part09 / u_drag.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-05-27  |  10.2 KB  |  426 lines

  1. /*
  2.  * FIG : Facility for Interactive Generation of figures
  3.  * Copyright (c) 1985 by Supoj Sutanthavibul
  4.  *
  5.  * "Permission to use, copy, modify, distribute, and sell this software and its
  6.  * documentation for any purpose is hereby granted without fee, provided that
  7.  * the above copyright notice appear in all copies and that both the copyright
  8.  * notice and this permission notice appear in supporting documentation. 
  9.  * No representations are made about the suitability of this software for 
  10.  * any purpose.  It is provided "as is" without express or implied warranty."
  11.  */
  12.  
  13. #include "fig.h"
  14. #include "resources.h"
  15. #include "object.h"
  16. #include "paintop.h"
  17. #include "u_draw.h"
  18. #include "u_elastic.h"
  19. #include "u_list.h"
  20. #include "u_undo.h"
  21. #include "mode.h"
  22. #include "w_canvas.h"
  23. #include "w_drawprim.h"
  24. #include "w_zoom.h"
  25.  
  26. static int    place_line(), cancel_line();
  27. static int    place_arc(), cancel_arc();
  28. static int    place_spline(), cancel_spline();
  29. static int    place_ellipse(), cancel_ellipse();
  30. static int    place_text(), cancel_text();
  31. static int    place_compound(), cancel_compound();
  32.  
  33. extern int    copy_selected();
  34.  
  35. /***************************** ellipse section ************************/
  36.  
  37. init_ellipsedragging(e, x, y)
  38.     F_ellipse       *e;
  39.     int            x, y;
  40. {
  41.     new_e = e;
  42.     fix_x = cur_x = x;
  43.     fix_y = cur_y = y;
  44.     cur_angle = e->angle;
  45.     x1off = (e->center.x - e->radiuses.x) - cur_x;
  46.     x2off = (e->center.x + e->radiuses.x) - cur_x;
  47.     y1off = (e->center.y - e->radiuses.y) - cur_y;
  48.     y2off = (e->center.y + e->radiuses.y) - cur_y;
  49.     canvas_locmove_proc = moving_ellipse;
  50.     canvas_leftbut_proc = place_ellipse;
  51.     canvas_rightbut_proc = cancel_ellipse;
  52.     set_action_on();
  53.     elastic_moveellipse();
  54. }
  55.  
  56. static
  57. cancel_ellipse()
  58. {
  59.     elastic_moveellipse();
  60.     if (return_proc == copy_selected) {
  61.     free_ellipse(&new_e);
  62.     } else {
  63.     list_add_ellipse(&objects.ellipses, new_e);
  64.     redisplay_ellipse(new_e);
  65.     }
  66.     (*return_proc) ();
  67.     draw_mousefun_canvas();
  68. }
  69.  
  70. static
  71. place_ellipse(x, y)
  72.     int            x, y;
  73. {
  74.     elastic_moveellipse();
  75.     adjust_pos(x, y, fix_x, fix_y, &x, &y);
  76.     translate_ellipse(new_e, x - fix_x, y - fix_y);
  77.     if (return_proc == copy_selected) {
  78.     add_ellipse(new_e);
  79.     } else {
  80.     list_add_ellipse(&objects.ellipses, new_e);
  81.     clean_up();
  82.     set_lastposition(fix_x, fix_y);
  83.     set_newposition(x, y);
  84.     set_action_object(F_MOVE, O_ELLIPSE);
  85.     set_latestellipse(new_e);
  86.     set_modifiedflag();
  87.     }
  88.     redisplay_ellipse(new_e);
  89.     (*return_proc) ();
  90.     draw_mousefun_canvas();
  91. }
  92.  
  93. /*****************************    arc  section  *******************/
  94.  
  95. init_arcdragging(a, x, y)
  96.     F_arc       *a;
  97.     int            x, y;
  98. {
  99.     new_a = a;
  100.     fix_x = cur_x = x;
  101.     fix_y = cur_y = y;
  102.     canvas_locmove_proc = moving_arc;
  103.     canvas_leftbut_proc = place_arc;
  104.     canvas_rightbut_proc = cancel_arc;
  105.     set_action_on();
  106.     elastic_movearc(new_a);
  107. }
  108.  
  109. static
  110. cancel_arc()
  111. {
  112.     elastic_movearc(new_a);
  113.     if (return_proc == copy_selected) {
  114.     free_arc(&new_a);
  115.     } else {
  116.     list_add_arc(&objects.arcs, new_a);
  117.     redisplay_arc(new_a);
  118.     }
  119.     (*return_proc) ();
  120.     draw_mousefun_canvas();
  121. }
  122.  
  123. static
  124. place_arc(x, y)
  125.     int            x, y;
  126. {
  127.     elastic_movearc(new_a);
  128.     adjust_pos(x, y, fix_x, fix_y, &x, &y);
  129.     translate_arc(new_a, x - fix_x, y - fix_y);
  130.     if (return_proc == copy_selected) {
  131.     add_arc(new_a);
  132.     } else {
  133.     list_add_arc(&objects.arcs, new_a);
  134.     clean_up();
  135.     set_lastposition(fix_x, fix_y);
  136.     set_newposition(x, y);
  137.     set_action_object(F_MOVE, O_ARC);
  138.     set_latestarc(new_a);
  139.     set_modifiedflag();
  140.     }
  141.     redisplay_arc(new_a);
  142.     (*return_proc) ();
  143.     draw_mousefun_canvas();
  144. }
  145.  
  146. /*************************  line  section  **********************/
  147.  
  148. init_linedragging(l, x, y)
  149.     F_line       *l;
  150.     int            x, y;
  151. {
  152.     int            xmin, ymin, xmax, ymax;
  153.  
  154.     new_l = l;
  155.     cur_x = fix_x = x;
  156.     cur_y = fix_y = y;
  157.     canvas_locmove_proc = moving_line;
  158.     canvas_leftbut_proc = place_line;
  159.     canvas_rightbut_proc = cancel_line;
  160.     set_action_on();
  161.     if (l->type == T_BOX || l->type == T_ARC_BOX || l->type == T_EPS_BOX) {
  162.     line_bound(l, &xmin, &ymin, &xmax, &ymax);
  163.     get_links(xmin, ymin, xmax, ymax);
  164.     }
  165.     elastic_moveline(new_l->points);
  166. }
  167.  
  168. static
  169. cancel_line()
  170. {
  171.     elastic_moveline(new_l->points);
  172.     free_linkinfo(&cur_links);
  173.     if (return_proc == copy_selected) {
  174.     free_line(&new_l);
  175.     } else {
  176.     list_add_line(&objects.lines, new_l);
  177.     redisplay_line(new_l);
  178.     }
  179.     (*return_proc) ();
  180.     draw_mousefun_canvas();
  181. }
  182.  
  183. static
  184. place_line(x, y)
  185.     int            x, y;
  186. {
  187.     int            dx, dy;
  188.  
  189.     elastic_moveline(new_l->points);
  190.     adjust_pos(x, y, fix_x, fix_y, &x, &y);
  191.     dx = x - fix_x;
  192.     dy = y - fix_y;
  193.     translate_line(new_l, dx, dy);
  194.     clean_up();
  195.     set_latestline(new_l);
  196.     if (return_proc == copy_selected) {
  197.     adjust_links(cur_linkmode, cur_links, dx, dy, 0, 0, 1.0, 1.0, 1);
  198.     tail(&objects, &object_tails);
  199.     append_objects(&objects, &saved_objects, &object_tails);
  200.     set_action_object(F_ADD, O_ALL_OBJECT);
  201.     free_linkinfo(&cur_links);
  202.     } else {
  203.     list_add_line(&objects.lines, new_l);
  204.     adjust_links(cur_linkmode, cur_links, dx, dy, 0, 0, 1.0, 1.0, 0);
  205.     set_lastposition(fix_x, fix_y);
  206.     set_newposition(x, y);
  207.     set_lastlinkinfo(cur_linkmode, cur_links);
  208.     cur_links = NULL;
  209.     set_action_object(F_MOVE, O_POLYLINE);
  210.     }
  211.     set_modifiedflag();
  212.     redisplay_line(new_l);
  213.     (*return_proc) ();
  214.     draw_mousefun_canvas();
  215. }
  216.  
  217. /************************  text section     **************************/
  218.  
  219. static PR_SIZE    txsize;
  220.  
  221. init_textdragging(t, x, y)
  222.     F_text       *t;
  223.     int            x, y;
  224. {
  225.     int           cw,cw2;
  226.     float       angle;
  227.  
  228.     new_t = t;
  229.     /* adjust in case text was off positioning grid and positioning is now on */
  230.     round_coords(new_t->base_x,new_t->base_y);
  231.     fix_x = cur_x = x;
  232.     fix_y = cur_y = y;
  233.     x1off = new_t->base_x - x;
  234.     y1off = new_t->base_y - y;
  235.     if (t->type == T_CENTER_JUSTIFIED || t->type == T_RIGHT_JUSTIFIED) {
  236.     txsize = pf_textwidth(t->fontstruct, strlen(t->cstring), t->cstring);
  237.     angle = t->angle*180.0/M_PI;
  238.     if (t->type == T_CENTER_JUSTIFIED) {
  239.         cw2 = round(txsize.x/2/zoomscale);
  240.         if (angle < 90.0 - 0.001)
  241.         x1off -= cw2;
  242.         else if (angle < 180.0 - 0.001) 
  243.         y1off += cw2;
  244.         else if (angle < 270.0 - 0.001) 
  245.         x1off += cw2;
  246.         else 
  247.         y1off -= cw2;
  248.     } else { /* T_RIGHT_JUSTIFIED */
  249.         cw = round(txsize.x/zoomscale);
  250.         if (angle < 90.0 - 0.001)
  251.         x1off -= cw;
  252.         else if (angle < 180.0 - 0.001) 
  253.         y1off += cw;
  254.         else if (angle < 270.0 - 0.001) 
  255.         x1off += cw;
  256.         else 
  257.         y1off -= cw;
  258.     }
  259.     }
  260.     canvas_locmove_proc = moving_text;
  261.     canvas_leftbut_proc = place_text;
  262.     canvas_rightbut_proc = cancel_text;
  263.     elastic_movetext();
  264.     set_action_on();
  265. }
  266.  
  267. static
  268. cancel_text()
  269. {
  270.     elastic_movetext();
  271.     if (return_proc == copy_selected) {
  272.     free_text(&new_t);
  273.     } else {
  274.     list_add_text(&objects.texts, new_t);
  275.     redisplay_text(new_t);
  276.     }
  277.     (*return_proc) ();
  278.     draw_mousefun_canvas();
  279. }
  280.  
  281. static
  282. place_text(x, y)
  283.     int            x, y;
  284. {
  285.     elastic_movetext();
  286.     adjust_pos(x, y, fix_x, fix_y, &x, &y);
  287.     translate_text(new_t, x - fix_x, y - fix_y);
  288.     if (return_proc == copy_selected) {
  289.     add_text(new_t);
  290.     } else {
  291.     list_add_text(&objects.texts, new_t);
  292.     clean_up();
  293.     set_lastposition(fix_x, fix_y);
  294.     set_newposition(x, y);
  295.     set_action_object(F_MOVE, O_TEXT);
  296.     set_latesttext(new_t);
  297.     set_modifiedflag();
  298.     }
  299.     redisplay_text(new_t);
  300.     (*return_proc) ();
  301.     draw_mousefun_canvas();
  302. }
  303.  
  304. /*************************  spline  section  **********************/
  305.  
  306. init_splinedragging(s, x, y)
  307.     F_spline       *s;
  308.     int            x, y;
  309. {
  310.     new_s = s;
  311.     cur_x = fix_x = x;
  312.     cur_y = fix_y = y;
  313.     canvas_locmove_proc = moving_spline;
  314.     canvas_leftbut_proc = place_spline;
  315.     canvas_rightbut_proc = cancel_spline;
  316.     set_action_on();
  317.     elastic_moveline(new_s->points);
  318. }
  319.  
  320. static
  321. cancel_spline()
  322. {
  323.     elastic_moveline(new_s->points);
  324.     if (return_proc == copy_selected) {
  325.     free_spline(&new_s);
  326.     } else {
  327.     list_add_spline(&objects.splines, new_s);
  328.     redisplay_spline(new_s);
  329.     }
  330.     (*return_proc) ();
  331.     draw_mousefun_canvas();
  332. }
  333.  
  334. static
  335. place_spline(x, y)
  336.     int            x, y;
  337. {
  338.     elastic_moveline(new_s->points);
  339.     adjust_pos(x, y, fix_x, fix_y, &x, &y);
  340.     translate_spline(new_s, x - fix_x, y - fix_y);
  341.     if (return_proc == copy_selected) {
  342.     add_spline(new_s);
  343.     } else {
  344.     list_add_spline(&objects.splines, new_s);
  345.     clean_up();
  346.     set_lastposition(fix_x, fix_y);
  347.     set_newposition(x, y);
  348.     set_action_object(F_MOVE, O_SPLINE);
  349.     set_latestspline(new_s);
  350.     set_modifiedflag();
  351.     }
  352.     redisplay_spline(new_s);
  353.     (*return_proc) ();
  354.     draw_mousefun_canvas();
  355. }
  356.  
  357. /*****************************    Compound section  *******************/
  358.  
  359. init_compounddragging(c, x, y)
  360.     F_compound       *c;
  361.     int            x, y;
  362. {
  363.     new_c = c;
  364.     fix_x = cur_x = x;
  365.     fix_y = cur_y = y;
  366.     x1off = c->nwcorner.x - x;
  367.     x2off = c->secorner.x - x;
  368.     y1off = c->nwcorner.y - y;
  369.     y2off = c->secorner.y - y;
  370.     canvas_locmove_proc = moving_box;
  371.     canvas_leftbut_proc = place_compound;
  372.     canvas_rightbut_proc = cancel_compound;
  373.     set_action_on();
  374.     get_links(c->nwcorner.x, c->nwcorner.y, c->secorner.x, c->secorner.y);
  375.     elastic_movebox();
  376. }
  377.  
  378. static
  379. cancel_compound()
  380. {
  381.     elastic_movebox();
  382.     free_linkinfo(&cur_links);
  383.     if (return_proc == copy_selected) {
  384.     free_compound(&new_c);
  385.     } else {
  386.     list_add_compound(&objects.compounds, new_c);
  387.     redisplay_compound(new_c);
  388.     }
  389.     (*return_proc) ();
  390.     draw_mousefun_canvas();
  391. }
  392.  
  393. static
  394. place_compound(x, y)
  395.     int            x, y;
  396. {
  397.     int            dx, dy;
  398.  
  399.     elastic_movebox();
  400.     adjust_pos(x, y, fix_x, fix_y, &x, &y);
  401.     dx = x - fix_x;
  402.     dy = y - fix_y;
  403.     translate_compound(new_c, dx, dy);
  404.     clean_up();
  405.     set_latestcompound(new_c);
  406.     if (return_proc == copy_selected) {
  407.     adjust_links(cur_linkmode, cur_links, dx, dy, 0, 0, 1.0, 1.0, 1);
  408.     tail(&objects, &object_tails);
  409.     append_objects(&objects, &saved_objects, &object_tails);
  410.     set_action_object(F_ADD, O_ALL_OBJECT);
  411.     free_linkinfo(&cur_links);
  412.     } else {
  413.     list_add_compound(&objects.compounds, new_c);
  414.     adjust_links(cur_linkmode, cur_links, dx, dy, 0, 0, 1.0, 1.0, 0);
  415.     set_lastposition(fix_x, fix_y);
  416.     set_newposition(x, y);
  417.     set_lastlinkinfo(cur_linkmode, cur_links);
  418.     cur_links = NULL;
  419.     set_action_object(F_MOVE, O_COMPOUND);
  420.     }
  421.     set_modifiedflag();
  422.     redisplay_compound(new_c);
  423.     (*return_proc) ();
  424.     draw_mousefun_canvas();
  425. }
  426.