/**************************************************************************** ** COPYRIGHT (C): 1997 Cay S. Horstmann. All Rights Reserved. ** PROJECT: Practical OO Development with C++ and Java ** FILE: MainFrm.cpp ** PURPOSE: MFC class with Window|New that shows doc templates (chapter 19) ** VERSION 1.0 ** PROGRAMMERS: Cay Horstmann (CSH) ** RELEASE DATE: 3-15-97 (CSH) ** UPDATE HISTORY: ****************************************************************************/ #if _MSC_VER != 1100 #define MUST_USE_ALLOCATOR #endif #include #ifndef PRACTICALOO_NO_STD_NAMESPACE using namespace std; #endif class CDocTemplate; #ifndef MUST_USE_ALLOCATOR typedef list DocTemplateList; #else typedef list > DocTemplateList; #endif #define VC_EXTRALEAN #include #include "MainFrm.h" BEGIN_MESSAGE_MAP(CMainFrame, CMDIFrameWnd) ON_COMMAND(ID_WINDOW_NEW, OnWindowNew) END_MESSAGE_MAP() IMPLEMENT_DYNAMIC(CMainFrame, CMDIFrameWnd) class CNewViewDlg : public CDialog { public: CNewViewDlg(DocTemplateList &pList) : CDialog(CNewViewDlg::IDD), _pList(pList), _pSelectedTemplate(NULL) {} CDocTemplate* selectedTemplate() const { return _pSelectedTemplate; } enum { IDD = AFX_IDD_NEWTYPEDLG }; protected: BOOL OnInitDialog(); void OnOK(); DECLARE_MESSAGE_MAP() private: DocTemplateList& _pList; CDocTemplate* _pSelectedTemplate; }; BEGIN_MESSAGE_MAP(CNewViewDlg, CDialog) ON_LBN_DBLCLK(AFX_IDC_LISTBOX, OnOK) END_MESSAGE_MAP() BOOL CNewViewDlg::OnInitDialog() { CListBox* pListBox = (CListBox*)GetDlgItem(AFX_IDC_LISTBOX); // fill with document templates in list pListBox->ResetContent(); for (DocTemplateList::iterator it = _pList.begin(); it != _pList.end(); it++) { CDocTemplate* pTemplate = *it; CString strTypeName; if (pTemplate->GetDocString(strTypeName, CDocTemplate::docName) && !strTypeName.IsEmpty()) { // add it to the listbox int nIndex = pListBox->AddString(strTypeName); if (nIndex == -1) { EndDialog(-1); return FALSE; } pListBox->SetItemDataPtr(nIndex, pTemplate); } } int nTemplates = pListBox->GetCount(); if (nTemplates == 0) EndDialog(-1); // abort else if (nTemplates == 1) { // get the first/only item _pSelectedTemplate = (CDocTemplate*)pListBox->GetItemDataPtr(0); EndDialog(IDOK); // done } else // set selection to the first one (NOT SORTED) pListBox->SetCurSel(0); return CDialog::OnInitDialog(); } void CNewViewDlg::OnOK() { CListBox* pListBox = (CListBox*)GetDlgItem(AFX_IDC_LISTBOX); // if listbox has selection, set the selected template int nIndex; if ((nIndex = pListBox->GetCurSel()) == -1) // no selection _pSelectedTemplate = NULL; else _pSelectedTemplate = (CDocTemplate*)pListBox->GetItemDataPtr(nIndex); CDialog::OnOK(); } void CMainFrame::OnWindowNew() { CMDIChildWnd* pActiveChild = MDIGetActive(); CDocument* pDocument; if (pActiveChild == NULL || (pDocument = pActiveChild->GetActiveDocument()) == NULL) return; // command failed // otherwise we have a new frame ! CDocTemplate* pTemplate = pDocument->GetDocTemplate(); // now find all other document templates with this document type CString templateDocName; pTemplate->GetDocString(templateDocName, CDocTemplate::regFileTypeName); list > tlist; CWinApp* app = AfxGetApp(); POSITION pos = app->GetFirstDocTemplatePosition(); while (pos != NULL) { CDocTemplate* t = app->GetNextDocTemplate(pos); CString tDocName; t->GetDocString(tDocName, CDocTemplate::regFileTypeName); if (templateDocName == tDocName) tlist.push_back(t); } CDocTemplate* newWinTemplate = NULL; if (tlist.size() > 1) { CNewViewDlg dlg(tlist); int id = dlg.DoModal(); if (id == IDOK) newWinTemplate = dlg.selectedTemplate(); else return; // canceled by user } else newWinTemplate = pTemplate; CFrameWnd* pFrame = newWinTemplate->CreateNewFrame(pDocument, pActiveChild); if (pFrame == NULL) return; // command failed newWinTemplate->InitialUpdateFrame(pFrame, pDocument); }