HDrawDoc.cpp 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  1. // HDrawDoc.cpp : implementation of the CHDrawDoc class
  2. // Download by http://www.codefans.net
  3. #include "stdafx.h"
  4. #include "HDraw.h"
  5. #include "HDrawDoc.h"
  6. #ifdef _DEBUG
  7. #define new DEBUG_NEW
  8. #undef THIS_FILE
  9. static char THIS_FILE[] = __FILE__;
  10. #endif
  11. /////////////////////////////////////////////////////////////////////////////
  12. // CHDrawDoc
  13. IMPLEMENT_DYNCREATE(CHDrawDoc, CDocument)
  14. BEGIN_MESSAGE_MAP(CHDrawDoc, CDocument)
  15. //{{AFX_MSG_MAP(CHDrawDoc)
  16. ON_COMMAND(ID_DRAW_CLEAR, OnDrawClear)
  17. ON_COMMAND(ID_DRAW_ELLIPSE, OnDrawEllipse)
  18. ON_COMMAND(ID_DRAW_LINE, OnDrawLine)
  19. ON_COMMAND(ID_DRAW_POLY, OnDrawPoly)
  20. ON_COMMAND(ID_DRAW_RECT, OnDrawRect)
  21. ON_COMMAND(ID_DRAW_SET, OnDrawSet)
  22. ON_COMMAND(ID_DRAW_TEXT, OnDrawText)
  23. ON_COMMAND(ID_BT_W1, OnBtW1)
  24. ON_COMMAND(ID_BT_W2, OnBtW2)
  25. ON_COMMAND(ID_BT_W3, OnBtW3)
  26. ON_UPDATE_COMMAND_UI(ID_BT_W1, OnUpdateBtW1)
  27. ON_UPDATE_COMMAND_UI(ID_BT_W2, OnUpdateBtW2)
  28. ON_UPDATE_COMMAND_UI(ID_BT_W3, OnUpdateBtW3)
  29. ON_UPDATE_COMMAND_UI(ID_DRAW_ELLIPSE, OnUpdateDrawEllipse)
  30. ON_UPDATE_COMMAND_UI(ID_DRAW_LINE, OnUpdateDrawLine)
  31. ON_UPDATE_COMMAND_UI(ID_DRAW_POLY, OnUpdateDrawPoly)
  32. ON_UPDATE_COMMAND_UI(ID_DRAW_RECT, OnUpdateDrawRect)
  33. ON_UPDATE_COMMAND_UI(ID_DRAW_TEXT, OnUpdateDrawText)
  34. ON_COMMAND(ID_PEN_DASH, OnPenDash)
  35. ON_UPDATE_COMMAND_UI(ID_PEN_DASH, OnUpdatePenDash)
  36. ON_COMMAND(ID_PEN_SOLID, OnPenSolid)
  37. ON_UPDATE_COMMAND_UI(ID_PEN_SOLID, OnUpdatePenSolid)
  38. ON_UPDATE_COMMAND_UI(ID_EDIT_UNDO, OnUpdateEditUndo)
  39. ON_UPDATE_COMMAND_UI(ID_EDIT_REDO, OnUpdateEditRedo)
  40. ON_COMMAND(ID_EDIT_REDO, OnEditRedo)
  41. ON_COMMAND(ID_EDIT_UNDO, OnEditUndo)
  42. ON_COMMAND(ID_SELECT, OnSelect)
  43. ON_UPDATE_COMMAND_UI(ID_SELECT, OnUpdateSelect)
  44. //}}AFX_MSG_MAP
  45. END_MESSAGE_MAP()
  46. /////////////////////////////////////////////////////////////////////////////
  47. // CHDrawDoc construction/destruction
  48. CHDrawDoc::CHDrawDoc()
  49. {
  50. m_picType = PIC_line;
  51. m_picColor = RGB(0,0,0);
  52. m_picWidth = 1;
  53. m_picStyle = PS_SOLID;
  54. m_cavasH = 300;
  55. m_cavasW = 600;
  56. m_backup_index = 0;
  57. BackUp();
  58. }
  59. CHDrawDoc::~CHDrawDoc()
  60. {
  61. DeleteContents();
  62. //backup information
  63. for(int i = 0; i < m_backup.GetSize(); i ++){
  64. CFile::Remove(m_backup.GetAt(i));
  65. }
  66. m_backup.RemoveAll();
  67. }
  68. BOOL CHDrawDoc::OnNewDocument()
  69. {
  70. if (!CDocument::OnNewDocument())
  71. return FALSE;
  72. return TRUE;
  73. }
  74. /////////////////////////////////////////////////////////////////////////////
  75. // CHDrawDoc serialization
  76. void CHDrawDoc::Serialize(CArchive& ar)
  77. {
  78. if (ar.IsStoring())
  79. {
  80. // TODO: add storing code here
  81. ar<<m_cavasH<<m_cavasW;
  82. m_strokeList.Serialize(ar);
  83. }
  84. else
  85. {
  86. // TODO: add loading code here
  87. ar>>m_cavasH>>m_cavasW;
  88. m_strokeList.Serialize(ar);
  89. }
  90. }
  91. /////////////////////////////////////////////////////////////////////////////
  92. // CHDrawDoc diagnostics
  93. #ifdef _DEBUG
  94. void CHDrawDoc::AssertValid() const
  95. {
  96. CDocument::AssertValid();
  97. }
  98. void CHDrawDoc::Dump(CDumpContext& dc) const
  99. {
  100. CDocument::Dump(dc);
  101. }
  102. #endif //_DEBUG
  103. HStroke * CHDrawDoc::NewStroke()
  104. {
  105. HStroke *p;
  106. switch(m_picType){
  107. case PIC_line:
  108. p = new HStrokeLine();
  109. break;
  110. case PIC_poly:
  111. p = new HStrokePoly();
  112. break;
  113. case PIC_ellipse:
  114. p = new HStrokeEllipse();
  115. break;
  116. case PIC_rect:
  117. p = new HStrokeRect();
  118. break;
  119. case PIC_text:
  120. p = new HStrokeText(m_picInfo);
  121. break;
  122. case PIC_select:
  123. p = new HStrokeSelect();
  124. break;
  125. default:
  126. p = NULL;
  127. }
  128. p->m_penColor = m_picColor;
  129. p->m_penWidth = m_picWidth;
  130. p->m_penStyle = m_picStyle;
  131. return p;
  132. }
  133. /////////////////////////////////////////////////////////////////////////////
  134. // CHDrawDoc commands
  135. void CHDrawDoc::OnDrawClear()
  136. {
  137. // TODO: Add your command handler code here
  138. m_strokeList.RemoveAll();
  139. UpdateAllViews(NULL);
  140. }
  141. void CHDrawDoc::OnDrawEllipse()
  142. {
  143. // TODO: Add your command handler code here
  144. m_picType = PIC_ellipse;
  145. }
  146. void CHDrawDoc::OnDrawLine()
  147. {
  148. // TODO: Add your command handler code here
  149. m_picType = PIC_line;
  150. }
  151. void CHDrawDoc::OnDrawPoly()
  152. {
  153. // TODO: Add your command handler code here
  154. m_picType = PIC_poly;
  155. }
  156. void CHDrawDoc::OnDrawRect()
  157. {
  158. // TODO: Add your command handler code here
  159. m_picType = PIC_rect;
  160. }
  161. //设置图形参数和其它属性
  162. void CHDrawDoc::OnDrawSet()
  163. {
  164. // TODO: Add your command handler code here
  165. HStrokeEditDlg dlg;
  166. dlg.setStrokeList(&m_strokeList);
  167. dlg.DoModal();
  168. }
  169. void CHDrawDoc::OnDrawText()
  170. {
  171. // TODO: Add your command handler code here
  172. HStrokeTextDlg dlg;
  173. if(dlg.DoModal() == IDOK){
  174. m_picType = PIC_text;
  175. m_picInfo = dlg.m_editText;
  176. }
  177. }
  178. void CHDrawDoc::OnBtW1()
  179. {
  180. m_picWidth = 1;
  181. for(int i = 0; i < m_strokeList.GetSize(); i ++){
  182. if(m_strokeList.GetAt(i)->m_bSelected){
  183. m_strokeList.GetAt(i)->m_penWidth = m_picWidth;
  184. }
  185. }
  186. UpdateAllViews(NULL);
  187. }
  188. void CHDrawDoc::OnBtW2()
  189. {
  190. // TODO: Add your command handler code here
  191. m_picWidth = 2;
  192. for(int i = 0; i < m_strokeList.GetSize(); i ++){
  193. if(m_strokeList.GetAt(i)->m_bSelected){
  194. m_strokeList.GetAt(i)->m_penWidth = m_picWidth;
  195. }
  196. }
  197. UpdateAllViews(NULL);
  198. }
  199. void CHDrawDoc::OnBtW3()
  200. {
  201. // TODO: Add your command handler code here
  202. m_picWidth = 3;
  203. for(int i = 0; i < m_strokeList.GetSize(); i ++){
  204. if(m_strokeList.GetAt(i)->m_bSelected){
  205. m_strokeList.GetAt(i)->m_penWidth = m_picWidth;
  206. }
  207. }
  208. UpdateAllViews(NULL);
  209. }
  210. void CHDrawDoc::OnUpdateBtW1(CCmdUI* pCmdUI)
  211. {
  212. // TODO: Add your command update UI handler code here
  213. pCmdUI->SetCheck(1 == m_picWidth);
  214. }
  215. void CHDrawDoc::OnUpdateBtW2(CCmdUI* pCmdUI)
  216. {
  217. // TODO: Add your command update UI handler code here
  218. pCmdUI->SetCheck(2 == m_picWidth);
  219. }
  220. void CHDrawDoc::OnUpdateBtW3(CCmdUI* pCmdUI)
  221. {
  222. // TODO: Add your command update UI handler code here
  223. pCmdUI->SetCheck(2 < m_picWidth);
  224. }
  225. void CHDrawDoc::OnUpdateDrawEllipse(CCmdUI* pCmdUI)
  226. {
  227. // TODO: Add your command update UI handler code here
  228. pCmdUI->SetCheck(PIC_ellipse == m_picType);
  229. }
  230. void CHDrawDoc::OnUpdateDrawLine(CCmdUI* pCmdUI)
  231. {
  232. // TODO: Add your command update UI handler code here
  233. pCmdUI->SetCheck(PIC_line == m_picType);
  234. }
  235. void CHDrawDoc::OnUpdateDrawPoly(CCmdUI* pCmdUI)
  236. {
  237. // TODO: Add your command update UI handler code here
  238. pCmdUI->SetCheck(PIC_poly == m_picType);
  239. }
  240. void CHDrawDoc::OnUpdateDrawRect(CCmdUI* pCmdUI)
  241. {
  242. // TODO: Add your command update UI handler code here
  243. pCmdUI->SetCheck(PIC_rect == m_picType);
  244. }
  245. void CHDrawDoc::OnUpdateDrawText(CCmdUI* pCmdUI)
  246. {
  247. // TODO: Add your command update UI handler code here
  248. pCmdUI->SetCheck(PIC_text == m_picType);
  249. }
  250. void CHDrawDoc::OnPenDash()
  251. {
  252. // TODO: Add your command handler code here
  253. m_picStyle = PS_DASH;
  254. for(int i = 0; i < m_strokeList.GetSize(); i ++){
  255. if(m_strokeList.GetAt(i)->m_bSelected
  256. ||m_strokeList.GetAt(i)->m_bHighLight){
  257. m_strokeList.GetAt(i)->m_penStyle = m_picStyle;
  258. }
  259. }
  260. BackUp();
  261. UpdateAllViews(NULL);
  262. }
  263. void CHDrawDoc::OnUpdatePenDash(CCmdUI* pCmdUI)
  264. {
  265. // TODO: Add your command update UI handler code here
  266. pCmdUI->SetCheck(PS_DASH == m_picStyle);
  267. }
  268. void CHDrawDoc::OnPenSolid()
  269. {
  270. // TODO: Add your command handler code here
  271. m_picStyle = PS_SOLID;
  272. for(int i = 0; i < m_strokeList.GetSize(); i ++){
  273. if(m_strokeList.GetAt(i)->m_bSelected
  274. ||m_strokeList.GetAt(i)->m_bHighLight){
  275. m_strokeList.GetAt(i)->m_penStyle = m_picStyle;
  276. }
  277. }
  278. BackUp();
  279. UpdateAllViews(NULL);
  280. }
  281. void CHDrawDoc::OnUpdatePenSolid(CCmdUI* pCmdUI)
  282. {
  283. // TODO: Add your command update UI handler code here
  284. pCmdUI->SetCheck(PS_SOLID == m_picStyle);
  285. }
  286. void CHDrawDoc::BackUp()
  287. {
  288. //CArchive cr;
  289. CString fileName;
  290. fileName.Format("C:\\Windows\\Temp\\hankjin.temp.%d", m_backup.GetSize());
  291. OnSaveDocument(fileName);
  292. m_backup.InsertAt(m_backup_index, fileName);
  293. m_backup_index ++;
  294. }
  295. void CHDrawDoc::ReStore(BOOL backward)
  296. {
  297. m_backup_index -= backward ? 1 : -1;
  298. //make m_backup[m_backup_index] the current document data
  299. OnOpenDocument(m_backup.GetAt(m_backup_index-1));
  300. }
  301. void CHDrawDoc::OnUpdateEditRedo(CCmdUI* pCmdUI)
  302. {
  303. // TODO: Add your command update UI handler code here
  304. pCmdUI->Enable(m_backup_index < m_backup.GetSize());
  305. }
  306. void CHDrawDoc::OnUpdateEditUndo(CCmdUI* pCmdUI)
  307. {
  308. // TODO: Add your command update UI handler code here
  309. pCmdUI->Enable(m_backup_index > 1);
  310. }
  311. void CHDrawDoc::OnEditRedo()
  312. {
  313. // TODO: Add your command handler code here
  314. ReStore(FALSE);
  315. UpdateAllViews(NULL);
  316. }
  317. void CHDrawDoc::OnEditUndo()
  318. {
  319. // TODO: Add your command handler code here
  320. ReStore(TRUE);
  321. UpdateAllViews(NULL);
  322. }
  323. void CHDrawDoc::DeleteContents()
  324. {
  325. // TODO: Add your specialized code here and/or call the base class
  326. //strokeList
  327. for(int i = 0; i < m_strokeList.GetSize(); i ++)
  328. delete m_strokeList.ElementAt(i);
  329. m_strokeList.RemoveAll();
  330. CDocument::DeleteContents();
  331. }
  332. void CHDrawDoc::OnSelect()
  333. {
  334. // TODO: Add your command handler code here
  335. m_picType = PIC_select;
  336. }
  337. void CHDrawDoc::OnUpdateSelect(CCmdUI* pCmdUI)
  338. {
  339. // TODO: Add your command update UI handler code here
  340. pCmdUI->SetCheck(PIC_select == m_picType);
  341. }