Solved

Keeping original doc name even if saving as another name.

Posted on 2000-02-16
8
169 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
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 
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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Complete beginner needs help making a cron job 9 116
Detect CR LF to each line 12 159
SQUD PROXY SERVER, UNIX, SLL/HTTPS 5 94
unix example issues 18 89
Introduction: Database storage, where is the exe actually on the disc? Playing a game selected randomly (how to generate random numbers).  Error trapping with try..catch to help the code run even if something goes wrong. Continuing from the seve…
Introduction: Dialogs (1) modal - maintaining the database. Continuing from the ninth article about sudoku.   You might have heard of modal and modeless dialogs.  Here with this Sudoku application will we use one of each type: a modal dialog …
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.
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

776 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