]> git.saurik.com Git - wxWidgets.git/blob - utils/ogl/src/basic.cpp
Removed DrawOpenSpline since it doesn't seem to be needed, with required changes
[wxWidgets.git] / utils / ogl / src / basic.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: basic.cpp
3 // Purpose: Basic OGL classes
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 "basic.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 <stdio.h>
38 #include <ctype.h>
39 #include <math.h>
40
41 #include "basic.h"
42 #include "basicp.h"
43 #include "composit.h"
44 #include "lines.h"
45 #include "canvas.h"
46 #include "divided.h"
47 #include "misc.h"
48
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
54
55 // Line
56 #define CONTROL_POINT_ENDPOINT_TO 4
57 #define CONTROL_POINT_ENDPOINT_FROM 5
58 #define CONTROL_POINT_LINE 6
59
60 IMPLEMENT_DYNAMIC_CLASS(wxShapeTextLine, wxObject)
61 IMPLEMENT_DYNAMIC_CLASS(wxAttachmentPoint, wxObject)
62
63 wxShapeTextLine::wxShapeTextLine(double the_x, double the_y, const wxString& the_line)
64 {
65 m_x = the_x; m_y = the_y; m_line = the_line;
66 }
67
68 wxShapeTextLine::~wxShapeTextLine()
69 {
70 }
71
72 IMPLEMENT_ABSTRACT_CLASS(wxShapeEvtHandler, wxObject)
73
74 wxShapeEvtHandler::wxShapeEvtHandler(wxShapeEvtHandler *prev, wxShape *shape)
75 {
76 m_previousHandler = prev;
77 m_handlerShape = shape;
78 }
79
80 wxShapeEvtHandler::~wxShapeEvtHandler()
81 {
82 }
83
84 // Creates a copy of this event handler.
85 wxShapeEvtHandler* wxShapeEvtHandler::CreateNewCopy()
86 {
87 wxShapeEvtHandler* newObject = (wxShapeEvtHandler*) GetClassInfo()->CreateObject();
88
89 wxASSERT( (newObject != NULL) );
90 wxASSERT( (newObject->IsKindOf(CLASSINFO(wxShapeEvtHandler))) );
91
92 newObject->m_previousHandler = newObject;
93
94 CopyData(*newObject);
95
96 return newObject;
97 }
98
99
100 void wxShapeEvtHandler::OnDelete()
101 {
102 if (this != GetShape())
103 delete this;
104 }
105
106 void wxShapeEvtHandler::OnDraw(wxDC& dc)
107 {
108 if (m_previousHandler)
109 m_previousHandler->OnDraw(dc);
110 }
111
112 void wxShapeEvtHandler::OnMoveLinks(wxDC& dc)
113 {
114 if (m_previousHandler)
115 m_previousHandler->OnMoveLinks(dc);
116 }
117
118 void wxShapeEvtHandler::OnMoveLink(wxDC& dc, bool moveControlPoints)
119 {
120 if (m_previousHandler)
121 m_previousHandler->OnMoveLink(dc, moveControlPoints);
122 }
123
124 void wxShapeEvtHandler::OnDrawContents(wxDC& dc)
125 {
126 if (m_previousHandler)
127 m_previousHandler->OnDrawContents(dc);
128 }
129
130 void wxShapeEvtHandler::OnDrawBranches(wxDC& dc, bool erase)
131 {
132 if (m_previousHandler)
133 m_previousHandler->OnDrawBranches(dc, erase);
134 }
135
136 void wxShapeEvtHandler::OnSize(double x, double y)
137 {
138 if (m_previousHandler)
139 m_previousHandler->OnSize(x, y);
140 }
141
142 bool wxShapeEvtHandler::OnMovePre(wxDC& dc, double x, double y, double old_x, double old_y, bool display)
143 {
144 if (m_previousHandler)
145 return m_previousHandler->OnMovePre(dc, x, y, old_x, old_y, display);
146 else
147 return TRUE;
148 }
149
150 void wxShapeEvtHandler::OnMovePost(wxDC& dc, double x, double y, double old_x, double old_y, bool display)
151 {
152 if (m_previousHandler)
153 m_previousHandler->OnMovePost(dc, x, y, old_x, old_y, display);
154 }
155
156 void wxShapeEvtHandler::OnErase(wxDC& dc)
157 {
158 if (m_previousHandler)
159 m_previousHandler->OnErase(dc);
160 }
161
162 void wxShapeEvtHandler::OnEraseContents(wxDC& dc)
163 {
164 if (m_previousHandler)
165 m_previousHandler->OnEraseContents(dc);
166 }
167
168 void wxShapeEvtHandler::OnHighlight(wxDC& dc)
169 {
170 if (m_previousHandler)
171 m_previousHandler->OnHighlight(dc);
172 }
173
174 void wxShapeEvtHandler::OnLeftClick(double x, double y, int keys, int attachment)
175 {
176 if (m_previousHandler)
177 m_previousHandler->OnLeftClick(x, y, keys, attachment);
178 }
179
180 void wxShapeEvtHandler::OnLeftDoubleClick(double x, double y, int keys, int attachment)
181 {
182 if (m_previousHandler)
183 m_previousHandler->OnLeftDoubleClick(x, y, keys, attachment);
184 }
185
186 void wxShapeEvtHandler::OnRightClick(double x, double y, int keys, int attachment)
187 {
188 if (m_previousHandler)
189 m_previousHandler->OnRightClick(x, y, keys, attachment);
190 }
191
192 void wxShapeEvtHandler::OnDragLeft(bool draw, double x, double y, int keys, int attachment)
193 {
194 if (m_previousHandler)
195 m_previousHandler->OnDragLeft(draw, x, y, keys, attachment);
196 }
197
198 void wxShapeEvtHandler::OnBeginDragLeft(double x, double y, int keys, int attachment)
199 {
200 if (m_previousHandler)
201 m_previousHandler->OnBeginDragLeft(x, y, keys, attachment);
202 }
203
204 void wxShapeEvtHandler::OnEndDragLeft(double x, double y, int keys, int attachment)
205 {
206 if (m_previousHandler)
207 m_previousHandler->OnEndDragLeft(x, y, keys, attachment);
208 }
209
210 void wxShapeEvtHandler::OnDragRight(bool draw, double x, double y, int keys, int attachment)
211 {
212 if (m_previousHandler)
213 m_previousHandler->OnDragRight(draw, x, y, keys, attachment);
214 }
215
216 void wxShapeEvtHandler::OnBeginDragRight(double x, double y, int keys, int attachment)
217 {
218 if (m_previousHandler)
219 m_previousHandler->OnBeginDragRight(x, y, keys, attachment);
220 }
221
222 void wxShapeEvtHandler::OnEndDragRight(double x, double y, int keys, int attachment)
223 {
224 if (m_previousHandler)
225 m_previousHandler->OnEndDragRight(x, y, keys, attachment);
226 }
227
228 // Control points ('handles') redirect control to the actual shape, to make it easier
229 // to override sizing behaviour.
230 void wxShapeEvtHandler::OnSizingDragLeft(wxControlPoint* pt, bool draw, double x, double y, int keys, int attachment)
231 {
232 if (m_previousHandler)
233 m_previousHandler->OnSizingDragLeft(pt, draw, x, y, keys, attachment);
234 }
235
236 void wxShapeEvtHandler::OnSizingBeginDragLeft(wxControlPoint* pt, double x, double y, int keys, int attachment)
237 {
238 if (m_previousHandler)
239 m_previousHandler->OnSizingBeginDragLeft(pt, x, y, keys, attachment);
240 }
241
242 void wxShapeEvtHandler::OnSizingEndDragLeft(wxControlPoint* pt, double x, double y, int keys, int attachment)
243 {
244 if (m_previousHandler)
245 m_previousHandler->OnSizingEndDragLeft(pt, x, y, keys, attachment);
246 }
247
248 void wxShapeEvtHandler::OnDrawOutline(wxDC& dc, double x, double y, double w, double h)
249 {
250 if (m_previousHandler)
251 m_previousHandler->OnDrawOutline(dc, x, y, w, h);
252 }
253
254 void wxShapeEvtHandler::OnDrawControlPoints(wxDC& dc)
255 {
256 if (m_previousHandler)
257 m_previousHandler->OnDrawControlPoints(dc);
258 }
259
260 void wxShapeEvtHandler::OnEraseControlPoints(wxDC& dc)
261 {
262 if (m_previousHandler)
263 m_previousHandler->OnEraseControlPoints(dc);
264 }
265
266 // Can override this to prevent or intercept line reordering.
267 void wxShapeEvtHandler::OnChangeAttachment(int attachment, wxLineShape* line, wxList& ordering)
268 {
269 if (m_previousHandler)
270 m_previousHandler->OnChangeAttachment(attachment, line, ordering);
271 }
272
273 IMPLEMENT_ABSTRACT_CLASS(wxShape, wxShapeEvtHandler)
274
275 wxShape::wxShape(wxShapeCanvas *can)
276 {
277 m_eventHandler = this;
278 SetShape(this);
279 m_id = 0;
280 m_formatted = FALSE;
281 m_canvas = can;
282 m_xpos = 0.0; m_ypos = 0.0;
283 m_pen = g_oglBlackPen;
284 m_brush = wxWHITE_BRUSH;
285 m_font = g_oglNormalFont;
286 m_textColour = wxBLACK;
287 m_textColourName = "BLACK";
288 m_visible = FALSE;
289 m_clientData = NULL;
290 m_selected = FALSE;
291 m_attachmentMode = ATTACHMENT_MODE_NONE;
292 m_spaceAttachments = TRUE;
293 m_disableLabel = FALSE;
294 m_fixedWidth = FALSE;
295 m_fixedHeight = FALSE;
296 m_drawHandles = TRUE;
297 m_sensitivity = OP_ALL;
298 m_draggable = TRUE;
299 m_parent = NULL;
300 m_formatMode = FORMAT_CENTRE_HORIZ | FORMAT_CENTRE_VERT;
301 m_shadowMode = SHADOW_NONE;
302 m_shadowOffsetX = 6.0;
303 m_shadowOffsetY = 6.0;
304 m_shadowBrush = wxBLACK_BRUSH;
305 m_textMarginX = 5.0;
306 m_textMarginY = 5.0;
307 m_regionName = "0";
308 m_centreResize = TRUE;
309 m_maintainAspectRatio = FALSE;
310 m_highlighted = FALSE;
311 m_rotation = 0.0;
312 m_branchNeckLength = 10;
313 m_branchStemLength = 10;
314 m_branchSpacing = 10;
315 m_branchStyle = BRANCHING_ATTACHMENT_NORMAL;
316
317 // Set up a default region. Much of the above will be put into
318 // the region eventually (the duplication is for compatibility)
319 wxShapeRegion *region = new wxShapeRegion;
320 m_regions.Append(region);
321 region->SetName("0");
322 region->SetFont(g_oglNormalFont);
323 region->SetFormatMode(FORMAT_CENTRE_HORIZ | FORMAT_CENTRE_VERT);
324 region->SetColour("BLACK");
325 }
326
327 wxShape::~wxShape()
328 {
329 if (m_parent)
330 m_parent->GetChildren().DeleteObject(this);
331
332 ClearText();
333 ClearRegions();
334 ClearAttachments();
335
336 if (m_canvas)
337 m_canvas->RemoveShape(this);
338
339 GetEventHandler()->OnDelete();
340 }
341
342 void wxShape::SetHighlight(bool hi, bool recurse)
343 {
344 m_highlighted = hi;
345 if (recurse)
346 {
347 wxNode *node = m_children.First();
348 while (node)
349 {
350 wxShape *child = (wxShape *)node->Data();
351 child->SetHighlight(hi, recurse);
352 node = node->Next();
353 }
354 }
355 }
356
357 void wxShape::SetSensitivityFilter(int sens, bool recursive)
358 {
359 if (sens & OP_DRAG_LEFT)
360 m_draggable = TRUE;
361 else
362 m_draggable = FALSE;
363
364 m_sensitivity = sens;
365 if (recursive)
366 {
367 wxNode *node = m_children.First();
368 while (node)
369 {
370 wxShape *obj = (wxShape *)node->Data();
371 obj->SetSensitivityFilter(sens, TRUE);
372 node = node->Next();
373 }
374 }
375 }
376
377 void wxShape::SetDraggable(bool drag, bool recursive)
378 {
379 m_draggable = drag;
380 if (m_draggable)
381 m_sensitivity |= OP_DRAG_LEFT;
382 else
383 if (m_sensitivity & OP_DRAG_LEFT)
384 m_sensitivity = m_sensitivity - OP_DRAG_LEFT;
385
386 if (recursive)
387 {
388 wxNode *node = m_children.First();
389 while (node)
390 {
391 wxShape *obj = (wxShape *)node->Data();
392 obj->SetDraggable(drag, TRUE);
393 node = node->Next();
394 }
395 }
396 }
397
398 void wxShape::SetDrawHandles(bool drawH)
399 {
400 m_drawHandles = drawH;
401 wxNode *node = m_children.First();
402 while (node)
403 {
404 wxShape *obj = (wxShape *)node->Data();
405 obj->SetDrawHandles(drawH);
406 node = node->Next();
407 }
408 }
409
410 void wxShape::SetShadowMode(int mode, bool redraw)
411 {
412 if (redraw && GetCanvas())
413 {
414 wxClientDC dc(GetCanvas());
415 GetCanvas()->PrepareDC(dc);
416 Erase(dc);
417
418 m_shadowMode = mode;
419
420 Draw(dc);
421 }
422 else
423 {
424 m_shadowMode = mode;
425 }
426 }
427
428 void wxShape::SetCanvas(wxShapeCanvas *theCanvas)
429 {
430 m_canvas = theCanvas;
431 wxNode *node = m_children.First();
432 while (node)
433 {
434 wxShape *child = (wxShape *)node->Data();
435 child->SetCanvas(theCanvas);
436 node = node->Next();
437 }
438 }
439
440 void wxShape::AddToCanvas(wxShapeCanvas *theCanvas, wxShape *addAfter)
441 {
442 theCanvas->AddShape(this, addAfter);
443 wxNode *node = m_children.First();
444 wxShape *lastImage = this;
445 while (node)
446 {
447 wxShape *object = (wxShape *)node->Data();
448 object->AddToCanvas(theCanvas, lastImage);
449 lastImage = object;
450
451 node = node->Next();
452 }
453 }
454
455 // Insert at front of canvas
456 void wxShape::InsertInCanvas(wxShapeCanvas *theCanvas)
457 {
458 theCanvas->InsertShape(this);
459 wxNode *node = m_children.First();
460 wxShape *lastImage = this;
461 while (node)
462 {
463 wxShape *object = (wxShape *)node->Data();
464 object->AddToCanvas(theCanvas, lastImage);
465 lastImage = object;
466
467 node = node->Next();
468 }
469 }
470
471 void wxShape::RemoveFromCanvas(wxShapeCanvas *theCanvas)
472 {
473 if (Selected())
474 Select(FALSE);
475 theCanvas->RemoveShape(this);
476 wxNode *node = m_children.First();
477 while (node)
478 {
479 wxShape *object = (wxShape *)node->Data();
480 object->RemoveFromCanvas(theCanvas);
481
482 node = node->Next();
483 }
484 }
485
486 void wxShape::ClearAttachments()
487 {
488 wxNode *node = m_attachmentPoints.First();
489 while (node)
490 {
491 wxAttachmentPoint *point = (wxAttachmentPoint *)node->Data();
492 delete point;
493 node = node->Next();
494 }
495 m_attachmentPoints.Clear();
496 }
497
498 void wxShape::ClearText(int regionId)
499 {
500 if (regionId == 0)
501 {
502 m_text.DeleteContents(TRUE);
503 m_text.Clear();
504 m_text.DeleteContents(FALSE);
505 }
506 wxNode *node = m_regions.Nth(regionId);
507 if (!node)
508 return;
509 wxShapeRegion *region = (wxShapeRegion *)node->Data();
510 region->ClearText();
511 }
512
513 void wxShape::ClearRegions()
514 {
515 wxNode *node = m_regions.First();
516 while (node)
517 {
518 wxShapeRegion *region = (wxShapeRegion *)node->Data();
519 wxNode *next = node->Next();
520 delete region;
521 delete node;
522 node = next;
523 }
524 }
525
526 void wxShape::AddRegion(wxShapeRegion *region)
527 {
528 m_regions.Append(region);
529 }
530
531 void wxShape::SetDefaultRegionSize()
532 {
533 wxNode *node = m_regions.First();
534 if (!node) return;
535 wxShapeRegion *region = (wxShapeRegion *)node->Data();
536 double w, h;
537 GetBoundingBoxMin(&w, &h);
538 region->SetSize(w, h);
539 }
540
541 bool wxShape::HitTest(double x, double y, int *attachment, double *distance)
542 {
543 // if (!sensitive)
544 // return FALSE;
545
546 double width = 0.0, height = 0.0;
547 GetBoundingBoxMin(&width, &height);
548 if (fabs(width) < 4.0) width = 4.0;
549 if (fabs(height) < 4.0) height = 4.0;
550
551 width += (double)4.0; height += (double)4.0; // Allowance for inaccurate mousing
552
553 double left = (double)(m_xpos - (width/2.0));
554 double top = (double)(m_ypos - (height/2.0));
555 double right = (double)(m_xpos + (width/2.0));
556 double bottom = (double)(m_ypos + (height/2.0));
557
558 int nearest_attachment = 0;
559
560 // If within the bounding box, check the attachment points
561 // within the object.
562
563 if (x >= left && x <= right && y >= top && y <= bottom)
564 {
565 int n = GetNumberOfAttachments();
566 double nearest = 999999.0;
567
568 // GetAttachmentPosition[Edge] takes a logical attachment position,
569 // i.e. if it's rotated through 90%, position 0 is East-facing.
570
571 for (int i = 0; i < n; i++)
572 {
573 double xp, yp;
574 if (GetAttachmentPositionEdge(i, &xp, &yp))
575 {
576 double l = (double)sqrt(((xp - x) * (xp - x)) +
577 ((yp - y) * (yp - y)));
578
579 if (l < nearest)
580 {
581 nearest = l;
582 nearest_attachment = i;
583 }
584 }
585 }
586 *attachment = nearest_attachment;
587 *distance = nearest;
588 return TRUE;
589 }
590 else return FALSE;
591 }
592
593 // Format a text string according to the region size, adding
594 // strings with positions to region text list
595
596 static bool GraphicsInSizeToContents = FALSE; // Infinite recursion elimination
597 void wxShape::FormatText(wxDC& dc, const wxString& s, int i)
598 {
599 double w, h;
600 ClearText(i);
601
602 if (m_regions.Number() < 1)
603 return;
604 wxNode *node = m_regions.Nth(i);
605 if (!node)
606 return;
607
608 wxShapeRegion *region = (wxShapeRegion *)node->Data();
609 region->SetText(s);
610 dc.SetFont(region->GetFont());
611
612 region->GetSize(&w, &h);
613
614 wxStringList *stringList = oglFormatText(dc, s, (w-5), (h-5), region->GetFormatMode());
615 node = stringList->First();
616 while (node)
617 {
618 char *s = (char *)node->Data();
619 wxShapeTextLine *line = new wxShapeTextLine(0.0, 0.0, s);
620 region->GetFormattedText().Append((wxObject *)line);
621 node = node->Next();
622 }
623 delete stringList;
624 double actualW = w;
625 double actualH = h;
626 // Don't try to resize an object with more than one image (this case should be dealt
627 // with by overriden handlers)
628 if ((region->GetFormatMode() & FORMAT_SIZE_TO_CONTENTS) &&
629 (region->GetFormattedText().Number() > 0) &&
630 (m_regions.Number() == 1) && !GraphicsInSizeToContents)
631 {
632 oglGetCentredTextExtent(dc, &(region->GetFormattedText()), m_xpos, m_ypos, w, h, &actualW, &actualH);
633 if ((actualW+m_textMarginX != w ) || (actualH+m_textMarginY != h))
634 {
635 // If we are a descendant of a composite, must make sure the composite gets
636 // resized properly
637 wxShape *topAncestor = GetTopAncestor();
638
639 if (topAncestor != this)
640 {
641 // Make sure we don't recurse infinitely
642 GraphicsInSizeToContents = TRUE;
643
644 wxCompositeShape *composite = (wxCompositeShape *)topAncestor;
645 composite->Erase(dc);
646 SetSize(actualW+m_textMarginX, actualH+m_textMarginY);
647 Move(dc, m_xpos, m_ypos);
648 composite->CalculateSize();
649 if (composite->Selected())
650 {
651 composite->DeleteControlPoints(& dc);
652 composite->MakeControlPoints();
653 composite->MakeMandatoryControlPoints();
654 }
655 // Where infinite recursion might happen if we didn't stop it
656 composite->Draw(dc);
657
658 GraphicsInSizeToContents = FALSE;
659 }
660 else
661 {
662 Erase(dc);
663 SetSize(actualW+m_textMarginX, actualH+m_textMarginY);
664 Move(dc, m_xpos, m_ypos);
665 }
666 SetSize(actualW+m_textMarginX, actualH+m_textMarginY);
667 Move(dc, m_xpos, m_ypos);
668 EraseContents(dc);
669 }
670 }
671 oglCentreText(dc, &(region->GetFormattedText()), m_xpos, m_ypos, actualW, actualH, region->GetFormatMode());
672 m_formatted = TRUE;
673 }
674
675 void wxShape::Recentre(wxDC& dc)
676 {
677 double w, h;
678 GetBoundingBoxMin(&w, &h);
679
680 int noRegions = m_regions.Number();
681 for (int i = 0; i < noRegions; i++)
682 {
683 wxNode *node = m_regions.Nth(i);
684 if (node)
685 {
686 wxShapeRegion *region = (wxShapeRegion *)node->Data();
687 oglCentreText(dc, &(region->GetFormattedText()), m_xpos, m_ypos, w, h, region->GetFormatMode());
688 }
689 }
690 }
691
692 bool wxShape::GetPerimeterPoint(double x1, double y1,
693 double x2, double y2,
694 double *x3, double *y3)
695 {
696 return FALSE;
697 }
698
699 void wxShape::SetPen(wxPen *the_pen)
700 {
701 m_pen = the_pen;
702 }
703
704 void wxShape::SetBrush(wxBrush *the_brush)
705 {
706 m_brush = the_brush;
707 }
708
709 // Get the top-most (non-division) ancestor, or self
710 wxShape *wxShape::GetTopAncestor()
711 {
712 if (!GetParent())
713 return this;
714
715 if (GetParent()->IsKindOf(CLASSINFO(wxDivisionShape)))
716 return this;
717 else return GetParent()->GetTopAncestor();
718 }
719
720 /*
721 * Region functions
722 *
723 */
724 void wxShape::SetFont(wxFont *the_font, int regionId)
725 {
726 m_font = the_font;
727 wxNode *node = m_regions.Nth(regionId);
728 if (!node)
729 return;
730 wxShapeRegion *region = (wxShapeRegion *)node->Data();
731 region->SetFont(the_font);
732 }
733
734 wxFont *wxShape::GetFont(int n) const
735 {
736 wxNode *node = m_regions.Nth(n);
737 if (!node)
738 return NULL;
739 wxShapeRegion *region = (wxShapeRegion *)node->Data();
740 return region->GetFont();
741 }
742
743 void wxShape::SetFormatMode(int mode, int regionId)
744 {
745 wxNode *node = m_regions.Nth(regionId);
746 if (!node)
747 return;
748 wxShapeRegion *region = (wxShapeRegion *)node->Data();
749 region->SetFormatMode(mode);
750 }
751
752 int wxShape::GetFormatMode(int regionId) const
753 {
754 wxNode *node = m_regions.Nth(regionId);
755 if (!node)
756 return 0;
757 wxShapeRegion *region = (wxShapeRegion *)node->Data();
758 return region->GetFormatMode();
759 }
760
761 void wxShape::SetTextColour(const wxString& the_colour, int regionId)
762 {
763 wxColour *wxcolour = wxTheColourDatabase->FindColour(the_colour);
764 m_textColour = wxcolour;
765 m_textColourName = the_colour;
766
767 wxNode *node = m_regions.Nth(regionId);
768 if (!node)
769 return;
770 wxShapeRegion *region = (wxShapeRegion *)node->Data();
771 region->SetColour(the_colour);
772 }
773
774 wxString wxShape::GetTextColour(int regionId) const
775 {
776 wxNode *node = m_regions.Nth(regionId);
777 if (!node)
778 return wxString("");
779 wxShapeRegion *region = (wxShapeRegion *)node->Data();
780 return region->GetColour();
781 }
782
783 void wxShape::SetRegionName(const wxString& name, int regionId)
784 {
785 wxNode *node = m_regions.Nth(regionId);
786 if (!node)
787 return;
788 wxShapeRegion *region = (wxShapeRegion *)node->Data();
789 region->SetName(name);
790 }
791
792 wxString wxShape::GetRegionName(int regionId)
793 {
794 wxNode *node = m_regions.Nth(regionId);
795 if (!node)
796 return wxString("");
797 wxShapeRegion *region = (wxShapeRegion *)node->Data();
798 return region->GetName();
799 }
800
801 int wxShape::GetRegionId(const wxString& name)
802 {
803 wxNode *node = m_regions.First();
804 int i = 0;
805 while (node)
806 {
807 wxShapeRegion *region = (wxShapeRegion *)node->Data();
808 if (region->GetName() == name)
809 return i;
810 node = node->Next();
811 i ++;
812 }
813 return -1;
814 }
815
816 // Name all m_regions in all subimages recursively.
817 void wxShape::NameRegions(const wxString& parentName)
818 {
819 int n = GetNumberOfTextRegions();
820 char buf[100];
821 for (int i = 0; i < n; i++)
822 {
823 if (parentName.Length() > 0)
824 sprintf(buf, "%s.%d", (const char*) parentName, i);
825 else
826 sprintf(buf, "%d", i);
827 SetRegionName(buf, i);
828 }
829 wxNode *node = m_children.First();
830 int j = 0;
831 while (node)
832 {
833 wxShape *child = (wxShape *)node->Data();
834 if (parentName.Length() > 0)
835 sprintf(buf, "%s.%d", (const char*) parentName, j);
836 else
837 sprintf(buf, "%d", j);
838 child->NameRegions(buf);
839 node = node->Next();
840 j ++;
841 }
842 }
843
844 // Get a region by name, possibly looking recursively into composites.
845 wxShape *wxShape::FindRegion(const wxString& name, int *regionId)
846 {
847 int id = GetRegionId(name);
848 if (id > -1)
849 {
850 *regionId = id;
851 return this;
852 }
853
854 wxNode *node = m_children.First();
855 while (node)
856 {
857 wxShape *child = (wxShape *)node->Data();
858 wxShape *actualImage = child->FindRegion(name, regionId);
859 if (actualImage)
860 return actualImage;
861 node = node->Next();
862 }
863 return NULL;
864 }
865
866 // Finds all region names for this image (composite or simple).
867 // Supply empty string list.
868 void wxShape::FindRegionNames(wxStringList& list)
869 {
870 int n = GetNumberOfTextRegions();
871 for (int i = 0; i < n; i++)
872 {
873 wxString name(GetRegionName(i));
874 list.Add((const char*) name);
875 }
876
877 wxNode *node = m_children.First();
878 while (node)
879 {
880 wxShape *child = (wxShape *)node->Data();
881 child->FindRegionNames(list);
882 node = node->Next();
883 }
884 }
885
886 void wxShape::AssignNewIds()
887 {
888 // if (m_id == 0)
889 m_id = NewId();
890 wxNode *node = m_children.First();
891 while (node)
892 {
893 wxShape *child = (wxShape *)node->Data();
894 child->AssignNewIds();
895 node = node->Next();
896 }
897 }
898
899 void wxShape::OnDraw(wxDC& dc)
900 {
901 }
902
903 void wxShape::OnMoveLinks(wxDC& dc)
904 {
905 // Want to set the ends of all attached links
906 // to point to/from this object
907
908 wxNode *current = m_lines.First();
909 while (current)
910 {
911 wxLineShape *line = (wxLineShape *)current->Data();
912 line->GetEventHandler()->OnMoveLink(dc);
913 current = current->Next();
914 }
915 }
916
917
918 void wxShape::OnDrawContents(wxDC& dc)
919 {
920 double bound_x, bound_y;
921 GetBoundingBoxMin(&bound_x, &bound_y);
922 if (m_regions.Number() < 1) return;
923
924 if (m_pen) dc.SetPen(m_pen);
925
926 wxShapeRegion *region = (wxShapeRegion *)m_regions.First()->Data();
927 if (region->GetFont()) dc.SetFont(region->GetFont());
928
929 dc.SetTextForeground(* (region->GetActualColourObject()));
930 dc.SetBackgroundMode(wxTRANSPARENT);
931 if (!m_formatted)
932 {
933 oglCentreText(dc, &(region->GetFormattedText()), m_xpos, m_ypos, bound_x, bound_y, region->GetFormatMode());
934 m_formatted = TRUE;
935 }
936 if (!GetDisableLabel())
937 {
938 oglDrawFormattedText(dc, &(region->GetFormattedText()), m_xpos, m_ypos, bound_x, bound_y, region->GetFormatMode());
939 }
940 }
941
942 void wxShape::DrawContents(wxDC& dc)
943 {
944 GetEventHandler()->OnDrawContents(dc);
945 }
946
947 void wxShape::OnSize(double x, double y)
948 {
949 }
950
951 bool wxShape::OnMovePre(wxDC& dc, double x, double y, double old_x, double old_y, bool display)
952 {
953 return TRUE;
954 }
955
956 void wxShape::OnMovePost(wxDC& dc, double x, double y, double old_x, double old_y, bool display)
957 {
958 }
959
960 void wxShape::OnErase(wxDC& dc)
961 {
962 if (!m_visible)
963 return;
964
965 // Erase links
966 wxNode *current = m_lines.First();
967 while (current)
968 {
969 wxLineShape *line = (wxLineShape *)current->Data();
970 line->GetEventHandler()->OnErase(dc);
971 current = current->Next();
972 }
973 GetEventHandler()->OnEraseContents(dc);
974 }
975
976 void wxShape::OnEraseContents(wxDC& dc)
977 {
978 if (!m_visible)
979 return;
980
981 double maxX, maxY, minX, minY;
982 double xp = GetX();
983 double yp = GetY();
984 GetBoundingBoxMin(&minX, &minY);
985 GetBoundingBoxMax(&maxX, &maxY);
986 double topLeftX = (double)(xp - (maxX / 2.0) - 2.0);
987 double topLeftY = (double)(yp - (maxY / 2.0) - 2.0);
988
989 int penWidth = 0;
990 if (m_pen)
991 penWidth = m_pen->GetWidth();
992
993 dc.SetPen(g_oglWhiteBackgroundPen);
994 dc.SetBrush(g_oglWhiteBackgroundBrush);
995 dc.DrawRectangle(WXROUND(topLeftX - penWidth), WXROUND(topLeftY - penWidth),
996 WXROUND(maxX + penWidth*2.0 + 4.0), WXROUND(maxY + penWidth*2.0 + 4.0));
997 }
998
999 void wxShape::EraseLinks(wxDC& dc, int attachment, bool recurse)
1000 {
1001 if (!m_visible)
1002 return;
1003
1004 wxNode *current = m_lines.First();
1005 while (current)
1006 {
1007 wxLineShape *line = (wxLineShape *)current->Data();
1008 if (attachment == -1 || ((line->GetTo() == this && line->GetAttachmentTo() == attachment) ||
1009 (line->GetFrom() == this && line->GetAttachmentFrom() == attachment)))
1010 line->GetEventHandler()->OnErase(dc);
1011 current = current->Next();
1012 }
1013 if (recurse)
1014 {
1015 wxNode *node = m_children.First();
1016 while (node)
1017 {
1018 wxShape *child = (wxShape *)node->Data();
1019 child->EraseLinks(dc, attachment, recurse);
1020 node = node->Next();
1021 }
1022 }
1023 }
1024
1025 void wxShape::DrawLinks(wxDC& dc, int attachment, bool recurse)
1026 {
1027 if (!m_visible)
1028 return;
1029
1030 wxNode *current = m_lines.First();
1031 while (current)
1032 {
1033 wxLineShape *line = (wxLineShape *)current->Data();
1034 if (attachment == -1 ||
1035 (line->GetTo() == this && line->GetAttachmentTo() == attachment) ||
1036 (line->GetFrom() == this && line->GetAttachmentFrom() == attachment))
1037 line->Draw(dc);
1038 current = current->Next();
1039 }
1040 if (recurse)
1041 {
1042 wxNode *node = m_children.First();
1043 while (node)
1044 {
1045 wxShape *child = (wxShape *)node->Data();
1046 child->DrawLinks(dc, attachment, recurse);
1047 node = node->Next();
1048 }
1049 }
1050 }
1051
1052 // Returns TRUE if pt1 <= pt2 in the sense that one point comes before another on an
1053 // edge of the shape.
1054 // attachmentPoint is the attachment point (= side) in question.
1055
1056 // This is the default, rectangular implementation.
1057 bool wxShape::AttachmentSortTest(int attachmentPoint, const wxRealPoint& pt1, const wxRealPoint& pt2)
1058 {
1059 int physicalAttachment = LogicalToPhysicalAttachment(attachmentPoint);
1060 switch (physicalAttachment)
1061 {
1062 case 0:
1063 case 2:
1064 {
1065 return (pt1.x <= pt2.x) ;
1066 break;
1067 }
1068 case 1:
1069 case 3:
1070 {
1071 return (pt1.y <= pt2.y) ;
1072 break;
1073 }
1074 }
1075
1076 return FALSE;
1077 }
1078
1079 bool wxShape::MoveLineToNewAttachment(wxDC& dc, wxLineShape *to_move,
1080 double x, double y)
1081 {
1082 if (GetAttachmentMode() == ATTACHMENT_MODE_NONE)
1083 return FALSE;
1084
1085 int newAttachment, oldAttachment;
1086 double distance;
1087
1088 // Is (x, y) on this object? If so, find the new attachment point
1089 // the user has moved the point to
1090 bool hit = HitTest(x, y, &newAttachment, &distance);
1091 if (!hit)
1092 return FALSE;
1093
1094 EraseLinks(dc);
1095
1096 if (to_move->GetTo() == this)
1097 oldAttachment = to_move->GetAttachmentTo();
1098 else
1099 oldAttachment = to_move->GetAttachmentFrom();
1100
1101 // The links in a new ordering.
1102 wxList newOrdering;
1103
1104 // First, add all links to the new list.
1105 wxNode *node = m_lines.First();
1106 while (node)
1107 {
1108 newOrdering.Append(node->Data());
1109 node = node->Next();
1110 }
1111
1112 // Delete the line object from the list of links; we're going to move
1113 // it to another position in the list
1114 newOrdering.DeleteObject(to_move);
1115
1116 double old_x = (double) -99999.9;
1117 double old_y = (double) -99999.9;
1118
1119 node = newOrdering.First();
1120 bool found = FALSE;
1121
1122 while (!found && node)
1123 {
1124 wxLineShape *line = (wxLineShape *)node->Data();
1125 if ((line->GetTo() == this && oldAttachment == line->GetAttachmentTo()) ||
1126 (line->GetFrom() == this && oldAttachment == line->GetAttachmentFrom()))
1127 {
1128 double startX, startY, endX, endY;
1129 double xp, yp;
1130 line->GetEnds(&startX, &startY, &endX, &endY);
1131 if (line->GetTo() == this)
1132 {
1133 xp = endX;
1134 yp = endY;
1135 } else
1136 {
1137 xp = startX;
1138 yp = startY;
1139 }
1140
1141 wxRealPoint thisPoint(xp, yp);
1142 wxRealPoint lastPoint(old_x, old_y);
1143 wxRealPoint newPoint(x, y);
1144
1145 if (AttachmentSortTest(newAttachment, newPoint, thisPoint) && AttachmentSortTest(newAttachment, lastPoint, newPoint))
1146 {
1147 found = TRUE;
1148 newOrdering.Insert(node, to_move);
1149 }
1150
1151 old_x = xp;
1152 old_y = yp;
1153 }
1154 node = node->Next();
1155 }
1156
1157 if (!found)
1158 newOrdering.Append(to_move);
1159
1160 GetEventHandler()->OnChangeAttachment(newAttachment, to_move, newOrdering);
1161
1162 return TRUE;
1163 }
1164
1165 void wxShape::OnChangeAttachment(int attachment, wxLineShape* line, wxList& ordering)
1166 {
1167 if (line->GetTo() == this)
1168 line->SetAttachmentTo(attachment);
1169 else
1170 line->SetAttachmentFrom(attachment);
1171
1172 ApplyAttachmentOrdering(ordering);
1173
1174 wxClientDC dc(GetCanvas());
1175 GetCanvas()->PrepareDC(dc);
1176
1177 MoveLinks(dc);
1178
1179 if (!GetCanvas()->GetQuickEditMode()) GetCanvas()->Redraw(dc);
1180 }
1181
1182 // Reorders the lines according to the given list.
1183 void wxShape::ApplyAttachmentOrdering(wxList& linesToSort)
1184 {
1185 // This is a temporary store of all the lines.
1186 wxList linesStore;
1187
1188 wxNode *node = m_lines.First();
1189 while (node)
1190 {
1191 wxLineShape *line = (wxLineShape *)node->Data();
1192 linesStore.Append(line);
1193 node = node->Next();;
1194 }
1195
1196 m_lines.Clear();
1197
1198 node = linesToSort.First();
1199 while (node)
1200 {
1201 wxLineShape *line = (wxLineShape *)node->Data();
1202 if (linesStore.Member(line))
1203 {
1204 // Done this one
1205 linesStore.DeleteObject(line);
1206 m_lines.Append(line);
1207 }
1208 node = node->Next();
1209 }
1210
1211 // Now add any lines that haven't been listed in linesToSort.
1212 node = linesStore.First();
1213 while (node)
1214 {
1215 wxLineShape *line = (wxLineShape *)node->Data();
1216 m_lines.Append(line);
1217 node = node->Next();
1218 }
1219 }
1220
1221 // Reorders the lines coming into the node image at this attachment
1222 // position, in the order in which they appear in linesToSort.
1223 // Any remaining lines not in the list will be added to the end.
1224 void wxShape::SortLines(int attachment, wxList& linesToSort)
1225 {
1226 // This is a temporary store of all the lines at this attachment
1227 // point. We'll tick them off as we've processed them.
1228 wxList linesAtThisAttachment;
1229
1230 wxNode *node = m_lines.First();
1231 while (node)
1232 {
1233 wxLineShape *line = (wxLineShape *)node->Data();
1234 wxNode *next = node->Next();
1235 if ((line->GetTo() == this && line->GetAttachmentTo() == attachment) ||
1236 (line->GetFrom() == this && line->GetAttachmentFrom() == attachment))
1237 {
1238 linesAtThisAttachment.Append(line);
1239 delete node;
1240 node = next;
1241 }
1242 else node = node->Next();
1243 }
1244
1245 node = linesToSort.First();
1246 while (node)
1247 {
1248 wxLineShape *line = (wxLineShape *)node->Data();
1249 if (linesAtThisAttachment.Member(line))
1250 {
1251 // Done this one
1252 linesAtThisAttachment.DeleteObject(line);
1253 m_lines.Append(line);
1254 }
1255 node = node->Next();
1256 }
1257
1258 // Now add any lines that haven't been listed in linesToSort.
1259 node = linesAtThisAttachment.First();
1260 while (node)
1261 {
1262 wxLineShape *line = (wxLineShape *)node->Data();
1263 m_lines.Append(line);
1264 node = node->Next();
1265 }
1266 }
1267
1268 void wxShape::OnHighlight(wxDC& dc)
1269 {
1270 }
1271
1272 void wxShape::OnLeftClick(double x, double y, int keys, int attachment)
1273 {
1274 if ((m_sensitivity & OP_CLICK_LEFT) != OP_CLICK_LEFT)
1275 {
1276 attachment = 0;
1277 double dist;
1278 if (m_parent)
1279 {
1280 m_parent->HitTest(x, y, &attachment, &dist);
1281 m_parent->GetEventHandler()->OnLeftClick(x, y, keys, attachment);
1282 }
1283 return;
1284 }
1285 }
1286
1287 void wxShape::OnRightClick(double x, double y, int keys, int attachment)
1288 {
1289 if ((m_sensitivity & OP_CLICK_RIGHT) != OP_CLICK_RIGHT)
1290 {
1291 attachment = 0;
1292 double dist;
1293 if (m_parent)
1294 {
1295 m_parent->HitTest(x, y, &attachment, &dist);
1296 m_parent->GetEventHandler()->OnRightClick(x, y, keys, attachment);
1297 }
1298 return;
1299 }
1300 }
1301
1302 double DragOffsetX = 0.0;
1303 double DragOffsetY = 0.0;
1304
1305 void wxShape::OnDragLeft(bool draw, double x, double y, int keys, int attachment)
1306 {
1307 if ((m_sensitivity & OP_DRAG_LEFT) != OP_DRAG_LEFT)
1308 {
1309 attachment = 0;
1310 double dist;
1311 if (m_parent)
1312 {
1313 m_parent->HitTest(x, y, &attachment, &dist);
1314 m_parent->GetEventHandler()->OnDragLeft(draw, x, y, keys, attachment);
1315 }
1316 return;
1317 }
1318
1319 wxClientDC dc(GetCanvas());
1320 GetCanvas()->PrepareDC(dc);
1321
1322 dc.SetLogicalFunction(wxXOR);
1323
1324 wxPen dottedPen(wxColour(0, 0, 0), 1, wxDOT);
1325 dc.SetPen(dottedPen);
1326 dc.SetBrush(* wxTRANSPARENT_BRUSH);
1327
1328 double xx, yy;
1329 xx = x + DragOffsetX;
1330 yy = y + DragOffsetY;
1331
1332 m_canvas->Snap(&xx, &yy);
1333 // m_xpos = xx; m_ypos = yy;
1334 double w, h;
1335 GetBoundingBoxMax(&w, &h);
1336 GetEventHandler()->OnDrawOutline(dc, xx, yy, w, h);
1337 }
1338
1339 void wxShape::OnBeginDragLeft(double x, double y, int keys, int attachment)
1340 {
1341 if ((m_sensitivity & OP_DRAG_LEFT) != OP_DRAG_LEFT)
1342 {
1343 attachment = 0;
1344 double dist;
1345 if (m_parent)
1346 {
1347 m_parent->HitTest(x, y, &attachment, &dist);
1348 m_parent->GetEventHandler()->OnBeginDragLeft(x, y, keys, attachment);
1349 }
1350 return;
1351 }
1352
1353 DragOffsetX = m_xpos - x;
1354 DragOffsetY = m_ypos - y;
1355
1356 wxClientDC dc(GetCanvas());
1357 GetCanvas()->PrepareDC(dc);
1358
1359 // New policy: don't erase shape until end of drag.
1360 // Erase(dc);
1361
1362 double xx, yy;
1363 xx = x + DragOffsetX;
1364 yy = y + DragOffsetY;
1365 m_canvas->Snap(&xx, &yy);
1366 // m_xpos = xx; m_ypos = yy;
1367 dc.SetLogicalFunction(wxXOR);
1368
1369 wxPen dottedPen(wxColour(0, 0, 0), 1, wxDOT);
1370 dc.SetPen(dottedPen);
1371 dc.SetBrush((* wxTRANSPARENT_BRUSH));
1372
1373 double w, h;
1374 GetBoundingBoxMax(&w, &h);
1375 GetEventHandler()->OnDrawOutline(dc, xx, yy, w, h);
1376 m_canvas->CaptureMouse();
1377 }
1378
1379 void wxShape::OnEndDragLeft(double x, double y, int keys, int attachment)
1380 {
1381 m_canvas->ReleaseMouse();
1382 if ((m_sensitivity & OP_DRAG_LEFT) != OP_DRAG_LEFT)
1383 {
1384 attachment = 0;
1385 double dist;
1386 if (m_parent)
1387 {
1388 m_parent->HitTest(x, y, &attachment, &dist);
1389 m_parent->GetEventHandler()->OnEndDragLeft(x, y, keys, attachment);
1390 }
1391 return;
1392 }
1393
1394 wxClientDC dc(GetCanvas());
1395 GetCanvas()->PrepareDC(dc);
1396
1397 dc.SetLogicalFunction(wxCOPY);
1398
1399 double xx = x + DragOffsetX;
1400 double yy = y + DragOffsetY;
1401 m_canvas->Snap(&xx, &yy);
1402 // canvas->Snap(&m_xpos, &m_ypos);
1403
1404 // New policy: erase shape at end of drag.
1405 Erase(dc);
1406
1407 Move(dc, xx, yy);
1408 if (m_canvas && !m_canvas->GetQuickEditMode()) m_canvas->Redraw(dc);
1409 }
1410
1411 void wxShape::OnDragRight(bool draw, double x, double y, int keys, int attachment)
1412 {
1413 if ((m_sensitivity & OP_DRAG_RIGHT) != OP_DRAG_RIGHT)
1414 {
1415 attachment = 0;
1416 double dist;
1417 if (m_parent)
1418 {
1419 m_parent->HitTest(x, y, &attachment, &dist);
1420 m_parent->GetEventHandler()->OnDragRight(draw, x, y, keys, attachment);
1421 }
1422 return;
1423 }
1424 }
1425
1426 void wxShape::OnBeginDragRight(double x, double y, int keys, int attachment)
1427 {
1428 if ((m_sensitivity & OP_DRAG_RIGHT) != OP_DRAG_RIGHT)
1429 {
1430 attachment = 0;
1431 double dist;
1432 if (m_parent)
1433 {
1434 m_parent->HitTest(x, y, &attachment, &dist);
1435 m_parent->GetEventHandler()->OnBeginDragRight(x, y, keys, attachment);
1436 }
1437 return;
1438 }
1439 }
1440
1441 void wxShape::OnEndDragRight(double x, double y, int keys, int attachment)
1442 {
1443 if ((m_sensitivity & OP_DRAG_RIGHT) != OP_DRAG_RIGHT)
1444 {
1445 attachment = 0;
1446 double dist;
1447 if (m_parent)
1448 {
1449 m_parent->HitTest(x, y, &attachment, &dist);
1450 m_parent->GetEventHandler()->OnEndDragRight(x, y, keys, attachment);
1451 }
1452 return;
1453 }
1454 }
1455
1456 void wxShape::OnDrawOutline(wxDC& dc, double x, double y, double w, double h)
1457 {
1458 double top_left_x = (double)(x - w/2.0);
1459 double top_left_y = (double)(y - h/2.0);
1460 double top_right_x = (double)(top_left_x + w);
1461 double top_right_y = (double)top_left_y;
1462 double bottom_left_x = (double)top_left_x;
1463 double bottom_left_y = (double)(top_left_y + h);
1464 double bottom_right_x = (double)top_right_x;
1465 double bottom_right_y = (double)bottom_left_y;
1466
1467 wxPoint points[5];
1468 points[0].x = WXROUND(top_left_x); points[0].y = WXROUND(top_left_y);
1469 points[1].x = WXROUND(top_right_x); points[1].y = WXROUND(top_right_y);
1470 points[2].x = WXROUND(bottom_right_x); points[2].y = WXROUND(bottom_right_y);
1471 points[3].x = WXROUND(bottom_left_x); points[3].y = WXROUND(bottom_left_y);
1472 points[4].x = WXROUND(top_left_x); points[4].y = WXROUND(top_left_y);
1473
1474 dc.DrawLines(5, points);
1475 }
1476
1477 void wxShape::Attach(wxShapeCanvas *can)
1478 {
1479 m_canvas = can;
1480 }
1481
1482 void wxShape::Detach()
1483 {
1484 m_canvas = NULL;
1485 }
1486
1487 void wxShape::Move(wxDC& dc, double x, double y, bool display)
1488 {
1489 double old_x = m_xpos;
1490 double old_y = m_ypos;
1491
1492 if (!GetEventHandler()->OnMovePre(dc, x, y, old_x, old_y, display))
1493 {
1494 // m_xpos = old_x;
1495 // m_ypos = old_y;
1496 return;
1497 }
1498
1499 m_xpos = x; m_ypos = y;
1500
1501 ResetControlPoints();
1502
1503 if (display)
1504 Draw(dc);
1505
1506 MoveLinks(dc);
1507
1508 GetEventHandler()->OnMovePost(dc, x, y, old_x, old_y, display);
1509 }
1510
1511 void wxShape::MoveLinks(wxDC& dc)
1512 {
1513 GetEventHandler()->OnMoveLinks(dc);
1514 }
1515
1516
1517 void wxShape::Draw(wxDC& dc)
1518 {
1519 if (m_visible)
1520 {
1521 GetEventHandler()->OnDraw(dc);
1522 GetEventHandler()->OnDrawContents(dc);
1523 GetEventHandler()->OnDrawControlPoints(dc);
1524 GetEventHandler()->OnDrawBranches(dc);
1525 }
1526 }
1527
1528 void wxShape::Flash()
1529 {
1530 if (GetCanvas())
1531 {
1532 wxClientDC dc(GetCanvas());
1533 GetCanvas()->PrepareDC(dc);
1534
1535 dc.SetLogicalFunction(wxXOR);
1536 Draw(dc);
1537 dc.SetLogicalFunction(wxCOPY);
1538 Draw(dc);
1539 }
1540 }
1541
1542 void wxShape::Show(bool show)
1543 {
1544 m_visible = show;
1545 wxNode *node = m_children.First();
1546 while (node)
1547 {
1548 wxShape *image = (wxShape *)node->Data();
1549 image->Show(show);
1550 node = node->Next();
1551 }
1552 }
1553
1554 void wxShape::Erase(wxDC& dc)
1555 {
1556 GetEventHandler()->OnErase(dc);
1557 GetEventHandler()->OnEraseControlPoints(dc);
1558 GetEventHandler()->OnDrawBranches(dc, TRUE);
1559 }
1560
1561 void wxShape::EraseContents(wxDC& dc)
1562 {
1563 GetEventHandler()->OnEraseContents(dc);
1564 }
1565
1566 void wxShape::AddText(const wxString& string)
1567 {
1568 wxNode *node = m_regions.First();
1569 if (!node)
1570 return;
1571 wxShapeRegion *region = (wxShapeRegion *)node->Data();
1572 region->ClearText();
1573 wxShapeTextLine *new_line =
1574 new wxShapeTextLine(0.0, 0.0, string);
1575 region->GetFormattedText().Append(new_line);
1576
1577 m_formatted = FALSE;
1578 }
1579
1580 void wxShape::SetSize(double x, double y, bool recursive)
1581 {
1582 SetAttachmentSize(x, y);
1583 SetDefaultRegionSize();
1584 }
1585
1586 void wxShape::SetAttachmentSize(double w, double h)
1587 {
1588 double scaleX;
1589 double scaleY;
1590 double width, height;
1591 GetBoundingBoxMin(&width, &height);
1592 if (width == 0.0)
1593 scaleX = 1.0;
1594 else scaleX = w/width;
1595 if (height == 0.0)
1596 scaleY = 1.0;
1597 else scaleY = h/height;
1598
1599 wxNode *node = m_attachmentPoints.First();
1600 while (node)
1601 {
1602 wxAttachmentPoint *point = (wxAttachmentPoint *)node->Data();
1603 point->m_x = (double)(point->m_x * scaleX);
1604 point->m_y = (double)(point->m_y * scaleY);
1605 node = node->Next();
1606 }
1607 }
1608
1609 // Add line FROM this object
1610 void wxShape::AddLine(wxLineShape *line, wxShape *other,
1611 int attachFrom, int attachTo,
1612 // The line ordering
1613 int positionFrom, int positionTo)
1614 {
1615 if (positionFrom == -1)
1616 {
1617 if (!m_lines.Member(line))
1618 m_lines.Append(line);
1619 }
1620 else
1621 {
1622 // Don't preserve old ordering if we have new ordering instructions
1623 m_lines.DeleteObject(line);
1624 if (positionFrom < m_lines.Number())
1625 {
1626 wxNode* node = m_lines.Nth(positionFrom);
1627 m_lines.Insert(node, line);
1628 }
1629 else
1630 m_lines.Append(line);
1631 }
1632
1633 if (positionTo == -1)
1634 {
1635 if (!other->m_lines.Member(line))
1636 other->m_lines.Append(line);
1637 }
1638 else
1639 {
1640 // Don't preserve old ordering if we have new ordering instructions
1641 other->m_lines.DeleteObject(line);
1642 if (positionTo < other->m_lines.Number())
1643 {
1644 wxNode* node = other->m_lines.Nth(positionTo);
1645 other->m_lines.Insert(node, line);
1646 }
1647 else
1648 other->m_lines.Append(line);
1649 }
1650 #if 0
1651 // Wrong: doesn't preserve ordering of shape already linked
1652 m_lines.DeleteObject(line);
1653 other->m_lines.DeleteObject(line);
1654
1655 if (positionFrom == -1)
1656 m_lines.Append(line);
1657 else
1658 {
1659 if (positionFrom < m_lines.Number())
1660 {
1661 wxNode* node = m_lines.Nth(positionFrom);
1662 m_lines.Insert(node, line);
1663 }
1664 else
1665 m_lines.Append(line);
1666 }
1667
1668 if (positionTo == -1)
1669 other->m_lines.Append(line);
1670 else
1671 {
1672 if (positionTo < other->m_lines.Number())
1673 {
1674 wxNode* node = other->m_lines.Nth(positionTo);
1675 other->m_lines.Insert(node, line);
1676 }
1677 else
1678 other->m_lines.Append(line);
1679 }
1680 #endif
1681
1682 line->SetFrom(this);
1683 line->SetTo(other);
1684 line->SetAttachments(attachFrom, attachTo);
1685 }
1686
1687 void wxShape::RemoveLine(wxLineShape *line)
1688 {
1689 if (line->GetFrom() == this)
1690 line->GetTo()->m_lines.DeleteObject(line);
1691 else
1692 line->GetFrom()->m_lines.DeleteObject(line);
1693
1694 m_lines.DeleteObject(line);
1695 }
1696
1697 #ifdef PROLOGIO
1698 void wxShape::WriteAttributes(wxExpr *clause)
1699 {
1700 clause->AddAttributeValueString("type", GetClassInfo()->GetClassName());
1701 clause->AddAttributeValue("id", m_id);
1702
1703 if (m_pen)
1704 {
1705 int penWidth = m_pen->GetWidth();
1706 int penStyle = m_pen->GetStyle();
1707 if (penWidth != 1)
1708 clause->AddAttributeValue("pen_width", (long)penWidth);
1709 if (penStyle != wxSOLID)
1710 clause->AddAttributeValue("pen_style", (long)penStyle);
1711
1712 wxString penColour = wxTheColourDatabase->FindName(m_pen->GetColour());
1713 if (penColour == "")
1714 {
1715 wxString hex(oglColourToHex(m_pen->GetColour()));
1716 hex = wxString("#") + hex;
1717 clause->AddAttributeValueString("pen_colour", hex);
1718 }
1719 else if (penColour != "BLACK")
1720 clause->AddAttributeValueString("pen_colour", penColour);
1721 }
1722
1723 if (m_brush)
1724 {
1725 wxString brushColour = wxTheColourDatabase->FindName(m_brush->GetColour());
1726
1727 if (brushColour == "")
1728 {
1729 wxString hex(oglColourToHex(m_brush->GetColour()));
1730 hex = wxString("#") + hex;
1731 clause->AddAttributeValueString("brush_colour", hex);
1732 }
1733 else if (brushColour != "WHITE")
1734 clause->AddAttributeValueString("brush_colour", brushColour);
1735
1736 if (m_brush->GetStyle() != wxSOLID)
1737 clause->AddAttributeValue("brush_style", (long)m_brush->GetStyle());
1738 }
1739
1740 // Output line ids
1741
1742 int n_lines = m_lines.Number();
1743 if (n_lines > 0)
1744 {
1745 wxExpr *list = new wxExpr(wxExprList);
1746 wxNode *node = m_lines.First();
1747 while (node)
1748 {
1749 wxShape *line = (wxShape *)node->Data();
1750 wxExpr *id_expr = new wxExpr(line->GetId());
1751 list->Append(id_expr);
1752 node = node->Next();
1753 }
1754 clause->AddAttributeValue("arcs", list);
1755 }
1756
1757 // Miscellaneous members
1758 if (m_attachmentMode != 0)
1759 clause->AddAttributeValue("use_attachments", (long)m_attachmentMode);
1760 if (m_sensitivity != OP_ALL)
1761 clause->AddAttributeValue("sensitivity", (long)m_sensitivity);
1762 if (!m_spaceAttachments)
1763 clause->AddAttributeValue("space_attachments", (long)m_spaceAttachments);
1764 if (m_fixedWidth)
1765 clause->AddAttributeValue("fixed_width", (long)m_fixedWidth);
1766 if (m_fixedHeight)
1767 clause->AddAttributeValue("fixed_height", (long)m_fixedHeight);
1768 if (m_shadowMode != SHADOW_NONE)
1769 clause->AddAttributeValue("shadow_mode", (long)m_shadowMode);
1770 if (m_centreResize != TRUE)
1771 clause->AddAttributeValue("centre_resize", (long)0);
1772 clause->AddAttributeValue("maintain_aspect_ratio", (long) m_maintainAspectRatio);
1773 if (m_highlighted != FALSE)
1774 clause->AddAttributeValue("hilite", (long)m_highlighted);
1775
1776 if (m_parent) // For composite objects
1777 clause->AddAttributeValue("parent", (long)m_parent->GetId());
1778
1779 if (m_rotation != 0.0)
1780 clause->AddAttributeValue("rotation", m_rotation);
1781
1782 if (!this->IsKindOf(CLASSINFO(wxLineShape)))
1783 {
1784 clause->AddAttributeValue("neck_length", (long) m_branchNeckLength);
1785 clause->AddAttributeValue("stem_length", (long) m_branchStemLength);
1786 clause->AddAttributeValue("branch_spacing", (long) m_branchSpacing);
1787 clause->AddAttributeValue("branch_style", (long) m_branchStyle);
1788 }
1789
1790 // Write user-defined attachment points, if any
1791 if (m_attachmentPoints.Number() > 0)
1792 {
1793 wxExpr *attachmentList = new wxExpr(wxExprList);
1794 wxNode *node = m_attachmentPoints.First();
1795 while (node)
1796 {
1797 wxAttachmentPoint *point = (wxAttachmentPoint *)node->Data();
1798 wxExpr *pointExpr = new wxExpr(wxExprList);
1799 pointExpr->Append(new wxExpr((long)point->m_id));
1800 pointExpr->Append(new wxExpr(point->m_x));
1801 pointExpr->Append(new wxExpr(point->m_y));
1802 attachmentList->Append(pointExpr);
1803 node = node->Next();
1804 }
1805 clause->AddAttributeValue("user_attachments", attachmentList);
1806 }
1807
1808 // Write text regions
1809 WriteRegions(clause);
1810 }
1811
1812 void wxShape::WriteRegions(wxExpr *clause)
1813 {
1814 // Output regions as region1 = (...), region2 = (...), etc
1815 // and formatted text as text1 = (...), text2 = (...) etc.
1816 int regionNo = 1;
1817 char regionNameBuf[20];
1818 char textNameBuf[20];
1819 wxNode *node = m_regions.First();
1820 while (node)
1821 {
1822 wxShapeRegion *region = (wxShapeRegion *)node->Data();
1823 sprintf(regionNameBuf, "region%d", regionNo);
1824 sprintf(textNameBuf, "text%d", regionNo);
1825
1826 // Original text and region attributes:
1827 // region1 = (regionName regionText x y width height minWidth minHeight proportionX proportionY
1828 // formatMode fontSize fontFamily fontStyle fontWeight textColour)
1829 wxExpr *regionExpr = new wxExpr(wxExprList);
1830 regionExpr->Append(new wxExpr(wxExprString, (region->m_regionName ? region->m_regionName : "")));
1831 regionExpr->Append(new wxExpr(wxExprString, (region->m_regionText ? region->m_regionText : "")));
1832
1833 regionExpr->Append(new wxExpr(region->m_x));
1834 regionExpr->Append(new wxExpr(region->m_y));
1835 regionExpr->Append(new wxExpr(region->GetWidth()));
1836 regionExpr->Append(new wxExpr(region->GetHeight()));
1837
1838 regionExpr->Append(new wxExpr(region->m_minWidth));
1839 regionExpr->Append(new wxExpr(region->m_minHeight));
1840 regionExpr->Append(new wxExpr(region->m_regionProportionX));
1841 regionExpr->Append(new wxExpr(region->m_regionProportionY));
1842
1843 regionExpr->Append(new wxExpr((long)region->m_formatMode));
1844
1845 regionExpr->Append(new wxExpr((long)(region->m_font ? region->m_font->GetPointSize() : 10)));
1846 regionExpr->Append(new wxExpr((long)(region->m_font ? region->m_font->GetFamily() : wxDEFAULT)));
1847 regionExpr->Append(new wxExpr((long)(region->m_font ? region->m_font->GetStyle() : wxDEFAULT)));
1848 regionExpr->Append(new wxExpr((long)(region->m_font ? region->m_font->GetWeight() : wxNORMAL)));
1849 regionExpr->Append(new wxExpr(wxExprString, region->m_textColour ? region->m_textColour : "BLACK"));
1850
1851 // New members for pen colour/style
1852 regionExpr->Append(new wxExpr(wxExprString, region->m_penColour ? region->m_penColour : "BLACK"));
1853 regionExpr->Append(new wxExpr((long)region->m_penStyle));
1854
1855 // Formatted text:
1856 // text1 = ((x y string) (x y string) ...)
1857 wxExpr *textExpr = new wxExpr(wxExprList);
1858
1859 wxNode *textNode = region->m_formattedText.First();
1860 while (textNode)
1861 {
1862 wxShapeTextLine *line = (wxShapeTextLine *)textNode->Data();
1863 wxExpr *list2 = new wxExpr(wxExprList);
1864 list2->Append(new wxExpr(line->GetX()));
1865 list2->Append(new wxExpr(line->GetY()));
1866 list2->Append(new wxExpr(wxExprString, line->GetText()));
1867 textExpr->Append(list2);
1868 textNode = textNode->Next();
1869 }
1870
1871 // Now add both attributes to the clause
1872 clause->AddAttributeValue(regionNameBuf, regionExpr);
1873 clause->AddAttributeValue(textNameBuf, textExpr);
1874
1875 node = node->Next();
1876 regionNo ++;
1877 }
1878 }
1879
1880 void wxShape::ReadAttributes(wxExpr *clause)
1881 {
1882 clause->GetAttributeValue("id", m_id);
1883 RegisterId(m_id);
1884
1885 clause->GetAttributeValue("x", m_xpos);
1886 clause->GetAttributeValue("y", m_ypos);
1887
1888 // Input text strings (FOR COMPATIBILITY WITH OLD FILES ONLY. SEE REGION CODE BELOW.)
1889 ClearText();
1890 wxExpr *strings = clause->AttributeValue("text");
1891 if (strings && strings->Type() == wxExprList)
1892 {
1893 m_formatted = TRUE; // Assume text is formatted unless we prove otherwise
1894 wxExpr *node = strings->value.first;
1895 while (node)
1896 {
1897 wxExpr *string_expr = node;
1898 double the_x = 0.0;
1899 double the_y = 0.0;
1900 wxString the_string("");
1901
1902 // string_expr can either be a string, or a list of
1903 // 3 elements: x, y, and string.
1904 if (string_expr->Type() == wxExprString)
1905 {
1906 the_string = string_expr->StringValue();
1907 m_formatted = FALSE;
1908 }
1909 else if (string_expr->Type() == wxExprList)
1910 {
1911 wxExpr *first = string_expr->value.first;
1912 wxExpr *second = first ? first->next : NULL;
1913 wxExpr *third = second ? second->next : NULL;
1914
1915 if (first && second && third &&
1916 (first->Type() == wxExprReal || first->Type() == wxExprInteger) &&
1917 (second->Type() == wxExprReal || second->Type() == wxExprInteger) &&
1918 third->Type() == wxExprString)
1919 {
1920 if (first->Type() == wxExprReal)
1921 the_x = first->RealValue();
1922 else the_x = (double)first->IntegerValue();
1923
1924 if (second->Type() == wxExprReal)
1925 the_y = second->RealValue();
1926 else the_y = (double)second->IntegerValue();
1927
1928 the_string = third->StringValue();
1929 }
1930 }
1931 wxShapeTextLine *line =
1932 new wxShapeTextLine(the_x, the_y, (char*) (const char*) the_string);
1933 m_text.Append(line);
1934
1935 node = node->next;
1936 }
1937 }
1938
1939 wxString pen_string = "";
1940 wxString brush_string = "";
1941 int pen_width = 1;
1942 int pen_style = wxSOLID;
1943 int brush_style = wxSOLID;
1944 m_attachmentMode = ATTACHMENT_MODE_NONE;
1945
1946 clause->GetAttributeValue("pen_colour", pen_string);
1947 clause->GetAttributeValue("text_colour", m_textColourName);
1948
1949 SetTextColour(m_textColourName);
1950
1951 clause->GetAttributeValue("region_name", m_regionName);
1952
1953 clause->GetAttributeValue("brush_colour", brush_string);
1954 clause->GetAttributeValue("pen_width", pen_width);
1955 clause->GetAttributeValue("pen_style", pen_style);
1956 clause->GetAttributeValue("brush_style", brush_style);
1957
1958 int iVal = (int) m_attachmentMode;
1959 clause->GetAttributeValue("use_attachments", iVal);
1960 m_attachmentMode = iVal;
1961
1962 clause->GetAttributeValue("sensitivity", m_sensitivity);
1963
1964 iVal = (int) m_spaceAttachments;
1965 clause->GetAttributeValue("space_attachments", iVal);
1966 m_spaceAttachments = (iVal != 0);
1967
1968 iVal = (int) m_fixedWidth;
1969 clause->GetAttributeValue("fixed_width", iVal);
1970 m_fixedWidth = (iVal != 0);
1971
1972 iVal = (int) m_fixedHeight;
1973 clause->GetAttributeValue("fixed_height", iVal);
1974 m_fixedHeight = (iVal != 0);
1975
1976 clause->GetAttributeValue("format_mode", m_formatMode);
1977 clause->GetAttributeValue("shadow_mode", m_shadowMode);
1978
1979 iVal = m_branchNeckLength;
1980 clause->GetAttributeValue("neck_length", iVal);
1981 m_branchNeckLength = iVal;
1982
1983 iVal = m_branchStemLength;
1984 clause->GetAttributeValue("stem_length", iVal);
1985 m_branchStemLength = iVal;
1986
1987 iVal = m_branchSpacing;
1988 clause->GetAttributeValue("branch_spacing", iVal);
1989 m_branchSpacing = iVal;
1990
1991 clause->GetAttributeValue("branch_style", m_branchStyle);
1992
1993 iVal = (int) m_centreResize;
1994 clause->GetAttributeValue("centre_resize", iVal);
1995 m_centreResize = (iVal != 0);
1996
1997 iVal = (int) m_maintainAspectRatio;
1998 clause->GetAttributeValue("maintain_aspect_ratio", iVal);
1999 m_maintainAspectRatio = (iVal != 0);
2000
2001 iVal = (int) m_highlighted;
2002 clause->GetAttributeValue("hilite", iVal);
2003 m_highlighted = (iVal != 0);
2004
2005 clause->GetAttributeValue("rotation", m_rotation);
2006
2007 if (pen_string == "")
2008 pen_string = "BLACK";
2009 if (brush_string == "")
2010 brush_string = "WHITE";
2011
2012 if (pen_string[0] == '#')
2013 {
2014 wxColour col(oglHexToColour(pen_string.After('#')));
2015 m_pen = wxThePenList->FindOrCreatePen(col, pen_width, pen_style);
2016 }
2017 else
2018 m_pen = wxThePenList->FindOrCreatePen(pen_string, pen_width, pen_style);
2019
2020 if (!m_pen)
2021 m_pen = wxBLACK_PEN;
2022
2023 if (brush_string[0] == '#')
2024 {
2025 wxColour col(oglHexToColour(brush_string.After('#')));
2026 m_brush = wxTheBrushList->FindOrCreateBrush(col, brush_style);
2027 }
2028 else
2029 m_brush = wxTheBrushList->FindOrCreateBrush(brush_string, brush_style);
2030
2031 if (!m_brush)
2032 m_brush = wxWHITE_BRUSH;
2033
2034 int point_size = 10;
2035 clause->GetAttributeValue("point_size", point_size);
2036 SetFont(oglMatchFont(point_size));
2037
2038 // Read user-defined attachment points, if any
2039 wxExpr *attachmentList = clause->AttributeValue("user_attachments");
2040 if (attachmentList)
2041 {
2042 wxExpr *pointExpr = attachmentList->GetFirst();
2043 while (pointExpr)
2044 {
2045 wxExpr *idExpr = pointExpr->Nth(0);
2046 wxExpr *xExpr = pointExpr->Nth(1);
2047 wxExpr *yExpr = pointExpr->Nth(2);
2048 if (idExpr && xExpr && yExpr)
2049 {
2050 wxAttachmentPoint *point = new wxAttachmentPoint;
2051 point->m_id = (int)idExpr->IntegerValue();
2052 point->m_x = xExpr->RealValue();
2053 point->m_y = yExpr->RealValue();
2054 m_attachmentPoints.Append((wxObject *)point);
2055 }
2056 pointExpr = pointExpr->GetNext();
2057 }
2058 }
2059
2060 // Read text regions
2061 ReadRegions(clause);
2062 }
2063
2064 void wxShape::ReadRegions(wxExpr *clause)
2065 {
2066 ClearRegions();
2067
2068 // region1 = (regionName regionText x y width height minWidth minHeight proportionX proportionY
2069 // formatMode fontSize fontFamily fontStyle fontWeight textColour)
2070 int regionNo = 1;
2071 char regionNameBuf[20];
2072 char textNameBuf[20];
2073
2074 wxExpr *regionExpr = NULL;
2075 wxExpr *textExpr = NULL;
2076 sprintf(regionNameBuf, "region%d", regionNo);
2077 sprintf(textNameBuf, "text%d", regionNo);
2078
2079 m_formatted = TRUE; // Assume text is formatted unless we prove otherwise
2080
2081 while (regionExpr = clause->AttributeValue(regionNameBuf))
2082 {
2083 /*
2084 * Get the region information
2085 *
2086 */
2087
2088 wxString regionName("");
2089 wxString regionText("");
2090 double x = 0.0;
2091 double y = 0.0;
2092 double width = 0.0;
2093 double height = 0.0;
2094 double minWidth = 5.0;
2095 double minHeight = 5.0;
2096 double m_regionProportionX = -1.0;
2097 double m_regionProportionY = -1.0;
2098 int formatMode = FORMAT_NONE;
2099 int fontSize = 10;
2100 int fontFamily = wxSWISS;
2101 int fontStyle = wxNORMAL;
2102 int fontWeight = wxNORMAL;
2103 wxString regionTextColour("");
2104 wxString penColour("");
2105 int penStyle = wxSOLID;
2106
2107 if (regionExpr->Type() == wxExprList)
2108 {
2109 wxExpr *nameExpr = regionExpr->Nth(0);
2110 wxExpr *textExpr = regionExpr->Nth(1);
2111 wxExpr *xExpr = regionExpr->Nth(2);
2112 wxExpr *yExpr = regionExpr->Nth(3);
2113 wxExpr *widthExpr = regionExpr->Nth(4);
2114 wxExpr *heightExpr = regionExpr->Nth(5);
2115 wxExpr *minWidthExpr = regionExpr->Nth(6);
2116 wxExpr *minHeightExpr = regionExpr->Nth(7);
2117 wxExpr *propXExpr = regionExpr->Nth(8);
2118 wxExpr *propYExpr = regionExpr->Nth(9);
2119 wxExpr *formatExpr = regionExpr->Nth(10);
2120 wxExpr *sizeExpr = regionExpr->Nth(11);
2121 wxExpr *familyExpr = regionExpr->Nth(12);
2122 wxExpr *styleExpr = regionExpr->Nth(13);
2123 wxExpr *weightExpr = regionExpr->Nth(14);
2124 wxExpr *colourExpr = regionExpr->Nth(15);
2125 wxExpr *penColourExpr = regionExpr->Nth(16);
2126 wxExpr *penStyleExpr = regionExpr->Nth(17);
2127
2128 regionName = nameExpr->StringValue();
2129 regionText = textExpr->StringValue();
2130
2131 x = xExpr->RealValue();
2132 y = yExpr->RealValue();
2133
2134 width = widthExpr->RealValue();
2135 height = heightExpr->RealValue();
2136
2137 minWidth = minWidthExpr->RealValue();
2138 minHeight = minHeightExpr->RealValue();
2139
2140 m_regionProportionX = propXExpr->RealValue();
2141 m_regionProportionY = propYExpr->RealValue();
2142
2143 formatMode = (int) formatExpr->IntegerValue();
2144 fontSize = (int)sizeExpr->IntegerValue();
2145 fontFamily = (int)familyExpr->IntegerValue();
2146 fontStyle = (int)styleExpr->IntegerValue();
2147 fontWeight = (int)weightExpr->IntegerValue();
2148
2149 if (colourExpr)
2150 {
2151 regionTextColour = colourExpr->StringValue();
2152 }
2153 else
2154 regionTextColour = "BLACK";
2155
2156 if (penColourExpr)
2157 penColour = penColourExpr->StringValue();
2158 if (penStyleExpr)
2159 penStyle = (int)penStyleExpr->IntegerValue();
2160 }
2161 wxFont *font = wxTheFontList->FindOrCreateFont(fontSize, fontFamily, fontStyle, fontWeight);
2162
2163 wxShapeRegion *region = new wxShapeRegion;
2164 region->SetProportions(m_regionProportionX, m_regionProportionY);
2165 region->SetFont(font);
2166 region->SetSize(width, height);
2167 region->SetPosition(x, y);
2168 region->SetMinSize(minWidth, minHeight);
2169 region->SetFormatMode(formatMode);
2170 region->SetPenStyle(penStyle);
2171 if (penColour != "")
2172 region->SetPenColour(penColour);
2173
2174 region->m_textColour = regionTextColour;
2175 region->m_regionText = regionText;
2176 region->m_regionName = regionName;
2177
2178 m_regions.Append(region);
2179
2180 /*
2181 * Get the formatted text strings
2182 *
2183 */
2184 textExpr = clause->AttributeValue(textNameBuf);
2185 if (textExpr && (textExpr->Type() == wxExprList))
2186 {
2187 wxExpr *node = textExpr->value.first;
2188 while (node)
2189 {
2190 wxExpr *string_expr = node;
2191 double the_x = 0.0;
2192 double the_y = 0.0;
2193 wxString the_string("");
2194
2195 // string_expr can either be a string, or a list of
2196 // 3 elements: x, y, and string.
2197 if (string_expr->Type() == wxExprString)
2198 {
2199 the_string = string_expr->StringValue();
2200 m_formatted = FALSE;
2201 }
2202 else if (string_expr->Type() == wxExprList)
2203 {
2204 wxExpr *first = string_expr->value.first;
2205 wxExpr *second = first ? first->next : NULL;
2206 wxExpr *third = second ? second->next : NULL;
2207
2208 if (first && second && third &&
2209 (first->Type() == wxExprReal || first->Type() == wxExprInteger) &&
2210 (second->Type() == wxExprReal || second->Type() == wxExprInteger) &&
2211 third->Type() == wxExprString)
2212 {
2213 if (first->Type() == wxExprReal)
2214 the_x = first->RealValue();
2215 else the_x = (double)first->IntegerValue();
2216
2217 if (second->Type() == wxExprReal)
2218 the_y = second->RealValue();
2219 else the_y = (double)second->IntegerValue();
2220
2221 the_string = third->StringValue();
2222 }
2223 }
2224 if (the_string)
2225 {
2226 wxShapeTextLine *line =
2227 new wxShapeTextLine(the_x, the_y, (char*) (const char*) the_string);
2228 region->m_formattedText.Append(line);
2229 }
2230 node = node->next;
2231 }
2232 }
2233
2234 regionNo ++;
2235 sprintf(regionNameBuf, "region%d", regionNo);
2236 sprintf(textNameBuf, "text%d", regionNo);
2237 }
2238
2239 // Compatibility: check for no regions (old file).
2240 // Lines and divided rectangles must deal with this compatibility
2241 // theirselves. Composites _may_ not have any regions anyway.
2242 if ((m_regions.Number() == 0) &&
2243 !this->IsKindOf(CLASSINFO(wxLineShape)) && !this->IsKindOf(CLASSINFO(wxDividedShape)) &&
2244 !this->IsKindOf(CLASSINFO(wxCompositeShape)))
2245 {
2246 wxShapeRegion *newRegion = new wxShapeRegion;
2247 newRegion->SetName("0");
2248 m_regions.Append((wxObject *)newRegion);
2249 if (m_text.Number() > 0)
2250 {
2251 newRegion->ClearText();
2252 wxNode *node = m_text.First();
2253 while (node)
2254 {
2255 wxShapeTextLine *textLine = (wxShapeTextLine *)node->Data();
2256 wxNode *next = node->Next();
2257 newRegion->GetFormattedText().Append((wxObject *)textLine);
2258 delete node;
2259 node = next;
2260 }
2261 }
2262 }
2263 }
2264
2265 #endif
2266
2267 void wxShape::Copy(wxShape& copy)
2268 {
2269 copy.m_id = m_id;
2270 copy.m_xpos = m_xpos;
2271 copy.m_ypos = m_ypos;
2272 copy.m_pen = m_pen;
2273 copy.m_brush = m_brush;
2274 copy.m_textColour = m_textColour;
2275 copy.m_centreResize = m_centreResize;
2276 copy.m_maintainAspectRatio = m_maintainAspectRatio;
2277 copy.m_attachmentMode = m_attachmentMode;
2278 copy.m_spaceAttachments = m_spaceAttachments;
2279 copy.m_highlighted = m_highlighted;
2280 copy.m_rotation = m_rotation;
2281 copy.m_textColourName = m_textColourName;
2282 copy.m_regionName = m_regionName;
2283
2284 copy.m_sensitivity = m_sensitivity;
2285 copy.m_draggable = m_draggable;
2286 copy.m_fixedWidth = m_fixedWidth;
2287 copy.m_fixedHeight = m_fixedHeight;
2288 copy.m_formatMode = m_formatMode;
2289 copy.m_drawHandles = m_drawHandles;
2290
2291 copy.m_visible = m_visible;
2292 copy.m_shadowMode = m_shadowMode;
2293 copy.m_shadowOffsetX = m_shadowOffsetX;
2294 copy.m_shadowOffsetY = m_shadowOffsetY;
2295 copy.m_shadowBrush = m_shadowBrush;
2296
2297 copy.m_branchNeckLength = m_branchNeckLength;
2298 copy.m_branchStemLength = m_branchStemLength;
2299 copy.m_branchSpacing = m_branchSpacing;
2300
2301 // Copy text regions
2302 copy.ClearRegions();
2303 wxNode *node = m_regions.First();
2304 while (node)
2305 {
2306 wxShapeRegion *region = (wxShapeRegion *)node->Data();
2307 wxShapeRegion *newRegion = new wxShapeRegion(*region);
2308 copy.m_regions.Append(newRegion);
2309 node = node->Next();
2310 }
2311
2312 // Copy attachments
2313 copy.ClearAttachments();
2314 node = m_attachmentPoints.First();
2315 while (node)
2316 {
2317 wxAttachmentPoint *point = (wxAttachmentPoint *)node->Data();
2318 wxAttachmentPoint *newPoint = new wxAttachmentPoint;
2319 newPoint->m_id = point->m_id;
2320 newPoint->m_x = point->m_x;
2321 newPoint->m_y = point->m_y;
2322 copy.m_attachmentPoints.Append((wxObject *)newPoint);
2323 node = node->Next();
2324 }
2325
2326 // Copy lines
2327 copy.m_lines.Clear();
2328 node = m_lines.First();
2329 while (node)
2330 {
2331 wxLineShape* line = (wxLineShape*) node->Data();
2332 copy.m_lines.Append(line);
2333 node = node->Next();
2334 }
2335 }
2336
2337 // Create and return a new, fully copied object.
2338 wxShape *wxShape::CreateNewCopy(bool resetMapping, bool recompute)
2339 {
2340 if (resetMapping)
2341 oglObjectCopyMapping.Clear();
2342
2343 wxShape* newObject = (wxShape*) GetClassInfo()->CreateObject();
2344
2345 wxASSERT( (newObject != NULL) );
2346 wxASSERT( (newObject->IsKindOf(CLASSINFO(wxShape))) );
2347
2348 Copy(*newObject);
2349
2350 if (GetEventHandler() != this)
2351 {
2352 wxShapeEvtHandler* newHandler = GetEventHandler()->CreateNewCopy();
2353 newObject->SetEventHandler(newHandler);
2354 newObject->SetPreviousHandler(NULL);
2355 newHandler->SetPreviousHandler(newObject);
2356 newHandler->SetShape(newObject);
2357 }
2358
2359 if (recompute)
2360 newObject->Recompute();
2361 return newObject;
2362 }
2363
2364 // Does the copying for this object, including copying event
2365 // handler data if any. Calls the virtual Copy function.
2366 void wxShape::CopyWithHandler(wxShape& copy)
2367 {
2368 Copy(copy);
2369
2370 if (GetEventHandler() != this)
2371 {
2372 wxASSERT( copy.GetEventHandler() != NULL );
2373 wxASSERT( copy.GetEventHandler() != (&copy) );
2374 wxASSERT( GetEventHandler()->GetClassInfo() == copy.GetEventHandler()->GetClassInfo() );
2375 GetEventHandler()->CopyData(* (copy.GetEventHandler()));
2376 }
2377 }
2378
2379
2380 // Default - make 6 control points
2381 void wxShape::MakeControlPoints()
2382 {
2383 double maxX, maxY, minX, minY;
2384
2385 GetBoundingBoxMax(&maxX, &maxY);
2386 GetBoundingBoxMin(&minX, &minY);
2387
2388 double widthMin = (double)(minX + CONTROL_POINT_SIZE + 2);
2389 double heightMin = (double)(minY + CONTROL_POINT_SIZE + 2);
2390
2391 // Offsets from main object
2392 double top = (double)(- (heightMin / 2.0));
2393 double bottom = (double)(heightMin / 2.0 + (maxY - minY));
2394 double left = (double)(- (widthMin / 2.0));
2395 double right = (double)(widthMin / 2.0 + (maxX - minX));
2396
2397 wxControlPoint *control = new wxControlPoint(m_canvas, this, CONTROL_POINT_SIZE, left, top,
2398 CONTROL_POINT_DIAGONAL);
2399 m_canvas->AddShape(control);
2400 m_controlPoints.Append(control);
2401
2402 control = new wxControlPoint(m_canvas, this, CONTROL_POINT_SIZE, 0, top,
2403 CONTROL_POINT_VERTICAL);
2404 m_canvas->AddShape(control);
2405 m_controlPoints.Append(control);
2406
2407 control = new wxControlPoint(m_canvas, this, CONTROL_POINT_SIZE, right, top,
2408 CONTROL_POINT_DIAGONAL);
2409 m_canvas->AddShape(control);
2410 m_controlPoints.Append(control);
2411
2412 control = new wxControlPoint(m_canvas, this, CONTROL_POINT_SIZE, right, 0,
2413 CONTROL_POINT_HORIZONTAL);
2414 m_canvas->AddShape(control);
2415 m_controlPoints.Append(control);
2416
2417 control = new wxControlPoint(m_canvas, this, CONTROL_POINT_SIZE, right, bottom,
2418 CONTROL_POINT_DIAGONAL);
2419 m_canvas->AddShape(control);
2420 m_controlPoints.Append(control);
2421
2422 control = new wxControlPoint(m_canvas, this, CONTROL_POINT_SIZE, 0, bottom,
2423 CONTROL_POINT_VERTICAL);
2424 m_canvas->AddShape(control);
2425 m_controlPoints.Append(control);
2426
2427 control = new wxControlPoint(m_canvas, this, CONTROL_POINT_SIZE, left, bottom,
2428 CONTROL_POINT_DIAGONAL);
2429 m_canvas->AddShape(control);
2430 m_controlPoints.Append(control);
2431
2432 control = new wxControlPoint(m_canvas, this, CONTROL_POINT_SIZE, left, 0,
2433 CONTROL_POINT_HORIZONTAL);
2434 m_canvas->AddShape(control);
2435 m_controlPoints.Append(control);
2436
2437 }
2438
2439 void wxShape::MakeMandatoryControlPoints()
2440 {
2441 wxNode *node = m_children.First();
2442 while (node)
2443 {
2444 wxShape *child = (wxShape *)node->Data();
2445 child->MakeMandatoryControlPoints();
2446 node = node->Next();
2447 }
2448 }
2449
2450 void wxShape::ResetMandatoryControlPoints()
2451 {
2452 wxNode *node = m_children.First();
2453 while (node)
2454 {
2455 wxShape *child = (wxShape *)node->Data();
2456 child->ResetMandatoryControlPoints();
2457 node = node->Next();
2458 }
2459 }
2460
2461 void wxShape::ResetControlPoints()
2462 {
2463 ResetMandatoryControlPoints();
2464
2465 if (m_controlPoints.Number() < 1)
2466 return;
2467
2468 double maxX, maxY, minX, minY;
2469
2470 GetBoundingBoxMax(&maxX, &maxY);
2471 GetBoundingBoxMin(&minX, &minY);
2472
2473 double widthMin = (double)(minX + CONTROL_POINT_SIZE + 2);
2474 double heightMin = (double)(minY + CONTROL_POINT_SIZE + 2);
2475
2476 // Offsets from main object
2477 double top = (double)(- (heightMin / 2.0));
2478 double bottom = (double)(heightMin / 2.0 + (maxY - minY));
2479 double left = (double)(- (widthMin / 2.0));
2480 double right = (double)(widthMin / 2.0 + (maxX - minX));
2481
2482 wxNode *node = m_controlPoints.First();
2483 wxControlPoint *control = (wxControlPoint *)node->Data();
2484 control->m_xoffset = left; control->m_yoffset = top;
2485
2486 node = node->Next(); control = (wxControlPoint *)node->Data();
2487 control->m_xoffset = 0; control->m_yoffset = top;
2488
2489 node = node->Next(); control = (wxControlPoint *)node->Data();
2490 control->m_xoffset = right; control->m_yoffset = top;
2491
2492 node = node->Next(); control = (wxControlPoint *)node->Data();
2493 control->m_xoffset = right; control->m_yoffset = 0;
2494
2495 node = node->Next(); control = (wxControlPoint *)node->Data();
2496 control->m_xoffset = right; control->m_yoffset = bottom;
2497
2498 node = node->Next(); control = (wxControlPoint *)node->Data();
2499 control->m_xoffset = 0; control->m_yoffset = bottom;
2500
2501 node = node->Next(); control = (wxControlPoint *)node->Data();
2502 control->m_xoffset = left; control->m_yoffset = bottom;
2503
2504 node = node->Next(); control = (wxControlPoint *)node->Data();
2505 control->m_xoffset = left; control->m_yoffset = 0;
2506 }
2507
2508 void wxShape::DeleteControlPoints(wxDC *dc)
2509 {
2510 wxNode *node = m_controlPoints.First();
2511 while (node)
2512 {
2513 wxControlPoint *control = (wxControlPoint *)node->Data();
2514 if (dc)
2515 control->GetEventHandler()->OnErase(*dc);
2516 m_canvas->RemoveShape(control);
2517 delete control;
2518 delete node;
2519 node = m_controlPoints.First();
2520 }
2521 // Children of divisions are contained objects,
2522 // so stop here
2523 if (!IsKindOf(CLASSINFO(wxDivisionShape)))
2524 {
2525 node = m_children.First();
2526 while (node)
2527 {
2528 wxShape *child = (wxShape *)node->Data();
2529 child->DeleteControlPoints(dc);
2530 node = node->Next();
2531 }
2532 }
2533 }
2534
2535 void wxShape::OnDrawControlPoints(wxDC& dc)
2536 {
2537 if (!m_drawHandles)
2538 return;
2539
2540 dc.SetBrush(wxBLACK_BRUSH);
2541 dc.SetPen(wxBLACK_PEN);
2542
2543 wxNode *node = m_controlPoints.First();
2544 while (node)
2545 {
2546 wxControlPoint *control = (wxControlPoint *)node->Data();
2547 control->Draw(dc);
2548 node = node->Next();
2549 }
2550 // Children of divisions are contained objects,
2551 // so stop here.
2552 // This test bypasses the type facility for speed
2553 // (critical when drawing)
2554 if (!IsKindOf(CLASSINFO(wxDivisionShape)))
2555 {
2556 node = m_children.First();
2557 while (node)
2558 {
2559 wxShape *child = (wxShape *)node->Data();
2560 child->GetEventHandler()->OnDrawControlPoints(dc);
2561 node = node->Next();
2562 }
2563 }
2564 }
2565
2566 void wxShape::OnEraseControlPoints(wxDC& dc)
2567 {
2568 wxNode *node = m_controlPoints.First();
2569 while (node)
2570 {
2571 wxControlPoint *control = (wxControlPoint *)node->Data();
2572 control->Erase(dc);
2573 node = node->Next();
2574 }
2575 if (!IsKindOf(CLASSINFO(wxDivisionShape)))
2576 {
2577 node = m_children.First();
2578 while (node)
2579 {
2580 wxShape *child = (wxShape *)node->Data();
2581 child->GetEventHandler()->OnEraseControlPoints(dc);
2582 node = node->Next();
2583 }
2584 }
2585 }
2586
2587 void wxShape::Select(bool select, wxDC* dc)
2588 {
2589 m_selected = select;
2590 if (select)
2591 {
2592 MakeControlPoints();
2593 // Children of divisions are contained objects,
2594 // so stop here
2595 if (!IsKindOf(CLASSINFO(wxDivisionShape)))
2596 {
2597 wxNode *node = m_children.First();
2598 while (node)
2599 {
2600 wxShape *child = (wxShape *)node->Data();
2601 child->MakeMandatoryControlPoints();
2602 node = node->Next();
2603 }
2604 }
2605 if (dc)
2606 GetEventHandler()->OnDrawControlPoints(*dc);
2607 }
2608 if (!select)
2609 {
2610 DeleteControlPoints(dc);
2611 if (!IsKindOf(CLASSINFO(wxDivisionShape)))
2612 {
2613 wxNode *node = m_children.First();
2614 while (node)
2615 {
2616 wxShape *child = (wxShape *)node->Data();
2617 child->DeleteControlPoints(dc);
2618 node = node->Next();
2619 }
2620 }
2621 }
2622 }
2623
2624 bool wxShape::Selected() const
2625 {
2626 return m_selected;
2627 }
2628
2629 bool wxShape::AncestorSelected() const
2630 {
2631 if (m_selected) return TRUE;
2632 if (!GetParent())
2633 return FALSE;
2634 else
2635 return GetParent()->AncestorSelected();
2636 }
2637
2638 int wxShape::GetNumberOfAttachments() const
2639 {
2640 // Should return the MAXIMUM attachment point id here,
2641 // so higher-level functions can iterate through all attachments,
2642 // even if they're not contiguous.
2643 if (m_attachmentPoints.Number() == 0)
2644 return 4;
2645 else
2646 {
2647 int maxN = 3;
2648 wxNode *node = m_attachmentPoints.First();
2649 while (node)
2650 {
2651 wxAttachmentPoint *point = (wxAttachmentPoint *)node->Data();
2652 if (point->m_id > maxN)
2653 maxN = point->m_id;
2654 node = node->Next();
2655 }
2656 return maxN+1;;
2657 }
2658 }
2659
2660 bool wxShape::AttachmentIsValid(int attachment) const
2661 {
2662 if (m_attachmentPoints.Number() == 0)
2663 {
2664 return ((attachment >= 0) && (attachment < 4)) ;
2665 }
2666
2667 wxNode *node = m_attachmentPoints.First();
2668 while (node)
2669 {
2670 wxAttachmentPoint *point = (wxAttachmentPoint *)node->Data();
2671 if (point->m_id == attachment)
2672 return TRUE;
2673 node = node->Next();
2674 }
2675 return FALSE;
2676 }
2677
2678 bool wxShape::GetAttachmentPosition(int attachment, double *x, double *y,
2679 int nth, int no_arcs, wxLineShape *line)
2680 {
2681 if (m_attachmentMode == ATTACHMENT_MODE_NONE)
2682 {
2683 *x = m_xpos; *y = m_ypos;
2684 return TRUE;
2685 }
2686 else if (m_attachmentMode == ATTACHMENT_MODE_BRANCHING)
2687 {
2688 wxRealPoint pt, stemPt;
2689 GetBranchingAttachmentPoint(attachment, nth, pt, stemPt);
2690 *x = pt.x;
2691 *y = pt.y;
2692 return TRUE;
2693 }
2694 else if (m_attachmentMode == ATTACHMENT_MODE_EDGE)
2695 {
2696 if (m_attachmentPoints.Number() > 0)
2697 {
2698 wxNode *node = m_attachmentPoints.First();
2699 while (node)
2700 {
2701 wxAttachmentPoint *point = (wxAttachmentPoint *)node->Data();
2702 if (point->m_id == attachment)
2703 {
2704 *x = (double)(m_xpos + point->m_x);
2705 *y = (double)(m_ypos + point->m_y);
2706 return TRUE;
2707 }
2708 node = node->Next();
2709 }
2710 *x = m_xpos; *y = m_ypos;
2711 return FALSE;
2712 }
2713 else
2714 {
2715 // Assume is rectangular
2716 double w, h;
2717 GetBoundingBoxMax(&w, &h);
2718 double top = (double)(m_ypos + h/2.0);
2719 double bottom = (double)(m_ypos - h/2.0);
2720 double left = (double)(m_xpos - w/2.0);
2721 double right = (double)(m_xpos + w/2.0);
2722
2723 bool isEnd = (line && line->IsEnd(this));
2724
2725 int physicalAttachment = LogicalToPhysicalAttachment(attachment);
2726
2727 // Simplified code
2728 switch (physicalAttachment)
2729 {
2730 case 0:
2731 {
2732 wxRealPoint pt = CalcSimpleAttachment(wxRealPoint(left, bottom), wxRealPoint(right, bottom),
2733 nth, no_arcs, line);
2734
2735 *x = pt.x; *y = pt.y;
2736 break;
2737 }
2738 case 1:
2739 {
2740 wxRealPoint pt = CalcSimpleAttachment(wxRealPoint(right, bottom), wxRealPoint(right, top),
2741 nth, no_arcs, line);
2742
2743 *x = pt.x; *y = pt.y;
2744 break;
2745 }
2746 case 2:
2747 {
2748 wxRealPoint pt = CalcSimpleAttachment(wxRealPoint(left, top), wxRealPoint(right, top),
2749 nth, no_arcs, line);
2750
2751 *x = pt.x; *y = pt.y;
2752 break;
2753 }
2754 case 3:
2755 {
2756 wxRealPoint pt = CalcSimpleAttachment(wxRealPoint(left, bottom), wxRealPoint(left, top),
2757 nth, no_arcs, line);
2758
2759 *x = pt.x; *y = pt.y;
2760 break;
2761 }
2762 default:
2763 {
2764 return FALSE;
2765 break;
2766 }
2767 }
2768 return TRUE;
2769 }
2770 }
2771 return FALSE;
2772 }
2773
2774 void wxShape::GetBoundingBoxMax(double *w, double *h)
2775 {
2776 double ww, hh;
2777 GetBoundingBoxMin(&ww, &hh);
2778 if (m_shadowMode != SHADOW_NONE)
2779 {
2780 ww += m_shadowOffsetX;
2781 hh += m_shadowOffsetY;
2782 }
2783 *w = ww;
2784 *h = hh;
2785 }
2786
2787 // Returns TRUE if image is a descendant of this composite
2788 bool wxShape::HasDescendant(wxShape *image)
2789 {
2790 if (image == this)
2791 return TRUE;
2792 wxNode *node = m_children.First();
2793 while (node)
2794 {
2795 wxShape *child = (wxShape *)node->Data();
2796 bool ans = child->HasDescendant(image);
2797 if (ans)
2798 return TRUE;
2799 node = node->Next();
2800 }
2801 return FALSE;
2802 }
2803
2804 // Clears points from a list of wxRealPoints, and clears list
2805 void wxShape::ClearPointList(wxList& list)
2806 {
2807 wxNode* node = list.First();
2808 while (node)
2809 {
2810 wxRealPoint* pt = (wxRealPoint*) node->Data();
2811 delete pt;
2812
2813 node = node->Next();
2814 }
2815 list.Clear();
2816 }
2817
2818 // Assuming the attachment lies along a vertical or horizontal line,
2819 // calculate the position on that point.
2820 wxRealPoint wxShape::CalcSimpleAttachment(const wxRealPoint& pt1, const wxRealPoint& pt2,
2821 int nth, int noArcs, wxLineShape* line)
2822 {
2823 bool isEnd = (line && line->IsEnd(this));
2824
2825 // Are we horizontal or vertical?
2826 bool isHorizontal = (oglRoughlyEqual(pt1.y, pt2.y) == TRUE);
2827
2828 double x, y;
2829
2830 if (isHorizontal)
2831 {
2832 wxRealPoint firstPoint, secondPoint;
2833 if (pt1.x > pt2.x)
2834 {
2835 firstPoint = pt2;
2836 secondPoint = pt1;
2837 }
2838 else
2839 {
2840 firstPoint = pt1;
2841 secondPoint = pt2;
2842 }
2843
2844 if (m_spaceAttachments)
2845 {
2846 if (line && (line->GetAlignmentType(isEnd) == LINE_ALIGNMENT_TO_NEXT_HANDLE))
2847 {
2848 // Align line according to the next handle along
2849 wxRealPoint *point = line->GetNextControlPoint(this);
2850 if (point->x < firstPoint.x)
2851 x = firstPoint.x;
2852 else if (point->x > secondPoint.x)
2853 x = secondPoint.x;
2854 else
2855 x = point->x;
2856 }
2857 else
2858 x = firstPoint.x + (nth + 1)*(secondPoint.x - firstPoint.x)/(noArcs + 1);
2859 }
2860 else x = (secondPoint.x - firstPoint.x)/2.0; // Midpoint
2861
2862 y = pt1.y;
2863 }
2864 else
2865 {
2866 wxASSERT( oglRoughlyEqual(pt1.x, pt2.x) == TRUE );
2867
2868 wxRealPoint firstPoint, secondPoint;
2869 if (pt1.y > pt2.y)
2870 {
2871 firstPoint = pt2;
2872 secondPoint = pt1;
2873 }
2874 else
2875 {
2876 firstPoint = pt1;
2877 secondPoint = pt2;
2878 }
2879
2880 if (m_spaceAttachments)
2881 {
2882 if (line && (line->GetAlignmentType(isEnd) == LINE_ALIGNMENT_TO_NEXT_HANDLE))
2883 {
2884 // Align line according to the next handle along
2885 wxRealPoint *point = line->GetNextControlPoint(this);
2886 if (point->y < firstPoint.y)
2887 y = firstPoint.y;
2888 else if (point->y > secondPoint.y)
2889 y = secondPoint.y;
2890 else
2891 y = point->y;
2892 }
2893 else
2894 y = firstPoint.y + (nth + 1)*(secondPoint.y - firstPoint.y)/(noArcs + 1);
2895 }
2896 else y = (secondPoint.y - firstPoint.y)/2.0; // Midpoint
2897
2898 x = pt1.x;
2899 }
2900
2901 return wxRealPoint(x, y);
2902 }
2903
2904 // Return the zero-based position in m_lines of line.
2905 int wxShape::GetLinePosition(wxLineShape* line)
2906 {
2907 int i = 0;
2908 for (i = 0; i < m_lines.Number(); i++)
2909 if ((wxLineShape*) (m_lines.Nth(i)->Data()) == line)
2910 return i;
2911
2912 return 0;
2913 }
2914
2915 //
2916 // |________|
2917 // | <- root
2918 // | <- neck
2919 // shoulder1 ->---------<- shoulder2
2920 // | | | | |
2921 // <- branching attachment point N-1
2922
2923 // This function gets information about where branching connections go.
2924 // Returns FALSE if there are no lines at this attachment.
2925 bool wxShape::GetBranchingAttachmentInfo(int attachment, wxRealPoint& root, wxRealPoint& neck,
2926 wxRealPoint& shoulder1, wxRealPoint& shoulder2)
2927 {
2928 int physicalAttachment = LogicalToPhysicalAttachment(attachment);
2929
2930 // Number of lines at this attachment.
2931 int lineCount = GetAttachmentLineCount(attachment);
2932
2933 if (lineCount == 0)
2934 return FALSE;
2935
2936 int totalBranchLength = m_branchSpacing * (lineCount - 1);
2937
2938 root = GetBranchingAttachmentRoot(attachment);
2939
2940 // Assume that we have attachment points 0 to 3: top, right, bottom, left.
2941 switch (physicalAttachment)
2942 {
2943 case 0:
2944 {
2945 neck.x = GetX();
2946 neck.y = root.y - m_branchNeckLength;
2947
2948 shoulder1.x = root.x - (totalBranchLength/2.0) ;
2949 shoulder2.x = root.x + (totalBranchLength/2.0) ;
2950
2951 shoulder1.y = neck.y;
2952 shoulder2.y = neck.y;
2953 break;
2954 }
2955 case 1:
2956 {
2957 neck.x = root.x + m_branchNeckLength;
2958 neck.y = root.y;
2959
2960 shoulder1.x = neck.x ;
2961 shoulder2.x = neck.x ;
2962
2963 shoulder1.y = neck.y - (totalBranchLength/2.0) ;
2964 shoulder2.y = neck.y + (totalBranchLength/2.0) ;
2965 break;
2966 }
2967 case 2:
2968 {
2969 neck.x = GetX();
2970 neck.y = root.y + m_branchNeckLength;
2971
2972 shoulder1.x = root.x - (totalBranchLength/2.0) ;
2973 shoulder2.x = root.x + (totalBranchLength/2.0) ;
2974
2975 shoulder1.y = neck.y;
2976 shoulder2.y = neck.y;
2977 break;
2978 }
2979 case 3:
2980 {
2981 neck.x = root.x - m_branchNeckLength;
2982 neck.y = root.y ;
2983
2984 shoulder1.x = neck.x ;
2985 shoulder2.x = neck.x ;
2986
2987 shoulder1.y = neck.y - (totalBranchLength/2.0) ;
2988 shoulder2.y = neck.y + (totalBranchLength/2.0) ;
2989 break;
2990 }
2991 default:
2992 {
2993 wxFAIL_MSG( "Unrecognised attachment point in GetBranchingAttachmentInfo." );
2994 break;
2995 }
2996 }
2997 return TRUE;
2998 }
2999
3000 // n is the number of the adjoining line, from 0 to N-1 where N is the number of lines
3001 // at this attachment point.
3002 // Get the attachment point where the arc joins the stem, and also the point where the
3003 // the stem meets the shoulder.
3004 bool wxShape::GetBranchingAttachmentPoint(int attachment, int n, wxRealPoint& pt, wxRealPoint& stemPt)
3005 {
3006 int physicalAttachment = LogicalToPhysicalAttachment(attachment);
3007
3008 wxRealPoint root, neck, shoulder1, shoulder2;
3009 GetBranchingAttachmentInfo(attachment, root, neck, shoulder1, shoulder2);
3010
3011 // Assume that we have attachment points 0 to 3: top, right, bottom, left.
3012 switch (physicalAttachment)
3013 {
3014 case 0:
3015 {
3016 pt.y = neck.y - m_branchStemLength;
3017 pt.x = shoulder1.x + n*m_branchSpacing;
3018
3019 stemPt.x = pt.x;
3020 stemPt.y = neck.y;
3021 break;
3022 }
3023 case 2:
3024 {
3025 pt.y = neck.y + m_branchStemLength;
3026 pt.x = shoulder1.x + n*m_branchSpacing;
3027
3028 stemPt.x = pt.x;
3029 stemPt.y = neck.y;
3030 break;
3031 }
3032 case 1:
3033 {
3034 pt.x = neck.x + m_branchStemLength;
3035 pt.y = shoulder1.y + n*m_branchSpacing;
3036
3037 stemPt.x = neck.x;
3038 stemPt.y = pt.y;
3039 break;
3040 }
3041 case 3:
3042 {
3043 pt.x = neck.x - m_branchStemLength;
3044 pt.y = shoulder1.y + n*m_branchSpacing;
3045
3046 stemPt.x = neck.x;
3047 stemPt.y = pt.y;
3048 break;
3049 }
3050 default:
3051 {
3052 wxFAIL_MSG( "Unrecognised attachment point in GetBranchingAttachmentPoint." );
3053 break;
3054 }
3055 }
3056
3057 return TRUE;
3058 }
3059
3060 // Get the number of lines at this attachment position.
3061 int wxShape::GetAttachmentLineCount(int attachment) const
3062 {
3063 int count = 0;
3064 wxNode* node = m_lines.First();
3065 while (node)
3066 {
3067 wxLineShape* lineShape = (wxLineShape*) node->Data();
3068 if ((lineShape->GetFrom() == this) && (lineShape->GetAttachmentFrom() == attachment))
3069 count ++;
3070 else if ((lineShape->GetTo() == this) && (lineShape->GetAttachmentTo() == attachment))
3071 count ++;
3072
3073 node = node->Next();
3074 }
3075 return count;
3076 }
3077
3078 // This function gets the root point at the given attachment.
3079 wxRealPoint wxShape::GetBranchingAttachmentRoot(int attachment)
3080 {
3081 int physicalAttachment = LogicalToPhysicalAttachment(attachment);
3082
3083 wxRealPoint root;
3084
3085 double width, height;
3086 GetBoundingBoxMax(& width, & height);
3087
3088 // Assume that we have attachment points 0 to 3: top, right, bottom, left.
3089 switch (physicalAttachment)
3090 {
3091 case 0:
3092 {
3093 root.x = GetX() ;
3094 root.y = GetY() - height/2.0;
3095 break;
3096 }
3097 case 1:
3098 {
3099 root.x = GetX() + width/2.0;
3100 root.y = GetY() ;
3101 break;
3102 }
3103 case 2:
3104 {
3105 root.x = GetX() ;
3106 root.y = GetY() + height/2.0;
3107 break;
3108 }
3109 case 3:
3110 {
3111 root.x = GetX() - width/2.0;
3112 root.y = GetY() ;
3113 break;
3114 }
3115 default:
3116 {
3117 wxFAIL_MSG( "Unrecognised attachment point in GetBranchingAttachmentRoot." );
3118 break;
3119 }
3120 }
3121 return root;
3122 }
3123
3124 // Draw or erase the branches (not the actual arcs though)
3125 void wxShape::OnDrawBranches(wxDC& dc, int attachment, bool erase)
3126 {
3127 int count = GetAttachmentLineCount(attachment);
3128 if (count == 0)
3129 return;
3130
3131 wxRealPoint root, neck, shoulder1, shoulder2;
3132 GetBranchingAttachmentInfo(attachment, root, neck, shoulder1, shoulder2);
3133
3134 if (erase)
3135 {
3136 dc.SetPen(*wxWHITE_PEN);
3137 dc.SetBrush(*wxWHITE_BRUSH);
3138 }
3139 else
3140 {
3141 dc.SetPen(*wxBLACK_PEN);
3142 dc.SetBrush(*wxBLACK_BRUSH);
3143 }
3144
3145 // Draw neck
3146 dc.DrawLine((long) root.x, (long) root.y, (long) neck.x, (long) neck.y);
3147
3148 if (count > 1)
3149 {
3150 // Draw shoulder-to-shoulder line
3151 dc.DrawLine((long) shoulder1.x, (long) shoulder1.y, (long) shoulder2.x, (long) shoulder2.y);
3152 }
3153 // Draw all the little branches
3154 int i;
3155 for (i = 0; i < count; i++)
3156 {
3157 wxRealPoint pt, stemPt;
3158 GetBranchingAttachmentPoint(attachment, i, pt, stemPt);
3159 dc.DrawLine((long) stemPt.x, (long) stemPt.y, (long) pt.x, (long) pt.y);
3160
3161 if ((GetBranchStyle() & BRANCHING_ATTACHMENT_BLOB) && (count > 1))
3162 {
3163 long blobSize=6;
3164 // dc.DrawEllipse((long) (stemPt.x + 0.5 - (blobSize/2.0)), (long) (stemPt.y + 0.5 - (blobSize/2.0)), blobSize, blobSize);
3165 dc.DrawEllipse((long) (stemPt.x - (blobSize/2.0)), (long) (stemPt.y - (blobSize/2.0)), blobSize, blobSize);
3166 }
3167 }
3168 }
3169
3170 // Draw or erase the branches (not the actual arcs though)
3171 void wxShape::OnDrawBranches(wxDC& dc, bool erase)
3172 {
3173 if (m_attachmentMode != ATTACHMENT_MODE_BRANCHING)
3174 return;
3175
3176 int count = GetNumberOfAttachments();
3177 int i;
3178 for (i = 0; i < count; i++)
3179 OnDrawBranches(dc, i, erase);
3180 }
3181
3182 // Only get the attachment position at the _edge_ of the shape, ignoring
3183 // branching mode. This is used e.g. to indicate the edge of interest, not the point
3184 // on the attachment branch.
3185 bool wxShape::GetAttachmentPositionEdge(int attachment, double *x, double *y,
3186 int nth, int no_arcs, wxLineShape *line)
3187 {
3188 int oldMode = m_attachmentMode;
3189
3190 // Calculate as if to edge, not branch
3191 if (m_attachmentMode == ATTACHMENT_MODE_BRANCHING)
3192 m_attachmentMode = ATTACHMENT_MODE_EDGE;
3193 bool success = GetAttachmentPosition(attachment, x, y, nth, no_arcs, line);
3194 m_attachmentMode = oldMode;
3195
3196 return success;
3197 }
3198
3199 // Rotate the standard attachment point from physical (0 is always North)
3200 // to logical (0 -> 1 if rotated by 90 degrees)
3201 int wxShape::PhysicalToLogicalAttachment(int physicalAttachment) const
3202 {
3203 const double pi = 3.1415926535897932384626433832795 ;
3204 int i;
3205 if (oglRoughlyEqual(GetRotation(), 0.0))
3206 {
3207 i = physicalAttachment;
3208 }
3209 else if (oglRoughlyEqual(GetRotation(), (pi/2.0)))
3210 {
3211 i = physicalAttachment - 1;
3212 }
3213 else if (oglRoughlyEqual(GetRotation(), pi))
3214 {
3215 i = physicalAttachment - 2;
3216 }
3217 else if (oglRoughlyEqual(GetRotation(), (3.0*pi/2.0)))
3218 {
3219 i = physicalAttachment - 3;
3220 }
3221 else
3222 // Can't handle -- assume the same.
3223 return physicalAttachment;
3224
3225 if (i < 0)
3226 i += 4;
3227
3228 return i;
3229 }
3230
3231 // Rotate the standard attachment point from logical
3232 // to physical (0 is always North)
3233 int wxShape::LogicalToPhysicalAttachment(int logicalAttachment) const
3234 {
3235 const double pi = 3.1415926535897932384626433832795 ;
3236 int i;
3237 if (oglRoughlyEqual(GetRotation(), 0.0))
3238 {
3239 i = logicalAttachment;
3240 }
3241 else if (oglRoughlyEqual(GetRotation(), (pi/2.0)))
3242 {
3243 i = logicalAttachment + 1;
3244 }
3245 else if (oglRoughlyEqual(GetRotation(), pi))
3246 {
3247 i = logicalAttachment + 2;
3248 }
3249 else if (oglRoughlyEqual(GetRotation(), (3.0*pi/2.0)))
3250 {
3251 i = logicalAttachment + 3;
3252 }
3253 else
3254 // Can't handle -- assume the same.
3255 return logicalAttachment;
3256
3257 if (i > 3)
3258 i -= 4;
3259
3260 return i;
3261 }
3262
3263 void wxShape::Rotate(double WXUNUSED(x), double WXUNUSED(y), double theta)
3264 {
3265 const double pi = 3.1415926535897932384626433832795 ;
3266 m_rotation = theta;
3267 if (m_rotation < 0.0)
3268 {
3269 m_rotation += 2*pi;
3270 }
3271 else if (m_rotation > 2*pi)
3272 {
3273 m_rotation -= 2*pi;
3274 }
3275 }
3276