ToolStripZoomControl.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. using FastReport.Utils;
  2. using System;
  3. using System.Drawing;
  4. using System.Drawing.Drawing2D;
  5. using System.Windows.Forms;
  6. namespace FastReport.Controls
  7. {
  8. internal class ToolStripZoomControl : ToolStripItem
  9. {
  10. private bool hlButton1;
  11. private bool hlButton2;
  12. private bool hlButton3;
  13. private bool hlMinus;
  14. private bool hlPlus;
  15. private bool movingSlider;
  16. private int Minimum { get; set; }
  17. private int Maximum { get; set; }
  18. private int _value;
  19. private int Value
  20. {
  21. get => _value;
  22. set
  23. {
  24. if (value < Minimum)
  25. value = Minimum;
  26. if (value > Maximum)
  27. value = Maximum;
  28. _value = value;
  29. ValueChanged?.Invoke(this, EventArgs.Empty);
  30. Invalidate();
  31. }
  32. }
  33. public float ZoomValue
  34. {
  35. get
  36. {
  37. float val = Value;
  38. if (val < 100)
  39. val = val * 0.75f + 25;
  40. else
  41. val = (val - 100) * 4 + 100;
  42. return val / 100f;
  43. }
  44. set
  45. {
  46. if (value < 1)
  47. value = (value - 0.25f) / 0.75f;
  48. else if (value > 1)
  49. value = (value - 1) / 4 + 1;
  50. Value = (int)Math.Round(value * 100);
  51. }
  52. }
  53. private int Step { get; set; }
  54. private UIStyle style;
  55. public UIStyle UIStyle
  56. {
  57. get => style;
  58. set
  59. {
  60. style = value;
  61. Invalidate();
  62. }
  63. }
  64. public event EventHandler ValueChanged;
  65. public event EventHandler ZoomPageWidthClick;
  66. public event EventHandler ZoomWholePageClick;
  67. public event EventHandler Zoom100Click;
  68. private void PaintBackground(Graphics g)
  69. {
  70. Rectangle rect = new Rectangle(0, 0, Width, Height);
  71. var colorTable = UIStyleUtils.GetColorTable(UIStyle);
  72. using (var brush = new LinearGradientBrush(rect, colorTable.Workspace.ZoomControl.GradientBegin, colorTable.Workspace.ZoomControl.GradientEnd, 90))
  73. g.FillRectangle(brush, rect);
  74. using (var pen = new Pen(colorTable.GripDark))
  75. g.DrawLine(pen, 0, 0, 0, Height);
  76. using (var pen = new Pen(colorTable.GripLight))
  77. g.DrawLine(pen, 1, 0, 1, Height);
  78. }
  79. private void PaintHighlight(Graphics g, Rectangle rect)
  80. {
  81. int _22 = Owner.LogicalToDevice(22);
  82. var colorTable = UIStyleUtils.GetColorTable(UIStyle);
  83. using (var brush = new LinearGradientBrush(rect, colorTable.ButtonSelectedGradientBegin, colorTable.ButtonSelectedGradientEnd, 90))
  84. g.FillRectangle(brush, rect);
  85. using (var pen = new Pen(colorTable.ButtonSelectedHighlightBorder, (int)Math.Round(Owner.LogicalToDevice(1f))))
  86. g.DrawRectangle(pen, rect);
  87. }
  88. private void PaintButton(Graphics g, int imageIndex, int x, int baseLine, bool highlight)
  89. {
  90. var bmp = Owner.GetImage(imageIndex);
  91. if (highlight)
  92. {
  93. int _22 = Owner.LogicalToDevice(22);
  94. PaintHighlight(g, new Rectangle(x, baseLine - _22 / 2, _22, _22));
  95. }
  96. int _3 = Owner.LogicalToDevice(3);
  97. int _16 = Owner.LogicalToDevice(16);
  98. g.DrawImage(bmp, new Rectangle(x + _3, baseLine - _16 / 2, _16, _16));
  99. }
  100. private void PaintText(Graphics g, int x)
  101. {
  102. Color foreColor = UIStyleUtils.GetColorTable(UIStyle).Workspace.ZoomControl.Foreground;
  103. TextRenderer.DrawText(g, ((int)(Math.Round(ZoomValue * 100))).ToString() + "%",
  104. Font, new Rectangle(x, 0, Owner.LogicalToDevice(40), Height), foreColor, TextFormatFlags.Left | TextFormatFlags.VerticalCenter);
  105. }
  106. private void PaintSlider(Graphics g, int x, int baseLine)
  107. {
  108. int btnSize = Owner.LogicalToDevice(11);
  109. int thumb = Owner.LogicalToDevice(5);
  110. int gap = Owner.LogicalToDevice(20);
  111. int padRight = Owner.LogicalToDevice(8);
  112. int _1 = (int)Math.Round(Owner.LogicalToDevice(1f));
  113. int _2 = _1 + _1;
  114. int _3 = Owner.LogicalToDevice(3) + 1;
  115. int _6 = Owner.LogicalToDevice(6) + 1;
  116. int y = baseLine;
  117. var colorTable = UIStyleUtils.GetColorTable(UIStyle);
  118. Color foreColor = colorTable.Workspace.ZoomControl.Foreground;
  119. Color altColor = Color.Black;
  120. Pen p = new Pen(foreColor, _1);
  121. Pen p1 = new Pen(altColor, _1);
  122. Brush b = new SolidBrush(foreColor);
  123. Brush b1 = new SolidBrush(altColor);
  124. // minus
  125. if (hlMinus)
  126. PaintHighlight(g, new Rectangle(x - _3, y - btnSize / 2 - _3, btnSize + _6, btnSize + _6));
  127. g.DrawLine(hlMinus ? p1 : p, x, y, x + btnSize - 1, y);
  128. // line
  129. x += gap;
  130. g.DrawLine(p, x, y, Width - gap - padRight, y);
  131. // thumb
  132. int lineWidth = Width - gap - padRight - x;
  133. int pos = lineWidth * (Value - Minimum) / (Maximum - Minimum) + x;
  134. g.FillRectangle(b, pos - thumb / 2, y - btnSize / 2, thumb, btnSize);
  135. g.FillRectangle(b1, pos - thumb / 2 + _1, y - btnSize / 2 + _1, thumb - _2, btnSize - _2);
  136. // plus
  137. x = Width - padRight - btnSize;
  138. if (hlPlus)
  139. PaintHighlight(g, new Rectangle(x - _3, y - btnSize / 2 - _3, btnSize + _6, btnSize + _6));
  140. g.DrawLine(hlPlus? p1 : p, x, y, x + btnSize - 1, y);
  141. g.DrawLine(hlPlus ? p1 : p, x + btnSize / 2, y - btnSize / 2, x + btnSize / 2, y + btnSize / 2);
  142. p.Dispose();
  143. p1.Dispose();
  144. b.Dispose();
  145. b1.Dispose();
  146. }
  147. private void ResetHighlight()
  148. {
  149. hlButton1 = false;
  150. hlButton2 = false;
  151. hlButton3 = false;
  152. hlMinus = false;
  153. hlPlus = false;
  154. }
  155. protected override void OnPaint(PaintEventArgs e)
  156. {
  157. Graphics g = e.Graphics;
  158. PaintBackground(g);
  159. int gap = Owner.LogicalToDevice(6);
  160. int btnSize = Owner.LogicalToDevice(22);
  161. int textSize = Owner.LogicalToDevice(40);
  162. int x = gap;
  163. int baseLine = Height / 2;
  164. PaintButton(g, 235, x, baseLine, hlButton1);
  165. x += btnSize;
  166. PaintButton(g, 236, x, baseLine, hlButton2);
  167. x += btnSize;
  168. PaintButton(g, 237, x, baseLine, hlButton3);
  169. x += btnSize + gap;
  170. PaintText(g, x);
  171. x += textSize + gap;
  172. PaintSlider(g, x, baseLine);
  173. }
  174. protected override void OnMouseDown(MouseEventArgs e)
  175. {
  176. base.OnMouseDown(e);
  177. int padRight = Owner.LogicalToDevice(8);
  178. int gap = Owner.LogicalToDevice(6);
  179. int btnSize = Owner.LogicalToDevice(22);
  180. int textSize = Owner.LogicalToDevice(40);
  181. int lineGap = Owner.LogicalToDevice(20);
  182. int mouseX = e.X;
  183. if (Config.IsRunningOnMono)
  184. {
  185. mouseX -= this.Bounds.Left;
  186. }
  187. movingSlider = false;
  188. if (e.Button == MouseButtons.Left)
  189. {
  190. // slider area
  191. int lineStart = gap * 3 + btnSize * 3 + textSize + lineGap;
  192. int lineWidth = Width - padRight - lineGap - lineStart;
  193. if (mouseX >= lineStart && mouseX <= lineStart + lineWidth)
  194. {
  195. Value = (mouseX - lineStart) * (Maximum - Minimum) / lineWidth + Minimum;
  196. movingSlider = true;
  197. }
  198. }
  199. }
  200. protected override void OnMouseMove(MouseEventArgs e)
  201. {
  202. base.OnMouseMove(e);
  203. int padRight = Owner.LogicalToDevice(8);
  204. int gap = Owner.LogicalToDevice(6);
  205. int btnSize = Owner.LogicalToDevice(22);
  206. int textSize = Owner.LogicalToDevice(40);
  207. int sz = Owner.LogicalToDevice(11);
  208. int lineGap = Owner.LogicalToDevice(20);
  209. int baseLine = Height / 2;
  210. int _3 = Owner.LogicalToDevice(3);
  211. int mouseX = e.X;
  212. int mouseY = e.Y;
  213. if (Config.IsRunningOnMono)
  214. {
  215. mouseX -= this.Bounds.Left;
  216. }
  217. ResetHighlight();
  218. if (movingSlider)
  219. {
  220. // slider area
  221. int lineStart = gap * 3 + btnSize * 3 + textSize + lineGap;
  222. int lineWidth = Width - padRight - lineGap - lineStart;
  223. if (mouseX >= lineStart && mouseX <= lineStart + lineWidth)
  224. {
  225. Value = (mouseX - lineStart) * (Maximum - Minimum) / lineWidth + Minimum;
  226. }
  227. }
  228. else
  229. {
  230. int x = gap;
  231. if (mouseY > baseLine - btnSize / 2 && mouseY < baseLine + btnSize / 2)
  232. {
  233. // hit test buttons
  234. hlButton1 = mouseX > x && mouseX < x + btnSize;
  235. x += btnSize;
  236. hlButton2 = mouseX > x && mouseX < x + btnSize;
  237. x += btnSize;
  238. hlButton3 = mouseX > x && mouseX < x + btnSize;
  239. // hit text + -
  240. x = gap * 3 + btnSize * 3 + textSize;
  241. hlMinus = mouseX > x - _3 && mouseX < x + sz + _3;
  242. x = Width - padRight - sz;
  243. hlPlus = mouseX > x - _3 && mouseX < x + sz + _3;
  244. }
  245. }
  246. Invalidate();
  247. }
  248. protected override void OnMouseUp(MouseEventArgs e)
  249. {
  250. base.OnMouseUp(e);
  251. if (e.Button == MouseButtons.Left)
  252. {
  253. if (hlButton1)
  254. ZoomPageWidthClick?.Invoke(this, EventArgs.Empty);
  255. else if (hlButton2)
  256. ZoomWholePageClick?.Invoke(this, EventArgs.Empty);
  257. else if (hlButton3)
  258. Zoom100Click?.Invoke(this, EventArgs.Empty);
  259. else if (hlMinus)
  260. Value -= Step;
  261. else if (hlPlus)
  262. Value += Step;
  263. }
  264. movingSlider = false;
  265. ResetHighlight();
  266. Invalidate();
  267. }
  268. protected override void OnMouseLeave(EventArgs e)
  269. {
  270. base.OnMouseLeave(e);
  271. ResetHighlight();
  272. Invalidate();
  273. }
  274. public ToolStripZoomControl()
  275. {
  276. Maximum = 200;
  277. Value = 100;
  278. Step = 5;
  279. AutoSize = false;
  280. }
  281. }
  282. }