home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / x / volume8 / xfig2.8 / part06 / deletept.c < prev    next >
C/C++ Source or Header  |  1990-07-02  |  6KB  |  224 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 "func.h"
  13. #include "object.h"
  14. #include "paintop.h"
  15.  
  16. #define            TOLERANCE    3
  17.  
  18. extern            (*canvas_kbd_proc)();
  19. extern            (*canvas_locmove_proc)();
  20. extern            (*canvas_leftbut_proc)();
  21. extern            (*canvas_middlebut_proc)();
  22. extern            (*canvas_rightbut_proc)();
  23. extern            null_proc();
  24. extern            set_popupmenu();
  25.  
  26. extern F_line        *line_point_search();
  27. extern F_spline        *spline_point_search();
  28. extern int        last_object;
  29. extern int        fix_x, fix_y, cur_x, cur_y;
  30. extern int        pointmarker_shown;
  31.  
  32. extern F_point        *left_point, *right_point;
  33. extern F_point        *deleted_point;
  34. extern F_line        *line;
  35. extern F_spline        *spline;
  36.  
  37. extern int        init_delete_point();
  38.  
  39. delete_point_selected()
  40. {
  41.     canvas_kbd_proc = null_proc;
  42.     canvas_locmove_proc = null_proc;
  43.     canvas_leftbut_proc = init_delete_point;
  44.     canvas_middlebut_proc = null_proc;
  45.     canvas_rightbut_proc = set_popupmenu;
  46.     set_cursor(&pick9_cursor);
  47.     reset_action_on();
  48.     }
  49.  
  50. init_delete_point(x, y)
  51. int     x, y;
  52. {
  53.     F_spline    *spline;
  54.     F_line        *line;
  55.     F_point        *p, *q;
  56.     int        n;
  57.  
  58.     /*
  59.     If the attemp to delete point fails, we wouldn't want any important
  60.     variables (left_point, right_point and delted_point) to change. 
  61.     So we used p and q in the search.
  62.     */
  63.  
  64.     if ((line = line_point_search(x, y, TOLERANCE, &p, &q)) != NULL) {
  65.         if (line->type == T_BOX || line->type == T_ARC_BOX) {
  66.         put_msg("Deleting box corners is not allowed");
  67.         return;
  68.         }
  69.         n = num_points(line->points);
  70.         if (line->type == T_POLYGON) {
  71.         if (n <= 4) {
  72.             put_msg("A polygon cannot have less than 3 points");
  73.             return;
  74.             }
  75.         }
  76.         else if (n <= 1) {
  77.         put_msg("A line cannot have less than 2 points");
  78.         return;
  79.         }
  80.         clean_up();
  81.         left_point = p;
  82.         deleted_point = q;
  83.         right_point = q->next;
  84.         linepoint_deleting(line);
  85.         set_action_object(F_DELETE_POINT, O_POLYLINE);
  86.         set_latestline(line);
  87.         }
  88.     else if ((spline=spline_point_search(x, y, TOLERANCE, &p, &q)) != NULL){
  89.         n = num_points(spline->points);
  90.         if (closed_spline(spline)) {
  91.         if (n <= 4) {
  92.             put_msg("A closed spline cannot have less than 3 points");
  93.             return;
  94.             }
  95.         }
  96.         else if (normal_spline(spline)) {
  97.         if (n <= 2) {
  98.             put_msg("A spline cannot have less than 2 points");
  99.             return;
  100.             }
  101.         }
  102.         else if (n <= 3) { /* it must be an interpolated spline */
  103.         put_msg("An interpolated spline cannot have less than 3 points");
  104.         return;
  105.         }
  106.         clean_up();
  107.         left_point = p;
  108.         deleted_point = q;
  109.         right_point = q->next;
  110.         splinepoint_deleting(spline);
  111.         set_action_object(F_DELETE_POINT, O_SPLINE);
  112.         set_latestspline(spline);
  113.         }
  114.     }
  115.  
  116. /**************************  spline  *******************************/
  117.  
  118. splinepoint_deleting(spline)
  119. F_spline    *spline;
  120. {
  121.     F_point        *p;
  122.  
  123.     set_temp_cursor(&wait_cursor);
  124.     if (closed_spline(spline)) {
  125.         if (pointmarker_shown) toggle_splinepointmarker(spline);
  126.         draw_spline(spline, ERASE); /*erase the spline */
  127.         if (left_point == NULL) { 
  128.         /* The deleted point is the first point */
  129.         spline->points = right_point;
  130.         for (left_point = right_point, p = left_point->next;
  131.             p->next != NULL;
  132.             left_point = p, p = p->next);
  133.         /*
  134.         left_point now points at next to last point
  135.         (the last point is a copy of the first).
  136.         */
  137.         p->x = spline->points->x;
  138.         p->y = spline->points->y;
  139.         right_point = p;
  140.         /*
  141.         Right_point becomes the last point.  If this operation
  142.         (point deletion) is reversed (undo), the deleted_point
  143.         will not be inserted into it original place, but will
  144.         be between left_point and right_point.
  145.         */
  146.         }
  147.         else
  148.         left_point->next = right_point;
  149.         }
  150.     else { /* open spline */
  151.         if (pointmarker_shown) toggle_splinepointmarker(spline);
  152.         draw_spline(spline, ERASE); /*erase the spline */
  153.         if (left_point == NULL) 
  154.         spline->points = right_point;
  155.         else 
  156.         left_point->next = right_point;
  157.         }
  158.     if (int_spline(spline)) {
  159.         F_control    *c;
  160.  
  161.         c = spline->controls;
  162.         spline->controls = c->next;
  163.         c->next = NULL;
  164.         free((char*)c);
  165.         remake_control_points(spline);
  166.         }
  167.     draw_spline(spline, PAINT);
  168.     if (pointmarker_shown) toggle_splinepointmarker(spline);
  169.     set_modifiedflag();
  170.     reset_cursor();
  171.     }
  172.  
  173. /***************************  line  ********************************/
  174.  
  175. /*
  176. In deleting a point p, linepoint_deleting uses left_point and
  177. right_point of point p.  The relationship of the three points
  178. are : left_point->p->right_point except when p is the first
  179. point in the list, in which case left_point will be NULL.
  180. */
  181. linepoint_deleting(line)
  182. F_line    *line;
  183. {
  184.     F_point    *p;
  185.  
  186.     if (line->type == T_POLYGON) {
  187.         if (pointmarker_shown) toggle_linepointmarker(line);
  188.         draw_line(line, ERASE); /*erase the line */
  189.         if (left_point == NULL) {
  190.         /* The deleted point is the first point */
  191.         line->points = right_point;
  192.         for (left_point = right_point, p = left_point->next; 
  193.             p->next != NULL; 
  194.             left_point = p, p = p->next);
  195.         /*
  196.         left_point now points at next to last point
  197.         (the last point is a copy of the first).
  198.         */
  199.         p->x = right_point->x;
  200.         p->y = right_point->y;
  201.         right_point = p;
  202.         /*
  203.         Right_point becomes the last point.  If this operation
  204.         (point deletion) is reversed (undo), the deleted_point
  205.         will not be inserted into it original place, but will
  206.         be between left_point and right_point.
  207.         */
  208.         }
  209.         else
  210.         left_point->next = right_point;
  211.         }
  212.     else { /* polyline */
  213.         if (pointmarker_shown) toggle_linepointmarker(line);
  214.         draw_line(line, ERASE); /*erase the line */
  215.         if (left_point == NULL) 
  216.         line->points = right_point;
  217.         else 
  218.         left_point->next = right_point;
  219.         }
  220.     draw_line(line, PAINT);
  221.     if (pointmarker_shown) toggle_linepointmarker(line);
  222.     set_modifiedflag();
  223.     }
  224.