ToolWindowBase.cs 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. using FastReport.Controls;
  2. using FastReport.DevComponents.DotNetBar;
  3. using FastReport.Forms;
  4. using FastReport.Utils;
  5. using System;
  6. using System.Windows.Forms;
  7. namespace FastReport.Design.ToolWindows
  8. {
  9. /// <summary>
  10. /// Base class for all tool windows such as "Properties", "Data Dictionary" etc.
  11. /// </summary>
  12. /// <remarks>
  13. /// <para>Use this class to create own tool window. To do this:</para>
  14. /// <para>- in the constructor, set the <b>Name</b> and <b>Image</b> properties and create necessary controls.
  15. /// The <b>Name</b> will be used to restore window's state;</para>
  16. /// <para>- override the <b>SelectionChanged</b> method. This method is called when current selection
  17. /// is changed. In this method, you should update buttons state to reflect the current selection.
  18. /// Selected objects can be accessed via <b>Designer.SelectedObjects</b> property;</para>
  19. /// <para>- override the <b>UpdateContent</b> method. This method is called when the report
  20. /// content was changed. Typically you need to do the same actions in <b>SelectionChanged</b> and
  21. /// <b>UpdateContent</b> methods;</para>
  22. /// <para>- to register a toolwindow, add its type to the <see cref="DesignerPlugins"/> global collection:
  23. /// <code>
  24. /// DesignerPlugins.Add(typeof(MyToolWindow));
  25. /// </code>
  26. /// </para>
  27. /// </remarks>
  28. public class ToolWindowBase : DockContainerItem, IDesignerPlugin
  29. {
  30. #region Properties
  31. /// <summary>
  32. /// Gets the report designer.
  33. /// </summary>
  34. public Designer Designer { get; }
  35. /// <summary>
  36. /// Gets a value indicating that window is locked.
  37. /// </summary>
  38. public bool Locked { get; private set; }
  39. /// <inheritdoc/>
  40. public string PluginName => Name;
  41. /// <summary>
  42. /// Gets or sets shortcut keys used to show this toolwindow.
  43. /// </summary>
  44. public eShortcut Shortcut { get; set; }
  45. /// <summary>
  46. /// Gets or sets a value indicating that the toolwindow can be closed by the x button.
  47. /// </summary>
  48. public bool CanHide
  49. {
  50. get => Bar.CanHide;
  51. set => Bar.CanHide = value;
  52. }
  53. internal Bar Bar
  54. {
  55. get
  56. {
  57. BaseItem item = this;
  58. while (item.Parent != null)
  59. item = item.Parent;
  60. return item.ContainerControl as Bar;
  61. }
  62. }
  63. /// <summary>
  64. /// Gets the control collection.
  65. /// </summary>
  66. public Control.ControlCollection Controls => Control.Controls;
  67. private ControlStorageService storage;
  68. public ControlStorageService Storage => storage ?? new ControlStorageService(Control, "Designer," + Name);
  69. #endregion
  70. #region Private Methods
  71. private Bar CreateBar()
  72. {
  73. Bar bar = new Bar();
  74. bar.Name = Name + "Bar";
  75. bar.CanHide = true;
  76. bar.CloseSingleTab = true;
  77. bar.GrabHandleStyle = eGrabHandleStyle.Caption;
  78. bar.LayoutType = eLayoutType.DockContainer;
  79. bar.Stretch = true;
  80. bar.AutoSyncBarCaption = true;
  81. DockTo(bar);
  82. return bar;
  83. }
  84. #endregion
  85. #region Public Methods
  86. internal void DoDefaultDock()
  87. {
  88. DockTo(Designer.DotNetBarManager.RightDockSite, Designer.DataWindow, eDockSide.Top);
  89. }
  90. internal void DockTo(DockSite site)
  91. {
  92. site.GetDocumentUIManager().Dock(CreateBar());
  93. }
  94. internal void DockTo(DockSite site, ToolWindowBase referenceWindow, eDockSide side)
  95. {
  96. site.GetDocumentUIManager().Dock(referenceWindow.Bar, CreateBar(), side);
  97. }
  98. internal void DockTo(ToolWindowBase win)
  99. {
  100. DockTo(win.Bar);
  101. }
  102. internal void DockTo(Bar bar)
  103. {
  104. bar.Controls.Add(Control);
  105. bar.Items.Add(this);
  106. }
  107. /// <summary>
  108. /// Shows the toolwindow.
  109. /// </summary>
  110. public void Show()
  111. {
  112. // force SetDockContainerVisible to do the work
  113. Visible = false;
  114. BarUtilities.SetDockContainerVisible(this, true);
  115. Activate();
  116. }
  117. /// <summary>
  118. /// Hides the toolwindow.
  119. /// </summary>
  120. public void Hide()
  121. {
  122. BarUtilities.SetDockContainerVisible(this, false);
  123. }
  124. internal void Close()
  125. {
  126. Bar bar = Bar;
  127. if (bar != null)
  128. bar.CloseDockTab(this);
  129. }
  130. internal void Activate()
  131. {
  132. Bar bar = Bar;
  133. if (bar != null)
  134. {
  135. bar.SelectedDockContainerItem = this;
  136. if (bar.AutoHide)
  137. bar.AutoHide = false;
  138. }
  139. }
  140. internal ToolbarButton AddButton(int imageIndex, EventHandler click) => AddButton(imageIndex, false, click);
  141. internal ToolbarButton AddButton(int imageIndex, bool imageAndText, EventHandler click)
  142. {
  143. var button = new ToolbarButton("", imageIndex, click);
  144. if (imageAndText)
  145. button.ButtonStyle = eButtonStyle.ImageAndText;
  146. return button;
  147. }
  148. internal ContextMenuItem AddMenuItem(int imageIndex, EventHandler click)
  149. {
  150. var mi = new ContextMenuItem();
  151. mi.Image = Designer.GetImage(imageIndex);
  152. mi.ImageIndex = imageIndex;
  153. mi.Click += click;
  154. return mi;
  155. }
  156. internal ContextMenuItem AddMenuItem(EventHandler click)
  157. {
  158. return AddMenuItem(-1, click);
  159. }
  160. #endregion
  161. #region IDesignerPlugin
  162. /// <inheritdoc/>
  163. public virtual void SaveState()
  164. {
  165. }
  166. /// <inheritdoc/>
  167. public virtual void RestoreState()
  168. {
  169. }
  170. /// <inheritdoc/>
  171. public virtual void SelectionChanged()
  172. {
  173. }
  174. /// <inheritdoc/>
  175. public virtual void UpdateContent()
  176. {
  177. }
  178. /// <inheritdoc/>
  179. public virtual void Lock()
  180. {
  181. Locked = true;
  182. }
  183. /// <inheritdoc/>
  184. public virtual void Unlock()
  185. {
  186. Locked = false;
  187. UpdateContent();
  188. }
  189. /// <inheritdoc/>
  190. public virtual void Localize()
  191. {
  192. }
  193. /// <summary>
  194. /// Implements <see cref="IDesignerPlugin.GetOptionsPage"/> method.
  195. /// </summary>
  196. /// <returns>The options page, if implemented; otherwise, <b>null</b>.</returns>
  197. public virtual DesignerOptionsPage GetOptionsPage()
  198. {
  199. return null;
  200. }
  201. /// <inheritdoc/>
  202. public virtual void UpdateUIStyle()
  203. {
  204. }
  205. /// <inheritdoc/>
  206. public new virtual void UpdateDpiDependencies()
  207. {
  208. if (Bar != null)
  209. {
  210. Bar.UpdateDpiDependencies();
  211. // this will take effect if high dpi is not enabled in DpiHelper
  212. Bar.DockTabStripHeight = Designer.LogicalToDevice(25);
  213. Bar.PaddingLeft = -2;
  214. Bar.PaddingRight = -2;
  215. Bar.PaddingTop = -2;
  216. Bar.PaddingBottom = -2;
  217. }
  218. base.UpdateDpiDependencies();
  219. }
  220. #endregion
  221. /// <summary>
  222. /// Initializes a new instance of the <see cref="ToolWindowBase"/> class with default settings.
  223. /// </summary>
  224. /// <param name="designer">The report designer.</param>
  225. /// <remarks>
  226. /// You don't need to call this constructor. The designer will do this automatically.
  227. /// </remarks>
  228. public ToolWindowBase(Designer designer) : base()
  229. {
  230. Designer = designer;
  231. Shortcut = eShortcut.None;
  232. Control = new PanelDockContainer();
  233. }
  234. }
  235. }