home *** CD-ROM | disk | FTP | other *** search
/ Supercompiler 1997 / SUPERCOMPILER97.iso / MS_VC.50 / VC / MFC / SRC / DLGPRNT.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1996-10-30  |  10.7 KB  |  402 lines

  1. // This is a part of the Microsoft Foundation Classes C++ library.
  2. // Copyright (C) 1992-1997 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. #ifndef _MAC
  26. /////////////////////////////////////////////////////////////////////////////
  27. // Page Setup dialog
  28.  
  29. CPageSetupDialog::CPageSetupDialog(DWORD dwFlags, CWnd* pParentWnd) :
  30.     CCommonDialog(pParentWnd)
  31. {
  32.     memset(&m_psd, 0, sizeof(m_psd));
  33.  
  34.     m_psd.lStructSize = sizeof(m_psd);
  35.     m_psd.Flags = (dwFlags | PSD_ENABLEPAGESETUPHOOK | PSD_ENABLEPAGEPAINTHOOK);
  36.     if (!afxData.bWin4 && AfxHelpEnabled())
  37.         m_psd.Flags |= PSD_SHOWHELP;
  38.     m_psd.lpfnPageSetupHook = (COMMDLGPROC)_AfxCommDlgProc;
  39.     m_psd.lpfnPagePaintHook = (COMMDLGPROC)CPageSetupDialog::PaintHookProc;
  40. }
  41.  
  42. int CPageSetupDialog::DoModal()
  43. {
  44.     ASSERT_VALID(this);
  45.     ASSERT(m_psd.Flags & PSD_ENABLEPAGESETUPHOOK);
  46.     ASSERT(m_psd.Flags & PSD_ENABLEPAGEPAINTHOOK);
  47.     ASSERT(m_psd.lpfnPageSetupHook != NULL); // can still be a user hook
  48.     ASSERT(m_psd.lpfnPagePaintHook != NULL); // can still be a user hook
  49.  
  50.     m_psd.hwndOwner = PreModal();
  51.     int nResult = ::PageSetupDlg(&m_psd);
  52.     PostModal();
  53.     return nResult ? nResult : IDCANCEL;
  54. }
  55.  
  56. ////////////////////////////////////////////////////////////////////////////
  57. // CPageSetupDialog attributes
  58.  
  59. LPDEVMODE CPageSetupDialog::GetDevMode() const
  60. {
  61.     if (m_psd.hDevMode == NULL)
  62.         return NULL;
  63.  
  64.     return (LPDEVMODE)::GlobalLock(m_psd.hDevMode);
  65. }
  66.  
  67. CString CPageSetupDialog::GetDriverName() const
  68. {
  69.     if (m_psd.hDevNames == NULL)
  70.         return afxEmptyString;
  71.  
  72.     LPDEVNAMES lpDev = (LPDEVNAMES)GlobalLock(m_psd.hDevNames);
  73.     return (LPCTSTR)lpDev + lpDev->wDriverOffset;
  74. }
  75.  
  76. CString CPageSetupDialog::GetDeviceName() const
  77. {
  78.     if (m_psd.hDevNames == NULL)
  79.         return afxEmptyString;
  80.  
  81.     LPDEVNAMES lpDev = (LPDEVNAMES)GlobalLock(m_psd.hDevNames);
  82.     return (LPCTSTR)lpDev + lpDev->wDeviceOffset;
  83. }
  84.  
  85. CString CPageSetupDialog::GetPortName() const
  86. {
  87.     if (m_psd.hDevNames == NULL)
  88.         return afxEmptyString;
  89.  
  90.     LPDEVNAMES lpDev = (LPDEVNAMES)GlobalLock(m_psd.hDevNames);
  91.     return (LPCTSTR)lpDev + lpDev->wOutputOffset;
  92. }
  93.  
  94. // Create an HDC from the devnames and devmode.
  95. HDC CPageSetupDialog::CreatePrinterDC()
  96. {
  97.     ASSERT_VALID(this);
  98.     return AfxCreateDC(m_psd.hDevNames, m_psd.hDevMode);
  99. }
  100.  
  101. void CPageSetupDialog::GetMargins(LPRECT lpRectMargins, LPRECT lpRectMinMargins) const
  102. {
  103.     if (lpRectMargins != NULL)
  104.         memcpy(lpRectMargins, &m_psd.rtMargin, sizeof(RECT));
  105.     if (lpRectMinMargins != NULL)
  106.         memcpy(lpRectMinMargins, &m_psd.rtMinMargin, sizeof(RECT));
  107. }
  108.  
  109. ////////////////////////////////////////////////////////////////////////////
  110. // CPageSetupDialog diagnostics
  111.  
  112. #ifdef _DEBUG
  113. void CPageSetupDialog::Dump(CDumpContext& dc) const
  114. {
  115.     CDialog::Dump(dc);
  116.  
  117.     dc << "m_psd.hwndOwner = " << (UINT)m_psd.hwndOwner;
  118.     dc << "\nm_psd.Flags = " << (LPVOID)m_psd.Flags;
  119.  
  120.     dc << "\nm_psd.ptPaperSize = " << m_psd.ptPaperSize;
  121.     dc << "\nm_psd.rtMinMargin = " << m_psd.rtMinMargin;
  122.     dc << "\nm_psd.rtMinMargin = " << m_psd.rtMinMargin;
  123.  
  124.     if (m_psd.lpfnPageSetupHook == (COMMDLGPROC)_AfxCommDlgProc)
  125.         dc << "\nsetup hook function set to standard MFC hook function";
  126.     else
  127.         dc << "\nsetup hook function set to non-standard hook function";
  128.  
  129.     if (m_psd.lpfnPagePaintHook == (COMMDLGPROC)_AfxCommDlgProc)
  130.         dc << "\nprint hook function set to standard MFC hook function";
  131.     else
  132.         dc << "\nprint hook function set to non-standard hook function";
  133.  
  134.     dc << "\n";
  135. }
  136. #endif //_DEBUG
  137.  
  138. ////////////////////////////////////////////////////////////////////////////
  139. // CPageSetupDialog hook
  140.  
  141. UINT CPageSetupDialog::PreDrawPage(WORD /*wPaperType*/, WORD /*wFlags*/,
  142.     LPPAGESETUPDLG)
  143. {
  144.     return 0;
  145.     //return 1 to prevent any more drawing
  146. }
  147.  
  148. UINT CPageSetupDialog::OnDrawPage(CDC*, UINT /*nMessage*/, LPRECT)
  149. {
  150.     return 0; // do the default
  151. }
  152.  
  153. UINT CALLBACK CPageSetupDialog::PaintHookProc(HWND hWnd, UINT message, WPARAM wParam,
  154.     LPARAM lParam)
  155. {
  156.     if (hWnd == NULL)
  157.         return 0;
  158.     // Get our Window
  159.     // assume it is already wired up to a permanent one
  160.     // the hWnd is the HWND of a control in the page setup proc
  161.     CPageSetupDialog* pDlg = DYNAMIC_DOWNCAST(CPageSetupDialog,
  162.         CWnd::FromHandlePermanent(::GetParent(hWnd)));
  163.     if (pDlg == NULL)
  164.         return 0;
  165.     switch (message)
  166.     {
  167.     case WM_PSD_PAGESETUPDLG:
  168.         return pDlg->PreDrawPage(LOWORD(wParam), HIWORD(wParam),
  169.             (LPPAGESETUPDLG) lParam);
  170.         break;
  171.     case WM_PSD_FULLPAGERECT:
  172.     case WM_PSD_MINMARGINRECT:
  173.     case WM_PSD_MARGINRECT:
  174.     case WM_PSD_GREEKTEXTRECT:
  175.     case WM_PSD_ENVSTAMPRECT:
  176.     case WM_PSD_YAFULLPAGERECT:
  177.         return pDlg->OnDrawPage(CDC::FromHandle((HDC)wParam), message, (LPRECT)lParam);
  178.         break;
  179.     }
  180.     return 0;
  181. }
  182. #endif // !_MAC
  183.  
  184. /////////////////////////////////////////////////////////////////////////////
  185. // Print/Print Setup dialog
  186.  
  187. BEGIN_MESSAGE_MAP(CPrintDialog, CCommonDialog)
  188.     //{{AFX_MSG_MAP(CPrintDialog)
  189.     ON_COMMAND(psh1, OnPrintSetup) // print setup button when print is displayed
  190.     //}}AFX_MSG_MAP
  191. END_MESSAGE_MAP()
  192.  
  193. CPrintDialog::CPrintDialog(BOOL bPrintSetupOnly,
  194.     DWORD dwFlags, CWnd* pParentWnd)
  195.     : m_pd(m_pdActual), CCommonDialog(pParentWnd)
  196. {
  197.     memset(&m_pdActual, 0, sizeof(m_pdActual));
  198.  
  199.     m_pd.lStructSize = sizeof(m_pdActual);
  200.     m_pd.Flags = (dwFlags | PD_ENABLEPRINTHOOK | PD_ENABLESETUPHOOK);
  201.     if (!afxData.bWin4 && AfxHelpEnabled())
  202.         m_pd.Flags |= PD_SHOWHELP;
  203.     m_pd.lpfnPrintHook = (COMMDLGPROC)_AfxCommDlgProc;
  204.     m_pd.lpfnSetupHook = (COMMDLGPROC)_AfxCommDlgProc;
  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.     ASSERT(m_pd.Flags & PD_ENABLEPRINTHOOK);
  262.     ASSERT(m_pd.Flags & PD_ENABLESETUPHOOK);
  263.     ASSERT(m_pd.lpfnPrintHook != NULL); // can still be a user hook
  264.     ASSERT(m_pd.lpfnSetupHook != NULL); // can still be a user hook
  265.  
  266.     m_pd.hwndOwner = PreModal();
  267.     int nResult = ::PrintDlg(&m_pd);
  268.     PostModal();
  269.     return nResult ? nResult : IDCANCEL;
  270. }
  271.  
  272. // Create an HDC without calling DoModal.
  273. HDC CPrintDialog::CreatePrinterDC()
  274. {
  275.     ASSERT_VALID(this);
  276.     m_pd.hDC = AfxCreateDC(m_pd.hDevNames, m_pd.hDevMode);
  277.     return m_pd.hDC;
  278. }
  279.  
  280. int CPrintDialog::GetCopies() const
  281. {
  282.     ASSERT_VALID(this);
  283.  
  284.     if (m_pd.Flags & PD_USEDEVMODECOPIES)
  285.         return GetDevMode()->dmCopies;
  286.     else
  287.         return m_pd.nCopies;
  288. }
  289.  
  290. LPDEVMODE CPrintDialog::GetDevMode() const
  291. {
  292.     if (m_pd.hDevMode == NULL)
  293.         return NULL;
  294.  
  295.     return (LPDEVMODE)::GlobalLock(m_pd.hDevMode);
  296. }
  297.  
  298. CString CPrintDialog::GetDriverName() const
  299. {
  300.     if (m_pd.hDevNames == NULL)
  301.         return afxEmptyString;
  302.  
  303.     LPDEVNAMES lpDev = (LPDEVNAMES)GlobalLock(m_pd.hDevNames);
  304.     return (LPCTSTR)lpDev + lpDev->wDriverOffset;
  305. }
  306.  
  307. CString CPrintDialog::GetDeviceName() const
  308. {
  309.     if (m_pd.hDevNames == NULL)
  310.         return afxEmptyString;
  311.  
  312.     LPDEVNAMES lpDev = (LPDEVNAMES)GlobalLock(m_pd.hDevNames);
  313.     return (LPCTSTR)lpDev + lpDev->wDeviceOffset;
  314. }
  315.  
  316. CString CPrintDialog::GetPortName() const
  317. {
  318.     if (m_pd.hDevNames == NULL)
  319.         return afxEmptyString;
  320.  
  321.     LPDEVNAMES lpDev = (LPDEVNAMES)GlobalLock(m_pd.hDevNames);
  322.     return (LPCTSTR)lpDev + lpDev->wOutputOffset;
  323. }
  324.  
  325. // this function must not be in afxdlgs.inl because of DLL delay loading
  326. BOOL CPrintDialog::GetDefaults()
  327. {
  328.     m_pd.Flags |= PD_RETURNDEFAULT;
  329.     return ::PrintDlg(&m_pd);
  330. }
  331.  
  332. ////////////////////////////////////////////////////////////////////////////
  333. // CPrintDialog diagnostics
  334.  
  335. #ifdef _DEBUG
  336. void CPrintDialog::Dump(CDumpContext& dc) const
  337. {
  338.     CDialog::Dump(dc);
  339.  
  340.     dc << "m_pd.hwndOwner = " << (UINT)m_pd.hwndOwner;
  341.  
  342.     if (m_pd.hDC != NULL)
  343.         dc << "\nm_pd.hDC = " << CDC::FromHandle(m_pd.hDC);
  344.  
  345.     dc << "\nm_pd.Flags = " << (LPVOID)m_pd.Flags;
  346.     dc << "\nm_pd.nFromPage = " << m_pd.nFromPage;
  347.     dc << "\nm_pd.nToPage = " << m_pd.nToPage;
  348.     dc << "\nm_pd.nMinPage = " << m_pd.nMinPage;
  349.     dc << "\nm_pd.nMaxPage = " << m_pd.nMaxPage;
  350.     dc << "\nm_pd.nCopies = " << m_pd.nCopies;
  351.  
  352.     if (m_pd.lpfnSetupHook == (COMMDLGPROC)_AfxCommDlgProc)
  353.         dc << "\nsetup hook function set to standard MFC hook function";
  354.     else
  355.         dc << "\nsetup hook function set to non-standard hook function";
  356.  
  357.     if (m_pd.lpfnPrintHook == (COMMDLGPROC)_AfxCommDlgProc)
  358.         dc << "\nprint hook function set to standard MFC hook function";
  359.     else
  360.         dc << "\nprint hook function set to non-standard hook function";
  361.  
  362.     dc << "\n";
  363. }
  364. #endif //_DEBUG
  365.  
  366. ////////////////////////////////////////////////////////////////////////////
  367. // AfxCreateDC
  368.  
  369. HDC AFXAPI AfxCreateDC(HGLOBAL hDevNames, HGLOBAL hDevMode)
  370. {
  371.     if (hDevNames == NULL)
  372.         return NULL;
  373.  
  374.     LPDEVNAMES lpDevNames = (LPDEVNAMES)::GlobalLock(hDevNames);
  375.     LPDEVMODE  lpDevMode = (hDevMode != NULL) ?
  376.                         (LPDEVMODE)::GlobalLock(hDevMode) : NULL;
  377.  
  378.     if (lpDevNames == NULL)
  379.         return NULL;
  380.  
  381.     HDC hDC = ::CreateDC((LPCTSTR)lpDevNames + lpDevNames->wDriverOffset,
  382.                       (LPCTSTR)lpDevNames + lpDevNames->wDeviceOffset,
  383.                       (LPCTSTR)lpDevNames + lpDevNames->wOutputOffset,
  384.                       lpDevMode);
  385.  
  386.     ::GlobalUnlock(hDevNames);
  387.     if (hDevMode != NULL)
  388.         ::GlobalUnlock(hDevMode);
  389.     return hDC;
  390. }
  391.  
  392. #ifdef AFX_INIT_SEG
  393. #pragma code_seg(AFX_INIT_SEG)
  394. #endif
  395.  
  396. IMPLEMENT_DYNAMIC(CPrintDialog, CDialog)
  397. #ifndef _MAC
  398. IMPLEMENT_DYNAMIC(CPageSetupDialog, CDialog)
  399. #endif
  400.  
  401. ////////////////////////////////////////////////////////////////////////////
  402.