]> git.saurik.com Git - wxWidgets.git/blob - src/msw/printwin.cpp
Add support for IEC and SI size units to wxFileName::GetHumanReadableSize().
[wxWidgets.git] / src / msw / printwin.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/msw/printwin.cpp
3 // Purpose: wxWindowsPrinter framework
4 // Author: Julian Smart
5 // Modified by:
6 // Created: 04/01/98
7 // RCS-ID: $Id$
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 // ===========================================================================
13 // declarations
14 // ===========================================================================
15
16 // ---------------------------------------------------------------------------
17 // headers
18 // ---------------------------------------------------------------------------
19
20 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
22
23 #ifdef __BORLANDC__
24 #pragma hdrstop
25 #endif
26
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)
30
31 #ifndef WX_PRECOMP
32 #include "wx/msw/wrapcdlg.h"
33 #include "wx/window.h"
34 #include "wx/msw/private.h"
35 #include "wx/utils.h"
36 #include "wx/dc.h"
37 #include "wx/app.h"
38 #include "wx/msgdlg.h"
39 #include "wx/intl.h"
40 #include "wx/log.h"
41 #include "wx/dcprint.h"
42 #include "wx/dcmemory.h"
43 #include "wx/image.h"
44 #endif
45
46 #include "wx/msw/dib.h"
47 #include "wx/msw/dcmemory.h"
48 #include "wx/msw/printwin.h"
49 #include "wx/msw/printdlg.h"
50 #include "wx/msw/private.h"
51 #include "wx/msw/dcprint.h"
52 #include "wx/msw/enhmeta.h"
53
54 #include <stdlib.h>
55
56 #ifndef __WIN32__
57 #include <print.h>
58 #endif
59
60 // ---------------------------------------------------------------------------
61 // private functions
62 // ---------------------------------------------------------------------------
63
64 LONG APIENTRY _EXPORT wxAbortProc(HDC hPr, int Code);
65
66 // ---------------------------------------------------------------------------
67 // wxWin macros
68 // ---------------------------------------------------------------------------
69
70 IMPLEMENT_DYNAMIC_CLASS(wxWindowsPrinter, wxPrinterBase)
71 IMPLEMENT_CLASS(wxWindowsPrintPreview, wxPrintPreviewBase)
72
73 // ===========================================================================
74 // implementation
75 // ===========================================================================
76
77 // ---------------------------------------------------------------------------
78 // Printer
79 // ---------------------------------------------------------------------------
80
81 wxWindowsPrinter::wxWindowsPrinter(wxPrintDialogData *data)
82 : wxPrinterBase(data)
83 {
84 m_lpAbortProc = (WXFARPROC)wxAbortProc;
85 }
86
87 wxWindowsPrinter::~wxWindowsPrinter()
88 {
89 // avoids warning about statement with no effect (FreeProcInstance
90 // doesn't do anything under Win32)
91 #if !defined(__WIN32__) && !defined(__NT__)
92 FreeProcInstance((FARPROC) m_lpAbortProc);
93 #endif
94 }
95
96 bool wxWindowsPrinter::Print(wxWindow *parent, wxPrintout *printout, bool prompt)
97 {
98 sm_abortIt = false;
99 sm_abortWindow = NULL;
100
101 if (!printout)
102 {
103 sm_lastError = wxPRINTER_ERROR;
104 return false;
105 }
106
107 printout->SetIsPreview(false);
108
109 if (m_printDialogData.GetMinPage() < 1)
110 m_printDialogData.SetMinPage(1);
111 if (m_printDialogData.GetMaxPage() < 1)
112 m_printDialogData.SetMaxPage(9999);
113
114 // Create a suitable device context
115 wxPrinterDC *dc wxDUMMY_INITIALIZE(NULL);
116 if (prompt)
117 {
118 dc = wxDynamicCast(PrintDialog(parent), wxPrinterDC);
119 if (!dc)
120 return false;
121 }
122 else
123 {
124 dc = new wxPrinterDC(m_printDialogData.GetPrintData());
125 }
126
127 // May have pressed cancel.
128 if (!dc || !dc->IsOk())
129 {
130 if (dc) delete dc;
131 return false;
132 }
133
134 wxPrinterDCImpl *impl = (wxPrinterDCImpl*) dc->GetImpl();
135
136 HDC hdc = ::GetDC(NULL);
137 int logPPIScreenX = ::GetDeviceCaps(hdc, LOGPIXELSX);
138 int logPPIScreenY = ::GetDeviceCaps(hdc, LOGPIXELSY);
139 ::ReleaseDC(NULL, hdc);
140
141 int logPPIPrinterX = ::GetDeviceCaps((HDC) impl->GetHDC(), LOGPIXELSX);
142 int logPPIPrinterY = ::GetDeviceCaps((HDC) impl->GetHDC(), LOGPIXELSY);
143 if (logPPIPrinterX == 0 || logPPIPrinterY == 0)
144 {
145 delete dc;
146 sm_lastError = wxPRINTER_ERROR;
147 return false;
148 }
149
150 printout->SetPPIScreen(logPPIScreenX, logPPIScreenY);
151 printout->SetPPIPrinter(logPPIPrinterX, logPPIPrinterY);
152
153 // Set printout parameters
154 printout->SetDC(dc);
155
156 int w, h;
157 dc->GetSize(&w, &h);
158 printout->SetPageSizePixels((int)w, (int)h);
159 printout->SetPaperRectPixels(dc->GetPaperRect());
160
161 dc->GetSizeMM(&w, &h);
162 printout->SetPageSizeMM((int)w, (int)h);
163
164 // Create an abort window
165 wxBusyCursor busyCursor;
166
167 printout->OnPreparePrinting();
168
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;
177 return false;
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
185 wxWindow *win = CreateAbortWindow(parent, printout);
186 wxYield();
187
188 #if defined(__WATCOMC__) || defined(__BORLANDC__) || defined(__GNUWIN32__) || !defined(__WIN32__)
189 #ifdef STRICT
190 ::SetAbortProc((HDC) impl->GetHDC(), (ABORTPROC) m_lpAbortProc);
191 #else
192 ::SetAbortProc((HDC) impl->GetHDC(), (FARPROC) m_lpAbortProc);
193 #endif
194 #else
195 ::SetAbortProc((HDC) impl->GetHDC(), (int (_stdcall *)
196 // cast it to right type only if required
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
199 #ifdef STRICT
200 (HDC, int)
201 #else
202 ()
203 #endif
204 )m_lpAbortProc);
205 #endif
206
207 if (!win)
208 {
209 wxLogDebug(wxT("Could not create an abort dialog."));
210 sm_lastError = wxPRINTER_ERROR;
211
212 delete dc;
213 return false;
214 }
215 sm_abortWindow = win;
216 sm_abortWindow->Show();
217 wxSafeYield();
218
219 printout->OnBeginPrinting();
220
221 sm_lastError = wxPRINTER_NO_ERROR;
222
223 int minPageNum = minPage, maxPageNum = maxPage;
224
225 if ( !m_printDialogData.GetAllPages() )
226 {
227 minPageNum = m_printDialogData.GetFromPage();
228 maxPageNum = m_printDialogData.GetToPage();
229 }
230
231 int copyCount;
232 for ( copyCount = 1;
233 copyCount <= m_printDialogData.GetNoCopies();
234 copyCount++ )
235 {
236 if ( !printout->OnBeginDocument(minPageNum, maxPageNum) )
237 {
238 wxLogError(_("Could not start printing."));
239 sm_lastError = wxPRINTER_ERROR;
240 break;
241 }
242 if (sm_abortIt)
243 {
244 sm_lastError = wxPRINTER_CANCELLED;
245 break;
246 }
247
248 int pn;
249
250 for ( pn = minPageNum;
251 pn <= maxPageNum && printout->HasPage(pn);
252 pn++ )
253 {
254 if ( sm_abortIt )
255 {
256 sm_lastError = wxPRINTER_CANCELLED;
257 break;
258 }
259
260 dc->StartPage();
261 bool cont = printout->OnPrintPage(pn);
262 dc->EndPage();
263
264 if ( !cont )
265 {
266 sm_lastError = wxPRINTER_CANCELLED;
267 break;
268 }
269 }
270
271 printout->OnEndDocument();
272 }
273
274 printout->OnEndPrinting();
275
276 if (sm_abortWindow)
277 {
278 sm_abortWindow->Show(false);
279 delete sm_abortWindow;
280 sm_abortWindow = NULL;
281 }
282
283 delete dc;
284
285 return sm_lastError == wxPRINTER_NO_ERROR;
286 }
287
288 wxDC *wxWindowsPrinter::PrintDialog(wxWindow *parent)
289 {
290 wxDC *dc = NULL;
291
292 wxWindowsPrintDialog dialog(parent, & m_printDialogData);
293 int ret = dialog.ShowModal();
294
295 if (ret == wxID_OK)
296 {
297 dc = dialog.GetPrintDC();
298 m_printDialogData = dialog.GetPrintDialogData();
299 if (dc == NULL)
300 sm_lastError = wxPRINTER_ERROR;
301 else
302 sm_lastError = wxPRINTER_NO_ERROR;
303 }
304 else
305 sm_lastError = wxPRINTER_CANCELLED;
306
307 return dc;
308 }
309
310 bool wxWindowsPrinter::Setup(wxWindow *WXUNUSED(parent))
311 {
312 #if 0
313 // We no longer expose that dialog
314 wxPrintDialog dialog(parent, & m_printDialogData);
315 dialog.GetPrintDialogData().SetSetupDialog(true);
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);
325 #else
326 return false;
327 #endif
328 }
329
330 /*
331 * Print preview
332 */
333
334 wxWindowsPrintPreview::wxWindowsPrintPreview(wxPrintout *printout,
335 wxPrintout *printoutForPrinting,
336 wxPrintDialogData *data)
337 : wxPrintPreviewBase(printout, printoutForPrinting, data)
338 {
339 DetermineScaling();
340 }
341
342 wxWindowsPrintPreview::wxWindowsPrintPreview(wxPrintout *printout,
343 wxPrintout *printoutForPrinting,
344 wxPrintData *data)
345 : wxPrintPreviewBase(printout, printoutForPrinting, data)
346 {
347 DetermineScaling();
348 }
349
350 wxWindowsPrintPreview::~wxWindowsPrintPreview()
351 {
352 }
353
354 bool wxWindowsPrintPreview::Print(bool interactive)
355 {
356 if (!m_printPrintout)
357 return false;
358 wxWindowsPrinter printer(&m_printDialogData);
359 return printer.Print(m_previewFrame, m_printPrintout, interactive);
360 }
361
362 void wxWindowsPrintPreview::DetermineScaling()
363 {
364 ScreenHDC dc;
365 int logPPIScreenX = ::GetDeviceCaps(dc, LOGPIXELSX);
366 int logPPIScreenY = ::GetDeviceCaps(dc, LOGPIXELSY);
367 m_previewPrintout->SetPPIScreen(logPPIScreenX, logPPIScreenY);
368
369 // Get a device context for the currently selected printer
370 wxPrinterDC printerDC(m_printDialogData.GetPrintData());
371
372 int printerWidthMM;
373 int printerHeightMM;
374 int printerXRes;
375 int printerYRes;
376 int logPPIPrinterX;
377 int logPPIPrinterY;
378
379 wxRect paperRect;
380
381 if ( printerDC.IsOk() )
382 {
383 wxPrinterDCImpl *impl = (wxPrinterDCImpl*) printerDC.GetImpl();
384 HDC dc = GetHdcOf(*impl);
385 printerWidthMM = ::GetDeviceCaps(dc, HORZSIZE);
386 printerHeightMM = ::GetDeviceCaps(dc, VERTSIZE);
387 printerXRes = ::GetDeviceCaps(dc, HORZRES);
388 printerYRes = ::GetDeviceCaps(dc, VERTRES);
389 logPPIPrinterX = ::GetDeviceCaps(dc, LOGPIXELSX);
390 logPPIPrinterY = ::GetDeviceCaps(dc, LOGPIXELSY);
391
392 paperRect = printerDC.GetPaperRect();
393
394 if ( logPPIPrinterX == 0 ||
395 logPPIPrinterY == 0 ||
396 printerWidthMM == 0 ||
397 printerHeightMM == 0 )
398 {
399 m_isOk = false;
400 }
401 }
402 else
403 {
404 // use some defaults
405 printerWidthMM = 150;
406 printerHeightMM = 250;
407 printerXRes = 1500;
408 printerYRes = 2500;
409 logPPIPrinterX = 600;
410 logPPIPrinterY = 600;
411
412 paperRect = wxRect(0, 0, printerXRes, printerYRes);
413 m_isOk = false;
414 }
415 m_pageWidth = printerXRes;
416 m_pageHeight = printerYRes;
417 m_previewPrintout->SetPageSizePixels(printerXRes, printerYRes);
418 m_previewPrintout->SetPageSizeMM(printerWidthMM, printerHeightMM);
419 m_previewPrintout->SetPaperRectPixels(paperRect);
420 m_previewPrintout->SetPPIPrinter(logPPIPrinterX, logPPIPrinterY);
421
422 // At 100%, the page should look about page-size on the screen.
423 m_previewScaleX = float(logPPIScreenX) / logPPIPrinterX;
424 m_previewScaleY = float(logPPIScreenY) / logPPIPrinterY;
425 }
426
427 bool wxWindowsPrintPreview::RenderPageIntoBitmap(wxBitmap& bmp, int pageNum)
428 {
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.
433 //
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.
438 //
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.
442
443
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);
449
450 if ( !RenderPageIntoDC(metaDC, pageNum) )
451 return false;
452
453 wxEnhMetaFile *metafile = metaDC.Close();
454 if ( !metafile )
455 return false;
456
457 // now render the metafile:
458 wxMemoryDC bmpDC;
459 bmpDC.SelectObject(bmp);
460 bmpDC.Clear();
461
462 wxRect outRect(0, 0, bmp.GetWidth(), bmp.GetHeight());
463 metafile->Play(&bmpDC, &outRect);
464
465
466 delete metafile;
467
468 // TODO: we should keep the metafile and reuse it when changing zoom level
469
470 return true;
471 }
472
473
474 /****************************************************************************
475
476 FUNCTION: wxAbortProc()
477
478 PURPOSE: Processes messages for the Abort Dialog box
479
480 ****************************************************************************/
481
482 LONG APIENTRY _EXPORT wxAbortProc(HDC WXUNUSED(hPr), int WXUNUSED(Code))
483 {
484 MSG msg;
485
486 if (!wxPrinterBase::sm_abortWindow) /* If the abort dialog isn't up yet */
487 return(TRUE);
488
489 /* Process messages intended for the abort dialog box */
490
491 while (!wxPrinterBase::sm_abortIt && ::PeekMessage(&msg, 0, 0, 0, TRUE))
492 if (!IsDialogMessage((HWND) wxPrinterBase::sm_abortWindow->GetHWND(), &msg)) {
493 TranslateMessage(&msg);
494 DispatchMessage(&msg);
495 }
496
497 /* bAbort is TRUE (return is FALSE) if the user has aborted */
498
499 return !wxPrinterBase::sm_abortIt;
500 }
501
502 #endif
503 // wxUSE_PRINTING_ARCHITECTURE