AdvMatrixObject.DragDrop.cs 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. using System;
  2. using System.Drawing;
  3. using System.Windows.Forms;
  4. using FastReport.Data;
  5. using FastReport.Table;
  6. using FastReport.Utils;
  7. namespace FastReport.AdvMatrix
  8. {
  9. public partial class AdvMatrixObject
  10. {
  11. #region Fields
  12. internal DragInfo dragInfo = new DragInfo();
  13. #endregion
  14. #region Properties
  15. private bool DragCellMode
  16. {
  17. get { return dragInfo.SelectedCell != null && dragInfo.DragSelectedCell; }
  18. }
  19. #endregion
  20. #region Private Methods
  21. #endregion
  22. #region Public Methods
  23. /// <inheritdoc/>
  24. public override void SelectionChanged()
  25. {
  26. base.SelectionChanged();
  27. dragInfo.SelectedCell = null;
  28. dragInfo.Source = MatrixElement.None;
  29. if (!IsSelected || Report.Designer.SelectedObjects.Count != 1)
  30. return;
  31. TableCell cell = Report.Designer.SelectedObjects[0] as TableCell;
  32. if (cell == null)
  33. return;
  34. MatrixElement el = cell.GetMatrixElement();
  35. if (el == MatrixElement.Column || el == MatrixElement.Row)
  36. {
  37. dragInfo.SelectedCell = cell;
  38. dragInfo.Source = el;
  39. }
  40. }
  41. /// <inheritdoc/>
  42. public override void HandleMouseDown(FRMouseEventArgs e)
  43. {
  44. if (DragCellMode)
  45. {
  46. e.handled = true;
  47. e.mode = WorkspaceMode2.Custom;
  48. e.activeObject = this;
  49. }
  50. else
  51. base.HandleMouseDown(e);
  52. }
  53. /// <inheritdoc/>
  54. public override void HandleMouseMove(FRMouseEventArgs e)
  55. {
  56. if (DragCellMode && e.button == MouseButtons.Left)
  57. {
  58. HandleDragOver(e);
  59. }
  60. else
  61. {
  62. base.HandleMouseMove(e);
  63. if (dragInfo.SelectedCell != null && e.button == MouseButtons.None)
  64. {
  65. PointF point = new PointF(e.x, e.y);
  66. RectangleF innerRect = dragInfo.SelectedCell.AbsBounds;
  67. RectangleF outerRect = innerRect;
  68. innerRect.Inflate(-3, -3);
  69. outerRect.Inflate(3, 3);
  70. dragInfo.DragSelectedCell = outerRect.Contains(point) && !innerRect.Contains(point);
  71. if (dragInfo.DragSelectedCell)
  72. {
  73. e.DragSource = dragInfo.SelectedCell;
  74. e.handled = true;
  75. e.cursor = Cursors.SizeAll;
  76. }
  77. }
  78. }
  79. }
  80. /// <inheritdoc/>
  81. public override void HandleMouseUp(FRMouseEventArgs e)
  82. {
  83. if (DragCellMode)
  84. {
  85. HandleDragDrop(e);
  86. Report.Designer.SetModified(this, "Change");
  87. Report.Designer.SelectedObjects.Clear();
  88. Report.Designer.SelectedObjects.Add(dragInfo.SelectedCell);
  89. dragInfo.DragSelectedCell = false;
  90. }
  91. else
  92. base.HandleMouseUp(e);
  93. }
  94. /// <inheritdoc/>
  95. public override void HandleDragOver(FRMouseEventArgs e)
  96. {
  97. // matrix that is defined in the base report cannot be configured this way
  98. if (IsAncestor)
  99. return;
  100. dragInfo.Target = MatrixElement.None;
  101. dragInfo.TargetCell = null;
  102. dragInfo.Indicator = Rectangle.Empty;
  103. if (!(e.DragSource is TextObject))
  104. return;
  105. PointF point = new PointF(e.x, e.y);
  106. for (int x = 0; x < Columns.Count; x++)
  107. {
  108. for (int y = 0; y < Rows.Count; y++)
  109. {
  110. TableCell cell = this[x, y];
  111. if (cell != e.DragSource && cell.PointInObject(point))
  112. {
  113. MatrixElement el = cell.GetMatrixElement();
  114. if (el == MatrixElement.Column)
  115. {
  116. dragInfo.SetHeaderTargetInfo(cell, point);
  117. e.dragMessage = Res.Get("ComponentsMisc,Matrix,NewColumn");
  118. }
  119. else if (el == MatrixElement.Row)
  120. {
  121. dragInfo.SetHeaderTargetInfo(cell, point);
  122. e.dragMessage = Res.Get("ComponentsMisc,Matrix,NewRow");
  123. }
  124. else if (el == MatrixElement.Cell && dragInfo.Source == MatrixElement.None)
  125. {
  126. dragInfo.SetCellTargetInfo(cell, point);
  127. e.dragMessage = Res.Get("ComponentsMisc,Matrix,NewCell");
  128. }
  129. else if (dragInfo.Source == MatrixElement.None)
  130. {
  131. dragInfo.Target = el;
  132. dragInfo.TargetCell = cell;
  133. }
  134. e.handled = true;
  135. return;
  136. }
  137. }
  138. }
  139. e.handled = PointInObject(point);
  140. }
  141. /// <inheritdoc/>
  142. public override void HandleDragDrop(FRMouseEventArgs e)
  143. {
  144. if (e.DragSource == null)
  145. return;
  146. TextObject draggedText = e.DragSource as TextObject;
  147. if (dragInfo.Target == MatrixElement.Column || dragInfo.Target == MatrixElement.Row)
  148. {
  149. HeaderDescriptor sourceDescr = dragInfo.SelectedCell != null ? dragInfo.SelectedCell.GetHeaderDescriptor() : null;
  150. HeaderDescriptor targetDescr = dragInfo.TargetCell.GetHeaderDescriptor();
  151. HeaderDescriptor newItem = sourceDescr;
  152. HeaderInsertPosition position = dragInfo.InsertPosition;
  153. if (sourceDescr == null)
  154. {
  155. // we're inserting a new item from the Data window
  156. newItem = new HeaderDescriptor();
  157. newItem.Expression = draggedText.Text;
  158. }
  159. else
  160. {
  161. // we're reordering header items
  162. // Whole descriptor tree can be inserted before or after target item, or as a child if there is no children in the target.
  163. // Also target should not be a child of source. In other cases insert selected descriptor only
  164. bool canDragItemTree = !targetDescr.HasParent(sourceDescr) &&
  165. position != HeaderInsertPosition.Parent &&
  166. (position != HeaderInsertPosition.Child || targetDescr.Items.Count == 0);
  167. // Delete source descriptor from its current position.
  168. if (canDragItemTree)
  169. sourceDescr.DeleteTree();
  170. else
  171. sourceDescr.DeleteThis();
  172. // item from another header: copy template items to fill in the cell area properly
  173. if (dragInfo.Source != dragInfo.Target)
  174. {
  175. newItem.TemplateColumn = targetDescr.TemplateColumn;
  176. newItem.TemplateRow = targetDescr.TemplateRow;
  177. }
  178. }
  179. switch (position)
  180. {
  181. case HeaderInsertPosition.After:
  182. targetDescr.InsertAfter(newItem);
  183. break;
  184. case HeaderInsertPosition.Before:
  185. targetDescr.InsertBefore(newItem);
  186. break;
  187. case HeaderInsertPosition.Parent:
  188. targetDescr.InsertParent(newItem);
  189. break;
  190. case HeaderInsertPosition.Child:
  191. targetDescr.InsertChild(newItem);
  192. break;
  193. case HeaderInsertPosition.Inside:
  194. targetDescr.InsertAfter(newItem);
  195. targetDescr.DeleteThis();
  196. break;
  197. }
  198. BuildTemplate();
  199. FixParentHeight();
  200. e.dragTarget = newItem.TemplateCell;
  201. dragInfo.SelectedCell = newItem.TemplateCell;
  202. if (sourceDescr == null)
  203. newItem.TemplateCell.Format = draggedText.Format;
  204. }
  205. else if (dragInfo.Target == MatrixElement.Cell)
  206. {
  207. TableCell cell = dragInfo.TargetCell;
  208. switch (dragInfo.Position)
  209. {
  210. case DragPosition.Right:
  211. cell.InsertAfter(draggedText);
  212. break;
  213. case DragPosition.Left:
  214. cell.InsertBefore(draggedText);
  215. break;
  216. case DragPosition.Top:
  217. cell.InsertAbove(draggedText);
  218. break;
  219. case DragPosition.Bottom:
  220. cell.InsertBelow(draggedText);
  221. break;
  222. case DragPosition.Inside:
  223. cell.InsertInside(draggedText);
  224. break;
  225. }
  226. FixParentHeight();
  227. }
  228. else if (dragInfo.Target == MatrixElement.Corner)
  229. {
  230. dragInfo.TargetCell.Text = draggedText.Text;
  231. }
  232. if (DataSource == null)
  233. {
  234. string text = draggedText.Text;
  235. if (text.StartsWith("[") && text.EndsWith("]"))
  236. text = text.Substring(1, text.Length - 2);
  237. DataSource = DataHelper.GetDataSource(Report.Dictionary, text);
  238. }
  239. dragInfo.Target = MatrixElement.None;
  240. dragInfo.TargetCell = null;
  241. dragInfo.Source = MatrixElement.None;
  242. }
  243. #endregion
  244. }
  245. }