2002-06-19 20:44:05 +00:00
|
|
|
/* print_mswin.c
|
|
|
|
* Printing support for MSWindows
|
|
|
|
*
|
2003-11-11 13:59:53 +00:00
|
|
|
* $Id: print_mswin.c,v 1.7 2003/11/11 13:59:53 ulfl Exp $
|
2002-06-19 20:44:05 +00:00
|
|
|
*
|
|
|
|
* Ethereal - Network traffic analyzer
|
|
|
|
* By Gerald Combs <gerald@ethereal.com>
|
|
|
|
* Copyright 2002, Jeffrey C. Foster <jfoste@woodward.com>
|
2002-08-28 21:04:11 +00:00
|
|
|
*
|
2002-06-19 20:44:05 +00:00
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
2002-08-28 21:04:11 +00:00
|
|
|
*
|
2002-06-19 20:44:05 +00:00
|
|
|
* This program 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 General Public License for more details.
|
2002-08-28 21:04:11 +00:00
|
|
|
*
|
2002-06-19 20:44:05 +00:00
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* This original code was from the Technet Article Q139652 :
|
|
|
|
* HOWTO: Print a Document
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
2002-09-07 00:02:24 +00:00
|
|
|
#include <string.h>
|
2002-09-09 20:16:12 +00:00
|
|
|
#include <stdio.h>
|
2002-09-07 00:02:24 +00:00
|
|
|
|
2003-10-07 03:09:50 +00:00
|
|
|
#include <windows.h>
|
|
|
|
#include <commdlg.h>
|
2002-06-19 20:44:05 +00:00
|
|
|
|
|
|
|
#ifdef __WIN32__
|
|
|
|
#include <winspool.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
BOOL CALLBACK abort_proc( HDC hDC, int Error );
|
|
|
|
HDC get_printer_dc(void);
|
|
|
|
void init_doc_struct( DOCINFO* di, char* docname);
|
|
|
|
void print_file( char* file_name, HDC hdc);
|
|
|
|
|
|
|
|
void print_mswin(char *file_name)
|
|
|
|
|
|
|
|
{
|
|
|
|
HDC hDC;
|
|
|
|
DOCINFO di;
|
|
|
|
|
|
|
|
HWND hWndParent = HWND_DESKTOP; /* would be better to be a real window */
|
2002-08-28 21:04:11 +00:00
|
|
|
|
2002-06-19 20:44:05 +00:00
|
|
|
/* Need a printer DC to print to. */
|
|
|
|
hDC = get_printer_dc();
|
2002-08-28 21:04:11 +00:00
|
|
|
|
2002-06-19 20:44:05 +00:00
|
|
|
/* Did you get a good DC?, Cancel will return NULL also, so what to do? */
|
|
|
|
if( !hDC)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2002-08-28 21:04:11 +00:00
|
|
|
|
2002-06-19 20:44:05 +00:00
|
|
|
/* You always have to use an AbortProc(). */
|
|
|
|
if( SetAbortProc( hDC, abort_proc ) == SP_ERROR )
|
|
|
|
{
|
|
|
|
MessageBox( NULL, "Error setting up AbortProc",
|
|
|
|
"Error", MB_APPLMODAL | MB_OK);
|
|
|
|
return;
|
|
|
|
}
|
2002-08-28 21:04:11 +00:00
|
|
|
|
2002-06-19 20:44:05 +00:00
|
|
|
/* Init the DOCINFO and start the document. */
|
|
|
|
init_doc_struct( &di, "MyDoc");
|
|
|
|
StartDoc( hDC, &di );
|
2002-08-28 21:04:11 +00:00
|
|
|
|
2002-06-19 20:44:05 +00:00
|
|
|
/* Print one page. */
|
|
|
|
StartPage( hDC );
|
|
|
|
print_file(file_name, hDC );
|
|
|
|
EndPage( hDC );
|
2002-08-28 21:04:11 +00:00
|
|
|
|
2002-06-19 20:44:05 +00:00
|
|
|
/* Indicate end of document. */
|
|
|
|
EndDoc( hDC );
|
2002-08-28 21:04:11 +00:00
|
|
|
|
2002-06-19 20:44:05 +00:00
|
|
|
/* Clean up */
|
|
|
|
DeleteDC( hDC );
|
|
|
|
}
|
2002-08-28 21:04:11 +00:00
|
|
|
|
2002-06-19 20:44:05 +00:00
|
|
|
/*===============================*/
|
|
|
|
/* Obtain printer device context */
|
|
|
|
/* ==============================*/
|
|
|
|
HDC get_printer_dc(void)
|
|
|
|
{
|
|
|
|
PRINTDLG pdlg;
|
2002-08-28 21:04:11 +00:00
|
|
|
|
2002-06-19 20:44:05 +00:00
|
|
|
/* Initialize the PRINTDLG structure. */
|
|
|
|
memset( &pdlg, 0, sizeof( PRINTDLG ) );
|
|
|
|
pdlg.lStructSize = sizeof( PRINTDLG );
|
|
|
|
/* Set the flag to return printer DC. */
|
2003-11-11 13:59:53 +00:00
|
|
|
pdlg.Flags =
|
|
|
|
/* return the device context we need */
|
|
|
|
PD_RETURNDC |
|
|
|
|
/* disable the "Pages" radio button */
|
|
|
|
PD_NOPAGENUMS |
|
|
|
|
/* disable the "Selection" radio button */
|
|
|
|
PD_NOSELECTION |
|
|
|
|
/* let device print multiple pages (if requested) */
|
|
|
|
PD_USEDEVMODECOPIESANDCOLLATE;
|
2002-08-28 21:04:11 +00:00
|
|
|
|
2002-06-19 20:44:05 +00:00
|
|
|
/* Invoke the printer dialog box. */
|
|
|
|
PrintDlg( &pdlg );
|
|
|
|
|
|
|
|
/* hDC member of the PRINTDLG structure contains the printer DC. */
|
|
|
|
return pdlg.hDC;
|
|
|
|
}
|
2002-08-28 21:04:11 +00:00
|
|
|
|
2002-06-19 20:44:05 +00:00
|
|
|
/*===============================*/
|
|
|
|
/* The Abort Procudure */
|
|
|
|
/* ==============================*/
|
|
|
|
BOOL CALLBACK abort_proc( HDC hDC, int Error )
|
|
|
|
{
|
|
|
|
MSG msg;
|
|
|
|
while( PeekMessage( &msg, NULL, 0, 0, PM_REMOVE ) )
|
|
|
|
{
|
|
|
|
TranslateMessage( &msg );
|
|
|
|
DispatchMessage( &msg );
|
|
|
|
}
|
|
|
|
return TRUE;
|
|
|
|
}
|
2002-08-28 21:04:11 +00:00
|
|
|
|
2002-06-19 20:44:05 +00:00
|
|
|
/*===============================*/
|
|
|
|
/* Initialize DOCINFO structure */
|
|
|
|
/* ==============================*/
|
|
|
|
void init_doc_struct( DOCINFO* di, char* docname)
|
|
|
|
{
|
|
|
|
/* Always zero it before using it. */
|
|
|
|
memset( di, 0, sizeof( DOCINFO ) );
|
|
|
|
/* Fill in the required members. */
|
|
|
|
di->cbSize = sizeof( DOCINFO );
|
|
|
|
di->lpszDocName = docname;
|
|
|
|
}
|
2002-08-28 21:04:11 +00:00
|
|
|
|
2002-06-19 20:44:05 +00:00
|
|
|
/*===============================*/
|
|
|
|
/* Drawing on the DC */
|
|
|
|
/* ==============================*/
|
|
|
|
void print_file( char *file_name, HDC hdc) {
|
|
|
|
|
|
|
|
#define max_buf_size 1024
|
|
|
|
#define max_lines 66
|
|
|
|
#define y_offset 5
|
|
|
|
#define x_offset 5
|
|
|
|
|
|
|
|
FILE* fh1;
|
|
|
|
int results, cnt=0, y_pos = y_offset, y_cnt = 0;
|
|
|
|
char buf[ max_buf_size];
|
|
|
|
char ch;
|
2002-08-28 21:04:11 +00:00
|
|
|
TEXTMETRIC tm;
|
2002-06-19 20:44:05 +00:00
|
|
|
|
|
|
|
GetTextMetrics(hdc, &tm);
|
|
|
|
SetMapMode (hdc, MM_TEXT);
|
|
|
|
|
|
|
|
|
|
|
|
fh1 = fopen( file_name, "r" );
|
|
|
|
if( !fh1 )
|
|
|
|
perror( "open failed on input file" );
|
|
|
|
|
|
|
|
else {
|
|
|
|
while ((results = fread( &ch, 1, 1, fh1 )) != 0) {
|
|
|
|
|
|
|
|
/* if end of line send buffer and more y position */
|
|
|
|
|
|
|
|
if ( ch == 0x0a){
|
|
|
|
buf[ cnt] = 0;
|
|
|
|
TextOut(hdc, x_offset,y_pos, buf, strlen(buf));
|
2002-08-28 21:04:11 +00:00
|
|
|
y_pos += tm.tmHeight;
|
2002-06-19 20:44:05 +00:00
|
|
|
cnt = 0;
|
|
|
|
if ( ++y_cnt == max_lines){
|
|
|
|
/* Print one page. */
|
|
|
|
EndPage( hdc );
|
|
|
|
StartPage( hdc );
|
2002-08-28 21:04:11 +00:00
|
|
|
y_pos = y_offset;
|
2002-06-19 20:44:05 +00:00
|
|
|
y_cnt = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* if line buffer is full, dump it */
|
|
|
|
}else { if ( cnt == ( max_buf_size - 1)) {
|
|
|
|
buf[ cnt] = 0;
|
|
|
|
TextOut(hdc, x_offset, y_pos, buf, strlen(buf));
|
2002-08-28 21:04:11 +00:00
|
|
|
y_pos += tm.tmHeight;
|
2002-06-19 20:44:05 +00:00
|
|
|
cnt = 0;
|
|
|
|
|
|
|
|
if ( ++y_cnt == max_lines){
|
|
|
|
/* Print one page. */
|
|
|
|
EndPage( hdc );
|
|
|
|
StartPage( hdc );
|
2002-08-28 21:04:11 +00:00
|
|
|
y_pos = y_offset;
|
2002-06-19 20:44:05 +00:00
|
|
|
y_cnt = 0;
|
|
|
|
}
|
|
|
|
}
|
2002-08-28 21:04:11 +00:00
|
|
|
|
2002-06-19 20:44:05 +00:00
|
|
|
buf[ cnt++] = ch;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/*XXX need feof test here ? */
|
|
|
|
|
|
|
|
/* Print the last text if needed */
|
|
|
|
if ( cnt > 0) {
|
|
|
|
buf[ cnt] = 0;
|
|
|
|
TextOut(hdc, 0,y_pos, buf, strlen(buf));
|
|
|
|
}
|
|
|
|
fclose(fh1);
|
|
|
|
}
|
|
|
|
}
|