--- mstools/samples/multipad/mpfile.c 2018/08/09 18:20:40 1.1.1.2 +++ mstools/samples/multipad/mpfile.c 2018/08/09 18:23:38 1.1.1.4 @@ -1,28 +1,39 @@ + +/******************************************************************************\ +* This is a part of the Microsoft Source Code Samples. +* Copyright (C) 1993 Microsoft Corporation. +* All rights reserved. +* This source code is only intended as a supplement to +* Microsoft Development Tools and/or WinHelp documentation. +* See these sources for detailed information regarding the +* Microsoft samples programs. +\******************************************************************************/ + /*************************************************************************** - * * - * MODULE : MpFile.c * - * * - * PURPOSE : Contains the code for File I/O for Multipad. * - * * - * FUNCTIONS : AlreadyOpen - Determines if a file is already open. * - * * - * AddFile - Creates a new MDI window and, if specified,* - * loads a file into it. * - * * - * LoadFile - Loads a file into a MDI window. * - * * - * MyReadFile - Calls File/Open dialog and appropriately * - * responds to the user's input. * - * * - * SaveFile - Saves the contents of a MDI window's edit * - * control to a file. * - * * - * SetSaveFrom - Formats the "Save 'file' to" string. * - * * - * SaveAsDlgProc - Dialog function for the File/SaveAs dialog.* - * * - * ChangeFile - Calls File/SaveAs dialog. * - * * + * * + * MODULE : MpFile.c * + * * + * PURPOSE : Contains the code for File I/O for Multipad. * + * * + * FUNCTIONS : AlreadyOpen - Determines if a file is already open. * + * * + * AddFile - Creates a new MDI window and, if specified,* + * loads a file into it. * + * * + * LoadFile - Loads a file into a MDI window. * + * * + * MyReadFile - Calls File/Open dialog and appropriately * + * responds to the user's input. * + * * + * SaveFile - Saves the contents of a MDI window's edit * + * control to a file. * + * * + * SetSaveFrom - Formats the "Save 'file' to" string. * + * * + * SaveAsDlgProc - Dialog function for the File/SaveAs dialog.* + * * + * ChangeFile - Calls File/SaveAs dialog. * + * * ***************************************************************************/ #include "multipad.h" #include @@ -33,19 +44,19 @@ VOID APIENTRY GetFileName(PSTR); //INT APIENTRY DialogBoxParam(HANDLE,LPSTR,HWND,FARPROC,LONG); -LPSTR AnsiUpper(LPSTR); +//LPSTR WINAPI AnsiUpper(LPSTR); -OFSTRUCT of; +OFSTRUCT of; /**************************************************************************** - * * - * FUNCTION : AlreadyOpen(szFile) * - * * + * * + * FUNCTION : AlreadyOpen(szFile) * + * * * PURPOSE : Checks to see if the file described by the string pointed * - * to by 'szFile' is already open. * - * * + * to by 'szFile' is already open. * + * * * RETURNS : a handle to the described file's window if that file is * - * already open; NULL otherwise. * - * * + * already open; NULL otherwise. * + * * ****************************************************************************/ HWND AlreadyOpen(CHAR *szFile) @@ -61,73 +72,73 @@ HWND AlreadyOpen(CHAR *szFile) */ wFileTemp = OpenFile((LPSTR)szFile, (LPOFSTRUCT)&of, OF_PARSE); if (! wFileTemp) - return(NULL); + return(NULL); _lclose(wFileTemp); /* Check each MDI child window in Multipad */ for ( hwndCheck = GetWindow(hwndMDIClient, GW_CHILD); - hwndCheck; - hwndCheck = GetWindow(hwndCheck, GW_HWNDNEXT) ) { - /* Initialization for comparison */ - lpChild = szChild; - lpFile = (LPSTR)AnsiUpper((LPSTR) of.szPathName); - iDiff = 0; - - /* Skip icon title windows */ - if (GetWindow(hwndCheck, GW_OWNER)) - continue; - - /* Get current child window's name */ - GetWindowText(hwndCheck, lpChild, 64); - - /* Compare window name with given name */ - while ((*lpChild) && (*lpFile) && (!iDiff)){ - if (*lpChild++ != *lpFile++) - iDiff = 1; - } - - /* If the two names matched, the file is already */ - /* open -- return handle to matching child window. */ - if (!iDiff) - return(hwndCheck); + hwndCheck; + hwndCheck = GetWindow(hwndCheck, GW_HWNDNEXT) ) { + /* Initialization for comparison */ + lpChild = szChild; + lpFile = (LPSTR)AnsiUpper((LPSTR) of.szPathName); + iDiff = 0; + + /* Skip icon title windows */ + if (GetWindow(hwndCheck, GW_OWNER)) + continue; + + /* Get current child window's name */ + GetWindowText(hwndCheck, lpChild, 64); + + /* Compare window name with given name */ + while ((*lpChild) && (*lpFile) && (!iDiff)){ + if (*lpChild++ != *lpFile++) + iDiff = 1; + } + + /* If the two names matched, the file is already */ + /* open -- return handle to matching child window. */ + if (!iDiff) + return(hwndCheck); } /* No match found -- file is not open -- return NULL handle */ return(NULL); } /**************************************************************************** - * * - * FUNCTION : AddFile (lpName) * - * * + * * + * FUNCTION : AddFile (lpName) * + * * * PURPOSE : Creates a new MDI window. If the lpName parameter is not * - * NULL, it loads a file into the window. * - * * - * RETURNS : HWND - A handle to the new window. * - * * + * NULL, it loads a file into the window. * + * * + * RETURNS : HWND - A handle to the new window. * + * * ****************************************************************************/ HWND APIENTRY AddFile(CHAR * pName) { HWND hwnd; - CHAR sz[160]; + CHAR sz[160]; MDICREATESTRUCT mcs; if (!pName) { - /* The pName parameter is NULL -- load the "Untitled" string from */ - /* STRINGTABLE and set the title field of the MDI CreateStruct. */ - LoadString (hInst, IDS_UNTITLED, sz, sizeof(sz)); - mcs.szTitle = (LPSTR)sz; + /* The pName parameter is NULL -- load the "Untitled" string from */ + /* STRINGTABLE and set the title field of the MDI CreateStruct. */ + LoadString (hInst, IDS_UNTITLED, sz, sizeof(sz)); + mcs.szTitle = (LPSTR)sz; } else - /* Title the window with the fully qualified pathname obtained by - * calling OpenFile() with the OF_PARSE flag (in function - * AlreadyOpen(), which is called before AddFile(). - */ - mcs.szTitle = of.szPathName; + /* Title the window with the fully qualified pathname obtained by + * calling OpenFile() with the OF_PARSE flag (in function + * AlreadyOpen(), which is called before AddFile(). + */ + mcs.szTitle = of.szPathName; mcs.szClass = szChild; - mcs.hOwner = hInst; + mcs.hOwner = hInst; /* Use the default size for the window */ mcs.x = mcs.cx = CW_USEDEFAULT; @@ -138,43 +149,43 @@ HWND APIENTRY AddFile(CHAR * pName) /* tell the MDI Client to create the child */ hwnd = (HWND)SendMessage (hwndMDIClient, - WM_MDICREATE, - 0, - (LONG)(LPMDICREATESTRUCT)&mcs); + WM_MDICREATE, + 0, + (LONG)(LPMDICREATESTRUCT)&mcs); /* Did we get a file? Read it into the window */ if (pName){ - if (!LoadFile(hwnd, pName)){ - /* File couldn't be loaded -- close window */ - SendMessage(hwndMDIClient, WM_MDIDESTROY, (DWORD) hwnd, 0L); - } + if (!LoadFile(hwnd, pName)){ + /* File couldn't be loaded -- close window */ + SendMessage(hwndMDIClient, WM_MDIDESTROY, (DWORD) hwnd, 0L); + } } return hwnd; } /**************************************************************************** - * * - * FUNCTION : LoadFile (lpName) * - * * + * * + * FUNCTION : LoadFile (lpName) * + * * * PURPOSE : Given the handle to a MDI window and a filename, reads the * - * file into the window's edit control child. * - * * - * RETURNS : TRUE - If file is sucessfully loaded. * - * FALSE - Otherwise. * - * * + * file into the window's edit control child. * + * * + * RETURNS : TRUE - If file is sucessfully loaded. * + * FALSE - Otherwise. * + * * ****************************************************************************/ INT APIENTRY LoadFile ( - HWND hwnd, - CHAR * pName) + HWND hwnd, + CHAR * pName) { LONG wLength; HANDLE hT; LPSTR lpB; HWND hwndEdit; HFILE fh; - OFSTRUCT of; + OFSTRUCT of; hwndEdit = (HWND)GetWindowLong (hwnd, GWL_HWNDEDIT); /* The file has a title, so reset the UNTITLED flag. */ @@ -184,7 +195,7 @@ INT APIENTRY LoadFile ( /* Make sure file has been opened correctly */ if ( fh < 0 ) - goto error; + goto error; /* Find the length of the file */ wLength = (DWORD)_llseek(fh, 0L, 2); @@ -193,14 +204,14 @@ INT APIENTRY LoadFile ( /* Attempt to reallocate the edit control's buffer to the file size */ hT = (HANDLE)SendMessage (hwndEdit, EM_GETHANDLE, 0, 0L); if (LocalReAlloc(hT, wLength+1, LHND) == NULL) { - /* Couldn't reallocate to new size -- error */ - _lclose(fh); - goto error; + /* Couldn't reallocate to new size -- error */ + _lclose(fh); + goto error; } /* read the file into the buffer */ if (wLength != (LONG)_lread(fh, (lpB = (LPSTR)LocalLock (hT)), (UINT)wLength)) - MPError (hwnd, MB_OK|MB_ICONHAND, IDS_CANTREAD, (LPSTR)pName); + MPError (hwnd, MB_OK|MB_ICONHAND, IDS_CANTREAD, (LPSTR)pName); /* Zero terminate the edit buffer */ lpB[wLength] = 0; @@ -218,12 +229,12 @@ error: } /**************************************************************************** - * * - * FUNCTION : MyReadFile(hwnd) * - * * + * * + * FUNCTION : MyReadFile(hwnd) * + * * * PURPOSE : Called in response to a File/Open menu selection. It asks * - * the user for a file name and responds appropriately. * - * * + * the user for a file name and responds appropriately. * + * * ****************************************************************************/ VOID APIENTRY MyReadFile(HWND hwnd) @@ -235,25 +246,25 @@ VOID APIENTRY MyReadFile(HWND hwnd) /* If the result is not the empty string -- take appropriate action */ if (*szFile) { - /* Is file already open?? */ - if (hwndFile = AlreadyOpen(szFile)) { - /* Yes -- bring the file's window to the top */ - BringWindowToTop(hwndFile); - } - else { - /* No -- make a new window and load file into it */ - AddFile(szFile); - } + /* Is file already open?? */ + if (hwndFile = AlreadyOpen(szFile)) { + /* Yes -- bring the file's window to the top */ + BringWindowToTop(hwndFile); + } + else { + /* No -- make a new window and load file into it */ + AddFile(szFile); + } } - UNREFERENCED_PARAMETER(hwnd); + UNREFERENCED_PARAMETER(hwnd); } /**************************************************************************** - * * - * FUNCTION : SaveFile (hwnd) * - * * - * PURPOSE : Saves contents of current edit control to disk. * - * * + * * + * FUNCTION : SaveFile (hwnd) * + * * + * PURPOSE : Saves contents of current edit control to disk. * + * * ****************************************************************************/ VOID APIENTRY SaveFile(HWND hwnd) @@ -273,37 +284,37 @@ VOID APIENTRY SaveFile(HWND hwnd) /* If there is no extension (control is 'Untitled') add .TXT as extension */ for (cch = FALSE, lpT = szFile; *lpT; lpT++) - switch (*lpT){ - case '.': - cch = TRUE; - break; - - case '\\': - case ':' : - cch = FALSE; - break; - } + switch (*lpT){ + case '.': + cch = TRUE; + break; + + case '\\': + case ':' : + cch = FALSE; + break; + } if (!cch) - LoadString (hInst, IDS_ADDEXT, lpT, lpT - (LPSTR)szFile); + LoadString (hInst, IDS_ADDEXT, lpT, lpT - (LPSTR)szFile); fh = open(szFile, O_BINARY | O_WRONLY | O_CREAT, S_IWRITE); /* If file could not be opened, quit */ if (fh < 0){ - MPError (hwnd, MB_OK | MB_ICONHAND, IDS_CANTCREATE, (LPSTR)szFile); - return; + MPError (hwnd, MB_OK | MB_ICONHAND, IDS_CANTCREATE, (LPSTR)szFile); + return; } /* Find out the length of the text in the edit control */ cch = GetWindowTextLength (hwndEdit); /* Obtain a handle to the text buffer */ - hT = (HANDLE)SendMessage (hwndEdit, EM_GETHANDLE, 0, 0L); + hT = (HANDLE)SendMessage (hwndEdit, EM_GETHANDLE, 0, 0L); lpT = (LPSTR)LocalLock (hT); /* Write out the contents of the buffer to the file. */ if (cch != write(fh, lpT, cch)) - MPError (hwnd, MB_OK | MB_ICONHAND, IDS_CANTWRITE, (LPSTR)szFile); + MPError (hwnd, MB_OK | MB_ICONHAND, IDS_CANTWRITE, (LPSTR)szFile); /* Clean up */ LocalUnlock (hT); @@ -312,20 +323,20 @@ VOID APIENTRY SaveFile(HWND hwnd) close(fh); return; - UNREFERENCED_PARAMETER(pch); + UNREFERENCED_PARAMETER(pch); } /**************************************************************************** - * * - * FUNCTION : SetSaveFrom () * - * * + * * + * FUNCTION : SetSaveFrom () * + * * * PURPOSE : Formats the "Save 'file' to .." string. * - * * + * * ****************************************************************************/ VOID NEAR PASCAL SetSaveFrom ( - HWND hwnd, - PSTR psz) + HWND hwnd, + PSTR psz) { CHAR szFmt[32]; CHAR szText[160]; @@ -345,19 +356,19 @@ VOID NEAR PASCAL SetSaveFrom ( } /**************************************************************************** - * * - * FUNCTION : SaveAsDlgProc(hwnd, message, wParam, lParam) * - * * + * * + * FUNCTION : SaveAsDlgProc(hwnd, message, wParam, lParam) * + * * * PURPOSE : Dialog function File/SaveAs. It waits for a filename, and * - * then calls SaveFile or cancels the operation. * - * * + * then calls SaveFile or cancels the operation. * + * * ****************************************************************************/ BOOL APIENTRY SaveAsDlgProc( - HWND hwnd, - UINT message, - UINT wParam, - LONG lParam) + HWND hwnd, + UINT message, + UINT wParam, + LONG lParam) { CHAR sz[64]; CHAR *pch; @@ -366,108 +377,108 @@ BOOL APIENTRY SaveAsDlgProc( switch (message){ - case WM_INITDIALOG: + case WM_INITDIALOG: - /* Identify the window whose contents we're saving */ + /* Identify the window whose contents we're saving */ #ifdef ORGCODE - hwndSave = LOWORD (lParam); + hwndSave = LOWORD (lParam); #else - hwndSave = (HWND)lParam; /*passed in from another procedure*/ + hwndSave = (HWND)lParam; /*passed in from another procedure*/ #endif - /* Set it's name in the property list */ - SetProp (hwnd, PROP_FILENAME, hwndSave); + /* Set it's name in the property list */ + SetProp (hwnd, PROP_FILENAME, hwndSave); - GetWindowText (hwndSave, sz, sizeof(sz)); + GetWindowText (hwndSave, sz, sizeof(sz)); - /* Set the save from string... */ - SetSaveFrom (hwnd,sz); + /* Set the save from string... */ + SetSaveFrom (hwnd,sz); - /* Generate a filename complete with extension */ - AnsiUpper (sz); - for (fExt = FALSE, pch = sz; *pch; pch++) - if (*pch == '.') - fExt = TRUE; - else if (*pch == '\\') - fExt = FALSE; - if (!fExt) - LoadString (hInst, IDS_ADDEXT, (LPSTR)pch, pch - sz); - - /* Display the filename in the edit control */ - SetDlgItemText (hwnd, IDD_SAVETO, sz); - - /* Select the entire range of text */ - SendMessage(GetDlgItem(hwnd, IDD_SAVETO), EM_SETSEL, GET_EM_SETSEL_MPS(0, 100)); - - DlgDirList (hwnd, "*.*", (INT)IDD_DIRS, (INT)IDD_PATH, (WORD)ATTR_DIRS); - - /* enable OK butto iff edit control is nonempty */ - if (!*sz) - EnableWindow (GetDlgItem (hwnd, IDOK), FALSE); - break; - - case WM_COMMAND: - switch (LOWORD(wParam)){ - case IDCANCEL: - /* Abort operation */ - EndDialog(hwnd,1); - break; - - case IDOK: - /* Just change the title of the MDI child. The calling - * function of ChangeFile(), which uses the title text - * for the filename, will do the actual save. - */ - hwndSave = GetProp (hwnd, PROP_FILENAME); - GetDlgItemText (hwnd, IDD_SAVETO, sz, sizeof(sz)); - AnsiUpper ((LPSTR)sz); - SetWindowText (hwndSave, sz); - EndDialog (hwnd, 0); - break; - - case IDD_SAVETO: - /* If the edit control changes, check to see if its empty. - * enable OK if it contains something - */ - if (HIWORD (lParam) != EN_CHANGE) - return FALSE; - EnableWindow (GetDlgItem (hwnd, IDOK), - SendDlgItemMessage (hwnd, - IDD_SAVETO, - WM_GETTEXTLENGTH, - 0, - 0L)); - break; - - case IDD_DIRS: - if (HIWORD(lParam)==LBN_DBLCLK){ - CHAR szT[64]; - - DlgDirSelectEx(hwnd, szT, 64, IDD_DIRS); - lstrcat ( szT, "*.*"); - DlgDirList (hwnd, szT, (INT)IDD_DIRS, (INT)IDD_PATH, (WORD)ATTR_DIRS); - break; - } - return FALSE; - - default: - return FALSE; - } + /* Generate a filename complete with extension */ + AnsiUpper (sz); + for (fExt = FALSE, pch = sz; *pch; pch++) + if (*pch == '.') + fExt = TRUE; + else if (*pch == '\\') + fExt = FALSE; + if (!fExt) + LoadString (hInst, IDS_ADDEXT, (LPSTR)pch, pch - sz); + + /* Display the filename in the edit control */ + SetDlgItemText (hwnd, IDD_SAVETO, sz); + + /* Select the entire range of text */ + SendMessage(GetDlgItem(hwnd, IDD_SAVETO), EM_SETSEL, GET_EM_SETSEL_MPS(0, 100)); + + DlgDirList (hwnd, "*.*", (INT)IDD_DIRS, (INT)IDD_PATH, (WORD)ATTR_DIRS); + + /* enable OK butto iff edit control is nonempty */ + if (!*sz) + EnableWindow (GetDlgItem (hwnd, IDOK), FALSE); + break; + + case WM_COMMAND: + switch (LOWORD(wParam)){ + case IDCANCEL: + /* Abort operation */ + EndDialog(hwnd,1); + break; + + case IDOK: + /* Just change the title of the MDI child. The calling + * function of ChangeFile(), which uses the title text + * for the filename, will do the actual save. + */ + hwndSave = GetProp (hwnd, PROP_FILENAME); + GetDlgItemText (hwnd, IDD_SAVETO, sz, sizeof(sz)); + AnsiUpper ((LPSTR)sz); + SetWindowText (hwndSave, sz); + EndDialog (hwnd, 0); + break; + + case IDD_SAVETO: + /* If the edit control changes, check to see if its empty. + * enable OK if it contains something + */ + if (HIWORD (lParam) != EN_CHANGE) + return FALSE; + EnableWindow (GetDlgItem (hwnd, IDOK), + SendDlgItemMessage (hwnd, + IDD_SAVETO, + WM_GETTEXTLENGTH, + 0, + 0L)); + break; + + case IDD_DIRS: + if (HIWORD(lParam)==LBN_DBLCLK){ + CHAR szT[64]; + + DlgDirSelectEx(hwnd, szT, 64, IDD_DIRS); + lstrcat ( szT, "*.*"); + DlgDirList (hwnd, szT, (INT)IDD_DIRS, (INT)IDD_PATH, (WORD)ATTR_DIRS); + break; + } + return FALSE; + + default: + return FALSE; + } - default: - return FALSE; + default: + return FALSE; } return TRUE; } /**************************************************************************** - * * - * FUNCTION : ChangeFile (hwnd) * - * * - * PURPOSE : Invokes the File/SaveAs dialog. * - * * - * RETURNS : TRUE - if user selected OK or NO. * - * FALSE - otherwise. * - * * + * * + * FUNCTION : ChangeFile (hwnd) * + * * + * PURPOSE : Invokes the File/SaveAs dialog. * + * * + * RETURNS : TRUE - if user selected OK or NO. * + * FALSE - otherwise. * + * * ****************************************************************************/ BOOL APIENTRY ChangeFile (HWND hwnd) @@ -477,7 +488,7 @@ BOOL APIENTRY ChangeFile (HWND hwnd) #ifdef NOTCOMMONDIALOGS i = DialogBoxParam (hInst, IDD_SAVEAS, hwnd, SaveAsDlgProc, (LONG)hwnd); if (!i) - SetWindowWord (hwnd, GWW_UNTITLED, 0); + SetWindowWord (hwnd, GWW_UNTITLED, 0); return !i; #else OPENFILENAME ofn; @@ -493,25 +504,25 @@ BOOL APIENTRY ChangeFile (HWND hwnd) /* fill in non-variant fields of OPENFILENAME struct. */ ofn.lStructSize = sizeof(OPENFILENAME); - ofn.hwndOwner = hwnd; - ofn.lpstrFilter = szFilterSpec; + ofn.hwndOwner = hwnd; + ofn.lpstrFilter = szFilterSpec; ofn.lpstrCustomFilter = NULL; - ofn.nMaxCustFilter = 0; - ofn.nFilterIndex = 0; + ofn.nMaxCustFilter = 0; + ofn.nFilterIndex = 0; ofn.lpstrFile = szFileName; - ofn.nMaxFile = MAXFILENAME; + ofn.nMaxFile = MAXFILENAME; ofn.lpstrInitialDir = NULL; ofn.lpstrFileTitle = szFileTitle; ofn.nMaxFileTitle = MAXFILENAME; ofn.lpstrTitle = "Save TextFiles"; ofn.lpstrDefExt = "TXT"; - ofn.Flags = NULL; + ofn.Flags = 0; /* Use standard open dialog */ i = GetSaveFileName ((LPOPENFILENAME)&ofn); - AnsiUpper ((LPSTR)ofn.lpstrFile); - SetWindowText (hwnd, ofn.lpstrFile); + AnsiUpper ((LPSTR)ofn.lpstrFile); + SetWindowText (hwnd, ofn.lpstrFile); if (i) - SetWindowWord (hwnd, GWW_UNTITLED, 0); + SetWindowWord (hwnd, GWW_UNTITLED, 0); return i; #endif