In GTK+ 2.x, "gtk_entry_get_text()" returns a "const gchar *"; assign
[obnox/wireshark/wip.git] / gtk / print_mswin.c
1 /* print_mswin.c
2  * Printing support for MSWindows
3  *
4  * $Id: print_mswin.c,v 1.7 2003/11/11 13:59:53 ulfl Exp $
5  *
6  * Ethereal - Network traffic analyzer
7  * By Gerald Combs <gerald@ethereal.com>
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 BOOL CALLBACK abort_proc( HDC hDC, int Error );
41 HDC get_printer_dc(void);
42 void init_doc_struct( DOCINFO* di, char* docname);
43 void print_file( char* file_name, HDC hdc);
44
45 void print_mswin(char *file_name)
46
47    {
48        HDC        hDC;
49        DOCINFO    di;
50
51         HWND hWndParent = HWND_DESKTOP; /* would be better to be a real window */
52
53        /* Need a printer DC to print to. */
54        hDC = get_printer_dc();
55
56        /* Did you get a good DC?, Cancel will return NULL also, so what to do? */
57        if( !hDC)
58        {
59            return;
60        }
61
62        /* You always have to use an AbortProc(). */
63        if( SetAbortProc( hDC, abort_proc ) == SP_ERROR )
64        {
65            MessageBox( NULL, "Error setting up AbortProc",
66                                        "Error", MB_APPLMODAL | MB_OK);
67            return;
68        }
69
70        /* Init the DOCINFO and start the document. */
71        init_doc_struct( &di, "MyDoc");
72        StartDoc( hDC, &di );
73
74        /* Print one page. */
75        StartPage( hDC );
76        print_file(file_name, hDC );
77        EndPage( hDC );
78
79        /* Indicate end of document. */
80        EndDoc( hDC );
81
82        /* Clean up */
83        DeleteDC( hDC );
84    }
85
86    /*===============================*/
87    /* Obtain printer device context */
88    /* ==============================*/
89    HDC get_printer_dc(void)
90    {
91        PRINTDLG pdlg;
92
93        /* Initialize the PRINTDLG structure. */
94        memset( &pdlg, 0, sizeof( PRINTDLG ) );
95        pdlg.lStructSize = sizeof( PRINTDLG );
96        /* Set the flag to return printer DC. */
97        pdlg.Flags =  
98            /* return the device context we need */
99            PD_RETURNDC |        
100            /* disable the "Pages" radio button */
101            PD_NOPAGENUMS |      
102            /* disable the "Selection" radio button */
103            PD_NOSELECTION |     
104            /* let device print multiple pages (if requested) */
105            PD_USEDEVMODECOPIESANDCOLLATE; 
106
107        /* Invoke the printer dialog box. */
108        PrintDlg( &pdlg );
109
110        /* hDC member of the PRINTDLG structure contains the printer DC. */
111        return pdlg.hDC;
112    }
113
114    /*===============================*/
115    /* The Abort Procudure           */
116    /* ==============================*/
117    BOOL CALLBACK abort_proc( HDC hDC, int Error )
118    {
119        MSG   msg;
120        while( PeekMessage( &msg, NULL, 0, 0, PM_REMOVE ) )
121        {
122            TranslateMessage( &msg );
123            DispatchMessage( &msg );
124        }
125        return TRUE;
126    }
127
128    /*===============================*/
129    /* Initialize DOCINFO structure  */
130    /* ==============================*/
131    void init_doc_struct( DOCINFO* di, char* docname)
132    {
133        /* Always zero it before using it. */
134        memset( di, 0, sizeof( DOCINFO ) );
135        /* Fill in the required members. */
136        di->cbSize = sizeof( DOCINFO );
137        di->lpszDocName = docname;
138    }
139
140    /*===============================*/
141    /* Drawing on the DC             */
142    /* ==============================*/
143 void print_file( char *file_name, HDC hdc) {
144
145     #define max_buf_size 1024
146     #define max_lines 66
147     #define y_offset 5
148     #define x_offset 5
149
150     FILE* fh1;
151     int results, cnt=0, y_pos = y_offset, y_cnt = 0;
152     char buf[ max_buf_size];
153     char ch;
154     TEXTMETRIC tm;
155
156     GetTextMetrics(hdc, &tm);
157     SetMapMode (hdc, MM_TEXT);
158
159
160     fh1 = fopen( file_name, "r" );
161     if( !fh1 )
162         perror( "open failed on input file" );
163
164      else {
165         while ((results = fread( &ch, 1, 1, fh1 )) != 0) {
166
167 /* if end of line send buffer and more y position */
168
169             if ( ch == 0x0a){
170                 buf[ cnt] = 0;
171                 TextOut(hdc, x_offset,y_pos, buf, strlen(buf));
172                 y_pos += tm.tmHeight;
173                 cnt = 0;
174                 if ( ++y_cnt == max_lines){
175        /* Print one page. */
176                     EndPage( hdc );
177                     StartPage( hdc );
178                     y_pos = y_offset;
179                     y_cnt = 0;
180                 }
181
182 /* if line buffer is full, dump it */
183             }else { if ( cnt == ( max_buf_size - 1)) {
184                 buf[ cnt] = 0;
185                 TextOut(hdc, x_offset, y_pos, buf, strlen(buf));
186                 y_pos += tm.tmHeight;
187                 cnt = 0;
188
189                 if ( ++y_cnt == max_lines){
190        /* Print one page. */
191                     EndPage( hdc );
192                     StartPage( hdc );
193                     y_pos = y_offset;
194                     y_cnt = 0;
195                 }
196             }
197
198             buf[ cnt++] = ch;
199         }
200     }
201 /*XXX  need feof test here ? */
202
203 /* Print the last text if needed */
204     if ( cnt > 0) {
205         buf[ cnt] = 0;
206         TextOut(hdc, 0,y_pos, buf, strlen(buf));
207     }
208     fclose(fh1);
209 }
210 }