home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-385-Vol-1of3.iso / i / iv26_w_3.zip / EXAMPLES / IDRAW / TOOLS.C < prev    next >
C/C++ Source or Header  |  1992-01-28  |  10KB  |  378 lines

  1. /*
  2.  * Copyright (c) 1987, 1988, 1989 Stanford University
  3.  *
  4.  * Permission to use, copy, modify, distribute, and sell this software and its
  5.  * documentation for any purpose is hereby granted without fee, provided
  6.  * that the above copyright notice appear in all copies and that both that
  7.  * copyright notice and this permission notice appear in supporting
  8.  * documentation, and that the name of Stanford not be used in advertising or
  9.  * publicity pertaining to distribution of the software without specific,
  10.  * written prior permission.  Stanford makes no representations about
  11.  * the suitability of this software for any purpose.  It is provided "as is"
  12.  * without express or implied warranty.
  13.  *
  14.  * STANFORD DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
  15.  * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS.
  16.  * IN NO EVENT SHALL STANFORD BE LIABLE FOR ANY SPECIAL, INDIRECT OR
  17.  * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
  18.  * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
  19.  * OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
  20.  * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  21.  */
  22.  
  23. // $Header: tools.c,v 1.8 89/10/09 14:50:03 linton Exp $
  24. // implements class Tools.
  25.  
  26. #include "editor.h"
  27. #include "keystrokes.h"
  28. #include "mapkey.h"
  29. #include "tools.h"
  30. #include <InterViews/box.h>
  31. #include <InterViews/event.h>
  32. #include <InterViews/painter.h>
  33. #include <InterViews/shape.h>
  34.  
  35. // An IdrawTool enters itself into the MapKey so Idraw can send
  36. // a KeyEvent to the right IdrawTool.
  37.  
  38. class IdrawTool : public PanelItem {
  39. public:
  40.     IdrawTool(Panel*, const char*, char, Editor*, MapKey*);
  41. protected:
  42.     Editor* editor;        // handles drawing and editing operations
  43. };
  44.  
  45. // IdrawTool stores the editor pointer and enters itself and its
  46. // associated character into the MapKey.
  47.  
  48. IdrawTool::IdrawTool (Panel* p, const char* n, char c, Editor* e,
  49. MapKey* mapkey) : (p, n, mapkey->ToStr(c), c) {
  50.     editor = e;
  51.     mapkey->Enter(this, c);
  52. }
  53.  
  54. // A SelectTool selects a set of Selections.
  55.  
  56. class SelectTool : public IdrawTool {
  57. public:
  58.     SelectTool (Panel* p, Editor* e, MapKey* mk)
  59.     : (p,  "Select", SELECTCHAR, e, mk) {}
  60.     void Perform (Event& e) {
  61.     editor->HandleSelect(e);
  62.     }
  63. };
  64.  
  65. // A MoveTool moves a set of Selections.
  66.  
  67. class MoveTool : public IdrawTool {
  68. public:
  69.     MoveTool (Panel* p, Editor* e, MapKey* mk)
  70.     : (p,  "Move", MOVECHAR, e, mk) {}
  71.     void Perform (Event& e) {
  72.     editor->HandleMove(e);
  73.     }
  74. };
  75.  
  76. // A ScaleTool scales a set of Selections.
  77.  
  78. class ScaleTool : public IdrawTool {
  79. public:
  80.     ScaleTool (Panel* p, Editor* e, MapKey* mk)
  81.     : (p,  "Scale", SCALECHAR, e, mk) {}
  82.     void Perform (Event& e) {
  83.     editor->HandleScale(e);
  84.     }
  85. };
  86.  
  87. // A StretchTool stretches a set of Selections.
  88.  
  89. class StretchTool : public IdrawTool {
  90. public:
  91.     StretchTool (Panel* p, Editor* e, MapKey* mk)
  92.     : (p,  "Stretch", STRETCHCHAR, e, mk) {}
  93.     void Perform (Event& e) {
  94.     editor->HandleStretch(e);
  95.     }
  96. };
  97.  
  98. // A RotateTool rotates a set of Selections.
  99.  
  100. class RotateTool : public IdrawTool {
  101. public:
  102.     RotateTool (Panel* p, Editor* e, MapKey* mk)
  103.     : (p,  "Rotate", ROTATECHAR, e, mk) {}
  104.     void Perform (Event& e) {
  105.     editor->HandleRotate(e);
  106.     }
  107. };
  108.  
  109. // A ReshapeTool reshapes a Selection.
  110.  
  111. class ReshapeTool : public IdrawTool {
  112. public:
  113.     ReshapeTool (Panel* p, Editor* e, MapKey* mk)
  114.     : (p,  "Reshape", RESHAPECHAR, e, mk) {}
  115.     void Perform (Event& e) {
  116.     editor->HandleReshape(e);
  117.     }
  118. };
  119.  
  120. // A MagnifyTool magnifies a part of the drawing.
  121.  
  122. class MagnifyTool : public IdrawTool {
  123. public:
  124.     MagnifyTool (Panel* p, Editor* e, MapKey* mk)
  125.     : (p,  "Magnify", MAGNIFYCHAR, e, mk) {}
  126.     void Perform (Event& e) {
  127.     editor->HandleMagnify(e);
  128.     }
  129. };
  130.  
  131. // A TextTool draws some text.
  132.  
  133. class TextTool : public IdrawTool {
  134. public:
  135.     TextTool (Panel* p, Editor* e, MapKey* mk)
  136.     : (p,  "Text", TEXTCHAR, e, mk) {}
  137.     void Perform (Event& e) {
  138.     editor->HandleText(e);
  139.     }
  140. };
  141.  
  142. // A LineTool draws a line.
  143.  
  144. class LineTool : public IdrawTool {
  145. public:
  146.     LineTool (Panel* p, Editor* e, MapKey* mk)
  147.     : (p,  "", LINECHAR, e, mk) {}
  148.     void Perform (Event& e) {
  149.     editor->HandleLine(e);
  150.     }
  151. protected:
  152.     void Redraw (Coord l, Coord b, Coord r, Coord t) {
  153.     IdrawTool::Redraw(l, b, r, t);
  154.     Coord x0 = offx + side * 1/5;
  155.     Coord y0 = offy + side * 4/5;
  156.     Coord x1 = offx + side * 4/5;
  157.     Coord y1 = offy + side * 1/5;
  158.     output->Line(canvas, x0, y0, x1, y1);
  159.     }
  160. };
  161.  
  162. // A MultiLineTool draws a set of connected lines.
  163.  
  164. class MultiLineTool : public IdrawTool {
  165. public:
  166.     MultiLineTool (Panel* p, Editor* e, MapKey* mk)
  167.     : (p,  "", MULTILINECHAR, e, mk) {}
  168.     void Perform (Event& e) {
  169.     editor->HandleMultiLine(e);
  170.     }
  171. protected:
  172.     void Redraw (Coord l, Coord b, Coord r, Coord t) {
  173.     IdrawTool::Redraw(l, b, r, t);
  174.     const int N = 4;
  175.     Coord x[N];
  176.     Coord y[N];
  177.     x[0] = offx + side * 1/5;
  178.     y[0] = offy + side * 4/5;
  179.     x[1] = offx + side * 1/2;
  180.     y[1] = offy + side * 4/5 - side * 1/10;
  181.     x[2] = offx + side * 1/2;
  182.     y[2] = offy + side * 1/5 + side * 1/10;
  183.     x[3] = offx + side * 4/5;
  184.     y[3] = offy + side * 1/5;
  185.     output->MultiLine(canvas, x, y, N);
  186.     }
  187. };
  188.  
  189. // A BSplineTool draws an open B-spline.
  190.  
  191. class BSplineTool : public IdrawTool {
  192. public:
  193.     BSplineTool (Panel* p, Editor* e, MapKey* mk)
  194.     : (p,  "", BSPLINECHAR, e, mk) {}
  195.     void Perform (Event& e) {
  196.     editor->HandleBSpline(e);
  197.     }
  198. protected:
  199.     void Redraw (Coord l, Coord b, Coord r, Coord t) {
  200.     IdrawTool::Redraw(l, b, r, t);
  201.     const int N = 4;
  202.     Coord x[N];
  203.     Coord y[N];
  204.     x[0] = offx + side * 1/5;
  205.     y[0] = offy + side * 4/5;
  206.     x[1] = offx + side * 1/2;
  207.     y[1] = offy + side * 4/5;
  208.     x[2] = offx + side * 1/2;
  209.     y[2] = offy + side * 1/5;
  210.     x[3] = offx + side * 4/5;
  211.     y[3] = offy + side * 1/5;
  212.     output->BSpline(canvas, x, y, N);
  213.     }
  214. };
  215.  
  216. // An EllipseTool draws an ellipse.
  217.  
  218. class EllipseTool : public IdrawTool {
  219. public:
  220.     EllipseTool (Panel* p, Editor* e, MapKey* mk)
  221.     : (p,  "", ELLIPSECHAR, e, mk) {}
  222.     void Perform (Event& e) {
  223.     editor->HandleEllipse(e);
  224.     }
  225. protected:
  226.     void Redraw (Coord l, Coord b, Coord r, Coord t) {
  227.     IdrawTool::Redraw(l, b, r, t);
  228.     Coord x0 = offx + side * 1/2;
  229.     Coord y0 = offy + side * 1/2;
  230.     Coord xradius = side * 1/3 + side * 1/16;
  231.     Coord yradius = side * 1/3 - side * 1/16;
  232.     output->Ellipse(canvas, x0, y0, xradius, yradius);
  233.     }
  234. };
  235.  
  236. // A RectTool draws a rectangle.
  237.  
  238. class RectTool : public IdrawTool {
  239. public:
  240.     RectTool (Panel* p, Editor* e, MapKey* mk)
  241.     : (p,  "", RECTCHAR, e, mk) {}
  242.     void Perform (Event& e) {
  243.     editor->HandleRect(e);
  244.     }
  245. protected:
  246.     void Redraw (Coord l, Coord b, Coord r, Coord t) {
  247.     IdrawTool::Redraw(l, b, r, t);
  248.     Coord x0 = offx + side * 1/5;
  249.     Coord y0 = offy + side * 1/5;
  250.     Coord x1 = offx + side * 4/5;
  251.     Coord y1 = offy + side * 4/5;
  252.     output->Rect(canvas, x0, y0, x1, y1);
  253.     }
  254. };
  255.  
  256. // A PolygonTool draws a polygon.
  257.  
  258. class PolygonTool : public IdrawTool {
  259. public:
  260.     PolygonTool (Panel* p, Editor* e, MapKey* mk)
  261.     : (p,  "", POLYGONCHAR, e, mk) {}
  262.     void Perform (Event& e) {
  263.     editor->HandlePolygon(e);
  264.     }
  265. protected:
  266.     void Redraw (Coord l, Coord b, Coord r, Coord t) {
  267.     IdrawTool::Redraw(l, b, r, t);
  268.     const int N = 5;
  269.     Coord x[N];
  270.     Coord y[N];
  271.     x[0] = offx + side * 1/5 + side * 1/8;
  272.     y[0] = offy + side * 1/5;
  273.     x[1] = offx + side * 1/5;
  274.     y[1] = offy + side * 1/2;
  275.     x[2] = offx + side * 1/2;
  276.     y[2] = offy + side * 4/5;
  277.     x[3] = offx + side * 4/5;
  278.     y[3] = offy + side * 1/2 + side * 1/8;
  279.     x[4] = offx + side * 4/5 - side * 1/32;
  280.     y[4] = offy + side * 1/5 + side * 1/8;
  281.     output->Polygon(canvas, x, y, N);
  282.     }
  283. };
  284.  
  285. // A ClosedBSplineTool draws a closed B-spline.
  286.  
  287. class ClosedBSplineTool : public IdrawTool {
  288. public:
  289.     ClosedBSplineTool (Panel* p, Editor* e, MapKey* mk)
  290.     : (p,  "", CLOSEDBSPLINECHAR, e, mk) {}
  291.     void Perform (Event& e) {
  292.     editor->HandleClosedBSpline(e);
  293.     }
  294. protected:
  295.     void Redraw (Coord l, Coord b, Coord r, Coord t) {
  296.     IdrawTool::Redraw(l, b, r, t);
  297.     const int N = 6;
  298.     Coord x[N];
  299.     Coord y[N];
  300.     x[0] = offx + side * 1/10;
  301.     y[0] = offy + side * 1/2;
  302.     x[1] = offx + side * 3/5;
  303.     y[1] = offy + side * 1/5;
  304.     x[2] = offx + side * 4/5;
  305.     y[2] = offy + side * 2/5;
  306.     x[3] = offx + side * 1/2;
  307.     y[3] = offy + side * 1/2;
  308.     x[4] = offx + side * 4/5;
  309.     y[4] = offy + side * 3/5;
  310.     x[5] = offx + side * 3/5;
  311.     y[5] = offy + side * 4/5;
  312.     output->ClosedBSpline(canvas, x, y, N);
  313.     }
  314. };
  315.  
  316. // Tools creates its tools.
  317.  
  318. Tools::Tools (Editor* e, MapKey* mk) {
  319.     Init(e, mk);
  320. }
  321.  
  322. // Handle tells one of the tools to perform its function if a
  323. // DownEvent occurs.
  324.  
  325. void Tools::Handle (Event& e) {
  326.     switch (e.eventType) {
  327.     case DownEvent:
  328.     switch (e.button) {
  329.     case LEFTMOUSE:
  330.         PerformCurrentFunction(e);
  331.         break;
  332.     case MIDDLEMOUSE:
  333.         PerformTemporaryFunction(e, MOVECHAR);
  334.         break;
  335.     case RIGHTMOUSE:
  336.         PerformTemporaryFunction(e, SELECTCHAR);
  337.         break;
  338.     default:
  339.         break;
  340.     }
  341.     default:
  342.     break;
  343.     }
  344. }
  345.  
  346. // Init creates the tools, lays them together, and inserts them.
  347.  
  348. void Tools::Init (Editor* e, MapKey* mk) {
  349.     PanelItem* first = new SelectTool(this, e, mk);
  350.  
  351.     VBox* tools = new VBox;
  352.     tools->Insert(first);
  353.     tools->Insert(new MoveTool(this, e, mk));
  354.     tools->Insert(new ScaleTool(this, e, mk));
  355.     tools->Insert(new StretchTool(this, e, mk));
  356.     tools->Insert(new RotateTool(this, e, mk));
  357.     tools->Insert(new ReshapeTool(this, e, mk));
  358.     tools->Insert(new MagnifyTool(this, e, mk));
  359.     tools->Insert(new TextTool(this, e, mk));
  360.     tools->Insert(new LineTool(this, e, mk));
  361.     tools->Insert(new MultiLineTool(this, e, mk));
  362.     tools->Insert(new BSplineTool(this, e, mk));
  363.     tools->Insert(new EllipseTool(this, e, mk));
  364.     tools->Insert(new RectTool(this, e, mk));
  365.     tools->Insert(new PolygonTool(this, e, mk));
  366.     tools->Insert(new ClosedBSplineTool(this, e, mk));
  367.  
  368.     Insert(tools);
  369.     SetCur(first);
  370. }
  371.  
  372. // Reconfig makes Tools's shape unstretchable but shrinkable.
  373.  
  374. void Tools::Reconfig () {
  375.     Panel::Reconfig();
  376.     shape->Rigid(0, 0, vfil, 0);
  377. }
  378.