RibbonGroup.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. using FastReport.Forms;
  2. using FastReport.Utils;
  3. using System.Collections;
  4. using System.Windows;
  5. using System.Windows.Forms;
  6. namespace FastReport.Design.RibbonDesigner
  7. {
  8. internal enum GroupState
  9. {
  10. Normal,
  11. Reduced,
  12. Collapsed
  13. }
  14. internal enum GroupPriority
  15. {
  16. Low,
  17. Medium,
  18. High
  19. }
  20. internal class RibbonGroup : GridPanel, IDesignerPlugin
  21. {
  22. private Hashtable cachedWidths = new Hashtable();
  23. public Designer Designer { get; }
  24. public StackedPanel Panel { get; }
  25. public Label Caption { get; }
  26. private GroupState state;
  27. public GroupState State
  28. {
  29. get => state;
  30. set
  31. {
  32. state = value;
  33. UpdateState();
  34. control.UpdateLayout();
  35. }
  36. }
  37. public double GroupWidth
  38. {
  39. get
  40. {
  41. if (!cachedWidths.ContainsKey(state))
  42. cachedWidths[state] = this.Measure();
  43. return (double)cachedWidths[state];
  44. }
  45. }
  46. public ToolbarLayoutDropDownButton CollapsedButton { get; }
  47. public string CollapsedImageName { get; set; }
  48. public GroupPriority Priority { get; set; }
  49. private ControlStorageService storage;
  50. /// <summary>
  51. /// Gets storage service.
  52. /// </summary>
  53. public ControlStorageService Storage
  54. {
  55. get
  56. {
  57. if (storage == null)
  58. storage = new ControlStorageService(this, "Designer," + Name);
  59. return storage;
  60. }
  61. }
  62. public override string Text
  63. {
  64. get => base.Text;
  65. set
  66. {
  67. base.Text = value;
  68. Caption.Text = value;
  69. CollapsedButton.Text = value;
  70. }
  71. }
  72. public override bool Visible
  73. {
  74. get => base.Visible;
  75. set
  76. {
  77. if (value != Visible)
  78. {
  79. base.Visible = value;
  80. control.Visibility = value ? Visibility.Visible : Visibility.Collapsed;
  81. cachedWidths.Clear();
  82. control.UpdateLayout();
  83. (Parent as Ribbon).UpdateLayout();
  84. }
  85. }
  86. }
  87. #region IDesignerPlugin
  88. public string PluginName => Name;
  89. /// <inheritdoc/>
  90. public virtual void SaveState()
  91. {
  92. }
  93. /// <inheritdoc/>
  94. public virtual void RestoreState()
  95. {
  96. }
  97. /// <inheritdoc/>
  98. public virtual void SelectionChanged()
  99. {
  100. }
  101. /// <inheritdoc/>
  102. public virtual void UpdateContent()
  103. {
  104. }
  105. /// <inheritdoc/>
  106. public void Lock()
  107. {
  108. }
  109. /// <inheritdoc/>
  110. public void Unlock()
  111. {
  112. UpdateContent();
  113. }
  114. /// <inheritdoc/>
  115. public virtual void Localize()
  116. {
  117. cachedWidths.Clear();
  118. }
  119. /// <inheritdoc/>
  120. public virtual DesignerOptionsPage GetOptionsPage()
  121. {
  122. return null;
  123. }
  124. /// <inheritdoc/>
  125. public virtual void UpdateUIStyle()
  126. {
  127. Panel.UpdateUIStyle(Designer.UIStyle);
  128. CollapsedButton.UpdateUIStyle(Designer.UIStyle);
  129. }
  130. /// <inheritdoc/>
  131. public virtual void UpdateDpiDependencies()
  132. {
  133. RibbonExtensions.UpdateDpiDependencies(this);
  134. Panel.UpdateDpiDependencies();
  135. if (!string.IsNullOrEmpty(CollapsedImageName))
  136. CollapsedButton.Image = Designer.GetImage(CollapsedImageName);
  137. }
  138. #endregion
  139. public virtual void UpdateState()
  140. {
  141. if (State == GroupState.Collapsed)
  142. {
  143. Panel.CollapseTo(CollapsedButton);
  144. }
  145. else
  146. {
  147. CollapsedButton.ExpandTo(Panel);
  148. }
  149. }
  150. public void SetItemText(ToolStripItem item, string text)
  151. {
  152. if (text.EndsWith("..."))
  153. text = text.Substring(0, text.Length - 3);
  154. item.ToolTipText = text;
  155. item.Text = text;
  156. }
  157. public RibbonGroup(Designer designer)
  158. {
  159. Designer = designer;
  160. state = GroupState.Normal;
  161. control.Margin = new Thickness(4, 1, 4, 1);
  162. Caption = new RibbonGroupCaption();
  163. this.Controls.Add(Caption);
  164. Panel = new StackedPanel() { Vertical = false };
  165. Panel.control.Margin = new Thickness(0, 3, 0, 16);
  166. this.Controls.Add(Panel);
  167. CollapsedButton = new ToolbarLayoutDropDownButton();
  168. CollapsedButton.MakeBig();
  169. CollapsedButton.DropDownOpening += (s, e) => UpdateDpiDependencies();
  170. }
  171. }
  172. }