2 * Printing support for MSWindows
6 * Wireshark - Network traffic analyzer
7 * By Gerald Combs <gerald@wireshark.org>
8 * Copyright 2002, Jeffrey C. Foster <jfoste@woodward.com>
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License
12 * as published by the Free Software Foundation; either version 2
13 * of the License, or (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
25 * This original code was from the Technet Article Q139652 :
26 * HOWTO: Print a Document
40 #include "print_mswin.h"
41 #include "file_util.h"
44 Some thoughts about a GTK win32 printer dialog:
46 "EnumPrinters()", asking for information level 2 - the PRINTER_INFO_2
47 structure contains a pLocation string pointer, along with other
50 "PrinterProperties", could be used to show a native printer property page?!?
54 http://msdn.microsoft.com/library/default.asp?url=/library/en-us/gdi/prntspol_62ia.asp
56 for information on printer APIs.
59 BOOL CALLBACK abort_proc( HDC hDC, int Error );
60 HDC get_printer_dc(void);
61 void init_doc_struct( DOCINFO* di, char* docname);
62 void print_file( char* file_name, HDC hdc);
64 void print_mswin(char *file_name)
70 HWND hWndParent = HWND_DESKTOP; /* would be better to be a real window */
72 /* Need a printer DC to print to. */
73 hDC = get_printer_dc();
75 /* Did you get a good DC?, Cancel will return NULL also, so what to do? */
81 /* You always have to use an AbortProc(). */
82 if( SetAbortProc( hDC, abort_proc ) == SP_ERROR )
84 MessageBox( NULL, "Error setting up AbortProc",
85 "Error", MB_APPLMODAL | MB_OK);
89 /* Init the DOCINFO and start the document. */
90 init_doc_struct( &di, "MyDoc");
95 print_file(file_name, hDC );
98 /* Indicate end of document. */
105 /*===============================*/
106 /* Obtain printer device context */
107 /* ==============================*/
108 HDC get_printer_dc(void)
113 * XXX - can this be done without a Windows print dialog?
115 * "CreateDC()" creates a device context, and you can
116 * apparently specify WINSPL16 as the driver name on
117 * Windows OT, or the name of a "print provider", such as
118 * "WINSPOOL" on Windows NT, to get a context for a printer.
120 * The device name would be the printer name as shown by the
121 * Print Manager; is there a way to enumerate those?
124 /* Initialize the PRINTDLG structure. */
125 memset( &pdlg, 0, sizeof( PRINTDLG ) );
126 pdlg.lStructSize = sizeof( PRINTDLG );
127 /* Set the flag to return printer DC. */
129 /* return the device context we need */
131 /* disable the "Pages" radio button */
133 /* disable the "Selection" radio button */
135 /* let device print multiple pages (if requested) */
136 PD_USEDEVMODECOPIESANDCOLLATE;
138 /* Invoke the printer dialog box. */
141 /* hDC member of the PRINTDLG structure contains the printer DC. */
145 /*===============================*/
146 /* The Abort Procudure */
147 /* ==============================*/
148 BOOL CALLBACK abort_proc( HDC hDC, int Error )
151 while( PeekMessage( &msg, NULL, 0, 0, PM_REMOVE ) )
153 TranslateMessage( &msg );
154 DispatchMessage( &msg );
159 /*===============================*/
160 /* Initialize DOCINFO structure */
161 /* ==============================*/
162 void init_doc_struct( DOCINFO* di, char* docname)
164 /* Always zero it before using it. */
165 memset( di, 0, sizeof( DOCINFO ) );
166 /* Fill in the required members. */
167 di->cbSize = sizeof( DOCINFO );
168 di->lpszDocName = docname;
171 /*===============================*/
172 /* Drawing on the DC */
173 /* ==============================*/
174 void print_file( char *file_name, HDC hdc) {
176 #define max_buf_size 1024
182 int results, cnt=0, y_pos = y_offset, y_cnt = 0;
183 char buf[ max_buf_size];
187 GetTextMetrics(hdc, &tm);
188 SetMapMode (hdc, MM_TEXT);
191 fh1 = eth_fopen( file_name, "r" );
193 perror( "open failed on input file" );
197 while ((results = fread( &ch, 1, 1, fh1 )) != 0) {
199 /* end of page (form feed)? */
203 TextOut(hdc, x_offset,y_pos, buf, strlen(buf));
204 y_pos += tm.tmHeight;
215 /* end of line (line feed)? */
219 TextOut(hdc, x_offset,y_pos, buf, strlen(buf));
220 y_pos += tm.tmHeight;
222 /* last line on page? -> reset page */
223 if ( ++y_cnt == max_lines){
233 if ( cnt == ( max_buf_size - 1)) {
236 TextOut(hdc, x_offset, y_pos, buf, strlen(buf));
237 y_pos += tm.tmHeight;
239 /* last line on page? -> reset page */
240 if ( ++y_cnt == max_lines){
252 /* Print the remaining text if needed */
255 TextOut(hdc, 0,y_pos, buf, strlen(buf));