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