Windows NT 4.0 source code leak
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1488 lines
36 KiB

4 years ago
  1. // drawvw.cpp : implementation of the CDrawView class
  2. //
  3. // This is a part of the Microsoft Foundation Classes C++ library.
  4. // Copyright (C) 1992-1993 Microsoft Corporation
  5. // All rights reserved.
  6. //
  7. // This source code is only intended as a supplement to the
  8. // Microsoft Foundation Classes Reference and Microsoft
  9. // QuickHelp and/or WinHelp documentation provided with the library.
  10. // See these sources for detailed information regarding the
  11. // Microsoft Foundation Classes product.
  12. #include "stdafx.h"
  13. #include <afxpriv.h>
  14. #include <afx.h>
  15. #include "SepEdt.h"
  16. #include "drawdoc.h"
  17. #include "drawobj.h"
  18. #include "cntritem.h"
  19. #include "drawvw.h"
  20. #include "drawobj.h"
  21. #include "drawtool.h"
  22. #include "mainfrm.h"
  23. #include "textbox.h"
  24. #ifdef _DEBUG
  25. #undef THIS_FILE
  26. static char BASED_CODE THIS_FILE[] = __FILE__;
  27. #endif
  28. // private clipboard format (list of Draw objects)
  29. CLIPFORMAT CDrawView::m_cfDraw =
  30. (CLIPFORMAT)::RegisterClipboardFormat(_T("MFC Draw Sample"));
  31. BOOL CDrawView::m_IsRecording = FALSE;
  32. /////////////////////////////////////////////////////////////////////////////
  33. // CDrawView
  34. IMPLEMENT_DYNCREATE(CDrawView, CScrollView)
  35. BEGIN_MESSAGE_MAP(CDrawView, CScrollView)
  36. //{{AFX_MSG_MAP(CDrawView)
  37. ON_COMMAND(ID_OLE_INSERT_NEW, OnInsertObject)
  38. ON_COMMAND(ID_CANCEL_EDIT, OnCancelEdit)
  39. ON_WM_LBUTTONDOWN()
  40. ON_WM_LBUTTONUP()
  41. ON_WM_MOUSEMOVE()
  42. ON_WM_LBUTTONDBLCLK()
  43. ON_COMMAND(ID_DRAW_SELECT, OnDrawSelect)
  44. ON_COMMAND(ID_DRAW_ROUNDRECT, OnDrawRoundRect)
  45. ON_COMMAND(ID_DRAW_RECT, OnDrawRect)
  46. ON_COMMAND(ID_DRAW_LINE, OnDrawLine)
  47. ON_COMMAND(ID_DRAW_ELLIPSE, OnDrawEllipse)
  48. ON_UPDATE_COMMAND_UI(ID_DRAW_ELLIPSE, OnUpdateDrawEllipse)
  49. ON_UPDATE_COMMAND_UI(ID_DRAW_LINE, OnUpdateDrawLine)
  50. ON_UPDATE_COMMAND_UI(ID_DRAW_RECT, OnUpdateDrawRect)
  51. ON_UPDATE_COMMAND_UI(ID_DRAW_ROUNDRECT, OnUpdateDrawRoundRect)
  52. ON_UPDATE_COMMAND_UI(ID_DRAW_SELECT, OnUpdateDrawSelect)
  53. ON_UPDATE_COMMAND_UI(ID_OBJECT_MOVEBACK, OnUpdateSingleSelect)
  54. ON_COMMAND(ID_EDIT_SELECT_ALL, OnEditSelectAll)
  55. ON_COMMAND(ID_EDIT_CLEAR, OnEditClear)
  56. ON_UPDATE_COMMAND_UI(ID_EDIT_CLEAR, OnUpdateAnySelect)
  57. ON_COMMAND(ID_DRAW_POLYGON, OnDrawPolygon)
  58. ON_UPDATE_COMMAND_UI(ID_DRAW_POLYGON, OnUpdateDrawPolygon)
  59. ON_WM_SIZE()
  60. ON_COMMAND(ID_VIEW_GRID, OnViewGrid)
  61. ON_UPDATE_COMMAND_UI(ID_VIEW_GRID, OnUpdateViewGrid)
  62. ON_WM_ERASEBKGND()
  63. ON_COMMAND(ID_OBJECT_FILLCOLOR, OnObjectFillColor)
  64. ON_COMMAND(ID_OBJECT_LINECOLOR, OnObjectLineColor)
  65. ON_COMMAND(ID_OBJECT_MOVEBACK, OnObjectMoveBack)
  66. ON_COMMAND(ID_OBJECT_MOVEFORWARD, OnObjectMoveForward)
  67. ON_COMMAND(ID_OBJECT_MOVETOBACK, OnObjectMoveToBack)
  68. ON_COMMAND(ID_OBJECT_MOVETOFRONT, OnObjectMoveToFront)
  69. ON_COMMAND(ID_EDIT_COPY, OnEditCopy)
  70. ON_UPDATE_COMMAND_UI(ID_EDIT_COPY, OnUpdateEditCopy)
  71. ON_COMMAND(ID_EDIT_CUT, OnEditCut)
  72. ON_UPDATE_COMMAND_UI(ID_EDIT_CUT, OnUpdateEditCut)
  73. ON_COMMAND(ID_EDIT_PASTE, OnEditPaste)
  74. ON_UPDATE_COMMAND_UI(ID_EDIT_PASTE, OnUpdateEditPaste)
  75. ON_WM_SETFOCUS()
  76. ON_COMMAND(ID_VIEW_SHOWOBJECTS, OnViewShowObjects)
  77. ON_UPDATE_COMMAND_UI(ID_VIEW_SHOWOBJECTS, OnUpdateViewShowObjects)
  78. ON_COMMAND(ID_EDIT_PROPERTIES, OnEditProperties)
  79. ON_UPDATE_COMMAND_UI(ID_EDIT_PROPERTIES, OnUpdateEditProperties)
  80. ON_WM_DESTROY()
  81. ON_UPDATE_COMMAND_UI(ID_EDIT_SELECT_ALL, OnUpdateEditSelectAll)
  82. ON_COMMAND(ID_FILE_RECORD, OnFileRecord)
  83. ON_COMMAND(ID_DRAW_TEXT, OnDrawText)
  84. ON_UPDATE_COMMAND_UI(ID_DRAW_TEXT, OnUpdateDrawText)
  85. ON_COMMAND(ID_TEXT_FONT, OnTextFont)
  86. ON_UPDATE_COMMAND_UI(ID_TEXT_FONT, OnUpdateTextFont)
  87. ON_COMMAND(ID_TEXT_INS_JOBINFO, OnTextInsJobinfo)
  88. ON_UPDATE_COMMAND_UI(ID_TEXT_INS_JOBINFO, OnUpdateTextInsJobinfo)
  89. ON_WM_CTLCOLOR()
  90. ON_COMMAND(ID_TEXT_LEFT, OnTextLeft)
  91. ON_UPDATE_COMMAND_UI(ID_TEXT_LEFT, OnUpdateTextLeft)
  92. ON_UPDATE_COMMAND_UI(ID_OBJECT_MOVEFORWARD, OnUpdateSingleSelect)
  93. ON_UPDATE_COMMAND_UI(ID_OBJECT_MOVETOBACK, OnUpdateSingleSelect)
  94. ON_UPDATE_COMMAND_UI(ID_OBJECT_MOVETOFRONT, OnUpdateSingleSelect)
  95. ON_COMMAND(ID_TEXT_CENTER, OnTextCenter)
  96. ON_UPDATE_COMMAND_UI(ID_TEXT_CENTER, OnUpdateTextCenter)
  97. ON_COMMAND(ID_TEXT_RIGHT, OnTextRight)
  98. ON_UPDATE_COMMAND_UI(ID_TEXT_RIGHT, OnUpdateTextRight)
  99. //}}AFX_MSG_MAP
  100. // Standard printing commands
  101. ON_COMMAND(ID_FILE_PRINT, OnFilePrint)
  102. ON_COMMAND(ID_FILE_PRINT_PREVIEW, CScrollView::OnFilePrintPreview)
  103. END_MESSAGE_MAP()
  104. /////////////////////////////////////////////////////////////////////////////
  105. // CDrawView construction/destruction
  106. CDrawView::CDrawView()
  107. {
  108. m_bGrid = TRUE;
  109. m_gridColor = RGB(0, 0, 128);
  110. m_bActive = FALSE;
  111. m_pedit = NULL;
  112. int i=1;
  113. i=(int)(-3.86-i);
  114. }
  115. CDrawView::~CDrawView()
  116. {
  117. }
  118. BOOL CDrawView::PreCreateWindow(CREATESTRUCT& cs)
  119. {
  120. ASSERT(cs.style & WS_CHILD);
  121. if (cs.lpszClass == NULL)
  122. cs.lpszClass = AfxRegisterWndClass(CS_DBLCLKS);
  123. return TRUE;
  124. }
  125. void CDrawView::OnActivateView(BOOL bActivate, CView* pActiveView,
  126. CView* pDeactiveView)
  127. {
  128. CView::OnActivateView(bActivate, pActiveView, pDeactiveView);
  129. // invalidate selections when active status changes
  130. if (m_bActive != bActivate)
  131. {
  132. if (bActivate) // if becoming active update as if active
  133. m_bActive = bActivate;
  134. if (!m_selection.IsEmpty())
  135. OnUpdate(NULL, HINT_UPDATE_SELECTION, NULL);
  136. m_bActive = bActivate;
  137. }
  138. }
  139. /////////////////////////////////////////////////////////////////////////////
  140. // CDrawView drawing
  141. void CDrawView::InvalObj(CDrawObj* pObj)
  142. {
  143. CRect rect = pObj->m_position;
  144. DocToClient(rect);
  145. if (m_bActive && IsSelected(pObj))
  146. {
  147. rect.left -= 4;
  148. rect.top -= 5;
  149. rect.right += 5;
  150. rect.bottom += 4;
  151. }
  152. rect.InflateRect(1, 1); // handles CDrawOleObj objects
  153. InvalidateRect(rect, FALSE);
  154. }
  155. void CDrawView::OnUpdate(CView* , LPARAM lHint, CObject* pHint)
  156. {
  157. switch (lHint)
  158. {
  159. case HINT_UPDATE_WINDOW: // redraw entire window
  160. Invalidate(FALSE);
  161. break;
  162. case HINT_UPDATE_DRAWOBJ: // a single object has changed
  163. InvalObj((CDrawObj*)pHint);
  164. break;
  165. case HINT_UPDATE_SELECTION: // an entire selection has changed
  166. {
  167. CDrawObjList* pList = pHint != NULL ?
  168. (CDrawObjList*)pHint : &m_selection;
  169. POSITION pos = pList->GetHeadPosition();
  170. while (pos != NULL)
  171. InvalObj(pList->GetNext(pos));
  172. }
  173. break;
  174. case HINT_DELETE_SELECTION: // an entire selection has been removed
  175. if (pHint != &m_selection)
  176. {
  177. CDrawObjList* pList = (CDrawObjList*)pHint;
  178. POSITION pos = pList->GetHeadPosition();
  179. while (pos != NULL)
  180. {
  181. CDrawObj* pObj = pList->GetNext(pos);
  182. InvalObj(pObj);
  183. Remove(pObj); // remove it from this view's selection
  184. }
  185. }
  186. break;
  187. case HINT_UPDATE_OLE_ITEMS:
  188. {
  189. CDrawDoc* pDoc = GetDocument();
  190. POSITION pos = pDoc->GetObjects()->GetHeadPosition();
  191. while (pos != NULL)
  192. {
  193. CDrawObj* pObj = pDoc->GetObjects()->GetNext(pos);
  194. if (pObj->IsKindOf(RUNTIME_CLASS(CDrawOleObj)))
  195. InvalObj(pObj);
  196. }
  197. }
  198. break;
  199. default:
  200. ASSERT(FALSE);
  201. break;
  202. }
  203. }
  204. void CDrawView::OnPrepareDC(CDC* pDC, CPrintInfo* pInfo)
  205. {
  206. CScrollView::OnPrepareDC(pDC, pInfo);
  207. // mapping mode is MM_ANISOTROPIC
  208. // these extents setup a mode similar to MM_LOENGLISH
  209. // MM_LOENGLISH is in .01 physical inches
  210. // these extents provide .01 logical inches
  211. pDC->SetMapMode(MM_ANISOTROPIC);
  212. pDC->SetViewportExt(pDC->GetDeviceCaps(LOGPIXELSX),
  213. pDC->GetDeviceCaps(LOGPIXELSY));
  214. pDC->SetWindowExt(100, -100); //====
  215. // for recording emf, always set viewport org to (0,0) first,
  216. // disable window-scroll in recording, just as in printing
  217. if (CDrawView::m_IsRecording)
  218. pDC->SetViewportOrg(0,0);
  219. // set the origin of the coordinate system to the center of the page
  220. CPoint ptOrg;
  221. ptOrg.x = GetDocument()->GetSize().cx / 2;
  222. ptOrg.y = GetDocument()->GetSize().cy / 2;
  223. // ptOrg is in logical coordinates
  224. pDC->SetWindowOrg(-ptOrg.x,ptOrg.y);
  225. }
  226. BOOL CDrawView::OnScrollBy(CSize sizeScroll, BOOL bDoScroll)
  227. {
  228. // do the scroll
  229. if (!CScrollView::OnScrollBy(sizeScroll, bDoScroll))
  230. return FALSE;
  231. // update the position of any in-place active item
  232. if (bDoScroll)
  233. {
  234. UpdateActiveItem();
  235. UpdateWindow();
  236. }
  237. return TRUE;
  238. }
  239. void CDrawView::OnDraw(CDC* pDC)
  240. {
  241. CDrawDoc* pDoc = GetDocument();
  242. ASSERT_VALID(pDoc);
  243. CDC dc;
  244. CDC* pDrawDC = pDC;
  245. CBitmap bitmap;
  246. CBitmap* pOldBitmap;
  247. // only paint the rect that needs repainting
  248. CRect client;
  249. pDC->GetClipBox(client);
  250. client.NormalizeRect();
  251. client.InflateRect(2,2); // in order to clear completely
  252. CRect rect = client;
  253. DocToClient(rect);
  254. rect.NormalizeRect();
  255. if (!pDC->IsPrinting() && !CDrawView::m_IsRecording)
  256. {
  257. // draw to offscreen bitmap for fast looking repaints
  258. if (dc.CreateCompatibleDC(pDC))
  259. {
  260. if (bitmap.CreateCompatibleBitmap(pDC, rect.Width(), rect.Height()))
  261. {
  262. OnPrepareDC(&dc, NULL);
  263. pDrawDC = &dc;
  264. // offset origin more because bitmap is just piece of the whole drawing
  265. dc.OffsetViewportOrg(-rect.left, -rect.top);
  266. pOldBitmap = dc.SelectObject(&bitmap);
  267. dc.SetBrushOrg(rect.left % 8, rect.top % 8);
  268. // might as well clip to the same rectangle
  269. dc.IntersectClipRect(client);
  270. }
  271. }
  272. }
  273. // paint background
  274. CBrush brush;
  275. if (!brush.CreateSolidBrush(pDoc->GetPaperColor()))
  276. return;
  277. brush.UnrealizeObject();
  278. pDrawDC->FillRect(client, &brush);
  279. if (!pDC->IsPrinting() && !CDrawView::m_IsRecording && m_bGrid)
  280. DrawGrid(pDrawDC);
  281. pDoc->Draw(pDrawDC, this);
  282. if (pDrawDC != pDC)
  283. {
  284. pDC->SetViewportOrg(0, 0);
  285. pDC->SetWindowOrg(0,0);
  286. pDC->SetMapMode(MM_TEXT);
  287. dc.SetViewportOrg(0, 0);
  288. dc.SetWindowOrg(0,0);
  289. dc.SetMapMode(MM_TEXT);
  290. pDC->BitBlt(rect.left, rect.top, rect.Width(), rect.Height(),
  291. &dc, 0, 0, SRCCOPY);
  292. dc.SelectObject(pOldBitmap);
  293. }
  294. }
  295. void CDrawView::Remove(CDrawObj* pObj)
  296. {
  297. POSITION pos = m_selection.Find(pObj);
  298. if (pos != NULL)
  299. m_selection.RemoveAt(pos);
  300. }
  301. void CDrawView::PasteNative(COleDataObject& dataObject)
  302. {
  303. // get file refering to clipboard data
  304. CFile* pFile = dataObject.GetFileData(m_cfDraw);
  305. if (pFile == NULL)
  306. return;
  307. // connect the file to the archive
  308. CArchive ar(pFile, CArchive::load);
  309. TRY
  310. {
  311. ar.m_pDocument = GetDocument(); // set back-pointer in archive
  312. // read the selection
  313. m_selection.Serialize(ar);
  314. }
  315. CATCH_ALL(e)
  316. {
  317. ar.Close();
  318. delete pFile;
  319. THROW_LAST();
  320. }
  321. END_CATCH_ALL
  322. ar.Close();
  323. delete pFile;
  324. }
  325. void CDrawView::PasteEmbedded(COleDataObject& dataObject)
  326. {
  327. BeginWaitCursor();
  328. // paste embedded
  329. CDrawOleObj* pObj = new CDrawOleObj(GetInitialPosition());
  330. ASSERT_VALID(pObj);
  331. CDrawItem* pItem = new CDrawItem(GetDocument(), pObj);
  332. ASSERT_VALID(pItem);
  333. pObj->m_pClientItem = pItem;
  334. TRY
  335. {
  336. if (!pItem->CreateFromData(&dataObject) &&
  337. !pItem->CreateStaticFromData(&dataObject))
  338. {
  339. AfxThrowMemoryException(); // any exception will do
  340. }
  341. // add the object to the document
  342. GetDocument()->Add(pObj);
  343. m_selection.AddTail(pObj);
  344. // try to get initial presentation data
  345. pItem->UpdateLink();
  346. pItem->UpdateExtent();
  347. }
  348. CATCH_ALL(e)
  349. {
  350. // clean up item
  351. pItem->Delete();
  352. pObj->m_pClientItem = NULL;
  353. GetDocument()->Remove(pObj);
  354. pObj->Remove();
  355. AfxMessageBox(IDP_FAILED_TO_CREATE);
  356. }
  357. END_CATCH_ALL
  358. EndWaitCursor();
  359. }
  360. void CDrawView::DrawGrid(CDC* pDC)
  361. {
  362. CDrawDoc* pDoc = GetDocument();
  363. COLORREF oldBkColor = pDC->SetBkColor(pDoc->GetPaperColor());
  364. CRect rect;
  365. rect.left = -pDoc->GetSize().cx / 2;
  366. rect.top = -pDoc->GetSize().cy / 2;
  367. rect.right = rect.left + pDoc->GetSize().cx;
  368. rect.bottom = rect.top + pDoc->GetSize().cy;
  369. // Center lines
  370. CPen penDash;
  371. penDash.CreatePen(PS_DASH, 1, m_gridColor);
  372. CPen* pOldPen = pDC->SelectObject(&penDash);
  373. pDC->MoveTo(0, rect.top);
  374. pDC->LineTo(0, rect.bottom);
  375. pDC->MoveTo(rect.left, 0);
  376. pDC->LineTo(rect.right, 0);
  377. // Major unit lines
  378. CPen penDot;
  379. penDot.CreatePen(PS_DOT, 1, m_gridColor);
  380. pDC->SelectObject(&penDot);
  381. for (int x = rect.left / 100 * 100; x < rect.right; x += 100) //====
  382. {
  383. if (x != 0)
  384. {
  385. pDC->MoveTo(x, rect.top);
  386. pDC->LineTo(x, rect.bottom);
  387. }
  388. }
  389. for (int y = rect.top / 100 * 100; y < rect.bottom; y += 100) //====
  390. {
  391. if (y != 0)
  392. {
  393. pDC->MoveTo(rect.left, y);
  394. pDC->LineTo(rect.right, y);
  395. }
  396. }
  397. // Outlines
  398. CPen penSolid;
  399. penSolid.CreatePen(PS_SOLID, 1, m_gridColor);
  400. pDC->SelectObject(&penSolid);
  401. pDC->MoveTo(rect.left, rect.top);
  402. pDC->LineTo(rect.right, rect.top);
  403. pDC->LineTo(rect.right, rect.bottom);
  404. pDC->LineTo(rect.left, rect.bottom);
  405. pDC->LineTo(rect.left, rect.top);
  406. pDC->SelectObject(pOldPen);
  407. pDC->SetBkColor(oldBkColor);
  408. }
  409. void CDrawView::OnInitialUpdate()
  410. {
  411. CloseEdit(); // close edit if any
  412. CSize size = GetDocument()->GetSize();
  413. CClientDC dc(NULL);
  414. size.cx = MulDiv(size.cx, dc.GetDeviceCaps(LOGPIXELSX), 100); //====
  415. size.cy = MulDiv(size.cy, dc.GetDeviceCaps(LOGPIXELSY), 100); //====
  416. SetScrollSizes(MM_TEXT, size);
  417. CScrollView::OnInitialUpdate(); // to do update
  418. }
  419. void CDrawView::SetPageSize(CSize size)
  420. {
  421. CClientDC dc(NULL);
  422. size.cx = MulDiv(size.cx, dc.GetDeviceCaps(LOGPIXELSX), 100); //====
  423. size.cy = MulDiv(size.cy, dc.GetDeviceCaps(LOGPIXELSY), 100); //====
  424. SetScrollSizes(MM_TEXT, size);
  425. GetDocument()->UpdateAllViews(NULL, HINT_UPDATE_WINDOW, NULL);
  426. }
  427. /////////////////////////////////////////////////////////////////////////////
  428. // CDrawView printing
  429. BOOL CDrawView::OnPreparePrinting(CPrintInfo* pInfo)
  430. {
  431. CloseEdit();
  432. // default preparation
  433. return DoPreparePrinting(pInfo);
  434. }
  435. void CDrawView::OnBeginPrinting(CDC* pDC, CPrintInfo* pInfo)
  436. {
  437. CScrollView::OnBeginPrinting(pDC,pInfo);
  438. // check page size -- user could have gone into print setup
  439. // from print dialog and changed paper or orientation
  440. GetDocument()->ComputePageSize();
  441. }
  442. void CDrawView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
  443. {
  444. // TODO: add cleanup after printing
  445. }
  446. /////////////////////////////////////////////////////////////////////////////
  447. // OLE Client support and commands
  448. BOOL CDrawView::IsSelected(const CObject* pDocItem) const
  449. {
  450. CDrawObj* pDrawObj = (CDrawObj*)pDocItem;
  451. if (pDocItem->IsKindOf(RUNTIME_CLASS(CDrawItem)))
  452. pDrawObj = ((CDrawItem*)pDocItem)->m_pDrawObj;
  453. return m_selection.Find(pDrawObj) != NULL;
  454. }
  455. void CDrawView::OnInsertObject()
  456. {
  457. // Invoke the standard Insert Object dialog box to obtain information
  458. // for new CDrawItem object.
  459. COleInsertDialog dlg;
  460. if (dlg.DoModal() != IDOK)
  461. return;
  462. BeginWaitCursor();
  463. // First create the C++ object
  464. CDrawOleObj* pObj = new CDrawOleObj(GetInitialPosition());
  465. ASSERT_VALID(pObj);
  466. CDrawItem* pItem = new CDrawItem(GetDocument(), pObj);
  467. ASSERT_VALID(pItem);
  468. pObj->m_pClientItem = pItem;
  469. // Now create the OLE object/item
  470. TRY
  471. {
  472. if (!dlg.CreateItem(pObj->m_pClientItem))
  473. AfxThrowMemoryException();
  474. // add the object to the document
  475. GetDocument()->Add(pObj);
  476. // try to get initial presentation data
  477. pItem->UpdateLink();
  478. pItem->UpdateExtent();
  479. // if insert new object -- initially show the object
  480. if (dlg.GetSelectionType() == COleInsertDialog::createNewItem)
  481. pItem->DoVerb(OLEIVERB_SHOW, this);
  482. }
  483. CATCH_ALL(e)
  484. {
  485. // clean up item
  486. pItem->Delete();
  487. pObj->m_pClientItem = NULL;
  488. GetDocument()->Remove(pObj);
  489. pObj->Remove();
  490. AfxMessageBox(IDP_FAILED_TO_CREATE);
  491. }
  492. END_CATCH_ALL
  493. EndWaitCursor();
  494. }
  495. // The following command handler provides the standard keyboard
  496. // user interface to cancel an in-place editing session.
  497. void CDrawView::OnCancelEdit()
  498. {
  499. // deactivate any in-place active item on this view!
  500. COleClientItem* pActiveItem = GetDocument()->GetInPlaceActiveItem(this);
  501. if (pActiveItem != NULL)
  502. {
  503. // if we found one, deactivate it
  504. pActiveItem->Close();
  505. }
  506. ASSERT(GetDocument()->GetInPlaceActiveItem(this) == NULL);
  507. // escape also brings us back into select mode
  508. ReleaseCapture();
  509. CDrawTool* pTool = CDrawTool::FindTool(CDrawTool::c_drawShape);
  510. if (pTool != NULL)
  511. pTool->OnCancel();
  512. CDrawTool::c_drawShape = selection;
  513. }
  514. void CDrawView::OnSetFocus(CWnd* pOldWnd)
  515. {
  516. COleClientItem* pActiveItem = GetDocument()->GetInPlaceActiveItem(this);
  517. if (pActiveItem != NULL &&
  518. pActiveItem->GetItemState() == COleClientItem::activeUIState)
  519. {
  520. // need to set focus to this item if it is in the same view
  521. CWnd* pWnd = pActiveItem->GetInPlaceWindow();
  522. if (pWnd != NULL)
  523. {
  524. pWnd->SetFocus();
  525. return;
  526. }
  527. }
  528. if (m_pedit!=NULL) m_pedit->SetFocus(); // set focus if in editing text
  529. CScrollView::OnSetFocus(pOldWnd);
  530. }
  531. CRect CDrawView::GetInitialPosition()
  532. {
  533. CRect rect(10, 10, 10, 10);
  534. ClientToDoc(rect);
  535. return rect;
  536. }
  537. void CDrawView::ClientToDoc(CPoint& point)
  538. {
  539. CClientDC dc(this);
  540. OnPrepareDC(&dc, NULL);
  541. dc.DPtoLP(&point);
  542. }
  543. void CDrawView::ClientToDoc(CRect& rect)
  544. {
  545. CClientDC dc(this);
  546. OnPrepareDC(&dc, NULL);
  547. dc.DPtoLP(rect);
  548. ASSERT(rect.left <= rect.right);
  549. ASSERT(rect.bottom <= rect.top);
  550. }
  551. void CDrawView::DocToClient(CPoint& point)
  552. {
  553. CClientDC dc(this);
  554. OnPrepareDC(&dc, NULL);
  555. dc.LPtoDP(&point);
  556. }
  557. void CDrawView::DocToClient(CRect& rect)
  558. {
  559. CClientDC dc(this);
  560. OnPrepareDC(&dc, NULL);
  561. dc.LPtoDP(rect);
  562. rect.NormalizeRect();
  563. }
  564. void CDrawView::Select(CDrawObj* pObj, BOOL bAdd)
  565. {
  566. if (!bAdd)
  567. {
  568. OnUpdate(NULL, HINT_UPDATE_SELECTION, NULL);
  569. m_selection.RemoveAll(); // remove all selections
  570. }
  571. if (pObj == NULL || IsSelected(pObj))
  572. return;
  573. m_selection.AddTail(pObj);
  574. InvalObj(pObj);
  575. }
  576. // rect is in device coordinates
  577. void CDrawView::SelectWithinRect(CRect rect, BOOL bAdd)
  578. {
  579. if (!bAdd)
  580. Select(NULL);
  581. ClientToDoc(rect);
  582. CDrawObjList* pObList = GetDocument()->GetObjects();
  583. POSITION posObj = pObList->GetHeadPosition();
  584. while (posObj != NULL)
  585. {
  586. CDrawObj* pObj = pObList->GetNext(posObj);
  587. if (pObj->Intersects(rect))
  588. Select(pObj, TRUE);
  589. }
  590. }
  591. void CDrawView::Deselect(CDrawObj* pObj)
  592. {
  593. POSITION pos = m_selection.Find(pObj);
  594. if (pos != NULL)
  595. {
  596. InvalObj(pObj);
  597. m_selection.RemoveAt(pos);
  598. }
  599. }
  600. void CDrawView::CloneSelection()
  601. {
  602. POSITION pos = m_selection.GetHeadPosition();
  603. while (pos != NULL)
  604. {
  605. CDrawObj* pObj = m_selection.GetNext(pos);
  606. pObj->Clone(pObj->m_pDocument);
  607. // copies object and adds it to the document
  608. }
  609. }
  610. void CDrawView::UpdateActiveItem()
  611. {
  612. COleClientItem* pActiveItem = GetDocument()->GetInPlaceActiveItem(this);
  613. if (pActiveItem != NULL &&
  614. pActiveItem->GetItemState() == COleClientItem::activeUIState)
  615. {
  616. // this will update the item rectangles by calling
  617. // OnGetPosRect & OnGetClipRect.
  618. pActiveItem->SetItemRects();
  619. }
  620. }
  621. /////////////////////////////////////////////////////////////////////////////
  622. // CDrawView message handlers
  623. void CDrawView::OnLButtonDown(UINT nFlags, CPoint point)
  624. {
  625. if (!m_bActive)
  626. return;
  627. CDrawTool* pTool = CDrawTool::FindTool(CDrawTool::c_drawShape);
  628. if (pTool != NULL)
  629. pTool->OnLButtonDown(this, nFlags, point);
  630. }
  631. void CDrawView::OnLButtonUp(UINT nFlags, CPoint point)
  632. {
  633. if (!m_bActive)
  634. return;
  635. CDrawTool* pTool = CDrawTool::FindTool(CDrawTool::c_drawShape);
  636. if (pTool != NULL)
  637. pTool->OnLButtonUp(this, nFlags, point);
  638. }
  639. void CDrawView::OnMouseMove(UINT nFlags, CPoint point)
  640. {
  641. if (!m_bActive)
  642. return;
  643. CDrawTool* pTool = CDrawTool::FindTool(CDrawTool::c_drawShape);
  644. if (pTool != NULL)
  645. pTool->OnMouseMove(this, nFlags, point);
  646. }
  647. void CDrawView::OnLButtonDblClk(UINT nFlags, CPoint point)
  648. {
  649. if (!m_bActive)
  650. return;
  651. CDrawTool* pTool = CDrawTool::FindTool(CDrawTool::c_drawShape);
  652. if (pTool != NULL)
  653. pTool->OnLButtonDblClk(this, nFlags, point);
  654. }
  655. void CDrawView::OnDestroy()
  656. {
  657. CScrollView::OnDestroy();
  658. // deactivate the inplace active item on this view
  659. COleClientItem* pActiveItem = GetDocument()->GetInPlaceActiveItem(this);
  660. if (pActiveItem != NULL && pActiveItem->GetActiveView() == this)
  661. {
  662. pActiveItem->Deactivate();
  663. ASSERT(GetDocument()->GetInPlaceActiveItem(this) == NULL);
  664. }
  665. }
  666. void CDrawView::OnDrawSelect()
  667. {
  668. CDrawTool::c_drawShape = selection;
  669. }
  670. void CDrawView::OnDrawRoundRect()
  671. {
  672. CDrawTool::c_drawShape = roundRect;
  673. }
  674. void CDrawView::OnDrawRect()
  675. {
  676. CDrawTool::c_drawShape = rect;
  677. }
  678. void CDrawView::OnDrawLine()
  679. {
  680. CDrawTool::c_drawShape = line;
  681. }
  682. void CDrawView::OnDrawEllipse()
  683. {
  684. CDrawTool::c_drawShape = ellipse;
  685. }
  686. void CDrawView::OnDrawPolygon()
  687. {
  688. CDrawTool::c_drawShape = poly;
  689. }
  690. void CDrawView::OnUpdateDrawEllipse(CCmdUI* pCmdUI)
  691. {
  692. pCmdUI->SetRadio(CDrawTool::c_drawShape == ellipse);
  693. }
  694. void CDrawView::OnUpdateDrawLine(CCmdUI* pCmdUI)
  695. {
  696. pCmdUI->SetRadio(CDrawTool::c_drawShape == line);
  697. }
  698. void CDrawView::OnUpdateDrawRect(CCmdUI* pCmdUI)
  699. {
  700. pCmdUI->SetRadio(CDrawTool::c_drawShape == rect);
  701. }
  702. void CDrawView::OnUpdateDrawRoundRect(CCmdUI* pCmdUI)
  703. {
  704. pCmdUI->SetRadio(CDrawTool::c_drawShape == roundRect);
  705. }
  706. void CDrawView::OnUpdateDrawSelect(CCmdUI* pCmdUI)
  707. {
  708. pCmdUI->SetRadio(CDrawTool::c_drawShape == selection);
  709. }
  710. void CDrawView::OnUpdateSingleSelect(CCmdUI* pCmdUI)
  711. {
  712. pCmdUI->Enable(m_selection.GetCount() == 1);
  713. }
  714. void CDrawView::OnEditSelectAll()
  715. {
  716. CDrawObjList* pObList = GetDocument()->GetObjects();
  717. POSITION pos = pObList->GetHeadPosition();
  718. while (pos != NULL)
  719. Select(pObList->GetNext(pos), TRUE);
  720. }
  721. void CDrawView::OnUpdateEditSelectAll(CCmdUI* pCmdUI)
  722. {
  723. pCmdUI->Enable(GetDocument()->GetObjects()->GetCount() != 0);
  724. }
  725. void CDrawView::OnEditClear()
  726. {
  727. CloseEdit(); // close edit if necessary
  728. // update all the views before the selection goes away
  729. GetDocument()->UpdateAllViews(NULL, HINT_DELETE_SELECTION, &m_selection);
  730. OnUpdate(NULL, HINT_UPDATE_SELECTION, NULL);
  731. // now remove the selection from the document
  732. POSITION pos = m_selection.GetHeadPosition();
  733. while (pos != NULL)
  734. {
  735. CDrawObj* pObj = m_selection.GetNext(pos);
  736. GetDocument()->Remove(pObj);
  737. pObj->Remove();
  738. }
  739. m_selection.RemoveAll();
  740. }
  741. void CDrawView::OnUpdateAnySelect(CCmdUI* pCmdUI)
  742. {
  743. pCmdUI->Enable(!m_selection.IsEmpty());
  744. }
  745. void CDrawView::OnUpdateDrawPolygon(CCmdUI* pCmdUI)
  746. {
  747. pCmdUI->SetRadio(CDrawTool::c_drawShape == poly);
  748. }
  749. void CDrawView::OnSize(UINT nType, int cx, int cy)
  750. {
  751. CScrollView::OnSize(nType, cx, cy);
  752. UpdateActiveItem();
  753. }
  754. void CDrawView::OnViewGrid()
  755. {
  756. m_bGrid = !m_bGrid;
  757. Invalidate(FALSE);
  758. }
  759. void CDrawView::OnUpdateViewGrid(CCmdUI* pCmdUI)
  760. {
  761. pCmdUI->SetCheck(m_bGrid);
  762. }
  763. BOOL CDrawView::OnEraseBkgnd(CDC*)
  764. {
  765. return TRUE;
  766. }
  767. void CDrawView::OnObjectFillColor()
  768. {
  769. CColorDialog dlg;
  770. if (dlg.DoModal() != IDOK)
  771. return;
  772. COLORREF color = dlg.GetColor();
  773. POSITION pos = m_selection.GetHeadPosition();
  774. while (pos != NULL)
  775. {
  776. CDrawObj* pObj = m_selection.GetNext(pos);
  777. pObj->SetFillColor(color);
  778. }
  779. }
  780. void CDrawView::OnObjectLineColor()
  781. {
  782. CColorDialog dlg;
  783. if (dlg.DoModal() != IDOK)
  784. return;
  785. COLORREF color = dlg.GetColor();
  786. POSITION pos = m_selection.GetHeadPosition();
  787. while (pos != NULL)
  788. {
  789. CDrawObj* pObj = m_selection.GetNext(pos);
  790. pObj->SetLineColor(color);
  791. }
  792. }
  793. void CDrawView::OnObjectMoveBack()
  794. {
  795. CDrawDoc* pDoc = GetDocument();
  796. CDrawObj* pObj = m_selection.GetHead();
  797. CDrawObjList* pObjects = pDoc->GetObjects();
  798. POSITION pos = pObjects->Find(pObj);
  799. ASSERT(pos != NULL);
  800. if (pos != pObjects->GetHeadPosition())
  801. {
  802. POSITION posPrev = pos;
  803. pObjects->GetPrev(posPrev);
  804. pObjects->RemoveAt(pos);
  805. pObjects->InsertBefore(posPrev, pObj);
  806. InvalObj(pObj);
  807. }
  808. }
  809. void CDrawView::OnObjectMoveForward()
  810. {
  811. CDrawDoc* pDoc = GetDocument();
  812. CDrawObj* pObj = m_selection.GetHead();
  813. CDrawObjList* pObjects = pDoc->GetObjects();
  814. POSITION pos = pObjects->Find(pObj);
  815. ASSERT(pos != NULL);
  816. if (pos != pObjects->GetTailPosition())
  817. {
  818. POSITION posNext = pos;
  819. pObjects->GetNext(posNext);
  820. pObjects->RemoveAt(pos);
  821. pObjects->InsertAfter(posNext, pObj);
  822. InvalObj(pObj);
  823. }
  824. }
  825. void CDrawView::OnObjectMoveToBack()
  826. {
  827. CDrawDoc* pDoc = GetDocument();
  828. CDrawObj* pObj = m_selection.GetHead();
  829. CDrawObjList* pObjects = pDoc->GetObjects();
  830. POSITION pos = pObjects->Find(pObj);
  831. ASSERT(pos != NULL);
  832. pObjects->RemoveAt(pos);
  833. pObjects->AddHead(pObj);
  834. InvalObj(pObj);
  835. }
  836. void CDrawView::OnObjectMoveToFront()
  837. {
  838. CDrawDoc* pDoc = GetDocument();
  839. CDrawObj* pObj = m_selection.GetHead();
  840. CDrawObjList* pObjects = pDoc->GetObjects();
  841. POSITION pos = pObjects->Find(pObj);
  842. ASSERT(pos != NULL);
  843. pObjects->RemoveAt(pos);
  844. pObjects->AddTail(pObj);
  845. InvalObj(pObj);
  846. }
  847. void CDrawView::OnEditCopy()
  848. {
  849. CloseEdit(); // close edit whenever necessary
  850. ASSERT_VALID(this);
  851. ASSERT(m_cfDraw != NULL);
  852. // Create a shared file and associate a CArchive with it
  853. CSharedFile file;
  854. CArchive ar(&file, CArchive::store);
  855. // Serialize selected objects to the archive
  856. m_selection.Serialize(ar);
  857. ar.Close();
  858. COleDataSource* pDataSource = NULL;
  859. TRY
  860. {
  861. pDataSource = new COleDataSource;
  862. // put on local format instead of or in addation to
  863. pDataSource->CacheGlobalData(m_cfDraw, file.Detach());
  864. // if only one item and it is a COleClientItem then also
  865. // paste in that format
  866. CDrawObj* pDrawObj = m_selection.GetHead();
  867. if (m_selection.GetCount() == 1 &&
  868. pDrawObj->IsKindOf(RUNTIME_CLASS(CDrawOleObj)))
  869. {
  870. CDrawOleObj* pDrawOle = (CDrawOleObj*)pDrawObj;
  871. pDrawOle->m_pClientItem->GetClipboardData(pDataSource, FALSE);
  872. }
  873. pDataSource->SetClipboard();
  874. }
  875. CATCH_ALL(e)
  876. {
  877. delete pDataSource;
  878. THROW_LAST();
  879. }
  880. END_CATCH_ALL
  881. }
  882. void CDrawView::OnUpdateEditCopy(CCmdUI* pCmdUI)
  883. {
  884. pCmdUI->Enable(!m_selection.IsEmpty());
  885. }
  886. void CDrawView::OnEditCut()
  887. {
  888. OnEditCopy();
  889. OnEditClear();
  890. }
  891. void CDrawView::OnUpdateEditCut(CCmdUI* pCmdUI)
  892. {
  893. pCmdUI->Enable(!m_selection.IsEmpty());
  894. }
  895. void CDrawView::OnEditPaste()
  896. {
  897. COleDataObject dataObject;
  898. dataObject.AttachClipboard();
  899. CloseEdit(); // close edit
  900. // invalidate current selection since it will be deselected
  901. OnUpdate(NULL, HINT_UPDATE_SELECTION, NULL);
  902. m_selection.RemoveAll();
  903. if (dataObject.IsDataAvailable(m_cfDraw))
  904. {
  905. PasteNative(dataObject);
  906. // now add all items in m_selection to document
  907. POSITION pos = m_selection.GetHeadPosition();
  908. while (pos != NULL)
  909. GetDocument()->Add(m_selection.GetNext(pos));
  910. }
  911. else
  912. PasteEmbedded(dataObject);
  913. GetDocument()->SetModifiedFlag();
  914. // invalidate new pasted stuff
  915. GetDocument()->UpdateAllViews(NULL, HINT_UPDATE_SELECTION, &m_selection);
  916. }
  917. void CDrawView::OnUpdateEditPaste(CCmdUI* pCmdUI)
  918. {
  919. // determine if private or standard OLE formats are on the clipboard
  920. COleDataObject dataObject;
  921. BOOL bEnable = dataObject.AttachClipboard() &&
  922. (dataObject.IsDataAvailable(m_cfDraw) ||
  923. COleClientItem::CanCreateFromData(&dataObject));
  924. // enable command based on availability
  925. pCmdUI->Enable(bEnable);
  926. }
  927. void CDrawView::OnFilePrint()
  928. {
  929. CScrollView::OnFilePrint();
  930. GetDocument()->ComputePageSize();
  931. }
  932. void CDrawView::OnViewShowObjects()
  933. {
  934. CDrawOleObj::c_bShowItems = !CDrawOleObj::c_bShowItems;
  935. GetDocument()->UpdateAllViews(NULL, HINT_UPDATE_WINDOW, NULL); // n/show text border
  936. // GetDocument()->UpdateAllViews(NULL, HINT_UPDATE_OLE_ITEMS, NULL);
  937. }
  938. void CDrawView::OnUpdateViewShowObjects(CCmdUI* pCmdUI)
  939. {
  940. pCmdUI->SetCheck(CDrawOleObj::c_bShowItems);
  941. }
  942. void CDrawView::OnEditProperties()
  943. {
  944. CloseEdit(); // close edit
  945. if (m_selection.GetCount() == 1 && CDrawTool::c_drawShape == selection)
  946. {
  947. CDrawTool* pTool = CDrawTool::FindTool(CDrawTool::c_drawShape);
  948. ASSERT(pTool != NULL);
  949. pTool->OnLButtonDblClk(this, 0, CPoint(0, 0));
  950. }
  951. }
  952. void CDrawView::OnUpdateEditProperties(CCmdUI* pCmdUI)
  953. {
  954. pCmdUI->Enable(m_selection.GetCount() == 1 &&
  955. CDrawTool::c_drawShape == selection);
  956. }
  957. /////////////////////////////////////////////////////////////////////////////
  958. // CDrawView diagnostics
  959. #ifdef _DEBUG
  960. void CDrawView::AssertValid() const
  961. {
  962. CScrollView::AssertValid();
  963. }
  964. void CDrawView::Dump(CDumpContext& dc) const
  965. {
  966. CScrollView::Dump(dc);
  967. }
  968. #endif //_DEBUG
  969. /////////////////////////////////////////////////////////////////////////////
  970. void CDrawView::OnFileRecord()
  971. {
  972. CDC dc;
  973. HDC hPrt;
  974. hPrt = ((CDrawApp*)AfxGetApp()) -> GetDefaultPrinterIC() ;
  975. if (!hPrt) return;
  976. RECT rect={0,0,GetDeviceCaps(hPrt,HORZSIZE)*100,GetDeviceCaps(hPrt,VERTSIZE)*100};
  977. if (!(dc.m_hDC=CreateEnhMetaFile(hPrt,"c:\\users\\default\\sp.emf",&rect,NULL)))
  978. MessageBox("error creating emf");
  979. dc.SetAttribDC(dc.m_hDC);
  980. /* CDrawObjList* m_objects=pDoc->GetObjects();
  981. POSITION pos = m_objects->GetHeadPosition();
  982. CDrawText* pObj = (CDrawText*)(m_objects->GetNext(pos));
  983. SetMapMode(dc.m_hDC,MM_ANISOTROPIC);
  984. SetViewportExtEx(dc.m_hDC,96, 96,NULL);
  985. SetWindowExtEx(dc.m_hDC,1000, -1000,NULL);
  986. SetViewportOrgEx(dc.m_hDC,0, 0,NULL);
  987. SetWindowOrgEx(dc.m_hDC,0, 0,NULL);
  988. LOGFONT lf=*(pObj->GetLogFontCopy());
  989. memset(&lf,0,sizeof(LOGFONT));
  990. lf.lfHeight = 250;
  991. lf.lfWidth=0;
  992. lf.lfWeight=700;
  993. strcpy(lf.lfFaceName,"MS Sans Serif");
  994. HFONT hfont=CreateFontIndirect(&lf);
  995. */
  996. // RECT r2={1500,0,2500,2000};
  997. // RECT r2={1500,3000,2500,4000};
  998. // RECT r2={1500,-1000,2500,-2000};
  999. // SelectObject(dc.m_hDC,hfont/*pObj->m_pfont->m_hObject*/);
  1000. // DrawText(dc.m_hDC,"Printed"/*pObj->m_text*/,-1,&r2/*&(pObj->m_position)*/,0);
  1001. CDrawView::m_IsRecording=TRUE;
  1002. OnPrepareDC(&dc,NULL);
  1003. CDrawDoc* pDoc = GetDocument();
  1004. pDoc->Draw(&dc,this);
  1005. CDrawView::m_IsRecording=FALSE;
  1006. HENHMETAFILE hEmf=CloseEnhMetaFile(dc.m_hDC);
  1007. UINT size=GetEnhMetaFileBits(hEmf,NULL,NULL);
  1008. CString strEmf((char)NULL,size);
  1009. char* bufEmf=strEmf.GetBuffer(size);
  1010. GetEnhMetaFileBits(hEmf,size,(LPBYTE)bufEmf);
  1011. strEmf.ReleaseBuffer(size);
  1012. DeleteEnhMetaFile(hEmf);
  1013. // Save EMF to an Archive
  1014. CFileException fe;
  1015. CFile* pFile = new CFile;
  1016. if (!pFile->Open("c:\\users\\default\\sp.arc",
  1017. CFile::modeCreate | CFile::modeWrite | CFile::shareExclusive, &fe)) return;
  1018. CArchive saveArchive(pFile, CArchive::store | CArchive::bNoFlushOnDelete);
  1019. saveArchive << strEmf;
  1020. saveArchive.Close();
  1021. pFile->Close();
  1022. // Load EMF from an Archive
  1023. CString readEmf;
  1024. if (!pFile->Open("c:\\users\\default\\sp.arc",
  1025. CFile::modeRead| CFile::shareDenyWrite, &fe)) return;
  1026. CArchive loadArchive(pFile, CArchive::load | CArchive::bNoFlushOnDelete);
  1027. loadArchive >> readEmf;
  1028. int l=readEmf.GetLength();
  1029. loadArchive.Close();
  1030. pFile->Close();
  1031. delete pFile;
  1032. }
  1033. void CDrawView::OnDrawText()
  1034. {
  1035. CDrawTool::c_drawShape = text;
  1036. }
  1037. void CDrawView::OnUpdateDrawText(CCmdUI* pCmdUI)
  1038. {
  1039. pCmdUI->SetRadio(CDrawTool::c_drawShape == text);
  1040. }
  1041. void CDrawView::OnTextFont()
  1042. {
  1043. CHOOSEFONT cf ;
  1044. LOGFONT lf;
  1045. // set up initial font setting
  1046. memset(&lf,0,sizeof(LOGFONT)); // initialize
  1047. POSITION pos = m_selection.GetHeadPosition();
  1048. LOGFONT* plf;
  1049. cf.rgbColors = RGB(0,0,0);
  1050. while (pos != NULL)
  1051. {
  1052. CDrawObj* pObj = m_selection.GetNext(pos);
  1053. if ((plf=pObj->GetLogFontCopy()) != NULL) // is text object
  1054. {
  1055. memcpy(&lf,plf,sizeof(LOGFONT)); // get selected object's font as initial
  1056. cf.rgbColors = ((CDrawText*)pObj)->m_color; // fill the color field
  1057. break;
  1058. }
  1059. }
  1060. // match from LOGLOENGLISH to PIXELS used in common Dialog
  1061. CClientDC dc(this);
  1062. lf.lfHeight = MulDiv(lf.lfHeight,GetDeviceCaps(dc.m_hDC, LOGPIXELSY),100); //====
  1063. lf.lfCharSet = ANSI_CHARSET;
  1064. // set up choose font dialog data
  1065. cf.lStructSize = sizeof (CHOOSEFONT) ;
  1066. cf.hwndOwner = m_hWnd ;
  1067. cf.hDC = NULL ;
  1068. cf.lpLogFont = &lf ;
  1069. cf.iPointSize = 0 ;
  1070. cf.Flags = CF_INITTOLOGFONTSTRUCT | CF_SCREENFONTS | CF_EFFECTS ;
  1071. cf.lCustData = 0L ;
  1072. cf.lpfnHook = NULL ;
  1073. cf.lpTemplateName = NULL ;
  1074. cf.hInstance = NULL ;
  1075. cf.lpszStyle = NULL ;
  1076. cf.nFontType = 0 ; // Returned from ChooseFont
  1077. cf.nSizeMin = 0 ;
  1078. cf.nSizeMax = 0 ;
  1079. // do choose font dialog
  1080. if(!ChooseFont (&cf)) return; // aborts
  1081. // adjust back to using TWIPS
  1082. lf.lfHeight = MulDiv(lf.lfHeight,100,GetDeviceCaps(dc.m_hDC, LOGPIXELSY)); // -.5 ====
  1083. lf.lfWidth=0; //lf.lfOutPrecision=7; // ====
  1084. LOGFONT lfcopy=lf;
  1085. if (!AdjustLogFont(&lf))
  1086. MessageBox("Error occured in Adjust Font");
  1087. // change the fonts of selected objects
  1088. pos = m_selection.GetHeadPosition();
  1089. while (pos != NULL)
  1090. {
  1091. CDrawObj* pObj = m_selection.GetNext(pos);
  1092. pObj->SetLogFont(&lf,&lfcopy);
  1093. ((CDrawText*)pObj)->m_color = cf.rgbColors; // set text color
  1094. // set font for edit box
  1095. if (m_pedit!=NULL && m_ptext==pObj)
  1096. {
  1097. delete m_peditfont;
  1098. // match from LOGLOENGLISH to PIXELS used in common Dialog
  1099. LOGFONT lf = *(m_ptext->GetLogFont());
  1100. CWindowDC dc(AfxGetMainWnd());
  1101. lf.lfHeight = MulDiv(lf.lfHeight,GetDeviceCaps(dc.m_hDC, LOGPIXELSY),100); //====
  1102. lf.lfWidth = 0;
  1103. m_peditfont = new CFont;
  1104. m_peditfont->CreateFontIndirect(&lf); // create font
  1105. m_pedit->SetFont(m_peditfont);
  1106. }
  1107. }
  1108. }
  1109. void CDrawView::OnUpdateTextFont(CCmdUI* pCmdUI)
  1110. {
  1111. POSITION pos = m_selection.GetHeadPosition();
  1112. while (pos != NULL)
  1113. {
  1114. CDrawObj* pObj = m_selection.GetNext(pos);
  1115. if (pObj->GetLogFontCopy()) {pCmdUI->Enable(TRUE); return;}
  1116. }
  1117. pCmdUI->Enable(FALSE);
  1118. }
  1119. void CDrawView::OnTextInsJobinfo()
  1120. {
  1121. CMainFrame* pMainWnd=(CMainFrame*)AfxGetMainWnd();
  1122. UINT index=pMainWnd->m_wndToolBar.m_comboBox.GetCurSel();
  1123. if (index==CB_ERR) return; // no job-info selected
  1124. if (m_pedit) // text in edit
  1125. {
  1126. m_pedit->ReplaceSel(JobInfo[index].insert);
  1127. CString str;
  1128. m_pedit->GetWindowText(str);
  1129. m_ptext->SetText(str);
  1130. return;
  1131. }
  1132. POSITION pos = m_selection.GetHeadPosition();
  1133. while (pos != NULL)
  1134. {
  1135. CDrawObj* pObj = m_selection.GetNext(pos);
  1136. pObj->SetText(pObj->GetText()+JobInfo[index].insert);
  1137. }
  1138. }
  1139. void CDrawView::CloseEdit()
  1140. {
  1141. if (m_pedit!=NULL)
  1142. {
  1143. CString str;
  1144. m_pedit->GetWindowText(str);
  1145. m_ptext->SetText(str); // retrieve modified text
  1146. delete m_peditfont;
  1147. delete m_peditbrush;
  1148. delete m_pedit;
  1149. m_pedit=NULL;
  1150. m_ptext->Invalidate(); // redraw
  1151. // reload normal-state accelerator
  1152. ReplaceAccelTable(IDR_MAINFRAME);
  1153. }
  1154. }
  1155. void CDrawView::OnUpdateTextInsJobinfo(CCmdUI* pCmdUI)
  1156. {
  1157. if (((CMainFrame*)AfxGetMainWnd())->m_wndToolBar.m_comboBox.GetCurSel()==CB_ERR)
  1158. {pCmdUI->Enable(FALSE); return;}
  1159. OnUpdateTextFont(pCmdUI);
  1160. }
  1161. void CDrawView::ReplaceAccelTable(UINT nID)
  1162. {
  1163. CMainFrame* pframe=(CMainFrame*)AfxGetMainWnd();
  1164. if (pframe->m_hAccelTable!=NULL)
  1165. {
  1166. DestroyAcceleratorTable(pframe->m_hAccelTable);
  1167. pframe->m_hAccelTable=NULL;
  1168. }
  1169. pframe->LoadAccelTable(MAKEINTRESOURCE(nID));
  1170. }
  1171. int CDrawView::AdjustLogFont(LOGFONT *plf)
  1172. {
  1173. CClientDC display(this);
  1174. CFont font1;
  1175. TEXTMETRIC tm;
  1176. OnPrepareDC(&display,NULL);
  1177. if (!font1.CreateFontIndirect(plf)) return FALSE;
  1178. if (!display.SelectObject(&font1)) return FALSE;
  1179. if (!display.GetTextMetrics(&tm)) return FALSE;
  1180. plf->lfHeight=tm.tmHeight;
  1181. CPrintDialog printdialog(TRUE);
  1182. CDC printer;
  1183. CFont font2;
  1184. HDC hPrt;
  1185. hPrt = ((CDrawApp*)AfxGetApp()) -> GetDefaultPrinterIC() ;
  1186. if (!hPrt) return FALSE;
  1187. if (!printer.Attach(hPrt)) return FALSE;
  1188. OnPrepareDC(&printer,NULL);
  1189. if (!font2.CreateFontIndirect(plf)) return FALSE;
  1190. if (!printer.SelectObject(&font2)) return FALSE;
  1191. if (!printer.GetTextMetrics(&tm)) return FALSE;
  1192. if (plf->lfHeight==tm.tmHeight) return TRUE;
  1193. plf->lfHeight=tm.tmHeight;
  1194. CFont font3;
  1195. if (!font3.CreateFontIndirect(plf)) return FALSE;
  1196. if (!display.SelectObject(&font3)) return FALSE;
  1197. if (!display.GetTextMetrics(&tm)) return FALSE;
  1198. if (plf->lfHeight==tm.tmHeight) return TRUE;
  1199. return FALSE;
  1200. }
  1201. HBRUSH CDrawView::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlcolor)
  1202. {
  1203. pDC->SetTextColor(m_ptext->m_color);
  1204. pDC->SetBkColor(GetDocument()->GetPaperColor());
  1205. return (HBRUSH)m_peditbrush->GetSafeHandle();
  1206. }
  1207. void CDrawView::SetSelectionTextAlign(UINT align)
  1208. {
  1209. POSITION pos = m_selection.GetHeadPosition();
  1210. while (pos != NULL)
  1211. {
  1212. CDrawObj* pObj = m_selection.GetNext(pos);
  1213. int oldalign=pObj->GetTextAlign();
  1214. pObj->SetTextAlign(align);
  1215. pObj->Invalidate();
  1216. if (m_pedit!=NULL && m_ptext==pObj)
  1217. {
  1218. CloseEdit();
  1219. m_ptext->OnOpen(this);
  1220. // LONG style=GetWindowLong(m_pedit->m_hWnd,GWL_STYLE);
  1221. // style &= ~AlignTable[oldalign].editstyle;
  1222. // style |= AlignTable[m_ptext->GetTextAlign()].editstyle;
  1223. // style=SetWindowLong(m_pedit->m_hWnd,GWL_STYLE,style);
  1224. }
  1225. }
  1226. }
  1227. int CDrawView::GetSelectionTextAlign()
  1228. {
  1229. int oldalign=-1, newalign;
  1230. POSITION pos = m_selection.GetHeadPosition();
  1231. while (pos != NULL)
  1232. {
  1233. CDrawObj* pObj = m_selection.GetNext(pos);
  1234. if ((newalign=pObj->GetTextAlign()) < 0) continue;
  1235. if (oldalign<0) oldalign=newalign;
  1236. else if (oldalign!=newalign) return -1; // no common align
  1237. }
  1238. return oldalign;
  1239. }
  1240. void CDrawView::OnTextLeft()
  1241. {
  1242. SetSelectionTextAlign(ALIGNLEFT);
  1243. }
  1244. void CDrawView::OnUpdateTextLeft(CCmdUI* pCmdUI)
  1245. {
  1246. pCmdUI->SetRadio(GetSelectionTextAlign()==ALIGNLEFT);
  1247. OnUpdateTextFont(pCmdUI);
  1248. }
  1249. void CDrawView::OnTextCenter()
  1250. {
  1251. SetSelectionTextAlign(ALIGNCENTER);
  1252. }
  1253. void CDrawView::OnUpdateTextCenter(CCmdUI* pCmdUI)
  1254. {
  1255. pCmdUI->SetRadio(GetSelectionTextAlign()==ALIGNCENTER);
  1256. OnUpdateTextFont(pCmdUI);
  1257. }
  1258. void CDrawView::OnTextRight()
  1259. {
  1260. SetSelectionTextAlign(ALIGNRIGHT);
  1261. }
  1262. void CDrawView::OnUpdateTextRight(CCmdUI* pCmdUI)
  1263. {
  1264. pCmdUI->SetRadio(GetSelectionTextAlign()==ALIGNRIGHT);
  1265. OnUpdateTextFont(pCmdUI);
  1266. }