forked from jgraph/mxgraph
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmxGraphView.cs
More file actions
1382 lines (1192 loc) · 49.4 KB
/
mxGraphView.cs
File metadata and controls
1382 lines (1192 loc) · 49.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// $Id: mxGraphView.cs,v 1.2 2014/02/19 09:40:59 gaudenz Exp $
// Copyright (c) 2007-2008, Gaudenz Alder
using System;
using System.Diagnostics;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Drawing.Imaging;
namespace com.mxgraph
{
/// <summary>
/// Implements a view for the graph. This class is in charge of computing the
/// absolute coordinates for the relative child geometries, the points for
/// perimeters and edge styles and keeping them cached in mxCellStates for
/// faster retrieval. The states are updated whenever the model or the view
/// state (translate, scale) changes. The scale and translate are honoured in
/// the bounds.
/// </summary>
public class mxGraphView
{
/// <summary>
/// Shared instance of an empty point.
/// </summary>
private static mxPoint EMPTY_POINT = new mxPoint();
/// <summary>
/// Reference to the enclosing graph.
/// </summary>
protected mxGraph graph;
/// <summary>
/// Caches the current bounds of the graph.
/// </summary>
protected mxRectangle graphBounds = new mxRectangle();
/// <summary>
/// Specifies the scale. Default is 1 (100%).
/// </summary>
protected double scale = 1;
/// <summary>
/// Point that specifies the current translation. Default is a new
/// empty point.
/// </summary>
protected mxPoint translate = new mxPoint(0, 0);
/// <summary>
/// Maps from cells to cell states.
/// </summary>
protected Dictionary<Object, mxCellState> states = new Dictionary<Object, mxCellState>();
/// <summary>
/// Specifies if the view should be revalidated if the scale or
/// translation changes.
/// </summary>
protected bool eventsEnabled = true;
/// <summary>
/// Constructs a new view for the given graph.
/// </summary>
/// <param name="graph">Reference to the enclosing graph.</param>
public mxGraphView(mxGraph graph)
{
this.graph = graph;
}
/// <summary>
/// Returns the enclosing graph.
/// </summary>
public mxGraph Graph
{
get { return graph; }
}
/// <summary>
/// Returns the cached diagram bounds.
/// </summary>
public mxRectangle GraphBounds
{
get { return graphBounds; }
set { graphBounds = value; }
}
/// <summary>
/// Sets or returns the current scale.
/// </summary>
public double Scale
{
get { return scale; }
set
{
if (scale != value)
{
scale = value;
Revalidate();
}
}
}
/// <summary>
/// Sets or returns the current translation.
/// </summary>
public mxPoint Translate
{
get { return translate; }
set
{
if (translate.X != value.X ||
translate.Y != value.Y)
{
translate = value;
Revalidate();
}
}
}
/// <summary>
/// Sets or returns the current translation.
/// </summary>
public Dictionary<Object, mxCellState> States
{
get { return states; }
set { states = value; }
}
/// <summary>
/// Sets or returns the current scale.
/// </summary>
public bool IsEventsEnabled
{
get { return eventsEnabled; }
set { eventsEnabled = value; }
}
/// <summary>
/// Returns the bounding box for an array of cells or null, if no cells are
/// specified.
/// </summary>
/// <param name="cells"></param>
/// <returns></returns>
public mxRectangle GetBounds(Object[] cells)
{
return GetBounds(cells, false);
}
/// <summary>
/// Returns the bounding box for an array of cells or null, if no cells are
/// specified.
/// </summary>
/// <param name="cells"></param>
/// <returns></returns>
public mxRectangle GetBoundingBox(Object[] cells)
{
return GetBounds(cells, true);
}
/// <summary>
/// Returns the bounding box for an array of cells or null, if no cells are
/// specified.
/// </summary>
public mxRectangle GetBounds(Object[] cells, bool boundingBox)
{
mxRectangle result = null;
if (cells != null && cells.Length > 0)
{
mxIGraphModel model = graph.Model;
for (int i = 0; i < cells.Length; i++)
{
if (model.IsVertex(cells[i]) || model.IsEdge(cells[i]))
{
mxCellState state = GetState(cells[i]);
if (state != null)
{
mxRectangle tmp = (boundingBox) ? state.BoundingBox : state;
if (tmp != null)
{
if (result == null)
{
result = new mxRectangle(tmp);
}
else
{
result.Add(tmp);
}
}
}
}
}
}
return result;
}
/// <summary>
/// First invalidates, then validates all cell states.
/// </summary>
public void Revalidate()
{
Invalidate();
Validate();
}
/// <summary>
/// Invalidates all cell states.
/// </summary>
public void Invalidate()
{
// LATER: Invalidate cell states recursively
states.Clear();
}
/// <summary>
/// First validates all bounds and then validates all points recursively on
/// all visible cells.
/// </summary>
public void Validate()
{
Object cell = graph.Model.Root;
if (cell != null && states.Count == 0)
{
mxRectangle graphBounds = GetBoundingBox(ValidateCellState(ValidateCell(cell)));
GraphBounds = (graphBounds != null) ? graphBounds : new mxRectangle();
}
}
/// <summary>
/// Shortcut to validateCell with visible set to true.
/// </summary>
public mxRectangle GetBoundingBox(mxCellState state)
{
return GetBoundingBox(state, true);
}
/// <summary>
/// Returns the bounding box of the shape and the label for the given
/// cell state and its children if recurse is true.
/// </summary>
/// <param name="state">Cell state whose bounding box should be returned.</param>
/// <param name="recurse">Boolean indicating if the children should be included.</param>
public mxRectangle GetBoundingBox(mxCellState state, Boolean recurse)
{
mxRectangle bbox = null;
if (state != null)
{
if (state.BoundingBox != null)
{
bbox = (mxRectangle)state.BoundingBox.Clone();
}
if (recurse)
{
mxIGraphModel model = graph.Model;
int childCount = model.GetChildCount(state.Cell);
for (int i = 0; i < childCount; i++)
{
mxRectangle bounds = GetBoundingBox(
GetState(model.GetChildAt(state.Cell, i)), true);
if (bounds != null)
{
if (bbox == null)
{
bbox = bounds;
}
else
{
bbox.Add(bounds);
}
}
}
}
}
return bbox;
}
/// <summary>
/// Shortcut to validateCell with visible set to true.
/// </summary>
public Object ValidateCell(Object cell)
{
return ValidateCell(cell, true);
}
/// <summary>
/// Recursively creates the cell state for the given cell if visible is true and
/// the given cell is visible. If the cell is not visible but the state exists
/// then it is removed using removeState.
/// </summary>
/// <param name="cell">Cell whose cell state should be created.</param>
/// <param name="visible">Boolean indicating if the cell should be visible.</param>
public Object ValidateCell(Object cell, Boolean visible)
{
if (cell != null)
{
visible = visible && graph.IsCellVisible(cell);
mxCellState state = GetState(cell, visible);
if (state != null && !visible)
{
RemoveState(cell);
}
else
{
mxIGraphModel model = graph.Model;
int childCount = model.GetChildCount(cell);
for (int i = 0; i < childCount; i++)
{
ValidateCell(
model.GetChildAt(cell, i),
visible && !graph.IsCellCollapsed(cell));
}
}
}
return cell;
}
/// <summary>
/// Shortcut to validateCellState with recurse set to true.
/// </summary>
public mxCellState ValidateCellState(Object cell)
{
return ValidateCellState(cell, true);
}
/// <summary>
/// Validates the cell state for the given cell.
/// </summary>
/// <param name="cell">Cell whose cell state should be validated.</param>
/// <param name="recurse">Boolean indicating if the children of the cell should be
/// validated.</param>
/// <returns></returns>
public mxCellState ValidateCellState(Object cell, Boolean recurse)
{
mxCellState state = null;
if (cell != null)
{
state = GetState(cell);
if (state != null)
{
mxIGraphModel model = graph.Model;
if (state.Invalid)
{
state.Invalid = false;
ValidateCellState(model.GetParent(cell), false);
mxCellState source = ValidateCellState(GetVisibleTerminal(cell, true), false);
mxCellState target = ValidateCellState(GetVisibleTerminal(cell, false), false);
UpdateCellState(state, source, target);
if (model.IsEdge(cell) || model.IsVertex(cell))
{
UpdateLabelBounds(state);
UpdateBoundingBox(state);
}
}
if (recurse)
{
int childCount = model.GetChildCount(cell);
for (int i = 0; i < childCount; i++)
{
ValidateCellState(model.GetChildAt(cell, i));
}
}
}
}
return state;
}
/// <summary>
/// Updates the given cell state.
/// </summary>
/// <param name="state"></param>
public void UpdateCellState(mxCellState state, mxCellState source, mxCellState target)
{
state.AbsoluteOffset.X = 0;
state.AbsoluteOffset.Y = 0;
state.Origin.X = 0;
state.Origin.Y = 0;
state.Length = 0;
mxIGraphModel model = graph.Model;
mxCellState pState = GetState(model.GetParent(state.Cell));
if (pState != null)
{
state.Origin.X += pState.Origin.X;
state.Origin.Y += pState.Origin.Y;
}
mxPoint offset = graph.GetChildOffsetForCell(state.Cell);
if (offset != null)
{
state.Origin.X += offset.X;
state.Origin.Y += offset.Y;
}
mxGeometry geo = graph.GetCellGeometry(state.Cell);
if (geo != null)
{
if (!model.IsEdge(state.Cell))
{
mxPoint origin = state.Origin;
offset = geo.Offset;
if (offset == null)
{
offset = EMPTY_POINT;
}
if (geo.Relative && pState != null)
{
if (model.IsEdge(pState.Cell))
{
mxPoint orig = GetPoint(pState, geo);
if (orig != null)
{
origin.X += (orig.X / scale) - pState.Origin.X - translate.X;
origin.Y += (orig.Y / scale) - pState.Origin.Y - translate.Y;
}
}
else
{
origin.X += geo.X * pState.Width / scale + offset.X;
origin.Y += geo.Y * pState.Height / scale + offset.Y;
}
}
else
{
state.AbsoluteOffset = new mxPoint(scale * offset.X,
scale * offset.Y);
origin.X += geo.X;
origin.Y += geo.Y;
}
}
state.X = scale * (translate.X + state.Origin.X);
state.Y = scale * (translate.Y + state.Origin.Y);
state.Width = scale * geo.Width;
state.Height = scale * geo.Height;
if (model.IsVertex(state.Cell))
{
UpdateVertexState(state, geo);
}
if (model.IsEdge(state.Cell))
{
UpdateEdgeState(state, geo, source, target);
}
}
}
/// <summary>
/// Validates the given cell state.
/// </summary>
public void UpdateVertexState(mxCellState state, mxGeometry geo)
{
// LATER: Add support for rotation
UpdateVertexLabelOffset(state);
}
/// <summary>
/// Validates the given cell state.
/// </summary>
public void UpdateEdgeState(mxCellState state, mxGeometry geo, mxCellState source, mxCellState target)
{
// This will remove edges with no terminals and no terminal points
// as such edges are invalid and produce NPEs in the edge styles.
// Also removes connected edges that have no visible terminals.
if ((graph.Model.GetTerminal(state.Cell, true) != null && source == null) ||
(source == null && geo.GetTerminalPoint(true) == null) ||
(graph.Model.GetTerminal(state.Cell, false) != null && target == null) ||
(target == null && geo.GetTerminalPoint(false) == null))
{
RemoveState(state.Cell, true);
}
else
{
UpdateFixedTerminalPoints(state, source, target);
UpdatePoints(state, geo.Points, source, target);
UpdateFloatingTerminalPoints(state, source, target);
if (state.AbsolutePointCount() < 2 || state.AbsolutePoints[0] == null || state
.AbsolutePoints[state.AbsolutePointCount() - 1] == null)
{
// This will remove edges with invalid points from the list of states in the view.
// Happens if the one of the terminals and the corresponding terminal point is null.
RemoveState(state.Cell, true);
}
else
{
UpdateEdgeBounds(state);
state.AbsoluteOffset = GetPoint(state, geo);
}
}
}
/// <summary>
/// Updates the absoluteOffset of the given vertex cell state. This takes
/// into account the label position styles.
/// </summary>
/// <param name="state">Cell state whose absolute offset should be updated.</param>
public void UpdateVertexLabelOffset(mxCellState state)
{
string horizontal = mxUtils.GetString(state.Style,
mxConstants.STYLE_LABEL_POSITION,
mxConstants.ALIGN_CENTER);
if (horizontal.Equals(mxConstants.ALIGN_LEFT))
{
state.AbsoluteOffset.X -= state.Width;
}
else if (horizontal.Equals(mxConstants.ALIGN_RIGHT))
{
state.AbsoluteOffset.X += state.Width;
}
string vertical = mxUtils.GetString(state.Style,
mxConstants.STYLE_VERTICAL_LABEL_POSITION,
mxConstants.ALIGN_MIDDLE);
if (vertical.Equals(mxConstants.ALIGN_TOP))
{
state.AbsoluteOffset.Y -= state.Height;
}
else if (vertical.Equals(mxConstants.ALIGN_BOTTOM))
{
state.AbsoluteOffset.Y += state.Height;
}
}
/// <summary>
/// Updates the label bounds in the given state.
/// </summary>
/// <param name="state"></param>
public void UpdateLabelBounds(mxCellState state)
{
Object cell = state.Cell;
Dictionary<string, Object> style = state.Style;
if (mxUtils.GetString(style, mxConstants.STYLE_OVERFLOW, "").Equals("fill"))
{
state.LabelBounds = new mxRectangle(state);
}
else
{
string label = graph.GetLabel(cell);
mxRectangle vertexBounds = (!graph.Model.IsEdge(cell)) ?
state : null;
state.LabelBounds = mxUtils.GetLabelPaintBounds(label,
style, false, state.AbsoluteOffset, vertexBounds,
scale);
}
}
/// <summary>
/// Updates the bounding box in the given cell state.
/// </summary>
/// <param name="state">Cell state whose bounding box should be
/// updated.</param>
/// <returns></returns>
public mxRectangle UpdateBoundingBox(mxCellState state)
{
// Gets the cell bounds and adds shadows and markers
mxRectangle rect = new mxRectangle(state.GetRectangle());
Dictionary<string, Object> style = state.Style;
// Adds extra pixels for the marker and stroke assuming
// that the border stroke is centered around the bounds
// and the first pixel is drawn inside the bounds
double strokeWidth = Math.Max(1, Math.Round(mxUtils.GetInt(style,
mxConstants.STYLE_STROKEWIDTH, 1)
* scale));
strokeWidth -= Math.Max(1, strokeWidth / 2);
if (graph.Model.IsEdge(state.Cell))
{
int ms = 0;
if (style.ContainsKey(mxConstants.STYLE_ENDARROW)
|| style.ContainsKey(mxConstants.STYLE_STARTARROW))
{
ms = (int)Math.Round(mxConstants.DEFAULT_MARKERSIZE * scale);
}
// Adds the strokewidth
rect.Grow(ms + strokeWidth);
// Adds worst case border for an arrow shape
if (mxUtils.GetString(style, mxConstants.STYLE_SHAPE, "").Equals(
mxConstants.SHAPE_ARROW))
{
rect.Grow(mxConstants.ARROW_WIDTH / 2);
}
}
else
{
rect.Grow(strokeWidth);
}
// Adds extra pixels for the shadow
if (mxUtils.IsTrue(style, mxConstants.STYLE_SHADOW))
{
rect.Width += mxConstants.SHADOW_OFFSETX;
rect.Height += mxConstants.SHADOW_OFFSETY;
}
// Adds oversize images in labels
if (mxUtils.GetString(style, mxConstants.STYLE_SHAPE, "").Equals(
mxConstants.SHAPE_LABEL))
{
if (mxUtils.GetString(style, mxConstants.STYLE_IMAGE) != null)
{
double w = mxUtils.GetInt(style,
mxConstants.STYLE_IMAGE_WIDTH,
mxConstants.DEFAULT_IMAGESIZE) * scale;
double h = mxUtils.GetInt(style,
mxConstants.STYLE_IMAGE_HEIGHT,
mxConstants.DEFAULT_IMAGESIZE) * scale;
double x = state.X;
double y = 0;
string imgAlign = mxUtils
.GetString(style, mxConstants.STYLE_IMAGE_ALIGN,
mxConstants.ALIGN_LEFT);
string imgValign = mxUtils.GetString(style,
mxConstants.STYLE_IMAGE_VERTICAL_ALIGN,
mxConstants.ALIGN_MIDDLE);
if (imgAlign.Equals(mxConstants.ALIGN_RIGHT))
{
x += state.Width - w;
}
else if (imgAlign.Equals(mxConstants.ALIGN_CENTER))
{
x += (state.Width - w) / 2;
}
if (imgValign.Equals(mxConstants.ALIGN_TOP))
{
y = state.Y;
}
else if (imgValign.Equals(mxConstants.ALIGN_BOTTOM))
{
y = state.Y + state.Height - h;
}
else
{
y = state.Y + (state.Height - h) / 2;
}
rect.Add(new mxRectangle(x, y, w, h));
}
}
// Adds the rotated bounds to the bounding box if the
// shape is rotated
double rotation = mxUtils.GetDouble(style, mxConstants.STYLE_ROTATION);
mxRectangle bbox = mxUtils.GetBoundingBox(rect, rotation);
// Add the rotated bounding box to the non-rotated so
// that all handles are also covered
if (bbox != null)
{
rect.Add(bbox);
}
// Unifies the cell bounds and the label bounds
if (!mxUtils.GetString(style, mxConstants.STYLE_OVERFLOW, "").Equals("hidden"))
{
rect.Add(state.LabelBounds);
}
state.BoundingBox = rect;
return rect;
}
/// <summary>
/// Sets the initial absolute terminal points in the given state before the edge
/// style is computed.
/// </summary>
/// <param name="edge">Cell state whose initial terminal points should be updated.</param>
/// <param name="source">Cell state which represents the source terminal.</param>
/// <param name="target">Cell state which represents the target terminal.</param>
public void UpdateFixedTerminalPoints(mxCellState edge, mxCellState source, mxCellState target)
{
UpdateFixedTerminalPoint(edge, source, true,
graph.GetConnectionConstraint(edge, source, true));
UpdateFixedTerminalPoint(edge, target, false,
graph.GetConnectionConstraint(edge, target, false));
}
/// <summary>
/// Sets the fixed source or target terminal point on the given edge.
/// </summary>
/// <param name="edge">State whose terminal point should be updated.</param>
/// <param name="terminal">State which represents the actual terminal.</param>
/// <param name="source">Boolean that specifies if the terminal is the source.</param>
/// <param name="constraint">Constraint that specifies the connection.</param>
public void UpdateFixedTerminalPoint(mxCellState edge, mxCellState terminal,
bool source, mxConnectionConstraint constraint)
{
mxPoint pt = null;
if (constraint != null)
{
pt = graph.GetConnectionPoint(terminal, constraint);
}
if (pt == null && terminal == null)
{
mxPoint orig = edge.Origin;
mxGeometry geo = graph.GetCellGeometry(edge.Cell);
pt = geo.GetTerminalPoint(source);
if (pt != null)
{
pt = new mxPoint(scale * (translate.X + pt.X + orig.X),
scale * (translate.Y + pt.Y + orig.Y));
}
}
edge.SetAbsoluteTerminalPoint(pt, source);
}
/// <summary>
/// Updates the absolute points in the given state using the specified array
/// of points as the relative points.
/// </summary>
/// <param name="edge">Cell state whose absolute points should be updated.</param>
/// <param name="points">Array of points that constitute the relative points.</param>
/// <param name="source">Cell that represents the source terminal.</param>
/// <param name="target">Cell that represents the target terminal.</param>
public void UpdatePoints(mxCellState edge, List<mxPoint> points, mxCellState source, mxCellState target)
{
if (edge != null)
{
List<mxPoint> pts = new List<mxPoint>();
pts.Add(edge.AbsolutePoints[0]);
mxEdgeStyleFunction edgeStyle = GetEdgeStyle(edge, points, source, target);
if (edgeStyle != null)
{
mxCellState src = GetTerminalPort(edge, source, true);
mxCellState trg = GetTerminalPort(edge, target, false);
((mxEdgeStyleFunction)edgeStyle)(edge, src, trg, points, pts);
}
else if (points != null)
{
for (int i = 0; i < points.Count; i++)
{
if (points[i] is mxPoint)
{
mxPoint pt = points[i].Clone();
pts.Add(TransformControlPoint(edge, pt));
}
}
}
List<mxPoint> tmp = edge.AbsolutePoints;
pts.Add(tmp[tmp.Count - 1]);
edge.AbsolutePoints = pts;
}
}
/// <summary>
/// Transforms the given control point to an absolute point.
/// </summary>
public mxPoint TransformControlPoint(mxCellState state, mxPoint pt)
{
mxPoint orig = state.Origin;
return new mxPoint(scale * (pt.X + translate.X + orig.X),
scale * (pt.Y + translate.Y + orig.Y));
}
/// <summary>
/// Returns the edge style function to be used to render the given edge
/// state.
/// </summary>
public mxEdgeStyleFunction GetEdgeStyle(mxCellState edge, List<mxPoint> points,
Object source, Object target)
{
object edgeStyle = null;
if (source != null && source == target)
{
edge.Style.TryGetValue(mxConstants.STYLE_LOOP, out edgeStyle);
if (edgeStyle == null)
{
edgeStyle = graph.DefaultLoopStyle;
}
}
else if (!mxUtils.IsTrue(edge.Style,
mxConstants.STYLE_NOEDGESTYLE, false))
{
edge.Style.TryGetValue(mxConstants.STYLE_EDGE, out edgeStyle);
}
// Converts string values to objects
if (edgeStyle is String)
{
string str = edgeStyle.ToString();
Object tmp = mxStyleRegistry.GetValue(str);
if (tmp == null)
{
tmp = mxUtils.Eval(str);
}
edgeStyle = tmp;
}
if (edgeStyle is mxEdgeStyleFunction)
{
return (mxEdgeStyleFunction)edgeStyle;
}
return null;
}
/// <summary>
/// Updates the terminal points in the given state after the edge style was
/// computed for the edge.
/// </summary>
/// <param name="state">State whose terminal points should be updated.</param>
/// <param name="source">State that represents the source terminal.</param>
/// <param name="target">State that represents the target terminal.</param>
public void UpdateFloatingTerminalPoints(mxCellState state, mxCellState source, mxCellState target)
{
mxPoint p0 = state.AbsolutePoints[0];
mxPoint pe = state.AbsolutePoints[state.AbsolutePointCount() - 1];
if (pe == null && target != null)
{
UpdateFloatingTerminalPoint(state, target, source, false);
}
if (p0 == null && source != null)
{
UpdateFloatingTerminalPoint(state, source, target, true);
}
}
/// <summary>
/// Updates the absolute terminal point in the given state for the given
/// start and end state, where start is the source if source is true.
/// </summary>
/// <param name="edge">State whose terminal point should be updated.</param>
/// <param name="start">for the terminal on "this" side of the edge.</param>
/// <param name="end">for the terminal on the other side of the edge.</param>
/// <param name="source">Boolean indicating if start is the source terminal state.</param>
public void UpdateFloatingTerminalPoint(mxCellState edge, mxCellState start,
mxCellState end, bool source)
{
start = GetTerminalPort(edge, start, source);
mxPoint next = GetNextPoint(edge, end, source);
double border = mxUtils.GetDouble(edge.Style, mxConstants.STYLE_PERIMETER_SPACING);
border += mxUtils.GetDouble(edge.Style, (source) ?
mxConstants.STYLE_SOURCE_PERIMETER_SPACING :
mxConstants.STYLE_TARGET_PERIMETER_SPACING);
mxPoint pt = GetPerimeterPoint(start, next, graph.IsOrthogonal(edge), border);
edge.SetAbsoluteTerminalPoint(pt, source);
}
/// <summary>
/// Returns the given terminal or the port defined in the given edge state if a
/// cell state exists for that port.
/// </summary>
public mxCellState GetTerminalPort(mxCellState state, mxCellState terminal, bool source)
{
string key = (source) ? mxConstants.STYLE_SOURCE_PORT
: mxConstants.STYLE_TARGET_PORT;
string id = mxUtils.GetString(state.Style, key);
if (id != null && graph.Model is mxGraphModel)
{
mxCellState tmp = GetState(((mxGraphModel)graph.Model).GetCell(id));
// Only uses ports where a cell state exists
if (tmp != null)
{
terminal = tmp;
}
}
return terminal;
}
/// <summary>
/// Returns a point that defines the location of the intersection point between
/// the perimeter and the line between the center of the shape and the given point.
/// </summary>
public mxPoint GetPerimeterPoint(mxCellState terminal, mxPoint next, bool orthogonal)
{
return GetPerimeterPoint(terminal, next, orthogonal, 0);
}
/// <summary>
/// Returns a point that defines the location of the intersection point between
/// the perimeter and the line between the center of the shape and the given point.
/// </summary>
/// <param name="terminal">State for the source or target terminal.</param>
/// <param name="next">Point that lies outside of the given terminal.</param>
/// <param name="orthogonal">Specifies if the orthogonal projection onto
/// the perimeter should be returned. If this is false then the intersection
/// of the perimeter and the line between the next and the center point is
/// returned.</param>
/// <param name="border">Optional border between the perimeter and the shape.</param>
public mxPoint GetPerimeterPoint(mxCellState terminal, mxPoint next, bool orthogonal, double border)
{
mxPoint point = null;
if (terminal != null)
{
mxPerimeterFunction perimeter = GetPerimeterFunction(terminal);
if (perimeter != null && next != null)
{
mxRectangle bounds = GetPerimeterBounds(terminal, border);
if (bounds.Width > 0 || bounds.Height > 0)
{
point = perimeter(bounds, terminal, next, orthogonal);
}
}
if (point == null)
{
point = GetPoint(terminal);
}
}
return point;
}
/// <summary>
/// Returns the x-coordinate of the center point for automatic routing.
/// </summary>
/// <returns>Returns the x-coordinate of the routing center point.</returns>
public double GetRoutingCenterX(mxCellState state)
{
float f = (state.Style != null) ? mxUtils.GetFloat(state.
Style, mxConstants.STYLE_ROUTING_CENTER_X) : 0;
return state.GetCenterX() + f * state.Width;
}
/// <summary>
/// Returns the y-coordinate of the center point for automatic routing.
/// </summary>
/// <returns>Returns the y-coordinate of the routing center point.</returns>
public double GetRoutingCenterY(mxCellState state)
{
float f = (state.Style != null) ? mxUtils.GetFloat(state.
Style, mxConstants.STYLE_ROUTING_CENTER_Y) : 0;
return state.GetCenterY() + f * state.Height;
}
/// <summary>
/// Returns the perimeter bounds for the given terminal, edge pair.
/// </summary>
public mxRectangle GetPerimeterBounds(mxCellState terminal, double border)
{
if (terminal != null)
{
border += mxUtils.GetDouble(terminal.Style, mxConstants.STYLE_PERIMETER_SPACING);
}
return terminal.GetPerimeterBounds(border * scale);
}