TableView.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  1. using FastReport.Utils;
  2. using System;
  3. using System.Drawing;
  4. using System.Drawing.Drawing2D;
  5. using System.Windows.Forms;
  6. namespace FastReport.FastQueryBuilder
  7. {
  8. internal partial class TableView : UserControl, ITableView
  9. {
  10. private enum Mode { None, Border, Caption, Button1, Button2, Button3 }
  11. private int frameWidth = 3;
  12. private int captionHeight = 19;
  13. private int buttonWidth = 12;
  14. private int gapSize = 4;
  15. private Mode mode;
  16. private Point oldPos = new Point(0, 0);
  17. private Point minSize = new Point(50, 20);
  18. private TableBorder myBorder;
  19. private Table table;
  20. private Size oldSize;
  21. private FqbCheckedListBox checkedListBox1;
  22. public TableView()
  23. {
  24. BackColor = SystemColors.AppWorkspace;
  25. Size = new Size(150, 200);
  26. myBorder = new TableBorder(this);
  27. checkedListBox1 = new FqbCheckedListBox();
  28. checkedListBox1.AllowDrop = true;
  29. checkedListBox1.Anchor = AnchorStyles.Top | AnchorStyles.Bottom | AnchorStyles.Left | AnchorStyles.Right;
  30. checkedListBox1.BorderStyle = BorderStyle.None;
  31. checkedListBox1.CheckOnClick = true;
  32. checkedListBox1.IntegralHeight = false;
  33. checkedListBox1.Name = "checkedListBox1";
  34. checkedListBox1.TabIndex = 1;
  35. checkedListBox1.ItemCheck += checkedListBox1_ItemCheck;
  36. checkedListBox1.DragOver += checkedListBox1_DragOver;
  37. checkedListBox1.DragDrop += checkedListBox1_DragDrop;
  38. checkedListBox1.MouseEnter += checkedListBox1_MouseEnter;
  39. checkedListBox1.VertScrollValueChanged += checkedListBox1_Scroll;
  40. checkedListBox1.Parent = this;
  41. SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.OptimizedDoubleBuffer, true);
  42. }
  43. #region Public Members
  44. public event EventHandler OnChangeAlias;
  45. public event CheckFieldEventHandler OnSelectField;
  46. public event AddLinkEventHandler OnAddLink;
  47. public event AddTableEventHandler OnDeleteTable;
  48. public void SetTableName(string tableName)
  49. {
  50. Text = tableName;
  51. }
  52. public Table Table
  53. {
  54. get { return table; }
  55. set
  56. {
  57. table = value;
  58. SetTableName(table.getNameAndAlias());
  59. checkedListBox1.Items.Add('*');
  60. foreach (Field fld in table.FieldList)
  61. {
  62. checkedListBox1.Items.Add(fld);
  63. }
  64. }
  65. }
  66. public Point GetPosition(Field field, LinkPosition lp)
  67. {
  68. int pos = checkedListBox1.FindString(field.ToString());
  69. Rectangle rec = checkedListBox1.GetItemRectangle(pos);
  70. Point pnt = new Point();
  71. pnt.Y = rec.Top + rec.Height / 2;
  72. if (pnt.Y < 0)
  73. pnt.Y = 0;
  74. else if (pnt.Y > checkedListBox1.Height)
  75. pnt.Y = checkedListBox1.Height;
  76. int _10 = this.LogicalToDevice(10);
  77. if (lp == LinkPosition.Left)
  78. pnt.X = -_10;
  79. else
  80. pnt.X = checkedListBox1.Width + _10;
  81. return new Point(pnt.X + checkedListBox1.Left + Left, pnt.Y + checkedListBox1.Top + Top);
  82. }
  83. public int GetLeft() => Left;
  84. public int GetWidth() => Width;
  85. public bool SelectCheckBox(string fieldName, string function, string alias)
  86. {
  87. for (int i = 0; i < checkedListBox1.Items.Count; i++)
  88. {
  89. var f = checkedListBox1.Items[i] as Field;
  90. if (f != null && f.Name == fieldName)
  91. {
  92. if (!String.IsNullOrEmpty(function))
  93. f.Func = function;
  94. if (!String.IsNullOrEmpty(alias))
  95. f.Alias = alias;
  96. checkedListBox1.SetItemChecked(i, true);
  97. return true;
  98. }
  99. }
  100. return false;
  101. }
  102. public void UpdateDpiDependencies()
  103. {
  104. frameWidth = this.LogicalToDevice(3);
  105. captionHeight = this.LogicalToDevice(20);
  106. buttonWidth = this.LogicalToDevice(12);
  107. gapSize = this.LogicalToDevice(4);
  108. checkedListBox1.Top = frameWidth + captionHeight;
  109. checkedListBox1.Left = frameWidth;
  110. checkedListBox1.Width = ClientRectangle.Width - 2 * frameWidth;
  111. checkedListBox1.Height = ClientRectangle.Height - captionHeight - frameWidth * 2;
  112. }
  113. #endregion
  114. private void checkedListBox1_DragOver(object sender, DragEventArgs e)
  115. {
  116. TableView _sender = (sender as Control).Parent as TableView;
  117. int n = _sender.checkedListBox1.IndexFromPoint(_sender.checkedListBox1.PointToClient(new Point(e.X, e.Y)));
  118. if (n == -1)
  119. {
  120. e.Effect = DragDropEffects.None;
  121. return;
  122. }
  123. Field _sended = _sender.checkedListBox1.Items[n] as Field;
  124. Field _current = (Field)e.Data.GetData(typeof(Field));
  125. if (_current != null && _current.CanLink(_sended))
  126. e.Effect = DragDropEffects.Copy;
  127. else
  128. e.Effect = DragDropEffects.None;
  129. }
  130. private void checkedListBox1_DragDrop(object sender, DragEventArgs e)
  131. {
  132. TableView _sender = (sender as Control).Parent as TableView;
  133. int n = _sender.checkedListBox1.IndexFromPoint(_sender.checkedListBox1.PointToClient(new Point(e.X, e.Y)));
  134. if (n == -1)
  135. {
  136. e.Effect = DragDropEffects.None;
  137. return;
  138. }
  139. Field _sended = _sender.checkedListBox1.Items[n] as Field;
  140. Field _current = (Field)e.Data.GetData(typeof(Field));
  141. if (_current.CanLink(_sended))
  142. {
  143. OnAddLink?.Invoke(sender, new AddLinkEventArgs(_current, _sended));
  144. if (Parent != null)
  145. Parent.Refresh();
  146. }
  147. }
  148. private void checkedListBox1_ItemCheck(object sender, ItemCheckEventArgs e)
  149. {
  150. if (OnSelectField != null)
  151. {
  152. if (checkedListBox1.Items[e.Index] is Field)
  153. {
  154. Field field = checkedListBox1.Items[e.Index] as Field;
  155. CheckFieldEventArgs e2 = new CheckFieldEventArgs(field);
  156. e2.value = e.NewValue == CheckState.Checked;
  157. OnSelectField(sender, e2);
  158. checkedListBox1.ItemCheck -= checkedListBox1_ItemCheck;
  159. checkedListBox1.SetItemChecked(0, false);
  160. checkedListBox1.ItemCheck += checkedListBox1_ItemCheck;
  161. }
  162. else
  163. {
  164. for (int i = 1; i < checkedListBox1.Items.Count; i++)
  165. {
  166. checkedListBox1.SetItemChecked(i, e.NewValue == CheckState.Checked);
  167. }
  168. }
  169. }
  170. }
  171. private void checkedListBox1_Scroll(object sender, EventArgs e)
  172. {
  173. Parent?.Refresh();
  174. }
  175. private void checkedListBox1_MouseEnter(object sender, EventArgs e)
  176. {
  177. myBorder.SelectedBorder = BorderPosition.None;
  178. mode = Mode.None;
  179. RefreshCaption();
  180. }
  181. private void RefreshCaption()
  182. {
  183. Invalidate(false);
  184. }
  185. private void Button1Click()
  186. {
  187. if (Height <= captionHeight + frameWidth * 2)
  188. Height = oldSize.Height;
  189. else
  190. {
  191. oldSize = this.Size;
  192. Height = captionHeight + frameWidth * 2;
  193. }
  194. Parent.Refresh();
  195. }
  196. private void Button2Click()
  197. {
  198. using (var box = new InputBox())
  199. {
  200. box.TextBox.Text = table.Alias;
  201. if (box.ShowDialog() == DialogResult.OK)
  202. {
  203. OnChangeAlias?.Invoke(this, EventArgs.Empty);
  204. table.Alias = box.TextBox.Text;
  205. Text = table.getNameAndAlias();
  206. }
  207. }
  208. }
  209. private void Button3Click()
  210. {
  211. this.Dispose();
  212. OnDeleteTable?.Invoke(this, new AddTableEventArgs(this.table, new Point()));
  213. }
  214. protected override void OnMouseMove(MouseEventArgs e)
  215. {
  216. base.OnMouseMove(e);
  217. if (e.Button == MouseButtons.Left)
  218. {
  219. if (mode == Mode.Border)
  220. {
  221. switch (myBorder.SelectedBorder)
  222. {
  223. case BorderPosition.Top:
  224. if (Height - e.Y >= minSize.Y)
  225. SetBounds(Left, Top + e.Y, Width, Height - e.Y);
  226. break;
  227. case BorderPosition.Bottom:
  228. if (Height - (oldPos.Y - e.Y) >= minSize.Y)
  229. Height -= oldPos.Y - e.Y;
  230. break;
  231. case BorderPosition.Left:
  232. if (Width - e.X >= minSize.X)
  233. SetBounds(Left + e.X, Top, Width - e.X, Height);
  234. break;
  235. case BorderPosition.Right:
  236. if (Width - (oldPos.X - e.X) >= minSize.X)
  237. Width -= oldPos.X - e.X;
  238. break;
  239. }
  240. oldPos.X = e.X;
  241. oldPos.Y = e.Y;
  242. Parent.Refresh();
  243. }
  244. else if (mode == Mode.Caption)
  245. {
  246. Left += e.X - oldPos.X;
  247. Top += e.Y - oldPos.Y;
  248. }
  249. }
  250. else
  251. {
  252. mode = Mode.None;
  253. // check border
  254. if (e.X >= Width - frameWidth && e.X <= Width)
  255. myBorder.SelectedBorder = BorderPosition.Right;
  256. else if (e.X >= 0 && e.X <= frameWidth)
  257. myBorder.SelectedBorder = BorderPosition.Left;
  258. else if (e.Y >= 0 && e.Y <= frameWidth)
  259. myBorder.SelectedBorder = BorderPosition.Top;
  260. else if (e.Y >= Height - frameWidth && e.Y <= Height)
  261. myBorder.SelectedBorder = BorderPosition.Bottom;
  262. else
  263. myBorder.SelectedBorder = BorderPosition.None;
  264. if (myBorder.SelectedBorder != BorderPosition.None)
  265. mode = Mode.Border;
  266. else
  267. {
  268. if (e.Y < captionHeight + frameWidth)
  269. {
  270. // check buttons
  271. int x = Width - frameWidth;
  272. int w = buttonWidth + gapSize * 2;
  273. if (e.X >= x - w && e.X <= x)
  274. mode = Mode.Button3;
  275. x -= w;
  276. if (e.X >= x - w && e.X <= x)
  277. mode = Mode.Button2;
  278. x -= w;
  279. if (e.X >= x - w && e.X <= x)
  280. mode = Mode.Button1;
  281. // it's a caption
  282. if (mode == Mode.None)
  283. mode = Mode.Caption;
  284. RefreshCaption();
  285. }
  286. }
  287. }
  288. }
  289. protected override void OnMouseDown(MouseEventArgs e)
  290. {
  291. base.OnMouseDown(e);
  292. oldPos.X = e.X;
  293. oldPos.Y = e.Y;
  294. }
  295. protected override void OnMouseUp(MouseEventArgs e)
  296. {
  297. base.OnMouseUp(e);
  298. if (mode == Mode.Button1)
  299. Button1Click();
  300. else if (mode == Mode.Button2)
  301. Button2Click();
  302. else if (mode == Mode.Button3)
  303. Button3Click();
  304. }
  305. protected override void OnMouseLeave(EventArgs e)
  306. {
  307. base.OnMouseLeave(e);
  308. mode = Mode.None;
  309. RefreshCaption();
  310. }
  311. protected override void OnLocationChanged(EventArgs e)
  312. {
  313. base.OnLocationChanged(e);
  314. Parent?.Refresh();
  315. }
  316. protected override void OnPaint(PaintEventArgs e)
  317. {
  318. base.OnPaint(e);
  319. var g = e.Graphics;
  320. g.SmoothingMode = SmoothingMode.HighQuality;
  321. // caption
  322. var captionRect = new Rectangle(frameWidth, frameWidth, Width - frameWidth * 2, captionHeight);
  323. g.FillRectangle(SystemBrushes.Control, captionRect);
  324. captionRect.Offset(this.LogicalToDevice(2), 0);
  325. TextRenderer.DrawText(g, Text, Font, captionRect, SystemColors.WindowText, TextFormatFlags.VerticalCenter);
  326. // buttons
  327. int x = Width - frameWidth - gapSize - buttonWidth;
  328. int y = frameWidth + gapSize;
  329. int sz = buttonWidth + gapSize * 2;
  330. float _1 = this.LogicalToDevice(1f);
  331. float _2 = this.LogicalToDevice(2f);
  332. float _6 = this.LogicalToDevice(6f);
  333. var pen = new Pen(Color.Black, _1);
  334. var whitePen = new Pen(Color.White, _1);
  335. // button3
  336. var p = pen;
  337. if (mode == Mode.Button3)
  338. {
  339. g.FillRectangle(Brushes.Firebrick, new Rectangle(x - gapSize, y - gapSize, sz, sz));
  340. p = whitePen;
  341. }
  342. g.DrawLine(p, x + _2, y + _2, x + buttonWidth - _2, y + buttonWidth - _2);
  343. g.DrawLine(p, x + _2, y + buttonWidth - _2, x + buttonWidth - _2, y + _2);
  344. // button2
  345. x -= sz;
  346. if (mode == Mode.Button2)
  347. g.FillRectangle(Brushes.White, new Rectangle(x - gapSize, y - gapSize, sz, sz));
  348. g.DrawRectangle(pen, x + _1 * 2, y + _6, 1, 1);
  349. g.DrawRectangle(pen, x + _1 * 6, y + _6, 1, 1);
  350. g.DrawRectangle(pen, x + _1 * 10, y + _6, 1, 1);
  351. // button1
  352. x -= sz;
  353. if (mode == Mode.Button1)
  354. g.FillRectangle(Brushes.White, new Rectangle(x - gapSize, y - gapSize, sz, sz));
  355. g.DrawLine(pen, x + _1, y + _6, x + buttonWidth - _2, y + _6);
  356. pen.Dispose();
  357. whitePen.Dispose();
  358. }
  359. }
  360. }