www.pudn.com > helpview.zip > Print.c, change:1997-10-15,size:11638b


/* 
  
Copyright 1997 Willows Software, Inc.  
 
This library is free software; you can redistribute it and/or 
modify it under the terms of the GNU Library General Public License as 
published by the Free Software Foundation; either version 2 of the 
License, or (at your option) any later version. 
 
This library is distributed in the hope that it will be useful, 
but WITHOUT ANY WARRANTY; without even the implied warranty of 
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU 
Library General Public License for more details. 
 
You should have received a copy of the GNU Library General Public 
License along with this library; see the file COPYING.LIB.  If 
not, write to the Free Software Foundation, Inc., 675 Mass Ave, 
Cambridge, MA 02139, USA. 
 
*/ 
 
/************************************************************************* 
* 
* PRINT.C 
* Printing routines.      
* 
* Author: Paul E. Kissel 
* 
**************************************************************************/ 
 
/************************************* 
* 
*  Custom Includes 
* 
**************************************/ 
#include "print.h" 
#include "wnddata.h" 
#include "globals.h" 
#include "display.h" 
 
/************************************* 
* 
*  System Includes 
* 
**************************************/ 
#include <commdlg.h> 
#include <string.h> 
 
 
/*********************************** 
** 
**  Private functions 
** 
***********************************/ 
BOOL CALLBACK PrintDlgProc( HWND hDlg, WORD message, WORD wParam, LONG lParam ); 
BOOL CALLBACK AbortProc( HDC hPrinterDC, short int nCode ); 
 
 
/********************************************************************* 
** 
**  Starts the printing system. 
** 
**********************************************************************/ 
void StartPrintSystem( HWND hMainWnd ) 
{ 
  PRINTDLG pd;      /* Common print dialog structure */ 
 
  /* Set all structure members to zero. */ 
  memset( &pd, 0, sizeof(PRINTDLG) ); 
   
  /* Fill in non-variant fields of PRINTDLG struct. */ 
  pd.lStructSize = sizeof(PRINTDLG); 
   
  /* Set printer system data. */ 
  WndData( hMainWnd, WDM_SETPRINTSYSTEMDATA, 0, (void *) &pd ); 
} 
 
 
/********************************************************************* 
** 
**  Stops the printing system. 
** 
**********************************************************************/ 
void StopPrintSystem( HWND hMainWnd ) 
{ 
  PRINTDLG pd;      /* Common print dialog structure */ 
 
  /* Get printer system data. */ 
  WndData( hMainWnd, WDM_GETPRINTSYSTEMDATA, 0, (void *) &pd ); 
 
  /* Free any memory allocated by the PrintDlg() function. */ 
  if( pd.hDevMode != NULL ) GlobalFree( pd.hDevMode ); 
  if( pd.hDevNames != NULL ) GlobalFree( pd.hDevNames ); 
} 
 
 
/********************************************************************* 
** 
**  Shows the Printer Setup dialog box if it isn't already displayed. 
** 
**********************************************************************/ 
void ShowPrinterSetupDialogBox( HWND hParentWnd, HWND hMainWnd ) 
{ 
  PRINTDLG pd;                    /* Common print dialog structure */ 
  BOOL bDlgAlreadyDisplayed;      /* Is it already being displayed? */ 
  DWORD dwError; 
  BOOL bStopTrying; 
              
              
  /* Is the dialog box already displayed. */ 
  bDlgAlreadyDisplayed = (BOOL) WndData( hMainWnd, WDM_GETPRINTSETUPDLGDISPLAYED, 0, NULL ); 
              
  /* The dialog box is already displayed. */ 
  if( bDlgAlreadyDisplayed == TRUE ) 
  { 
    /* Failure or aborted. */ 
    return;       
  } 
 
  /* The dialog box is being displayed. */ 
  WndData( hMainWnd, WDM_SETPRINTSETUPDLGDISPLAYED, (DWORD) TRUE, NULL ); 
              
  /* Get printer system data. */ 
  WndData( hMainWnd, WDM_GETPRINTSYSTEMDATA, 0, (void *) &pd ); 
 
  /* Fill in non-variant fields of PRINTDLG struct. */ 
  pd.hwndOwner = hParentWnd; 
  pd.Flags     = PD_PRINTSETUP; 
 
  while( TRUE ) 
  { 
    /* Show dialog box. */ 
    if( PrintDlg( (LPPRINTDLG) &pd ) ) 
    { 
      /* Set printer system data. */ 
      WndData( hMainWnd, WDM_SETPRINTSYSTEMDATA, 0, (void *) &pd ); 
     
      /* Exit while loop. */ 
      break; 
    } 
     
    /* Error or user canceled. */ 
    else 
    { 
      /* Did we have an error. */ 
      dwError = CommDlgExtendedError(); 
      if( dwError != 0 ) 
      { 
        /*  
        ** Try to correct error condition by going back to the 
        ** default printer.  If we are already doing that, 
        ** just stop trying to show dialog box. 
        */ 
         
        /* If we are already using default printer. */ 
        if( pd.hDevMode == NULL && pd.hDevNames == NULL ) 
        { 
          bStopTrying = TRUE; 
        } 
        else 
        { 
          bStopTrying = FALSE; 
        } 
         
        /* Free memory allocated by the PrintDlg() function. */ 
        if( pd.hDevMode != NULL ) GlobalFree( pd.hDevMode ); 
        if( pd.hDevNames != NULL ) GlobalFree( pd.hDevNames ); 
        pd.hDevMode  = NULL; 
        pd.hDevNames = NULL; 
           
        /* Set printer system data. */ 
        WndData( hMainWnd, WDM_SETPRINTSYSTEMDATA, 0, (void *) &pd ); 
 
        /* Stop trying. */ 
        if( bStopTrying == TRUE ) 
        { 
          break; 
        } 
      } 
       
      /* User canceled. */ 
      else 
      { 
        /* Exit while loop. */ 
        break; 
      } 
    } 
  } 
   
  /* The dialog box is not being displayed. */ 
  WndData( hMainWnd, WDM_SETPRINTSETUPDLGDISPLAYED, (DWORD) FALSE, NULL ); 
} 
 
 
/********************************************************************* 
** 
**  Shows the Print dialog box if it isn't already displayed. 
** 
**  Returns: 
** 
**       NULL: Dialog box already visible or an error occurred. 
**       HDC : Handle to printer device context. 
** 
**********************************************************************/ 
HDC ShowPrintDialogBox( HWND hParentWnd, HWND hMainWnd ) 
{ 
  PRINTDLG pd;                    /* Common print dialog structure */ 
  BOOL bDlgAlreadyDisplayed;      /* Is it already being displayed? */ 
  DWORD dwError; 
  BOOL bStopTrying; 
  BOOL bError;            
              
  /* Is the dialog box already displayed. */ 
  bDlgAlreadyDisplayed = (BOOL) WndData( hMainWnd, WDM_GETPRINTDLGDISPLAYED, 0, NULL ); 
              
  /* The dialog box is already displayed. */ 
  if( bDlgAlreadyDisplayed == TRUE ) 
  { 
    /* Aborted. */ 
    return( NULL );       
  } 
 
  /* The dialog box is being displayed. */ 
  WndData( hMainWnd, WDM_SETPRINTDLGDISPLAYED, (DWORD) TRUE, NULL ); 
              
  /* Get printer system data. */ 
  WndData( hMainWnd, WDM_GETPRINTSYSTEMDATA, 0, (void *) &pd ); 
 
  /* Fill in non-variant fields of PRINTDLG struct. */ 
  pd.hDC       = NULL; 
  pd.hwndOwner = hParentWnd; 
  pd.Flags     = PD_RETURNDC | PD_HIDEPRINTTOFILE |  
                 PD_NOPAGENUMS | PD_NOSELECTION |  
                 PD_USEDEVMODECOPIES; 
 
  /* Assume error. */ 
  bError = TRUE; 
   
  while( TRUE ) 
  { 
    /* Show dialog box. */ 
    if( PrintDlg( (LPPRINTDLG) &pd ) ) 
    { 
      /* Set printer system data. */ 
      WndData( hMainWnd, WDM_SETPRINTSYSTEMDATA, 0, (void *) &pd ); 
     
      /* No error. */ 
      bError = FALSE; 
 
      /* Exit while loop. */ 
      break; 
    } 
     
    /* Error or user canceled. */ 
    else 
    { 
      /* Did we have an error. */ 
      dwError = CommDlgExtendedError(); 
      if( dwError != 0 ) 
      { 
        /*  
        ** Try to correct error condition by going back to the 
        ** default printer.  If we are already doing that, 
        ** just stop trying to show dialog box. 
        */ 
         
        /* If we are already using default printer. */ 
        if( pd.hDevMode == NULL && pd.hDevNames == NULL ) 
        { 
          bStopTrying = TRUE; 
        } 
        else 
        { 
          bStopTrying = FALSE; 
        } 
         
        /* Free memory allocated by the PrintDlg() function. */ 
        if( pd.hDevMode != NULL ) GlobalFree( pd.hDevMode ); 
        if( pd.hDevNames != NULL ) GlobalFree( pd.hDevNames ); 
        pd.hDevMode  = NULL; 
        pd.hDevNames = NULL; 
           
        /* Set printer system data. */ 
        WndData( hMainWnd, WDM_SETPRINTSYSTEMDATA, 0, (void *) &pd ); 
 
        /* Stop trying. */ 
        if( bStopTrying == TRUE ) 
        { 
          break; 
        } 
      } 
       
      /* User canceled. */ 
      else 
      { 
        /* Exit while loop. */ 
        break; 
      } 
    } 
  } 
   
  /* The dialog box is not being displayed. */ 
  WndData( hMainWnd, WDM_SETPRINTDLGDISPLAYED, (DWORD) FALSE, NULL ); 
 
  /* If had error or canceled. */ 
  if( bError == TRUE ) 
  { 
    return( NULL ); 
  } 
   
  /* Return DC to printer. */ 
  else  
  { 
    return( pd.hDC ); 
  } 
} 
 
 
BOOL CALLBACK PrintDlgProc( HWND hDlg, WORD message, WORD wParam, LONG lParam ) 
{ 
  switch (message) 
  { 
    case WM_INITDIALOG: 
    { 
      /* Don't allow the system menu close menu item. */ 
      EnableMenuItem ( GetSystemMenu ( hDlg, FALSE ), SC_CLOSE, MF_GRAYED ) ; 
       
      /* User has not aborted the print job. */ 
      SetUserAbortedPrint( FALSE ); 
       
      return TRUE; 
    } 
     
    case WM_COMMAND: 
    { 
      /* User has aborted the print job. */ 
      SetUserAbortedPrint( TRUE ); 
 
      /* Allow user input into the apps. window. */ 
      EnableWindow( GetParent( hDlg ), TRUE ); 
 
      /* Close dialog box. */ 
      DestroyWindow( hDlg ); 
      SetAbortPrintDlg( 0 ); 
 
      return TRUE ; 
    } 
  } 
  return FALSE ; 
}           
 
 
 
BOOL CALLBACK AbortProc( HDC hPrinterDC, short int nCode ) 
{ 
  MSG   msg ; 
  HWND hDlgPrint; 
   
  /* Get "Abort Printing" dialog box. */ 
  hDlgPrint = GetAbortPrintDlg(); 
   
  /* While printing has not been canceled and there is a message. */ 
  while ( !GetUserAbortedPrint() && PeekMessage( &msg, NULL, 0, 0, PM_REMOVE ) ) 
  { 
    /* If the message is not for the "Abort Printing" dialog box. */ 
    if( !hDlgPrint || !IsDialogMessage( hDlgPrint, &msg ) ) 
    { 
      /* Send message off. */ 
      TranslateMessage( &msg ); 
      DispatchMessage( &msg ); 
    } 
  } 
 
  /* Return if the user aborted the printing. */ 
  return( !GetUserAbortedPrint() ); 
} 
 
 
 
BOOL PrintWindowsTopic( HWND hParentWnd, HWND hMainWnd ) 
{ 
  HWND    hTopicWnd; 
  HDC     hPrinterDC; 
  HWND    hDlgPrint; 
   
  /* Show the Print dialog box. */ 
  hPrinterDC = ShowPrintDialogBox( hParentWnd, hMainWnd ); 
 
  /* If we did not get a DC for a printer. */ 
  if( hPrinterDC == NULL ) return( FALSE ); 
   
  /* Prevent all input while printing. */   
  EnableWindow( hMainWnd, FALSE ) ; 
   
  /* Create the "Cancel Printing" dialog box. */ 
  hDlgPrint = CreateDialog( GetLibInst(), "DLG_PRINTING", hMainWnd, PrintDlgProc ); 
  SetAbortPrintDlg( hDlgPrint ); 
 
  /* AbortProc() callback function. */ 
  SetAbortProc( hPrinterDC, AbortProc );  
 
  /* Get the parent window's topic window. */ 
  hTopicWnd = (HWND) WndData( hParentWnd, WDM_GETMAINTOPICHWND, 0, NULL ); 
 
  /* Show the "Cancel Printing" dialog box. */ 
  ShowWindow( hDlgPrint, SW_SHOW ); 
  UpdateWindow( hDlgPrint ); 
 
  /* Print the topic. */ 
  PrintPage( hPrinterDC, hMainWnd, hTopicWnd ); 
   
  /* If user didn't abort printing. */ 
  if( !GetUserAbortedPrint() ) 
  { 
    /* Allow user input into the apps. window. */ 
    EnableWindow( hMainWnd, TRUE ); 
 
    /* Close dialog box. */ 
    DestroyWindow( hDlgPrint ); 
  } 
 
  /* Delete the printer DC. */ 
  DeleteDC( hPrinterDC  ); 
}