home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c480 / 19.ddi / MFC / SRC / APPDLG.CP_ / APPDLG.CP
Encoding:
Text File  |  1993-02-08  |  6.9 KB  |  256 lines

  1. // This is a part of the Microsoft Foundation Classes C++ library.
  2. // Copyright (C) 1992 Microsoft Corporation
  3. // All rights reserved.
  4. //
  5. // This source code is only intended as a supplement to the
  6. // Microsoft Foundation Classes Reference and Microsoft
  7. // QuickHelp and/or WinHelp documentation provided with the library.
  8. // See these sources for detailed information regarding the
  9. // Microsoft Foundation Classes product.
  10.  
  11.  
  12. #include "stdafx.h"
  13.  
  14. #ifdef AFX_CORE1_SEG
  15. #pragma code_seg(AFX_CORE1_SEG)
  16. #endif
  17.  
  18. #ifdef _DEBUG
  19. #undef THIS_FILE
  20. static char BASED_CODE THIS_FILE[] = __FILE__;
  21. #endif
  22.  
  23. /////////////////////////////////////////////////////////////////////////////
  24. // WinApp features for new and open
  25.  
  26. // Get the best document template for the named file
  27.  
  28. class CNewTypeDlg : public CDialog
  29. {
  30. protected:
  31.     CPtrList*   m_pList;        // actually a list of doc templates
  32. public:
  33.     CDocTemplate*   m_pSelectedTemplate;
  34.  
  35. public:
  36.     //{{AFX_DATA(CNewTypeDlg)
  37.     enum { IDD = AFX_IDD_NEWTYPEDLG };
  38.     //}}AFX_DATA
  39.     CNewTypeDlg(CPtrList* pList)
  40.         : CDialog(CNewTypeDlg::IDD)
  41.         {
  42.             m_pList = pList;
  43.             m_pSelectedTemplate = NULL;
  44.         }
  45.  
  46. protected:
  47.     BOOL OnInitDialog();
  48.     void OnOK();
  49.     //{{AFX_MSG(CNewTypeDlg)
  50.     //}}AFX_MSG
  51.     DECLARE_MESSAGE_MAP()
  52. };
  53.  
  54. BEGIN_MESSAGE_MAP(CNewTypeDlg, CDialog)
  55.     //{{AFX_MSG_MAP(CNewTypeDlg)
  56.     ON_LBN_DBLCLK(AFX_IDC_LISTBOX, OnOK)
  57.     //}}AFX_MSG_MAP
  58. END_MESSAGE_MAP()
  59.  
  60. BOOL CNewTypeDlg::OnInitDialog()
  61. {
  62.     CListBox* pListBox = (CListBox*)GetDlgItem(AFX_IDC_LISTBOX);
  63.     ASSERT(pListBox != NULL);
  64.  
  65.     // fill with document templates in list
  66.     pListBox->ResetContent();
  67.  
  68.     POSITION pos = m_pList->GetHeadPosition();
  69.     // add all the CDocTemplates in the list by name
  70.     while (pos != NULL)
  71.     {
  72.         CDocTemplate* pTemplate = (CDocTemplate*)m_pList->GetNext(pos);
  73.         ASSERT(pTemplate->IsKindOf(RUNTIME_CLASS(CDocTemplate)));
  74.  
  75.         CString strTypeName;
  76.         if (pTemplate->GetDocString(strTypeName, CDocTemplate::fileNewName) &&
  77.            !strTypeName.IsEmpty())
  78.         {
  79.             // add it to the listbox
  80.             int nIndex = pListBox->AddString(strTypeName);
  81.             if (nIndex == -1)
  82.             {
  83.                 EndDialog(IDABORT);
  84.                 return FALSE;
  85.             }
  86.             pListBox->SetItemDataPtr(nIndex, pTemplate);
  87.         }
  88.     }
  89.  
  90.     int nTemplates = pListBox->GetCount();
  91.     if (nTemplates == 0)
  92.     {
  93.         TRACE0("Error: no document templates to select from!\n");
  94.         EndDialog(IDABORT); // abort
  95.     }
  96.     else if (nTemplates == 1)
  97.     {
  98.         // get the first/only item
  99.         m_pSelectedTemplate = (CDocTemplate*)pListBox->GetItemDataPtr(0);
  100.         ASSERT_VALID(m_pSelectedTemplate);
  101.         ASSERT(m_pSelectedTemplate->IsKindOf(RUNTIME_CLASS(CDocTemplate)));
  102.         EndDialog(IDOK);    // done
  103.     }
  104.     else
  105.     {
  106.         // set selection to the first one (NOT SORTED)
  107.         pListBox->SetCurSel(0);
  108.     }
  109.  
  110.     return CDialog::OnInitDialog();
  111. }
  112.  
  113. void CNewTypeDlg::OnOK()
  114. {
  115.     CListBox* pListBox = (CListBox*)GetDlgItem(AFX_IDC_LISTBOX);
  116.     ASSERT(pListBox != NULL);
  117.     // if listbox has selection, set the selected template
  118.     int nIndex;
  119.     if ((nIndex = pListBox->GetCurSel()) == -1)
  120.     {
  121.         // no selection
  122.         m_pSelectedTemplate = NULL;
  123.     }
  124.     else
  125.     {
  126.         m_pSelectedTemplate = (CDocTemplate*)pListBox->GetItemDataPtr(nIndex);
  127.         ASSERT_VALID(m_pSelectedTemplate);
  128.         ASSERT(m_pSelectedTemplate->IsKindOf(RUNTIME_CLASS(CDocTemplate)));
  129.     }
  130.     CDialog::OnOK();
  131. }
  132.  
  133. void CWinApp::OnFileNew()
  134. {
  135.     if (m_templateList.IsEmpty())
  136.     {
  137.         TRACE0("Error : no document templates registered with CWinApp\n");
  138.         AfxMessageBox(AFX_IDP_FAILED_TO_CREATE_DOC);
  139.         return;
  140.     }
  141.  
  142.     CDocTemplate* pTemplate = (CDocTemplate*)m_templateList.GetHead();
  143.     if (m_templateList.GetCount() > 1)
  144.     {
  145.         // more than one document template to choose from
  146.         // bring up dialog prompting user
  147.         CNewTypeDlg dlg(&m_templateList);
  148.         if (dlg.DoModal() != IDOK)
  149.             return;     // none - cancel operation
  150.         pTemplate = dlg.m_pSelectedTemplate;
  151.     }
  152.  
  153.     ASSERT(pTemplate != NULL);
  154.     ASSERT(pTemplate->IsKindOf(RUNTIME_CLASS(CDocTemplate)));
  155.  
  156.     pTemplate->OpenDocumentFile(NULL);
  157.         // if returns NULL, the user has already been alerted
  158. }
  159.  
  160. /////////////////////////////////////////////////////////////////////////////
  161.  
  162. void CWinApp::OnFileOpen()
  163. {
  164.     // prompt the user (with all document templates)
  165.     CString newName;
  166.     if (!DoPromptFileName(newName, AFX_IDS_OPENFILE,
  167.       OFN_HIDEREADONLY | OFN_FILEMUSTEXIST, TRUE, NULL))
  168.         return; // open cancelled
  169.  
  170.     OpenDocumentFile(newName);
  171.         // if returns NULL, the user has already been alerted
  172. }
  173.  
  174. static void AppendFilterSuffix(CString& filter, OPENFILENAME& ofn,
  175.     CDocTemplate* pTemplate, CString* pstrDefaultExt)
  176. {
  177.     ASSERT_VALID(pTemplate);
  178.     ASSERT(pTemplate->IsKindOf(RUNTIME_CLASS(CDocTemplate)));
  179.  
  180.     CString strFilterExt, strFilterName;
  181.     if (pTemplate->GetDocString(strFilterExt, CDocTemplate::filterExt) &&
  182.      !strFilterExt.IsEmpty() &&
  183.      pTemplate->GetDocString(strFilterName, CDocTemplate::filterName) &&
  184.      !strFilterName.IsEmpty())
  185.     {
  186.         // a file based document template - add to filter list
  187.         ASSERT(strFilterExt[0] == '.');
  188.         if (pstrDefaultExt != NULL)
  189.         {
  190.             // set the default extension
  191.             *pstrDefaultExt = ((const char*)strFilterExt) + 1;  // skip the '.'
  192.             ofn.lpstrDefExt = (LPSTR)(const char*)(*pstrDefaultExt);
  193.             ofn.nFilterIndex = ofn.nMaxCustFilter + 1;  // 1 based number
  194.         }
  195.  
  196.         // add to filter
  197.         filter += strFilterName;
  198.         ASSERT(!filter.IsEmpty());  // must have a file type name
  199.         filter += (char)'\0';       // next string please
  200.         filter += "*" + strFilterExt;
  201.         filter += (char)'\0';       // next string please
  202.         ofn.nMaxCustFilter++;
  203.     }
  204. }
  205.  
  206. // prompt for file name - used for open and save as
  207. BOOL CWinApp::DoPromptFileName(CString& fileName, UINT nIDSTitle, DWORD lFlags,
  208.     BOOL bOpenFileDialog, CDocTemplate* pTemplate)
  209.         // if pTemplate==NULL => all document templates
  210. {
  211.     CFileDialog dlgFile(bOpenFileDialog);
  212.  
  213.     CString title;
  214.     VERIFY(title.LoadString(nIDSTitle));
  215.     
  216.     dlgFile.m_ofn.Flags |= lFlags;
  217.  
  218.     CString strFilter;
  219.     CString strDefault;
  220.     if (pTemplate != NULL)
  221.     {
  222.         ASSERT_VALID(pTemplate);
  223.         AppendFilterSuffix(strFilter, dlgFile.m_ofn, pTemplate, &strDefault);
  224.     }
  225.     else
  226.     {
  227.         // do for all doc template
  228.         POSITION pos = m_templateList.GetHeadPosition();
  229.         while (pos)
  230.         {
  231.             AppendFilterSuffix(strFilter, dlgFile.m_ofn,
  232.                 (CDocTemplate*)m_templateList.GetNext(pos), NULL);
  233.         }
  234.     }
  235.  
  236.     // append the "*.*" all files filter
  237.     CString allFilter;
  238.     VERIFY(allFilter.LoadString(AFX_IDS_ALLFILTER));
  239.     strFilter += allFilter;
  240.     strFilter += (char)'\0';        // next string please
  241.     strFilter += "*.*";
  242.     strFilter += (char)'\0';        // last string
  243.     dlgFile.m_ofn.nMaxCustFilter++;
  244.  
  245.     dlgFile.m_ofn.lpstrFilter = strFilter;
  246.     dlgFile.m_ofn.hwndOwner = AfxGetApp()->m_pMainWnd->GetSafeHwnd();
  247.     dlgFile.m_ofn.lpstrTitle = title;
  248.     dlgFile.m_ofn.lpstrFile = fileName.GetBuffer(_MAX_PATH);
  249.  
  250.     BOOL bRet = dlgFile.DoModal() == IDOK ? TRUE : FALSE;
  251.     fileName.ReleaseBuffer();
  252.     return bRet;
  253. }
  254.  
  255. /////////////////////////////////////////////////////////////////////////////
  256.