Solved

Keeping original doc name even if saving as another name.

Posted on 2000-02-16
8
170 Views
Last Modified: 2013-11-20
I have an MDI doc/view with normal serialization for saving (it saves as myfile.cpw).  I've added an option to the CFileDialog so the user can save the contents of the edit boxes as a text file if they want (the default is myfile.cpw).  Everything works great except... when it comes back to the main view the document name changes to myfile.txt and the next save saves it as myfile.txt.  I'd like everything to revert back to how it was as if no save function has taken place.  In other words, I want the user to be able to save it as a text file, but when they come back it basically hasn't affected anything.  It still says myfile.cpw and is still dirty (if changes were made since the last "myfile.cpw" save).  I'm assuming I have to collect the original doc name and put it back after the functions, but I'm not sure where that's kept or the best way to handle it.  Thanks.

Jim Weiss
0
Comment
Question by:jjjkkklll
  • 3
  • 3
  • 2
8 Comments
 
LVL 1

Accepted Solution

by:
ScottyDawg earned 150 total points
ID: 2528296
You can use the functions SetTitle() and SetPathName().

However, what you're doing is non-standard perhaps you should consider and export menu item...?
0
 

Author Comment

by:jjjkkklll
ID: 2528638
Okay, I can get the title and pathname before the function does its business and put them back when it's done.  Will this affect the dirty flag?  

Also, I'm seriously considering adding an "export" menu item. If I use the same structure, though, it basically would do the same thing, right?  Only difference would be that the user would get to the CFileDialog override through an "export" menu button rather than through the "save as" item.  Would this still affect the title and pathname?  In other words, is there a way, if I put this in a separate "export" menu button, to avoid changing the title and pathname in the first place?  Is there a way to export to a text file in a vacuum?  Thanks for any help.

Jim Weiss
0
 
LVL 1

Expert Comment

by:ScottyDawg
ID: 2530364
I don't think so, but then I don't think its to much bother anyhow.

i.e.
// All called from CDocument OnExportFile() or something...

CString strPathName = GetPathName();
CString strTitle = GetTitle();
bool bDirty = IsModified();

CFileDialog FileDlg(false, ".exp");
if (FileDlg.DoModal() != IDOK)
    return;

CString strExport = FileDlg.GetPathName();
SetPathName(strExport);

AfxGetApp()->OnFileSave();

SetPathName(strPathName);
SetTitle(strTitle);
SetModified(bDirty);


A little buggy no doubt, but maybe along the lines of what you need?
0
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 31

Expert Comment

by:Zoppo
ID: 2530557
Hi jjjkkklll,

you can try following:

override your document's DoSave() function (unfortunately not documented virtual function of CDocument) like this:

BOOL
CMyDoc::DoSave(LPCTSTR lpszPathName, BOOL bReplace /*= TRUE*/ )
{
 CString oldName = GetPathName();
 BOOL bRet = CDocument::DoSave( lpszPathName, bReplace );
 SetPathName( oldName );
 return bRet;
}

hope that helps,

ZOPPO
0
 

Author Comment

by:jjjkkklll
ID: 2532052
Both answers are excellent and I wish I could split the points, but I'll give it to ScottyDawg for his quick first response.  The basic theory on both  seems to be to capture the oldName and put it back when I'm done saving the text file.  I have a lot of tweaking to do, but I'm on my way.

I just have one question for Zoppo (or anyone who knows) before I close this question. Regarding his (or her) code, don't I have to SetTitle() to the old name, too?  Or is setting the path name enough to handle the title and path name of the document? Actually, the more I study it, the more complex it gets. Where do I put the CFileDialog to get the new file name for the text file I'm creating?  I.e. what has to happen before I call the DoSave override?  Thanks.

Jim Weiss
0
 
LVL 1

Expert Comment

by:ScottyDawg
ID: 2534136
The FileSave business is really "busy" in CDocument. It does so much stuff before it actually calls your routines.

The best thing to do, is to step through the MFC sources and see what happens. You'll come across functions like DoPromptFilename(...), DoSave and DoFileSave. One of these you might just be able to override to get just what you want.
0
 
LVL 31

Expert Comment

by:Zoppo
ID: 2534196
Hi jjjkkklll,

You can place the file dialog into the DoSave routine, i.e.:

BOOL
CMyDoc::DoSave(LPCTSTR lpszPathName, BOOL bReplace /*= TRUE*/ )
{
 CString oldName = GetPathName();
 // remove this 'if' if you want the same handling for 'Save' and 'SaveAs'
 // if lpszPathName == NULL 'SaveAs' was selected, else 'Save'
 if ( NULL == lpszPathName )
 {
  // Do your file dialog here and set lpszPathName to the resulting file path
  // or return FALSE; if file dialog was cancelled
 }
 BOOL bRet = CDocument::DoSave( lpszPathName, bReplace );
 SetPathName( oldName );
 return bRet;
}

The CDocument::DoSave does handle the default 'Save' and 'SaveAs' behavior. It displays the file dialog if needed and validates the filename. Then it calls CDocument::OnSaveDocument().

SetTitle() is not needed when SetPathName() is used, because it's called by SetPathName().

hope that helps,

ZOPPO
0
 

Author Comment

by:jjjkkklll
ID: 2535832
Thanks, Zoppo. As I said, I wish I could split the points...
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction: Dynamic window placements and drawing on a form, simple usage of windows registry as a storage place for information. Continuing from the first article about sudoku.  There we have designed the application and put a lot of user int…
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question