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