|
|
1.1 root 1: /*----------------------------------------------------------------------------*\
2: | edit.c - routines for dealing with multi-line edit controls |
3: | |
4: | |
5: | History: |
6: | 01/01/88 toddla Created |
7: | 11/04/90 w-dougb Commented & formatted the code to look pretty |
8: | |
9: \*----------------------------------------------------------------------------*/
10:
11: /*----------------------------------------------------------------------------*\
12: | |
13: | i n c l u d e f i l e s |
14: | |
15: \*----------------------------------------------------------------------------*/
16:
17: #include <windows.h>
18: #include "gmem.h"
19: #include "edit.h"
20: #include "mcitest.h"
21:
22: /*----------------------------------------------------------------------------*\
23: | |
24: | c o n s t a n t a n d m a c r o d e f i n i t i o n s |
25: | |
26: \*----------------------------------------------------------------------------*/
27:
28: #define ISSPACE(c) ((c) == ' ' || (c) == '\t')
29: #define ISEOL(c) ((c) == '\n'|| (c) == '\r')
30: #define ISWHITE(c) (ISSPACE(c) || ISEOL(c))
31:
32:
33: /*----------------------------------------------------------------------------*\
34: | EditOpenFile(hwndEdit, lszFile) |
35: | |
36: | Description: |
37: | This function opens the file <lszFile>, copies the contents of the |
38: | file into the edit control with the handle <hwndEdit>, and then closes |
39: | the file. |
40: | |
41: | Arguments: |
42: | hwndEdit window handle of the edit box control |
43: | lszFile filename of the file to be opened |
44: | |
45: | Returns: |
46: | TRUE if the operation was successful, else FALSE |
47: | |
48: \*----------------------------------------------------------------------------*/
49:
50: BOOL EditOpenFile(
51: HWND hwndEdit,
52: LPSTR lszFile)
53: {
54: OFSTRUCT of; /* structure used by the OpenFile routine */
55: HFILE fh; /* DOS file handle returned by OpenFile */
56: LPSTR lszText; /* pointer to the opened file's text */
57: UINT nFileLen; /* length, in bytes, of the opened file */
58: HCURSOR hcur; /* handle to the pre-hourglass cursor */
59:
60: /* If a valid window handle or a filename was not specified, then exit.
61: */
62: if (!hwndEdit || !lszFile) {
63: dprintf1("EditOpenFile: Invalid window or filename");
64: return FALSE;
65: }
66:
67: /* Open the file for reading */
68:
69: fh = OpenFile(lszFile, &of, OF_READ);
70: if (fh == HFILE_ERROR) {
71: dprintf1("Failed to open: %s", lszFile);
72: return FALSE;
73: }
74:
75: nFileLen = (UINT)GetFileSize((HANDLE)fh, NULL);
76: if (HFILE_ERROR == nFileLen) {
77: dprintf1("Failed to find file size: %s", lszFile);
78: return FALSE;
79: }
80:
81: /*
82: * Create a pointer to a region of memory large enough to hold the entire
83: * contents of the file. If this was successful, then read the file into
84: * this region, and use this region as the text for the edit control.
85: * Finally, free up the region and its pointer, and close the file.
86: *
87: */
88:
89: lszText = (LPTSTR)GAllocPtr(nFileLen+1);
90: if (NULL != lszText) {
91:
92: BOOL fReturn;
93: /* This could take a while - show the hourglass cursor */
94:
95: hcur = SetCursor(LoadCursor(NULL, IDC_WAIT));
96:
97: /* Read the file and copy the contents into the edit control */
98:
99: dprintf3("reading file...");
100:
101: if ((_lread(fh, lszText, nFileLen)) == nFileLen) {
102: lszText[nFileLen]=0;
103: dprintf2("File loaded ok");
104: SetWindowText(hwndEdit, lszText);
105: fReturn = TRUE;
106: } else {
107: dprintf2("Error loading file");
108: fReturn = FALSE;
109: }
110:
111: /* Free up the memory, close the file, and restore the cursor */
112:
113: GFreePtr(lszText);
114: _lclose(fh);
115: SetCursor(hcur);
116:
117: return fReturn;
118: }
119:
120: /*
121: * We couldn't allocate the required memory, so close the file and
122: * return FALSE.
123: *
124: */
125:
126: dprintf1("Failed memory allocation for file");
127: _lclose(fh);
128: return FALSE;
129: }
130:
131:
132: /*----------------------------------------------------------------------------*\
133: | EditSaveFile(hwndEdit, lszFile) |
134: | |
135: | Description: |
136: | This function saves the contents of the edit control with the handle |
137: | <hwndEdit> into the file <lszFile>, creating the file if required. |
138: | |
139: | Arguments: |
140: | hwndEdit window handle of the edit box control |
141: | lszFile filename of the file to be saved |
142: | |
143: | Returns: |
144: | TRUE if the operation was successful, else FALSE |
145: | |
146: \*----------------------------------------------------------------------------*/
147:
148: BOOL EditSaveFile(
149: HWND hwndEdit,
150: LPSTR lszFile)
151: {
152: OFSTRUCT of; /* structure used by the OpenFile routine */
153: HFILE fh; /* DOS file handle returned by OpenFile */
154: LPSTR lszText; /* pointer to the saved file's text */
155: int nFileLen; /* length, in bytes, of the saved file */
156: HCURSOR hcur; /* handle to the pre-hourglass cursor */
157:
158: /* If a valid window handle or a filename was not specified, then exit */
159:
160: if (!hwndEdit || !lszFile) {
161: dprintf1("EditSaveFile: Invalid window or filename");
162: return FALSE;
163: }
164:
165: /* Create (or overwrite) the save file */
166:
167: fh = OpenFile(lszFile, &of, OF_CREATE);
168: if (fh == HFILE_ERROR) {
169: dprintf1("EditSaveFile: Error opening file");
170: return FALSE;
171: }
172:
173: /* Find out how big the contents of the edit box are */
174:
175: nFileLen = GetWindowTextLength(hwndEdit) + 1;
176:
177: /*
178: * Create a pointer to a region of memory large enough to hold the entire
179: * contents of the edit box. If this was successful, then read the contents
180: * of the edit box into this region, and write the contents of this region
181: * into the save file. Finally, free up the region and its pointer, and
182: * close the file.
183: *
184: */
185:
186: lszText = GAllocPtr(nFileLen);
187: if (NULL != lszText) {
188:
189: /* This could take a while - show the hourglass cursor */
190:
191: hcur = SetCursor(LoadCursor(NULL, IDC_WAIT));
192:
193: /* Read the contents of the edit box, and write it to the save file */
194:
195: GetWindowText(hwndEdit, lszText, nFileLen);
196: _lwrite(fh, lszText, nFileLen);
197:
198: /* Free up the memory, close the file, and restore the cursor */
199:
200: GFreePtr(lszText);
201: _lclose(fh);
202: SetCursor(hcur);
203:
204: return TRUE;
205: }
206:
207: /*
208: * We couldn't allocate the required memory, so close the file and
209: * return FALSE.
210: */
211:
212: _lclose(fh);
213: return FALSE;
214: }
215:
216:
217: /*----------------------------------------------------------------------------*\
218: | EditGetLineCount(hwndEdit) |
219: | |
220: | Description: |
221: | This function finds out how many lines of text are in the edit box and |
222: | returns this value. |
223: | |
224: | Arguments: |
225: | hwndEdit window handle of the edit box control |
226: | |
227: | Returns: |
228: | The number of lines of text in the edit control. |
229: | |
230: \*----------------------------------------------------------------------------*/
231:
232: DWORD EditGetLineCount(
233: HWND hwndEdit)
234: {
235: return SendMessage(hwndEdit, EM_GETLINECOUNT, 0, 0L);
236: }
237:
238:
239: /*----------------------------------------------------------------------------*\
240: | EditGetLine(hwndEdit, iLine, lszLineBuffer, cch) |
241: | |
242: | Description: |
243: | This function retrieves the contents of line # <iLine> from the edit |
244: | box control with handle <hwndEdit>. If <iLine> is out of range (that |
245: | number line does not exist in the multi line edit field) then FALSE is |
246: | returned. Otherwise the line is copied into the buffer pointed to by |
247: | <lszLineBuffer> with white space removed. |
248: | The string is also null terminated even if it means truncation. |
249: | |
250: | Arguments: |
251: | hwndEdit window handle of the edit box control |
252: | iLine line # to get the contents of |
253: | lszLineBuffer pointer to the buffer to copy the line to |
254: | cch max # of characters to copy (MIN value on entry==2) |
255: | |
256: | Returns: |
257: | TRUE. |
258: | |
259: \*----------------------------------------------------------------------------*/
260:
261: BOOL EditGetLine(
262: HWND hwndEdit,
263: int iLine,
264: LPSTR lszLineBuffer,
265: int cch)
266: {
267: int nLines; /* total number of lines in the edit box */
268:
269: /*
270: * Find out how many lines are in the edit control. If the requested line
271: * is out of range, then return.
272: */
273:
274: nLines = (int)EditGetLineCount(hwndEdit);
275: if (iLine < 0 || iLine >= nLines) {
276: dprintf1("Requested line count %d is out of range (%d)", iLine, nLines);
277: return *lszLineBuffer = 0;
278: /* This sets the buffer to null and returns FALSE */
279: }
280:
281: /* Read the requested line into the string pointed to by <lszLineBuffer> */
282: /* NOTE: This routine is always called with cch at least TWO */
283:
284: *((LPWORD)lszLineBuffer) = (WORD)cch;
285: cch = (int)SendMessage(hwndEdit, EM_GETLINE, iLine, (LONG)(LPSTR)lszLineBuffer);
286: /* The returned string is NOT null terminated */
287:
288: /* Strip trailing white spaces from the string, and null-terminate it */
289:
290: while(cch > 0 && ISWHITE(lszLineBuffer[cch-1])) {
291: cch--;
292: }
293: lszLineBuffer[cch] = 0;
294:
295: return TRUE;
296: }
297:
298:
299: /*----------------------------------------------------------------------------*\
300: | EditGetCurLine(hwndEdit) |
301: | |
302: | Description: |
303: | This function retrieves the line number of the current line in the |
304: | edit box control with handle <hwndEdit>. It returns this line number. |
305: | |
306: | Arguments: |
307: | hwndEdit window handle of the edit box control |
308: | |
309: | Returns: |
310: | The line number of the current line. |
311: | |
312: \*----------------------------------------------------------------------------*/
313:
314: int EditGetCurLine(
315: HWND hwndEdit)
316: {
317: int iLine; /* Line number of the currently active line */
318:
319: iLine = (int)SendMessage(hwndEdit, EM_LINEFROMCHAR, (WPARAM)-1, 0L);
320:
321: if (iLine < 0) {
322: iLine = 0;
323: }
324:
325: return iLine;
326: }
327:
328:
329: /*----------------------------------------------------------------------------*\
330: | EditSetCurLine(hwndEdit, iLine) |
331: | |
332: | Description: |
333: | This function sets the current line in the edit box control with |
334: | handle <hwndEdit> to the number given in <iLine>. |
335: | |
336: | Arguments: |
337: | hwndEdit window handle of the edit box control |
338: | iLine the line number to be made the current line |
339: | |
340: | Returns: |
341: | void |
342: | |
343: \*----------------------------------------------------------------------------*/
344:
345: void EditSetCurLine(
346: HWND hwndEdit,
347: int iLine)
348: {
349: int off;
350:
351: off = (int)SendMessage(hwndEdit, EM_LINEINDEX, iLine, 0L);
352: SendMessage(hwndEdit, EM_SETSEL, off, off);
353:
354: }
355:
356: /*----------------------------------------------------------------------------*\
357: | EditSelectLine(hwndEdit, iLine) |
358: | |
359: | Description: |
360: | This function selects line # <iLine> in the edit box control with |
361: | handle <hwndEdit>. |
362: | |
363: | Arguments: |
364: | hwndEdit window handle of the edit box control |
365: | iLine the line number to be selected |
366: | |
367: | Returns: |
368: | void |
369: | |
370: \*----------------------------------------------------------------------------*/
371:
372: void EditSelectLine(
373: HWND hwndEdit,
374: int iLine)
375: {
376: int offS;
377: int offE;
378:
379: offS = (int)SendMessage(hwndEdit, EM_LINEINDEX, iLine, 0L);
380: offE = (int)SendMessage(hwndEdit, EM_LINEINDEX, iLine+1, 0L);
381:
382:
383: if (offE < offS) { /* Select to the end */
384: offE = -1;
385: }
386:
387: SendMessage(hwndEdit, EM_SETSEL, offS, offE);
388: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.