]>
Commit | Line | Data |
---|---|---|
2bda0e17 KB |
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__ | |
2bda0e17 KB |
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 | ||
0c589ad0 | 26 | #include "wx/string.h" |
6776a0b2 | 27 | #include "wx/log.h" |
0c589ad0 | 28 | #include "wx/window.h" |
42e69d6b | 29 | #include "wx/msw/private.h" |
0c589ad0 BM |
30 | #include "wx/dcprint.h" |
31 | #include "math.h" | |
2bda0e17 | 32 | |
c455ab93 | 33 | #if wxUSE_COMMON_DIALOGS || defined(__WXWINE__) |
2bda0e17 KB |
34 | #include <commdlg.h> |
35 | #endif | |
36 | ||
37 | #ifndef __WIN32__ | |
38 | #include <print.h> | |
39 | #endif | |
40 | ||
2bda0e17 | 41 | IMPLEMENT_CLASS(wxPrinterDC, wxDC) |
2bda0e17 | 42 | |
7bcb11d3 | 43 | // This form is deprecated |
debe6624 | 44 | wxPrinterDC::wxPrinterDC(const wxString& driver_name, const wxString& device_name, const wxString& file, bool interactive, int orientation) |
2bda0e17 | 45 | { |
7bcb11d3 | 46 | m_isInteractive = interactive; |
a17e237f | 47 | |
223d09f6 | 48 | if (!file.IsNull() && file != wxT("")) |
7bcb11d3 | 49 | m_printData.SetFilename(file); |
a17e237f | 50 | |
47d67540 | 51 | #if wxUSE_COMMON_DIALOGS |
7bcb11d3 JS |
52 | if (interactive) |
53 | { | |
54 | PRINTDLG pd; | |
a17e237f | 55 | |
7bcb11d3 JS |
56 | pd.lStructSize = sizeof( PRINTDLG ); |
57 | pd.hwndOwner=(HWND) NULL; | |
58 | pd.hDevMode=(HANDLE)NULL; | |
59 | pd.hDevNames=(HANDLE)NULL; | |
60 | pd.Flags=PD_RETURNDC | PD_NOSELECTION | PD_NOPAGENUMS; | |
61 | pd.nFromPage=0; | |
62 | pd.nToPage=0; | |
63 | pd.nMinPage=0; | |
64 | pd.nMaxPage=0; | |
65 | pd.nCopies=1; | |
66 | pd.hInstance=(HINSTANCE)NULL; | |
a17e237f | 67 | |
7bcb11d3 JS |
68 | if ( PrintDlg( &pd ) != 0 ) |
69 | { | |
70 | m_hDC = (WXHDC) pd.hDC; | |
71 | m_ok = TRUE; | |
72 | } | |
73 | else | |
74 | { | |
75 | m_ok = FALSE; | |
76 | return; | |
77 | } | |
a17e237f | 78 | |
7bcb11d3 JS |
79 | // m_dontDelete = TRUE; |
80 | } | |
81 | else | |
82 | #endif | |
223d09f6 KB |
83 | if ((!driver_name.IsNull() && driver_name != wxT("")) && |
84 | (!device_name.IsNull() && device_name != wxT("")) && | |
85 | (!file.IsNull() && file != wxT(""))) | |
7bcb11d3 | 86 | { |
837e5743 | 87 | m_hDC = (WXHDC) CreateDC(WXSTRINGCAST driver_name, WXSTRINGCAST device_name, WXSTRINGCAST file, NULL); |
7bcb11d3 JS |
88 | m_ok = m_hDC ? TRUE: FALSE; |
89 | } | |
90 | else | |
91 | { | |
92 | wxPrintData printData; | |
93 | printData.SetOrientation(orientation); | |
94 | m_hDC = wxGetPrinterDC(printData); | |
95 | m_ok = m_hDC ? TRUE: FALSE; | |
96 | } | |
a17e237f | 97 | |
7bcb11d3 JS |
98 | if (m_hDC) |
99 | { | |
100 | // int width = GetDeviceCaps(m_hDC, VERTRES); | |
101 | // int height = GetDeviceCaps(m_hDC, HORZRES); | |
102 | SetMapMode(wxMM_TEXT); | |
103 | } | |
104 | SetBrush(*wxBLACK_BRUSH); | |
105 | SetPen(*wxBLACK_PEN); | |
106 | } | |
107 | ||
108 | wxPrinterDC::wxPrinterDC(const wxPrintData& printData) | |
109 | { | |
110 | m_printData = printData; | |
111 | ||
112 | m_isInteractive = FALSE; | |
113 | ||
114 | m_hDC = wxGetPrinterDC(printData); | |
115 | m_ok = (m_hDC != 0); | |
a17e237f | 116 | |
7bcb11d3 JS |
117 | if (m_hDC) |
118 | SetMapMode(wxMM_TEXT); | |
a17e237f | 119 | |
7bcb11d3 JS |
120 | SetBrush(*wxBLACK_BRUSH); |
121 | SetPen(*wxBLACK_PEN); | |
2bda0e17 KB |
122 | } |
123 | ||
7bcb11d3 | 124 | |
2bda0e17 KB |
125 | wxPrinterDC::wxPrinterDC(WXHDC theDC) |
126 | { | |
7bcb11d3 | 127 | m_isInteractive = FALSE; |
a17e237f | 128 | |
7bcb11d3 JS |
129 | m_hDC = theDC; |
130 | m_ok = TRUE; | |
131 | if (m_hDC) | |
132 | { | |
133 | // int width = GetDeviceCaps(m_hDC, VERTRES); | |
134 | // int height = GetDeviceCaps(m_hDC, HORZRES); | |
135 | SetMapMode(wxMM_TEXT); | |
136 | } | |
137 | SetBrush(*wxBLACK_BRUSH); | |
138 | SetPen(*wxBLACK_PEN); | |
2bda0e17 KB |
139 | } |
140 | ||
141 | wxPrinterDC::~wxPrinterDC(void) | |
142 | { | |
143 | } | |
144 | ||
7bcb11d3 JS |
145 | bool wxPrinterDC::StartDoc(const wxString& message) |
146 | { | |
147 | DOCINFO docinfo; | |
148 | docinfo.cbSize = sizeof(DOCINFO); | |
837e5743 | 149 | docinfo.lpszDocName = (const wxChar*)message; |
7bcb11d3 JS |
150 | |
151 | wxString filename(m_printData.GetFilename()); | |
152 | ||
153 | if (filename.IsEmpty()) | |
154 | docinfo.lpszOutput = NULL; | |
155 | else | |
837e5743 | 156 | docinfo.lpszOutput = (const wxChar *) filename; |
7bcb11d3 JS |
157 | |
158 | #if defined(__WIN95__) | |
159 | docinfo.lpszDatatype = NULL; | |
160 | docinfo.fwType = 0; | |
161 | #endif | |
a17e237f | 162 | |
7bcb11d3 JS |
163 | if (!m_hDC) |
164 | return FALSE; | |
a17e237f | 165 | |
7bcb11d3 JS |
166 | int ret = |
167 | #ifndef __WIN32__ | |
168 | ::StartDoc((HDC) m_hDC, &docinfo); | |
169 | #else | |
170 | #ifdef UNICODE | |
171 | ::StartDocW((HDC) m_hDC, &docinfo); | |
172 | #else | |
173 | #ifdef __TWIN32__ | |
174 | ::StartDoc((HDC) m_hDC, &docinfo); | |
175 | #else | |
176 | ::StartDocA((HDC) m_hDC, &docinfo); | |
177 | #endif | |
178 | #endif | |
179 | #endif | |
a17e237f | 180 | |
7bcb11d3 JS |
181 | #ifndef __WIN16__ |
182 | if (ret <= 0) | |
183 | { | |
184 | DWORD lastError = GetLastError(); | |
223d09f6 | 185 | wxLogDebug(wxT("wxDC::StartDoc failed with error: %d\n"), lastError); |
7bcb11d3 JS |
186 | } |
187 | #endif | |
a17e237f | 188 | |
7bcb11d3 JS |
189 | return (ret > 0); |
190 | } | |
191 | ||
192 | void wxPrinterDC::EndDoc(void) | |
193 | { | |
194 | if (m_hDC) ::EndDoc((HDC) m_hDC); | |
195 | } | |
196 | ||
197 | void wxPrinterDC::StartPage(void) | |
198 | { | |
199 | if (m_hDC) | |
200 | ::StartPage((HDC) m_hDC); | |
201 | } | |
202 | ||
203 | void wxPrinterDC::EndPage(void) | |
204 | { | |
205 | if (m_hDC) | |
206 | ::EndPage((HDC) m_hDC); | |
207 | } | |
208 | ||
209 | // Returns default device and port names | |
210 | static bool wxGetDefaultDeviceName(wxString& deviceName, wxString& portName) | |
211 | { | |
212 | deviceName = ""; | |
213 | ||
214 | LPDEVNAMES lpDevNames; | |
215 | LPSTR lpszDriverName; | |
216 | LPSTR lpszDeviceName; | |
217 | LPSTR lpszPortName; | |
a17e237f | 218 | |
7bcb11d3 JS |
219 | PRINTDLG pd; |
220 | ||
221 | // Cygwin has trouble believing PRINTDLG is 66 bytes - thinks it is 68 | |
222 | #ifdef __GNUWIN32__ | |
223 | pd.lStructSize = 66; // sizeof(PRINTDLG); | |
224 | #else | |
225 | pd.lStructSize = sizeof(PRINTDLG); | |
226 | #endif | |
227 | ||
228 | pd.hwndOwner = (HWND)NULL; | |
229 | pd.hDevMode = NULL; // Will be created by PrintDlg | |
230 | pd.hDevNames = NULL; // Ditto | |
231 | pd.Flags = PD_RETURNDEFAULT; | |
232 | pd.nCopies = 1; | |
a17e237f | 233 | |
7bcb11d3 JS |
234 | if (!PrintDlg((LPPRINTDLG)&pd)) |
235 | { | |
236 | if ( pd.hDevMode ) | |
237 | GlobalFree(pd.hDevMode); | |
238 | if (pd.hDevNames) | |
239 | GlobalFree(pd.hDevNames); | |
a17e237f | 240 | |
7bcb11d3 JS |
241 | return FALSE; |
242 | } | |
a17e237f | 243 | |
7bcb11d3 JS |
244 | if (pd.hDevNames) |
245 | { | |
246 | lpDevNames = (LPDEVNAMES)GlobalLock(pd.hDevNames); | |
247 | lpszDriverName = (LPSTR)lpDevNames + lpDevNames->wDriverOffset; | |
248 | lpszDeviceName = (LPSTR)lpDevNames + lpDevNames->wDeviceOffset; | |
249 | lpszPortName = (LPSTR)lpDevNames + lpDevNames->wOutputOffset; | |
7bcb11d3 JS |
250 | |
251 | deviceName = lpszDeviceName; | |
252 | portName = lpszPortName; | |
60fe7303 JS |
253 | |
254 | GlobalUnlock(pd.hDevNames); | |
255 | GlobalFree(pd.hDevNames); | |
256 | pd.hDevNames=NULL; | |
7bcb11d3 | 257 | } |
a17e237f | 258 | |
7bcb11d3 JS |
259 | if (pd.hDevMode) |
260 | { | |
261 | GlobalFree(pd.hDevMode); | |
262 | pd.hDevMode=NULL; | |
263 | } | |
223d09f6 | 264 | return ( deviceName != wxT("") ); |
7bcb11d3 JS |
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. | |
2bda0e17 KB |
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; | |
a17e237f | 278 | |
2bda0e17 KB |
279 | PRINTDLG pd; |
280 | // __GNUWIN32__ has trouble believing PRINTDLG is 66 bytes - thinks it is 68 | |
7bcb11d3 | 281 | #ifdef __GNUWIN32__ |
2bda0e17 | 282 | pd.lStructSize = 66; // sizeof(PRINTDLG); |
7bcb11d3 JS |
283 | #else |
284 | pd.lStructSize = sizeof(PRINTDLG); | |
285 | #endif | |
2bda0e17 KB |
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; | |
a17e237f | 291 | |
2bda0e17 KB |
292 | if (!PrintDlg((LPPRINTDLG)&pd)) |
293 | { | |
294 | if ( pd.hDevMode ) | |
295 | GlobalFree(pd.hDevMode); | |
296 | if (pd.hDevNames) | |
297 | GlobalFree(pd.hDevNames); | |
a17e237f | 298 | |
2bda0e17 KB |
299 | return(0); |
300 | } | |
a17e237f | 301 | |
2bda0e17 KB |
302 | if (!pd.hDevNames) |
303 | { | |
304 | if ( pd.hDevMode ) | |
305 | GlobalFree(pd.hDevMode); | |
306 | } | |
a17e237f | 307 | |
2bda0e17 KB |
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); | |
a17e237f | 313 | |
2bda0e17 KB |
314 | if ( pd.hDevMode ) |
315 | { | |
316 | lpDevMode = (DEVMODE*) GlobalLock(pd.hDevMode); | |
317 | lpDevMode->dmOrientation = orientation; | |
318 | lpDevMode->dmFields |= DM_ORIENTATION; | |
319 | } | |
a17e237f | 320 | |
2bda0e17 KB |
321 | #ifdef __WIN32__ |
322 | hDC = CreateDC(lpszDriverName, lpszDeviceName, lpszPortName, (DEVMODE *)lpDevMode); | |
323 | #else | |
324 | hDC = CreateDC(lpszDriverName, lpszDeviceName, lpszPortName, (LPSTR)lpDevMode); | |
325 | #endif | |
a17e237f | 326 | |
2bda0e17 KB |
327 | if (pd.hDevMode && lpDevMode) |
328 | GlobalUnlock(pd.hDevMode); | |
a17e237f | 329 | |
2bda0e17 KB |
330 | if (pd.hDevNames) |
331 | { | |
7bcb11d3 JS |
332 | GlobalFree(pd.hDevNames); |
333 | pd.hDevNames=NULL; | |
2bda0e17 KB |
334 | } |
335 | if (pd.hDevMode) | |
336 | { | |
337 | GlobalFree(pd.hDevMode); | |
338 | pd.hDevMode=NULL; | |
339 | } | |
340 | return (WXHDC) hDC; | |
341 | } | |
7bcb11d3 JS |
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(); | |
a17e237f | 349 | |
837e5743 | 350 | wxChar* driverName = (wxChar*) NULL; |
a17e237f | 351 | |
7bcb11d3 | 352 | wxString devNameStr = printData.GetPrinterName(); |
837e5743 OK |
353 | wxChar* deviceName; |
354 | wxChar* portName = (wxChar*) NULL; // Obsolete in WIN32 | |
a17e237f | 355 | |
223d09f6 | 356 | if (devNameStr == wxT("")) |
837e5743 | 357 | deviceName = (wxChar*) NULL; |
7bcb11d3 | 358 | else |
837e5743 | 359 | deviceName = WXSTRINGCAST devNameStr; |
7bcb11d3 JS |
360 | |
361 | LPDEVMODE lpDevMode = (LPDEVMODE) NULL; | |
362 | ||
48c12cb1 | 363 | HGLOBAL hDevMode = (HGLOBAL)(DWORD) printData.GetNativeData(); |
7bcb11d3 JS |
364 | |
365 | if ( hDevMode ) | |
366 | lpDevMode = (DEVMODE*) GlobalLock(hDevMode); | |
367 | ||
223d09f6 | 368 | if (devNameStr == wxT("")) |
7bcb11d3 JS |
369 | { |
370 | // Retrieve the default device name | |
371 | wxString portName; | |
a17e237f VZ |
372 | #ifdef __WXDEBUG__ |
373 | bool ret = | |
374 | #else // !Debug | |
375 | (void) | |
376 | #endif // Debug/Release | |
377 | wxGetDefaultDeviceName(devNameStr, portName); | |
7bcb11d3 | 378 | |
223d09f6 | 379 | wxASSERT_MSG( ret, wxT("Could not get default device name.") ); |
2bda0e17 | 380 | |
837e5743 | 381 | deviceName = WXSTRINGCAST devNameStr; |
7bcb11d3 | 382 | } |
a17e237f | 383 | |
7bcb11d3 JS |
384 | #ifdef __WIN32__ |
385 | HDC hDC = CreateDC(driverName, deviceName, portName, (DEVMODE *) lpDevMode); | |
386 | #else | |
387 | HDC hDC = CreateDC(driverName, deviceName, portName, (LPSTR) lpDevMode); | |
388 | #endif | |
a17e237f | 389 | |
7bcb11d3 JS |
390 | if (hDevMode && lpDevMode) |
391 | GlobalUnlock(hDevMode); | |
a17e237f | 392 | |
7bcb11d3 JS |
393 | return (WXHDC) hDC; |
394 | } | |
2bda0e17 | 395 |