Leaked source code of windows server 2003
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.

460 lines
11 KiB

  1. /*************************************************************************
  2. **
  3. ** OLE 2 Sample Code
  4. **
  5. ** outlntbl.c
  6. **
  7. ** This file contains OutlineNameTable functions.
  8. **
  9. ** (c) Copyright Microsoft Corp. 1992 - 1993 All Rights Reserved
  10. **
  11. *************************************************************************/
  12. #include "outline.h"
  13. OLEDBGDATA
  14. extern LPOUTLINEAPP g_lpApp;
  15. char ErrMsgNameTable[] = "Can't create NameTable!";
  16. /* OutlineNameTable_Init
  17. * ---------------------
  18. *
  19. * initialize a name table.
  20. */
  21. BOOL OutlineNameTable_Init(LPOUTLINENAMETABLE lpOutlineNameTable, LPOUTLINEDOC lpOutlineDoc)
  22. {
  23. HWND lpParent = OutlineDoc_GetWindow(lpOutlineDoc);
  24. lpOutlineNameTable->m_nCount = 0;
  25. /* We will use an OwnerDraw listbox as our data structure to
  26. ** maintain the table of Names. this listbox will never be made
  27. ** visible. the listbox is just a convenient data structure to
  28. ** manage a collection.
  29. */
  30. lpOutlineNameTable->m_hWndListBox = CreateWindow(
  31. "listbox", /* Window class name */
  32. NULL, /* Window's title */
  33. WS_CHILDWINDOW |
  34. LBS_OWNERDRAWFIXED,
  35. 0, 0, /* Use default X, Y */
  36. 0, 0, /* Use default X, Y */
  37. lpParent, /* Parent window's handle */
  38. (HMENU)IDC_NAMETABLE, /* Child Window ID */
  39. g_lpApp->m_hInst, /* Instance of window */
  40. NULL); /* Create struct for WM_CREATE */
  41. if (! lpOutlineNameTable->m_hWndListBox) {
  42. OutlineApp_ErrorMessage(g_lpApp, ErrMsgNameTable);
  43. return FALSE;
  44. }
  45. return TRUE;
  46. }
  47. /* OutlineNameTable_Destroy
  48. * ------------------------
  49. *
  50. * Free memory used by the name table.
  51. */
  52. void OutlineNameTable_Destroy(LPOUTLINENAMETABLE lpOutlineNameTable)
  53. {
  54. // Delete all names
  55. OutlineNameTable_ClearAll(lpOutlineNameTable);
  56. DestroyWindow(lpOutlineNameTable->m_hWndListBox);
  57. Delete(lpOutlineNameTable);
  58. }
  59. /* OutlineNameTable_AddName
  60. * ------------------------
  61. *
  62. * Add a name to the table
  63. */
  64. void OutlineNameTable_AddName(LPOUTLINENAMETABLE lpOutlineNameTable, LPOUTLINENAME lpOutlineName)
  65. {
  66. SendMessage(
  67. lpOutlineNameTable->m_hWndListBox,
  68. LB_ADDSTRING,
  69. 0,
  70. (DWORD)lpOutlineName
  71. );
  72. lpOutlineNameTable->m_nCount++;
  73. }
  74. /* OutlineNameTable_DeleteName
  75. * ---------------------------
  76. *
  77. * Delete a name from table
  78. */
  79. void OutlineNameTable_DeleteName(LPOUTLINENAMETABLE lpOutlineNameTable,int nIndex)
  80. {
  81. LPOUTLINENAME lpOutlineName = OutlineNameTable_GetName(lpOutlineNameTable, nIndex);
  82. #if defined( OLE_SERVER )
  83. /* OLE2NOTE: if there is a pseudo object attached to this name, it
  84. ** must first be closed before deleting the Name. this will
  85. ** cause OnClose notification to be sent to all linking clients.
  86. */
  87. ServerName_ClosePseudoObj((LPSERVERNAME)lpOutlineName);
  88. #endif
  89. if (lpOutlineName)
  90. Delete(lpOutlineName); // free memory for name
  91. SendMessage(
  92. lpOutlineNameTable->m_hWndListBox,
  93. LB_DELETESTRING,
  94. (WPARAM)nIndex,
  95. 0L
  96. );
  97. lpOutlineNameTable->m_nCount--;
  98. }
  99. /* OutlineNameTable_GetNameIndex
  100. * -----------------------------
  101. *
  102. * Return the index of the Name given a pointer to the Name.
  103. * Return -1 if the Name is not found.
  104. */
  105. int OutlineNameTable_GetNameIndex(LPOUTLINENAMETABLE lpOutlineNameTable, LPOUTLINENAME lpOutlineName)
  106. {
  107. LRESULT lReturn;
  108. if (! lpOutlineName) return -1;
  109. lReturn = SendMessage(
  110. lpOutlineNameTable->m_hWndListBox,
  111. LB_FINDSTRING,
  112. (WPARAM)-1,
  113. (LPARAM)(LPCSTR)lpOutlineName
  114. );
  115. return ((lReturn == LB_ERR) ? -1 : (int)lReturn);
  116. }
  117. /* OutlineNameTable_GetName
  118. * ------------------------
  119. *
  120. * Retrieve the pointer to the Name given its index in the NameTable
  121. */
  122. LPOUTLINENAME OutlineNameTable_GetName(LPOUTLINENAMETABLE lpOutlineNameTable, int nIndex)
  123. {
  124. LPOUTLINENAME lpOutlineName = NULL;
  125. LRESULT lResult;
  126. if (lpOutlineNameTable->m_nCount == 0 ||
  127. nIndex > lpOutlineNameTable->m_nCount ||
  128. nIndex < 0) {
  129. return NULL;
  130. }
  131. lResult = SendMessage(
  132. lpOutlineNameTable->m_hWndListBox,
  133. LB_GETTEXT,
  134. nIndex,
  135. (LPARAM)(LPCSTR)&lpOutlineName
  136. );
  137. OleDbgAssert(lResult != LB_ERR);
  138. return lpOutlineName;
  139. }
  140. /* OutlineNameTable_FindName
  141. * -------------------------
  142. *
  143. * Find a name in the name table given a string.
  144. */
  145. LPOUTLINENAME OutlineNameTable_FindName(LPOUTLINENAMETABLE lpOutlineNameTable, LPSTR lpszName)
  146. {
  147. LPOUTLINENAME lpOutlineName;
  148. BOOL fFound = FALSE;
  149. int i;
  150. for (i = 0; i < lpOutlineNameTable->m_nCount; i++) {
  151. lpOutlineName = OutlineNameTable_GetName(lpOutlineNameTable, i);
  152. if (lstrcmp(lpOutlineName->m_szName, lpszName) == 0) {
  153. fFound = TRUE;
  154. break; // FOUND MATCH!
  155. }
  156. }
  157. return (fFound ? lpOutlineName : NULL);
  158. }
  159. /* OutlineNameTable_FindNamedRange
  160. * -------------------------------
  161. *
  162. * Find a name in the name table which matches a given line range.
  163. */
  164. LPOUTLINENAME OutlineNameTable_FindNamedRange(LPOUTLINENAMETABLE lpOutlineNameTable, LPLINERANGE lplrSel)
  165. {
  166. LPOUTLINENAME lpOutlineName;
  167. BOOL fFound = FALSE;
  168. int i;
  169. for (i = 0; i < lpOutlineNameTable->m_nCount; i++) {
  170. lpOutlineName = OutlineNameTable_GetName(lpOutlineNameTable, i);
  171. if ((lpOutlineName->m_nStartLine == lplrSel->m_nStartLine) &&
  172. (lpOutlineName->m_nEndLine == lplrSel->m_nEndLine) ) {
  173. fFound = TRUE;
  174. break; // FOUND MATCH!
  175. }
  176. }
  177. return (fFound ? lpOutlineName : NULL);
  178. }
  179. /* OutlineNameTable_GetCount
  180. * -------------------------
  181. *
  182. * Return number of names in nametable
  183. */
  184. int OutlineNameTable_GetCount(LPOUTLINENAMETABLE lpOutlineNameTable)
  185. {
  186. if (!lpOutlineNameTable)
  187. return 0;
  188. return lpOutlineNameTable->m_nCount;
  189. }
  190. /* OutlineNameTable_ClearAll
  191. * -------------------------
  192. *
  193. * Remove all names from table
  194. */
  195. void OutlineNameTable_ClearAll(LPOUTLINENAMETABLE lpOutlineNameTable)
  196. {
  197. LPOUTLINENAME lpOutlineName;
  198. int i;
  199. int nCount = lpOutlineNameTable->m_nCount;
  200. for (i = 0; i < nCount; i++) {
  201. lpOutlineName = OutlineNameTable_GetName(lpOutlineNameTable, i);
  202. Delete(lpOutlineName); // free memory for name
  203. }
  204. lpOutlineNameTable->m_nCount = 0;
  205. SendMessage(lpOutlineNameTable->m_hWndListBox,LB_RESETCONTENT,0,0L);
  206. }
  207. /* OutlineNameTable_AddLineUpdate
  208. * ------------------------------
  209. *
  210. * Update table when a new line is added at nAddIndex
  211. * The line used to be at nAddIndex is pushed down
  212. */
  213. void OutlineNameTable_AddLineUpdate(LPOUTLINENAMETABLE lpOutlineNameTable, int nAddIndex)
  214. {
  215. LPOUTLINENAME lpOutlineName;
  216. LINERANGE lrSel;
  217. int i;
  218. BOOL fRangeModified = FALSE;
  219. for(i = 0; i < lpOutlineNameTable->m_nCount; i++) {
  220. lpOutlineName=OutlineNameTable_GetName(lpOutlineNameTable, i);
  221. OutlineName_GetSel(lpOutlineName, &lrSel);
  222. if((int)lrSel.m_nStartLine > nAddIndex) {
  223. lrSel.m_nStartLine++;
  224. fRangeModified = !fRangeModified;
  225. }
  226. if((int)lrSel.m_nEndLine > nAddIndex) {
  227. lrSel.m_nEndLine++;
  228. fRangeModified = !fRangeModified;
  229. }
  230. OutlineName_SetSel(lpOutlineName, &lrSel, fRangeModified);
  231. }
  232. }
  233. /* OutlineNameTable_DeleteLineUpdate
  234. * ---------------------------------
  235. *
  236. * Update the table when a line at nDeleteIndex is removed
  237. */
  238. void OutlineNameTable_DeleteLineUpdate(LPOUTLINENAMETABLE lpOutlineNameTable, int nDeleteIndex)
  239. {
  240. LPOUTLINENAME lpOutlineName;
  241. LINERANGE lrSel;
  242. int i;
  243. BOOL fRangeModified = FALSE;
  244. for(i = 0; i < lpOutlineNameTable->m_nCount; i++) {
  245. lpOutlineName=OutlineNameTable_GetName(lpOutlineNameTable, i);
  246. OutlineName_GetSel(lpOutlineName, &lrSel);
  247. if((int)lrSel.m_nStartLine > nDeleteIndex) {
  248. lrSel.m_nStartLine--;
  249. fRangeModified = !fRangeModified;
  250. }
  251. if((int)lrSel.m_nEndLine >= nDeleteIndex) {
  252. lrSel.m_nEndLine--;
  253. fRangeModified = !fRangeModified;
  254. }
  255. // delete the name if its entire range is deleted
  256. if(lrSel.m_nStartLine > lrSel.m_nEndLine) {
  257. OutlineNameTable_DeleteName(lpOutlineNameTable, i);
  258. i--; // re-examine this name
  259. } else {
  260. OutlineName_SetSel(lpOutlineName, &lrSel, fRangeModified);
  261. }
  262. }
  263. }
  264. /* OutlineNameTable_SaveSelToStg
  265. * -----------------------------
  266. *
  267. * Save only the names that refer to lines completely contained in the
  268. * specified selection range.
  269. */
  270. BOOL OutlineNameTable_SaveSelToStg(
  271. LPOUTLINENAMETABLE lpOutlineNameTable,
  272. LPLINERANGE lplrSel,
  273. UINT uFormat,
  274. LPSTREAM lpNTStm
  275. )
  276. {
  277. HRESULT hrErr;
  278. ULONG nWritten;
  279. LPOUTLINENAME lpOutlineName;
  280. short nNameCount = 0;
  281. BOOL fNameSaved;
  282. BOOL fStatus;
  283. int i;
  284. LARGE_INTEGER dlibZeroOffset;
  285. LISet32( dlibZeroOffset, 0 );
  286. /* initially write 0 for count of names. the correct count will be
  287. ** written at the end when we know how many names qualified to
  288. ** be written (within the selection).
  289. */
  290. hrErr = lpNTStm->lpVtbl->Write(
  291. lpNTStm,
  292. (short FAR*)&nNameCount,
  293. sizeof(nNameCount),
  294. &nWritten
  295. );
  296. if (hrErr != NOERROR) {
  297. OleDbgOutHResult("Write NameTable header returned", hrErr);
  298. goto error;
  299. }
  300. for(i = 0; i < lpOutlineNameTable->m_nCount; i++) {
  301. lpOutlineName=OutlineNameTable_GetName(lpOutlineNameTable, i);
  302. fStatus = OutlineName_SaveToStg(
  303. lpOutlineName,
  304. lplrSel,
  305. uFormat,
  306. lpNTStm,
  307. (BOOL FAR*)&fNameSaved
  308. );
  309. if (! fStatus) goto error;
  310. if (fNameSaved) nNameCount++;
  311. }
  312. /* write the final count of names written. */
  313. hrErr = lpNTStm->lpVtbl->Seek(
  314. lpNTStm,
  315. dlibZeroOffset,
  316. STREAM_SEEK_SET,
  317. NULL
  318. );
  319. if (hrErr != NOERROR) {
  320. OleDbgOutHResult("Seek to NameTable header returned", hrErr);
  321. goto error;
  322. }
  323. hrErr = lpNTStm->lpVtbl->Write(
  324. lpNTStm,
  325. (short FAR*)&nNameCount,
  326. sizeof(nNameCount),
  327. &nWritten
  328. );
  329. if (hrErr != NOERROR) {
  330. OleDbgOutHResult("Write NameTable count in header returned", hrErr);
  331. goto error;
  332. }
  333. OleStdRelease((LPUNKNOWN)lpNTStm);
  334. return TRUE;
  335. error:
  336. if (lpNTStm)
  337. OleStdRelease((LPUNKNOWN)lpNTStm);
  338. return FALSE;
  339. }
  340. /* OutlineNameTable_LoadFromStg
  341. * ----------------------------
  342. *
  343. * Load Name Table from file
  344. *
  345. * Return TRUE if ok, FALSE if error
  346. */
  347. BOOL OutlineNameTable_LoadFromStg(LPOUTLINENAMETABLE lpOutlineNameTable, LPSTORAGE lpSrcStg)
  348. {
  349. LPOUTLINEAPP lpOutlineApp = (LPOUTLINEAPP)g_lpApp;
  350. HRESULT hrErr;
  351. IStream FAR* lpNTStm;
  352. ULONG nRead;
  353. short nCount;
  354. LPOUTLINENAME lpOutlineName;
  355. BOOL fStatus;
  356. short i;
  357. hrErr = CallIStorageOpenStreamA(
  358. lpSrcStg,
  359. "NameTable",
  360. NULL,
  361. STGM_READ | STGM_SHARE_EXCLUSIVE,
  362. 0,
  363. &lpNTStm
  364. );
  365. if (hrErr != NOERROR) {
  366. OleDbgOutHResult("OpenStream NameTable returned", hrErr);
  367. goto error;
  368. }
  369. hrErr = lpNTStm->lpVtbl->Read(lpNTStm,&nCount,sizeof(nCount),&nRead);
  370. if (hrErr != NOERROR) {
  371. OleDbgOutHResult("Read NameTable header returned", hrErr);
  372. goto error;
  373. }
  374. for (i = 0; i < nCount; i++) {
  375. lpOutlineName = OutlineApp_CreateName(lpOutlineApp);
  376. if (! lpOutlineName) goto error;
  377. fStatus = OutlineName_LoadFromStg(lpOutlineName, lpNTStm);
  378. if (! fStatus) goto error;
  379. OutlineNameTable_AddName(lpOutlineNameTable, lpOutlineName);
  380. }
  381. OleStdRelease((LPUNKNOWN)lpNTStm);
  382. return TRUE;
  383. error:
  384. if (lpNTStm)
  385. OleStdRelease((LPUNKNOWN)lpNTStm);
  386. return FALSE;
  387. }