viewrich.cpp 56 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144
  1. // This is a part of the Microsoft Foundation Classes C++ library.
  2. // Copyright (C) 1992-1998 Microsoft Corporation
  3. // All rights reserved.
  4. //
  5. // This source code is only intended as a supplement to the
  6. // Microsoft Foundation Classes Reference and related
  7. // electronic documentation provided with the library.
  8. // See these sources for detailed information regarding the
  9. // Microsoft Foundation Classes product.
  10. #include "stdafx.h"
  11. #ifdef AFX_CORE4_SEG
  12. #pragma code_seg(AFX_CORE4_SEG)
  13. #endif
  14. #ifdef _DEBUG
  15. #undef THIS_FILE
  16. static char THIS_FILE[] = __FILE__;
  17. #endif
  18. #define new DEBUG_NEW
  19. /////////////////////////////////////////////////////////////////////////////
  20. // CReObject
  21. class CReObject : public _reobject
  22. {
  23. public:
  24. CReObject();
  25. CReObject(CRichEditCntrItem* pItem);
  26. ~CReObject();
  27. };
  28. CReObject::CReObject()
  29. {
  30. cbStruct = sizeof(REOBJECT);
  31. memset(&cbStruct+1, 0, sizeof(*this)-sizeof(cbStruct));
  32. }
  33. CReObject::CReObject(CRichEditCntrItem* pItem)
  34. {
  35. ASSERT(pItem != NULL);
  36. cbStruct = sizeof(REOBJECT);
  37. pItem->GetClassID(&clsid);
  38. poleobj = pItem->m_lpObject;
  39. pstg = pItem->m_lpStorage;
  40. polesite = pItem->m_lpClientSite;
  41. ASSERT(poleobj != NULL);
  42. ASSERT(pstg != NULL);
  43. ASSERT(polesite != NULL);
  44. poleobj->AddRef();
  45. pstg->AddRef();
  46. polesite->AddRef();
  47. sizel.cx = sizel.cy = 0; // let richedit determine initial size
  48. dvaspect = pItem->GetDrawAspect();
  49. dwFlags = REO_RESIZABLE;
  50. dwUser = 0;
  51. }
  52. CReObject::~CReObject()
  53. {
  54. if (poleobj != NULL)
  55. poleobj->Release();
  56. if (pstg != NULL)
  57. pstg->Release();
  58. if (polesite != NULL)
  59. polesite->Release();
  60. }
  61. /////////////////////////////////////////////////////////////////////////////
  62. // CRichEditView
  63. AFX_STATIC const UINT _afxMsgFindReplace2 = ::RegisterWindowMessage(FINDMSGSTRING);
  64. BEGIN_MESSAGE_MAP(CRichEditView, CCtrlView)
  65. //{{AFX_MSG_MAP(CRichEditView)
  66. ON_UPDATE_COMMAND_UI(ID_EDIT_CUT, OnUpdateNeedSel)
  67. ON_UPDATE_COMMAND_UI(ID_EDIT_PASTE, OnUpdateNeedClip)
  68. ON_UPDATE_COMMAND_UI(ID_EDIT_FIND, OnUpdateNeedText)
  69. ON_UPDATE_COMMAND_UI(ID_EDIT_REPEAT, OnUpdateNeedFind)
  70. ON_UPDATE_COMMAND_UI(ID_EDIT_UNDO, OnUpdateEditUndo)
  71. ON_UPDATE_COMMAND_UI(ID_EDIT_PASTE_SPECIAL, OnUpdateEditPasteSpecial)
  72. ON_UPDATE_COMMAND_UI(ID_OLE_EDIT_PROPERTIES, OnUpdateEditProperties)
  73. ON_UPDATE_COMMAND_UI(ID_EDIT_COPY, OnUpdateNeedSel)
  74. ON_UPDATE_COMMAND_UI(ID_EDIT_CLEAR, OnUpdateNeedSel)
  75. ON_UPDATE_COMMAND_UI(ID_EDIT_SELECT_ALL, OnUpdateNeedText)
  76. ON_UPDATE_COMMAND_UI(ID_EDIT_REPLACE, OnUpdateNeedText)
  77. ON_COMMAND(ID_EDIT_CUT, OnEditCut)
  78. ON_COMMAND(ID_EDIT_COPY, OnEditCopy)
  79. ON_COMMAND(ID_EDIT_PASTE, OnEditPaste)
  80. ON_COMMAND(ID_EDIT_CLEAR, OnEditClear)
  81. ON_COMMAND(ID_EDIT_UNDO, OnEditUndo)
  82. ON_COMMAND(ID_EDIT_SELECT_ALL, OnEditSelectAll)
  83. ON_COMMAND(ID_EDIT_FIND, OnEditFind)
  84. ON_COMMAND(ID_EDIT_REPLACE, OnEditReplace)
  85. ON_COMMAND(ID_EDIT_REPEAT, OnEditRepeat)
  86. ON_COMMAND(ID_EDIT_PASTE_SPECIAL, OnEditPasteSpecial)
  87. ON_COMMAND(ID_OLE_EDIT_PROPERTIES, OnEditProperties)
  88. ON_COMMAND(ID_OLE_INSERT_NEW, OnInsertObject)
  89. ON_COMMAND(ID_FORMAT_FONT, OnFormatFont)
  90. ON_WM_SIZE()
  91. ON_WM_CREATE()
  92. ON_WM_DESTROY()
  93. //}}AFX_MSG_MAP
  94. ON_NOTIFY_REFLECT(EN_SELCHANGE, OnSelChange)
  95. ON_REGISTERED_MESSAGE(_afxMsgFindReplace2, OnFindReplaceCmd)
  96. END_MESSAGE_MAP()
  97. // richedit buffer limit -- let's set it at 16M
  98. AFX_DATADEF ULONG CRichEditView::lMaxSize = 0xffffff;
  99. /////////////////////////////////////////////////////////////////////////////
  100. // CRichEditView construction/destruction
  101. CRichEditView::CRichEditView() : CCtrlView(_T("RICHEDIT"), AFX_WS_DEFAULT_VIEW |
  102. WS_HSCROLL | WS_VSCROLL | ES_AUTOHSCROLL | ES_AUTOVSCROLL |
  103. ES_MULTILINE | ES_NOHIDESEL | ES_SAVESEL | ES_SELECTIONBAR)
  104. {
  105. m_bSyncCharFormat = m_bSyncParaFormat = TRUE;
  106. m_lpRichEditOle = NULL;
  107. m_nBulletIndent = 720; // 1/2 inch
  108. m_nWordWrap = WrapToWindow;
  109. m_nPasteType = 0;
  110. SetPaperSize(CSize(8*1440+720, 11*1440));
  111. SetMargins(CRect(0,0,0,0));
  112. m_charformat.cbSize = sizeof(CHARFORMAT);
  113. m_paraformat.cbSize = sizeof(PARAFORMAT);
  114. }
  115. BOOL CRichEditView::PreCreateWindow(CREATESTRUCT& cs)
  116. {
  117. if (!AfxInitRichEdit())
  118. return FALSE;
  119. CCtrlView::PreCreateWindow(cs);
  120. cs.lpszName = _T("");
  121. cs.cx = cs.cy = 100; // necessary to avoid bug with ES_SELECTIONBAR and zero for cx and cy
  122. cs.style |= WS_CLIPSIBLINGS;
  123. return TRUE;
  124. }
  125. int CRichEditView::OnCreate(LPCREATESTRUCT lpcs)
  126. {
  127. if (CCtrlView::OnCreate(lpcs) != 0)
  128. return -1;
  129. GetRichEditCtrl().LimitText(lMaxSize);
  130. GetRichEditCtrl().SetEventMask(ENM_SELCHANGE | ENM_CHANGE | ENM_SCROLL);
  131. VERIFY(GetRichEditCtrl().SetOLECallback(&m_xRichEditOleCallback));
  132. m_lpRichEditOle = GetRichEditCtrl().GetIRichEditOle();
  133. DragAcceptFiles();
  134. GetRichEditCtrl().SetOptions(ECOOP_OR, ECO_AUTOWORDSELECTION);
  135. WrapChanged();
  136. ASSERT(m_lpRichEditOle != NULL);
  137. return 0;
  138. }
  139. void CRichEditView::OnInitialUpdate()
  140. {
  141. CCtrlView::OnInitialUpdate();
  142. m_bSyncCharFormat = m_bSyncParaFormat = TRUE;
  143. }
  144. /////////////////////////////////////////////////////////////////////////////
  145. // CRichEditView document like functions
  146. void CRichEditView::DeleteContents()
  147. {
  148. ASSERT_VALID(this);
  149. ASSERT(m_hWnd != NULL);
  150. SetWindowText(_T(""));
  151. GetRichEditCtrl().EmptyUndoBuffer();
  152. m_bSyncCharFormat = m_bSyncParaFormat = TRUE;
  153. ASSERT_VALID(this);
  154. }
  155. void CRichEditView::WrapChanged()
  156. {
  157. CWaitCursor wait;
  158. CRichEditCtrl& ctrl = GetRichEditCtrl();
  159. if (m_nWordWrap == WrapNone)
  160. ctrl.SetTargetDevice(NULL, 1);
  161. else if (m_nWordWrap == WrapToWindow)
  162. ctrl.SetTargetDevice(NULL, 0);
  163. else if (m_nWordWrap == WrapToTargetDevice) // wrap to ruler
  164. {
  165. AfxGetApp()->CreatePrinterDC(m_dcTarget);
  166. if (m_dcTarget.m_hDC == NULL)
  167. m_dcTarget.CreateDC(_T("DISPLAY"), NULL, NULL, NULL);
  168. ctrl.SetTargetDevice(m_dcTarget, GetPrintWidth());
  169. }
  170. }
  171. /////////////////////////////////////////////////////////////////////////////
  172. // CRichEditView serialization support
  173. class _afxRichEditCookie
  174. {
  175. public:
  176. CArchive& m_ar;
  177. DWORD m_dwError;
  178. _afxRichEditCookie(CArchive& ar) : m_ar(ar) {m_dwError=0;}
  179. };
  180. void CRichEditView::Serialize(CArchive& ar)
  181. // Read and write CRichEditView object to archive, with length prefix.
  182. {
  183. ASSERT_VALID(this);
  184. ASSERT(m_hWnd != NULL);
  185. Stream(ar, FALSE);
  186. ASSERT_VALID(this);
  187. }
  188. void CRichEditView::Stream(CArchive& ar, BOOL bSelection)
  189. {
  190. EDITSTREAM es = {0, 0, EditStreamCallBack};
  191. _afxRichEditCookie cookie(ar);
  192. es.dwCookie = (DWORD)&cookie;
  193. int nFormat = GetDocument()->GetStreamFormat();
  194. if (bSelection)
  195. nFormat |= SFF_SELECTION;
  196. if (ar.IsStoring())
  197. GetRichEditCtrl().StreamOut(nFormat, es);
  198. else
  199. {
  200. GetRichEditCtrl().StreamIn(nFormat, es);
  201. Invalidate();
  202. }
  203. if (cookie.m_dwError != 0)
  204. AfxThrowFileException(cookie.m_dwError);
  205. }
  206. // return 0 for no error, otherwise return error code
  207. DWORD CALLBACK CRichEditView::EditStreamCallBack(DWORD dwCookie, LPBYTE pbBuff, LONG cb, LONG *pcb)
  208. {
  209. _afxRichEditCookie* pCookie = (_afxRichEditCookie*)dwCookie;
  210. CArchive& ar = pCookie->m_ar;
  211. ar.Flush();
  212. DWORD dw = 0;
  213. *pcb = cb;
  214. TRY
  215. {
  216. if (ar.IsStoring())
  217. ar.GetFile()->WriteHuge(pbBuff, cb);
  218. else
  219. *pcb = ar.GetFile()->ReadHuge(pbBuff, cb);
  220. }
  221. CATCH(CFileException, e)
  222. {
  223. *pcb = 0;
  224. pCookie->m_dwError = (DWORD)e->m_cause;
  225. dw = 1;
  226. DELETE_EXCEPTION(e);
  227. }
  228. AND_CATCH_ALL(e)
  229. {
  230. *pcb = 0;
  231. pCookie->m_dwError = (DWORD)CFileException::generic;
  232. dw = 1;
  233. DELETE_EXCEPTION(e);
  234. }
  235. END_CATCH_ALL
  236. return dw;
  237. }
  238. /////////////////////////////////////////////////////////////////////////////
  239. // CRichEditView Printing support
  240. void CRichEditView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo*)
  241. {
  242. ASSERT_VALID(this);
  243. // ASSERT_VALID(pDC);
  244. // initialize page start vector
  245. ASSERT(m_aPageStart.GetSize() == 0);
  246. m_aPageStart.Add(0);
  247. ASSERT(m_aPageStart.GetSize() > 0);
  248. GetRichEditCtrl().FormatRange(NULL, FALSE); // required by RichEdit to clear out cache
  249. ASSERT_VALID(this);
  250. }
  251. BOOL CRichEditView::PaginateTo(CDC* pDC, CPrintInfo* pInfo)
  252. // attempts pagination to pInfo->m_nCurPage, TRUE == success
  253. {
  254. ASSERT_VALID(this);
  255. ASSERT_VALID(pDC);
  256. CRect rectSave = pInfo->m_rectDraw;
  257. UINT nPageSave = pInfo->m_nCurPage;
  258. ASSERT(nPageSave > 1);
  259. ASSERT(nPageSave >= (UINT)m_aPageStart.GetSize());
  260. VERIFY(pDC->SaveDC() != 0);
  261. pDC->IntersectClipRect(0, 0, 0, 0);
  262. pInfo->m_nCurPage = m_aPageStart.GetSize();
  263. while (pInfo->m_nCurPage < nPageSave)
  264. {
  265. ASSERT(pInfo->m_nCurPage == (UINT)m_aPageStart.GetSize());
  266. OnPrepareDC(pDC, pInfo);
  267. ASSERT(pInfo->m_bContinuePrinting);
  268. pInfo->m_rectDraw.SetRect(0, 0,
  269. pDC->GetDeviceCaps(HORZRES), pDC->GetDeviceCaps(VERTRES));
  270. pDC->DPtoLP(&pInfo->m_rectDraw);
  271. OnPrint(pDC, pInfo);
  272. if (pInfo->m_nCurPage == (UINT)m_aPageStart.GetSize())
  273. break;
  274. ++pInfo->m_nCurPage;
  275. }
  276. BOOL bResult = pInfo->m_nCurPage == nPageSave;
  277. pDC->RestoreDC(-1);
  278. pInfo->m_nCurPage = nPageSave;
  279. pInfo->m_rectDraw = rectSave;
  280. ASSERT_VALID(this);
  281. return bResult;
  282. }
  283. void CRichEditView::OnPrepareDC(CDC* pDC, CPrintInfo* pInfo)
  284. {
  285. ASSERT_VALID(this);
  286. ASSERT_VALID(pDC);
  287. ASSERT(pInfo != NULL); // overriding OnPaint -- never get this.
  288. pDC->SetMapMode(MM_TEXT);
  289. if (pInfo->m_nCurPage > (UINT)m_aPageStart.GetSize() &&
  290. !PaginateTo(pDC, pInfo))
  291. {
  292. // can't paginate to that page, thus cannot print it.
  293. pInfo->m_bContinuePrinting = FALSE;
  294. }
  295. ASSERT_VALID(this);
  296. }
  297. long CRichEditView::PrintPage(CDC* pDC, long nIndexStart, long nIndexStop)
  298. // worker function for laying out text in a rectangle.
  299. {
  300. ASSERT_VALID(this);
  301. ASSERT_VALID(pDC);
  302. FORMATRANGE fr;
  303. // offset by printing offset
  304. pDC->SetViewportOrg(-pDC->GetDeviceCaps(PHYSICALOFFSETX),
  305. -pDC->GetDeviceCaps(PHYSICALOFFSETY));
  306. // adjust DC because richedit doesn't do things like MFC
  307. if (::GetDeviceCaps(pDC->m_hDC, TECHNOLOGY) != DT_METAFILE && pDC->m_hAttribDC != NULL)
  308. {
  309. ::ScaleWindowExtEx(pDC->m_hDC,
  310. ::GetDeviceCaps(pDC->m_hDC, LOGPIXELSX),
  311. ::GetDeviceCaps(pDC->m_hAttribDC, LOGPIXELSX),
  312. ::GetDeviceCaps(pDC->m_hDC, LOGPIXELSY),
  313. ::GetDeviceCaps(pDC->m_hAttribDC, LOGPIXELSY), NULL);
  314. }
  315. fr.hdcTarget = pDC->m_hAttribDC;
  316. fr.hdc = pDC->m_hDC;
  317. fr.rcPage = GetPageRect();
  318. fr.rc = GetPrintRect();
  319. fr.chrg.cpMin = nIndexStart;
  320. fr.chrg.cpMax = nIndexStop;
  321. long lRes = GetRichEditCtrl().FormatRange(&fr,TRUE);
  322. return lRes;
  323. }
  324. long CRichEditView::PrintInsideRect(CDC* pDC, RECT& rectLayout,
  325. long nIndexStart, long nIndexStop, BOOL bOutput)
  326. {
  327. ASSERT_VALID(this);
  328. ASSERT_VALID(pDC);
  329. FORMATRANGE fr;
  330. // adjust DC because richedit doesn't do things like MFC
  331. if (::GetDeviceCaps(pDC->m_hDC, TECHNOLOGY) != DT_METAFILE && pDC->m_hAttribDC != NULL)
  332. {
  333. ::ScaleWindowExtEx(pDC->m_hDC,
  334. ::GetDeviceCaps(pDC->m_hDC, LOGPIXELSX),
  335. ::GetDeviceCaps(pDC->m_hAttribDC, LOGPIXELSX),
  336. ::GetDeviceCaps(pDC->m_hDC, LOGPIXELSY),
  337. ::GetDeviceCaps(pDC->m_hAttribDC, LOGPIXELSY), NULL);
  338. }
  339. fr.hdcTarget = pDC->m_hAttribDC;
  340. fr.hdc = pDC->m_hDC;
  341. // convert rect to twips
  342. fr.rcPage = rectLayout;
  343. fr.rc = rectLayout;
  344. fr.chrg.cpMin = nIndexStart;
  345. fr.chrg.cpMax = nIndexStop;
  346. GetRichEditCtrl().FormatRange(NULL, FALSE); // required by RichEdit to clear out cache
  347. // if bOutput is FALSE, we only measure
  348. long lres = GetRichEditCtrl().FormatRange(&fr, bOutput);
  349. GetRichEditCtrl().FormatRange(NULL, FALSE); // required by RichEdit to clear out cache
  350. rectLayout = fr.rc;
  351. return lres;
  352. }
  353. void CRichEditView::OnPrint(CDC* pDC, CPrintInfo* pInfo)
  354. {
  355. ASSERT_VALID(this);
  356. ASSERT_VALID(pDC);
  357. ASSERT(pInfo != NULL);
  358. ASSERT(pInfo->m_bContinuePrinting);
  359. UINT nPage = pInfo->m_nCurPage;
  360. ASSERT(nPage <= (UINT)m_aPageStart.GetSize());
  361. long nIndex = (long) m_aPageStart[nPage-1];
  362. // print as much as possible in the current page.
  363. nIndex = PrintPage(pDC, nIndex, 0xFFFFFFFF);
  364. if (nIndex >= GetTextLength())
  365. {
  366. TRACE0("End of Document\n");
  367. pInfo->SetMaxPage(nPage);
  368. }
  369. // update pagination information for page just printed
  370. if (nPage == (UINT)m_aPageStart.GetSize())
  371. {
  372. if (nIndex < GetTextLength())
  373. m_aPageStart.Add(nIndex);
  374. }
  375. else
  376. {
  377. ASSERT(nPage+1 <= (UINT)m_aPageStart.GetSize());
  378. ASSERT(nIndex == (long)m_aPageStart[nPage+1-1]);
  379. }
  380. }
  381. void CRichEditView::OnEndPrinting(CDC*, CPrintInfo*)
  382. {
  383. ASSERT_VALID(this);
  384. GetRichEditCtrl().FormatRange(NULL, FALSE); // required by RichEdit to clear out cache
  385. m_aPageStart.RemoveAll();
  386. }
  387. /////////////////////////////////////////////////////////////////////////////
  388. // CRichEditView::XRichEditOleCallback
  389. BEGIN_INTERFACE_MAP(CRichEditView, CCtrlView)
  390. // we use IID_IUnknown because richedit doesn't define an IID
  391. INTERFACE_PART(CRichEditView, IID_IUnknown, RichEditOleCallback)
  392. END_INTERFACE_MAP()
  393. STDMETHODIMP_(ULONG) CRichEditView::XRichEditOleCallback::AddRef()
  394. {
  395. METHOD_PROLOGUE_EX_(CRichEditView, RichEditOleCallback)
  396. return (ULONG)pThis->InternalAddRef();
  397. }
  398. STDMETHODIMP_(ULONG) CRichEditView::XRichEditOleCallback::Release()
  399. {
  400. METHOD_PROLOGUE_EX_(CRichEditView, RichEditOleCallback)
  401. return (ULONG)pThis->InternalRelease();
  402. }
  403. STDMETHODIMP CRichEditView::XRichEditOleCallback::QueryInterface(
  404. REFIID iid, LPVOID* ppvObj)
  405. {
  406. METHOD_PROLOGUE_EX_(CRichEditView, RichEditOleCallback)
  407. return (HRESULT)pThis->InternalQueryInterface(&iid, ppvObj);
  408. }
  409. STDMETHODIMP CRichEditView::XRichEditOleCallback::GetNewStorage(LPSTORAGE* ppstg)
  410. {
  411. METHOD_PROLOGUE_EX_(CRichEditView, RichEditOleCallback)
  412. // Create a flat storage and steal it from the client item
  413. // the client item is only used for creating the storage
  414. COleClientItem item;
  415. item.GetItemStorageFlat();
  416. *ppstg = item.m_lpStorage;
  417. HRESULT hRes = E_OUTOFMEMORY;
  418. if (item.m_lpStorage != NULL)
  419. {
  420. item.m_lpStorage = NULL;
  421. hRes = S_OK;
  422. }
  423. pThis->GetDocument()->InvalidateObjectCache();
  424. return hRes;
  425. }
  426. STDMETHODIMP CRichEditView::XRichEditOleCallback::GetInPlaceContext(
  427. LPOLEINPLACEFRAME* lplpFrame, LPOLEINPLACEUIWINDOW* lplpDoc,
  428. LPOLEINPLACEFRAMEINFO lpFrameInfo)
  429. {
  430. METHOD_PROLOGUE_EX(CRichEditView, RichEditOleCallback)
  431. return pThis->GetWindowContext(lplpFrame, lplpDoc, lpFrameInfo);
  432. }
  433. STDMETHODIMP CRichEditView::XRichEditOleCallback::ShowContainerUI(BOOL fShow)
  434. {
  435. METHOD_PROLOGUE_EX(CRichEditView, RichEditOleCallback)
  436. return pThis->ShowContainerUI(fShow);
  437. }
  438. STDMETHODIMP CRichEditView::XRichEditOleCallback::QueryInsertObject(
  439. LPCLSID /*lpclsid*/, LPSTORAGE /*pstg*/, LONG /*cp*/)
  440. {
  441. METHOD_PROLOGUE_EX(CRichEditView, RichEditOleCallback)
  442. pThis->GetDocument()->InvalidateObjectCache();
  443. return S_OK;
  444. }
  445. STDMETHODIMP CRichEditView::XRichEditOleCallback::DeleteObject(LPOLEOBJECT /*lpoleobj*/)
  446. {
  447. METHOD_PROLOGUE_EX_(CRichEditView, RichEditOleCallback)
  448. pThis->GetDocument()->InvalidateObjectCache();
  449. return S_OK;
  450. }
  451. STDMETHODIMP CRichEditView::XRichEditOleCallback::QueryAcceptData(
  452. LPDATAOBJECT lpdataobj, CLIPFORMAT* lpcfFormat, DWORD reco,
  453. BOOL fReally, HGLOBAL hMetaPict)
  454. {
  455. METHOD_PROLOGUE_EX(CRichEditView, RichEditOleCallback)
  456. return pThis->QueryAcceptData(lpdataobj, lpcfFormat, reco,
  457. fReally, hMetaPict);
  458. }
  459. STDMETHODIMP CRichEditView::XRichEditOleCallback::ContextSensitiveHelp(BOOL /*fEnterMode*/)
  460. {
  461. return E_NOTIMPL;
  462. }
  463. STDMETHODIMP CRichEditView::XRichEditOleCallback::GetClipboardData(
  464. CHARRANGE* lpchrg, DWORD reco, LPDATAOBJECT* lplpdataobj)
  465. {
  466. METHOD_PROLOGUE_EX(CRichEditView, RichEditOleCallback)
  467. LPDATAOBJECT lpOrigDataObject = NULL;
  468. // get richedit's data object
  469. if (FAILED(pThis->m_lpRichEditOle->GetClipboardData(lpchrg, reco,
  470. &lpOrigDataObject)))
  471. {
  472. return E_NOTIMPL;
  473. }
  474. // allow changes
  475. HRESULT hRes = pThis->GetClipboardData(lpchrg, reco, lpOrigDataObject,
  476. lplpdataobj);
  477. // if changed then free original object
  478. if (SUCCEEDED(hRes))
  479. {
  480. if (lpOrigDataObject!=NULL)
  481. lpOrigDataObject->Release();
  482. return hRes;
  483. }
  484. else
  485. {
  486. // use richedit's data object
  487. *lplpdataobj = lpOrigDataObject;
  488. return S_OK;
  489. }
  490. }
  491. STDMETHODIMP CRichEditView::XRichEditOleCallback::GetDragDropEffect(
  492. BOOL fDrag, DWORD grfKeyState, LPDWORD pdwEffect)
  493. {
  494. if (!fDrag) // allowable dest effects
  495. {
  496. DWORD dwEffect;
  497. // check for force link
  498. if ((grfKeyState & (MK_CONTROL|MK_SHIFT)) == (MK_CONTROL|MK_SHIFT))
  499. dwEffect = DROPEFFECT_LINK;
  500. // check for force copy
  501. else if ((grfKeyState & MK_CONTROL) == MK_CONTROL)
  502. dwEffect = DROPEFFECT_COPY;
  503. // check for force move
  504. else if ((grfKeyState & MK_ALT) == MK_ALT)
  505. dwEffect = DROPEFFECT_MOVE;
  506. // default -- recommended action is move
  507. else
  508. dwEffect = DROPEFFECT_MOVE;
  509. if (dwEffect & *pdwEffect) // make sure allowed type
  510. *pdwEffect = dwEffect;
  511. }
  512. return S_OK;
  513. }
  514. STDMETHODIMP CRichEditView::XRichEditOleCallback::GetContextMenu(
  515. WORD seltype, LPOLEOBJECT lpoleobj, CHARRANGE* lpchrg,
  516. HMENU* lphmenu)
  517. {
  518. METHOD_PROLOGUE_EX(CRichEditView, RichEditOleCallback)
  519. HMENU hMenu = pThis->GetContextMenu(seltype, lpoleobj, lpchrg);
  520. if (hMenu == NULL)
  521. return E_NOTIMPL;
  522. *lphmenu = hMenu;
  523. return S_OK;
  524. }
  525. /////////////////////////////////////////////////////////////////////////////
  526. // CRichEditView command helpers
  527. void CRichEditView::OnCharEffect(DWORD dwMask, DWORD dwEffect)
  528. {
  529. GetCharFormatSelection();
  530. if (m_charformat.dwMask & dwMask) // selection is all the same
  531. m_charformat.dwEffects ^= dwEffect;
  532. else
  533. m_charformat.dwEffects |= dwEffect;
  534. m_charformat.dwMask = dwMask;
  535. SetCharFormat(m_charformat);
  536. }
  537. void CRichEditView::OnUpdateCharEffect(CCmdUI* pCmdUI, DWORD dwMask, DWORD dwEffect)
  538. {
  539. GetCharFormatSelection();
  540. pCmdUI->SetCheck((m_charformat.dwMask & dwMask) ?
  541. ((m_charformat.dwEffects & dwEffect) ? 1 : 0) : 2);
  542. }
  543. void CRichEditView::OnParaAlign(WORD wAlign)
  544. {
  545. GetParaFormatSelection();
  546. m_paraformat.dwMask = PFM_ALIGNMENT;
  547. m_paraformat.wAlignment = wAlign;
  548. SetParaFormat(m_paraformat);
  549. }
  550. void CRichEditView::OnUpdateParaAlign(CCmdUI* pCmdUI, WORD wAlign)
  551. {
  552. GetParaFormatSelection();
  553. // disable if no word wrap since alignment is meaningless
  554. pCmdUI->Enable( (m_nWordWrap == WrapNone) ?
  555. FALSE : TRUE);
  556. pCmdUI->SetCheck( (m_paraformat.dwMask & PFM_ALIGNMENT) ?
  557. ((m_paraformat.wAlignment == wAlign) ? 1 : 0) : 2);
  558. }
  559. /////////////////////////////////////////////////////////////////////////////
  560. // CRichEditView commands
  561. void CRichEditView::OnUpdateNeedSel(CCmdUI* pCmdUI)
  562. {
  563. ASSERT_VALID(this);
  564. long nStartChar, nEndChar;
  565. GetRichEditCtrl().GetSel(nStartChar, nEndChar);
  566. pCmdUI->Enable(nStartChar != nEndChar);
  567. ASSERT_VALID(this);
  568. }
  569. void CRichEditView::OnUpdateNeedClip(CCmdUI* pCmdUI)
  570. {
  571. ASSERT_VALID(this);
  572. pCmdUI->Enable(CanPaste());
  573. }
  574. void CRichEditView::OnUpdateNeedText(CCmdUI* pCmdUI)
  575. {
  576. ASSERT_VALID(this);
  577. pCmdUI->Enable(GetTextLength() != 0);
  578. }
  579. void CRichEditView::OnUpdateNeedFind(CCmdUI* pCmdUI)
  580. {
  581. ASSERT_VALID(this);
  582. _AFX_RICHEDIT_STATE* pEditState = _afxRichEditState;
  583. pCmdUI->Enable(GetTextLength() != 0 &&
  584. !pEditState->strFind.IsEmpty());
  585. }
  586. void CRichEditView::OnUpdateEditUndo(CCmdUI* pCmdUI)
  587. {
  588. ASSERT_VALID(this);
  589. pCmdUI->Enable(GetRichEditCtrl().CanUndo());
  590. }
  591. void CRichEditView::OnEditCut()
  592. {
  593. ASSERT_VALID(this);
  594. GetRichEditCtrl().Cut();
  595. }
  596. void CRichEditView::OnEditCopy()
  597. {
  598. ASSERT_VALID(this);
  599. GetRichEditCtrl().Copy();
  600. }
  601. void CRichEditView::OnEditPaste()
  602. {
  603. ASSERT_VALID(this);
  604. m_nPasteType = 0;
  605. GetRichEditCtrl().Paste();
  606. }
  607. void CRichEditView::OnEditClear()
  608. {
  609. ASSERT_VALID(this);
  610. GetRichEditCtrl().Clear();
  611. }
  612. void CRichEditView::OnEditUndo()
  613. {
  614. ASSERT_VALID(this);
  615. GetRichEditCtrl().Undo();
  616. m_bSyncCharFormat = m_bSyncParaFormat = TRUE;
  617. }
  618. void CRichEditView::OnEditSelectAll()
  619. {
  620. ASSERT_VALID(this);
  621. GetRichEditCtrl().SetSel(0, -1);
  622. }
  623. void CRichEditView::OnEditFind()
  624. {
  625. ASSERT_VALID(this);
  626. OnEditFindReplace(TRUE);
  627. }
  628. void CRichEditView::OnEditReplace()
  629. {
  630. ASSERT_VALID(this);
  631. OnEditFindReplace(FALSE);
  632. }
  633. void CRichEditView::OnEditRepeat()
  634. {
  635. ASSERT_VALID(this);
  636. _AFX_RICHEDIT_STATE* pEditState = _afxRichEditState;
  637. if (!FindText(pEditState))
  638. TextNotFound(pEditState->strFind);
  639. }
  640. void CRichEditView::OnCancelEditCntr()
  641. {
  642. m_lpRichEditOle->InPlaceDeactivate();
  643. }
  644. void CRichEditView::OnInsertObject()
  645. {
  646. // Invoke the standard Insert Object dialog box to obtain information
  647. COleInsertDialog dlg;
  648. if (dlg.DoModal() != IDOK)
  649. return;
  650. CWaitCursor wait;
  651. CRichEditCntrItem* pItem = NULL;
  652. TRY
  653. {
  654. // create item from dialog results
  655. pItem = GetDocument()->CreateClientItem();
  656. pItem->m_bLock = TRUE;
  657. if (!dlg.CreateItem(pItem))
  658. {
  659. pItem->m_bLock = FALSE;
  660. AfxThrowMemoryException(); // any exception will do
  661. }
  662. HRESULT hr = InsertItem(pItem);
  663. pItem->UpdateItemType();
  664. pItem->m_bLock = FALSE;
  665. if (hr != NOERROR)
  666. AfxThrowOleException(hr);
  667. // if insert new object -- initially show the object
  668. if (dlg.GetSelectionType() == COleInsertDialog::createNewItem)
  669. pItem->DoVerb(OLEIVERB_SHOW, this);
  670. }
  671. CATCH(CException, e)
  672. {
  673. if (pItem != NULL)
  674. {
  675. ASSERT_VALID(pItem);
  676. pItem->Delete();
  677. }
  678. AfxMessageBox(AFX_IDP_FAILED_TO_CREATE);
  679. }
  680. END_CATCH
  681. }
  682. void CRichEditView::OnSelChange(NMHDR* pNMHDR, LRESULT* pResult)
  683. {
  684. ASSERT(pNMHDR->code == EN_SELCHANGE);
  685. UNUSED(pNMHDR); // not used in release builds
  686. m_bSyncCharFormat = m_bSyncParaFormat = TRUE;
  687. *pResult = 0;
  688. }
  689. void CRichEditView::OnDestroy()
  690. {
  691. if (m_lpRichEditOle != NULL)
  692. m_lpRichEditOle->Release();
  693. CCtrlView::OnDestroy();
  694. }
  695. void CRichEditView::OnEditProperties()
  696. {
  697. ASSERT(m_lpRichEditOle != NULL);
  698. CRichEditCntrItem* pSelection = GetSelectedItem();
  699. // make sure item is in sync with richedit's item
  700. CReObject reo;
  701. m_lpRichEditOle->GetObject(REO_IOB_SELECTION, &reo,
  702. REO_GETOBJ_NO_INTERFACES);
  703. pSelection->SyncToRichEditObject(reo);
  704. COlePropertiesDialog dlg(pSelection);
  705. dlg.DoModal();
  706. }
  707. void CRichEditView::OnUpdateEditProperties(CCmdUI* pCmdUI)
  708. {
  709. pCmdUI->Enable(GetSelectedItem() != NULL);
  710. }
  711. void CRichEditView::OnCharBold()
  712. {
  713. OnCharEffect(CFM_BOLD, CFE_BOLD);
  714. }
  715. void CRichEditView::OnUpdateCharBold(CCmdUI* pCmdUI)
  716. {
  717. OnUpdateCharEffect(pCmdUI, CFM_BOLD, CFE_BOLD);
  718. }
  719. void CRichEditView::OnCharItalic()
  720. {
  721. OnCharEffect(CFM_ITALIC, CFE_ITALIC);
  722. }
  723. void CRichEditView::OnUpdateCharItalic(CCmdUI* pCmdUI)
  724. {
  725. OnUpdateCharEffect(pCmdUI, CFM_ITALIC, CFE_ITALIC);
  726. }
  727. void CRichEditView::OnCharUnderline()
  728. {
  729. OnCharEffect(CFM_UNDERLINE, CFE_UNDERLINE);
  730. }
  731. void CRichEditView::OnUpdateCharUnderline(CCmdUI* pCmdUI)
  732. {
  733. OnUpdateCharEffect(pCmdUI, CFM_UNDERLINE, CFE_UNDERLINE);
  734. }
  735. void CRichEditView::OnParaCenter()
  736. {
  737. OnParaAlign(PFA_CENTER);
  738. }
  739. void CRichEditView::OnUpdateParaCenter(CCmdUI* pCmdUI)
  740. {
  741. OnUpdateParaAlign(pCmdUI, PFA_CENTER);
  742. }
  743. void CRichEditView::OnParaLeft()
  744. {
  745. OnParaAlign(PFA_LEFT);
  746. }
  747. void CRichEditView::OnUpdateParaLeft(CCmdUI* pCmdUI)
  748. {
  749. OnUpdateParaAlign(pCmdUI, PFA_LEFT);
  750. }
  751. void CRichEditView::OnParaRight()
  752. {
  753. OnParaAlign(PFA_RIGHT);
  754. }
  755. void CRichEditView::OnUpdateParaRight(CCmdUI* pCmdUI)
  756. {
  757. OnUpdateParaAlign(pCmdUI, PFA_RIGHT);
  758. }
  759. void CRichEditView::OnBullet()
  760. {
  761. GetParaFormatSelection();
  762. if (m_paraformat.dwMask & PFM_NUMBERING && m_paraformat.wNumbering == PFN_BULLET)
  763. {
  764. m_paraformat.wNumbering = 0;
  765. m_paraformat.dxOffset = 0;
  766. m_paraformat.dxStartIndent = 0;
  767. m_paraformat.dwMask = PFM_NUMBERING | PFM_STARTINDENT | PFM_OFFSET;
  768. }
  769. else
  770. {
  771. m_paraformat.wNumbering = PFN_BULLET;
  772. m_paraformat.dwMask = PFM_NUMBERING;
  773. if (m_paraformat.dxOffset == 0)
  774. {
  775. m_paraformat.dxOffset = m_nBulletIndent;
  776. m_paraformat.dwMask = PFM_NUMBERING | PFM_STARTINDENT | PFM_OFFSET;
  777. }
  778. }
  779. SetParaFormat(m_paraformat);
  780. }
  781. void CRichEditView::OnUpdateBullet(CCmdUI* pCmdUI)
  782. {
  783. GetParaFormatSelection();
  784. pCmdUI->SetCheck( (m_paraformat.dwMask & PFM_NUMBERING) ? ((m_paraformat.wNumbering & PFN_BULLET) ? 1 : 0) : 2);
  785. }
  786. void CRichEditView::OnFormatFont()
  787. {
  788. GetCharFormatSelection();
  789. CFontDialog dlg(m_charformat, CF_BOTH|CF_NOOEMFONTS);
  790. if (dlg.DoModal() == IDOK)
  791. {
  792. dlg.GetCharFormat(m_charformat);
  793. SetCharFormat(m_charformat);
  794. }
  795. }
  796. void CRichEditView::OnColorPick(COLORREF cr)
  797. {
  798. GetCharFormatSelection();
  799. m_charformat.dwMask = CFM_COLOR;
  800. m_charformat.dwEffects = NULL;
  801. m_charformat.crTextColor = cr;
  802. SetCharFormat(m_charformat);
  803. }
  804. void CRichEditView::OnColorDefault()
  805. {
  806. GetCharFormatSelection();
  807. m_charformat.dwMask = CFM_COLOR;
  808. m_charformat.dwEffects = CFE_AUTOCOLOR;
  809. SetCharFormat(m_charformat);
  810. }
  811. void CRichEditView::OnEditPasteSpecial()
  812. {
  813. COlePasteSpecialDialog dlg;
  814. dlg.AddStandardFormats();
  815. dlg.AddFormat(_oleData.cfRichTextFormat, TYMED_HGLOBAL, AFX_IDS_RTF_FORMAT, FALSE, FALSE);
  816. dlg.AddFormat(CF_TEXT, TYMED_HGLOBAL, AFX_IDS_TEXT_FORMAT, FALSE, FALSE);
  817. if (dlg.DoModal() != IDOK)
  818. return;
  819. DVASPECT dv = dlg.GetDrawAspect();
  820. HMETAFILE hMF = (HMETAFILE)dlg.GetIconicMetafile();
  821. CLIPFORMAT cf =
  822. dlg.m_ps.arrPasteEntries[dlg.m_ps.nSelectedIndex].fmtetc.cfFormat;
  823. CWaitCursor wait;
  824. SetCapture();
  825. // we set the target type so that QueryAcceptData know what to paste
  826. m_nPasteType = dlg.GetSelectionType();
  827. GetRichEditCtrl().PasteSpecial(cf, dv, hMF);
  828. m_nPasteType = 0;
  829. ReleaseCapture();
  830. }
  831. void CRichEditView::OnUpdateEditPasteSpecial(CCmdUI* pCmdUI)
  832. {
  833. pCmdUI->Enable(CanPaste());
  834. }
  835. void CRichEditView::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags)
  836. {
  837. if (nChar == VK_F10 && GetKeyState(VK_SHIFT) < 0)
  838. {
  839. CRect rect;
  840. GetClientRect(rect);
  841. CPoint pt = rect.CenterPoint();
  842. SendMessage(WM_CONTEXTMENU, (WPARAM)m_hWnd, MAKELPARAM(pt.x, pt.y));
  843. }
  844. else
  845. CCtrlView::OnKeyDown(nChar, nRepCnt, nFlags);
  846. }
  847. void CRichEditView::OnDropFiles(HDROP hDropInfo)
  848. {
  849. TCHAR szFileName[_MAX_PATH];
  850. UINT nFileCount = ::DragQueryFile(hDropInfo, 0xFFFFFFFF, NULL, 0);
  851. ASSERT(nFileCount != 0);
  852. CHARRANGE cr;
  853. GetRichEditCtrl().GetSel(cr);
  854. int nMin = cr.cpMin;
  855. for (UINT i=0;i<nFileCount;i++)
  856. {
  857. ::DragQueryFile(hDropInfo, i, szFileName, _MAX_PATH);
  858. InsertFileAsObject(szFileName);
  859. GetRichEditCtrl().GetSel(cr);
  860. cr.cpMin = cr.cpMax;
  861. GetRichEditCtrl().SetSel(cr);
  862. UpdateWindow();
  863. }
  864. cr.cpMin = nMin;
  865. GetRichEditCtrl().SetSel(cr);
  866. ::DragFinish(hDropInfo);
  867. }
  868. void CRichEditView::OnDevModeChange(LPTSTR /*lpDeviceName*/)
  869. {
  870. // WM_DEVMODECHANGE forwarded by the main window of the app
  871. CDC dc;
  872. AfxGetApp()->CreatePrinterDC(dc);
  873. OnPrinterChanged(dc);
  874. }
  875. /////////////////////////////////////////////////////////////////////////////
  876. // CRichEditView attributes
  877. BOOL AFX_CDECL CRichEditView::IsRichEditFormat(CLIPFORMAT cf)
  878. {
  879. return ((cf == _oleData.cfRichTextFormat) ||
  880. (cf == _oleData.cfRichTextAndObjects) || (cf == CF_TEXT));
  881. }
  882. BOOL CRichEditView::CanPaste() const
  883. {
  884. return (CountClipboardFormats() != 0) &&
  885. (IsClipboardFormatAvailable(CF_TEXT) ||
  886. IsClipboardFormatAvailable(_oleData.cfRichTextFormat) ||
  887. IsClipboardFormatAvailable(_oleData.cfEmbedSource) ||
  888. IsClipboardFormatAvailable(_oleData.cfEmbeddedObject) ||
  889. IsClipboardFormatAvailable(_oleData.cfFileName) ||
  890. IsClipboardFormatAvailable(_oleData.cfFileNameW) ||
  891. IsClipboardFormatAvailable(CF_METAFILEPICT) ||
  892. IsClipboardFormatAvailable(CF_DIB) ||
  893. IsClipboardFormatAvailable(CF_BITMAP) ||
  894. GetRichEditCtrl().CanPaste());
  895. }
  896. CHARFORMAT& CRichEditView::GetCharFormatSelection()
  897. {
  898. if (m_bSyncCharFormat)
  899. {
  900. GetRichEditCtrl().GetSelectionCharFormat(m_charformat);
  901. m_bSyncCharFormat = FALSE;
  902. }
  903. return m_charformat;
  904. }
  905. PARAFORMAT& CRichEditView::GetParaFormatSelection()
  906. {
  907. if (m_bSyncParaFormat)
  908. {
  909. GetRichEditCtrl().GetParaFormat(m_paraformat);
  910. m_bSyncParaFormat = FALSE;
  911. }
  912. return m_paraformat;
  913. }
  914. void CRichEditView::SetCharFormat(CHARFORMAT cf)
  915. {
  916. CWaitCursor wait;
  917. GetRichEditCtrl().SetSelectionCharFormat(cf);
  918. m_bSyncCharFormat = TRUE;
  919. }
  920. BOOL CRichEditView::SetParaFormat(PARAFORMAT& pf)
  921. {
  922. BOOL bRetVal;
  923. CWaitCursor wait;
  924. bRetVal = GetRichEditCtrl().SetParaFormat(pf);
  925. m_bSyncParaFormat = TRUE;
  926. return bRetVal;
  927. }
  928. CRichEditCntrItem* CRichEditView::GetSelectedItem() const
  929. {
  930. ASSERT(m_lpRichEditOle != NULL);
  931. CRichEditDoc* pDoc = GetDocument();
  932. CRichEditCntrItem* pItem = NULL;
  933. CReObject reo;
  934. HRESULT hr = m_lpRichEditOle->GetObject(REO_IOB_SELECTION, &reo,
  935. REO_GETOBJ_ALL_INTERFACES);
  936. //reo's interfaces are all in UNICODE
  937. if (GetScode(hr) == S_OK)
  938. {
  939. pItem = pDoc->LookupItem(reo.poleobj);
  940. if (pItem == NULL)
  941. pItem = pDoc->CreateClientItem(&reo);
  942. ASSERT(pItem != NULL);
  943. }
  944. return pItem;
  945. }
  946. CRichEditCntrItem* CRichEditView::GetInPlaceActiveItem() const
  947. {
  948. ASSERT(m_lpRichEditOle != NULL);
  949. CRichEditDoc* pDoc = GetDocument();
  950. CRichEditCntrItem* pItem = NULL;
  951. CReObject reo;
  952. HRESULT hr = m_lpRichEditOle->GetObject(REO_IOB_SELECTION, &reo,
  953. REO_GETOBJ_ALL_INTERFACES);
  954. //reo's interfaces are all in UNICODE
  955. if (GetScode(hr) == S_OK && (reo.dwFlags & REO_INPLACEACTIVE))
  956. {
  957. pItem = pDoc->LookupItem(reo.poleobj);
  958. if (pItem == NULL)
  959. pItem = pDoc->CreateClientItem(&reo);
  960. ASSERT(pItem != NULL);
  961. }
  962. return pItem;
  963. }
  964. /////////////////////////////////////////////////////////////////////////////
  965. // CRichEditView operations
  966. HRESULT CRichEditView::InsertItem(CRichEditCntrItem* pItem)
  967. {
  968. ASSERT(m_lpRichEditOle != NULL);
  969. CReObject reo(pItem);
  970. reo.cp = REO_CP_SELECTION;
  971. HRESULT hr = m_lpRichEditOle->InsertObject(&reo);
  972. CHARRANGE cr;
  973. GetRichEditCtrl().GetSel(cr);
  974. cr.cpMin = cr.cpMax -1;
  975. GetRichEditCtrl().SetSel(cr);
  976. return hr;
  977. }
  978. void CRichEditView::InsertFileAsObject(LPCTSTR lpszFileName)
  979. {
  980. CString str = lpszFileName;
  981. CWaitCursor wait;
  982. CRichEditCntrItem* pItem = NULL;
  983. TRY
  984. {
  985. // create item from dialog results
  986. pItem = GetDocument()->CreateClientItem();
  987. pItem->m_bLock = TRUE;
  988. if (!pItem->CreateFromFile(str))
  989. AfxThrowMemoryException(); // any exception will do
  990. pItem->UpdateLink();
  991. InsertItem(pItem);
  992. pItem->m_bLock = FALSE;
  993. }
  994. CATCH(CException, e)
  995. {
  996. if (pItem != NULL)
  997. {
  998. pItem->m_bLock = FALSE;
  999. ASSERT_VALID(pItem);
  1000. pItem->Delete();
  1001. }
  1002. }
  1003. END_CATCH
  1004. }
  1005. void CRichEditView::DoPaste(COleDataObject& dataobj, CLIPFORMAT cf, HMETAFILEPICT hMetaPict)
  1006. {
  1007. CWaitCursor wait;
  1008. CRichEditCntrItem* pItem = NULL;
  1009. TRY
  1010. {
  1011. // create item from dialog results
  1012. pItem = GetDocument()->CreateClientItem();
  1013. pItem->m_bLock = TRUE;
  1014. if (m_nPasteType == COlePasteSpecialDialog::pasteLink) // paste link
  1015. {
  1016. if (!pItem->CreateLinkFromData(&dataobj))
  1017. AfxThrowMemoryException(); // any exception will do
  1018. }
  1019. else if (m_nPasteType == COlePasteSpecialDialog::pasteNormal)
  1020. {
  1021. if (!pItem->CreateFromData(&dataobj))
  1022. AfxThrowMemoryException(); // any exception will do
  1023. }
  1024. else if (m_nPasteType == COlePasteSpecialDialog::pasteStatic)
  1025. {
  1026. if (!pItem->CreateStaticFromData(&dataobj))
  1027. AfxThrowMemoryException(); // any exception will do
  1028. }
  1029. else
  1030. {
  1031. // paste embedded
  1032. if (!pItem->CreateFromData(&dataobj) &&
  1033. !pItem->CreateStaticFromData(&dataobj))
  1034. {
  1035. AfxThrowMemoryException(); // any exception will do
  1036. }
  1037. }
  1038. if (cf == 0)
  1039. {
  1040. // copy the current iconic representation
  1041. FORMATETC fmtetc;
  1042. fmtetc.cfFormat = CF_METAFILEPICT;
  1043. fmtetc.dwAspect = DVASPECT_ICON;
  1044. fmtetc.ptd = NULL;
  1045. fmtetc.tymed = TYMED_MFPICT;
  1046. fmtetc.lindex = 1;
  1047. HGLOBAL hObj = dataobj.GetGlobalData(CF_METAFILEPICT, &fmtetc);
  1048. if (hObj != NULL)
  1049. {
  1050. pItem->SetIconicMetafile(hObj);
  1051. // the following code is an easy way to free a metafile pict
  1052. STGMEDIUM stgMed;
  1053. memset(&stgMed, 0, sizeof(stgMed));
  1054. stgMed.tymed = TYMED_MFPICT;
  1055. stgMed.hGlobal = hObj;
  1056. ReleaseStgMedium(&stgMed);
  1057. }
  1058. // set the current drawing aspect
  1059. hObj = dataobj.GetGlobalData((CLIPFORMAT)_oleData.cfObjectDescriptor);
  1060. if (hObj != NULL)
  1061. {
  1062. ASSERT(hObj != NULL);
  1063. // got CF_OBJECTDESCRIPTOR ok. Lock it down and extract size.
  1064. LPOBJECTDESCRIPTOR pObjDesc = (LPOBJECTDESCRIPTOR)GlobalLock(hObj);
  1065. ASSERT(pObjDesc != NULL);
  1066. ((COleClientItem*)pItem)->SetDrawAspect((DVASPECT)pObjDesc->dwDrawAspect);
  1067. GlobalUnlock(hObj);
  1068. GlobalFree(hObj);
  1069. }
  1070. }
  1071. else
  1072. {
  1073. if (hMetaPict != NULL)
  1074. {
  1075. pItem->SetIconicMetafile(hMetaPict);
  1076. ((COleClientItem*)pItem)->SetDrawAspect(DVASPECT_ICON);
  1077. }
  1078. else
  1079. ((COleClientItem*)pItem)->SetDrawAspect(DVASPECT_CONTENT);
  1080. }
  1081. HRESULT hr = InsertItem(pItem);
  1082. pItem->UpdateItemType();
  1083. pItem->m_bLock = FALSE;
  1084. if (hr != NOERROR)
  1085. AfxThrowOleException(hr);
  1086. }
  1087. CATCH(CException, e)
  1088. {
  1089. if (pItem != NULL)
  1090. {
  1091. pItem->m_bLock = FALSE;
  1092. ASSERT_VALID(pItem);
  1093. pItem->Delete();
  1094. }
  1095. }
  1096. END_CATCH
  1097. }
  1098. /////////////////////////////////////////////////////////////////////////////
  1099. // CRichEditView virtuals
  1100. void CRichEditView::OnPrinterChanged(const CDC& dcPrinter)
  1101. {
  1102. // this is typically called by the view when it gets a WM_DEVMODECHANGE
  1103. // also called during page setup
  1104. CSize size;
  1105. if (dcPrinter.m_hDC != NULL)
  1106. {
  1107. // this will fill in the page size
  1108. size.cx = MulDiv(dcPrinter.GetDeviceCaps(PHYSICALWIDTH), 1440,
  1109. dcPrinter.GetDeviceCaps(LOGPIXELSX));
  1110. size.cy = MulDiv(dcPrinter.GetDeviceCaps(PHYSICALHEIGHT), 1440,
  1111. dcPrinter.GetDeviceCaps(LOGPIXELSY));
  1112. }
  1113. else
  1114. size = CSize(8*1440+720, 11*1440); // 8.5" by 11"
  1115. if (GetPaperSize() != size)
  1116. {
  1117. SetPaperSize(size);
  1118. if (m_nWordWrap == WrapToTargetDevice) //wrap to ruler
  1119. WrapChanged();
  1120. }
  1121. }
  1122. BOOL CRichEditView::OnPasteNativeObject(LPSTORAGE)
  1123. {
  1124. // use this function to pull out native data from an embedded object
  1125. // one would typically do this by create a COleStreamFile and attaching it
  1126. // to an archive
  1127. return FALSE;
  1128. }
  1129. HMENU CRichEditView::GetContextMenu(WORD, LPOLEOBJECT, CHARRANGE* )
  1130. {
  1131. return NULL;
  1132. }
  1133. HRESULT CRichEditView::GetClipboardData(CHARRANGE* /*lpchrg*/, DWORD /*reco*/,
  1134. LPDATAOBJECT /*lpRichDataObj*/, LPDATAOBJECT* /*lplpdataobj*/)
  1135. {
  1136. return E_NOTIMPL;
  1137. }
  1138. HRESULT CRichEditView::QueryAcceptData(LPDATAOBJECT lpdataobj,
  1139. CLIPFORMAT* lpcfFormat, DWORD /*dwReco*/, BOOL bReally, HGLOBAL hMetaPict)
  1140. {
  1141. ASSERT(lpcfFormat != NULL);
  1142. if (!bReally) // not actually pasting
  1143. return S_OK;
  1144. // if direct pasting a particular native format allow it
  1145. if (IsRichEditFormat(*lpcfFormat))
  1146. return S_OK;
  1147. COleDataObject dataobj;
  1148. dataobj.Attach(lpdataobj, FALSE);
  1149. // if format is 0, then force particular formats if available
  1150. if (*lpcfFormat == 0 && (m_nPasteType == 0))
  1151. {
  1152. if (dataobj.IsDataAvailable((CLIPFORMAT)_oleData.cfRichTextAndObjects)) // native avail, let richedit do as it wants
  1153. return S_OK;
  1154. else if (dataobj.IsDataAvailable((CLIPFORMAT)_oleData.cfRichTextFormat))
  1155. {
  1156. *lpcfFormat = (CLIPFORMAT)_oleData.cfRichTextFormat;
  1157. return S_OK;
  1158. }
  1159. else if (dataobj.IsDataAvailable(CF_TEXT))
  1160. {
  1161. *lpcfFormat = CF_TEXT;
  1162. return S_OK;
  1163. }
  1164. }
  1165. // paste OLE formats
  1166. DoPaste(dataobj, *lpcfFormat, hMetaPict);
  1167. return S_FALSE;
  1168. }
  1169. HRESULT CRichEditView::GetWindowContext(LPOLEINPLACEFRAME* lplpFrame,
  1170. LPOLEINPLACEUIWINDOW* lplpDoc, LPOLEINPLACEFRAMEINFO lpFrameInfo)
  1171. {
  1172. CRichEditCntrItem* pItem = GetSelectedItem();
  1173. if (pItem == NULL)
  1174. return E_FAIL;
  1175. pItem->m_pView = this;
  1176. HRESULT hr = pItem->GetWindowContext(lplpFrame, lplpDoc, lpFrameInfo);
  1177. pItem->m_pView = NULL;
  1178. return hr;
  1179. }
  1180. HRESULT CRichEditView::ShowContainerUI(BOOL b)
  1181. {
  1182. CRichEditCntrItem* pItem = GetSelectedItem();
  1183. if (pItem == NULL)
  1184. return E_FAIL;
  1185. if (b)
  1186. pItem->m_pView = this;
  1187. HRESULT hr = pItem->ShowContainerUI(b);
  1188. if (FAILED(hr) || !b)
  1189. pItem->m_pView = NULL;
  1190. return hr;
  1191. }
  1192. /////////////////////////////////////////////////////////////////////////////
  1193. // CRichEditView Find & Replace
  1194. void CRichEditView::AdjustDialogPosition(CDialog* pDlg)
  1195. {
  1196. ASSERT(pDlg != NULL);
  1197. long lStart, lEnd;
  1198. GetRichEditCtrl().GetSel(lStart, lEnd);
  1199. CPoint point = GetRichEditCtrl().GetCharPos(lStart);
  1200. ClientToScreen(&point);
  1201. CRect rectDlg;
  1202. pDlg->GetWindowRect(&rectDlg);
  1203. if (rectDlg.PtInRect(point))
  1204. {
  1205. if (point.y > rectDlg.Height())
  1206. rectDlg.OffsetRect(0, point.y - rectDlg.bottom - 20);
  1207. else
  1208. {
  1209. int nVertExt = GetSystemMetrics(SM_CYSCREEN);
  1210. if (point.y + rectDlg.Height() < nVertExt)
  1211. rectDlg.OffsetRect(0, 40 + point.y - rectDlg.top);
  1212. }
  1213. pDlg->MoveWindow(&rectDlg);
  1214. }
  1215. }
  1216. void CRichEditView::OnEditFindReplace(BOOL bFindOnly)
  1217. {
  1218. ASSERT_VALID(this);
  1219. m_bFirstSearch = TRUE;
  1220. _AFX_RICHEDIT_STATE* pEditState = _afxRichEditState;
  1221. if (pEditState->pFindReplaceDlg != NULL)
  1222. {
  1223. if (pEditState->bFindOnly == bFindOnly)
  1224. {
  1225. pEditState->pFindReplaceDlg->SetActiveWindow();
  1226. pEditState->pFindReplaceDlg->ShowWindow(SW_SHOW);
  1227. return;
  1228. }
  1229. else
  1230. {
  1231. ASSERT(pEditState->bFindOnly != bFindOnly);
  1232. pEditState->pFindReplaceDlg->SendMessage(WM_CLOSE);
  1233. ASSERT(pEditState->pFindReplaceDlg == NULL);
  1234. ASSERT_VALID(this);
  1235. }
  1236. }
  1237. CString strFind = GetRichEditCtrl().GetSelText();
  1238. // if selection is empty or spans multiple lines use old find text
  1239. if (strFind.IsEmpty() || (strFind.FindOneOf(_T("\n\r")) != -1))
  1240. strFind = pEditState->strFind;
  1241. CString strReplace = pEditState->strReplace;
  1242. pEditState->pFindReplaceDlg = new CFindReplaceDialog;
  1243. ASSERT(pEditState->pFindReplaceDlg != NULL);
  1244. DWORD dwFlags = NULL;
  1245. if (pEditState->bNext)
  1246. dwFlags |= FR_DOWN;
  1247. if (pEditState->bCase)
  1248. dwFlags |= FR_MATCHCASE;
  1249. if (pEditState->bWord)
  1250. dwFlags |= FR_WHOLEWORD;
  1251. // hide stuff that RichEdit doesn't support
  1252. dwFlags |= FR_HIDEUPDOWN;
  1253. if (!pEditState->pFindReplaceDlg->Create(bFindOnly, strFind,
  1254. strReplace, dwFlags, this))
  1255. {
  1256. pEditState->pFindReplaceDlg = NULL;
  1257. ASSERT_VALID(this);
  1258. return;
  1259. }
  1260. ASSERT(pEditState->pFindReplaceDlg != NULL);
  1261. pEditState->bFindOnly = bFindOnly;
  1262. pEditState->pFindReplaceDlg->SetActiveWindow();
  1263. pEditState->pFindReplaceDlg->ShowWindow(SW_SHOW);
  1264. ASSERT_VALID(this);
  1265. }
  1266. void CRichEditView::OnFindNext(LPCTSTR lpszFind, BOOL bNext, BOOL bCase, BOOL bWord)
  1267. {
  1268. ASSERT_VALID(this);
  1269. _AFX_RICHEDIT_STATE* pEditState = _afxRichEditState;
  1270. pEditState->strFind = lpszFind;
  1271. pEditState->bCase = bCase;
  1272. pEditState->bWord = bWord;
  1273. pEditState->bNext = bNext;
  1274. if (!FindText(pEditState))
  1275. TextNotFound(pEditState->strFind);
  1276. else
  1277. AdjustDialogPosition(pEditState->pFindReplaceDlg);
  1278. ASSERT_VALID(this);
  1279. }
  1280. void CRichEditView::OnReplaceSel(LPCTSTR lpszFind, BOOL bNext, BOOL bCase,
  1281. BOOL bWord, LPCTSTR lpszReplace)
  1282. {
  1283. ASSERT_VALID(this);
  1284. _AFX_RICHEDIT_STATE* pEditState = _afxRichEditState;
  1285. pEditState->strFind = lpszFind;
  1286. pEditState->strReplace = lpszReplace;
  1287. pEditState->bCase = bCase;
  1288. pEditState->bWord = bWord;
  1289. pEditState->bNext = bNext;
  1290. if (!SameAsSelected(pEditState->strFind, pEditState->bCase, pEditState->bWord))
  1291. {
  1292. if (!FindText(pEditState))
  1293. TextNotFound(pEditState->strFind);
  1294. else
  1295. AdjustDialogPosition(pEditState->pFindReplaceDlg);
  1296. return;
  1297. }
  1298. GetRichEditCtrl().ReplaceSel(pEditState->strReplace);
  1299. if (!FindText(pEditState))
  1300. TextNotFound(pEditState->strFind);
  1301. else
  1302. AdjustDialogPosition(pEditState->pFindReplaceDlg);
  1303. ASSERT_VALID(this);
  1304. }
  1305. void CRichEditView::OnReplaceAll(LPCTSTR lpszFind, LPCTSTR lpszReplace, BOOL bCase, BOOL bWord)
  1306. {
  1307. ASSERT_VALID(this);
  1308. _AFX_RICHEDIT_STATE* pEditState = _afxRichEditState;
  1309. pEditState->strFind = lpszFind;
  1310. pEditState->strReplace = lpszReplace;
  1311. pEditState->bCase = bCase;
  1312. pEditState->bWord = bWord;
  1313. pEditState->bNext = TRUE;
  1314. CWaitCursor wait;
  1315. // no selection or different than what looking for
  1316. if (!SameAsSelected(pEditState->strFind, pEditState->bCase, pEditState->bWord))
  1317. {
  1318. if (!FindText(pEditState))
  1319. {
  1320. TextNotFound(pEditState->strFind);
  1321. return;
  1322. }
  1323. }
  1324. GetRichEditCtrl().HideSelection(TRUE, FALSE);
  1325. do
  1326. {
  1327. GetRichEditCtrl().ReplaceSel(pEditState->strReplace);
  1328. } while (FindTextSimple(pEditState));
  1329. TextNotFound(pEditState->strFind);
  1330. GetRichEditCtrl().HideSelection(FALSE, FALSE);
  1331. ASSERT_VALID(this);
  1332. }
  1333. LRESULT CRichEditView::OnFindReplaceCmd(WPARAM, LPARAM lParam)
  1334. {
  1335. ASSERT_VALID(this);
  1336. CFindReplaceDialog* pDialog = CFindReplaceDialog::GetNotifier(lParam);
  1337. ASSERT(pDialog != NULL);
  1338. _AFX_RICHEDIT_STATE* pEditState = _afxRichEditState;
  1339. ASSERT(pDialog == pEditState->pFindReplaceDlg);
  1340. if (pDialog->IsTerminating())
  1341. pEditState->pFindReplaceDlg = NULL;
  1342. else if (pDialog->FindNext())
  1343. {
  1344. OnFindNext(pDialog->GetFindString(), pDialog->SearchDown(),
  1345. pDialog->MatchCase(), pDialog->MatchWholeWord());
  1346. }
  1347. else if (pDialog->ReplaceCurrent())
  1348. {
  1349. ASSERT(!pEditState->bFindOnly);
  1350. OnReplaceSel(pDialog->GetFindString(),
  1351. pDialog->SearchDown(), pDialog->MatchCase(), pDialog->MatchWholeWord(),
  1352. pDialog->GetReplaceString());
  1353. }
  1354. else if (pDialog->ReplaceAll())
  1355. {
  1356. ASSERT(!pEditState->bFindOnly);
  1357. OnReplaceAll(pDialog->GetFindString(), pDialog->GetReplaceString(),
  1358. pDialog->MatchCase(), pDialog->MatchWholeWord());
  1359. }
  1360. ASSERT_VALID(this);
  1361. return 0;
  1362. }
  1363. BOOL CRichEditView::SameAsSelected(LPCTSTR lpszCompare, BOOL bCase, BOOL /*bWord*/)
  1364. {
  1365. // check length first
  1366. size_t nLen = lstrlen(lpszCompare);
  1367. long lStartChar, lEndChar;
  1368. GetRichEditCtrl().GetSel(lStartChar, lEndChar);
  1369. if (nLen != (size_t)(lEndChar - lStartChar))
  1370. return FALSE;
  1371. // length is the same, check contents
  1372. CString strSelect = GetRichEditCtrl().GetSelText();
  1373. return (bCase && lstrcmp(lpszCompare, strSelect) == 0) ||
  1374. (!bCase && lstrcmpi(lpszCompare, strSelect) == 0);
  1375. }
  1376. BOOL CRichEditView::FindText(_AFX_RICHEDIT_STATE* pEditState)
  1377. {
  1378. ASSERT(pEditState != NULL);
  1379. return FindText(pEditState->strFind, pEditState->bCase, pEditState->bWord);
  1380. }
  1381. BOOL CRichEditView::FindText(LPCTSTR lpszFind, BOOL bCase, BOOL bWord)
  1382. {
  1383. ASSERT_VALID(this);
  1384. CWaitCursor wait;
  1385. return FindTextSimple(lpszFind, bCase, bWord);
  1386. }
  1387. BOOL CRichEditView::FindTextSimple(_AFX_RICHEDIT_STATE* pEditState)
  1388. {
  1389. ASSERT(pEditState != NULL);
  1390. return FindTextSimple(pEditState->strFind, pEditState->bCase, pEditState->bWord);
  1391. }
  1392. BOOL CRichEditView::FindTextSimple(LPCTSTR lpszFind, BOOL bCase, BOOL bWord)
  1393. {
  1394. USES_CONVERSION;
  1395. ASSERT(lpszFind != NULL);
  1396. FINDTEXTEX ft;
  1397. GetRichEditCtrl().GetSel(ft.chrg);
  1398. if (m_bFirstSearch)
  1399. {
  1400. m_lInitialSearchPos = ft.chrg.cpMin;
  1401. m_bFirstSearch = FALSE;
  1402. }
  1403. ft.lpstrText = T2A((LPTSTR)lpszFind);
  1404. if (ft.chrg.cpMin != ft.chrg.cpMax) // i.e. there is a selection
  1405. {
  1406. #ifndef _UNICODE
  1407. // If byte at beginning of selection is a DBCS lead byte,
  1408. // increment by one extra byte.
  1409. TEXTRANGE textRange;
  1410. TCHAR ch[2];
  1411. textRange.chrg.cpMin = ft.chrg.cpMin;
  1412. textRange.chrg.cpMax = ft.chrg.cpMin + 1;
  1413. textRange.lpstrText = ch;
  1414. GetRichEditCtrl().SendMessage(EM_GETTEXTRANGE, 0, (LPARAM)&textRange);
  1415. if (_istlead(ch[0]))
  1416. {
  1417. ASSERT(ft.chrg.cpMax - ft.chrg.cpMin >= 2);
  1418. ft.chrg.cpMin++;
  1419. }
  1420. #endif
  1421. ft.chrg.cpMin++;
  1422. }
  1423. if (m_lInitialSearchPos >= 0)
  1424. ft.chrg.cpMax = GetTextLength();
  1425. else
  1426. ft.chrg.cpMax = GetTextLength()+m_lInitialSearchPos;
  1427. DWORD dwFlags = bCase ? FR_MATCHCASE : 0;
  1428. dwFlags |= bWord ? FR_WHOLEWORD : 0;
  1429. // if we find the text return TRUE
  1430. if (FindAndSelect(dwFlags, ft) != -1)
  1431. return TRUE;
  1432. // if the original starting point was not the beginning of the buffer
  1433. // and we haven't already been here
  1434. else if (m_lInitialSearchPos > 0)
  1435. {
  1436. ft.chrg.cpMin = 0;
  1437. ft.chrg.cpMax = m_lInitialSearchPos;
  1438. m_lInitialSearchPos = m_lInitialSearchPos - GetTextLength();
  1439. return FindAndSelect(dwFlags, ft) != -1;
  1440. }
  1441. // not found
  1442. else
  1443. return FALSE;
  1444. }
  1445. long CRichEditView::FindAndSelect(DWORD dwFlags, FINDTEXTEX& ft)
  1446. {
  1447. long index = GetRichEditCtrl().FindText(dwFlags, &ft);
  1448. if (index != -1) // i.e. we found something
  1449. GetRichEditCtrl().SetSel(ft.chrgText);
  1450. return index;
  1451. }
  1452. void CRichEditView::TextNotFound(LPCTSTR lpszFind)
  1453. {
  1454. ASSERT_VALID(this);
  1455. m_bFirstSearch = TRUE;
  1456. OnTextNotFound(lpszFind);
  1457. }
  1458. void CRichEditView::OnTextNotFound(LPCTSTR)
  1459. {
  1460. MessageBeep(MB_ICONHAND);
  1461. }
  1462. /////////////////////////////////////////////////////////////////////////////
  1463. // CRichEditView diagnostics
  1464. #ifdef _DEBUG
  1465. void CRichEditView::AssertValid() const
  1466. {
  1467. CCtrlView::AssertValid();
  1468. ASSERT_VALID(&m_aPageStart);
  1469. _AFX_RICHEDIT_STATE* pEditState = _afxRichEditState;
  1470. if (pEditState->pFindReplaceDlg != NULL)
  1471. ASSERT_VALID(pEditState->pFindReplaceDlg);
  1472. }
  1473. void CRichEditView::Dump(CDumpContext& dc) const
  1474. {
  1475. CCtrlView::Dump(dc);
  1476. AFX_DUMP1(dc, "\nm_aPageStart ", &m_aPageStart);
  1477. AFX_DUMP0(dc, "\n Static Member Data:");
  1478. _AFX_RICHEDIT_STATE* pEditState = _afxRichEditState;
  1479. if (pEditState->pFindReplaceDlg != NULL)
  1480. {
  1481. AFX_DUMP1(dc, "\npFindReplaceDlg = ",
  1482. (void*)pEditState->pFindReplaceDlg);
  1483. AFX_DUMP1(dc, "\nbFindOnly = ", pEditState->bFindOnly);
  1484. }
  1485. AFX_DUMP1(dc, "\nstrFind = ", pEditState->strFind);
  1486. AFX_DUMP1(dc, "\nstrReplace = ", pEditState->strReplace);
  1487. AFX_DUMP1(dc, "\nbCase = ", pEditState->bCase);
  1488. AFX_DUMP1(dc, "\nbWord = ", pEditState->bWord);
  1489. AFX_DUMP1(dc, "\nbNext = ", pEditState->bNext);
  1490. }
  1491. #endif //_DEBUG
  1492. /////////////////////////////////////////////////////////////////////////////
  1493. // OLE Client support and commands
  1494. BOOL CRichEditView::IsSelected(const CObject* pDocItem) const
  1495. {
  1496. return (pDocItem == GetSelectedItem());
  1497. }
  1498. /////////////////////////////////////////////////////////////////////////////
  1499. // CRichEditDoc
  1500. CRichEditDoc::CRichEditDoc()
  1501. {
  1502. m_bRTF = TRUE;
  1503. m_bUpdateObjectCache = FALSE;
  1504. ASSERT_VALID(this);
  1505. }
  1506. CRichEditView* CRichEditDoc::GetView() const
  1507. {
  1508. // find the first view - if there are no views
  1509. // we must return NULL
  1510. POSITION pos = GetFirstViewPosition();
  1511. if (pos == NULL)
  1512. return NULL;
  1513. // find the first view that is a CRichEditView
  1514. CView* pView;
  1515. while (pos != NULL)
  1516. {
  1517. pView = GetNextView(pos);
  1518. if (pView->IsKindOf(RUNTIME_CLASS(CRichEditView)))
  1519. return (CRichEditView*) pView;
  1520. }
  1521. // can't find one--return NULL
  1522. return NULL;
  1523. }
  1524. BOOL CRichEditDoc::IsModified()
  1525. {
  1526. return GetView()->GetRichEditCtrl().GetModify();
  1527. }
  1528. void CRichEditDoc::SetModifiedFlag(BOOL bModified)
  1529. {
  1530. GetView()->GetRichEditCtrl().SetModify(bModified);
  1531. ASSERT(!!GetView()->GetRichEditCtrl().GetModify() == !!bModified);
  1532. }
  1533. COleClientItem* CRichEditDoc::GetInPlaceActiveItem(CWnd* pWnd)
  1534. {
  1535. ASSERT_KINDOF(CRichEditView, pWnd);
  1536. CRichEditView* pView = (CRichEditView*)pWnd;
  1537. return pView->GetInPlaceActiveItem();
  1538. }
  1539. void CRichEditDoc::SetPathName(LPCTSTR lpszPathName, BOOL bAddToMRU)
  1540. {
  1541. // we call CDocument and not COleServerDoc because we don't want to do the
  1542. // SetHostNames stuff here. The richedit will do it. And we tell the richedit
  1543. // in SetTitle
  1544. CDocument::SetPathName(lpszPathName, bAddToMRU);
  1545. }
  1546. void CRichEditDoc::SetTitle(LPCTSTR lpszTitle)
  1547. {
  1548. USES_CONVERSION;
  1549. COleServerDoc::SetTitle(lpszTitle);
  1550. CRichEditView *pView = GetView();
  1551. ASSERT(pView != NULL);
  1552. ASSERT(pView->m_lpRichEditOle != NULL);
  1553. pView->m_lpRichEditOle->SetHostNames(T2CA(AfxGetAppName()),
  1554. T2CA(lpszTitle));
  1555. }
  1556. CRichEditCntrItem* CRichEditDoc::LookupItem(LPOLEOBJECT lpobj) const
  1557. {
  1558. POSITION pos = COleServerDoc::GetStartPosition();
  1559. CRichEditCntrItem* pItem;
  1560. while (pos != NULL)
  1561. {
  1562. pItem = (CRichEditCntrItem*) COleServerDoc::GetNextItem(pos);
  1563. // delete item is right type and not under construction
  1564. if (pItem->IsKindOf(RUNTIME_CLASS(CRichEditCntrItem)) &&
  1565. pItem->m_lpObject == lpobj)
  1566. {
  1567. return pItem;
  1568. }
  1569. }
  1570. return NULL;
  1571. }
  1572. CRichEditCntrItem* CRichEditDoc::CreateClientItem(REOBJECT* preo) const
  1573. {
  1574. // cast away constness of this
  1575. return new CRichEditCntrItem(preo, (CRichEditDoc*)this);
  1576. // a derived class typically needs to return its own item of a class
  1577. // derived from CRichEditCntrItem
  1578. }
  1579. void CRichEditDoc::MarkItemsClear() const
  1580. {
  1581. POSITION pos = COleServerDoc::GetStartPosition();
  1582. CRichEditCntrItem* pItem;
  1583. while (pos != NULL)
  1584. {
  1585. pItem = (CRichEditCntrItem*) COleServerDoc::GetNextItem(pos);
  1586. // Mark item as not in use unless under construction (i.e. m_lpObject == NULL)
  1587. if (pItem->IsKindOf(RUNTIME_CLASS(CRichEditCntrItem)))
  1588. pItem->Mark(pItem->m_lpObject == NULL);
  1589. }
  1590. }
  1591. void CRichEditDoc::DeleteUnmarkedItems() const
  1592. {
  1593. POSITION pos = COleServerDoc::GetStartPosition();
  1594. CRichEditCntrItem* pItem;
  1595. while (pos != NULL)
  1596. {
  1597. pItem = (CRichEditCntrItem*) COleServerDoc::GetNextItem(pos);
  1598. // Mark item as not in use unless under construction (i.e. m_lpObject == NULL)
  1599. if (pItem->IsKindOf(RUNTIME_CLASS(CRichEditCntrItem)) && !pItem->IsMarked())
  1600. delete pItem;
  1601. }
  1602. }
  1603. POSITION CRichEditDoc::GetStartPosition() const
  1604. {
  1605. if (m_bUpdateObjectCache)
  1606. ((CRichEditDoc*)this)->UpdateObjectCache(); //cast away const
  1607. return COleServerDoc::GetStartPosition();
  1608. }
  1609. void CRichEditDoc::UpdateObjectCache()
  1610. {
  1611. CRichEditView* pView = GetView();
  1612. CRichEditCntrItem* pItem;
  1613. if (pView != NULL)
  1614. {
  1615. ASSERT(pView->m_lpRichEditOle != NULL);
  1616. MarkItemsClear();
  1617. long i,nCount = pView->m_lpRichEditOle->GetObjectCount();
  1618. for (i=0;i<nCount;i++)
  1619. {
  1620. CReObject reo; // needs to be in here so destructor called to release interfaces
  1621. HRESULT hr = pView->m_lpRichEditOle->GetObject(i, &reo, REO_GETOBJ_ALL_INTERFACES);
  1622. //reo interfaces are UNICODE
  1623. ASSERT(SUCCEEDED(hr));
  1624. if (GetScode(hr) == S_OK)
  1625. {
  1626. pItem = LookupItem(reo.poleobj);
  1627. if (pItem == NULL)
  1628. {
  1629. pItem = ((CRichEditDoc*)this)->CreateClientItem(&reo);
  1630. pItem->UpdateItemType();
  1631. }
  1632. ASSERT(pItem != NULL);
  1633. pItem->Mark(TRUE);
  1634. }
  1635. }
  1636. DeleteUnmarkedItems();
  1637. }
  1638. m_bUpdateObjectCache = FALSE;
  1639. }
  1640. /////////////////////////////////////////////////////////////////////////////
  1641. // CRichEditDoc Attributes
  1642. COleClientItem* CRichEditDoc::GetPrimarySelectedItem(CView* pView)
  1643. {
  1644. ASSERT(pView->IsKindOf(RUNTIME_CLASS(CRichEditView)));
  1645. return ((CRichEditView*)pView)->GetSelectedItem();
  1646. }
  1647. /////////////////////////////////////////////////////////////////////////////
  1648. // CRichEditDoc Operations
  1649. void CRichEditDoc::DeleteContents()
  1650. {
  1651. COleServerDoc::DeleteContents();
  1652. CWaitCursor wait;
  1653. CRichEditView *pView = GetView();
  1654. if (pView != NULL)
  1655. {
  1656. pView->DeleteContents();
  1657. pView->GetRichEditCtrl().SetModify(FALSE);
  1658. ASSERT(!pView->GetRichEditCtrl().GetModify());
  1659. }
  1660. }
  1661. /////////////////////////////////////////////////////////////////////////////
  1662. // CRichEditDoc serialization
  1663. void CRichEditDoc::Serialize(CArchive& ar)
  1664. {
  1665. CRichEditView *pView = GetView();
  1666. if (pView != NULL)
  1667. pView->Serialize(ar);
  1668. // we don't call the base class COleServerDoc::Serialize
  1669. // because we don't want the client items serialized
  1670. // the client items are handled directly by the RichEdit control
  1671. }
  1672. /////////////////////////////////////////////////////////////////////////////
  1673. // CRichEditDoc diagnostics
  1674. #ifdef _DEBUG
  1675. void CRichEditDoc::AssertValid() const
  1676. {
  1677. COleServerDoc::AssertValid();
  1678. }
  1679. void CRichEditDoc::Dump(CDumpContext& dc) const
  1680. {
  1681. COleServerDoc::Dump(dc);
  1682. }
  1683. #endif //_DEBUG
  1684. /////////////////////////////////////////////////////////////////////////////
  1685. // CRichEditDoc commands
  1686. void CRichEditDoc::PreCloseFrame(CFrameWnd* pFrameArg)
  1687. {
  1688. ASSERT_VALID(this);
  1689. ASSERT_VALID(pFrameArg);
  1690. // turn off redraw so the user doesn't see the deactivation happening
  1691. BOOL bSetRedraw = FALSE;
  1692. if (pFrameArg->GetStyle() & WS_VISIBLE)
  1693. {
  1694. pFrameArg->SendMessage(WM_SETREDRAW, (WPARAM)FALSE);
  1695. bSetRedraw = TRUE;
  1696. }
  1697. // deactivate any inplace active items on this frame
  1698. GetView()->m_lpRichEditOle->InPlaceDeactivate();
  1699. POSITION pos = GetStartPosition();
  1700. CRichEditCntrItem* pItem;
  1701. while (pos != NULL)
  1702. {
  1703. pItem = (CRichEditCntrItem*) GetNextClientItem(pos);
  1704. if (pItem == NULL)
  1705. break;
  1706. ASSERT(pItem->IsKindOf(RUNTIME_CLASS(CRichEditCntrItem)));
  1707. pItem->Close();
  1708. }
  1709. // turn redraw back on
  1710. if (bSetRedraw)
  1711. pFrameArg->SendMessage(WM_SETREDRAW, (WPARAM)TRUE);
  1712. }
  1713. void CRichEditDoc::UpdateModifiedFlag()
  1714. {
  1715. // don't do anything here
  1716. // let the richedit handle all of this
  1717. }
  1718. COleServerItem* CRichEditDoc::OnGetEmbeddedItem()
  1719. {
  1720. ASSERT(FALSE);
  1721. return NULL;
  1722. }
  1723. /////////////////////////////////////////////////////////////////////////////
  1724. // CRichEditCntrItem implementation
  1725. CRichEditCntrItem::CRichEditCntrItem(REOBJECT *preo, CRichEditDoc* pContainer)
  1726. : COleClientItem(pContainer)
  1727. {
  1728. m_bMark = FALSE;
  1729. m_bLock = FALSE;
  1730. if (preo != NULL)
  1731. {
  1732. ASSERT(preo->poleobj != NULL);
  1733. ASSERT(preo->pstg != NULL);
  1734. ASSERT(preo->polesite != NULL);
  1735. m_lpObject = preo->poleobj;
  1736. m_lpStorage = preo->pstg;
  1737. m_lpClientSite = preo->polesite;
  1738. m_lpObject->AddRef();
  1739. m_lpStorage->AddRef();
  1740. m_lpClientSite->AddRef();
  1741. }
  1742. else
  1743. {
  1744. m_lpObject = NULL;
  1745. m_lpStorage = NULL;
  1746. m_lpClientSite = NULL;
  1747. }
  1748. }
  1749. CRichEditCntrItem::~CRichEditCntrItem()
  1750. {
  1751. if (m_lpClientSite != NULL)
  1752. m_lpClientSite->Release();
  1753. }
  1754. void CRichEditCntrItem::OnDeactivateUI(BOOL bUndoable)
  1755. {
  1756. CView* pView = GetActiveView();
  1757. if (pView != NULL)
  1758. {
  1759. ASSERT(pView->GetParentFrame() != NULL);
  1760. pView->GetParentFrame()->SendMessage(WM_SETMESSAGESTRING,
  1761. (WPARAM)AFX_IDS_IDLEMESSAGE);
  1762. }
  1763. COleClientItem::OnDeactivateUI(bUndoable);
  1764. }
  1765. HRESULT CRichEditCntrItem::ShowContainerUI(BOOL b)
  1766. {
  1767. if (!CanActivate())
  1768. return E_NOTIMPL;
  1769. if (b)
  1770. {
  1771. OnDeactivateUI(FALSE);
  1772. OnDeactivate();
  1773. }
  1774. else
  1775. {
  1776. OnActivate();
  1777. OnActivateUI();
  1778. }
  1779. return S_OK;
  1780. }
  1781. BOOL CRichEditCntrItem::OnChangeItemPosition(const CRect& /*rectPos*/)
  1782. {
  1783. ASSERT_VALID(this);
  1784. // richedit handles this
  1785. return FALSE;
  1786. }
  1787. BOOL CRichEditCntrItem::CanActivate()
  1788. {
  1789. // Editing in-place while the server itself is being edited in-place
  1790. // does not work and is not supported. So, disable in-place
  1791. // activation in this case.
  1792. COleServerDoc* pDoc = DYNAMIC_DOWNCAST(COleServerDoc, GetDocument());
  1793. if (pDoc != NULL && pDoc->IsInPlaceActive())
  1794. return FALSE;
  1795. // otherwise, rely on default behavior
  1796. return COleClientItem::CanActivate();
  1797. }
  1798. HRESULT CRichEditCntrItem::GetWindowContext(LPOLEINPLACEFRAME* lplpFrame,
  1799. LPOLEINPLACEUIWINDOW* lplpDoc, LPOLEINPLACEFRAMEINFO lpFrameInfo)
  1800. {
  1801. CRect rc1,rc2;
  1802. if (!CanActivate())
  1803. return E_NOTIMPL;
  1804. return m_xOleIPSite.GetWindowContext(lplpFrame, lplpDoc, &rc1, &rc2, lpFrameInfo);
  1805. }
  1806. BOOL CRichEditCntrItem::ConvertTo(REFCLSID clsidNew)
  1807. {
  1808. USES_CONVERSION;
  1809. LPRICHEDITOLE preole = GetDocument()->GetView()->m_lpRichEditOle;
  1810. LPOLESTR lpOleStr;
  1811. OleRegGetUserType(clsidNew, USERCLASSTYPE_FULL, &lpOleStr);
  1812. LPCTSTR lpsz = OLE2CT(lpOleStr);
  1813. HRESULT hRes = preole->ConvertObject(REO_IOB_SELECTION, clsidNew, T2CA(lpsz));
  1814. CoTaskMemFree(lpOleStr);
  1815. return (SUCCEEDED(hRes));
  1816. }
  1817. BOOL CRichEditCntrItem::ActivateAs(LPCTSTR, REFCLSID clsidOld,
  1818. REFCLSID clsidNew)
  1819. {
  1820. LPRICHEDITOLE preole = GetDocument()->GetView()->m_lpRichEditOle;
  1821. HRESULT hRes = preole->ActivateAs(clsidOld, clsidNew);
  1822. return (SUCCEEDED(hRes));
  1823. }
  1824. void CRichEditCntrItem::SetDrawAspect(DVASPECT nDrawAspect)
  1825. {
  1826. LPRICHEDITOLE preole = GetDocument()->GetView()->m_lpRichEditOle;
  1827. preole->SetDvaspect(REO_IOB_SELECTION, nDrawAspect);
  1828. COleClientItem::SetDrawAspect(nDrawAspect);
  1829. }
  1830. void CRichEditCntrItem::SyncToRichEditObject(REOBJECT& reo)
  1831. {
  1832. COleClientItem::SetDrawAspect((DVASPECT)reo.dvaspect);
  1833. }
  1834. /////////////////////////////////////////////////////////////////////////////
  1835. // CRichEditCntrItem diagnostics
  1836. #ifdef _DEBUG
  1837. void CRichEditCntrItem::AssertValid() const
  1838. {
  1839. COleClientItem::AssertValid();
  1840. }
  1841. void CRichEditCntrItem::Dump(CDumpContext& dc) const
  1842. {
  1843. COleClientItem::Dump(dc);
  1844. }
  1845. #endif
  1846. /////////////////////////////////////////////////////////////////////////////
  1847. LPOLECLIENTSITE CRichEditCntrItem::GetClientSite()
  1848. {
  1849. if (m_lpClientSite == NULL)
  1850. {
  1851. CRichEditDoc* pDoc = DYNAMIC_DOWNCAST(CRichEditDoc, GetDocument());
  1852. CRichEditView* pView = DYNAMIC_DOWNCAST(CRichEditView, pDoc->GetView());
  1853. ASSERT(pView->m_lpRichEditOle != NULL);
  1854. HRESULT hr = pView->m_lpRichEditOle->GetClientSite(&m_lpClientSite);
  1855. if (hr != S_OK)
  1856. AfxThrowOleException(hr);
  1857. }
  1858. ASSERT(m_lpClientSite != NULL);
  1859. return m_lpClientSite;
  1860. }
  1861. /////////////////////////////////////////////////////////////////////////////
  1862. #ifndef _AFX_ENABLE_INLINES
  1863. static const char _szAfxWinInl[] = "afxrich.inl";
  1864. #undef THIS_FILE
  1865. #define THIS_FILE _szAfxWinInl
  1866. #define _AFXRICH_INLINE
  1867. #include "afxrich.inl"
  1868. #endif //_AFX_ENABLE_INLINES
  1869. /////////////////////////////////////////////////////////////////////////////
  1870. #ifdef AFX_INIT_SEG
  1871. #pragma code_seg(AFX_INIT_SEG)
  1872. #endif
  1873. IMPLEMENT_SERIAL(CRichEditCntrItem, COleClientItem, 0)
  1874. IMPLEMENT_DYNAMIC(CRichEditDoc, COleServerDoc)
  1875. IMPLEMENT_DYNCREATE(CRichEditView, CCtrlView)
  1876. /////////////////////////////////////////////////////////////////////////////