home *** CD-ROM | disk | FTP | other *** search
/ Borland Programmer's Resource / Borland_Programmers_Resource_CD_1995.iso / code / wxwin140 / samples / objects / graphics.h < prev    next >
Encoding:
C/C++ Source or Header  |  1995-05-19  |  18.2 KB  |  624 lines

  1. /*
  2.  * File:     graphics.h
  3.  * Purpose:  Object graphics library for wxWindows.
  4.  *           Defines a canvas which repaints its own graphics objects.
  5.  *           Sorry, this library is not documented but you should be able to work
  6.  *           most of it out, with help from the `objects' demo.
  7.  *
  8.  *                       wxWindows 1.40
  9.  * Copyright (c) 1993 Artificial Intelligence Applications Institute,
  10.  *                   The University of Edinburgh
  11.  *
  12.  *                     Author: Julian Smart
  13.  *                        Date: 18-4-93
  14.  *
  15.  * Permission to use, copy, modify, and distribute this software and its
  16.  * documentation for any purpose is hereby granted without fee, provided
  17.  * that the above copyright notice, author statement and this permission
  18.  * notice appear in all copies of this software and related documentation.
  19.  *
  20.  * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND, EXPRESS,
  21.  * IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF
  22.  * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
  23.  *
  24.  * IN NO EVENT SHALL THE ARTIFICIAL INTELLIGENCE APPLICATIONS INSTITUTE OR THE
  25.  * UNIVERSITY OF EDINBURGH BE LIABLE FOR ANY SPECIAL, INCIDENTAL, INDIRECT OR
  26.  * CONSEQUENTIAL DAMAGES OF ANY KIND, OR ANY DAMAGES WHATSOEVER RESULTING FROM
  27.  * LOSS OF USE, DATA OR PROFITS, WHETHER OR NOT ADVISED OF THE POSSIBILITY OF
  28.  * DAMAGE, AND ON ANY THEORY OF LIABILITY, ARISING OUT OF OR IN CONNECTION WITH
  29.  * THE USE OR PERFORMANCE OF THIS SOFTWARE.
  30.  *
  31.  */
  32.  
  33. #ifndef hy_graphicsh
  34. #define hy_graphicsh
  35.  
  36. // Key identifiers
  37. #define KEY_SHIFT 1
  38. #define KEY_CTRL  2
  39.  
  40. #define CONTROL_POINT_SIZE       6
  41.  
  42. // Control point types
  43. // Rectangle and most other shapes
  44. #define CONTROL_POINT_VERTICAL   1
  45. #define CONTROL_POINT_HORIZONTAL 2
  46. #define CONTROL_POINT_DIAGONAL   3
  47.  
  48. // Line
  49. #define CONTROL_POINT_ENDPOINT_TO 4
  50. #define CONTROL_POINT_ENDPOINT_FROM 5
  51. #define CONTROL_POINT_LINE       6
  52.  
  53.  
  54. class ObjectCanvas;
  55. class LineObject;
  56. class ControlPoint;
  57.  
  58. /*
  59.  * The basic canvas object, defining all the shared members
  60.  *
  61.  */
  62.  
  63. class CanvasObject: public wxObject
  64. {
  65.   wxObject *ClientData;
  66.  public:
  67.   Bool draggable;
  68.   Bool formatted;
  69.   float xpos, ypos;
  70.   wxPen *pen;
  71.   wxBrush *brush;
  72.   wxFont *font;
  73.   wxColour *text_colour;
  74.   ObjectCanvas *canvas;
  75.   wxDC *dc;
  76.   wxList lines;
  77.   wxList text;
  78.   wxList control_points; // Control points for when the object is selected
  79.   Bool visible;
  80.   Bool disable_label;
  81.   long id;
  82.   Bool selected;
  83.   Bool attachment_mode;  // TRUE if using attachments, FALSE otherwise
  84.  
  85.   CanvasObject(void);
  86.   CanvasObject(ObjectCanvas *can);
  87.   virtual ~CanvasObject(void);
  88.   virtual void GetBoundingBox(float *width, float *height);
  89.   virtual Bool GetPerimeterPoint(float x1, float y1,
  90.                                  float x2, float y2,
  91.                                  float *x3, float *y3);
  92.   ObjectCanvas *GetCanvas(void);
  93.   void SetCanvas(ObjectCanvas *canvas);
  94.   float GetX(void);
  95.   float GetY(void);
  96.  
  97.   virtual void OnDraw(void);
  98.   virtual void OnDrawContents(void);
  99.   virtual void OnMoveLinks(void);
  100.   virtual void OnErase(void);
  101.   virtual void OnHighlight(void);
  102.   virtual void OnLeftClick(float x, float y, int keys = 0, int attachment = 0);
  103.   virtual void OnRightClick(float x, float y, int keys = 0, int attachment = 0);
  104.   virtual void OnSize(float x, float y);
  105.   virtual void OnMove(float x, float y, float old_x, float old_y);
  106.  
  107.   virtual void OnDragLeft(Bool draw, float x, float y, int keys=0, int attachment = 0); // Erase if draw false
  108.   virtual void OnBeginDragLeft(float x, float y, int keys=0, int attachment = 0);
  109.   virtual void OnEndDragLeft(float x, float y, int keys=0, int attachment = 0);
  110.   virtual void OnDragRight(Bool draw, float x, float y, int keys=0, int attachment = 0); // Erase if draw false
  111.   virtual void OnBeginDragRight(float x, float y, int keys=0, int attachment = 0);
  112.   virtual void OnEndDragRight(float x, float y, int keys=0, int attachment = 0);
  113.   virtual void OnDrawOutline(void);
  114.   virtual void OnDrawControlPoints(void);
  115.   virtual void OnEraseControlPoints(void);
  116.  
  117.   virtual void MakeControlPoints(void);
  118.   virtual void DeleteControlPoints(void);
  119.   virtual void ResetControlPoints(void);
  120.   virtual void Select(Bool select = TRUE);
  121.   virtual Bool Selected(void);
  122.  
  123.   virtual Bool HitTest(float x, float y, int *attachment, float *distance);
  124.  
  125.   void SetPen(wxPen *pen);
  126.   void SetBrush(wxBrush *brush);
  127.   void SetFont(wxFont *font);
  128.   void SetClientData(wxObject *client_data);
  129.   wxObject *GetClientData(void);
  130.  
  131.   virtual void Show(Bool show);
  132.   virtual void Draggable(Bool truth);
  133.   virtual void Move(float x1, float y1);
  134.   virtual void Erase(void);
  135.   virtual void Draw(void);
  136.   virtual void Flash(void);
  137.   virtual void MoveLinks(void);
  138.   virtual void DrawContents(void);  // E.g. for drawing text label
  139.   virtual void SetSize(float x, float y);
  140.   void Attach(ObjectCanvas *can);
  141.   void Detach(void);
  142.   void SetDC(wxDC *the_dc);
  143.  
  144.   void AddLine(LineObject *line, CanvasObject *other);
  145.   void AddText(char *string);
  146.   virtual void FormatText(char *s);
  147.   void ClearText(void);
  148.   void RemoveLine(LineObject *line);
  149.  
  150.   // Make a copy
  151.   virtual void Copy(CanvasObject& copy);
  152.  
  153.   // Attachment code
  154.   virtual void GetAttachmentPosition(int attachment, float *x, float *y,
  155.                                      int nth = 0, int no_arcs = 1);
  156.   virtual int GetNumberOfAttachments(void);
  157.   virtual void SetAttachmentMode(Bool flag);
  158.  
  159.   virtual void EraseLinks(int attachment = -1);
  160.   virtual void DrawLinks(int attachment = -1);
  161.  
  162.   virtual void MoveLineToNewAttachment(LineObject *to_move,
  163.                                        float x, float y);
  164. };
  165.  
  166. /*
  167.  * Polygon object - has an arbitrary number of vertices
  168.  *
  169.  */
  170.  
  171. class PolygonObject: public CanvasObject
  172. {
  173.   wxList *points;
  174.   wxList *original_points;
  175.   float bound_width;
  176.   float bound_height;
  177.   float original_width;
  178.   float original_height;
  179.  public:
  180.  
  181.   PolygonObject(void);
  182.   ~PolygonObject(void);
  183.  
  184.   // Takes a list of wxPoints; each point is an OFFSET from the centre.
  185.   // Deletes user's points in destructor.
  186.   virtual void Create(wxList *points);
  187.  
  188.   void GetBoundingBox(float *w, float *h);
  189.   void CalculateBoundingBox(void);
  190.   Bool GetPerimeterPoint(float x1, float y1,
  191.                                  float x2, float y2,
  192.                                  float *x3, float *y3);
  193.   virtual void SetSize(float x, float y);
  194.   virtual void OnDraw(void);
  195.  
  196.   // Make a copy
  197.   void Copy(PolygonObject& copy);
  198.  
  199.   int GetNumberOfAttachments(void);
  200.   void GetAttachmentPosition(int attachment, float *x, float *y,
  201.                                      int nth = 0, int no_arcs = 1);
  202. };
  203.  
  204. /*
  205.  * Rectangle object (rounded/non-rounded)
  206.  *
  207.  */
  208.  
  209. class RectangleObject: public CanvasObject
  210. {
  211.  public:
  212.   float width;
  213.   float height;
  214.   float corner_radius;
  215.  
  216.   RectangleObject(float w, float h);
  217.   void GetBoundingBox(float *w, float *h);
  218.   Bool GetPerimeterPoint(float x1, float y1,
  219.                                  float x2, float y2,
  220.                                  float *x3, float *y3);
  221.   void OnDraw(void);
  222.   void SetSize(float x, float y);
  223.   virtual void SetCornerRadius(float rad); // If > 0, rounded corners
  224.  
  225.   // Make a copy
  226.   virtual void Copy(RectangleObject& copy);
  227.  
  228.   int GetNumberOfAttachments(void);
  229.   void GetAttachmentPosition(int attachment, float *x, float *y,
  230.                                      int nth = 0, int no_arcs = 1);
  231. };
  232.  
  233. /*
  234.  * Text object - text with no visible outline
  235.  *
  236.  */
  237.  
  238. class TextObject: public RectangleObject
  239. {
  240.  public:
  241.  
  242.   TextObject(float width, float height);
  243.  
  244.   void OnDraw(void);
  245. };
  246.  
  247.  
  248. /*
  249.  * Ellipse object
  250.  *
  251.  */
  252.  
  253. class EllipseObject: public CanvasObject
  254. {
  255.  public:
  256.   float width;
  257.   float height;
  258.  
  259.   EllipseObject(float w, float h);
  260.   void GetBoundingBox(float *w, float *h);
  261.   Bool GetPerimeterPoint(float x1, float y1,
  262.                                  float x2, float y2,
  263.                                  float *x3, float *y3);
  264.  
  265.   void OnDraw(void);
  266.   void SetSize(float x, float y);
  267.  
  268.   // Make a copy
  269.   virtual void Copy(EllipseObject& copy);
  270.  
  271.   int GetNumberOfAttachments(void);
  272.   void GetAttachmentPosition(int attachment, float *x, float *y,
  273.                                      int nth = 0, int no_arcs = 1);
  274. };
  275.  
  276. /*
  277.  * Circle object
  278.  *
  279.  */
  280.  
  281. class CircleObject: public EllipseObject
  282. {
  283.  public:
  284.   CircleObject(float w);
  285.   virtual Bool GetPerimeterPoint(float x1, float y1,
  286.                                  float x2, float y2,
  287.                                  float *x3, float *y3);
  288.  
  289. };
  290.  
  291. // Arrow styles
  292. #define ARROW_NONE         0
  293. #define ARROW_ONE          1
  294. #define ARROW_BOTH         2
  295. #define ARROW_MIDDLE       3
  296.  
  297. /*
  298.  * Line object
  299.  *
  300.  */
  301.  
  302. class LineObject: public CanvasObject
  303. {
  304.  public:
  305.   // These define the segmented line - not to be confused with temporary control
  306.   // points which appear when object is selected (although in this case they'll
  307.   // probably be the same)
  308.   wxList *line_control_points;
  309.  
  310.   float xpos1, ypos1, xpos2, ypos2;
  311.   Bool linked_up; // Flag used when loading from file
  312.  
  313.   // Arrow head styles
  314.   int start_style;
  315.   int end_style;
  316.   int middle_style;
  317.   float arrow_length;
  318.   float arrow_width;
  319.   CanvasObject *to;
  320.   CanvasObject *from;
  321.   float actual_text_width;  // Space the text takes up
  322.   float actual_text_height; // (depends on text content unlike nodes)
  323.  
  324.   int attachment_to;   // Attachment point at one end
  325.   int attachment_from; // Attachment point at other end
  326.  
  327.   LineObject(void);
  328.   LineObject(wxList *list);
  329.   ~LineObject(void);
  330.  
  331.   // Called when a connected object has moved, to move the link to
  332.   // correct position
  333.   void OnMoveLink(void);
  334.   void OnMove(float x, float y, float old_x, float old_y);
  335.   void OnDraw(void);
  336.   void OnDrawContents(void);
  337.   void OnErase(void);
  338.   void OnDrawOutline(void);
  339.   void GetBoundingBox(float *w, float *h);
  340.   virtual void SetEnds(float x1, float y1, float x2, float y2);
  341.   virtual void GetEnds(float *x1, float *y1, float *x2, float *y2);
  342.   virtual CanvasObject *GetFrom(void);
  343.   virtual CanvasObject *GetTo(void);
  344.   virtual void SetFrom(CanvasObject *object);
  345.   virtual void SetTo(CanvasObject *object);
  346.   virtual void DrawArrows(void);
  347.   void FormatText(char *s);
  348.  
  349.   // Straighten verticals and horizontals
  350.   virtual void Straighten(void);
  351.  
  352.   // Make handle control points
  353.   void MakeControlPoints(void);
  354.   void ResetControlPoints(void);
  355.  
  356.   // Make a given number of control points
  357.   virtual void MakeLineControlPoints(int n);
  358.   virtual void InsertLineControlPoint(void);
  359.   virtual Bool DeleteLineControlPoint(void);
  360.   virtual void Initialise(void);
  361.  
  362.   // Override dragging behaviour - don't want to be able to drag lines!
  363.   void OnDragLeft(Bool draw, float x, float y, int keys=0, int attachment = 0);
  364.   void OnBeginDragLeft(float x, float y, int keys=0, int attachment = 0);
  365.   void OnEndDragLeft(float x, float y, int keys=0, int attachment = 0);
  366.  
  367.   void SetArrowSize(float length, float width);
  368.   void SetStartArrow(int style);
  369.   void SetMiddleArrow(int style);
  370.   void SetEndArrow(int style);
  371.   void Unlink(void);
  372.   void SetAttachments(int from_attach, int to_attach);
  373.  
  374.   Bool HitTest(float x, float y, int *attachment, float *distance);
  375.  
  376.   // Make a copy
  377.   virtual void Copy(LineObject& copy);
  378.  
  379.   virtual void FindNth(CanvasObject *image, int *nth, int *no_arcs);
  380. };
  381.  
  382. /*
  383.  * Spline object
  384.  *
  385.  */
  386.  
  387. class SplineObject: public LineObject
  388. {
  389.  public:
  390.   SplineObject(void);
  391.   // Supply a list of initial control points. SplineObject will delete list
  392.   // when object deleted.
  393.   SplineObject(wxList *list);
  394.   ~SplineObject(void);
  395.  
  396.   virtual void OnDraw(void);
  397.  
  398.   Bool HitTest(float x, float y, int *attachment, float *distance);
  399.  
  400.   // Make a copy
  401.   virtual void Copy(SplineObject& copy);
  402. };
  403.  
  404. /*
  405.  * Control point. These black squares are ordinary canvas objects,
  406.  * so their behaviour may be defined in the same way as usual.
  407.  * However, they contain a pointer to the canvas object they are
  408.  * meant to control.
  409.  */
  410.  
  411. class ControlPoint: public RectangleObject
  412. {
  413.  public:
  414.  
  415.   int type;
  416.   float xoffset;
  417.   float yoffset;
  418.   CanvasObject *canvas_object;
  419.   wxCursor *old_cursor;
  420.  
  421.   ControlPoint(ObjectCanvas *the_canvas, CanvasObject *object, float size, float the_xoffset, float the_yoffset, int the_type);
  422.   ~ControlPoint(void);
  423.  
  424.   void OnDraw(void);
  425.   void OnErase(void);
  426.   void OnDrawContents(void);
  427.   void OnDragLeft(Bool draw, float x, float y, int keys=0, int attachment = 0);
  428.   void OnBeginDragLeft(float x, float y, int keys=0, int attachment = 0);
  429.   void OnEndDragLeft(float x, float y, int keys=0, int attachment = 0);
  430.  
  431.   void GetAttachmentPosition(int attachment, float *x, float *y,
  432.                                      int nth = 0, int no_arcs = 1);
  433.   int GetNumberOfAttachments(void);
  434. };
  435.  
  436. /*
  437.  * Line control point
  438.  *
  439.  */
  440.  
  441. class LineControlPoint: public ControlPoint
  442. {
  443.  public:
  444.  
  445.   int type;
  446.   wxPoint *point;  // Line point
  447.  
  448.   LineControlPoint(ObjectCanvas *the_canvas, CanvasObject *object, float size, float x, float y, int the_type);
  449.   ~LineControlPoint(void);
  450.  
  451.   void OnDraw(void);
  452.   void OnDragLeft(Bool draw, float x, float y, int keys=0, int attachment = 0);
  453.   void OnBeginDragLeft(float x, float y, int keys=0, int attachment = 0);
  454.   void OnEndDragLeft(float x, float y, int keys=0, int attachment = 0);
  455.  
  456.   void OnDragRight(Bool draw, float x, float y, int keys=0, int attachment = 0);
  457.   void OnBeginDragRight(float x, float y, int keys=0, int attachment = 0);
  458.   void OnEndDragRight(float x, float y, int keys=0, int attachment = 0);
  459. };
  460.  
  461. // Drag states
  462. #define NoDragging             0
  463. #define StartDraggingLeft      1
  464. #define ContinueDraggingLeft   2
  465. #define StartDraggingRight     3
  466. #define ContinueDraggingRight  4
  467.  
  468. /*
  469.  * Object canvas; maintains a list of objects
  470.  *
  471.  */
  472.  
  473. class ObjectCanvas: public wxCanvas
  474. {
  475.  public:
  476.   Bool quick_edit_mode;
  477.   Bool snap_to_grid;
  478.   float grid_spacing;
  479.  
  480.   wxList *object_list;
  481.   int DragState;
  482.   float old_drag_x, old_drag_y;  // Previous drag coordinates
  483.   float OutlineStartX;
  484.   float OutlineStartY;
  485.  
  486.   CanvasObject *DraggedObject;
  487.   int DraggedAttachment;
  488.  
  489.   ObjectCanvas(wxFrame *frame, int x = -1, int y = -1, int width = -1, int height = -1,
  490.                int style = wxRETAINED);
  491.   ~ObjectCanvas(void);
  492.  
  493.   void DrawOutline(float x1, float y1, float x2, float y2);
  494.  
  495.   virtual void OnPaint(void);
  496.   // This sends higher-level events to images or canvas
  497.   virtual void OnEvent(wxEvent& event);
  498.  
  499.   virtual void OnLeftClick(float x, float y, int keys = 0);
  500.   virtual void OnRightClick(float x, float y, int keys = 0);
  501.  
  502.   virtual void OnDragLeft(Bool draw, float x, float y, int keys=0); // Erase if draw false
  503.   virtual void OnBeginDragLeft(float x, float y, int keys=0);
  504.   virtual void OnEndDragLeft(float x, float y, int keys=0);
  505.  
  506.   virtual void OnDragRight(Bool draw, float x, float y, int keys=0); // Erase if draw false
  507.   virtual void OnBeginDragRight(float x, float y, int keys=0);
  508.   virtual void OnEndDragRight(float x, float y, int keys=0);
  509.  
  510.   virtual void Redraw(void); // Called automatically by default OnPaint handler
  511.   virtual void Clear(void);
  512.  
  513.   // Add object to end of object list
  514.   virtual void AddObject(CanvasObject *object);
  515.  
  516.   // Add object to front of object list
  517.   virtual void InsertObject(CanvasObject *object);
  518.  
  519.   void SetSnapToGrid(Bool snap);
  520.   void SetGridSpacing(float spacing);
  521.   void Snap(float *x, float *y);
  522.  
  523.   virtual void RemoveObject(CanvasObject *object);
  524.   virtual void RemoveAllObjects(void);
  525.   virtual void ShowAll(Bool show);
  526.   virtual CanvasObject *FindObject(float x, float y, int *attachment);  // Find object for mouse click
  527.  
  528. };
  529.  
  530. /*
  531.  * Defines a string plus a position
  532.  *
  533.  */
  534.  
  535. class CanvasObjectTextLine: public wxObject
  536. {
  537.  public:
  538.    float x;
  539.    float y;
  540.    char *line;
  541.    CanvasObjectTextLine(float the_x, float the_y, char *the_line);
  542.    ~CanvasObjectTextLine(void);
  543. };
  544.  
  545. /*
  546.  * Utility functions
  547.  *
  548.  */
  549.  
  550. void CentreText(wxDC *dc, wxList *text, float xpos, float ypos,
  551.                           float width, float height);
  552.  
  553. wxList *FormatText(wxDC *dc, char *text, float width, float height);
  554.   
  555. void CentreTextNoClipping(wxDC *dc, wxList *text_list,
  556.                               float xpos, float ypos, float width, float height);
  557. void GetCentredTextExtent(wxDC *dc, wxList *text_list,
  558.                               float xpos, float ypos, float width, float height,
  559.                               float *actual_width, float *actual_height);
  560. void DrawFormattedText(wxDC *context, wxList *text_list,
  561.                               float xpos, float ypos, float width, float height);
  562.  
  563. // Give it a list of points, finds the centre.
  564. void find_polyline_centroid(wxList *points, float *x, float *y);
  565.  
  566. void check_line_intersection(float x1, float y1, float x2, float y2, 
  567.                              float x3, float y3, float x4, float y4,
  568.                              float *ratio1, float *ratio2);
  569.  
  570. void find_end_for_polyline(float n, float xvec[], float yvec[], 
  571.                            float x1, float y1, float x2, float y2, float *x3, float *y3);
  572.  
  573.  
  574. void find_end_for_box(float width, float height, 
  575.                       float x1, float y1,         // Centre of box (possibly)
  576.                       float x2, float y2,         // other end of line
  577.                       float *x3, float *y3);      // End on box edge
  578.  
  579. void find_end_for_circle(float radius, 
  580.                          float x1, float y1,  // Centre of circle
  581.                          float x2, float y2,  // Other end of line
  582.                          float *x3, float *y3);
  583.  
  584. void get_arrow_points(float x1, float y1, float x2, float y2,
  585.                       float length, float width,
  586.                       float *tip_x, float *tip_y,
  587.                       float *side1_x, float *side1_y,
  588.                       float *side2_x, float *side2_y);
  589.  
  590. extern wxFont *normal_font;
  591. extern wxFont *italic_font;
  592. extern wxFont *swiss_font_4;
  593. extern wxFont *swiss_font_6;
  594. extern wxFont *swiss_font_8;
  595. extern wxFont *swiss_font_10;
  596. extern wxFont *swiss_font_12;
  597. extern wxFont *swiss_font_14;
  598. extern wxFont *swiss_font_18;
  599. extern wxFont *swiss_font_24;
  600. extern wxPen *red_pen;
  601. extern wxPen *cyan_pen;
  602. extern wxPen *green_pen;
  603. extern wxPen *black_pen;
  604.  
  605. extern wxBrush *blue_brush;
  606. extern wxBrush *green_brush;
  607. extern wxBrush *white_brush;
  608. extern wxBrush *black_brush;
  609. extern wxBrush *cyan_brush;
  610. extern wxBrush *red_brush;
  611.  
  612. extern wxPen *white_background_pen;
  613. extern wxBrush *transparent_brush;
  614. extern wxBrush *white_background_brush;
  615. extern wxPen *black_foreground_pen;
  616. extern wxPen *black_dashed_pen;
  617.  
  618. void UpdateListBox(wxListBox *item, wxList *list);
  619. void GraphicsInitialize(void);
  620. wxFont *FontSizeDialog(wxFrame *parent);
  621. wxFont *MatchFont(int point_size);
  622.  
  623. #endif // hy_graphicsh
  624.