PluginCollection.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. using System;
  2. using System.Collections;
  3. namespace FastReport.Design
  4. {
  5. /// <summary>
  6. /// Represents collection of designer plugins.
  7. /// </summary>
  8. /// <remarks>
  9. /// <para>This class is used in the <b>Designer.Plugins</b> property.</para>
  10. /// <para>To register own plugin, add its type to the <see cref="DesignerPlugins"/> global collection:
  11. /// <code>
  12. /// DesignerPlugins.Add(typeof(MyToolbar));
  13. /// </code>
  14. /// </para>
  15. /// </remarks>
  16. public class PluginCollection : CollectionBase
  17. {
  18. private Designer designer;
  19. internal IDesignerPlugin this[int index]
  20. {
  21. get { return List[index] as IDesignerPlugin; }
  22. }
  23. internal IDesignerPlugin Add(Type plugin)
  24. {
  25. foreach (IDesignerPlugin p in this)
  26. {
  27. if (p.GetType() == plugin)
  28. return p;
  29. }
  30. IDesignerPlugin newPlugin = Activator.CreateInstance(plugin, new object[] { designer }) as IDesignerPlugin;
  31. Add(newPlugin);
  32. return newPlugin;
  33. }
  34. internal void Add(IDesignerPlugin plugin)
  35. {
  36. int i = List.IndexOf(plugin);
  37. if (i == -1)
  38. List.Add(plugin);
  39. }
  40. internal void AddRange(Type[] range)
  41. {
  42. if (range == null)
  43. return;
  44. foreach (Type t in range)
  45. {
  46. Add(t);
  47. }
  48. }
  49. internal void AddRange(IDesignerPlugin[] range)
  50. {
  51. if (range == null)
  52. return;
  53. foreach (IDesignerPlugin plugin in range)
  54. {
  55. Add(plugin);
  56. }
  57. }
  58. internal void Remove(Type plugin)
  59. {
  60. for (int i = 0; i < Count; i++)
  61. {
  62. IDesignerPlugin p = this[i];
  63. if (p.GetType() == plugin)
  64. {
  65. Remove(p);
  66. return;
  67. }
  68. }
  69. }
  70. internal void Remove(IDesignerPlugin plugin)
  71. {
  72. List.Remove(plugin);
  73. plugin.SaveState();
  74. if (plugin is IDisposable)
  75. (plugin as IDisposable).Dispose();
  76. }
  77. internal void RemoveRange(Type[] range)
  78. {
  79. if (range == null)
  80. return;
  81. foreach (Type t in range)
  82. {
  83. Remove(t);
  84. }
  85. }
  86. internal void SelectionChanged(object sender)
  87. {
  88. foreach (IDesignerPlugin plugin in this)
  89. {
  90. if (plugin != sender)
  91. plugin.SelectionChanged();
  92. }
  93. }
  94. internal void Lock()
  95. {
  96. foreach (IDesignerPlugin plugin in this)
  97. {
  98. plugin.Lock();
  99. }
  100. }
  101. internal void Unlock()
  102. {
  103. foreach (IDesignerPlugin plugin in this)
  104. {
  105. plugin.Unlock();
  106. }
  107. }
  108. internal void Update(object sender)
  109. {
  110. foreach (IDesignerPlugin plugin in this)
  111. {
  112. if (plugin != sender)
  113. plugin.UpdateContent();
  114. }
  115. }
  116. internal void UpdateDpiDependencies(object sender)
  117. {
  118. foreach (IDesignerPlugin plugin in this)
  119. {
  120. if (plugin != sender)
  121. plugin.UpdateDpiDependencies();
  122. }
  123. }
  124. internal void Localize()
  125. {
  126. foreach (IDesignerPlugin plugin in this)
  127. {
  128. plugin.Localize();
  129. }
  130. }
  131. internal void UpdateUIStyle()
  132. {
  133. foreach (IDesignerPlugin plugin in this)
  134. {
  135. plugin.UpdateUIStyle();
  136. }
  137. }
  138. /// <summary>
  139. /// Finds a plugin by its name.
  140. /// </summary>
  141. /// <param name="pluginName">The plugin's name.</param>
  142. /// <returns>The plugin, if found; otherwise, <b>null</b>.</returns>
  143. /// <example>This example shows how to find a plugin.
  144. /// <code>
  145. /// MessagesWindow window = designer.Plugins.Find("MessagesWindow") as MessagesWindow;
  146. /// </code>
  147. /// </example>
  148. public IDesignerPlugin Find(string pluginName)
  149. {
  150. foreach (IDesignerPlugin plugin in this)
  151. {
  152. if (plugin.PluginName == pluginName)
  153. return plugin;
  154. }
  155. return null;
  156. }
  157. /// <summary>
  158. /// Finds a plugin by its type name.
  159. /// </summary>
  160. /// <param name="typeName">The plugin's type name.</param>
  161. /// <returns>The plugin, if found; otherwise, <b>null</b>.</returns>
  162. /// <example>This example shows how to find a plugin.
  163. /// <code>
  164. /// MessagesWindow window = designer.Plugins.FindType("MessagesWindow") as MessagesWindow;
  165. /// </code>
  166. /// </example>
  167. public IDesignerPlugin FindType(string typeName)
  168. {
  169. foreach (IDesignerPlugin plugin in this)
  170. {
  171. if (plugin.GetType().ToString() == typeName)
  172. return plugin;
  173. }
  174. return null;
  175. }
  176. internal void SaveState()
  177. {
  178. foreach (IDesignerPlugin plugin in this)
  179. {
  180. plugin.SaveState();
  181. }
  182. }
  183. internal void RestoreState()
  184. {
  185. foreach (IDesignerPlugin plugin in this)
  186. {
  187. plugin.RestoreState();
  188. }
  189. }
  190. internal PluginCollection(Designer designer)
  191. : base()
  192. {
  193. this.designer = designer;
  194. }
  195. }
  196. }