]> git.saurik.com Git - wxWidgets.git/blob - src/msw/dcprint.cpp
1. wxMSW seems to work (please test and send your bug reports!)
[wxWidgets.git] / src / msw / dcprint.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: dcprint.cpp
3 // Purpose: wxPrinterDC class
4 // Author: Julian Smart
5 // Modified by:
6 // Created: 01/02/97
7 // RCS-ID: $Id$
8 // Copyright: (c) Julian Smart and Markus Holzem
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 #ifdef __GNUG__
13 #pragma implementation "dcprint.h"
14 #endif
15
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
18
19 #ifdef __BORLANDC__
20 #pragma hdrstop
21 #endif
22
23 #ifndef WX_PRECOMP
24 #endif
25
26 #include "wx/dcprint.h"
27 #include "wx/log.h"
28 #include "math.h"
29
30 #include "wx/msw/private.h"
31
32 #if wxUSE_COMMON_DIALOGS
33 #include <commdlg.h>
34 #endif
35
36 #ifndef __WIN32__
37 #include <print.h>
38 #endif
39
40 #if !USE_SHARED_LIBRARY
41 IMPLEMENT_CLASS(wxPrinterDC, wxDC)
42 #endif
43
44 // This form is deprecated
45 wxPrinterDC::wxPrinterDC(const wxString& driver_name, const wxString& device_name, const wxString& file, bool interactive, int orientation)
46 {
47 m_isInteractive = interactive;
48
49 if (!file.IsNull() && file != "")
50 m_printData.SetFilename(file);
51
52 #if wxUSE_COMMON_DIALOGS
53 if (interactive)
54 {
55 PRINTDLG pd;
56
57 pd.lStructSize = sizeof( PRINTDLG );
58 pd.hwndOwner=(HWND) NULL;
59 pd.hDevMode=(HANDLE)NULL;
60 pd.hDevNames=(HANDLE)NULL;
61 pd.Flags=PD_RETURNDC | PD_NOSELECTION | PD_NOPAGENUMS;
62 pd.nFromPage=0;
63 pd.nToPage=0;
64 pd.nMinPage=0;
65 pd.nMaxPage=0;
66 pd.nCopies=1;
67 pd.hInstance=(HINSTANCE)NULL;
68
69 if ( PrintDlg( &pd ) != 0 )
70 {
71 m_hDC = (WXHDC) pd.hDC;
72 m_ok = TRUE;
73 }
74 else
75 {
76 m_ok = FALSE;
77 return;
78 }
79
80 // m_dontDelete = TRUE;
81 }
82 else
83 #endif
84 if ((!driver_name.IsNull() && driver_name != "") &&
85 (!device_name.IsNull() && device_name != "") &&
86 (!file.IsNull() && file != ""))
87 {
88 m_hDC = (WXHDC) CreateDC((char *) (const char *) driver_name, (char *) (const char *) device_name, (char *) (const char *) file, NULL);
89 m_ok = m_hDC ? TRUE: FALSE;
90 }
91 else
92 {
93 wxPrintData printData;
94 printData.SetOrientation(orientation);
95 m_hDC = wxGetPrinterDC(printData);
96 m_ok = m_hDC ? TRUE: FALSE;
97 }
98
99 if (m_hDC)
100 {
101 // int width = GetDeviceCaps(m_hDC, VERTRES);
102 // int height = GetDeviceCaps(m_hDC, HORZRES);
103 SetMapMode(wxMM_TEXT);
104 }
105 SetBrush(*wxBLACK_BRUSH);
106 SetPen(*wxBLACK_PEN);
107 }
108
109 wxPrinterDC::wxPrinterDC(const wxPrintData& printData)
110 {
111 m_printData = printData;
112
113 m_isInteractive = FALSE;
114
115 m_hDC = wxGetPrinterDC(printData);
116 m_ok = (m_hDC != 0);
117
118 if (m_hDC)
119 SetMapMode(wxMM_TEXT);
120
121 SetBrush(*wxBLACK_BRUSH);
122 SetPen(*wxBLACK_PEN);
123 }
124
125
126 wxPrinterDC::wxPrinterDC(WXHDC theDC)
127 {
128 m_isInteractive = FALSE;
129
130 m_hDC = theDC;
131 m_ok = TRUE;
132 if (m_hDC)
133 {
134 // int width = GetDeviceCaps(m_hDC, VERTRES);
135 // int height = GetDeviceCaps(m_hDC, HORZRES);
136 SetMapMode(wxMM_TEXT);
137 }
138 SetBrush(*wxBLACK_BRUSH);
139 SetPen(*wxBLACK_PEN);
140 }
141
142 wxPrinterDC::~wxPrinterDC(void)
143 {
144 }
145
146 bool wxPrinterDC::StartDoc(const wxString& message)
147 {
148 DOCINFO docinfo;
149 docinfo.cbSize = sizeof(DOCINFO);
150 docinfo.lpszDocName = (const char *)message;
151
152 wxString filename(m_printData.GetFilename());
153
154 if (filename.IsEmpty())
155 docinfo.lpszOutput = NULL;
156 else
157 docinfo.lpszOutput = (const char *) filename;
158
159 #if defined(__WIN95__)
160 docinfo.lpszDatatype = NULL;
161 docinfo.fwType = 0;
162 #endif
163
164 if (!m_hDC)
165 return FALSE;
166
167 int ret =
168 #ifndef __WIN32__
169 ::StartDoc((HDC) m_hDC, &docinfo);
170 #else
171 #ifdef UNICODE
172 ::StartDocW((HDC) m_hDC, &docinfo);
173 #else
174 #ifdef __TWIN32__
175 ::StartDoc((HDC) m_hDC, &docinfo);
176 #else
177 ::StartDocA((HDC) m_hDC, &docinfo);
178 #endif
179 #endif
180 #endif
181
182 #ifndef __WIN16__
183 if (ret <= 0)
184 {
185 DWORD lastError = GetLastError();
186 wxLogDebug("wxDC::StartDoc failed with error: %d\n", lastError);
187 }
188 #endif
189
190 return (ret > 0);
191 }
192
193 void wxPrinterDC::EndDoc(void)
194 {
195 if (m_hDC) ::EndDoc((HDC) m_hDC);
196 }
197
198 void wxPrinterDC::StartPage(void)
199 {
200 if (m_hDC)
201 ::StartPage((HDC) m_hDC);
202 }
203
204 void wxPrinterDC::EndPage(void)
205 {
206 if (m_hDC)
207 ::EndPage((HDC) m_hDC);
208 }
209
210 // Returns default device and port names
211 static bool wxGetDefaultDeviceName(wxString& deviceName, wxString& portName)
212 {
213 deviceName = "";
214
215 LPDEVNAMES lpDevNames;
216 LPSTR lpszDriverName;
217 LPSTR lpszDeviceName;
218 LPSTR lpszPortName;
219
220 PRINTDLG pd;
221
222 // Cygwin has trouble believing PRINTDLG is 66 bytes - thinks it is 68
223 #ifdef __GNUWIN32__
224 pd.lStructSize = 66; // sizeof(PRINTDLG);
225 #else
226 pd.lStructSize = sizeof(PRINTDLG);
227 #endif
228
229 pd.hwndOwner = (HWND)NULL;
230 pd.hDevMode = NULL; // Will be created by PrintDlg
231 pd.hDevNames = NULL; // Ditto
232 pd.Flags = PD_RETURNDEFAULT;
233 pd.nCopies = 1;
234
235 if (!PrintDlg((LPPRINTDLG)&pd))
236 {
237 if ( pd.hDevMode )
238 GlobalFree(pd.hDevMode);
239 if (pd.hDevNames)
240 GlobalFree(pd.hDevNames);
241
242 return FALSE;
243 }
244
245 if (pd.hDevNames)
246 {
247 lpDevNames = (LPDEVNAMES)GlobalLock(pd.hDevNames);
248 lpszDriverName = (LPSTR)lpDevNames + lpDevNames->wDriverOffset;
249 lpszDeviceName = (LPSTR)lpDevNames + lpDevNames->wDeviceOffset;
250 lpszPortName = (LPSTR)lpDevNames + lpDevNames->wOutputOffset;
251 GlobalUnlock(pd.hDevNames);
252 GlobalFree(pd.hDevNames);
253 pd.hDevNames=NULL;
254
255 deviceName = lpszDeviceName;
256 portName = lpszPortName;
257 }
258
259 if (pd.hDevMode)
260 {
261 GlobalFree(pd.hDevMode);
262 pd.hDevMode=NULL;
263 }
264 return ( deviceName != "" );
265 }
266
267 #if 0
268 // This uses defaults, except for orientation, so we should eliminate this function
269 // and use the 2nd form (passing wxPrintData) instead.
270 WXHDC wxGetPrinterDC(int orientation)
271 {
272 HDC hDC;
273 LPDEVMODE lpDevMode = NULL;
274 LPDEVNAMES lpDevNames;
275 LPSTR lpszDriverName;
276 LPSTR lpszDeviceName;
277 LPSTR lpszPortName;
278
279 PRINTDLG pd;
280 // __GNUWIN32__ has trouble believing PRINTDLG is 66 bytes - thinks it is 68
281 #ifdef __GNUWIN32__
282 pd.lStructSize = 66; // sizeof(PRINTDLG);
283 #else
284 pd.lStructSize = sizeof(PRINTDLG);
285 #endif
286 pd.hwndOwner = (HWND)NULL;
287 pd.hDevMode = NULL; // Will be created by PrintDlg
288 pd.hDevNames = NULL; // Ditto
289 pd.Flags = PD_RETURNDEFAULT;
290 pd.nCopies = 1;
291
292 if (!PrintDlg((LPPRINTDLG)&pd))
293 {
294 if ( pd.hDevMode )
295 GlobalFree(pd.hDevMode);
296 if (pd.hDevNames)
297 GlobalFree(pd.hDevNames);
298
299 return(0);
300 }
301
302 if (!pd.hDevNames)
303 {
304 if ( pd.hDevMode )
305 GlobalFree(pd.hDevMode);
306 }
307
308 lpDevNames = (LPDEVNAMES)GlobalLock(pd.hDevNames);
309 lpszDriverName = (LPSTR)lpDevNames + lpDevNames->wDriverOffset;
310 lpszDeviceName = (LPSTR)lpDevNames + lpDevNames->wDeviceOffset;
311 lpszPortName = (LPSTR)lpDevNames + lpDevNames->wOutputOffset;
312 GlobalUnlock(pd.hDevNames);
313
314 if ( pd.hDevMode )
315 {
316 lpDevMode = (DEVMODE*) GlobalLock(pd.hDevMode);
317 lpDevMode->dmOrientation = orientation;
318 lpDevMode->dmFields |= DM_ORIENTATION;
319 }
320
321 #ifdef __WIN32__
322 hDC = CreateDC(lpszDriverName, lpszDeviceName, lpszPortName, (DEVMODE *)lpDevMode);
323 #else
324 hDC = CreateDC(lpszDriverName, lpszDeviceName, lpszPortName, (LPSTR)lpDevMode);
325 #endif
326
327 if (pd.hDevMode && lpDevMode)
328 GlobalUnlock(pd.hDevMode);
329
330 if (pd.hDevNames)
331 {
332 GlobalFree(pd.hDevNames);
333 pd.hDevNames=NULL;
334 }
335 if (pd.hDevMode)
336 {
337 GlobalFree(pd.hDevMode);
338 pd.hDevMode=NULL;
339 }
340 return (WXHDC) hDC;
341 }
342 #endif
343
344 // Gets an HDC for the specified printer configuration
345 WXHDC WXDLLEXPORT wxGetPrinterDC(const wxPrintData& printDataConst)
346 {
347 wxPrintData printData = printDataConst;
348 printData.ConvertToNative();
349
350 char* driverName = (char*) NULL;
351
352 wxString devNameStr = printData.GetPrinterName();
353 char* deviceName;
354 char* portName = (char*) NULL; // Obsolete in WIN32
355
356 if (devNameStr == "")
357 deviceName = (char*) NULL;
358 else
359 deviceName = (char*) (const char*) devNameStr;
360
361 LPDEVMODE lpDevMode = (LPDEVMODE) NULL;
362
363 HGLOBAL hDevMode = (HGLOBAL) printData.GetNativeData();
364
365 if ( hDevMode )
366 lpDevMode = (DEVMODE*) GlobalLock(hDevMode);
367
368 if (devNameStr == "")
369 {
370 // Retrieve the default device name
371 wxString portName;
372 bool ret = wxGetDefaultDeviceName(devNameStr, portName);
373
374 wxASSERT_MSG( ret, "Could not get default device name." );
375
376 deviceName = (char*) (const char*) devNameStr;
377 }
378
379 #ifdef __WIN32__
380 HDC hDC = CreateDC(driverName, deviceName, portName, (DEVMODE *) lpDevMode);
381 #else
382 HDC hDC = CreateDC(driverName, deviceName, portName, (LPSTR) lpDevMode);
383 #endif
384
385 if (hDevMode && lpDevMode)
386 GlobalUnlock(hDevMode);
387
388 return (WXHDC) hDC;
389 }
390