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