home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / x / volume8 / xfig2.8 / part09 / flip.c < prev    next >
C/C++ Source or Header  |  1990-07-03  |  9KB  |  404 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    7
  17. #define            min(a, b)    (((a) < (b)) ? (a) : (b))
  18. #define            max(a, b)    (((a) > (b)) ? (a) : (b))
  19.  
  20. extern            (*canvas_kbd_proc)();
  21. extern            (*canvas_locmove_proc)();
  22. extern            (*canvas_leftbut_proc)();
  23. extern            (*canvas_middlebut_proc)();
  24. extern            (*canvas_rightbut_proc)();
  25. extern            null_proc();
  26. extern            set_popupmenu();
  27.  
  28. extern F_line        *line_search(), *copy_line();
  29. extern F_ellipse    *ellipse_search(), *copy_ellipse();
  30. extern F_ellipse    *ellipse_point_search();
  31. extern F_text        *copy_text();
  32. extern F_spline        *spline_search(), *copy_spline();
  33. extern F_arc        *arc_point_search(), *copy_arc();
  34. extern F_compound    *compound_search(), *copy_compound();
  35.  
  36. extern F_compound    objects;
  37.  
  38. extern int        flip_axis;
  39. extern int        pointmarker_shown;
  40. extern int        foreground_color, background_color;
  41.  
  42. static int        copy;
  43. extern int        init_flip();
  44. extern int        init_copynflip();
  45.  
  46. flip_selected()
  47. {
  48.     canvas_kbd_proc = null_proc;
  49.     canvas_locmove_proc = null_proc;
  50.     canvas_leftbut_proc = init_copynflip;
  51.     canvas_middlebut_proc = init_flip;
  52.     canvas_rightbut_proc = set_popupmenu;
  53.     set_cursor(&pick15_cursor);
  54.     }
  55.  
  56. init_flip(x, y)
  57. int    x, y;
  58. {
  59.     copy = 0;
  60.     flip_search(x, y);
  61.     flip_selected();
  62.     }
  63.  
  64. init_copynflip(x, y)
  65. int    x, y;
  66. {
  67.     copy = 1;
  68.     flip_search(x, y);
  69.     flip_selected();
  70.     }
  71.  
  72. flip_search(x, y)
  73. int    x, y;
  74. {
  75.     F_line        *l;
  76.     F_arc        *a;
  77.     F_ellipse    *e;
  78.     F_spline    *s;
  79.     F_compound    *c;
  80.     int        px, py;
  81.  
  82.     if ((l = line_search(x, y, TOLERANCE, &px, &py)) != NULL) {
  83.         init_flipline(l, px, py);
  84.         }
  85.     else if ((a = arc_point_search(x, y, TOLERANCE, &px)) != NULL) {
  86.         init_fliparc(a, a->point[px].x, a->point[px].y);
  87.         }
  88.     else if ((e = ellipse_point_search(x, y, TOLERANCE, &px)) != NULL) {
  89.         if (px == 0) /* start point */
  90.         init_flipellipse(e, e->start.x, e->start.y);
  91.         else
  92.         init_flipellipse(e, e->end.x, e->end.y);
  93.         }
  94.     else if ((e = ellipse_search(x, y, TOLERANCE, &px, &py)) != NULL) {
  95.         init_flipellipse(e, px, py);
  96.         }
  97.     else if ((s = spline_search(x, y, TOLERANCE, &px, &py)) != NULL) {
  98.         init_flipspline(s, px, py);
  99.         }
  100.     else if ((c = compound_search(x, y, TOLERANCE, &px, &py)) != NULL) {
  101.         init_flipcompound(c, px, py);
  102.         }
  103.     set_modifiedflag();
  104.     }
  105.  
  106. init_fliparc(a, px, py)
  107. F_arc    *a;
  108. int    px, py;
  109. {
  110.     F_arc    *arc;
  111.  
  112.     win_setmouseposition(canvas_win, px, py);
  113.     if (pointmarker_shown) toggle_arcpointmarker(a);
  114.     if (copy) {
  115.         arc = copy_arc(a);
  116.         flip_arc(arc, px, py, flip_axis);
  117.         clean_up();
  118.         set_action_object(F_CREATE, O_ARC);
  119.         insert_arc(&objects.arcs, arc);
  120.         }
  121.     else {
  122.         draw_arc(a, background_color);
  123.         flip_arc(a, px, py, flip_axis);
  124.         clean_up();
  125.         set_action_object(F_FLIP, O_ARC);
  126.         set_lastaxis(flip_axis);
  127.         set_lastposition(px, py);
  128.         arc = a;
  129.         }
  130.     draw_arc(arc, foreground_color);
  131.     if (pointmarker_shown) {
  132.         toggle_arcpointmarker(arc);
  133.         if (copy) toggle_arcpointmarker(a);
  134.         }
  135.     set_latestarc(arc);
  136.     }
  137.  
  138. init_flipcompound(c, px, py)
  139. F_compound    *c;
  140. int        px, py;
  141. {
  142.     F_compound    *compound;
  143.  
  144.     win_setmouseposition(canvas_win, px, py);
  145.     set_temp_cursor(&wait_cursor);
  146.     if (copy) {
  147.         compound = copy_compound(c);
  148.         flip_compound(compound, px, py, flip_axis);
  149.         clean_up();
  150.         set_action_object(F_CREATE, O_COMPOUND);
  151.         insert_compound(&objects.compounds, compound);
  152.         }
  153.     else {
  154.         draw_compoundbox(c, INV_PAINT);
  155.         erase_compound(c);
  156.         flip_compound(c, px, py, flip_axis);
  157.         clean_up();
  158.         set_action_object(F_FLIP, O_COMPOUND);
  159.         set_lastaxis(flip_axis);
  160.         set_lastposition(px, py);
  161.         compound = c;
  162.         }
  163.     draw_compoundbox(compound, INV_PAINT);
  164.     draw_compound(compound);
  165.     set_latestcompound(compound);
  166.     set_temp_cursor(cur_cursor);
  167.     }
  168.  
  169. init_flipellipse(e, px, py)
  170. F_ellipse    *e;
  171. {
  172.     F_ellipse    *ellipse;
  173.  
  174.     win_setmouseposition(canvas_win, px, py);
  175.     if (pointmarker_shown) toggle_ellipsepointmarker(e);
  176.     if (copy) {
  177.         ellipse = copy_ellipse(e);
  178.         flip_ellipse(ellipse, px, py, flip_axis);
  179.         clean_up();
  180.         set_action_object(F_CREATE, O_ELLIPSE);
  181.         insert_ellipse(&objects.ellipses, ellipse);
  182.         }
  183.     else {
  184.         draw_ellipse(e, background_color);
  185.         flip_ellipse(e, px, py, flip_axis);
  186.         clean_up();
  187.         set_action_object(F_FLIP, O_ELLIPSE);
  188.         set_lastaxis(flip_axis);
  189.         set_lastposition(px, py);
  190.         ellipse = e;
  191.         }
  192.     draw_ellipse(ellipse, foreground_color);
  193.     if (pointmarker_shown) {
  194.         toggle_ellipsepointmarker(ellipse);
  195.         if (copy) toggle_ellipsepointmarker(e);
  196.         }
  197.     set_latestellipse(ellipse);
  198.     }
  199.  
  200. init_flipline(l, px, py)
  201. F_line    *l;
  202. int    px, py;
  203. {
  204.     F_line    *line;
  205.  
  206.     win_setmouseposition(canvas_win, px, py);
  207.     if (pointmarker_shown) toggle_linepointmarker(l);
  208.     if (copy) {
  209.         line = copy_line(l);
  210.         flip_line(line, px, py, flip_axis);
  211.         clean_up();
  212.         set_action_object(F_CREATE, O_POLYLINE);
  213.         insert_line(&objects.lines, line);
  214.         }
  215.     else {
  216.         draw_line(l, ERASE);
  217.         flip_line(l, px, py, flip_axis);
  218.         clean_up();
  219.         set_action_object(F_FLIP, O_POLYLINE);
  220.         set_lastaxis(flip_axis);
  221.         set_lastposition(px, py);
  222.         line = l;
  223.         }
  224.     draw_line(line, PAINT);
  225.     if (pointmarker_shown) {
  226.         toggle_linepointmarker(line);
  227.         if (copy) toggle_linepointmarker(l);
  228.         }
  229.     set_latestline(line);
  230.     }
  231.  
  232. init_flipspline(s, px, py)
  233. F_spline    *s;
  234. int        px, py;
  235. {
  236.     F_spline    *spline;
  237.  
  238.     win_setmouseposition(canvas_win, px, py);
  239.     if (pointmarker_shown) toggle_splinepointmarker(s);
  240.     if (copy) {
  241.         spline = copy_spline(s);
  242.         flip_spline(spline, px, py, flip_axis);
  243.         clean_up();
  244.         set_action_object(F_CREATE, O_SPLINE);
  245.         insert_spline(&objects.splines, spline);
  246.         }
  247.     else {  /*  delete the original */
  248.         draw_spline(s, ERASE);
  249.         flip_spline(s, px, py, flip_axis);
  250.         clean_up();
  251.         set_action_object(F_FLIP, O_SPLINE);
  252.         set_lastaxis(flip_axis);
  253.         set_lastposition(px, py);
  254.         spline = s;
  255.         }
  256.     draw_spline(spline, PAINT);
  257.     if (pointmarker_shown) {
  258.         toggle_splinepointmarker(spline);
  259.         if (copy) toggle_splinepointmarker(s);
  260.         }
  261.     set_latestspline(spline);
  262.     }
  263.  
  264. flip_line(l, x, y, flip_axis)
  265. F_line    *l;
  266. int    x, y, flip_axis;
  267. {
  268.     F_point    *p;
  269.  
  270.     switch(flip_axis) {
  271.         case 1 :    /*  x axis  */
  272.         for (p = l->points; p != NULL; p = p->next)
  273.             p->y = y + (y - p->y);
  274.         break;
  275.         case 2 :    /*  y axis  */
  276.         for (p = l->points; p != NULL; p = p->next)
  277.             p->x = x + (x - p->x);
  278.         break;
  279.         }
  280.     }
  281.  
  282. flip_spline(s, x, y, flip_axis)
  283. F_spline    *s;
  284. int        x, y, flip_axis;
  285. {
  286.     F_point        *p;
  287.     F_control    *cp;
  288.  
  289.     switch(flip_axis) {
  290.         case 1 :    /*  x axis  */
  291.         for (p = s->points; p != NULL; p = p->next)
  292.             p->y = y + (y - p->y);
  293.         for (cp = s->controls; cp != NULL; cp = cp->next) {
  294.             cp->ly = y + (y - cp->ly);
  295.             cp->ry = y + (y - cp->ry);
  296.             }
  297.         break;
  298.         case 2 :    /*  y axis  */
  299.         for (p = s->points; p != NULL; p = p->next)
  300.             p->x = x + (x - p->x);
  301.         for (cp = s->controls; cp != NULL; cp = cp->next) {
  302.             cp->lx = x + (x - cp->lx);
  303.             cp->rx = x + (x - cp->rx);
  304.             }
  305.         break;
  306.         }
  307.     }
  308.  
  309. flip_text(t, x, y, flip_axis)
  310. F_text    *t;
  311. int    x, y, flip_axis;
  312. {
  313.     switch(flip_axis) {
  314.         case 1 :    /*  x axis  */
  315.         t->base_y = y + (y - t->base_y);
  316.         break;
  317.         case 2 :    /*  y axis  */
  318.         t->base_x = x + (x - t->base_x);
  319.         break;
  320.         }
  321.     }
  322.  
  323. flip_ellipse(e, x, y, flip_axis)
  324. F_ellipse    *e;
  325. int        x, y, flip_axis;
  326. {
  327.     switch(flip_axis) {
  328.         case 1 :    /*  x axis  */
  329.         e->direction ^= 1;
  330.         e->center.y = y + (y - e->center.y);
  331.         e->start.y = y + (y - e->start.y);
  332.         e->end.y = y + (y - e->end.y);
  333.         break;
  334.         case 2 :    /*  y axis  */
  335.         e->direction ^= 1;
  336.         e->center.x = x + (x - e->center.x);
  337.         e->start.x = x + (x - e->start.x);
  338.         e->end.x = x + (x - e->end.x);
  339.         break;
  340.         }
  341.     }
  342.  
  343. flip_arc(a, x, y, flip_axis)
  344. F_arc    *a;
  345. int    x, y, flip_axis;
  346. {
  347.     switch(flip_axis) {
  348.         case 1 :    /*  x axis  */
  349.         a->direction ^= 1;
  350.         a->center.y = y + (y - a->center.y);
  351.         a->point[0].y = y + (y - a->point[0].y);
  352.         a->point[1].y = y + (y - a->point[1].y);
  353.         a->point[2].y = y + (y - a->point[2].y);
  354.         break;
  355.         case 2 :    /*  y axis  */
  356.         a->direction ^= 1;
  357.         a->center.x = x + (x - a->center.x);
  358.         a->point[0].x = x + (x - a->point[0].x);
  359.         a->point[1].x = x + (x - a->point[1].x);
  360.         a->point[2].x = x + (x - a->point[2].x);
  361.         break;
  362.         }
  363.     }
  364.  
  365. flip_compound(c, x, y, flip_axis)
  366. F_compound    *c;
  367. int        x, y, flip_axis;
  368. {
  369.     F_line        *l;
  370.     F_arc        *a;
  371.     F_ellipse    *e;
  372.     F_spline    *s;
  373.     F_text        *t;
  374.     F_compound    *c1;
  375.     int        p, q;
  376.     
  377.     switch(flip_axis) {
  378.         case 1 :    /*  x axis  */
  379.         p = y + (y - c->nwcorner.y);
  380.         q = y + (y - c->secorner.y);
  381.         c->nwcorner.y = min(p, q);
  382.         c->secorner.y = max(p, q);
  383.         break;
  384.         case 2 :    /*  y axis  */
  385.         p = x + (x - c->nwcorner.x);
  386.         q = x + (x - c->secorner.x);
  387.         c->nwcorner.x = min(p, q);
  388.         c->secorner.x = max(p, q);
  389.         break;
  390.         }
  391.     for (l = c->lines; l != NULL; l = l->next)
  392.         flip_line(l, x, y, flip_axis);
  393.     for (a = c->arcs; a != NULL; a = a->next)
  394.         flip_arc(a, x, y, flip_axis);
  395.     for (e = c->ellipses; e != NULL; e = e->next)
  396.         flip_ellipse(e, x, y, flip_axis);
  397.     for (s = c->splines; s != NULL; s = s->next)
  398.         flip_spline(s, x, y, flip_axis);
  399.     for (t = c->texts; t != NULL; t = t->next)
  400.         flip_text(t, x, y, flip_axis);
  401.     for (c1 = c->compounds; c1 != NULL; c1 = c1->next)
  402.         flip_compound(c1, x, y, flip_axis);
  403.     }
  404.