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

2403 lines
70 KiB

  1. /* Windows Write, Copyright 1985-1992 Microsoft Corporation */
  2. /************************************************************/
  3. /*
  4. Some comments on how the Obj subsystem for handling OLE objects
  5. is herein implemented.
  6. Written (2.14.92) by v-dougk (Doug Kent)
  7. Metafile and bitmaps have always been described in the Write
  8. file format (and in memory) by a PICINFOX structure. For OLE
  9. objects the PICINFOX structure is replaced by the OBJPICINFO
  10. structure of the same size.
  11. OBJPICINFO has a field (lpObjInfo) which is a pointer to another
  12. structure (OBJINFO) which is globally allocated. This structure contains
  13. info about the object which doesn't need to be stored in the file
  14. with the object data.
  15. The object data as obtained from OleSaveToStream() is stored in the
  16. Write file format immediately following the OBJPICINFO structure.
  17. Object data is not actually written to the file until the user
  18. does a File.Save. For a newly-created object, the OBJPICINFO structure
  19. is the only data saved until that point. If the object is an
  20. unfinished one from an Insert.Object operation, then not even the
  21. OBJPICINFO structure is saved. In that case the object is solely
  22. represented by the OBJINFO structure until an OLE_CHANGED or OLE_SAVED
  23. is received. Then an OBJPICINFO structure is saved to the file.
  24. The OBJINFO structures are allocated for all objects when the file
  25. is opened. This involves writing new OBJPICINFO data to the doc
  26. (with the new lpObjInfo values) on open. This makes the doc dirty,
  27. but we reset the doc dirty flag to make it undirty.
  28. The lpObjInfo pointers are passed to the OLE create functions for the
  29. lpClient value. This is a crucial aspect of the implementation as
  30. the CallBack function passes back this pointer, allowing us to identify
  31. the object, and query and set its state without having to access
  32. the file on disk. As I was not aware of the importance (or existence)
  33. of this feature until late, it was patched in. It is not perfectly
  34. implemented and could use some polishing.
  35. As much as I tried to seamlessly integrate objects into the existing
  36. Write architecture, there have been glitches:
  37. 1) Writing into the doc as the visible page is being drawn
  38. (See ObjLoadObjectInDoc() in DisplayGraphics() in picture.c)
  39. tends to mess things up. The drawing code (namely
  40. UpdateWw(), expects certain state variables (like the
  41. pedls and the pwwds) to be constant during a single call
  42. to UpdateWw(). Writing into the doc alters those variables.
  43. Workaround uses vfInvalid in ObjSetPicInfo().
  44. 2) Write does not expect data to be entered other than in data
  45. currently being displayed on the screen, whereas we
  46. frequently operate on objects all over the doc (ObjEnumInDoc()).
  47. Workaround is to use ObjCachePara() instead of CachePara().
  48. 3) Asynchronicity wreaks havoc on Write. Since every action
  49. in Write is affected by the current state, if an action
  50. occurs out of normal sequence, i.e., occurs in an improper
  51. state, then blamo big trouble. When events happen
  52. recursively state variables cannot be restored without a
  53. state 'Pushing and Popping' mechanism.
  54. This is especially true for 'cp' variables. cp's are pointers
  55. into the document and global cp state variables are ubiguitous.
  56. Global cp variables include the selection and undo variables
  57. mentioned above, plus many others. See ObjPopParms() and
  58. ObjPushParms().
  59. While waiting for an object to release (see WMsgLoop()) we
  60. stub out WM_PAINT message responses using the nBlocking variable.
  61. Between document opening and closing we set the doc fDisplayable
  62. flag to FALSE. NOTE: we ought to do something like this in
  63. IDPromptBoxSz() -- many of the asynchronicity problems occur
  64. when calling MessageBox() (which yields).
  65. Hopefully OLE 2.0 will implement an OleBlockClient() mechanism.
  66. Notes on Asynchronicity: Ole calls that require communication with the
  67. server may be asynchronous. The following rules apply:
  68. 1) Only one asynchronous call at a time may be made for a given object.
  69. 2) The OleCreate* calls must complete before some synchronous
  70. calls such as OleGetData().
  71. 3) All asynchronous activity must be complete before calling
  72. OleRevokeClientDoc.
  73. Asynchronous calls return OLE_WAIT_FOR_RELEASE. You don't know
  74. that an asynchronous call has completed until the CallBack function
  75. receives an OLE_RELEASE message for that object. If you ignore this
  76. and issue an offending call for the object, the call will
  77. return OLE_BUSY.
  78. We deal with these rules by calling OleQueryReleaseStatus to
  79. determine whether an object is busy before making any Ole call
  80. (see ObjWaitForObject()). If the object is busy then we spin until the
  81. object is not busy. After 6 seconds we put up a msg box which, depending
  82. on the flags we set, may allow the user to stop waiting and cancel the
  83. operation (see notes in fnObjWait()).
  84. Note that the OleCreate calls can fail in 3 different ways that aren't
  85. documented (not at this point anyway).
  86. 1) The call returns error immediately. In this case you mustn't depend
  87. on the returned lpObject to be NULL. If it is not NULL, ignore
  88. it -- it needn't be deleted.
  89. 2) The call returns OLE_WAIT_FOR_RELEASE and eventually you get an
  90. OLE_RELEASE and OleQueryReleaseError() is != OLE_OK. In
  91. this case you gotta delete the lpObject that was returned by
  92. the original call.
  93. 3) The call completes, but you receive OLE_CLOSED on the object
  94. without receiving OLE_CHANGED. This indicates that for some
  95. reason the native data could not be obtained from the server.
  96. The object must be deleted in this case. Write currently
  97. handles this properly for Insert.Object (OleCreate), but not
  98. for other cases (OleCreateFromFile,...).
  99. The Links dialog should be optimized so that it doesn't require that all
  100. links be loaded at once. The Printing process could use the same
  101. optimization.
  102. Cutting, copying and pasting works as follows. Any object that exists
  103. in docScrap (where clipboard contents are stored) must be a unique
  104. object. Thus when we copy we clone any objects in the selection.
  105. When we cut we needn't clone since the objects have been deleted
  106. from the document. When we paste we clone.
  107. Deleted objects and cut objects that get purged from docScrap (and
  108. therefore become effectively deleted), are not actually deleted right
  109. away. These objects get shovelled into docUndo for access by the
  110. Undo function. Even when the objects get purged from docUndo
  111. (by another undoable operation), they are still not deleted. These
  112. objects never finally get deleted until ObjCollectGarbage() is
  113. called. This function is called on a timer about every 5 minutes.
  114. It enumerates all documents and deletes any objects not found.
  115. The reason for not deleting objects right away is that it would've
  116. hurt performance to check the contents of docUndo and docScrap
  117. every time they change. It also would've been a nasty programming job
  118. since those change points are not localized physically or logically.
  119. */
  120. #include "windows.h"
  121. #include "mw.h"
  122. #include "winddefs.h"
  123. #include "obj.h"
  124. #include "menudefs.h"
  125. #include "cmddefs.h"
  126. #include "str.h"
  127. #include "objreg.h"
  128. #include "docdefs.h"
  129. #include "editdefs.h"
  130. #include "propdefs.h"
  131. #include "wwdefs.h"
  132. #include "filedefs.h"
  133. #include <shellapi.h>
  134. #include <stdlib.h>
  135. extern struct FCB (**hpfnfcb)[];
  136. HANDLE hlpObjInfo = NULL; // array of allocated ObjInfos
  137. LPLPOBJINFO lplpObjInfo = NULL; // array of allocated ObjInfos
  138. static BOOL bSavedDoc=FALSE;
  139. static BOOL bDontFix=FALSE;
  140. int vcObjects=0; // count in doc. Note limit of 32K!!!
  141. BOOL fOleEnabled=FALSE;
  142. BOOL bKillMe=FALSE;
  143. OLECLIENTVTBL clientTbl = {NULL};
  144. OLESTREAMVTBL streamTbl;
  145. //LPOLECLIENT lpclient = NULL;
  146. LPOLESTREAM lpStream = NULL;
  147. OLECLIPFORMAT vcfLink = 0;
  148. OLECLIPFORMAT vcfOwnerLink = 0;
  149. OLECLIPFORMAT vcfNative = 0;
  150. int cOleWait = 0;
  151. HWND hwndWait=NULL;
  152. BOOL vbObjLinkOnly;
  153. int vObjVerb;
  154. OBJECTTYPE votObjSelType;
  155. ATOM aNewName=NULL;
  156. ATOM aOldName=NULL;
  157. LHCLIENTDOC lhClientDoc=NULL;
  158. BOOL bLinkProps=FALSE;
  159. FARPROC lpfnLinkProps=NULL;
  160. FARPROC lpfnInvalidLink=NULL;
  161. FARPROC lpfnInsertNew=NULL;
  162. FARPROC lpfnWaitForObject=NULL;
  163. FARPROC lpfnPasteSpecial=NULL;
  164. static BOOL WMsgLoop ( BOOL fExitOnIdle, BOOL fIgnoreInput, BOOL bOK2Cancel, LPOLEOBJECT lpObject);
  165. BOOL ObjFreeAllObjInfos();
  166. static BOOL ObjUpdateAllOpenObjects(void);
  167. int nBlocking=0; // block WM_PAINTS if > 0
  168. static int nWaitingForObject=0; // in ObjWaitForObject()
  169. int nGarbageTime=0;
  170. extern struct UAB vuab;
  171. extern HCURSOR vhcIBeam;
  172. extern HCURSOR vhcHourGlass;
  173. extern int docUndo;
  174. extern struct PAP vpapAbs;
  175. extern struct DOD (**hpdocdod)[];
  176. extern struct WWD rgwwd[];
  177. extern BOOL ferror;
  178. extern int vfDeactByOtherApp;
  179. extern HCURSOR vhcArrow;
  180. extern HANDLE hStdTargetDevice;
  181. extern typeCP cpMinCur,cpMacCur;
  182. BOOL fPropsError=FALSE;
  183. static HANDLE hobjStream = NULL;
  184. char szOPropMenuStr[21];
  185. char szPPropMenuStr[21];
  186. int ObjPlayEdit=OLEVERB_PRIMARY;
  187. int FAR PASCAL CallBack(LPOLECLIENT, OLE_NOTIFICATION, LPOLEOBJECT);
  188. /****************************************************************/
  189. /******************** STARTUP/SHUTDOWN **************************/
  190. /****************************************************************/
  191. BOOL ObjInit(HANDLE hInstance)
  192. {
  193. int bRetval=TRUE;
  194. vcfLink = RegisterClipboardFormat("ObjectLink");
  195. vcfNative = RegisterClipboardFormat("Native");
  196. vcfOwnerLink = RegisterClipboardFormat("OwnerLink");
  197. if ((clientTbl.CallBack = MakeProcInstance(CallBack, hInstance)) == NULL)
  198. goto error;
  199. if ((hobjStream = GlobalAlloc (GMEM_MOVEABLE | GMEM_ZEROINIT, sizeof(OLESTREAM))) == NULL)
  200. goto error;
  201. ObjSetTargetDevice(FALSE);
  202. if((lpStream = (LPOLESTREAM)(GlobalLock(hobjStream))) == NULL)
  203. goto error;
  204. else
  205. {
  206. lpStream->lpstbl = (LPOLESTREAMVTBL)&streamTbl;
  207. lpStream->lpstbl->Get = MakeProcInstance( (FARPROC)BufReadStream, hInstance );
  208. lpStream->lpstbl->Put = MakeProcInstance( (FARPROC)BufWriteStream, hInstance);
  209. //lpStream->lpstbl->Seek = MakeProcInstance( (FARPROC)BufPosStream, hInstance);
  210. }
  211. /* Initialize the registration database */
  212. RegInit(hINSTANCE);
  213. /* commdlg stuff */
  214. OfnInit(hInstance);
  215. /* dragdrop */
  216. DragAcceptFiles(hMAINWINDOW,TRUE);
  217. if ((hlpObjInfo = GlobalAlloc(GMEM_MOVEABLE|GMEM_ZEROINIT, sizeof(LPOBJINFO))) == NULL)
  218. goto error;
  219. if ((lplpObjInfo = (LPLPOBJINFO)GlobalLock(hlpObjInfo)) == NULL)
  220. goto error;
  221. if (LoadString(hInstance, IDSTRMenuVerb, szOPropMenuStr, sizeof(szOPropMenuStr)) == NULL)
  222. goto error;
  223. if (LoadString(hInstance, IDSTRMenuVerbP, szPPropMenuStr, sizeof(szPPropMenuStr)) == NULL)
  224. goto error;
  225. #if !defined(SMALL_OLE_UI)
  226. lpfnLinkProps = MakeProcInstance(fnProperties, hInstance);
  227. #endif
  228. lpfnInvalidLink = MakeProcInstance(fnInvalidLink, hInstance);
  229. lpfnInsertNew = MakeProcInstance(fnInsertNew, hInstance);
  230. lpfnWaitForObject = MakeProcInstance(fnObjWait, hInstance);
  231. lpfnPasteSpecial = MakeProcInstance(fnPasteSpecial, hInstance);
  232. goto end;
  233. error:
  234. bRetval = FALSE;
  235. ObjShutDown();
  236. end:
  237. return bRetval;
  238. }
  239. void ObjShutDown(void)
  240. {
  241. extern HANDLE vhMenu;
  242. #ifdef KKBUGFIX
  243. //if we close write.exe when write.exe is iconic. GetSubMenu() return NULL
  244. if (vhMenu) {
  245. HMENU hsMenu;
  246. hsMenu = GetSubMenu(vhMenu,EDIT);
  247. if(hsMenu)
  248. DeleteMenu(hsMenu, EDITMENUPOS, MF_BYPOSITION);
  249. }
  250. #else
  251. if (vhMenu)
  252. DeleteMenu(GetSubMenu(vhMenu,EDIT), EDITMENUPOS, MF_BYPOSITION);
  253. #endif
  254. if (hobjStream)
  255. {
  256. if (lpStream)
  257. if (lpStream->lpstbl)
  258. {
  259. if (lpStream->lpstbl->Get)
  260. FreeProcInstance((FARPROC)(lpStream->lpstbl->Get));
  261. if (lpStream->lpstbl->Put)
  262. FreeProcInstance((FARPROC)(lpStream->lpstbl->Put));
  263. }
  264. GlobalUnlock(hobjStream);
  265. if (lpStream)
  266. GlobalFree(hobjStream);
  267. }
  268. if (hStdTargetDevice)
  269. GlobalFree(hStdTargetDevice);
  270. if (clientTbl.CallBack)
  271. FreeProcInstance(clientTbl.CallBack);
  272. if (lpfnLinkProps)
  273. FreeProcInstance(lpfnLinkProps);
  274. if (lpfnInvalidLink)
  275. FreeProcInstance(lpfnInvalidLink);
  276. if (lpfnInsertNew)
  277. FreeProcInstance(lpfnInsertNew);
  278. if (lpfnWaitForObject)
  279. FreeProcInstance(lpfnWaitForObject);
  280. if (lpfnPasteSpecial)
  281. FreeProcInstance(lpfnPasteSpecial);
  282. RegTerm();
  283. /* dragdrop */
  284. DragAcceptFiles(hMAINWINDOW,FALSE);
  285. /* make sure all OInfo structures are freed */
  286. ObjFreeAllObjInfos();
  287. if (hlpObjInfo)
  288. GlobalFree(hlpObjInfo);
  289. }
  290. #if 0
  291. BOOL ObjFreeObjInfo(OBJPICINFO *pPicInfo)
  292. /* return whether an error */
  293. {
  294. LPLPOBJINFO lplpObjTmp;
  295. if (lpOBJ_QUERY_INFO(pPicInfo) == NULL)
  296. return FALSE;
  297. /* find slot in lplpObjInfo and NULL it out */
  298. for (lplpObjTmp = NULL; lplpObjTmp = EnumObjInfos(lplpObjTmp) ;)
  299. if (*lplpObjTmp == lpOBJ_QUERY_INFO(pPicInfo))
  300. {
  301. lpOBJ_QUERY_INFO(pPicInfo) = NULL;
  302. return ObjDeleteObjInfo(*lplpObjTmp);
  303. }
  304. Assert(0); // make sure we found it
  305. return TRUE;
  306. }
  307. #endif
  308. BOOL ObjUpdateFromObjInfo(OBJPICINFO *pPicInfo)
  309. /* update picinfo from objinfo */
  310. {
  311. char *pdumb;
  312. szOBJNAME szObjName;
  313. LPOBJINFO lpObjInfo = lpOBJ_QUERY_INFO(pPicInfo);
  314. if (lpObjInfo == NULL)
  315. return TRUE;
  316. /* we won't do data size, that'll get set in SaveObjectToDoc */
  317. /* object name */
  318. if (lpObjInfo->aObjName)
  319. {
  320. GetAtomName(lpObjInfo->aObjName,szObjName,sizeof(szObjName));
  321. pPicInfo->dwObjNum = strtoul(szObjName,&pdumb,16);
  322. }
  323. /* object type */
  324. pPicInfo->objectType = lpObjInfo->objectType;
  325. return FALSE;
  326. }
  327. BOOL ObjUpdateFromPicInfo(OBJPICINFO *pPicInfo,szOBJNAME szObjName)
  328. /* update objinfo from picInfo, return szObjName */
  329. {
  330. char *pdumb;
  331. LPOBJINFO lpObjInfo = lpOBJ_QUERY_INFO(pPicInfo);
  332. szOBJNAME szTmp;
  333. if (lpObjInfo == NULL)
  334. return TRUE;
  335. /* object name */
  336. wsprintf(szTmp, "%lx", dwOBJ_QUERY_OBJECT_NUM(pPicInfo));
  337. lpObjInfo->aObjName = AddAtom(szTmp);
  338. if (szObjName)
  339. lstrcpy(szObjName,szTmp);
  340. /* object type */
  341. lpObjInfo->objectType = pPicInfo->objectType;
  342. return FALSE;
  343. }
  344. BOOL ObjFreeObjInfoWithObject(LPOLEOBJECT lpObject)
  345. /**
  346. Given lpObject, find lpObjInfo for that object and free the ObjInfo.
  347. Return whether an error.
  348. This deletes objects if not already deleted.
  349. **/
  350. {
  351. LPLPOBJINFO lplpObjTmp;
  352. /* find slot in lplpObjInfo, free it and NULL it out */
  353. for (lplpObjTmp = NULL; lplpObjTmp = EnumObjInfos(lplpObjTmp) ;)
  354. {
  355. if ((*lplpObjTmp)->lpobject == lpObject)
  356. return ObjDeleteObjInfo(*lplpObjTmp);
  357. }
  358. #ifdef DEBUG
  359. OutputDebugString("No OInfo for object\n\r");
  360. #endif
  361. return TRUE;
  362. }
  363. BOOL ObjDeleteObjInfo(LPOBJINFO lpOInfo)
  364. {
  365. WORD wSegment;
  366. HANDLE hInfo;
  367. LPLPOBJINFO lplpObjTmp;
  368. #ifdef DEBUG
  369. OutputDebugString( (LPSTR) "Nulling objinfo slot.\n\r");
  370. #endif
  371. /* find slot in lplpObjInfo, free it and NULL it out */
  372. for (lplpObjTmp = NULL; lplpObjTmp = EnumObjInfos(lplpObjTmp) ;)
  373. {
  374. if (*lplpObjTmp == lpOInfo)
  375. break;
  376. }
  377. if (lplpObjTmp == NULL) // hmmm, shouldn't happen
  378. {
  379. Assert(0);
  380. return NULL;
  381. }
  382. if ((*lplpObjTmp)->aName)
  383. DeleteAtom((*lplpObjTmp)->aName);
  384. if ((*lplpObjTmp)->aObjName)
  385. DeleteAtom((*lplpObjTmp)->aObjName);
  386. wSegment = HIWORD(((DWORD)*lplpObjTmp));
  387. *lplpObjTmp = NULL;
  388. hInfo = GlobalHandle(wSegment) & 0xFFFFL;
  389. return (BOOL)GlobalFree(hInfo);
  390. }
  391. LPOBJINFO ObjGetObjInfo(szOBJNAME szObjName)
  392. /* allocate objinfo, return szObjName if !NULL */
  393. {
  394. HANDLE hObjInfo=NULL;
  395. DWORD dwCount,dwCountSave;
  396. LPLPOBJINFO lplpObjTmp;
  397. LPOBJINFO lpObjInfoNew=NULL;
  398. if (lplpObjInfo == NULL)
  399. return NULL;
  400. if ((hObjInfo = GlobalAlloc(GMEM_MOVEABLE|GMEM_ZEROINIT, sizeof(OBJINFO))) == NULL)
  401. {
  402. /* gotta recover better here !!! (7.5.91) v-dougk */
  403. Error(IDPMTCantRunM);
  404. return NULL;
  405. }
  406. if ((lpObjInfoNew = (LPOBJINFO)GlobalLock(hObjInfo)) == NULL)
  407. {
  408. /* gotta recover better here !!! (7.5.91) v-dougk */
  409. Error(IDPMTCantRunM);
  410. GlobalFree(hObjInfo);
  411. return NULL;
  412. }
  413. /* now add to lplpObjInfo array */
  414. dwCount = dwCountSave = GlobalSize(hlpObjInfo) / sizeof(LPLPOBJINFO);
  415. /* find if any NULL slots */
  416. for (lplpObjTmp = lplpObjInfo; dwCount ; --dwCount, ++lplpObjTmp)
  417. if (*lplpObjTmp == NULL)
  418. break;
  419. if (dwCount) // then found a NULL slot
  420. {
  421. #ifdef DEBUG
  422. OutputDebugString( (LPSTR) "Adding objinfo to empty slot.\n\r");
  423. #endif
  424. *lplpObjTmp = lpObjInfoNew;
  425. }
  426. else // gotta reallocate
  427. {
  428. ++dwCountSave;
  429. if ((hlpObjInfo = GlobalRealloc(hlpObjInfo,dwCountSave * sizeof(LPLPOBJINFO),GMEM_MOVEABLE|GMEM_ZEROINIT)) == NULL)
  430. {
  431. /* gotta recover better here !!! (7.5.91) v-dougk */
  432. GlobalFree(hObjInfo);
  433. lplpObjInfo = NULL;
  434. Error(IDPMTCantRunM);
  435. return NULL;
  436. }
  437. if ((lplpObjInfo = (LPLPOBJINFO)GlobalLock(hlpObjInfo)) == NULL)
  438. {
  439. /* gotta recover better here !!! (7.5.91) v-dougk */
  440. GlobalFree(hObjInfo);
  441. Error(IDPMTCantRunM);
  442. return NULL;
  443. }
  444. #ifdef DEBUG
  445. OutputDebugString( (LPSTR) "Adding objinfo to new slot.\n\r");
  446. #endif
  447. /* put new gal in last slot */
  448. lplpObjInfo[dwCountSave-1] = lpObjInfoNew;
  449. }
  450. if (szObjName)
  451. /* make object's unique name */
  452. ObjMakeObjectName(lpObjInfoNew, szObjName);
  453. else
  454. lpObjInfoNew->aObjName = NULL;
  455. /* this is a requirement of the OLECLIENT structure */
  456. lpObjInfoNew->lpvtbl = (LPOLECLIENTVTBL)&clientTbl;
  457. return lpObjInfoNew;
  458. }
  459. BOOL ObjAllocObjInfo(OBJPICINFO *pPicInfo, typeCP cpParaStart, OBJECTTYPE ot, BOOL bInitPicinfo, szOBJNAME szObjName)
  460. /* return whether an error */
  461. {
  462. if (lpOBJ_QUERY_INFO(pPicInfo) = ObjGetObjInfo(NULL))
  463. {
  464. //cpOBJ_QUERY_WHERE(pPicInfo) = cpParaStart;
  465. if (bInitPicinfo)
  466. /* new object, make a new szObjName, use passed-in ot */
  467. {
  468. /* get new values and put into ObjInfo */
  469. if (szObjName)
  470. ObjMakeObjectName(lpOBJ_QUERY_INFO(pPicInfo), szObjName);
  471. lpOBJ_QUERY_INFO(pPicInfo)->objectType = ot;
  472. /* this'll take values from ObjInfo and put into PicInfo */
  473. return GimmeNewPicinfo(pPicInfo,lpOBJ_QUERY_INFO(pPicInfo));
  474. }
  475. else // picInfo already has values. Put 'em into ObjInfo
  476. {
  477. ObjUpdateFromPicInfo(pPicInfo,szObjName);
  478. return FALSE;
  479. }
  480. }
  481. else
  482. return TRUE;
  483. }
  484. BOOL ObjCloneObjInfo(OBJPICINFO *pPicInfo, typeCP cpParaStart, szOBJNAME szObjName)
  485. /* clone pPicInfo->lpObjInfo */
  486. {
  487. if (ObjCopyObjInfo(lpOBJ_QUERY_INFO(pPicInfo),
  488. &lpOBJ_QUERY_INFO(pPicInfo),
  489. szObjName))
  490. return TRUE;
  491. return FALSE;
  492. }
  493. BOOL ObjCopyObjInfo(LPOBJINFO lpOldObjInfo,
  494. LPLPOBJINFO lplpNewObjInfo,
  495. szOBJNAME szObjName)
  496. /* lpobject field is NULL'd! Atoms are cloned, new unique object name */
  497. {
  498. char szTmp[180];
  499. if (lplpObjInfo == NULL)
  500. return TRUE;
  501. if ((*lplpNewObjInfo = ObjGetObjInfo(NULL)) == NULL)
  502. return TRUE;
  503. /* copy old stuff over to new */
  504. /* note new will inherit old lpclone if there is one */
  505. **lplpNewObjInfo = *lpOldObjInfo;
  506. (*lplpNewObjInfo)->lpobject = NULL;
  507. /* clone the utility (class or whatever) name */
  508. if (lpOldObjInfo->aName)
  509. {
  510. GetAtomName(lpOldObjInfo->aName,szTmp,sizeof(szTmp));
  511. (*lplpNewObjInfo)->aName = AddAtom(szTmp);
  512. }
  513. /* make a new ObjName if requested */
  514. if (szObjName)
  515. /* make object's unique name */
  516. ObjMakeObjectName(*lplpNewObjInfo, szObjName);
  517. else
  518. (*lplpNewObjInfo)->aObjName = NULL;
  519. return FALSE;
  520. }
  521. BOOL ObjFreeAllObjInfos()
  522. /**
  523. Return whether an error.
  524. **/
  525. {
  526. WORD wSegment;
  527. HANDLE hInfo;
  528. DWORD dwCount;
  529. LPLPOBJINFO lplpObjTmp;
  530. if (lplpObjInfo == NULL)
  531. return FALSE;
  532. /* find slot in lplpObjInfo, free it and NULL it out */
  533. dwCount = GlobalSize(hlpObjInfo) / sizeof(LPLPOBJINFO);
  534. for (lplpObjTmp = lplpObjInfo; dwCount ; --dwCount, ++lplpObjTmp)
  535. {
  536. if (*lplpObjTmp)
  537. {
  538. #ifdef DEBUG
  539. OutputDebugString( (LPSTR) "Nulling objinfo slot (from object).\n\r");
  540. #endif
  541. /** Delete info about this object. This assumes picinfo will not
  542. be reused unless via file.open. **/
  543. if ((*lplpObjTmp)->aName)
  544. DeleteAtom((*lplpObjTmp)->aName);
  545. if ((*lplpObjTmp)->aObjName)
  546. DeleteAtom((*lplpObjTmp)->aObjName);
  547. wSegment = HIWORD(((DWORD)*lplpObjTmp));
  548. *lplpObjTmp = NULL;
  549. hInfo = GlobalHandle(wSegment) & 0xFFFF;
  550. GlobalFree(hInfo);
  551. }
  552. }
  553. return FALSE;
  554. }
  555. LPLPOBJINFO EnumObjInfos(LPLPOBJINFO lplpObjInfoPrev)
  556. {
  557. LPLPOBJINFO lplpOIMax;
  558. if (lplpObjInfo == NULL)
  559. return NULL;
  560. if (lplpObjInfoPrev == NULL) // starting out
  561. lplpObjInfoPrev = lplpObjInfo;
  562. else
  563. ++lplpObjInfoPrev;
  564. lplpOIMax = (LPLPOBJINFO)((LPSTR)lplpObjInfo + GlobalSize(hlpObjInfo));
  565. for ( ; lplpObjInfoPrev < lplpOIMax ; ++lplpObjInfoPrev)
  566. {
  567. if (*lplpObjInfoPrev == NULL)
  568. continue;
  569. else
  570. return lplpObjInfoPrev;
  571. }
  572. return NULL;
  573. }
  574. void ObjCollectGarbage()
  575. {
  576. LPLPOBJINFO lplpObjTmp;
  577. int nObjCount=0,doc;
  578. if (nBlocking)
  579. return;
  580. StartLongOp();
  581. #ifdef DEBUG
  582. OutputDebugString("Collecting Garbage...\n\r");
  583. #endif
  584. ObjPushParms(docCur);
  585. /* mark all as not in doc */
  586. for (lplpObjTmp = NULL; lplpObjTmp = EnumObjInfos(lplpObjTmp) ;)
  587. {
  588. ++nObjCount;
  589. (*lplpObjTmp)->fInDoc = FALSE;
  590. }
  591. if (nObjCount == 0)
  592. goto end;
  593. /* mark in doc if in */
  594. /* go through all the docs */
  595. for (doc = 0; doc < docMac; doc++)
  596. {
  597. OBJPICINFO picInfo;
  598. typeCP cpPicInfo;
  599. if ((doc != docNil) && (**hpdocdod)[doc].hpctb)
  600. for (cpPicInfo = cpNil;
  601. ObjPicEnumInRange(&picInfo,doc,cp0,CpMacText(doc),&cpPicInfo);
  602. )
  603. {
  604. if (lpOBJ_QUERY_INFO(&picInfo) == NULL)
  605. continue;
  606. fOBJ_INDOC(&picInfo) = TRUE;
  607. }
  608. }
  609. /* if not in doc delete */
  610. for (lplpObjTmp = NULL; lplpObjTmp = EnumObjInfos(lplpObjTmp) ;)
  611. {
  612. if (!(*lplpObjTmp)->fInDoc)
  613. ObjDeleteObject(*lplpObjTmp,TRUE);
  614. }
  615. end:
  616. /* 'til next time... */
  617. nGarbageTime=0;
  618. ObjPopParms(TRUE);
  619. EndLongOp(vhcArrow);
  620. }
  621. /****************************************************************/
  622. /********************** OLE DOCUMENT FUNCTIONS ******************/
  623. /****************************************************************/
  624. BOOL ObjClosingDoc(int docOld,LPSTR szNewDocName)
  625. /**
  626. Clone objects in docScrap from docOld into szNewDocName.
  627. Release all objects in docOld.
  628. Return whether an error occurred. Error indicates to caller that
  629. a new document cannot be opened. No error indicates to caller
  630. that a new document *must* be reopened via ObjOpenedDoc() (because
  631. all lpObjInfos have been deleted).
  632. If szNewDocName is NULL then don't make a new doc.
  633. Important point to remember is that for objects whose data has never
  634. been saved, there is no recovery from release/delete in an error
  635. condition. There is no state to which we can recover in an
  636. error condition. This function should not be allowed to exit
  637. on the basis of a busy object.
  638. **/
  639. {
  640. char szTitle[120];
  641. BOOL bRetval=FALSE;
  642. OBJPICINFO picInfo;
  643. typeCP cpPicInfo;
  644. extern int vfScrapIsPic;
  645. extern int docScrap;
  646. extern int vfOwnClipboard;
  647. extern CHAR szUntitled[];
  648. LPOBJINFO lpObjInfo;
  649. LPOLEOBJECT lpObject;
  650. LHCLIENTDOC lhNewClientDoc=NULL;
  651. OLESTATUS olestat;
  652. LPLPOBJINFO lplpObjTmp;
  653. if (!lhClientDoc)
  654. return FALSE;
  655. #ifdef DEBUG
  656. OutputDebugString( (LPSTR) "Closing Doc\n\r");
  657. #endif
  658. /* ensure all async ops are complete */
  659. if (FinishAllAsyncs(TRUE))
  660. {
  661. UPDATE_INVALID();
  662. return TRUE;
  663. }
  664. ++nBlocking; // inhibit repaints
  665. /* drag drop */
  666. DragAcceptFiles(hDOCWINDOW,FALSE);
  667. if (szNewDocName)
  668. /* then we'll be opening another doc soon, so plan ahead */
  669. {
  670. if (!szNewDocName[0])
  671. lstrcpy((LPSTR)szTitle,(LPSTR)szUntitled);
  672. else
  673. lstrcpy((LPSTR)szTitle,szNewDocName);
  674. if (ObjError(OleRegisterClientDoc(szDOCCLASS,szTitle,0L,&lhNewClientDoc)))
  675. {
  676. lhNewClientDoc = NULL; // just in case OLE flatlined
  677. goto error;
  678. }
  679. /**
  680. Clone any objects from docScrap into new doc. Since this
  681. requires knowing the name of the new doc you
  682. would think it should go into ObjOpeningDoc, but it can't.
  683. You have to do it *before* releasing the objects below!
  684. (Because OleEnumObjects keys off of the lhClientDoc).
  685. **/
  686. ObjCloneScrapToNewDoc(lhNewClientDoc);
  687. /* ensure all async ops are complete */
  688. if (FinishAllAsyncs(FALSE))
  689. goto error;
  690. }
  691. /* release objects in docOld (assume not busy) */
  692. if ((**hpdocdod)[docOld].fFormatted)
  693. {
  694. /* objects are being released/deleted, don't allow display */
  695. (**hpdocdod)[docOld].fDisplayable = FALSE;
  696. for (cpPicInfo = cpNil;
  697. ObjPicEnumInRange(&picInfo,docOld,cp0,CpMacText(docOld),&cpPicInfo);
  698. )
  699. {
  700. lpObjInfo=lpOBJ_QUERY_INFO(&picInfo);
  701. if (lpObjInfo == NULL)
  702. continue;
  703. if (lpObjInfo->lpobject == NULL)
  704. continue;
  705. #ifdef DEBUG
  706. OutputDebugString( (LPSTR) "Releasing object\n\r");
  707. #endif
  708. switch (olestat = OleRelease(lpObjInfo->lpobject))
  709. {
  710. case OLE_OK:
  711. lpObjInfo->lpobject = NULL;
  712. break;
  713. case OLE_WAIT_FOR_RELEASE:
  714. lpObjInfo->fCompleteAsync = TRUE;
  715. /* No cancel allowed! */
  716. if (ObjWaitForObject(lpObjInfo,FALSE))
  717. goto error;
  718. else
  719. lpObjInfo->lpobject = NULL;
  720. break;
  721. }
  722. }
  723. }
  724. if (FinishAllAsyncs(FALSE)) // necessary?
  725. goto error;
  726. /* delete remaining objects in lhClientDoc (assume not busy) */
  727. lpObject=NULL;
  728. do
  729. {
  730. lpObject=NULL;
  731. OleEnumObjects(lhClientDoc,&lpObject);
  732. if (lpObject)
  733. {
  734. lpObjInfo = GetObjInfo(lpObject);
  735. switch (olestat = OleDelete(lpObject))
  736. {
  737. case OLE_OK:
  738. if (lpObjInfo)
  739. lpObjInfo->lpobject = NULL;
  740. break;
  741. case OLE_WAIT_FOR_RELEASE:
  742. if (lpObjInfo)
  743. {
  744. lpObjInfo->fCompleteAsync = TRUE;
  745. /* no cancel allowed! */
  746. if (ObjWaitForObject(lpObjInfo,FALSE))
  747. goto error;
  748. else
  749. lpObjInfo->lpobject = NULL;
  750. }
  751. else
  752. FinishUp();
  753. break;
  754. default:
  755. ObjError(olestat);
  756. goto error;
  757. }
  758. }
  759. }
  760. while (lpObject);
  761. /* say goodbye to old doc if there was one */
  762. if (!bSavedDoc)
  763. {
  764. ObjRevertedDoc();
  765. bSavedDoc=FALSE;
  766. }
  767. #ifdef DEBUG
  768. OutputDebugString("Revoking doc\n\r");
  769. #endif
  770. if (ObjError(OleRevokeClientDoc(lhClientDoc)))
  771. goto error;
  772. lhClientDoc = lhNewClientDoc;
  773. /**
  774. Delete all the lpObjInfos having NULL lpobjects (non-NULLs
  775. belong to docScrap). Make sure that ObjOpenedDoc is
  776. called if this doc is reopened. This is the point of no return.
  777. **/
  778. for (lplpObjTmp = NULL; lplpObjTmp = EnumObjInfos(lplpObjTmp) ;)
  779. if ((*lplpObjTmp)->lpobject == NULL)
  780. ObjDeleteObjInfo(*lplpObjTmp);
  781. goto end;
  782. error:
  783. bRetval = TRUE;
  784. if (lhNewClientDoc)
  785. {
  786. OleRevokeClientDoc(lhNewClientDoc);
  787. /**
  788. If any objects are in the scrap, they must be made to belong to
  789. lhClientDoc, or be deleted.
  790. **/
  791. ObjCloneScrapToNewDoc(lhClientDoc);
  792. }
  793. end:
  794. --nBlocking; // reenable repaints
  795. UPDATE_INVALID(); // let WM_PAINTS get through now that we're no longer blocking
  796. return bRetval;
  797. }
  798. BOOL ObjOpenedDoc(int doc)
  799. /*
  800. Return whether error that precludes continuing with opening this doc.
  801. If an error, then a new doc *must* be opened via ObjOpenedDoc(). We
  802. will clean this doc up to closed state if couldn't open it (ie,
  803. delete all objects and lpObjInfos).
  804. */
  805. {
  806. BOOL bRetval=FALSE,bLinkError=FALSE;
  807. OBJPICINFO picInfo;
  808. typeCP cpPicInfo;
  809. BOOL bPrompted=FALSE;
  810. char szMsg[cchMaxSz];
  811. extern CHAR szUntitled[];
  812. #ifdef DEBUG
  813. OutputDebugString( (LPSTR) "Opened Doc\n\r");
  814. #endif
  815. /* prevent display until done updating */
  816. ++nBlocking;
  817. StartLongOp();
  818. /* start collection timer all over */
  819. nGarbageTime=0;
  820. /* in case contains any picInfos left over from previous doc */
  821. ClobberDoc(docUndo,docNil,cp0,cp0);
  822. /* drag drop */
  823. DragAcceptFiles(hDOCWINDOW,TRUE);
  824. if (!lhClientDoc)
  825. if (ObjError(OleRegisterClientDoc(szDOCCLASS,szUntitled,0L,&lhClientDoc)))
  826. goto error;
  827. /* Do this first because much code assumes every picInfo has an
  828. lpObjInfo associated with it */
  829. if ((**hpdocdod)[doc].fFormatted)
  830. {
  831. for (cpPicInfo = cpNil;
  832. ObjPicEnumInRange(&picInfo,doc,cp0,CpMacText(doc),&cpPicInfo);
  833. )
  834. {
  835. if (ObjAllocObjInfo(&picInfo,cpPicInfo,picInfo.objectType,FALSE,NULL))
  836. goto error; // this is a real problem condition
  837. /* note this makes doc dirty right off the bat, but gotta do it because
  838. we gotta save ObjInfo handle in doc. (8.20.91) v-dougk */
  839. if (ObjSetPicInfo(&picInfo, doc, cpPicInfo))
  840. goto error;
  841. }
  842. /* OK to display. Any error hereafter is not fatal. */
  843. (**hpdocdod)[doc].fDisplayable = TRUE;
  844. #if !defined(SMALL_OLE_UI)
  845. /**** Now see if links need updating ****/
  846. bDontFix=TRUE; // don't bring up change links on release error
  847. if ((**hpdocdod)[doc].fFormatted)
  848. for (cpPicInfo = cpNil;
  849. ObjPicEnumInRange(&picInfo,doc,cp0,CpMacText(doc),&cpPicInfo);
  850. )
  851. {
  852. if (otOBJ_QUERY_TYPE(&picInfo) == LINK)
  853. if (ObjLoadObjectInDoc(&picInfo, doc, cpPicInfo) != cp0)
  854. {
  855. OLESTATUS olestat;
  856. if (!bPrompted)
  857. {
  858. LoadString(hINSTANCE, IDSTRUpdateObject, szMsg, sizeof(szMsg));
  859. if (MessageBox(hPARENTWINDOW, (LPSTR)szMsg, (LPSTR)szAppName, MB_YESNO|MB_ICONEXCLAMATION) == IDYES)
  860. bPrompted = TRUE;
  861. else
  862. break; // no updating requested
  863. }
  864. #ifdef DEBUG
  865. OutputDebugString( (LPSTR) "Updating link\n\r");
  866. #endif
  867. if (ObjError(OleUpdate(lpOBJ_QUERY_OBJECT(&picInfo))))
  868. {
  869. bLinkError = TRUE;
  870. fOBJ_BADLINK(&picInfo) = TRUE; // in case didn't get release, gotta set
  871. ferror = FALSE; // to reenable error messages
  872. }
  873. }
  874. else /* if load object failed, then give up */
  875. {
  876. bLinkError = FALSE; /* Don't put up Links dialog */
  877. goto end; /* Not fatal, though they need to reopen doc
  878. after freeing memory */
  879. }
  880. }
  881. bDontFix=FALSE;
  882. #endif
  883. }
  884. else /* OK to display. */
  885. (**hpdocdod)[doc].fDisplayable = TRUE;
  886. goto end;
  887. error:
  888. bRetval = TRUE;
  889. end:
  890. if (bLinkError)
  891. /* fix 'em */
  892. {
  893. if (DialogBox(hINSTANCE, "DTINVALIDLINKS",
  894. hPARENTWINDOW, lpfnInvalidLink) == IDD_CHANGE)
  895. fnObjProperties();
  896. }
  897. --nBlocking;
  898. EndLongOp(vhcArrow);
  899. UPDATE_INVALID(); // let WM_PAINTS get through now that we're no longer blocking
  900. return bRetval;
  901. }
  902. BOOL ObjSavingDoc(BOOL bFormatted)
  903. /* return whether there was an error */
  904. {
  905. /* drag drop */
  906. DragAcceptFiles(hDOCWINDOW,FALSE);
  907. /* update any other objects */
  908. vcObjects = 0;
  909. if (bFormatted)
  910. vcObjects = ObjEnumInDoc(docCur,ObjSaveObjectToDoc);
  911. return (vcObjects < 0); // return whether error
  912. }
  913. void ObjSavedDoc(void)
  914. {
  915. #ifdef DEBUG
  916. OutputDebugString( (LPSTR) "Saved Doc\n\r");
  917. #endif
  918. if (lhClientDoc)
  919. ObjError(OleSavedClientDoc(lhClientDoc));
  920. bSavedDoc=TRUE;
  921. /* drag drop */
  922. DragAcceptFiles(hDOCWINDOW,TRUE);
  923. }
  924. static BOOL ObjUpdateAllOpenObjects(void)
  925. /* Update all open embedded objects. Return whether successful.
  926. Called on file.close. */
  927. {
  928. OBJPICINFO picInfo;
  929. typeCP cpPicInfo;
  930. BOOL bRetval=TRUE;
  931. StartLongOp();
  932. for (cpPicInfo = cpNil;
  933. ObjPicEnumInRange(&picInfo,docCur,cp0,CpMacText(docCur),&cpPicInfo);
  934. )
  935. {
  936. if (((otOBJ_QUERY_TYPE(&picInfo) == NONE) ||
  937. (otOBJ_QUERY_TYPE(&picInfo) == EMBEDDED)) &&
  938. (OleQueryOpen(lpOBJ_QUERY_OBJECT(&picInfo)) == OLE_OK))
  939. {
  940. fnObjUpdate(lpOBJ_QUERY_INFO(&picInfo));
  941. }
  942. }
  943. end:
  944. /* To make sure we've gotten all messages relevant to calling
  945. ObjObjectHasChanged() and setting doc dirty. */
  946. if (FinishAllAsyncs(TRUE))
  947. bRetval = FALSE;
  948. EndLongOp(vhcArrow);
  949. UPDATE_INVALID(); // let WM_PAINTS get through now that we're no longer blocking
  950. return bRetval;
  951. }
  952. BOOL CloseUnfinishedObjects(BOOL bSaving)
  953. /**
  954. Used with File.Save or File.Exit.
  955. Return TRUE whether should proceed with Save/Exit.
  956. **/
  957. {
  958. char szMsg[cchMaxSz];
  959. if (ObjContainsOpenEmb(docCur, cp0, CpMacText(docCur),TRUE))
  960. {
  961. if (bSaving)
  962. LoadString(hINSTANCE, IDPMTSaveOpenEmb, szMsg, sizeof(szMsg));
  963. else
  964. LoadString(hINSTANCE, IDPMTExitOpenEmb, szMsg, sizeof(szMsg));
  965. switch (MessageBox(hPARENTWINDOW, (LPSTR)szMsg, (LPSTR)szAppName, MB_YESNOCANCEL))
  966. {
  967. case IDYES:
  968. return ObjUpdateAllOpenObjects();
  969. case IDNO:
  970. default:
  971. return TRUE;
  972. case IDCANCEL:
  973. return FALSE;
  974. }
  975. }
  976. return TRUE;
  977. }
  978. void ObjRenamedDoc(LPSTR szNewName)
  979. {
  980. OBJPICINFO picInfo;
  981. typeCP cpPicInfo;
  982. #ifdef DEBUG
  983. OutputDebugString( (LPSTR) "Renamed Doc\n\r");
  984. #endif
  985. if (lhClientDoc)
  986. ObjError(OleRenameClientDoc(lhClientDoc,szNewName));
  987. /* don't need to do all docs since objects can only be active in docCur */
  988. for (cpPicInfo = cpNil;
  989. ObjPicEnumInRange(&picInfo,docCur,cp0,CpMacText(docCur),&cpPicInfo);
  990. )
  991. /* ignore return value on purpose */
  992. ObjSetHostNameInDoc(&picInfo,docCur,cpPicInfo);
  993. }
  994. void ObjRevertedDoc()
  995. {
  996. #ifdef DEBUG
  997. OutputDebugString( (LPSTR) "Reverted Doc\n\r");
  998. #endif
  999. if (lhClientDoc)
  1000. ObjError(OleRevertClientDoc(lhClientDoc));
  1001. }
  1002. /****************************************************************/
  1003. /*********************** ERROR HANDLING *************************/
  1004. /****************************************************************/
  1005. BOOL FAR
  1006. ObjError(OLESTATUS olestat)
  1007. {
  1008. register HWND hWndParent = hPARENTWINDOW;
  1009. switch (olestat) {
  1010. case OLE_WAIT_FOR_RELEASE:
  1011. case OLE_OK:
  1012. return FALSE;
  1013. }
  1014. ferror = FALSE; // to enable error message
  1015. switch (olestat) {
  1016. case OLE_ERROR_LAUNCH:
  1017. Error(IDPMTFailedToLaunchServer);
  1018. break;
  1019. case OLE_ERROR_COMM:
  1020. Error(IDPMTFailedToCommWithServer);
  1021. break;
  1022. case OLE_ERROR_MEMORY:
  1023. Error(IDPMTWinFailure);
  1024. break;
  1025. case OLE_BUSY:
  1026. Error(IDPMTServerBusy);
  1027. break;
  1028. case OLE_ERROR_FORMAT:
  1029. Error(IDPMTFormat);
  1030. break;
  1031. case OLE_ERROR_DRAW:
  1032. Error(IDPMTFailedToDraw);
  1033. break;
  1034. default:
  1035. #ifdef DEBUG
  1036. ObjPrintError(olestat,FALSE);
  1037. #endif
  1038. Error(IDPMTOLEError);
  1039. break;
  1040. }
  1041. return TRUE;
  1042. }
  1043. void
  1044. ObjReleaseError(OLE_RELEASE_METHOD rm)
  1045. /*
  1046. There's an async problem here, in that the posted message
  1047. that invokes this routine may be allowed through by Error() which
  1048. isn't reentrant (or at least causes problems when called recursively).
  1049. */
  1050. {
  1051. register HWND hWndParent = hPARENTWINDOW;
  1052. switch (rm) {
  1053. case OLE_DELETE:
  1054. Error(IDPMTFailedToDeleteObject);
  1055. break;
  1056. case OLE_LOADFROMSTREAM:
  1057. Error(IDPMTFailedToReadObject);
  1058. break;
  1059. case OLE_LNKPASTE:
  1060. case OLE_EMBPASTE:
  1061. Error(IDPMTGetFromClipboardFailed);
  1062. break;
  1063. case OLE_ACTIVATE:
  1064. Error(IDPMTFailedToLaunchServer);
  1065. break;
  1066. case OLE_UPDATE:
  1067. Error(IDPMTFailedToUpdate);
  1068. break;
  1069. case OLE_CREATE:
  1070. case OLE_CREATELINKFROMFILE:
  1071. case OLE_CREATEFROMFILE:
  1072. Error(IDPMTFailedToCreateObject);
  1073. break;
  1074. case OLE_SETUPDATEOPTIONS:
  1075. Error(IDPMTImproperLinkOptionsError);
  1076. break;
  1077. default:
  1078. Error(IDPMTOLEError);
  1079. break;
  1080. }
  1081. #ifdef DEBUG
  1082. ObjPrintError(rm,TRUE);
  1083. #endif
  1084. }
  1085. #ifdef DEBUG
  1086. void ObjPrintError(WORD olestat, BOOL bRelease)
  1087. {
  1088. #define szMsgMax 100
  1089. char szError[szMsgMax];
  1090. if (!bRelease)
  1091. wsprintf(szError, "***Ole Error #%d\n\r",olestat);
  1092. else
  1093. wsprintf(szError, "***Ole Release Error on Method #%d\n\r",olestat);
  1094. OutputDebugString(szError);
  1095. }
  1096. #endif
  1097. /****************************************************************/
  1098. /***************** ASYNCRONICITY HANDLING ***********************/
  1099. /****************************************************************/
  1100. int FAR PASCAL
  1101. CallBack(LPOLECLIENT lpclient,
  1102. OLE_NOTIFICATION flags,
  1103. LPOLEOBJECT lpObject)
  1104. {
  1105. extern int vdocParaCache;
  1106. LPOBJINFO lpOInfo = (LPOBJINFO)lpclient;
  1107. switch(flags)
  1108. {
  1109. case OLE_SAVED:
  1110. case OLE_CLOSED:
  1111. case OLE_CHANGED:
  1112. /**
  1113. Post a message instead of process here because we have to return from
  1114. CallBack before making any other OLE calls.
  1115. **/
  1116. #ifdef DEBUG
  1117. OutputDebugString(flags == OLE_CHANGED ? "received OLE_CHANGED\n\r" :
  1118. flags == OLE_SAVED ? "received OLE_SAVED\n\r" :
  1119. "received OLE_CLOSED\n\r");
  1120. #endif
  1121. PostMessage(hDOCWINDOW,WM_OBJUPDATE,flags,(DWORD)lpOInfo);
  1122. break;
  1123. case OLE_RELEASE:
  1124. {
  1125. OLE_RELEASE_METHOD ReleaseMethod = OleQueryReleaseMethod(lpObject);
  1126. if (!CheckPointer((LPSTR)lpOInfo,1))
  1127. return FALSE;
  1128. lpOInfo->fKillMe = FALSE; // pending async is dead
  1129. if (lpOInfo->fDeleteMe && (ReleaseMethod != OLE_DELETE)) // not dead enough
  1130. {
  1131. PostMessage(hDOCWINDOW,WM_OBJDELETE,1,(DWORD)lpOInfo);
  1132. return FALSE; // error message will already have been given
  1133. }
  1134. if (lpOInfo->fReleaseMe && (ReleaseMethod != OLE_DELETE)) // not dead enough
  1135. {
  1136. PostMessage(hDOCWINDOW,WM_OBJDELETE,0,(DWORD)lpOInfo);
  1137. return FALSE; // error message will already have been given
  1138. }
  1139. if (lpOInfo->fFreeMe && (ReleaseMethod == OLE_DELETE)) // on OLE_DELETE release
  1140. {
  1141. ObjDeleteObjInfo(lpOInfo);
  1142. return FALSE;
  1143. }
  1144. if (OleQueryReleaseError(lpObject) == OLE_OK)
  1145. {
  1146. switch (ReleaseMethod)
  1147. {
  1148. case OLE_SETUPDATEOPTIONS:
  1149. {
  1150. if (bLinkProps) // we're in Link Properties dialog
  1151. {
  1152. PostMessage(hPARENTWINDOW, WM_UPDATELB, 0, 0L);
  1153. PostMessage(hPARENTWINDOW, WM_COMMAND, IDD_REFRESH, (DWORD)lpOInfo);
  1154. }
  1155. }
  1156. break;
  1157. case OLE_UPDATE:
  1158. ObjInvalidateObj(lpObject);
  1159. break;
  1160. case OLE_DELETE: // get this for delete and release
  1161. lpOInfo->lpobject = NULL;
  1162. break;
  1163. }
  1164. }
  1165. else // release error != OLE_OK
  1166. {
  1167. #ifdef DEBUG
  1168. PostMessage(hDOCWINDOW,WM_OBJERROR,ReleaseMethod,0L);
  1169. #endif
  1170. switch(ReleaseMethod)
  1171. {
  1172. case OLE_CREATE:
  1173. case OLE_CREATELINKFROMFILE:
  1174. case OLE_CREATEFROMFILE:
  1175. /*
  1176. OleQueryReleaseError won't help us after callback returns
  1177. so this is how we tell that the object wasn't created.
  1178. */
  1179. lpOInfo->fDeleteMe = TRUE;
  1180. // creator should ObjDeleteObject and issue error message
  1181. break;
  1182. default:
  1183. switch (OleQueryReleaseError(lpObject))
  1184. {
  1185. case OLE_ERROR_OPEN:
  1186. case OLE_ERROR_ADVISE_NATIVE:
  1187. case OLE_ERROR_ADVISE_PICT:
  1188. case OLE_ERROR_REQUEST_NATIVE:
  1189. case OLE_ERROR_REQUEST_PICT:
  1190. /**
  1191. Post a message instead of process here because we have to return from
  1192. CallBack before making any other OLE calls.
  1193. **/
  1194. if (lpOInfo->objectType == LINK)
  1195. lpOInfo->fBadLink = TRUE;
  1196. if (bLinkProps)
  1197. fPropsError = TRUE; // so linkprops knows there was a problem
  1198. else if (!bDontFix && (lpOInfo->objectType == LINK))
  1199. PostMessage(hDOCWINDOW,WM_OBJBADLINK,OleQueryReleaseMethod(lpObject),(DWORD)lpObject);
  1200. break;
  1201. }
  1202. break;
  1203. }
  1204. }
  1205. }
  1206. break;
  1207. case OLE_QUERY_RETRY:
  1208. {
  1209. Assert(CheckPointer((LPSTR)lpOInfo,1));
  1210. if (lpOInfo->fKillMe)
  1211. {
  1212. lpOInfo->fKillMe = FALSE;
  1213. return FALSE;
  1214. }
  1215. else if (hwndWait)
  1216. PostMessage(hwndWait,WM_UKANKANCEL,0,0L);
  1217. else if (nWaitingForObject == 0)
  1218. PostMessage(hDOCWINDOW,WM_WAITFORSERVER,TRUE,(DWORD)lpOInfo);
  1219. return TRUE;
  1220. }
  1221. break;
  1222. case OLE_QUERY_PAINT:
  1223. return TRUE;
  1224. break;
  1225. default:
  1226. break;
  1227. }
  1228. return FALSE;
  1229. }
  1230. void ObjObjectHasChanged(int flags, LPOBJINFO lpObjInfo)
  1231. {
  1232. typeCP cpParaStart,
  1233. cpParaCache = vcpFirstParaCache;
  1234. int docCache = vdocParaCache;
  1235. OBJPICINFO picInfo;
  1236. LPOLEOBJECT lpObject = lpObjInfo->lpobject;
  1237. /**
  1238. For Embeds (including InsertObject objects):
  1239. OLE_SAVED is sent with server File.Update
  1240. (set undo if not NONE)
  1241. OLE_CHANGED is sent with server File.Save or File.Close (?)
  1242. with update (set undo if not NONE), or when OleSetData()
  1243. causes a change in the presentation of the object.
  1244. OLE_CLOSED is sent when doc closes in server (clear undo if set)
  1245. For Links:
  1246. OLE_SAVED is sent with server File.Save (As?) if update_options
  1247. == update_on_save (that never happens)
  1248. OLE_CHANGED is sent when something in the server doc changes
  1249. OLE_CLOSED is never sent
  1250. **/
  1251. Assert(lpObjInfo != NULL);
  1252. if (lpObjInfo == NULL)
  1253. return;
  1254. if (lpObjInfo->objectType == NONE) // result of InsertObject
  1255. {
  1256. cpParaStart=lpObjInfo->cpWhere; // note only used here!
  1257. if (flags == OLE_CLOSED) // delete object
  1258. {
  1259. if (lpObject) /* may already be released or deleted */
  1260. ObjDeleteObject(lpObjInfo,TRUE);
  1261. NoUndo();
  1262. BringWindowToTop(hMAINWINDOW);
  1263. }
  1264. else
  1265. {
  1266. extern int vfSeeSel;
  1267. (**hpdocdod)[docCur].fFormatted = fTrue;
  1268. /* insert EOL if needed */
  1269. if (cpParaStart > cp0)
  1270. {
  1271. ObjCachePara(docCur, cpParaStart - 1);
  1272. if (vcpLimParaCache != cpParaStart)
  1273. {
  1274. InsertEolPap(docCur, cpParaStart, &vpapAbs);
  1275. cpParaStart += ccpEol;
  1276. }
  1277. }
  1278. GimmeNewPicinfo(&picInfo, lpObjInfo);
  1279. ObjCachePara(docCur,cpParaStart);
  1280. /* this'll clear selection. */
  1281. if (ObjSaveObjectToDoc(&picInfo,docCur,cpParaStart) == cp0)
  1282. Error(IDPMTFailedToCreateObject);
  1283. NoUndo();
  1284. ObjInvalidatePict(&picInfo,cpParaStart);
  1285. vfSeeSel = true; /* Tell Idle() to scroll the selection into view */
  1286. (**hpdocdod) [docCur].fDirty = TRUE;
  1287. }
  1288. }
  1289. else if (ObjGetPicInfo(lpObject,docCur,&picInfo,&cpParaStart))
  1290. {
  1291. BOOL bSizeChanged;
  1292. //GetPicInfo(cpParaStart,cpParaStart + cchPICINFOX, docCur, &picInfo);
  1293. /* invalidate rect before updating size (cause invalidate
  1294. needs to know old pic size) */
  1295. ObjInvalidatePict(&picInfo,cpParaStart);
  1296. bSizeChanged = ObjUpdatePicSize(&picInfo,cpParaStart);
  1297. if ((flags == OLE_CHANGED) ||
  1298. (flags == OLE_SAVED) && (otOBJ_QUERY_TYPE(&picInfo) == EMBEDDED))
  1299. {
  1300. NoUndo();
  1301. #ifdef DEBUG
  1302. if (!fOBJ_QUERY_DIRTY_OBJECT(&picInfo))
  1303. OutputDebugString( (LPSTR) "Marking object dirty\n\r");
  1304. #endif
  1305. fOBJ_QUERY_DIRTY_OBJECT(&picInfo) = TRUE;
  1306. #ifdef DEBUG
  1307. if (!(**hpdocdod) [docCur].fDirty)
  1308. OutputDebugString( (LPSTR) "Marking doc dirty\n\r");
  1309. #endif
  1310. (**hpdocdod) [docCur].fDirty = TRUE;
  1311. }
  1312. if (bSizeChanged)
  1313. if (ObjSetPicInfo(&picInfo,docCur,cpParaStart))
  1314. Error(IDPMTFailedToUpdate);
  1315. if (otOBJ_QUERY_TYPE(&picInfo) == EMBEDDED)
  1316. {
  1317. if (flags == OLE_CLOSED)
  1318. {
  1319. //if (fOBJ_QUERY_DIRTY_OBJECT(&picInfo))
  1320. BringWindowToTop(hMAINWINDOW);
  1321. #ifdef UPDATE_UNDO
  1322. ObjClearUpdateUndo(&picInfo,docCur,cpParaStart);
  1323. #endif
  1324. }
  1325. #ifdef UPDATE_UNDO
  1326. else if (flags == OLE_SAVED)
  1327. {
  1328. ObjSetUpdateUndo(&picInfo,docCur,cpParaStart);
  1329. SetUndo(uacObjUpdate,docCur,cpParaStart,cp0,docNil,cp0,cp0,0);
  1330. }
  1331. #endif
  1332. }
  1333. }
  1334. ObjCachePara(docCache,cpParaCache); // reset state
  1335. }
  1336. BOOL ObjUpdatePicSize(OBJPICINFO *pPicInfo, typeCP cpParaStart)
  1337. /* returns whether size changed */
  1338. {
  1339. int xSize,ySize;
  1340. BOOL bUpdate = FALSE;
  1341. /* object may have changed size */
  1342. if (!FComputePictSize(pPicInfo, &xSize, &ySize ))
  1343. Error(IDPMTFailedToUpdate);
  1344. else
  1345. bUpdate = (xSize != pPicInfo->dxaSize) ||
  1346. (ySize != pPicInfo->dyaSize);
  1347. if (bUpdate)
  1348. {
  1349. int yOldSize = pPicInfo->dyaSize;
  1350. pPicInfo->dxaSize = xSize;
  1351. pPicInfo->dyaSize = ySize;
  1352. if (yOldSize < pPicInfo->dyaSize)
  1353. { /* If the picture height was increased, make sure proper EDLs are
  1354. invalidated. */
  1355. typeCP dcp = CpMacText(docCur) - cpParaStart + (typeCP) 1;
  1356. ObjPushParms(docCur);
  1357. AdjustCp(docCur, cpParaStart, dcp, dcp); // major async problems here?
  1358. ObjPopParms(TRUE);
  1359. }
  1360. }
  1361. return bUpdate;
  1362. }
  1363. void ObjHandleBadLink(OLE_RELEASE_METHOD rm, LPOLEOBJECT lpObject)
  1364. {
  1365. switch (rm)
  1366. {
  1367. case OLE_ACTIVATE:
  1368. case OLE_UPDATE:
  1369. {
  1370. typeCP cpParaStart,cpParaCache = vcpFirstParaCache;
  1371. int docCache = vdocParaCache;
  1372. OBJPICINFO picInfo;
  1373. /* don't need to do all docs since objects can only be active in docCur */
  1374. if (!ObjGetPicInfo(lpObject,docCur,&picInfo,&cpParaStart))
  1375. {
  1376. /* maybe in scrap, just ignore */
  1377. ObjCachePara(docCache,cpParaCache); // reset state
  1378. return;
  1379. }
  1380. ObjCachePara(docCur,cpParaStart);
  1381. if (FixInvalidLink(&picInfo,docCur,cpParaStart))
  1382. switch (rm)
  1383. {
  1384. case OLE_ACTIVATE:
  1385. StartLongOp();
  1386. ObjError(OleActivate(lpObject,
  1387. fOBJ_QUERY_PLAY(&picInfo),
  1388. TRUE,
  1389. TRUE,
  1390. hDOCWINDOW,
  1391. NULL));
  1392. EndLongOp(vhcArrow);
  1393. break;
  1394. case OLE_UPDATE:
  1395. StartLongOp();
  1396. ObjError(OleUpdate(lpObject));
  1397. EndLongOp(vhcArrow);
  1398. break;
  1399. }
  1400. ObjCachePara(docCache,cpParaCache); // reset state
  1401. }
  1402. break;
  1403. }
  1404. }
  1405. BOOL ObjWaitForObject(LPOBJINFO lpObjInfo, BOOL bOK2Cancel)
  1406. {
  1407. HCURSOR hCursor = NULL;
  1408. BOOL bRetval;
  1409. /**
  1410. WMsgLoop allows WM_PAINT messages which wreak havoc. Try to
  1411. recover from the insult.
  1412. **/
  1413. typeCP cpParaCache = vcpFirstParaCache;
  1414. int docCache = vdocParaCache;
  1415. LPOLEOBJECT lpObject;
  1416. if (lpObjInfo == NULL) // shouldn't happen
  1417. {
  1418. Assert(0);
  1419. return FALSE;
  1420. }
  1421. /* Since ObjPicEnumInRange returns unloaded picinfo's this is a
  1422. valid possibility, but we shouldn't be getting called!!! */
  1423. Assert(lpObjInfo->lpobject != NULL);
  1424. Assert (CheckPointer((LPSTR)lpObjInfo,1));
  1425. lpObject = lpObjInfo->lpobject;
  1426. if (!ObjIsValid(lpObject))
  1427. {
  1428. Assert (0);
  1429. return FALSE;
  1430. }
  1431. hCursor = SetCursor(vhcHourGlass);
  1432. StartLongOp();
  1433. bRetval = WMsgLoop(TRUE,TRUE,bOK2Cancel,lpObject);
  1434. if (hCursor)
  1435. EndLongOp(hCursor);
  1436. ObjCachePara(docCache,cpParaCache); // reset state
  1437. /* problem here is that we may have been waiting for a release or delete */
  1438. if (ObjIsValid(lpObject))
  1439. {
  1440. lpObjInfo->fCancelAsync = FALSE; // clear after use
  1441. lpObjInfo->fCompleteAsync = FALSE; // clear after use
  1442. lpObjInfo->fCanKillAsync = FALSE;
  1443. }
  1444. UPDATE_INVALID(); // let WM_PAINTS get through now that we're no longer blocking
  1445. return bRetval;
  1446. }
  1447. #if 0
  1448. BOOL ObjObjectSync(LPOBJINFO lpObjInfo, OLESTATUS (FAR PASCAL *lpProc)(LPOLEOBJECT lpObject), BOOL bOK2Cancel)
  1449. /*
  1450. This makes an asynchronous call synchronous. lpProc must have only
  1451. lpObject as argument. This will block if operation cannot be completed
  1452. or cancelled.
  1453. */
  1454. {
  1455. /* caller has set or not set CancelAsync flag for object */
  1456. if (ObjWaitForObject(lpObjInfo,bOK2Cancel))
  1457. return TRUE;
  1458. switch((*lpProc)(lpObjInfo->lpobject))
  1459. {
  1460. case OLE_WAIT_FOR_RELEASE:
  1461. {
  1462. /* cancel button should only be enabled if this operation
  1463. can be cancelled. */
  1464. lpObjInfo->fCancelAsync = FALSE; // don't cancel automatically
  1465. lpObjInfo->fCompleteAsync = TRUE; // this op must complete or be cancelled
  1466. if (ObjWaitForObject(lpObjInfo,TRUE))
  1467. return TRUE;
  1468. /* the trouble with this is that lpObject may now be released
  1469. (and thus invalid):
  1470. return ObjError(OleQueryReleaseError(lpObjInfo->lpobject));
  1471. */
  1472. return FALSE;
  1473. }
  1474. case OLE_OK:
  1475. return FALSE;
  1476. default:
  1477. return TRUE;
  1478. }
  1479. }
  1480. #endif
  1481. #define WM_NCMOUSEFIRST 0x00A0
  1482. #define WM_NCMOUSELAST 0x00A9
  1483. static BOOL WMsgLoop
  1484. (
  1485. BOOL fExitOnIdle, // if true, return as soon as no messages to process
  1486. // (not used, assumed FALSE).
  1487. BOOL fIgnoreInput, // if true, ignore keyboard and mouse input
  1488. BOOL bOK2Cancel,
  1489. LPOLEOBJECT lpObject
  1490. )
  1491. {
  1492. MSG msg;
  1493. DWORD GetCurrentTime();
  1494. DWORD cTime=GetCurrentTime();
  1495. BOOL bRetval=FALSE,bBeeped=FALSE;
  1496. int fParentEnable;
  1497. extern int flashID;
  1498. #ifdef DEBUG
  1499. if (OleQueryReleaseStatus(lpObject) == OLE_BUSY)
  1500. OutputDebugString("waiting for object\n\r");
  1501. #endif
  1502. ++nBlocking;
  1503. ferror = FALSE;
  1504. ++nWaitingForObject;
  1505. StartLongOp();
  1506. while (OleQueryReleaseStatus(lpObject) == OLE_BUSY)
  1507. {
  1508. /* put up wait dialog after 6 seconds */
  1509. if ((GetCurrentTime() - cTime) > 6000L)
  1510. {
  1511. // bring up wait dialog.
  1512. if (!hwndWait)
  1513. {
  1514. if (vfDeactByOtherApp)
  1515. {
  1516. if (!bBeeped) // flash until we're activated
  1517. {
  1518. fParentEnable = IsWindowEnabled(hMAINWINDOW);
  1519. //MessageBeep(0);
  1520. bBeeped = TRUE;
  1521. if (!fParentEnable)
  1522. EnableWindow(hMAINWINDOW, TRUE); /* make sure parent window is enabled
  1523. to let the user click in it */
  1524. flashID = 1234; // arbitrary ID
  1525. SetTimer(hMAINWINDOW, flashID, 500, (FARPROC)NULL);
  1526. // this'll cause flashing, see mmw.c
  1527. }
  1528. }
  1529. else // Write is active app
  1530. {
  1531. if (bBeeped)
  1532. /* then we've regained the activation */
  1533. {
  1534. if (!fParentEnable)
  1535. EnableWindow(hMAINWINDOW, FALSE); /* reset */
  1536. bBeeped = FALSE;
  1537. KillTimer(hMAINWINDOW, flashID);
  1538. flashID = 0;
  1539. FlashWindow(hMAINWINDOW, FALSE);
  1540. }
  1541. if (OleQueryReleaseStatus(lpObject) == OLE_BUSY)
  1542. {
  1543. /* this'll set hwndWait */
  1544. vbCancelOK = bOK2Cancel;
  1545. bRetval = DialogBoxParam(hINSTANCE, (LPSTR)"DTWAIT", hPARENTWINDOW, lpfnWaitForObject, (DWORD)lpObject);
  1546. break;
  1547. }
  1548. }
  1549. }
  1550. else
  1551. {
  1552. bRetval = TRUE;
  1553. break;
  1554. }
  1555. }
  1556. if (!PeekMessage (&msg, NULL, 0, 0, PM_REMOVE))
  1557. {
  1558. /* No messages, do Idle processing */
  1559. /* this is where we'd use ExitOnIdle */
  1560. }
  1561. else
  1562. {
  1563. /* The following code will put the app into a
  1564. sleeping state if the fIgnoreInput flag is set
  1565. to TRUE. It will allow Quit, DDE, and any
  1566. other non input messages to be passed along
  1567. and dispatched but will prohibit the user from
  1568. interacting with the app. The user can
  1569. Alt-(sh)Tab, Alt-(sh)Esc, and Ctrl-Esc away from the
  1570. app as well as use any Windows hot keys to
  1571. activate other apps */
  1572. /* if we pass this test, then the message may be one we can ignore */
  1573. if ((fIgnoreInput) &&
  1574. (!(vfDeactByOtherApp &&
  1575. (msg.message == WM_NCLBUTTONDOWN))) &&
  1576. ((msg.message >= WM_NCMOUSEFIRST &&
  1577. msg.message <= WM_NCMOUSELAST) ||
  1578. (msg.message >= WM_KEYFIRST &&
  1579. msg.message <= WM_KEYLAST) ||
  1580. (msg.message >= WM_MOUSEFIRST &&
  1581. msg.message <= WM_MOUSELAST)))
  1582. {
  1583. static BOOL fAltCtl = FALSE;
  1584. if (msg.message != WM_SYSKEYDOWN)
  1585. continue; // ignore
  1586. if (msg.wParam == VK_MENU)
  1587. fAltCtl = TRUE;
  1588. else if (fAltCtl && msg.wParam != VK_SHIFT)
  1589. {
  1590. fAltCtl = FALSE;
  1591. if (msg.wParam != VK_TAB && msg.wParam != VK_ESCAPE)
  1592. continue; // ignore
  1593. }
  1594. }
  1595. if ((vfDeactByOtherApp &&
  1596. (msg.message == WM_NCLBUTTONDOWN)))
  1597. BringWindowToTop(hwndWait ? hwndWait : msg.hwnd);
  1598. else
  1599. {
  1600. TranslateMessage ((LPMSG) &msg);
  1601. DispatchMessage ((LPMSG) &msg);
  1602. }
  1603. }
  1604. }
  1605. Assert(hwndWait == NULL);
  1606. if (bBeeped) // then beeped but done before received activation
  1607. {
  1608. if (!fParentEnable)
  1609. EnableWindow(hMAINWINDOW, FALSE); /* reset */
  1610. KillTimer(hMAINWINDOW, flashID);
  1611. flashID = 0;
  1612. FlashWindow(hMAINWINDOW, FALSE);
  1613. }
  1614. --nBlocking;
  1615. --nWaitingForObject;
  1616. EndLongOp(vhcArrow);
  1617. Assert(nBlocking >= 0);
  1618. return bRetval;
  1619. }
  1620. void FinishUp(void)
  1621. /* let all pending messages through and return */
  1622. /*
  1623. !!! Note that we may accumulate WM_PAINTS. Caller is responsible for
  1624. calling UPDATE_INVALID() to catch up on them!!!
  1625. */
  1626. {
  1627. MSG msg;
  1628. /* now allow through all messages posted from callback */
  1629. ++nBlocking; // block WM_PAINTS
  1630. while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
  1631. {
  1632. TranslateMessage ((LPMSG) &msg);
  1633. DispatchMessage ((LPMSG) &msg);
  1634. }
  1635. --nBlocking;
  1636. return FALSE;
  1637. }
  1638. BOOL FinishAllAsyncs(BOOL bAllowCancel)
  1639. /*
  1640. !!! Note that we may accumulate WM_PAINTS. Caller is responsible for
  1641. calling UPDATE_INVALID() to catch up on them!!! (see FinishUp())
  1642. */
  1643. {
  1644. LPOLEOBJECT lpObject;
  1645. LPOBJINFO lpObjInfo;
  1646. /* first make sure all async ops are complete */
  1647. lpObject=NULL;
  1648. do
  1649. {
  1650. OleEnumObjects(lhClientDoc,&lpObject);
  1651. if (lpObject)
  1652. {
  1653. lpObjInfo = GetObjInfo(lpObject);
  1654. if (lpObjInfo)
  1655. {
  1656. /* cancel takes us outa here. Pending asyncs will block us. */
  1657. if (ObjWaitForObject(lpObjInfo,bAllowCancel))
  1658. return TRUE;
  1659. }
  1660. else // shouldn't happen, but don't assume for now
  1661. {
  1662. Assert(0);
  1663. if (WMsgLoop(TRUE,TRUE,bAllowCancel,lpObject))
  1664. return TRUE;
  1665. }
  1666. if (!ObjIsValid(lpObject)) // then got deleted
  1667. lpObject = NULL; // start over
  1668. }
  1669. }
  1670. while (lpObject);
  1671. /* let all messages posted from callback get through */
  1672. FinishUp();
  1673. return FALSE;
  1674. }
  1675. static typeCP cpPPSave;
  1676. static int docPPSave;
  1677. static struct SEL selPPSave;
  1678. static int nPushed=FALSE;
  1679. ObjPushParms(int doc)
  1680. /* Save selCur and Cache info to reset with Pop after writing to
  1681. doc. Assumes aren't changing size of doc.
  1682. */
  1683. {
  1684. if (nPushed) // prevent recursion
  1685. {
  1686. #ifdef DEBUG
  1687. OutputDebugString("Unmatched ObjPushParms\n\r");
  1688. #endif
  1689. return;
  1690. }
  1691. ++nPushed;
  1692. cpPPSave = vcpFirstParaCache;
  1693. docPPSave = vdocParaCache;
  1694. selPPSave = selCur;
  1695. Select(selCur.cpFirst,selCur.cpFirst); // this caches para
  1696. //T-HIROYN raid #3538
  1697. #ifdef KKBUGFIX
  1698. if(docPPSave == docNil)
  1699. docPPSave = vdocParaCache;
  1700. #endif
  1701. CachePara(docPPSave,cpPPSave);
  1702. }
  1703. ObjPopParms(BOOL bCache)
  1704. {
  1705. typeCP cpMac = (**hpdocdod) [docPPSave].cpMac;
  1706. if (!nPushed) // unmatched push/pops
  1707. {
  1708. #ifdef DEBUG
  1709. OutputDebugString("Unmatched ObjPopParms\n\r");
  1710. #endif
  1711. return;
  1712. }
  1713. --nPushed;
  1714. if (docPPSave == docCur)
  1715. {
  1716. if (selPPSave.cpLim > cpMac)
  1717. selPPSave.cpLim = cpMac;
  1718. if (cpPPSave > cpMac)
  1719. cpPPSave = cpMac;
  1720. }
  1721. Select(selPPSave.cpFirst,selPPSave.cpLim); // this caches para
  1722. if (bCache)
  1723. CachePara(docPPSave,cpPPSave);
  1724. //(**hpdocdod) [docPPSave].fDirty = TRUE; /* why? */
  1725. }
  1726. void ObjCachePara(int doc, typeCP cp)
  1727. {
  1728. typeCP cpMac = (**hpdocdod) [doc].cpMac;
  1729. typeCP cpMacCurSave = cpMacCur,
  1730. cpMinCurSave = cpMinCur;
  1731. if (doc == docNil)
  1732. return;
  1733. /**
  1734. cpMinCur and cpMacCur are the min and mac value for whatever is
  1735. currently docCur. Their values will be different for the header,
  1736. footer and regular docs. OBJ code doesn't distinguish. Async
  1737. operations can happen on any and all cps at once. Gotta set so
  1738. CachePara wil understand that.
  1739. **/
  1740. cpMinCur = cp0;
  1741. cpMacCur = cpMac;
  1742. if (cp >= cpMac)
  1743. cp = cpMac;
  1744. else if (cp < cp0)
  1745. cp = cp0;
  1746. CachePara(doc,cp);
  1747. cpMinCur = cpMinCurSave;
  1748. cpMacCur = cpMacCurSave;
  1749. }
  1750. #if 0
  1751. void ObjWriteFixup(int doc, BOOL bStart, typeCP cpStart)
  1752. /* note this must not be called recursively!! It is for use where size of
  1753. doc may change between bStart=TRUE and bStart=FALSE. */
  1754. {
  1755. static typeCP dcp,cpLim;
  1756. static struct SEL selSave;
  1757. typeCP cpMac;
  1758. /* reset selection accounting for change in size if any */
  1759. if (bStart)
  1760. {
  1761. cpLim = CpMacText(doc);
  1762. selSave=selCur;
  1763. if ((selCur.cpFirst != selCur.cpLim) && (doc == docCur))
  1764. Select(selCur.cpFirst,selCur.cpFirst); /* Take down sel before we mess with cp's */
  1765. /* select undoes cache */
  1766. ObjCachePara(doc,cpStart);
  1767. }
  1768. else
  1769. {
  1770. cpMac = CpMacText(doc);
  1771. dcp = cpMac-cpLim; /* change in size of doc */
  1772. if (doc == docCur)
  1773. {
  1774. if (selSave.cpFirst <= cpStart)
  1775. {
  1776. if ((selSave.cpLim) > cpStart)
  1777. selSave.cpLim += dcp;
  1778. }
  1779. else if (selSave.cpFirst > cpStart)
  1780. /* selection proceeds object */
  1781. {
  1782. selSave.cpFirst += dcp;
  1783. selSave.cpLim += dcp;
  1784. }
  1785. if (selSave.cpFirst > cpMac)
  1786. selSave.cpFirst = selSave.cpLim = cpMac;
  1787. else if (selSave.cpLim > cpMac)
  1788. selSave.cpLim = cpMac;
  1789. /* this'll cache first para in selection */
  1790. if (selSave.cpFirst != selSave.cpLim)
  1791. Select(selSave.cpFirst,selSave.cpLim);
  1792. }
  1793. ObjCachePara(doc,cpStart);
  1794. /* Fixup Undo pointers */
  1795. if (vuab.doc == docCur)
  1796. {
  1797. if (doc == docUndo) /* operating on docUndo, cpStart is irrelevant */
  1798. vuab.dcp += dcp;
  1799. else if (doc == docCur)
  1800. {
  1801. if (vuab.cp <= cpStart)
  1802. {
  1803. /* undo encloses object */
  1804. if ((vuab.cp+vuab.dcp) > cpStart)
  1805. vuab.dcp += dcp;
  1806. }
  1807. else if (vuab.cp > cpStart)
  1808. /* undo proceeds object */
  1809. vuab.cp += dcp;
  1810. }
  1811. }
  1812. if (vuab.doc2 == docCur)
  1813. {
  1814. if (doc == docUndo) /* operating on docUndo, cpStart is irrelevant */
  1815. vuab.dcp += dcp;
  1816. else if (doc == docCur)
  1817. {
  1818. if (vuab.cp2 <= cpStart)
  1819. {
  1820. /* undo encloses object */
  1821. if ((vuab.cp2+vuab.dcp2) > cpStart)
  1822. vuab.dcp2 += dcp;
  1823. }
  1824. else if (vuab.cp2 > cpStart)
  1825. /* undo proceeds object */
  1826. vuab.cp2 += dcp;
  1827. }
  1828. }
  1829. }
  1830. }
  1831. #endif
  1832. void ObjWriteClearState(int doc)
  1833. /** Call this before writing asynchronously to doc. In practise, this is
  1834. being called in synchronous times as well, so higher level code
  1835. must take care of resetting selection and undo after writing to doc.
  1836. **/
  1837. {
  1838. typeCP cpSave=vcpFirstParaCache;
  1839. int docSave=vdocParaCache;
  1840. if (doc == docCur)
  1841. {
  1842. Select(selCur.cpFirst,selCur.cpFirst); /* Take down sel before we mess with cp's */
  1843. /* select undoes cache */
  1844. ObjCachePara(docSave,cpSave);
  1845. }
  1846. //NoUndo(); /** Higher level code must SetUndo *after* calling **/
  1847. }
  1848. LPOBJINFO GetObjInfo(LPOLEOBJECT lpObject)
  1849. {
  1850. LPLPOBJINFO lplpObjTmp;
  1851. for (lplpObjTmp = NULL; lplpObjTmp = EnumObjInfos(lplpObjTmp) ;)
  1852. if ((*lplpObjTmp)->lpobject == lpObject)
  1853. return *lplpObjTmp;
  1854. Assert(0);
  1855. return NULL;
  1856. }
  1857. BOOL ObjIsValid(LPOLEOBJECT lpobj)
  1858. {
  1859. if (!CheckPointer((LPSTR)lpobj, 1))
  1860. return FALSE;
  1861. if (OleQueryReleaseStatus(lpobj) == OLE_ERROR_OBJECT)
  1862. return FALSE;
  1863. #if 0 // can't depend on this in future version of OLE
  1864. if (!(((LPRAWOBJECT)lpobj)->objId[0] == 'L' && ((LPRAWOBJECT)lpobj)->objId[1] == 'E'))
  1865. return FALSE;
  1866. #endif
  1867. return TRUE;
  1868. }
  1869. #if 0 // these should work, but not using them now
  1870. LPOBJINFO ObjGetClientInfo(LPOLEOBJECT lpobj)
  1871. {
  1872. LPOBJINFO lpObjInfo;
  1873. if (!CheckPointer((LPSTR)lpobj, 1))
  1874. {
  1875. Assert(0);
  1876. return NULL;
  1877. }
  1878. #if 0 // can't depend on this in future versions of OLE
  1879. if (!CheckPointer((LPSTR)(((LPRAWOBJECT)lpobj)->lpclient), 1))
  1880. {
  1881. Assert(0);
  1882. return NULL;
  1883. }
  1884. else
  1885. return (LPOBJINFO)(((LPRAWOBJECT)lpobj)->lpclient);
  1886. #endif
  1887. if (*(lpObject->lpvtbl->ObjectLong)(lpObject,OF_GET,(LPLONG)&lpObjInfo) == OLE_OK)
  1888. return lpClient;
  1889. else
  1890. return NULL;
  1891. }
  1892. BOOL ObjSetClientInfo(LPOBJINFO lpObjInfoNew, LPOLEOBJECT lpobj)
  1893. /* return if error */
  1894. {
  1895. if (!CheckPointer((LPSTR)lpobj, 0))
  1896. {
  1897. Assert(0);
  1898. return TRUE;
  1899. }
  1900. if (*(lpObject->lpvtbl->ObjectLong)(lpObject,OF_SET,(LPLONG)&lpObjInfoNew) == OLE_OK)
  1901. return FALSE;
  1902. else
  1903. {
  1904. Assert(0);
  1905. return TRUE;
  1906. }
  1907. }
  1908. #endif
  1909. #if 0
  1910. int ObjMarkInDoc(int doc)
  1911. /* mark as 'InDoc' all objects located in docScrap, all others are marked
  1912. as not 'InDoc'. Return count of objects in doc. */
  1913. {
  1914. LPLPOBJINFO lplpObjTmp;
  1915. int nObjCount=0,doc;
  1916. OBJPICINFO picInfo;
  1917. typeCP cpPicInfo;
  1918. /* mark all as not in doc */
  1919. for (lplpObjTmp = NULL; lplpObjTmp = EnumObjInfos(lplpObjTmp) ;)
  1920. {
  1921. ++nObjCount;
  1922. (*lplpObjTmp)->fInDoc = FALSE;
  1923. }
  1924. if (nObjCount == 0)
  1925. return 0;
  1926. for (cpPicInfo = cpNil,nObjCount=0;
  1927. ObjPicEnumInRange(&picInfo,doc,cp0,CpMacText(doc),&cpPicInfo);
  1928. )
  1929. {
  1930. if (lpOBJ_QUERY_INFO(&picInfo))
  1931. {
  1932. fOBJ_INDOC(&picInfo) = TRUE;
  1933. ++nObjCount;
  1934. }
  1935. }
  1936. return nObjCount;
  1937. }
  1938. BOOL AllocObjInfos()
  1939. /* return whether error */
  1940. {
  1941. OBJPICINFO picInfo;
  1942. typeCP cpPicInfo;
  1943. int doc;
  1944. for (doc = 0; doc < docMac; doc++)
  1945. {
  1946. if ((doc != docNil) && (**hpdocdod)[doc].hpctb)
  1947. {
  1948. for (cpPicInfo = cpNil;
  1949. ObjPicEnumInRange(&picInfo,doc,cp0,CpMacText(doc),&cpPicInfo);
  1950. )
  1951. {
  1952. if (picInfo.lpObjInfo)
  1953. continue;
  1954. if (ObjAllocObjInfo(&picInfo,cpPicInfo,picInfo.objectType,FALSE,NULL))
  1955. return TRUE;
  1956. /* note this makes doc dirty right off the bat, but gotta do it because
  1957. we gotta save ObjInfo handle in doc. (8.20.91) v-dougk */
  1958. ObjWriteFixup(doc,TRUE,cpPicInfo);
  1959. if (ObjSetPicInfo(&picInfo, doc, cpPicInfo))
  1960. return TRUE;
  1961. ObjWriteFixup(doc,FALSE,cpPicInfo);
  1962. }
  1963. }
  1964. }
  1965. }
  1966. #endif
  1967. BOOL ObjCloneScrapToNewDoc(LHCLIENTDOC lhNewClientDoc)
  1968. /* return whether an error */
  1969. {
  1970. szOBJNAME szObjName;
  1971. OBJPICINFO picInfo;
  1972. typeCP cpPicInfo;
  1973. int nCount=0;
  1974. extern int docScrap;
  1975. for (cpPicInfo = cpNil;
  1976. ObjPicEnumInRange(&picInfo,docScrap,cp0,CpMacText(docScrap),&cpPicInfo);
  1977. )
  1978. {
  1979. szOBJNAME szObjName;
  1980. LPOBJINFO lpObjInfo = lpOBJ_QUERY_INFO(&picInfo);
  1981. LPOLEOBJECT lpObject = lpObjInfo->lpobject;
  1982. OBJPICINFO NewPicInfo = picInfo;
  1983. #ifdef DEBUG
  1984. OutputDebugString( (LPSTR) "Cloning object in scrap\n\r");
  1985. #endif
  1986. if (ObjCloneObjInfo(&NewPicInfo, cpPicInfo, szObjName))
  1987. goto error;
  1988. if (ObjError(OleClone(lpObject,
  1989. (LPOLECLIENT)lpOBJ_QUERY_INFO(&NewPicInfo),
  1990. lhNewClientDoc,szObjName,
  1991. &lpOBJ_QUERY_OBJECT(&NewPicInfo))))
  1992. goto error;
  1993. if (ObjSetPicInfo(&NewPicInfo, docScrap, cpPicInfo))
  1994. goto error;
  1995. ObjDeleteObject(lpObjInfo,TRUE);
  1996. ++nCount;
  1997. }
  1998. goto end;
  1999. error:
  2000. /* cleanup after failure by deleting docScrap objects */
  2001. for (cpPicInfo = cpNil;
  2002. ObjPicEnumInRange(&picInfo,docScrap,cp0,CpMacText(docScrap),&cpPicInfo);
  2003. )
  2004. {
  2005. ObjDeleteObject(lpOBJ_QUERY_INFO(&picInfo),TRUE);
  2006. }
  2007. ClobberDoc(docScrap,docNil,cp0,cp0);
  2008. nCount = -1;
  2009. end:
  2010. if (nCount) // docScrap has changed
  2011. NoUndo();
  2012. return nCount;
  2013. }