[2768] | 1 | using System;
|
---|
| 2 | using System.Drawing;
|
---|
[4068] | 3 | using System.Drawing.Drawing2D;
|
---|
[2768] | 4 | using System.IO;
|
---|
[4068] | 5 | namespace Netron.Diagramming.Core {
|
---|
| 6 | /// <summary>
|
---|
| 7 | /// Icon shape material without <see cref="IMouseListener"/> service.
|
---|
| 8 | /// <seealso cref="ClickableIconMaterial"/>
|
---|
| 9 | /// </summary>
|
---|
| 10 | public partial class IconLabelMaterial : ShapeMaterialBase {
|
---|
| 11 |
|
---|
[2768] | 12 | /// <summary>
|
---|
[4068] | 13 | /// The distance between the icon and the text.
|
---|
[2768] | 14 | /// </summary>
|
---|
[4068] | 15 | public const int constTextShift = 2;
|
---|
[2768] | 16 |
|
---|
[4068] | 17 | #region Fields
|
---|
[2768] | 18 |
|
---|
[4068] | 19 | // ------------------------------------------------------------------
|
---|
| 20 | /// <summary>
|
---|
| 21 | /// Implementation of IVersion - the current version of
|
---|
| 22 | /// IconLabelMaterial.
|
---|
| 23 | /// </summary>
|
---|
| 24 | // ------------------------------------------------------------------
|
---|
| 25 | protected double iconLabelMaterialVersion = 1.0;
|
---|
[2768] | 26 |
|
---|
[4068] | 27 | // ------------------------------------------------------------------
|
---|
| 28 | /// <summary>
|
---|
| 29 | /// the Text field
|
---|
| 30 | /// </summary>
|
---|
| 31 | // ------------------------------------------------------------------
|
---|
| 32 | private Bitmap mIcon;
|
---|
[2768] | 33 |
|
---|
[4068] | 34 | // ------------------------------------------------------------------
|
---|
| 35 | /// <summary>
|
---|
| 36 | /// the Text field
|
---|
| 37 | /// </summary>
|
---|
| 38 | // ------------------------------------------------------------------
|
---|
| 39 | private string mText = string.Empty;
|
---|
[2768] | 40 |
|
---|
[4068] | 41 | private Rectangle textRectangle = Rectangle.Empty;
|
---|
[2768] | 42 |
|
---|
[4068] | 43 | #endregion
|
---|
[2768] | 44 |
|
---|
[4068] | 45 | #region Properties
|
---|
[2768] | 46 |
|
---|
[4068] | 47 | // ------------------------------------------------------------------
|
---|
| 48 | /// <summary>
|
---|
| 49 | /// Gets the current version.
|
---|
| 50 | /// </summary>
|
---|
| 51 | // ------------------------------------------------------------------
|
---|
| 52 | public override double Version {
|
---|
| 53 | get {
|
---|
| 54 | return iconLabelMaterialVersion;
|
---|
| 55 | }
|
---|
| 56 | }
|
---|
[2768] | 57 |
|
---|
[4068] | 58 | /// <summary>
|
---|
| 59 | /// Gets the bounds of the text.
|
---|
| 60 | /// </summary>
|
---|
| 61 | public Rectangle TextArea {
|
---|
| 62 | get {
|
---|
| 63 | return textRectangle;
|
---|
| 64 | }
|
---|
| 65 | }
|
---|
[2768] | 66 |
|
---|
[4068] | 67 | /// <summary>
|
---|
| 68 | /// Gets or sets the Text
|
---|
| 69 | /// </summary>
|
---|
| 70 | public string Text {
|
---|
| 71 | get {
|
---|
| 72 | return mText;
|
---|
| 73 | }
|
---|
| 74 | set {
|
---|
| 75 | mText = value;
|
---|
| 76 | }
|
---|
| 77 | }
|
---|
[2768] | 78 |
|
---|
[4068] | 79 | /// <summary>
|
---|
| 80 | /// Gets or sets the Text
|
---|
| 81 | /// </summary>
|
---|
| 82 | public Bitmap Icon {
|
---|
| 83 | get {
|
---|
| 84 | return mIcon;
|
---|
| 85 | }
|
---|
| 86 | set {
|
---|
| 87 | mIcon = value;
|
---|
| 88 | }
|
---|
| 89 | }
|
---|
| 90 | #endregion
|
---|
[2768] | 91 |
|
---|
[4068] | 92 | #region Constructor
|
---|
| 93 | public IconLabelMaterial(string text)
|
---|
| 94 | : base() {
|
---|
| 95 | mText = text;
|
---|
| 96 | }
|
---|
[2768] | 97 |
|
---|
[4068] | 98 | /// <summary>
|
---|
| 99 | /// Initializes a new instance of the <see cref="IconLabelMaterial"/> class.
|
---|
| 100 | /// </summary>
|
---|
| 101 | /// <param name="resourceLocation">The resource location.</param>
|
---|
| 102 | /// <param name="text">The text.</param>
|
---|
| 103 | public IconLabelMaterial(string text, string resourceLocation)
|
---|
| 104 | : this(text) {
|
---|
| 105 | mIcon = GetBitmap(resourceLocation);
|
---|
| 106 | }
|
---|
[2768] | 107 |
|
---|
[4068] | 108 | /// <summary>
|
---|
| 109 | /// Opens the icon (image) from the location specified and sets it
|
---|
| 110 | /// as the image to use.
|
---|
| 111 | /// </summary>
|
---|
| 112 | /// <param name="resourceLocation">string: The resource location.
|
---|
| 113 | /// </param>
|
---|
| 114 | public void SetIcon(string resourceLocation) {
|
---|
| 115 | mIcon = GetBitmap(resourceLocation);
|
---|
| 116 | }
|
---|
[2768] | 117 |
|
---|
[4068] | 118 | /// <summary>
|
---|
| 119 | /// Gets the bitmap at the location specified.
|
---|
| 120 | /// </summary>
|
---|
| 121 | /// <param name="resourceLocation">The resource location.</param>
|
---|
| 122 | /// <returns></returns>
|
---|
| 123 | protected Bitmap GetBitmap(string resourceLocation) {
|
---|
| 124 | if (resourceLocation.Length == 0)
|
---|
| 125 | return null;
|
---|
| 126 | try {
|
---|
| 127 | //first try if it's defined in this assembly somewhere
|
---|
| 128 | return new Bitmap(this.GetType(), resourceLocation);
|
---|
| 129 | }
|
---|
| 130 | catch {
|
---|
[2768] | 131 |
|
---|
[4068] | 132 | if (File.Exists(resourceLocation)) {
|
---|
| 133 | return new Bitmap(resourceLocation);
|
---|
| 134 | } else
|
---|
| 135 | return null;
|
---|
| 136 | }
|
---|
| 137 | }
|
---|
| 138 | /// <summary>
|
---|
| 139 | /// Initializes a new instance of the <see cref="T:IconLabelMaterial"/> class.
|
---|
| 140 | /// </summary>
|
---|
| 141 | public IconLabelMaterial()
|
---|
| 142 | : base() {
|
---|
| 143 | }
|
---|
[2768] | 144 |
|
---|
[4068] | 145 | #endregion
|
---|
[2768] | 146 |
|
---|
[4068] | 147 | #region Methods
|
---|
[2768] | 148 |
|
---|
[4068] | 149 | // ------------------------------------------------------------------
|
---|
| 150 | /// <summary>
|
---|
| 151 | /// Calculates the min size needed to fit this material in.
|
---|
| 152 | /// </summary>
|
---|
| 153 | /// <param name="g">Graphics</param>
|
---|
| 154 | /// <returns>Size</returns>
|
---|
| 155 | // ------------------------------------------------------------------
|
---|
| 156 | public override Size CalculateMinSize(Graphics g) {
|
---|
| 157 | Size minSizeNeeded = new Size(0, 0);
|
---|
[2768] | 158 |
|
---|
[4068] | 159 | if (mText != String.Empty) {
|
---|
| 160 | minSizeNeeded = Size.Round(g.MeasureString(
|
---|
| 161 | this.myTextStyle.GetFormattedText(this.mText),
|
---|
| 162 | this.myTextStyle.Font));
|
---|
| 163 | }
|
---|
[2768] | 164 |
|
---|
[4068] | 165 | minSizeNeeded.Width += constTextShift;
|
---|
[2768] | 166 |
|
---|
[4068] | 167 | if (mIcon != null) {
|
---|
| 168 | minSizeNeeded.Width += mIcon.Width;
|
---|
[2768] | 169 |
|
---|
[4068] | 170 | if (mIcon.Height > minSizeNeeded.Height) {
|
---|
| 171 | minSizeNeeded.Height = mIcon.Height;
|
---|
[2768] | 172 | }
|
---|
[4068] | 173 | }
|
---|
| 174 | return minSizeNeeded;
|
---|
| 175 | }
|
---|
[2768] | 176 |
|
---|
[4068] | 177 | public override void Transform(Rectangle rectangle) {
|
---|
| 178 | textRectangle = new Rectangle(
|
---|
| 179 | rectangle.X + (mIcon == null ? 0 : mIcon.Width) + constTextShift,
|
---|
| 180 | rectangle.Y,
|
---|
| 181 | rectangle.Width - (mIcon == null ? 0 : mIcon.Width) - constTextShift,
|
---|
| 182 | rectangle.Height);
|
---|
| 183 | base.Transform(rectangle);
|
---|
[2768] | 184 |
|
---|
[4068] | 185 | }
|
---|
| 186 | /// <summary>
|
---|
| 187 | /// Paints the entity using the given graphics object
|
---|
| 188 | /// </summary>
|
---|
| 189 | /// <param name="g"></param>
|
---|
| 190 | public override void Paint(Graphics g) {
|
---|
| 191 | if (!Visible)
|
---|
| 192 | return;
|
---|
| 193 | GraphicsContainer cto = g.BeginContainer();
|
---|
| 194 | g.SetClip(Shape.Rectangle);
|
---|
| 195 | if (mIcon != null) {
|
---|
| 196 | g.DrawImage(mIcon, new Rectangle(Rectangle.Location, mIcon.Size));
|
---|
| 197 | }
|
---|
[2768] | 198 |
|
---|
[4068] | 199 | StringFormat stringFormat = myTextStyle.StringFormat;
|
---|
| 200 | stringFormat.Trimming = StringTrimming.EllipsisWord;
|
---|
| 201 | stringFormat.FormatFlags = StringFormatFlags.LineLimit;
|
---|
[2768] | 202 |
|
---|
[4068] | 203 | g.DrawString(
|
---|
| 204 | myTextStyle.GetFormattedText(mText),
|
---|
| 205 | this.myTextStyle.Font,
|
---|
| 206 | this.myTextStyle.GetBrush(),
|
---|
| 207 | textRectangle,
|
---|
| 208 | stringFormat);
|
---|
| 209 | g.EndContainer(cto);
|
---|
| 210 | }
|
---|
| 211 | #endregion
|
---|
[2768] | 212 |
|
---|
[4068] | 213 | }
|
---|
[2768] | 214 | }
|
---|