2 * Printing support for MSWindows
6 * Ethereal - Network traffic analyzer
7 * By Gerald Combs <gerald@ethereal.com>
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
41 Some thoughts about a GTK win32 printer dialog:
43 "EnumPrinters()", asking for information level 2 - the PRINTER_INFO_2
44 structure contains a pLocation string pointer, along with other
47 "PrinterProperties", could be used to show a native printer property page?!?
51 http://msdn.microsoft.com/library/default.asp?url=/library/en-us/gdi/prntspol_62ia.asp
53 for information on printer APIs.
56 BOOL CALLBACK abort_proc( HDC hDC, int Error );
57 HDC get_printer_dc(void);
58 void init_doc_struct( DOCINFO* di, char* docname);
59 void print_file( char* file_name, HDC hdc);
61 void print_mswin(char *file_name)
67 HWND hWndParent = HWND_DESKTOP; /* would be better to be a real window */
69 /* Need a printer DC to print to. */
70 hDC = get_printer_dc();
72 /* Did you get a good DC?, Cancel will return NULL also, so what to do? */
78 /* You always have to use an AbortProc(). */
79 if( SetAbortProc( hDC, abort_proc ) == SP_ERROR )
81 MessageBox( NULL, "Error setting up AbortProc",
82 "Error", MB_APPLMODAL | MB_OK);
86 /* Init the DOCINFO and start the document. */
87 init_doc_struct( &di, "MyDoc");
92 print_file(file_name, hDC );
95 /* Indicate end of document. */
102 /*===============================*/
103 /* Obtain printer device context */
104 /* ==============================*/
105 HDC get_printer_dc(void)
110 * XXX - can this be done without a Windows print dialog?
112 * "CreateDC()" creates a device context, and you can
113 * apparently specify WINSPL16 as the driver name on
114 * Windows OT, or ther name of a "print provider", such as
115 * "WINSPOOL" on Windows NT, to get a context for a printer.
117 * The device name would be the printer name as shown by the
118 * Print Manager; is there a way to enumerate those?
121 /* Initialize the PRINTDLG structure. */
122 memset( &pdlg, 0, sizeof( PRINTDLG ) );
123 pdlg.lStructSize = sizeof( PRINTDLG );
124 /* Set the flag to return printer DC. */
126 /* return the device context we need */
128 /* disable the "Pages" radio button */
130 /* disable the "Selection" radio button */
132 /* let device print multiple pages (if requested) */
133 PD_USEDEVMODECOPIESANDCOLLATE;
135 /* Invoke the printer dialog box. */
138 /* hDC member of the PRINTDLG structure contains the printer DC. */
142 /*===============================*/
143 /* The Abort Procudure */
144 /* ==============================*/
145 BOOL CALLBACK abort_proc( HDC hDC, int Error )
148 while( PeekMessage( &msg, NULL, 0, 0, PM_REMOVE ) )
150 TranslateMessage( &msg );
151 DispatchMessage( &msg );
156 /*===============================*/
157 /* Initialize DOCINFO structure */
158 /* ==============================*/
159 void init_doc_struct( DOCINFO* di, char* docname)
161 /* Always zero it before using it. */
162 memset( di, 0, sizeof( DOCINFO ) );
163 /* Fill in the required members. */
164 di->cbSize = sizeof( DOCINFO );
165 di->lpszDocName = docname;
168 /*===============================*/
169 /* Drawing on the DC */
170 /* ==============================*/
171 void print_file( char *file_name, HDC hdc) {
173 #define max_buf_size 1024
179 int results, cnt=0, y_pos = y_offset, y_cnt = 0;
180 char buf[ max_buf_size];
184 GetTextMetrics(hdc, &tm);
185 SetMapMode (hdc, MM_TEXT);
188 fh1 = fopen( file_name, "r" );
190 perror( "open failed on input file" );
193 while ((results = fread( &ch, 1, 1, fh1 )) != 0) {
195 /* if end of line send buffer and more y position */
199 TextOut(hdc, x_offset,y_pos, buf, strlen(buf));
200 y_pos += tm.tmHeight;
202 if ( ++y_cnt == max_lines){
203 /* Print one page. */
210 /* if line buffer is full, dump it */
211 }else { if ( cnt == ( max_buf_size - 1)) {
213 TextOut(hdc, x_offset, y_pos, buf, strlen(buf));
214 y_pos += tm.tmHeight;
217 if ( ++y_cnt == max_lines){
218 /* Print one page. */
229 /*XXX need feof test here ? */
231 /* Print the last text if needed */
234 TextOut(hdc, 0,y_pos, buf, strlen(buf));