home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / vc98 / mfc / src / appprnt.cpp < prev    next >
C/C++ Source or Header  |  1998-06-16  |  5KB  |  185 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 <cderr.h>      // Commdlg Error definitions
  13. #include <winspool.h>
  14.  
  15. #ifdef AFX_PRINT_SEG
  16. #pragma code_seg(AFX_PRINT_SEG)
  17. #endif
  18.  
  19. #ifdef _DEBUG
  20. #undef THIS_FILE
  21. static char THIS_FILE[] = __FILE__;
  22. #endif
  23.  
  24. /////////////////////////////////////////////////////////////////////////////
  25. // WinApp support for printing
  26.  
  27. BOOL CWinApp::GetPrinterDeviceDefaults(PRINTDLG* pPrintDlg)
  28. {
  29.     UpdatePrinterSelection(m_hDevNames == NULL); //force default if no current
  30.     if (m_hDevNames == NULL)
  31.         return FALSE;               // no printer defaults
  32.  
  33.     pPrintDlg->hDevNames = m_hDevNames;
  34.     pPrintDlg->hDevMode = m_hDevMode;
  35.  
  36.     ::GlobalUnlock(m_hDevNames);
  37.     ::GlobalUnlock(m_hDevMode);
  38.     return TRUE;
  39. }
  40.  
  41. void CWinApp::UpdatePrinterSelection(BOOL bForceDefaults)
  42. {
  43.     if (!bForceDefaults && m_hDevNames != NULL)
  44.     {
  45.         LPDEVNAMES lpDevNames = (LPDEVNAMES)::GlobalLock(m_hDevNames);
  46.         ASSERT(lpDevNames != NULL);
  47.         if (lpDevNames->wDefault & DN_DEFAULTPRN)
  48.         {
  49.             CPrintDialog pd(TRUE);
  50.             pd.GetDefaults();
  51.  
  52.             if (pd.m_pd.hDevNames == NULL)
  53.             {
  54.                 // Printer was default, but now there are no printers at all!
  55.                 if (m_hDevMode != NULL)
  56.                     AfxGlobalFree(m_hDevMode);
  57.                 AfxGlobalFree(m_hDevNames);
  58.                 m_hDevMode = NULL;
  59.                 m_hDevNames = NULL;
  60.             }
  61.             else if (
  62.                 lstrcmp((LPCTSTR)lpDevNames + lpDevNames->wDriverOffset,
  63.                     pd.GetDriverName()) != 0 ||
  64.                 lstrcmp((LPCTSTR)lpDevNames + lpDevNames->wDeviceOffset,
  65.                     pd.GetDeviceName()) != 0 ||
  66.                 lstrcmp((LPCTSTR)lpDevNames + lpDevNames->wOutputOffset,
  67.                     pd.GetPortName()) != 0)
  68.             {
  69.                 // Printer was default, and default has changed...assume default
  70.                 if (m_hDevMode != NULL)
  71.                     AfxGlobalFree(m_hDevMode);
  72.                 AfxGlobalFree(m_hDevNames);
  73.                 m_hDevMode = pd.m_pd.hDevMode;
  74.                 m_hDevNames = pd.m_pd.hDevNames;
  75.             }
  76.             else
  77.             {
  78.                 // Printer was default, and still is...keep the same
  79.                 if (pd.m_pd.hDevMode != NULL)
  80.                     AfxGlobalFree(pd.m_pd.hDevMode);
  81.                 if (pd.m_pd.hDevNames != NULL)
  82.                     AfxGlobalFree(pd.m_pd.hDevNames);
  83.             }
  84.         }
  85.     }
  86.     else
  87.     {
  88.         // First time or Forced -- Get defaults
  89.         CPrintDialog pd(TRUE);
  90.         pd.GetDefaults();
  91.  
  92.         if (m_hDevMode != NULL)
  93.             AfxGlobalFree(m_hDevMode);
  94.         if (m_hDevNames != NULL)
  95.             AfxGlobalFree(m_hDevNames);
  96.  
  97.         m_hDevMode = pd.m_pd.hDevMode;
  98.         m_hDevNames = pd.m_pd.hDevNames;
  99.     }
  100. }
  101.  
  102. int CWinApp::DoPrintDialog(CPrintDialog* pPD)
  103. {
  104.     UpdatePrinterSelection(FALSE);
  105.  
  106.     pPD->m_pd.hDevMode = m_hDevMode;
  107.     pPD->m_pd.hDevNames = m_hDevNames;
  108.     int nResponse = pPD->DoModal();
  109.  
  110.     // if OK or Cancel is selected we need to update cached devMode/Names
  111.     while (nResponse != IDOK && nResponse != IDCANCEL)
  112.     {
  113.         switch (::CommDlgExtendedError())
  114.         {
  115.         // CommDlg cannot give these errors after NULLing these handles
  116.         case PDERR_PRINTERNOTFOUND:
  117.         case PDERR_DNDMMISMATCH:
  118.         case PDERR_DEFAULTDIFFERENT:
  119.             if (pPD->m_pd.hDevNames != NULL)
  120.             {
  121.                 ASSERT(m_hDevNames == pPD->m_pd.hDevNames);
  122.                 AfxGlobalFree(pPD->m_pd.hDevNames);
  123.                 pPD->m_pd.hDevNames = NULL;
  124.                 m_hDevNames = NULL;
  125.             }
  126.  
  127.             if (pPD->m_pd.hDevMode)
  128.             {
  129.                 ASSERT(m_hDevMode == pPD->m_pd.hDevMode);
  130.                 AfxGlobalFree(pPD->m_pd.hDevMode);
  131.                 pPD->m_pd.hDevMode = NULL;
  132.                 m_hDevMode = NULL;
  133.             }
  134.             break;
  135.  
  136.         default:
  137.             return nResponse;       // do not update cached devMode/Names
  138.         }
  139.  
  140.         nResponse = pPD->DoModal();
  141.     }
  142.  
  143.     // refresh current CWinApp cache of printer device information
  144.     m_hDevMode = pPD->m_pd.hDevMode;
  145.     m_hDevNames = pPD->m_pd.hDevNames;
  146.  
  147.     return nResponse;
  148. }
  149.  
  150. void CWinApp::OnFilePrintSetup()
  151. {
  152.     CPrintDialog pd(TRUE);
  153.     DoPrintDialog(&pd);
  154. }
  155.  
  156. void CWinApp::SelectPrinter(HANDLE hDevNames, HANDLE hDevMode, BOOL bFreeOld)
  157. {
  158.     if (m_hDevNames != hDevNames)
  159.     {
  160.         if (m_hDevNames != NULL && bFreeOld)
  161.             AfxGlobalFree(m_hDevNames);
  162.         m_hDevNames = hDevNames;
  163.     }
  164.     if (m_hDevMode != hDevMode)
  165.     {
  166.         if (m_hDevMode != NULL && bFreeOld)
  167.             AfxGlobalFree(m_hDevMode);
  168.         m_hDevMode = hDevMode;
  169.     }
  170. }
  171.  
  172. BOOL CWinApp::CreatePrinterDC(CDC& dc)
  173. {
  174.     HDC hDC = AfxCreateDC(m_hDevNames, m_hDevMode);
  175.     if (hDC != NULL)
  176.     {
  177.         dc.DeleteDC();
  178.         VERIFY(dc.Attach(hDC));
  179.         return TRUE;
  180.     }
  181.     return FALSE;
  182. }
  183.  
  184. /////////////////////////////////////////////////////////////////////////////
  185.