Solved

Keeping original doc name even if saving as another name.

Posted on 2000-02-16
8
166 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
 
LVL 30

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
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

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 30

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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Suggested Solutions

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
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.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

706 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now