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