Annotation of mstools/samples/multipad/mpfile.c, revision 1.1.1.3

1.1       root        1: /***************************************************************************
                      2:  *                                                                        *
                      3:  *  MODULE    : MpFile.c                                                  *
                      4:  *                                                                        *
                      5:  *  PURPOSE   : Contains the code for File I/O for Multipad.              *
                      6:  *                                                                        *
                      7:  *  FUNCTIONS : AlreadyOpen   - Determines if a file is already open.     *
                      8:  *                                                                        *
                      9:  *             AddFile       - Creates a new MDI window and, if specified,*
                     10:  *                             loads a file into it.                      *
                     11:  *                                                                        *
                     12:  *             LoadFile      - Loads a file into a MDI window.            *
                     13:  *                                                                        *
                     14:  *             MyReadFile    - Calls File/Open dialog and appropriately   *
                     15:  *                             responds to the user's input.              *
                     16:  *                                                                        *
                     17:  *             SaveFile      - Saves the contents of a MDI window's edit  *
                     18:  *                             control to a file.                         *
                     19:  *                                                                        *
                     20:  *             SetSaveFrom   - Formats the "Save 'file' to" string.       *
                     21:  *                                                                        *
                     22:  *             SaveAsDlgProc - Dialog function for the File/SaveAs dialog.*
                     23:  *                                                                        *
                     24:  *             ChangeFile    - Calls File/SaveAs dialog.                  *
                     25:  *                                                                        *
                     26:  ***************************************************************************/
                     27: #include "multipad.h"
                     28: #include <fcntl.h>
                     29: #include <SYS\types.h>
                     30: #include <SYS\stat.h>
                     31: #include <io.h>
                     32: #include <string.h>
                     33: 
                     34: VOID APIENTRY GetFileName(PSTR);
                     35: //INT APIENTRY DialogBoxParam(HANDLE,LPSTR,HWND,FARPROC,LONG);
1.1.1.3 ! root       36: //LPSTR WINAPI AnsiUpper(LPSTR);
1.1       root       37: 
                     38: OFSTRUCT       of;
                     39: /****************************************************************************
                     40:  *                                                                         *
                     41:  *  FUNCTION   : AlreadyOpen(szFile)                                       *
                     42:  *                                                                         *
                     43:  *  PURPOSE    : Checks to see if the file described by the string pointed  *
                     44:  *              to by 'szFile' is already open.                            *
                     45:  *                                                                         *
                     46:  *  RETURNS    : a handle to the described file's window if that file is    *
                     47:  *              already open;  NULL otherwise.                             *
                     48:  *                                                                         *
                     49:  ****************************************************************************/
                     50: 
                     51: HWND AlreadyOpen(CHAR *szFile)
                     52: {
                     53:     INT     iDiff;
                     54:     HWND    hwndCheck;
                     55:     CHAR    szChild[64];
                     56:     LPSTR   lpChild, lpFile;
                     57:     HFILE     wFileTemp;
                     58: 
                     59:     /* Open the file with the OF_PARSE flag to obtain the fully qualified
                     60:      * pathname in the OFSTRUCT structure.
                     61:      */
                     62:     wFileTemp = OpenFile((LPSTR)szFile, (LPOFSTRUCT)&of, OF_PARSE);
                     63:     if (! wFileTemp)
                     64:        return(NULL);
                     65:     _lclose(wFileTemp);
                     66: 
                     67:     /* Check each MDI child window in Multipad */
                     68:     for (   hwndCheck = GetWindow(hwndMDIClient, GW_CHILD);
                     69:            hwndCheck;
                     70:            hwndCheck = GetWindow(hwndCheck, GW_HWNDNEXT)   ) {
                     71:        /* Initialization  for comparison */
                     72:        lpChild = szChild;
                     73:        lpFile = (LPSTR)AnsiUpper((LPSTR) of.szPathName);
                     74:        iDiff = 0;
                     75: 
                     76:        /* Skip icon title windows */
                     77:        if (GetWindow(hwndCheck, GW_OWNER))
                     78:            continue;
                     79: 
                     80:        /* Get current child window's name */
                     81:        GetWindowText(hwndCheck, lpChild, 64);
                     82: 
                     83:        /* Compare window name with given name */
                     84:        while ((*lpChild) && (*lpFile) && (!iDiff)){
                     85:            if (*lpChild++ != *lpFile++)
                     86:                iDiff = 1;
                     87:        }
                     88: 
                     89:        /* If the two names matched, the file is already   */
                     90:        /* open -- return handle to matching child window. */
                     91:        if (!iDiff)
                     92:            return(hwndCheck);
                     93:     }
                     94:     /* No match found -- file is not open -- return NULL handle */
                     95:     return(NULL);
                     96: }
                     97: 
                     98: /****************************************************************************
                     99:  *                                                                         *
                    100:  *  FUNCTION   : AddFile (lpName)                                          *
                    101:  *                                                                         *
                    102:  *  PURPOSE    : Creates a new MDI window. If the lpName parameter is not   *
                    103:  *              NULL, it loads a file into the window.                     *
                    104:  *                                                                         *
                    105:  *  RETURNS    : HWND  - A handle to the new window.                       *
                    106:  *                                                                         *
                    107:  ****************************************************************************/
                    108: 
                    109: HWND APIENTRY AddFile(CHAR * pName)
                    110: {
                    111:     HWND hwnd;
                    112: 
                    113:     CHAR           sz[160];
                    114:     MDICREATESTRUCT mcs;
                    115: 
                    116:     if (!pName) {
                    117:        /* The pName parameter is NULL -- load the "Untitled" string from */
                    118:        /* STRINGTABLE and set the title field of the MDI CreateStruct.    */
                    119:        LoadString (hInst, IDS_UNTITLED, sz, sizeof(sz));
                    120:        mcs.szTitle = (LPSTR)sz;
                    121:     }
                    122:     else
                    123:        /* Title the window with the fully qualified pathname obtained by
                    124:         * calling OpenFile() with the OF_PARSE flag (in function
                    125:         * AlreadyOpen(), which is called before AddFile().
                    126:         */
                    127:        mcs.szTitle = of.szPathName;
                    128: 
                    129:     mcs.szClass = szChild;
                    130:     mcs.hOwner = hInst;
                    131: 
                    132:     /* Use the default size for the window */
                    133:     mcs.x = mcs.cx = CW_USEDEFAULT;
                    134:     mcs.y = mcs.cy = CW_USEDEFAULT;
                    135: 
                    136:     /* Set the style DWORD of the window to default */
                    137:     mcs.style = styleDefault;
                    138: 
                    139:     /* tell the MDI Client to create the child */
                    140:     hwnd = (HWND)SendMessage (hwndMDIClient,
                    141:                              WM_MDICREATE,
                    142:                              0,
                    143:                              (LONG)(LPMDICREATESTRUCT)&mcs);
                    144: 
                    145:     /* Did we get a file? Read it into the window */
                    146:     if (pName){
                    147:        if (!LoadFile(hwnd, pName)){
                    148:            /* File couldn't be loaded -- close window */
                    149:            SendMessage(hwndMDIClient, WM_MDIDESTROY, (DWORD) hwnd, 0L);
                    150:        }
                    151:     }
                    152: 
                    153:     return hwnd;
                    154: }
                    155: 
                    156: /****************************************************************************
                    157:  *                                                                         *
                    158:  *  FUNCTION   : LoadFile (lpName)                                         *
                    159:  *                                                                         *
                    160:  *  PURPOSE    : Given the handle to a MDI window and a filename, reads the *
                    161:  *              file into the window's edit control child.                 *
                    162:  *                                                                         *
                    163:  *  RETURNS    : TRUE  - If file is sucessfully loaded.                    *
                    164:  *              FALSE - Otherwise.                                         *
                    165:  *                                                                         *
                    166:  ****************************************************************************/
                    167: 
                    168: INT APIENTRY LoadFile (
                    169:        HWND hwnd,
                    170:        CHAR * pName)
                    171: {
                    172:     LONG   wLength;
                    173:     HANDLE hT;
                    174:     LPSTR  lpB;
                    175:     HWND   hwndEdit;
                    176:     HFILE  fh;
                    177:        OFSTRUCT  of;
                    178:     hwndEdit = (HWND)GetWindowLong (hwnd, GWL_HWNDEDIT);
                    179: 
                    180:     /* The file has a title, so reset the UNTITLED flag. */
                    181:     SetWindowWord(hwnd, GWW_UNTITLED, FALSE);
                    182: 
                    183:     fh = OpenFile(pName, &of, OF_READ);  /* JAP was 0, which is OF_READ)*/
                    184: 
                    185:     /* Make sure file has been opened correctly */
                    186:     if ( fh < 0 )
                    187:        goto error;
                    188: 
                    189:     /* Find the length of the file */
                    190:     wLength = (DWORD)_llseek(fh, 0L, 2);
                    191:     _llseek(fh, 0L, 0);
                    192: 
                    193:     /* Attempt to reallocate the edit control's buffer to the file size */
                    194:     hT = (HANDLE)SendMessage (hwndEdit, EM_GETHANDLE, 0, 0L);
                    195:     if (LocalReAlloc(hT, wLength+1, LHND) == NULL) {
                    196:        /* Couldn't reallocate to new size -- error */
                    197:        _lclose(fh);
                    198:        goto error;
                    199:     }
                    200: 
                    201:     /* read the file into the buffer */
                    202:     if (wLength != (LONG)_lread(fh, (lpB = (LPSTR)LocalLock (hT)), (UINT)wLength))
                    203:        MPError (hwnd, MB_OK|MB_ICONHAND, IDS_CANTREAD, (LPSTR)pName);
                    204: 
                    205:     /* Zero terminate the edit buffer */
                    206:     lpB[wLength] = 0;
                    207:     LocalUnlock (hT);
                    208: 
                    209:     SendMessage (hwndEdit, EM_SETHANDLE, (UINT)hT, 0L);
                    210:     _lclose(fh);
                    211: 
                    212:     return TRUE;
                    213: 
                    214: error:
                    215:     /* Report the error and quit */
                    216:     MPError(hwnd, MB_OK | MB_ICONHAND, IDS_CANTOPEN, (LPSTR)pName);
                    217:     return FALSE;
                    218: }
                    219: 
                    220: /****************************************************************************
                    221:  *                                                                         *
                    222:  *  FUNCTION   : MyReadFile(hwnd)                                          *
                    223:  *                                                                         *
                    224:  *  PURPOSE    : Called in response to a File/Open menu selection. It asks  *
                    225:  *              the user for a file name and responds appropriately.       *
                    226:  *                                                                         *
                    227:  ****************************************************************************/
                    228: 
                    229: VOID APIENTRY MyReadFile(HWND hwnd)
                    230: {
                    231:     CHAR    szFile[128];
                    232:     HWND    hwndFile;
                    233: 
                    234:     GetFileName (szFile);
                    235: 
                    236:     /* If the result is not the empty string -- take appropriate action */
                    237:     if (*szFile) {
                    238:            /* Is file already open?? */
                    239:            if (hwndFile = AlreadyOpen(szFile)) {
                    240:                /* Yes -- bring the file's window to the top */
                    241:                BringWindowToTop(hwndFile);
                    242:            }
                    243:            else {
                    244:                /* No -- make a new window and load file into it */
                    245:                AddFile(szFile);
                    246:            }
                    247:     }
                    248:        UNREFERENCED_PARAMETER(hwnd);
                    249: }
                    250: 
                    251: /****************************************************************************
                    252:  *                                                                         *
                    253:  *  FUNCTION   : SaveFile (hwnd)                                           *
                    254:  *                                                                         *
                    255:  *  PURPOSE    : Saves contents of current edit control to disk.           *
                    256:  *                                                                         *
                    257:  ****************************************************************************/
                    258: 
                    259: VOID APIENTRY SaveFile(HWND hwnd)
                    260: {
                    261:     HANDLE   hT;
                    262:     LPSTR    lpT;
                    263:     CHAR     szFile[128];
                    264:     INT      cch;
                    265:     INT      fh;
                    266: //    OFSTRUCT of;
                    267:     HWND     hwndEdit;
                    268: 
                    269:     PSTR     pch;
                    270: 
                    271:     hwndEdit = (HWND)GetWindowLong ( hwnd, GWL_HWNDEDIT);
                    272:     GetWindowText (hwnd, szFile, sizeof(szFile));
                    273: 
                    274:     /* If there is no extension (control is 'Untitled') add .TXT as extension */
                    275:     for (cch = FALSE, lpT = szFile; *lpT; lpT++)
                    276:        switch (*lpT){
                    277:            case '.':
                    278:                 cch = TRUE;
                    279:                 break;
                    280: 
                    281:            case '\\':
                    282:            case ':' :
                    283:                 cch = FALSE;
                    284:                 break;
                    285:        }
                    286:     if (!cch)
                    287:        LoadString (hInst, IDS_ADDEXT, lpT, lpT - (LPSTR)szFile);
                    288: 
                    289:     fh = open(szFile, O_BINARY | O_WRONLY | O_CREAT, S_IWRITE);
                    290: 
                    291:     /* If file could not be opened, quit */
                    292:     if (fh < 0){
                    293:        MPError (hwnd, MB_OK | MB_ICONHAND, IDS_CANTCREATE, (LPSTR)szFile);
                    294:        return;
                    295:     }
                    296: 
                    297:     /* Find out the length of the text in the edit control */
                    298:     cch = GetWindowTextLength (hwndEdit);
                    299: 
                    300:     /* Obtain a handle to the text buffer */
                    301:     hT = (HANDLE)SendMessage (hwndEdit, EM_GETHANDLE, 0, 0L);
                    302:     lpT = (LPSTR)LocalLock (hT);
                    303: 
                    304:     /* Write out the contents of the buffer to the file. */
                    305:     if (cch != write(fh, lpT, cch))
                    306:        MPError (hwnd, MB_OK | MB_ICONHAND, IDS_CANTWRITE, (LPSTR)szFile);
                    307: 
                    308:     /* Clean up */
                    309:     LocalUnlock (hT);
                    310:     SendMessage (hwndEdit, EM_SETHANDLE, (UINT)hT, 0L);
                    311: 
                    312:     close(fh);
                    313: 
                    314:     return;
                    315:        UNREFERENCED_PARAMETER(pch);
                    316: }
                    317: 
                    318: /****************************************************************************
                    319:  *                                                                         *
                    320:  *  FUNCTION   : SetSaveFrom ()                                            *
                    321:  *                                                                         *
                    322:  *  PURPOSE    : Formats the "Save 'file' to .." string.                    *
                    323:  *                                                                         *
                    324:  ****************************************************************************/
                    325: 
                    326: VOID NEAR PASCAL SetSaveFrom (
                    327:        HWND hwnd,
                    328:        PSTR psz)
                    329: {
                    330:     CHAR szFmt[32];
                    331:     CHAR szText[160];
                    332: 
                    333:     /* The text string in the .RC file contains the format string... */
                    334:     GetDlgItemText( hwnd, IDD_SAVEFROM, szFmt, sizeof(szFmt));
                    335: 
                    336:     /* NOTE: this (LPSTR) cast MUST be here... wsprintf() is a cdecl
                    337:      * (C calling conventions) function with varying args... there is
                    338:      * no way for the compiler to know that all strings must be LPSTR's
                    339:      * and do the conversion, so we have to be careful about wsprintf()'s.
                    340:      */
                    341:     wsprintf ( szText, szFmt, (LPSTR)psz);
                    342: 
                    343:     /* set the text in the static control */
                    344:     SetDlgItemText (hwnd, IDD_SAVEFROM, szText);
                    345: }
                    346: 
                    347: /****************************************************************************
                    348:  *                                                                         *
                    349:  *  FUNCTION   : SaveAsDlgProc(hwnd, message, wParam, lParam)              *
                    350:  *                                                                         *
                    351:  *  PURPOSE    : Dialog function File/SaveAs. It waits for a filename, and  *
                    352:  *              then calls SaveFile or cancels the operation.              *
                    353:  *                                                                         *
                    354:  ****************************************************************************/
                    355: 
                    356: BOOL APIENTRY SaveAsDlgProc(
                    357:        HWND hwnd,
                    358:        UINT message,
                    359:        UINT wParam,
                    360:        LONG lParam)
                    361: {
                    362:     CHAR   sz[64];
                    363:     CHAR   *pch;
                    364:     BOOL   fExt;
                    365:     HWND   hwndSave;
                    366: 
                    367:     switch (message){
                    368: 
                    369:        case WM_INITDIALOG:
                    370: 
                    371:            /* Identify the window whose contents we're saving */
                    372: #ifdef ORGCODE
                    373:            hwndSave = LOWORD (lParam);
                    374: #else
                    375:                hwndSave = (HWND)lParam;                /*passed in from another procedure*/
                    376: #endif
                    377:            /* Set it's name in the property list */
                    378:            SetProp (hwnd, PROP_FILENAME, hwndSave);
                    379: 
                    380:            GetWindowText (hwndSave, sz, sizeof(sz));
                    381: 
                    382:            /* Set the save from string... */
                    383:            SetSaveFrom (hwnd,sz);
                    384: 
                    385:            /* Generate a filename complete with extension */
                    386:            AnsiUpper (sz);
                    387:            for (fExt = FALSE, pch = sz; *pch; pch++)
                    388:                if (*pch == '.')
                    389:                    fExt = TRUE;
                    390:                else if (*pch == '\\')
                    391:                    fExt = FALSE;
                    392:            if (!fExt)
                    393:                LoadString (hInst, IDS_ADDEXT, (LPSTR)pch, pch - sz);
                    394: 
                    395:            /* Display the filename in the edit control */
                    396:            SetDlgItemText (hwnd, IDD_SAVETO, sz);
                    397: 
                    398:            /* Select the entire range of text */
                    399:            SendMessage(GetDlgItem(hwnd, IDD_SAVETO), EM_SETSEL, GET_EM_SETSEL_MPS(0, 100));
                    400: 
                    401:            DlgDirList (hwnd, "*.*", (INT)IDD_DIRS, (INT)IDD_PATH, (WORD)ATTR_DIRS);
                    402: 
                    403:            /* enable OK butto iff edit control is nonempty */
                    404:            if (!*sz)
                    405:                EnableWindow (GetDlgItem (hwnd, IDOK), FALSE);
                    406:            break;
                    407: 
                    408:        case WM_COMMAND:
                    409:            switch (LOWORD(wParam)){
                    410:                case IDCANCEL:
                    411:                    /* Abort operation */
                    412:                    EndDialog(hwnd,1);
                    413:                    break;
                    414: 
                    415:                case IDOK:
                    416:                   /*  Just change the title of the MDI child. The calling
                    417:                    *  function of ChangeFile(), which uses the title text
                    418:                    *  for the filename, will do the actual save.
                    419:                    */
                    420:                    hwndSave = GetProp (hwnd, PROP_FILENAME);
                    421:                    GetDlgItemText (hwnd, IDD_SAVETO, sz, sizeof(sz));
                    422:                    AnsiUpper ((LPSTR)sz);
                    423:                    SetWindowText (hwndSave, sz);
                    424:                    EndDialog (hwnd, 0);
                    425:                    break;
                    426: 
                    427:                case IDD_SAVETO:
                    428:                   /* If the edit control changes, check to see if its empty.
                    429:                    * enable OK if it contains something
                    430:                    */
                    431:                    if (HIWORD (lParam) != EN_CHANGE)
                    432:                        return FALSE;
                    433:                    EnableWindow (GetDlgItem (hwnd, IDOK),
                    434:                                SendDlgItemMessage (hwnd,
                    435:                                                   IDD_SAVETO,
                    436:                                                   WM_GETTEXTLENGTH,
                    437:                                                   0,
                    438:                                                   0L));
                    439:                    break;
                    440: 
                    441:                case IDD_DIRS:
                    442:                    if (HIWORD(lParam)==LBN_DBLCLK){
                    443:                        CHAR szT[64];
                    444: 
                    445:                        DlgDirSelectEx(hwnd, szT, 64, IDD_DIRS);
                    446:                        lstrcat ( szT, "*.*");
                    447:                        DlgDirList (hwnd, szT, (INT)IDD_DIRS, (INT)IDD_PATH, (WORD)ATTR_DIRS);
                    448:                        break;
                    449:                    }
                    450:                    return FALSE;
                    451: 
                    452:                default:
                    453:                    return FALSE;
                    454:            }
                    455: 
                    456:        default:
                    457:            return FALSE;
                    458:     }
                    459:     return TRUE;
                    460: }
                    461: 
                    462: /****************************************************************************
                    463:  *                                                                         *
                    464:  *  FUNCTION   : ChangeFile (hwnd)                                         *
                    465:  *                                                                         *
                    466:  *  PURPOSE    : Invokes the File/SaveAs dialog.                           *
                    467:  *                                                                         *
                    468:  *  RETURNS    : TRUE  - if user selected OK or NO.                        *
                    469:  *              FALSE - otherwise.                                         *
                    470:  *                                                                         *
                    471:  ****************************************************************************/
                    472: 
                    473: BOOL APIENTRY ChangeFile (HWND hwnd)
                    474: {
                    475:     INT     i;
                    476: 
                    477: #ifdef NOTCOMMONDIALOGS
1.1.1.2   root      478:     i = DialogBoxParam (hInst, IDD_SAVEAS, hwnd, SaveAsDlgProc, (LONG)hwnd);
1.1       root      479:     if (!i)
                    480:            SetWindowWord (hwnd, GWW_UNTITLED, 0);
                    481:     return !i;
                    482: #else
                    483:     OPENFILENAME ofn;
                    484:     CHAR szFilterSpec [128] =                       /* file type filters */
                    485:              "TEXT FILES(*.TXT)\0*.txt\0";
                    486: 
                    487:     #define MAXFILENAME 256
                    488:     CHAR szFileName[MAXFILENAME];
                    489:     CHAR szFileTitle[MAXFILENAME];
                    490: 
                    491:     strcpy(szFileName, "");   /* these need be NULL*/
                    492:     strcpy(szFileTitle, "");
                    493: 
                    494:     /* fill in non-variant fields of OPENFILENAME struct. */
                    495:     ofn.lStructSize       = sizeof(OPENFILENAME);
                    496:     ofn.hwndOwner        = hwnd;
                    497:     ofn.lpstrFilter      = szFilterSpec;
                    498:     ofn.lpstrCustomFilter = NULL;
                    499:     ofn.nMaxCustFilter   = 0;
                    500:     ofn.nFilterIndex     = 0;
                    501:     ofn.lpstrFile         = szFileName;
                    502:     ofn.nMaxFile         = MAXFILENAME;
                    503:     ofn.lpstrInitialDir   = NULL;
                    504:     ofn.lpstrFileTitle    = szFileTitle;
                    505:     ofn.nMaxFileTitle     = MAXFILENAME;
                    506:     ofn.lpstrTitle        = "Save TextFiles";
                    507:     ofn.lpstrDefExt       = "TXT";
                    508:     ofn.Flags             = NULL;
                    509:     /* Use standard open dialog */
                    510:     i = GetSaveFileName ((LPOPENFILENAME)&ofn);
                    511:        AnsiUpper ((LPSTR)ofn.lpstrFile);
                    512:        SetWindowText (hwnd, ofn.lpstrFile);
                    513:     if (i)
                    514:            SetWindowWord (hwnd, GWW_UNTITLED, 0);
                    515:     return i;
                    516: #endif
                    517: 
                    518: }

unix.superglobalmegacorp.com

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