home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c480 / 19.ddi / MFC / SRC / DLGPRNT.CP_ / DLGPRNT.CP
Encoding:
Text File  |  1993-02-08  |  6.1 KB  |  215 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. /////////////////////////////////////////////////////////////////////////////
  26. // Print/Print Setup dialog
  27.  
  28. IMPLEMENT_DYNAMIC(CPrintDialog, CDialog)
  29.  
  30. BEGIN_MESSAGE_MAP(CPrintDialog, CDialog)
  31.     //{{AFX_MSG_MAP(CPrintDialog)
  32.     ON_COMMAND(psh1, OnPrintSetup) // print setup button when print is displayed
  33.     //}}AFX_MSG_MAP
  34. END_MESSAGE_MAP()
  35.  
  36. CPrintDialog::CPrintDialog(BOOL bPrintSetupOnly,
  37.     DWORD dwFlags /* = PD_ALLPAGES | PD_USEDEVMODECOPIES | PD_NOPAGENUMS
  38.         | PD_HIDEPRINTTOFILE | PD_NOSELECTION */,
  39.     CWnd* pParentWnd /* = NULL */) 
  40.         : m_pd(m_pdActual), CDialog((UINT)0, pParentWnd)
  41. {
  42.     memset(&m_pdActual, 0, sizeof(m_pdActual));
  43.  
  44.     m_pd.lStructSize = sizeof(m_pdActual);
  45.     m_pd.Flags = (dwFlags | PD_ENABLEPRINTHOOK | PD_ENABLESETUPHOOK);
  46.     if (_AfxHelpEnabled())
  47.         m_pd.Flags |= PD_SHOWHELP;
  48.     m_pd.lpfnPrintHook = (COMMDLGPROC)_AfxCommDlgProc;
  49.     m_pd.lpfnSetupHook = (COMMDLGPROC)_AfxCommDlgProc;
  50.         
  51.     if (bPrintSetupOnly)
  52.     {
  53.         m_nIDHelp = AFX_IDD_PRINTSETUP;
  54.         m_pd.Flags |= PD_PRINTSETUP;
  55.     }
  56.     else
  57.     {
  58.         m_nIDHelp = AFX_IDD_PRINT;
  59.         m_pd.Flags |= PD_RETURNDC;
  60.     }
  61.  
  62.     m_pd.Flags &= ~PD_RETURNIC; // do not support information context
  63. }
  64.  
  65. // Helper ctor for AttachOnSetup
  66. #ifdef AFX_CLASS_MODEL
  67. CPrintDialog::CPrintDialog(PRINTDLG FAR& pdInit)
  68. #else
  69. CPrintDialog::CPrintDialog(PRINTDLG& pdInit)
  70. #endif
  71.         : m_pd(pdInit), CDialog((UINT)0, NULL)
  72. {   
  73. }
  74.  
  75. // Function to keep m_pd in sync after user invokes Setup from
  76. // the print dialog (via the Setup button)
  77. // If you decide to handle any messages/notifications and wish to
  78. // handle them differently between Print/PrintSetup then override
  79. // this function and create an object of a derived class
  80. CPrintDialog* CPrintDialog::AttachOnSetup()
  81. {
  82.     ASSERT_VALID(this);
  83.  
  84.     CPrintDialog* pDlgSetup;
  85.  
  86.     pDlgSetup = new CPrintDialog(m_pd);
  87.     pDlgSetup->m_hWnd = NULL;
  88.     pDlgSetup->m_pParentWnd = m_pParentWnd;
  89.     pDlgSetup->m_nIDHelp = AFX_IDD_PRINTSETUP;
  90.     return pDlgSetup;
  91. }
  92.  
  93. void CPrintDialog::OnPrintSetup()
  94. {
  95.     ASSERT_VALID(this);
  96.  
  97.     CPrintDialog* pDlgSetup = AttachOnSetup();
  98.     ASSERT(pDlgSetup != NULL);
  99.  
  100.     _AfxHookWindowCreate(pDlgSetup);
  101.     Default();
  102.     _AfxUnhookWindowCreate();
  103.  
  104.     delete pDlgSetup;
  105. }
  106.  
  107. int CPrintDialog::DoModal()
  108. {
  109.     ASSERT_VALID(this);
  110.     ASSERT(m_pd.Flags & PD_ENABLEPRINTHOOK);
  111.     ASSERT(m_pd.Flags & PD_ENABLESETUPHOOK);
  112.     ASSERT(m_pd.lpfnPrintHook != NULL); // can still be a user hook
  113.     ASSERT(m_pd.lpfnSetupHook != NULL); // can still be a user hook
  114.  
  115.     m_pd.hwndOwner = _AfxGetSafeOwner(m_pParentWnd);
  116.     _AfxHookWindowCreate(this);
  117.     BOOL bResult = ::PrintDlg(&m_pd);
  118.     _AfxUnhookWindowCreate();   // just in case
  119.     Detach();                   // just in case
  120.  
  121.     return bResult ? IDOK : IDCANCEL;
  122. }
  123.  
  124. // Create an HDC without calling DoModal.
  125. HDC CPrintDialog::CreatePrinterDC()
  126. {
  127.     ASSERT_VALID(this);
  128.     ASSERT(m_pd.hDevNames != NULL);
  129.  
  130.     LPDEVNAMES lpDevNames = (LPDEVNAMES)::GlobalLock(m_pd.hDevNames);
  131.     LPDEVMODE  lpDevMode = (m_pd.hDevMode != NULL) ? 
  132.                         (LPDEVMODE)::GlobalLock(m_pd.hDevMode) : NULL;
  133.  
  134.     if (lpDevNames == NULL)
  135.         return NULL;
  136.  
  137.     m_pd.hDC = ::CreateDC((LPCSTR)lpDevNames + lpDevNames->wDriverOffset,
  138.                       (LPCSTR)lpDevNames + lpDevNames->wDeviceOffset,
  139.                       (LPCSTR)lpDevNames + lpDevNames->wOutputOffset,
  140.                       lpDevMode);
  141.  
  142.     // Unnecessary global unlocks (not needed in protect mode)
  143.     // ::GlobalUnlock(m_pd.hDevNames);
  144.     // ::GlobalUnlock(m_pd.hDevMode);
  145.     return m_pd.hDC;
  146. }
  147.  
  148.  
  149. // Return an HDC.  We don't return a CDC* so the user can decide
  150. // where to attach this HDC: either to a newly allocated object
  151. // (use operator delete to clean up) or to an embedded/frame
  152. // object (destructor will clean up when leaving scope)
  153. HDC CPrintDialog::GetPrinterDC() const
  154. {
  155.     ASSERT_VALID(this);
  156.     ASSERT(m_pd.Flags & PD_RETURNDC);
  157.     
  158.     return m_pd.hDC;
  159. }
  160.  
  161. int CPrintDialog::GetCopies() const
  162. {   
  163.     ASSERT_VALID(this);
  164.  
  165.     if (m_pd.Flags & PD_USEDEVMODECOPIES)
  166.         return GetDevMode()->dmCopies;
  167.     else
  168.         return m_pd.nCopies; 
  169. }
  170.  
  171. void CPrintDialog::OnOK()
  172. {
  173.     // Common dialogs do not require ::EndDialog
  174.     ASSERT_VALID(this);
  175.     Default();
  176. }
  177.  
  178. void CPrintDialog::OnCancel()
  179. {
  180.     // Common dialogs do not require ::EndDialog
  181.     ASSERT_VALID(this);
  182.     Default();
  183. }
  184.  
  185. ////////////////////////////////////////////////////////////////////////////
  186. // CPrintDialog diagnostics
  187.  
  188. #ifdef _DEBUG
  189. void CPrintDialog::Dump(CDumpContext& dc) const
  190. {
  191.     ASSERT_VALID(this);
  192.     CDialog::Dump(dc);
  193.  
  194.     AFX_DUMP1(dc, "\nm_pd.hwndOwner = ", (UINT)m_pd.hwndOwner);
  195.     if (m_pd.hDC != NULL)
  196.         AFX_DUMP1(dc, "\nm_pd.hDC = ", CDC::FromHandle(m_pd.hDC));
  197.     AFX_DUMP1(dc, "\nm_pd.Flags = ", (LPVOID)m_pd.Flags);
  198.     AFX_DUMP1(dc, "\nm_pd.nFromPage = ", m_pd.nFromPage);
  199.     AFX_DUMP1(dc, "\nm_pd.nToPage = ", m_pd.nToPage);
  200.     AFX_DUMP1(dc, "\nm_pd.nMinPage = ", m_pd.nMinPage);
  201.     AFX_DUMP1(dc, "\nm_pd.nMaxPage = ", m_pd.nMaxPage);
  202.     AFX_DUMP1(dc, "\nm_pd.nCopies = ", m_pd.nCopies);
  203.     if (m_pd.lpfnSetupHook == (COMMDLGPROC)_AfxCommDlgProc)
  204.         AFX_DUMP0(dc, "\nsetup hook function set to standard MFC hook function");
  205.     else
  206.         AFX_DUMP0(dc, "\nsetup hook function set to non-standard hook function");
  207.     if (m_pd.lpfnPrintHook == (COMMDLGPROC)_AfxCommDlgProc)
  208.         AFX_DUMP0(dc, "\nprint hook function set to standard MFC hook function");
  209.     else
  210.         AFX_DUMP0(dc, "\nprint hook function set to non-standard hook function");
  211. }
  212. #endif //_DEBUG
  213.  
  214. ////////////////////////////////////////////////////////////////////////////
  215.