Ribbon.Extensions.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. using FastReport.Utils;
  2. using System.Windows;
  3. using System.Windows.Forms;
  4. namespace FastReport.Design.RibbonDesigner
  5. {
  6. internal static class RibbonExtensions
  7. {
  8. public static double Measure(this Control c)
  9. {
  10. c.control.Measure(new Size(double.PositiveInfinity, double.PositiveInfinity));
  11. return c.control.DesiredSize.Width;
  12. }
  13. public static void AddItems(this Control ctl, params IToolbarItem[] items)
  14. {
  15. // convert item.BeginGroup to a separator
  16. foreach (var item in items)
  17. {
  18. if (item.BeginGroup && ctl.Controls.Count > 0)
  19. ctl.Controls.Add(new ToolStripSeparator(true));
  20. ctl.Controls.Add(item as ToolStripItem);
  21. }
  22. }
  23. public static void AddPanels(this Control ctl, params PanelBase[] panels)
  24. {
  25. foreach (var panel in panels)
  26. ctl.Controls.Add(panel);
  27. }
  28. public static void UpdateDpiDependencies(this Control ctl)
  29. {
  30. ctl.SuspendLayout();
  31. foreach (var c in ctl.Controls)
  32. {
  33. if (c is ToolStripItem item)
  34. {
  35. if (item.ImageIndex != -1)
  36. item.Image = ctl.GetImage(item.ImageIndex);
  37. }
  38. }
  39. ctl.ResumeLayout();
  40. }
  41. public static void UpdateUIStyle(this Control ctl, UIStyle style)
  42. {
  43. var renderer = UIStyleUtils.GetToolStripRenderer(style);
  44. if (ctl is ToolStripItem item)
  45. item.ApplyStyle(renderer);
  46. foreach (Control c in ctl.Controls)
  47. {
  48. c.UpdateUIStyle(style);
  49. }
  50. }
  51. public static void MakeBig(this ToolStripButton btn, int maxWidth = 80)
  52. {
  53. btn.ImageSize = new System.Drawing.Size(32, 32);
  54. btn.Vertical = true;
  55. btn.DisplayStyle = ToolStripItemDisplayStyle.ImageAndText;
  56. btn.control.VerticalAlignment = VerticalAlignment.Stretch;
  57. btn.control.VerticalContentAlignment = VerticalAlignment.Stretch;
  58. btn.control.MaxWidth = maxWidth;
  59. }
  60. public static void MakeBig(this ToolStripDropDownButtonBase btn, int maxWidth = 80)
  61. {
  62. btn.ImageSize = new System.Drawing.Size(32, 32);
  63. btn.Vertical = true;
  64. btn.DisplayStyle = ToolStripItemDisplayStyle.ImageAndText;
  65. btn.control.VerticalAlignment = VerticalAlignment.Stretch;
  66. btn.control.VerticalContentAlignment = VerticalAlignment.Stretch;
  67. btn.control.MaxWidth = maxWidth;
  68. }
  69. public static void CollapseTo(this PanelBase panel, ToolbarLayoutDropDownButton button, bool isGroup = true)
  70. {
  71. var parent = panel.Parent;
  72. if (parent == null)
  73. return;
  74. int index = parent.Controls.IndexOf(panel);
  75. if (index != -1)
  76. {
  77. parent.Controls.Remove(panel);
  78. parent.Controls.Add(button);
  79. parent.Controls.SetChildIndex(button, index);
  80. button.DropDown.Items.Clear();
  81. button.DropDown.Items.Add(new ToolStripControlHost(panel));
  82. panel.control.Margin = new Thickness(10);
  83. button.control.HorizontalAlignment = System.Windows.HorizontalAlignment.Stretch;
  84. if (isGroup && parent is RibbonGroup group)
  85. {
  86. button.control.Margin = new Thickness(0, 3, 0, 16);
  87. button.Text = group.Caption.Text;
  88. group.Caption.Visible = false;
  89. }
  90. }
  91. }
  92. public static void ExpandTo(this ToolbarLayoutDropDownButton button, PanelBase panel, bool isGroup = true)
  93. {
  94. var parent = button.Parent;
  95. if (parent == null)
  96. return;
  97. int index = parent.Controls.IndexOf(button);
  98. if (index != -1)
  99. {
  100. button.DropDown.Items.Clear();
  101. parent.Controls.Remove(button);
  102. parent.Controls.Add(panel);
  103. parent.Controls.SetChildIndex(panel, index);
  104. panel.control.Margin = new Thickness(0);
  105. if (isGroup && parent is RibbonGroup group)
  106. {
  107. panel.VerticalAlignment = VerticalAlignment.Center;
  108. panel.control.Margin = new Thickness(0, 3, 0, 16);
  109. group.Caption.Visible = true;
  110. }
  111. }
  112. }
  113. }
  114. }