home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / vc98 / mfc / src / dlgprnt.cpp < prev    next >
C/C++ Source or Header  |  1998-06-16  |  11KB  |  396 lines

  1. // This is a part of the Microsoft Foundation Classes C++ library.
  2. // Copyright (C) 1992-1998 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 related
  7. // electronic documentation provided with the library.
  8. // See these sources for detailed information regarding the
  9. // Microsoft Foundation Classes product.
  10.  
  11. #include "stdafx.h"
  12. #include <dlgs.h>       // for standard control IDs for commdlg
  13.  
  14. #ifdef AFX_AUX_SEG
  15. #pragma code_seg(AFX_AUX_SEG)
  16. #endif
  17.  
  18. #ifdef _DEBUG
  19. #undef THIS_FILE
  20. static char THIS_FILE[] = __FILE__;
  21. #endif
  22.  
  23. #define new DEBUG_NEW
  24.  
  25. /////////////////////////////////////////////////////////////////////////////
  26. // Page Setup dialog
  27.  
  28. CPageSetupDialog::CPageSetupDialog(DWORD dwFlags, CWnd* pParentWnd) :
  29.     CCommonDialog(pParentWnd)
  30. {
  31.     memset(&m_psd, 0, sizeof(m_psd));
  32.  
  33.     m_psd.lStructSize = sizeof(m_psd);
  34.     m_psd.Flags = (dwFlags | PSD_ENABLEPAGESETUPHOOK | PSD_ENABLEPAGEPAINTHOOK);
  35.     if (!afxData.bWin4 && AfxHelpEnabled())
  36.         m_psd.Flags |= PSD_SHOWHELP;
  37.     m_psd.lpfnPageSetupHook = (COMMDLGPROC)_AfxCommDlgProc;
  38.     m_psd.lpfnPagePaintHook = (COMMDLGPROC)CPageSetupDialog::PaintHookProc;
  39. }
  40.  
  41. int CPageSetupDialog::DoModal()
  42. {
  43.     ASSERT_VALID(this);
  44.     ASSERT(m_psd.Flags & PSD_ENABLEPAGESETUPHOOK);
  45.     ASSERT(m_psd.Flags & PSD_ENABLEPAGEPAINTHOOK);
  46.     ASSERT(m_psd.lpfnPageSetupHook != NULL); // can still be a user hook
  47.     ASSERT(m_psd.lpfnPagePaintHook != NULL); // can still be a user hook
  48.  
  49.     m_psd.hwndOwner = PreModal();
  50.     int nResult = ::PageSetupDlg(&m_psd);
  51.     PostModal();
  52.     return nResult ? nResult : IDCANCEL;
  53. }
  54.  
  55. ////////////////////////////////////////////////////////////////////////////
  56. // CPageSetupDialog attributes
  57.  
  58. LPDEVMODE CPageSetupDialog::GetDevMode() const
  59. {
  60.     if (m_psd.hDevMode == NULL)
  61.         return NULL;
  62.  
  63.     return (LPDEVMODE)::GlobalLock(m_psd.hDevMode);
  64. }
  65.  
  66. CString CPageSetupDialog::GetDriverName() const
  67. {
  68.     if (m_psd.hDevNames == NULL)
  69.         return afxEmptyString;
  70.  
  71.     LPDEVNAMES lpDev = (LPDEVNAMES)GlobalLock(m_psd.hDevNames);
  72.     return (LPCTSTR)lpDev + lpDev->wDriverOffset;
  73. }
  74.  
  75. CString CPageSetupDialog::GetDeviceName() const
  76. {
  77.     if (m_psd.hDevNames == NULL)
  78.         return afxEmptyString;
  79.  
  80.     LPDEVNAMES lpDev = (LPDEVNAMES)GlobalLock(m_psd.hDevNames);
  81.     return (LPCTSTR)lpDev + lpDev->wDeviceOffset;
  82. }
  83.  
  84. CString CPageSetupDialog::GetPortName() const
  85. {
  86.     if (m_psd.hDevNames == NULL)
  87.         return afxEmptyString;
  88.  
  89.     LPDEVNAMES lpDev = (LPDEVNAMES)GlobalLock(m_psd.hDevNames);
  90.     return (LPCTSTR)lpDev + lpDev->wOutputOffset;
  91. }
  92.  
  93. // Create an HDC from the devnames and devmode.
  94. HDC CPageSetupDialog::CreatePrinterDC()
  95. {
  96.     ASSERT_VALID(this);
  97.     return AfxCreateDC(m_psd.hDevNames, m_psd.hDevMode);
  98. }
  99.  
  100. void CPageSetupDialog::GetMargins(LPRECT lpRectMargins, LPRECT lpRectMinMargins) const
  101. {
  102.     if (lpRectMargins != NULL)
  103.         memcpy(lpRectMargins, &m_psd.rtMargin, sizeof(RECT));
  104.     if (lpRectMinMargins != NULL)
  105.         memcpy(lpRectMinMargins, &m_psd.rtMinMargin, sizeof(RECT));
  106. }
  107.  
  108. ////////////////////////////////////////////////////////////////////////////
  109. // CPageSetupDialog diagnostics
  110.  
  111. #ifdef _DEBUG
  112. void CPageSetupDialog::Dump(CDumpContext& dc) const
  113. {
  114.     CDialog::Dump(dc);
  115.  
  116.     dc << "m_psd.hwndOwner = " << (UINT)m_psd.hwndOwner;
  117.     dc << "\nm_psd.Flags = " << (LPVOID)m_psd.Flags;
  118.  
  119.     dc << "\nm_psd.ptPaperSize = " << m_psd.ptPaperSize;
  120.     dc << "\nm_psd.rtMinMargin = " << m_psd.rtMinMargin;
  121.     dc << "\nm_psd.rtMinMargin = " << m_psd.rtMinMargin;
  122.  
  123.     if (m_psd.lpfnPageSetupHook == (COMMDLGPROC)_AfxCommDlgProc)
  124.         dc << "\nsetup hook function set to standard MFC hook function";
  125.     else
  126.         dc << "\nsetup hook function set to non-standard hook function";
  127.  
  128.     if (m_psd.lpfnPagePaintHook == (COMMDLGPROC)_AfxCommDlgProc)
  129.         dc << "\nprint hook function set to standard MFC hook function";
  130.     else
  131.         dc << "\nprint hook function set to non-standard hook function";
  132.  
  133.     dc << "\n";
  134. }
  135. #endif //_DEBUG
  136.  
  137. ////////////////////////////////////////////////////////////////////////////
  138. // CPageSetupDialog hook
  139.  
  140. UINT CPageSetupDialog::PreDrawPage(WORD /*wPaperType*/, WORD /*wFlags*/,
  141.     LPPAGESETUPDLG)
  142. {
  143.     return 0;
  144.     //return 1 to prevent any more drawing
  145. }
  146.  
  147. UINT CPageSetupDialog::OnDrawPage(CDC*, UINT /*nMessage*/, LPRECT)
  148. {
  149.     return 0; // do the default
  150. }
  151.  
  152. UINT CALLBACK CPageSetupDialog::PaintHookProc(HWND hWnd, UINT message, WPARAM wParam,
  153.     LPARAM lParam)
  154. {
  155.     if (hWnd == NULL)
  156.         return 0;
  157.     // Get our Window
  158.     // assume it is already wired up to a permanent one
  159.     // the hWnd is the HWND of a control in the page setup proc
  160.     CPageSetupDialog* pDlg = DYNAMIC_DOWNCAST(CPageSetupDialog,
  161.         CWnd::FromHandlePermanent(::GetParent(hWnd)));
  162.     if (pDlg == NULL)
  163.         return 0;
  164.     switch (message)
  165.     {
  166.     case WM_PSD_PAGESETUPDLG:
  167.         return pDlg->PreDrawPage(LOWORD(wParam), HIWORD(wParam),
  168.             (LPPAGESETUPDLG) lParam);
  169.         break;
  170.     case WM_PSD_FULLPAGERECT:
  171.     case WM_PSD_MINMARGINRECT:
  172.     case WM_PSD_MARGINRECT:
  173.     case WM_PSD_GREEKTEXTRECT:
  174.     case WM_PSD_ENVSTAMPRECT:
  175.     case WM_PSD_YAFULLPAGERECT:
  176.         return pDlg->OnDrawPage(CDC::FromHandle((HDC)wParam), message, (LPRECT)lParam);
  177.         break;
  178.     }
  179.     return 0;
  180. }
  181.  
  182. /////////////////////////////////////////////////////////////////////////////
  183. // Print/Print Setup dialog
  184.  
  185. BEGIN_MESSAGE_MAP(CPrintDialog, CCommonDialog)
  186.     //{{AFX_MSG_MAP(CPrintDialog)
  187.     ON_COMMAND(psh1, OnPrintSetup) // print setup button when print is displayed
  188.     //}}AFX_MSG_MAP
  189. END_MESSAGE_MAP()
  190.  
  191. CPrintDialog::CPrintDialog(BOOL bPrintSetupOnly,
  192.     DWORD dwFlags, CWnd* pParentWnd)
  193.     : m_pd(m_pdActual), CCommonDialog(pParentWnd)
  194. {
  195.     memset(&m_pdActual, 0, sizeof(m_pdActual));
  196.  
  197.     m_pd.lStructSize = sizeof(m_pdActual);
  198.     m_pd.Flags = dwFlags;
  199.     if (!afxData.bWin4 && AfxHelpEnabled())
  200.     {
  201.         m_pd.Flags |= PD_SHOWHELP | PD_ENABLEPRINTHOOK | PD_ENABLESETUPHOOK;
  202.         m_pd.lpfnPrintHook = (COMMDLGPROC)_AfxCommDlgProc;
  203.         m_pd.lpfnSetupHook = (COMMDLGPROC)_AfxCommDlgProc;
  204.     }
  205.  
  206.     if (bPrintSetupOnly)
  207.     {
  208.         m_nIDHelp = AFX_IDD_PRINTSETUP;
  209.         m_pd.Flags |= PD_PRINTSETUP;
  210.     }
  211.     else
  212.     {
  213.         m_nIDHelp = AFX_IDD_PRINT;
  214.         m_pd.Flags |= PD_RETURNDC;
  215.     }
  216.  
  217.     m_pd.Flags &= ~PD_RETURNIC; // do not support information context
  218. }
  219.  
  220. // Helper ctor for AttachOnSetup
  221. CPrintDialog::CPrintDialog(PRINTDLG& pdInit)
  222.     : m_pd(pdInit), CCommonDialog(NULL)
  223. {
  224. }
  225.  
  226. // Function to keep m_pd in sync after user invokes Setup from
  227. // the print dialog (via the Setup button)
  228. // If you decide to handle any messages/notifications and wish to
  229. // handle them differently between Print/PrintSetup then override
  230. // this function and create an object of a derived class
  231. CPrintDialog* CPrintDialog::AttachOnSetup()
  232. {
  233.     ASSERT_VALID(this);
  234.  
  235.     CPrintDialog* pDlgSetup;
  236.  
  237.     pDlgSetup = new CPrintDialog(m_pd);
  238.     pDlgSetup->m_hWnd = NULL;
  239.     pDlgSetup->m_pParentWnd = m_pParentWnd;
  240.     pDlgSetup->m_nIDHelp = AFX_IDD_PRINTSETUP;
  241.     return pDlgSetup;
  242. }
  243.  
  244. void CPrintDialog::OnPrintSetup()
  245. {
  246.     ASSERT_VALID(this);
  247.  
  248.     CPrintDialog* pDlgSetup = AttachOnSetup();
  249.     ASSERT(pDlgSetup != NULL);
  250.  
  251.     AfxHookWindowCreate(pDlgSetup);
  252.     Default();
  253.     AfxUnhookWindowCreate();
  254.  
  255.     delete pDlgSetup;
  256. }
  257.  
  258. int CPrintDialog::DoModal()
  259. {
  260.     ASSERT_VALID(this);
  261.  
  262.     m_pd.hwndOwner = PreModal();
  263.     int nResult = ::PrintDlg(&m_pd);
  264.     PostModal();
  265.     return nResult ? nResult : IDCANCEL;
  266. }
  267.  
  268. // Create an HDC without calling DoModal.
  269. HDC CPrintDialog::CreatePrinterDC()
  270. {
  271.     ASSERT_VALID(this);
  272.     m_pd.hDC = AfxCreateDC(m_pd.hDevNames, m_pd.hDevMode);
  273.     return m_pd.hDC;
  274. }
  275.  
  276. int CPrintDialog::GetCopies() const
  277. {
  278.     ASSERT_VALID(this);
  279.  
  280.     if (m_pd.Flags & PD_USEDEVMODECOPIES)
  281.         return GetDevMode()->dmCopies;
  282.     else
  283.         return m_pd.nCopies;
  284. }
  285.  
  286. LPDEVMODE CPrintDialog::GetDevMode() const
  287. {
  288.     if (m_pd.hDevMode == NULL)
  289.         return NULL;
  290.  
  291.     return (LPDEVMODE)::GlobalLock(m_pd.hDevMode);
  292. }
  293.  
  294. CString CPrintDialog::GetDriverName() const
  295. {
  296.     if (m_pd.hDevNames == NULL)
  297.         return afxEmptyString;
  298.  
  299.     LPDEVNAMES lpDev = (LPDEVNAMES)GlobalLock(m_pd.hDevNames);
  300.     return (LPCTSTR)lpDev + lpDev->wDriverOffset;
  301. }
  302.  
  303. CString CPrintDialog::GetDeviceName() const
  304. {
  305.     if (m_pd.hDevNames == NULL)
  306.         return afxEmptyString;
  307.  
  308.     LPDEVNAMES lpDev = (LPDEVNAMES)GlobalLock(m_pd.hDevNames);
  309.     return (LPCTSTR)lpDev + lpDev->wDeviceOffset;
  310. }
  311.  
  312. CString CPrintDialog::GetPortName() const
  313. {
  314.     if (m_pd.hDevNames == NULL)
  315.         return afxEmptyString;
  316.  
  317.     LPDEVNAMES lpDev = (LPDEVNAMES)GlobalLock(m_pd.hDevNames);
  318.     return (LPCTSTR)lpDev + lpDev->wOutputOffset;
  319. }
  320.  
  321. // this function must not be in afxdlgs.inl because of DLL delay loading
  322. BOOL CPrintDialog::GetDefaults()
  323. {
  324.     m_pd.Flags |= PD_RETURNDEFAULT;
  325.     return ::PrintDlg(&m_pd);
  326. }
  327.  
  328. ////////////////////////////////////////////////////////////////////////////
  329. // CPrintDialog diagnostics
  330.  
  331. #ifdef _DEBUG
  332. void CPrintDialog::Dump(CDumpContext& dc) const
  333. {
  334.     CDialog::Dump(dc);
  335.  
  336.     dc << "m_pd.hwndOwner = " << (UINT)m_pd.hwndOwner;
  337.  
  338.     if (m_pd.hDC != NULL)
  339.         dc << "\nm_pd.hDC = " << CDC::FromHandle(m_pd.hDC);
  340.  
  341.     dc << "\nm_pd.Flags = " << (LPVOID)m_pd.Flags;
  342.     dc << "\nm_pd.nFromPage = " << m_pd.nFromPage;
  343.     dc << "\nm_pd.nToPage = " << m_pd.nToPage;
  344.     dc << "\nm_pd.nMinPage = " << m_pd.nMinPage;
  345.     dc << "\nm_pd.nMaxPage = " << m_pd.nMaxPage;
  346.     dc << "\nm_pd.nCopies = " << m_pd.nCopies;
  347.  
  348.     if (m_pd.lpfnSetupHook == (COMMDLGPROC)_AfxCommDlgProc)
  349.         dc << "\nsetup hook function set to standard MFC hook function";
  350.     else
  351.         dc << "\nsetup hook function set to non-standard hook function";
  352.  
  353.     if (m_pd.lpfnPrintHook == (COMMDLGPROC)_AfxCommDlgProc)
  354.         dc << "\nprint hook function set to standard MFC hook function";
  355.     else
  356.         dc << "\nprint hook function set to non-standard hook function";
  357.  
  358.     dc << "\n";
  359. }
  360. #endif //_DEBUG
  361.  
  362. ////////////////////////////////////////////////////////////////////////////
  363. // AfxCreateDC
  364.  
  365. HDC AFXAPI AfxCreateDC(HGLOBAL hDevNames, HGLOBAL hDevMode)
  366. {
  367.     if (hDevNames == NULL)
  368.         return NULL;
  369.  
  370.     LPDEVNAMES lpDevNames = (LPDEVNAMES)::GlobalLock(hDevNames);
  371.     LPDEVMODE  lpDevMode = (hDevMode != NULL) ?
  372.                         (LPDEVMODE)::GlobalLock(hDevMode) : NULL;
  373.  
  374.     if (lpDevNames == NULL)
  375.         return NULL;
  376.  
  377.     HDC hDC = ::CreateDC((LPCTSTR)lpDevNames + lpDevNames->wDriverOffset,
  378.                       (LPCTSTR)lpDevNames + lpDevNames->wDeviceOffset,
  379.                       (LPCTSTR)lpDevNames + lpDevNames->wOutputOffset,
  380.                       lpDevMode);
  381.  
  382.     ::GlobalUnlock(hDevNames);
  383.     if (hDevMode != NULL)
  384.         ::GlobalUnlock(hDevMode);
  385.     return hDC;
  386. }
  387.  
  388. #ifdef AFX_INIT_SEG
  389. #pragma code_seg(AFX_INIT_SEG)
  390. #endif
  391.  
  392. IMPLEMENT_DYNAMIC(CPrintDialog, CDialog)
  393. IMPLEMENT_DYNAMIC(CPageSetupDialog, CDialog)
  394.  
  395. ////////////////////////////////////////////////////////////////////////////
  396.