AxesArrayDesigner.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the MIT license.
  3. // See the LICENSE file in the project root for more information.
  4. //
  5. // Purpose: Design-time editors and converters for the Axes array.
  6. //
  7. using System;
  8. using System.Collections;
  9. using System.ComponentModel;
  10. using System.ComponentModel.Design;
  11. using System.ComponentModel.Design.Serialization;
  12. using System.Globalization;
  13. using System.Reflection;
  14. using FastReport.DataVisualization.Charting;
  15. namespace FastReport.Design.DataVisualization.Charting
  16. {
  17. #if DESIGNER
  18. /// <summary>
  19. /// Designer editor for the chart areas collection.
  20. /// </summary>
  21. internal class AxesArrayEditor : ArrayEditor
  22. {
  23. #region Fields and Constructor
  24. // Collection form
  25. CollectionForm _form = null;
  26. // Help topic string
  27. private string _helpTopic = "";
  28. /// <summary>
  29. /// Object constructor.
  30. /// </summary>
  31. public AxesArrayEditor() : base(typeof(Axis[]))
  32. {
  33. }
  34. #endregion
  35. #region Methods
  36. /// <summary>
  37. /// Items can not be removed.
  38. /// </summary>
  39. /// <param name="value">Value.</param>
  40. /// <returns>False if can't remove.</returns>
  41. protected override bool CanRemoveInstance(object value)
  42. {
  43. return false;
  44. }
  45. /// <summary>
  46. /// Ovveride the HelpTopic property to provide different topics,
  47. /// depending on selected property.
  48. /// </summary>
  49. protected override string HelpTopic
  50. {
  51. get
  52. {
  53. return (_helpTopic.Length == 0) ? base.HelpTopic : _helpTopic;
  54. }
  55. }
  56. /// <summary>
  57. /// Returns the collection form property grid. Added for VS2005 compatibility.
  58. /// </summary>
  59. /// <param name="controls"></param>
  60. /// <returns></returns>
  61. private PropertyGrid GetPropertyGrid(System.Windows.Forms.Control.ControlCollection controls)
  62. {
  63. foreach (System.Windows.Forms.Control control in controls)
  64. {
  65. PropertyGrid grid = control as PropertyGrid;
  66. if (grid != null)
  67. {
  68. return grid;
  69. }
  70. if (control.Controls.Count > 0)
  71. {
  72. grid = GetPropertyGrid(control.Controls);
  73. if (grid != null)
  74. {
  75. return grid;
  76. }
  77. }
  78. }
  79. return null;
  80. }
  81. /// <summary>
  82. /// Collect the collection editor form buttons into array. Added for VS2005 compatibility.
  83. /// </summary>
  84. /// <param name="buttons"></param>
  85. /// <param name="controls"></param>
  86. private void CollectButtons(ArrayList buttons, System.Windows.Forms.Control.ControlCollection controls)
  87. {
  88. foreach (System.Windows.Forms.Control control in controls)
  89. {
  90. if (control is System.Windows.Forms.Button)
  91. {
  92. buttons.Add(control);
  93. }
  94. if (control.Controls.Count > 0)
  95. {
  96. CollectButtons(buttons, control.Controls);
  97. }
  98. }
  99. }
  100. /// <summary>
  101. /// Displaying help for the curently selected item in the property grid
  102. /// </summary>
  103. protected override void ShowHelp()
  104. {
  105. // Init topic name
  106. _helpTopic = "";
  107. PropertyGrid grid = this.GetPropertyGrid(_form.Controls);;
  108. // Check currently selected grid item
  109. if(grid != null)
  110. {
  111. GridItem item = grid.SelectedGridItem;
  112. if(item != null && (item.GridItemType == GridItemType.Property || item.GridItemType == GridItemType.ArrayValue))
  113. {
  114. _helpTopic = item.PropertyDescriptor.ComponentType.ToString() + "." + item.PropertyDescriptor.Name;
  115. }
  116. }
  117. // Call base class
  118. base.ShowHelp();
  119. // Re-Init topic name
  120. _helpTopic = "";
  121. }
  122. /// <summary>
  123. /// Creates editor's form.
  124. /// </summary>
  125. /// <returns>Collection form.</returns>
  126. protected override CollectionForm CreateCollectionForm()
  127. {
  128. // Create collection form using the base class
  129. _form = base.CreateCollectionForm();
  130. // Changed Apr 29, DT, for VS2005 compatibility
  131. PropertyGrid grid = GetPropertyGrid(_form.Controls);
  132. if (grid != null)
  133. {
  134. // Show properties help
  135. grid.HelpVisible = true;
  136. grid.CommandsVisibleIfAvailable = true;
  137. }
  138. // Changed Apr 29, DT, for VS2005 compatibility
  139. ArrayList buttons = new ArrayList();
  140. this.CollectButtons(buttons, _form.Controls);
  141. foreach (System.Windows.Forms.Button button in buttons)
  142. {
  143. if (button.Name.StartsWith("add", StringComparison.OrdinalIgnoreCase) ||
  144. button.Name.StartsWith("remove", StringComparison.OrdinalIgnoreCase) ||
  145. button.Text.Length == 0)
  146. {
  147. button.Enabled = false;
  148. button.EnabledChanged += new EventHandler(Button_EnabledChanged);
  149. }
  150. }
  151. return _form;
  152. }
  153. /// <summary>
  154. /// Flag to prevent stack overflow.
  155. /// </summary>
  156. private bool _button_EnabledChanging = false;
  157. /// <summary>
  158. /// Handles the EnabledChanged event of the Button control.
  159. /// </summary>
  160. /// <param name="sender">The source of the event.</param>
  161. /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
  162. private void Button_EnabledChanged(object sender, EventArgs e)
  163. {
  164. if ( _button_EnabledChanging ) return;
  165. _button_EnabledChanging = true;
  166. try
  167. {
  168. ((System.Windows.Forms.Button)sender).Enabled = false;
  169. }
  170. finally
  171. {
  172. _button_EnabledChanging = false;
  173. }
  174. }
  175. #endregion
  176. }
  177. #endif
  178. internal class DataPointCustomPropertiesConverter : TypeConverter
  179. {
  180. /// <summary>
  181. /// Returns whether this object supports properties, using the specified context.
  182. /// </summary>
  183. /// <param name="context">An <see cref="T:System.ComponentModel.ITypeDescriptorContext"/> that provides a format context.</param>
  184. /// <returns>
  185. /// true if <see cref="M:System.ComponentModel.TypeConverter.GetProperties(System.Object)"/> should be called to find the properties of this object; otherwise, false.
  186. /// </returns>
  187. public override bool GetPropertiesSupported(ITypeDescriptorContext context)
  188. {
  189. return true;
  190. }
  191. /// <summary>
  192. /// Returns a collection of properties for the type of array specified by the value parameter, using the specified context and attributes.
  193. /// </summary>
  194. /// <param name="context">An <see cref="T:System.ComponentModel.ITypeDescriptorContext"/> that provides a format context.</param>
  195. /// <param name="value">An <see cref="T:System.Object"/> that specifies the type of array for which to get properties.</param>
  196. /// <param name="attributes">An array of type <see cref="T:System.Attribute"/> that is used as a filter.</param>
  197. /// <returns>
  198. /// A <see cref="T:System.ComponentModel.PropertyDescriptorCollection"/> with the properties that are exposed for this data type, or null if there are no properties.
  199. /// </returns>
  200. public override PropertyDescriptorCollection GetProperties(ITypeDescriptorContext context, object value, Attribute[] attributes)
  201. {
  202. // Fill collection with properties descriptors
  203. PropertyDescriptorCollection propDescriptors = TypeDescriptor.GetProperties(value, attributes, false);
  204. // Return original collection if not in design mode
  205. if (context != null && context.Instance is ChartElement &&
  206. (context.Instance as ChartElement).Chart != null &&
  207. (context.Instance as ChartElement).Chart.IsDesignMode())
  208. {
  209. // Create new descriptors collection
  210. PropertyDescriptorCollection newPropDescriptors = new PropertyDescriptorCollection(null);
  211. // Loop through all original property descriptors
  212. foreach (PropertyDescriptor propertyDescriptor in propDescriptors)
  213. {
  214. // Change name of "CustomAttributesEx" property to "CustomProperties"
  215. if (propertyDescriptor.Name == "CustomAttributesEx")
  216. {
  217. DynamicPropertyDescriptor dynPropDesc = new DynamicPropertyDescriptor(
  218. propertyDescriptor,
  219. "CustomProperties");
  220. newPropDescriptors.Add(dynPropDesc);
  221. }
  222. else
  223. {
  224. newPropDescriptors.Add(propertyDescriptor);
  225. }
  226. }
  227. return newPropDescriptors;
  228. }
  229. // Return original collection if not in design mode
  230. return propDescriptors;
  231. }
  232. /// <summary>
  233. /// Converts the given value object to the specified type, using the specified context and culture information.
  234. /// </summary>
  235. /// <param name="context">An <see cref="T:System.ComponentModel.ITypeDescriptorContext"/> that provides a format context.</param>
  236. /// <param name="culture">A <see cref="T:System.Globalization.CultureInfo"/>. If null is passed, the current culture is assumed.</param>
  237. /// <param name="value">The <see cref="T:System.Object"/> to convert.</param>
  238. /// <param name="destinationType">The <see cref="T:System.Type"/> to convert the <paramref name="value"/> parameter to.</param>
  239. /// <returns>
  240. /// An <see cref="T:System.Object"/> that represents the converted value.
  241. /// </returns>
  242. /// <exception cref="T:System.ArgumentNullException">The <paramref name="destinationType"/> parameter is null. </exception>
  243. /// <exception cref="T:System.NotSupportedException">The conversion cannot be performed. </exception>
  244. public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
  245. {
  246. if (context != null)
  247. {
  248. if (destinationType == typeof(string))
  249. {
  250. return "";
  251. }
  252. } // Always call base, even if you can't convert.
  253. return base.ConvertTo(context, culture, value, destinationType);
  254. }
  255. }
  256. /// <summary>
  257. /// DataPoint Converter - helps windows form serializer to create inline datapoints.
  258. /// </summary>
  259. internal class DataPointConverter : DataPointCustomPropertiesConverter
  260. {
  261. /// <summary>
  262. /// This method overrides CanConvertTo from TypeConverter. This is called when someone
  263. /// wants to convert an instance of object to another type. Here,
  264. /// only conversion to an InstanceDescriptor is supported.
  265. /// </summary>
  266. /// <param name="context">Descriptor context.</param>
  267. /// <param name="destinationType">Destination type.</param>
  268. /// <returns>True if object can be converted.</returns>
  269. public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
  270. {
  271. if (destinationType == typeof(InstanceDescriptor))
  272. {
  273. return true;
  274. }
  275. // Always call the base to see if it can perform the conversion.
  276. return base.CanConvertTo(context, destinationType);
  277. }
  278. /// <summary>
  279. /// This methods performs the actual conversion from an object to an InstanceDescriptor.
  280. /// </summary>
  281. /// <param name="context">Descriptor context.</param>
  282. /// <param name="culture">Culture information.</param>
  283. /// <param name="value">Object value.</param>
  284. /// <param name="destinationType">Destination type.</param>
  285. /// <returns>Converted object.</returns>
  286. public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
  287. {
  288. DataPoint dataPoint = value as DataPoint;
  289. if (destinationType == typeof(InstanceDescriptor) && dataPoint != null)
  290. {
  291. if (dataPoint.YValues.Length > 1)
  292. {
  293. ConstructorInfo ci = typeof(DataPoint).GetConstructor(new Type[] { typeof(double), typeof(string) });
  294. string yValues = "";
  295. foreach (double y in dataPoint.YValues)
  296. {
  297. yValues += y.ToString(System.Globalization.CultureInfo.InvariantCulture) + ",";
  298. }
  299. return new InstanceDescriptor(ci, new object[] { dataPoint.XValue, yValues.TrimEnd(',') }, false);
  300. }
  301. else
  302. {
  303. ConstructorInfo ci = typeof(DataPoint).GetConstructor(new Type[] { typeof(double), typeof(double) });
  304. return new InstanceDescriptor(ci, new object[] { dataPoint.XValue, dataPoint.YValues[0] }, false);
  305. }
  306. }
  307. // Always call base, even if you can't convert.
  308. return base.ConvertTo(context, culture, value, destinationType);
  309. }
  310. }
  311. }