CPDFViewerTool.SelectedRect.cs 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. using ComPDFKit.Tool.DrawTool;
  2. using ComPDFKitViewer.Layer;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. using System.Windows.Input;
  9. using System.Windows;
  10. using ComPDFKit.Viewer.Layer;
  11. using System.Windows.Media;
  12. using ComPDFKit.Tool.Help;
  13. using ComPDFKitViewer;
  14. using ComPDFKit.PDFAnnotation;
  15. namespace ComPDFKit.Tool
  16. {
  17. public partial class CPDFViewerTool
  18. {
  19. internal bool isDrawSelectRect = false;
  20. int selectedRectViewTag = -1;
  21. private bool isOutSideScaling = false;
  22. public event EventHandler<SelectedAnnotData> SelectedDataChanging;
  23. public event EventHandler<SelectedAnnotData> SelectedDataChanged;
  24. private void InsertSelectedRectView()
  25. {
  26. int selectedRectViewIndex = PDFViewer.GetMaxViewIndex();
  27. CustomizeLayer customizeLayer = new CustomizeLayer();
  28. SelectedRect selectedRect = new SelectedRect(GetDefaultDrawParam(), SelectedType.Annot);
  29. selectedRect.SetDrawMoveType(DrawMoveType.kDefault);
  30. customizeLayer.Children.Add(selectedRect);
  31. selectedRect.DataChanged += SelectedRect_DataChanged;
  32. selectedRect.DataChanging += SelectedRect_DataChanging;
  33. PDFViewer.InsertView(selectedRectViewIndex, customizeLayer);
  34. selectedRectViewTag = customizeLayer.GetResTag();
  35. }
  36. /// <summary>
  37. /// Set whether the border can be moved outside the border
  38. /// </summary>
  39. /// <param name="IsOutSideScaling"></param>
  40. public void SetOutSideScaling(bool IsOutSideScaling)
  41. {
  42. isOutSideScaling = IsOutSideScaling;
  43. }
  44. private void SelectedRect_DataChanging(object sender, SelectedAnnotData e)
  45. {
  46. SelectedDataChanging?.Invoke(this, e);
  47. }
  48. private void SelectedRect_DataChanged(object sender, SelectedAnnotData e)
  49. {
  50. SelectedDataChanged?.Invoke(this, e);
  51. }
  52. /// <summary>
  53. /// Start to draw the rectangle
  54. /// </summary>
  55. public void DrawStartSelectedRect()
  56. {
  57. Point point = Mouse.GetPosition(this);
  58. BaseLayer baseLayer = PDFViewer.GetViewForTag(selectedRectViewTag);
  59. SelectedRect selectedRect = CommonHelper.FindVisualChild<SelectedRect>(baseLayer as CustomizeLayer);
  60. if (selectedRect != null)
  61. {
  62. selectedRect.Draw();
  63. selectedRect.OnMouseLeftButtonDown(point);
  64. isDrawSelectRect = true;
  65. }
  66. }
  67. public SelectedRect GetSelectedRect()
  68. {
  69. Point point = Mouse.GetPosition(this);
  70. BaseLayer baseLayer = PDFViewer.GetViewForTag(selectedRectViewTag);
  71. SelectedRect selectedRect = CommonHelper.FindVisualChild<SelectedRect>(baseLayer as CustomizeLayer);
  72. if (selectedRect != null && isDrawSelectRect == true)
  73. {
  74. return selectedRect;
  75. }
  76. return null;
  77. }
  78. public Cursor GetMoveSelectedRectCursor()
  79. {
  80. Point point = Mouse.GetPosition(this);
  81. BaseLayer baseLayer = PDFViewer.GetViewForTag(selectedRectViewTag);
  82. SelectedRect selectedRect = CommonHelper.FindVisualChild<SelectedRect>(baseLayer as CustomizeLayer);
  83. if (selectedRect != null)
  84. {
  85. return selectedRect.GetCursor(point, this.Cursor);
  86. }
  87. return this.Cursor;
  88. }
  89. /// <summary>
  90. /// Draw the rectangle when dragging
  91. /// </summary>
  92. public bool DrawMoveSelectedRect()
  93. {
  94. bool DrawTag = false;
  95. Point point = Mouse.GetPosition(this);
  96. BaseLayer baseLayer = PDFViewer.GetViewForTag(selectedRectViewTag);
  97. SelectedRect selectedRect = CommonHelper.FindVisualChild<SelectedRect>(baseLayer as CustomizeLayer);
  98. if (selectedRect != null)
  99. {
  100. selectedRect.SetOutSideScaling(isOutSideScaling);
  101. selectedRect.OnMouseMove(point, out DrawTag, PDFViewer.ActualWidth, PDFViewer.ActualHeight);
  102. selectedRect.Draw();
  103. }
  104. return DrawTag;
  105. }
  106. /// <summary>
  107. /// End of drawing the rectangle
  108. /// </summary>
  109. public void DrawEndSelectedRect()
  110. {
  111. Point point = Mouse.GetPosition(this);
  112. BaseLayer baseLayer = PDFViewer.GetViewForTag(selectedRectViewTag);
  113. SelectedRect selectedRect = CommonHelper.FindVisualChild<SelectedRect>(baseLayer as CustomizeLayer);
  114. if (selectedRect != null)
  115. {
  116. selectedRect.OnMouseLeftButtonUp(point);
  117. selectedRect.Draw();
  118. }
  119. }
  120. /// <summary>
  121. /// Clear the rectangle drawing
  122. /// </summary>
  123. public void CleanSelectedRect()
  124. {
  125. Point point = Mouse.GetPosition(this);
  126. BaseLayer baseLayer = PDFViewer.GetViewForTag(selectedRectViewTag);
  127. SelectedRect selectedRect = CommonHelper.FindVisualChild<SelectedRect>(baseLayer as CustomizeLayer);
  128. if (selectedRect != null)
  129. {
  130. selectedRect.ClearDraw();
  131. isDrawSelectRect = false;
  132. }
  133. }
  134. internal void SelectedAnnot()
  135. {
  136. if (!isHitTestLink && cacheHitTestAnnot?.CurrentType == C_ANNOTATION_TYPE.C_ANNOTATION_LINK)
  137. {
  138. return;
  139. }
  140. if (isHitTestRedact && cacheHitTestAnnot?.CurrentType != C_ANNOTATION_TYPE.C_ANNOTATION_REDACT)
  141. {
  142. return;
  143. }
  144. SelectAnnot(cacheHitTestAnnot.GetAnnotData());
  145. }
  146. internal void SelectAnnot(AnnotData selectData)
  147. {
  148. BaseLayer baseLayer = PDFViewer.GetViewForTag(selectedRectViewTag);
  149. SelectedRect selectedRect = CommonHelper.FindVisualChild<SelectedRect>(baseLayer as CustomizeLayer);
  150. if (selectedRect != null)
  151. {
  152. selectedRect.SetAnnotData(selectData);
  153. }
  154. }
  155. private void SelectedAnnot(AnnotData annotData)
  156. {
  157. BaseLayer baseLayer = PDFViewer.GetViewForTag(selectedRectViewTag);
  158. SelectedRect selectedRect = CommonHelper.FindVisualChild<SelectedRect>(baseLayer as CustomizeLayer);
  159. if (selectedRect != null)
  160. {
  161. if (annotData == null)
  162. {
  163. selectedRect.ClearDraw();
  164. }
  165. else
  166. {
  167. selectedRect.SetAnnotData(annotData);
  168. }
  169. }
  170. }
  171. /// <summary>
  172. /// Refresh the drawing
  173. /// </summary>
  174. internal void DrawSelectedLayer()
  175. {
  176. BaseLayer baseLayer = PDFViewer.GetViewForTag(selectedRectViewTag);
  177. SelectedRect selectedRect = CommonHelper.FindVisualChild<SelectedRect>(baseLayer as CustomizeLayer);
  178. if (selectedRect != null)
  179. {
  180. selectedRect.Draw();
  181. }
  182. }
  183. /// <summary>
  184. /// Identify whether the mouse is on the rectangle
  185. /// </summary>
  186. /// <returns></returns>
  187. internal bool DrawSelectRectDownEvent()
  188. {
  189. BaseLayer baseLayer = PDFViewer.GetViewForTag(selectedRectViewTag);
  190. SelectedRect selectedRect = CommonHelper.FindVisualChild<SelectedRect>(baseLayer as CustomizeLayer);
  191. if (selectedRect != null)
  192. {
  193. if (selectedRect.GetHitControlIndex(Mouse.GetPosition(this)) != PointControlType.None)
  194. {
  195. return true;
  196. }
  197. }
  198. return false;
  199. }
  200. internal AnnotData GetSelectAnnotData()
  201. {
  202. BaseLayer baseLayer = PDFViewer.GetViewForTag(selectedRectViewTag);
  203. SelectedRect selectedRect = CommonHelper.FindVisualChild<SelectedRect>(baseLayer as CustomizeLayer);
  204. if (selectedRect != null && isDrawSelectRect == true)
  205. {
  206. return selectedRect.GetAnnotData();
  207. }
  208. return null;
  209. }
  210. }
  211. }