]>
git.saurik.com Git - wxWidgets.git/blob - contrib/src/ogl/canvas.cpp
1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: Shape canvas class
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
13 #pragma implementation "canvas.h"
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
28 #include <wx/deprecated/wxexpr.h>
39 #include <wx/ogl/basic.h>
40 #include <wx/ogl/basicp.h>
41 #include <wx/ogl/canvas.h>
42 #include <wx/ogl/ogldiag.h>
43 #include <wx/ogl/misc.h>
44 #include <wx/ogl/lines.h>
45 #include <wx/ogl/composit.h>
47 #define CONTROL_POINT_SIZE 6
49 // Control point types
50 // Rectangle and most other shapes
51 #define CONTROL_POINT_VERTICAL 1
52 #define CONTROL_POINT_HORIZONTAL 2
53 #define CONTROL_POINT_DIAGONAL 3
56 #define CONTROL_POINT_ENDPOINT_TO 4
57 #define CONTROL_POINT_ENDPOINT_FROM 5
58 #define CONTROL_POINT_LINE 6
60 IMPLEMENT_DYNAMIC_CLASS(wxShapeCanvas
, wxScrolledWindow
)
62 BEGIN_EVENT_TABLE(wxShapeCanvas
, wxScrolledWindow
)
63 EVT_PAINT(wxShapeCanvas::OnPaint
)
64 EVT_MOUSE_EVENTS(wxShapeCanvas::OnMouseEvent
)
67 const wxChar
* wxShapeCanvasNameStr
= wxT("shapeCanvas");
70 wxShapeCanvas::wxShapeCanvas(wxWindow
*parent
, wxWindowID id
,
74 const wxString
& name
):
75 wxScrolledWindow(parent
, id
, pos
, size
, style
, name
)
77 m_shapeDiagram
= NULL
;
78 m_dragState
= NoDragging
;
79 m_draggedShape
= NULL
;
84 m_checkTolerance
= TRUE
;
87 wxShapeCanvas::~wxShapeCanvas()
91 void wxShapeCanvas::OnPaint(wxPaintEvent
& event
)
97 dc
.SetBackground(wxBrush(GetBackgroundColour(), wxSOLID
));
101 GetDiagram()->Redraw(dc
);
104 void wxShapeCanvas::OnMouseEvent(wxMouseEvent
& event
)
109 wxPoint
logPos(event
.GetLogicalPosition(dc
));
112 x
= (double) logPos
.x
;
113 y
= (double) logPos
.y
;
116 if (event
.ShiftDown())
117 keys
= keys
| KEY_SHIFT
;
118 if (event
.ControlDown())
119 keys
= keys
| KEY_CTRL
;
121 bool dragging
= event
.Dragging();
123 // Check if we're within the tolerance for mouse movements.
124 // If we're very close to the position we started dragging
125 // from, this may not be an intentional drag at all.
128 int dx
= abs(dc
.LogicalToDeviceX((long) (x
- m_firstDragX
)));
129 int dy
= abs(dc
.LogicalToDeviceY((long) (y
- m_firstDragY
)));
130 if (m_checkTolerance
&& (dx
<= GetDiagram()->GetMouseTolerance()) && (dy
<= GetDiagram()->GetMouseTolerance()))
135 // If we've ignored the tolerance once, then ALWAYS ignore
136 // tolerance in this drag, even if we come back within
137 // the tolerance range.
138 m_checkTolerance
= FALSE
;
141 // Dragging - note that the effect of dragging is left entirely up
142 // to the object, so no movement is done unless explicitly done by
144 if (dragging
&& m_draggedShape
&& m_dragState
== StartDraggingLeft
)
146 m_dragState
= ContinueDraggingLeft
;
148 // If the object isn't m_draggable, transfer message to canvas
149 if (m_draggedShape
->Draggable())
150 m_draggedShape
->GetEventHandler()->OnBeginDragLeft((double)x
, (double)y
, keys
, m_draggedAttachment
);
153 m_draggedShape
= NULL
;
154 OnBeginDragLeft((double)x
, (double)y
, keys
);
157 m_oldDragX
= x
; m_oldDragY
= y
;
159 else if (dragging
&& m_draggedShape
&& m_dragState
== ContinueDraggingLeft
)
162 m_draggedShape
->GetEventHandler()->OnDragLeft(FALSE
, m_oldDragX
, m_oldDragY
, keys
, m_draggedAttachment
);
163 m_draggedShape
->GetEventHandler()->OnDragLeft(TRUE
, (double)x
, (double)y
, keys
, m_draggedAttachment
);
164 m_oldDragX
= x
; m_oldDragY
= y
;
166 else if (event
.LeftUp() && m_draggedShape
&& m_dragState
== ContinueDraggingLeft
)
168 m_dragState
= NoDragging
;
169 m_checkTolerance
= TRUE
;
171 m_draggedShape
->GetEventHandler()->OnDragLeft(FALSE
, m_oldDragX
, m_oldDragY
, keys
, m_draggedAttachment
);
173 m_draggedShape
->GetEventHandler()->OnEndDragLeft((double)x
, (double)y
, keys
, m_draggedAttachment
);
174 m_draggedShape
= NULL
;
176 else if (dragging
&& m_draggedShape
&& m_dragState
== StartDraggingRight
)
178 m_dragState
= ContinueDraggingRight
;
180 if (m_draggedShape
->Draggable())
181 m_draggedShape
->GetEventHandler()->OnBeginDragRight((double)x
, (double)y
, keys
, m_draggedAttachment
);
184 m_draggedShape
= NULL
;
185 OnBeginDragRight((double)x
, (double)y
, keys
);
187 m_oldDragX
= x
; m_oldDragY
= y
;
189 else if (dragging
&& m_draggedShape
&& m_dragState
== ContinueDraggingRight
)
192 m_draggedShape
->GetEventHandler()->OnDragRight(FALSE
, m_oldDragX
, m_oldDragY
, keys
, m_draggedAttachment
);
193 m_draggedShape
->GetEventHandler()->OnDragRight(TRUE
, (double)x
, (double)y
, keys
, m_draggedAttachment
);
194 m_oldDragX
= x
; m_oldDragY
= y
;
196 else if (event
.RightUp() && m_draggedShape
&& m_dragState
== ContinueDraggingRight
)
198 m_dragState
= NoDragging
;
199 m_checkTolerance
= TRUE
;
201 m_draggedShape
->GetEventHandler()->OnDragRight(FALSE
, m_oldDragX
, m_oldDragY
, keys
, m_draggedAttachment
);
203 m_draggedShape
->GetEventHandler()->OnEndDragRight((double)x
, (double)y
, keys
, m_draggedAttachment
);
204 m_draggedShape
= NULL
;
207 // All following events sent to canvas, not object
208 else if (dragging
&& !m_draggedShape
&& m_dragState
== StartDraggingLeft
)
210 m_dragState
= ContinueDraggingLeft
;
211 OnBeginDragLeft((double)x
, (double)y
, keys
);
212 m_oldDragX
= x
; m_oldDragY
= y
;
214 else if (dragging
&& !m_draggedShape
&& m_dragState
== ContinueDraggingLeft
)
217 OnDragLeft(FALSE
, m_oldDragX
, m_oldDragY
, keys
);
218 OnDragLeft(TRUE
, (double)x
, (double)y
, keys
);
219 m_oldDragX
= x
; m_oldDragY
= y
;
221 else if (event
.LeftUp() && !m_draggedShape
&& m_dragState
== ContinueDraggingLeft
)
223 m_dragState
= NoDragging
;
224 m_checkTolerance
= TRUE
;
226 OnDragLeft(FALSE
, m_oldDragX
, m_oldDragY
, keys
);
227 OnEndDragLeft((double)x
, (double)y
, keys
);
228 m_draggedShape
= NULL
;
230 else if (dragging
&& !m_draggedShape
&& m_dragState
== StartDraggingRight
)
232 m_dragState
= ContinueDraggingRight
;
233 OnBeginDragRight((double)x
, (double)y
, keys
);
234 m_oldDragX
= x
; m_oldDragY
= y
;
236 else if (dragging
&& !m_draggedShape
&& m_dragState
== ContinueDraggingRight
)
239 OnDragRight(FALSE
, m_oldDragX
, m_oldDragY
, keys
);
240 OnDragRight(TRUE
, (double)x
, (double)y
, keys
);
241 m_oldDragX
= x
; m_oldDragY
= y
;
243 else if (event
.RightUp() && !m_draggedShape
&& m_dragState
== ContinueDraggingRight
)
245 m_dragState
= NoDragging
;
246 m_checkTolerance
= TRUE
;
248 OnDragRight(FALSE
, m_oldDragX
, m_oldDragY
, keys
);
249 OnEndDragRight((double)x
, (double)y
, keys
);
250 m_draggedShape
= NULL
;
253 // Non-dragging events
254 else if (event
.IsButton())
256 m_checkTolerance
= TRUE
;
258 // Find the nearest object
260 wxShape
*nearest_object
= FindShape(x
, y
, &attachment
);
261 if (nearest_object
) // Object event
263 if (event
.LeftDown())
265 m_draggedShape
= nearest_object
;
266 m_draggedAttachment
= attachment
;
267 m_dragState
= StartDraggingLeft
;
271 else if (event
.LeftUp())
273 // N.B. Only register a click if the same object was
274 // identified for down *and* up.
275 if (nearest_object
== m_draggedShape
)
276 nearest_object
->GetEventHandler()->OnLeftClick((double)x
, (double)y
, keys
, attachment
);
278 m_draggedShape
= NULL
;
279 m_dragState
= NoDragging
;
281 else if (event
.LeftDClick())
283 nearest_object
->GetEventHandler()->OnLeftDoubleClick((double)x
, (double)y
, keys
, attachment
);
285 m_draggedShape
= NULL
;
286 m_dragState
= NoDragging
;
288 else if (event
.RightDown())
290 m_draggedShape
= nearest_object
;
291 m_draggedAttachment
= attachment
;
292 m_dragState
= StartDraggingRight
;
296 else if (event
.RightUp())
298 if (nearest_object
== m_draggedShape
)
299 nearest_object
->GetEventHandler()->OnRightClick((double)x
, (double)y
, keys
, attachment
);
301 m_draggedShape
= NULL
;
302 m_dragState
= NoDragging
;
305 else // Canvas event (no nearest object)
307 if (event
.LeftDown())
309 m_draggedShape
= NULL
;
310 m_dragState
= StartDraggingLeft
;
314 else if (event
.LeftUp())
316 OnLeftClick((double)x
, (double)y
, keys
);
318 m_draggedShape
= NULL
;
319 m_dragState
= NoDragging
;
321 else if (event
.RightDown())
323 m_draggedShape
= NULL
;
324 m_dragState
= StartDraggingRight
;
328 else if (event
.RightUp())
330 OnRightClick((double)x
, (double)y
, keys
);
332 m_draggedShape
= NULL
;
333 m_dragState
= NoDragging
;
340 * Try to find a sensitive object, working up the hierarchy of composites.
343 wxShape
*wxShapeCanvas::FindFirstSensitiveShape(double x
, double y
, int *new_attachment
, int op
)
345 wxShape
*image
= FindShape(x
, y
, new_attachment
);
346 if (!image
) return NULL
;
348 wxShape
*actualImage
= FindFirstSensitiveShape1(image
, op
);
352 // Find actual attachment
353 actualImage
->HitTest(x
, y
, new_attachment
, &dist
);
358 wxShape
*wxShapeCanvas::FindFirstSensitiveShape1(wxShape
*image
, int op
)
360 if (image
->GetSensitivityFilter() & op
)
362 if (image
->GetParent())
363 return FindFirstSensitiveShape1(image
->GetParent(), op
);
367 // Helper function: TRUE if 'contains' wholly contains 'contained'.
368 static bool WhollyContains(wxShape
*contains
, wxShape
*contained
)
370 double xp1
, yp1
, xp2
, yp2
;
371 double w1
, h1
, w2
, h2
;
372 double left1
, top1
, right1
, bottom1
, left2
, top2
, right2
, bottom2
;
374 xp1
= contains
->GetX(); yp1
= contains
->GetY(); xp2
= contained
->GetX(); yp2
= contained
->GetY();
375 contains
->GetBoundingBoxMax(&w1
, &h1
);
376 contained
->GetBoundingBoxMax(&w2
, &h2
);
378 left1
= (double)(xp1
- (w1
/ 2.0));
379 top1
= (double)(yp1
- (h1
/ 2.0));
380 right1
= (double)(xp1
+ (w1
/ 2.0));
381 bottom1
= (double)(yp1
+ (h1
/ 2.0));
383 left2
= (double)(xp2
- (w2
/ 2.0));
384 top2
= (double)(yp2
- (h2
/ 2.0));
385 right2
= (double)(xp2
+ (w2
/ 2.0));
386 bottom2
= (double)(yp2
+ (h2
/ 2.0));
388 return ((left1
<= left2
) && (top1
<= top2
) && (right1
>= right2
) && (bottom1
>= bottom2
));
391 wxShape
*wxShapeCanvas::FindShape(double x
, double y
, int *attachment
, wxClassInfo
*info
, wxShape
*notObject
)
393 double nearest
= 100000.0;
394 int nearest_attachment
= 0;
395 wxShape
*nearest_object
= NULL
;
397 // Go backward through the object list, since we want:
398 // (a) to have the control points drawn LAST to overlay
400 // (b) to find the control points FIRST if they exist
402 wxNode
*current
= GetDiagram()->GetShapeList()->GetLast();
405 wxShape
*object
= (wxShape
*)current
->GetData();
410 // First pass for lines, which might be inside a container, so we
411 // want lines to take priority over containers. This first loop
412 // could fail if we clickout side a line, so then we'll
414 if (object
->IsShown() &&
415 object
->IsKindOf(CLASSINFO(wxLineShape
)) &&
416 object
->HitTest(x
, y
, &temp_attachment
, &dist
) &&
417 ((info
== NULL
) || object
->IsKindOf(info
)) &&
418 (!notObject
|| !notObject
->HasDescendant(object
)))
420 // A line is trickier to spot than a normal object.
421 // For a line, since it's the diagonal of the box
422 // we use for the hit test, we may have several
423 // lines in the box and therefore we need to be able
424 // to specify the nearest point to the centre of the line
425 // as our hit criterion, to give the user some room for
430 nearest_object
= object
;
431 nearest_attachment
= temp_attachment
;
435 current
= current
->GetPrevious();
438 current
= GetDiagram()->GetShapeList()->GetLast();
441 wxShape
*object
= (wxShape
*)current
->GetData();
445 // On second pass, only ever consider non-composites or divisions. If children want to pass
446 // up control to the composite, that's up to them.
447 if (object
->IsShown() && (object
->IsKindOf(CLASSINFO(wxDivisionShape
)) || !object
->IsKindOf(CLASSINFO(wxCompositeShape
)))
448 && object
->HitTest(x
, y
, &temp_attachment
, &dist
) && ((info
== NULL
) || object
->IsKindOf(info
)) &&
449 (!notObject
|| !notObject
->HasDescendant(object
)))
451 if (!object
->IsKindOf(CLASSINFO(wxLineShape
)))
453 // If we've hit a container, and we have already found a line in the
454 // first pass, then ignore the container in case the line is in the container.
455 // Check for division in case line straddles divisions (i.e. is not wholly contained).
456 if (!nearest_object
|| !(object
->IsKindOf(CLASSINFO(wxDivisionShape
)) || WhollyContains(object
, nearest_object
)))
459 nearest_object
= object
;
460 nearest_attachment
= temp_attachment
;
466 current
= current
->GetPrevious();
469 *attachment
= nearest_attachment
;
470 return nearest_object
;
474 * Higher-level events called by OnEvent
478 void wxShapeCanvas::OnLeftClick(double x
, double y
, int keys
)
482 void wxShapeCanvas::OnRightClick(double x
, double y
, int keys
)
486 void wxShapeCanvas::OnDragLeft(bool draw
, double x
, double y
, int keys
)
490 void wxShapeCanvas::OnBeginDragLeft(double x
, double y
, int keys
)
494 void wxShapeCanvas::OnEndDragLeft(double x
, double y
, int keys
)
498 void wxShapeCanvas::OnDragRight(bool draw
, double x
, double y
, int keys
)
502 void wxShapeCanvas::OnBeginDragRight(double x
, double y
, int keys
)
506 void wxShapeCanvas::OnEndDragRight(double x
, double y
, int keys
)
510 void wxShapeCanvas::AddShape(wxShape
*object
, wxShape
*addAfter
)
511 { GetDiagram()->AddShape(object
, addAfter
); }
512 void wxShapeCanvas::InsertShape(wxShape
*object
)
513 { GetDiagram()->InsertShape(object
); }
514 void wxShapeCanvas::RemoveShape(wxShape
*object
)
515 { GetDiagram()->RemoveShape(object
); }
516 bool wxShapeCanvas::GetQuickEditMode()
517 { return GetDiagram()->GetQuickEditMode(); }
518 void wxShapeCanvas::Redraw(wxDC
& dc
)
519 { GetDiagram()->Redraw(dc
); }
520 void wxShapeCanvas::Snap(double *x
, double *y
)
521 { GetDiagram()->Snap(x
, y
); }