forked from GeoDaCenter/geoda
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdivided.cpp
701 lines (599 loc) · 19.3 KB
/
divided.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
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
/////////////////////////////////////////////////////////////////////////////
// Name: divided.cpp
// Purpose: wxDividedShape class
// Author: Julian Smart
// Modified by:
// Created: 12/07/98
// RCS-ID: $Id: divided.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"
#ifdef __BORLANDC__
#pragma hdrstop
#endif
#ifndef WX_PRECOMP
#include "wx/wx.h"
#endif
#if wxUSE_PROLOGIO
#include "wx/deprecated/wxexpr.h"
#endif
#include "ogl.h"
class wxDividedShapeControlPoint: public wxControlPoint
{
DECLARE_DYNAMIC_CLASS(wxDividedShapeControlPoint)
private:
int regionId;
public:
wxDividedShapeControlPoint() { regionId = 0; }
wxDividedShapeControlPoint(wxShapeCanvas *the_canvas, wxShape *object, int region,
double size, double the_xoffset, double the_yoffset, int the_type);
~wxDividedShapeControlPoint();
void OnDragLeft(bool draw, double x, double y, int keys=0, int attachment = 0);
void OnBeginDragLeft(double x, double y, int keys=0, int attachment = 0);
void OnEndDragLeft(double x, double y, int keys=0, int attachment = 0);
};
IMPLEMENT_DYNAMIC_CLASS(wxDividedShapeControlPoint, wxControlPoint)
/*
* Divided object
*
*/
IMPLEMENT_DYNAMIC_CLASS(wxDividedShape, wxRectangleShape)
wxDividedShape::wxDividedShape(double w, double h): wxRectangleShape(w, h)
{
ClearRegions();
}
wxDividedShape::~wxDividedShape()
{
}
void wxDividedShape::OnDraw(wxDC& dc)
{
wxRectangleShape::OnDraw(dc);
}
void wxDividedShape::OnDrawContents(wxDC& dc)
{
double defaultProportion = (double)(GetRegions().GetCount() > 0 ? (1.0/((double)(GetRegions().GetCount()))) : 0.0);
double currentY = (double)(m_ypos - (m_height / 2.0));
double maxY = (double)(m_ypos + (m_height / 2.0));
double leftX = (double)(m_xpos - (m_width / 2.0));
double rightX = (double)(m_xpos + (m_width / 2.0));
if (m_pen) dc.SetPen(* m_pen);
dc.SetTextForeground(m_textColour);
#ifdef __WXMSW__
// For efficiency, don't do this under X - doesn't make
// any visible difference for our purposes.
if (m_brush)
dc.SetTextBackground(m_brush->GetColour());
#endif
/*
if (!formatted)
{
FormatRegionText();
formatted = true;
}
*/
if (GetDisableLabel()) return;
double xMargin = 2;
double yMargin = 2;
dc.SetBackgroundMode(wxTRANSPARENT);
wxObjectList::compatibility_iterator node = GetRegions().GetFirst();
while (node)
{
wxShapeRegion *region = (wxShapeRegion *)node->GetData();
dc.SetFont(* region->GetFont());
dc.SetTextForeground(region->GetActualColourObject());
double proportion =
region->m_regionProportionY < 0.0 ? defaultProportion : region->m_regionProportionY;
double y = currentY + m_height*proportion;
double actualY = maxY < y ? maxY : y;
double centreX = m_xpos;
double centreY = (double)(currentY + (actualY - currentY)/2.0);
oglDrawFormattedText(dc, ®ion->m_formattedText,
(double)(centreX), (double)(centreY), (double)(m_width-2*xMargin), (double)(actualY - currentY - 2*yMargin),
region->m_formatMode);
if ((y <= maxY) && (node->GetNext()))
{
wxPen *regionPen = region->GetActualPen();
if (regionPen)
{
dc.SetPen(* regionPen);
dc.DrawLine(WXROUND(leftX), WXROUND(y), WXROUND(rightX), WXROUND(y));
}
}
currentY = actualY;
node = node->GetNext();
}
}
void wxDividedShape::SetSize(double w, double h, bool WXUNUSED(recursive))
{
SetAttachmentSize(w, h);
m_width = w;
m_height = h;
SetRegionSizes();
}
void wxDividedShape::SetRegionSizes()
{
if (GetRegions().GetCount() == 0)
return;
double defaultProportion = (double)(GetRegions().GetCount() > 0 ? (1.0/((double)(GetRegions().GetCount()))) : 0.0);
double currentY = (double)(m_ypos - (m_height / 2.0));
double maxY = (double)(m_ypos + (m_height / 2.0));
// double leftX = (double)(m_xpos - (m_width / 2.0));
// double rightX = (double)(m_xpos + (m_width / 2.0));
wxObjectList::compatibility_iterator node = GetRegions().GetFirst();
while (node)
{
wxShapeRegion *region = (wxShapeRegion *)node->GetData();
double proportion =
region->m_regionProportionY <= 0.0 ? defaultProportion : region->m_regionProportionY;
double sizeY = (double)proportion*m_height;
double y = currentY + sizeY;
double actualY = maxY < y ? maxY : y;
double centreY = (double)(currentY + (actualY - currentY)/2.0);
region->SetSize(m_width, sizeY);
region->SetPosition(0.0, (double)(centreY - m_ypos));
currentY = actualY;
node = node->GetNext();
}
}
// Attachment points correspond to regions in the divided box
bool wxDividedShape::GetAttachmentPosition(int attachment, double *x, double *y, int nth, int no_arcs,
wxLineShape *line)
{
int totalNumberAttachments = (GetRegions().GetCount() * 2) + 2;
if ((GetAttachmentMode() == ATTACHMENT_MODE_NONE) || (attachment >= totalNumberAttachments))
{
return wxShape::GetAttachmentPosition(attachment, x, y, nth, no_arcs);
}
int n = GetRegions().GetCount();
bool isEnd = (line && line->IsEnd(this));
double left = (double)(m_xpos - m_width/2.0);
double right = (double)(m_xpos + m_width/2.0);
double top = (double)(m_ypos - m_height/2.0);
double bottom = (double)(m_ypos + m_height/2.0);
// Zero is top, n+1 is bottom.
if (attachment == 0)
{
*y = top;
if (m_spaceAttachments)
{
if (line && (line->GetAlignmentType(isEnd) == LINE_ALIGNMENT_TO_NEXT_HANDLE))
{
// Align line according to the next handle along
wxRealPoint *point = line->GetNextControlPoint(this);
if (point->x < left)
*x = left;
else if (point->x > right)
*x = right;
else
*x = point->x;
}
else
*x = left + (nth + 1)*m_width/(no_arcs + 1);
}
else
*x = m_xpos;
}
else if (attachment == (n+1))
{
*y = bottom;
if (m_spaceAttachments)
{
if (line && (line->GetAlignmentType(isEnd) == LINE_ALIGNMENT_TO_NEXT_HANDLE))
{
// Align line according to the next handle along
wxRealPoint *point = line->GetNextControlPoint(this);
if (point->x < left)
*x = left;
else if (point->x > right)
*x = right;
else
*x = point->x;
}
else
*x = left + (nth + 1)*m_width/(no_arcs + 1);
}
else
*x = m_xpos;
}
// Left or right.
else
{
bool isLeft = !(attachment < (n+1));
int i = (isLeft) ? (totalNumberAttachments - attachment - 1) : (attachment-1);
wxObjectList::compatibility_iterator node = GetRegions().Item(i);
if (node)
{
wxShapeRegion *region = (wxShapeRegion *)node->GetData();
if (isLeft)
*x = left;
else
*x = right;
// Calculate top and bottom of region
top = (double)((m_ypos + region->m_y) - (region->m_height/2.0));
bottom = (double)((m_ypos + region->m_y) + (region->m_height/2.0));
// Assuming we can trust the absolute size and
// position of these regions...
if (m_spaceAttachments)
{
if (line && (line->GetAlignmentType(isEnd) == LINE_ALIGNMENT_TO_NEXT_HANDLE))
{
// Align line according to the next handle along
wxRealPoint *point = line->GetNextControlPoint(this);
if (point->y < bottom)
*y = bottom;
else if (point->y > top)
*y = top;
else
*y = point->y;
}
else
// *y = (double)(((m_ypos + region->m_y) - (region->m_height/2.0)) + (nth + 1)*region->m_height/(no_arcs+1));
*y = (double)(top + (nth + 1)*region->m_height/(no_arcs+1));
}
else
*y = (double)(m_ypos + region->m_y);
}
else
{
*x = m_xpos;
*y = m_ypos;
return false;
}
}
return true;
}
int wxDividedShape::GetNumberOfAttachments() const
{
// There are two attachments for each region (left and right),
// plus one on the top and one on the bottom.
int n = (GetRegions().GetCount() * 2) + 2;
int maxN = n - 1;
wxObjectList::compatibility_iterator node = m_attachmentPoints.GetFirst();
while (node)
{
wxAttachmentPoint *point = (wxAttachmentPoint *)node->GetData();
if (point->m_id > maxN)
maxN = point->m_id;
node = node->GetNext();
}
return maxN + 1;
}
bool wxDividedShape::AttachmentIsValid(int attachment) const
{
int totalNumberAttachments = (GetRegions().GetCount() * 2) + 2;
if (attachment >= totalNumberAttachments)
{
return wxShape::AttachmentIsValid(attachment);
}
else if (attachment >= 0)
return true;
else
return false;
}
void wxDividedShape::Copy(wxShape& copy)
{
wxRectangleShape::Copy(copy);
}
// Region operations
void wxDividedShape::MakeControlPoints()
{
wxRectangleShape::MakeControlPoints();
MakeMandatoryControlPoints();
}
void wxDividedShape::MakeMandatoryControlPoints()
{
double currentY = (double)(GetY() - (m_height / 2.0));
double maxY = (double)(GetY() + (m_height / 2.0));
wxObjectList::compatibility_iterator node = GetRegions().GetFirst();
int i = 0;
while (node)
{
wxShapeRegion *region = (wxShapeRegion *)node->GetData();
double proportion = region->m_regionProportionY;
double y = currentY + m_height*proportion;
double actualY = (double)(maxY < y ? maxY : y);
if (node->GetNext())
{
wxDividedShapeControlPoint *controlPoint =
new wxDividedShapeControlPoint(m_canvas, this, i, CONTROL_POINT_SIZE, 0.0, (double)(actualY - GetY()), 0);
m_canvas->AddShape(controlPoint);
m_controlPoints.Append(controlPoint);
}
currentY = actualY;
i ++;
node = node->GetNext();
}
}
void wxDividedShape::ResetControlPoints()
{
// May only have the region handles, (n - 1) of them.
if (m_controlPoints.GetCount() > (GetRegions().GetCount() - 1))
wxRectangleShape::ResetControlPoints();
ResetMandatoryControlPoints();
}
void wxDividedShape::ResetMandatoryControlPoints()
{
double currentY = (double)(GetY() - (m_height / 2.0));
double maxY = (double)(GetY() + (m_height / 2.0));
wxObjectList::compatibility_iterator node = m_controlPoints.GetFirst();
int i = 0;
while (node)
{
wxControlPoint *controlPoint = (wxControlPoint *)node->GetData();
if (controlPoint->IsKindOf(CLASSINFO(wxDividedShapeControlPoint)))
{
wxObjectList::compatibility_iterator node1 = GetRegions().Item(i);
wxShapeRegion *region = (wxShapeRegion *)node1->GetData();
double proportion = region->m_regionProportionY;
double y = currentY + m_height*proportion;
double actualY = (double)(maxY < y ? maxY : y);
controlPoint->m_xoffset = 0.0;
controlPoint->m_yoffset = (double)(actualY - GetY());
currentY = actualY;
i ++;
}
node = node->GetNext();
}
}
#if wxUSE_PROLOGIO
void wxDividedShape::WriteAttributes(wxExpr *clause)
{
wxRectangleShape::WriteAttributes(clause);
}
void wxDividedShape::ReadAttributes(wxExpr *clause)
{
wxRectangleShape::ReadAttributes(clause);
}
#endif
/*
* Edit the division colour/style
*
*/
void wxDividedShape::EditRegions()
{
wxMessageBox("EditRegions() is unimplemented.", "OGL", wxOK);
// TODO
#if 0
if (GetRegions().GetCount() < 2)
return;
wxBeginBusyCursor();
GraphicsForm *form = new GraphicsForm("Divided nodes");
// Need an array to store all the style strings,
// since they need to be converted to integers
char **styleStrings = new char *[GetRegions().GetCount()];
for (int j = 0; j < GetRegions().GetCount(); j++)
styleStrings[j] = NULL;
int i = 0;
wxNode *node = GetRegions().GetFirst();
while (node && node->GetNext())
{
wxShapeRegion *region = (wxShapeRegion *)node->GetData();
char buf[50];
sprintf(buf, "Region %d", (i+1));
form->Add(wxMakeFormMessage(buf));
form->Add(wxMakeFormNewLine());
form->Add(wxMakeFormString("Colour", ®ion->penColour, wxFORM_CHOICE,
new wxList(wxMakeConstraintStrings(
"Invisible" ,
"BLACK" ,
"BLUE" ,
"BROWN" ,
"CORAL" ,
"CYAN" ,
"DARK GREY" ,
"DARK GREEN" ,
"DIM GREY" ,
"GREY" ,
"GREEN" ,
"LIGHT BLUE" ,
"LIGHT GREY" ,
"MAGENTA" ,
"MAROON" ,
"NAVY" ,
"ORANGE" ,
"PURPLE" ,
"RED" ,
"TURQUOISE" ,
"VIOLET" ,
"WHITE" ,
"YELLOW" ,
NULL),
NULL), NULL, wxVERTICAL, 150));
char *styleString = NULL;
switch (region->penStyle)
{
case wxSHORT_DASH:
styleString = "Short Dash";
break;
case wxLONG_DASH:
styleString = "Long Dash";
break;
case wxDOT:
styleString = "Dot";
break;
case wxDOT_DASH:
styleString = "Dot Dash";
break;
case wxSOLID:
default:
styleString = "Solid";
break;
}
styleStrings[i] = copystring(styleString);
form->Add(wxMakeFormString("Style", &(styleStrings[i]), wxFORM_CHOICE,
new wxList(wxMakeConstraintStrings(
"Solid" ,
"Short Dash" ,
"Long Dash" ,
"Dot" ,
"Dot Dash" ,
NULL),
NULL), NULL, wxVERTICAL, 100));
node = node->GetNext();
i ++;
if (node && node->GetNext())
form->Add(wxMakeFormNewLine());
}
wxDialogBox *dialog = new wxDialogBox(m_canvas->GetParent(), "Divided object properties", 10, 10, 500, 500);
if (GraphicsLabelFont)
dialog->SetLabelFont(GraphicsLabelFont);
if (GraphicsButtonFont)
dialog->SetButtonFont(GraphicsButtonFont);
form->AssociatePanel(dialog);
form->dialog = dialog;
dialog->Fit();
dialog->Centre(wxBOTH);
wxEndBusyCursor();
dialog->Show(true);
node = GetRegions().GetFirst();
i = 0;
while (node)
{
wxShapeRegion *region = (wxShapeRegion *)node->GetData();
if (styleStrings[i])
{
if (strcmp(styleStrings[i], "Solid") == 0)
region->penStyle = wxSOLID;
else if (strcmp(styleStrings[i], "Dot") == 0)
region->penStyle = wxDOT;
else if (strcmp(styleStrings[i], "Short Dash") == 0)
region->penStyle = wxSHORT_DASH;
else if (strcmp(styleStrings[i], "Long Dash") == 0)
region->penStyle = wxLONG_DASH;
else if (strcmp(styleStrings[i], "Dot Dash") == 0)
region->penStyle = wxDOT_DASH;
delete[] styleStrings[i];
}
region->m_actualPenObject = NULL;
node = node->GetNext();
i ++;
}
delete[] styleStrings;
Draw(dc);
#endif
}
void wxDividedShape::OnRightClick(double x, double y, int keys, int attachment)
{
if (keys & KEY_CTRL)
{
EditRegions();
}
else
{
wxRectangleShape::OnRightClick(x, y, keys, attachment);
}
}
wxDividedShapeControlPoint::wxDividedShapeControlPoint(wxShapeCanvas *the_canvas, wxShape *object,
int region, double size, double the_m_xoffset, double the_m_yoffset, int the_type):
wxControlPoint(the_canvas, object, size, the_m_xoffset, the_m_yoffset, the_type)
{
regionId = region;
}
wxDividedShapeControlPoint::~wxDividedShapeControlPoint()
{
}
// Implement resizing of divided object division
void wxDividedShapeControlPoint::OnDragLeft(bool WXUNUSED(draw), double WXUNUSED(x), double y, int WXUNUSED(keys), int WXUNUSED(attachment))
{
wxClientDC dc(GetCanvas());
GetCanvas()->PrepareDC(dc);
dc.SetLogicalFunction(OGLRBLF);
wxPen dottedPen(*wxBLACK, 1, wxDOT);
dc.SetPen(dottedPen);
dc.SetBrush((* wxTRANSPARENT_BRUSH));
wxDividedShape *dividedObject = (wxDividedShape *)m_shape;
double x1 = (double)(dividedObject->GetX() - (dividedObject->GetWidth()/2.0));
double y1 = y;
double x2 = (double)(dividedObject->GetX() + (dividedObject->GetWidth()/2.0));
double y2 = y;
dc.DrawLine(WXROUND(x1), WXROUND(y1), WXROUND(x2), WXROUND(y2));
}
void wxDividedShapeControlPoint::OnBeginDragLeft(double WXUNUSED(x), double y, int WXUNUSED(keys), int WXUNUSED(attachment))
{
wxClientDC dc(GetCanvas());
GetCanvas()->PrepareDC(dc);
wxDividedShape *dividedObject = (wxDividedShape *)m_shape;
dc.SetLogicalFunction(OGLRBLF);
wxPen dottedPen(*wxBLACK, 1, wxDOT);
dc.SetPen(dottedPen);
dc.SetBrush((* wxTRANSPARENT_BRUSH));
double x1 = (double)(dividedObject->GetX() - (dividedObject->GetWidth()/2.0));
double y1 = y;
double x2 = (double)(dividedObject->GetX() + (dividedObject->GetWidth()/2.0));
double y2 = y;
dc.DrawLine(WXROUND(x1), WXROUND(y1), WXROUND(x2), WXROUND(y2));
m_canvas->CaptureMouse();
}
void wxDividedShapeControlPoint::OnEndDragLeft(double WXUNUSED(x), double y, int WXUNUSED(keys), int WXUNUSED(attachment))
{
wxClientDC dc(GetCanvas());
GetCanvas()->PrepareDC(dc);
wxDividedShape *dividedObject = (wxDividedShape *)m_shape;
wxObjectList::compatibility_iterator node = dividedObject->GetRegions().Item(regionId);
if (!node)
return;
wxShapeRegion *thisRegion = (wxShapeRegion *)node->GetData();
wxShapeRegion *nextRegion = NULL; // Region below this one
dc.SetLogicalFunction(wxCOPY);
m_canvas->ReleaseMouse();
// Find the old top and bottom of this region,
// and calculate the new proportion for this region
// if legal.
double currentY = (double)(dividedObject->GetY() - (dividedObject->GetHeight() / 2.0));
double maxY = (double)(dividedObject->GetY() + (dividedObject->GetHeight() / 2.0));
// Save values
double thisRegionTop = 0.0;
double nextRegionBottom = 0.0;
node = dividedObject->GetRegions().GetFirst();
while (node)
{
wxShapeRegion *region = (wxShapeRegion *)node->GetData();
double proportion = region->m_regionProportionY;
double yy = currentY + (dividedObject->GetHeight()*proportion);
double actualY = (double)(maxY < yy ? maxY : yy);
if (region == thisRegion)
{
thisRegionTop = currentY;
if (node->GetNext())
nextRegion = (wxShapeRegion *)node->GetNext()->GetData();
}
if (region == nextRegion)
{
nextRegionBottom = actualY;
}
currentY = actualY;
node = node->GetNext();
}
if (!nextRegion)
return;
// Check that we haven't gone above this region or below
// next region.
if ((y <= thisRegionTop) || (y >= nextRegionBottom))
return;
dividedObject->EraseLinks(dc);
// Now calculate the new proportions of this region and the next region.
double thisProportion = (double)((y - thisRegionTop)/dividedObject->GetHeight());
double nextProportion = (double)((nextRegionBottom - y)/dividedObject->GetHeight());
thisRegion->SetProportions(0.0, thisProportion);
nextRegion->SetProportions(0.0, nextProportion);
m_yoffset = (double)(y - dividedObject->GetY());
// Now reformat text
int i = 0;
node = dividedObject->GetRegions().GetFirst();
while (node)
{
wxShapeRegion *region = (wxShapeRegion *)node->GetData();
if (region->GetText().IsEmpty())
{
wxString s(region->GetText());
dividedObject->FormatText(dc, s.c_str(), i);
}
node = node->GetNext();
i++;
}
dividedObject->SetRegionSizes();
dividedObject->Draw(dc);
dividedObject->GetEventHandler()->OnMoveLinks(dc);
}