1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: Shape canvas class
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // For compilers that support precompilation, includes "wx.h".
13 #include "wx/wxprec.h"
24 #include "wx/deprecated/wxexpr.h"
35 #include "wx/ogl/ogl.h"
37 #define CONTROL_POINT_SIZE 6
39 // Control point types
40 // Rectangle and most other shapes
41 #define CONTROL_POINT_VERTICAL 1
42 #define CONTROL_POINT_HORIZONTAL 2
43 #define CONTROL_POINT_DIAGONAL 3
46 #define CONTROL_POINT_ENDPOINT_TO 4
47 #define CONTROL_POINT_ENDPOINT_FROM 5
48 #define CONTROL_POINT_LINE 6
50 IMPLEMENT_DYNAMIC_CLASS(wxShapeCanvas
, wxScrolledWindow
)
52 BEGIN_EVENT_TABLE(wxShapeCanvas
, wxScrolledWindow
)
53 EVT_PAINT(wxShapeCanvas::OnPaint
)
54 EVT_MOUSE_EVENTS(wxShapeCanvas::OnMouseEvent
)
57 const wxChar
* wxShapeCanvasNameStr
= wxT("shapeCanvas");
60 wxShapeCanvas::wxShapeCanvas(wxWindow
*parent
, wxWindowID id
,
64 const wxString
& name
):
65 wxScrolledWindow(parent
, id
, pos
, size
, style
, name
)
67 m_shapeDiagram
= NULL
;
68 m_dragState
= NoDragging
;
69 m_draggedShape
= NULL
;
74 m_checkTolerance
= true;
77 wxShapeCanvas::~wxShapeCanvas()
81 void wxShapeCanvas::OnPaint(wxPaintEvent
& WXUNUSED(event
))
87 dc
.SetBackground(wxBrush(GetBackgroundColour(), wxSOLID
));
91 GetDiagram()->Redraw(dc
);
94 void wxShapeCanvas::OnMouseEvent(wxMouseEvent
& event
)
99 wxPoint
logPos(event
.GetLogicalPosition(dc
));
102 x
= (double) logPos
.x
;
103 y
= (double) logPos
.y
;
106 if (event
.ShiftDown())
107 keys
= keys
| KEY_SHIFT
;
108 if (event
.ControlDown())
109 keys
= keys
| KEY_CTRL
;
111 bool dragging
= event
.Dragging();
113 // Check if we're within the tolerance for mouse movements.
114 // If we're very close to the position we started dragging
115 // from, this may not be an intentional drag at all.
118 int dx
= abs(dc
.LogicalToDeviceX((long) (x
- m_firstDragX
)));
119 int dy
= abs(dc
.LogicalToDeviceY((long) (y
- m_firstDragY
)));
120 if (m_checkTolerance
&& (dx
<= GetDiagram()->GetMouseTolerance()) && (dy
<= GetDiagram()->GetMouseTolerance()))
125 // If we've ignored the tolerance once, then ALWAYS ignore
126 // tolerance in this drag, even if we come back within
127 // the tolerance range.
128 m_checkTolerance
= false;
131 // Dragging - note that the effect of dragging is left entirely up
132 // to the object, so no movement is done unless explicitly done by
134 if (dragging
&& m_draggedShape
&& m_dragState
== StartDraggingLeft
)
136 m_dragState
= ContinueDraggingLeft
;
138 // If the object isn't m_draggable, transfer message to canvas
139 if (m_draggedShape
->Draggable())
140 m_draggedShape
->GetEventHandler()->OnBeginDragLeft((double)x
, (double)y
, keys
, m_draggedAttachment
);
143 m_draggedShape
= NULL
;
144 OnBeginDragLeft((double)x
, (double)y
, keys
);
147 m_oldDragX
= x
; m_oldDragY
= y
;
149 else if (dragging
&& m_draggedShape
&& m_dragState
== ContinueDraggingLeft
)
152 m_draggedShape
->GetEventHandler()->OnDragLeft(false, m_oldDragX
, m_oldDragY
, keys
, m_draggedAttachment
);
153 m_draggedShape
->GetEventHandler()->OnDragLeft(true, (double)x
, (double)y
, keys
, m_draggedAttachment
);
154 m_oldDragX
= x
; m_oldDragY
= y
;
156 else if (event
.LeftUp() && m_draggedShape
&& m_dragState
== ContinueDraggingLeft
)
158 m_dragState
= NoDragging
;
159 m_checkTolerance
= true;
161 m_draggedShape
->GetEventHandler()->OnDragLeft(false, m_oldDragX
, m_oldDragY
, keys
, m_draggedAttachment
);
163 m_draggedShape
->GetEventHandler()->OnEndDragLeft((double)x
, (double)y
, keys
, m_draggedAttachment
);
164 m_draggedShape
= NULL
;
166 else if (dragging
&& m_draggedShape
&& m_dragState
== StartDraggingRight
)
168 m_dragState
= ContinueDraggingRight
;
170 if (m_draggedShape
->Draggable())
171 m_draggedShape
->GetEventHandler()->OnBeginDragRight((double)x
, (double)y
, keys
, m_draggedAttachment
);
174 m_draggedShape
= NULL
;
175 OnBeginDragRight((double)x
, (double)y
, keys
);
177 m_oldDragX
= x
; m_oldDragY
= y
;
179 else if (dragging
&& m_draggedShape
&& m_dragState
== ContinueDraggingRight
)
182 m_draggedShape
->GetEventHandler()->OnDragRight(false, m_oldDragX
, m_oldDragY
, keys
, m_draggedAttachment
);
183 m_draggedShape
->GetEventHandler()->OnDragRight(true, (double)x
, (double)y
, keys
, m_draggedAttachment
);
184 m_oldDragX
= x
; m_oldDragY
= y
;
186 else if (event
.RightUp() && m_draggedShape
&& m_dragState
== ContinueDraggingRight
)
188 m_dragState
= NoDragging
;
189 m_checkTolerance
= true;
191 m_draggedShape
->GetEventHandler()->OnDragRight(false, m_oldDragX
, m_oldDragY
, keys
, m_draggedAttachment
);
193 m_draggedShape
->GetEventHandler()->OnEndDragRight((double)x
, (double)y
, keys
, m_draggedAttachment
);
194 m_draggedShape
= NULL
;
197 // All following events sent to canvas, not object
198 else if (dragging
&& !m_draggedShape
&& m_dragState
== StartDraggingLeft
)
200 m_dragState
= ContinueDraggingLeft
;
201 OnBeginDragLeft((double)x
, (double)y
, keys
);
202 m_oldDragX
= x
; m_oldDragY
= y
;
204 else if (dragging
&& !m_draggedShape
&& m_dragState
== ContinueDraggingLeft
)
207 OnDragLeft(false, m_oldDragX
, m_oldDragY
, keys
);
208 OnDragLeft(true, (double)x
, (double)y
, keys
);
209 m_oldDragX
= x
; m_oldDragY
= y
;
211 else if (event
.LeftUp() && !m_draggedShape
&& m_dragState
== ContinueDraggingLeft
)
213 m_dragState
= NoDragging
;
214 m_checkTolerance
= true;
216 OnDragLeft(false, m_oldDragX
, m_oldDragY
, keys
);
217 OnEndDragLeft((double)x
, (double)y
, keys
);
218 m_draggedShape
= NULL
;
220 else if (dragging
&& !m_draggedShape
&& m_dragState
== StartDraggingRight
)
222 m_dragState
= ContinueDraggingRight
;
223 OnBeginDragRight((double)x
, (double)y
, keys
);
224 m_oldDragX
= x
; m_oldDragY
= y
;
226 else if (dragging
&& !m_draggedShape
&& m_dragState
== ContinueDraggingRight
)
229 OnDragRight(false, m_oldDragX
, m_oldDragY
, keys
);
230 OnDragRight(true, (double)x
, (double)y
, keys
);
231 m_oldDragX
= x
; m_oldDragY
= y
;
233 else if (event
.RightUp() && !m_draggedShape
&& m_dragState
== ContinueDraggingRight
)
235 m_dragState
= NoDragging
;
236 m_checkTolerance
= true;
238 OnDragRight(false, m_oldDragX
, m_oldDragY
, keys
);
239 OnEndDragRight((double)x
, (double)y
, keys
);
240 m_draggedShape
= NULL
;
243 // Non-dragging events
244 else if (event
.IsButton())
246 m_checkTolerance
= true;
248 // Find the nearest object
250 wxShape
*nearest_object
= FindShape(x
, y
, &attachment
);
251 if (nearest_object
) // Object event
253 if (event
.LeftDown())
255 m_draggedShape
= nearest_object
;
256 m_draggedAttachment
= attachment
;
257 m_dragState
= StartDraggingLeft
;
261 else if (event
.LeftUp())
263 // N.B. Only register a click if the same object was
264 // identified for down *and* up.
265 if (nearest_object
== m_draggedShape
)
266 nearest_object
->GetEventHandler()->OnLeftClick((double)x
, (double)y
, keys
, attachment
);
268 m_draggedShape
= NULL
;
269 m_dragState
= NoDragging
;
271 else if (event
.LeftDClick())
273 nearest_object
->GetEventHandler()->OnLeftDoubleClick((double)x
, (double)y
, keys
, attachment
);
275 m_draggedShape
= NULL
;
276 m_dragState
= NoDragging
;
278 else if (event
.RightDown())
280 m_draggedShape
= nearest_object
;
281 m_draggedAttachment
= attachment
;
282 m_dragState
= StartDraggingRight
;
286 else if (event
.RightUp())
288 if (nearest_object
== m_draggedShape
)
289 nearest_object
->GetEventHandler()->OnRightClick((double)x
, (double)y
, keys
, attachment
);
291 m_draggedShape
= NULL
;
292 m_dragState
= NoDragging
;
295 else // Canvas event (no nearest object)
297 if (event
.LeftDown())
299 m_draggedShape
= NULL
;
300 m_dragState
= StartDraggingLeft
;
304 else if (event
.LeftUp())
306 OnLeftClick((double)x
, (double)y
, keys
);
308 m_draggedShape
= NULL
;
309 m_dragState
= NoDragging
;
311 else if (event
.RightDown())
313 m_draggedShape
= NULL
;
314 m_dragState
= StartDraggingRight
;
318 else if (event
.RightUp())
320 OnRightClick((double)x
, (double)y
, keys
);
322 m_draggedShape
= NULL
;
323 m_dragState
= NoDragging
;
330 * Try to find a sensitive object, working up the hierarchy of composites.
333 wxShape
*wxShapeCanvas::FindFirstSensitiveShape(double x
, double y
, int *new_attachment
, int op
)
335 wxShape
*image
= FindShape(x
, y
, new_attachment
);
336 if (!image
) return NULL
;
338 wxShape
*actualImage
= FindFirstSensitiveShape1(image
, op
);
342 // Find actual attachment
343 actualImage
->HitTest(x
, y
, new_attachment
, &dist
);
348 wxShape
*wxShapeCanvas::FindFirstSensitiveShape1(wxShape
*image
, int op
)
350 if (image
->GetSensitivityFilter() & op
)
352 if (image
->GetParent())
353 return FindFirstSensitiveShape1(image
->GetParent(), op
);
357 // Helper function: true if 'contains' wholly contains 'contained'.
358 static bool WhollyContains(wxShape
*contains
, wxShape
*contained
)
360 double xp1
, yp1
, xp2
, yp2
;
361 double w1
, h1
, w2
, h2
;
362 double left1
, top1
, right1
, bottom1
, left2
, top2
, right2
, bottom2
;
364 xp1
= contains
->GetX(); yp1
= contains
->GetY(); xp2
= contained
->GetX(); yp2
= contained
->GetY();
365 contains
->GetBoundingBoxMax(&w1
, &h1
);
366 contained
->GetBoundingBoxMax(&w2
, &h2
);
368 left1
= (double)(xp1
- (w1
/ 2.0));
369 top1
= (double)(yp1
- (h1
/ 2.0));
370 right1
= (double)(xp1
+ (w1
/ 2.0));
371 bottom1
= (double)(yp1
+ (h1
/ 2.0));
373 left2
= (double)(xp2
- (w2
/ 2.0));
374 top2
= (double)(yp2
- (h2
/ 2.0));
375 right2
= (double)(xp2
+ (w2
/ 2.0));
376 bottom2
= (double)(yp2
+ (h2
/ 2.0));
378 return ((left1
<= left2
) && (top1
<= top2
) && (right1
>= right2
) && (bottom1
>= bottom2
));
381 wxShape
*wxShapeCanvas::FindShape(double x
, double y
, int *attachment
, wxClassInfo
*info
, wxShape
*notObject
)
383 double nearest
= 100000.0;
384 int nearest_attachment
= 0;
385 wxShape
*nearest_object
= NULL
;
387 // Go backward through the object list, since we want:
388 // (a) to have the control points drawn LAST to overlay
390 // (b) to find the control points FIRST if they exist
392 wxObjectList::compatibility_iterator current
= GetDiagram()->GetShapeList()->GetLast();
395 wxShape
*object
= (wxShape
*)current
->GetData();
400 // First pass for lines, which might be inside a container, so we
401 // want lines to take priority over containers. This first loop
402 // could fail if we clickout side a line, so then we'll
404 if (object
->IsShown() &&
405 object
->IsKindOf(CLASSINFO(wxLineShape
)) &&
406 object
->HitTest(x
, y
, &temp_attachment
, &dist
) &&
407 ((info
== NULL
) || object
->IsKindOf(info
)) &&
408 (!notObject
|| !notObject
->HasDescendant(object
)))
410 // A line is trickier to spot than a normal object.
411 // For a line, since it's the diagonal of the box
412 // we use for the hit test, we may have several
413 // lines in the box and therefore we need to be able
414 // to specify the nearest point to the centre of the line
415 // as our hit criterion, to give the user some room for
420 nearest_object
= object
;
421 nearest_attachment
= temp_attachment
;
425 current
= current
->GetPrevious();
428 current
= GetDiagram()->GetShapeList()->GetLast();
431 wxShape
*object
= (wxShape
*)current
->GetData();
435 // On second pass, only ever consider non-composites or divisions. If children want to pass
436 // up control to the composite, that's up to them.
437 if (object
->IsShown() && (object
->IsKindOf(CLASSINFO(wxDivisionShape
)) || !object
->IsKindOf(CLASSINFO(wxCompositeShape
)))
438 && object
->HitTest(x
, y
, &temp_attachment
, &dist
) && ((info
== NULL
) || object
->IsKindOf(info
)) &&
439 (!notObject
|| !notObject
->HasDescendant(object
)))
441 if (!object
->IsKindOf(CLASSINFO(wxLineShape
)))
443 // If we've hit a container, and we have already found a line in the
444 // first pass, then ignore the container in case the line is in the container.
445 // Check for division in case line straddles divisions (i.e. is not wholly contained).
446 if (!nearest_object
|| !(object
->IsKindOf(CLASSINFO(wxDivisionShape
)) || WhollyContains(object
, nearest_object
)))
448 nearest_object
= object
;
449 nearest_attachment
= temp_attachment
;
450 current
= GetDiagram()->GetShapeList()->GetFirst()->GetPrevious(); // finish loop
455 current
= current
->GetPrevious();
458 *attachment
= nearest_attachment
;
459 return nearest_object
;
463 * Higher-level events called by OnEvent
467 void wxShapeCanvas::OnLeftClick(double WXUNUSED(x
), double WXUNUSED(y
), int WXUNUSED(keys
))
471 void wxShapeCanvas::OnRightClick(double WXUNUSED(x
), double WXUNUSED(y
), int WXUNUSED(keys
))
475 void wxShapeCanvas::OnDragLeft(bool WXUNUSED(draw
), double WXUNUSED(x
), double WXUNUSED(y
), int WXUNUSED(keys
))
479 void wxShapeCanvas::OnBeginDragLeft(double WXUNUSED(x
), double WXUNUSED(y
), int WXUNUSED(keys
))
483 void wxShapeCanvas::OnEndDragLeft(double WXUNUSED(x
), double WXUNUSED(y
), int WXUNUSED(keys
))
487 void wxShapeCanvas::OnDragRight(bool WXUNUSED(draw
), double WXUNUSED(x
), double WXUNUSED(y
), int WXUNUSED(keys
))
491 void wxShapeCanvas::OnBeginDragRight(double WXUNUSED(x
), double WXUNUSED(y
), int WXUNUSED(keys
))
495 void wxShapeCanvas::OnEndDragRight(double WXUNUSED(x
), double WXUNUSED(y
), int WXUNUSED(keys
))
499 void wxShapeCanvas::AddShape(wxShape
*object
, wxShape
*addAfter
)
500 { GetDiagram()->AddShape(object
, addAfter
); }
501 void wxShapeCanvas::InsertShape(wxShape
*object
)
502 { GetDiagram()->InsertShape(object
); }
503 void wxShapeCanvas::RemoveShape(wxShape
*object
)
504 { GetDiagram()->RemoveShape(object
); }
505 bool wxShapeCanvas::GetQuickEditMode()
506 { return GetDiagram()->GetQuickEditMode(); }
507 void wxShapeCanvas::Redraw(wxDC
& dc
)
508 { GetDiagram()->Redraw(dc
); }
509 void wxShapeCanvas::Snap(double *x
, double *y
)
510 { GetDiagram()->Snap(x
, y
); }