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