remove some warnings
[obnox/wireshark/wip.git] / gtk / print_mswin.c
1 /* print_mswin.c
2  * Printing support for MSWindows
3  *
4  * $Id$
5  *
6  * Wireshark - Network traffic analyzer
7  * By Gerald Combs <gerald@wireshark.org>
8  * Copyright 2002, Jeffrey C. Foster <jfoste@woodward.com>
9  *
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.
14  *
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.
19  *
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.
23  *
24  *
25  * This original code was from the Technet Article Q139652 :
26  *       HOWTO: Print a Document
27  */
28
29
30 #include <string.h>
31 #include <stdio.h>
32
33 #include <windows.h>
34 #include <commdlg.h>
35
36 #ifdef __WIN32__
37 #include <winspool.h>
38 #endif
39
40 #include "print_mswin.h"
41 #include "file_util.h"
42
43 /*
44 Some thoughts about a GTK win32 printer dialog:
45
46 "EnumPrinters()", asking for information level 2 - the PRINTER_INFO_2
47 structure contains a pLocation string pointer, along with other
48 information.
49  
50 "PrinterProperties", could be used to show a native printer property page?!?
51
52 See
53  
54         http://msdn.microsoft.com/library/default.asp?url=/library/en-us/gdi/prntspol_62ia.asp
55  
56 for information on printer APIs. 
57
58 */
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);
63
64 void print_mswin(char *file_name)
65
66    {
67        HDC        hDC;
68        DOCINFO    di;
69
70         HWND hWndParent = HWND_DESKTOP; /* would be better to be a real window */
71
72        /* Need a printer DC to print to. */
73        hDC = get_printer_dc();
74
75        /* Did you get a good DC?, Cancel will return NULL also, so what to do? */
76        if( !hDC)
77        {
78            return;
79        }
80
81        /* You always have to use an AbortProc(). */
82        if( SetAbortProc( hDC, abort_proc ) == SP_ERROR )
83        {
84            MessageBox( NULL, "Error setting up AbortProc",
85                                        "Error", MB_APPLMODAL | MB_OK);
86            return;
87        }
88
89        /* Init the DOCINFO and start the document. */
90        init_doc_struct( &di, "MyDoc");
91        StartDoc( hDC, &di );
92
93        /* Print one page. */
94        StartPage( hDC );
95        print_file(file_name, hDC );
96        EndPage( hDC );
97
98        /* Indicate end of document. */
99        EndDoc( hDC );
100
101        /* Clean up */
102        DeleteDC( hDC );
103    }
104
105    /*===============================*/
106    /* Obtain printer device context */
107    /* ==============================*/
108    HDC get_printer_dc(void)
109    {
110        PRINTDLG pdlg;
111
112        /*
113         * XXX - can this be done without a Windows print dialog?
114         *
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.
119         *
120         * The device name would be the printer name as shown by the
121         * Print Manager; is there a way to enumerate those?
122         */
123
124        /* Initialize the PRINTDLG structure. */
125        memset( &pdlg, 0, sizeof( PRINTDLG ) );
126        pdlg.lStructSize = sizeof( PRINTDLG );
127        /* Set the flag to return printer DC. */
128        pdlg.Flags =  
129            /* return the device context we need */
130            PD_RETURNDC |        
131            /* disable the "Pages" radio button */
132            PD_NOPAGENUMS |      
133            /* disable the "Selection" radio button */
134            PD_NOSELECTION |     
135            /* let device print multiple pages (if requested) */
136            PD_USEDEVMODECOPIESANDCOLLATE; 
137
138        /* Invoke the printer dialog box. */
139        PrintDlg( &pdlg );
140
141        /* hDC member of the PRINTDLG structure contains the printer DC. */
142        return pdlg.hDC;
143    }
144
145    /*===============================*/
146    /* The Abort Procudure           */
147    /* ==============================*/
148    BOOL CALLBACK abort_proc( HDC hDC, int Error )
149    {
150        MSG   msg;
151        while( PeekMessage( &msg, NULL, 0, 0, PM_REMOVE ) )
152        {
153            TranslateMessage( &msg );
154            DispatchMessage( &msg );
155        }
156        return TRUE;
157    }
158
159    /*===============================*/
160    /* Initialize DOCINFO structure  */
161    /* ==============================*/
162    void init_doc_struct( DOCINFO* di, char* docname)
163    {
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;
169    }
170
171    /*===============================*/
172    /* Drawing on the DC             */
173    /* ==============================*/
174 void print_file( char *file_name, HDC hdc) {
175
176     #define max_buf_size 1024
177     #define max_lines 66
178     #define y_offset 5
179     #define x_offset 5
180
181     FILE* fh1;
182     int results, cnt=0, y_pos = y_offset, y_cnt = 0;
183     char buf[ max_buf_size];
184     char ch;
185     TEXTMETRIC tm;
186
187     GetTextMetrics(hdc, &tm);
188     SetMapMode (hdc, MM_TEXT);
189
190
191     fh1 = eth_fopen( file_name, "r" );
192     if( !fh1 ) {
193         perror( "open failed on input file" );
194         return;
195     }
196
197     while ((results = fread( &ch, 1, 1, fh1 )) != 0) {
198
199         /* end of page (form feed)? */
200         if ( ch == 0x0c){
201             /* send buffer */
202             buf[ cnt] = 0;
203             TextOut(hdc, x_offset,y_pos, buf, strlen(buf));
204             y_pos += tm.tmHeight;
205             cnt = 0;
206
207             /* reset page */
208             EndPage( hdc );
209             StartPage( hdc );
210             y_pos = y_offset;
211             y_cnt = 0;
212             continue;
213         }
214
215         /* end of line (line feed)? */
216         if ( ch == 0x0a){
217             /* send buffer */
218             buf[ cnt] = 0;
219             TextOut(hdc, x_offset,y_pos, buf, strlen(buf));
220             y_pos += tm.tmHeight;
221             cnt = 0;
222             /* last line on page? -> reset page */
223             if ( ++y_cnt == max_lines){
224                 EndPage( hdc );
225                 StartPage( hdc );
226                 y_pos = y_offset;
227                 y_cnt = 0;
228             }
229             continue;
230         }
231
232         /* buffer full? */
233         if ( cnt == ( max_buf_size - 1)) {
234             /* send buffer */
235             buf[ cnt] = 0;
236             TextOut(hdc, x_offset, y_pos, buf, strlen(buf));
237             y_pos += tm.tmHeight;
238             cnt = 0;
239             /* last line on page? -> reset page */
240             if ( ++y_cnt == max_lines){
241                 EndPage( hdc );
242                 StartPage( hdc );
243                 y_pos = y_offset;
244                 y_cnt = 0;
245             }
246             continue;
247         }
248
249         buf[ cnt++] = ch;
250     } /* while */
251
252     /* Print the remaining text if needed */
253     if ( cnt > 0) {
254         buf[ cnt] = 0;
255         TextOut(hdc, 0,y_pos, buf, strlen(buf));
256     }
257
258     fclose(fh1);
259 }