home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c500 / 4.ddi / EDIT.WEX / EPRINT.C < prev    next >
Encoding:
C/C++ Source or Header  |  1992-05-28  |  10.0 KB  |  403 lines

  1. /*
  2.  *%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  3.  *%                                       %
  4.  *%    Copyright (C) 1991, by WATCOM Systems Inc. All rights reserved.    %
  5.  *%                                       %
  6.  *%     Permission is granted to anyone to use this example program for       %
  7.  *%     any purpose on any computer system, subject to the following       %
  8.  *%    restrictions:                               %
  9.  *%                                       %
  10.  *%     1. This example is provided on an "as is" basis, without warranty. %
  11.  *%       You indemnify, hold harmless and defend WATCOM from and against %
  12.  *%       any claims or lawsuits, including attorney's, that arise or       %
  13.  *%       result from the use or distribution of this example, or any     %
  14.  *%       modification thereof.                       %
  15.  *%                                       %
  16.  *%     2. You may not remove, alter or suppress this notice from this       %
  17.  *%        example program or any modification thereof.               %
  18.  *%                                       %
  19.  *%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  20.  *
  21.  * EPRINT.C
  22.  *
  23.  * Windows edit program: printer functions
  24.  *
  25.  */
  26. #define INCLUDE_DRIVINIT_H
  27. #include <windows.h>
  28. #include <string.h>
  29. #include <stdio.h>
  30. #include "edit.h"
  31.  
  32. #define PSTR_LEN    256
  33. static char    ProfileString[PSTR_LEN];
  34. static char    _FAR *Title;
  35. static char    *Rest;
  36. static char    *DeviceName;
  37. static char    *DriverName;
  38. static char    *PortName;
  39. static char    _FAR *PrinterData;
  40. static char    DriverFormat[] = "%s.DRV";
  41.  
  42. /*
  43.  * PrinterDC - obtains a DC to the current printer
  44.  */
  45. HDC PrinterDC( void )
  46. {
  47.     char    *tmp;
  48.     HDC        hdc;
  49.     char    str[128];
  50.     HANDLE    hlib;
  51.     FARPROC    fp;
  52.  
  53.     /*
  54.      * get printer device type
  55.      */
  56.     PrinterSupport = PSUPP_NONE;
  57.     GetProfileString( "WiNdOwS", "dEvIcE", "", ProfileString, PSTR_LEN );
  58.  
  59.     /*
  60.      * find first ',' and insert a 0; this is the device name
  61.      */
  62.     DeviceName = ProfileString;
  63.     tmp = ProfileString;
  64.     while( 1 ) {
  65.         if( *tmp == ',' ) {
  66.         *tmp = 0;
  67.         tmp++;
  68.         break;
  69.     }
  70.     if( !(*tmp) ) {
  71.         DeviceName[0] = 0;
  72.         return( NULL );
  73.     }
  74.     tmp++;
  75.     }
  76.  
  77.     /*
  78.      * find next ',' and insert a 0; this terminates the driver name
  79.      */
  80.     DriverName = tmp;
  81.     while( 1 ) {
  82.         if( *tmp == ',' ) {
  83.         *tmp = 0;
  84.         tmp++;
  85.         break;
  86.     }
  87.     if( !(*tmp) ) {
  88.         DeviceName[0] = 0;
  89.         return( NULL );
  90.     }
  91.     tmp++;
  92.     }
  93.     if( !(*tmp) ) {
  94.     DeviceName[0] = 0;
  95.     return( NULL );
  96.     }
  97.     PortName = tmp;
  98.  
  99.     /*
  100.      * check if printer has changed, if so, toss the data
  101.      */
  102.     if( PrinterData != NULL && _strcmp( PrinterData, DeviceName ) ) {
  103.     MemFree( PrinterData );
  104.     PrinterData = NULL;
  105.     }
  106.  
  107.     /*
  108.      * get the printer dc
  109.      */
  110.     hdc = CreateDC( DriverName, DeviceName, PortName, NULL );
  111.     if( hdc == NULL ) return( NULL );
  112.     PrinterSupport = PSUPP_CANPRINT;
  113.  
  114.     /*
  115.      * look for ExtDeviceMode
  116.      */
  117.     sprintf( str, DriverFormat, DriverName );
  118.     hlib = LoadLibrary( str );
  119.     if( hlib >= 32 ) {
  120.     fp = GetProcAddress( hlib, "EXTDEVICEMODE" );
  121.     if( fp != NULL ) PrinterSupport = PSUPP_CANPRINTANDSET;
  122.     FreeLibrary( hlib );
  123.     }
  124.     return( hdc );
  125.  
  126. } /* PrinterDC */
  127.  
  128. #ifndef __WINDOWS_386__
  129. typedef int (FAR PASCAL *EDMPROC)(HWND,HANDLE,LPDEVMODE,LPSTR, LPSTR,LPDEVMODE,LPSTR,WORD);
  130. #endif
  131. /*
  132.  * GetPrinterSetup - get data about this printer
  133.  */
  134. void GetPrinterSetup( HWND hwnd )
  135. {
  136.     char    str[128];
  137.     HANDLE    hlib;
  138.     int        edm_flag;
  139.     char    _FAR *newdata;
  140.     char    _FAR *olddata;
  141.     WORD    bytes;
  142.     WORD    rc;
  143. #ifdef __WINDOWS_386__
  144.     FARPROC    fp;
  145.     HINDIR    hindir;
  146. #else
  147.     EDMPROC    fp;
  148. #endif
  149.     
  150.     /*
  151.      * get ExtDeviceMode address
  152.      */
  153.     sprintf( str, DriverFormat, DriverName );
  154.     hlib = LoadLibrary( str );
  155.     if( hlib < 32 ) return;
  156.     fp = GetProcAddress( hlib, "EXTDEVICEMODE" );
  157.     if( fp == NULL ) {
  158.     FreeLibrary( hlib );
  159.     return;
  160.     }
  161.  
  162.     /*
  163.      * get number of bytes
  164.      */
  165. #ifdef __WINDOWS_386__
  166.     hindir = GetIndirectFunctionHandle( fp,
  167.             INDIR_WORD, INDIR_WORD, INDIR_DWORD,
  168.             INDIR_PTR, INDIR_PTR, INDIR_DWORD,
  169.             INDIR_DWORD, INDIR_WORD, INDIR_ENDLIST );
  170.     bytes = (WORD) InvokeIndirectFunction( hindir, hwnd, hlib, NULL,
  171.                 DeviceName, PortName, NULL, NULL, 0 );
  172.     MemFree( (void *) hindir );
  173. #else
  174.     bytes = fp( hwnd, hlib, (LPDEVMODE) NULL, (LPSTR) DeviceName,
  175.               (LPSTR) PortName, (LPDEVMODE) NULL, (LPSTR) NULL, 0);
  176. #endif
  177.     /*
  178.      * if we already have data, use that in the dialog
  179.      */
  180.     newdata = MemAlloc( bytes );
  181.     edm_flag = DM_PROMPT | DM_COPY;
  182.     if( PrinterData != NULL ) edm_flag |= DM_MODIFY;
  183.     olddata = PrinterData;
  184.  
  185.     /*
  186.      * now, get the new data from the dialog
  187.      */
  188. #ifdef __WINDOWS_386__
  189.     hindir = GetIndirectFunctionHandle( fp,
  190.                 INDIR_WORD, INDIR_WORD, INDIR_PTR,
  191.             INDIR_PTR, INDIR_PTR, INDIR_PTR,
  192.                 INDIR_DWORD, INDIR_WORD, INDIR_ENDLIST );
  193.     rc = InvokeIndirectFunction(
  194.                 hindir,
  195.                     hwnd,
  196.                      hlib,
  197.                      (LPDEVMODE) newdata,
  198.                      (LPSTR) DeviceName,
  199.                      (LPSTR) PortName,
  200.                      (LPDEVMODE) olddata,
  201.                      0L,
  202.                      edm_flag );
  203.     MemFree( (void *) hindir );
  204. #else
  205.     rc =  fp( hwnd, hlib, (LPDEVMODE) newdata, (LPSTR) DeviceName,
  206.              (LPSTR) PortName, (LPDEVMODE) olddata,
  207.          (LPSTR) NULL, edm_flag );
  208. #endif
  209.  
  210.     FreeLibrary( hlib );
  211.     if( rc != IDOK ) {
  212.         MemFree( newdata );
  213.     } else {
  214.     MemFree( olddata );
  215.     PrinterData = newdata;
  216.     }
  217.  
  218. } /* GetPrinterSetup */
  219.  
  220. static volatile BOOL WasAborted;
  221. static HWND DlgWnd;
  222.  
  223. /*
  224.  * Abort - procedure to abort a print job
  225.  */
  226. int _EXPORT FAR PASCAL Abort( HDC hdc, WORD w )
  227. {
  228.     MSG    msg;
  229.  
  230.     w = w;    /* shut up warning */
  231.     hdc = hdc;
  232.  
  233.     while( !WasAborted && PeekMessage( &msg, NULL, NULL, NULL, TRUE )) {
  234.         if( !IsDialogMessage( DlgWnd, &msg ) ) {
  235.             TranslateMessage( &msg );
  236.             DispatchMessage ( &msg );
  237.         }
  238.     }
  239.     return( !WasAborted );
  240.  
  241. } /* Abort */
  242.  
  243. /*
  244.  * AbortDialog - wait for the person to press cancel
  245.  */
  246. int _EXPORT FAR PASCAL AbortDialog( HWND hwnd, unsigned msg, WORD wparam,
  247.                 LONG lparam )
  248. {
  249.     lparam = lparam;
  250.     wparam = wparam;
  251.  
  252.     switch(msg) {
  253.     case WM_COMMAND:
  254.     WasAborted = TRUE;
  255.     return( TRUE );
  256.  
  257.     case WM_INITDIALOG:
  258.     SetDlgItemText( hwnd, PRINT_TITLE, (LPSTR) Title );
  259.     SetDlgItemText( hwnd, PRINT_REST, (LPSTR) Rest );
  260.     return( TRUE );
  261.     }
  262.     return( FALSE );
  263.  
  264. } /* AbortDialog */
  265.  
  266.  
  267. /*
  268.  * CleanUp - clean up after print
  269.  */
  270. static BOOL CleanUp( HDC hdc, FARPROC abort, FARPROC abortdlg, BOOL err )
  271. {
  272.  
  273.     if( err ) Escape( hdc, ABORTDOC, 0, NULL, NULL );
  274.     if( DlgWnd != NULL ) DestroyWindow( DlgWnd );
  275.     DlgWnd = NULL;
  276.     if( hdc != NULL ) DeleteDC( hdc );
  277.     if( abortdlg != NULL ) FreeProcInstance( abortdlg );
  278.     if( abort != NULL ) FreeProcInstance( abort );
  279.     /* Error? make sure the user knows... */
  280.     if( WasAborted ) {
  281.         MessageBox( NULL, Title, "Error printing file", MB_OK );
  282.     } 
  283.     return( 0 );
  284.  
  285. } /* CleanUp */
  286.  
  287. /*
  288.  * Print - print current file
  289.  */
  290. BOOL Print( LPEDATA ed )
  291. {
  292.     FARPROC    abort;
  293.     FARPROC    abortdlg;
  294.     HDC        hdc;
  295.     WORD    pageheight;
  296.     WORD    lineheight;
  297.     WORD    height;
  298.     WORD    linecnt;
  299.     WORD    currline;
  300.     WORD    offset;
  301.     WORD    len;
  302.     LOCALHANDLE    hdata;
  303.     BOOL    error;
  304.     char    FAR *buff;
  305.     char    _FAR *tmpbuff;
  306.     char    rest[256];
  307.  
  308.     /*
  309.      * init. misc.
  310.      */
  311.     Title = ed->filename;
  312.     if( Title == NULL ) Title = (LPSTR) "untitled";
  313.     WasAborted = FALSE;
  314.     error = FALSE;
  315.     Rest = rest;
  316.     sprintf( Rest,"%s %s", PortName, DeviceName );
  317.  
  318.     /*
  319.      * create procs
  320.      */
  321.     abort = MakeProcInstance( Abort, ed->inst );
  322.     if( abort == NULL ) return( 0 );
  323.     abortdlg = MakeProcInstance( AbortDialog, ed->inst );
  324.     if( abortdlg == NULL ) return( CleanUp( NULL, abort, NULL, FALSE ) );
  325.  
  326.     /*
  327.      * init. printer
  328.      */
  329.     hdc = PrinterDC();
  330.     if( !hdc ) return( CleanUp( NULL, abort, abortdlg, FALSE ) );
  331.  
  332.     /*
  333.      * Create abort dialog
  334.      */
  335.     DlgWnd = CreateDialog( ed->inst, "AbortDialog", ed->hwnd, abortdlg );
  336.     if( DlgWnd == NULL ) return( CleanUp( hdc, abort, abortdlg, 0 ) );
  337.     ShowWindow( DlgWnd, SW_SHOW );
  338.     UpdateWindow( DlgWnd );
  339.  
  340.     /*
  341.      * set up escape function
  342.      */
  343.     if( Escape( hdc, SETABORTPROC, 0, (LPSTR) abort, NULL ) < 0 ) {
  344.         return( CleanUp( hdc, abort, abortdlg, FALSE ) );
  345.     }
  346.  
  347.     /*
  348.      * start document
  349.      */
  350.     if( Escape( hdc, STARTDOC, _strlen( Title ), (LPSTR) Title, NULL ) < 0 ) {
  351.         return( CleanUp( hdc, abort, abortdlg, FALSE ) );
  352.     }
  353.  
  354.     /*
  355.      * get info
  356.      */
  357.     pageheight = GetDeviceCaps( hdc, VERTRES );
  358.     lineheight = HIWORD( GetTextExtent( hdc, "CC", 2 ) );
  359.     height = 0;
  360.     linecnt = SendMessage( ed->editwnd, EM_GETLINECOUNT, 0, 0L );
  361.     currline = 0;
  362.     hdata = SendMessage( ed->editwnd, EM_GETHANDLE, 0, 0L );
  363.  
  364.     /*
  365.      * print all lines
  366.      */
  367.     while( currline < linecnt ) {
  368.  
  369.         if( height + lineheight > pageheight ) {
  370.         height = 0;
  371.             if( Escape( hdc, NEWFRAME, 0, NULL, NULL ) < 0 ) error = TRUE;
  372.         }
  373.     if( WasAborted || error ) return( CleanUp( hdc, abort, abortdlg, TRUE ) );
  374.  
  375.     /*
  376.      * get the data and print it
  377.      */
  378.     offset = SendMessage( ed->editwnd, EM_LINEINDEX, currline, 0L );
  379.     len = SendMessage( ed->editwnd, EM_LINELENGTH, offset, 0L );
  380.         buff = MK_LOCAL32( LocalLock( hdata ) ) ;
  381.     tmpbuff = MemAlloc( len );
  382.         _fmemcpy( tmpbuff, buff + offset, len );
  383.         LocalUnlock( hdata );
  384.  
  385.         TextOut( hdc, 0, height, (LPSTR)tmpbuff, len );
  386.     MemFree( tmpbuff );
  387.  
  388.         currline++;
  389.         height += lineheight;
  390.     }
  391.  
  392.     /*
  393.      * eject final page, and tidy up
  394.      */
  395.     if( Escape( hdc, NEWFRAME, 0, NULL, NULL) >= 0 ) {
  396.     if( Escape( hdc, ENDDOC, 0, NULL, NULL ) < 0 ) error = TRUE;
  397.     } else {
  398.     error = TRUE;
  399.     }
  400.     return( CleanUp( hdc, abort, abortdlg, error ) );
  401.  
  402. } /* Print */
  403.