/******************************************************************************/
/*                                                                            */
/*  PRINT.CPP - Oversee control of print operations                           */
/*                                                                            */
/******************************************************************************/

#define STRICT
#include <windows.h>
#include <commctrl.h>
#include <assert.h>
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <ctype.h>
#include "const.h"
#include "classes.h"
#include "funcdefs.h"
#include "lin.rh"
#include "extern.h"

int initialize_for_printing ( HWND hwnd )
{
   int ret_val, raster_caps ;
   char msg[256] ;

   memset ( &print_dlg , 0 , sizeof(PRINTDLG) ) ;
   print_dlg.lStructSize = sizeof(PRINTDLG) ;
   print_dlg.hwndOwner = hwnd ;
   print_dlg.hDevMode = (HANDLE) NULL ;
   print_dlg.hDevNames = (HANDLE) NULL ;
   print_dlg.hDC = (HDC) NULL ;
   print_dlg.Flags = PD_RETURNDC | PD_NOPAGENUMS | PD_NOSELECTION ;
   print_dlg.nFromPage = 1 ;
   print_dlg.nToPage = 1 ;
   print_dlg.nMinPage = 0 ;
   print_dlg.nMaxPage = 0 ;
   print_dlg.nCopies = 1 ;
   print_dlg.hInstance = (HINSTANCE) NULL ;
   print_dlg.lCustData = 0L ;
   print_dlg.lpfnPrintHook = (LPPRINTHOOKPROC) NULL ;
   print_dlg.lpfnSetupHook = (LPSETUPHOOKPROC) NULL ;
   print_dlg.lpPrintTemplateName = (LPSTR) NULL ;
   print_dlg.lpSetupTemplateName = (LPSTR) NULL ;
   print_dlg.hPrintTemplate = (HANDLE) NULL ;
   print_dlg.hSetupTemplate = (HANDLE) NULL ;

   SetLastError ( 0 ) ;
   if (! PrintDlg ( &print_dlg )) {
      ret_val = GetLastError () ;
      sprintf_s ( msg , "PrintDlg ERROR %d", ret_val ) ;
      audit ( msg ) ;
      SetLastError ( 0 ) ;
      return 1 ;
      }

   raster_caps = GetDeviceCaps ( print_dlg.hDC , RASTERCAPS ) ;
   if (! (raster_caps & RC_STRETCHDIB)) {
      MessageBox ( hwnd , "Printer does not support StretchDIB printing" ,
                  "Problem", MB_ICONEXCLAMATION | MB_OK ) ;
      DeleteDC ( print_dlg.hDC ) ;
      return 1 ;
      }

   memset ( &doc_info , 0 , sizeof(DOCINFO) ) ;
   doc_info.cbSize = sizeof(DOCINFO) ;
   doc_info.lpszDocName = "" ;
   doc_info.lpszOutput = NULL ;

   SetLastError ( 0 ) ;
   ret_val = StartDoc ( print_dlg.hDC , &doc_info ) ;

   if (ret_val <= 0) {
      ret_val = GetLastError () ;
      sprintf_s ( msg , "StartDoc ERROR %d", ret_val ) ;
      audit ( msg ) ;
      SetLastError ( 0 ) ;
      return 1 ;
      }

   SetLastError ( 0 ) ;
   ret_val = StartPage ( print_dlg.hDC ) ;

   if (ret_val <= 0) {
      ret_val = GetLastError () ;
      sprintf_s ( msg , "StartPage ERROR %d", ret_val ) ;
      audit ( msg ) ;
      SetLastError ( 0 ) ;
      return 1 ;
      }

   EnableWindow ( hwnd , FALSE ) ;
   return 0 ;
}

void end_printing ( HWND hwnd )
{
   int ret_val ;
   char msg[256] ;

   SetLastError ( 0 ) ;
   ret_val = EndPage ( print_dlg.hDC ) ;

   if (ret_val <= 0) {
      ret_val = GetLastError () ;
      sprintf_s ( msg , "EndPage ERROR %d", ret_val ) ;
      audit ( msg ) ;
      SetLastError ( 0 ) ;
      }

   SetLastError ( 0 ) ;
   ret_val = EndDoc ( print_dlg.hDC ) ;

   if (ret_val <= 0) {
      ret_val = GetLastError () ;
      sprintf_s ( msg , "EndDoc ERROR %d", ret_val ) ;
      audit ( msg ) ;
      SetLastError ( 0 ) ;
      }

   EnableWindow ( hwnd , TRUE ) ;
   DeleteDC ( print_dlg.hDC ) ;
}
