ToolStripUndoRedoButton.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. using FastReport.Design;
  2. using FastReport.Utils;
  3. using System;
  4. using System.ComponentModel;
  5. using System.Drawing;
  6. using System.Windows.Forms;
  7. namespace FastReport.Controls
  8. {
  9. internal class ToolStripUndoButton : ToolStripSplitButton
  10. {
  11. public ToolStripUndoButton(Designer designer)
  12. {
  13. DisplayStyle = ToolStripItemDisplayStyle.Image;
  14. DropDown = new UndoDropDown(designer);
  15. ButtonClick += designer.cmdUndo.Invoke;
  16. }
  17. }
  18. internal class ToolStripRedoButton : ToolStripSplitButton
  19. {
  20. public ToolStripRedoButton(Designer designer)
  21. {
  22. DisplayStyle = ToolStripItemDisplayStyle.Image;
  23. DropDown = new RedoDropDown(designer);
  24. ButtonClick += designer.cmdRedo.Invoke;
  25. }
  26. }
  27. internal class UndoRedoDropDown : ToolStripDropDown
  28. {
  29. private bool updating;
  30. private int actionsCount;
  31. protected Designer designer;
  32. protected ListBox lbxActions;
  33. protected Label lblUndoRedo;
  34. protected ToolStripControlHost actionsHost;
  35. protected ToolStripControlHost labelHost;
  36. private void SetSelected(int index)
  37. {
  38. if (updating)
  39. return;
  40. updating = true;
  41. int saveTop = lbxActions.TopIndex;
  42. lbxActions.BeginUpdate();
  43. lbxActions.SelectedIndices.Clear();
  44. if (lbxActions.Items.Count > 0)
  45. {
  46. for (int i = index; i >= 0; i--)
  47. lbxActions.SelectedIndices.Add(i);
  48. }
  49. lbxActions.TopIndex = saveTop;
  50. lbxActions.EndUpdate();
  51. UpdateLabel();
  52. updating = false;
  53. }
  54. private void lbxActions_MouseMove(object sender, MouseEventArgs e)
  55. {
  56. int index = lbxActions.IndexFromPoint(e.X, e.Y);
  57. SetSelected(index);
  58. actionsCount = lbxActions.SelectedItems.Count;
  59. }
  60. private void lbxActions_MouseDown(object sender, MouseEventArgs e)
  61. {
  62. Close(ToolStripDropDownCloseReason.ItemClicked);
  63. DoUndoRedo(actionsCount);
  64. }
  65. private void UpdateSizes()
  66. {
  67. lbxActions.Size = designer.LogicalToDevice(new Size(150, 200));
  68. lblUndoRedo.Size = designer.LogicalToDevice(new Size(150, 30));
  69. }
  70. protected virtual void PopulateItems()
  71. {
  72. }
  73. protected virtual void UpdateLabel()
  74. {
  75. }
  76. protected virtual void DoUndoRedo(int actionsCount)
  77. {
  78. }
  79. protected override void OnOpening(CancelEventArgs e)
  80. {
  81. base.OnOpening(e);
  82. PopulateItems();
  83. UpdateSizes();
  84. SetSelected(0);
  85. }
  86. public UndoRedoDropDown(Designer designer)
  87. : base()
  88. {
  89. this.designer = designer;
  90. Padding = new Padding(1);
  91. lbxActions = new ListBox();
  92. lbxActions.Dock = DockStyle.Fill;
  93. lbxActions.BorderStyle = BorderStyle.None;
  94. lbxActions.SelectionMode = SelectionMode.MultiSimple;
  95. lbxActions.MouseMove += new MouseEventHandler(lbxActions_MouseMove);
  96. lbxActions.MouseDown += new MouseEventHandler(lbxActions_MouseDown);
  97. actionsHost = new ToolStripControlHost(lbxActions);
  98. actionsHost.AutoSize = false;
  99. actionsHost.Size = new Size(150, 250);
  100. Items.Add(actionsHost);
  101. lblUndoRedo = new Label();
  102. lblUndoRedo.Dock = DockStyle.Fill;
  103. lblUndoRedo.AutoSize = false;
  104. lblUndoRedo.TextAlign = ContentAlignment.MiddleCenter;
  105. lblUndoRedo.Text = "test";
  106. lblUndoRedo.BackColor = Color.Transparent;
  107. labelHost = new ToolStripControlHost(lblUndoRedo);
  108. labelHost.AutoSize = false;
  109. labelHost.Size = new Size(100, 30);
  110. Items.Add(labelHost);
  111. }
  112. }
  113. internal class UndoDropDown : UndoRedoDropDown
  114. {
  115. protected override void PopulateItems()
  116. {
  117. lbxActions.Items.Clear();
  118. if (designer.ActiveReport != null)
  119. {
  120. string[] undoNames = designer.ActiveReportTab.UndoRedo.UndoNames;
  121. foreach (string s in undoNames)
  122. {
  123. lbxActions.Items.Add(s);
  124. }
  125. }
  126. }
  127. protected override void UpdateLabel()
  128. {
  129. lblUndoRedo.Text = String.Format(Res.Get("Designer,UndoRedo,UndoN"), lbxActions.SelectedItems.Count);
  130. }
  131. protected override void DoUndoRedo(int actionsCount)
  132. {
  133. designer.cmdUndo.Undo(actionsCount);
  134. }
  135. public UndoDropDown(Designer designer)
  136. : base(designer)
  137. {
  138. }
  139. }
  140. internal class RedoDropDown : UndoRedoDropDown
  141. {
  142. protected override void PopulateItems()
  143. {
  144. lbxActions.Items.Clear();
  145. if (designer.ActiveReport != null)
  146. {
  147. string[] undoNames = designer.ActiveReportTab.UndoRedo.RedoNames;
  148. foreach (string s in undoNames)
  149. {
  150. lbxActions.Items.Add(s);
  151. }
  152. }
  153. }
  154. protected override void UpdateLabel()
  155. {
  156. lblUndoRedo.Text = String.Format(Res.Get("Designer,UndoRedo,RedoN"), lbxActions.SelectedItems.Count);
  157. }
  158. protected override void DoUndoRedo(int actionsCount)
  159. {
  160. designer.cmdRedo.Redo(actionsCount);
  161. }
  162. public RedoDropDown(Designer designer)
  163. : base(designer)
  164. {
  165. }
  166. }
  167. }