TableView.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  1. using System;
  2. using System.Drawing;
  3. using System.Windows.Forms;
  4. using System.ComponentModel;
  5. namespace FastReport.FastQueryBuilder
  6. {
  7. #if !DEBUG
  8. [DesignTimeVisible(false)]
  9. #endif
  10. internal partial class TableView : UserControl, ITableView
  11. {
  12. private int frameWidth = 3;
  13. private Point oldPos = new Point(0, 0);
  14. private Point minSize = new Point(50, 20);
  15. private TableBorder myBorder;
  16. private Table table;
  17. private Rectangle dragBoxFromMouseDown;
  18. private Size oldSize;
  19. FqbCheckedListBox checkedListBox1;
  20. public TableView()
  21. {
  22. InitializeComponent();
  23. myBorder = new TableBorder(this);
  24. label1.Left = frameWidth;
  25. label1.Top = frameWidth;
  26. label1.Width = ClientRectangle.Width - 2 * frameWidth;
  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.FormattingEnabled = true;
  33. checkedListBox1.IntegralHeight = false;
  34. checkedListBox1.Location = new Point(18, 43);
  35. checkedListBox1.Name = "checkedListBox1";
  36. checkedListBox1.Size = new Size(115, 132);
  37. checkedListBox1.TabIndex = 1;
  38. checkedListBox1.MouseUp += checkedListBox1_MouseUp;
  39. checkedListBox1.DragOver += checkedListBox1_DragOver;
  40. checkedListBox1.ItemCheck += checkedListBox1_ItemCheck;
  41. checkedListBox1.DragDrop += checkedListBox1_DragDrop;
  42. checkedListBox1.MouseEnter += checkedListBox1_MouseEnter;
  43. checkedListBox1.MouseMove += checkedListBox1_MouseMove;
  44. checkedListBox1.MouseDown += checkedListBox1_MouseDown;
  45. checkedListBox1.VertScrollValueChanged += checkedListBox1_Scroll;
  46. checkedListBox1.Parent = this;
  47. checkedListBox1.Top = frameWidth + label1.Height;
  48. checkedListBox1.Left = frameWidth;
  49. checkedListBox1.Width = ClientRectangle.Width - 2 * frameWidth;
  50. checkedListBox1.Height = ClientRectangle.Height - label1.Height - frameWidth * 2;
  51. }
  52. private void label1_MouseDown(object sender, MouseEventArgs e)
  53. {
  54. if (e.Button == MouseButtons.Left)
  55. {
  56. BringToFront();
  57. Update();
  58. Win32.ReleaseCapture();
  59. Win32.SendMessage(Handle, Win32.WM_NCLBUTTONDOWN, Win32.HT_CAPTION, 0);
  60. }
  61. }
  62. #region ITableView Members
  63. public event EventHandler OnChangeAlias;
  64. public event CheckFieldEventHandler OnSelectField;
  65. public event AddLinkEventHandler OnAddLink;
  66. public event AddTableEventHandler OnDeleteTable;
  67. public void DoAddLink()
  68. {
  69. }
  70. public void SetTableName(string tableName)
  71. {
  72. label1.Text = tableName;
  73. }
  74. public void SetTabeleAlias()
  75. {
  76. throw new Exception("The method or operation is not implemented.");
  77. }
  78. public Table Table
  79. {
  80. get { return table; }
  81. set
  82. {
  83. table = value;
  84. SetTableName(table.getNameAndAlias());
  85. checkedListBox1.Items.Add('*');
  86. foreach (Field fld in table.FieldList)
  87. {
  88. checkedListBox1.Items.Add(fld);
  89. }
  90. }
  91. }
  92. public Point GetPosition(Field field, LinkPosition lp)
  93. {
  94. int pos = checkedListBox1.FindString(field.ToString());
  95. Rectangle rec = checkedListBox1.GetItemRectangle(pos);
  96. Point pnt = new Point();
  97. pnt.Y = rec.Top + rec.Height / 2;
  98. if (pnt.Y < 0)
  99. pnt.Y = 0;
  100. else if (pnt.Y > checkedListBox1.Height)
  101. pnt.Y = checkedListBox1.Height;
  102. if (lp == LinkPosition.Left)
  103. pnt.X = -10;
  104. else
  105. pnt.X = checkedListBox1.Width + 10;
  106. return checkedListBox1.PointToScreen(pnt);
  107. }
  108. public int GetLeft()
  109. {
  110. return Left;
  111. }
  112. public int GetWidth()
  113. {
  114. return Width;
  115. }
  116. #endregion
  117. protected override void OnPaint(PaintEventArgs e)
  118. {
  119. base.OnPaint(e);
  120. //e.Graphics.DrawRectangle(new Pen(Color.Black), new Rectangle(0, 0, ClientRectangle.Width - 1, ClientRectangle.Height - 1));
  121. Parent.Update();
  122. }
  123. private void TableView_MouseMove(object sender, MouseEventArgs e)
  124. {
  125. if (myBorder.isResize)
  126. {
  127. switch (myBorder.SelectedBorder)
  128. {
  129. case BorderPosition.Top:
  130. if ((Height - e.Y) >= minSize.Y)
  131. SetBounds(Left, Top + e.Y, Width, Height - e.Y);
  132. break;
  133. case BorderPosition.Bottom:
  134. if ((Height - (oldPos.Y - e.Y)) >= minSize.Y)
  135. Height = Height - (oldPos.Y - e.Y);
  136. break;
  137. case BorderPosition.Left:
  138. if ((Width - e.X) >= minSize.X)
  139. SetBounds(Left + e.X, Top, Width - e.X, Height);
  140. break;
  141. case BorderPosition.Right:
  142. if ((Width - (oldPos.X - e.X)) >= minSize.X)
  143. Width = Width - (oldPos.X - e.X);
  144. break;
  145. }
  146. }
  147. else
  148. {
  149. if ((e.X > ClientRectangle.Width - frameWidth) && (e.X < ClientRectangle.Width))
  150. myBorder.SelectedBorder = BorderPosition.Right;
  151. else
  152. if ((e.X > 0) && (e.X < frameWidth))
  153. myBorder.SelectedBorder = BorderPosition.Left;
  154. else
  155. if ((e.Y > 0) && (e.Y < frameWidth))
  156. myBorder.SelectedBorder = BorderPosition.Top;
  157. else
  158. if ((e.Y > ClientRectangle.Height - frameWidth) && (e.Y < ClientRectangle.Height))
  159. myBorder.SelectedBorder = BorderPosition.Bottom;
  160. else
  161. myBorder.SelectedBorder = BorderPosition.None;
  162. }
  163. oldPos.X = e.X;
  164. oldPos.Y = e.Y;
  165. Parent.Refresh();
  166. }
  167. private void checkedListBox1_MouseEnter(object sender, EventArgs e)
  168. {
  169. myBorder.SelectedBorder = BorderPosition.None;
  170. }
  171. private void TableView_MouseDown(object sender, MouseEventArgs e)
  172. {
  173. if ((myBorder.SelectedBorder != BorderPosition.None) && (e.Button == MouseButtons.Left))
  174. myBorder.isResize = true;
  175. }
  176. private void TableView_MouseUp(object sender, MouseEventArgs e)
  177. {
  178. myBorder.isResize = false;
  179. }
  180. private void checkedListBox1_MouseDown(object sender, MouseEventArgs e)
  181. {
  182. if (checkedListBox1.SelectedItem != null)
  183. {
  184. Size dragSize = SystemInformation.DragSize;
  185. dragBoxFromMouseDown = new Rectangle(new Point(e.X - (dragSize.Width / 2),
  186. e.Y - (dragSize.Height / 2)), dragSize);
  187. }
  188. else
  189. dragBoxFromMouseDown = Rectangle.Empty;
  190. }
  191. private void checkedListBox1_MouseMove(object sender, MouseEventArgs e)
  192. {
  193. if ((e.Button & MouseButtons.Left) == MouseButtons.Left)
  194. {
  195. if ((dragBoxFromMouseDown != Rectangle.Empty) &&
  196. !dragBoxFromMouseDown.Contains(e.X, e.Y))
  197. {
  198. checkedListBox1.DoDragDrop(checkedListBox1.SelectedItem, DragDropEffects.Copy);
  199. }
  200. }
  201. }
  202. private void checkedListBox1_MouseUp(object sender, MouseEventArgs e)
  203. {
  204. dragBoxFromMouseDown = Rectangle.Empty;
  205. }
  206. private void checkedListBox1_DragOver(object sender, DragEventArgs e)
  207. {
  208. TableView _sender = ((sender as Control).Parent as TableView);
  209. int n = _sender.checkedListBox1.IndexFromPoint(_sender.checkedListBox1.PointToClient(new Point(e.X, e.Y)));
  210. Field _sended = _sender.checkedListBox1.Items[n] as Field;
  211. Field _current = (Field)e.Data.GetData(typeof(Field));
  212. if (_current.CanLink(_sended))
  213. e.Effect = DragDropEffects.Copy;
  214. else
  215. e.Effect = DragDropEffects.None;
  216. }
  217. private void checkedListBox1_DragDrop(object sender, DragEventArgs e)
  218. {
  219. TableView _sender = ((sender as Control).Parent as TableView);
  220. int n = _sender.checkedListBox1.IndexFromPoint(_sender.checkedListBox1.PointToClient(new Point(e.X, e.Y)));
  221. Field _sended = _sender.checkedListBox1.Items[n] as Field;
  222. Field _current = (Field)e.Data.GetData(typeof(Field));
  223. if (_current.CanLink(_sended))
  224. {
  225. if (OnAddLink != null)
  226. OnAddLink(sender, new AddLinkEventArgs(_current, _sended));
  227. if (Parent != null)
  228. Parent.Refresh();
  229. }
  230. }
  231. private void pictureBox1_Click(object sender, EventArgs e)
  232. {
  233. this.Dispose();
  234. if (OnDeleteTable != null)
  235. {
  236. AddTableEventArgs ate = new AddTableEventArgs(this.table, new Point());
  237. OnDeleteTable(sender, ate);
  238. }
  239. }
  240. private void pictureBox2_Click(object sender, EventArgs e)
  241. {
  242. InputBox box = new InputBox();
  243. box.TextBox.Text = table.Alias;
  244. if (box.ShowDialog() == DialogResult.OK)
  245. {
  246. if (OnChangeAlias != null)
  247. OnChangeAlias(sender, e);
  248. table.Alias = box.TextBox.Text;
  249. label1.Text = table.getNameAndAlias();
  250. }
  251. }
  252. private void TableView_Paint(object sender, PaintEventArgs e)
  253. {
  254. Parent.Update();
  255. }
  256. private void checkedListBox1_ItemCheck(object sender, ItemCheckEventArgs e)
  257. {
  258. if (OnSelectField != null)
  259. {
  260. if (checkedListBox1.Items[e.Index] is Field)
  261. {
  262. Field field = checkedListBox1.Items[e.Index] as Field;
  263. CheckFieldEventArgs e2 = new CheckFieldEventArgs(field);
  264. e2.value = e.NewValue == CheckState.Checked;
  265. OnSelectField(sender, e2);
  266. checkedListBox1.ItemCheck -= checkedListBox1_ItemCheck;
  267. checkedListBox1.SetItemChecked(0, false);
  268. checkedListBox1.ItemCheck += checkedListBox1_ItemCheck;
  269. }
  270. else
  271. {
  272. for (int i = 1; i < checkedListBox1.Items.Count; i++)
  273. {
  274. checkedListBox1.SetItemChecked(i, e.NewValue == CheckState.Checked);
  275. }
  276. }
  277. }
  278. }
  279. private void TableView_Move(object sender, EventArgs e)
  280. {
  281. if (Parent != null)
  282. Parent.Refresh();
  283. }
  284. private void checkedListBox1_Scroll(object sender, ScrollEventArgs e)
  285. {
  286. if (Parent != null)
  287. Parent.Refresh();
  288. }
  289. private void pictureBox3_Click(object sender, EventArgs e)
  290. {
  291. if (Height <= label1.Height + 6)
  292. Height = oldSize.Height;
  293. else
  294. {
  295. oldSize = this.Size;
  296. Height = label1.Height + 6;
  297. }
  298. Parent.Refresh();
  299. }
  300. public bool SelectCheckBox(string fieldName, string function, string alias)
  301. {
  302. for (int i = 0; i < checkedListBox1.Items.Count; i++)
  303. {
  304. Field f = checkedListBox1.Items[i] as Field;
  305. if (f != null && f.Name == fieldName)
  306. {
  307. if (!String.IsNullOrEmpty(function))
  308. f.Func = function;
  309. if (!String.IsNullOrEmpty(alias))
  310. f.Alias = alias;
  311. checkedListBox1.SetItemChecked(i, true);
  312. return true;
  313. }
  314. }
  315. return false;
  316. }
  317. public void ScaleCaption()
  318. {
  319. label1.AutoSize = false;
  320. label1.Height = pictureBox1.Bottom;
  321. }
  322. }
  323. }