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.

348 lines
11 KiB

  1. /*
  2. * stream.c - io stream function callbacks
  3. *
  4. * Created by Microsoft Corporation.
  5. * (c) Copyright Microsoft Corp. 1990 - 1992 All Rights Reserved
  6. */
  7. /***************************************************************************
  8. * This file contains all routines that directly and indirectly deal with
  9. * file i/o. The OLE stream call back functions exist in this file.
  10. **************************************************************************/
  11. //*** INCLUDES ***
  12. #include <windows.h>
  13. #include <ole.h>
  14. #include "global.h"
  15. #include "utility.h"
  16. #include "stream.h"
  17. #include "object.h"
  18. #include "demorc.h"
  19. //*** Globals ***
  20. BOOL fLoadFile = FALSE;
  21. /***************************************************************************
  22. * ReadStream() - OLE Callback Function (Get)
  23. *
  24. * This function is pointed to from the OLESTREAM vtbl; it is Get.
  25. *
  26. * returns DWORD - number of bytes actually read
  27. **************************************************************************/
  28. DWORD APIENTRY ReadStream( //* ENTRY:
  29. LPAPPSTREAM lpStream, //* application stream pointer
  30. LPSTR lpstr, //* string pointer
  31. DWORD cb //* byte count
  32. ){
  33. return _lread(lpStream->fh, lpstr, cb);
  34. }
  35. /***************************************************************************
  36. * WriteStream() - OLE Callback function (Put)
  37. *
  38. * This function is pointed to from the OLESTREAM vtbl; it is Put.
  39. *
  40. * Returns DWORD - number of bytes actually written
  41. **************************************************************************/
  42. DWORD APIENTRY WriteStream( //* ENTRY:
  43. LPAPPSTREAM lpStream, //* application stream pointer
  44. LPSTR lpstr, //* string pointer
  45. DWORD cb //* number of bytes to write
  46. ){
  47. return _lwrite(lpStream->fh, lpstr, cb);
  48. }
  49. /****************************************************************************
  50. * ReadFromFile()
  51. *
  52. * This function reads OLE objects from a file. If the document
  53. * contains manual links, the user will be prompted to update those links.
  54. *
  55. * Returns BOOL - TRUE if the read(s) were successful
  56. ***************************************************************************/
  57. BOOL FAR ReadFromFile( //* ENTRY:
  58. LPAPPSTREAM lpStream, //* application stream pointer
  59. LHCLIENTDOC lhcDoc, //* document handle
  60. LPOLECLIENT lpClient //* pointer to OLE client structure
  61. ){ //* LOCAL:
  62. BOOL bReturn = FALSE; //* return value
  63. INT cFileObjects; //* number of file objects
  64. Hourglass(TRUE);
  65. fLoadFile = TRUE;
  66. SetFilePointer((HANDLE)lpStream->fh, 0, NULL, 0);
  67. //* in the file
  68. if (_lread(lpStream->fh, (LPSTR)&cFileObjects, sizeof(INT)) < sizeof(INT))
  69. goto Error;
  70. for (; cFileObjects; --cFileObjects)
  71. {
  72. if (!ObjRead(lpStream,lhcDoc,lpClient))
  73. {
  74. ErrorMessage(E_FAILED_TO_READ_OBJECT);
  75. goto Error;
  76. }
  77. }
  78. ShowDoc(lhcDoc,1);
  79. UpdateLinks(lhcDoc);
  80. bReturn = TRUE; //* SUCCESS
  81. Error: //* ERROR Tag
  82. Hourglass(FALSE);
  83. fLoadFile = FALSE;
  84. return bReturn; //* return
  85. }
  86. /****************************************************************************
  87. * ObjRead()
  88. *
  89. * Rread an object from the specified file. The file pointer will
  90. * be advanced past the object.
  91. *
  92. * HANDLE fh - DOS file handle of file to be read from
  93. *
  94. * returns HWND - window handle to item window containing the OLE object
  95. ***************************************************************************/
  96. BOOL FAR ObjRead( //* ENTRY:
  97. LPAPPSTREAM lpStream, //* application stream pointer
  98. LHCLIENTDOC lhcDoc, //* document handle
  99. LPOLECLIENT lpClient //* pointer to OLE client structure
  100. ){ //* LOCAL:
  101. APPITEMPTR pItem; //* application item pointer
  102. LPOLEOBJECT lpObject; //* pointer ole object
  103. LONG otObject; //* type of object
  104. RECT rcObject; //* object rect
  105. CHAR szTmp[CBOBJNAMEMAX]; //* temporary string buffer
  106. CHAR szProto[PROTOCOL_STRLEN+1];//* protocol string
  107. INT i; //* index
  108. if (_lread(lpStream->fh, szTmp, CBOBJNAMEMAX) < CBOBJNAMEMAX )
  109. return FALSE;
  110. if (_lread(lpStream->fh, szProto, PROTOCOL_STRLEN) < PROTOCOL_STRLEN )
  111. return FALSE;
  112. for (i=0; szProto[i] != ' '; i++);
  113. szProto[i] = 0;
  114. ValidateName( szTmp );
  115. if (!(pItem = PreItemCreate(lpClient, TRUE, lhcDoc)))
  116. return FALSE;
  117. if (Error(OleLoadFromStream((LPOLESTREAM)&(lpStream->olestream),
  118. szProto,(LPOLECLIENT)&(pItem->oleclient), lhcDoc, szTmp, &lpObject)))
  119. goto Error;
  120. if (_lread(lpStream->fh, (LPSTR)&rcObject, sizeof(RECT)) < sizeof(RECT))
  121. goto Error;
  122. if (_lread(lpStream->fh, (LPSTR)&otObject, sizeof(LONG)) < sizeof(LONG))
  123. goto Error;
  124. if (PostItemCreate(lpObject, otObject, &rcObject, pItem))
  125. {
  126. pItem->fNew = TRUE;
  127. ObjSetBounds(pItem);
  128. return TRUE; //* SUCCESS return
  129. }
  130. else
  131. return FALSE;
  132. Error: //* ERROR Tag
  133. FreeAppItem(pItem);
  134. return FALSE;
  135. }
  136. /*************************************************************************
  137. * WriteToFile()
  138. *
  139. * Write current document to a file.
  140. *
  141. * returns BOOL - TRUE if file successfully written
  142. ************************************************************************/
  143. BOOL FAR WriteToFile( //* ENTRY:
  144. LPAPPSTREAM lpStream //* application stream pointer
  145. ){ //* LOCAL:
  146. INT iObjectsWritten=0; //* counter of objects written to file
  147. APPITEMPTR pItem; //* application Item pointer
  148. UpdateFromOpenServers();
  149. SetFilePointer((HANDLE)lpStream->fh, 0, NULL, 0);
  150. Hourglass(TRUE);
  151. if (_lwrite(lpStream->fh, (LPSTR)&iObjects, sizeof(INT)) < sizeof(INT))
  152. goto Error;
  153. for (pItem = GetTopItem(); pItem; pItem = GetNextItem(pItem))
  154. {
  155. if (!ObjWrite(lpStream, pItem))
  156. goto Error;
  157. iObjectsWritten++;
  158. }
  159. if (iObjectsWritten != iObjects)
  160. goto Error;
  161. Dirty(DOC_CLEAN);
  162. Hourglass(FALSE);
  163. return(TRUE); //* SUCCESS return
  164. Error: //* ERROR Tag
  165. Hourglass(FALSE);
  166. return(FALSE); //* ERROR return
  167. }
  168. /****************************************************************************
  169. * ObjWrite()
  170. *
  171. * This function writes an object to the specified
  172. * file. The file pointer will be advanced past the end of
  173. * the written object.
  174. * Returns BOOL - TRUE if object written successfully
  175. ***************************************************************************/
  176. BOOL FAR ObjWrite( //* ENTRY:
  177. LPAPPSTREAM lpStream, //* application stream pointer
  178. APPITEMPTR pItem //* application item pointer
  179. ){ //* LOCAL:
  180. POINT pt; //* center of rec point
  181. RECT rc; //* bounding rectangle
  182. UINT cbTmp = CBOBJNAMEMAX;
  183. CHAR szTmp[PROTOCOL_STRLEN];//* protocol string
  184. OleQueryName(pItem->lpObject, szTmp, &cbTmp);
  185. if (_lwrite(lpStream->fh, szTmp, CBOBJNAMEMAX) < CBOBJNAMEMAX )
  186. return FALSE;
  187. if (pItem->otObject == OT_STATIC)
  188. wsprintf(szTmp, "%-15s", STATICP);
  189. else
  190. wsprintf(szTmp, "%-15s", STDFILEEDITING);
  191. if (_lwrite(lpStream->fh, szTmp, PROTOCOL_STRLEN) < PROTOCOL_STRLEN )
  192. return FALSE;
  193. if (Error(OleSaveToStream(pItem->lpObject, (LPOLESTREAM)&(lpStream->olestream))))
  194. return FALSE;
  195. GetClientRect(pItem->hwnd, (LPRECT)&rc);
  196. pt = *(LPPOINT)&rc;
  197. ClientToScreen(pItem->hwnd, (LPPOINT)&pt);
  198. ScreenToClient(hwndFrame, (LPPOINT)&pt);
  199. OffsetRect(
  200. &rc,
  201. pt.x - rc.left - GetSystemMetrics(SM_CXFRAME),
  202. pt.y - rc.top - GetSystemMetrics(SM_CYFRAME)
  203. );
  204. if (_lwrite(lpStream->fh, (LPSTR)&rc, sizeof(RECT)) < sizeof(RECT)
  205. || _lwrite(lpStream->fh, (LPSTR)&(pItem->otObject), sizeof(LONG)) < sizeof(LONG))
  206. return FALSE;
  207. return TRUE; //* SUCCESS return
  208. }
  209. /****************************************************************************
  210. * UpdateLinks()
  211. *
  212. * Get the most up to date rendering information and show it.
  213. ***************************************************************************/
  214. static VOID UpdateLinks( //* ENTRY
  215. LHCLIENTDOC lhcDoc //* client document handle
  216. ){ //* LOCAL:
  217. INT i=0; //* index
  218. APPITEMPTR pItem; //* temporary item pointer
  219. CHAR szUpdate[CBMESSAGEMAX];//* update message?
  220. for (pItem = GetTopItem(); pItem; pItem = GetNextItem(pItem))
  221. {
  222. if (pItem->lhcDoc == lhcDoc && pItem->otObject == OT_LINK)
  223. {
  224. if (!i)
  225. {
  226. LoadString(hInst, IDS_UPDATELINKS, szUpdate, CBMESSAGEMAX);
  227. if (MessageBox(hwndFrame, szUpdate, szAppName,
  228. MB_YESNO | MB_ICONEXCLAMATION) != IDYES)
  229. break;
  230. i++;
  231. }
  232. Error(OleUpdate(pItem->lpObject));
  233. }
  234. }
  235. WaitForAllObjects();
  236. }
  237. /****************************************************************************
  238. * UpdateFromOpenServers()
  239. *
  240. * Get the most up to date rendering information before storing it.
  241. ***************************************************************************/
  242. static VOID UpdateFromOpenServers(VOID)
  243. { //* LOCAL:
  244. APPITEMPTR pItem; //* temporary item pointer
  245. APPITEMPTR pItemNext;
  246. for (pItem = GetTopItem(); pItem; pItem = pItemNext)
  247. {
  248. pItemNext = GetNextItem(pItem);
  249. if (pItem->otObject == OT_EMBEDDED ||
  250. (pItem->uoObject == oleupdate_oncall
  251. && pItem->otObject == OT_LINK ))
  252. if (OleQueryOpen(pItem->lpObject) == OLE_OK)
  253. {
  254. CHAR szMessage[2*CBMESSAGEMAX];
  255. CHAR szBuffer[CBMESSAGEMAX];
  256. UINT cb = CBOBJNAMEMAX; //* The name will be the server window title.
  257. CHAR szTmp[CBOBJNAMEMAX]; //* when the object is edited.
  258. Error(OleQueryName(pItem->lpObject,szTmp,&cb));
  259. LoadString(hInst, IDS_UPDATE_OBJ, szBuffer, CBMESSAGEMAX);
  260. wsprintf(szMessage, szBuffer, (LPSTR)szTmp);
  261. if (MessageBox(hwndFrame, szMessage, szAppName, MB_YESNO | MB_ICONEXCLAMATION) == IDYES)
  262. {
  263. Error(OleUpdate(pItem->lpObject));
  264. WaitForObject(pItem);
  265. }
  266. if (!pItem->fVisible)
  267. ObjDelete(pItem, OLE_OBJ_DELETE);
  268. }
  269. }
  270. WaitForAllObjects();
  271. }