]>
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"
27 #include <wx/wxexpr.h>
43 #include <wx/ogl/basic.h>
44 #include <wx/ogl/basicp.h>
45 #include <wx/ogl/canvas.h>
46 #include <wx/ogl/ogldiag.h>
47 #include <wx/ogl/misc.h>
48 #include <wx/ogl/lines.h>
49 #include <wx/ogl/composit.h>
51 #define CONTROL_POINT_SIZE 6
53 // Control point types
54 // Rectangle and most other shapes
55 #define CONTROL_POINT_VERTICAL 1
56 #define CONTROL_POINT_HORIZONTAL 2
57 #define CONTROL_POINT_DIAGONAL 3
60 #define CONTROL_POINT_ENDPOINT_TO 4
61 #define CONTROL_POINT_ENDPOINT_FROM 5
62 #define CONTROL_POINT_LINE 6
64 IMPLEMENT_DYNAMIC_CLASS(wxShapeCanvas
, wxScrolledWindow
)
66 BEGIN_EVENT_TABLE(wxShapeCanvas
, wxScrolledWindow
)
67 EVT_PAINT(wxShapeCanvas::OnPaint
)
68 EVT_MOUSE_EVENTS(wxShapeCanvas::OnMouseEvent
)
72 wxShapeCanvas::wxShapeCanvas(wxWindow
*parent
, wxWindowID id
, const wxPoint
& pos
, const wxSize
& size
, long style
):
73 wxScrolledWindow(parent
, id
, pos
, size
, style
)
75 m_shapeDiagram
= NULL
;
76 m_dragState
= NoDragging
;
77 m_draggedShape
= NULL
;
82 m_checkTolerance
= TRUE
;
85 wxShapeCanvas::~wxShapeCanvas()
89 void wxShapeCanvas::OnPaint(wxPaintEvent
& event
)
95 dc
.SetBackground(wxBrush(GetBackgroundColour(), wxSOLID
));
99 GetDiagram()->Redraw(dc
);
102 void wxShapeCanvas::OnMouseEvent(wxMouseEvent
& event
)
107 wxPoint
logPos(event
.GetLogicalPosition(dc
));
110 x
= (double) logPos
.x
;
111 y
= (double) logPos
.y
;
114 if (event
.ShiftDown())
115 keys
= keys
| KEY_SHIFT
;
116 if (event
.ControlDown())
117 keys
= keys
| KEY_CTRL
;
119 bool dragging
= event
.Dragging();
121 // Check if we're within the tolerance for mouse movements.
122 // If we're very close to the position we started dragging
123 // from, this may not be an intentional drag at all.
126 int dx
= abs(dc
.LogicalToDeviceX((long) (x
- m_firstDragX
)));
127 int dy
= abs(dc
.LogicalToDeviceY((long) (y
- m_firstDragY
)));
128 if (m_checkTolerance
&& (dx
<= GetDiagram()->GetMouseTolerance()) && (dy
<= GetDiagram()->GetMouseTolerance()))
133 // If we've ignored the tolerance once, then ALWAYS ignore
134 // tolerance in this drag, even if we come back within
135 // the tolerance range.
136 m_checkTolerance
= FALSE
;
139 // Dragging - note that the effect of dragging is left entirely up
140 // to the object, so no movement is done unless explicitly done by
142 if (dragging
&& m_draggedShape
&& m_dragState
== StartDraggingLeft
)
144 m_dragState
= ContinueDraggingLeft
;
146 // If the object isn't m_draggable, transfer message to canvas
147 if (m_draggedShape
->Draggable())
148 m_draggedShape
->GetEventHandler()->OnBeginDragLeft((double)x
, (double)y
, keys
, m_draggedAttachment
);
151 m_draggedShape
= NULL
;
152 OnBeginDragLeft((double)x
, (double)y
, keys
);
155 m_oldDragX
= x
; m_oldDragY
= y
;
157 else if (dragging
&& m_draggedShape
&& m_dragState
== ContinueDraggingLeft
)
160 m_draggedShape
->GetEventHandler()->OnDragLeft(FALSE
, m_oldDragX
, m_oldDragY
, keys
, m_draggedAttachment
);
161 m_draggedShape
->GetEventHandler()->OnDragLeft(TRUE
, (double)x
, (double)y
, keys
, m_draggedAttachment
);
162 m_oldDragX
= x
; m_oldDragY
= y
;
164 else if (event
.LeftUp() && m_draggedShape
&& m_dragState
== ContinueDraggingLeft
)
166 m_dragState
= NoDragging
;
167 m_checkTolerance
= TRUE
;
169 m_draggedShape
->GetEventHandler()->OnDragLeft(FALSE
, m_oldDragX
, m_oldDragY
, keys
, m_draggedAttachment
);
171 m_draggedShape
->GetEventHandler()->OnEndDragLeft((double)x
, (double)y
, keys
, m_draggedAttachment
);
172 m_draggedShape
= NULL
;
174 else if (dragging
&& m_draggedShape
&& m_dragState
== StartDraggingRight
)
176 m_dragState
= ContinueDraggingRight
;
178 if (m_draggedShape
->Draggable())
179 m_draggedShape
->GetEventHandler()->OnBeginDragRight((double)x
, (double)y
, keys
, m_draggedAttachment
);
182 m_draggedShape
= NULL
;
183 OnBeginDragRight((double)x
, (double)y
, keys
);
185 m_oldDragX
= x
; m_oldDragY
= y
;
187 else if (dragging
&& m_draggedShape
&& m_dragState
== ContinueDraggingRight
)
190 m_draggedShape
->GetEventHandler()->OnDragRight(FALSE
, m_oldDragX
, m_oldDragY
, keys
, m_draggedAttachment
);
191 m_draggedShape
->GetEventHandler()->OnDragRight(TRUE
, (double)x
, (double)y
, keys
, m_draggedAttachment
);
192 m_oldDragX
= x
; m_oldDragY
= y
;
194 else if (event
.RightUp() && m_draggedShape
&& m_dragState
== ContinueDraggingRight
)
196 m_dragState
= NoDragging
;
197 m_checkTolerance
= TRUE
;
199 m_draggedShape
->GetEventHandler()->OnDragRight(FALSE
, m_oldDragX
, m_oldDragY
, keys
, m_draggedAttachment
);
201 m_draggedShape
->GetEventHandler()->OnEndDragRight((double)x
, (double)y
, keys
, m_draggedAttachment
);
202 m_draggedShape
= NULL
;
205 // All following events sent to canvas, not object
206 else if (dragging
&& !m_draggedShape
&& m_dragState
== StartDraggingLeft
)
208 m_dragState
= ContinueDraggingLeft
;
209 OnBeginDragLeft((double)x
, (double)y
, keys
);
210 m_oldDragX
= x
; m_oldDragY
= y
;
212 else if (dragging
&& !m_draggedShape
&& m_dragState
== ContinueDraggingLeft
)
215 OnDragLeft(FALSE
, m_oldDragX
, m_oldDragY
, keys
);
216 OnDragLeft(TRUE
, (double)x
, (double)y
, keys
);
217 m_oldDragX
= x
; m_oldDragY
= y
;
219 else if (event
.LeftUp() && !m_draggedShape
&& m_dragState
== ContinueDraggingLeft
)
221 m_dragState
= NoDragging
;
222 m_checkTolerance
= TRUE
;
224 OnDragLeft(FALSE
, m_oldDragX
, m_oldDragY
, keys
);
225 OnEndDragLeft((double)x
, (double)y
, keys
);
226 m_draggedShape
= NULL
;
228 else if (dragging
&& !m_draggedShape
&& m_dragState
== StartDraggingRight
)
230 m_dragState
= ContinueDraggingRight
;
231 OnBeginDragRight((double)x
, (double)y
, keys
);
232 m_oldDragX
= x
; m_oldDragY
= y
;
234 else if (dragging
&& !m_draggedShape
&& m_dragState
== ContinueDraggingRight
)
237 OnDragRight(FALSE
, m_oldDragX
, m_oldDragY
, keys
);
238 OnDragRight(TRUE
, (double)x
, (double)y
, keys
);
239 m_oldDragX
= x
; m_oldDragY
= y
;
241 else if (event
.RightUp() && !m_draggedShape
&& m_dragState
== ContinueDraggingRight
)
243 m_dragState
= NoDragging
;
244 m_checkTolerance
= TRUE
;
246 OnDragRight(FALSE
, m_oldDragX
, m_oldDragY
, keys
);
247 OnEndDragRight((double)x
, (double)y
, keys
);
248 m_draggedShape
= NULL
;
251 // Non-dragging events
252 else if (event
.IsButton())
254 m_checkTolerance
= TRUE
;
256 // Find the nearest object
258 wxShape
*nearest_object
= FindShape(x
, y
, &attachment
);
259 if (nearest_object
) // Object event
261 if (event
.LeftDown())
263 m_draggedShape
= nearest_object
;
264 m_draggedAttachment
= attachment
;
265 m_dragState
= StartDraggingLeft
;
269 else if (event
.LeftUp())
271 // N.B. Only register a click if the same object was
272 // identified for down *and* up.
273 if (nearest_object
== m_draggedShape
)
274 nearest_object
->GetEventHandler()->OnLeftClick((double)x
, (double)y
, keys
, attachment
);
276 m_draggedShape
= NULL
;
277 m_dragState
= NoDragging
;
279 else if (event
.LeftDClick())
281 nearest_object
->GetEventHandler()->OnLeftDoubleClick((double)x
, (double)y
, keys
, attachment
);
283 m_draggedShape
= NULL
;
284 m_dragState
= NoDragging
;
286 else if (event
.RightDown())
288 m_draggedShape
= nearest_object
;
289 m_draggedAttachment
= attachment
;
290 m_dragState
= StartDraggingRight
;
294 else if (event
.RightUp())
296 if (nearest_object
== m_draggedShape
)
297 nearest_object
->GetEventHandler()->OnRightClick((double)x
, (double)y
, keys
, attachment
);
299 m_draggedShape
= NULL
;
300 m_dragState
= NoDragging
;
303 else // Canvas event (no nearest object)
305 if (event
.LeftDown())
307 m_draggedShape
= NULL
;
308 m_dragState
= StartDraggingLeft
;
312 else if (event
.LeftUp())
314 OnLeftClick((double)x
, (double)y
, keys
);
316 m_draggedShape
= NULL
;
317 m_dragState
= NoDragging
;
319 else if (event
.RightDown())
321 m_draggedShape
= NULL
;
322 m_dragState
= StartDraggingRight
;
326 else if (event
.RightUp())
328 OnRightClick((double)x
, (double)y
, keys
);
330 m_draggedShape
= NULL
;
331 m_dragState
= NoDragging
;
338 * Try to find a sensitive object, working up the hierarchy of composites.
341 wxShape
*wxShapeCanvas::FindFirstSensitiveShape(double x
, double y
, int *new_attachment
, int op
)
343 wxShape
*image
= FindShape(x
, y
, new_attachment
);
344 if (!image
) return NULL
;
346 wxShape
*actualImage
= FindFirstSensitiveShape1(image
, op
);
350 // Find actual attachment
351 actualImage
->HitTest(x
, y
, new_attachment
, &dist
);
356 wxShape
*wxShapeCanvas::FindFirstSensitiveShape1(wxShape
*image
, int op
)
358 if (image
->GetSensitivityFilter() & op
)
360 if (image
->GetParent())
361 return FindFirstSensitiveShape1(image
->GetParent(), op
);
365 // Helper function: TRUE if 'contains' wholly contains 'contained'.
366 static bool WhollyContains(wxShape
*contains
, wxShape
*contained
)
368 double xp1
, yp1
, xp2
, yp2
;
369 double w1
, h1
, w2
, h2
;
370 double left1
, top1
, right1
, bottom1
, left2
, top2
, right2
, bottom2
;
372 xp1
= contains
->GetX(); yp1
= contains
->GetY(); xp2
= contained
->GetX(); yp2
= contained
->GetY();
373 contains
->GetBoundingBoxMax(&w1
, &h1
);
374 contained
->GetBoundingBoxMax(&w2
, &h2
);
376 left1
= (double)(xp1
- (w1
/ 2.0));
377 top1
= (double)(yp1
- (h1
/ 2.0));
378 right1
= (double)(xp1
+ (w1
/ 2.0));
379 bottom1
= (double)(yp1
+ (h1
/ 2.0));
381 left2
= (double)(xp2
- (w2
/ 2.0));
382 top2
= (double)(yp2
- (h2
/ 2.0));
383 right2
= (double)(xp2
+ (w2
/ 2.0));
384 bottom2
= (double)(yp2
+ (h2
/ 2.0));
386 return ((left1
<= left2
) && (top1
<= top2
) && (right1
>= right2
) && (bottom1
>= bottom2
));
389 wxShape
*wxShapeCanvas::FindShape(double x
, double y
, int *attachment
, wxClassInfo
*info
, wxShape
*notObject
)
391 double nearest
= 100000.0;
392 int nearest_attachment
= 0;
393 wxShape
*nearest_object
= NULL
;
395 // Go backward through the object list, since we want:
396 // (a) to have the control points drawn LAST to overlay
398 // (b) to find the control points FIRST if they exist
400 wxNode
*current
= GetDiagram()->GetShapeList()->Last();
403 wxShape
*object
= (wxShape
*)current
->Data();
408 // First pass for lines, which might be inside a container, so we
409 // want lines to take priority over containers. This first loop
410 // could fail if we clickout side a line, so then we'll
412 if (object
->IsShown() &&
413 object
->IsKindOf(CLASSINFO(wxLineShape
)) &&
414 object
->HitTest(x
, y
, &temp_attachment
, &dist
) &&
415 ((info
== NULL
) || object
->IsKindOf(info
)) &&
416 (!notObject
|| !notObject
->HasDescendant(object
)))
418 // A line is trickier to spot than a normal object.
419 // For a line, since it's the diagonal of the box
420 // we use for the hit test, we may have several
421 // lines in the box and therefore we need to be able
422 // to specify the nearest point to the centre of the line
423 // as our hit criterion, to give the user some room for
428 nearest_object
= object
;
429 nearest_attachment
= temp_attachment
;
433 current
= current
->Previous();
436 current
= GetDiagram()->GetShapeList()->Last();
439 wxShape
*object
= (wxShape
*)current
->Data();
443 // On second pass, only ever consider non-composites or divisions. If children want to pass
444 // up control to the composite, that's up to them.
445 if (object
->IsShown() && (object
->IsKindOf(CLASSINFO(wxDivisionShape
)) || !object
->IsKindOf(CLASSINFO(wxCompositeShape
)))
446 && object
->HitTest(x
, y
, &temp_attachment
, &dist
) && ((info
== NULL
) || object
->IsKindOf(info
)) &&
447 (!notObject
|| !notObject
->HasDescendant(object
)))
449 if (!object
->IsKindOf(CLASSINFO(wxLineShape
)))
451 // If we've hit a container, and we have already found a line in the
452 // first pass, then ignore the container in case the line is in the container.
453 // Check for division in case line straddles divisions (i.e. is not wholly contained).
454 if (!nearest_object
|| !(object
->IsKindOf(CLASSINFO(wxDivisionShape
)) || WhollyContains(object
, nearest_object
)))
457 nearest_object
= object
;
458 nearest_attachment
= temp_attachment
;
464 current
= current
->Previous();
467 *attachment
= nearest_attachment
;
468 return nearest_object
;
472 * Higher-level events called by OnEvent
476 void wxShapeCanvas::OnLeftClick(double x
, double y
, int keys
)
480 void wxShapeCanvas::OnRightClick(double x
, double y
, int keys
)
484 void wxShapeCanvas::OnDragLeft(bool draw
, double x
, double y
, int keys
)
488 void wxShapeCanvas::OnBeginDragLeft(double x
, double y
, int keys
)
492 void wxShapeCanvas::OnEndDragLeft(double x
, double y
, int keys
)
496 void wxShapeCanvas::OnDragRight(bool draw
, double x
, double y
, int keys
)
500 void wxShapeCanvas::OnBeginDragRight(double x
, double y
, int keys
)
504 void wxShapeCanvas::OnEndDragRight(double x
, double y
, int keys
)
508 void wxShapeCanvas::AddShape(wxShape
*object
, wxShape
*addAfter
)
509 { GetDiagram()->AddShape(object
, addAfter
); }
510 void wxShapeCanvas::InsertShape(wxShape
*object
)
511 { GetDiagram()->InsertShape(object
); }
512 void wxShapeCanvas::RemoveShape(wxShape
*object
)
513 { GetDiagram()->RemoveShape(object
); }
514 bool wxShapeCanvas::GetQuickEditMode()
515 { return GetDiagram()->GetQuickEditMode(); }
516 void wxShapeCanvas::Redraw(wxDC
& dc
)
517 { GetDiagram()->Redraw(dc
); }
518 void wxShapeCanvas::Snap(double *x
, double *y
)
519 { GetDiagram()->Snap(x
, y
); }