1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: Basic OGL classes and definitions
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
16 #pragma interface "basic.h"
19 #define OGL_VERSION 2.0
21 #ifndef DEFAULT_MOUSE_TOLERANCE
22 #define DEFAULT_MOUSE_TOLERANCE 3
25 // Edit these lines if you positively don't want PROLOGIO support
39 #define ARROW_MIDDLE 3
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
49 #define CONTROL_POINT_ENDPOINT_TO 4
50 #define CONTROL_POINT_ENDPOINT_FROM 5
51 #define CONTROL_POINT_LINE 6
53 // Types of formatting: can be combined in a bit list
56 #define FORMAT_CENTRE_HORIZ 1
57 // Centre horizontally
58 #define FORMAT_CENTRE_VERT 2
60 #define FORMAT_SIZE_TO_CONTENTS 4
61 // Resize shape to contents
66 #define SHADOW_RIGHT 2
73 #define SHAPE_BASIC wxTYPE_USER + 1
74 #define SHAPE_RECTANGLE wxTYPE_USER + 2
75 #define SHAPE_ELLIPSE wxTYPE_USER + 3
76 #define SHAPE_POLYGON wxTYPE_USER + 4
77 #define SHAPE_CIRCLE wxTYPE_USER + 5
78 #define SHAPE_LINE wxTYPE_USER + 6
79 #define SHAPE_DIVIDED_RECTANGLE wxTYPE_USER + 8
80 #define SHAPE_COMPOSITE wxTYPE_USER + 9
81 #define SHAPE_CONTROL_POINT wxTYPE_USER + 10
82 #define SHAPE_DRAWN wxTYPE_USER + 11
83 #define SHAPE_DIVISION wxTYPE_USER + 12
84 #define SHAPE_LABEL_OBJECT wxTYPE_USER + 13
85 #define SHAPE_BITMAP wxTYPE_USER + 14
86 #define SHAPE_DIVIDED_OBJECT_CONTROL_POINT wxTYPE_USER + 15
88 #define OBJECT_REGION wxTYPE_USER + 20
90 #define OP_CLICK_LEFT 1
91 #define OP_CLICK_RIGHT 2
92 #define OP_DRAG_LEFT 4
93 #define OP_DRAG_RIGHT 8
95 #define OP_ALL (OP_CLICK_LEFT | OP_CLICK_RIGHT | OP_DRAG_LEFT | OP_DRAG_RIGHT)
97 class wxShapeTextLine
;
100 class wxControlPoint
;
109 class wxShapeEvtHandler
: public wxObject
111 DECLARE_DYNAMIC_CLASS(wxShapeEvtHandler
)
114 wxShapeEvtHandler(wxShapeEvtHandler
*prev
= NULL
, wxShape
*shape
= NULL
);
115 virtual ~wxShapeEvtHandler();
117 inline void SetHandlerShape(wxShape
*sh
) { m_handlerShape
= sh
; }
118 inline wxShape
*GetShape() const { return m_handlerShape
; }
120 // This is called when the _shape_ is deleted.
121 virtual void OnDelete();
122 virtual void OnDraw(wxDC
& dc
);
123 virtual void OnDrawContents(wxDC
& dc
);
124 virtual void OnMoveLinks(wxDC
& dc
);
125 virtual void OnErase(wxDC
& dc
);
126 virtual void OnEraseContents(wxDC
& dc
);
127 virtual void OnHighlight(wxDC
& dc
);
128 virtual void OnLeftClick(float x
, float y
, int keys
= 0, int attachment
= 0);
129 virtual void OnRightClick(float x
, float y
, int keys
= 0, int attachment
= 0);
130 virtual void OnSize(float x
, float y
);
131 virtual bool OnMovePre(wxDC
& dc
, float x
, float y
, float old_x
, float old_y
, bool display
= TRUE
);
132 virtual void OnMovePost(wxDC
& dc
, float x
, float y
, float old_x
, float old_y
, bool display
= TRUE
);
134 virtual void OnDragLeft(bool draw
, float x
, float y
, int keys
=0, int attachment
= 0); // Erase if draw false
135 virtual void OnBeginDragLeft(float x
, float y
, int keys
=0, int attachment
= 0);
136 virtual void OnEndDragLeft(float x
, float y
, int keys
=0, int attachment
= 0);
137 virtual void OnDragRight(bool draw
, float x
, float y
, int keys
=0, int attachment
= 0); // Erase if draw false
138 virtual void OnBeginDragRight(float x
, float y
, int keys
=0, int attachment
= 0);
139 virtual void OnEndDragRight(float x
, float y
, int keys
=0, int attachment
= 0);
140 virtual void OnDrawOutline(wxDC
& dc
, float x
, float y
, float w
, float h
);
141 virtual void OnDrawControlPoints(wxDC
& dc
);
142 virtual void OnEraseControlPoints(wxDC
& dc
);
143 virtual void OnMoveLink(wxDC
& dc
, bool moveControlPoints
= TRUE
);
145 virtual void OnBeginSize(float WXUNUSED(w
), float WXUNUSED(h
)) { }
146 virtual void OnEndSize(float WXUNUSED(w
), float WXUNUSED(h
)) { }
149 wxShapeEvtHandler
* m_previousHandler
;
150 wxShape
* m_handlerShape
;
153 class wxShape
: public wxShapeEvtHandler
155 DECLARE_ABSTRACT_CLASS(wxShape
)
159 wxShape(wxShapeCanvas
*can
= NULL
);
161 virtual void GetBoundingBoxMax(float *width
, float *height
);
162 virtual void GetBoundingBoxMin(float *width
, float *height
) = 0;
163 virtual bool GetPerimeterPoint(float x1
, float y1
,
165 float *x3
, float *y3
);
166 inline wxShapeCanvas
*GetCanvas() { return m_canvas
; }
167 void SetCanvas(wxShapeCanvas
*the_canvas
);
168 virtual void AddToCanvas(wxShapeCanvas
*the_canvas
, wxShape
*addAfter
= NULL
);
169 virtual void InsertInCanvas(wxShapeCanvas
*the_canvas
);
171 virtual void RemoveFromCanvas(wxShapeCanvas
*the_canvas
);
172 inline float GetX() const { return m_xpos
; }
173 inline float GetY() const { return m_ypos
; }
174 inline void SetX(float x
) { m_xpos
= x
; }
175 inline void SetY(float y
) { m_ypos
= y
; }
177 inline wxShape
*GetParent() const { return m_parent
; }
178 inline void SetParent(wxShape
*p
) { m_parent
= p
; }
179 wxShape
*GetTopAncestor();
180 inline wxList
& GetChildren() { return m_children
; }
182 virtual void OnDraw(wxDC
& dc
);
183 virtual void OnDrawContents(wxDC
& dc
);
184 virtual void OnMoveLinks(wxDC
& dc
);
185 virtual void Unlink() { };
186 void SetDrawHandles(bool drawH
);
187 inline bool GetDrawHandles() { return m_drawHandles
; }
188 virtual void OnErase(wxDC
& dc
);
189 virtual void OnEraseContents(wxDC
& dc
);
190 virtual void OnHighlight(wxDC
& dc
);
191 virtual void OnLeftClick(float x
, float y
, int keys
= 0, int attachment
= 0);
192 virtual void OnRightClick(float x
, float y
, int keys
= 0, int attachment
= 0);
193 virtual void OnSize(float x
, float y
);
194 virtual bool OnMovePre(wxDC
& dc
, float x
, float y
, float old_x
, float old_y
, bool display
= TRUE
);
195 virtual void OnMovePost(wxDC
& dc
, float x
, float y
, float old_x
, float old_y
, bool display
= TRUE
);
197 virtual void OnDragLeft(bool draw
, float x
, float y
, int keys
=0, int attachment
= 0); // Erase if draw false
198 virtual void OnBeginDragLeft(float x
, float y
, int keys
=0, int attachment
= 0);
199 virtual void OnEndDragLeft(float x
, float y
, int keys
=0, int attachment
= 0);
200 virtual void OnDragRight(bool draw
, float x
, float y
, int keys
=0, int attachment
= 0); // Erase if draw false
201 virtual void OnBeginDragRight(float x
, float y
, int keys
=0, int attachment
= 0);
202 virtual void OnEndDragRight(float x
, float y
, int keys
=0, int attachment
= 0);
203 virtual void OnDrawOutline(wxDC
& dc
, float x
, float y
, float w
, float h
);
204 virtual void OnDrawControlPoints(wxDC
& dc
);
205 virtual void OnEraseControlPoints(wxDC
& dc
);
207 virtual void OnBeginSize(float WXUNUSED(w
), float WXUNUSED(h
)) { }
208 virtual void OnEndSize(float WXUNUSED(w
), float WXUNUSED(h
)) { }
210 virtual void MakeControlPoints();
211 virtual void DeleteControlPoints(wxDC
*dc
= NULL
);
212 virtual void ResetControlPoints();
214 inline wxShapeEvtHandler
*GetEventHandler() { return m_eventHandler
; }
215 inline void SetEventHandler(wxShapeEvtHandler
*handler
) { m_eventHandler
= handler
; }
217 // Mandatory control points, e.g. the divided line moving handles
218 // should appear even if a child of the 'selected' image
219 virtual void MakeMandatoryControlPoints();
220 virtual void ResetMandatoryControlPoints();
222 inline virtual bool Recompute() { return TRUE
; };
223 // Calculate size recursively, if size changes. Size might depend on children.
224 inline virtual void CalculateSize() { };
225 virtual void Select(bool select
= TRUE
, wxDC
* dc
= NULL
);
226 virtual void SetHighlight(bool hi
= TRUE
, bool recurse
= FALSE
);
227 inline virtual bool IsHighlighted() const { return m_highlighted
; };
228 virtual bool Selected() const;
229 virtual bool AncestorSelected() const;
230 void SetSensitivityFilter(int sens
= OP_ALL
, bool recursive
= FALSE
);
231 int GetSensitivityFilter() const { return m_sensitivity
; }
232 void SetDraggable(bool drag
, bool recursive
= FALSE
);
233 inline void SetFixedSize(bool x
, bool y
) { m_fixedWidth
= x
; m_fixedHeight
= y
; };
234 inline void GetFixedSize(bool *x
, bool *y
) const { *x
= m_fixedWidth
; *y
= m_fixedHeight
; };
235 inline bool GetFixedWidth() const { return m_fixedWidth
; }
236 inline bool GetFixedHeight() const { return m_fixedHeight
; }
237 inline void SetSpaceAttachments(bool sp
) { m_spaceAttachments
= sp
; };
238 inline bool GetSpaceAttachments() const { return m_spaceAttachments
; };
239 void SetShadowMode(int mode
, bool redraw
= FALSE
);
240 inline int GetShadowMode() const { return m_shadowMode
; }
241 virtual bool HitTest(float x
, float y
, int *attachment
, float *distance
);
242 inline void SetCentreResize(bool cr
) { m_centreResize
= cr
; }
243 inline bool GetCentreResize() const { return m_centreResize
; }
244 inline wxList
& GetLines() { return m_lines
; }
245 inline void SetDisableLabel(bool flag
) { m_disableLabel
= flag
; }
246 inline bool GetDisableLabel() const { return m_disableLabel
; }
247 inline void SetAttachmentMode(bool flag
) { m_attachmentMode
= flag
; }
248 inline bool GetAttachmentMode() const { return m_attachmentMode
; }
249 inline void SetId(long i
) { m_id
= i
; }
250 inline long GetId() const { return m_id
; }
252 void SetPen(wxPen
*pen
);
253 void SetBrush(wxBrush
*brush
);
254 inline void SetClientData(wxObject
*client_data
) { m_clientData
= client_data
; };
255 inline wxObject
*GetClientData() const { return m_clientData
; };
257 virtual void Show(bool show
);
258 virtual bool IsShown() const { return m_visible
; }
259 virtual void Move(wxDC
& dc
, float x1
, float y1
, bool display
= TRUE
);
260 virtual void Erase(wxDC
& dc
);
261 virtual void EraseContents(wxDC
& dc
);
262 virtual void Draw(wxDC
& dc
);
263 virtual void Flash();
264 virtual void MoveLinks(wxDC
& dc
);
265 virtual void DrawContents(wxDC
& dc
); // E.g. for drawing text label
266 virtual void SetSize(float x
, float y
, bool recursive
= TRUE
);
267 virtual void SetAttachmentSize(float x
, float y
);
268 void Attach(wxShapeCanvas
*can
);
271 inline virtual bool Constrain() { return FALSE
; } ;
273 void AddLine(wxLineShape
*line
, wxShape
*other
,
274 int attachFrom
= 0, int attachTo
= 0);
275 void AddText(const wxString
& string
);
277 inline wxPen
*GetPen() const { return m_pen
; }
278 inline wxBrush
*GetBrush() const { return m_brush
; }
281 * Region-specific functions (defaults to the default region
285 // Set the default, single region size to be consistent
286 // with the object size
287 void SetDefaultRegionSize();
288 virtual void FormatText(wxDC
& dc
, const wxString
& s
, int regionId
= 0);
289 virtual void SetFormatMode(int mode
, int regionId
= 0);
290 virtual int GetFormatMode(int regionId
= 0) const;
291 virtual void SetFont(wxFont
*font
, int regionId
= 0);
292 virtual wxFont
*GetFont(int regionId
= 0) const;
293 virtual void SetTextColour(const wxString
& colour
, int regionId
= 0);
294 virtual wxString
GetTextColour(int regionId
= 0) const;
295 virtual inline int GetNumberOfTextRegions() const { return m_regions
.Number(); }
296 virtual void SetRegionName(const wxString
& name
, int regionId
= 0);
298 // Get the name representing the region for this image alone.
299 // I.e. this image's region ids go from 0 to N-1.
300 // But the names might be "0.2.0", "0.2.1" etc. depending on position in composite.
301 // So the last digit represents the region Id, the others represent positions
303 virtual wxString
GetRegionName(int regionId
);
305 // Gets the region corresponding to the name, or -1 if not found.
306 virtual int GetRegionId(const wxString
& name
);
308 // Construct names for regions, unique even for children of a composite.
309 virtual void NameRegions(const wxString
& parentName
= "");
311 // Get list of regions
312 inline wxList
& GetRegions() { return m_regions
; }
314 virtual void AddRegion(wxShapeRegion
*region
);
316 virtual void ClearRegions();
318 // Assign new ids to this image and children (if composite)
321 // Returns actual image (same as 'this' if non-composite) and region id
322 // for given region name.
323 virtual wxShape
*FindRegion(const wxString
& regionName
, int *regionId
);
325 // Finds all region names for this image (composite or simple).
326 // Supply empty string list.
327 virtual void FindRegionNames(wxStringList
& list
);
329 virtual void ClearText(int regionId
= 0);
330 void RemoveLine(wxLineShape
*line
);
333 // Prolog database stuff
334 virtual char *GetFunctor();
335 virtual void WritePrologAttributes(wxExpr
*clause
);
336 virtual void ReadPrologAttributes(wxExpr
*clause
);
338 // In case the object has constraints it needs to read in in a different pass
339 inline virtual void ReadConstraints(wxExpr
*WXUNUSED(clause
), wxExprDatabase
*WXUNUSED(database
)) { };
340 virtual void WriteRegions(wxExpr
*clause
);
341 virtual void ReadRegions(wxExpr
*clause
);
344 // Does the WHOLE copy calling PrivateCopy - don't redefine.
345 // If canvas is non-null, set the canvas too.
346 wxShape
*CreateNewCopy(wxShapeCanvas
*theCanvas
= NULL
);
349 virtual bool GetAttachmentPosition(int attachment
, float *x
, float *y
,
350 int nth
= 0, int no_arcs
= 1, wxLineShape
*line
= NULL
);
351 virtual int GetNumberOfAttachments();
352 virtual bool AttachmentIsValid(int attachment
);
354 virtual void EraseLinks(wxDC
& dc
, int attachment
= -1, bool recurse
= FALSE
);
355 virtual void DrawLinks(wxDC
& dc
, int attachment
= -1, bool recurse
= FALSE
);
357 virtual void MoveLineToNewAttachment(wxDC
& dc
, wxLineShape
*to_move
,
360 // Reorders the lines coming into the node image at this attachment
361 // position, in the order in which they appear in linesToSort.
362 virtual void SortLines(int attachment
, wxList
& linesToSort
);
364 // This is really to distinguish between lines and other images.
365 // For lines, want to pass drag to canvas, since lines tend to prevent
366 // dragging on a canvas (they get in the way.)
367 virtual bool Draggable() const { return TRUE
; }
369 // Returns TRUE if image is a descendant of this image
370 bool HasDescendant(wxShape
*image
);
372 // Does the copying for this object
373 void Copy(wxShape
& copy
);
374 // Returns a new instance, and does the copy for this class. Define for each class.
375 virtual wxShape
*PrivateCopy() = 0;
377 // Rotate about the given axis by the given amount in radians
378 // (does nothing for most objects)
379 // But even non-rotating objects should record their notional
380 // rotation in case it's important (e.g. in dog-leg code).
381 virtual inline void Rotate(float WXUNUSED(x
), float WXUNUSED(y
), float theta
) { m_rotation
= theta
; }
382 virtual inline float GetRotation() const { return m_rotation
; }
384 void ClearAttachments();
386 // Recentres all the text regions for this object
387 void Recentre(wxDC
& dc
);
389 // Clears points from a list of wxRealPoints
390 void ClearPointList(wxList
& list
);
393 wxObject
* m_clientData
;
396 wxShapeEvtHandler
* m_eventHandler
;
398 float m_xpos
, m_ypos
;
402 wxColour
* m_textColour
;
403 wxString m_textColourName
;
404 wxShapeCanvas
* m_canvas
;
407 wxList m_controlPoints
;
409 wxList m_attachmentPoints
;
414 bool m_highlighted
; // Different from selected: user-defined highlighting,
415 // e.g. thick border.
419 bool m_attachmentMode
; // TRUE if using attachments, FALSE otherwise
420 bool m_spaceAttachments
; // TRUE if lines at one side should be spaced
423 bool m_centreResize
; // Default is to resize keeping the centre constant (TRUE)
424 bool m_drawHandles
; // Don't draw handles if FALSE, usually TRUE
425 wxList m_children
; // In case it's composite
426 wxShape
* m_parent
; // In case it's a child
429 wxBrush
* m_shadowBrush
;
432 int m_textMarginX
; // Gap between text and border
434 wxString m_regionName
;
437 class wxPolygonShape
: public wxShape
439 DECLARE_DYNAMIC_CLASS(wxPolygonShape
)
444 // Takes a list of wxRealPoints; each point is an OFFSET from the centre.
445 // Deletes user's points in destructor.
446 virtual void Create(wxList
*points
);
448 void GetBoundingBoxMin(float *w
, float *h
);
449 void CalculateBoundingBox();
450 bool GetPerimeterPoint(float x1
, float y1
,
452 float *x3
, float *y3
);
453 bool HitTest(float x
, float y
, int *attachment
, float *distance
);
454 void SetSize(float x
, float y
, bool recursive
= TRUE
);
455 void OnDraw(wxDC
& dc
);
456 void OnDrawOutline(wxDC
& dc
, float x
, float y
, float w
, float h
);
458 // A polygon should have a control point at each vertex,
459 // with the option of moving the control points individually
460 // to change the shape.
461 void MakeControlPoints();
462 void ResetControlPoints();
464 // If we've changed the shape, must make the original
465 // points match the working points
466 void UpdateOriginalPoints();
468 // Add a control point after the given point
469 virtual void AddPolygonPoint(int pos
= 0);
471 // Delete a control point
472 virtual void DeletePolygonPoint(int pos
= 0);
474 // Recalculates the centre of the polygon
475 virtual void CalculatePolygonCentre();
478 // Prolog database stuff
479 void WritePrologAttributes(wxExpr
*clause
);
480 void ReadPrologAttributes(wxExpr
*clause
);
483 int GetNumberOfAttachments();
484 bool GetAttachmentPosition(int attachment
, float *x
, float *y
,
485 int nth
= 0, int no_arcs
= 1, wxLineShape
*line
= NULL
);
486 bool AttachmentIsValid(int attachment
);
487 // Does the copying for this object
488 void Copy(wxPolygonShape
& copy
);
489 wxShape
*PrivateCopy();
491 inline wxList
*GetPoints() { return m_points
; }
495 wxList
* m_originalPoints
;
498 float m_originalWidth
;
499 float m_originalHeight
;
502 class wxRectangleShape
: public wxShape
504 DECLARE_DYNAMIC_CLASS(wxRectangleShape
)
506 wxRectangleShape(float w
= 0.0, float h
= 0.0);
507 void GetBoundingBoxMin(float *w
, float *h
);
508 bool GetPerimeterPoint(float x1
, float y1
,
510 float *x3
, float *y3
);
511 void OnDraw(wxDC
& dc
);
512 void SetSize(float x
, float y
, bool recursive
= TRUE
);
513 void SetCornerRadius(float rad
); // If > 0, rounded corners
516 // Prolog database stuff
517 void WritePrologAttributes(wxExpr
*clause
);
518 void ReadPrologAttributes(wxExpr
*clause
);
521 int GetNumberOfAttachments();
522 bool GetAttachmentPosition(int attachment
, float *x
, float *y
,
523 int nth
= 0, int no_arcs
= 1, wxLineShape
*line
= NULL
);
524 // Does the copying for this object
525 void Copy(wxRectangleShape
& copy
);
526 wxShape
*PrivateCopy();
528 inline float GetWidth() const { return m_width
; }
529 inline float GetHeight() const { return m_height
; }
534 float m_cornerRadius
;
537 class wxTextShape
: public wxRectangleShape
539 DECLARE_DYNAMIC_CLASS(wxTextShape
)
541 wxTextShape(float width
= 0.0, float height
= 0.0);
543 void OnDraw(wxDC
& dc
);
546 void WritePrologAttributes(wxExpr
*clause
);
549 // Does the copying for this object
550 void Copy(wxTextShape
& copy
);
551 wxShape
*PrivateCopy();
554 class wxEllipseShape
: public wxShape
556 DECLARE_DYNAMIC_CLASS(wxEllipseShape
)
558 wxEllipseShape(float w
= 0.0, float h
= 0.0);
560 void GetBoundingBoxMin(float *w
, float *h
);
561 bool GetPerimeterPoint(float x1
, float y1
,
563 float *x3
, float *y3
);
565 void OnDraw(wxDC
& dc
);
566 void SetSize(float x
, float y
, bool recursive
= TRUE
);
569 // Prolog database stuff
570 void WritePrologAttributes(wxExpr
*clause
);
571 void ReadPrologAttributes(wxExpr
*clause
);
574 int GetNumberOfAttachments();
575 bool GetAttachmentPosition(int attachment
, float *x
, float *y
,
576 int nth
= 0, int no_arcs
= 1, wxLineShape
*line
= NULL
);
578 // Does the copying for this object
579 void Copy(wxEllipseShape
& copy
);
580 wxShape
*PrivateCopy();
582 inline float GetWidth() const { return m_width
; }
583 inline float GetHeight() const { return m_height
; }
590 class wxCircleShape
: public wxEllipseShape
592 DECLARE_DYNAMIC_CLASS(wxCircleShape
)
594 wxCircleShape(float w
= 0.0);
596 bool GetPerimeterPoint(float x1
, float y1
,
598 float *x3
, float *y3
);
599 // Does the copying for this object
600 void Copy(wxCircleShape
& copy
);
601 wxShape
*PrivateCopy();