Updates referencesource to .NET 4.7
[mono-project.git] / mcs / class / referencesource / System.Web.DataVisualization / Common / Annotation / ImageAnnotation.cs
blobb19820f90b1f9f36201fc8466428fde09b8cc4fe
1 //-------------------------------------------------------------
2 // <copyright company=’Microsoft Corporation’>
3 // Copyright © Microsoft Corporation. All Rights Reserved.
4 // </copyright>
5 //-------------------------------------------------------------
6 // @owner=alexgor, deliant
7 //=================================================================
8 // File: ImageAnnotation.cs
9 //
10 // Namespace: System.Web.UI.WebControls[Windows.Forms].Charting
12 // Classes: ImageAnnotation
14 // Purpose: Image annotation classes.
16 // Reviewed:
18 //===================================================================
20 #region Used namespace
21 using System;
22 using System.Collections;
23 using System.Collections.Specialized;
24 using System.ComponentModel;
25 using System.ComponentModel.Design;
26 using System.Data;
27 using System.Drawing;
28 using System.Drawing.Design;
29 using System.Drawing.Text;
30 using System.Drawing.Drawing2D;
31 #if Microsoft_CONTROL
32 using System.Windows.Forms.DataVisualization.Charting;
33 using System.Windows.Forms.DataVisualization.Charting.Data;
34 using System.Windows.Forms.DataVisualization.Charting.ChartTypes;
35 using System.Windows.Forms.DataVisualization.Charting.Utilities;
36 using System.Windows.Forms.DataVisualization.Charting.Borders3D;
38 #else
39 using System.Web;
40 using System.Web.UI;
41 using System.Web.UI.DataVisualization.Charting;
42 using System.Web.UI.DataVisualization.Charting.Data;
43 using System.Web.UI.DataVisualization.Charting.Utilities;
44 using System.Web.UI.DataVisualization.Charting.Borders3D;
45 #endif
48 #endregion
50 #if Microsoft_CONTROL
51 namespace System.Windows.Forms.DataVisualization.Charting
53 #else
54 namespace System.Web.UI.DataVisualization.Charting
56 #endif
58 /// <summary>
59 /// <b>ImageAnnotation</b> is a class that represents an image annotation.
60 /// </summary>
62 SRDescription("DescriptionAttributeImageAnnotation_ImageAnnotation"),
64 #if ASPPERM_35
65 [AspNetHostingPermission(System.Security.Permissions.SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
66 [AspNetHostingPermission(System.Security.Permissions.SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
67 #endif
68 public class ImageAnnotation : Annotation
70 #region Fields
72 // Annotation image name
73 private string _imageName = String.Empty;
75 // Image wrapping mode
76 private ChartImageWrapMode _imageWrapMode = ChartImageWrapMode.Scaled;
78 // Image transparent color
79 private Color _imageTransparentColor = Color.Empty;
81 #endregion
83 #region Construction and Initialization
85 /// <summary>
86 /// Default public constructor.
87 /// </summary>
88 public ImageAnnotation()
89 : base()
93 #endregion
95 #region Properties
97 #region Image properties
99 /// <summary>
100 /// Gets or sets the name of an annotation's image.
101 /// <seealso cref="ImageTransparentColor"/>
102 /// </summary>
103 /// <value>
104 /// A string value representing the name of an annotation's image.
105 /// </value>
106 /// <remarks>
107 /// The name can be a file name, URL for the web control or a name from
108 /// the <see cref="NamedImagesCollection"/> class.
109 /// </remarks>
111 SRCategory("CategoryAttributeImage"),
112 Bindable(true),
113 DefaultValue(""),
114 Editor(Editors.ImageValueEditor.Editor, Editors.ImageValueEditor.Base),
115 SRDescription("DescriptionAttributeImageAnnotation_Image"),
117 public virtual string Image
121 return _imageName;
125 _imageName = value;
126 this.Invalidate();
131 /// <summary>
132 /// Gets or sets the drawing mode of the image.
133 /// </summary>
134 /// <value>
135 /// A <see cref="ChartImageWrapMode"/> value that defines the drawing mode of the image.
136 /// </value>
138 SRCategory("CategoryAttributeImage"),
139 Bindable(true),
140 DefaultValue(ChartImageWrapMode.Scaled),
141 SRDescription("DescriptionAttributeImageWrapMode"),
143 public ChartImageWrapMode ImageWrapMode
147 return _imageWrapMode;
151 _imageWrapMode = value;
152 this.Invalidate();
156 /// <summary>
157 /// Gets or sets a color which will be replaced with a transparent color while drawing the image.
158 /// </summary>
159 /// <value>
160 /// A <see cref="Color"/> value which will be replaced with a transparent color while drawing the image.
161 /// </value>
163 SRCategory("CategoryAttributeImage"),
164 Bindable(true),
165 DefaultValue(typeof(Color), ""),
166 SRDescription("DescriptionAttributeImageTransparentColor"),
167 TypeConverter(typeof(ColorConverter)),
168 Editor(Editors.ChartColorEditor.Editor, Editors.ChartColorEditor.Base)
170 public Color ImageTransparentColor
174 return _imageTransparentColor;
178 _imageTransparentColor = value;
179 this.Invalidate();
183 /// <summary>
184 /// Gets or sets an annotation's content alignment.
185 /// </summary>
186 /// <value>
187 /// A <see cref="ContentAlignment"/> value that represents the content alignment.
188 /// </value>
189 /// <remarks>
190 /// This property is used to align text for <see cref="TextAnnotation"/>, <see cref="RectangleAnnotation"/>,
191 /// <see cref="EllipseAnnotation"/> and <see cref="CalloutAnnotation"/> objects, and to align
192 /// a non-scaled image inside an <see cref="ImageAnnotation"/> object.
193 /// </remarks>
195 SRCategory("CategoryAttributeImage"),
196 DefaultValue(typeof(ContentAlignment), "MiddleCenter"),
197 SRDescription("DescriptionAttributeImageAnnotation_Alignment"),
199 override public ContentAlignment Alignment
203 return base.Alignment;
207 base.Alignment = value;
208 Invalidate();
212 /// <summary>
213 /// Gets or sets an annotation's text style.
214 /// <seealso cref="Font"/>
215 /// <seealso cref="ForeColor"/>
216 /// </summary>
217 /// <value>
218 /// A <see cref="TextStyle"/> value used to draw an annotation's text.
219 /// </value>
220 [Browsable(false)]
221 [EditorBrowsable(EditorBrowsableState.Never)]
222 public override TextStyle TextStyle
226 return base.TextStyle;
230 base.TextStyle = value;
234 #endregion // Image properties
236 #region Other
238 /// <summary>
239 /// Gets or sets an annotation's type name.
240 /// </summary>
241 /// <remarks>
242 /// This property is used to get the name of each annotation type
243 /// (e.g. Line, Rectangle, Ellipse).
244 /// <para>
245 /// This property is for internal use and is hidden at design and run time.
246 /// </para>
247 /// </remarks>
249 SRCategory("CategoryAttributeMisc"),
250 Bindable(true),
251 Browsable(false),
252 EditorBrowsableAttribute(EditorBrowsableState.Never),
253 DesignerSerializationVisibilityAttribute(DesignerSerializationVisibility.Hidden),
254 SerializationVisibilityAttribute(SerializationVisibility.Hidden),
255 SRDescription("DescriptionAttributeAnnotationType"),
257 public override string AnnotationType
261 return "Image";
265 /// <summary>
266 /// Gets or sets annotation selection points style.
267 /// </summary>
268 /// <value>
269 /// A <see cref="SelectionPointsStyle"/> value that represents annotation
270 /// selection style.
271 /// </value>
272 /// <remarks>
273 /// This property is for internal use and is hidden at design and run time.
274 /// </remarks>
276 SRCategory("CategoryAttributeAppearance"),
277 DefaultValue(SelectionPointsStyle.Rectangle),
278 ParenthesizePropertyNameAttribute(true),
279 Browsable(false),
280 EditorBrowsableAttribute(EditorBrowsableState.Never),
281 DesignerSerializationVisibilityAttribute(DesignerSerializationVisibility.Hidden),
282 SerializationVisibilityAttribute(SerializationVisibility.Hidden),
283 SRDescription("DescriptionAttributeSelectionPointsStyle"),
285 override internal SelectionPointsStyle SelectionPointsStyle
289 return SelectionPointsStyle.Rectangle;
293 #endregion
295 #region Non Applicable Annotation Appearance Attributes (set as Non-Browsable)
297 /// <summary>
298 /// Not applicable to this type of annotation.
299 /// <seealso cref="Font"/>
300 /// </summary>
301 /// <value>
302 /// A <see cref="Color"/> value.
303 /// </value>
305 SRCategory("CategoryAttributeAppearance"),
306 Browsable(false),
307 DefaultValue(typeof(Color), "Black"),
308 TypeConverter(typeof(ColorConverter)),
309 Editor(Editors.ChartColorEditor.Editor, Editors.ChartColorEditor.Base)
311 override public Color ForeColor
315 return base.ForeColor;
319 base.ForeColor = value;
323 /// <summary>
324 /// Not applicable to this type of annotation.
325 /// </summary>
326 /// <value>
327 /// A <see cref="Font"/> object.
328 /// </value>
330 SRCategory("CategoryAttributeAppearance"),
331 Browsable(false),
332 DefaultValue(typeof(Font), "Microsoft Sans Serif, 8pt"),
334 override public Font Font
338 return base.Font;
342 base.Font = value;
346 /// <summary>
347 /// Not applicable to this annotation type.
348 /// </summary>
350 SRCategory("CategoryAttributeAppearance"),
351 Browsable(false),
352 DefaultValue(typeof(Color), ""),
353 NotifyParentPropertyAttribute(true),
354 TypeConverter(typeof(ColorConverter)),
355 Editor(Editors.ChartColorEditor.Editor, Editors.ChartColorEditor.Base)
357 override public Color BackColor
361 return base.BackColor;
365 base.BackColor = value;
369 /// <summary>
370 /// Not applicable to this annotation type.
371 /// </summary>
373 SRCategory("CategoryAttributeAppearance"),
374 Browsable(false),
375 DefaultValue(ChartHatchStyle.None),
376 NotifyParentPropertyAttribute(true),
377 Editor(Editors.HatchStyleEditor.Editor, Editors.HatchStyleEditor.Base)
379 override public ChartHatchStyle BackHatchStyle
383 return base.BackHatchStyle;
387 base.BackHatchStyle = value;
391 /// <summary>
392 /// Not applicable to this annotation type.
393 /// </summary>
395 SRCategory("CategoryAttributeAppearance"),
396 Browsable(false),
397 DefaultValue(GradientStyle.None),
398 NotifyParentPropertyAttribute(true),
399 Editor(Editors.GradientEditor.Editor, Editors.GradientEditor.Base)
401 override public GradientStyle BackGradientStyle
405 return base.BackGradientStyle;
409 base.BackGradientStyle = value;
413 /// <summary>
414 /// Not applicable to this annotation type.
415 /// </summary>
417 SRCategory("CategoryAttributeAppearance"),
418 Browsable(false),
419 DefaultValue(typeof(Color), ""),
420 NotifyParentPropertyAttribute(true),
421 TypeConverter(typeof(ColorConverter)),
422 Editor(Editors.ChartColorEditor.Editor, Editors.ChartColorEditor.Base)
424 override public Color BackSecondaryColor
428 return base.BackSecondaryColor;
432 base.BackSecondaryColor = value;
436 /// <summary>
437 /// Not applicable to this annotation type.
438 /// </summary>
440 SRCategory("CategoryAttributeAppearance"),
441 Browsable(false),
442 DefaultValue(typeof(Color), "Black"),
443 TypeConverter(typeof(ColorConverter)),
444 Editor(Editors.ChartColorEditor.Editor, Editors.ChartColorEditor.Base)
446 override public Color LineColor
450 return base.LineColor;
454 base.LineColor = value;
458 /// <summary>
459 /// Not applicable to this annotation type.
460 /// </summary>
462 SRCategory("CategoryAttributeAppearance"),
463 DefaultValue(1),
464 Browsable(false),
465 SRDescription("DescriptionAttributeLineWidth"),
467 override public int LineWidth
471 return base.LineWidth;
475 base.LineWidth = value;
479 /// <summary>
480 /// Not applicable to this annotation type.
481 /// </summary>
483 SRCategory("CategoryAttributeAppearance"),
484 Browsable(false),
485 DefaultValue(ChartDashStyle.Solid),
486 SRDescription("DescriptionAttributeLineDashStyle"),
488 override public ChartDashStyle LineDashStyle
492 return base.LineDashStyle;
496 base.LineDashStyle = value;
501 #endregion
503 #endregion
505 #region Methods
507 #region Painting
509 /// <summary>
510 /// Paints the annotation object on the specified graphics.
511 /// </summary>
512 /// <param name="graphics">
513 /// A <see cref="ChartGraphics"/> object, used to paint the annotation object.
514 /// </param>
515 /// <param name="chart">
516 /// Reference to the <see cref="Chart"/> owner control.
517 /// </param>
518 override internal void Paint(Chart chart, ChartGraphics graphics)
520 // Get annotation position in relative coordinates
521 PointF firstPoint = PointF.Empty;
522 PointF anchorPoint = PointF.Empty;
523 SizeF size = SizeF.Empty;
524 GetRelativePosition(out firstPoint, out size, out anchorPoint);
525 PointF secondPoint = new PointF(firstPoint.X + size.Width, firstPoint.Y + size.Height);
527 // Create selection rectangle
528 RectangleF selectionRect = new RectangleF(firstPoint, new SizeF(secondPoint.X - firstPoint.X, secondPoint.Y - firstPoint.Y));
530 // Get position
531 RectangleF rectanglePosition = new RectangleF(selectionRect.Location, selectionRect.Size);
532 if(rectanglePosition.Width < 0)
534 rectanglePosition.X = rectanglePosition.Right;
535 rectanglePosition.Width = -rectanglePosition.Width;
537 if(rectanglePosition.Height < 0)
539 rectanglePosition.Y = rectanglePosition.Bottom;
540 rectanglePosition.Height = -rectanglePosition.Height;
543 // Check if position is valid
544 if( float.IsNaN(rectanglePosition.X) ||
545 float.IsNaN(rectanglePosition.Y) ||
546 float.IsNaN(rectanglePosition.Right) ||
547 float.IsNaN(rectanglePosition.Bottom) )
549 return;
552 if(this.Common.ProcessModePaint)
554 // Draw "empty" image at design time
555 if(this._imageName.Length == 0 && this.Chart.IsDesignMode() )
557 graphics.FillRectangleRel(
558 rectanglePosition,
559 this.BackColor,
560 this.BackHatchStyle,
561 this._imageName,
562 this._imageWrapMode,
563 this._imageTransparentColor,
564 GetImageAlignment(this.Alignment),
565 this.BackGradientStyle,
566 this.BackSecondaryColor,
567 this.LineColor,
568 this.LineWidth,
569 this.LineDashStyle,
570 this.ShadowColor,
571 this.ShadowOffset,
572 PenAlignment.Center);
574 // Draw text
575 using( Brush textBrush = new SolidBrush(this.ForeColor) )
577 using (StringFormat format = new StringFormat(StringFormat.GenericTypographic))
579 format.Alignment = StringAlignment.Center;
580 format.LineAlignment = StringAlignment.Center;
581 format.FormatFlags = StringFormatFlags.LineLimit;
582 format.Trimming = StringTrimming.EllipsisCharacter;
583 graphics.DrawStringRel(
584 "(no image)",
585 this.Font,
586 textBrush,
587 rectanglePosition,
588 format);
592 else
594 // Draw image
595 graphics.FillRectangleRel(
596 rectanglePosition,
597 Color.Transparent,
598 this.BackHatchStyle,
599 this._imageName,
600 this._imageWrapMode,
601 this._imageTransparentColor,
602 GetImageAlignment(this.Alignment),
603 this.BackGradientStyle,
604 Color.Transparent,
605 Color.Transparent,
607 this.LineDashStyle,
608 this.ShadowColor,
609 this.ShadowOffset,
610 PenAlignment.Center);
614 if(this.Common.ProcessModeRegions)
616 // Add hot region
617 this.Common.HotRegionsList.AddHotRegion(
618 rectanglePosition,
619 ReplaceKeywords(this.ToolTip),
620 #if Microsoft_CONTROL
621 String.Empty,
622 String.Empty,
623 String.Empty,
624 #else // Microsoft_CONTROL
625 ReplaceKeywords(this.Url),
626 ReplaceKeywords(this.MapAreaAttributes),
627 ReplaceKeywords(this.PostBackValue),
628 #endif // Microsoft_CONTROL
629 this,
630 ChartElementType.Annotation,
631 String.Empty);
634 // Paint selection handles
635 PaintSelectionHandles(graphics, selectionRect, null);
638 /// <summary>
639 /// Coverts ContentAlignment enumeration to ChartImageAlignmentStyle enumeration.
640 /// </summary>
641 /// <param name="alignment">Content alignment.</param>
642 /// <returns>Image content alignment.</returns>
643 private ChartImageAlignmentStyle GetImageAlignment(ContentAlignment alignment)
645 if(alignment == ContentAlignment.TopLeft)
647 return ChartImageAlignmentStyle.TopLeft;
649 else if(alignment == ContentAlignment.TopCenter)
651 return ChartImageAlignmentStyle.Top;
653 else if(alignment == ContentAlignment.TopRight)
655 return ChartImageAlignmentStyle.TopRight;
657 else if(alignment == ContentAlignment.MiddleRight)
659 return ChartImageAlignmentStyle.Right;
661 else if(alignment == ContentAlignment.BottomRight)
663 return ChartImageAlignmentStyle.BottomRight;
665 else if(alignment == ContentAlignment.BottomCenter)
667 return ChartImageAlignmentStyle.Bottom;
669 else if(alignment == ContentAlignment.BottomLeft)
671 return ChartImageAlignmentStyle.BottomLeft;
673 else if(alignment == ContentAlignment.MiddleLeft)
675 return ChartImageAlignmentStyle.Left;
677 return ChartImageAlignmentStyle.Center;
680 #endregion // Painting
682 #region Content Size
684 /// <summary>
685 /// Gets text annotation content size based on the text and font.
686 /// </summary>
687 /// <returns>Annotation content position.</returns>
688 override internal RectangleF GetContentPosition()
690 // Check image size
691 if(this.Image.Length > 0)
693 // Try loading image and getting its size
696 if(this.Chart != null)
698 ImageLoader imageLoader = this.Common.ImageLoader;
700 if(imageLoader != null)
702 ChartGraphics chartGraphics = this.GetGraphics();
704 if (chartGraphics != null)
706 SizeF absSize = new SizeF();
708 if (imageLoader.GetAdjustedImageSize(this.Image, chartGraphics.Graphics, ref absSize))
710 SizeF imageSize = chartGraphics.GetRelativeSize(absSize);
711 return new RectangleF(float.NaN, float.NaN, imageSize.Width, imageSize.Height);
717 catch(ArgumentException)
719 // ArgumentException is thrown by LoadImage in certain situations when it can't load the image
723 return new RectangleF(float.NaN, float.NaN, float.NaN, float.NaN);
726 #endregion
728 #endregion