Merge branch 'alexischr/nursery-canaries-managed-alloc'
[mono.git] / mcs / class / referencesource / System.Activities.Presentation / System.Activities.Presentation / System / Activities / Presentation / FreeFormEditing / ConnectorLabelMarginConverter.cs
1 //------------------------------------------------------------
2 // Copyright (c) Microsoft Corporation.  All rights reserved.
3 //------------------------------------------------------------
4
5 namespace System.Activities.Presentation.FreeFormEditing
6 {
7     using System;
8     using System.Globalization;
9     using System.Windows;
10     using System.Windows.Data;
11     using System.Windows.Media;
12     using System.Diagnostics.CodeAnalysis;
13
14     [SuppressMessage("Microsoft.Performance", "CA1812:AvoidUninstantiatedInternalClasses", Justification = "The class is used in xaml.")]
15     class ConnectorLabelMarginConverter : IMultiValueConverter
16     {
17         const double EPS = 1e-6;
18
19         [SuppressMessage("Microsoft.Design", "CA1062:Validate arguments of public methods", Justification = "The class is only used internally and not accessible externally.")]
20         public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
21         {
22             Thickness margin = new Thickness(0);
23             PointCollection connectorPoints = values[0] as PointCollection;
24             // 8 and 4 is calcuated from the margin / padding settings related to the label in xaml
25             double labelBorderWidth = (double)values[1] + 8;
26             double labelBorderHeight = (double)values[2] + 4;
27             if (connectorPoints != null)
28             {
29                 int longestSegmentIndex;
30                 DesignerGeometryHelper.LongestSegmentLength(connectorPoints, out longestSegmentIndex);
31                 if (longestSegmentIndex >= 0)
32                 {
33                     Point labelLocation = DesignerGeometryHelper.MidPointOfLineSegment(connectorPoints[longestSegmentIndex], connectorPoints[longestSegmentIndex + 1]);
34                     labelLocation.X = (int)(labelLocation.X - labelBorderWidth / 2 + EPS);
35                     labelLocation.Y = (int)(labelLocation.Y - labelBorderHeight / 2 + EPS);
36                     margin.Top = labelLocation.Y;
37                     margin.Left = labelLocation.X;
38                 }
39             }
40             return margin;
41         }
42
43         public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
44         {
45             throw FxTrace.Exception.AsError(new NotSupportedException());
46         }
47     }
48 }