DrawTool.cpp
上传用户:seaboy_04
上传日期:2013-02-24
资源大小:284k
文件大小:9k
源码类别:

其他行业

开发平台:

Visual C++

  1. // DrawTool.cpp: implementation of the CDrawTool class.
  2. //
  3. //////////////////////////////////////////////////////////////////////
  4. #include "stdafx.h"
  5. #include "DrawCli.h"
  6. #include "DrawTool.h"
  7.  
  8. #include "drawdoc.h"
  9. #include "drawvw.h"
  10. #include "drawobj.h"
  11.  
  12. #ifdef _DEBUG
  13. #undef THIS_FILE
  14. static char THIS_FILE[]=__FILE__;
  15. #define new DEBUG_NEW
  16. #endif
  17. //////////////////////////////////////////////////////////////////////
  18. // Construction/Destruction
  19. //////////////////////////////////////////////////////////////////////
  20. /////////////////////////////////////////////////////////////////////////////
  21. // CDrawTool implementation
  22. CPtrList CDrawTool::c_tools;
  23. static CSelectTool selectTool;
  24. static CRectTool lineTool(line);
  25. static CRectTool rectTool(rect);
  26. static CRectTool roundRectTool(roundRect);
  27. static CRectTool ellipseTool(ellipse);
  28. static CRectTool circleTool(circle);
  29. //static CPolyTool polyTool;
  30. CPoint CDrawTool::c_down;
  31. UINT CDrawTool::c_nDownFlags;
  32. CPoint CDrawTool::c_last;
  33. DrawShape CDrawTool::c_drawShape = selection;
  34. CDrawTool::CDrawTool(DrawShape drawShape)
  35. {
  36. m_drawShape = drawShape;
  37. c_tools.AddTail(this);
  38. }
  39. CDrawTool* CDrawTool::FindTool(DrawShape drawShape)
  40. {
  41. POSITION pos = c_tools.GetHeadPosition();
  42. while (pos != NULL)
  43. {
  44. CDrawTool* pTool = (CDrawTool*)c_tools.GetNext(pos);
  45. if (pTool->m_drawShape == drawShape)
  46. return pTool;
  47. }
  48. return NULL;
  49. }
  50. void CDrawTool::OnLButtonDown(CDrawView* pView, UINT nFlags, const CPoint& point)
  51. {
  52. // deactivate any in-place active item on this view!
  53. COleClientItem* pActiveItem = pView->GetDocument()->GetInPlaceActiveItem(pView);
  54. if (pActiveItem != NULL)
  55. {
  56. pActiveItem->Close();
  57. ASSERT(pView->GetDocument()->GetInPlaceActiveItem(pView) == NULL);
  58. }
  59. pView->SetCapture();
  60. c_nDownFlags = nFlags;
  61. c_down = point;
  62. c_last = point;
  63. }
  64. void CDrawTool::OnLButtonDblClk(CDrawView* /*pView*/, UINT /*nFlags*/, const CPoint& /*point*/)
  65. {
  66. }
  67. void CDrawTool::OnLButtonUp(CDrawView* /*pView*/, UINT /*nFlags*/, const CPoint& point)
  68. {
  69. ReleaseCapture();
  70. if (point == c_down)
  71. c_drawShape = selection;
  72. }
  73. void CDrawTool::OnMouseMove(CDrawView* /*pView*/, UINT /*nFlags*/, const CPoint& point)
  74. {
  75. c_last = point;
  76. SetCursor(AfxGetApp()->LoadStandardCursor(IDC_ARROW));
  77. }
  78. void CDrawTool::OnEditProperties(CDrawView* /*pView*/)
  79. {
  80. }
  81. void CDrawTool::OnCancel()
  82. {
  83. c_drawShape = selection;
  84. }
  85. ////////////////////////////////////////////////////////////////////////////
  86. // CResizeTool
  87. enum SelectMode
  88. {
  89. none,
  90. netSelect,
  91. move,
  92. size
  93. };
  94. SelectMode selectMode = none;
  95. int nDragHandle;
  96. CPoint lastPoint;
  97. CSelectTool::CSelectTool()
  98. : CDrawTool(selection)
  99. {
  100. }
  101. void CSelectTool::OnLButtonDown(CDrawView* pView, UINT nFlags, const CPoint& point)
  102. {
  103. CPoint local = point;
  104. pView->ClientToDoc(local);
  105. CDrawObj* pObj;
  106. selectMode = none;
  107. // Check for resizing (only allowed on single selections)
  108. if (pView->m_selection.GetCount() == 1)
  109. {
  110. pObj = pView->m_selection.GetHead();
  111. nDragHandle = pObj->HitTest(local, pView, TRUE);
  112. if (nDragHandle != 0)
  113. selectMode = size;
  114. }
  115. // See if the click was on an object, select and start move if so
  116. if (selectMode == none)
  117. {
  118. pObj = pView->GetDocument()->ObjectAt(local);
  119. if (pObj != NULL)
  120. {
  121. selectMode = move;
  122. if (!pView->IsSelected(pObj))
  123. pView->Select(pObj, (nFlags & MK_SHIFT) != 0);
  124. // Ctrl+Click clones the selection...
  125. #ifndef _MAC
  126. if ((nFlags & MK_CONTROL) != 0)
  127. #else
  128. if ((nFlags & MK_OPTION) != 0)
  129. #endif
  130. pView->CloneSelection();
  131. }
  132. }
  133.  
  134. // Click on background, start a net-selection
  135. if (selectMode == none)
  136. {
  137. if ((nFlags & MK_SHIFT) == 0)
  138. pView->Select(NULL);
  139. selectMode = netSelect;
  140. CClientDC dc(pView);
  141. CRect rect(point.x, point.y, point.x, point.y);
  142. rect.NormalizeRect();
  143. dc.DrawFocusRect(rect);
  144. }
  145. lastPoint = local;
  146. CDrawTool::OnLButtonDown(pView, nFlags, point);
  147.   
  148. }
  149. void CSelectTool::OnLButtonDblClk(CDrawView* pView, UINT nFlags, const CPoint& point)
  150. {
  151. if ((nFlags & MK_SHIFT) != 0)
  152. {
  153. // Shift+DblClk deselects object...
  154. CPoint local = point;
  155. pView->ClientToDoc(local);
  156. CDrawObj* pObj = pView->GetDocument()->ObjectAt(local);
  157. if (pObj != NULL)
  158. pView->Deselect(pObj);
  159. }
  160. else
  161. {
  162. // "Normal" DblClk opens properties, or OLE server...
  163. if (pView->m_selection.GetCount() == 1)
  164. pView->m_selection.GetHead()->OnOpen(pView);
  165. }
  166. CDrawTool::OnLButtonDblClk(pView, nFlags, point);
  167. }
  168. void CSelectTool::OnEditProperties(CDrawView* pView)
  169. {
  170. if (pView->m_selection.GetCount() == 1)
  171. pView->m_selection.GetHead()->OnEditProperties();
  172. }
  173. void CSelectTool::OnLButtonUp(CDrawView* pView, UINT nFlags, const CPoint& point)
  174. {
  175. if (pView->GetCapture() == pView)
  176. {
  177. if (selectMode == netSelect)
  178. {
  179. CClientDC dc(pView);
  180. CRect rect(c_down.x, c_down.y, c_last.x, c_last.y);
  181. rect.NormalizeRect();
  182. dc.DrawFocusRect(rect);
  183. pView->SelectWithinRect(rect, TRUE);
  184. }
  185. else if (selectMode != none)
  186. {
  187. pView->GetDocument()->UpdateAllViews(pView);
  188. }
  189. }
  190. CDrawTool::OnLButtonUp(pView, nFlags, point);
  191. }
  192. void CSelectTool::OnMouseMove(CDrawView* pView, UINT nFlags, const CPoint& point)
  193. {
  194. if (pView->GetCapture() != pView)
  195. {
  196. if (c_drawShape == selection && pView->m_selection.GetCount() == 1)
  197. {
  198. CDrawObj* pObj = pView->m_selection.GetHead();
  199. CPoint local = point;
  200. pView->ClientToDoc(local);
  201. int nHandle = pObj->HitTest(local, pView, TRUE);
  202. if (nHandle != 0)
  203. {
  204. SetCursor(pObj->GetHandleCursor(nHandle));
  205. return; // bypass CDrawTool
  206. }
  207. }
  208. if (c_drawShape == selection)
  209. CDrawTool::OnMouseMove(pView, nFlags, point);
  210. return;
  211. }
  212. if (selectMode == netSelect)
  213. {
  214. CClientDC dc(pView);
  215. CRect rect(c_down.x, c_down.y, c_last.x, c_last.y);
  216. rect.NormalizeRect();
  217. dc.DrawFocusRect(rect);
  218. rect.SetRect(c_down.x, c_down.y, point.x, point.y);
  219. rect.NormalizeRect();
  220. dc.DrawFocusRect(rect);
  221. CDrawTool::OnMouseMove(pView, nFlags, point);
  222. return;
  223. }
  224. CPoint local = point;
  225. pView->ClientToDoc(local);
  226. CPoint delta = (CPoint)(local - lastPoint);
  227. POSITION pos = pView->m_selection.GetHeadPosition();
  228. while (pos != NULL)
  229. {
  230. CDrawObj* pObj = pView->m_selection.GetNext(pos);
  231. CRect position = pObj->m_position;
  232. if (selectMode == move)
  233. {
  234. position += delta;
  235. pObj->MoveTo(position, pView);
  236. }
  237. else if (nDragHandle != 0)
  238. {
  239. pObj->MoveHandleTo(nDragHandle, local, pView);
  240. }
  241. }
  242. lastPoint = local;
  243. if (selectMode == size && c_drawShape == selection)
  244. {
  245. c_last = point;
  246. SetCursor(pView->m_selection.GetHead()->GetHandleCursor(nDragHandle));
  247. return; // bypass CDrawTool
  248. }
  249. c_last = point;
  250. if (c_drawShape == selection)
  251. CDrawTool::OnMouseMove(pView, nFlags, point);
  252. }
  253. ////////////////////////////////////////////////////////////////////////////
  254. // CRectTool (does rectangles, round-rectangles, and ellipses)
  255. CRectTool::CRectTool(DrawShape drawShape)
  256. : CDrawTool(drawShape)
  257. {
  258. }
  259. void CRectTool::OnLButtonDown(CDrawView* pView, UINT nFlags, const CPoint& point)
  260. {
  261. CDrawTool::OnLButtonDown(pView, nFlags, point);
  262. CPoint local = point;
  263. pView->ClientToDoc(local);
  264. CDrawRect* pObj = new CDrawRect(CRect(local, CSize(0, 0)));
  265. switch (m_drawShape)
  266. {
  267. default:
  268. ASSERT(FALSE); // unsuported shape!
  269. case rect:
  270. pObj->m_nShape = CDrawRect::rectangle;
  271. break;
  272. case roundRect:
  273. pObj->m_nShape = CDrawRect::roundRectangle;
  274. break;
  275. case ellipse:
  276. pObj->m_nShape = CDrawRect::ellipse;
  277. break;
  278. case line:
  279. pObj->m_nShape = CDrawRect::line;
  280. break;
  281. case circle:
  282. pObj->m_nShape = CDrawRect::circle;
  283. break;
  284. }
  285. pView->GetDocument()->Add(pObj);
  286. pView->Select(pObj);
  287. selectMode = size;
  288. nDragHandle = 1;
  289. lastPoint = local;
  290. }
  291. void CRectTool::OnLButtonDblClk(CDrawView* pView, UINT nFlags, const CPoint& point)
  292. {
  293. CDrawTool::OnLButtonDblClk(pView, nFlags, point);
  294. }
  295. void CRectTool::OnLButtonUp(CDrawView* pView, UINT nFlags, const CPoint& point)
  296. {  
  297. if (point == c_down)
  298. {
  299. // Don't create empty objects...
  300. CDrawObj *pObj = pView->m_selection.GetTail();
  301. pView->GetDocument()->Remove(pObj);
  302. pObj->Remove();
  303. selectTool.OnLButtonDown(pView, nFlags, point); // try a select!
  304. }
  305. selectTool.OnLButtonUp(pView, nFlags, point);
  306. }
  307. void CRectTool::OnMouseMove(CDrawView* pView, UINT nFlags, const CPoint& point)
  308. {
  309. SetCursor(AfxGetApp()->LoadStandardCursor(IDC_CROSS));
  310. selectTool.OnMouseMove(pView, nFlags, point);
  311. }