Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Problems.GaussianProcessTuning/ILNumerics.2.14.4735.573/Drawing/Labeling/ILLabelingElement.cs @ 12448

Last change on this file since 12448 was 9102, checked in by gkronber, 12 years ago

#1967: ILNumerics source for experimentation

File size: 12.6 KB
Line 
1///
2///    This file is part of ILNumerics Community Edition.
3///
4///    ILNumerics Community Edition - high performance computing for applications.
5///    Copyright (C) 2006 - 2012 Haymo Kutschbach, http://ilnumerics.net
6///
7///    ILNumerics Community Edition is free software: you can redistribute it and/or modify
8///    it under the terms of the GNU General Public License version 3 as published by
9///    the Free Software Foundation.
10///
11///    ILNumerics Community Edition is distributed in the hope that it will be useful,
12///    but WITHOUT ANY WARRANTY; without even the implied warranty of
13///    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14///    GNU General Public License for more details.
15///
16///    You should have received a copy of the GNU General Public License
17///    along with ILNumerics Community Edition. See the file License.txt in the root
18///    of your distribution package. If not, see <http://www.gnu.org/licenses/>.
19///
20///    In addition this software uses the following components and/or licenses:
21///
22///    =================================================================================
23///    The Open Toolkit Library License
24///   
25///    Copyright (c) 2006 - 2009 the Open Toolkit library.
26///   
27///    Permission is hereby granted, free of charge, to any person obtaining a copy
28///    of this software and associated documentation files (the "Software"), to deal
29///    in the Software without restriction, including without limitation the rights to
30///    use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
31///    the Software, and to permit persons to whom the Software is furnished to do
32///    so, subject to the following conditions:
33///
34///    The above copyright notice and this permission notice shall be included in all
35///    copies or substantial portions of the Software.
36///
37///    =================================================================================
38///   
39
40using System;
41using System.Drawing;
42using System.Collections.Generic;
43using System.Text;
44using ILNumerics.Drawing.Interfaces;
45using ILNumerics.Drawing;
46
47namespace ILNumerics.Drawing.Labeling {
48    /// <summary>
49    /// Base class for text elements
50    /// </summary>
51    public abstract class ILLabelingElement {
52
53#region event handler
54
55        /// <summary>
56        /// fires when an element's property has changed
57        /// </summary>
58        public event EventHandler Changed;
59       
60        protected virtual void OnChanged() {
61            if (Changed != null)
62                Changed(this,null);
63        }
64
65#endregion
66
67#region attributes
68
69        protected String m_expression;
70        protected String m_cachedExpression;
71        protected Color m_color;
72        protected Font m_font;
73        protected Size m_size;
74        internal PointF m_anchor;
75        protected TextOrientation m_orientation;
76        protected IILTextInterpreter m_interpreter;
77        protected IILTextRenderer m_renderer;
78        protected ILRenderQueue m_renderQueue;
79
80#endregion
81
82#region properties
83
84        /// <summary>
85        /// Size of the label for rendering, regardless of orientation
86        /// </summary>
87        public virtual Size Size {
88            get {
89                if (m_cachedExpression != m_expression) {
90                    interprete(m_expression);
91                }
92                return m_size;
93            }
94        }
95        /// <summary>
96        /// color used to display the element
97        /// </summary>
98        public virtual Color Color {
99            get { return m_color; }
100            set {
101                m_color = value;
102                OnChanged();
103            }
104        }
105        /// <summary>
106        /// Get/set system font used to draw the text
107        /// </summary>
108        public virtual Font Font {
109            get { return m_font; }
110            set {
111                if (m_font != null &&
112                    (m_font.Name != value.Name ||
113                    m_font.Style != value.Style ||
114                    m_font.Size != value.Size)) {
115                    m_font = value;
116                    interprete(m_expression); 
117                    OnChanged();
118                }
119            }
120        }
121        /// <summary>
122        /// Get the orientation for the labeling element or sets it
123        /// </summary>
124        public virtual TextOrientation Orientation {
125            get { return m_orientation; }
126            set {
127                m_orientation = value;
128                OnChanged();
129            }
130        }
131        /// <summary>
132        /// Get/set the relative offset of the 'Position' point rel. to the overall label size, range 0..1 for X and Y
133        /// </summary>
134        public PointF Anchor {
135            get {
136                return m_anchor;
137            }
138            set {
139                m_anchor = value;
140                OnChanged();
141            }
142        }
143        /// <summary>
144        /// Text interpreter used to transform labeling source into bitmap
145        /// </summary>
146        public IILTextInterpreter Interpreter {
147            get { return m_interpreter; }
148            set {
149                if (value != null) {
150                    m_interpreter = value;
151                    OnChanged();
152                }
153            }
154        }
155        /// <summary>
156        /// device dependent renderer, used to draw the labeling bitmap onto panel
157        /// </summary>
158        public IILTextRenderer Renderer {
159            get { return m_renderer; }
160            set {
161                if (value != null) {
162                    m_renderer = value;
163                    OnChanged();
164                }
165            }
166        }
167        /// <summary>
168        /// Get / set the text expression defining the content to be displayed
169        /// </summary>
170        /// <remarks><para>The expression may contain markups according to
171        /// the specific interpreter instance.</para></remarks>
172        public string Text {
173            get {
174                return m_expression;
175            }
176            set {
177                if (value != m_expression) {
178                    m_expression = value;
179                    // (clean up, interpret & cache: when drawing)
180                    OnChanged();
181                }
182            }
183        }
184
185#endregion
186
187#region constructor
188        /// <summary>
189        /// [abstract] Create a new labeling element
190        /// </summary>
191        /// <param name="panel">panel hosting the element</param>
192        /// <param name="font">default font for the element</param>
193        /// <param name="color">default color for the element</param>
194        public ILLabelingElement(ILPanel panel, Font font, Color color)
195            : this(panel,font,color,CoordSystem.Screen) {
196        }
197
198        /// <summary>
199        /// [abstract] Create a new labeling element
200        /// </summary>
201        /// <param name="panel">panel hosting the element</param>
202        /// <param name="font">default font for the element</param>
203        /// <param name="color">default color for the element</param>
204        /// <param name="coordSystem">world / screen rendering method</param>
205        public ILLabelingElement (ILPanel panel, Font font, Color color, CoordSystem coordSystem) {
206            if (font != null) {
207                m_font = font;
208            } else {
209                m_font = new System.Drawing.Font(
210                    System.Drawing.FontFamily.GenericSansSerif,10.0f);
211            }
212            m_color = color;
213            m_orientation = TextOrientation.Horizontal;
214            m_renderQueue = null;
215            m_size = Size.Empty;
216            m_expression = string.Empty;
217            // init interpreter & renderer
218            m_renderer = panel.TextRendererManager.GetDefault(coordSystem);
219            m_renderer.CacheCleared += new EventHandler(m_renderer_CacheCleared);
220            m_interpreter = new ILSimpleTexInterpreter();
221        }
222
223#endregion
224
225#region public methods
226
227        /// <summary>
228        /// Dispose off this element's ressources
229        /// </summary>
230        internal void Dispose() {
231            if (m_renderer != null) {
232                m_renderer.CacheCleared -= m_renderer_CacheCleared;
233                if (m_renderer is IDisposable) {
234                    (m_renderer as IDisposable).Dispose();
235                }
236                m_renderer = null;
237            }
238        }
239        /// <summary>
240        /// draws the whole rendering queue
241        /// </summary>
242        public virtual void Draw(ILRenderProperties p) {
243            if (m_expression != m_cachedExpression)
244                interprete(m_expression);
245            m_renderer.Begin(p);
246            m_renderer.Draw(m_renderQueue,offsetAlignment(m_renderQueue.Size),m_orientation,m_color);
247            m_renderer.End(p);
248        }
249
250#endregion
251
252#region helper methods
253       
254        /// <summary>
255        /// if the renderer cleares its cache, the expression needs to be re-interpreted
256        /// </summary>
257        /// <param name="sender"></param>
258        /// <param name="e"></param>
259        protected void m_renderer_CacheCleared(object sender, EventArgs e) {
260            m_cachedExpression = "";
261        }
262        /// <summary>
263        /// add offset to an existing point according to internal alignment
264        /// </summary>
265        /// <param name="size">size of content to be aligned</param>
266        /// <param name="point">original point</param>
267        protected void offsetAlignment(Size size, ref Point point) {
268            if (m_orientation == TextOrientation.Vertical) {
269                point.X = point.X + (int)(m_anchor.Y * size.Height);
270                point.Y = point.Y - (int)(m_anchor.X * size.Width);
271                //if (0 != (TickLabelAlign.right & m_alignment)) {
272                //    // point.X = point.X;
273                //} else if (0 != (TickLabelAlign.center & m_alignment)) {
274                //    point.X = point.X - size.Height / 2;
275                //} else
276                //    point.X = point.X + size.Height;
277                //if (0 != (TickLabelAlign.bottom & m_alignment)) {
278                //    point.Y = point.Y - size.Width;     
279                //} else if (0 != (TickLabelAlign.vertCenter & m_alignment)) {
280                //    point.Y = point.Y - size.Width / 2;
281                //} //else point.Y = point.Y;
282            } else {
283                point.X = point.X - (int)(m_anchor.X * size.Width);
284                point.Y = point.Y - (int)(m_anchor.Y * size.Height);
285                //if (0 != (TickLabelAlign.right & m_alignment)) {
286                //    point.X = point.X - size.Width;     
287                //} else if (0 != (TickLabelAlign.center & m_alignment)) {
288                //    point.X = point.X - size.Width / 2;
289                //} // else point.X = point.X;
290                //if (0 != (TickLabelAlign.bottom & m_alignment)) {
291                //    point.Y = point.Y - size.Height;     
292                //} else if (0 != (TickLabelAlign.vertCenter & m_alignment)) {
293                //    point.Y = point.Y - size.Height / 2;
294                //} // else point.Y = point.Y;
295            }
296        }
297        /// <summary>
298        /// return offset according to current setting of m_alignment
299        /// </summary>
300        /// <param name="size">size of an element to align</param>
301        /// <returns>offset according to Alignment property</returns>
302        protected Point offsetAlignment(Size size) {
303            Point ret = new Point();
304            offsetAlignment(size, ref ret);
305            return ret;
306        }
307        /// <summary>
308        /// return offset according to current setting of m_alignment
309        /// </summary>
310        /// <param name="size">size of an element to align</param>
311        /// <param name="p">point inside rectangle of size 'size'</param>
312        /// <returns>offset according to Alignment property</returns>
313        protected Point offsetAlignment(Size size, Point p) {
314            offsetAlignment(size, ref p);
315            return p;
316        }
317        /// <summary>
318        /// interprete the expression and cache render queue
319        /// </summary>
320        /// <param name="expression"></param>
321        protected void interprete(string expression) {
322            if (m_renderQueue != null)
323                m_renderQueue.Clear();
324            m_renderQueue =
325                m_interpreter.Transform(expression,m_font,m_color,m_renderer);
326            m_size = m_renderQueue.Size;
327            m_cachedExpression = expression;
328        }
329
330#endregion
331
332    }
333}
Note: See TracBrowser for help on using the repository browser.