home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c480 / 19.ddi / MFC / SRC / APPPRNT.CP_ / APPPRNT.CP
Encoding:
Text File  |  1993-02-08  |  3.5 KB  |  147 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 <cderr.h>      // Commdlg Error definitions
  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 BASED_CODE THIS_FILE[] = __FILE__;
  22. #endif
  23.  
  24. /////////////////////////////////////////////////////////////////////////////
  25. // WinApp support for printing
  26.  
  27. BOOL CWinApp::GetPrinterDeviceDefaults(PRINTDLG FAR* pPrintDlg)
  28. {
  29.     UpdatePrinterSelection(FALSE);
  30.  
  31.     if (m_hDevNames == NULL)
  32.     {
  33.         // try to get defaults
  34.         UpdatePrinterSelection(TRUE);
  35.         if (m_hDevNames == NULL)
  36.             return FALSE;               // no printer defaults
  37.     }
  38.  
  39.     pPrintDlg->hDevNames = m_hDevNames;
  40.     pPrintDlg->hDevMode = m_hDevMode;
  41.  
  42.     ::GlobalUnlock(m_hDevNames);
  43.     ::GlobalUnlock(m_hDevMode);
  44.     return TRUE;
  45. }
  46.  
  47. void CWinApp::UpdatePrinterSelection(BOOL bForceDefaults)
  48. {
  49.     if (!bForceDefaults && m_hDevNames != NULL)
  50.     {
  51.         LPDEVNAMES lpDevNames = (LPDEVNAMES)::GlobalLock(m_hDevNames);
  52.         ASSERT(lpDevNames != NULL);
  53.         if ((lpDevNames->wDefault & DN_DEFAULTPRN) != 0)
  54.         {
  55.             CPrintDialog pd(TRUE);
  56.             pd.GetDefaults();
  57.  
  58.             if (lstrcmp((LPSTR)lpDevNames + lpDevNames->wDeviceOffset,
  59.                                 pd.GetDeviceName()) != 0)
  60.             {
  61.                 // Printer was default, and default has changed...assume default
  62.                 if (m_hDevMode != NULL)
  63.                     ::GlobalFree(m_hDevMode);
  64.                 ::GlobalFree(m_hDevNames);
  65.                 m_hDevMode = pd.m_pd.hDevMode;
  66.                 m_hDevNames = pd.m_pd.hDevNames;
  67.                 lpDevNames = (LPDEVNAMES)::GlobalLock(m_hDevNames);
  68.             }
  69.             else
  70.             {
  71.                 if (pd.m_pd.hDevMode != NULL)
  72.                     ::GlobalFree(pd.m_pd.hDevMode);
  73.                 if (pd.m_pd.hDevNames != NULL)
  74.                     ::GlobalFree(pd.m_pd.hDevNames);
  75.             }
  76.         }
  77.     }
  78.     else
  79.     {
  80.         // First time or Forced -- Get defaults
  81.         CPrintDialog pd(TRUE);
  82.         pd.GetDefaults();
  83.  
  84.         if (m_hDevMode != NULL)
  85.             ::GlobalFree(m_hDevMode);
  86.         if (m_hDevNames != NULL)
  87.             ::GlobalFree(m_hDevNames);
  88.  
  89.         m_hDevMode = pd.m_pd.hDevMode;
  90.         m_hDevNames = pd.m_pd.hDevNames;
  91.     }
  92. }
  93.  
  94. int CWinApp::DoPrintDialog(CPrintDialog* pPD)
  95. {
  96.     UpdatePrinterSelection(FALSE);
  97.  
  98.     pPD->m_pd.hDevMode = m_hDevMode;
  99.     pPD->m_pd.hDevNames = m_hDevNames;
  100.  
  101.     int nResponse;
  102.     
  103.     while ((nResponse = pPD->DoModal()) != IDOK)
  104.     {
  105.         switch (::CommDlgExtendedError())
  106.         {
  107.         // CommDlg cannot give these errors after NULLing these handles
  108.         case PDERR_PRINTERNOTFOUND:
  109.         case PDERR_DNDMMISMATCH:
  110.             if (pPD->m_pd.hDevNames != NULL)
  111.             {
  112.                 ASSERT(m_hDevNames == pPD->m_pd.hDevNames);
  113.  
  114.                 ::GlobalFree(pPD->m_pd.hDevNames);
  115.                 pPD->m_pd.hDevNames = NULL;
  116.                 m_hDevNames = NULL;
  117.             }
  118.  
  119.             if (pPD->m_pd.hDevMode)
  120.             {
  121.                 ASSERT(m_hDevMode == pPD->m_pd.hDevMode);
  122.  
  123.                 ::GlobalFree(pPD->m_pd.hDevMode);
  124.                 pPD->m_pd.hDevMode = NULL;
  125.                 m_hDevMode = NULL;
  126.             }
  127.             break;
  128.  
  129.         default:
  130.             return nResponse;       // do not update cached devMode/Names
  131.         }
  132.     }
  133.     
  134.     m_hDevMode = pPD->m_pd.hDevMode;
  135.     m_hDevNames = pPD->m_pd.hDevNames;
  136.  
  137.     return nResponse;
  138. }
  139.  
  140. void CWinApp::OnFilePrintSetup()
  141. {
  142.     CPrintDialog pd(TRUE);
  143.     DoPrintDialog(&pd);
  144. }
  145.  
  146. /////////////////////////////////////////////////////////////////////////////
  147.