How to reload a modified CDocument
-
Hi, in a SDI application I want to reload a document that was modified with CDosument::SetModified (TRUE) by clicking on the document in the MRU list. But the framework will recognize the document as already beeing loaded and only activates it as current active document again. There seems to be also no simple method to override to allow such a behavoir. Two functions are involved in this: CDocManager::OpenDocumentFile and CDocTemplate::MatchDocType Both are virtual but both are also non trivial. This function does not belong into the CDocTemplate, since I would like to have it for all Documents in a MDI Application. But this function isn't trivial at all. Finally I have CWinApp::OnOpenRecentFile. But this isn't a real option also. Is there any simple way to do this? Thanks Dirk
-
Hi, in a SDI application I want to reload a document that was modified with CDosument::SetModified (TRUE) by clicking on the document in the MRU list. But the framework will recognize the document as already beeing loaded and only activates it as current active document again. There seems to be also no simple method to override to allow such a behavoir. Two functions are involved in this: CDocManager::OpenDocumentFile and CDocTemplate::MatchDocType Both are virtual but both are also non trivial. This function does not belong into the CDocTemplate, since I would like to have it for all Documents in a MDI Application. But this function isn't trivial at all. Finally I have CWinApp::OnOpenRecentFile. But this isn't a real option also. Is there any simple way to do this? Thanks Dirk
I took a look at your problem, and it seems like there's no simple way to do this. You need to derive from the DocManager class being used -- it's probably CFileDialogDocManager but look in your InitInstanstance for the exact name. Then you can override the OpenDocumentFile and pretty much copy the whole thing from the base class, except the part that checks the file's name. Well, you actually do want to check the file name so you can close the existing document. It's not bad, just not very pretty. BTW, don't forget to change InitInstance. Regards, Alvaro
There are no stupid questions, but there are a lot of inquisitive idiots. -- despair.com
-
I took a look at your problem, and it seems like there's no simple way to do this. You need to derive from the DocManager class being used -- it's probably CFileDialogDocManager but look in your InitInstanstance for the exact name. Then you can override the OpenDocumentFile and pretty much copy the whole thing from the base class, except the part that checks the file's name. Well, you actually do want to check the file name so you can close the existing document. It's not bad, just not very pretty. BTW, don't forget to change InitInstance. Regards, Alvaro
There are no stupid questions, but there are a lot of inquisitive idiots. -- despair.com
Thanks Alvaro, I hoped there was a simpler solution. Perhaps only setting a flag or something ;-) I realized it now, not overriding the OpenDocumentFile member function, but the MatchDocType function. This results in a much cleaner solution, since I don't have to copy code from the CSingleDocTemplate implementation :-) class CSingleReloadDocTemplate : public CSingleDocTemplate { DECLARE_DYNAMIC(CSingleReloadDocTemplate) public: CSingleReloadDocTemplate(UINT nIDResource, CRuntimeClass* pDocClass, CRuntimeClass* pFrameClass, CRuntimeClass* pViewClass) : CSingleDocTemplate (nIDResource, pDocClass, pFrameClass, pViewClass) { } virtual Confidence MatchDocType(LPCTSTR lpszPathName, CDocument*& rpDocMatch) { Confidence confidence = CSingleDocTemplate::MatchDocType (lpszPathName, rpDocMatch); if (yesAlreadyOpen == confidence && rpDocMatch->IsModified()) { int answer = AfxMessageBox ("Do you want to reload the document", MB_YESNO); if (answer == IDYES) { // set the document modification state to "not modified" so the document template // will not complain during OpenDocumentFile () rpDocMatch->SetModifiedFlag(FALSE); // tell the DocumentManager, that no document with this title is already opened rpDocMatch = NULL; confidence = yesAttemptNative; } } return confidence; } }; IMPLEMENT_DYNAMIC (CSingleReloadDocTemplate, CSingleDocTemplate); For a MultiDocTemplate one should perhaps close the found document in some way (perhaps via CDocument::OnCloseDocument) since the CMultiDocTemplate::OpenDocumentFile() function will not reuse an existing open document like the CSingleDocTemplate. I'm not sure wether there is a combined method for this task that can be used for a SingleDocTemplate and a MultiDocTemplate (perhaps one can use CDocument::OnCloseDocument() already) Regards, Dirk