]> git.saurik.com Git - wxWidgets.git/blob - utils/ogl/src/canvas.cpp
Removed DrawOpenSpline since it doesn't seem to be needed, with required changes
[wxWidgets.git] / utils / ogl / src / canvas.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: canvas.cpp
3 // Purpose: Shape canvas class
4 // Author: Julian Smart
5 // Modified by:
6 // Created: 12/07/98
7 // RCS-ID: $Id$
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 #ifdef __GNUG__
13 #pragma implementation "canvas.h"
14 #endif
15
16 // For compilers that support precompilation, includes "wx.h".
17 #include <wx/wxprec.h>
18
19 #ifdef __BORLANDC__
20 #pragma hdrstop
21 #endif
22
23 #ifndef WX_PRECOMP
24 #include <wx/wx.h>
25 #endif
26
27 #ifdef PROLOGIO
28 #include <wx/wxexpr.h>
29 #endif
30
31 #if wxUSE_IOSTREAMH
32 #include <iostream.h>
33 #else
34 #include <iostream>
35 #endif
36
37 #include <ctype.h>
38 #include <math.h>
39 #include <stdlib.h>
40
41 #include "basic.h"
42 #include "basicp.h"
43 #include "canvas.h"
44 #include "ogldiag.h"
45 #include "misc.h"
46 #include "lines.h"
47 #include "composit.h"
48
49 #define CONTROL_POINT_SIZE 6
50
51 // Control point types
52 // Rectangle and most other shapes
53 #define CONTROL_POINT_VERTICAL 1
54 #define CONTROL_POINT_HORIZONTAL 2
55 #define CONTROL_POINT_DIAGONAL 3
56
57 // Line
58 #define CONTROL_POINT_ENDPOINT_TO 4
59 #define CONTROL_POINT_ENDPOINT_FROM 5
60 #define CONTROL_POINT_LINE 6
61
62 extern wxCursor *g_oglBullseyeCursor;
63
64 IMPLEMENT_DYNAMIC_CLASS(wxShapeCanvas, wxScrolledWindow)
65
66 BEGIN_EVENT_TABLE(wxShapeCanvas, wxScrolledWindow)
67 EVT_PAINT(wxShapeCanvas::OnPaint)
68 EVT_MOUSE_EVENTS(wxShapeCanvas::OnMouseEvent)
69 END_EVENT_TABLE()
70
71 // Object canvas
72 wxShapeCanvas::wxShapeCanvas(wxWindow *parent, wxWindowID id, const wxPoint& pos, const wxSize& size, long style):
73 wxScrolledWindow(parent, id, pos, size, style)
74 {
75 m_shapeDiagram = NULL;
76 m_dragState = NoDragging;
77 m_draggedShape = NULL;
78 m_oldDragX = 0;
79 m_oldDragY = 0;
80 m_firstDragX = 0;
81 m_firstDragY = 0;
82 m_checkTolerance = TRUE;
83 }
84
85 wxShapeCanvas::~wxShapeCanvas()
86 {
87 }
88
89 void wxShapeCanvas::OnPaint(wxPaintEvent& event)
90 {
91 wxPaintDC dc(this);
92
93 PrepareDC(dc);
94
95 dc.Clear();
96
97 if (GetDiagram())
98 GetDiagram()->Redraw(dc);
99 }
100
101 void wxShapeCanvas::OnMouseEvent(wxMouseEvent& event)
102 {
103 wxClientDC dc(this);
104 PrepareDC(dc);
105
106 wxPoint logPos(event.GetLogicalPosition(dc));
107
108 double x, y;
109 x = (double) logPos.x;
110 y = (double) logPos.y;
111
112 int keys = 0;
113 if (event.ShiftDown())
114 keys = keys | KEY_SHIFT;
115 if (event.ControlDown())
116 keys = keys | KEY_CTRL;
117
118 bool dragging = event.Dragging();
119
120 // Check if we're within the tolerance for mouse movements.
121 // If we're very close to the position we started dragging
122 // from, this may not be an intentional drag at all.
123 if (dragging)
124 {
125 int dx = abs(dc.LogicalToDeviceX(x - m_firstDragX));
126 int dy = abs(dc.LogicalToDeviceY(y - m_firstDragY));
127 if (m_checkTolerance && (dx <= GetDiagram()->GetMouseTolerance()) && (dy <= GetDiagram()->GetMouseTolerance()))
128 {
129 return;
130 }
131 else
132 // If we've ignored the tolerance once, then ALWAYS ignore
133 // tolerance in this drag, even if we come back within
134 // the tolerance range.
135 m_checkTolerance = FALSE;
136 }
137
138 // Dragging - note that the effect of dragging is left entirely up
139 // to the object, so no movement is done unless explicitly done by
140 // object.
141 if (dragging && m_draggedShape && m_dragState == StartDraggingLeft)
142 {
143 m_dragState = ContinueDraggingLeft;
144
145 // If the object isn't m_draggable, transfer message to canvas
146 if (m_draggedShape->Draggable())
147 m_draggedShape->GetEventHandler()->OnBeginDragLeft((double)x, (double)y, keys, m_draggedAttachment);
148 else
149 {
150 m_draggedShape = NULL;
151 OnBeginDragLeft((double)x, (double)y, keys);
152 }
153
154 m_oldDragX = x; m_oldDragY = y;
155 }
156 else if (dragging && m_draggedShape && m_dragState == ContinueDraggingLeft)
157 {
158 // Continue dragging
159 m_draggedShape->GetEventHandler()->OnDragLeft(FALSE, m_oldDragX, m_oldDragY, keys, m_draggedAttachment);
160 m_draggedShape->GetEventHandler()->OnDragLeft(TRUE, (double)x, (double)y, keys, m_draggedAttachment);
161 m_oldDragX = x; m_oldDragY = y;
162 }
163 else if (event.LeftUp() && m_draggedShape && m_dragState == ContinueDraggingLeft)
164 {
165 m_dragState = NoDragging;
166 m_checkTolerance = TRUE;
167
168 m_draggedShape->GetEventHandler()->OnDragLeft(FALSE, m_oldDragX, m_oldDragY, keys, m_draggedAttachment);
169
170 m_draggedShape->GetEventHandler()->OnEndDragLeft((double)x, (double)y, keys, m_draggedAttachment);
171 m_draggedShape = NULL;
172 }
173 else if (dragging && m_draggedShape && m_dragState == StartDraggingRight)
174 {
175 m_dragState = ContinueDraggingRight;
176
177 if (m_draggedShape->Draggable())
178 m_draggedShape->GetEventHandler()->OnBeginDragRight((double)x, (double)y, keys, m_draggedAttachment);
179 else
180 {
181 m_draggedShape = NULL;
182 OnBeginDragRight((double)x, (double)y, keys);
183 }
184 m_oldDragX = x; m_oldDragY = y;
185 }
186 else if (dragging && m_draggedShape && m_dragState == ContinueDraggingRight)
187 {
188 // Continue dragging
189 m_draggedShape->GetEventHandler()->OnDragRight(FALSE, m_oldDragX, m_oldDragY, keys, m_draggedAttachment);
190 m_draggedShape->GetEventHandler()->OnDragRight(TRUE, (double)x, (double)y, keys, m_draggedAttachment);
191 m_oldDragX = x; m_oldDragY = y;
192 }
193 else if (event.RightUp() && m_draggedShape && m_dragState == ContinueDraggingRight)
194 {
195 m_dragState = NoDragging;
196 m_checkTolerance = TRUE;
197
198 m_draggedShape->GetEventHandler()->OnDragRight(FALSE, m_oldDragX, m_oldDragY, keys, m_draggedAttachment);
199
200 m_draggedShape->GetEventHandler()->OnEndDragRight((double)x, (double)y, keys, m_draggedAttachment);
201 m_draggedShape = NULL;
202 }
203
204 // All following events sent to canvas, not object
205 else if (dragging && !m_draggedShape && m_dragState == StartDraggingLeft)
206 {
207 m_dragState = ContinueDraggingLeft;
208 OnBeginDragLeft((double)x, (double)y, keys);
209 m_oldDragX = x; m_oldDragY = y;
210 }
211 else if (dragging && !m_draggedShape && m_dragState == ContinueDraggingLeft)
212 {
213 // Continue dragging
214 OnDragLeft(FALSE, m_oldDragX, m_oldDragY, keys);
215 OnDragLeft(TRUE, (double)x, (double)y, keys);
216 m_oldDragX = x; m_oldDragY = y;
217 }
218 else if (event.LeftUp() && !m_draggedShape && m_dragState == ContinueDraggingLeft)
219 {
220 m_dragState = NoDragging;
221 m_checkTolerance = TRUE;
222
223 OnDragLeft(FALSE, m_oldDragX, m_oldDragY, keys);
224 OnEndDragLeft((double)x, (double)y, keys);
225 m_draggedShape = NULL;
226 }
227 else if (dragging && !m_draggedShape && m_dragState == StartDraggingRight)
228 {
229 m_dragState = ContinueDraggingRight;
230 OnBeginDragRight((double)x, (double)y, keys);
231 m_oldDragX = x; m_oldDragY = y;
232 }
233 else if (dragging && !m_draggedShape && m_dragState == ContinueDraggingRight)
234 {
235 // Continue dragging
236 OnDragRight(FALSE, m_oldDragX, m_oldDragY, keys);
237 OnDragRight(TRUE, (double)x, (double)y, keys);
238 m_oldDragX = x; m_oldDragY = y;
239 }
240 else if (event.RightUp() && !m_draggedShape && m_dragState == ContinueDraggingRight)
241 {
242 m_dragState = NoDragging;
243 m_checkTolerance = TRUE;
244
245 OnDragRight(FALSE, m_oldDragX, m_oldDragY, keys);
246 OnEndDragRight((double)x, (double)y, keys);
247 m_draggedShape = NULL;
248 }
249
250 // Non-dragging events
251 else if (event.IsButton())
252 {
253 m_checkTolerance = TRUE;
254
255 // Find the nearest object
256 int attachment = 0;
257 wxShape *nearest_object = FindShape(x, y, &attachment);
258 if (nearest_object) // Object event
259 {
260 if (event.LeftDown())
261 {
262 m_draggedShape = nearest_object;
263 m_draggedAttachment = attachment;
264 m_dragState = StartDraggingLeft;
265 m_firstDragX = x;
266 m_firstDragY = y;
267 }
268 else if (event.LeftUp())
269 {
270 // N.B. Only register a click if the same object was
271 // identified for down *and* up.
272 if (nearest_object == m_draggedShape)
273 nearest_object->GetEventHandler()->OnLeftClick((double)x, (double)y, keys, attachment);
274
275 m_draggedShape = NULL;
276 m_dragState = NoDragging;
277 }
278 else if (event.LeftDClick())
279 {
280 nearest_object->GetEventHandler()->OnLeftDoubleClick((double)x, (double)y, keys, attachment);
281
282 m_draggedShape = NULL;
283 m_dragState = NoDragging;
284 }
285 else if (event.RightDown())
286 {
287 m_draggedShape = nearest_object;
288 m_draggedAttachment = attachment;
289 m_dragState = StartDraggingRight;
290 m_firstDragX = x;
291 m_firstDragY = y;
292 }
293 else if (event.RightUp())
294 {
295 if (nearest_object == m_draggedShape)
296 nearest_object->GetEventHandler()->OnRightClick((double)x, (double)y, keys, attachment);
297
298 m_draggedShape = NULL;
299 m_dragState = NoDragging;
300 }
301 }
302 else // Canvas event (no nearest object)
303 {
304 if (event.LeftDown())
305 {
306 m_draggedShape = NULL;
307 m_dragState = StartDraggingLeft;
308 m_firstDragX = x;
309 m_firstDragY = y;
310 }
311 else if (event.LeftUp())
312 {
313 OnLeftClick((double)x, (double)y, keys);
314
315 m_draggedShape = NULL;
316 m_dragState = NoDragging;
317 }
318 else if (event.RightDown())
319 {
320 m_draggedShape = NULL;
321 m_dragState = StartDraggingRight;
322 m_firstDragX = x;
323 m_firstDragY = y;
324 }
325 else if (event.RightUp())
326 {
327 OnRightClick((double)x, (double)y, keys);
328
329 m_draggedShape = NULL;
330 m_dragState = NoDragging;
331 }
332 }
333 }
334 }
335
336 /*
337 * Try to find a sensitive object, working up the hierarchy of composites.
338 *
339 */
340 wxShape *wxShapeCanvas::FindFirstSensitiveShape(double x, double y, int *new_attachment, int op)
341 {
342 wxShape *image = FindShape(x, y, new_attachment);
343 if (!image) return NULL;
344
345 wxShape *actualImage = FindFirstSensitiveShape1(image, op);
346 if (actualImage)
347 {
348 double dist;
349 // Find actual attachment
350 actualImage->HitTest(x, y, new_attachment, &dist);
351 }
352 return actualImage;
353 }
354
355 wxShape *wxShapeCanvas::FindFirstSensitiveShape1(wxShape *image, int op)
356 {
357 if (image->GetSensitivityFilter() & op)
358 return image;
359 if (image->GetParent())
360 return FindFirstSensitiveShape1(image->GetParent(), op);
361 return NULL;
362 }
363
364 // Helper function: TRUE if 'contains' wholly contains 'contained'.
365 static bool WhollyContains(wxShape *contains, wxShape *contained)
366 {
367 double xp1, yp1, xp2, yp2;
368 double w1, h1, w2, h2;
369 double left1, top1, right1, bottom1, left2, top2, right2, bottom2;
370
371 xp1 = contains->GetX(); yp1 = contains->GetY(); xp2 = contained->GetX(); yp2 = contained->GetY();
372 contains->GetBoundingBoxMax(&w1, &h1);
373 contained->GetBoundingBoxMax(&w2, &h2);
374
375 left1 = (double)(xp1 - (w1 / 2.0));
376 top1 = (double)(yp1 - (h1 / 2.0));
377 right1 = (double)(xp1 + (w1 / 2.0));
378 bottom1 = (double)(yp1 + (h1 / 2.0));
379
380 left2 = (double)(xp2 - (w2 / 2.0));
381 top2 = (double)(yp2 - (h2 / 2.0));
382 right2 = (double)(xp2 + (w2 / 2.0));
383 bottom2 = (double)(yp2 + (h2 / 2.0));
384
385 return ((left1 <= left2) && (top1 <= top2) && (right1 >= right2) && (bottom1 >= bottom2));
386 }
387
388 wxShape *wxShapeCanvas::FindShape(double x, double y, int *attachment, wxClassInfo *info, wxShape *notObject)
389 {
390 double nearest = 100000.0;
391 int nearest_attachment = 0;
392 wxShape *nearest_object = NULL;
393
394 // Go backward through the object list, since we want:
395 // (a) to have the control points drawn LAST to overlay
396 // the other objects
397 // (b) to find the control points FIRST if they exist
398
399 wxNode *current = GetDiagram()->GetShapeList()->Last();
400 while (current)
401 {
402 wxShape *object = (wxShape *)current->Data();
403
404 double dist;
405 int temp_attachment;
406
407 // First pass for lines, which might be inside a container, so we
408 // want lines to take priority over containers. This first loop
409 // could fail if we clickout side a line, so then we'll
410 // try other shapes.
411 if (object->IsShown() &&
412 object->IsKindOf(CLASSINFO(wxLineShape)) &&
413 object->HitTest(x, y, &temp_attachment, &dist) &&
414 ((info == NULL) || object->IsKindOf(info)) &&
415 (!notObject || !notObject->HasDescendant(object)))
416 {
417 // A line is trickier to spot than a normal object.
418 // For a line, since it's the diagonal of the box
419 // we use for the hit test, we may have several
420 // lines in the box and therefore we need to be able
421 // to specify the nearest point to the centre of the line
422 // as our hit criterion, to give the user some room for
423 // manouevre.
424 if (dist < nearest)
425 {
426 nearest = dist;
427 nearest_object = object;
428 nearest_attachment = temp_attachment;
429 }
430 }
431 if (current)
432 current = current->Previous();
433 }
434
435 current = GetDiagram()->GetShapeList()->Last();
436 while (current)
437 {
438 wxShape *object = (wxShape *)current->Data();
439 double dist;
440 int temp_attachment;
441
442 // On second pass, only ever consider non-composites or divisions. If children want to pass
443 // up control to the composite, that's up to them.
444 if (object->IsShown() && (object->IsKindOf(CLASSINFO(wxDivisionShape)) || !object->IsKindOf(CLASSINFO(wxCompositeShape)))
445 && object->HitTest(x, y, &temp_attachment, &dist) && ((info == NULL) || object->IsKindOf(info)) &&
446 (!notObject || !notObject->HasDescendant(object)))
447 {
448 if (!object->IsKindOf(CLASSINFO(wxLineShape)))
449 {
450 // If we've hit a container, and we have already found a line in the
451 // first pass, then ignore the container in case the line is in the container.
452 // Check for division in case line straddles divisions (i.e. is not wholly contained).
453 if (!nearest_object || !(object->IsKindOf(CLASSINFO(wxDivisionShape)) || WhollyContains(object, nearest_object)))
454 {
455 nearest = dist;
456 nearest_object = object;
457 nearest_attachment = temp_attachment;
458 current = NULL;
459 }
460 }
461 }
462 if (current)
463 current = current->Previous();
464 }
465
466 *attachment = nearest_attachment;
467 return nearest_object;
468 }
469
470 /*
471 * Higher-level events called by OnEvent
472 *
473 */
474
475 void wxShapeCanvas::OnLeftClick(double x, double y, int keys)
476 {
477 }
478
479 void wxShapeCanvas::OnRightClick(double x, double y, int keys)
480 {
481 }
482
483 void wxShapeCanvas::OnDragLeft(bool draw, double x, double y, int keys)
484 {
485 }
486
487 void wxShapeCanvas::OnBeginDragLeft(double x, double y, int keys)
488 {
489 }
490
491 void wxShapeCanvas::OnEndDragLeft(double x, double y, int keys)
492 {
493 }
494
495 void wxShapeCanvas::OnDragRight(bool draw, double x, double y, int keys)
496 {
497 }
498
499 void wxShapeCanvas::OnBeginDragRight(double x, double y, int keys)
500 {
501 }
502
503 void wxShapeCanvas::OnEndDragRight(double x, double y, int keys)
504 {
505 }
506
507 void wxShapeCanvas::AddShape(wxShape *object, wxShape *addAfter)
508 { GetDiagram()->AddShape(object, addAfter); }
509 void wxShapeCanvas::InsertShape(wxShape *object)
510 { GetDiagram()->InsertShape(object); }
511 void wxShapeCanvas::RemoveShape(wxShape *object)
512 { GetDiagram()->RemoveShape(object); }
513 bool wxShapeCanvas::GetQuickEditMode()
514 { return GetDiagram()->GetQuickEditMode(); }
515 void wxShapeCanvas::Redraw(wxDC& dc)
516 { GetDiagram()->Redraw(dc); }
517 void wxShapeCanvas::Snap(double *x, double *y)
518 { GetDiagram()->Snap(x, y); }