home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / x / volume8 / xfig2.8 / part07 / copy.c < prev    next >
C/C++ Source or Header  |  1990-07-02  |  8KB  |  341 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 "alloc.h"
  13. #include "func.h"
  14. #include "object.h"
  15. #include "paintop.h"
  16.  
  17. #define            TOLERANCE    7
  18.  
  19. extern            (*canvas_kbd_proc)();
  20. extern            (*canvas_locmove_proc)();
  21. extern            (*canvas_leftbut_proc)();
  22. extern            (*canvas_middlebut_proc)();
  23. extern            (*canvas_rightbut_proc)();
  24. extern            (*return_proc)();
  25. extern            null_proc();
  26. extern            set_popupmenu();
  27. extern F_line        *line_search(), *copy_line();
  28. extern F_arc        *arc_search(), *copy_arc();
  29. extern F_ellipse    *ellipse_search(), *copy_ellipse();
  30. extern F_text        *text_search(), *copy_text();
  31. extern F_spline        *spline_search(), *copy_spline();
  32. extern F_compound    *compound_search(), *copy_compound();
  33.  
  34. extern F_compound    objects;
  35.  
  36. extern int        copy_selected();
  37. extern int        init_copy();
  38.  
  39. copy_selected()
  40. {
  41.     canvas_kbd_proc = null_proc;
  42.     canvas_locmove_proc = null_proc;
  43.     canvas_leftbut_proc = init_copy;
  44.     canvas_middlebut_proc = null_proc;
  45.     canvas_rightbut_proc = set_popupmenu;
  46.     return_proc = copy_selected;
  47.     set_cursor(&pick15_cursor);
  48.     reset_action_on();
  49.     }
  50.  
  51. init_copy(x, y)
  52. int    x, y;
  53. {
  54.     F_line        *l, *line;
  55.     F_ellipse    *e, *ellipse;
  56.     F_text        *t, *text;
  57.     F_spline    *s, *spline;
  58.     F_arc        *a, *arc;
  59.     F_compound    *c, *compound;
  60.     int        px, py;
  61.  
  62.     if ((c = compound_search(x, y, TOLERANCE, &px, &py)) != NULL) {
  63.         compound = copy_compound(c);
  64.         erase_pointmarker();
  65.         set_temp_cursor(&null_cursor);
  66.         win_setmouseposition(canvas_win, px, py);
  67.         clean_up();
  68.         set_action_object(F_CREATE, O_COMPOUND);
  69.         insert_compound(&objects.compounds, compound);
  70.         set_latestcompound(compound);
  71.         init_compounddragging(compound, px, py);
  72.         }
  73.     else if ((l = line_search(x, y, TOLERANCE, &px, &py)) != NULL) {
  74.         line = copy_line(l);
  75.         erase_pointmarker();
  76.         set_temp_cursor(&null_cursor);
  77.         win_setmouseposition(canvas_win, px, py);
  78.         clean_up();
  79.         set_action_object(F_CREATE, O_POLYLINE);
  80.         insert_line(&objects.lines, line);
  81.         set_latestline(line);
  82.         init_linedragging(line, px, py);
  83.         }
  84.     else if ((t = text_search(x, y)) != NULL) {
  85.         text = copy_text(t);
  86.         erase_pointmarker();
  87.         set_temp_cursor(&null_cursor);
  88.         clean_up();
  89.         set_action_object(F_CREATE, O_TEXT);
  90.         insert_text(&objects.texts, text);
  91.         set_latesttext(text);
  92.         init_textdragging(text, x, y);
  93.         }
  94.     else if ((e = ellipse_search(x, y, TOLERANCE, &px, &py)) != NULL) {
  95.         ellipse = copy_ellipse(e);
  96.         erase_pointmarker();
  97.         set_temp_cursor(&null_cursor);
  98.         win_setmouseposition(canvas_win, px, py);
  99.         clean_up();
  100.         set_action_object(F_CREATE, O_ELLIPSE);
  101.         insert_ellipse(&objects.ellipses, ellipse);
  102.         set_latestellipse(ellipse);
  103.         init_ellipsedragging(ellipse, px, py);
  104.         }
  105.     else if ((a = arc_search(x, y, TOLERANCE, &px, &py)) != NULL) {
  106.         arc = copy_arc(a);
  107.         erase_pointmarker();
  108.         set_temp_cursor(&null_cursor);
  109.         win_setmouseposition(canvas_win, px, py);
  110.         clean_up();
  111.         set_action_object(F_CREATE, O_ARC);
  112.         insert_arc(&objects.arcs, arc);
  113.         set_latestarc(arc);
  114.         init_arcdragging(arc, px, py);
  115.         }
  116.     else if ((s = spline_search(x, y, TOLERANCE, &px, &py)) != NULL) {
  117.         spline = copy_spline(s);
  118.         erase_pointmarker();
  119.         set_temp_cursor(&null_cursor);
  120.         win_setmouseposition(canvas_win, px, py);
  121.         clean_up();
  122.         set_action_object(F_CREATE, O_SPLINE);
  123.         insert_spline(&objects.splines, spline);
  124.         set_latestspline(spline);
  125.         init_splinedragging(spline, px, py);
  126.         }
  127.     else
  128.         return;
  129.     canvas_leftbut_proc = canvas_rightbut_proc = null_proc;
  130.     }
  131.  
  132. F_arc *
  133. copy_arc(a)
  134. F_arc    *a;
  135. {
  136.     F_arc    *arc;
  137.  
  138.     if (NULL == (Arc_malloc(arc))) {
  139.         put_msg(Err_mem);
  140.         return(NULL);
  141.         }
  142.     *arc = *a;
  143.     arc->next = NULL;
  144.     return(arc);
  145.     }
  146.  
  147. F_ellipse *
  148. copy_ellipse(e)
  149. F_ellipse    *e;
  150. {
  151.     F_ellipse    *ellipse;
  152.  
  153.     if (NULL == (Ellipse_malloc(ellipse))) {
  154.         put_msg(Err_mem);
  155.         return(NULL);
  156.         }
  157.     *ellipse = *e;
  158.     ellipse->next = NULL;
  159.     return(ellipse);
  160.     }
  161.  
  162. F_line *
  163. copy_line(l)
  164. F_line    *l;
  165. {
  166.     F_line    *line;
  167.     F_point    *p, *point, *last_point;
  168.  
  169.     if (NULL == (Line_malloc(line))) {
  170.         put_msg(Err_mem);
  171.         return(NULL);
  172.         }
  173.     *line = *l;
  174.     if (NULL == (Point_malloc(point))) {
  175.         put_msg(Err_mem);
  176.         free((char *)line);
  177.         return(NULL);
  178.         }
  179.     line->points = point;
  180.     last_point = point;
  181.     p = l->points;
  182.     *point = *p;
  183.     point->next = NULL;
  184.     for (p = p->next; p != NULL; p = p->next) {
  185.         last_point->next = Point_malloc(point);
  186.         if (point == NULL) return(NULL);
  187.         *point = *p;
  188.         point->next = NULL;
  189.         last_point = point;
  190.         }
  191.     line->next = NULL;
  192.     return(line);
  193.     }
  194.  
  195. F_spline *
  196. copy_spline(s)
  197. F_spline    *s;
  198. {
  199.     F_spline    *spline;
  200.     F_point        *p, *point, *last_point;
  201.     F_control    *cntrl_pnt, *cp, *last_cntrl_pnt;
  202.  
  203.     if (NULL == (Spline_malloc(spline))) {
  204.         put_msg(Err_mem);
  205.         return(NULL);
  206.         }
  207.     *spline = *s;
  208.     spline->next = NULL;
  209.  
  210.     if (NULL == (Point_malloc(point))) {
  211.         put_msg(Err_mem);
  212.         free((char *)spline);
  213.         return(NULL);
  214.         }
  215.     last_point = spline->points = point;
  216.     p = s->points;
  217.     *point = *p;
  218.     for (p = p->next; p != NULL; p = p->next) {
  219.         last_point->next = Point_malloc(point);
  220.         if (point == NULL) return(NULL);
  221.         *point = *p;
  222.         last_point = point;
  223.         }
  224.     last_point->next = NULL;
  225.  
  226.     spline->controls = NULL;
  227.     if (s->controls == NULL) return(spline);
  228.  
  229.     if (NULL == (Control_malloc(cntrl_pnt))) {
  230.         put_msg(Err_mem);
  231.         free((char *)spline);
  232.         free((char *)point);
  233.         return(NULL);
  234.         }
  235.     last_cntrl_pnt = spline->controls = cntrl_pnt;
  236.     cp = s->controls;
  237.     *cntrl_pnt = *cp;
  238.     for (cp = cp->next; cp != NULL; cp = cp->next) {
  239.         last_cntrl_pnt->next = Control_malloc(cntrl_pnt);
  240.         if (cntrl_pnt == NULL) return(NULL);
  241.         *cntrl_pnt = *cp;
  242.         last_cntrl_pnt = cntrl_pnt;
  243.         }
  244.     last_cntrl_pnt->next = NULL;
  245.  
  246.     return(spline);
  247.     }
  248.  
  249. F_text *
  250. copy_text(t)
  251. F_text    *t;
  252. {
  253.     F_text        *text;
  254.     extern char    *calloc();
  255.  
  256.     if (NULL == (Text_malloc(text))) {
  257.         put_msg(Err_mem);
  258.         return(NULL);
  259.         }
  260.     *text = *t;
  261.     text->cstring = calloc((unsigned)(strlen(t->cstring)+1), sizeof(char));
  262.     if (text->cstring == NULL) {
  263.         free((char*)text);
  264.         text = NULL;
  265.         put_msg(Err_mem);
  266.         return(NULL);
  267.         }
  268.     strcpy(text->cstring, t->cstring);
  269.     text->next = NULL;
  270.     return(text);
  271.     }
  272.  
  273. F_compound *
  274. copy_compound(c)
  275. F_compound    *c;
  276. {
  277.     F_ellipse    *e, *ee;
  278.     F_arc        *a, *aa;
  279.     F_line        *l, *ll;
  280.     F_spline    *s, *ss;
  281.     F_text        *t, *tt;
  282.     F_compound    *cc, *ccc, *compound;
  283.  
  284.     if (NULL == (Compound_malloc(compound))) {
  285.         put_msg(Err_mem);
  286.         return(NULL);
  287.         }
  288.     compound->nwcorner = c->nwcorner;
  289.     compound->secorner = c->secorner;
  290.     compound->arcs = NULL;
  291.     compound->ellipses = NULL;
  292.     compound->lines = NULL;
  293.     compound->splines = NULL;
  294.     compound->texts = NULL;
  295.     compound->compounds = NULL;
  296.     compound->next = NULL;
  297.     for (e = c->ellipses; e != NULL; e = e->next) {
  298.         if (NULL == (ee = copy_ellipse(e))) {
  299.         put_msg(Err_mem);
  300.         return(NULL);
  301.         }
  302.         insert_ellipse(&compound->ellipses, ee);
  303.         }
  304.     for (a = c->arcs; a != NULL; a = a->next) {
  305.         if (NULL == (aa = copy_arc(a))) {
  306.         put_msg(Err_mem);
  307.         return(NULL);
  308.         }
  309.         insert_arc(&compound->arcs, aa);
  310.         }
  311.     for (l = c->lines; l != NULL; l = l->next) {
  312.         if (NULL == (ll = copy_line(l))) {
  313.         put_msg(Err_mem);
  314.         return(NULL);
  315.         }
  316.         insert_line(&compound->lines, ll);
  317.         }
  318.     for (s = c->splines; s != NULL; s = s->next) {
  319.         if (NULL == (ss = copy_spline(s))) {
  320.         put_msg(Err_mem);
  321.         return(NULL);
  322.         }
  323.         insert_spline(&compound->splines, ss);
  324.         }
  325.     for (t = c->texts; t != NULL; t = t->next) {
  326.         if (NULL == (tt = copy_text(t))) {
  327.         put_msg(Err_mem);
  328.         return(NULL);
  329.         }
  330.         insert_text(&compound->texts, tt);
  331.         }
  332.     for (cc = c->compounds; cc != NULL; cc = cc->next) {
  333.         if (NULL == (ccc = copy_compound(cc))) {
  334.         put_msg(Err_mem);
  335.         return(NULL);
  336.         }
  337.         insert_compound(&compound->compounds, ccc);
  338.         }
  339.     return(compound);
  340.     }
  341.