home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c480 / 19.ddi / MFC / SRC / DLGFILE.CP_ / DLGFILE.CP
Encoding:
Text File  |  1993-02-08  |  8.1 KB  |  278 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. #include <dlgs.h>       // for standard control IDs for commdlg
  14.  
  15. #ifdef AFX_AUX_SEG
  16. #pragma code_seg(AFX_AUX_SEG)
  17. #endif
  18.  
  19. #ifdef _DEBUG
  20. #undef THIS_FILE
  21. static char BASED_CODE THIS_FILE[] = __FILE__;
  22. #define new DEBUG_NEW
  23. #endif
  24.  
  25. static char BASED_CODE szLBSELCH[] = LBSELCHSTRING;
  26. static char BASED_CODE szSHAREVI[] = SHAREVISTRING;
  27. static char BASED_CODE szFILEOK[] = FILEOKSTRING;
  28. static char BASED_CODE szCOLOROK[] = COLOROKSTRING;
  29. static char BASED_CODE szSETRGB[] = SETRGBSTRING;
  30.  
  31.  
  32. static const UINT NEAR nMsgLBSELCHANGE = ::RegisterWindowMessage(szLBSELCH);
  33. static const UINT NEAR nMsgSHAREVI = ::RegisterWindowMessage(szSHAREVI);
  34. static const UINT NEAR nMsgFILEOK = ::RegisterWindowMessage(szFILEOK);
  35. static const UINT NEAR nMsgCOLOROK = ::RegisterWindowMessage(szCOLOROK);
  36. const UINT NEAR _afxNMsgSETRGB = ::RegisterWindowMessage(szSETRGB);
  37.  
  38.  
  39. UINT CALLBACK AFX_EXPORT
  40. _AfxCommDlgProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
  41. {
  42.     if (message == WM_SETFONT || message == WM_INITDIALOG)
  43.         return (UINT)_AfxDlgProc(hWnd, message, wParam, lParam);
  44.  
  45.     if (message == WM_COMMAND && LOWORD(wParam) == pshHelp)
  46.     {
  47.         // just translate the message into the AFX standard help command.
  48.         SendMessage(hWnd, WM_COMMAND, ID_HELP, 0);
  49.         return TRUE;
  50.     }
  51.  
  52.     if (message < 0xC000)
  53.         // not a ::RegisterWindowMessage message
  54.         return 0;
  55.  
  56.     // RegisterWindowMessage - does not copy to lastState buffer, so
  57.     // CWnd::GetCurrentMessage and CWnd::Default will NOT work
  58.     // while in these handlers
  59.  
  60.     // Get our Window
  61.     // assume it is already wired up to a permanent one
  62.     CDialog* pDlg = (CDialog*) CWnd::FromHandlePermanent(hWnd);
  63.     ASSERT(pDlg != NULL);
  64.     ASSERT(pDlg->m_hWnd == hWnd);
  65.     ASSERT(pDlg->IsKindOf(RUNTIME_CLASS(CDialog)));
  66.  
  67.     // Dispatch special commdlg messages through our virtual callbacks
  68.     if (message == nMsgSHAREVI)
  69.     {
  70.         ASSERT(pDlg->IsKindOf(RUNTIME_CLASS(CFileDialog)));
  71.         return ((CFileDialog*)pDlg)->OnShareViolation((LPCSTR)lParam);
  72.     }
  73.     else if (message == nMsgFILEOK)
  74.     {
  75.         ASSERT(pDlg->IsKindOf(RUNTIME_CLASS(CFileDialog)));
  76.         return ((CFileDialog*)pDlg)->OnFileNameOK();
  77.     }
  78.     else if (message == nMsgLBSELCHANGE)
  79.     {
  80.         ASSERT(pDlg->IsKindOf(RUNTIME_CLASS(CFileDialog)));
  81.         ((CFileDialog*)pDlg)->OnLBSelChangedNotify(wParam, LOWORD(lParam), 
  82.                 HIWORD(lParam));
  83.         return 0;
  84.     }
  85.     else if (message == nMsgCOLOROK)
  86.     {
  87.         ASSERT(pDlg->IsKindOf(RUNTIME_CLASS(CColorDialog)));
  88.         return ((CColorDialog*)pDlg)->OnColorOK();
  89.     }
  90.     else if (message == _afxNMsgSETRGB)
  91.     {
  92.         // nothing to do here, since this is a SendMessage
  93.         return 0;
  94.     }
  95.  
  96.     TRACE0("_AfxCommDlgProc: received unknown user message, returning 0\n");
  97.     return 0;
  98. }
  99.  
  100. ////////////////////////////////////////////////////////////////////////////
  101. // FileOpen/FileSaveAs common dialog helper
  102.  
  103. IMPLEMENT_DYNAMIC(CFileDialog, CDialog)
  104.  
  105. CFileDialog::CFileDialog(BOOL bOpenFileDialog,
  106.         LPCSTR lpszDefExt /* = NULL */,
  107.         LPCSTR lpszFileName /* = NULL */,
  108.         DWORD dwFlags /* = OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT */,
  109.         LPCSTR lpszFilter /* = NULL */, 
  110.         CWnd* pParentWnd /* = NULL */) : CDialog((UINT)0, pParentWnd)
  111. {
  112.     memset(&m_ofn, 0, sizeof(m_ofn)); // initialize structure to 0/NULL
  113.     m_szFileName[0] = '\0';
  114.     m_szFileTitle[0] = '\0';
  115.  
  116.     m_bOpenFileDialog = bOpenFileDialog;
  117.     m_nIDHelp = bOpenFileDialog ? AFX_IDD_FILEOPEN : AFX_IDD_FILESAVE;
  118.     
  119.     m_ofn.lStructSize = sizeof(m_ofn);
  120.     m_ofn.lpstrFile = (LPSTR)&m_szFileName;
  121.     m_ofn.nMaxFile = sizeof(m_szFileName);
  122.     m_ofn.lpstrDefExt = lpszDefExt;
  123.     m_ofn.lpstrFileTitle = (LPSTR)m_szFileTitle;
  124.     m_ofn.nMaxFileTitle = sizeof(m_szFileTitle);
  125.     m_ofn.Flags |= dwFlags | OFN_ENABLEHOOK;
  126.     if (_AfxHelpEnabled())
  127.         m_ofn.Flags |= OFN_SHOWHELP;
  128.     m_ofn.lpfnHook = (COMMDLGPROC)_AfxCommDlgProc;
  129.  
  130.     // setup initial file name
  131.     if (lpszFileName != NULL)
  132.         _AfxStrCpy(m_szFileName, lpszFileName, sizeof(m_szFileName));
  133.  
  134.     // Translate filter into commdlg format (lots of \0)
  135.     if (lpszFilter != NULL)
  136.     {
  137.         m_strFilter = lpszFilter;
  138.         LPSTR pch = m_strFilter.GetBuffer(0); // modify the buffer in place
  139.         // MFC delimits with '|' not '\0'
  140.         while ((pch = _AfxStrChr(pch, '|')) != NULL)
  141.             *pch++ = '\0';
  142.         m_ofn.lpstrFilter = m_strFilter;
  143.         // do not call ReleaseBuffer() since the string contains '\0' characters
  144.     }
  145. }
  146.  
  147. int CFileDialog::DoModal()
  148. {
  149.     ASSERT_VALID(this);
  150.     ASSERT(m_ofn.Flags & OFN_ENABLEHOOK);
  151.     ASSERT(m_ofn.lpfnHook != NULL); // can still be a user hook
  152.  
  153.     BOOL bResult;
  154.  
  155.     m_ofn.hwndOwner = _AfxGetSafeOwner(m_pParentWnd);
  156.     _AfxHookWindowCreate(this);
  157.     if (m_bOpenFileDialog)
  158.         bResult = ::GetOpenFileName(&m_ofn);
  159.     else
  160.         bResult = ::GetSaveFileName(&m_ofn);
  161.     _AfxUnhookWindowCreate();   // just in case
  162.     Detach();                   // just in case
  163.  
  164.     return bResult ? IDOK : IDCANCEL;
  165. }
  166.  
  167. CString CFileDialog::GetFileName() const
  168.     ASSERT_VALID(this);
  169.     char szFile[_MAX_PATH];
  170.  
  171.     if (m_ofn.nFileExtension == 0 || 
  172.             m_ofn.lpstrFile[m_ofn.nFileExtension] == '\0')
  173.         return m_ofn.lpstrFile + m_ofn.nFileOffset;
  174.     else
  175.     {
  176.         ASSERT(m_ofn.nFileExtension - m_ofn.nFileOffset < sizeof(szFile));
  177.         _AfxStrCpy(szFile, m_ofn.lpstrFile + m_ofn.nFileOffset,
  178.                 m_ofn.nFileExtension - m_ofn.nFileOffset);
  179.         return szFile;
  180.     }
  181. }
  182.  
  183. UINT CFileDialog::OnShareViolation(LPCSTR)
  184. {
  185.     ASSERT_VALID(this);
  186.  
  187.     // Do not call Default() if you override
  188.     return OFN_SHAREWARN; // default
  189. }
  190.  
  191. BOOL CFileDialog::OnFileNameOK()
  192. {
  193.     ASSERT_VALID(this);
  194.  
  195.     // Do not call Default() if you override
  196.     return FALSE;
  197. }
  198.  
  199. void CFileDialog::OnLBSelChangedNotify(UINT, UINT, UINT)
  200. {
  201.     ASSERT_VALID(this);
  202.  
  203.     // Do not call Default() if you override
  204.     // no default processing needed
  205. }
  206.  
  207. void CFileDialog::OnOK()
  208. {
  209.     // Common dialogs do not require ::EndDialog
  210.     ASSERT_VALID(this);
  211.     Default();
  212. }
  213.  
  214. void CFileDialog::OnCancel()
  215. {
  216.     // Common dialogs do not require ::EndDialog
  217.     ASSERT_VALID(this);
  218.     Default();
  219. }
  220.  
  221. ////////////////////////////////////////////////////////////////////////////
  222. // CFileDialog diagnostics
  223.  
  224. #ifdef _DEBUG
  225. void CFileDialog::Dump(CDumpContext& dc) const
  226. {
  227.     ASSERT_VALID(this);
  228.  
  229.     CDialog::Dump(dc);
  230.  
  231.     if (m_bOpenFileDialog)
  232.         AFX_DUMP0(dc, "\nFile open dialog");
  233.     else
  234.         AFX_DUMP0(dc, "\nFile save dialog");
  235.     AFX_DUMP1(dc, "\nm_ofn.hwndOwner = ", (UINT)m_ofn.hwndOwner);
  236.     AFX_DUMP1(dc, "\nm_ofn.nFilterIndex = ", m_ofn.nFilterIndex);
  237.     AFX_DUMP1(dc, "\nm_ofn.lpstrFile = ", m_ofn.lpstrFile);
  238.     AFX_DUMP1(dc, "\nm_ofn.nMaxFile = ", m_ofn.nMaxFile);
  239.     AFX_DUMP1(dc, "\nm_ofn.lpstrFileTitle = ", m_ofn.lpstrFileTitle);
  240.     AFX_DUMP1(dc, "\nm_ofn.nMaxFileTitle = ", m_ofn.nMaxFileTitle);
  241.     AFX_DUMP1(dc, "\nm_ofn.lpstrTitle = ", m_ofn.lpstrTitle);
  242.     AFX_DUMP1(dc, "\nm_ofn.Flags = ", (LPVOID)m_ofn.Flags);
  243.     AFX_DUMP1(dc, "\nm_ofn.lpstrDefExt = ", m_ofn.lpstrDefExt);
  244.     AFX_DUMP1(dc, "\nm_ofn.nFileOffset = ", m_ofn.nFileOffset);
  245.     AFX_DUMP1(dc, "\nm_ofn.nFileExtension = ", m_ofn.nFileExtension);
  246.  
  247.     AFX_DUMP0(dc, "\nm_ofn.lpstrFilter = ");
  248.     LPCSTR lpstrItem = m_ofn.lpstrFilter;
  249.     char* pszBreak = "|";
  250.  
  251.     while (lpstrItem != NULL && *lpstrItem != '\0')
  252.     {
  253.         dc << lpstrItem << pszBreak;
  254.         lpstrItem += lstrlen(lpstrItem) + 1;
  255.     }
  256.     if (lpstrItem != NULL)
  257.         dc << pszBreak;
  258.  
  259.     AFX_DUMP0(dc, "\nm_ofn.lpstrCustomFilter = ");
  260.     lpstrItem = m_ofn.lpstrCustomFilter;
  261.     while (lpstrItem != NULL && *lpstrItem != '\0')
  262.     {
  263.         dc << lpstrItem << pszBreak;
  264.         lpstrItem += lstrlen(lpstrItem) + 1;
  265.     }
  266.     if (lpstrItem != NULL)
  267.         dc << pszBreak;
  268.     
  269.     if (m_ofn.lpfnHook == (COMMDLGPROC)_AfxCommDlgProc)
  270.         AFX_DUMP0(dc, "\nhook function set to standard MFC hook function");
  271.     else
  272.         AFX_DUMP0(dc, "\nhook function set to non-standard hook function");
  273. }
  274. #endif //_DEBUG
  275.  
  276. ////////////////////////////////////////////////////////////////////////////
  277.