]> git.saurik.com Git - wxWidgets.git/blob - src/msw/printwin.cpp
Add wxItemContainer::DetachClientObject() and use it in wxRearrangeList.
[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 wxDELETE(sm_abortWindow);
247 }
248
249 delete dc;
250
251 return sm_lastError == wxPRINTER_NO_ERROR;
252 }
253
254 wxDC *wxWindowsPrinter::PrintDialog(wxWindow *parent)
255 {
256 wxDC *dc = NULL;
257
258 wxWindowsPrintDialog dialog(parent, & m_printDialogData);
259 int ret = dialog.ShowModal();
260
261 if (ret == wxID_OK)
262 {
263 dc = dialog.GetPrintDC();
264 m_printDialogData = dialog.GetPrintDialogData();
265 if (dc == NULL)
266 sm_lastError = wxPRINTER_ERROR;
267 else
268 sm_lastError = wxPRINTER_NO_ERROR;
269 }
270 else
271 sm_lastError = wxPRINTER_CANCELLED;
272
273 return dc;
274 }
275
276 bool wxWindowsPrinter::Setup(wxWindow *WXUNUSED(parent))
277 {
278 #if 0
279 // We no longer expose that dialog
280 wxPrintDialog dialog(parent, & m_printDialogData);
281 dialog.GetPrintDialogData().SetSetupDialog(true);
282
283 int ret = dialog.ShowModal();
284
285 if (ret == wxID_OK)
286 {
287 m_printDialogData = dialog.GetPrintDialogData();
288 }
289
290 return (ret == wxID_OK);
291 #else
292 return false;
293 #endif
294 }
295
296 /*
297 * Print preview
298 */
299
300 wxWindowsPrintPreview::wxWindowsPrintPreview(wxPrintout *printout,
301 wxPrintout *printoutForPrinting,
302 wxPrintDialogData *data)
303 : wxPrintPreviewBase(printout, printoutForPrinting, data)
304 {
305 DetermineScaling();
306 }
307
308 wxWindowsPrintPreview::wxWindowsPrintPreview(wxPrintout *printout,
309 wxPrintout *printoutForPrinting,
310 wxPrintData *data)
311 : wxPrintPreviewBase(printout, printoutForPrinting, data)
312 {
313 DetermineScaling();
314 }
315
316 wxWindowsPrintPreview::~wxWindowsPrintPreview()
317 {
318 }
319
320 bool wxWindowsPrintPreview::Print(bool interactive)
321 {
322 if (!m_printPrintout)
323 return false;
324 wxWindowsPrinter printer(&m_printDialogData);
325 return printer.Print(m_previewFrame, m_printPrintout, interactive);
326 }
327
328 void wxWindowsPrintPreview::DetermineScaling()
329 {
330 ScreenHDC dc;
331 int logPPIScreenX = ::GetDeviceCaps(dc, LOGPIXELSX);
332 int logPPIScreenY = ::GetDeviceCaps(dc, LOGPIXELSY);
333 m_previewPrintout->SetPPIScreen(logPPIScreenX, logPPIScreenY);
334
335 // Get a device context for the currently selected printer
336 wxPrinterDC printerDC(m_printDialogData.GetPrintData());
337
338 int printerWidthMM;
339 int printerHeightMM;
340 int printerXRes;
341 int printerYRes;
342 int logPPIPrinterX;
343 int logPPIPrinterY;
344
345 wxRect paperRect;
346
347 if ( printerDC.IsOk() )
348 {
349 wxPrinterDCImpl *impl = (wxPrinterDCImpl*) printerDC.GetImpl();
350 HDC dc = GetHdcOf(*impl);
351 printerWidthMM = ::GetDeviceCaps(dc, HORZSIZE);
352 printerHeightMM = ::GetDeviceCaps(dc, VERTSIZE);
353 printerXRes = ::GetDeviceCaps(dc, HORZRES);
354 printerYRes = ::GetDeviceCaps(dc, VERTRES);
355 logPPIPrinterX = ::GetDeviceCaps(dc, LOGPIXELSX);
356 logPPIPrinterY = ::GetDeviceCaps(dc, LOGPIXELSY);
357
358 paperRect = printerDC.GetPaperRect();
359
360 if ( logPPIPrinterX == 0 ||
361 logPPIPrinterY == 0 ||
362 printerWidthMM == 0 ||
363 printerHeightMM == 0 )
364 {
365 m_isOk = false;
366 }
367 }
368 else
369 {
370 // use some defaults
371 printerWidthMM = 150;
372 printerHeightMM = 250;
373 printerXRes = 1500;
374 printerYRes = 2500;
375 logPPIPrinterX = 600;
376 logPPIPrinterY = 600;
377
378 paperRect = wxRect(0, 0, printerXRes, printerYRes);
379 m_isOk = false;
380 }
381 m_pageWidth = printerXRes;
382 m_pageHeight = printerYRes;
383 m_previewPrintout->SetPageSizePixels(printerXRes, printerYRes);
384 m_previewPrintout->SetPageSizeMM(printerWidthMM, printerHeightMM);
385 m_previewPrintout->SetPaperRectPixels(paperRect);
386 m_previewPrintout->SetPPIPrinter(logPPIPrinterX, logPPIPrinterY);
387
388 // At 100%, the page should look about page-size on the screen.
389 m_previewScaleX = float(logPPIScreenX) / logPPIPrinterX;
390 m_previewScaleY = float(logPPIScreenY) / logPPIPrinterY;
391 }
392
393 bool wxWindowsPrintPreview::RenderPageIntoBitmap(wxBitmap& bmp, int pageNum)
394 {
395 // The preview, as implemented in wxPrintPreviewBase (and as used prior to
396 // wx3) is inexact: it uses screen DC, which has much lower resolution and
397 // has other properties different from printer DC, so the preview is not
398 // quite right.
399 //
400 // To make matters worse, if the application depends heavily on
401 // GetTextExtent() or does text layout itself, the output in preview and on
402 // paper can be very different. In particular, wxHtmlEasyPrinting is
403 // affected and the preview can be easily off by several pages.
404 //
405 // To fix this, we render the preview into high-resolution enhanced
406 // metafile with properties identical to the printer DC. This guarantees
407 // metrics correctness while still being fast.
408
409
410 // print the preview into a metafile:
411 wxPrinterDC printerDC(m_printDialogData.GetPrintData());
412 wxEnhMetaFileDC metaDC(printerDC,
413 wxEmptyString,
414 printerDC.GetSize().x, printerDC.GetSize().y);
415
416 if ( !RenderPageIntoDC(metaDC, pageNum) )
417 return false;
418
419 wxEnhMetaFile *metafile = metaDC.Close();
420 if ( !metafile )
421 return false;
422
423 // now render the metafile:
424 wxMemoryDC bmpDC;
425 bmpDC.SelectObject(bmp);
426 bmpDC.Clear();
427
428 wxRect outRect(0, 0, bmp.GetWidth(), bmp.GetHeight());
429 metafile->Play(&bmpDC, &outRect);
430
431
432 delete metafile;
433
434 // TODO: we should keep the metafile and reuse it when changing zoom level
435
436 return true;
437 }
438
439
440 BOOL CALLBACK wxAbortProc(HDC WXUNUSED(hdc), int WXUNUSED(error))
441 {
442 MSG msg;
443
444 if (!wxPrinterBase::sm_abortWindow) /* If the abort dialog isn't up yet */
445 return(TRUE);
446
447 /* Process messages intended for the abort dialog box */
448
449 while (!wxPrinterBase::sm_abortIt && ::PeekMessage(&msg, 0, 0, 0, TRUE))
450 if (!IsDialogMessage((HWND) wxPrinterBase::sm_abortWindow->GetHWND(), &msg)) {
451 TranslateMessage(&msg);
452 DispatchMessage(&msg);
453 }
454
455 /* bAbort is TRUE (return is FALSE) if the user has aborted */
456
457 return !wxPrinterBase::sm_abortIt;
458 }
459
460 #endif
461 // wxUSE_PRINTING_ARCHITECTURE