WORDPVW.CPP
上传用户:aakk678
上传日期:2022-07-09
资源大小:406k
文件大小:24k
源码类别:

界面编程

开发平台:

Visual C++

  1. // wordpvw.cpp : implementation of the CWordPadView class
  2. //
  3. // This is a part of the Microsoft Foundation Classes C++ library.
  4. // Copyright (C) 1992-1997 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 related
  9. // electronic 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 "wordpad.h"
  14. #include "cntritem.h"
  15. #include "srvritem.h"
  16. #include "wordpdoc.h"
  17. #include "wordpvw.h"
  18. #include "formatta.h"
  19. #include "datedial.h"
  20. #include "formatpa.h"
  21. #include "formatba.h"
  22. #include "ruler.h"
  23. #include "strings.h"
  24. #include "colorlis.h"
  25. #include "pageset.h"
  26. #include <penwin.h>
  27. extern CLIPFORMAT cfEmbeddedObject;
  28. extern CLIPFORMAT cfRTO;
  29. #ifdef _DEBUG
  30. #undef THIS_FILE
  31. static char BASED_CODE THIS_FILE[] = __FILE__;
  32. #endif
  33. BOOL CCharFormat::operator==(CCharFormat& cf)
  34. {
  35. return 
  36. dwMask == cf.dwMask
  37. && dwEffects == cf.dwEffects
  38. && yHeight == cf.yHeight
  39. && yOffset == cf.yOffset
  40. && crTextColor == cf.crTextColor
  41. && bPitchAndFamily == cf.bPitchAndFamily
  42. && (lstrcmpA(szFaceName, cf.szFaceName) == 0);
  43. }
  44. BOOL CParaFormat::operator==(PARAFORMAT& pf)
  45. {
  46. if(
  47. dwMask != pf.dwMask
  48. || wNumbering != pf.wNumbering
  49. || wReserved != pf.wReserved
  50. || dxStartIndent != pf.dxStartIndent
  51. || dxRightIndent != pf.dxRightIndent
  52. || dxOffset != pf.dxOffset
  53. || cTabCount != pf.cTabCount
  54. )
  55. {
  56. return FALSE;
  57. }
  58. for (int i=0;i<pf.cTabCount;i++)
  59. {
  60. if (rgxTabs[i] != pf.rgxTabs[i])
  61. return FALSE;
  62. }
  63. return TRUE;
  64. }
  65. /////////////////////////////////////////////////////////////////////////////
  66. // CWordPadView
  67. IMPLEMENT_DYNCREATE(CWordPadView, CRichEditView)
  68. //WM_SETTINGCHANGE -- default printer might have changed
  69. //WM_FONTCHANGE -- pool of fonts changed
  70. //WM_DEVMODECHANGE -- printer settings changes
  71. BEGIN_MESSAGE_MAP(CWordPadView, CRichEditView)
  72. ON_COMMAND(ID_CANCEL_EDIT_CNTR, OnCancelEditCntr)
  73. ON_COMMAND(ID_CANCEL_EDIT_SRVR, OnCancelEditSrvr)
  74. //{{AFX_MSG_MAP(CWordPadView)
  75. ON_COMMAND(ID_PAGE_SETUP, OnPageSetup)
  76. ON_COMMAND(ID_CHAR_BOLD, OnCharBold)
  77. ON_UPDATE_COMMAND_UI(ID_CHAR_BOLD, OnUpdateCharBold)
  78. ON_COMMAND(ID_CHAR_ITALIC, OnCharItalic)
  79. ON_UPDATE_COMMAND_UI(ID_CHAR_ITALIC, OnUpdateCharItalic)
  80. ON_COMMAND(ID_CHAR_UNDERLINE, OnCharUnderline)
  81. ON_UPDATE_COMMAND_UI(ID_CHAR_UNDERLINE, OnUpdateCharUnderline)
  82. ON_COMMAND(ID_PARA_CENTER, OnParaCenter)
  83. ON_UPDATE_COMMAND_UI(ID_PARA_CENTER, OnUpdateParaCenter)
  84. ON_COMMAND(ID_PARA_LEFT, OnParaLeft)
  85. ON_UPDATE_COMMAND_UI(ID_PARA_LEFT, OnUpdateParaLeft)
  86. ON_COMMAND(ID_PARA_RIGHT, OnParaRight)
  87. ON_UPDATE_COMMAND_UI(ID_PARA_RIGHT, OnUpdateParaRight)
  88. ON_WM_CREATE()
  89. ON_COMMAND(ID_INSERT_DATE_TIME, OnInsertDateTime)
  90. ON_COMMAND(ID_FORMAT_PARAGRAPH, OnFormatParagraph)
  91. ON_COMMAND(ID_FORMAT_TABS, OnFormatTabs)
  92. ON_COMMAND(ID_COLOR16, OnColorDefault)
  93. ON_WM_TIMER()
  94. ON_WM_DESTROY()
  95. ON_WM_MEASUREITEM()
  96. ON_COMMAND(ID_PEN_BACKSPACE, OnPenBackspace)
  97. ON_COMMAND(ID_PEN_NEWLINE, OnPenNewline)
  98. ON_COMMAND(ID_PEN_PERIOD, OnPenPeriod)
  99. ON_COMMAND(ID_PEN_SPACE, OnPenSpace)
  100. ON_WM_SIZE()
  101. ON_WM_KEYDOWN()
  102. ON_COMMAND(ID_FILE_PRINT, OnFilePrint)
  103. ON_COMMAND(ID_FILE_PRINT_DIRECT, OnFilePrint)
  104. ON_WM_DROPFILES()
  105. ON_COMMAND(ID_PEN_LENS, OnPenLens)
  106. ON_COMMAND(ID_PEN_TAB, OnPenTab)
  107. ON_WM_PALETTECHANGED()
  108. ON_WM_QUERYNEWPALETTE()
  109. ON_WM_SETTINGCHANGE()
  110. //}}AFX_MSG_MAP
  111. // Standard printing commands
  112. ON_COMMAND(ID_INSERT_BULLET, CRichEditView::OnBullet)
  113. ON_UPDATE_COMMAND_UI(ID_INSERT_BULLET, CRichEditView::OnUpdateBullet)
  114. ON_COMMAND(ID_FILE_PRINT_PREVIEW, CRichEditView::OnFilePrintPreview)
  115. ON_COMMAND_RANGE(ID_COLOR0, ID_COLOR16, OnColorPick)
  116. ON_EN_CHANGE(AFX_IDW_PANE_FIRST, OnEditChange)
  117. ON_WM_MOUSEACTIVATE()
  118. ON_REGISTERED_MESSAGE(CWordPadApp::m_nPrinterChangedMsg, OnPrinterChangedMsg)
  119. ON_NOTIFY(FN_GETFORMAT, ID_VIEW_FORMATBAR, OnGetCharFormat)
  120. ON_NOTIFY(FN_SETFORMAT, ID_VIEW_FORMATBAR, OnSetCharFormat)
  121. ON_NOTIFY(NM_SETFOCUS, ID_VIEW_FORMATBAR, OnBarSetFocus)
  122. ON_NOTIFY(NM_KILLFOCUS, ID_VIEW_FORMATBAR, OnBarKillFocus)
  123. ON_NOTIFY(NM_RETURN, ID_VIEW_FORMATBAR, OnBarReturn)
  124. END_MESSAGE_MAP()
  125. /////////////////////////////////////////////////////////////////////////////
  126. // CWordPadView construction/destruction
  127. CWordPadView::CWordPadView()
  128. {
  129. m_bSyncCharFormat = m_bSyncParaFormat = TRUE;
  130. m_uTimerID = 0;
  131. m_bDelayUpdateItems = FALSE;
  132. m_bOnBar = FALSE;
  133. m_bInPrint = FALSE;
  134. m_nPasteType = 0;
  135. m_rectMargin = theApp.m_rectPageMargin;
  136. }
  137. BOOL CWordPadView::PreCreateWindow(CREATESTRUCT& cs) 
  138. {
  139. BOOL bRes = CRichEditView::PreCreateWindow(cs);
  140. cs.style |= ES_SELECTIONBAR;
  141. return bRes;
  142. }
  143. /////////////////////////////////////////////////////////////////////////////
  144. // CWordPadView attributes
  145. BOOL CWordPadView::IsFormatText()
  146. {
  147. // this function checks to see if any formatting is not default text
  148. BOOL bRes = FALSE;
  149. CHARRANGE cr;
  150. CCharFormat cf;
  151. CParaFormat pf;
  152. GetRichEditCtrl().GetSel(cr);
  153. GetRichEditCtrl().HideSelection(TRUE, FALSE);
  154. GetRichEditCtrl().SetSel(0,-1);
  155. if (!(GetRichEditCtrl().GetSelectionType() & (SEL_OBJECT|SEL_MULTIOBJECT)))
  156. {
  157. GetRichEditCtrl().GetSelectionCharFormat(cf);
  158. if (cf == m_defTextCharFormat)
  159. {
  160. GetRichEditCtrl().GetParaFormat(pf);
  161. if (pf == m_defParaFormat) //compared using CParaFormat::operator==
  162. bRes = TRUE;
  163. }
  164. }
  165. GetRichEditCtrl().SetSel(cr);
  166. GetRichEditCtrl().HideSelection(FALSE, FALSE);
  167. return bRes;
  168. }
  169. HMENU CWordPadView::GetContextMenu(WORD, LPOLEOBJECT, CHARRANGE* )
  170. {
  171. CRichEditCntrItem* pItem = GetSelectedItem();
  172. if (pItem == NULL || !pItem->IsInPlaceActive())
  173. {
  174. CMenu menuText;
  175. menuText.LoadMenu(IDR_TEXT_POPUP);
  176. CMenu* pMenuPopup = menuText.GetSubMenu(0);
  177. menuText.RemoveMenu(0, MF_BYPOSITION);
  178. if (!GetSystemMetrics(SM_PENWINDOWS))
  179. {
  180. //delete pen specific stuff
  181. // remove Insert Keystrokes
  182. pMenuPopup->DeleteMenu(ID_PEN_LENS, MF_BYCOMMAND);
  183. int nIndex = pMenuPopup->GetMenuItemCount()-1; //index of last item
  184. // remove Edit Text...
  185. pMenuPopup->DeleteMenu(nIndex, MF_BYPOSITION);
  186. // remove separator
  187. pMenuPopup->DeleteMenu(nIndex-1, MF_BYPOSITION);
  188. }
  189. return pMenuPopup->Detach();
  190. }
  191. return NULL;
  192. }
  193. /////////////////////////////////////////////////////////////////////////////
  194. // CWordPadView operations
  195. void CWordPadView::WrapChanged()
  196. {
  197. CWaitCursor wait;
  198. CFrameWnd* pFrameWnd = GetParentFrame();
  199. ASSERT(pFrameWnd != NULL);
  200. pFrameWnd->SetMessageText(IDS_FORMATTING);
  201. CWnd* pBarWnd = pFrameWnd->GetMessageBar();
  202. if (pBarWnd != NULL)
  203. pBarWnd->UpdateWindow();
  204. CRichEditView::WrapChanged();
  205. pFrameWnd->SetMessageText(AFX_IDS_IDLEMESSAGE);
  206. if (pBarWnd != NULL)
  207. pBarWnd->UpdateWindow();
  208. }
  209. void CWordPadView::SetUpdateTimer()
  210. {
  211. if (m_uTimerID != 0) // if outstanding timer kill it
  212. KillTimer(m_uTimerID);
  213. m_uTimerID = SetTimer(1, 1000, NULL); //set a timer for 1000 milliseconds
  214. if (m_uTimerID == 0) // no timer available so force update now
  215. GetDocument()->UpdateAllItems(NULL);
  216. else
  217. m_bDelayUpdateItems = TRUE;
  218. }
  219. void CWordPadView::DeleteContents()
  220. {
  221. ASSERT_VALID(this);
  222. ASSERT(m_hWnd != NULL);
  223. CRichEditView::DeleteContents();
  224. SetDefaultFont(IsTextType(GetDocument()->m_nNewDocType));
  225. }
  226. void CWordPadView::SetDefaultFont(BOOL bText)
  227. {
  228. ASSERT_VALID(this);
  229. ASSERT(m_hWnd != NULL);
  230. m_bSyncCharFormat = m_bSyncParaFormat = TRUE;
  231. CHARFORMAT* pCharFormat = bText ? &m_defTextCharFormat : &m_defCharFormat;
  232. // set the default character format -- the FALSE makes it the default
  233. GetRichEditCtrl().SetSel(0,-1);
  234. GetRichEditCtrl().SetDefaultCharFormat(*pCharFormat);
  235. GetRichEditCtrl().SetSelectionCharFormat(*pCharFormat);
  236. GetRichEditCtrl().SetParaFormat(m_defParaFormat);
  237. GetRichEditCtrl().SetSel(0,0);
  238. GetRichEditCtrl().EmptyUndoBuffer();
  239. GetRichEditCtrl().SetModify(FALSE);
  240. ASSERT_VALID(this);
  241. }
  242. /////////////////////////////////////////////////////////////////////////////
  243. // CWordPadView drawing
  244. /////////////////////////////////////////////////////////////////////////////
  245. // CWordPadView printing
  246. void CWordPadView::OnPrint(CDC* pDC, CPrintInfo* pInfo) 
  247. {
  248. CRichEditView::OnPrint(pDC, pInfo);
  249. if (pInfo != NULL && pInfo->m_bPreview)
  250. DrawMargins(pDC);
  251. }
  252. void CWordPadView::DrawMargins(CDC* pDC)
  253. {
  254. if (pDC->m_hAttribDC != NULL)
  255. {
  256. CRect rect;
  257. rect.left = m_rectMargin.left;
  258. rect.right = m_sizePaper.cx - m_rectMargin.right;
  259. rect.top = m_rectMargin.top;
  260. rect.bottom = m_sizePaper.cy - m_rectMargin.bottom;
  261. //rect in twips
  262. int logx = ::GetDeviceCaps(pDC->m_hDC, LOGPIXELSX);
  263. int logy = ::GetDeviceCaps(pDC->m_hDC, LOGPIXELSY);
  264. rect.left = MulDiv(rect.left, logx, 1440);
  265. rect.right = MulDiv(rect.right, logx, 1440);
  266. rect.top = MulDiv(rect.top, logy, 1440);
  267. rect.bottom = MulDiv(rect.bottom, logy, 1440);
  268. CPen pen(PS_DOT, 0, pDC->GetTextColor());
  269. CPen* ppen = pDC->SelectObject(&pen);
  270. pDC->MoveTo(0, rect.top);
  271. pDC->LineTo(10000, rect.top);
  272. pDC->MoveTo(rect.left, 0);
  273. pDC->LineTo(rect.left, 10000);
  274. pDC->MoveTo(0, rect.bottom);
  275. pDC->LineTo(10000, rect.bottom);
  276. pDC->MoveTo(rect.right, 0);
  277. pDC->LineTo(rect.right, 10000);
  278. pDC->SelectObject(ppen);
  279. }
  280. }
  281. BOOL CWordPadView::OnPreparePrinting(CPrintInfo* pInfo)
  282. {
  283. return DoPreparePrinting(pInfo);
  284. }
  285. /////////////////////////////////////////////////////////////////////////////
  286. // OLE Client support and commands
  287. inline int roundleast(int n)
  288. {
  289. int mod = n%10;
  290. n -= mod;
  291. if (mod >= 5)
  292. n += 10;
  293. else if (mod <= -5)
  294. n -= 10;
  295. return n;
  296. }
  297. static void RoundRect(LPRECT r1)
  298. {
  299. r1->left = roundleast(r1->left);
  300. r1->right = roundleast(r1->right);
  301. r1->top = roundleast(r1->top);
  302. r1->bottom = roundleast(r1->bottom);
  303. }
  304. static void MulDivRect(LPRECT r1, LPRECT r2, int num, int div)
  305. {
  306. r1->left = MulDiv(r2->left, num, div);
  307. r1->top = MulDiv(r2->top, num, div);
  308. r1->right = MulDiv(r2->right, num, div);
  309. r1->bottom = MulDiv(r2->bottom, num, div);
  310. }
  311. void CWordPadView::OnPageSetup()
  312. {
  313. CPageSetupDialog dlg;
  314. PAGESETUPDLG& psd = dlg.m_psd;
  315. BOOL bMetric = theApp.GetUnits() == 1; //centimeters
  316. psd.Flags |= PSD_MARGINS | (bMetric ? PSD_INHUNDREDTHSOFMILLIMETERS : 
  317. PSD_INTHOUSANDTHSOFINCHES);
  318. int nUnitsPerInch = bMetric ? 2540 : 1000;
  319. MulDivRect(&psd.rtMargin, m_rectMargin, nUnitsPerInch, 1440);
  320. RoundRect(&psd.rtMargin);
  321. // get the current device from the app
  322. PRINTDLG pd;
  323. pd.hDevNames = NULL;
  324. pd.hDevMode = NULL;
  325. theApp.GetPrinterDeviceDefaults(&pd);
  326. psd.hDevNames = pd.hDevNames;
  327. psd.hDevMode = pd.hDevMode;
  328. if (dlg.DoModal() == IDOK)
  329. {
  330. RoundRect(&psd.rtMargin);
  331. MulDivRect(m_rectMargin, &psd.rtMargin, 1440, nUnitsPerInch);
  332. theApp.m_rectPageMargin = m_rectMargin;
  333. theApp.SelectPrinter(psd.hDevNames, psd.hDevMode);
  334. theApp.NotifyPrinterChanged();
  335. }
  336. // PageSetupDlg failed
  337. if (CommDlgExtendedError() != 0)
  338. {
  339. CPageSetupDlg dlg;
  340. dlg.m_nBottomMargin = m_rectMargin.bottom;
  341. dlg.m_nLeftMargin = m_rectMargin.left;
  342. dlg.m_nRightMargin = m_rectMargin.right;
  343. dlg.m_nTopMargin = m_rectMargin.top;
  344. if (dlg.DoModal() == IDOK)
  345. {
  346. m_rectMargin.SetRect(dlg.m_nLeftMargin, dlg.m_nTopMargin, 
  347. dlg.m_nRightMargin, dlg.m_nBottomMargin);
  348. // m_page will be changed at this point
  349. theApp.m_rectPageMargin = m_rectMargin;
  350. theApp.NotifyPrinterChanged();
  351. }
  352. }
  353. }
  354. /////////////////////////////////////////////////////////////////////////////
  355. // OLE Server support
  356. // The following command handler provides the standard keyboard
  357. //  user interface to cancel an in-place editing session.  Here,
  358. //  the server (not the container) causes the deactivation.
  359. void CWordPadView::OnCancelEditSrvr()
  360. {
  361. GetDocument()->OnDeactivateUI(FALSE);
  362. }
  363. /////////////////////////////////////////////////////////////////////////////
  364. // CWordPadView diagnostics
  365. #ifdef _DEBUG
  366. void CWordPadView::AssertValid() const
  367. {
  368. CRichEditView::AssertValid();
  369. }
  370. void CWordPadView::Dump(CDumpContext& dc) const
  371. {
  372. CRichEditView::Dump(dc);
  373. }
  374. CWordPadDoc* CWordPadView::GetDocument() // non-debug version is inline
  375. {
  376. return (CWordPadDoc*)m_pDocument;
  377. }
  378. #endif //_DEBUG
  379. /////////////////////////////////////////////////////////////////////////////
  380. // CWordPadView message helpers
  381. /////////////////////////////////////////////////////////////////////////////
  382. // CWordPadView message handlers
  383. int CWordPadView::OnCreate(LPCREATESTRUCT lpCreateStruct) 
  384. {
  385. if (CRichEditView::OnCreate(lpCreateStruct) == -1)
  386. return -1;
  387. theApp.m_listPrinterNotify.AddTail(m_hWnd);
  388. if (theApp.m_bWordSel)
  389. GetRichEditCtrl().SetOptions(ECOOP_OR, ECO_AUTOWORDSELECTION);
  390. else
  391. GetRichEditCtrl().SetOptions(ECOOP_AND, ~(DWORD)ECO_AUTOWORDSELECTION);
  392. // GetRichEditCtrl().SetOptions(ECOOP_OR, ECO_SELECTIONBAR);
  393. GetDefaultFont(m_defTextCharFormat, IDS_DEFAULTTEXTFONT);
  394. GetDefaultFont(m_defCharFormat, IDS_DEFAULTFONT);
  395. GetRichEditCtrl().GetParaFormat(m_defParaFormat);
  396. m_defParaFormat.cTabCount = 0;
  397. return 0;
  398. }
  399. void CWordPadView::GetDefaultFont(CCharFormat& cf, UINT nFontNameID)
  400. {
  401. USES_CONVERSION;
  402. CString strDefFont;
  403. VERIFY(strDefFont.LoadString(nFontNameID));
  404. ASSERT(cf.cbSize == sizeof(CHARFORMAT));
  405. cf.dwMask = CFM_BOLD|CFM_ITALIC|CFM_UNDERLINE|CFM_STRIKEOUT|CFM_SIZE|
  406. CFM_COLOR|CFM_OFFSET|CFM_PROTECTED;
  407. cf.dwEffects = CFE_AUTOCOLOR;
  408. cf.yHeight = 200; //10pt
  409. cf.yOffset = 0;
  410. cf.crTextColor = RGB(0, 0, 0);
  411. cf.bCharSet = 0;
  412. cf.bPitchAndFamily = DEFAULT_PITCH | FF_DONTCARE;
  413. ASSERT(strDefFont.GetLength() < LF_FACESIZE);
  414. lstrcpynA(cf.szFaceName, T2A((LPTSTR) (LPCTSTR) strDefFont), LF_FACESIZE);
  415. cf.dwMask |= CFM_FACE;
  416. }
  417. void CWordPadView::OnInsertDateTime() 
  418. {
  419. CDateDialog dlg;
  420. if (dlg.DoModal() == IDOK)
  421. GetRichEditCtrl().ReplaceSel(dlg.m_strSel);;
  422. }
  423. void CWordPadView::OnFormatParagraph() 
  424. {
  425. CFormatParaDlg dlg(GetParaFormatSelection());
  426. dlg.m_nWordWrap = m_nWordWrap;
  427. if (dlg.DoModal() == IDOK)
  428. SetParaFormat(dlg.m_pf);
  429. }
  430. void CWordPadView::OnFormatTabs() 
  431. {
  432. CFormatTabDlg dlg(GetParaFormatSelection());
  433. if (dlg.DoModal() == IDOK)
  434. SetParaFormat(dlg.m_pf);
  435. }
  436. void CWordPadView::OnTextNotFound(LPCTSTR lpStr)
  437. {
  438. ASSERT_VALID(this);
  439. MessageBeep(0);
  440. AfxMessageBox(IDS_FINISHED_SEARCH,MB_OK|MB_ICONINFORMATION);
  441. CRichEditView::OnTextNotFound(lpStr);
  442. }
  443. void CWordPadView::OnColorPick(UINT nID)
  444. {
  445. CRichEditView::OnColorPick(CColorMenu::GetColor(nID));
  446. }
  447. void CWordPadView::OnTimer(UINT nIDEvent) 
  448. {
  449. if (m_uTimerID != nIDEvent) // not our timer
  450. CRichEditView::OnTimer(nIDEvent);
  451. else
  452. {
  453. KillTimer(m_uTimerID); // kill one-shot timer
  454. m_uTimerID = 0;
  455. if (m_bDelayUpdateItems)
  456. GetDocument()->UpdateAllItems(NULL);
  457. m_bDelayUpdateItems = FALSE;
  458. }
  459. }
  460. void CWordPadView::OnEditChange()
  461. {
  462. SetUpdateTimer();
  463. }
  464. void CWordPadView::OnDestroy() 
  465. {
  466. POSITION pos = theApp.m_listPrinterNotify.Find(m_hWnd);
  467. ASSERT(pos != NULL);
  468. theApp.m_listPrinterNotify.RemoveAt(pos);
  469. CRichEditView::OnDestroy();
  470. if (m_uTimerID != 0) // if outstanding timer kill it
  471. OnTimer(m_uTimerID);
  472. ASSERT(m_uTimerID == 0);
  473. }
  474. void CWordPadView::CalcWindowRect(LPRECT lpClientRect, UINT nAdjustType) 
  475. {
  476. int nOldWidth = lpClientRect->right - lpClientRect->left;
  477. CRichEditView::CalcWindowRect(lpClientRect, nAdjustType);
  478. if (theApp.m_bWin4 && nAdjustType != 0 && (GetStyle() & WS_VSCROLL))
  479. lpClientRect->right--;
  480. // if the ruler is visible then slide the view up under the ruler to avoid
  481. // showing the top border of the view
  482. if (GetExStyle() & WS_EX_CLIENTEDGE)
  483. {
  484. CFrameWnd* pFrame = GetParentFrame();
  485. if (pFrame != NULL)
  486. {
  487. CRulerBar* pBar = (CRulerBar*)pFrame->GetControlBar(ID_VIEW_RULER);
  488. if (pBar != NULL)
  489. {
  490. BOOL bVis = pBar->IsVisible();
  491. if (pBar->m_bDeferInProgress)
  492. bVis = !bVis;
  493. if (bVis)
  494. lpClientRect->top -= 2;
  495. }
  496. }
  497. }
  498. }
  499. void CWordPadView::OnMeasureItem(int nIDCtl, LPMEASUREITEMSTRUCT lpMIS) 
  500. {
  501. lpMIS->itemID = (UINT)(WORD)lpMIS->itemID;
  502. CRichEditView::OnMeasureItem(nIDCtl, lpMIS);
  503. }
  504. void CWordPadView::OnPenBackspace() 
  505. {
  506. SendMessage(WM_KEYDOWN, VK_BACK, 0);
  507. SendMessage(WM_KEYUP, VK_BACK, 0);
  508. }
  509. void CWordPadView::OnPenNewline() 
  510. {
  511. SendMessage(WM_CHAR, 'n', 0);
  512. }
  513. void CWordPadView::OnPenPeriod() 
  514. {
  515. SendMessage(WM_CHAR, '.', 0);
  516. }
  517. void CWordPadView::OnPenSpace() 
  518. {
  519. SendMessage(WM_CHAR, ' ', 0);
  520. }
  521. void CWordPadView::OnPenTab() 
  522. {
  523. SendMessage(WM_CHAR, VK_TAB, 0);
  524. }
  525. void CWordPadView::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags) 
  526. {
  527. if (nChar == VK_F10 && GetKeyState(VK_SHIFT) < 0)
  528. {
  529. long nStart, nEnd;
  530. GetRichEditCtrl().GetSel(nStart, nEnd);
  531. CPoint pt = GetRichEditCtrl().GetCharPos(nEnd);
  532. SendMessage(WM_CONTEXTMENU, (WPARAM)m_hWnd, MAKELPARAM(pt.x, pt.y));
  533. }
  534. CRichEditView::OnKeyDown(nChar, nRepCnt, nFlags);
  535. }
  536. HRESULT CWordPadView::GetClipboardData(CHARRANGE* lpchrg, DWORD /*reco*/, 
  537. LPDATAOBJECT lpRichDataObj, LPDATAOBJECT* lplpdataobj)
  538. {
  539. CHARRANGE& cr = *lpchrg;
  540. if ((cr.cpMax - cr.cpMin == 1) && 
  541. GetRichEditCtrl().GetSelectionType() == SEL_OBJECT)
  542. {
  543. return E_NOTIMPL;
  544. }
  545. BeginWaitCursor();
  546. //create the data source
  547. COleDataSource* pDataSource = new COleDataSource;
  548. // put the formats into the data source
  549. LPENUMFORMATETC lpEnumFormatEtc;
  550. lpRichDataObj->EnumFormatEtc(DATADIR_SET, &lpEnumFormatEtc);
  551. if (lpEnumFormatEtc != NULL)
  552. {
  553. FORMATETC etc;
  554. while (lpEnumFormatEtc->Next(1, &etc, NULL) == S_OK) 
  555. {
  556. STGMEDIUM stgMedium;
  557. lpRichDataObj->GetData(&etc, &stgMedium);
  558. pDataSource->CacheData(etc.cfFormat, &stgMedium, &etc);
  559. }
  560. lpEnumFormatEtc->Release();
  561. }
  562. CEmbeddedItem item(GetDocument(), cr.cpMin, cr.cpMax);
  563. item.m_lpRichDataObj = lpRichDataObj;
  564. // get wordpad formats
  565. item.GetClipboardData(pDataSource);
  566. // get the IDataObject from the data source
  567. *lplpdataobj =  (LPDATAOBJECT)pDataSource->GetInterface(&IID_IDataObject);
  568. EndWaitCursor();
  569. return S_OK;
  570. }
  571. HRESULT CWordPadView::QueryAcceptData(LPDATAOBJECT lpdataobj,
  572. CLIPFORMAT* lpcfFormat, DWORD reco, BOOL bReally, 
  573. HGLOBAL hMetaPict)
  574. {
  575. if (bReally && *lpcfFormat == 0 && (m_nPasteType == 0))
  576. {
  577. COleDataObject dataobj;
  578. dataobj.Attach(lpdataobj, FALSE);
  579. if (!dataobj.IsDataAvailable(cfRTO)) // native avail, let richedit do as it wants
  580. {
  581. if (dataobj.IsDataAvailable(cfEmbeddedObject))
  582. {
  583. if (PasteNative(lpdataobj))
  584. return S_FALSE;
  585. }
  586. }
  587. }
  588. return CRichEditView::QueryAcceptData(lpdataobj, lpcfFormat, reco, bReally,
  589. hMetaPict);
  590. }
  591. BOOL CWordPadView::PasteNative(LPDATAOBJECT lpdataobj)
  592. {
  593. // check data object for wordpad object
  594. // if true, suck out RTF directly
  595. FORMATETC etc = {NULL, NULL, DVASPECT_CONTENT, -1, TYMED_ISTORAGE};
  596. etc.cfFormat = (CLIPFORMAT)cfEmbeddedObject;
  597. STGMEDIUM stgMedium = {TYMED_ISTORAGE, 0, NULL};
  598. // create an IStorage to transfer the data in
  599. LPLOCKBYTES lpLockBytes;
  600. if (FAILED(::CreateILockBytesOnHGlobal(NULL, TRUE, &lpLockBytes)))
  601. return FALSE;
  602. ASSERT(lpLockBytes != NULL);
  603. HRESULT hr = ::StgCreateDocfileOnILockBytes(lpLockBytes,
  604. STGM_SHARE_EXCLUSIVE|STGM_CREATE|STGM_READWRITE, 0, &stgMedium.pstg);
  605. lpLockBytes->Release(); //storage addref'd
  606. if (FAILED(hr))
  607. return FALSE;
  608. ASSERT(stgMedium.pstg != NULL);
  609. CLSID clsid;
  610. BOOL bRes = FALSE; //let richedit do what it wants
  611. if (SUCCEEDED(lpdataobj->GetDataHere(&etc, &stgMedium)) &&
  612. SUCCEEDED(ReadClassStg(stgMedium.pstg, &clsid)) &&
  613. clsid == GetDocument()->GetClassID())
  614. {
  615. //suck out RTF now
  616. // open Contents stream
  617. COleStreamFile file;
  618. CFileException fe;
  619. if (file.OpenStream(stgMedium.pstg, szContents,
  620. CFile::modeReadWrite|CFile::shareExclusive, &fe))
  621. {
  622. // load it with CArchive (loads from Contents stream)
  623. CArchive loadArchive(&file, CArchive::load | 
  624. CArchive::bNoFlushOnDelete);
  625. Stream(loadArchive, TRUE); //stream in selection
  626. hr = TRUE; // don't let richedit do anything
  627. }
  628. }
  629. ::ReleaseStgMedium(&stgMedium);
  630. return bRes;
  631. }
  632. // things to fix
  633. // if format==0 we are doing a straight EM_PASTE
  634. //  look for native formats
  635. // richedit specific -- allow richedit to handle (these will be first)
  636. //  look for RTF, CF_TEXT.  If there paste special as these
  637. //  Do standard OLE scenario
  638. // if pasting a particular format (format != 0)
  639. // if richedit specific, allow through
  640. // if RTF, CF_TEXT. paste special
  641. // if OLE format, do standard OLE scenario
  642. void CWordPadView::OnFilePrint() 
  643. {
  644. // don't allow winini changes to occur while printing
  645. m_bInPrint = TRUE;
  646. CRichEditView::OnFilePrint();
  647. // printer may have changed
  648. theApp.NotifyPrinterChanged(); // this will cause a GetDocument()->PrinterChanged();
  649. m_bInPrint = FALSE;
  650. }
  651. int CWordPadView::OnMouseActivate(CWnd* pWnd, UINT nHitTest, UINT message)
  652. {
  653. if (m_bOnBar)
  654. {
  655. SetFocus();
  656. return MA_ACTIVATEANDEAT;
  657. }
  658. else
  659. return CRichEditView::OnMouseActivate(pWnd, nHitTest, message);
  660. }
  661. typedef BOOL (WINAPI *PCWPROC)(HWND, LPSTR, UINT, LPVOID, DWORD, DWORD);
  662. void CWordPadView::OnPenLens() 
  663. {
  664. USES_CONVERSION;
  665. HINSTANCE hLib = LoadLibrary(_T("PENWIN32.DLL"));
  666. if (hLib == NULL)
  667. return;
  668. PCWPROC pCorrectWriting = (PCWPROC)GetProcAddress(hLib, "CorrectWriting");
  669. ASSERT(pCorrectWriting != NULL);
  670. if (pCorrectWriting != NULL)
  671. {
  672. CHARRANGE cr;
  673. GetRichEditCtrl().GetSel(cr);
  674. int nCnt = 2*(cr.cpMax-cr.cpMin);
  675. BOOL bSel = (nCnt != 0);
  676. nCnt = max(1024, nCnt);
  677. char* pBuf = new char[nCnt];
  678. pBuf[0] = NULL;
  679. if (bSel)
  680. GetRichEditCtrl().GetSelText(pBuf);
  681. if (pCorrectWriting(m_hWnd, pBuf, nCnt, 0, bSel ? 0 : CWR_INSERT, 0))
  682. GetRichEditCtrl().ReplaceSel(A2T(pBuf));
  683. delete [] pBuf;
  684. }
  685. FreeLibrary(hLib);
  686. }
  687. LONG CWordPadView::OnPrinterChangedMsg(UINT, LONG)
  688. {
  689. CDC dc;
  690. AfxGetApp()->CreatePrinterDC(dc);
  691. OnPrinterChanged(dc);
  692. return 0;
  693. }
  694. static void ForwardPaletteChanged(HWND hWndParent, HWND hWndFocus)
  695. {
  696. // this is a quick and dirty hack to send the WM_QUERYNEWPALETTE to a window that is interested
  697. HWND hWnd = NULL;
  698. for (hWnd = ::GetWindow(hWndParent, GW_CHILD); hWnd != NULL; hWnd = ::GetWindow(hWnd, GW_HWNDNEXT))
  699. {
  700. if (hWnd != hWndFocus)
  701. {
  702. ::SendMessage(hWnd, WM_PALETTECHANGED, (WPARAM)hWndFocus, 0L);
  703. ForwardPaletteChanged(hWnd, hWndFocus);
  704. }
  705. }
  706. }
  707. void CWordPadView::OnPaletteChanged(CWnd* pFocusWnd) 
  708. {
  709. ForwardPaletteChanged(m_hWnd, pFocusWnd->GetSafeHwnd());
  710. // allow the richedit control to realize its palette
  711. // remove this if if richedit fixes their code so that
  712. // they don't realize their palette into foreground
  713. if (::GetWindow(m_hWnd, GW_CHILD) == NULL)
  714. CRichEditView::OnPaletteChanged(pFocusWnd);
  715. }
  716. static BOOL FindQueryPalette(HWND hWndParent)
  717. {
  718. // this is a quick and dirty hack to send the WM_QUERYNEWPALETTE to a window that is interested
  719. HWND hWnd = NULL;
  720. for (hWnd = ::GetWindow(hWndParent, GW_CHILD); hWnd != NULL; hWnd = ::GetWindow(hWnd, GW_HWNDNEXT))
  721. {
  722. if (::SendMessage(hWnd, WM_QUERYNEWPALETTE, 0, 0L))
  723. return TRUE;
  724. else if (FindQueryPalette(hWnd))
  725. return TRUE;
  726. }
  727. return FALSE;
  728. }
  729. BOOL CWordPadView::OnQueryNewPalette() 
  730. {
  731. if(FindQueryPalette(m_hWnd))
  732. return TRUE;
  733. return CRichEditView::OnQueryNewPalette();
  734. }
  735. void CWordPadView::OnSettingChange(UINT uFlags, LPCTSTR lpszSection)
  736. {
  737. CRichEditView::OnSettingChange(uFlags, lpszSection);
  738. //printer might have changed
  739. if (!m_bInPrint)
  740. {
  741. if (lstrcmpi(lpszSection, _T("windows")) == 0)
  742. theApp.NotifyPrinterChanged(TRUE); // force update to defaults
  743. }
  744. }
  745. void CWordPadView::OnSize(UINT nType, int cx, int cy)
  746. {
  747. CRichEditView::OnSize(nType, cx, cy);
  748. CRect rect(HORZ_TEXTOFFSET, VERT_TEXTOFFSET, cx, cy);
  749. GetRichEditCtrl().SetRect(rect);
  750. }
  751. void CWordPadView::OnGetCharFormat(NMHDR* pNMHDR, LRESULT* pRes)
  752. {
  753. ASSERT(pNMHDR != NULL);
  754. ASSERT(pRes != NULL);
  755. ((CHARHDR*)pNMHDR)->cf = GetCharFormatSelection();
  756. *pRes = 1;
  757. }
  758. void CWordPadView::OnSetCharFormat(NMHDR* pNMHDR, LRESULT* pRes)
  759. {
  760. ASSERT(pNMHDR != NULL);
  761. ASSERT(pRes != NULL);
  762. SetCharFormat(((CHARHDR*)pNMHDR)->cf);
  763. *pRes = 1;
  764. }
  765. void CWordPadView::OnBarSetFocus(NMHDR*, LRESULT*)
  766. {
  767. m_bOnBar = TRUE;
  768. }
  769. void CWordPadView::OnBarKillFocus(NMHDR*, LRESULT*)
  770. {
  771. m_bOnBar = FALSE;
  772. }
  773. void CWordPadView::OnBarReturn(NMHDR*, LRESULT* )
  774. {
  775. SetFocus();
  776. }