Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 174
  • Last Modified:

Keeping original doc name even if saving as another name.

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
jjjkkklll
Asked:
jjjkkklll
  • 3
  • 3
  • 2
1 Solution
 
ScottyDawgCommented:
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
 
jjjkkklllAuthor Commented:
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
 
ScottyDawgCommented:
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
ZoppoCommented:
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
 
jjjkkklllAuthor Commented:
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
 
ScottyDawgCommented:
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
 
ZoppoCommented:
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
 
jjjkkklllAuthor Commented:
Thanks, Zoppo. As I said, I wish I could split the points...
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 3
  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now