Updates referencesource to .NET 4.7
[mono-project.git] / mcs / class / referencesource / System.Web.DataVisualization / Common / Utilities / ElementPosition.cs
blob2b8707df6c8a9036a25b7497f10b53b460d10fff
1 //-------------------------------------------------------------
2 // <copyright company=’Microsoft Corporation’>
3 // Copyright © Microsoft Corporation. All Rights Reserved.
4 // </copyright>
5 //-------------------------------------------------------------
6 // @owner=alexgor, deliant
7 //=================================================================
8 // File: ElementPosition.cs
9 //
10 // Namespace: System.Web.UI.WebControls[Windows.Forms].Charting
12 // Classes: ElementPosition
14 // Purpose: Class is used to store relative position of the chart
15 // elements like Legend, Title and others. It uses
16 // relative coordinate system where top left corner is
17 // 0,0 and bottom right is 100,100.
18 //
19 // If Auto property is set to true, all position properties
20 // (X,Y,Width and Height) are ignored and they automatically
21 // calculated during chart rendering.
22 //
23 // Note that setting any of the position properties will
24 // automatically set Auto property to false.
26 // Reviewed: AG - August 7, 2002
27 // AG - Microsoft 5, 2007
29 //===================================================================
32 #region Used Namespaces
34 using System;
35 using System.Collections;
36 using System.Collections.Specialized;
37 using System.ComponentModel;
38 using System.ComponentModel.Design;
39 using System.Data;
40 using System.Drawing;
41 using System.Drawing.Design;
42 using System.Drawing.Drawing2D;
43 using System.Diagnostics.CodeAnalysis;
44 using System.Globalization;
46 #if Microsoft_CONTROL
47 using System.Windows.Forms.DataVisualization.Charting;
48 using System.Windows.Forms.DataVisualization.Charting.Data;
49 using System.Windows.Forms.DataVisualization.Charting.ChartTypes;
50 using System.Windows.Forms.DataVisualization.Charting.Utilities;
51 using System.Windows.Forms.DataVisualization.Charting.Borders3D;
52 #else
53 using System.Web;
54 using System.Web.UI;
55 using System.Web.UI.DataVisualization.Charting;
56 using System.Web.UI.DataVisualization.Charting.Data;
57 #endif
60 #endregion
62 #if Microsoft_CONTROL
63 namespace System.Windows.Forms.DataVisualization.Charting
64 #else
65 namespace System.Web.UI.DataVisualization.Charting
67 #endif
69 /// <summary>
70 /// ElementPosition is the base class for many chart visual
71 /// elements like Legend, Title and ChartArea. It provides
72 /// the position of the chart element in relative coordinates,
73 /// from (0,0) to (100,100).
74 /// </summary>
76 SRDescription("DescriptionAttributeElementPosition_ElementPosition"),
77 DefaultProperty("Data"),
79 #if ASPPERM_35
80 [AspNetHostingPermission(System.Security.Permissions.SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
81 [AspNetHostingPermission(System.Security.Permissions.SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
82 #endif
83 public class ElementPosition : ChartElement
85 #region Fields
87 // Private data members, which store properties values
88 private float _x = 0;
89 private float _y = 0;
90 private float _width = 0;
91 private float _height = 0;
92 internal bool _auto = true;
94 // Indicates the auto position of all areas must be reset
95 internal bool resetAreaAutoPosition = false;
97 #endregion
99 #region Constructors
101 /// <summary>
102 /// ElementPosition default constructor
103 /// </summary>
104 public ElementPosition()
108 /// <summary>
109 /// ElementPosition default constructor
110 /// </summary>
111 internal ElementPosition(IChartElement parent)
112 : base(parent)
117 /// <summary>
118 /// ElementPosition constructor.
119 /// </summary>
120 /// <param name="x">X position.</param>
121 /// <param name="y">Y position.</param>
122 /// <param name="width">Width.</param>
123 /// <param name="height">Height.</param>
124 [SuppressMessage("Microsoft.Naming", "CA1704:IdentifiersShouldBeSpelledCorrectly",
125 Justification = "X and Y are cartesian coordinates and well understood")]
126 public ElementPosition(float x, float y, float width, float height)
128 this._auto = false;
129 this._x = x;
130 this._y = y;
131 this._width = width;
132 this._height = height;
135 #endregion
137 #region Methods
139 /// <summary>
140 /// Asks the user at design-time if he wants to change the Auto position
141 /// of all areas at the same time.
142 /// </summary>
143 /// <param name="autoValue">Value to be set for the Auto property.</param>
144 private void ResetAllAreasAutoPosition(bool autoValue)
146 if(resetAreaAutoPosition)
148 // Proceed only if at design time
149 if(Chart != null && Chart.IsDesignMode() && !Chart.serializing && Chart.Site != null)
151 // Check if there is more than one area and Auto position set to the same value
152 if(Chart.ChartAreas.Count > 1)
154 bool firstAutoValue = Chart.ChartAreas[0].Position.Auto;
155 bool sameAutoValue = true;
156 foreach(ChartArea area in Chart.ChartAreas)
158 if(area.Position.Auto != firstAutoValue)
160 sameAutoValue = false;
161 break;
165 // Proceed only all Auto values are the same
166 if(sameAutoValue)
168 string message = SR.MessageChangingChartAreaPositionProperty;
169 if (autoValue)
171 message += SR.MessageChangingChartAreaPositionConfirmAutomatic;
173 else
175 message += SR.MessageChangingChartAreaPositionConfirmCustom;
179 IDesignerMessageBoxDialog confirm = Chart.Site.GetService(typeof(IDesignerMessageBoxDialog)) as IDesignerMessageBoxDialog;
180 if (confirm != null && confirm.ShowQuestion(message))
182 foreach (ChartArea area in Chart.ChartAreas)
184 if (autoValue)
186 this.SetPositionNoAuto(0f, 0f, 0f, 0f);
188 area.Position._auto = autoValue;
198 /// <summary>
199 /// Convert element position into RectangleF
200 /// </summary>
201 /// <returns>RectangleF structure.</returns>
202 public RectangleF ToRectangleF()
204 return new RectangleF(_x, _y, _width, _height);
207 /// <summary>
208 /// Initializes ElementPosition from RectangleF
209 /// </summary>
210 /// <param name="rect">RectangleF structure.</param>
211 public void FromRectangleF(RectangleF rect)
213 if (rect == null)
214 throw new ArgumentNullException("rect");
216 this._x = rect.X;
217 this._y = rect.Y;
218 this._width = rect.Width;
219 this._height = rect.Height;
220 this._auto = false;
223 /// <summary>
224 /// Gets the size of the ElementPosition object.
225 /// </summary>
226 /// <returns>The size of the ElementPosition object.</returns>
227 [Browsable(false)]
228 [Utilities.SerializationVisibility(Utilities.SerializationVisibility.Hidden)]
229 public SizeF Size
231 get { return new SizeF(this._width, this._height); }
234 /// <summary>
235 /// Gets the bottom position in relative coordinates.
236 /// </summary>
237 /// <returns>Bottom position.</returns>
238 [Browsable(false)]
239 [Utilities.SerializationVisibility(Utilities.SerializationVisibility.Hidden)]
240 public float Bottom
242 get { return this._y + this._height; }
245 /// <summary>
246 /// Gets the right position in relative coordinates.
247 /// </summary>
248 /// <returns>Right position.</returns>
249 [Browsable(false)]
250 [Utilities.SerializationVisibility(Utilities.SerializationVisibility.Hidden)]
251 public float Right
253 get{ return this._x + this._width; }
256 /// <summary>
257 /// Determines whether the specified Object is equal to the current Object.
258 /// </summary>
259 /// <param name="obj">The Object to compare with the current Object.</param>
260 /// <returns>true if the specified Object is equal to the current Object; otherwise, false.</returns>
261 internal override bool EqualsInternal(object obj)
263 ElementPosition pos = obj as ElementPosition;
264 if(pos != null)
266 if(this._auto == true && this._auto == pos._auto)
268 return true;
270 else if(this._x == pos._x && this._y == pos._y &&
271 this._width == pos._width && this._height == pos._height)
273 return true;
277 return false;
280 /// <summary>
281 /// Returns a string that represents the element position data.
282 /// </summary>
283 /// <returns>Element position data as a string.</returns>
284 internal override string ToStringInternal()
286 string posString = Constants.AutoValue;
287 if(!this._auto)
289 posString =
290 this._x.ToString(System.Globalization.CultureInfo.CurrentCulture)+", "+
291 this._y.ToString(System.Globalization.CultureInfo.CurrentCulture)+", "+
292 this._width.ToString(System.Globalization.CultureInfo.CurrentCulture)+", "+
293 this._height.ToString(System.Globalization.CultureInfo.CurrentCulture);
295 return posString;
298 /// <summary>
299 /// Set the element position without modifying the "Auto" property
300 /// </summary>
301 /// <param name="x">X position.</param>
302 /// <param name="y">Y position.</param>
303 /// <param name="width">Width.</param>
304 /// <param name="height">Height.</param>
305 internal void SetPositionNoAuto(float x, float y, float width, float height)
307 bool oldValue = this._auto;
308 this._x = x;
309 this._y = y;
310 this._width = width;
311 this._height = height;
312 this._auto = oldValue;
315 #endregion
317 #region Element Position properties
319 /// <summary>
320 /// X position of element.
321 /// </summary>
323 SRCategory("CategoryAttributeMisc"),
324 Bindable(true),
325 DefaultValue(0.0F),
326 SRDescription("DescriptionAttributeElementPosition_X"),
327 NotifyParentPropertyAttribute(true),
328 RefreshPropertiesAttribute(RefreshProperties.All),
329 #if !Microsoft_CONTROL
330 PersistenceMode(PersistenceMode.Attribute)
331 #endif
333 [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1704:IdentifiersShouldBeSpelledCorrectly", MessageId = "X")]
334 public float X
338 return _x;
342 if(value < 0.0 || value > 100.0)
344 throw(new ArgumentOutOfRangeException("value", SR.ExceptionElementPositionArgumentOutOfRange));
346 _x = value;
347 Auto = false;
349 // Adjust width
350 if( (_x + Width) > 100)
352 Width = 100 - _x;
355 this.Invalidate();
359 /// <summary>
360 /// Y position of element.
361 /// </summary>
363 SRCategory("CategoryAttributeMisc"),
364 Bindable(true),
365 DefaultValue(0.0F),
366 SRDescription("DescriptionAttributeElementPosition_Y"),
367 NotifyParentPropertyAttribute(true),
368 RefreshPropertiesAttribute(RefreshProperties.All),
369 #if !Microsoft_CONTROL
370 PersistenceMode(PersistenceMode.Attribute)
371 #endif
373 [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1704:IdentifiersShouldBeSpelledCorrectly", MessageId = "Y")]
374 public float Y
378 return _y;
382 if(value < 0.0 || value > 100.0)
384 throw(new ArgumentOutOfRangeException("value", SR.ExceptionElementPositionArgumentOutOfRange));
386 _y = value;
387 Auto = false;
389 // Adjust heigth
390 if( (_y + Height) > 100)
392 Height = 100 - _y;
395 this.Invalidate();
399 /// <summary>
400 /// Width of element.
401 /// </summary>
403 SRCategory("CategoryAttributeMisc"),
404 Bindable(true),
405 DefaultValue(0.0F),
406 SRDescription("DescriptionAttributeElementPosition_Width"),
407 NotifyParentPropertyAttribute(true),
408 RefreshPropertiesAttribute(RefreshProperties.All),
409 #if !Microsoft_CONTROL
410 PersistenceMode(PersistenceMode.Attribute)
411 #endif
413 public float Width
417 return _width;
421 if(value < 0.0 || value > 100.0)
423 throw(new ArgumentOutOfRangeException("value", SR.ExceptionElementPositionArgumentOutOfRange));
425 _width = value;
426 Auto = false;
428 // Adjust x
429 if( (_x + Width) > 100)
431 _x = 100 - Width;
434 this.Invalidate();
438 /// <summary>
439 /// Height of element.
440 /// </summary>
442 SRCategory("CategoryAttributeMisc"),
443 Bindable(true),
444 DefaultValue(0.0F),
445 SRDescription("DescriptionAttributeElementPosition_Height"),
446 NotifyParentPropertyAttribute(true),
447 RefreshPropertiesAttribute(RefreshProperties.All),
448 #if !Microsoft_CONTROL
449 PersistenceMode(PersistenceMode.Attribute)
450 #endif
452 public float Height
456 return _height;
460 if(value < 0.0 || value > 100.0)
462 throw(new ArgumentOutOfRangeException("value", SR.ExceptionElementPositionArgumentOutOfRange));
464 _height = value;
465 Auto = false;
467 // Adjust y
468 if( (_y + Height) > 100)
470 _y = 100 - Height;
474 this.Invalidate();
478 /// <summary>
479 /// Gets or sets a flag which indicates whether positioning is on.
480 /// </summary>
482 SRCategory("CategoryAttributeMisc"),
483 Bindable(true),
484 DefaultValue(true),
485 SRDescription("DescriptionAttributeElementPosition_Auto"),
486 NotifyParentPropertyAttribute(true),
487 RefreshPropertiesAttribute(RefreshProperties.All),
488 #if !Microsoft_CONTROL
489 PersistenceMode(PersistenceMode.Attribute)
490 #endif
492 public bool Auto
496 return _auto;
500 if(value != _auto)
502 ResetAllAreasAutoPosition(value);
504 if(value)
506 this._x = 0;
507 this._y = 0;
508 this._width = 0;
509 this._height = 0;
511 _auto = value;
513 this.Invalidate();
518 #endregion
521 /// <summary>
522 /// Used for invoking windows forms MesageBox dialog.
523 /// </summary>
524 internal interface IDesignerMessageBoxDialog
526 /// <summary>
527 /// Shows Yes/No MessageBox.
528 /// </summary>
529 /// <param name="message">The message.</param>
530 /// <returns>
531 /// true if user confirms with Yes
532 /// </returns>
533 bool ShowQuestion(string message);