summaryrefslogtreecommitdiffstats
path: root/private/oleutest/letest/outline/outlntbl.c
blob: e4263c9fe3c723700fe474f265b336761ca4b070 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
/*************************************************************************
**
**    OLE 2 Sample Code
**
**    outlntbl.c
**
**    This file contains OutlineNameTable functions.
**
**    (c) Copyright Microsoft Corp. 1992 - 1993 All Rights Reserved
**
*************************************************************************/


#include "outline.h"

OLEDBGDATA

extern LPOUTLINEAPP g_lpApp;

char ErrMsgNameTable[] = "Can't create NameTable!";


/* OutlineNameTable_Init
 * ---------------------
 *
 *      initialize a name table.
 */
BOOL OutlineNameTable_Init(LPOUTLINENAMETABLE lpOutlineNameTable, LPOUTLINEDOC lpOutlineDoc)
{
	HWND lpParent = OutlineDoc_GetWindow(lpOutlineDoc);

	lpOutlineNameTable->m_nCount = 0;

	/* We will use an OwnerDraw listbox as our data structure to
	**    maintain the table of Names. this listbox will never be made
	**    visible. the listbox is just a convenient data structure to
	**    manage a collection.
	*/
	lpOutlineNameTable->m_hWndListBox = CreateWindow(
					"listbox",              /* Window class name           */
					NULL,                   /* Window's title              */
					WS_CHILDWINDOW |
					LBS_OWNERDRAWFIXED,
					0, 0,                   /* Use default X, Y            */
					0, 0,                   /* Use default X, Y            */
					lpParent,               /* Parent window's handle      */
					(HMENU)IDC_NAMETABLE,   /* Child Window ID             */
					g_lpApp->m_hInst,       /* Instance of window          */
					NULL);                  /* Create struct for WM_CREATE */

	if (! lpOutlineNameTable->m_hWndListBox) {
		OutlineApp_ErrorMessage(g_lpApp, ErrMsgNameTable);
		return FALSE;
	}

	return TRUE;
}


/* OutlineNameTable_Destroy
 * ------------------------
 *
 *      Free memory used by the name table.
 */
void OutlineNameTable_Destroy(LPOUTLINENAMETABLE lpOutlineNameTable)
{
	// Delete all names
	OutlineNameTable_ClearAll(lpOutlineNameTable);

	DestroyWindow(lpOutlineNameTable->m_hWndListBox);
	Delete(lpOutlineNameTable);
}


/* OutlineNameTable_AddName
 * ------------------------
 *
 *      Add a name to the table
 */
void OutlineNameTable_AddName(LPOUTLINENAMETABLE lpOutlineNameTable, LPOUTLINENAME lpOutlineName)
{
	SendMessage(
			lpOutlineNameTable->m_hWndListBox,
			LB_ADDSTRING,
			0,
			(DWORD)lpOutlineName
	);
	lpOutlineNameTable->m_nCount++;
}


/* OutlineNameTable_DeleteName
 * ---------------------------
 *
 *      Delete a name from table
 */
void OutlineNameTable_DeleteName(LPOUTLINENAMETABLE lpOutlineNameTable,int nIndex)
{
	LPOUTLINENAME lpOutlineName = OutlineNameTable_GetName(lpOutlineNameTable, nIndex);

#if defined( OLE_SERVER )
	/* OLE2NOTE: if there is a pseudo object attached to this name, it
	**    must first be closed before deleting the Name. this will
	**    cause OnClose notification to be sent to all linking clients.
	*/
	ServerName_ClosePseudoObj((LPSERVERNAME)lpOutlineName);
#endif

	if (lpOutlineName)
		Delete(lpOutlineName);      // free memory for name

	SendMessage(
			lpOutlineNameTable->m_hWndListBox,
			LB_DELETESTRING,
			(WPARAM)nIndex,
			0L
	);
	lpOutlineNameTable->m_nCount--;
}


/* OutlineNameTable_GetNameIndex
 * -----------------------------
 *
 *      Return the index of the Name given a pointer to the Name.
 *      Return -1 if the Name is not found.
 */
int OutlineNameTable_GetNameIndex(LPOUTLINENAMETABLE lpOutlineNameTable, LPOUTLINENAME lpOutlineName)
{
	LRESULT lReturn;

	if (! lpOutlineName) return -1;

	lReturn = SendMessage(
			lpOutlineNameTable->m_hWndListBox,
			LB_FINDSTRING,
			(WPARAM)-1,
			(LPARAM)(LPCSTR)lpOutlineName
		);

	return ((lReturn == LB_ERR) ? -1 : (int)lReturn);
}


/* OutlineNameTable_GetName
 * ------------------------
 *
 *      Retrieve the pointer to the Name given its index in the NameTable
 */
LPOUTLINENAME OutlineNameTable_GetName(LPOUTLINENAMETABLE lpOutlineNameTable, int nIndex)
{
	LPOUTLINENAME lpOutlineName = NULL;
    LRESULT lResult;

	if (lpOutlineNameTable->m_nCount == 0 ||
		nIndex > lpOutlineNameTable->m_nCount ||
		nIndex < 0) {
		return NULL;
	}

	lResult = SendMessage(
			lpOutlineNameTable->m_hWndListBox,
			LB_GETTEXT,
			nIndex,
			(LPARAM)(LPCSTR)&lpOutlineName
	);
    OleDbgAssert(lResult != LB_ERR);
	return lpOutlineName;
}


/* OutlineNameTable_FindName
 * -------------------------
 *
 *      Find a name in the name table given a string.
 */
LPOUTLINENAME OutlineNameTable_FindName(LPOUTLINENAMETABLE lpOutlineNameTable, LPSTR lpszName)
{
	LPOUTLINENAME lpOutlineName;
	BOOL fFound = FALSE;
	int i;

	for (i = 0; i < lpOutlineNameTable->m_nCount; i++) {
		lpOutlineName = OutlineNameTable_GetName(lpOutlineNameTable, i);
		if (lstrcmp(lpOutlineName->m_szName, lpszName) == 0) {
			fFound = TRUE;
			break;      // FOUND MATCH!
		}
	}

	return (fFound ? lpOutlineName : NULL);
}


/* OutlineNameTable_FindNamedRange
 * -------------------------------
 *
 *      Find a name in the name table which matches a given line range.
 */
LPOUTLINENAME OutlineNameTable_FindNamedRange(LPOUTLINENAMETABLE lpOutlineNameTable, LPLINERANGE lplrSel)
{
	LPOUTLINENAME lpOutlineName;
	BOOL fFound = FALSE;
	int i;

	for (i = 0; i < lpOutlineNameTable->m_nCount; i++) {
		lpOutlineName = OutlineNameTable_GetName(lpOutlineNameTable, i);
		if ((lpOutlineName->m_nStartLine == lplrSel->m_nStartLine) &&
			(lpOutlineName->m_nEndLine == lplrSel->m_nEndLine) ) {
			fFound = TRUE;
			break;      // FOUND MATCH!
		}
	}

	return (fFound ? lpOutlineName : NULL);
}


/* OutlineNameTable_GetCount
 * -------------------------
 *
 * Return number of names in nametable
 */
int OutlineNameTable_GetCount(LPOUTLINENAMETABLE lpOutlineNameTable)
{
	if (!lpOutlineNameTable)
		return 0;

	return lpOutlineNameTable->m_nCount;
}


/* OutlineNameTable_ClearAll
 * -------------------------
 *
 *      Remove all names from table
 */
void OutlineNameTable_ClearAll(LPOUTLINENAMETABLE lpOutlineNameTable)
{
	LPOUTLINENAME lpOutlineName;
	int i;
	int nCount = lpOutlineNameTable->m_nCount;

	for (i = 0; i < nCount; i++) {
		lpOutlineName = OutlineNameTable_GetName(lpOutlineNameTable, i);
		Delete(lpOutlineName);      // free memory for name
	}

	lpOutlineNameTable->m_nCount = 0;
	SendMessage(lpOutlineNameTable->m_hWndListBox,LB_RESETCONTENT,0,0L);
}


/* OutlineNameTable_AddLineUpdate
 * ------------------------------
 *
 *      Update table when a new line is added at nAddIndex
 * The line used to be at nAddIndex is pushed down
 */
void OutlineNameTable_AddLineUpdate(LPOUTLINENAMETABLE lpOutlineNameTable, int nAddIndex)
{
	LPOUTLINENAME lpOutlineName;
	LINERANGE lrSel;
	int i;
	BOOL fRangeModified = FALSE;

	for(i = 0; i < lpOutlineNameTable->m_nCount; i++) {
		lpOutlineName=OutlineNameTable_GetName(lpOutlineNameTable, i);
		OutlineName_GetSel(lpOutlineName, &lrSel);

		if((int)lrSel.m_nStartLine > nAddIndex) {
			lrSel.m_nStartLine++;
			fRangeModified = !fRangeModified;
		}
		if((int)lrSel.m_nEndLine > nAddIndex) {
			lrSel.m_nEndLine++;
			fRangeModified = !fRangeModified;
		}

		OutlineName_SetSel(lpOutlineName, &lrSel, fRangeModified);
	}
}


/* OutlineNameTable_DeleteLineUpdate
 * ---------------------------------
 *
 *      Update the table when a line at nDeleteIndex is removed
 */
void OutlineNameTable_DeleteLineUpdate(LPOUTLINENAMETABLE lpOutlineNameTable, int nDeleteIndex)
{
	LPOUTLINENAME lpOutlineName;
	LINERANGE lrSel;
	int i;
	BOOL fRangeModified = FALSE;

	for(i = 0; i < lpOutlineNameTable->m_nCount; i++) {
		lpOutlineName=OutlineNameTable_GetName(lpOutlineNameTable, i);
		OutlineName_GetSel(lpOutlineName, &lrSel);

		if((int)lrSel.m_nStartLine > nDeleteIndex) {
			lrSel.m_nStartLine--;
			fRangeModified = !fRangeModified;
		}
		if((int)lrSel.m_nEndLine >= nDeleteIndex) {
			lrSel.m_nEndLine--;
			fRangeModified = !fRangeModified;
		}

		// delete the name if its entire range is deleted
		if(lrSel.m_nStartLine > lrSel.m_nEndLine) {
			OutlineNameTable_DeleteName(lpOutlineNameTable, i);
			i--;  // re-examine this name
		} else {
			OutlineName_SetSel(lpOutlineName, &lrSel, fRangeModified);
		}
	}
}


/* OutlineNameTable_SaveSelToStg
 * -----------------------------
 *
 *      Save only the names that refer to lines completely contained in the
 * specified selection range.
 */
BOOL OutlineNameTable_SaveSelToStg(
		LPOUTLINENAMETABLE      lpOutlineNameTable,
		LPLINERANGE             lplrSel,
		UINT                    uFormat,
		LPSTREAM                lpNTStm
)
{
	HRESULT hrErr;
	ULONG nWritten;
	LPOUTLINENAME lpOutlineName;
	short nNameCount = 0;
	BOOL fNameSaved;
	BOOL fStatus;
	int i;
	LARGE_INTEGER dlibZeroOffset;
	LISet32( dlibZeroOffset, 0 );

	/* initially write 0 for count of names. the correct count will be
	**    written at the end when we know how many names qualified to
	**    be written (within the selection).
	*/
	hrErr = lpNTStm->lpVtbl->Write(
			lpNTStm,
			(short FAR*)&nNameCount,
			sizeof(nNameCount),
			&nWritten
	);
	if (hrErr != NOERROR) {
		OleDbgOutHResult("Write NameTable header returned", hrErr);
		goto error;
    }

	for(i = 0; i < lpOutlineNameTable->m_nCount; i++) {
		lpOutlineName=OutlineNameTable_GetName(lpOutlineNameTable, i);
		fStatus = OutlineName_SaveToStg(
				lpOutlineName,
				lplrSel,
				uFormat,
				lpNTStm,
				(BOOL FAR*)&fNameSaved
		);
		if (! fStatus) goto error;
		if (fNameSaved) nNameCount++;
	}

	/* write the final count of names written. */
	hrErr = lpNTStm->lpVtbl->Seek(
			lpNTStm,
			dlibZeroOffset,
			STREAM_SEEK_SET,
			NULL
	);
	if (hrErr != NOERROR) {
		OleDbgOutHResult("Seek to NameTable header returned", hrErr);
		goto error;
    }

	hrErr = lpNTStm->lpVtbl->Write(
			lpNTStm,
			(short FAR*)&nNameCount,
			sizeof(nNameCount),
			&nWritten
	);
	if (hrErr != NOERROR) {
		OleDbgOutHResult("Write NameTable count in header returned", hrErr);
		goto error;
    }

	OleStdRelease((LPUNKNOWN)lpNTStm);
	return TRUE;

error:
	if (lpNTStm)
		OleStdRelease((LPUNKNOWN)lpNTStm);

	return FALSE;
}


/* OutlineNameTable_LoadFromStg
 * ----------------------------
 *
 *      Load Name Table from file
 *
 *      Return TRUE if ok, FALSE if error
 */
BOOL OutlineNameTable_LoadFromStg(LPOUTLINENAMETABLE lpOutlineNameTable, LPSTORAGE lpSrcStg)
{
	LPOUTLINEAPP lpOutlineApp = (LPOUTLINEAPP)g_lpApp;
	HRESULT hrErr;
	IStream FAR* lpNTStm;
	ULONG nRead;
	short nCount;
	LPOUTLINENAME lpOutlineName;
	BOOL fStatus;
	short i;

	hrErr = CallIStorageOpenStreamA(
			lpSrcStg,
			"NameTable",
			NULL,
			STGM_READ | STGM_SHARE_EXCLUSIVE,
			0,
			&lpNTStm
	);

	if (hrErr != NOERROR) {
		OleDbgOutHResult("OpenStream NameTable returned", hrErr);
		goto error;
    }

	hrErr = lpNTStm->lpVtbl->Read(lpNTStm,&nCount,sizeof(nCount),&nRead);
	if (hrErr != NOERROR) {
		OleDbgOutHResult("Read NameTable header returned", hrErr);
		goto error;
    }

	for (i = 0; i < nCount; i++) {
		lpOutlineName = OutlineApp_CreateName(lpOutlineApp);
		if (! lpOutlineName) goto error;
		fStatus = OutlineName_LoadFromStg(lpOutlineName, lpNTStm);
		if (! fStatus) goto error;
		OutlineNameTable_AddName(lpOutlineNameTable, lpOutlineName);
	}

	OleStdRelease((LPUNKNOWN)lpNTStm);
	return TRUE;

error:
	if (lpNTStm)
		OleStdRelease((LPUNKNOWN)lpNTStm);

	return FALSE;
}