Annotation of mstools/samples/mcitest/edit.c, revision 1.1.1.2

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,
1.1.1.2 ! root       52:     LPTSTR  lszFile)
1.1       root       53: {
1.1.1.2 ! root       54: #ifdef UNICODE
        !            55:     HANDLE      fh;                 /* DOS file handle returned by OpenFile   */
        !            56: #else
1.1       root       57:     HFILE       fh;                 /* DOS file handle returned by OpenFile   */
1.1.1.2 ! root       58:     OFSTRUCT    of;                 /* structure used by the OpenFile routine */
        !            59: #endif
        !            60:     LPTSTR      lszText;            /* pointer to the opened file's text      */
1.1       root       61:     UINT        nFileLen;           /* length, in bytes, of the opened file   */
                     62:     HCURSOR     hcur;               /* handle to the pre-hourglass cursor     */
                     63: 
                     64:     /* If a valid window handle or a filename was not specified, then exit.
                     65:      */
                     66:     if (!hwndEdit || !lszFile) {
1.1.1.2 ! root       67:         dprintf1((TEXT("EditOpenFile: Invalid window or filename")));
1.1       root       68:         return FALSE;
                     69:     }
                     70: 
                     71:     /* Open the file for reading */
                     72: 
1.1.1.2 ! root       73: #ifdef UNICODE
        !            74:     fh = CreateFile(lszFile, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, 0);
        !            75:     if (fh == INVALID_HANDLE_VALUE) {
        !            76:         dprintf1((TEXT("Failed to open: %s"), lszFile));
        !            77:         return FALSE;
        !            78:     }
        !            79: #else
1.1       root       80:     fh = OpenFile(lszFile, &of, OF_READ);
                     81:     if (fh == HFILE_ERROR) {
1.1.1.2 ! root       82:         dprintf1((TEXT("Failed to open: %s"), lszFile));
1.1       root       83:         return FALSE;
                     84:     }
1.1.1.2 ! root       85: #endif
1.1       root       86: 
                     87:        nFileLen = (UINT)GetFileSize((HANDLE)fh, NULL);
                     88:        if (HFILE_ERROR == nFileLen) {
1.1.1.2 ! root       89:         dprintf1((TEXT("Failed to find file size: %s"), lszFile));
1.1       root       90:         return FALSE;
                     91:        }
                     92: 
                     93:     /*
                     94:      * Create a pointer to a region of memory large enough to hold the entire
                     95:      * contents of the file. If this was successful, then read the file into
                     96:      * this region, and use this region as the text for the edit control.
                     97:      * Finally, free up the region and its pointer, and close the file.
                     98:      *
                     99:      */
                    100: 
1.1.1.2 ! root      101:     lszText = (LPTSTR)GAllocPtr(nFileLen+1);   // Note: no *sizeof(TCHAR)
1.1       root      102:     if (NULL != lszText) {
                    103: 
                    104:                BOOL fReturn;
                    105:         /* This could take a while - show the hourglass cursor */
                    106: 
                    107:         hcur = SetCursor(LoadCursor(NULL, IDC_WAIT));
                    108: 
                    109:         /* Read the file and copy the contents into the edit control */
                    110: 
1.1.1.2 ! root      111:         dprintf3((TEXT("reading file...")));
        !           112: 
        !           113: #ifdef UNICODE
        !           114:         {
        !           115:         DWORD bytesRead;
        !           116: 
        !           117:         if (ReadFile(fh, lszText, nFileLen, &bytesRead, NULL)) {
        !           118:                        lszText[bytesRead]=0;
        !           119:                        dprintf2((TEXT("File loaded ok")));
        !           120:                        SetWindowTextA(hwndEdit, (LPSTR)lszText); // Until we have UNICODE files
        !           121:                        fReturn = TRUE;
        !           122:                } else {
        !           123:                        dprintf2((TEXT("Error loading file")));
        !           124:                        fReturn = FALSE;
        !           125:                }
        !           126: 
        !           127:         }
        !           128:         /* Free up the memory, close the file, and restore the cursor */
1.1       root      129: 
1.1.1.2 ! root      130:         GFreePtr(lszText);
        !           131:         CloseHandle(fh);
        !           132: #else
        !           133:         if ((_lread((HFILE)fh, lszText, nFileLen)) == nFileLen) {
        !           134:                        lszText[nFileLen/sizeof(TCHAR)]=0;
        !           135:                        dprintf2((TEXT("File loaded ok")));
1.1       root      136:                        SetWindowText(hwndEdit, lszText);
                    137:                        fReturn = TRUE;
                    138:                } else {
1.1.1.2 ! root      139:                        dprintf2((TEXT("Error loading file")));
1.1       root      140:                        fReturn = FALSE;
                    141:                }
                    142: 
                    143:         /* Free up the memory, close the file, and restore the cursor */
                    144: 
                    145:         GFreePtr(lszText);
1.1.1.2 ! root      146:         _lclose((HFILE)fh);
        !           147: #endif
1.1       root      148:         SetCursor(hcur);
                    149: 
                    150:         return fReturn;
                    151:     }
                    152: 
                    153:     /*
                    154:      * We couldn't allocate the required memory, so close the file and
                    155:      * return FALSE.
                    156:      *
                    157:      */
                    158: 
1.1.1.2 ! root      159:     dprintf1((TEXT("Failed memory allocation for file")));
        !           160: #ifdef UNICODE
        !           161:     CloseHandle(fh);
        !           162: #else
        !           163:     _lclose((HFILE)fh);
        !           164: #endif
1.1       root      165:     return FALSE;
                    166: }
                    167: 
                    168: 
                    169: /*----------------------------------------------------------------------------*\
                    170: |   EditSaveFile(hwndEdit, lszFile)                                            |
                    171: |                                                                              |
                    172: |   Description:                                                               |
                    173: |       This function saves the contents of the edit control with the handle   |
                    174: |       <hwndEdit> into the file  <lszFile>, creating the file if required.    |
                    175: |                                                                              |
                    176: |   Arguments:                                                                 |
                    177: |       hwndEdit        window handle of the edit box control                  |
                    178: |       lszFile         filename of the file to be saved                       |
                    179: |                                                                              |
                    180: |   Returns:                                                                   |
                    181: |       TRUE if the operation was successful, else FALSE                       |
                    182: |                                                                              |
                    183: \*----------------------------------------------------------------------------*/
                    184: 
                    185: BOOL               EditSaveFile(
                    186:     HWND    hwndEdit,
1.1.1.2 ! root      187:     LPTSTR  lszFile)
1.1       root      188: {
1.1.1.2 ! root      189: #ifdef UNICODE
        !           190:     HANDLE      fh;                 /* DOS file handle returned by OpenFile   */
        !           191:     DWORD       dwBytesWritten;
        !           192: #else
1.1       root      193:     OFSTRUCT    of;                 /* structure used by the OpenFile routine */
                    194:     HFILE       fh;                 /* DOS file handle returned by OpenFile   */
1.1.1.2 ! root      195: #endif
        !           196:     LPTSTR      lszText;            /* pointer to the saved file's text       */
1.1       root      197:     int         nFileLen;           /* length, in bytes, of the saved file    */
                    198:     HCURSOR     hcur;               /* handle to the pre-hourglass cursor     */
                    199: 
                    200:     /* If a valid window handle or a filename was not specified, then exit */
1.1.1.2 ! root      201:     dprintf2((TEXT("EditSaveFile:  Saving %s"), lszFile));
1.1       root      202: 
                    203:     if (!hwndEdit || !lszFile) {
1.1.1.2 ! root      204:         dprintf1((TEXT("EditSaveFile: Invalid window or filename")));
1.1       root      205:         return FALSE;
                    206:     }
                    207: 
                    208:     /* Create (or overwrite) the save file */
                    209: 
1.1.1.2 ! root      210: #ifdef UNICODE
        !           211:     fh = CreateFile(lszFile, GENERIC_WRITE, FILE_SHARE_READ, NULL, CREATE_ALWAYS, 0, 0);
        !           212:     if (fh == INVALID_HANDLE_VALUE) {
        !           213:         dprintf1((TEXT("EditSaveFile: Error opening file")));
        !           214:         return FALSE;
        !           215:     }
        !           216: #else
1.1       root      217:     fh = OpenFile(lszFile, &of, OF_CREATE);
                    218:     if (fh == HFILE_ERROR) {
1.1.1.2 ! root      219:         dprintf1((TEXT("EditSaveFile: Error opening file")));
1.1       root      220:         return FALSE;
                    221:     }
1.1.1.2 ! root      222: #endif
1.1       root      223: 
                    224:     /* Find out how big the contents of the edit box are */
                    225: 
1.1.1.2 ! root      226:     nFileLen = GetWindowTextLength(hwndEdit);
        !           227:        // nFileLen = nFileLen*sizeof(TCHAR);  We write ASCII
1.1       root      228: 
                    229:     /*
                    230:      * Create a pointer to a region of memory large enough to hold the entire
                    231:      * contents of the edit box. If this was successful, then read the contents
                    232:      * of the edit box into this region, and write the contents of this region
                    233:      * into the save file. Finally, free up the region and its pointer, and
                    234:      * close the file.
                    235:      *
                    236:      */
                    237: 
1.1.1.2 ! root      238:     lszText = (LPTSTR)GAllocPtr(nFileLen);
1.1       root      239:     if (NULL != lszText) {
                    240: 
                    241:         /* This could take a while - show the hourglass cursor */
                    242: 
                    243:         hcur = SetCursor(LoadCursor(NULL, IDC_WAIT));
                    244: 
                    245:         /* Read the contents of the edit box, and write it to the save file */
                    246: 
1.1.1.2 ! root      247:         GetWindowTextA(hwndEdit, (LPSTR)lszText, nFileLen);  // Save ASCII file
        !           248: 
        !           249: #ifdef UNICODE
        !           250:                WriteFile(fh, lszText, nFileLen, &dwBytesWritten, NULL);
        !           251: #else
1.1       root      252:         _lwrite(fh, lszText, nFileLen);
1.1.1.2 ! root      253: #endif
        !           254: 
1.1       root      255: 
                    256:         /* Free up the memory, close the file, and restore the cursor */
                    257: 
                    258:         GFreePtr(lszText);
1.1.1.2 ! root      259: #ifdef UNICODE
        !           260:                CloseHandle(fh);
        !           261: #else
1.1       root      262:         _lclose(fh);
1.1.1.2 ! root      263: #endif
1.1       root      264:         SetCursor(hcur);
                    265: 
                    266:         return TRUE;
                    267:     }
                    268: 
                    269:     /*
                    270:      * We couldn't allocate the required memory, so close the file and
                    271:      * return FALSE.
                    272:      */
                    273: 
1.1.1.2 ! root      274:     _lclose((HFILE)fh);
1.1       root      275:     return FALSE;
                    276: }
                    277: 
                    278: 
                    279: /*----------------------------------------------------------------------------*\
                    280: |   EditGetLineCount(hwndEdit)                                                    |
                    281: |                                                                              |
                    282: |   Description:                                                               |
                    283: |       This function finds out how many lines of text are in the edit box and |
                    284: |       returns this value.                                                    |
                    285: |                                                                              |
                    286: |   Arguments:                                                                 |
                    287: |       hwndEdit           window handle of the edit box control                  |
                    288: |                                                                              |
                    289: |   Returns:                                                                   |
                    290: |       The number of lines of text in the edit control.                       |
                    291: |                                                                              |
                    292: \*----------------------------------------------------------------------------*/
                    293: 
                    294: DWORD EditGetLineCount(
                    295:     HWND   hwndEdit)
                    296: {
                    297:     return SendMessage(hwndEdit, EM_GETLINECOUNT, 0, 0L);
                    298: }
                    299: 
                    300: 
                    301: /*----------------------------------------------------------------------------*\
                    302: |   EditGetLine(hwndEdit, iLine, lszLineBuffer, cch)                           |
                    303: |                                                                              |
                    304: |   Description:                                                               |
                    305: |       This function retrieves the contents of line # <iLine> from the edit   |
                    306: |       box control with handle <hwndEdit>.  If <iLine> is out of range (that  |
                    307: |       number line does not exist in the multi line edit field) then FALSE is |
                    308: |       returned.  Otherwise the line is copied into the buffer pointed to by  |
                    309: |       <lszLineBuffer> with white space removed.                              |
                    310: |       The string is also null terminated even if it means truncation.        |
                    311: |                                                                              |
                    312: |   Arguments:                                                                 |
                    313: |       hwndEdit        window handle of the edit box control                  |
                    314: |       iLine           line # to get the contents of                          |
                    315: |       lszLineBuffer   pointer to the buffer to copy the line to              |
                    316: |       cch             max # of characters to copy (MIN value on entry==2)    |
                    317: |                                                                              |
                    318: |   Returns:                                                                   |
                    319: |       TRUE.                                                                  |
                    320: |                                                                              |
                    321: \*----------------------------------------------------------------------------*/
                    322: 
                    323: BOOL EditGetLine(
                    324:     HWND    hwndEdit,
                    325:     int     iLine,
1.1.1.2 ! root      326:     LPTSTR  lszLineBuffer,
1.1       root      327:     int     cch)
                    328: {
                    329:     int     nLines;             /* total number of lines in the edit box */
                    330: 
                    331:     /*
                    332:      * Find out how many lines are in the edit control. If the requested line
                    333:      * is out of range, then return.
                    334:      */
                    335: 
                    336:     nLines = (int)EditGetLineCount(hwndEdit);
                    337:     if (iLine < 0 || iLine >= nLines) {
1.1.1.2 ! root      338:         if (iLine!= nLines) {  // Probably because the user pressed Enter
        !           339:                                // and the line number is beyond the end
        !           340:                    dprintf1((TEXT("Requested line count %d is out of range (%d)"), iLine, nLines));
        !           341:         }
1.1       root      342:         return *lszLineBuffer = 0;
                    343:                /* This sets the buffer to null and returns FALSE */
                    344:        }
                    345: 
                    346:     /* Read the requested line into the string pointed to by <lszLineBuffer> */
                    347:        /* NOTE:  This routine is always called with cch at least TWO */
                    348: 
                    349:     *((LPWORD)lszLineBuffer) = (WORD)cch;
1.1.1.2 ! root      350:     cch = (int)SendMessage(hwndEdit, EM_GETLINE, iLine, (LONG)(LPTSTR)lszLineBuffer);
1.1       root      351:        /* The returned string is NOT null terminated */
                    352: 
                    353:     /* Strip trailing white spaces from the string, and null-terminate it */
                    354:     while(cch > 0 && ISWHITE(lszLineBuffer[cch-1])) {
                    355:         cch--;
                    356:        }
                    357:     lszLineBuffer[cch] = 0;
                    358: 
                    359:     return TRUE;
                    360: }
                    361: 
                    362: 
                    363: /*----------------------------------------------------------------------------*\
                    364: |   EditGetCurLine(hwndEdit)                                                      |
                    365: |                                                                              |
                    366: |   Description:                                                               |
                    367: |       This function retrieves the line number of the current line in the     |
                    368: |       edit box control with handle <hwndEdit>. It returns this line number.     |
                    369: |                                                                              |
                    370: |   Arguments:                                                                 |
                    371: |       hwndEdit           window handle of the edit box control                  |
                    372: |                                                                              |
                    373: |   Returns:                                                                   |
                    374: |       The line number of the current line.                                   |
                    375: |                                                                              |
                    376: \*----------------------------------------------------------------------------*/
                    377: 
                    378: int EditGetCurLine(
                    379:     HWND    hwndEdit)
                    380: {
                    381:     int iLine;                  /* Line number of the currently active line   */
                    382: 
                    383:     iLine = (int)SendMessage(hwndEdit, EM_LINEFROMCHAR, (WPARAM)-1, 0L);
                    384: 
                    385:     if (iLine < 0) {
                    386:         iLine = 0;
                    387:        }
                    388: 
                    389:     return iLine;
                    390: }
                    391: 
                    392: 
                    393: /*----------------------------------------------------------------------------*\
                    394: |   EditSetCurLine(hwndEdit, iLine)                                               |
                    395: |                                                                              |
                    396: |   Description:                                                               |
                    397: |       This function sets the current line in the edit box control with       |
                    398: |       handle <hwndEdit> to the number given in <iLine>.                         |
                    399: |                                                                              |
                    400: |   Arguments:                                                                 |
                    401: |       hwndEdit           window handle of the edit box control                  |
                    402: |       iLine           the line number to be made the current line            |
                    403: |                                                                              |
                    404: |   Returns:                                                                   |
                    405: |       void                                                                   |
                    406: |                                                                              |
                    407: \*----------------------------------------------------------------------------*/
                    408: 
                    409: void EditSetCurLine(
                    410:     HWND    hwndEdit,
                    411:     int     iLine)
                    412: {
                    413:     int off;
                    414: 
                    415:     off = (int)SendMessage(hwndEdit, EM_LINEINDEX, iLine, 0L);
                    416:     SendMessage(hwndEdit, EM_SETSEL, off, off);
                    417: 
                    418: }
                    419: 
                    420: /*----------------------------------------------------------------------------*\
                    421: |   EditSelectLine(hwndEdit, iLine)                                               |
                    422: |                                                                              |
                    423: |   Description:                                                               |
                    424: |       This function selects line # <iLine> in the edit box control with      |
                    425: |       handle <hwndEdit>.                                                        |
                    426: |                                                                              |
                    427: |   Arguments:                                                                 |
                    428: |       hwndEdit           window handle of the edit box control                  |
                    429: |       iLine           the line number to be selected                         |
                    430: |                                                                              |
                    431: |   Returns:                                                                   |
                    432: |       void                                                                   |
                    433: |                                                                              |
                    434: \*----------------------------------------------------------------------------*/
                    435: 
                    436: void EditSelectLine(
                    437:     HWND    hwndEdit,
                    438:     int     iLine)
                    439: {
                    440:     int offS;
                    441:     int offE;
                    442: 
                    443:     offS = (int)SendMessage(hwndEdit, EM_LINEINDEX, iLine, 0L);
                    444:     offE = (int)SendMessage(hwndEdit, EM_LINEINDEX, iLine+1, 0L);
                    445: 
                    446: 
                    447:     if (offE < offS) {   /* Select to the end */
                    448:         offE = -1;
                    449:        }
                    450: 
                    451:     SendMessage(hwndEdit, EM_SETSEL, offS, offE);
                    452: }

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.