]>
Commit | Line | Data |
---|---|---|
2bda0e17 | 1 | ///////////////////////////////////////////////////////////////////////////// |
7520f3da | 2 | // Name: src/msw/printwin.cpp |
2bda0e17 KB |
3 | // Purpose: wxWindowsPrinter framework |
4 | // Author: Julian Smart | |
5 | // Modified by: | |
6 | // Created: 04/01/98 | |
7 | // RCS-ID: $Id$ | |
6c9a19aa | 8 | // Copyright: (c) Julian Smart |
65571936 | 9 | // Licence: wxWindows licence |
2bda0e17 KB |
10 | ///////////////////////////////////////////////////////////////////////////// |
11 | ||
103aec29 VZ |
12 | // =========================================================================== |
13 | // declarations | |
14 | // =========================================================================== | |
15 | ||
16 | // --------------------------------------------------------------------------- | |
17 | // headers | |
18 | // --------------------------------------------------------------------------- | |
19 | ||
2bda0e17 KB |
20 | // For compilers that support precompilation, includes "wx.h". |
21 | #include "wx/wxprec.h" | |
22 | ||
23 | #ifdef __BORLANDC__ | |
103aec29 | 24 | #pragma hdrstop |
2bda0e17 KB |
25 | #endif |
26 | ||
12bdd77c JS |
27 | // Don't use the Windows printer if we're in wxUniv mode and using |
28 | // the PostScript architecture | |
29 | #if wxUSE_PRINTING_ARCHITECTURE && (!defined(__WXUNIVERSAL__) || !wxUSE_POSTSCRIPT_ARCHITECTURE_IN_MSW) | |
f6bcfd97 | 30 | |
2bda0e17 | 31 | #ifndef WX_PRECOMP |
57bd4c60 | 32 | #include "wx/msw/wrapcdlg.h" |
0c589ad0 BM |
33 | #include "wx/window.h" |
34 | #include "wx/msw/private.h" | |
103aec29 VZ |
35 | #include "wx/utils.h" |
36 | #include "wx/dc.h" | |
37 | #include "wx/app.h" | |
38 | #include "wx/msgdlg.h" | |
0c589ad0 | 39 | #include "wx/intl.h" |
e4db172a | 40 | #include "wx/log.h" |
6d50343d | 41 | #include "wx/dcprint.h" |
25a3fca2 VS |
42 | #include "wx/dcmemory.h" |
43 | #include "wx/image.h" | |
2bda0e17 KB |
44 | #endif |
45 | ||
25a3fca2 VS |
46 | #include "wx/msw/dib.h" |
47 | #include "wx/msw/dcmemory.h" | |
2bda0e17 | 48 | #include "wx/msw/printwin.h" |
f4322df6 | 49 | #include "wx/msw/printdlg.h" |
2bda0e17 | 50 | #include "wx/msw/private.h" |
888dde65 | 51 | #include "wx/msw/dcprint.h" |
a5c1223d | 52 | #include "wx/msw/enhmeta.h" |
2bda0e17 KB |
53 | |
54 | #include <stdlib.h> | |
2bda0e17 | 55 | |
103aec29 VZ |
56 | // --------------------------------------------------------------------------- |
57 | // private functions | |
58 | // --------------------------------------------------------------------------- | |
59 | ||
5009cb6a | 60 | BOOL CALLBACK wxAbortProc(HDC hdc, int error); |
2bda0e17 | 61 | |
103aec29 VZ |
62 | // --------------------------------------------------------------------------- |
63 | // wxWin macros | |
64 | // --------------------------------------------------------------------------- | |
65 | ||
103aec29 VZ |
66 | IMPLEMENT_DYNAMIC_CLASS(wxWindowsPrinter, wxPrinterBase) |
67 | IMPLEMENT_CLASS(wxWindowsPrintPreview, wxPrintPreviewBase) | |
2bda0e17 | 68 | |
103aec29 VZ |
69 | // =========================================================================== |
70 | // implementation | |
71 | // =========================================================================== | |
72 | ||
73 | // --------------------------------------------------------------------------- | |
74 | // Printer | |
75 | // --------------------------------------------------------------------------- | |
7bcb11d3 | 76 | |
103aec29 VZ |
77 | wxWindowsPrinter::wxWindowsPrinter(wxPrintDialogData *data) |
78 | : wxPrinterBase(data) | |
2bda0e17 | 79 | { |
2bda0e17 KB |
80 | } |
81 | ||
82 | bool wxWindowsPrinter::Print(wxWindow *parent, wxPrintout *printout, bool prompt) | |
83 | { | |
e41e579f | 84 | sm_abortIt = false; |
7bcb11d3 | 85 | sm_abortWindow = NULL; |
103aec29 | 86 | |
7bcb11d3 | 87 | if (!printout) |
f6bcfd97 BP |
88 | { |
89 | sm_lastError = wxPRINTER_ERROR; | |
e41e579f | 90 | return false; |
f6bcfd97 | 91 | } |
103aec29 | 92 | |
d2b354f9 JS |
93 | if (m_printDialogData.GetMinPage() < 1) |
94 | m_printDialogData.SetMinPage(1); | |
95 | if (m_printDialogData.GetMaxPage() < 1) | |
96 | m_printDialogData.SetMaxPage(9999); | |
2bda0e17 | 97 | |
103aec29 | 98 | // Create a suitable device context |
f415cab9 | 99 | wxPrinterDC *dc wxDUMMY_INITIALIZE(NULL); |
7bcb11d3 JS |
100 | if (prompt) |
101 | { | |
f415cab9 | 102 | dc = wxDynamicCast(PrintDialog(parent), wxPrinterDC); |
7bcb11d3 | 103 | if (!dc) |
e41e579f | 104 | return false; |
7bcb11d3 JS |
105 | } |
106 | else | |
107 | { | |
7bcb11d3 JS |
108 | dc = new wxPrinterDC(m_printDialogData.GetPrintData()); |
109 | } | |
103aec29 | 110 | |
7bcb11d3 | 111 | // May have pressed cancel. |
888dde65 | 112 | if (!dc || !dc->IsOk()) |
7bcb11d3 JS |
113 | { |
114 | if (dc) delete dc; | |
e41e579f | 115 | return false; |
7bcb11d3 | 116 | } |
103aec29 | 117 | |
888dde65 RR |
118 | wxPrinterDCImpl *impl = (wxPrinterDCImpl*) dc->GetImpl(); |
119 | ||
7bcb11d3 | 120 | HDC hdc = ::GetDC(NULL); |
5cb598ae WS |
121 | int logPPIScreenX = ::GetDeviceCaps(hdc, LOGPIXELSX); |
122 | int logPPIScreenY = ::GetDeviceCaps(hdc, LOGPIXELSY); | |
7bcb11d3 | 123 | ::ReleaseDC(NULL, hdc); |
103aec29 | 124 | |
888dde65 RR |
125 | int logPPIPrinterX = ::GetDeviceCaps((HDC) impl->GetHDC(), LOGPIXELSX); |
126 | int logPPIPrinterY = ::GetDeviceCaps((HDC) impl->GetHDC(), LOGPIXELSY); | |
7bcb11d3 JS |
127 | if (logPPIPrinterX == 0 || logPPIPrinterY == 0) |
128 | { | |
129 | delete dc; | |
f6bcfd97 | 130 | sm_lastError = wxPRINTER_ERROR; |
e41e579f | 131 | return false; |
7bcb11d3 | 132 | } |
103aec29 | 133 | |
7bcb11d3 JS |
134 | printout->SetPPIScreen(logPPIScreenX, logPPIScreenY); |
135 | printout->SetPPIPrinter(logPPIPrinterX, logPPIPrinterY); | |
103aec29 VZ |
136 | |
137 | // Set printout parameters | |
7bcb11d3 | 138 | printout->SetDC(dc); |
103aec29 | 139 | |
7bcb11d3 JS |
140 | int w, h; |
141 | dc->GetSize(&w, &h); | |
142 | printout->SetPageSizePixels((int)w, (int)h); | |
f415cab9 | 143 | printout->SetPaperRectPixels(dc->GetPaperRect()); |
d6a1743b | 144 | |
7bcb11d3 JS |
145 | dc->GetSizeMM(&w, &h); |
146 | printout->SetPageSizeMM((int)w, (int)h); | |
103aec29 | 147 | |
7bcb11d3 | 148 | // Create an abort window |
0f07e3dc | 149 | wxBusyCursor busyCursor; |
103aec29 | 150 | |
1044a386 JS |
151 | printout->OnPreparePrinting(); |
152 | ||
d2b354f9 JS |
153 | // Get some parameters from the printout, if defined |
154 | int fromPage, toPage; | |
155 | int minPage, maxPage; | |
156 | printout->GetPageInfo(&minPage, &maxPage, &fromPage, &toPage); | |
157 | ||
158 | if (maxPage == 0) | |
159 | { | |
160 | sm_lastError = wxPRINTER_ERROR; | |
e41e579f | 161 | return false; |
d2b354f9 JS |
162 | } |
163 | ||
164 | // Only set min and max, because from and to have been | |
165 | // set by the user | |
166 | m_printDialogData.SetMinPage(minPage); | |
167 | m_printDialogData.SetMaxPage(maxPage); | |
168 | ||
7bcb11d3 JS |
169 | wxWindow *win = CreateAbortWindow(parent, printout); |
170 | wxYield(); | |
103aec29 | 171 | |
5009cb6a | 172 | ::SetAbortProc(GetHdcOf(*impl), wxAbortProc); |
103aec29 | 173 | |
7bcb11d3 | 174 | if (!win) |
2bda0e17 | 175 | { |
223d09f6 | 176 | wxLogDebug(wxT("Could not create an abort dialog.")); |
f6bcfd97 | 177 | sm_lastError = wxPRINTER_ERROR; |
103aec29 | 178 | |
7bcb11d3 | 179 | delete dc; |
e71693c3 | 180 | return false; |
2bda0e17 | 181 | } |
7bcb11d3 | 182 | sm_abortWindow = win; |
e41e579f | 183 | sm_abortWindow->Show(); |
103aec29 VZ |
184 | wxSafeYield(); |
185 | ||
7bcb11d3 | 186 | printout->OnBeginPrinting(); |
103aec29 | 187 | |
f6bcfd97 BP |
188 | sm_lastError = wxPRINTER_NO_ERROR; |
189 | ||
e41e579f DS |
190 | int minPageNum = minPage, maxPageNum = maxPage; |
191 | ||
192 | if ( !m_printDialogData.GetAllPages() ) | |
e41e579f DS |
193 | { |
194 | minPageNum = m_printDialogData.GetFromPage(); | |
195 | maxPageNum = m_printDialogData.GetToPage(); | |
196 | } | |
197 | ||
7bcb11d3 | 198 | int copyCount; |
70846f0a VZ |
199 | for ( copyCount = 1; |
200 | copyCount <= m_printDialogData.GetNoCopies(); | |
201 | copyCount++ ) | |
7bcb11d3 | 202 | { |
e41e579f | 203 | if ( !printout->OnBeginDocument(minPageNum, maxPageNum) ) |
7bcb11d3 | 204 | { |
103aec29 | 205 | wxLogError(_("Could not start printing.")); |
f6bcfd97 | 206 | sm_lastError = wxPRINTER_ERROR; |
7bcb11d3 JS |
207 | break; |
208 | } | |
209 | if (sm_abortIt) | |
f6bcfd97 BP |
210 | { |
211 | sm_lastError = wxPRINTER_CANCELLED; | |
7bcb11d3 | 212 | break; |
f6bcfd97 | 213 | } |
103aec29 | 214 | |
7bcb11d3 | 215 | int pn; |
e41e579f DS |
216 | |
217 | for ( pn = minPageNum; | |
218 | pn <= maxPageNum && printout->HasPage(pn); | |
70846f0a | 219 | pn++ ) |
7bcb11d3 | 220 | { |
70846f0a | 221 | if ( sm_abortIt ) |
7bcb11d3 | 222 | { |
f6bcfd97 | 223 | sm_lastError = wxPRINTER_CANCELLED; |
7bcb11d3 JS |
224 | break; |
225 | } | |
70846f0a VZ |
226 | |
227 | dc->StartPage(); | |
228 | bool cont = printout->OnPrintPage(pn); | |
229 | dc->EndPage(); | |
230 | ||
231 | if ( !cont ) | |
f6bcfd97 BP |
232 | { |
233 | sm_lastError = wxPRINTER_CANCELLED; | |
70846f0a | 234 | break; |
f6bcfd97 | 235 | } |
7bcb11d3 | 236 | } |
70846f0a | 237 | |
7bcb11d3 JS |
238 | printout->OnEndDocument(); |
239 | } | |
103aec29 | 240 | |
7bcb11d3 | 241 | printout->OnEndPrinting(); |
103aec29 | 242 | |
7bcb11d3 | 243 | if (sm_abortWindow) |
2bda0e17 | 244 | { |
e41e579f | 245 | sm_abortWindow->Show(false); |
5276b0a5 | 246 | wxDELETE(sm_abortWindow); |
2bda0e17 | 247 | } |
103aec29 | 248 | |
7bcb11d3 | 249 | delete dc; |
103aec29 | 250 | |
e71693c3 | 251 | return sm_lastError == wxPRINTER_NO_ERROR; |
7bcb11d3 | 252 | } |
2bda0e17 | 253 | |
f415cab9 | 254 | wxDC *wxWindowsPrinter::PrintDialog(wxWindow *parent) |
7bcb11d3 | 255 | { |
d3b9f782 | 256 | wxDC *dc = NULL; |
2bda0e17 | 257 | |
f415cab9 | 258 | wxWindowsPrintDialog dialog(parent, & m_printDialogData); |
7bcb11d3 | 259 | int ret = dialog.ShowModal(); |
2bda0e17 | 260 | |
7bcb11d3 JS |
261 | if (ret == wxID_OK) |
262 | { | |
263 | dc = dialog.GetPrintDC(); | |
264 | m_printDialogData = dialog.GetPrintDialogData(); | |
33ac7e6f | 265 | if (dc == NULL) |
f6bcfd97 BP |
266 | sm_lastError = wxPRINTER_ERROR; |
267 | else | |
268 | sm_lastError = wxPRINTER_NO_ERROR; | |
7bcb11d3 | 269 | } |
f6bcfd97 BP |
270 | else |
271 | sm_lastError = wxPRINTER_CANCELLED; | |
2bda0e17 | 272 | |
7bcb11d3 | 273 | return dc; |
2bda0e17 KB |
274 | } |
275 | ||
a12f001e | 276 | bool wxWindowsPrinter::Setup(wxWindow *WXUNUSED(parent)) |
2bda0e17 | 277 | { |
b45dfd0a RR |
278 | #if 0 |
279 | // We no longer expose that dialog | |
7bcb11d3 | 280 | wxPrintDialog dialog(parent, & m_printDialogData); |
e41e579f | 281 | dialog.GetPrintDialogData().SetSetupDialog(true); |
7bcb11d3 JS |
282 | |
283 | int ret = dialog.ShowModal(); | |
284 | ||
285 | if (ret == wxID_OK) | |
286 | { | |
287 | m_printDialogData = dialog.GetPrintDialogData(); | |
288 | } | |
289 | ||
290 | return (ret == wxID_OK); | |
b45dfd0a | 291 | #else |
3950f9e1 | 292 | return false; |
b45dfd0a | 293 | #endif |
2bda0e17 KB |
294 | } |
295 | ||
296 | /* | |
7bcb11d3 JS |
297 | * Print preview |
298 | */ | |
2bda0e17 | 299 | |
103aec29 VZ |
300 | wxWindowsPrintPreview::wxWindowsPrintPreview(wxPrintout *printout, |
301 | wxPrintout *printoutForPrinting, | |
302 | wxPrintDialogData *data) | |
303 | : wxPrintPreviewBase(printout, printoutForPrinting, data) | |
2bda0e17 | 304 | { |
7bcb11d3 | 305 | DetermineScaling(); |
2bda0e17 KB |
306 | } |
307 | ||
103aec29 VZ |
308 | wxWindowsPrintPreview::wxWindowsPrintPreview(wxPrintout *printout, |
309 | wxPrintout *printoutForPrinting, | |
310 | wxPrintData *data) | |
311 | : wxPrintPreviewBase(printout, printoutForPrinting, data) | |
312 | { | |
313 | DetermineScaling(); | |
314 | } | |
315 | ||
316 | wxWindowsPrintPreview::~wxWindowsPrintPreview() | |
2bda0e17 KB |
317 | { |
318 | } | |
319 | ||
320 | bool wxWindowsPrintPreview::Print(bool interactive) | |
321 | { | |
7bcb11d3 | 322 | if (!m_printPrintout) |
e41e579f | 323 | return false; |
7bcb11d3 JS |
324 | wxWindowsPrinter printer(&m_printDialogData); |
325 | return printer.Print(m_previewFrame, m_printPrintout, interactive); | |
2bda0e17 KB |
326 | } |
327 | ||
103aec29 | 328 | void wxWindowsPrintPreview::DetermineScaling() |
2bda0e17 | 329 | { |
f2a59080 | 330 | ScreenHDC dc; |
2bda0e17 KB |
331 | int logPPIScreenX = ::GetDeviceCaps(dc, LOGPIXELSX); |
332 | int logPPIScreenY = ::GetDeviceCaps(dc, LOGPIXELSY); | |
34da0970 | 333 | m_previewPrintout->SetPPIScreen(logPPIScreenX, logPPIScreenY); |
103aec29 | 334 | |
2bda0e17 | 335 | // Get a device context for the currently selected printer |
7bcb11d3 | 336 | wxPrinterDC printerDC(m_printDialogData.GetPrintData()); |
103aec29 | 337 | |
f415cab9 JS |
338 | int printerWidthMM; |
339 | int printerHeightMM; | |
340 | int printerXRes; | |
341 | int printerYRes; | |
342 | int logPPIPrinterX; | |
343 | int logPPIPrinterY; | |
344 | ||
f4322df6 | 345 | wxRect paperRect; |
103aec29 | 346 | |
888dde65 | 347 | if ( printerDC.IsOk() ) |
2bda0e17 | 348 | { |
888dde65 RR |
349 | wxPrinterDCImpl *impl = (wxPrinterDCImpl*) printerDC.GetImpl(); |
350 | HDC dc = GetHdcOf(*impl); | |
f415cab9 JS |
351 | printerWidthMM = ::GetDeviceCaps(dc, HORZSIZE); |
352 | printerHeightMM = ::GetDeviceCaps(dc, VERTSIZE); | |
f4fefc23 VZ |
353 | printerXRes = ::GetDeviceCaps(dc, HORZRES); |
354 | printerYRes = ::GetDeviceCaps(dc, VERTRES); | |
f415cab9 JS |
355 | logPPIPrinterX = ::GetDeviceCaps(dc, LOGPIXELSX); |
356 | logPPIPrinterY = ::GetDeviceCaps(dc, LOGPIXELSY); | |
103aec29 | 357 | |
f4322df6 | 358 | paperRect = printerDC.GetPaperRect(); |
103aec29 | 359 | |
f2a59080 VZ |
360 | if ( logPPIPrinterX == 0 || |
361 | logPPIPrinterY == 0 || | |
f415cab9 JS |
362 | printerWidthMM == 0 || |
363 | printerHeightMM == 0 ) | |
f2a59080 | 364 | { |
e41e579f | 365 | m_isOk = false; |
f2a59080 | 366 | } |
2bda0e17 KB |
367 | } |
368 | else | |
f2a59080 | 369 | { |
f415cab9 JS |
370 | // use some defaults |
371 | printerWidthMM = 150; | |
372 | printerHeightMM = 250; | |
373 | printerXRes = 1500; | |
374 | printerYRes = 2500; | |
375 | logPPIPrinterX = 600; | |
376 | logPPIPrinterY = 600; | |
377 | ||
f4322df6 | 378 | paperRect = wxRect(0, 0, printerXRes, printerYRes); |
e41e579f | 379 | m_isOk = false; |
f2a59080 | 380 | } |
34da0970 JS |
381 | m_pageWidth = printerXRes; |
382 | m_pageHeight = printerYRes; | |
f415cab9 JS |
383 | m_previewPrintout->SetPageSizePixels(printerXRes, printerYRes); |
384 | m_previewPrintout->SetPageSizeMM(printerWidthMM, printerHeightMM); | |
385 | m_previewPrintout->SetPaperRectPixels(paperRect); | |
386 | m_previewPrintout->SetPPIPrinter(logPPIPrinterX, logPPIPrinterY); | |
103aec29 | 387 | |
2bda0e17 | 388 | // At 100%, the page should look about page-size on the screen. |
f415cab9 JS |
389 | m_previewScaleX = float(logPPIScreenX) / logPPIPrinterX; |
390 | m_previewScaleY = float(logPPIScreenY) / logPPIPrinterY; | |
2bda0e17 KB |
391 | } |
392 | ||
a5c1223d | 393 | bool wxWindowsPrintPreview::RenderPageIntoBitmap(wxBitmap& bmp, int pageNum) |
25a3fca2 | 394 | { |
a5c1223d VS |
395 | // The preview, as implemented in wxPrintPreviewBase (and as used prior to |
396 | // wx3) is inexact: it uses screen DC, which has much lower resolution and | |
397 | // has other properties different from printer DC, so the preview is not | |
398 | // quite right. | |
25a3fca2 | 399 | // |
a5c1223d VS |
400 | // To make matters worse, if the application depends heavily on |
401 | // GetTextExtent() or does text layout itself, the output in preview and on | |
402 | // paper can be very different. In particular, wxHtmlEasyPrinting is | |
403 | // affected and the preview can be easily off by several pages. | |
25a3fca2 | 404 | // |
a5c1223d VS |
405 | // To fix this, we render the preview into high-resolution enhanced |
406 | // metafile with properties identical to the printer DC. This guarantees | |
407 | // metrics correctness while still being fast. | |
25a3fca2 | 408 | |
25a3fca2 | 409 | |
a5c1223d VS |
410 | // print the preview into a metafile: |
411 | wxPrinterDC printerDC(m_printDialogData.GetPrintData()); | |
412 | wxEnhMetaFileDC metaDC(printerDC, | |
413 | wxEmptyString, | |
414 | printerDC.GetSize().x, printerDC.GetSize().y); | |
25a3fca2 | 415 | |
a5c1223d | 416 | if ( !RenderPageIntoDC(metaDC, pageNum) ) |
25a3fca2 VS |
417 | return false; |
418 | ||
a5c1223d VS |
419 | wxEnhMetaFile *metafile = metaDC.Close(); |
420 | if ( !metafile ) | |
25a3fca2 VS |
421 | return false; |
422 | ||
a5c1223d | 423 | // now render the metafile: |
25a3fca2 VS |
424 | wxMemoryDC bmpDC; |
425 | bmpDC.SelectObject(bmp); | |
426 | bmpDC.Clear(); | |
427 | ||
a5c1223d VS |
428 | wxRect outRect(0, 0, bmp.GetWidth(), bmp.GetHeight()); |
429 | metafile->Play(&bmpDC, &outRect); | |
25a3fca2 | 430 | |
25a3fca2 | 431 | |
a5c1223d | 432 | delete metafile; |
25a3fca2 | 433 | |
a5c1223d | 434 | // TODO: we should keep the metafile and reuse it when changing zoom level |
25a3fca2 VS |
435 | |
436 | return true; | |
437 | } | |
438 | ||
25a3fca2 | 439 | |
5009cb6a | 440 | BOOL CALLBACK wxAbortProc(HDC WXUNUSED(hdc), int WXUNUSED(error)) |
2bda0e17 KB |
441 | { |
442 | MSG msg; | |
103aec29 | 443 | |
34da0970 | 444 | if (!wxPrinterBase::sm_abortWindow) /* If the abort dialog isn't up yet */ |
2bda0e17 | 445 | return(TRUE); |
103aec29 | 446 | |
2bda0e17 | 447 | /* Process messages intended for the abort dialog box */ |
103aec29 | 448 | |
078cf5cb | 449 | while (!wxPrinterBase::sm_abortIt && ::PeekMessage(&msg, 0, 0, 0, TRUE)) |
34da0970 | 450 | if (!IsDialogMessage((HWND) wxPrinterBase::sm_abortWindow->GetHWND(), &msg)) { |
2bda0e17 KB |
451 | TranslateMessage(&msg); |
452 | DispatchMessage(&msg); | |
453 | } | |
103aec29 | 454 | |
e71693c3 | 455 | /* bAbort is TRUE (return is FALSE) if the user has aborted */ |
103aec29 | 456 | |
e71693c3 | 457 | return !wxPrinterBase::sm_abortIt; |
2bda0e17 KB |
458 | } |
459 | ||
f6bcfd97 BP |
460 | #endif |
461 | // wxUSE_PRINTING_ARCHITECTURE |