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