Annotation of mstools/mfc/samples/multipad/mpfile.cpp, revision 1.1.1.2

1.1       root        1: // mpfile.cpp : Defines the child's file code for the application.
                      2: //
                      3: // This is a part of the Microsoft Foundation Classes C++ library.
                      4: // Copyright (C) 1992 Microsoft Corporation
                      5: // All rights reserved.
                      6: //
                      7: // This source code is only intended as a supplement to the
                      8: // Microsoft Foundation Classes Reference and Microsoft
                      9: // QuickHelp documentation provided with the library.
                     10: // See these sources for detailed information regarding the
                     11: // Microsoft Foundation Classes product.
                     12: 
                     13: #include "multipad.h"
                     14: 
                     15: #include <commdlg.h>
                     16: 
1.1.1.2 ! root       17: #ifndef _NTWIN
1.1       root       18: #pragma code_seg("_MPFILE")
1.1.1.2 ! root       19: #endif
1.1       root       20: 
                     21: static char BASED_CODE szFileDialogFilter[] = 
                     22:                "Text Files (*.txt)|*.txt|All Files (*.*)|*.*||";
                     23: static char BASED_CODE szFileDialogExt[] = "txt";
                     24: 
                     25: extern void AddFileToMRU(const char* szFileName);
                     26:        
                     27: /////////////////////////////////////////////////////////////////////////////
                     28: 
                     29: // AlreadyOpen:
                     30: // Just looks through the children for a child with the same filename.
                     31: // Strange path equivalents can confuse this simple approach, but these
                     32: // are rare.
                     33: //
                     34: CMPChild* CMPFrame::AlreadyOpen(char* szFile)
                     35: {
                     36:        CWnd* pwndCheck;
                     37:        char szChild[64];
                     38:        
                     39:        // Check each MDI child window in Multipad.
                     40:        //
                     41:        for (pwndCheck = CWnd::FromHandle(::GetWindow(m_hWndMDIClient, GW_CHILD));
                     42:                pwndCheck != NULL; pwndCheck = pwndCheck->GetNextWindow())
                     43:        {
                     44:                // Skip icon title windows.
                     45:                //
                     46:                if (pwndCheck->GetWindow(GW_OWNER) != NULL)
                     47:                        continue;
                     48:                
                     49:                // Get current child window's name
                     50:                //
                     51:                pwndCheck->GetWindowText(szChild, sizeof (szChild));
                     52:        
                     53:                if (lstrcmp(szChild, szFile) == 0)
                     54:                        return (CMPChild*)pwndCheck;
                     55:        }
                     56:        
                     57:        // No match found -- file is not open.
                     58:        //
                     59:        return NULL;
                     60: }
                     61: 
                     62: // CMPChild constructor:
                     63: // Opens the given file, reads the text into a buffer, and ties the buffer
                     64: // to a CEdit inside the child.
                     65: //
                     66: CMPChild::CMPChild(char* szName)
                     67: {
                     68:        static int cUntitled = 0;
                     69:        char sz[160];
                     70:        
                     71:        if (!szName)
                     72:        {
                     73:                char* pch = sz + ::LoadString(AfxGetResourceHandle(), IDS_UNTITLED,
                     74:                        sz, sizeof(sz));
                     75:                sprintf(pch, "%d", ++cUntitled);
                     76:        }
                     77:        else
                     78:        {
                     79:                strcpy(sz, szName);
                     80:        }
                     81:        
                     82:        if (CMPFrame::GetActiveChild() == NULL)
                     83:                Create(NULL, sz, WS_MAXIMIZE);
                     84:        else
                     85:                Create(NULL, sz, 0L);
                     86:        
                     87:        if (szName != NULL)
                     88:        {
                     89:                if (!LoadFile(szName))
                     90:                {
                     91:                        DestroyWindow();
                     92:                }
                     93:        }
                     94: }
                     95: 
                     96: // LoadFile:
                     97: // Reads one file to replace the current buffer's text.
                     98: //
                     99: int CMPChild::LoadFile(char* pName)
                    100: {
                    101:        UINT nLength;
                    102:        HANDLE hTextWnd;
                    103:        LPSTR lpBuffer;
                    104:        CFile file;
                    105:        
                    106:        // The file has a title, so reset the UNTITLED flag.
                    107:        //
                    108:        m_bUntitled = FALSE;
                    109:        
                    110:        if (!file.Open(pName, CFile::modeRead))
                    111:                goto error;
                    112: 
                    113:        nLength = (UINT) file.GetLength();
                    114: 
                    115:        // Attempt to reallocate the edit control's buffer to the file size.
                    116:        //
                    117:        hTextWnd = m_edit.GetHandle();
                    118:        if (LocalReAlloc(hTextWnd, nLength + 1, LHND) == NULL)
                    119:        {
                    120:                // Couldn't reallocate to new size -- error!
                    121:                //
                    122:                file.Close();
                    123: 
                    124:                TRACE("FILE TOO BIG %ld %s\n", file.GetLength(), pName);
                    125:                MPError(MB_OK | MB_ICONHAND, IDS_FILETOOBIG, (LPCSTR)pName);
                    126:                return FALSE;
                    127:        }
                    128:        
                    129:        // Read the file into the buffer.
                    130:        // If that fails, tell the user.
                    131:        //
                    132:        TRY
                    133:        {
                    134:                file.Read((lpBuffer = (LPSTR)LocalLock (hTextWnd)), nLength);
                    135:        }
                    136:        CATCH(CFileException, e)
                    137:        {
                    138:                MPError(MB_OK | MB_ICONHAND, IDS_CANTREAD, (LPCSTR)pName);
                    139:        }
                    140:        END_CATCH
                    141:        
                    142:        // Zero-terminate the edit buffer.
                    143:        //
                    144:        lpBuffer[nLength] = 0;
                    145:        LocalUnlock(hTextWnd);
                    146:        
                    147:        m_edit.SetHandle(hTextWnd);
                    148:        file.Close();
                    149:        
                    150:        AddFileToMRU(pName);
                    151:        
                    152:        return TRUE;
                    153:        
                    154: error:
                    155:        // Report the error and quit.
                    156:        //
                    157:        MPError(MB_OK | MB_ICONHAND, IDS_CANTOPEN, (LPCSTR)pName);
                    158:        return FALSE;
                    159: }
                    160: 
                    161: // ReadFile:
                    162: // Gets a filename from the user, then creates a new CMPChild for that file.
                    163: //
                    164: void CMPFrame::ReadFile()
                    165: {
                    166:        CFileDialog fileDialog(TRUE, szFileDialogExt, NULL,
                    167:                OFN_HIDEREADONLY, szFileDialogFilter);
                    168:        
                    169:        if (fileDialog.DoModal() == IDOK)
                    170:                ReadFile(fileDialog.GetPathName());
                    171: }
                    172: 
                    173: void CMPFrame::ReadFile(const char* szFile)
                    174: {
                    175:        CWnd* pwndFile;
                    176:        CFileStatus fileStatus;
                    177:        
                    178:        // Get full path to file.
                    179:        //
                    180:        if (CFile::GetStatus(szFile, fileStatus))
                    181:        {
                    182:                // Is file already open?
                    183:                // If so, just bring the file's window to the top.
                    184:                //
                    185:                if ((pwndFile = AlreadyOpen(fileStatus.m_szFullName)) != NULL)
                    186:                {
                    187:                        pwndFile->BringWindowToTop();
                    188:                        return;
                    189:                }
                    190:        }
                    191:        
                    192:        // Make a new window and load file into it.
                    193:        //
                    194:        new CMPChild(fileStatus.m_szFullName);
                    195: }
                    196: 
                    197: // SaveFile:
                    198: // Writes the text into its file.
                    199: //
                    200: void CMPChild::SaveFile()
                    201: {
                    202:        HANDLE hTextWnd;
                    203:        LPSTR lpExt;
                    204:        char szFile[128];
                    205:        UINT cch;
                    206:        CFile file;
                    207: 
                    208:        GetWindowText(szFile, sizeof(szFile));
                    209:        for (lpExt = szFile; *lpExt; lpExt++)
                    210:        {
                    211:                switch (*lpExt)
                    212:                {
                    213:                        case '.':
                    214:                         cch = TRUE;
                    215:                         break;
                    216: 
                    217:                        case '\\':
                    218:                        case ':' :
                    219:                         cch = FALSE;
                    220:                         break;
                    221:                }
                    222:        }
                    223: 
                    224:        if (!cch)
                    225:        {
                    226:                ::LoadString(AfxGetResourceHandle(), IDS_ADDEXT, lpExt,
                    227:                        lpExt - (LPSTR)szFile);
                    228:        }
                    229: 
                    230: 
                    231:        if (!file.Open(szFile, CFile::modeCreate | CFile::modeWrite))
                    232:        {
                    233:                MPError (MB_OK | MB_ICONHAND, IDS_CANTCREATE, (LPCSTR)szFile);
                    234:                return;
                    235:        }
                    236:        
                    237:        // Find out the length of the text in the edit control
                    238:        cch = m_edit.GetWindowTextLength();
                    239: 
                    240:        // Obtain a handle to the text buffer
                    241:        hTextWnd = m_edit.GetHandle();
                    242:        lpExt = (LPSTR)LocalLock (hTextWnd);
                    243:        
                    244:        // Write out the contents of the buffer to the file.
                    245:        TRY
                    246:        {
                    247:                file.Write(lpExt, cch);
                    248:                file.Close();
                    249:        }
                    250:        CATCH(CFileException, e)
                    251:        {
                    252:                file.Close();
                    253:                MPError(MB_OK | MB_ICONHAND, IDS_CANTWRITE, (LPCSTR)szFile);
                    254:        }
                    255:        END_CATCH
                    256: 
                    257:        // Clean up
                    258:        LocalUnlock(hTextWnd);
                    259:        m_edit.SetHandle(hTextWnd);
                    260:        
                    261:        m_bChanged = FALSE;
                    262: }
                    263: 
                    264: BOOL CMPChild::ChangeFile()
                    265: {
                    266:        char szTitle[_MAX_PATH];
                    267:        GetWindowText((LPSTR)szTitle, sizeof(szTitle));
                    268:        ::GetFileTitle((LPSTR)szTitle, (LPSTR)szTitle, sizeof(szTitle));
                    269:        
                    270:        CFileDialog fileDialog(FALSE, szFileDialogExt, szTitle,
                    271:                OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT, szFileDialogFilter);
                    272: 
                    273:        if (fileDialog.DoModal() == IDOK)
                    274:        {
                    275:                CString s = fileDialog.GetPathName();
                    276:                AddFileToMRU(s);
                    277:                SetWindowText(s);
                    278:                m_bUntitled = FALSE;
                    279:                return TRUE;
                    280:        }
                    281:        else
                    282:                return FALSE;
                    283: }

unix.superglobalmegacorp.com

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