1 | using System;
|
---|
2 | using System.Drawing;
|
---|
3 | using System.Drawing.Drawing2D;
|
---|
4 | using System.IO;
|
---|
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 |
|
---|
12 | /// <summary>
|
---|
13 | /// The distance between the icon and the text.
|
---|
14 | /// </summary>
|
---|
15 | public const int constTextShift = 2;
|
---|
16 |
|
---|
17 | #region Fields
|
---|
18 |
|
---|
19 | // ------------------------------------------------------------------
|
---|
20 | /// <summary>
|
---|
21 | /// Implementation of IVersion - the current version of
|
---|
22 | /// IconLabelMaterial.
|
---|
23 | /// </summary>
|
---|
24 | // ------------------------------------------------------------------
|
---|
25 | protected double iconLabelMaterialVersion = 1.0;
|
---|
26 |
|
---|
27 | // ------------------------------------------------------------------
|
---|
28 | /// <summary>
|
---|
29 | /// the Text field
|
---|
30 | /// </summary>
|
---|
31 | // ------------------------------------------------------------------
|
---|
32 | private Bitmap mIcon;
|
---|
33 |
|
---|
34 | // ------------------------------------------------------------------
|
---|
35 | /// <summary>
|
---|
36 | /// the Text field
|
---|
37 | /// </summary>
|
---|
38 | // ------------------------------------------------------------------
|
---|
39 | private string mText = string.Empty;
|
---|
40 |
|
---|
41 | private Rectangle textRectangle = Rectangle.Empty;
|
---|
42 |
|
---|
43 | #endregion
|
---|
44 |
|
---|
45 | #region Properties
|
---|
46 |
|
---|
47 | // ------------------------------------------------------------------
|
---|
48 | /// <summary>
|
---|
49 | /// Gets the current version.
|
---|
50 | /// </summary>
|
---|
51 | // ------------------------------------------------------------------
|
---|
52 | public override double Version {
|
---|
53 | get {
|
---|
54 | return iconLabelMaterialVersion;
|
---|
55 | }
|
---|
56 | }
|
---|
57 |
|
---|
58 | /// <summary>
|
---|
59 | /// Gets the bounds of the text.
|
---|
60 | /// </summary>
|
---|
61 | public Rectangle TextArea {
|
---|
62 | get {
|
---|
63 | return textRectangle;
|
---|
64 | }
|
---|
65 | }
|
---|
66 |
|
---|
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 | }
|
---|
78 |
|
---|
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
|
---|
91 |
|
---|
92 | #region Constructor
|
---|
93 | public IconLabelMaterial(string text)
|
---|
94 | : base() {
|
---|
95 | mText = text;
|
---|
96 | }
|
---|
97 |
|
---|
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 | }
|
---|
107 |
|
---|
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 | }
|
---|
117 |
|
---|
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 {
|
---|
131 |
|
---|
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 | }
|
---|
144 |
|
---|
145 | #endregion
|
---|
146 |
|
---|
147 | #region Methods
|
---|
148 |
|
---|
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);
|
---|
158 |
|
---|
159 | if (mText != String.Empty) {
|
---|
160 | minSizeNeeded = Size.Round(g.MeasureString(
|
---|
161 | this.myTextStyle.GetFormattedText(this.mText),
|
---|
162 | this.myTextStyle.Font));
|
---|
163 | }
|
---|
164 |
|
---|
165 | minSizeNeeded.Width += constTextShift;
|
---|
166 |
|
---|
167 | if (mIcon != null) {
|
---|
168 | minSizeNeeded.Width += mIcon.Width;
|
---|
169 |
|
---|
170 | if (mIcon.Height > minSizeNeeded.Height) {
|
---|
171 | minSizeNeeded.Height = mIcon.Height;
|
---|
172 | }
|
---|
173 | }
|
---|
174 | return minSizeNeeded;
|
---|
175 | }
|
---|
176 |
|
---|
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);
|
---|
184 |
|
---|
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 | }
|
---|
198 |
|
---|
199 | StringFormat stringFormat = myTextStyle.StringFormat;
|
---|
200 | stringFormat.Trimming = StringTrimming.EllipsisWord;
|
---|
201 | stringFormat.FormatFlags = StringFormatFlags.LineLimit;
|
---|
202 |
|
---|
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
|
---|
212 |
|
---|
213 | }
|
---|
214 | }
|
---|