ToolbarBase.cs 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. using System;
  2. using System.Drawing;
  3. using System.ComponentModel;
  4. using System.Windows.Forms;
  5. using FastReport.Utils;
  6. using FastReport.Forms;
  7. #if !MONO
  8. using FastReport.DevComponents.DotNetBar;
  9. #endif
  10. namespace FastReport.Design.Toolbars
  11. {
  12. /// <summary>
  13. /// Base class for all designer toolbars.
  14. /// </summary>
  15. /// <remarks>
  16. /// Use this class to write own designer's toolbar. To do this:
  17. /// <para>- in the constructor, set the <b>Name</b> property and create toolbar buttons.
  18. /// The <b>Name</b> will be used to restore toolbar's state;</para>
  19. /// <para>- override the <b>SelectionChanged</b> method. This method is called when current selection
  20. /// is changed. In this method, you should update buttons state to reflect the current selection.
  21. /// Selected objects can be accessed via <b>Designer.SelectedObjects</b> property;</para>
  22. /// <para>- override the <b>UpdateContent</b> method. This method is called when the report
  23. /// content was changed. Typically you need to do the same actions in <b>SelectionChanged</b> and
  24. /// <b>UpdateContent</b> methods;</para>
  25. /// <para>- to register a toolbar, add its type to the <see cref="DesignerPlugins"/> global collection:
  26. /// <code>
  27. /// DesignerPlugins.Add(typeof(MyToolbar));
  28. /// </code>
  29. /// </para>
  30. /// </remarks>
  31. [ToolboxItem(false)]
  32. #if !MONO
  33. public class ToolbarBase : Bar, IDesignerPlugin
  34. #else
  35. public class ToolbarBase : ToolStrip, IDesignerPlugin
  36. #endif
  37. {
  38. #region Fields
  39. private Designer designer;
  40. #if !MONO
  41. private CustomizeItem customizeItem;
  42. #endif
  43. #endregion
  44. #region Properties
  45. /// <summary>
  46. /// Gets the report designer.
  47. /// </summary>
  48. public Designer Designer
  49. {
  50. get { return designer; }
  51. }
  52. /// <inheritdoc/>
  53. public string PluginName
  54. {
  55. get { return Name; }
  56. }
  57. #if !MONO
  58. internal CustomizeItem CustomizeItem
  59. {
  60. get { return customizeItem; }
  61. }
  62. #endif
  63. #endregion
  64. #region IDesignerPlugin
  65. /// <inheritdoc/>
  66. public virtual void SaveState()
  67. {
  68. #if MONO
  69. XmlItem xi = Config.Root.FindItem("Designer").FindItem(Name);
  70. xi.SetProp("Visible", Visible ? "1" : "0");
  71. // xi.SetProp("Left", Location.X.ToString());
  72. // xi.SetProp("Top", Location.Y.ToString());
  73. #endif
  74. }
  75. /// <inheritdoc/>
  76. public virtual void RestoreState()
  77. {
  78. #if MONO
  79. XmlItem xi = Config.Root.FindItem("Designer").FindItem(Name);
  80. Visible = xi.GetProp("Visible") != "0";
  81. // string left = xi.GetProp("Left");
  82. // string top = xi.GetProp("Top");
  83. // if (left != "" && top != "")
  84. // Location = new Point(int.Parse(left), int.Parse(top));
  85. #endif
  86. }
  87. /// <inheritdoc/>
  88. public virtual void SelectionChanged()
  89. {
  90. }
  91. /// <inheritdoc/>
  92. public virtual void UpdateContent()
  93. {
  94. }
  95. /// <inheritdoc/>
  96. public void Lock()
  97. {
  98. }
  99. /// <inheritdoc/>
  100. public void Unlock()
  101. {
  102. UpdateContent();
  103. }
  104. /// <inheritdoc/>
  105. public virtual void Localize()
  106. {
  107. #if !MONO
  108. customizeItem.Text = Res.Get("Designer,Toolbar,AddOrRemove");
  109. #endif
  110. }
  111. /// <inheritdoc/>
  112. public virtual DesignerOptionsPage GetOptionsPage()
  113. {
  114. return null;
  115. }
  116. /// <inheritdoc/>
  117. public virtual void UpdateUIStyle()
  118. {
  119. #if !MONO
  120. Style = UIStyleUtils.GetDotNetBarStyle(Designer.UIStyle);
  121. #else
  122. Renderer = UIStyleUtils.GetToolStripRenderer(Designer.UIStyle);
  123. #endif
  124. }
  125. /// <inheritdoc/>
  126. public new virtual void UpdateDpiDependencies()
  127. {
  128. SuspendLayout();
  129. Font = Designer.LogicalToDevice(DrawUtils.DefaultFont);
  130. #if !MONO
  131. base.UpdateDpiDependencies();
  132. foreach (BaseItem item in Items)
  133. {
  134. ButtonItem b = item as ButtonItem;
  135. if (b != null && b.ImageIndex != -1)
  136. {
  137. // looks like this is the only way to completely refresh the image displayed (including disabled images).
  138. b.Image = Designer.GetImage(b.ImageIndex);
  139. }
  140. }
  141. #endif
  142. ResumeLayout();
  143. }
  144. #endregion
  145. #region Public Methods
  146. #if !MONO
  147. /// <summary>
  148. /// Creates a new button.
  149. /// </summary>
  150. /// <param name="name">Button's name.</param>
  151. /// <param name="imageIndex">Button's image index.</param>
  152. /// <param name="click">Click handler.</param>
  153. /// <returns>New button.</returns>
  154. public ButtonItem CreateButton(string name, int imageIndex, EventHandler click)
  155. {
  156. return CreateButton(name, imageIndex, "", click);
  157. }
  158. /// <summary>
  159. /// Creates a new button.
  160. /// </summary>
  161. /// <param name="name">Button's name.</param>
  162. /// <param name="imageIndex">Button's image index.</param>
  163. /// <param name="tooltip">Button's tooltip text.</param>
  164. /// <param name="click">Click handler.</param>
  165. /// <returns>New button.</returns>
  166. public ButtonItem CreateButton(string name, int imageIndex, string tooltip, EventHandler click)
  167. {
  168. ButtonItem item = new ButtonItem();
  169. item.Name = name;
  170. item.ImageIndex = imageIndex;
  171. item.Tooltip = tooltip;
  172. if (click != null)
  173. item.Click += click;
  174. return item;
  175. }
  176. internal void SetItemText(BaseItem item, string text)
  177. {
  178. item.Tooltip = text;
  179. item.Text = text;
  180. }
  181. #else
  182. /// <summary>
  183. /// Creates a new button.
  184. /// </summary>
  185. /// <param name="name">Button's name.</param>
  186. /// <param name="imageIndex">Button's image index.</param>
  187. /// <param name="click">Click handler.</param>
  188. /// <returns>New button.</returns>
  189. public ToolStripButton CreateButton(string name, int imageIndex, EventHandler click)
  190. {
  191. ToolStripButton item = new ToolStripButton();
  192. item.Name = name;
  193. item.Image = this.GetImage(imageIndex);
  194. item.DisplayStyle = ToolStripItemDisplayStyle.Image;
  195. item.AutoSize = false;
  196. item.Size = new Size(23, 22);
  197. if (click != null)
  198. item.Click += click;
  199. return item;
  200. }
  201. /// <summary>
  202. /// Creates a new split button.
  203. /// </summary>
  204. /// <param name="name">Button's name.</param>
  205. /// <param name="imageIndex">Button's image index.</param>
  206. /// <param name="click">Click handler.</param>
  207. /// <returns>New split button.</returns>
  208. public ToolStripSplitButton CreateSplitButton(string name, int imageIndex, EventHandler click)
  209. {
  210. ToolStripSplitButton item = new ToolStripSplitButton();
  211. item.Name = name;
  212. item.Image = this.GetImage(imageIndex);
  213. item.DisplayStyle = ToolStripItemDisplayStyle.Image;
  214. if (click != null)
  215. item.Click += click;
  216. return item;
  217. }
  218. internal void SetItemText(ToolStripItem item, string text)
  219. {
  220. item.ToolTipText = text;
  221. item.Text = text;
  222. }
  223. #endif
  224. #endregion
  225. /// <summary>
  226. /// Initializes a new instance of the <see cref="ToolbarBase"/> class with default settings.
  227. /// </summary>
  228. /// <param name="designer">The report designer.</param>
  229. /// <remarks>
  230. /// You don't need to call this constructor. The designer will do this automatically.
  231. /// </remarks>
  232. public ToolbarBase(Designer designer)
  233. : base()
  234. {
  235. this.designer = designer;
  236. #if !MONO
  237. GrabHandleStyle = eGrabHandleStyle.Office2003;
  238. customizeItem = new CustomizeItem();
  239. customizeItem.CustomizeItemVisible = false;
  240. #else
  241. Dock = DockStyle.None;
  242. #endif
  243. }
  244. }
  245. }