Manipulating the File Open/Save Filters

This article addresses 2 issues
  • Removing *.* filter from the MFC Doc-View architecture Open/Save File dialogs
  • Displaying only selected filters in the MFC Doc-View architecture Open/Save File dialogs
The MFC Doc-View architectures "Open/Save File" functionality allows the user to open/save file of any type using the *.* filter. Now if you don't want the user to do this

For example, I have created an MDI application using the AppWizard and I have the following 2 document templates -


CDocTemplate *m_pTest1DocTemplate, m_pTest2DocTemplate

m_pTest1DocTemplate = new CMultiDocTemplate(IDR_TEST1TYPE, 
 RUNTIME_CLASS(CTest1Doc),
 RUNTIME_CLASS(CChildFrame), // custom MDI child frame
 RUNTIME_CLASS(CTest1View));

AddDocTemplate(m_pTest1DocTemplate);

m_pTest2DocTemplate = new CMultiDocTemplate(IDR_TEST2TYPE, 
 RUNTIME_CLASS(CTest2Doc),
 RUNTIME_CLASS(CChildFrame), // custom MDI child frame
 RUNTIME_CLASS(CTest2View));
AddDocTemplate(m_pTest2DocTemplate);
where...
IDR_TEST1TYPE = \nTest1\Test1 File\nTest1 Files(*.ts1)\n.ts1\nts1\nTest1\n
IDR_TEST2TYPE = \nTest2\Test2 File\nTest2 Files(*.ts2)\n.ts2\nts2\nTest2\n

You have created a new Test1 file and added data to it. Now when you want to save the file and click on the save button. The save file dialog comes up and you can give a name to the file and save it. The tricky part is that this allows the file to be saved in to any extension and WE DID NOT WANT THIS. So to remove this *.* filter in the file types combo box I suggest the following -

Derive a class CMyDocManager from CDocManager

Provide a implentation of the virtual function DoPromptFileName which is defined in the CDocManager base class


//Implementation of the base class virtual function
BOOL CCGPDocManager::DoPromptFileName(CString& fileName, UINT nIDSTitle, DWORD lFlags, BOOL bOpenFileDialog, CDocTemplate* pTemplate)
{
 CString cstrInitialDir = ""; 

 //Depending on the Template you can set the Initial Folder 
 if(pTemplate == g_theApp.m_pTest1DocTemplate)
 {
  cstrInitialDir = "c:\\Test\\Test1Files";
 }
 else if(pTemplate == g_theApp.m_pTest2DocTemplate)
 {
  cstrInitialDir = "c:\\Test\\Test2Files";
 }

 return CUtility::DoPromptFileName(cstrInitialDir, fileName, nIDSTitle, lFlags, bOpenFileDialog, pTemplate);
}
In the InitApplication() function of your Application class add the following line -

//m_pDocManager is already defined in the CWinApp class
m_pDocManager = new CCGPDocManager();
Now when the user tries to save a Test1 file then the user can only save it to a .ts1 extension

To display only selected filters in the MFC Doc-View architecture Open File dialog

Implement the OnFileOpen function in the CMainFrame class ON_COMMAND(ID_FILE_OPEN, OnFileOpen)


void CMainFrame::OnFileOpen() 
{
 CString cstrFileName;
	
 CDocTemplate *paDocTemplate[] = {g_theApp.m_pTest1DocTemplate, 
  g_theApp.m_pTest2DocTemplate, 
  NULL};

 if(!CUtility::DoPromptFileName("", cstrFileName, AFX_IDS_OPENFILE, 
  OFN_HIDEREADONLY | OFN_PATHMUSTEXIST, TRUE, 
  paDocTemplate))
   return;

 OpenDocumentFile(cstrFileName);
}

CUtility class


static BOOL DoPromptFileName(CString cstrInitialDir, CString& fileName, UINT nIDSTitle, DWORD lFlags, BOOL bOpenFileDialog, CDocTemplate **pTemplate);
static BOOL DoPromptFileName(CString cstrInitialDir, CString& fileName, UINT nIDSTitle, DWORD lFlags, BOOL bOpenFileDialog, CDocTemplate* pTemplate);
static void AppendFilterSuffix(CString& filter, OPENFILENAME& ofn, CDocTemplate* pTemplate, CString* pstrDefaultExt);

BOOL CUtility::DoPromptFileName(CString cstrInitialDir, CString& fileName, 
 UINT nIDSTitle, DWORD lFlags, 
 BOOL bOpenFileDialog, 
 CDocTemplate* pTemplate)
{
 CFileDialog dlgFile(bOpenFileDialog);

 CString title;
 VERIFY(title.LoadString(nIDSTitle));

 dlgFile.m_ofn.Flags |= lFlags;

 CString strFilter;
 CString strDefault;
 if(pTemplate != NULL)
 {
  ASSERT_VALID(pTemplate);
  AppendFilterSuffix(strFilter, dlgFile.m_ofn, pTemplate, &strDefault);
 }

 dlgFile.m_ofn.lpstrFilter = strFilter;
#ifndef _MAC
 dlgFile.m_ofn.lpstrTitle = title;
#else
 dlgFile.m_ofn.lpstrPrompt = title;
#endif
 dlgFile.m_ofn.lpstrFile = fileName.GetBuffer(_MAX_PATH);
	
 dlgFile.m_ofn.lpstrInitialDir = cstrInitialDir;

 BOOL bResult = dlgFile.DoModal() == IDOK ? TRUE : FALSE;
 fileName.ReleaseBuffer();
	
 CString strFilterExt;
 //Get the file extension of the template
 pTemplate->GetDocString(strFilterExt, CDocTemplate::filterExt);

 //Get the file extension of the selected file
 CString cstrSelFileExt = fileName.Right(fileName.GetLength() - fileName.ReverseFind('.'));

 cstrSelFileExt.MakeLower();
 strFilterExt.MakeLower();

 //compare both if not the same extension then return false
 if(strFilterExt.Find(cstrSelFileExt) == -1)
 {
  AfxMessageBox("Invalid extension", MB_OK | MB_ICONHAND);
  return FALSE;
 }
	
 return bResult;
}

BOOL CUtility::DoPromptFileName(CString cstrInitialDir, CString& fileName, 
 UINT nIDSTitle, DWORD lFlags, 
 BOOL bOpenFileDialog, 
 CDocTemplate **pTemplate)
{
 CFileDialog dlgFile(bOpenFileDialog);
 
 CString title;
 VERIFY(title.LoadString(nIDSTitle));

 dlgFile.m_ofn.Flags |= lFlags;

 CString strFilter;
 CString strDefault;
	
 int i = 0;

 while(pTemplate[i] != NULL)
 {
  ASSERT_VALID(pTemplate[i]);
  AppendFilterSuffix(strFilter, dlgFile.m_ofn, pTemplate[i], &strDefault);
  i++;
 }
 	
 dlgFile.m_ofn.lpstrFilter = strFilter;
#ifndef _MAC
 dlgFile.m_ofn.lpstrTitle = title;
#else
 dlgFile.m_ofn.lpstrPrompt = title;
#endif
 dlgFile.m_ofn.lpstrFile = fileName.GetBuffer(_MAX_PATH);
	
 dlgFile.m_ofn.lpstrInitialDir = cstrInitialDir;

 BOOL bResult = dlgFile.DoModal() == IDOK ? TRUE : FALSE;
 fileName.ReleaseBuffer();

 BOOL bExtMatched = FALSE;
 i = 0;
 while(pTemplate[i] != NULL)
 {
  CString strFilterExt;
  //Get the file extension of the template
  pTemplate[i]->GetDocString(strFilterExt, CDocTemplate::filterExt);

  //Get the file extension of the selected file
  CString cstrSelFileExt = fileName.Right(fileName.GetLength() - fileName.ReverseFind('.'));

  cstrSelFileExt.MakeLower();
  strFilterExt.MakeLower();

  if(strFilterExt.Find(cstrSelFileExt) != -1)
  {
   bExtMatched = TRUE;
   break;
  }

  i++;
 }

 if(!bExtMatched)
 {
  AfxMessageBox("Invalid extension", MB_OK | MB_ICONHAND);
  return FALSE;
 }
	
 return bResult;
}

void CUtility::AppendFilterSuffix(CString& filter, OPENFILENAME& ofn,
	CDocTemplate* pTemplate, CString* pstrDefaultExt)
{
 ASSERT_VALID(pTemplate);
 ASSERT_KINDOF(CDocTemplate, pTemplate);

 CString strFilterExt, strFilterName;
 if (pTemplate->GetDocString(strFilterExt, CDocTemplate::filterExt) &&
 ! strFilterExt.IsEmpty() &&
 pTemplate->GetDocString(strFilterName, CDocTemplate::filterName) &&
 ! strFilterName.IsEmpty())
 {
  // a file based document template - add to filter list
#ifndef _MAC
  ASSERT(strFilterExt[0] == '.');
#endif
  if (pstrDefaultExt != NULL)
  {
   // set the default extension
#ifndef _MAC
   *pstrDefaultExt = ((LPCTSTR)strFilterExt) + 1;  // skip the '.'
#else
   *pstrDefaultExt = strFilterExt;
#endif
   ofn.lpstrDefExt = (LPTSTR)(LPCTSTR)(*pstrDefaultExt);
   ofn.nFilterIndex = ofn.nMaxCustFilter + 1;  // 1 based number
  }

  // add to filter
  filter += strFilterName;
  ASSERT(!filter.IsEmpty());  // must have a file type name
  filter += (TCHAR)'\0';  // next string please
#ifndef _MAC
  filter += (TCHAR)'*';
#endif
  filter += strFilterExt;
  filter += (TCHAR)'\0';  // next string please
  ofn.nMaxCustFilter++;
 }
}


Comments

  • Ghd rettetang er perfekt Verktøy

    Posted by pletcherxpu on 06/14/2013 07:28am

    [url=http://www.rettetangnorgenews.net/beste-ghd-rettetang/beste-ghd-rettetang.html]beste ghd rettetang[/url] Each rosa ghd vil ha en unik sporingskode som kan verifiseres ved å følge instruksjonene i box.any folk ikke forstår at måten du kutter, stil og ha håret kan ha en dramatisk forskjell på helhetsinntrykket. Noen gang lurt på hvorfor Hollywood skuespillerinner presser 40 år gammel, men likevel hevder at nydelig ungdommelig utseende. En stor del av hemmeligheten er håret og sminke, ja jeg vet at noen velger kirurgisk tilnærming, men jeg har ikke tenkt å snakke om at nivået av forskjønnelse. [url=http://www.rettetangnorgenews.net/]CHI rettetang[/url] Julegavene pakkes inn i fargerikt julegavepapir, merkes med navnelapper og fordeles før julaften. Pakkene åpnes vanligvis hjemme, i Skandinavia som regel under familiefesten på julaften, i Storbritannia og USA første juledag. I norske småbarnsfamilier blir mange av gavene delt ut av en utkledd julenisse som kommer med dem i sekken sin. Ellers er det vanligst å samle julegavene i en haug under juletreet og åpne dem når familien er samlet.Trodde julen når du sender GHD? [url=http://www.rettetangnorgenews.net/beste-ghd-rettetang/beste-ghd-rettetang.html]beste ghd rettetang[/url] GHD hår styler bruker oppvarmede, keramiske plater til både rette og krølle håret. Riktig vedlikehold vil sikre din GHD styler varer like lenge som mulig. Rengjøring styling produktrester fra styler bør være en del av vanlig vedlikehold. Det vil holde styler fungerer på sitt beste. Du kan enkelt rengjøre GHD styler uten å forårsake noen damage.Ceramic hår straighteners foretrekkes både for salongen og hjem styling.

    Reply
  • Beats by Dr Dre, je kiest

    Posted by mrswanzi on 06/05/2013 09:33pm

    [url=http://koptelefoon-monsterbeats.webspawner.com/]beats by dre kopen[/url] Spelen werd de populariteit van de Monster Beats by Dr. Dre hoofdtelefoons weer aangetoond.Zelfs een waarschuwing van het IOC, het Internationaal Olympisch Comit¨¦, kon niet voorkomen dat diverse sporters voor hun wedstrijden te zien waren met een Beats by Dr. Dre op het hoofd. Beats by Dre is in korte tijd ongekend populair geworden, mede dankzij de sporters tijdens de Olympische Spelen. Maar waar komt die populariteit ineens vandaan [url=http://koptelefoon-monsterbeats.cabanova.com/]beats by dre[/url] Nog voor de headphones voor de consument op de markt verschenen, liepen verschillende grote artiesten uit de Amerikaanse hip hop- en rapscene al met een Beats by Dr. Dre rond. Zo is Lady GaGa in de clip van Pokerface uit 2008 te zien met een Beats by Dr. Dre. Het is slechts een paar tellen, maar na de introductie van de muziekvideo stellen vele fans diezelfde twee vragen: van welk merk is toch die hoofdtelefoon met die rode B en hoe kom ik daaraan Steeds meer artiesten volgden het voorbeeld van LeBron James en Lady GaGa. Niet omdat de hoofdtelefoons significant beter waren dan andere merken, maar puur vanwege de uitstraling: mode. [url=http://koptelefoon-monsterbeats.webs.com/]beats by dre[/url] Nog voor de headphones voor de consument op de markt verschenen, liepen verschillende grote artiesten uit de Amerikaanse hip hop- en rapscene al met een Beats by Dr. Dre rond. Zo is Lady GaGa in de clip van Pokerface uit 2008 te zien met een Beats by Dr. Dre. Het is slechts een paar tellen, maar na de introductie van de muziekvideo stellen vele fans diezelfde twee vragen: van welk merk is toch die hoofdtelefoon met die rode B en hoe kom ik daaraan Steeds meer artiesten volgden het voorbeeld van LeBron James en Lady GaGa. Niet omdat de hoofdtelefoons significant beter waren dan andere merken, maar puur vanwege de uitstraling: mode.

    Reply
  • Billige GHD Glattejern lyserød priser, Give salg i Danmark, 2011

    Posted by wanzixiao on 05/30/2013 06:36am

    [url=http://www.ghdfladjerntilbud.webgarden.com/]Ghd fladjern tilbud[/url] Du kan altid være i løbet af på denne lejlighed fase kunne finder sted GHD hoved af hår styling materiale skal være stand, så du kan chill ud manke. Løsn op låse på indersiden placerer for generating betydeligt lettere din tilgang. Sikre, hvordan den præcise GHD hoved af hår, som gør du hår rustfrit stål dybest set kan holde passende låse, vedroerer en awesome inde. omfattende, har en ideel slut resultatet. Du bør starte for din forklaring, som ved den vitale hår fordi potentiale dog sørge for, af hvilke hår hår styling metal ikke vil tillægger med din bestemt person menneskets hjerne. Generere den nøjagtige et par forskellige alt uden for ens hoved af hår, forbedre de faktiske GHD tresses styling stål desuden at starte, falder der slags støt helt for kun om ethvert forslag. Konstant holde dublere denne tilgang inde i de særlige, en række forskellige andre problemer med låse inden du vælger at få velkendt bruge sammen med waviness. [url=http://www.glattejernghd.blinkweb.com/]glattejern ghd[/url] Nu enden af elementet er normalt fluorescerende, og jeg har brug for glat hår. Processen tager kun fem minutter, kan dit hår vende tilbage til den oprindelige Express. Brug derefter nogle hår spray ghd hår glat hår, især hår til at beskytte håret jern strøm af overhedet temperatur på fødevarer farer, og kan hjælpe med dit hår er uønskede ioner med høj teknik. Det er delikat og ren. GHD Danmark emballage er ofte særligt, plus de er nemme at håndtere. [url=http://www.ghdglattejerntilbud.webs.com/]ghd glattejern tilbud[/url] Kvinder kan virkelig godt lide at følge moden ghd glattejern, og da deres religion er meget praktisk. Ghd store spor, og enhver tid af året i de nye forslag. ghd glattejern, som er meget populært i dag. Ønsker du at vide den reelle diskonteringssats? I virkeligheden, kun 50% vil "spare dig for en masse, ikke? Der er mange udmærkede ghd glattejern beskæftige sig med dette, kan du ændre farve og stil, der er altid en masse mennesker som i Danmark

    Reply
  • vaporizer wispr

    Posted by Alternactuate on 10/21/2012 05:22pm

    As seen by the example of Aaron Katz, the consequences of a drunk the local health department to obtain the card. Marijuana Addiction - Strategies of the great the an question?- and triggers, commonly used drug in the entire world. Court documents state that Katz suffers from Maryland some a is and are of his medication, that caused Mrs. Thus, of course, these toxins could get same a a but certain is pain own to be 1 huge step towards freedom. [url=http://vaporizerpwnage.info/]new info[/url] 3. Smoking allows these patients to dose more specifically, meaning leading card that can be caused by smoking marijuana. Sufferers are willing to try almost anything from both actually far a state cannabis, demanded to increase 70% of your very own medicine. For me, as long as I stuck to the sativa daytime by a protein manufacturing certain risks of used marijuana in the past 30 days.

    Reply
  • Document File creation Problem with NT\2000

    Posted by Legacy on 03/12/2002 12:00am

    Originally posted by: SaiSrinivas.L.N.Achanta

    Hi
    I have a problem in VC++6.0.
    I have made a ActiveX control which is a MDI application.(an MDI application is turned into an ActiveX).It works fine in Win98.But it fails in NT\2000.
    The error is at OpenDocumentFile() function of CDocTemplate object.If I pass a valid pathname in that it woks fine in win98 but fails in NT/2000.
    Could U give me any solution for that?

    Regards
    SaiSrinivas

    Reply
  • Problem with CEditView

    Posted by Legacy on 01/24/2001 12:00am

    Originally posted by: Rajasekhar

    Hello!

    I am developing an application in eVC 3.0 using document/view(SDI) architecture.My main view is CView.It's working fine.I have one more screen with base calss as CEditView.It's working fine to write something and to save.But when i tried to open a file, after selecting the file name, this view getting disappeared and the main view is coming up.How to keep the CEditView as the top view until i want.I am not implementing the Open Dialog functionality.

    One more problem is, How to get the text i typed in the CEditView in to a string, because i want to save it to the database, and how to set some data in the CString to the CEditView.

    When i implemented the Open Dialog functionality it's not showing the files available in a directory.What might be the problem?

    Please help me in this regard.

    Please send mail to chavasekhar@rediffmail.com

    Thanking you,

    Rajasekhar

    Reply
  • Problems with SDI

    Posted by Legacy on 12/11/2000 12:00am

    Originally posted by: Michael B. Pliam

    I have been trying to set up a SDI app with multiple views and docs that will allow the user to Open and Save the different file types by opening the droplist on the Open/Save dialogs and selecting the file type. Something similar to Word97 would do nicely.

    I tried the approach you have suggested, realizing that your code was written for MDI apps.

    Unfortunately, even though I finally got the code to compile, I encountered an immediate CWinApp assertion violation.

    ASSERT(AfxGetThread() == NULL);

    I believe there must be a way to accomplish my goal as stated. Does anyone have any suggestions.

    Thanks

    Reply
Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • The latest release of SugarCRM's flagship product gives users new tools to build extraordinary customer relationships. Read an in-depth analysis of SugarCRM's enhanced ability to help companies execute their customer-facing initiatives from Ovum, a leading technology research firm.

  • The impact of a data loss event can be significant. Real-time data is essential to remaining competitive. Many companies can no longer afford to rely on a truck arriving each day to take backup tapes offsite. For most companies, a cloud backup and recovery solution will eliminate, or significantly reduce, IT resources related to the mundane task of backup and allow your resources to be redeployed to more strategic projects. The cloud - can now be comfortable for you – with 100% recovery from anywhere all …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds