Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.ExtLibs/HeuristicLab.Netron/3.0.2672.12446/Netron.Diagramming.Core-3.0.2672.12446/Diagram elements/Materials/IconLabelMaterial.cs @ 2768

Last change on this file since 2768 was 2768, checked in by mkommend, 14 years ago

added solution folders and sources for the netron library (ticket #867)

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