1
votes

I'm trying to build an MFC SDI (Single-Document Interface) application with these requirements:

  1. Show only one document at a time.

  2. Support different views based on the file type chosen from the file open dialog. For example, if a *.txt file is chosen, I would show MyCViewText, while I would show MyCViewImage if a *.jpg file is chosen.

  3. Show a custom list of file extensions file open dialog. For example, use a custom dialog and filter like: CFileDialog *dlg = new CFileDialog(TRUE, NULL, NULL, NULL, L"Text Files (*.txt)|*.txt|" L"JPEG Files (*.jpg)|*.jpg||");

But, I have two problems.

First, I don't know where to insert the file extensions filter for the File -> Open dialog. If I override CWinApp::OnFileOpen() with a custom CFileDialog, then I lose all the behind-the-scenes behavior that the SDI provides, like invoking the CDocument::OnDocumentOpen() call and initializing the document template's CView. I can't call CWinApp::OnFileOpen() in the override because a second file open dialog appears after the first and without the file extensions filter.

Second, I don't know where to insert the view toggle code after a file has been chosen from the file open dialog. The document template is set up like this:

pDocTemplate = new CSingleDocTemplate(
    IDR_MAINFRAME,
    RUNTIME_CLASS(CMyAppDoc),
    RUNTIME_CLASS(CMainFrame),
    RUNTIME_CLASS(CMyCView));

It seems like I would want to override CWinApp::OnFileOpen() and switch the document template view RUNTIME_CLASS (CMyCView), but I'm not sure this is even possible.

Is the MFC SDI just not suited for my three requirements? Do I have to use an MFC MDI application instead? If I have to somehow use MDI, then how would I restrict to only having a single document loaded and shown at any given time?

Any help is appreciated.

1

1 Answers

3
votes

In your InitInstance function, create a new SingleDocTemplate and add it to the collection of templates:

CSingleDocTemplate* pDocTemplate2;
  pDocTemplate2 = new CSingleDocTemplate(IDR_MAINFRAME2,
     RUNTIME_CLASS(CMyDoc2),
     RUNTIME_CLASS(CMainFrame),       // main SDI frame window
     RUNTIME_CLASS(CMyView2));
  if (!pDocTemplate2)
     return FALSE;

  AddDocTemplate(pDocTemplate2);

The file type and extension goes in a string table entry.

IDR_MAINFRAME2 "MyCalc Windows Application\nSheet\nWorksheet\n Worksheets (*.myc)\n.myc\nMyCalcSheet\n MyCalc Worksheet"

MFC will recognize the template, and offer the it as an option on File-New or File-Open. Depending on your requirements you may need to create new classes for CMyDoc2 and CMyView2, or possibly use the original classes if they can be made to work with either file type.