]> git.saurik.com Git - wxWidgets.git/blame - src/msw/printwin.cpp
wxMSW: return correct value from wxMessageDialog::GetReturnCode().
[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
6c9a19aa 7// Copyright: (c) Julian Smart
65571936 8// Licence: wxWindows licence
2bda0e17
KB
9/////////////////////////////////////////////////////////////////////////////
10
103aec29
VZ
11// ===========================================================================
12// declarations
13// ===========================================================================
14
15// ---------------------------------------------------------------------------
16// headers
17// ---------------------------------------------------------------------------
18
2bda0e17
KB
19// For compilers that support precompilation, includes "wx.h".
20#include "wx/wxprec.h"
21
22#ifdef __BORLANDC__
103aec29 23 #pragma hdrstop
2bda0e17
KB
24#endif
25
12bdd77c
JS
26// Don't use the Windows printer if we're in wxUniv mode and using
27// the PostScript architecture
28#if wxUSE_PRINTING_ARCHITECTURE && (!defined(__WXUNIVERSAL__) || !wxUSE_POSTSCRIPT_ARCHITECTURE_IN_MSW)
f6bcfd97 29
2bda0e17 30#ifndef WX_PRECOMP
57bd4c60 31 #include "wx/msw/wrapcdlg.h"
0c589ad0
BM
32 #include "wx/window.h"
33 #include "wx/msw/private.h"
103aec29
VZ
34 #include "wx/utils.h"
35 #include "wx/dc.h"
36 #include "wx/app.h"
37 #include "wx/msgdlg.h"
0c589ad0 38 #include "wx/intl.h"
e4db172a 39 #include "wx/log.h"
6d50343d 40 #include "wx/dcprint.h"
25a3fca2
VS
41 #include "wx/dcmemory.h"
42 #include "wx/image.h"
2bda0e17
KB
43#endif
44
25a3fca2
VS
45#include "wx/msw/dib.h"
46#include "wx/msw/dcmemory.h"
2bda0e17 47#include "wx/msw/printwin.h"
f4322df6 48#include "wx/msw/printdlg.h"
2bda0e17 49#include "wx/msw/private.h"
888dde65 50#include "wx/msw/dcprint.h"
c929ad91
VZ
51#if wxUSE_ENH_METAFILE
52 #include "wx/msw/enhmeta.h"
53#endif
2bda0e17 54#include <stdlib.h>
2bda0e17 55
103aec29
VZ
56// ---------------------------------------------------------------------------
57// private functions
58// ---------------------------------------------------------------------------
59
5009cb6a 60BOOL 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
77wxWindowsPrinter::wxWindowsPrinter(wxPrintDialogData *data)
78 : wxPrinterBase(data)
2bda0e17 79{
2bda0e17
KB
80}
81
82bool 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
ff471394
VZ
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
ff471394
VZ
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
d2b354f9
JS
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
446659ca 169 wxPrintAbortDialog *win = CreateAbortWindow(parent, printout);
7bcb11d3 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
a7587bc4
VZ
198 // The dc we get from the PrintDialog will do multiple copies without help
199 // if the device supports it. Loop only if we have created a dc from our
200 // own m_printDialogData or the device does not support multiple copies.
201 // m_printDialogData.GetPrintData().GetNoCopies() is set from device
202 // devMode in printdlg.cpp/wxWindowsPrintDialog::ConvertFromNative()
203 const int maxCopyCount = !prompt ||
204 !m_printDialogData.GetPrintData().GetNoCopies()
205 ? m_printDialogData.GetNoCopies() : 1;
e7aa9bb7 206 for ( int copyCount = 1; copyCount <= maxCopyCount; copyCount++ )
7bcb11d3 207 {
e41e579f 208 if ( !printout->OnBeginDocument(minPageNum, maxPageNum) )
7bcb11d3 209 {
103aec29 210 wxLogError(_("Could not start printing."));
f6bcfd97 211 sm_lastError = wxPRINTER_ERROR;
7bcb11d3
JS
212 break;
213 }
214 if (sm_abortIt)
f6bcfd97
BP
215 {
216 sm_lastError = wxPRINTER_CANCELLED;
7bcb11d3 217 break;
f6bcfd97 218 }
103aec29 219
7bcb11d3 220 int pn;
e41e579f
DS
221
222 for ( pn = minPageNum;
223 pn <= maxPageNum && printout->HasPage(pn);
70846f0a 224 pn++ )
7bcb11d3 225 {
e7aa9bb7
VS
226 win->SetProgress(pn - minPageNum + 1,
227 maxPageNum - minPageNum + 1,
228 copyCount, maxCopyCount);
229
70846f0a 230 if ( sm_abortIt )
7bcb11d3 231 {
f6bcfd97 232 sm_lastError = wxPRINTER_CANCELLED;
7bcb11d3
JS
233 break;
234 }
70846f0a
VZ
235
236 dc->StartPage();
237 bool cont = printout->OnPrintPage(pn);
238 dc->EndPage();
239
240 if ( !cont )
f6bcfd97
BP
241 {
242 sm_lastError = wxPRINTER_CANCELLED;
70846f0a 243 break;
f6bcfd97 244 }
7bcb11d3 245 }
70846f0a 246
7bcb11d3
JS
247 printout->OnEndDocument();
248 }
103aec29 249
7bcb11d3 250 printout->OnEndPrinting();
103aec29 251
7bcb11d3 252 if (sm_abortWindow)
2bda0e17 253 {
e41e579f 254 sm_abortWindow->Show(false);
5276b0a5 255 wxDELETE(sm_abortWindow);
2bda0e17 256 }
103aec29 257
7bcb11d3 258 delete dc;
103aec29 259
e71693c3 260 return sm_lastError == wxPRINTER_NO_ERROR;
7bcb11d3 261}
2bda0e17 262
f415cab9 263wxDC *wxWindowsPrinter::PrintDialog(wxWindow *parent)
7bcb11d3 264{
d3b9f782 265 wxDC *dc = NULL;
2bda0e17 266
f415cab9 267 wxWindowsPrintDialog dialog(parent, & m_printDialogData);
7bcb11d3 268 int ret = dialog.ShowModal();
2bda0e17 269
7bcb11d3
JS
270 if (ret == wxID_OK)
271 {
272 dc = dialog.GetPrintDC();
273 m_printDialogData = dialog.GetPrintDialogData();
33ac7e6f 274 if (dc == NULL)
f6bcfd97
BP
275 sm_lastError = wxPRINTER_ERROR;
276 else
277 sm_lastError = wxPRINTER_NO_ERROR;
7bcb11d3 278 }
f6bcfd97
BP
279 else
280 sm_lastError = wxPRINTER_CANCELLED;
2bda0e17 281
7bcb11d3 282 return dc;
2bda0e17
KB
283}
284
a12f001e 285bool wxWindowsPrinter::Setup(wxWindow *WXUNUSED(parent))
2bda0e17 286{
b45dfd0a
RR
287#if 0
288 // We no longer expose that dialog
7bcb11d3 289 wxPrintDialog dialog(parent, & m_printDialogData);
e41e579f 290 dialog.GetPrintDialogData().SetSetupDialog(true);
7bcb11d3
JS
291
292 int ret = dialog.ShowModal();
293
294 if (ret == wxID_OK)
295 {
296 m_printDialogData = dialog.GetPrintDialogData();
297 }
298
299 return (ret == wxID_OK);
b45dfd0a 300#else
3950f9e1 301 return false;
b45dfd0a 302#endif
2bda0e17
KB
303}
304
305/*
7bcb11d3
JS
306* Print preview
307*/
2bda0e17 308
103aec29
VZ
309wxWindowsPrintPreview::wxWindowsPrintPreview(wxPrintout *printout,
310 wxPrintout *printoutForPrinting,
311 wxPrintDialogData *data)
312 : wxPrintPreviewBase(printout, printoutForPrinting, data)
2bda0e17 313{
7bcb11d3 314 DetermineScaling();
2bda0e17
KB
315}
316
103aec29
VZ
317wxWindowsPrintPreview::wxWindowsPrintPreview(wxPrintout *printout,
318 wxPrintout *printoutForPrinting,
319 wxPrintData *data)
320 : wxPrintPreviewBase(printout, printoutForPrinting, data)
321{
322 DetermineScaling();
323}
324
325wxWindowsPrintPreview::~wxWindowsPrintPreview()
2bda0e17
KB
326{
327}
328
329bool wxWindowsPrintPreview::Print(bool interactive)
330{
7bcb11d3 331 if (!m_printPrintout)
e41e579f 332 return false;
7bcb11d3
JS
333 wxWindowsPrinter printer(&m_printDialogData);
334 return printer.Print(m_previewFrame, m_printPrintout, interactive);
2bda0e17
KB
335}
336
103aec29 337void wxWindowsPrintPreview::DetermineScaling()
2bda0e17 338{
f2a59080 339 ScreenHDC dc;
2bda0e17
KB
340 int logPPIScreenX = ::GetDeviceCaps(dc, LOGPIXELSX);
341 int logPPIScreenY = ::GetDeviceCaps(dc, LOGPIXELSY);
34da0970 342 m_previewPrintout->SetPPIScreen(logPPIScreenX, logPPIScreenY);
103aec29 343
2bda0e17 344 // Get a device context for the currently selected printer
7bcb11d3 345 wxPrinterDC printerDC(m_printDialogData.GetPrintData());
103aec29 346
f415cab9
JS
347 int printerWidthMM;
348 int printerHeightMM;
349 int printerXRes;
350 int printerYRes;
351 int logPPIPrinterX;
352 int logPPIPrinterY;
353
f4322df6 354 wxRect paperRect;
103aec29 355
888dde65 356 if ( printerDC.IsOk() )
2bda0e17 357 {
888dde65
RR
358 wxPrinterDCImpl *impl = (wxPrinterDCImpl*) printerDC.GetImpl();
359 HDC dc = GetHdcOf(*impl);
f415cab9
JS
360 printerWidthMM = ::GetDeviceCaps(dc, HORZSIZE);
361 printerHeightMM = ::GetDeviceCaps(dc, VERTSIZE);
f4fefc23
VZ
362 printerXRes = ::GetDeviceCaps(dc, HORZRES);
363 printerYRes = ::GetDeviceCaps(dc, VERTRES);
f415cab9
JS
364 logPPIPrinterX = ::GetDeviceCaps(dc, LOGPIXELSX);
365 logPPIPrinterY = ::GetDeviceCaps(dc, LOGPIXELSY);
103aec29 366
f4322df6 367 paperRect = printerDC.GetPaperRect();
103aec29 368
f2a59080
VZ
369 if ( logPPIPrinterX == 0 ||
370 logPPIPrinterY == 0 ||
f415cab9
JS
371 printerWidthMM == 0 ||
372 printerHeightMM == 0 )
f2a59080 373 {
e41e579f 374 m_isOk = false;
f2a59080 375 }
2bda0e17
KB
376 }
377 else
f2a59080 378 {
f415cab9
JS
379 // use some defaults
380 printerWidthMM = 150;
381 printerHeightMM = 250;
382 printerXRes = 1500;
383 printerYRes = 2500;
384 logPPIPrinterX = 600;
385 logPPIPrinterY = 600;
386
f4322df6 387 paperRect = wxRect(0, 0, printerXRes, printerYRes);
e41e579f 388 m_isOk = false;
f2a59080 389 }
34da0970
JS
390 m_pageWidth = printerXRes;
391 m_pageHeight = printerYRes;
f415cab9
JS
392 m_previewPrintout->SetPageSizePixels(printerXRes, printerYRes);
393 m_previewPrintout->SetPageSizeMM(printerWidthMM, printerHeightMM);
394 m_previewPrintout->SetPaperRectPixels(paperRect);
395 m_previewPrintout->SetPPIPrinter(logPPIPrinterX, logPPIPrinterY);
103aec29 396
2bda0e17 397 // At 100%, the page should look about page-size on the screen.
f415cab9
JS
398 m_previewScaleX = float(logPPIScreenX) / logPPIPrinterX;
399 m_previewScaleY = float(logPPIScreenY) / logPPIPrinterY;
2bda0e17
KB
400}
401
c929ad91 402#if wxUSE_ENH_METAFILE
a5c1223d 403bool wxWindowsPrintPreview::RenderPageIntoBitmap(wxBitmap& bmp, int pageNum)
25a3fca2 404{
a5c1223d
VS
405 // The preview, as implemented in wxPrintPreviewBase (and as used prior to
406 // wx3) is inexact: it uses screen DC, which has much lower resolution and
407 // has other properties different from printer DC, so the preview is not
408 // quite right.
25a3fca2 409 //
a5c1223d
VS
410 // To make matters worse, if the application depends heavily on
411 // GetTextExtent() or does text layout itself, the output in preview and on
412 // paper can be very different. In particular, wxHtmlEasyPrinting is
413 // affected and the preview can be easily off by several pages.
25a3fca2 414 //
a5c1223d
VS
415 // To fix this, we render the preview into high-resolution enhanced
416 // metafile with properties identical to the printer DC. This guarantees
417 // metrics correctness while still being fast.
25a3fca2 418
25a3fca2 419
a5c1223d
VS
420 // print the preview into a metafile:
421 wxPrinterDC printerDC(m_printDialogData.GetPrintData());
422 wxEnhMetaFileDC metaDC(printerDC,
423 wxEmptyString,
424 printerDC.GetSize().x, printerDC.GetSize().y);
25a3fca2 425
a5c1223d 426 if ( !RenderPageIntoDC(metaDC, pageNum) )
25a3fca2
VS
427 return false;
428
a5c1223d
VS
429 wxEnhMetaFile *metafile = metaDC.Close();
430 if ( !metafile )
25a3fca2
VS
431 return false;
432
a5c1223d 433 // now render the metafile:
25a3fca2
VS
434 wxMemoryDC bmpDC;
435 bmpDC.SelectObject(bmp);
436 bmpDC.Clear();
437
a5c1223d
VS
438 wxRect outRect(0, 0, bmp.GetWidth(), bmp.GetHeight());
439 metafile->Play(&bmpDC, &outRect);
25a3fca2 440
25a3fca2 441
a5c1223d 442 delete metafile;
25a3fca2 443
a5c1223d 444 // TODO: we should keep the metafile and reuse it when changing zoom level
25a3fca2
VS
445
446 return true;
447}
c929ad91 448#endif // wxUSE_ENH_METAFILE
25a3fca2 449
5009cb6a 450BOOL CALLBACK wxAbortProc(HDC WXUNUSED(hdc), int WXUNUSED(error))
2bda0e17
KB
451{
452 MSG msg;
103aec29 453
34da0970 454 if (!wxPrinterBase::sm_abortWindow) /* If the abort dialog isn't up yet */
2bda0e17 455 return(TRUE);
103aec29 456
2bda0e17 457 /* Process messages intended for the abort dialog box */
103aec29 458
078cf5cb 459 while (!wxPrinterBase::sm_abortIt && ::PeekMessage(&msg, 0, 0, 0, TRUE))
34da0970 460 if (!IsDialogMessage((HWND) wxPrinterBase::sm_abortWindow->GetHWND(), &msg)) {
2bda0e17
KB
461 TranslateMessage(&msg);
462 DispatchMessage(&msg);
463 }
103aec29 464
e71693c3 465 /* bAbort is TRUE (return is FALSE) if the user has aborted */
103aec29 466
e71693c3 467 return !wxPrinterBase::sm_abortIt;
2bda0e17
KB
468}
469
f6bcfd97
BP
470#endif
471 // wxUSE_PRINTING_ARCHITECTURE