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