home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / x / volume8 / xfig2.8 / part12 / undo.c < prev   
C/C++ Source or Header  |  1990-07-03  |  13KB  |  477 lines

  1. /* 
  2.  *    FIG : Facility for Interactive Generation of figures
  3.  *
  4.  *    Copyright (c) 1985, 1988 by Supoj Sutanthavibul (supoj@sally.UTEXAS.EDU)
  5.  *    January 1985.
  6.  *    1st revision : August 1985.
  7.  *    2nd revision : March 1988.
  8.  *
  9.  *    %W%    %G%
  10. */
  11. #include "fig.h"
  12. #include "resources.h"
  13. #include "func.h"
  14. #include "object.h"
  15. #include "paintop.h"
  16.  
  17. extern int        pointmarker_shown, compoundbox_shown;
  18.  
  19. extern int        foreground_color, background_color;
  20. extern int        last_action, last_object;
  21. extern int        last_axis, last_rotateangle;
  22. extern int        movedpoint_num;
  23. extern int        fix_x, fix_y;
  24. extern F_pos        last_position, new_position;
  25.  
  26. extern F_compound    objects;
  27. extern F_compound    saved_objects;
  28. extern F_compound    object_tails;
  29.  
  30. extern F_point        *left_point; 
  31. extern F_point        *moved_point;
  32. extern F_point        *deleted_point;
  33. extern F_point        *added_point;
  34.  
  35. undo()
  36. {
  37.     switch(last_action) {
  38.         case F_CREATE :
  39.         undo_create();
  40.         break;
  41.         case F_REMOVE :
  42.         undo_remove();
  43.         break;
  44.         case F_MOVE :
  45.         undo_move();
  46.         break;
  47.         case F_MOVE_POINT :
  48.         undo_movepoint();
  49.         break;
  50.         case F_FLIP :
  51.         undo_flip();
  52.         break;
  53.         case F_ROTATE :
  54.         undo_rotate();
  55.         break;
  56.         case F_GLUE :
  57.         undo_glue();
  58.         break;
  59.         case F_BREAK :
  60.         undo_break();
  61.         break;
  62.         case F_SCALE :
  63.         undo_scale();
  64.         break;
  65.         case F_ADD_POINT :
  66.         undo_addpoint();
  67.         break;
  68.         case F_DELETE_POINT :
  69.         undo_deletepoint();
  70.         break;
  71.         case F_CHANGE :
  72.         undo_change();
  73.         default :
  74.         return;
  75.         }    
  76.     }
  77.  
  78. undo_addpoint()
  79. {
  80.     if (last_object == O_POLYLINE)
  81.         linepoint_deleting(saved_objects.lines);
  82.     else
  83.         splinepoint_deleting(saved_objects.splines);
  84.     last_action = F_DELETE_POINT;
  85.     deleted_point = added_point;
  86.     }
  87.  
  88. undo_deletepoint()
  89. {
  90.     if (last_object == O_POLYLINE) 
  91.         linepoint_adding(saved_objects.lines, deleted_point);
  92.     else
  93.         splinepoint_adding(saved_objects.splines, deleted_point);
  94.     last_action = F_ADD_POINT;
  95.     added_point = deleted_point;
  96.     }
  97.  
  98. undo_break()
  99. {
  100.     if (compoundbox_shown)
  101.         draw_compoundbox(saved_objects.compounds, INV_PAINT);
  102.     cut_objects(&objects, &object_tails);
  103.     insert_compound(&objects.compounds, saved_objects.compounds);
  104.     last_action = F_GLUE;
  105.     }
  106.  
  107. undo_glue()
  108. {
  109.     put_msg("UNDO for GLUE not working");
  110.     return;
  111.  
  112.     if (compoundbox_shown)
  113.         draw_compoundbox(saved_objects.compounds, INV_PAINT);
  114.     delete_compound(&objects.compounds, saved_objects.compounds);
  115.     append_objects(&objects, saved_objects.compounds, &object_tails);
  116.     last_action = F_BREAK;
  117.     }
  118.  
  119. undo_change()
  120. {
  121.     switch (last_object) {
  122.         case O_POLYLINE :
  123.         change_line(saved_objects.lines->next,saved_objects.lines);
  124.         break;
  125.         case O_ELLIPSE:
  126.         change_ellipse(saved_objects.ellipses->next,saved_objects.ellipses);
  127.         break;
  128.         case O_TEXT :
  129.         change_text(saved_objects.texts->next,saved_objects.texts);
  130.         break;
  131.         case O_SPLINE :
  132.         change_spline(saved_objects.splines->next,saved_objects.splines);
  133.         break;
  134.         case O_ARC :
  135.         change_arc(saved_objects.arcs->next,saved_objects.arcs);
  136.         break;
  137.     }
  138.     last_action = F_CHANGE;
  139. }
  140.  
  141. /* 
  142. When a single object is created, it is inserted as the first object in
  143. the appropriate list in objects.  It is also placed in the appropriate
  144. list in saved_objects.
  145.  
  146. However when a number of objects are created (usually by reading them
  147. in from a file or undoing a remove-all action), they are appended to
  148. the lists in objects and also saved in saved_objects.  The pointers
  149. in object_tails will be set to point to the last members of the lists
  150. in objects prior to the appending.
  151.  
  152. Note: The read operation will set the pointers in object_tails
  153. while the remove-all operation will zero pointers in objects.
  154. */
  155.  
  156. undo_create()
  157. {
  158.     switch(last_object) {
  159.         case O_POLYLINE :
  160.         objects.lines = saved_objects.lines->next;
  161.         saved_objects.lines->next = NULL;
  162.         erase_lines(saved_objects.lines);
  163.         break;
  164.         case O_ELLIPSE :
  165.         objects.ellipses = saved_objects.ellipses->next;
  166.         saved_objects.ellipses->next = NULL;
  167.         erase_ellipses(saved_objects.ellipses);
  168.         break;
  169.         case O_TEXT :
  170.         objects.texts = saved_objects.texts->next;
  171.         saved_objects.texts->next = NULL;
  172.         erase_texts(saved_objects.texts);
  173.         break;
  174.         case O_SPLINE :
  175.             objects.splines = saved_objects.splines->next;
  176.             saved_objects.splines->next = NULL;
  177.         erase_splines(saved_objects.splines);
  178.         break;
  179.         case O_ARC :
  180.         objects.arcs = saved_objects.arcs->next;
  181.         saved_objects.arcs->next = NULL;
  182.         erase_arcs(saved_objects.arcs);
  183.         break;
  184.         case O_COMPOUND :
  185.         objects.compounds = saved_objects.compounds->next;
  186.         saved_objects.compounds->next = NULL;
  187.         erase_compounds(saved_objects.compounds);
  188.         break;
  189.         case O_ALL_OBJECT :
  190.         cut_objects(&objects, &object_tails);
  191.         redisplay_canvas();
  192.         break;
  193.         }
  194.     last_action = F_REMOVE;
  195.     }
  196.  
  197. undo_remove()
  198. {
  199.     switch (last_object) {
  200.         case O_POLYLINE :
  201.         draw_lines(saved_objects.lines);
  202.         insert_line(&objects.lines, saved_objects.lines);
  203.         break;
  204.         case O_ELLIPSE :
  205.         draw_ellipses(saved_objects.ellipses);
  206.         insert_ellipse(&objects.ellipses, saved_objects.ellipses);
  207.         break;
  208.         case O_TEXT :
  209.         draw_texts(saved_objects.texts);
  210.         insert_text(&objects.texts, saved_objects.texts);
  211.         break;
  212.         case O_SPLINE :
  213.         draw_splines(saved_objects.splines);
  214.         insert_spline(&objects.splines, saved_objects.splines);
  215.         break;
  216.         case O_ARC :
  217.         draw_arcs(saved_objects.arcs);
  218.         insert_arc(&objects.arcs, saved_objects.arcs);
  219.         break;
  220.         case O_COMPOUND :
  221.         draw_compounds(saved_objects.compounds);
  222.         insert_compound(&objects.compounds, saved_objects.compounds);
  223.         break;
  224.         case O_ALL_OBJECT :
  225.         append_objects(&objects, &saved_objects, &object_tails);
  226.         redisplay_canvas();
  227.         break;
  228.         }
  229.     last_action = F_CREATE;
  230.     }
  231.  
  232. undo_flip()
  233. {
  234.     switch (last_object) {
  235.         case O_POLYLINE :
  236.         if (pointmarker_shown)
  237.             toggle_linepointmarker(saved_objects.lines);
  238.         draw_line(saved_objects.lines, ERASE);
  239.         flip_line(saved_objects.lines, 
  240.             last_position.x, last_position.y,
  241.             last_axis);
  242.         draw_line(saved_objects.lines, PAINT);
  243.         if (pointmarker_shown)
  244.             toggle_linepointmarker(saved_objects.lines);
  245.         break;
  246.         case O_ELLIPSE :
  247.         if (pointmarker_shown)
  248.             toggle_ellipsepointmarker(saved_objects.ellipses);
  249.         draw_ellipse(saved_objects.ellipses, background_color);
  250.         flip_ellipse(saved_objects.ellipses, 
  251.             last_position.x, last_position.y,
  252.             last_axis);
  253.         draw_ellipse(saved_objects.ellipses, foreground_color);
  254.         if (pointmarker_shown)
  255.             toggle_ellipsepointmarker(saved_objects.ellipses);
  256.         break;
  257.         case O_SPLINE :
  258.         if (pointmarker_shown)
  259.             toggle_splinepointmarker(saved_objects.splines);
  260.         draw_spline(saved_objects.splines, ERASE);
  261.         flip_spline(saved_objects.splines,
  262.             last_position.x, last_position.y,
  263.             last_axis);
  264.         draw_spline(saved_objects.splines, PAINT);
  265.         if (pointmarker_shown)
  266.             toggle_splinepointmarker(saved_objects.splines);
  267.         break;
  268.         case O_ARC :
  269.         if (pointmarker_shown)
  270.             toggle_arcpointmarker(saved_objects.arcs);
  271.         draw_arc(saved_objects.arcs, background_color);
  272.         flip_arc(saved_objects.arcs, 
  273.             last_position.x, last_position.y,
  274.             last_axis);
  275.         draw_arc(saved_objects.arcs, foreground_color);
  276.         if (pointmarker_shown)
  277.             toggle_arcpointmarker(saved_objects.arcs);
  278.         break;
  279.         case O_COMPOUND :
  280.         if (compoundbox_shown) 
  281.             draw_compoundbox(saved_objects.compounds, INV_PAINT);
  282.         erase_compound(saved_objects.compounds);
  283.         flip_compound(saved_objects.compounds, 
  284.             last_position.x, last_position.y,
  285.             last_axis);
  286.         draw_compound(saved_objects.compounds);
  287.         if (compoundbox_shown) 
  288.             draw_compoundbox(saved_objects.compounds, INV_PAINT);
  289.         break;
  290.         }
  291.     }
  292.  
  293. undo_move()
  294. {
  295.     int    dx, dy;
  296.  
  297.     dx = last_position.x - new_position.x;
  298.     dy = last_position.y - new_position.y;
  299.     switch (last_object) {
  300.         case O_POLYLINE :
  301.         if (pointmarker_shown)
  302.             toggle_linepointmarker(saved_objects.lines);
  303.         draw_line(saved_objects.lines, ERASE);
  304.         translate_line(saved_objects.lines, dx, dy);
  305.         draw_line(saved_objects.lines, PAINT);
  306.         if (pointmarker_shown)
  307.             toggle_linepointmarker(saved_objects.lines);
  308.         break;
  309.         case O_ELLIPSE :
  310.         if (pointmarker_shown)
  311.             toggle_ellipsepointmarker(saved_objects.ellipses);
  312.         draw_ellipse(saved_objects.ellipses, background_color);
  313.         translate_ellipse(saved_objects.ellipses, dx, dy);
  314.         draw_ellipse(saved_objects.ellipses, foreground_color);
  315.         if (pointmarker_shown)
  316.             toggle_ellipsepointmarker(saved_objects.ellipses);
  317.         break;
  318.         case O_TEXT :
  319.         draw_text(saved_objects.texts, INV_PAINT);
  320.         translate_text(saved_objects.texts, dx, dy);
  321.         draw_text(saved_objects.texts, PAINT);
  322.         break;
  323.         case O_SPLINE :
  324.         if (pointmarker_shown)
  325.             toggle_splinepointmarker(saved_objects.splines);
  326.         draw_spline(saved_objects.splines, ERASE);
  327.         translate_spline(saved_objects.splines, dx, dy);
  328.         draw_spline(saved_objects.splines, PAINT);
  329.         if (pointmarker_shown)
  330.             toggle_splinepointmarker(saved_objects.splines);
  331.         break;
  332.         case O_ARC :
  333.         if (pointmarker_shown)
  334.             toggle_arcpointmarker(saved_objects.arcs);
  335.         draw_arc(saved_objects.arcs, background_color);
  336.         translate_arc(saved_objects.arcs, dx, dy);
  337.         draw_arc(saved_objects.arcs, foreground_color);
  338.         if (pointmarker_shown)
  339.             toggle_arcpointmarker(saved_objects.arcs);
  340.         break;
  341.         case O_COMPOUND :
  342.         if (compoundbox_shown) 
  343.             draw_compoundbox(saved_objects.compounds, INV_PAINT);
  344.         erase_compound(saved_objects.compounds);
  345.         translate_compound(saved_objects.compounds, dx, dy);
  346.         draw_compound(saved_objects.compounds);
  347.         if (compoundbox_shown) 
  348.             draw_compoundbox(saved_objects.compounds, INV_PAINT);
  349.         break;
  350.         }
  351.     swap_newp_lastp();
  352.     }
  353.  
  354. undo_movepoint()
  355. {
  356.     switch (last_object) {
  357.         case O_POLYLINE :
  358.         relocate_linepoint(saved_objects.lines, 
  359.             last_position.x, last_position.y, 
  360.             saved_objects.lines->points->x,
  361.             saved_objects.lines->points->y, 
  362.             moved_point, left_point);
  363.         break;
  364.         case O_SPLINE :
  365.         relocate_splinepoint(saved_objects.splines, last_position.x,
  366.             last_position.y, moved_point);
  367.         break;
  368.         case O_ARC :
  369.         relocate_arcpoint(saved_objects.arcs, last_position.x, 
  370.             last_position.y, movedpoint_num);
  371.         break;
  372.         case O_ELLIPSE :
  373.         relocate_ellipsepoint(saved_objects.ellipses, last_position.x,
  374.             last_position.y, movedpoint_num);
  375.         break;
  376.         }
  377.     swap_newp_lastp();
  378.     }
  379.  
  380. undo_rotate()
  381. {
  382.     switch (last_object) {
  383.         case O_POLYLINE :
  384.         if (pointmarker_shown)
  385.             toggle_linepointmarker(saved_objects.lines);
  386.         draw_line(saved_objects.lines, ERASE);
  387.         if (last_rotateangle == 90) last_rotateangle = 270;
  388.         else last_rotateangle = 90;
  389.         rotate_line(saved_objects.lines, 
  390.             last_position.x, last_position.y,
  391.             last_rotateangle);
  392.         draw_line(saved_objects.lines, PAINT);
  393.         if (pointmarker_shown)
  394.             toggle_linepointmarker(saved_objects.lines);
  395.         break;
  396.         case O_ELLIPSE :
  397.         if (pointmarker_shown)
  398.             toggle_ellipsepointmarker(saved_objects.ellipses);
  399.         draw_ellipse(saved_objects.ellipses, background_color);
  400.         if (last_rotateangle == 90) last_rotateangle = 270;
  401.         else last_rotateangle = 90;
  402.         rotate_ellipse(saved_objects.ellipses, 
  403.             last_position.x, last_position.y,
  404.             last_rotateangle);
  405.         draw_ellipse(saved_objects.ellipses, foreground_color);
  406.         if (pointmarker_shown)
  407.             toggle_ellipsepointmarker(saved_objects.ellipses);
  408.         break;
  409.         case O_SPLINE :
  410.         if (pointmarker_shown)
  411.             toggle_splinepointmarker(saved_objects.splines);
  412.         draw_spline(saved_objects.splines, ERASE);
  413.         if (last_rotateangle == 90) last_rotateangle = 270;
  414.         else last_rotateangle = 90;
  415.         rotate_spline(saved_objects.splines,
  416.             last_position.x, last_position.y,
  417.             last_rotateangle);
  418.         draw_spline(saved_objects.splines, PAINT);
  419.         if (pointmarker_shown)
  420.             toggle_splinepointmarker(saved_objects.splines);
  421.         break;
  422.         case O_ARC :
  423.         if (pointmarker_shown)
  424.             toggle_arcpointmarker(saved_objects.arcs);
  425.         draw_arc(saved_objects.arcs, background_color);
  426.         if (last_rotateangle == 90) last_rotateangle = 270;
  427.         else last_rotateangle = 90;
  428.         rotate_arc(saved_objects.arcs, 
  429.             last_position.x, last_position.y,
  430.             last_rotateangle);
  431.         draw_arc(saved_objects.arcs, foreground_color);
  432.         if (pointmarker_shown)
  433.             toggle_arcpointmarker(saved_objects.arcs);
  434.         break;
  435.         case O_COMPOUND :
  436.         if (compoundbox_shown)
  437.             draw_compoundbox(saved_objects.compounds, INV_PAINT);
  438.         erase_compound(saved_objects.compounds);
  439.         if (last_rotateangle == 90) last_rotateangle = 270;
  440.         else last_rotateangle = 90;
  441.         rotate_compound(saved_objects.compounds, 
  442.             last_position.x, last_position.y,
  443.             last_rotateangle);
  444.         draw_compound(saved_objects.compounds);
  445.         if (compoundbox_shown)
  446.             draw_compoundbox(saved_objects.compounds, INV_PAINT);
  447.         break;
  448.         }
  449.     }
  450.  
  451. undo_scale()
  452. {
  453.     float    scalex, scaley;
  454.  
  455.     if (compoundbox_shown)
  456.         draw_compoundbox(saved_objects.compounds, INV_PAINT);
  457.     erase_compound(saved_objects.compounds);
  458.     scalex = ((float)(last_position.x-fix_x)) / (new_position.x-fix_x);
  459.     scaley = ((float)(last_position.y-fix_y)) / (new_position.y-fix_y);
  460.     scale_compound(saved_objects.compounds, scalex, scaley, fix_x, fix_y);
  461.     draw_compound(saved_objects.compounds);
  462.     if (compoundbox_shown) draw_compoundbox(saved_objects.compounds, INV_PAINT);
  463.     swap_newp_lastp();
  464.     }
  465.  
  466. swap_newp_lastp()
  467. {
  468.     int    t;  /*  swap new_position and last_position  */
  469.  
  470.     t = new_position.x; 
  471.     new_position.x = last_position.x;
  472.     last_position.x = t;
  473.     t = new_position.y; 
  474.     new_position.y = last_position.y;
  475.     last_position.y = t;
  476.     }
  477.