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