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