]>
git.saurik.com Git - wxWidgets.git/blob - contrib/src/ogl/canvas.cpp
ac81c3d692a05a3589fe70292e6c09d38d903a30
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 extern wxCursor
*g_oglBullseyeCursor
;
66 IMPLEMENT_DYNAMIC_CLASS(wxShapeCanvas
, wxScrolledWindow
)
68 BEGIN_EVENT_TABLE(wxShapeCanvas
, wxScrolledWindow
)
69 EVT_PAINT(wxShapeCanvas::OnPaint
)
70 EVT_MOUSE_EVENTS(wxShapeCanvas::OnMouseEvent
)
74 wxShapeCanvas::wxShapeCanvas(wxWindow
*parent
, wxWindowID id
, const wxPoint
& pos
, const wxSize
& size
, long style
):
75 wxScrolledWindow(parent
, id
, pos
, size
, style
)
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
)
100 GetDiagram()->Redraw(dc
);
103 void wxShapeCanvas::OnMouseEvent(wxMouseEvent
& event
)
108 wxPoint
logPos(event
.GetLogicalPosition(dc
));
111 x
= (double) logPos
.x
;
112 y
= (double) logPos
.y
;
115 if (event
.ShiftDown())
116 keys
= keys
| KEY_SHIFT
;
117 if (event
.ControlDown())
118 keys
= keys
| KEY_CTRL
;
120 bool dragging
= event
.Dragging();
122 // Check if we're within the tolerance for mouse movements.
123 // If we're very close to the position we started dragging
124 // from, this may not be an intentional drag at all.
127 int dx
= abs(dc
.LogicalToDeviceX((long) (x
- m_firstDragX
)));
128 int dy
= abs(dc
.LogicalToDeviceY((long) (y
- m_firstDragY
)));
129 if (m_checkTolerance
&& (dx
<= GetDiagram()->GetMouseTolerance()) && (dy
<= GetDiagram()->GetMouseTolerance()))
134 // If we've ignored the tolerance once, then ALWAYS ignore
135 // tolerance in this drag, even if we come back within
136 // the tolerance range.
137 m_checkTolerance
= FALSE
;
140 // Dragging - note that the effect of dragging is left entirely up
141 // to the object, so no movement is done unless explicitly done by
143 if (dragging
&& m_draggedShape
&& m_dragState
== StartDraggingLeft
)
145 m_dragState
= ContinueDraggingLeft
;
147 // If the object isn't m_draggable, transfer message to canvas
148 if (m_draggedShape
->Draggable())
149 m_draggedShape
->GetEventHandler()->OnBeginDragLeft((double)x
, (double)y
, keys
, m_draggedAttachment
);
152 m_draggedShape
= NULL
;
153 OnBeginDragLeft((double)x
, (double)y
, keys
);
156 m_oldDragX
= x
; m_oldDragY
= y
;
158 else if (dragging
&& m_draggedShape
&& m_dragState
== ContinueDraggingLeft
)
161 m_draggedShape
->GetEventHandler()->OnDragLeft(FALSE
, m_oldDragX
, m_oldDragY
, keys
, m_draggedAttachment
);
162 m_draggedShape
->GetEventHandler()->OnDragLeft(TRUE
, (double)x
, (double)y
, keys
, m_draggedAttachment
);
163 m_oldDragX
= x
; m_oldDragY
= y
;
165 else if (event
.LeftUp() && m_draggedShape
&& m_dragState
== ContinueDraggingLeft
)
167 m_dragState
= NoDragging
;
168 m_checkTolerance
= TRUE
;
170 m_draggedShape
->GetEventHandler()->OnDragLeft(FALSE
, m_oldDragX
, m_oldDragY
, keys
, m_draggedAttachment
);
172 m_draggedShape
->GetEventHandler()->OnEndDragLeft((double)x
, (double)y
, keys
, m_draggedAttachment
);
173 m_draggedShape
= NULL
;
175 else if (dragging
&& m_draggedShape
&& m_dragState
== StartDraggingRight
)
177 m_dragState
= ContinueDraggingRight
;
179 if (m_draggedShape
->Draggable())
180 m_draggedShape
->GetEventHandler()->OnBeginDragRight((double)x
, (double)y
, keys
, m_draggedAttachment
);
183 m_draggedShape
= NULL
;
184 OnBeginDragRight((double)x
, (double)y
, keys
);
186 m_oldDragX
= x
; m_oldDragY
= y
;
188 else if (dragging
&& m_draggedShape
&& m_dragState
== ContinueDraggingRight
)
191 m_draggedShape
->GetEventHandler()->OnDragRight(FALSE
, m_oldDragX
, m_oldDragY
, keys
, m_draggedAttachment
);
192 m_draggedShape
->GetEventHandler()->OnDragRight(TRUE
, (double)x
, (double)y
, keys
, m_draggedAttachment
);
193 m_oldDragX
= x
; m_oldDragY
= y
;
195 else if (event
.RightUp() && m_draggedShape
&& m_dragState
== ContinueDraggingRight
)
197 m_dragState
= NoDragging
;
198 m_checkTolerance
= TRUE
;
200 m_draggedShape
->GetEventHandler()->OnDragRight(FALSE
, m_oldDragX
, m_oldDragY
, keys
, m_draggedAttachment
);
202 m_draggedShape
->GetEventHandler()->OnEndDragRight((double)x
, (double)y
, keys
, m_draggedAttachment
);
203 m_draggedShape
= NULL
;
206 // All following events sent to canvas, not object
207 else if (dragging
&& !m_draggedShape
&& m_dragState
== StartDraggingLeft
)
209 m_dragState
= ContinueDraggingLeft
;
210 OnBeginDragLeft((double)x
, (double)y
, keys
);
211 m_oldDragX
= x
; m_oldDragY
= y
;
213 else if (dragging
&& !m_draggedShape
&& m_dragState
== ContinueDraggingLeft
)
216 OnDragLeft(FALSE
, m_oldDragX
, m_oldDragY
, keys
);
217 OnDragLeft(TRUE
, (double)x
, (double)y
, keys
);
218 m_oldDragX
= x
; m_oldDragY
= y
;
220 else if (event
.LeftUp() && !m_draggedShape
&& m_dragState
== ContinueDraggingLeft
)
222 m_dragState
= NoDragging
;
223 m_checkTolerance
= TRUE
;
225 OnDragLeft(FALSE
, m_oldDragX
, m_oldDragY
, keys
);
226 OnEndDragLeft((double)x
, (double)y
, keys
);
227 m_draggedShape
= NULL
;
229 else if (dragging
&& !m_draggedShape
&& m_dragState
== StartDraggingRight
)
231 m_dragState
= ContinueDraggingRight
;
232 OnBeginDragRight((double)x
, (double)y
, keys
);
233 m_oldDragX
= x
; m_oldDragY
= y
;
235 else if (dragging
&& !m_draggedShape
&& m_dragState
== ContinueDraggingRight
)
238 OnDragRight(FALSE
, m_oldDragX
, m_oldDragY
, keys
);
239 OnDragRight(TRUE
, (double)x
, (double)y
, keys
);
240 m_oldDragX
= x
; m_oldDragY
= y
;
242 else if (event
.RightUp() && !m_draggedShape
&& m_dragState
== ContinueDraggingRight
)
244 m_dragState
= NoDragging
;
245 m_checkTolerance
= TRUE
;
247 OnDragRight(FALSE
, m_oldDragX
, m_oldDragY
, keys
);
248 OnEndDragRight((double)x
, (double)y
, keys
);
249 m_draggedShape
= NULL
;
252 // Non-dragging events
253 else if (event
.IsButton())
255 m_checkTolerance
= TRUE
;
257 // Find the nearest object
259 wxShape
*nearest_object
= FindShape(x
, y
, &attachment
);
260 if (nearest_object
) // Object event
262 if (event
.LeftDown())
264 m_draggedShape
= nearest_object
;
265 m_draggedAttachment
= attachment
;
266 m_dragState
= StartDraggingLeft
;
270 else if (event
.LeftUp())
272 // N.B. Only register a click if the same object was
273 // identified for down *and* up.
274 if (nearest_object
== m_draggedShape
)
275 nearest_object
->GetEventHandler()->OnLeftClick((double)x
, (double)y
, keys
, attachment
);
277 m_draggedShape
= NULL
;
278 m_dragState
= NoDragging
;
280 else if (event
.LeftDClick())
282 nearest_object
->GetEventHandler()->OnLeftDoubleClick((double)x
, (double)y
, keys
, attachment
);
284 m_draggedShape
= NULL
;
285 m_dragState
= NoDragging
;
287 else if (event
.RightDown())
289 m_draggedShape
= nearest_object
;
290 m_draggedAttachment
= attachment
;
291 m_dragState
= StartDraggingRight
;
295 else if (event
.RightUp())
297 if (nearest_object
== m_draggedShape
)
298 nearest_object
->GetEventHandler()->OnRightClick((double)x
, (double)y
, keys
, attachment
);
300 m_draggedShape
= NULL
;
301 m_dragState
= NoDragging
;
304 else // Canvas event (no nearest object)
306 if (event
.LeftDown())
308 m_draggedShape
= NULL
;
309 m_dragState
= StartDraggingLeft
;
313 else if (event
.LeftUp())
315 OnLeftClick((double)x
, (double)y
, keys
);
317 m_draggedShape
= NULL
;
318 m_dragState
= NoDragging
;
320 else if (event
.RightDown())
322 m_draggedShape
= NULL
;
323 m_dragState
= StartDraggingRight
;
327 else if (event
.RightUp())
329 OnRightClick((double)x
, (double)y
, keys
);
331 m_draggedShape
= NULL
;
332 m_dragState
= NoDragging
;
339 * Try to find a sensitive object, working up the hierarchy of composites.
342 wxShape
*wxShapeCanvas::FindFirstSensitiveShape(double x
, double y
, int *new_attachment
, int op
)
344 wxShape
*image
= FindShape(x
, y
, new_attachment
);
345 if (!image
) return NULL
;
347 wxShape
*actualImage
= FindFirstSensitiveShape1(image
, op
);
351 // Find actual attachment
352 actualImage
->HitTest(x
, y
, new_attachment
, &dist
);
357 wxShape
*wxShapeCanvas::FindFirstSensitiveShape1(wxShape
*image
, int op
)
359 if (image
->GetSensitivityFilter() & op
)
361 if (image
->GetParent())
362 return FindFirstSensitiveShape1(image
->GetParent(), op
);
366 // Helper function: TRUE if 'contains' wholly contains 'contained'.
367 static bool WhollyContains(wxShape
*contains
, wxShape
*contained
)
369 double xp1
, yp1
, xp2
, yp2
;
370 double w1
, h1
, w2
, h2
;
371 double left1
, top1
, right1
, bottom1
, left2
, top2
, right2
, bottom2
;
373 xp1
= contains
->GetX(); yp1
= contains
->GetY(); xp2
= contained
->GetX(); yp2
= contained
->GetY();
374 contains
->GetBoundingBoxMax(&w1
, &h1
);
375 contained
->GetBoundingBoxMax(&w2
, &h2
);
377 left1
= (double)(xp1
- (w1
/ 2.0));
378 top1
= (double)(yp1
- (h1
/ 2.0));
379 right1
= (double)(xp1
+ (w1
/ 2.0));
380 bottom1
= (double)(yp1
+ (h1
/ 2.0));
382 left2
= (double)(xp2
- (w2
/ 2.0));
383 top2
= (double)(yp2
- (h2
/ 2.0));
384 right2
= (double)(xp2
+ (w2
/ 2.0));
385 bottom2
= (double)(yp2
+ (h2
/ 2.0));
387 return ((left1
<= left2
) && (top1
<= top2
) && (right1
>= right2
) && (bottom1
>= bottom2
));
390 wxShape
*wxShapeCanvas::FindShape(double x
, double y
, int *attachment
, wxClassInfo
*info
, wxShape
*notObject
)
392 double nearest
= 100000.0;
393 int nearest_attachment
= 0;
394 wxShape
*nearest_object
= NULL
;
396 // Go backward through the object list, since we want:
397 // (a) to have the control points drawn LAST to overlay
399 // (b) to find the control points FIRST if they exist
401 wxNode
*current
= GetDiagram()->GetShapeList()->Last();
404 wxShape
*object
= (wxShape
*)current
->Data();
409 // First pass for lines, which might be inside a container, so we
410 // want lines to take priority over containers. This first loop
411 // could fail if we clickout side a line, so then we'll
413 if (object
->IsShown() &&
414 object
->IsKindOf(CLASSINFO(wxLineShape
)) &&
415 object
->HitTest(x
, y
, &temp_attachment
, &dist
) &&
416 ((info
== NULL
) || object
->IsKindOf(info
)) &&
417 (!notObject
|| !notObject
->HasDescendant(object
)))
419 // A line is trickier to spot than a normal object.
420 // For a line, since it's the diagonal of the box
421 // we use for the hit test, we may have several
422 // lines in the box and therefore we need to be able
423 // to specify the nearest point to the centre of the line
424 // as our hit criterion, to give the user some room for
429 nearest_object
= object
;
430 nearest_attachment
= temp_attachment
;
434 current
= current
->Previous();
437 current
= GetDiagram()->GetShapeList()->Last();
440 wxShape
*object
= (wxShape
*)current
->Data();
444 // On second pass, only ever consider non-composites or divisions. If children want to pass
445 // up control to the composite, that's up to them.
446 if (object
->IsShown() && (object
->IsKindOf(CLASSINFO(wxDivisionShape
)) || !object
->IsKindOf(CLASSINFO(wxCompositeShape
)))
447 && object
->HitTest(x
, y
, &temp_attachment
, &dist
) && ((info
== NULL
) || object
->IsKindOf(info
)) &&
448 (!notObject
|| !notObject
->HasDescendant(object
)))
450 if (!object
->IsKindOf(CLASSINFO(wxLineShape
)))
452 // If we've hit a container, and we have already found a line in the
453 // first pass, then ignore the container in case the line is in the container.
454 // Check for division in case line straddles divisions (i.e. is not wholly contained).
455 if (!nearest_object
|| !(object
->IsKindOf(CLASSINFO(wxDivisionShape
)) || WhollyContains(object
, nearest_object
)))
458 nearest_object
= object
;
459 nearest_attachment
= temp_attachment
;
465 current
= current
->Previous();
468 *attachment
= nearest_attachment
;
469 return nearest_object
;
473 * Higher-level events called by OnEvent
477 void wxShapeCanvas::OnLeftClick(double x
, double y
, int keys
)
481 void wxShapeCanvas::OnRightClick(double x
, double y
, int keys
)
485 void wxShapeCanvas::OnDragLeft(bool draw
, double x
, double y
, int keys
)
489 void wxShapeCanvas::OnBeginDragLeft(double x
, double y
, int keys
)
493 void wxShapeCanvas::OnEndDragLeft(double x
, double y
, int keys
)
497 void wxShapeCanvas::OnDragRight(bool draw
, double x
, double y
, int keys
)
501 void wxShapeCanvas::OnBeginDragRight(double x
, double y
, int keys
)
505 void wxShapeCanvas::OnEndDragRight(double x
, double y
, int keys
)
509 void wxShapeCanvas::AddShape(wxShape
*object
, wxShape
*addAfter
)
510 { GetDiagram()->AddShape(object
, addAfter
); }
511 void wxShapeCanvas::InsertShape(wxShape
*object
)
512 { GetDiagram()->InsertShape(object
); }
513 void wxShapeCanvas::RemoveShape(wxShape
*object
)
514 { GetDiagram()->RemoveShape(object
); }
515 bool wxShapeCanvas::GetQuickEditMode()
516 { return GetDiagram()->GetQuickEditMode(); }
517 void wxShapeCanvas::Redraw(wxDC
& dc
)
518 { GetDiagram()->Redraw(dc
); }
519 void wxShapeCanvas::Snap(double *x
, double *y
)
520 { GetDiagram()->Snap(x
, y
); }