]> git.saurik.com Git - wxWidgets.git/blame_incremental - utils/ogl/src/lines.cpp
Change a Popup menu from using a callback to using events. Can't test
[wxWidgets.git] / utils / ogl / src / lines.cpp
... / ...
CommitLineData
1/////////////////////////////////////////////////////////////////////////////
2// Name: lines.cpp
3// Purpose: wxLineShape
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 "lines.h"
14#pragma implementation "linesp.h"
15#endif
16
17// For compilers that support precompilation, includes "wx.h".
18#include <wx/wxprec.h>
19
20#ifdef __BORLANDC__
21#pragma hdrstop
22#endif
23
24#ifndef WX_PRECOMP
25#include <wx/wx.h>
26#endif
27
28#include <wx/wxexpr.h>
29
30#if wxUSE_IOSTREAMH
31#include <iostream.h>
32#else
33#include <iostream>
34#endif
35
36#include <ctype.h>
37#include <math.h>
38
39#include "basic.h"
40#include "basicp.h"
41#include "lines.h"
42#include "linesp.h"
43#include "drawn.h"
44#include "misc.h"
45#include "canvas.h"
46
47// Line shape
48IMPLEMENT_DYNAMIC_CLASS(wxLineShape, wxShape)
49
50wxLineShape::wxLineShape()
51{
52 m_sensitivity = OP_CLICK_LEFT | OP_CLICK_RIGHT;
53 m_draggable = FALSE;
54 m_attachmentTo = 0;
55 m_attachmentFrom = 0;
56/*
57 m_actualTextWidth = 0.0;
58 m_actualTextHeight = 0.0;
59*/
60 m_from = NULL;
61 m_to = NULL;
62 m_erasing = FALSE;
63 m_arrowSpacing = 5.0; // For the moment, don't bother saving this to file.
64 m_ignoreArrowOffsets = FALSE;
65 m_isSpline = FALSE;
66 m_maintainStraightLines = FALSE;
67 m_alignmentStart = 0;
68 m_alignmentEnd = 0;
69
70 m_lineControlPoints = NULL;
71
72 // Clear any existing regions (created in an earlier constructor)
73 // and make the three line regions.
74 ClearRegions();
75 wxShapeRegion *newRegion = new wxShapeRegion;
76 newRegion->SetName("Middle");
77 newRegion->SetSize(150, 50);
78 m_regions.Append((wxObject *)newRegion);
79
80 newRegion = new wxShapeRegion;
81 newRegion->SetName("Start");
82 newRegion->SetSize(150, 50);
83 m_regions.Append((wxObject *)newRegion);
84
85 newRegion = new wxShapeRegion;
86 newRegion->SetName("End");
87 newRegion->SetSize(150, 50);
88 m_regions.Append((wxObject *)newRegion);
89
90 for (int i = 0; i < 3; i++)
91 m_labelObjects[i] = NULL;
92}
93
94wxLineShape::~wxLineShape()
95{
96 if (m_lineControlPoints)
97 {
98 ClearPointList(*m_lineControlPoints);
99 delete m_lineControlPoints;
100 }
101 for (int i = 0; i < 3; i++)
102 {
103 if (m_labelObjects[i])
104 {
105 m_labelObjects[i]->Select(FALSE);
106 m_labelObjects[i]->RemoveFromCanvas(m_canvas);
107 delete m_labelObjects[i];
108 m_labelObjects[i] = NULL;
109 }
110 }
111 ClearArrowsAtPosition(-1);
112}
113
114void wxLineShape::MakeLineControlPoints(int n)
115{
116 if (m_lineControlPoints)
117 {
118 ClearPointList(*m_lineControlPoints);
119 delete m_lineControlPoints;
120 }
121 m_lineControlPoints = new wxList;
122
123 int i = 0;
124 for (i = 0; i < n; i++)
125 {
126 wxRealPoint *point = new wxRealPoint(-999, -999);
127 m_lineControlPoints->Append((wxObject*) point);
128 }
129}
130
131wxNode *wxLineShape::InsertLineControlPoint(wxDC* dc)
132{
133 if (dc)
134 Erase(*dc);
135
136 wxNode *last = m_lineControlPoints->Last();
137 wxNode *second_last = last->Previous();
138 wxRealPoint *last_point = (wxRealPoint *)last->Data();
139 wxRealPoint *second_last_point = (wxRealPoint *)second_last->Data();
140
141 // Choose a point half way between the last and penultimate points
142 double line_x = ((last_point->x + second_last_point->x)/2);
143 double line_y = ((last_point->y + second_last_point->y)/2);
144
145 wxRealPoint *point = new wxRealPoint(line_x, line_y);
146 wxNode *node = m_lineControlPoints->Insert(last, (wxObject*) point);
147 return node;
148}
149
150bool wxLineShape::DeleteLineControlPoint()
151{
152 if (m_lineControlPoints->Number() < 3)
153 return FALSE;
154
155 wxNode *last = m_lineControlPoints->Last();
156 wxNode *second_last = last->Previous();
157
158 wxRealPoint *second_last_point = (wxRealPoint *)second_last->Data();
159 delete second_last_point;
160 delete second_last;
161
162 return TRUE;
163}
164
165void wxLineShape::Initialise()
166{
167 if (m_lineControlPoints)
168 {
169 // Just move the first and last control points
170 wxNode *first = m_lineControlPoints->First();
171 wxRealPoint *first_point = (wxRealPoint *)first->Data();
172
173 wxNode *last = m_lineControlPoints->Last();
174 wxRealPoint *last_point = (wxRealPoint *)last->Data();
175
176 // If any of the line points are at -999, we must
177 // initialize them by placing them half way between the first
178 // and the last.
179 wxNode *node = first->Next();
180 while (node)
181 {
182 wxRealPoint *point = (wxRealPoint *)node->Data();
183 if (point->x == -999)
184 {
185 double x1, y1, x2, y2;
186 if (first_point->x < last_point->x)
187 { x1 = first_point->x; x2 = last_point->x; }
188 else
189 { x2 = first_point->x; x1 = last_point->x; }
190
191 if (first_point->y < last_point->y)
192 { y1 = first_point->y; y2 = last_point->y; }
193 else
194 { y2 = first_point->y; y1 = last_point->y; }
195
196 point->x = ((x2 - x1)/2 + x1);
197 point->y = ((y2 - y1)/2 + y1);
198 }
199 node = node->Next();
200 }
201 }
202}
203
204// Format a text string according to the region size, adding
205// strings with positions to region text list
206void wxLineShape::FormatText(wxDC& dc, const wxString& s, int i)
207{
208 double w, h;
209 ClearText(i);
210
211 if (m_regions.Number() < 1)
212 return;
213 wxNode *node = m_regions.Nth(i);
214 if (!node)
215 return;
216
217 wxShapeRegion *region = (wxShapeRegion *)node->Data();
218 region->SetText(s);
219 dc.SetFont(* region->GetFont());
220
221 region->GetSize(&w, &h);
222 // Initialize the size if zero
223 if (((w == 0) || (h == 0)) && (strlen(s) > 0))
224 {
225 w = 100; h = 50;
226 region->SetSize(w, h);
227 }
228
229 wxStringList *string_list = oglFormatText(dc, s, (w-5), (h-5), region->GetFormatMode());
230 node = string_list->First();
231 while (node)
232 {
233 char *s = (char *)node->Data();
234 wxShapeTextLine *line = new wxShapeTextLine(0.0, 0.0, s);
235 region->GetFormattedText().Append((wxObject *)line);
236 node = node->Next();
237 }
238 delete string_list;
239 double actualW = w;
240 double actualH = h;
241 if (region->GetFormatMode() & FORMAT_SIZE_TO_CONTENTS)
242 {
243 oglGetCentredTextExtent(dc, &(region->GetFormattedText()), m_xpos, m_ypos, w, h, &actualW, &actualH);
244 if ((actualW != w ) || (actualH != h))
245 {
246 double xx, yy;
247 GetLabelPosition(i, &xx, &yy);
248 EraseRegion(dc, region, xx, yy);
249 if (m_labelObjects[i])
250 {
251 m_labelObjects[i]->Select(FALSE, &dc);
252 m_labelObjects[i]->Erase(dc);
253 m_labelObjects[i]->SetSize(actualW, actualH);
254 }
255
256 region->SetSize(actualW, actualH);
257
258 if (m_labelObjects[i])
259 {
260 m_labelObjects[i]->Select(TRUE, & dc);
261 m_labelObjects[i]->Draw(dc);
262 }
263 }
264 }
265 oglCentreText(dc, &(region->GetFormattedText()), m_xpos, m_ypos, actualW, actualH, region->GetFormatMode());
266 m_formatted = TRUE;
267}
268
269void wxLineShape::DrawRegion(wxDC& dc, wxShapeRegion *region, double x, double y)
270{
271 if (GetDisableLabel())
272 return;
273
274 double w, h;
275 double xx, yy;
276 region->GetSize(&w, &h);
277
278 // Get offset from x, y
279 region->GetPosition(&xx, &yy);
280
281 double xp = xx + x;
282 double yp = yy + y;
283
284 // First, clear a rectangle for the text IF there is any
285 if (region->GetFormattedText().Number() > 0)
286 {
287 dc.SetPen(* g_oglWhiteBackgroundPen);
288 dc.SetBrush(* g_oglWhiteBackgroundBrush);
289
290 // Now draw the text
291 if (region->GetFont()) dc.SetFont(* region->GetFont());
292
293 dc.DrawRectangle((long)(xp - w/2.0), (long)(yp - h/2.0), (long)w, (long)h);
294
295 if (m_pen) dc.SetPen(* m_pen);
296 dc.SetTextForeground(* region->GetActualColourObject());
297
298#ifdef __WXMSW__
299 dc.SetTextBackground(g_oglWhiteBackgroundBrush->GetColour());
300#endif
301
302 oglDrawFormattedText(dc, &(region->GetFormattedText()), xp, yp, w, h, region->GetFormatMode());
303 }
304}
305
306void wxLineShape::EraseRegion(wxDC& dc, wxShapeRegion *region, double x, double y)
307{
308 if (GetDisableLabel())
309 return;
310
311 double w, h;
312 double xx, yy;
313 region->GetSize(&w, &h);
314
315 // Get offset from x, y
316 region->GetPosition(&xx, &yy);
317
318 double xp = xx + x;
319 double yp = yy + y;
320
321 if (region->GetFormattedText().Number() > 0)
322 {
323 dc.SetPen(* g_oglWhiteBackgroundPen);
324 dc.SetBrush(* g_oglWhiteBackgroundBrush);
325
326 dc.DrawRectangle((long)(xp - w/2.0), (long)(yp - h/2.0), (long)w, (long)h);
327 }
328}
329
330// Get the reference point for a label. Region x and y
331// are offsets from this.
332// position is 0, 1, 2
333void wxLineShape::GetLabelPosition(int position, double *x, double *y)
334{
335 switch (position)
336 {
337 case 0:
338 {
339 // Want to take the middle section for the label
340 int n = m_lineControlPoints->Number();
341 int half_way = (int)(n/2);
342
343 // Find middle of this line
344 wxNode *node = m_lineControlPoints->Nth(half_way - 1);
345 wxRealPoint *point = (wxRealPoint *)node->Data();
346 wxNode *next_node = node->Next();
347 wxRealPoint *next_point = (wxRealPoint *)next_node->Data();
348
349 double dx = (next_point->x - point->x);
350 double dy = (next_point->y - point->y);
351 *x = (double)(point->x + dx/2.0);
352 *y = (double)(point->y + dy/2.0);
353 break;
354 }
355 case 1:
356 {
357 wxNode *node = m_lineControlPoints->First();
358 *x = ((wxRealPoint *)node->Data())->x;
359 *y = ((wxRealPoint *)node->Data())->y;
360 break;
361 }
362 case 2:
363 {
364 wxNode *node = m_lineControlPoints->Last();
365 *x = ((wxRealPoint *)node->Data())->x;
366 *y = ((wxRealPoint *)node->Data())->y;
367 break;
368 }
369 default:
370 break;
371 }
372}
373
374/*
375 * Find whether line is supposed to be vertical or horizontal and
376 * make it so.
377 *
378 */
379void GraphicsStraightenLine(wxRealPoint *point1, wxRealPoint *point2)
380{
381 double dx = point2->x - point1->x;
382 double dy = point2->y - point1->y;
383
384 if (dx == 0.0)
385 return;
386 else if (fabs(dy/dx) > 1.0)
387 {
388 point2->x = point1->x;
389 }
390 else point2->y = point1->y;
391}
392
393void wxLineShape::Straighten(wxDC *dc)
394{
395 if (!m_lineControlPoints || m_lineControlPoints->Number() < 3)
396 return;
397
398 if (dc)
399 Erase(* dc);
400
401 wxNode *first_point_node = m_lineControlPoints->First();
402 wxNode *last_point_node = m_lineControlPoints->Last();
403 wxNode *second_last_point_node = last_point_node->Previous();
404
405 wxRealPoint *last_point = (wxRealPoint *)last_point_node->Data();
406 wxRealPoint *second_last_point = (wxRealPoint *)second_last_point_node->Data();
407
408 GraphicsStraightenLine(last_point, second_last_point);
409
410 wxNode *node = first_point_node;
411 while (node && (node != second_last_point_node))
412 {
413 wxRealPoint *point = (wxRealPoint *)node->Data();
414 wxRealPoint *next_point = (wxRealPoint *)(node->Next()->Data());
415
416 GraphicsStraightenLine(point, next_point);
417 node = node->Next();
418 }
419
420 if (dc)
421 Draw(* dc);
422}
423
424
425void wxLineShape::Unlink()
426{
427 if (m_to)
428 m_to->GetLines().DeleteObject(this);
429 if (m_from)
430 m_from->GetLines().DeleteObject(this);
431 m_to = NULL;
432 m_from = NULL;
433}
434
435void wxLineShape::SetEnds(double x1, double y1, double x2, double y2)
436{
437 // Find centre point
438 wxNode *first_point_node = m_lineControlPoints->First();
439 wxNode *last_point_node = m_lineControlPoints->Last();
440 wxRealPoint *first_point = (wxRealPoint *)first_point_node->Data();
441 wxRealPoint *last_point = (wxRealPoint *)last_point_node->Data();
442
443 first_point->x = x1;
444 first_point->y = y1;
445 last_point->x = x2;
446 last_point->y = y2;
447
448 m_xpos = (double)((x1 + x2)/2.0);
449 m_ypos = (double)((y1 + y2)/2.0);
450}
451
452// Get absolute positions of ends
453void wxLineShape::GetEnds(double *x1, double *y1, double *x2, double *y2)
454{
455 wxNode *first_point_node = m_lineControlPoints->First();
456 wxNode *last_point_node = m_lineControlPoints->Last();
457 wxRealPoint *first_point = (wxRealPoint *)first_point_node->Data();
458 wxRealPoint *last_point = (wxRealPoint *)last_point_node->Data();
459
460 *x1 = first_point->x; *y1 = first_point->y;
461 *x2 = last_point->x; *y2 = last_point->y;
462}
463
464void wxLineShape::SetAttachments(int from_attach, int to_attach)
465{
466 m_attachmentFrom = from_attach;
467 m_attachmentTo = to_attach;
468}
469
470bool wxLineShape::HitTest(double x, double y, int *attachment, double *distance)
471{
472 if (!m_lineControlPoints)
473 return FALSE;
474
475 // Look at label regions in case mouse is over a label
476 bool inLabelRegion = FALSE;
477 for (int i = 0; i < 3; i ++)
478 {
479 wxNode *regionNode = m_regions.Nth(i);
480 if (regionNode)
481 {
482 wxShapeRegion *region = (wxShapeRegion *)regionNode->Data();
483 if (region->m_formattedText.Number() > 0)
484 {
485 double xp, yp, cx, cy, cw, ch;
486 GetLabelPosition(i, &xp, &yp);
487 // Offset region from default label position
488 region->GetPosition(&cx, &cy);
489 region->GetSize(&cw, &ch);
490 cx += xp;
491 cy += yp;
492 double rLeft = (double)(cx - (cw/2.0));
493 double rTop = (double)(cy - (ch/2.0));
494 double rRight = (double)(cx + (cw/2.0));
495 double rBottom = (double)(cy + (ch/2.0));
496 if (x > rLeft && x < rRight && y > rTop && y < rBottom)
497 {
498 inLabelRegion = TRUE;
499 i = 3;
500 }
501 }
502 }
503 }
504
505 wxNode *node = m_lineControlPoints->First();
506
507 while (node && node->Next())
508 {
509 wxRealPoint *point1 = (wxRealPoint *)node->Data();
510 wxRealPoint *point2 = (wxRealPoint *)node->Next()->Data();
511
512 // Allow for inaccurate mousing or vert/horiz lines
513 int extra = 4;
514 double left = wxMin(point1->x, point2->x) - extra;
515 double right = wxMax(point1->x, point2->x) + extra;
516
517 double bottom = wxMin(point1->y, point2->y) - extra;
518 double top = wxMax(point1->y, point2->y) + extra;
519
520 if ((x > left && x < right && y > bottom && y < top) || inLabelRegion)
521 {
522 // Work out distance from centre of line
523 double centre_x = (double)(left + (right - left)/2.0);
524 double centre_y = (double)(bottom + (top - bottom)/2.0);
525
526 *attachment = 0;
527 *distance = (double)sqrt((centre_x - x)*(centre_x - x) + (centre_y - y)*(centre_y - y));
528 return TRUE;
529 }
530
531 node = node->Next();
532 }
533 return FALSE;
534}
535
536void wxLineShape::DrawArrows(wxDC& dc)
537{
538 // Distance along line of each arrow: space them out evenly.
539 double startArrowPos = 0.0;
540 double endArrowPos = 0.0;
541 double middleArrowPos = 0.0;
542
543 wxNode *node = m_arcArrows.First();
544 while (node)
545 {
546 wxArrowHead *arrow = (wxArrowHead *)node->Data();
547 switch (arrow->GetArrowEnd())
548 {
549 case ARROW_POSITION_START:
550 {
551 if ((arrow->GetXOffset() != 0.0) && !m_ignoreArrowOffsets)
552 // If specified, x offset is proportional to line length
553 DrawArrow(dc, arrow, arrow->GetXOffset(), TRUE);
554 else
555 {
556 DrawArrow(dc, arrow, startArrowPos, FALSE); // Absolute distance
557 startArrowPos += arrow->GetSize() + arrow->GetSpacing();
558 }
559 break;
560 }
561 case ARROW_POSITION_END:
562 {
563 if ((arrow->GetXOffset() != 0.0) && !m_ignoreArrowOffsets)
564 DrawArrow(dc, arrow, arrow->GetXOffset(), TRUE);
565 else
566 {
567 DrawArrow(dc, arrow, endArrowPos, FALSE);
568 endArrowPos += arrow->GetSize() + arrow->GetSpacing();
569 }
570 break;
571 }
572 case ARROW_POSITION_MIDDLE:
573 {
574 arrow->SetXOffset(middleArrowPos);
575 if ((arrow->GetXOffset() != 0.0) && !m_ignoreArrowOffsets)
576 DrawArrow(dc, arrow, arrow->GetXOffset(), TRUE);
577 else
578 {
579 DrawArrow(dc, arrow, middleArrowPos, FALSE);
580 middleArrowPos += arrow->GetSize() + arrow->GetSpacing();
581 }
582 break;
583 }
584 }
585 node = node->Next();
586 }
587}
588
589void wxLineShape::DrawArrow(wxDC& dc, wxArrowHead *arrow, double xOffset, bool proportionalOffset)
590{
591 wxNode *first_line_node = m_lineControlPoints->First();
592 wxRealPoint *first_line_point = (wxRealPoint *)first_line_node->Data();
593 wxNode *second_line_node = first_line_node->Next();
594 wxRealPoint *second_line_point = (wxRealPoint *)second_line_node->Data();
595
596 wxNode *last_line_node = m_lineControlPoints->Last();
597 wxRealPoint *last_line_point = (wxRealPoint *)last_line_node->Data();
598 wxNode *second_last_line_node = last_line_node->Previous();
599 wxRealPoint *second_last_line_point = (wxRealPoint *)second_last_line_node->Data();
600
601 // Position where we want to start drawing
602 double positionOnLineX, positionOnLineY;
603
604 // Position of start point of line, at the end of which we draw the arrow.
605 double startPositionX, startPositionY;
606
607 switch (arrow->GetPosition())
608 {
609 case ARROW_POSITION_START:
610 {
611 // If we're using a proportional offset, calculate just where this will
612 // be on the line.
613 double realOffset = xOffset;
614 if (proportionalOffset)
615 {
616 double totalLength =
617 (double)sqrt((second_line_point->x - first_line_point->x)*(second_line_point->x - first_line_point->x) +
618 (second_line_point->y - first_line_point->y)*(second_line_point->y - first_line_point->y));
619 realOffset = (double)(xOffset * totalLength);
620 }
621 GetPointOnLine(second_line_point->x, second_line_point->y,
622 first_line_point->x, first_line_point->y,
623 realOffset, &positionOnLineX, &positionOnLineY);
624 startPositionX = second_line_point->x;
625 startPositionY = second_line_point->y;
626 break;
627 }
628 case ARROW_POSITION_END:
629 {
630 // If we're using a proportional offset, calculate just where this will
631 // be on the line.
632 double realOffset = xOffset;
633 if (proportionalOffset)
634 {
635 double totalLength =
636 (double)sqrt((second_last_line_point->x - last_line_point->x)*(second_last_line_point->x - last_line_point->x) +
637 (second_last_line_point->y - last_line_point->y)*(second_last_line_point->y - last_line_point->y));
638 realOffset = (double)(xOffset * totalLength);
639 }
640 GetPointOnLine(second_last_line_point->x, second_last_line_point->y,
641 last_line_point->x, last_line_point->y,
642 realOffset, &positionOnLineX, &positionOnLineY);
643 startPositionX = second_last_line_point->x;
644 startPositionY = second_last_line_point->y;
645 break;
646 }
647 case ARROW_POSITION_MIDDLE:
648 {
649 // Choose a point half way between the last and penultimate points
650 double x = ((last_line_point->x + second_last_line_point->x)/2);
651 double y = ((last_line_point->y + second_last_line_point->y)/2);
652
653 // If we're using a proportional offset, calculate just where this will
654 // be on the line.
655 double realOffset = xOffset;
656 if (proportionalOffset)
657 {
658 double totalLength =
659 (double)sqrt((second_last_line_point->x - x)*(second_last_line_point->x - x) +
660 (second_last_line_point->y - y)*(second_last_line_point->y - y));
661 realOffset = (double)(xOffset * totalLength);
662 }
663
664 GetPointOnLine(second_last_line_point->x, second_last_line_point->y,
665 x, y, realOffset, &positionOnLineX, &positionOnLineY);
666 startPositionX = second_last_line_point->x;
667 startPositionY = second_last_line_point->y;
668 break;
669 }
670 }
671
672 /*
673 * Add yOffset to arrow, if any
674 */
675
676 const double myPi = (double) 3.14159265;
677 // The translation that the y offset may give
678 double deltaX = 0.0;
679 double deltaY = 0.0;
680 if ((arrow->GetYOffset() != 0.0) && !m_ignoreArrowOffsets)
681 {
682 /*
683 |(x4, y4)
684 |d
685 |
686 (x1, y1)--------------(x3, y3)------------------(x2, y2)
687 x4 = x3 - d * sin(theta)
688 y4 = y3 + d * cos(theta)
689
690 Where theta = tan(-1) of (y3-y1)/(x3-x1)
691 */
692 double x1 = startPositionX;
693 double y1 = startPositionY;
694 double x3 = positionOnLineX;
695 double y3 = positionOnLineY;
696 double d = -arrow->GetYOffset(); // Negate so +offset is above line
697
698 double theta = 0.0;
699 if (x3 == x1)
700 theta = (double)(myPi/2.0);
701 else
702 theta = (double)atan((y3-y1)/(x3-x1));
703
704 double x4 = (double)(x3 - (d*sin(theta)));
705 double y4 = (double)(y3 + (d*cos(theta)));
706
707 deltaX = x4 - positionOnLineX;
708 deltaY = y4 - positionOnLineY;
709 }
710
711 switch (arrow->_GetType())
712 {
713 case ARROW_ARROW:
714 {
715 double arrowLength = arrow->GetSize();
716 double arrowWidth = (double)(arrowLength/3.0);
717
718 double tip_x, tip_y, side1_x, side1_y, side2_x, side2_y;
719 oglGetArrowPoints(startPositionX+deltaX, startPositionY+deltaY,
720 positionOnLineX+deltaX, positionOnLineY+deltaY,
721 arrowLength, arrowWidth, &tip_x, &tip_y,
722 &side1_x, &side1_y, &side2_x, &side2_y);
723
724 wxPoint points[4];
725 points[0].x = (int) tip_x; points[0].y = (int) tip_y;
726 points[1].x = (int) side1_x; points[1].y = (int) side1_y;
727 points[2].x = (int) side2_x; points[2].y = (int) side2_y;
728 points[3].x = (int) tip_x; points[3].y = (int) tip_y;
729
730 dc.SetPen(* m_pen);
731 dc.SetBrush(* m_brush);
732 dc.DrawPolygon(4, points);
733 break;
734 }
735 case ARROW_HOLLOW_CIRCLE:
736 case ARROW_FILLED_CIRCLE:
737 {
738 // Find point on line of centre of circle, which is a radius away
739 // from the end position
740 double diameter = (double)(arrow->GetSize());
741 double x, y;
742 GetPointOnLine(startPositionX+deltaX, startPositionY+deltaY,
743 positionOnLineX+deltaX, positionOnLineY+deltaY,
744 (double)(diameter/2.0),
745 &x, &y);
746
747 // Convert ellipse centre to top-left coordinates
748 double x1 = (double)(x - (diameter/2.0));
749 double y1 = (double)(y - (diameter/2.0));
750
751 dc.SetPen(* m_pen);
752 if (arrow->_GetType() == ARROW_HOLLOW_CIRCLE)
753 dc.SetBrush(* g_oglWhiteBackgroundBrush);
754 else
755 dc.SetBrush(* m_brush);
756
757 dc.DrawEllipse((long) x1, (long) y1, (long) diameter, (long) diameter);
758 break;
759 }
760 case ARROW_SINGLE_OBLIQUE:
761 {
762 break;
763 }
764 case ARROW_METAFILE:
765 {
766 if (arrow->GetMetaFile())
767 {
768 // Find point on line of centre of object, which is a half-width away
769 // from the end position
770 /*
771 * width
772 * <-- start pos <-----><-- positionOnLineX
773 * _____
774 * --------------| x | <-- e.g. rectangular arrowhead
775 * -----
776 */
777 double x, y;
778 GetPointOnLine(startPositionX, startPositionY,
779 positionOnLineX, positionOnLineY,
780 (double)(arrow->GetMetaFile()->m_width/2.0),
781 &x, &y);
782
783 // Calculate theta for rotating the metafile.
784 /*
785 |
786 | o(x2, y2) 'o' represents the arrowhead.
787 | /
788 | /
789 | /theta
790 | /(x1, y1)
791 |______________________
792 */
793 double theta = 0.0;
794 double x1 = startPositionX;
795 double y1 = startPositionY;
796 double x2 = positionOnLineX;
797 double y2 = positionOnLineY;
798
799 if ((x1 == x2) && (y1 == y2))
800 theta = 0.0;
801
802 else if ((x1 == x2) && (y1 > y2))
803 theta = (double)(3.0*myPi/2.0);
804
805 else if ((x1 == x2) && (y2 > y1))
806 theta = (double)(myPi/2.0);
807
808 else if ((x2 > x1) && (y2 >= y1))
809 theta = (double)atan((y2 - y1)/(x2 - x1));
810
811 else if (x2 < x1)
812 theta = (double)(myPi + atan((y2 - y1)/(x2 - x1)));
813
814 else if ((x2 > x1) && (y2 < y1))
815 theta = (double)(2*myPi + atan((y2 - y1)/(x2 - x1)));
816
817 else
818 {
819 wxFatalError("Unknown arrowhead rotation case in lines.cc");
820 }
821
822 // Rotate about the centre of the object, then place
823 // the object on the line.
824 if (arrow->GetMetaFile()->GetRotateable())
825 arrow->GetMetaFile()->Rotate(0.0, 0.0, theta);
826
827 if (m_erasing)
828 {
829 // If erasing, just draw a rectangle.
830 double minX, minY, maxX, maxY;
831 arrow->GetMetaFile()->GetBounds(&minX, &minY, &maxX, &maxY);
832 // Make erasing rectangle slightly bigger or you get droppings.
833 int extraPixels = 4;
834 dc.DrawRectangle((long)(deltaX + x + minX - (extraPixels/2.0)), (long)(deltaY + y + minY - (extraPixels/2.0)),
835 (long)(maxX - minX + extraPixels), (long)(maxY - minY + extraPixels));
836 }
837 else
838 arrow->GetMetaFile()->Draw(dc, x+deltaX, y+deltaY);
839 }
840 break;
841 }
842 default:
843 {
844 }
845 }
846}
847
848void wxLineShape::OnErase(wxDC& dc)
849{
850 wxPen *old_pen = m_pen;
851 wxBrush *old_brush = m_brush;
852 SetPen(g_oglWhiteBackgroundPen);
853 SetBrush(g_oglWhiteBackgroundBrush);
854
855 double bound_x, bound_y;
856 GetBoundingBoxMax(&bound_x, &bound_y);
857 if (m_font) dc.SetFont(* m_font);
858
859 // Undraw text regions
860 for (int i = 0; i < 3; i++)
861 {
862 wxNode *node = m_regions.Nth(i);
863 if (node)
864 {
865 double x, y;
866 wxShapeRegion *region = (wxShapeRegion *)node->Data();
867 GetLabelPosition(i, &x, &y);
868 EraseRegion(dc, region, x, y);
869 }
870 }
871
872 // Undraw line
873 dc.SetPen(* g_oglWhiteBackgroundPen);
874 dc.SetBrush(* g_oglWhiteBackgroundBrush);
875
876 // Drawing over the line only seems to work if the line has a thickness
877 // of 1.
878 if (old_pen && (old_pen->GetWidth() > 1))
879 {
880 dc.DrawRectangle((long)(m_xpos - (bound_x/2.0) - 2.0), (long)(m_ypos - (bound_y/2.0) - 2.0),
881 (long)(bound_x+4.0), (long)(bound_y+4.0));
882 }
883 else
884 {
885 m_erasing = TRUE;
886 GetEventHandler()->OnDraw(dc);
887 GetEventHandler()->OnEraseControlPoints(dc);
888 m_erasing = FALSE;
889 }
890
891 if (old_pen) SetPen(old_pen);
892 if (old_brush) SetBrush(old_brush);
893}
894
895void wxLineShape::GetBoundingBoxMin(double *w, double *h)
896{
897 double x1 = 10000;
898 double y1 = 10000;
899 double x2 = -10000;
900 double y2 = -10000;
901
902 wxNode *node = m_lineControlPoints->First();
903 while (node)
904 {
905 wxRealPoint *point = (wxRealPoint *)node->Data();
906
907 if (point->x < x1) x1 = point->x;
908 if (point->y < y1) y1 = point->y;
909 if (point->x > x2) x2 = point->x;
910 if (point->y > y2) y2 = point->y;
911
912 node = node->Next();
913 }
914 *w = (double)(x2 - x1);
915 *h = (double)(y2 - y1);
916}
917
918/*
919 * For a node image of interest, finds the position of this arc
920 * amongst all the arcs which are attached to THIS SIDE of the node image,
921 * and the number of same.
922 */
923void wxLineShape::FindNth(wxShape *image, int *nth, int *no_arcs, bool incoming)
924{
925 int n = -1;
926 int num = 0;
927 wxNode *node = image->GetLines().First();
928 int this_attachment;
929 if (image == m_to)
930 this_attachment = m_attachmentTo;
931 else
932 this_attachment = m_attachmentFrom;
933
934 // Find number of lines going into/out of this particular attachment point
935 while (node)
936 {
937 wxLineShape *line = (wxLineShape *)node->Data();
938
939 if (line->m_from == image)
940 {
941 // This is the nth line attached to 'image'
942 if ((line == this) && !incoming)
943 n = num;
944
945 // Increment num count if this is the same side (attachment number)
946 if (line->m_attachmentFrom == this_attachment)
947 num ++;
948 }
949
950 if (line->m_to == image)
951 {
952 // This is the nth line attached to 'image'
953 if ((line == this) && incoming)
954 n = num;
955
956 // Increment num count if this is the same side (attachment number)
957 if (line->m_attachmentTo == this_attachment)
958 num ++;
959 }
960
961 node = node->Next();
962 }
963 *nth = n;
964 *no_arcs = num;
965}
966
967void wxLineShape::OnDrawOutline(wxDC& dc, double x, double y, double w, double h)
968{
969 wxPen *old_pen = m_pen;
970 wxBrush *old_brush = m_brush;
971
972 wxPen dottedPen(wxColour(0, 0, 0), 1, wxDOT);
973 SetPen(& dottedPen);
974 SetBrush( wxTRANSPARENT_BRUSH );
975
976 GetEventHandler()->OnDraw(dc);
977
978 if (old_pen) SetPen(old_pen);
979 else SetPen(NULL);
980 if (old_brush) SetBrush(old_brush);
981 else SetBrush(NULL);
982}
983
984bool wxLineShape::OnMovePre(wxDC& dc, double x, double y, double old_x, double old_y, bool display)
985{
986 double x_offset = x - old_x;
987 double y_offset = y - old_y;
988
989 if (m_lineControlPoints && !(x_offset == 0.0 && y_offset == 0.0))
990 {
991 wxNode *node = m_lineControlPoints->First();
992 while (node)
993 {
994 wxRealPoint *point = (wxRealPoint *)node->Data();
995 point->x += x_offset;
996 point->y += y_offset;
997 node = node->Next();
998 }
999
1000 }
1001
1002 // Move temporary label rectangles if necessary
1003 for (int i = 0; i < 3; i++)
1004 {
1005 if (m_labelObjects[i])
1006 {
1007 m_labelObjects[i]->Erase(dc);
1008 double xp, yp, xr, yr;
1009 GetLabelPosition(i, &xp, &yp);
1010 wxNode *node = m_regions.Nth(i);
1011 if (node)
1012 {
1013 wxShapeRegion *region = (wxShapeRegion *)node->Data();
1014 region->GetPosition(&xr, &yr);
1015 }
1016 else
1017 {
1018 xr = 0.0; yr = 0.0;
1019 }
1020
1021 m_labelObjects[i]->Move(dc, xp+xr, yp+yr);
1022 }
1023 }
1024 return TRUE;
1025}
1026
1027void wxLineShape::OnMoveLink(wxDC& dc, bool moveControlPoints)
1028{
1029 if (!m_from || !m_to)
1030 return;
1031
1032 if (m_lineControlPoints->Number() > 2)
1033 Initialise();
1034
1035 // Do each end - nothing in the middle. User has to move other points
1036 // manually if necessary.
1037 double end_x, end_y;
1038 double other_end_x, other_end_y;
1039
1040 FindLineEndPoints(&end_x, &end_y, &other_end_x, &other_end_y);
1041
1042 wxNode *first = m_lineControlPoints->First();
1043 wxRealPoint *first_point = (wxRealPoint *)first->Data();
1044 wxNode *last = m_lineControlPoints->Last();
1045 wxRealPoint *last_point = (wxRealPoint *)last->Data();
1046
1047/* This is redundant, surely? Done by SetEnds.
1048 first_point->x = end_x; first_point->y = end_y;
1049 last_point->x = other_end_x; last_point->y = other_end_y;
1050*/
1051
1052 double oldX = m_xpos;
1053 double oldY = m_ypos;
1054
1055 SetEnds(end_x, end_y, other_end_x, other_end_y);
1056
1057 // Do a second time, because one may depend on the other.
1058 FindLineEndPoints(&end_x, &end_y, &other_end_x, &other_end_y);
1059 SetEnds(end_x, end_y, other_end_x, other_end_y);
1060
1061 // Try to move control points with the arc
1062 double x_offset = m_xpos - oldX;
1063 double y_offset = m_ypos - oldY;
1064
1065// if (moveControlPoints && m_lineControlPoints && !(x_offset == 0.0 && y_offset == 0.0))
1066 // Only move control points if it's a self link. And only works if attachment mode is ON.
1067 if ((m_from == m_to) && (m_from->GetAttachmentMode() != ATTACHMENT_MODE_NONE) && moveControlPoints && m_lineControlPoints && !(x_offset == 0.0 && y_offset == 0.0))
1068 {
1069 wxNode *node = m_lineControlPoints->First();
1070 while (node)
1071 {
1072 if ((node != m_lineControlPoints->First()) && (node != m_lineControlPoints->Last()))
1073 {
1074 wxRealPoint *point = (wxRealPoint *)node->Data();
1075 point->x += x_offset;
1076 point->y += y_offset;
1077 }
1078 node = node->Next();
1079 }
1080 }
1081
1082 Move(dc, m_xpos, m_ypos);
1083}
1084
1085// Finds the x, y points at the two ends of the line.
1086// This function can be used by e.g. line-routing routines to
1087// get the actual points on the two node images where the lines will be drawn
1088// to/from.
1089void wxLineShape::FindLineEndPoints(double *fromX, double *fromY, double *toX, double *toY)
1090{
1091 if (!m_from || !m_to)
1092 return;
1093
1094 // Do each end - nothing in the middle. User has to move other points
1095 // manually if necessary.
1096 double end_x, end_y;
1097 double other_end_x, other_end_y;
1098
1099 wxNode *first = m_lineControlPoints->First();
1100 wxRealPoint *first_point = (wxRealPoint *)first->Data();
1101 wxNode *last = m_lineControlPoints->Last();
1102 wxRealPoint *last_point = (wxRealPoint *)last->Data();
1103
1104 wxNode *second = first->Next();
1105 wxRealPoint *second_point = (wxRealPoint *)second->Data();
1106
1107 wxNode *second_last = last->Previous();
1108 wxRealPoint *second_last_point = (wxRealPoint *)second_last->Data();
1109
1110 if (m_lineControlPoints->Number() > 2)
1111 {
1112 if (m_from->GetAttachmentMode() != ATTACHMENT_MODE_NONE)
1113 {
1114 int nth, no_arcs;
1115 FindNth(m_from, &nth, &no_arcs, FALSE); // Not incoming
1116 m_from->GetAttachmentPosition(m_attachmentFrom, &end_x, &end_y, nth, no_arcs, this);
1117 }
1118 else
1119 (void) m_from->GetPerimeterPoint(m_from->GetX(), m_from->GetY(),
1120 (double)second_point->x, (double)second_point->y,
1121 &end_x, &end_y);
1122
1123 if (m_to->GetAttachmentMode() != ATTACHMENT_MODE_NONE)
1124 {
1125 int nth, no_arcs;
1126 FindNth(m_to, &nth, &no_arcs, TRUE); // Incoming
1127 m_to->GetAttachmentPosition(m_attachmentTo, &other_end_x, &other_end_y, nth, no_arcs, this);
1128 }
1129 else
1130 (void) m_to->GetPerimeterPoint(m_to->GetX(), m_to->GetY(),
1131 (double)second_last_point->x, (double)second_last_point->y,
1132 &other_end_x, &other_end_y);
1133 }
1134 else
1135 {
1136 double fromX = m_from->GetX();
1137 double fromY = m_from->GetY();
1138 double toX = m_to->GetX();
1139 double toY = m_to->GetY();
1140
1141 if (m_from->GetAttachmentMode() != ATTACHMENT_MODE_NONE)
1142 {
1143 int nth, no_arcs;
1144 FindNth(m_from, &nth, &no_arcs, FALSE);
1145 m_from->GetAttachmentPosition(m_attachmentFrom, &end_x, &end_y, nth, no_arcs, this);
1146 fromX = end_x;
1147 fromY = end_y;
1148 }
1149
1150 if (m_to->GetAttachmentMode() != ATTACHMENT_MODE_NONE)
1151 {
1152 int nth, no_arcs;
1153 FindNth(m_to, &nth, &no_arcs, TRUE);
1154 m_to->GetAttachmentPosition(m_attachmentTo, &other_end_x, &other_end_y, nth, no_arcs, this);
1155 toX = other_end_x;
1156 toY = other_end_y;
1157 }
1158
1159 if (m_from->GetAttachmentMode() == ATTACHMENT_MODE_NONE)
1160 (void) m_from->GetPerimeterPoint(m_from->GetX(), m_from->GetY(),
1161 toX, toY,
1162 &end_x, &end_y);
1163
1164 if (m_to->GetAttachmentMode() == ATTACHMENT_MODE_NONE)
1165 (void) m_to->GetPerimeterPoint(m_to->GetX(), m_to->GetY(),
1166 fromX, fromY,
1167 &other_end_x, &other_end_y);
1168 }
1169 *fromX = end_x;
1170 *fromY = end_y;
1171 *toX = other_end_x;
1172 *toY = other_end_y;
1173}
1174
1175void wxLineShape::OnDraw(wxDC& dc)
1176{
1177 if (m_lineControlPoints)
1178 {
1179 if (m_pen)
1180 dc.SetPen(* m_pen);
1181 if (m_brush)
1182 dc.SetBrush(* m_brush);
1183
1184 int n = m_lineControlPoints->Number();
1185 wxPoint *points = new wxPoint[n];
1186 int i;
1187 for (i = 0; i < n; i++)
1188 {
1189 wxRealPoint* point = (wxRealPoint*) m_lineControlPoints->Nth(i)->Data();
1190 points[i].x = WXROUND(point->x);
1191 points[i].y = WXROUND(point->y);
1192 }
1193
1194 if (m_isSpline)
1195 dc.DrawSpline(n, points);
1196 else
1197 dc.DrawLines(n, points);
1198
1199#ifdef __WXMSW__
1200 // For some reason, last point isn't drawn under Windows.
1201 dc.DrawPoint(points[n-1]);
1202#endif
1203
1204 delete[] points;
1205
1206
1207 // Problem with pen - if not a solid pen, does strange things
1208 // to the arrowhead. So make (get) a new pen that's solid.
1209 if (m_pen && (m_pen->GetStyle() != wxSOLID))
1210 {
1211 wxPen *solid_pen =
1212 wxThePenList->FindOrCreatePen(m_pen->GetColour(), 1, wxSOLID);
1213 if (solid_pen)
1214 dc.SetPen(* solid_pen);
1215 }
1216 DrawArrows(dc);
1217 }
1218}
1219
1220void wxLineShape::OnDrawControlPoints(wxDC& dc)
1221{
1222 if (!m_drawHandles)
1223 return;
1224
1225 // Draw temporary label rectangles if necessary
1226 for (int i = 0; i < 3; i++)
1227 {
1228 if (m_labelObjects[i])
1229 m_labelObjects[i]->Draw(dc);
1230 }
1231 wxShape::OnDrawControlPoints(dc);
1232}
1233
1234void wxLineShape::OnEraseControlPoints(wxDC& dc)
1235{
1236 // Erase temporary label rectangles if necessary
1237 for (int i = 0; i < 3; i++)
1238 {
1239 if (m_labelObjects[i])
1240 m_labelObjects[i]->Erase(dc);
1241 }
1242 wxShape::OnEraseControlPoints(dc);
1243}
1244
1245void wxLineShape::OnDragLeft(bool draw, double x, double y, int keys, int attachment)
1246{
1247}
1248
1249void wxLineShape::OnBeginDragLeft(double x, double y, int keys, int attachment)
1250{
1251}
1252
1253void wxLineShape::OnEndDragLeft(double x, double y, int keys, int attachment)
1254{
1255}
1256
1257/*
1258void wxLineShape::SetArrowSize(double length, double width)
1259{
1260 arrow_length = length;
1261 arrow_width = width;
1262}
1263
1264void wxLineShape::SetStartArrow(int style)
1265{
1266 start_style = style;
1267}
1268
1269void wxLineShape::SetMiddleArrow(int style)
1270{
1271 middle_style = style;
1272}
1273
1274void wxLineShape::SetEndArrow(int style)
1275{
1276 end_style = style;
1277}
1278*/
1279
1280void wxLineShape::OnDrawContents(wxDC& dc)
1281{
1282 if (GetDisableLabel())
1283 return;
1284
1285 for (int i = 0; i < 3; i++)
1286 {
1287 wxNode *node = m_regions.Nth(i);
1288 if (node)
1289 {
1290 wxShapeRegion *region = (wxShapeRegion *)node->Data();
1291 double x, y;
1292 GetLabelPosition(i, &x, &y);
1293 DrawRegion(dc, region, x, y);
1294 }
1295 }
1296}
1297
1298void wxLineShape::SetTo(wxShape *object)
1299{
1300 m_to = object;
1301}
1302
1303void wxLineShape::SetFrom(wxShape *object)
1304{
1305 m_from = object;
1306}
1307
1308void wxLineShape::MakeControlPoints()
1309{
1310 if (m_canvas && m_lineControlPoints)
1311 {
1312 wxNode *first = m_lineControlPoints->First();
1313 wxNode *last = m_lineControlPoints->Last();
1314 wxRealPoint *first_point = (wxRealPoint *)first->Data();
1315 wxRealPoint *last_point = (wxRealPoint *)last->Data();
1316
1317 wxLineControlPoint *control = new wxLineControlPoint(m_canvas, this, CONTROL_POINT_SIZE,
1318 first_point->x, first_point->y,
1319 CONTROL_POINT_ENDPOINT_FROM);
1320 control->m_point = first_point;
1321 m_canvas->AddShape(control);
1322 m_controlPoints.Append(control);
1323
1324
1325 wxNode *node = first->Next();
1326 while (node != last)
1327 {
1328 wxRealPoint *point = (wxRealPoint *)node->Data();
1329
1330 control = new wxLineControlPoint(m_canvas, this, CONTROL_POINT_SIZE,
1331 point->x, point->y,
1332 CONTROL_POINT_LINE);
1333 control->m_point = point;
1334
1335 m_canvas->AddShape(control);
1336 m_controlPoints.Append(control);
1337
1338 node = node->Next();
1339 }
1340 control = new wxLineControlPoint(m_canvas, this, CONTROL_POINT_SIZE,
1341 last_point->x, last_point->y,
1342 CONTROL_POINT_ENDPOINT_TO);
1343 control->m_point = last_point;
1344 m_canvas->AddShape(control);
1345 m_controlPoints.Append(control);
1346
1347 }
1348
1349}
1350
1351void wxLineShape::ResetControlPoints()
1352{
1353 if (m_canvas && m_lineControlPoints && m_controlPoints.Number() > 0)
1354 {
1355 wxNode *node = m_controlPoints.First();
1356 wxNode *control_node = m_lineControlPoints->First();
1357 while (node && control_node)
1358 {
1359 wxRealPoint *point = (wxRealPoint *)control_node->Data();
1360 wxLineControlPoint *control = (wxLineControlPoint *)node->Data();
1361 control->SetX(point->x);
1362 control->SetY(point->y);
1363
1364 node = node->Next();
1365 control_node = control_node->Next();
1366 }
1367 }
1368}
1369
1370#ifdef PROLOGIO
1371void wxLineShape::WriteAttributes(wxExpr *clause)
1372{
1373 wxShape::WriteAttributes(clause);
1374
1375 if (m_from)
1376 clause->AddAttributeValue("from", m_from->GetId());
1377 if (m_to)
1378 clause->AddAttributeValue("to", m_to->GetId());
1379
1380 if (m_attachmentTo != 0)
1381 clause->AddAttributeValue("attachment_to", (long)m_attachmentTo);
1382 if (m_attachmentFrom != 0)
1383 clause->AddAttributeValue("attachment_from", (long)m_attachmentFrom);
1384
1385 if (m_alignmentStart != 0)
1386 clause->AddAttributeValue("align_start", (long)m_alignmentStart);
1387 if (m_alignmentEnd != 0)
1388 clause->AddAttributeValue("align_end", (long)m_alignmentEnd);
1389
1390 clause->AddAttributeValue("is_spline", (long)m_isSpline);
1391 if (m_maintainStraightLines)
1392 clause->AddAttributeValue("keep_lines_straight", (long)m_maintainStraightLines);
1393
1394 // Make a list of lists for the (sp)line controls
1395 wxExpr *list = new wxExpr(wxExprList);
1396 wxNode *node = m_lineControlPoints->First();
1397 while (node)
1398 {
1399 wxRealPoint *point = (wxRealPoint *)node->Data();
1400 wxExpr *point_list = new wxExpr(wxExprList);
1401 wxExpr *x_expr = new wxExpr((double) point->x);
1402 wxExpr *y_expr = new wxExpr((double) point->y);
1403 point_list->Append(x_expr);
1404 point_list->Append(y_expr);
1405 list->Append(point_list);
1406
1407 node = node->Next();
1408 }
1409 clause->AddAttributeValue("controls", list);
1410
1411 // Write arc arrows in new OGL format, if there are any.
1412 // This is a list of lists. Each sublist comprises:
1413 // (arrowType arrowEnd xOffset arrowSize)
1414 if (m_arcArrows.Number() > 0)
1415 {
1416 wxExpr *arrow_list = new wxExpr(wxExprList);
1417 node = m_arcArrows.First();
1418 while (node)
1419 {
1420 wxArrowHead *head = (wxArrowHead *)node->Data();
1421 wxExpr *head_list = new wxExpr(wxExprList);
1422 head_list->Append(new wxExpr((long)head->_GetType()));
1423 head_list->Append(new wxExpr((long)head->GetArrowEnd()));
1424 head_list->Append(new wxExpr(head->GetXOffset()));
1425 head_list->Append(new wxExpr(head->GetArrowSize()));
1426 head_list->Append(new wxExpr(wxExprString, head->GetName()));
1427 head_list->Append(new wxExpr(head->GetId()));
1428
1429 // New members of wxArrowHead
1430 head_list->Append(new wxExpr(head->GetYOffset()));
1431 head_list->Append(new wxExpr(head->GetSpacing()));
1432
1433 arrow_list->Append(head_list);
1434
1435 node = node->Next();
1436 }
1437 clause->AddAttributeValue("arrows", arrow_list);
1438 }
1439}
1440
1441void wxLineShape::ReadAttributes(wxExpr *clause)
1442{
1443 wxShape::ReadAttributes(clause);
1444
1445 int iVal = (int) m_isSpline;
1446 clause->AssignAttributeValue("is_spline", &iVal);
1447 m_isSpline = (iVal != 0);
1448
1449 iVal = (int) m_maintainStraightLines;
1450 clause->AssignAttributeValue("keep_lines_straight", &iVal);
1451 m_maintainStraightLines = (iVal != 0);
1452
1453 clause->AssignAttributeValue("align_start", &m_alignmentStart);
1454 clause->AssignAttributeValue("align_end", &m_alignmentEnd);
1455
1456 // Compatibility: check for no regions.
1457 if (m_regions.Number() == 0)
1458 {
1459 wxShapeRegion *newRegion = new wxShapeRegion;
1460 newRegion->SetName("Middle");
1461 newRegion->SetSize(150, 50);
1462 m_regions.Append((wxObject *)newRegion);
1463 if (m_text.Number() > 0)
1464 {
1465 newRegion->ClearText();
1466 wxNode *node = m_text.First();
1467 while (node)
1468 {
1469 wxShapeTextLine *textLine = (wxShapeTextLine *)node->Data();
1470 wxNode *next = node->Next();
1471 newRegion->GetFormattedText().Append((wxObject *)textLine);
1472 delete node;
1473 node = next;
1474 }
1475 }
1476
1477 newRegion = new wxShapeRegion;
1478 newRegion->SetName("Start");
1479 newRegion->SetSize(150, 50);
1480 m_regions.Append((wxObject *)newRegion);
1481
1482 newRegion = new wxShapeRegion;
1483 newRegion->SetName("End");
1484 newRegion->SetSize(150, 50);
1485 m_regions.Append((wxObject *)newRegion);
1486 }
1487
1488 m_attachmentTo = 0;
1489 m_attachmentFrom = 0;
1490
1491 clause->AssignAttributeValue("attachment_to", &m_attachmentTo);
1492 clause->AssignAttributeValue("attachment_from", &m_attachmentFrom);
1493
1494 wxExpr *line_list = NULL;
1495
1496 // When image is created, there are default control points. Override
1497 // them if there are some in the file.
1498 clause->AssignAttributeValue("controls", &line_list);
1499
1500 if (line_list)
1501 {
1502 // Read a list of lists for the spline controls
1503 if (m_lineControlPoints)
1504 {
1505 ClearPointList(*m_lineControlPoints);
1506 }
1507 else
1508 m_lineControlPoints = new wxList;
1509
1510 wxExpr *node = line_list->value.first;
1511
1512 while (node)
1513 {
1514 wxExpr *xexpr = node->value.first;
1515 double x = xexpr->RealValue();
1516
1517 wxExpr *yexpr = xexpr->next;
1518 double y = yexpr->RealValue();
1519
1520 wxRealPoint *point = new wxRealPoint(x, y);
1521 m_lineControlPoints->Append((wxObject*) point);
1522
1523 node = node->next;
1524 }
1525 }
1526
1527 // Read arrow list, for new OGL code
1528 wxExpr *arrow_list = NULL;
1529
1530 clause->AssignAttributeValue("arrows", &arrow_list);
1531 if (arrow_list)
1532 {
1533 wxExpr *node = arrow_list->value.first;
1534
1535 while (node)
1536 {
1537 WXTYPE arrowType = ARROW_ARROW;
1538 int arrowEnd = 0;
1539 double xOffset = 0.0;
1540 double arrowSize = 0.0;
1541 wxString arrowName("");
1542 long arrowId = -1;
1543
1544 wxExpr *type_expr = node->Nth(0);
1545 wxExpr *end_expr = node->Nth(1);
1546 wxExpr *dist_expr = node->Nth(2);
1547 wxExpr *size_expr = node->Nth(3);
1548 wxExpr *name_expr = node->Nth(4);
1549 wxExpr *id_expr = node->Nth(5);
1550
1551 // New members of wxArrowHead
1552 wxExpr *yOffsetExpr = node->Nth(6);
1553 wxExpr *spacingExpr = node->Nth(7);
1554
1555 if (type_expr)
1556 arrowType = (int)type_expr->IntegerValue();
1557 if (end_expr)
1558 arrowEnd = (int)end_expr->IntegerValue();
1559 if (dist_expr)
1560 xOffset = dist_expr->RealValue();
1561 if (size_expr)
1562 arrowSize = size_expr->RealValue();
1563 if (name_expr)
1564 arrowName = name_expr->StringValue();
1565 if (id_expr)
1566 arrowId = id_expr->IntegerValue();
1567
1568 if (arrowId == -1)
1569 arrowId = NewId();
1570 else
1571 RegisterId(arrowId);
1572
1573 wxArrowHead *arrowHead = AddArrow(arrowType, arrowEnd, arrowSize, xOffset, (char*) (const char*) arrowName, NULL, arrowId);
1574 if (yOffsetExpr)
1575 arrowHead->SetYOffset(yOffsetExpr->RealValue());
1576 if (spacingExpr)
1577 arrowHead->SetSpacing(spacingExpr->RealValue());
1578
1579 node = node->next;
1580 }
1581 }
1582}
1583#endif
1584
1585void wxLineShape::Copy(wxShape& copy)
1586{
1587 wxShape::Copy(copy);
1588
1589 wxASSERT( copy.IsKindOf(CLASSINFO(wxLineShape)) );
1590
1591 wxLineShape& lineCopy = (wxLineShape&) copy;
1592
1593 lineCopy.m_to = m_to;
1594 lineCopy.m_from = m_from;
1595 lineCopy.m_attachmentTo = m_attachmentTo;
1596 lineCopy.m_attachmentFrom = m_attachmentFrom;
1597 lineCopy.m_isSpline = m_isSpline;
1598 lineCopy.m_alignmentStart = m_alignmentStart;
1599 lineCopy.m_alignmentEnd = m_alignmentEnd;
1600 lineCopy.m_maintainStraightLines = m_maintainStraightLines;
1601 lineCopy.m_lineOrientations.Clear();
1602
1603 wxNode *node = m_lineOrientations.First();
1604 while (node)
1605 {
1606 lineCopy.m_lineOrientations.Append(node->Data());
1607 node = node->Next();
1608 }
1609
1610 if (lineCopy.m_lineControlPoints)
1611 {
1612 ClearPointList(*lineCopy.m_lineControlPoints);
1613 delete lineCopy.m_lineControlPoints;
1614 }
1615
1616 lineCopy.m_lineControlPoints = new wxList;
1617
1618 node = m_lineControlPoints->First();
1619 while (node)
1620 {
1621 wxRealPoint *point = (wxRealPoint *)node->Data();
1622 wxRealPoint *new_point = new wxRealPoint(point->x, point->y);
1623 lineCopy.m_lineControlPoints->Append((wxObject*) new_point);
1624 node = node->Next();
1625 }
1626
1627 // Copy arrows
1628 lineCopy.ClearArrowsAtPosition(-1);
1629 node = m_arcArrows.First();
1630 while (node)
1631 {
1632 wxArrowHead *arrow = (wxArrowHead *)node->Data();
1633 lineCopy.m_arcArrows.Append(new wxArrowHead(*arrow));
1634 node = node->Next();
1635 }
1636}
1637
1638// Override select, to create/delete temporary label-moving objects
1639void wxLineShape::Select(bool select, wxDC* dc)
1640{
1641 wxShape::Select(select, dc);
1642 if (select)
1643 {
1644 for (int i = 0; i < 3; i++)
1645 {
1646 wxNode *node = m_regions.Nth(i);
1647 if (node)
1648 {
1649 wxShapeRegion *region = (wxShapeRegion *)node->Data();
1650 if (region->m_formattedText.Number() > 0)
1651 {
1652 double w, h, x, y, xx, yy;
1653 region->GetSize(&w, &h);
1654 region->GetPosition(&x, &y);
1655 GetLabelPosition(i, &xx, &yy);
1656 if (m_labelObjects[i])
1657 {
1658 m_labelObjects[i]->Select(FALSE);
1659 m_labelObjects[i]->RemoveFromCanvas(m_canvas);
1660 delete m_labelObjects[i];
1661 }
1662 m_labelObjects[i] = OnCreateLabelShape(this, region, w, h);
1663 m_labelObjects[i]->AddToCanvas(m_canvas);
1664 m_labelObjects[i]->Show(TRUE);
1665 if (dc)
1666 m_labelObjects[i]->Move(*dc, (double)(x + xx), (double)(y + yy));
1667 m_labelObjects[i]->Select(TRUE, dc);
1668 }
1669 }
1670 }
1671 }
1672 else
1673 {
1674 for (int i = 0; i < 3; i++)
1675 {
1676 if (m_labelObjects[i])
1677 {
1678 m_labelObjects[i]->Select(FALSE, dc);
1679 m_labelObjects[i]->Erase(*dc);
1680 m_labelObjects[i]->RemoveFromCanvas(m_canvas);
1681 delete m_labelObjects[i];
1682 m_labelObjects[i] = NULL;
1683 }
1684 }
1685 }
1686}
1687
1688/*
1689 * Line control point
1690 *
1691 */
1692
1693IMPLEMENT_DYNAMIC_CLASS(wxLineControlPoint, wxControlPoint)
1694
1695wxLineControlPoint::wxLineControlPoint(wxShapeCanvas *theCanvas, wxShape *object, double size, double x, double y, int the_type):
1696 wxControlPoint(theCanvas, object, size, x, y, the_type)
1697{
1698 m_xpos = x;
1699 m_ypos = y;
1700 m_type = the_type;
1701 m_point = NULL;
1702}
1703
1704wxLineControlPoint::~wxLineControlPoint()
1705{
1706}
1707
1708void wxLineControlPoint::OnDraw(wxDC& dc)
1709{
1710 wxRectangleShape::OnDraw(dc);
1711}
1712
1713// Implement movement of Line point
1714void wxLineControlPoint::OnDragLeft(bool draw, double x, double y, int keys, int attachment)
1715{
1716 m_shape->GetEventHandler()->OnSizingDragLeft(this, draw, x, y, keys, attachment);
1717}
1718
1719void wxLineControlPoint::OnBeginDragLeft(double x, double y, int keys, int attachment)
1720{
1721 m_shape->GetEventHandler()->OnSizingBeginDragLeft(this, x, y, keys, attachment);
1722}
1723
1724void wxLineControlPoint::OnEndDragLeft(double x, double y, int keys, int attachment)
1725{
1726 m_shape->GetEventHandler()->OnSizingEndDragLeft(this, x, y, keys, attachment);
1727}
1728
1729// Control points ('handles') redirect control to the actual shape, to make it easier
1730// to override sizing behaviour.
1731void wxLineShape::OnSizingDragLeft(wxControlPoint* pt, bool draw, double x, double y, int keys, int attachment)
1732{
1733 wxLineControlPoint* lpt = (wxLineControlPoint*) pt;
1734
1735 wxClientDC dc(GetCanvas());
1736 GetCanvas()->PrepareDC(dc);
1737
1738 dc.SetLogicalFunction(OGLRBLF);
1739
1740 wxPen dottedPen(wxColour(0, 0, 0), 1, wxDOT);
1741 dc.SetPen(dottedPen);
1742 dc.SetBrush((* wxTRANSPARENT_BRUSH));
1743
1744 if (lpt->m_type == CONTROL_POINT_LINE)
1745 {
1746 m_canvas->Snap(&x, &y);
1747
1748 lpt->SetX(x); lpt->SetY(y);
1749 lpt->m_point->x = x; lpt->m_point->y = y;
1750
1751 wxLineShape *lineShape = (wxLineShape *)this;
1752
1753 wxPen *old_pen = lineShape->GetPen();
1754 wxBrush *old_brush = lineShape->GetBrush();
1755
1756 wxPen dottedPen(wxColour(0, 0, 0), 1, wxDOT);
1757 lineShape->SetPen(& dottedPen);
1758 lineShape->SetBrush(wxTRANSPARENT_BRUSH);
1759
1760 lineShape->GetEventHandler()->OnMoveLink(dc, FALSE);
1761
1762 lineShape->SetPen(old_pen);
1763 lineShape->SetBrush(old_brush);
1764 }
1765
1766 if (lpt->m_type == CONTROL_POINT_ENDPOINT_FROM || lpt->m_type == CONTROL_POINT_ENDPOINT_TO)
1767 {
1768// lpt->SetX(x); lpt->SetY(y);
1769 }
1770
1771}
1772
1773void wxLineShape::OnSizingBeginDragLeft(wxControlPoint* pt, double x, double y, int keys, int attachment)
1774{
1775 wxLineControlPoint* lpt = (wxLineControlPoint*) pt;
1776
1777 wxClientDC dc(GetCanvas());
1778 GetCanvas()->PrepareDC(dc);
1779
1780 wxLineShape *lineShape = (wxLineShape *)this;
1781 if (lpt->m_type == CONTROL_POINT_LINE)
1782 {
1783 lpt->m_originalPos = * (lpt->m_point);
1784 m_canvas->Snap(&x, &y);
1785
1786 this->Erase(dc);
1787
1788 // Redraw start and end objects because we've left holes
1789 // when erasing the line
1790 lineShape->GetFrom()->OnDraw(dc);
1791 lineShape->GetFrom()->OnDrawContents(dc);
1792 lineShape->GetTo()->OnDraw(dc);
1793 lineShape->GetTo()->OnDrawContents(dc);
1794
1795 this->SetDisableLabel(TRUE);
1796 dc.SetLogicalFunction(OGLRBLF);
1797
1798 lpt->m_xpos = x; lpt->m_ypos = y;
1799 lpt->m_point->x = x; lpt->m_point->y = y;
1800
1801 wxPen *old_pen = lineShape->GetPen();
1802 wxBrush *old_brush = lineShape->GetBrush();
1803
1804 wxPen dottedPen(wxColour(0, 0, 0), 1, wxDOT);
1805 lineShape->SetPen(& dottedPen);
1806 lineShape->SetBrush(wxTRANSPARENT_BRUSH);
1807
1808 lineShape->GetEventHandler()->OnMoveLink(dc, FALSE);
1809
1810 lineShape->SetPen(old_pen);
1811 lineShape->SetBrush(old_brush);
1812 }
1813
1814 if (lpt->m_type == CONTROL_POINT_ENDPOINT_FROM || lpt->m_type == CONTROL_POINT_ENDPOINT_TO)
1815 {
1816 m_canvas->SetCursor(* g_oglBullseyeCursor);
1817 lpt->m_oldCursor = wxSTANDARD_CURSOR;
1818 }
1819}
1820
1821void wxLineShape::OnSizingEndDragLeft(wxControlPoint* pt, double x, double y, int keys, int attachment)
1822{
1823 wxLineControlPoint* lpt = (wxLineControlPoint*) pt;
1824
1825 wxClientDC dc(GetCanvas());
1826 GetCanvas()->PrepareDC(dc);
1827
1828 this->SetDisableLabel(FALSE);
1829 wxLineShape *lineShape = (wxLineShape *)this;
1830
1831 if (lpt->m_type == CONTROL_POINT_LINE)
1832 {
1833 m_canvas->Snap(&x, &y);
1834
1835 wxRealPoint pt = wxRealPoint(x, y);
1836
1837 // Move the control point back to where it was;
1838 // MoveControlPoint will move it to the new position
1839 // if it decides it wants. We only moved the position
1840 // during user feedback so we could redraw the line
1841 // as it changed shape.
1842 lpt->m_xpos = lpt->m_originalPos.x; lpt->m_ypos = lpt->m_originalPos.y;
1843 lpt->m_point->x = lpt->m_originalPos.x; lpt->m_point->y = lpt->m_originalPos.y;
1844
1845 OnMoveMiddleControlPoint(dc, lpt, pt);
1846 }
1847 if (lpt->m_type == CONTROL_POINT_ENDPOINT_FROM)
1848 {
1849 if (lpt->m_oldCursor)
1850 m_canvas->SetCursor(* lpt->m_oldCursor);
1851
1852// this->Erase(dc);
1853
1854// lpt->m_xpos = x; lpt->m_ypos = y;
1855
1856 if (lineShape->GetFrom())
1857 {
1858 lineShape->GetFrom()->MoveLineToNewAttachment(dc, lineShape, x, y);
1859 }
1860 }
1861 if (lpt->m_type == CONTROL_POINT_ENDPOINT_TO)
1862 {
1863 if (lpt->m_oldCursor)
1864 m_canvas->SetCursor(* lpt->m_oldCursor);
1865
1866// lpt->m_xpos = x; lpt->m_ypos = y;
1867
1868 if (lineShape->GetTo())
1869 {
1870 lineShape->GetTo()->MoveLineToNewAttachment(dc, lineShape, x, y);
1871 }
1872 }
1873
1874 // Needed?
1875#if 0
1876 int i = 0;
1877 for (i = 0; i < lineShape->GetLineControlPoints()->Number(); i++)
1878 if (((wxRealPoint *)(lineShape->GetLineControlPoints()->Nth(i)->Data())) == lpt->m_point)
1879 break;
1880
1881 // N.B. in OnMoveControlPoint, an event handler in Hardy could have deselected
1882 // the line and therefore deleted 'this'. -> GPF, intermittently.
1883 // So assume at this point that we've been blown away.
1884
1885 lineShape->OnMoveControlPoint(i+1, x, y);
1886#endif
1887}
1888
1889// This is called only when a non-end control point is moved.
1890bool wxLineShape::OnMoveMiddleControlPoint(wxDC& dc, wxLineControlPoint* lpt, const wxRealPoint& pt)
1891{
1892 lpt->m_xpos = pt.x; lpt->m_ypos = pt.y;
1893 lpt->m_point->x = pt.x; lpt->m_point->y = pt.y;
1894
1895 GetEventHandler()->OnMoveLink(dc);
1896
1897 return TRUE;
1898}
1899
1900// Implement movement of endpoint to a new attachment
1901// OBSOLETE: done by dragging with the left button.
1902
1903#if 0
1904void wxLineControlPoint::OnDragRight(bool draw, double x, double y, int keys, int attachment)
1905{
1906 if (m_type == CONTROL_POINT_ENDPOINT_FROM || m_type == CONTROL_POINT_ENDPOINT_TO)
1907 {
1908 m_xpos = x; m_ypos = y;
1909 }
1910}
1911
1912void wxLineControlPoint::OnBeginDragRight(double x, double y, int keys, int attachment)
1913{
1914 wxClientDC dc(GetCanvas());
1915 GetCanvas()->PrepareDC(dc);
1916
1917 wxLineShape *lineShape = (wxLineShape *)m_shape;
1918 if (m_type == CONTROL_POINT_ENDPOINT_FROM || m_type == CONTROL_POINT_ENDPOINT_TO)
1919 {
1920 Erase(dc);
1921 lineShape->GetEventHandler()->OnDraw(dc);
1922 if (m_type == CONTROL_POINT_ENDPOINT_FROM)
1923 {
1924 lineShape->GetFrom()->GetEventHandler()->OnDraw(dc);
1925 lineShape->GetFrom()->GetEventHandler()->OnDrawContents(dc);
1926 }
1927 else
1928 {
1929 lineShape->GetTo()->GetEventHandler()->OnDraw(dc);
1930 lineShape->GetTo()->GetEventHandler()->OnDrawContents(dc);
1931 }
1932 m_canvas->SetCursor(g_oglBullseyeCursor);
1933 m_oldCursor = wxSTANDARD_CURSOR;
1934 }
1935}
1936
1937void wxLineControlPoint::OnEndDragRight(double x, double y, int keys, int attachment)
1938{
1939 wxClientDC dc(GetCanvas());
1940 GetCanvas()->PrepareDC(dc);
1941
1942 wxLineShape *lineShape = (wxLineShape *)m_shape;
1943 if (m_type == CONTROL_POINT_ENDPOINT_FROM)
1944 {
1945 if (m_oldCursor)
1946 m_canvas->SetCursor(m_oldCursor);
1947
1948 m_xpos = x; m_ypos = y;
1949
1950 if (lineShape->GetFrom())
1951 {
1952 lineShape->GetFrom()->EraseLinks(dc);
1953
1954 int new_attachment;
1955 double distance;
1956
1957 if (lineShape->GetFrom()->HitTest(x, y, &new_attachment, &distance))
1958 lineShape->SetAttachments(new_attachment, lineShape->GetAttachmentTo());
1959
1960 lineShape->GetFrom()->MoveLinks(dc);
1961 }
1962 }
1963 if (m_type == CONTROL_POINT_ENDPOINT_TO)
1964 {
1965 if (m_oldCursor)
1966 m_canvas->SetCursor(m_oldCursor);
1967 m_shape->Erase(dc);
1968
1969 m_xpos = x; m_ypos = y;
1970
1971 if (lineShape->GetTo())
1972 {
1973 lineShape->GetTo()->EraseLinks(dc);
1974
1975 int new_attachment;
1976 double distance;
1977 if (lineShape->GetTo()->HitTest(x, y, &new_attachment, &distance))
1978 lineShape->SetAttachments(lineShape->GetAttachmentFrom(), new_attachment);
1979
1980 lineShape->GetTo()->MoveLinks(dc);
1981 }
1982 }
1983 int i = 0;
1984 for (i = 0; i < lineShape->GetLineControlPoints()->Number(); i++)
1985 if (((wxRealPoint *)(lineShape->GetLineControlPoints()->Nth(i)->Data())) == m_point)
1986 break;
1987 lineShape->OnMoveControlPoint(i+1, x, y);
1988 if (!m_canvas->GetQuickEditMode()) m_canvas->Redraw(dc);
1989}
1990#endif
1991
1992/*
1993 * Get the point on the given line (x1, y1) (x2, y2)
1994 * distance 'length' along from the end,
1995 * returned values in x and y
1996 */
1997
1998void GetPointOnLine(double x1, double y1, double x2, double y2,
1999 double length, double *x, double *y)
2000{
2001 double l = (double)sqrt((x2 - x1)*(x2 - x1) + (y2 - y1)*(y2 - y1));
2002
2003 if (l < 0.01)
2004 l = (double) 0.01;
2005
2006 double i_bar = (x2 - x1)/l;
2007 double j_bar = (y2 - y1)/l;
2008
2009 *x = (- length*i_bar) + x2;
2010 *y = (- length*j_bar) + y2;
2011}
2012
2013wxArrowHead *wxLineShape::AddArrow(WXTYPE type, int end, double size, double xOffset,
2014 const wxString& name, wxPseudoMetaFile *mf, long arrowId)
2015{
2016 wxArrowHead *arrow = new wxArrowHead(type, end, size, xOffset, name, mf, arrowId);
2017 m_arcArrows.Append(arrow);
2018 return arrow;
2019}
2020
2021/*
2022 * Add arrowhead at a particular position in the arrowhead list.
2023 */
2024bool wxLineShape::AddArrowOrdered(wxArrowHead *arrow, wxList& referenceList, int end)
2025{
2026 wxNode *refNode = referenceList.First();
2027 wxNode *currNode = m_arcArrows.First();
2028 wxString targetName(arrow->GetName());
2029 if (!refNode) return FALSE;
2030
2031 // First check whether we need to insert in front of list,
2032 // because this arrowhead is the first in the reference
2033 // list and should therefore be first in the current list.
2034 wxArrowHead *refArrow = (wxArrowHead *)refNode->Data();
2035 if (refArrow->GetName() == targetName)
2036 {
2037 m_arcArrows.Insert(arrow);
2038 return TRUE;
2039 }
2040
2041 while (refNode && currNode)
2042 {
2043 wxArrowHead *currArrow = (wxArrowHead *)currNode->Data();
2044 refArrow = (wxArrowHead *)refNode->Data();
2045
2046 // Matching: advance current arrow pointer
2047 if ((currArrow->GetArrowEnd() == end) &&
2048 (currArrow->GetName() == refArrow->GetName()))
2049 {
2050 currNode = currNode->Next(); // Could be NULL now
2051 if (currNode)
2052 currArrow = (wxArrowHead *)currNode->Data();
2053 }
2054
2055 // Check if we're at the correct position in the
2056 // reference list
2057 if (targetName == refArrow->GetName())
2058 {
2059 if (currNode)
2060 m_arcArrows.Insert(currNode, arrow);
2061 else
2062 m_arcArrows.Append(arrow);
2063 return TRUE;
2064 }
2065 refNode = refNode->Next();
2066 }
2067 m_arcArrows.Append(arrow);
2068 return TRUE;
2069}
2070
2071void wxLineShape::ClearArrowsAtPosition(int end)
2072{
2073 wxNode *node = m_arcArrows.First();
2074 while (node)
2075 {
2076 wxArrowHead *arrow = (wxArrowHead *)node->Data();
2077 wxNode *next = node->Next();
2078 switch (end)
2079 {
2080 case -1:
2081 {
2082 delete arrow;
2083 delete node;
2084 break;
2085 }
2086 case ARROW_POSITION_START:
2087 {
2088 if (arrow->GetArrowEnd() == ARROW_POSITION_START)
2089 {
2090 delete arrow;
2091 delete node;
2092 }
2093 break;
2094 }
2095 case ARROW_POSITION_END:
2096 {
2097 if (arrow->GetArrowEnd() == ARROW_POSITION_END)
2098 {
2099 delete arrow;
2100 delete node;
2101 }
2102 break;
2103 }
2104 case ARROW_POSITION_MIDDLE:
2105 {
2106 if (arrow->GetArrowEnd() == ARROW_POSITION_MIDDLE)
2107 {
2108 delete arrow;
2109 delete node;
2110 }
2111 break;
2112 }
2113 }
2114 node = next;
2115 }
2116}
2117
2118bool wxLineShape::ClearArrow(const wxString& name)
2119{
2120 wxNode *node = m_arcArrows.First();
2121 while (node)
2122 {
2123 wxArrowHead *arrow = (wxArrowHead *)node->Data();
2124 if (arrow->GetName() == name)
2125 {
2126 delete arrow;
2127 delete node;
2128 return TRUE;
2129 }
2130 node = node->Next();
2131 }
2132 return FALSE;
2133}
2134
2135/*
2136 * Finds an arrowhead at the given position (if -1, any position)
2137 *
2138 */
2139
2140wxArrowHead *wxLineShape::FindArrowHead(int position, const wxString& name)
2141{
2142 wxNode *node = m_arcArrows.First();
2143 while (node)
2144 {
2145 wxArrowHead *arrow = (wxArrowHead *)node->Data();
2146 if (((position == -1) || (position == arrow->GetArrowEnd())) &&
2147 (arrow->GetName() == name))
2148 return arrow;
2149 node = node->Next();
2150 }
2151 return NULL;
2152}
2153
2154wxArrowHead *wxLineShape::FindArrowHead(long arrowId)
2155{
2156 wxNode *node = m_arcArrows.First();
2157 while (node)
2158 {
2159 wxArrowHead *arrow = (wxArrowHead *)node->Data();
2160 if (arrowId == arrow->GetId())
2161 return arrow;
2162 node = node->Next();
2163 }
2164 return NULL;
2165}
2166
2167/*
2168 * Deletes an arrowhead at the given position (if -1, any position)
2169 *
2170 */
2171
2172bool wxLineShape::DeleteArrowHead(int position, const wxString& name)
2173{
2174 wxNode *node = m_arcArrows.First();
2175 while (node)
2176 {
2177 wxArrowHead *arrow = (wxArrowHead *)node->Data();
2178 if (((position == -1) || (position == arrow->GetArrowEnd())) &&
2179 (arrow->GetName() == name))
2180 {
2181 delete arrow;
2182 delete node;
2183 return TRUE;
2184 }
2185 node = node->Next();
2186 }
2187 return FALSE;
2188}
2189
2190// Overloaded DeleteArrowHead: pass arrowhead id.
2191bool wxLineShape::DeleteArrowHead(long id)
2192{
2193 wxNode *node = m_arcArrows.First();
2194 while (node)
2195 {
2196 wxArrowHead *arrow = (wxArrowHead *)node->Data();
2197 if (arrow->GetId() == id)
2198 {
2199 delete arrow;
2200 delete node;
2201 return TRUE;
2202 }
2203 node = node->Next();
2204 }
2205 return FALSE;
2206}
2207
2208/*
2209 * Calculate the minimum width a line
2210 * occupies, for the purposes of drawing lines in tools.
2211 *
2212 */
2213
2214double wxLineShape::FindMinimumWidth()
2215{
2216 double minWidth = 0.0;
2217 wxNode *node = m_arcArrows.First();
2218 while (node)
2219 {
2220 wxArrowHead *arrowHead = (wxArrowHead *)node->Data();
2221 minWidth += arrowHead->GetSize();
2222 if (node->Next())
2223 minWidth += arrowHead->GetSpacing();
2224
2225 node = node->Next();
2226 }
2227 // We have ABSOLUTE minimum now. So
2228 // scale it to give it reasonable aesthetics
2229 // when drawing with line.
2230 if (minWidth > 0.0)
2231 minWidth = (double)(minWidth * 1.4);
2232 else
2233 minWidth = 20.0;
2234
2235 SetEnds(0.0, 0.0, minWidth, 0.0);
2236 Initialise();
2237
2238 return minWidth;
2239}
2240
2241// Find which position we're talking about at this (x, y).
2242// Returns ARROW_POSITION_START, ARROW_POSITION_MIDDLE, ARROW_POSITION_END
2243int wxLineShape::FindLinePosition(double x, double y)
2244{
2245 double startX, startY, endX, endY;
2246 GetEnds(&startX, &startY, &endX, &endY);
2247
2248 // Find distances from centre, start and end. The smallest wins.
2249 double centreDistance = (double)(sqrt((x - m_xpos)*(x - m_xpos) + (y - m_ypos)*(y - m_ypos)));
2250 double startDistance = (double)(sqrt((x - startX)*(x - startX) + (y - startY)*(y - startY)));
2251 double endDistance = (double)(sqrt((x - endX)*(x - endX) + (y - endY)*(y - endY)));
2252
2253 if (centreDistance < startDistance && centreDistance < endDistance)
2254 return ARROW_POSITION_MIDDLE;
2255 else if (startDistance < endDistance)
2256 return ARROW_POSITION_START;
2257 else
2258 return ARROW_POSITION_END;
2259}
2260
2261// Set alignment flags
2262void wxLineShape::SetAlignmentOrientation(bool isEnd, bool isHoriz)
2263{
2264 if (isEnd)
2265 {
2266 if (isHoriz && ((m_alignmentEnd & LINE_ALIGNMENT_HORIZ) != LINE_ALIGNMENT_HORIZ))
2267 m_alignmentEnd |= LINE_ALIGNMENT_HORIZ;
2268 else if (!isHoriz && ((m_alignmentEnd & LINE_ALIGNMENT_HORIZ) == LINE_ALIGNMENT_HORIZ))
2269 m_alignmentEnd -= LINE_ALIGNMENT_HORIZ;
2270 }
2271 else
2272 {
2273 if (isHoriz && ((m_alignmentStart & LINE_ALIGNMENT_HORIZ) != LINE_ALIGNMENT_HORIZ))
2274 m_alignmentStart |= LINE_ALIGNMENT_HORIZ;
2275 else if (!isHoriz && ((m_alignmentStart & LINE_ALIGNMENT_HORIZ) == LINE_ALIGNMENT_HORIZ))
2276 m_alignmentStart -= LINE_ALIGNMENT_HORIZ;
2277 }
2278}
2279
2280void wxLineShape::SetAlignmentType(bool isEnd, int alignType)
2281{
2282 if (isEnd)
2283 {
2284 if (alignType == LINE_ALIGNMENT_TO_NEXT_HANDLE)
2285 {
2286 if ((m_alignmentEnd & LINE_ALIGNMENT_TO_NEXT_HANDLE) != LINE_ALIGNMENT_TO_NEXT_HANDLE)
2287 m_alignmentEnd |= LINE_ALIGNMENT_TO_NEXT_HANDLE;
2288 }
2289 else if ((m_alignmentEnd & LINE_ALIGNMENT_TO_NEXT_HANDLE) == LINE_ALIGNMENT_TO_NEXT_HANDLE)
2290 m_alignmentEnd -= LINE_ALIGNMENT_TO_NEXT_HANDLE;
2291 }
2292 else
2293 {
2294 if (alignType == LINE_ALIGNMENT_TO_NEXT_HANDLE)
2295 {
2296 if ((m_alignmentStart & LINE_ALIGNMENT_TO_NEXT_HANDLE) != LINE_ALIGNMENT_TO_NEXT_HANDLE)
2297 m_alignmentStart |= LINE_ALIGNMENT_TO_NEXT_HANDLE;
2298 }
2299 else if ((m_alignmentStart & LINE_ALIGNMENT_TO_NEXT_HANDLE) == LINE_ALIGNMENT_TO_NEXT_HANDLE)
2300 m_alignmentStart -= LINE_ALIGNMENT_TO_NEXT_HANDLE;
2301 }
2302}
2303
2304bool wxLineShape::GetAlignmentOrientation(bool isEnd)
2305{
2306 if (isEnd)
2307 return ((m_alignmentEnd & LINE_ALIGNMENT_HORIZ) == LINE_ALIGNMENT_HORIZ);
2308 else
2309 return ((m_alignmentStart & LINE_ALIGNMENT_HORIZ) == LINE_ALIGNMENT_HORIZ);
2310}
2311
2312int wxLineShape::GetAlignmentType(bool isEnd)
2313{
2314 if (isEnd)
2315 return (m_alignmentEnd & LINE_ALIGNMENT_TO_NEXT_HANDLE);
2316 else
2317 return (m_alignmentStart & LINE_ALIGNMENT_TO_NEXT_HANDLE);
2318}
2319
2320wxRealPoint *wxLineShape::GetNextControlPoint(wxShape *nodeObject)
2321{
2322 int n = m_lineControlPoints->Number();
2323 int nn = 0;
2324 if (m_to == nodeObject)
2325 {
2326 // Must be END of line, so we want (n - 1)th control point.
2327 // But indexing ends at n-1, so subtract 2.
2328 nn = n - 2;
2329 }
2330 else nn = 1;
2331 wxNode *node = m_lineControlPoints->Nth(nn);
2332 if (node)
2333 {
2334 return (wxRealPoint *)node->Data();
2335 }
2336 else
2337 return FALSE;
2338}
2339
2340/*
2341 * Arrowhead
2342 *
2343 */
2344
2345IMPLEMENT_DYNAMIC_CLASS(wxArrowHead, wxObject)
2346
2347wxArrowHead::wxArrowHead(WXTYPE type, int end, double size, double dist, const wxString& name,
2348 wxPseudoMetaFile *mf, long arrowId)
2349{
2350 m_arrowType = type; m_arrowEnd = end; m_arrowSize = size;
2351 m_xOffset = dist;
2352 m_yOffset = 0.0;
2353 m_spacing = 5.0;
2354
2355 m_arrowName = name;
2356 m_metaFile = mf;
2357 m_id = arrowId;
2358 if (m_id == -1)
2359 m_id = NewId();
2360}
2361
2362wxArrowHead::wxArrowHead(wxArrowHead& toCopy)
2363{
2364 m_arrowType = toCopy.m_arrowType; m_arrowEnd = toCopy.GetArrowEnd();
2365 m_arrowSize = toCopy.m_arrowSize;
2366 m_xOffset = toCopy.m_xOffset;
2367 m_yOffset = toCopy.m_yOffset;
2368 m_spacing = toCopy.m_spacing;
2369 m_arrowName = toCopy.m_arrowName ;
2370 if (toCopy.m_metaFile)
2371 m_metaFile = new wxPseudoMetaFile(*(toCopy.m_metaFile));
2372 else
2373 m_metaFile = NULL;
2374 m_id = NewId();
2375}
2376
2377wxArrowHead::~wxArrowHead()
2378{
2379 if (m_metaFile) delete m_metaFile;
2380}
2381
2382void wxArrowHead::SetSize(double size)
2383{
2384 m_arrowSize = size;
2385 if ((m_arrowType == ARROW_METAFILE) && m_metaFile)
2386 {
2387 double oldWidth = m_metaFile->m_width;
2388 if (oldWidth == 0.0)
2389 return;
2390
2391 double scale = (double)(size/oldWidth);
2392 if (scale != 1.0)
2393 m_metaFile->Scale(scale, scale);
2394 }
2395}
2396
2397// Can override this to create a different class of label shape
2398wxLabelShape* wxLineShape::OnCreateLabelShape(wxLineShape *parent, wxShapeRegion *region, double w, double h)
2399{
2400 return new wxLabelShape(parent, region, w, h);
2401}
2402
2403/*
2404 * Label object
2405 *
2406 */
2407
2408IMPLEMENT_DYNAMIC_CLASS(wxLabelShape, wxRectangleShape)
2409
2410wxLabelShape::wxLabelShape(wxLineShape *parent, wxShapeRegion *region, double w, double h):wxRectangleShape(w, h)
2411{
2412 m_lineShape = parent;
2413 m_shapeRegion = region;
2414 SetPen(wxThePenList->FindOrCreatePen(wxColour(0, 0, 0), 1, wxDOT));
2415}
2416
2417wxLabelShape::~wxLabelShape()
2418{
2419}
2420
2421void wxLabelShape::OnDraw(wxDC& dc)
2422{
2423 if (m_lineShape && !m_lineShape->GetDrawHandles())
2424 return;
2425
2426 double x1 = (double)(m_xpos - m_width/2.0);
2427 double y1 = (double)(m_ypos - m_height/2.0);
2428
2429 if (m_pen)
2430 {
2431 if (m_pen->GetWidth() == 0)
2432 dc.SetPen(* g_oglTransparentPen);
2433 else
2434 dc.SetPen(* m_pen);
2435 }
2436 dc.SetBrush(* wxTRANSPARENT_BRUSH);
2437
2438 if (m_cornerRadius > 0.0)
2439 dc.DrawRoundedRectangle(WXROUND(x1), WXROUND(y1), WXROUND(m_width), WXROUND(m_height), m_cornerRadius);
2440 else
2441 dc.DrawRectangle(WXROUND(x1), WXROUND(y1), WXROUND(m_width), WXROUND(m_height));
2442}
2443
2444void wxLabelShape::OnDrawContents(wxDC& dc)
2445{
2446}
2447
2448void wxLabelShape::OnDragLeft(bool draw, double x, double y, int keys, int attachment)
2449{
2450 wxRectangleShape::OnDragLeft(draw, x, y, keys, attachment);
2451}
2452
2453void wxLabelShape::OnBeginDragLeft(double x, double y, int keys, int attachment)
2454{
2455 wxRectangleShape::OnBeginDragLeft(x, y, keys, attachment);
2456}
2457
2458void wxLabelShape::OnEndDragLeft(double x, double y, int keys, int attachment)
2459{
2460 wxRectangleShape::OnEndDragLeft(x, y, keys, attachment);
2461}
2462
2463bool wxLabelShape::OnMovePre(wxDC& dc, double x, double y, double old_x, double old_y, bool display)
2464{
2465 return m_lineShape->OnLabelMovePre(dc, this, x, y, old_x, old_y, display);
2466}
2467
2468bool wxLineShape::OnLabelMovePre(wxDC& dc, wxLabelShape* labelShape, double x, double y, double old_x, double old_y, bool display)
2469{
2470 labelShape->m_shapeRegion->SetSize(labelShape->GetWidth(), labelShape->GetHeight());
2471
2472 // Find position in line's region list
2473 int i = 0;
2474 wxNode *node = GetRegions().First();
2475 while (node)
2476 {
2477 if (labelShape->m_shapeRegion == (wxShapeRegion *)node->Data())
2478 node = NULL;
2479 else
2480 {
2481 node = node->Next();
2482 i ++;
2483 }
2484 }
2485 double xx, yy;
2486 GetLabelPosition(i, &xx, &yy);
2487 // Set the region's offset, relative to the default position for
2488 // each region.
2489 labelShape->m_shapeRegion->SetPosition((double)(x - xx), (double)(y - yy));
2490
2491 labelShape->SetX(x);
2492 labelShape->SetY(y);
2493
2494 // Need to reformat to fit region.
2495 if (labelShape->m_shapeRegion->GetText())
2496 {
2497
2498 wxString s(labelShape->m_shapeRegion->GetText());
2499 labelShape->FormatText(dc, s, i);
2500 DrawRegion(dc, labelShape->m_shapeRegion, xx, yy);
2501 }
2502 return TRUE;
2503}
2504
2505// Divert left and right clicks to line object
2506void wxLabelShape::OnLeftClick(double x, double y, int keys, int attachment)
2507{
2508 m_lineShape->GetEventHandler()->OnLeftClick(x, y, keys, attachment);
2509}
2510
2511void wxLabelShape::OnRightClick(double x, double y, int keys, int attachment)
2512{
2513 m_lineShape->GetEventHandler()->OnRightClick(x, y, keys, attachment);
2514}
2515