]>
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>
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 extern wxCursor
*g_oglBullseyeCursor
;
62 IMPLEMENT_DYNAMIC_CLASS(wxShapeCanvas
, wxScrolledWindow
)
64 BEGIN_EVENT_TABLE(wxShapeCanvas
, wxScrolledWindow
)
65 EVT_PAINT(wxShapeCanvas::OnPaint
)
66 EVT_MOUSE_EVENTS(wxShapeCanvas::OnMouseEvent
)
70 wxShapeCanvas::wxShapeCanvas(wxWindow
*parent
, wxWindowID id
, const wxPoint
& pos
, const wxSize
& size
, long style
):
71 wxScrolledWindow(parent
, id
, pos
, size
, style
)
73 m_shapeDiagram
= NULL
;
74 m_dragState
= NoDragging
;
75 m_draggedShape
= NULL
;
80 m_checkTolerance
= TRUE
;
83 wxShapeCanvas::~wxShapeCanvas()
87 void wxShapeCanvas::OnPaint(wxPaintEvent
& event
)
96 GetDiagram()->Redraw(dc
);
99 void wxShapeCanvas::OnMouseEvent(wxMouseEvent
& event
)
104 wxPoint
logPos(event
.GetLogicalPosition(dc
));
107 x
= (double) logPos
.x
;
108 y
= (double) logPos
.y
;
111 if (event
.ShiftDown())
112 keys
= keys
| KEY_SHIFT
;
113 if (event
.ControlDown())
114 keys
= keys
| KEY_CTRL
;
116 bool dragging
= event
.Dragging();
118 // Check if we're within the tolerance for mouse movements.
119 // If we're very close to the position we started dragging
120 // from, this may not be an intentional drag at all.
123 int dx
= abs(dc
.LogicalToDeviceX((long) (x
- m_firstDragX
)));
124 int dy
= abs(dc
.LogicalToDeviceY((long) (y
- m_firstDragY
)));
125 if (m_checkTolerance
&& (dx
<= GetDiagram()->GetMouseTolerance()) && (dy
<= GetDiagram()->GetMouseTolerance()))
130 // If we've ignored the tolerance once, then ALWAYS ignore
131 // tolerance in this drag, even if we come back within
132 // the tolerance range.
133 m_checkTolerance
= FALSE
;
136 // Dragging - note that the effect of dragging is left entirely up
137 // to the object, so no movement is done unless explicitly done by
139 if (dragging
&& m_draggedShape
&& m_dragState
== StartDraggingLeft
)
141 m_dragState
= ContinueDraggingLeft
;
143 // If the object isn't m_draggable, transfer message to canvas
144 if (m_draggedShape
->Draggable())
145 m_draggedShape
->GetEventHandler()->OnBeginDragLeft((double)x
, (double)y
, keys
, m_draggedAttachment
);
148 m_draggedShape
= NULL
;
149 OnBeginDragLeft((double)x
, (double)y
, keys
);
152 m_oldDragX
= x
; m_oldDragY
= y
;
154 else if (dragging
&& m_draggedShape
&& m_dragState
== ContinueDraggingLeft
)
157 m_draggedShape
->GetEventHandler()->OnDragLeft(FALSE
, m_oldDragX
, m_oldDragY
, keys
, m_draggedAttachment
);
158 m_draggedShape
->GetEventHandler()->OnDragLeft(TRUE
, (double)x
, (double)y
, keys
, m_draggedAttachment
);
159 m_oldDragX
= x
; m_oldDragY
= y
;
161 else if (event
.LeftUp() && m_draggedShape
&& m_dragState
== ContinueDraggingLeft
)
163 m_dragState
= NoDragging
;
164 m_checkTolerance
= TRUE
;
166 m_draggedShape
->GetEventHandler()->OnDragLeft(FALSE
, m_oldDragX
, m_oldDragY
, keys
, m_draggedAttachment
);
168 m_draggedShape
->GetEventHandler()->OnEndDragLeft((double)x
, (double)y
, keys
, m_draggedAttachment
);
169 m_draggedShape
= NULL
;
171 else if (dragging
&& m_draggedShape
&& m_dragState
== StartDraggingRight
)
173 m_dragState
= ContinueDraggingRight
;
175 if (m_draggedShape
->Draggable())
176 m_draggedShape
->GetEventHandler()->OnBeginDragRight((double)x
, (double)y
, keys
, m_draggedAttachment
);
179 m_draggedShape
= NULL
;
180 OnBeginDragRight((double)x
, (double)y
, keys
);
182 m_oldDragX
= x
; m_oldDragY
= y
;
184 else if (dragging
&& m_draggedShape
&& m_dragState
== ContinueDraggingRight
)
187 m_draggedShape
->GetEventHandler()->OnDragRight(FALSE
, m_oldDragX
, m_oldDragY
, keys
, m_draggedAttachment
);
188 m_draggedShape
->GetEventHandler()->OnDragRight(TRUE
, (double)x
, (double)y
, keys
, m_draggedAttachment
);
189 m_oldDragX
= x
; m_oldDragY
= y
;
191 else if (event
.RightUp() && m_draggedShape
&& m_dragState
== ContinueDraggingRight
)
193 m_dragState
= NoDragging
;
194 m_checkTolerance
= TRUE
;
196 m_draggedShape
->GetEventHandler()->OnDragRight(FALSE
, m_oldDragX
, m_oldDragY
, keys
, m_draggedAttachment
);
198 m_draggedShape
->GetEventHandler()->OnEndDragRight((double)x
, (double)y
, keys
, m_draggedAttachment
);
199 m_draggedShape
= NULL
;
202 // All following events sent to canvas, not object
203 else if (dragging
&& !m_draggedShape
&& m_dragState
== StartDraggingLeft
)
205 m_dragState
= ContinueDraggingLeft
;
206 OnBeginDragLeft((double)x
, (double)y
, keys
);
207 m_oldDragX
= x
; m_oldDragY
= y
;
209 else if (dragging
&& !m_draggedShape
&& m_dragState
== ContinueDraggingLeft
)
212 OnDragLeft(FALSE
, m_oldDragX
, m_oldDragY
, keys
);
213 OnDragLeft(TRUE
, (double)x
, (double)y
, keys
);
214 m_oldDragX
= x
; m_oldDragY
= y
;
216 else if (event
.LeftUp() && !m_draggedShape
&& m_dragState
== ContinueDraggingLeft
)
218 m_dragState
= NoDragging
;
219 m_checkTolerance
= TRUE
;
221 OnDragLeft(FALSE
, m_oldDragX
, m_oldDragY
, keys
);
222 OnEndDragLeft((double)x
, (double)y
, keys
);
223 m_draggedShape
= NULL
;
225 else if (dragging
&& !m_draggedShape
&& m_dragState
== StartDraggingRight
)
227 m_dragState
= ContinueDraggingRight
;
228 OnBeginDragRight((double)x
, (double)y
, keys
);
229 m_oldDragX
= x
; m_oldDragY
= y
;
231 else if (dragging
&& !m_draggedShape
&& m_dragState
== ContinueDraggingRight
)
234 OnDragRight(FALSE
, m_oldDragX
, m_oldDragY
, keys
);
235 OnDragRight(TRUE
, (double)x
, (double)y
, keys
);
236 m_oldDragX
= x
; m_oldDragY
= y
;
238 else if (event
.RightUp() && !m_draggedShape
&& m_dragState
== ContinueDraggingRight
)
240 m_dragState
= NoDragging
;
241 m_checkTolerance
= TRUE
;
243 OnDragRight(FALSE
, m_oldDragX
, m_oldDragY
, keys
);
244 OnEndDragRight((double)x
, (double)y
, keys
);
245 m_draggedShape
= NULL
;
248 // Non-dragging events
249 else if (event
.IsButton())
251 m_checkTolerance
= TRUE
;
253 // Find the nearest object
255 wxShape
*nearest_object
= FindShape(x
, y
, &attachment
);
256 if (nearest_object
) // Object event
258 if (event
.LeftDown())
260 m_draggedShape
= nearest_object
;
261 m_draggedAttachment
= attachment
;
262 m_dragState
= StartDraggingLeft
;
266 else if (event
.LeftUp())
268 // N.B. Only register a click if the same object was
269 // identified for down *and* up.
270 if (nearest_object
== m_draggedShape
)
271 nearest_object
->GetEventHandler()->OnLeftClick((double)x
, (double)y
, keys
, attachment
);
273 m_draggedShape
= NULL
;
274 m_dragState
= NoDragging
;
276 else if (event
.LeftDClick())
278 nearest_object
->GetEventHandler()->OnLeftDoubleClick((double)x
, (double)y
, keys
, attachment
);
280 m_draggedShape
= NULL
;
281 m_dragState
= NoDragging
;
283 else if (event
.RightDown())
285 m_draggedShape
= nearest_object
;
286 m_draggedAttachment
= attachment
;
287 m_dragState
= StartDraggingRight
;
291 else if (event
.RightUp())
293 if (nearest_object
== m_draggedShape
)
294 nearest_object
->GetEventHandler()->OnRightClick((double)x
, (double)y
, keys
, attachment
);
296 m_draggedShape
= NULL
;
297 m_dragState
= NoDragging
;
300 else // Canvas event (no nearest object)
302 if (event
.LeftDown())
304 m_draggedShape
= NULL
;
305 m_dragState
= StartDraggingLeft
;
309 else if (event
.LeftUp())
311 OnLeftClick((double)x
, (double)y
, keys
);
313 m_draggedShape
= NULL
;
314 m_dragState
= NoDragging
;
316 else if (event
.RightDown())
318 m_draggedShape
= NULL
;
319 m_dragState
= StartDraggingRight
;
323 else if (event
.RightUp())
325 OnRightClick((double)x
, (double)y
, keys
);
327 m_draggedShape
= NULL
;
328 m_dragState
= NoDragging
;
335 * Try to find a sensitive object, working up the hierarchy of composites.
338 wxShape
*wxShapeCanvas::FindFirstSensitiveShape(double x
, double y
, int *new_attachment
, int op
)
340 wxShape
*image
= FindShape(x
, y
, new_attachment
);
341 if (!image
) return NULL
;
343 wxShape
*actualImage
= FindFirstSensitiveShape1(image
, op
);
347 // Find actual attachment
348 actualImage
->HitTest(x
, y
, new_attachment
, &dist
);
353 wxShape
*wxShapeCanvas::FindFirstSensitiveShape1(wxShape
*image
, int op
)
355 if (image
->GetSensitivityFilter() & op
)
357 if (image
->GetParent())
358 return FindFirstSensitiveShape1(image
->GetParent(), op
);
362 // Helper function: TRUE if 'contains' wholly contains 'contained'.
363 static bool WhollyContains(wxShape
*contains
, wxShape
*contained
)
365 double xp1
, yp1
, xp2
, yp2
;
366 double w1
, h1
, w2
, h2
;
367 double left1
, top1
, right1
, bottom1
, left2
, top2
, right2
, bottom2
;
369 xp1
= contains
->GetX(); yp1
= contains
->GetY(); xp2
= contained
->GetX(); yp2
= contained
->GetY();
370 contains
->GetBoundingBoxMax(&w1
, &h1
);
371 contained
->GetBoundingBoxMax(&w2
, &h2
);
373 left1
= (double)(xp1
- (w1
/ 2.0));
374 top1
= (double)(yp1
- (h1
/ 2.0));
375 right1
= (double)(xp1
+ (w1
/ 2.0));
376 bottom1
= (double)(yp1
+ (h1
/ 2.0));
378 left2
= (double)(xp2
- (w2
/ 2.0));
379 top2
= (double)(yp2
- (h2
/ 2.0));
380 right2
= (double)(xp2
+ (w2
/ 2.0));
381 bottom2
= (double)(yp2
+ (h2
/ 2.0));
383 return ((left1
<= left2
) && (top1
<= top2
) && (right1
>= right2
) && (bottom1
>= bottom2
));
386 wxShape
*wxShapeCanvas::FindShape(double x
, double y
, int *attachment
, wxClassInfo
*info
, wxShape
*notObject
)
388 double nearest
= 100000.0;
389 int nearest_attachment
= 0;
390 wxShape
*nearest_object
= NULL
;
392 // Go backward through the object list, since we want:
393 // (a) to have the control points drawn LAST to overlay
395 // (b) to find the control points FIRST if they exist
397 wxNode
*current
= GetDiagram()->GetShapeList()->Last();
400 wxShape
*object
= (wxShape
*)current
->Data();
405 // First pass for lines, which might be inside a container, so we
406 // want lines to take priority over containers. This first loop
407 // could fail if we clickout side a line, so then we'll
409 if (object
->IsShown() &&
410 object
->IsKindOf(CLASSINFO(wxLineShape
)) &&
411 object
->HitTest(x
, y
, &temp_attachment
, &dist
) &&
412 ((info
== NULL
) || object
->IsKindOf(info
)) &&
413 (!notObject
|| !notObject
->HasDescendant(object
)))
415 // A line is trickier to spot than a normal object.
416 // For a line, since it's the diagonal of the box
417 // we use for the hit test, we may have several
418 // lines in the box and therefore we need to be able
419 // to specify the nearest point to the centre of the line
420 // as our hit criterion, to give the user some room for
425 nearest_object
= object
;
426 nearest_attachment
= temp_attachment
;
430 current
= current
->Previous();
433 current
= GetDiagram()->GetShapeList()->Last();
436 wxShape
*object
= (wxShape
*)current
->Data();
440 // On second pass, only ever consider non-composites or divisions. If children want to pass
441 // up control to the composite, that's up to them.
442 if (object
->IsShown() && (object
->IsKindOf(CLASSINFO(wxDivisionShape
)) || !object
->IsKindOf(CLASSINFO(wxCompositeShape
)))
443 && object
->HitTest(x
, y
, &temp_attachment
, &dist
) && ((info
== NULL
) || object
->IsKindOf(info
)) &&
444 (!notObject
|| !notObject
->HasDescendant(object
)))
446 if (!object
->IsKindOf(CLASSINFO(wxLineShape
)))
448 // If we've hit a container, and we have already found a line in the
449 // first pass, then ignore the container in case the line is in the container.
450 // Check for division in case line straddles divisions (i.e. is not wholly contained).
451 if (!nearest_object
|| !(object
->IsKindOf(CLASSINFO(wxDivisionShape
)) || WhollyContains(object
, nearest_object
)))
454 nearest_object
= object
;
455 nearest_attachment
= temp_attachment
;
461 current
= current
->Previous();
464 *attachment
= nearest_attachment
;
465 return nearest_object
;
469 * Higher-level events called by OnEvent
473 void wxShapeCanvas::OnLeftClick(double x
, double y
, int keys
)
477 void wxShapeCanvas::OnRightClick(double x
, double y
, int keys
)
481 void wxShapeCanvas::OnDragLeft(bool draw
, double x
, double y
, int keys
)
485 void wxShapeCanvas::OnBeginDragLeft(double x
, double y
, int keys
)
489 void wxShapeCanvas::OnEndDragLeft(double x
, double y
, int keys
)
493 void wxShapeCanvas::OnDragRight(bool draw
, double x
, double y
, int keys
)
497 void wxShapeCanvas::OnBeginDragRight(double x
, double y
, int keys
)
501 void wxShapeCanvas::OnEndDragRight(double x
, double y
, int keys
)
505 void wxShapeCanvas::AddShape(wxShape
*object
, wxShape
*addAfter
)
506 { GetDiagram()->AddShape(object
, addAfter
); }
507 void wxShapeCanvas::InsertShape(wxShape
*object
)
508 { GetDiagram()->InsertShape(object
); }
509 void wxShapeCanvas::RemoveShape(wxShape
*object
)
510 { GetDiagram()->RemoveShape(object
); }
511 bool wxShapeCanvas::GetQuickEditMode()
512 { return GetDiagram()->GetQuickEditMode(); }
513 void wxShapeCanvas::Redraw(wxDC
& dc
)
514 { GetDiagram()->Redraw(dc
); }
515 void wxShapeCanvas::Snap(double *x
, double *y
)
516 { GetDiagram()->Snap(x
, y
); }