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