home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c480 / 19.ddi / SAMPLES / MCITEST / EDIT.C_ / EDIT.C
Encoding:
C/C++ Source or Header  |  1993-02-08  |  7.6 KB  |  276 lines

  1. /*
  2.  * edit.c - Routines for dealing with multi-line edit controls.
  3.  */
  4.  
  5. #include <windows.h>
  6. #include "gmem.h"
  7. #include "edit.h"
  8.  
  9.  
  10. #define  SEEK_SET 0             // Flags for _lseek
  11. #define  SEEK_CUR 1
  12. #define  SEEK_END 2
  13.  
  14. #define ISSPACE(c) ((c) == ' ' || (c) == '\t')
  15. #define ISEOL(c)   ((c) == '\n'|| (c) == '\r')
  16. #define ISWHITE(c) (ISSPACE(c) || ISEOL(c))
  17.  
  18.  
  19. /* EditOpenFile - Opens the specified file, copies the contents of the file
  20.  *   into the edit control with the specified handle, and then closes the
  21.  *
  22.  * Params:      hwndEdit - window handle of the edit box control
  23.  *              lszFile - filename of the file to be opened
  24.  *
  25.  * Returns:     TRUE if the operation was successful, else FALSE
  26.  */
  27. BOOL    PASCAL FAR EditOpenFile(
  28.     HWND    hwndEdit,
  29.     LPSTR   lszFile)
  30. {
  31.     OFSTRUCT    of;
  32.     HFILE       fh;
  33.     LPSTR       lszText;
  34.     int         nFileLen;
  35.     HCURSOR     hcur;
  36.  
  37.     /* If a valid window handle or a filename was not specified, then exit.
  38.      */
  39.     if (!hwndEdit || !lszFile)
  40.         return FALSE;
  41.  
  42.     /* Open the file for reading.
  43.      */
  44.     fh = OpenFile(lszFile, &of, OF_READ);
  45.     if (fh == HFILE_ERROR)
  46.         return FALSE;
  47.  
  48.     /* Seek to the end of the file to find out the file's length in bytes.
  49.      */
  50.     nFileLen = (int)_llseek(fh, 0L, SEEK_END);
  51.     _llseek(fh, 0L, SEEK_SET);
  52.  
  53.     /* Create a pointer to a region of memory large enough to hold the entire
  54.      * contents of the file. If this was successful, then read the file into
  55.      * this region, and use this region as the text for the edit control.
  56.      * Finally, free up the region and its pointer, and close the file.
  57.      */
  58.     if (lszText = GAllocPtr(nFileLen + 1)) {
  59.  
  60.         /* This could take a while - show the hourglass cursor.
  61.          */
  62.         hcur = SetCursor(LoadCursor(NULL, IDC_WAIT));
  63.  
  64.         /* Read the file and copy the contents into the edit control.
  65.          */
  66.         _lread(fh, lszText, nFileLen);
  67.         lszText[nFileLen]=0;
  68.         SetWindowText(hwndEdit, lszText);
  69.  
  70.         /* Free up the memory, close the file, and restore the cursor.
  71.          */
  72.         GFreePtr(lszText);
  73.         _lclose(fh);
  74.         SetCursor(hcur);
  75.  
  76.         return TRUE;
  77.     }
  78.  
  79.     /* Could not allocate the required memory, so close the file and
  80.      * return FALSE.
  81.      */
  82.     _lclose(fh);
  83.     return FALSE;
  84. }
  85.  
  86.  
  87. /* EditSaveFile - Saves the contents of the edit control with the handle
  88.  *  hwndEdit> into the file named <lszFile>, creating the file if
  89.  *  required.
  90.  *
  91.  * Params:  hwndEdit - window handle of the edit box control
  92.  *          lszFile - filename of the file to be saved
  93.  *
  94.  * Returns: TRUE if the operation was successful, else FALSE
  95.  */
  96. BOOL    PASCAL FAR EditSaveFile(
  97.     HWND    hwndEdit,
  98.     LPSTR   lszFile)
  99. {
  100.     OFSTRUCT    of;
  101.     HFILE       fh;
  102.     LPSTR       lszText;
  103.     int         nFileLen;
  104.     HCURSOR     hcur;
  105.  
  106.     /* If a valid window handle or a filename was not specified, then exit.\
  107.      */
  108.     if (!hwndEdit || !lszFile)
  109.         return FALSE;
  110.  
  111.     /* Create (or overwrite) the save file.
  112.      */
  113.     fh = OpenFile(lszFile, &of, OF_CREATE);
  114.     if (fh == HFILE_ERROR)
  115.         return FALSE;
  116.  
  117.     /* Find out how big the contents of the edit box are.
  118.      */
  119.     nFileLen = GetWindowTextLength(hwndEdit) + 1;
  120.  
  121.     /* Create a pointer to a region of memory large enough to hold the entire
  122.      * contents of the edit box. If this was successful, then read contents
  123.      * of the edit box into this region, and write the contents of this region
  124.      * into the save file. Finally, free up the region and its pointer, and
  125.      * close the file.
  126.      */
  127.     if (lszText = GAllocPtr(nFileLen)) {
  128.  
  129.         /* This could take a while - show the hourglass cursor.
  130.          */
  131.         hcur = SetCursor(LoadCursor(NULL, IDC_WAIT));
  132.  
  133.         /* Read the contents of the edit box, and write it to the save file.
  134.          */
  135.         GetWindowText(hwndEdit, lszText, nFileLen);
  136.         _lwrite(fh, lszText, nFileLen);
  137.  
  138.         /* Free up the memory, close the file, and restore the cursor.
  139.          */
  140.         GFreePtr(lszText);
  141.         _lclose(fh);
  142.         SetCursor(hcur);
  143.  
  144.         return TRUE;
  145.     }
  146.  
  147.     /* Could not allocate the required memory, so close the file and
  148.      * return FALSE.
  149.      */
  150.     _lclose(fh);
  151.     return FALSE;
  152. }
  153.  
  154.  
  155. /* EditGetLineCount - Finds out how many lines of text are in the edit 
  156.  *   box and returns this value.
  157.  *
  158.  * Params:  hwndEdit - window handle of the edit box control
  159.  *
  160.  * Returns: The number of lines of text in the edit control.
  161.  */
  162. DWORD   PASCAL FAR EditGetLineCount(
  163.     HWND    hwndEdit)
  164. {
  165.     return SendMessage(hwndEdit, EM_GETLINECOUNT, 0, 0L);
  166. }
  167.  
  168.  
  169. /* EditGetLine - Retrieves the contents of line # <iLine> from the edit
  170.  *   box control with handle <hwndEdit>. It returns a pointer to a string
  171.  *   holding the contents of the given line. All trailing white spaces are
  172.  *   removed from the string, and it is null-terminated.
  173.  *
  174.  * Params:  hwndEdit -  window handle of the edit box control
  175.  *          iLine - line # to get the contents of
  176.  *          szLineBuffer - pointer to the buffer to copy the line to
  177.  *          cch - max # of characters to copy
  178.  *
  179.  * Returns: TRUE.
  180.  */
  181. BOOL    PASCAL FAR EditGetLine(
  182.     HWND    hwndEdit,
  183.     int     iLine,
  184.     LPSTR   lszLineBuffer,
  185.     int     cch)
  186. {
  187.     int     nLines;
  188.  
  189.     /* Find out how many lines are in the edit control. If the requested line
  190.      * is out of range, then return.
  191.      */
  192.     nLines = (int)EditGetLineCount(hwndEdit);
  193.     if (iLine < 0 || iLine >= nLines)
  194.         return *lszLineBuffer = 0;
  195.  
  196.     /* Read the requested line into the string pointed to by <lszLineBuffer>.
  197.      */
  198.     *((LPWORD)lszLineBuffer) = cch;
  199.     cch = (int)SendMessage(hwndEdit, EM_GETLINE, iLine, (LONG)(LPSTR)lszLineBuffer);
  200.  
  201.     /* Strip trailing white spaces from the string, and null-terminate it.
  202.      */
  203.     while(cch > 0 && ISWHITE(lszLineBuffer[cch-1]))
  204.         cch--;
  205.     lszLineBuffer[cch] = 0;
  206.  
  207.     return TRUE;
  208. }
  209.  
  210.  
  211. /* EditGetCurLine - Retrieves the line number of the current line in the
  212.  *    edit box control with handle <hwndEdit>. It returns this line number.
  213.  *
  214.  * Params:  hwndEdit - window handle of the edit box control
  215.  *
  216.  * Returns: The line number of the cuurent line.
  217.  */
  218. int PASCAL FAR EditGetCurLine(
  219.     HWND    hwndEdit)
  220. {
  221.     int iLine;
  222.  
  223.     iLine = (int)SendMessage(hwndEdit,(UINT) EM_LINEFROMCHAR,(WPARAM) -1,(LPARAM) 0L);
  224.  
  225.     if (iLine < 0)
  226.         iLine = 0;
  227.  
  228.     return iLine;
  229. }
  230.  
  231.  
  232. /* EditSetCurLine - Sets the current line in the edit box control with
  233.  *   handle <hwndEdit> to the number given in <iLine>.
  234.  *
  235.  * Params:  hwndEdit - window handle of the edit box control
  236.  *          iLine - the line number to be made the current line
  237.  *
  238.  * Returns: void
  239.  *
  240.  */
  241. void    PASCAL FAR EditSetCurLine(
  242.     HWND    hwndEdit,
  243.     int     iLine)
  244. {
  245.     int off;
  246.  
  247.     off = (int)SendMessage(hwndEdit, EM_LINEINDEX, iLine, 0L);
  248.     SendMessage(hwndEdit, EM_SETSEL, 0, MAKELONG(off, off));
  249.  
  250. }
  251.  
  252.  
  253. /* EditSelectLine - Selects line # <iLine> in the edit box control with
  254.  *   handle <hwndEdit>.
  255.  *
  256.  * Params:  hwndEdit - window handle of the edit box control
  257.  *          iLine - the line number to be selected
  258.  *
  259.  * Returns: void
  260.  */
  261. void    PASCAL FAR EditSelectLine(
  262.     HWND    hwndEdit,
  263.     int     iLine)
  264. {
  265.     int offS;
  266.     int offE;
  267.  
  268.     offS = (int)SendMessage(hwndEdit, EM_LINEINDEX, iLine, 0L);
  269.     offE = (int)SendMessage(hwndEdit, EM_LINEINDEX, iLine + 1, 0L);
  270.  
  271.     if (offE < offS)
  272.         offE = 0x7fff;
  273.  
  274.     SendMessage(hwndEdit, EM_SETSEL, 0, MAKELONG(offS, offE));
  275. }
  276.