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.

1851 lines
50 KiB

  1. /************************************************************/
  2. /* Windows Write, Copyright 1985-1992 Microsoft Corporation */
  3. /************************************************************/
  4. #include "windows.h"
  5. #include "mw.h"
  6. #include "winddefs.h"
  7. #include "docdefs.h"
  8. #include "propdefs.h"
  9. #include "editdefs.h"
  10. #include "filedefs.h"
  11. #include "cmddefs.h"
  12. #include "obj.h"
  13. #include "str.h"
  14. #include "objreg.h"
  15. #include <commdlg.h>
  16. #include <stdlib.h> // for strtoul()
  17. extern struct CHP vchpNormal;
  18. extern struct DOD (**hpdocdod)[];
  19. extern BOOL ferror;
  20. extern struct PAP vpapAbs;
  21. extern struct PAP vpapPrevIns;
  22. extern struct PAP *vppapNormal;
  23. extern int vdocParaCache;
  24. extern struct UAB vuab;
  25. extern int vfOutOfMemory,vfSysFull;
  26. extern int docUndo;
  27. extern struct FCB (**hpfnfcb)[];
  28. extern HCURSOR vhcArrow;
  29. /* intercepted stream functions work with this buffer */
  30. static typeCP cpObjectDataCurLoc=cp0, cpObjectDataBase=cp0;
  31. static DWORD cObjectData=0L,dwDataMax;
  32. static int docStream;
  33. static struct CHP *pchpStream;
  34. static struct PAP *ppapStream;
  35. static OPENFILENAME OFN;
  36. static void GetChp(struct CHP *pchp, typeCP cp, int doc);
  37. static BOOL ObjParaDup(int doc, typeCP cpFirst, typeCP cpLim);
  38. static BOOL bUniqueData(int doc, typeCP cpFirst, typeCP cpLim);
  39. static ObjOpenStreamIO(typeCP cpParaStart, int doc, struct CHP *pchp, struct PAP *ppapGraph, DWORD dwObjectSize);
  40. static void ObjCloseStreamIO(void);
  41. static typeCP ObjWriteDataToDoc( LPOLEOBJECT lpObject);
  42. static HANDLE OfnGetNewLinkName(HWND hwnd, HANDLE hData);
  43. static void Normalize(LPSTR lpstrFile) ;
  44. static HANDLE ObjMakeNewLinkName(HANDLE hData, ATOM atom) ;
  45. static char szCustFilterSpec[CBFILTERMAX];
  46. static char szFileName[CBPATHMAX];
  47. //static char szFilterSpec[CBFILTERMAX];
  48. //static char szLastDir[CBPATHMAX];
  49. static char szLinkCaption[cchMaxSz];
  50. static char szTemplateName[CBPATHMAX];
  51. static BOOL fUpdateAll = FALSE;
  52. static BOOL ObjStop=FALSE;
  53. static BOOL vbChangeOther;
  54. BOOL bNoEol=FALSE;
  55. #define ulmin(a,b) ((DWORD)(a) < (DWORD)(b) ? \
  56. (WORD)(a) : (WORD)(b))
  57. /****************************************************************/
  58. /**************** OLE ENUMERATION FUNCTIONS *********************/
  59. /****************************************************************/
  60. #if 0 // good, just not used
  61. int
  62. ObjEnumInAllDocs(cpFARPROC lpFunc)
  63. {
  64. int doc,count=0;
  65. for (doc = 0; doc < docMac; doc++)
  66. {
  67. int nRetval;
  68. nRetval = ObjEnumInDoc(doc, lpFunc);
  69. if (nRetval < 0)
  70. return -1;
  71. else
  72. count += nRetval;
  73. }
  74. return count;
  75. }
  76. #endif
  77. int
  78. ObjEnumInDoc(int doc, cpFARPROC lpFunc)
  79. {
  80. typeCP cpMac = (**hpdocdod) [doc].cpMac;
  81. return ObjEnumInRange(doc, cp0, cpMac, lpFunc);
  82. }
  83. int
  84. ObjEnumInRange(int doc, typeCP cpStart, typeCP cpEnd, cpFARPROC lpFunc)
  85. /* Call lpFunc for each OLE object.
  86. lpFunc takes the following args:
  87. a far pointer to a PICINFOX struct (can be NULL).
  88. an int for the doc we're operating on.
  89. a typeCP that gives the cp position of the PICINFOX struct.
  90. lpFunc returns the cp of the paragraph following the PICINFO struct
  91. if OK, or cp0 if error.
  92. Enumeration quits if error returned from FARPROC.
  93. Return number of objects operated on, or -1 if error.
  94. */
  95. {
  96. typeCP cpNow, cpLimPara;
  97. int count;
  98. /* Loop on paras */
  99. for ( count = 0, cpNow = cpStart; cpNow < cpEnd; cpNow = cpLimPara )
  100. {
  101. Assert(cpEnd <= (**hpdocdod) [doc].cpMac);
  102. /* this shouldn't happen */
  103. if (cpEnd > (**hpdocdod) [doc].cpMac)
  104. goto done;
  105. ObjCachePara( doc, cpNow );
  106. if (vpapAbs.fGraphics)
  107. {
  108. /* get PICINFO struct and see if its an object */
  109. OBJPICINFO picInfo;
  110. GetPicInfo(vcpFirstParaCache,vcpFirstParaCache + cchPICINFOX, doc, &picInfo);
  111. if (bOBJ_QUERY_IS_OBJECT(&picInfo))
  112. {
  113. typeCP cpOldLimPara = vcpLimParaCache;
  114. if (!lpFunc)
  115. cpLimPara = vcpLimParaCache;
  116. else
  117. if ((cpLimPara = (*lpFunc)(&picInfo,doc,vcpFirstParaCache)) == 0)
  118. goto error;
  119. ++count;
  120. /* amount para has grown ( < 0 if shrunk, 0 if none )*/
  121. cpEnd += cpLimPara - cpOldLimPara;
  122. continue;
  123. }
  124. }
  125. cpLimPara = vcpLimParaCache;
  126. }
  127. /* success */
  128. goto done;
  129. error:
  130. count = -1;
  131. done:
  132. return count;
  133. }
  134. ObjPicEnumInRange(OBJPICINFO *pPicInfo,int doc, typeCP cpFirst, typeCP cpLim, typeCP *cpCur)
  135. /*
  136. Enumerate over PicInfos between cpFirst and cpLim in doc. If
  137. cpCur == cpNil, then start at cpFirst, else start at *cpCur.
  138. Return 0 if done, 1 otherwise.
  139. Calls ObjCachePara() at picInfo.
  140. */
  141. {
  142. /* static typeCP cpCur;
  143. used to use static, but that prevented being able to recursively
  144. call this function, and its almost impossible to prevent with
  145. asynchronicity rampant.
  146. */
  147. typeCP cpMac = (**hpdocdod) [doc].cpMac;
  148. if (cpFirst >= cpMac)
  149. return 0;
  150. if (cpLim > cpMac)
  151. cpLim = cpMac;
  152. /* initialize cpCur */
  153. if (*cpCur == cpNil)
  154. /* then starting afresh */
  155. *cpCur = cpFirst;
  156. else
  157. {
  158. ObjCachePara(doc,*cpCur); // cache the previous para
  159. *cpCur = vcpLimParaCache; // get next para
  160. }
  161. /* pull in next para */
  162. do
  163. {
  164. if (*cpCur >= cpLim)
  165. {
  166. /* all done */
  167. *cpCur = vcpFirstParaCache; // we want to point to last para hit
  168. return 0;
  169. }
  170. ObjCachePara(doc,*cpCur);
  171. if (vpapAbs.fGraphics)
  172. {
  173. GetPicInfo(*cpCur,*cpCur + cchPICINFOX, doc, pPicInfo);
  174. if (bOBJ_QUERY_IS_OBJECT(pPicInfo))
  175. return 1;
  176. }
  177. *cpCur = vcpLimParaCache;
  178. }
  179. while (1);
  180. }
  181. typeCP ObjSaveObjectToDoc(OBJPICINFO *pPicInfo,int doc,typeCP cpParaStart)
  182. /*
  183. Assumes para is cached.
  184. In some cases we only write the picinfo. In others we write the
  185. object data after the picinfo. We assume that the latter case
  186. only occurs when the file is being saved.
  187. */
  188. {
  189. typeCP cpRetval; // cp of next byte in doc after what we just wrote
  190. static BOOL bMyRecurse=FALSE;
  191. DWORD dwObjectSize;
  192. OLESTATUS olestat;
  193. struct CHP chp;
  194. struct PAP papGraph;
  195. if (lpOBJ_QUERY_INFO(pPicInfo) == NULL)
  196. return(cp0);
  197. if (lpOBJ_QUERY_OBJECT(pPicInfo) == NULL)
  198. return(vcpLimParaCache);
  199. /* we don't save nothing if it ain't dirty and has data */
  200. if (!fOBJ_QUERY_DIRTY_OBJECT(pPicInfo) &&
  201. dwOBJ_QUERY_DATA_SIZE(pPicInfo) != 0L)
  202. return(vcpLimParaCache);
  203. if (vfOutOfMemory || vfSysFull /*|| ObjStop*/)
  204. return cp0;
  205. olestat = OleQuerySize(lpOBJ_QUERY_OBJECT(pPicInfo),&dwObjectSize);
  206. if (olestat == OLE_ERROR_BLANK)
  207. dwObjectSize = 0L;
  208. else if (ObjError(olestat))
  209. return cp0;
  210. /** don't let this function recurse (in CallBack) **/
  211. if (bMyRecurse)
  212. {
  213. Assert(0); // this has never happened yet (8.21.91) v-dougk
  214. return cp0;
  215. }
  216. bMyRecurse = TRUE;
  217. fOBJ_QUERY_DIRTY_OBJECT(pPicInfo) = FALSE;
  218. /**
  219. If docUndo will want to undo some region that contains this
  220. object, and if saving the object changes the size of that
  221. region, then vuab will become obsolete.
  222. **/
  223. ObjWriteClearState(doc);
  224. GetChp(&chp, cpParaStart, doc);
  225. if (dwOBJ_QUERY_DATA_SIZE(pPicInfo) != 0xFFFFFFFF)
  226. /* then has been saved before */
  227. {
  228. /* zap the entire existing object */
  229. papGraph = vpapAbs;
  230. Replace(doc, cpParaStart, (vcpLimParaCache - vcpFirstParaCache), fnNil, fc0, fc0);
  231. }
  232. else // new object
  233. {
  234. ObjCachePara(doc,cpParaStart-1); // use previous PAP
  235. papGraph = vpapAbs;
  236. papGraph.fGraphics = TRUE;
  237. ObjCachePara(doc,cpParaStart);
  238. }
  239. if (otOBJ_QUERY_TYPE(pPicInfo) == NONE)
  240. {
  241. if (dwObjectSize)
  242. /*
  243. Insert New has culminated in a new baby object!
  244. */
  245. {
  246. otOBJ_QUERY_TYPE(pPicInfo) = EMBEDDED; // do this first
  247. if (!FComputePictSize(pPicInfo, &(pPicInfo->dxaSize), &(pPicInfo->dyaSize)))
  248. {
  249. cpRetval = cp0;
  250. goto end;
  251. }
  252. #if 0
  253. DeleteAtom(aOBJ_QUERY_SERVER_CLASS(pPicInfo));
  254. aOBJ_QUERY_SERVER_CLASS(pPicInfo) = NULL;
  255. #endif
  256. }
  257. else // don't save empty object
  258. {
  259. Assert(0);
  260. goto end;
  261. }
  262. }
  263. #ifdef DEBUG
  264. OutputDebugString( (LPSTR) "Saving object\n\r");
  265. #endif
  266. /*
  267. Insert PICINFO struct. There is a problem here which is a
  268. bug in Write (CheckGraphic()). EOL gets inserted when we are
  269. replacing an object which is immediately in front of
  270. another object. Kludge is to set this flag to inhibit.
  271. */
  272. bNoEol = TRUE;
  273. if (bOBJ_QUERY_DONT_SAVE_DATA(pPicInfo))
  274. /* only save picinfo until user does File.Save */
  275. {
  276. ObjUpdateFromObjInfo(pPicInfo);
  277. bOBJ_QUERY_DONT_SAVE_DATA(pPicInfo) = FALSE;
  278. dwOBJ_QUERY_DATA_SIZE(pPicInfo) = 0L;
  279. pPicInfo->mm |= MM_EXTENDED; /* Extended file format */
  280. InsertRgch( doc, cpParaStart, pPicInfo, sizeof(OBJPICINFO), &chp, &papGraph);
  281. pPicInfo->mm &= ~MM_EXTENDED;
  282. ObjCachePara(doc,cpParaStart);
  283. cpRetval = vcpLimParaCache;
  284. }
  285. else
  286. {
  287. dwOBJ_QUERY_DATA_SIZE(pPicInfo) = dwObjectSize;
  288. ObjUpdateFromObjInfo(pPicInfo);
  289. pPicInfo->mm |= MM_EXTENDED; /* Extended file format */
  290. InsertRgch( doc, cpParaStart, pPicInfo, sizeof(OBJPICINFO), &chp, NULL );
  291. pPicInfo->mm &= ~MM_EXTENDED;
  292. /* insert object data into doc */
  293. ObjOpenStreamIO(cpParaStart + cchPICINFOX, doc, &chp, &papGraph, dwObjectSize);
  294. cpRetval = ObjWriteDataToDoc(lpOBJ_QUERY_OBJECT(pPicInfo));
  295. ObjCloseStreamIO();
  296. }
  297. bNoEol = FALSE;
  298. end:
  299. ObjCachePara(doc,cpParaStart);
  300. bMyRecurse = FALSE;
  301. return ((cpRetval == cp0) ? cp0 : vcpLimParaCache);
  302. }
  303. typeCP ObjLoadObjectInDoc(OBJPICINFO *pPicInfo,int doc,typeCP cpParaStart)
  304. /*
  305. Do an OleLoad from object data in doc. Set lpobject in PICINFO struct.
  306. Assumes para is cached.
  307. This is a *synchronous* function.
  308. */
  309. {
  310. typeCP cpRetval = vcpLimParaCache;
  311. szOBJNAME szObjName;
  312. if (lpOBJ_QUERY_INFO(pPicInfo) == NULL)
  313. {
  314. if (ObjAllocObjInfo(pPicInfo,cpParaStart,pPicInfo->objectType,FALSE,NULL))
  315. return(cp0);
  316. if (ObjSetPicInfo(pPicInfo, doc, cpParaStart))
  317. return(cp0);
  318. }
  319. else if (lpOBJ_QUERY_OBJECT(pPicInfo)) // already loaded
  320. return(vcpLimParaCache);
  321. if (otOBJ_QUERY_TYPE(pPicInfo) == NONE)
  322. return(vcpLimParaCache);
  323. if (bOBJ_QUERY_TOO_BIG(pPicInfo)) // ObjLoadObject previously failed
  324. return(cp0);
  325. if ((dwOBJ_QUERY_DATA_SIZE(pPicInfo) == 0L) ||
  326. (dwOBJ_QUERY_DATA_SIZE(pPicInfo) == 0xFFFFFFFFL))
  327. /* then has no data */
  328. return(cp0);
  329. if (vfOutOfMemory || vfSysFull /*|| ObjStop*/)
  330. return cp0;
  331. StartLongOp();
  332. ObjGetObjectName(lpOBJ_QUERY_INFO(pPicInfo),szObjName);
  333. Assert(szObjName[0]);
  334. ObjOpenStreamIO(cpParaStart + cchPICINFOX, doc, NULL, NULL, 0L);
  335. #ifdef DEBUG
  336. OutputDebugString( (LPSTR) "Loading object\n\r");
  337. #endif
  338. if (ObjError(OleLoadFromStream(lpStream,
  339. otOBJ_QUERY_TYPE(pPicInfo) == STATIC ? SPROTOCOL : PROTOCOL,
  340. (LPOLECLIENT)lpOBJ_QUERY_INFO(pPicInfo),
  341. lhClientDoc,szObjName,&lpOBJ_QUERY_OBJECT(pPicInfo))))
  342. {
  343. /* mark as unloadable to prevent infinite LoadObject loops */
  344. bOBJ_QUERY_TOO_BIG(pPicInfo) = TRUE;
  345. lpOBJ_QUERY_OBJECT(pPicInfo) = NULL; // just in case (OLE ain't good about this)
  346. ferror = FALSE; // be sure to issue this message
  347. Error(IDPMTFailedToLoadObject);
  348. cpRetval = cp0;
  349. goto end;
  350. }
  351. if (ObjInitServerInfo(lpOBJ_QUERY_INFO(pPicInfo)))
  352. {
  353. ferror = FALSE; // be sure to issue this message
  354. Error(IDPMTOLEError);
  355. goto end;
  356. }
  357. if (ObjUpdatePicSize(pPicInfo,cpParaStart))
  358. if (ObjSetPicInfo(pPicInfo, doc, cpParaStart))
  359. goto end;
  360. ObjCachePara(doc,cpParaStart); // just in case
  361. cpRetval = vcpLimParaCache;
  362. end:
  363. ObjCloseStreamIO();
  364. EndLongOp(vhcArrow);
  365. return cpRetval;
  366. }
  367. typeCP ObjEditObjectInDoc(OBJPICINFO *pPicInfo, int doc, typeCP cpParaStart)
  368. {
  369. typeCP cpRetval;
  370. OBJ_PLAYEDIT = OLEVERB_PRIMARY+1;
  371. cpRetval = ObjPlayObjectInDoc(pPicInfo, doc, cpParaStart);
  372. OBJ_PLAYEDIT = OLEVERB_PRIMARY; // the default
  373. return cpRetval;
  374. }
  375. typeCP ObjPlayObjectInDoc(OBJPICINFO *pPicInfo, int doc, typeCP cpParaStart)
  376. {
  377. OLESTATUS olestat;
  378. LPOBJINFO lpOInfo=lpOBJ_QUERY_INFO(pPicInfo);
  379. if ((otOBJ_QUERY_TYPE(pPicInfo) == STATIC) ||
  380. (otOBJ_QUERY_TYPE(pPicInfo) == NONE))
  381. return(vcpLimParaCache);
  382. if (ObjMakeObjectReady(pPicInfo,doc,cpParaStart))
  383. goto err;
  384. fOBJ_QUERY_PLAY(pPicInfo) = OBJ_PLAYEDIT;
  385. do
  386. {
  387. #ifdef DEBUG
  388. OutputDebugString( "Opening Object\n\r");
  389. #endif
  390. StartLongOp();
  391. olestat = OleActivate(lpOBJ_QUERY_OBJECT(pPicInfo),
  392. OBJ_PLAYEDIT,
  393. TRUE,
  394. TRUE,
  395. hDOCWINDOW,
  396. NULL);
  397. EndLongOp(vhcArrow);
  398. switch (olestat)
  399. {
  400. /* check for bad link */
  401. case OLE_ERROR_OPEN:
  402. case OLE_ERROR_ADVISE_NATIVE:
  403. case OLE_ERROR_ADVISE_PICT:
  404. case OLE_ERROR_REQUEST_NATIVE:
  405. case OLE_ERROR_REQUEST_PICT:
  406. fOBJ_BADLINK(pPicInfo) = TRUE;
  407. if (bLinkProps)
  408. {
  409. Error(IDPMTLinkUnavailable);
  410. goto err;
  411. }
  412. else
  413. {
  414. ObjCachePara(doc,cpParaStart);
  415. if (!FixInvalidLink(pPicInfo,doc,cpParaStart))
  416. goto err;
  417. olestat = OLE_OK;
  418. lpOInfo->fCompleteAsync = TRUE; // cancel OleSetData (FixInvalid) as well
  419. if (ObjWaitForObject(lpOInfo,TRUE))
  420. goto err;
  421. }
  422. break;
  423. }
  424. if (ObjError(olestat))
  425. goto err;
  426. else
  427. break;
  428. }
  429. while (1);
  430. fOBJ_BADLINK(pPicInfo) = FALSE; // can't be bad if succeeded
  431. //(**hpdocdod) [doc].fDirty = TRUE; // assume dirty is opened.
  432. ObjCachePara(doc,cpParaStart); // just in case
  433. return(vcpLimParaCache);
  434. err:
  435. Error(IDPMTFailedToActivate);
  436. return cp0;
  437. }
  438. typeCP ObjUpdateObjectInDoc(OBJPICINFO *pPicInfo, int doc, typeCP cpParaStart)
  439. {
  440. int xSize,ySize;
  441. BOOL bUpdate = FALSE;
  442. OLESTATUS olestat;
  443. if (lpOBJ_QUERY_INFO(pPicInfo) == NULL)
  444. return(cp0);
  445. if ((otOBJ_QUERY_TYPE(pPicInfo) == STATIC) ||
  446. (otOBJ_QUERY_TYPE(pPicInfo) == NONE))
  447. return(vcpLimParaCache);
  448. if (bLinkProps && bOBJ_WAS_UPDATED(pPicInfo))
  449. return(vcpLimParaCache);
  450. if (ObjMakeObjectReady(pPicInfo,doc,cpParaStart))
  451. goto err;
  452. do
  453. {
  454. #ifdef DEBUG
  455. OutputDebugString( "Updating Object\n\r");
  456. #endif
  457. StartLongOp();
  458. olestat = OleUpdate(lpOBJ_QUERY_OBJECT(pPicInfo));
  459. EndLongOp(vhcArrow);
  460. switch (olestat)
  461. {
  462. /* check for bad link */
  463. case OLE_ERROR_OPEN:
  464. case OLE_ERROR_ADVISE_NATIVE:
  465. case OLE_ERROR_ADVISE_PICT:
  466. case OLE_ERROR_REQUEST_NATIVE:
  467. case OLE_ERROR_REQUEST_PICT:
  468. fOBJ_BADLINK(pPicInfo) = TRUE;
  469. if (bLinkProps)
  470. {
  471. Error(IDPMTLinkUnavailable);
  472. goto err;
  473. }
  474. else
  475. {
  476. ObjCachePara(doc,cpParaStart);
  477. if (!FixInvalidLink(pPicInfo,doc,cpParaStart))
  478. goto err;
  479. olestat = OLE_OK;
  480. lpOBJ_QUERY_INFO(pPicInfo)->fCompleteAsync = TRUE; // cancel OleSetData (FixInvalid) as well
  481. if (ObjWaitForObject(lpOBJ_QUERY_INFO(pPicInfo),TRUE))
  482. goto err;
  483. }
  484. break;
  485. }
  486. if (ObjError(olestat))
  487. goto err;
  488. else
  489. break;
  490. }
  491. while (1);
  492. ObjCachePara(doc,cpParaStart); // just in case
  493. fOBJ_BADLINK(pPicInfo) = FALSE; // can't be bad if succeeded
  494. if (bLinkProps)
  495. {
  496. bOBJ_WAS_UPDATED(pPicInfo) = TRUE;
  497. }
  498. return(vcpLimParaCache);
  499. err:
  500. Error(IDPMTFailedToUpdate);
  501. return cp0;
  502. }
  503. typeCP ObjFreezeObjectInDoc(OBJPICINFO *pPicInfo,int doc,typeCP cpParaStart)
  504. {
  505. szOBJNAME szObjName;
  506. OBJPICINFO NewPicInfo = *pPicInfo;
  507. if ((otOBJ_QUERY_TYPE(pPicInfo) == STATIC) ||
  508. (otOBJ_QUERY_TYPE(pPicInfo) == NONE))
  509. return(vcpLimParaCache);
  510. if (ObjMakeObjectReady(pPicInfo,doc,cpParaStart))
  511. goto err;
  512. #ifdef DEBUG
  513. OutputDebugString( (LPSTR) "Freezing object\n\r");
  514. #endif
  515. if (ObjCloneObjInfo(&NewPicInfo, cpParaStart, szObjName))
  516. return cp0;
  517. /* Make the object static. Note side effect of changing lpObject!! */
  518. if (ObjError(OleObjectConvert(lpOBJ_QUERY_OBJECT(pPicInfo), SPROTOCOL,
  519. (LPOLECLIENT)lpOBJ_QUERY_INFO(&NewPicInfo),
  520. lhClientDoc, szObjName,
  521. &lpOBJ_QUERY_OBJECT(&NewPicInfo))))
  522. goto err;
  523. /* now delete original */
  524. ObjDeleteObject(lpOBJ_QUERY_INFO(pPicInfo),TRUE);
  525. *pPicInfo = NewPicInfo;
  526. fOBJ_QUERY_DIRTY_OBJECT(pPicInfo) = TRUE;
  527. otOBJ_QUERY_TYPE(pPicInfo) = STATIC;
  528. /* we got a new name to save */
  529. if (ObjSetPicInfo(pPicInfo, doc, cpParaStart))
  530. goto err;
  531. return(vcpLimParaCache);
  532. err:
  533. ObjDeleteObjInfo(lpOBJ_QUERY_INFO(&NewPicInfo));
  534. Error(IDPMTFailedToFreeze);
  535. return cp0;
  536. }
  537. typeCP ObjCloneObjectInDoc(OBJPICINFO *pPicInfo,int doc,typeCP cpParaStart)
  538. /* note we are *not* deleting the cloned object! Note side effect that
  539. *pPicInfo gets altered to new clone values. */
  540. {
  541. BOOL fDirty;
  542. szOBJNAME szObjName;
  543. OBJPICINFO NewPicInfo = *pPicInfo;
  544. LPOLEOBJECT lpObject;
  545. if (lpOBJ_QUERY_INFO(pPicInfo) == NULL)
  546. return(cp0);
  547. if (bOBJ_REUSE_ME(pPicInfo))
  548. /* assume that the original picInfo will be deleted!!! */
  549. {
  550. #ifdef DEBUG
  551. OutputDebugString( (LPSTR) "Reusing object\n\r");
  552. #endif
  553. bOBJ_REUSE_ME(pPicInfo) = FALSE;
  554. return(vcpLimParaCache);
  555. }
  556. if (ObjMakeObjectReady(pPicInfo,doc,cpParaStart))
  557. goto err;
  558. lpObject = lpOBJ_QUERY_OBJECT(pPicInfo);
  559. /* clone it. This assumes the one we're cloning from is still in use
  560. (shouldn't be deleted). */
  561. #ifdef DEBUG
  562. OutputDebugString( (LPSTR) "Cloning object\n\r");
  563. #endif
  564. if (ObjCloneObjInfo(&NewPicInfo, cpParaStart, szObjName))
  565. return cp0;
  566. if (ObjError(OleClone(lpObject,
  567. (LPOLECLIENT)lpOBJ_QUERY_INFO(&NewPicInfo),
  568. lhClientDoc,szObjName,
  569. &lpOBJ_QUERY_OBJECT(&NewPicInfo))))
  570. goto err;
  571. lpOBJ_QUERY_INFO(&NewPicInfo)->fCompleteAsync = TRUE;
  572. if (ObjWaitForObject(lpOBJ_QUERY_INFO(&NewPicInfo),TRUE))
  573. goto err;
  574. if (lpOBJ_QUERY_INFO(&NewPicInfo)->fDeleteMe)
  575. /* this is how we know it failed asynchronously */
  576. goto err;
  577. /**
  578. Save object name and objinfo that we just got.
  579. **/
  580. *pPicInfo = NewPicInfo;
  581. if (ObjSetPicInfo(pPicInfo, doc, cpParaStart))
  582. goto err;
  583. return(vcpLimParaCache);
  584. err:
  585. ObjDeleteObjInfo(lpOBJ_QUERY_INFO(&NewPicInfo));
  586. Error(IDPMTFailedToCreateObject);
  587. return cp0;
  588. }
  589. typeCP ObjToCloneInDoc(OBJPICINFO *pPicInfo,int doc,typeCP cpParaStart)
  590. {
  591. if (lpOBJ_QUERY_INFO(pPicInfo) == NULL)
  592. return(cp0);
  593. #ifdef DEBUG
  594. OutputDebugString( (LPSTR) "Marking object reusable\n\r");
  595. #endif
  596. bOBJ_REUSE_ME(pPicInfo) = TRUE;
  597. return(vcpLimParaCache);
  598. }
  599. typeCP ObjFromCloneInDoc(OBJPICINFO *pPicInfo,int doc,typeCP cpParaStart)
  600. {
  601. if (lpOBJ_QUERY_INFO(pPicInfo) == NULL)
  602. return(cp0);
  603. #ifdef DEBUG
  604. OutputDebugString( (LPSTR) "Marking object not reusable\n\r");
  605. #endif
  606. bOBJ_REUSE_ME(pPicInfo) = FALSE;
  607. return(vcpLimParaCache);
  608. }
  609. typeCP ObjBackupInDoc(OBJPICINFO *pPicInfo,int doc,typeCP cpParaStart)
  610. /* !!! used by link properties only !!!. Object guaranteed loaded */
  611. {
  612. szOBJNAME szObjName;
  613. LPOBJINFO lpCloneInfo=NULL;
  614. if (lpOBJ_QUERY_CLONE(pPicInfo) == NULL) // then clone it
  615. {
  616. #ifdef DEBUG
  617. OutputDebugString( (LPSTR) "Backing up object\n\r");
  618. #endif
  619. if (ObjWaitForObject(lpOBJ_QUERY_INFO(pPicInfo),TRUE))
  620. return cp0;
  621. if (ObjCopyObjInfo(lpOBJ_QUERY_INFO(pPicInfo),
  622. &lpOBJ_QUERY_CLONE(pPicInfo),
  623. szObjName))
  624. return(cp0);
  625. if (ObjError(OleClone(lpOBJ_QUERY_OBJECT(pPicInfo),
  626. (LPOLECLIENT)lpOBJ_QUERY_CLONE(pPicInfo),
  627. lhClientDoc,szObjName,&(lpOBJ_QUERY_CLONE(pPicInfo)->lpobject))))
  628. return cp0;
  629. }
  630. return(vcpLimParaCache);
  631. }
  632. typeCP ObjClearCloneInDoc(OBJPICINFO *pPicInfo,int doc,typeCP cpParaStart)
  633. /* !!! used by link properties only !!! Object guaranteed loaded */
  634. /* delete clone, don't use it */
  635. {
  636. if (lpOBJ_QUERY_CLONE(pPicInfo))
  637. {
  638. #ifdef DEBUG
  639. OutputDebugString( (LPSTR) "Deleting clone\n\r");
  640. #endif
  641. ObjDeleteObject(lpOBJ_QUERY_CLONE(pPicInfo),TRUE);
  642. lpOBJ_QUERY_CLONE(pPicInfo) = NULL;
  643. }
  644. return(vcpLimParaCache);
  645. }
  646. typeCP ObjUseCloneInDoc(OBJPICINFO *pPicInfo,int doc,typeCP cpParaStart)
  647. /* !!! used by link properties only !!! */
  648. {
  649. szOBJNAME szObjName;
  650. if (lpOBJ_QUERY_INFO(pPicInfo) == NULL)
  651. return(cp0);
  652. if (lpOBJ_QUERY_CLONE(pPicInfo))
  653. {
  654. extern int FAR _cdecl sscanf(const char FAR *, const char FAR *, ...);
  655. LPOBJINFO lpClone = lpOBJ_QUERY_CLONE(pPicInfo);
  656. #ifdef DEBUG
  657. OutputDebugString( (LPSTR) "using clone\n\r");
  658. #endif
  659. ObjDeleteObject(lpOBJ_QUERY_INFO(pPicInfo),TRUE);
  660. lpOBJ_QUERY_INFO(pPicInfo) = lpClone;
  661. lpOBJ_QUERY_CLONE(pPicInfo) = NULL;
  662. fOBJ_QUERY_DIRTY_OBJECT(pPicInfo) = TRUE; // wanna save clone information
  663. // just in case
  664. /* might've been frozen, used by LoadObject (this is what is unique
  665. in the context of link properties) */
  666. otOBJ_QUERY_TYPE(pPicInfo) = LINK;
  667. if (ObjSetPicInfo(pPicInfo, doc, cpParaStart))
  668. return cp0;
  669. }
  670. return(vcpLimParaCache);
  671. }
  672. typeCP ObjSetNoUpdate(OBJPICINFO *pPicInfo,int doc,typeCP cpParaStart)
  673. {
  674. if (lpOBJ_QUERY_INFO(pPicInfo) == NULL)
  675. return(cp0);
  676. #ifdef DEBUG
  677. OutputDebugString( (LPSTR) "Set no update for object\n\r");
  678. #endif
  679. if (otOBJ_QUERY_TYPE(pPicInfo) == LINK)
  680. {
  681. bOBJ_WAS_UPDATED(pPicInfo) = FALSE;
  682. }
  683. return(vcpLimParaCache);
  684. }
  685. typeCP ObjCheckObjectTypes(OBJPICINFO *pPicInfo,int doc,typeCP cpParaStart)
  686. {
  687. #ifdef DEBUG
  688. //OutputDebugString( (LPSTR) "Checking Object Type\n\r");
  689. #endif
  690. /* Result returned in OBJ_SELECTIONTYPE is highest object type which
  691. exists at cpParaStart relative to current value of OBJ_SELECTIONTYPE.
  692. */
  693. switch(otOBJ_QUERY_TYPE(pPicInfo))
  694. {
  695. case STATIC:
  696. if (OBJ_SELECTIONTYPE < STATIC)
  697. OBJ_SELECTIONTYPE = STATIC;
  698. //OBJ_CEMBEDS = 0;
  699. return(vcpLimParaCache);
  700. case LINK:
  701. OBJ_SELECTIONTYPE = LINK;
  702. //OBJ_CEMBEDS = 0;
  703. return(vcpLimParaCache);
  704. case NONE:
  705. if (OBJ_SELECTIONTYPE < NONE)
  706. OBJ_SELECTIONTYPE = NONE;
  707. //OBJ_CEMBEDS = 0;
  708. return(vcpLimParaCache);
  709. case EMBEDDED:
  710. if (OBJ_SELECTIONTYPE < EMBEDDED)
  711. OBJ_SELECTIONTYPE = EMBEDDED;
  712. //++OBJ_CEMBEDS;
  713. return(vcpLimParaCache);
  714. default:
  715. Assert(0);
  716. return cp0;
  717. }
  718. }
  719. typeCP ObjSetHostNameInDoc(OBJPICINFO *pPicInfo,int doc,typeCP cpParaStart)
  720. {
  721. if (lpOBJ_QUERY_INFO(pPicInfo) == NULL)
  722. return(cp0);
  723. if (lpOBJ_QUERY_OBJECT(pPicInfo) == NULL)
  724. return(vcpLimParaCache); // dont care if not loaded
  725. if ((otOBJ_QUERY_TYPE(pPicInfo) != EMBEDDED) &&
  726. (otOBJ_QUERY_TYPE(pPicInfo) != NONE))
  727. return(vcpLimParaCache);
  728. //if (OleQueryOpen(lpOBJ_QUERY_OBJECT(pPicInfo)) != OLE_OK)
  729. //return(vcpLimParaCache);
  730. if (ObjWaitForObject(lpOBJ_QUERY_INFO(pPicInfo),TRUE))
  731. return cp0;
  732. if (ObjSetHostName(lpOBJ_QUERY_INFO(pPicInfo),doc))
  733. return(cp0);
  734. return(vcpLimParaCache);
  735. }
  736. typeCP ObjChangeLinkInDoc(OBJPICINFO *pPicInfo,int doc,typeCP cpParaStart)
  737. /* assumes aNewName is set */
  738. {
  739. HANDLE hData,hNewData=NULL;
  740. typeCP cpRetval=cp0;
  741. OLESTATUS olestat=OLE_OK;
  742. if (otOBJ_QUERY_TYPE(pPicInfo) != LINK)
  743. return(vcpLimParaCache);
  744. if (ObjMakeObjectReady(pPicInfo,doc,cpParaStart))
  745. return cp0;
  746. /* Change the link information */
  747. /* if theres a newname, then use it. Else get a new name from user */
  748. olestat = ObjGetData(lpOBJ_QUERY_INFO(pPicInfo), vcfLink, &hData);
  749. if ((olestat == OLE_WARN_DELETE_DATA) || (olestat == OLE_OK))
  750. {
  751. if (!(hNewData = ObjMakeNewLinkName(hData, aNewName))
  752. || !ObjSetData(pPicInfo, vcfLink, hNewData))
  753. goto end;
  754. if (olestat == OLE_WARN_DELETE_DATA)
  755. GlobalFree(hData);
  756. /* this may not be necessary any more, check carefully. */
  757. if (ObjUpdateObjectInDoc(pPicInfo,doc,cpParaStart) == cp0)
  758. goto end;
  759. fOBJ_QUERY_DIRTY_OBJECT(pPicInfo) = TRUE;
  760. fOBJ_BADLINK(pPicInfo) = FALSE;
  761. cpRetval = vcpLimParaCache;
  762. }
  763. end:
  764. if (hNewData)
  765. GlobalFree(hNewData);
  766. return cpRetval;
  767. }
  768. typeCP ObjUpdateLinkInDoc(OBJPICINFO *pPicInfo,int doc,typeCP cpParaStart)
  769. /**
  770. Change or update link to aNewName if == aOldName. Assumes vbChangeOther
  771. has been initialized.
  772. **/
  773. {
  774. HANDLE hData;
  775. char szRename[cchMaxSz];
  776. if (otOBJ_QUERY_TYPE(pPicInfo) != LINK)
  777. return(vcpLimParaCache);
  778. if (aOBJ_QUERY_DOCUMENT_LINK(pPicInfo) == aOldName)
  779. /* Change the link information */
  780. {
  781. if (bLinkProps && bOBJ_WAS_UPDATED(pPicInfo))
  782. return(vcpLimParaCache);
  783. if (!fUpdateAll)
  784. {
  785. char szTmp[sizeof(szRename) + 90];
  786. char szLink[30],szDocName[30];
  787. CHAR szDocPath[ cchMaxFile ];
  788. CHAR szFullPath[ cchMaxFile ];
  789. SplitSzFilename( (**((**hpdocdod)[doc].hszFile)), szDocPath, szDocName );
  790. GetAtomName(aOBJ_QUERY_DOCUMENT_LINK(pPicInfo),szFullPath,sizeof(szFullPath));
  791. SplitSzFilename( szFullPath, szDocPath, szLink );
  792. if (!szDocName[0])
  793. LoadString(hINSTANCE, IDSTRUntitledDef, szDocName, sizeof(szDocName));
  794. /* Ask the user if they want to update the links */
  795. if (vbChangeOther)
  796. LoadString(hINSTANCE, IDSTRRename, szRename, sizeof(szRename));
  797. else // update other
  798. LoadString(hINSTANCE, IDSTRUpdate, szRename, sizeof(szRename));
  799. /* cast cause compiler is screwing up */
  800. wsprintf((LPSTR)szTmp,(LPSTR)szRename,(LPSTR)szLink,(LPSTR)szDocName,(LPSTR)szLink);
  801. if (MessageBox(hPARENTWINDOW, szTmp, szAppName,
  802. MB_YESNO|MB_ICONEXCLAMATION) == IDNO)
  803. return cp0;
  804. ObjCachePara(doc,cpParaStart); // MessageBox screws things up
  805. fUpdateAll = TRUE;
  806. }
  807. if (vbChangeOther)
  808. {
  809. if (ObjBackupInDoc(pPicInfo,doc,cpParaStart))
  810. return ObjChangeLinkInDoc(pPicInfo,doc,cpParaStart);
  811. }
  812. else
  813. {
  814. if (ObjBackupInDoc(pPicInfo,doc,cpParaStart))
  815. return ObjUpdateObjectInDoc(pPicInfo,doc,cpParaStart);
  816. }
  817. }
  818. return(vcpLimParaCache);
  819. }
  820. typeCP ObjCloseObjectInDoc(OBJPICINFO *pPicInfo, int doc, typeCP cpParaStart)
  821. {
  822. if (lpOBJ_QUERY_INFO(pPicInfo) == NULL)
  823. return(cp0);
  824. if (lpOBJ_QUERY_OBJECT(pPicInfo) == NULL)
  825. return(vcpLimParaCache);
  826. if (otOBJ_QUERY_TYPE(pPicInfo) == STATIC) // nothing to close
  827. return(vcpLimParaCache);
  828. if (ObjWaitForObject(lpOBJ_QUERY_INFO(pPicInfo),TRUE))
  829. return cp0;
  830. /*
  831. Note that if this is an unfinished object, then the OLE_CLOSE
  832. (whenever it happens to arrive) will cause the picinfo to be
  833. deleted.
  834. */
  835. if (ObjError(OleClose(lpOBJ_QUERY_OBJECT(pPicInfo))))
  836. return(cp0);
  837. return(vcpLimParaCache);
  838. }
  839. /****************************************************************/
  840. /****************** OLE OBJECT DATA I/O *************************/
  841. /****************************************************************/
  842. static typeCP ObjWriteDataToDoc(LPOLEOBJECT lpObject)
  843. /*
  844. Return cp after end of paragraph we've created or 0 if error.
  845. Assume ObjStream is initialized.
  846. */
  847. {
  848. BOOL fSaveError = ferror;
  849. if (vfOutOfMemory || vfSysFull /*|| ObjStop*/)
  850. return cp0;
  851. Assert(!ferror);
  852. ferror = FALSE; /* so we can still call Replace(). */
  853. if (ObjError(OleSaveToStream(lpObject,lpStream)))
  854. {
  855. if (ferror)
  856. /* uh oh, hope we can clean up enough to look clean */
  857. {
  858. /* delete what we inserted if possible */
  859. ferror = FALSE;
  860. Replace(docStream, cpObjectDataBase - cchPICINFOX, cpObjectDataCurLoc - cpObjectDataBase + cchPICINFOX, fnNil, fc0, fc0);
  861. ferror = TRUE;
  862. return cp0;
  863. }
  864. }
  865. /* is this call necessary? Important to do if InsertRgch doesn't. */
  866. ObjCachePara(docStream,cpObjectDataBase - cchPICINFOX);
  867. ferror = fSaveError || ferror;
  868. return vcpLimParaCache; // cp after para we just inserted
  869. }
  870. /****************************************************************/
  871. /******************** OLE STREAM I/O ****************************/
  872. /****************************************************************/
  873. static
  874. ObjOpenStreamIO(typeCP cpParaStart, int doc, struct CHP *pchp, struct PAP *ppapGraph, DWORD dwObjectSize)
  875. {
  876. if (cpObjectDataCurLoc)
  877. ObjCloseStreamIO();
  878. cpObjectDataBase = cpObjectDataCurLoc = cpParaStart;
  879. cObjectData = 0L;
  880. dwDataMax = dwObjectSize;
  881. docStream = doc;
  882. pchpStream = pchp;
  883. ppapStream = ppapGraph;
  884. }
  885. static void
  886. ObjCloseStreamIO(void)
  887. {
  888. cpObjectDataBase = cpObjectDataCurLoc = cp0;
  889. }
  890. LONG FAR PASCAL BufReadStream(LPOLESTREAM lpStream, char huge *lpstr, DWORD cb)
  891. {
  892. DWORD dwRetval;
  893. typeCP cpMac = vcpLimParaCache;
  894. int cchRun;
  895. if ((cb + cpObjectDataCurLoc) > cpMac) // reading past end of para
  896. {
  897. Assert(0);
  898. return 0L;
  899. }
  900. for ( dwRetval = 0L;
  901. cb;
  902. cb -= cchRun,
  903. cObjectData += cchRun,
  904. cpObjectDataCurLoc += (typeCP) cchRun,
  905. dwRetval += cchRun)
  906. {
  907. CHAR rgch[ 255 ];
  908. register char *chT;
  909. register unsigned cchT;
  910. unsigned cch = ulmin(cb, 255L);
  911. FetchRgch( &cchRun, rgch, docStream, cpObjectDataCurLoc, cpMac, cch);
  912. if (ferror)
  913. return -dwRetval;
  914. for(chT = rgch,cchT=cchRun; cchT--; )
  915. *lpstr++ = *chT++;
  916. }
  917. return dwRetval;
  918. }
  919. LONG FAR PASCAL BufWriteStream(LPOLESTREAM lpStream, char huge *lpstr, DWORD cb)
  920. {
  921. DWORD dwRetval;
  922. char *chT;
  923. unsigned cchT;
  924. CHAR rgch[255];
  925. unsigned cch;
  926. struct PAP *ppap=NULL;
  927. for ( dwRetval = 0L;
  928. dwRetval < cb;
  929. cpObjectDataCurLoc += (typeCP) cch,
  930. cObjectData += cch,
  931. dwRetval += cch)
  932. {
  933. cch = ulmin(cb - dwRetval, 255L);
  934. for(chT = rgch,cchT=cch; cchT--;)
  935. *chT++ = *lpstr++;
  936. if ((cObjectData + cch) == dwDataMax)
  937. ppap = ppapStream;
  938. InsertRgch( docStream, cpObjectDataCurLoc, rgch, cch, pchpStream, ppap);
  939. if (ferror)
  940. return 0L;
  941. }
  942. return dwRetval;
  943. }
  944. ObjGetPicInfo(LPOLEOBJECT lpObject, int doc, OBJPICINFO *pPicInfo, typeCP *pcpParaStart)
  945. /* get picInfo that has lpObject */
  946. /* !!! since writing this it has occurred to me that a quicker way to do
  947. this would be to keep a list of pieces that point to objects. Pieces
  948. never */
  949. {
  950. OBJPICINFO picInfoT;
  951. typeCP cpStart,cpMac= CpMacText(doc);
  952. for (cpStart = cpNil; ObjPicEnumInRange(&picInfoT,doc,cp0,cpMac,&cpStart); )
  953. {
  954. if (lpOBJ_QUERY_INFO(&picInfoT) == NULL)
  955. continue;
  956. if (lpOBJ_QUERY_OBJECT(&picInfoT) == lpObject) // bingo
  957. {
  958. if (pPicInfo)
  959. *pPicInfo = picInfoT;
  960. if (pcpParaStart)
  961. *pcpParaStart = cpStart;
  962. return TRUE;
  963. }
  964. }
  965. return FALSE;
  966. }
  967. BOOL vfObjDisplaying=FALSE;
  968. BOOL ObjSetPicInfo(OBJPICINFO *pSrcPicInfo, int doc, typeCP cpParaStart)
  969. {
  970. /*
  971. NOTE that you only gotta call this when you change the OBJPICINFO fields:
  972. mm,
  973. objecttype,
  974. dwDataSize,
  975. dwObjNum, or
  976. lpObjInfo.
  977. */
  978. BOOL bError = FALSE;
  979. typeFC fcT;
  980. extern BOOL vfInvalid;
  981. BOOL vfSaveInvalid = vfInvalid;
  982. BOOL docDirty = (**hpdocdod) [doc].fDirty;
  983. ObjPushParms(doc);
  984. ObjCachePara(doc,cpParaStart);
  985. if (vfObjDisplaying)
  986. vfInvalid = FALSE; // this'll suppress things that mess up UpdateWw()
  987. bNoEol = TRUE;
  988. if (dwOBJ_QUERY_DATA_SIZE(pSrcPicInfo) == 0L)
  989. /* is this ever executed? */
  990. {
  991. struct CHP chp;
  992. /* problem is to retain graphics property of picinfo structure */
  993. GetChp(&chp, cpParaStart, doc); // calls CachePara
  994. NewChpIns(&chp);
  995. ObjUpdateFromObjInfo(pSrcPicInfo);
  996. pSrcPicInfo->mm |= MM_EXTENDED;
  997. InsertRgch( doc, cpParaStart + (typeCP)cchPICINFOX, pSrcPicInfo,
  998. (unsigned)cchPICINFOX, &chp, &vpapAbs );
  999. pSrcPicInfo->mm &= ~MM_EXTENDED;
  1000. if (ferror)
  1001. return TRUE;
  1002. /* delete old pieces (in front) that pointed to duped data */
  1003. Replace(doc, cpParaStart, (typeCP)cchPICINFOX, fnNil, fc0, fc0);
  1004. }
  1005. else
  1006. {
  1007. ObjUpdateFromObjInfo(pSrcPicInfo);
  1008. pSrcPicInfo->mm |= MM_EXTENDED;
  1009. fcT = FcWScratch( pSrcPicInfo, cchPICINFOX );
  1010. pSrcPicInfo->mm &= ~MM_EXTENDED;
  1011. if (ferror)
  1012. return TRUE;
  1013. Replace( doc, cpParaStart, (typeCP)cchPICINFOX,
  1014. fnScratch, fcT, (typeFC)cchPICINFOX);
  1015. }
  1016. if (ferror)
  1017. return TRUE;
  1018. bNoEol = FALSE;
  1019. if (vfObjDisplaying)
  1020. vfInvalid = vfSaveInvalid;
  1021. /* don't want this to affect docDirty flag */
  1022. (**hpdocdod) [doc].fDirty = docDirty;
  1023. ObjPopParms(TRUE);
  1024. return bError;
  1025. }
  1026. void ChangeOtherLinks(int doc, BOOL bChange, BOOL bPrompt)
  1027. /** For any items == aOldName, set to aNewName. Query user if OK first.
  1028. Assumes aOldName and aNewName are set. (See FixInvalidLink() and
  1029. ObjChangeLinkInDoc()).
  1030. bChange is TRUE if ChangeLink, FALSE if UpdateObject.
  1031. bPrompt is TRUe if prompt user for change.
  1032. **/
  1033. {
  1034. fUpdateAll = !bPrompt;
  1035. vbChangeOther = bChange; // TRUE to change links, FALSE to update links
  1036. ObjEnumInDoc(doc,ObjUpdateLinkInDoc);
  1037. }
  1038. BOOL ObjQueryNewLinkName(OBJPICINFO *pPicInfo,int doc,typeCP cpParaStart)
  1039. /** Return whether obtained new link name from user. Set aOldName
  1040. and aNewName. **/
  1041. {
  1042. HANDLE hData,hNewData=NULL;
  1043. LPSTR lpdata=NULL;
  1044. BOOL bRetval = FALSE;
  1045. OLESTATUS olestat=OLE_OK;
  1046. if (lpOBJ_QUERY_INFO(pPicInfo) == NULL)
  1047. return(FALSE);
  1048. if (otOBJ_QUERY_TYPE(pPicInfo) != LINK)
  1049. return(FALSE);
  1050. if (ObjMakeObjectReady(pPicInfo,doc,cpParaStart))
  1051. return(FALSE);
  1052. /* query user for new name */
  1053. olestat = ObjGetData(lpOBJ_QUERY_INFO(pPicInfo), vcfLink, &hData);
  1054. if ((olestat == OLE_WARN_DELETE_DATA) || (olestat == OLE_OK))
  1055. if (!(hNewData = OfnGetNewLinkName(hPARENTWINDOW, hData)))
  1056. goto end;
  1057. if (olestat == OLE_WARN_DELETE_DATA)
  1058. GlobalFree(hData);
  1059. aOldName = aOBJ_QUERY_DOCUMENT_LINK(pPicInfo);
  1060. lpdata=MAKELP(hNewData,0);
  1061. while (*lpdata++);
  1062. aNewName = AddAtom(lpdata);
  1063. bRetval = TRUE;
  1064. end:
  1065. if (olestat == OLE_WARN_DELETE_DATA)
  1066. GlobalFree(hData);
  1067. if (hNewData)
  1068. GlobalFree(hNewData);
  1069. return bRetval;
  1070. }
  1071. FixInvalidLink(OBJPICINFO far *lpPicInfo, int doc, typeCP cpParaStart)
  1072. /* returns FALSE if couldn't or wouldn't do anything, RETRY if reset link */
  1073. {
  1074. fOBJ_BADLINK(lpPicInfo) = TRUE;
  1075. if (DialogBox(hINSTANCE, "DTINVALIDLINK",
  1076. hPARENTWINDOW, lpfnInvalidLink) == IDD_CHANGE)
  1077. #if !defined(SMALL_OLE_UI)
  1078. fnObjProperties();
  1079. #else
  1080. ObjChangeLinkInDoc(lpPicInfo,doc,cpParaStart);
  1081. #endif
  1082. return FALSE;
  1083. }
  1084. /* ObjMakeNewLinkName() - Constructs a new link name from an atom.
  1085. */
  1086. static HANDLE ObjMakeNewLinkName(HANDLE hData, ATOM atom)
  1087. {
  1088. BOOL fSuccess = FALSE;
  1089. HANDLE hData2 = NULL;
  1090. HANDLE hData3 = NULL;
  1091. LPSTR lpstrData = NULL;
  1092. LPSTR lpstrLink = NULL;
  1093. LPSTR lpstrTemp;
  1094. char szFile[CBPATHMAX];
  1095. if (!GetAtomName(atom, szFile, CBPATHMAX)
  1096. || !(lpstrData = (LPSTR)GlobalLock(hData))
  1097. || !(hData2 = GlobalAlloc(GMEM_DDESHARE | GMEM_ZEROINIT, CBPATHMAX * 2))
  1098. || !(lpstrLink = lpstrTemp = (LPSTR)GlobalLock(hData2)))
  1099. goto Error;
  1100. /* ... copy the server name */
  1101. while (*lpstrTemp++ = *lpstrData++);
  1102. /* ... copy the document name */
  1103. lstrcpy(lpstrTemp, szFile);
  1104. lpstrTemp += lstrlen(lpstrTemp) + 1;
  1105. lpstrData += lstrlen(lpstrData) + 1;
  1106. /* ... copy the item name */
  1107. while (*lpstrTemp++ = *lpstrData++);
  1108. *lpstrTemp = 0;
  1109. /* ... and compress the memory block to minimal size */
  1110. GlobalUnlock(hData2);
  1111. hData3 = GlobalReAlloc(hData2, (DWORD)(lpstrTemp - lpstrLink + 1), 0);
  1112. if (!hData3)
  1113. hData3 = hData2;
  1114. fSuccess = TRUE;
  1115. Error:
  1116. if (!fSuccess) {
  1117. if (lpstrLink)
  1118. GlobalUnlock(hData2);
  1119. if (hData2)
  1120. GlobalFree(hData2);
  1121. hData3 = NULL;
  1122. }
  1123. if (lpstrData)
  1124. GlobalUnlock(hData);
  1125. return hData3;
  1126. }
  1127. char *ObjGetServerName(LPOLEOBJECT lpObject, char *szServerName)
  1128. {
  1129. LPSTR lpstrData;
  1130. HANDLE hData;
  1131. LONG otobject;
  1132. OLESTATUS olestat;
  1133. /** NOTE: OleGetData can return OLE_BUSY. Because of how
  1134. ObjGetServerName is used, we're not going to wait for the
  1135. object here, we'll just return if its busy **/
  1136. if (OleQueryReleaseStatus(lpObject) == OLE_BUSY)
  1137. return NULL;
  1138. if (ObjError(OleQueryType(lpObject,&otobject)))
  1139. return NULL;
  1140. olestat = OleGetData(lpObject,
  1141. otobject == OT_LINK ? vcfLink : vcfOwnerLink,
  1142. &hData);
  1143. if ((olestat != OLE_WARN_DELETE_DATA) && (olestat != OLE_OK))
  1144. {
  1145. ObjError(olestat);
  1146. return NULL;
  1147. }
  1148. lpstrData = MAKELP(hData,0);
  1149. RegGetClassId(szServerName, lpstrData);
  1150. if (olestat == OLE_WARN_DELETE_DATA)
  1151. GlobalFree(hData);
  1152. return szServerName;
  1153. }
  1154. /* OfnInit() - Initializes the standard file dialog OFN structure.
  1155. */
  1156. void OfnInit(HANDLE hInst) {
  1157. LPSTR lpstr;
  1158. OFN.lStructSize = sizeof(OPENFILENAME);
  1159. OFN.hInstance = hInst;
  1160. OFN.nMaxCustFilter = CBFILTERMAX;
  1161. OFN.nMaxFile = CBPATHMAX;
  1162. OFN.Flags = OFN_HIDEREADONLY;
  1163. OFN.lCustData = NULL;
  1164. OFN.lpfnHook = NULL;
  1165. OFN.lpTemplateName = NULL;
  1166. OFN.lpstrDefExt = NULL;
  1167. OFN.lpstrFileTitle = NULL;
  1168. LoadString(hInst, IDSTRChangelink, szLinkCaption, sizeof(szLinkCaption));
  1169. }
  1170. /* OfnGetNewLinkName() - Sets up the "Change Link..." dialog box
  1171. */
  1172. static HANDLE OfnGetNewLinkName(HWND hwnd, HANDLE hData)
  1173. {
  1174. BOOL fSuccess = FALSE;
  1175. HANDLE hData2 = NULL;
  1176. HANDLE hData3 = NULL;
  1177. LPSTR lpData2 = NULL;
  1178. LPSTR lpstrData = NULL;
  1179. LPSTR lpstrFile = NULL;
  1180. LPSTR lpstrLink = NULL;
  1181. LPSTR lpstrPath = NULL;
  1182. LPSTR lpstrTemp = NULL;
  1183. char szDocFile[CBPATHMAX];
  1184. char szDocPath[CBPATHMAX];
  1185. HANDLE hServerFilter=NULL;
  1186. /* Get the link information */
  1187. if (!(lpstrData = GlobalLock(hData)))
  1188. goto Error;
  1189. /* Figure out the link's path name and file name */
  1190. lpstrTemp = lpstrData;
  1191. while (*lpstrTemp++);
  1192. lpstrPath = lpstrFile = lpstrTemp;
  1193. while (*(lpstrTemp = AnsiNext(lpstrTemp)))
  1194. if (*lpstrTemp == '\\')
  1195. lpstrFile = lpstrTemp + 1;
  1196. /* Copy the document name */
  1197. lstrcpy(szDocFile, lpstrFile);
  1198. *(lpstrFile - 1) = 0;
  1199. /* Copy the path name */
  1200. lstrcpy(szDocPath, ((lpstrPath != lpstrFile) ? lpstrPath : ""));
  1201. if (lpstrPath != lpstrFile) /* Restore the backslash */
  1202. *(lpstrFile - 1) = '\\';
  1203. while (*lpstrFile != '.' && *lpstrFile) /* Get the extension */
  1204. lpstrFile++;
  1205. /* Make a filter that respects the link's class name */
  1206. OFN.hwndOwner = hwnd;
  1207. OFN.nFilterIndex = RegMakeFilterSpec(lpstrData, lpstrFile, &hServerFilter);
  1208. if (OFN.nFilterIndex == -1)
  1209. goto Error;
  1210. OFN.lpstrFilter = (LPSTR)MAKELP(hServerFilter,0);
  1211. Normalize(szDocFile);
  1212. OFN.lpstrFile = (LPSTR)szDocFile;
  1213. OFN.lpstrInitialDir = (LPSTR)szDocPath;
  1214. OFN.lpstrTitle = (LPSTR)szLinkCaption;
  1215. OFN.lpstrCustomFilter = (LPSTR)szCustFilterSpec;
  1216. /* If we get a file... */
  1217. if (GetOpenFileName((LPOPENFILENAME)&OFN))
  1218. {
  1219. if (!(hData2 = GlobalAlloc(GMEM_DDESHARE | GMEM_ZEROINIT, CBPATHMAX * 2))
  1220. || !(lpstrLink = lpstrTemp = GlobalLock(hData2)))
  1221. goto Error;
  1222. /* ... copy the server name */
  1223. while (*lpstrTemp++ = *lpstrData++);
  1224. /* ... copy the document name */
  1225. lstrcpy(lpstrTemp, szDocFile);
  1226. lpstrTemp += lstrlen(lpstrTemp) + 1;
  1227. lpstrData += lstrlen(lpstrData) + 1;
  1228. /* ... copy the item name */
  1229. while (*lpstrTemp++ = *lpstrData++);
  1230. *lpstrTemp = 0;
  1231. /* ... and compress the memory block to minimal size */
  1232. GlobalUnlock(hData2);
  1233. hData3 = GlobalReAlloc(hData2, (DWORD)(lpstrTemp - lpstrLink + 1), 0);
  1234. if (!hData3)
  1235. hData3 = hData2;
  1236. fSuccess = TRUE;
  1237. }
  1238. Error:
  1239. if (!fSuccess)
  1240. {
  1241. if (lpstrLink)
  1242. GlobalUnlock(hData2);
  1243. if (hData2)
  1244. GlobalFree(hData2);
  1245. hData3 = NULL;
  1246. }
  1247. if (lpstrData)
  1248. GlobalUnlock(hData);
  1249. if (hServerFilter)
  1250. GlobalFree(hServerFilter);
  1251. return hData3;
  1252. }
  1253. /* Normalize() - Removes the path specification from the file name.
  1254. *
  1255. * Note: It isn't possible to get "<drive>:<filename>" as input because
  1256. * the path received will always be fully qualified.
  1257. */
  1258. static void Normalize(LPSTR lpstrFile)
  1259. {
  1260. LPSTR lpstrBackslash = NULL;
  1261. LPSTR lpstrTemp = lpstrFile;
  1262. while (*lpstrTemp) {
  1263. if (*lpstrTemp == '\\')
  1264. lpstrBackslash = lpstrTemp;
  1265. lpstrTemp = AnsiNext(lpstrTemp);
  1266. }
  1267. if (lpstrBackslash)
  1268. lstrcpy(lpstrFile, lpstrBackslash + 1);
  1269. }
  1270. /* ObjSetUpdateOptions() - Sets the update options of the object.
  1271. *
  1272. * Returns: TRUE iff the command completed successfully
  1273. */
  1274. BOOL ObjSetUpdateOptions(OBJPICINFO *pPicInfo, WORD wParam, int doc, typeCP cpParaStart)
  1275. /* !!! Used by Link Properties only!!! Object guaranteed loaded */
  1276. {
  1277. if (ObjWaitForObject(lpOBJ_QUERY_INFO(pPicInfo),TRUE))
  1278. {
  1279. Error(IDPMTFailedToUpdateLink);
  1280. return FALSE;
  1281. }
  1282. if (ObjError(OleSetLinkUpdateOptions(lpOBJ_QUERY_OBJECT(pPicInfo),
  1283. (wParam == IDD_AUTO) ? oleupdate_always : oleupdate_oncall)))
  1284. {
  1285. Error(IDPMTFailedToUpdateLink);
  1286. return FALSE;
  1287. }
  1288. fOBJ_QUERY_DIRTY_OBJECT(pPicInfo) = TRUE;
  1289. return TRUE;
  1290. }
  1291. /* ObjGetUpdateOptions() - Retrieves the update options of the object.
  1292. */
  1293. OLEOPT_UPDATE ObjGetUpdateOptions(OBJPICINFO far *lpPicInfo)
  1294. /* !!! Used by Link Properties only!!! Object guaranteed loaded */
  1295. {
  1296. BOOL fSuccess = FALSE;
  1297. OLEOPT_UPDATE fUpdate;
  1298. if (otOBJ_QUERY_TYPE(lpPicInfo) == LINK)
  1299. {
  1300. if (ObjWaitForObject(lpOBJ_QUERY_INFO(lpPicInfo),TRUE))
  1301. return FALSE;
  1302. fSuccess = !ObjError(OleGetLinkUpdateOptions(lpOBJ_QUERY_OBJECT(lpPicInfo), &fUpdate));
  1303. }
  1304. return (fSuccess ? fUpdate : oleupdate_onsave);
  1305. }
  1306. OLESTATUS ObjGetData(LPOBJINFO lpObjInfo, OLECLIPFORMAT cf, HANDLE far *lphData)
  1307. /*
  1308. Return olestat.
  1309. Put handle to data into lphData.
  1310. Assumes object is loaded.
  1311. */
  1312. {
  1313. HANDLE hData;
  1314. OLESTATUS olestat;
  1315. if (ObjWaitForObject(lpObjInfo,TRUE))
  1316. return OLE_BUSY;
  1317. olestat = OleGetData(lpObjInfo->lpobject, cf, lphData);
  1318. if ((olestat != OLE_WARN_DELETE_DATA) && (olestat != OLE_OK))
  1319. ObjError(olestat);
  1320. return olestat;
  1321. }
  1322. /* ObjSetData() - Set the object's (link) information
  1323. Sets DOCUMENT_LINK ATOM in pPicInfo
  1324. */
  1325. BOOL ObjSetData(OBJPICINFO far *lpPicInfo, OLECLIPFORMAT cf, HANDLE hData)
  1326. /* assumes object is loaded */
  1327. {
  1328. HANDLE hitem;
  1329. LPSTR lpdata;
  1330. if (ObjWaitForObject(lpOBJ_QUERY_INFO(lpPicInfo),TRUE))
  1331. return FALSE;
  1332. if (ObjError(OleSetData(lpOBJ_QUERY_OBJECT(lpPicInfo), cf, hData)))
  1333. return FALSE;
  1334. /* If we have a link, update the document name */
  1335. if (cf == vcfLink && (lpdata = GlobalLock(hData)))
  1336. {
  1337. ATOM aSaveOld = aOBJ_QUERY_DOCUMENT_LINK(lpPicInfo);
  1338. while (*lpdata++);
  1339. aOBJ_QUERY_DOCUMENT_LINK(lpPicInfo) = AddAtom(lpdata);
  1340. if (aSaveOld)
  1341. DeleteAtom(aSaveOld);
  1342. GlobalUnlock(hData);
  1343. }
  1344. else
  1345. return FALSE;
  1346. return TRUE;
  1347. }
  1348. int ObjSetSelectionType(int doc, typeCP cpFirst, typeCP cpLim)
  1349. {
  1350. /* set whether link or emb selected */
  1351. OBJ_SELECTIONTYPE = NONE; // this'll be set by ObjCheckObjectTypes()
  1352. //OBJ_CEMBEDS = 0; // this'll be set by ObjCheckObjectTypes()
  1353. return ObjEnumInRange(doc,cpFirst,cpLim,ObjCheckObjectTypes);
  1354. }
  1355. BOOL ObjQueryCpIsObject(int doc,typeCP cpFirst)
  1356. {
  1357. OBJPICINFO picInfo;
  1358. /* assume its cached already! */
  1359. //ObjCachePara(doc,cpFirst); /* NOTE side effect of caching */
  1360. if (!vpapAbs.fGraphics)
  1361. return FALSE;
  1362. if (cpFirst >= CpMacText(doc))
  1363. return FALSE;
  1364. GetPicInfo(cpFirst,cpFirst + cchPICINFOX, doc, &picInfo);
  1365. return bOBJ_QUERY_IS_OBJECT(&picInfo);
  1366. }
  1367. ATOM MakeLinkAtom(LPOBJINFO lpObjInfo)
  1368. {
  1369. HANDLE hData;
  1370. LPSTR lpdata;
  1371. ATOM aRetval=NULL;
  1372. OLESTATUS olestat=OLE_OK;
  1373. olestat = ObjGetData(lpObjInfo, vcfLink, &hData);
  1374. if ((olestat == OLE_WARN_DELETE_DATA) || (olestat == OLE_OK))
  1375. {
  1376. lpdata = MAKELP(hData,0);
  1377. while (*lpdata++);
  1378. aRetval = AddAtom(lpdata);
  1379. if (olestat == OLE_WARN_DELETE_DATA)
  1380. GlobalFree(hData);
  1381. }
  1382. return aRetval;
  1383. }
  1384. #include <time.h>
  1385. void ObjGetObjectName(LPOBJINFO lpObjInfo, szOBJNAME szObjName)
  1386. /* put object name from ObjInfo into szObjName */
  1387. {
  1388. if (szObjName && lpObjInfo)
  1389. GetAtomName(lpObjInfo->aObjName,szObjName,sizeof(szObjName));
  1390. }
  1391. void ObjMakeObjectName(LPOBJINFO lpObjInfo, LPSTR lpstr)
  1392. {
  1393. szOBJNAME szObjName;
  1394. time_t lTime;
  1395. time(&lTime);
  1396. wsprintf(szObjName, "%lx", lTime);
  1397. if (lpObjInfo)
  1398. lpObjInfo->aObjName = AddAtom(szObjName);
  1399. if (lpstr)
  1400. lstrcpy(lpstr,szObjName);
  1401. }
  1402. static void GetChp(struct CHP *pchp, typeCP cp, int doc)
  1403. {
  1404. /**
  1405. Return chp at cp in *pchp.
  1406. Resets Cache to cp when done.
  1407. We assume that we're always inserting after an EOL or at beginning of
  1408. document.
  1409. **/
  1410. extern struct CHP vchpAbs;
  1411. if (cp == cp0) // beginning of doc
  1412. {
  1413. typeCP cpMac = CpMacText(doc);
  1414. if (cpMac == cp0) // empty doc
  1415. {
  1416. /* force default character properties, font size to be 10 point */
  1417. *pchp = vchpNormal;
  1418. pchp->hps = hpsDefault;
  1419. return;
  1420. }
  1421. else // get next char props
  1422. {
  1423. ObjCachePara(doc,cp+1); // to reset
  1424. FetchCp( doc, cp+1, 0, fcmProps );
  1425. }
  1426. }
  1427. else
  1428. {
  1429. ObjCachePara(doc,cp-1); // to reset
  1430. FetchCp( doc, cp-1, 0, fcmProps ); // previous paragraph's chp
  1431. }
  1432. *pchp = vchpAbs;
  1433. if (pchp->fSpecial && pchp->hpsPos != 0)
  1434. { /* if this char is a footnote or page marker, then ignore */
  1435. pchp->hpsPos = 0; /* super/subscript stuff. */
  1436. pchp->hps = HpsAlter(pchp->hps, 1);
  1437. }
  1438. pchp->fSpecial = fFalse;
  1439. ObjCachePara(doc,cp); // to reset
  1440. }
  1441. BOOL ObjSetHostName(LPOBJINFO lpOInfo, int doc)
  1442. /* TRUE if error */
  1443. /* we assume object ain't busy!!! */
  1444. {
  1445. extern CHAR szUntitled[20];
  1446. CHAR *PchStartBaseNameSz(),*szTitle= **((**hpdocdod)[doc].hszFile);
  1447. if (szTitle[0])
  1448. szTitle = PchStartBaseNameSz(szTitle);
  1449. else
  1450. szTitle = szUntitled;
  1451. #ifdef DEBUG
  1452. OutputDebugString( (LPSTR) "Setting host name\n\r");
  1453. #endif
  1454. /*
  1455. Note that OleSetHostNames can return OLE_BUSY!!! So you
  1456. better call ObjWaitForObject() first.
  1457. */
  1458. if (ObjError(OleSetHostNames(lpOInfo->lpobject,szAppName,szTitle)))
  1459. return TRUE;
  1460. return FALSE;
  1461. }
  1462. BOOL ObjMakeObjectReady(OBJPICINFO *pPicInfo, int doc, typeCP cpParaStart)
  1463. /* Load object, complete async. Return whether an error. */
  1464. {
  1465. if (lpOBJ_QUERY_INFO(pPicInfo) == NULL)
  1466. return TRUE;
  1467. if (lpOBJ_QUERY_OBJECT(pPicInfo) == NULL)
  1468. {
  1469. if (ObjLoadObjectInDoc(pPicInfo,doc,cpParaStart) == cp0)
  1470. return TRUE;
  1471. }
  1472. else if (ObjWaitForObject(lpOBJ_QUERY_INFO(pPicInfo),TRUE))
  1473. return TRUE;
  1474. ObjCachePara(doc, cpParaStart);
  1475. return FALSE;
  1476. }
  1477.