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

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

unix.superglobalmegacorp.com

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