1 //-------------------------------------------------------------
2 // <copyright company=�Microsoft Corporation�>
3 //   Copyright � Microsoft Corporation. All Rights Reserved.
4 // </copyright>
5 //-------------------------------------------------------------
6 // @owner=alexgor, deliant
7 //=================================================================
8 //  File:		CommonElements.cs
9 //
10 //  Namespace:	DataVisualization.Charting
11 //
12 //	Classes:	CommonElements
13 //
14 //  Purpose:	CommonElements class provides references to common
15 //              chart classes like DataManager, ChartTypeRegistry,
16 //              ImageLoader and others. It is passed to different
17 //              chart elements to simplify access to those common
18 //              classes.
19 //
20 //	Reviewed:	GS - August 2, 2002
21 //				AG - August 8, 2002
22 //              AG - Microsoft 15, 2007
23 //
24 //===================================================================
25 
26 #region Used namespaces
27 
28 using System;
29 using System.ComponentModel.Design;
30 using System.Drawing.Drawing2D;
31 using System.Drawing;
32 using System.Globalization;
33 
34 #if Microsoft_CONTROL
35 	using System.Windows.Forms.DataVisualization.Charting.Data;
36 	using System.Windows.Forms.DataVisualization.Charting.ChartTypes;
37 	using System.Windows.Forms.DataVisualization.Charting.Utilities;
38 	using System.Windows.Forms.DataVisualization.Charting.Borders3D;
39 	using System.Windows.Forms.DataVisualization.Charting;
40 	using System.Windows.Forms.DataVisualization.Charting.Formulas;
41 #else
42 	using System.Web.UI.DataVisualization.Charting.ChartTypes;
43 	using System.Web.UI.DataVisualization.Charting.Data;
44 	using System.Web.UI.DataVisualization.Charting.Utilities;
45 	using System.Web.UI.DataVisualization.Charting.Borders3D;
46 	using System.Web.UI.DataVisualization.Charting.Formulas;
47 #endif
48 
49 #endregion
50 
51 #if Microsoft_CONTROL
52 	namespace System.Windows.Forms.DataVisualization.Charting
53 #else
54 namespace System.Web.UI.DataVisualization.Charting
55 
56 #endif
57 {
58 	/// <summary>
59     /// CommonElements class provides references to common chart classes like
60     /// DataManager, ChartTypeRegistry, ImageLoader and others. It is passed
61     /// to different chart elements to simplify access to those common classes.
62 	/// </summary>
63 	internal class CommonElements
64 	{
65 		#region Fields
66 
67         private Chart _chart;
68         private ChartImage _chartPicture;
69 
70 		// Reference to Chart Graphics Object
71 		internal ChartGraphics graph = null;
72 
73 		/// <summary>
74 		/// Service Container
75 		/// </summary>
76 		internal IServiceContainer	container = null;
77 
78 		/// <summary>
79 		/// Indicates painting mode
80 		/// </summary>
81 		internal bool processModePaint = true;
82 
83 		/// <summary>
84 		/// Indicates selection mode
85 		/// </summary>
86 		internal bool processModeRegions = false;
87 
88 		// Private Fields
89 		private int _width = 0;
90 		private int _height = 0;
91 
92 		#endregion
93 
94 		#region Properties
95 
96 		/// <summary>
97 		/// Reference to the Data Manager
98 		/// </summary>
99 		internal DataManager DataManager
100 		{
101 			get
102 			{
103 				return (DataManager)container.GetService(typeof(DataManager));
104 			}
105 		}
106 
107 		/// <summary>
108 		/// True if painting mode is active
109 		/// </summary>
110 		public bool ProcessModePaint
111 		{
112 			get
113 			{
114 				return processModePaint;
115 			}
116 		}
117 
118 		/// <summary>
119 		/// True if Hot region or image maps mode is active
120 		/// </summary>
121 		public bool ProcessModeRegions
122 		{
123 			get
124 			{
125 				return processModeRegions;
126 			}
127 		}
128 
129 		/// <summary>
130 		/// Reference to the hot regions object
131 		/// </summary>
132 		public HotRegionsList HotRegionsList
133 		{
134 			get
135 			{
136 				return ChartPicture.hotRegionsList;
137 			}
138 		}
139 
140 		/// <summary>
141 		/// Reference to the Data Manipulator
142 		/// </summary>
143 		public DataManipulator DataManipulator
144 		{
145 			get
146 			{
147 				return ChartPicture.DataManipulator;
148 			}
149 		}
150 
151 		/// <summary>
152 		/// Reference to the ImageLoader
153 		/// </summary>
154 		internal ImageLoader ImageLoader
155 		{
156 			get
157 			{
158 				return (ImageLoader)container.GetService(typeof(ImageLoader));
159 			}
160 		}
161 
162 		/// <summary>
163 		/// Reference to the Chart
164 		/// </summary>
165 		internal Chart Chart
166 		{
167 			get
168 			{
169 				if (_chart==null)
170                     _chart = (Chart)container.GetService(typeof(Chart));
171                 return _chart;
172 			}
173 		}
174 
175 		/// <summary>
176 		/// Reference to the ChartTypeRegistry
177 		/// </summary>
178 		internal ChartTypeRegistry ChartTypeRegistry
179 		{
180 			get
181 			{
182 				return (ChartTypeRegistry)container.GetService(typeof(ChartTypeRegistry));
183 			}
184 		}
185 
186 		/// <summary>
187 		/// Reference to the BorderTypeRegistry
188 		/// </summary>
189 		internal BorderTypeRegistry BorderTypeRegistry
190 		{
191 			get
192 			{
193 				return (BorderTypeRegistry)container.GetService(typeof(BorderTypeRegistry));
194 			}
195 		}
196 
197 		/// <summary>
198 		/// Reference to the FormulaRegistry
199 		/// </summary>
200 		internal FormulaRegistry FormulaRegistry
201 		{
202 			get
203 			{
204 				return (FormulaRegistry)container.GetService(typeof(FormulaRegistry));
205 			}
206 		}
207 
208 
209 
210 		/// <summary>
211 		/// Reference to the ChartPicture
212 		/// </summary>
213 		internal ChartImage ChartPicture
214 		{
215 			get
216 			{
217 				if (_chartPicture ==null)
218                     _chartPicture = (ChartImage)container.GetService(typeof(ChartImage));
219                 return _chartPicture;
220 			}
221 		}
222 
223 		/// <summary>
224 		/// Width of the chart picture
225 		/// </summary>
226 		internal int Width
227 		{
228 			get
229 			{
230 				return _width;
231 			}
232 			set
233 			{
234 				_width = value;
235 			}
236 		}
237 
238 		/// <summary>
239 		/// Height of the chart picture
240 		/// </summary>
241 		internal int Height
242 		{
243 			get
244 			{
245 				return _height;
246 			}
247 			set
248 			{
249 				_height = value;
250 			}
251 		}
252 
253 		#endregion
254 
255 		#region Methods
256 
257 		/// <summary>
258 		/// Constructor
259 		/// </summary>
260 		/// <param name="container">Service container.</param>
CommonElements(IServiceContainer container)261         internal CommonElements(IServiceContainer container)
262 		{
263 			this.container = container;
264 		}
265 
266 
267 		#endregion
268 
269 		#region String convertion helper methods
270 
271         /// <summary>
272 		/// Converts string to double.
273 		/// </summary>
274 		/// <param name="stringToParse">String to convert.</param>
275 		/// <returns>Double result.</returns>
ParseDouble(string stringToParse)276         internal static double ParseDouble(string stringToParse)
277         {
278             return ParseDouble(stringToParse, false);
279         }
280         /// <summary>
281         /// Converts string to double.
282         /// </summary>
283         /// <param name="stringToParse">String to convert.</param>
284         /// <param name="throwException">if set to <c>true</c> the exception thrown.</param>
285         /// <returns>Double result.</returns>
286         [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA1806:DoNotIgnoreMethodResults", MessageId = "System.Double.TryParse(System.String,System.Globalization.NumberStyles,System.IFormatProvider,System.Double@)")]
ParseDouble(string stringToParse, bool throwException)287         internal static double ParseDouble(string stringToParse, bool throwException)
288         {
289             Double result = 0.0;
290 
291             if (throwException)
292             {
293                 result = double.Parse(stringToParse, NumberStyles.Any, CultureInfo.InvariantCulture);
294             }
295             else
296             {
297                 bool parseSucceed = double.TryParse(stringToParse, NumberStyles.Any, CultureInfo.InvariantCulture, out result);
298                 if (!parseSucceed)
299                 {
300                     double.TryParse(stringToParse, NumberStyles.Any, CultureInfo.CurrentCulture, out result);
301                 }
302             }
303             return result;
304         }
305 
306 		/// <summary>
307 		/// Converts string to double.
308 		/// </summary>
309 		/// <param name="stringToParse">String to convert.</param>
310 		/// <returns>Double result.</returns>
311         [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA1806:DoNotIgnoreMethodResults", MessageId = "System.Single.TryParse(System.String,System.Globalization.NumberStyles,System.IFormatProvider,System.Single@)")]
ParseFloat(string stringToParse)312         internal static float ParseFloat(string stringToParse)
313         {
314             float result = 0f;
315             bool parseSucceed = float.TryParse(stringToParse, NumberStyles.Any, CultureInfo.InvariantCulture, out result);
316 
317             if (!parseSucceed)
318             {
319                 float.TryParse(stringToParse, NumberStyles.Any, CultureInfo.CurrentCulture, out result);
320             }
321 
322             return result;
323         }
324 
325 		#endregion
326 	}
327 }
328