]> git.saurik.com Git - wxWidgets.git/blob - src/msw/printwin.cpp
Remove support for Gnome printing from wxGTK.
[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 // Copyright: (c) Julian Smart
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
10
11 // ===========================================================================
12 // declarations
13 // ===========================================================================
14
15 // ---------------------------------------------------------------------------
16 // headers
17 // ---------------------------------------------------------------------------
18
19 // For compilers that support precompilation, includes "wx.h".
20 #include "wx/wxprec.h"
21
22 #ifdef __BORLANDC__
23 #pragma hdrstop
24 #endif
25
26 // Don't use the Windows printer if we're in wxUniv mode and using
27 // the PostScript architecture
28 #if wxUSE_PRINTING_ARCHITECTURE && (!defined(__WXUNIVERSAL__) || !wxUSE_POSTSCRIPT_ARCHITECTURE_IN_MSW)
29
30 #ifndef WX_PRECOMP
31 #include "wx/msw/wrapcdlg.h"
32 #include "wx/window.h"
33 #include "wx/msw/private.h"
34 #include "wx/utils.h"
35 #include "wx/dc.h"
36 #include "wx/app.h"
37 #include "wx/msgdlg.h"
38 #include "wx/intl.h"
39 #include "wx/log.h"
40 #include "wx/dcprint.h"
41 #include "wx/dcmemory.h"
42 #include "wx/image.h"
43 #endif
44
45 #include "wx/msw/dib.h"
46 #include "wx/msw/dcmemory.h"
47 #include "wx/msw/printwin.h"
48 #include "wx/msw/printdlg.h"
49 #include "wx/msw/private.h"
50 #include "wx/msw/dcprint.h"
51 #if wxUSE_ENH_METAFILE
52 #include "wx/msw/enhmeta.h"
53 #endif
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 wxPrintAbortDialog *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 // The dc we get from the PrintDialog will do multiple copies without help
199 // if the device supports it. Loop only if we have created a dc from our
200 // own m_printDialogData or the device does not support multiple copies.
201 // m_printDialogData.GetPrintData().GetNoCopies() is set from device
202 // devMode in printdlg.cpp/wxWindowsPrintDialog::ConvertFromNative()
203 const int maxCopyCount = !prompt ||
204 !m_printDialogData.GetPrintData().GetNoCopies()
205 ? m_printDialogData.GetNoCopies() : 1;
206 for ( int copyCount = 1; copyCount <= maxCopyCount; copyCount++ )
207 {
208 if ( !printout->OnBeginDocument(minPageNum, maxPageNum) )
209 {
210 wxLogError(_("Could not start printing."));
211 sm_lastError = wxPRINTER_ERROR;
212 break;
213 }
214 if (sm_abortIt)
215 {
216 sm_lastError = wxPRINTER_CANCELLED;
217 break;
218 }
219
220 int pn;
221
222 for ( pn = minPageNum;
223 pn <= maxPageNum && printout->HasPage(pn);
224 pn++ )
225 {
226 win->SetProgress(pn - minPageNum + 1,
227 maxPageNum - minPageNum + 1,
228 copyCount, maxCopyCount);
229
230 if ( sm_abortIt )
231 {
232 sm_lastError = wxPRINTER_CANCELLED;
233 break;
234 }
235
236 dc->StartPage();
237 bool cont = printout->OnPrintPage(pn);
238 dc->EndPage();
239
240 if ( !cont )
241 {
242 sm_lastError = wxPRINTER_CANCELLED;
243 break;
244 }
245 }
246
247 printout->OnEndDocument();
248 }
249
250 printout->OnEndPrinting();
251
252 if (sm_abortWindow)
253 {
254 sm_abortWindow->Show(false);
255 wxDELETE(sm_abortWindow);
256 }
257
258 delete dc;
259
260 return sm_lastError == wxPRINTER_NO_ERROR;
261 }
262
263 wxDC *wxWindowsPrinter::PrintDialog(wxWindow *parent)
264 {
265 wxDC *dc = NULL;
266
267 wxWindowsPrintDialog dialog(parent, & m_printDialogData);
268 int ret = dialog.ShowModal();
269
270 if (ret == wxID_OK)
271 {
272 dc = dialog.GetPrintDC();
273 m_printDialogData = dialog.GetPrintDialogData();
274 if (dc == NULL)
275 sm_lastError = wxPRINTER_ERROR;
276 else
277 sm_lastError = wxPRINTER_NO_ERROR;
278 }
279 else
280 sm_lastError = wxPRINTER_CANCELLED;
281
282 return dc;
283 }
284
285 bool wxWindowsPrinter::Setup(wxWindow *WXUNUSED(parent))
286 {
287 #if 0
288 // We no longer expose that dialog
289 wxPrintDialog dialog(parent, & m_printDialogData);
290 dialog.GetPrintDialogData().SetSetupDialog(true);
291
292 int ret = dialog.ShowModal();
293
294 if (ret == wxID_OK)
295 {
296 m_printDialogData = dialog.GetPrintDialogData();
297 }
298
299 return (ret == wxID_OK);
300 #else
301 return false;
302 #endif
303 }
304
305 /*
306 * Print preview
307 */
308
309 wxWindowsPrintPreview::wxWindowsPrintPreview(wxPrintout *printout,
310 wxPrintout *printoutForPrinting,
311 wxPrintDialogData *data)
312 : wxPrintPreviewBase(printout, printoutForPrinting, data)
313 {
314 DetermineScaling();
315 }
316
317 wxWindowsPrintPreview::wxWindowsPrintPreview(wxPrintout *printout,
318 wxPrintout *printoutForPrinting,
319 wxPrintData *data)
320 : wxPrintPreviewBase(printout, printoutForPrinting, data)
321 {
322 DetermineScaling();
323 }
324
325 wxWindowsPrintPreview::~wxWindowsPrintPreview()
326 {
327 }
328
329 bool wxWindowsPrintPreview::Print(bool interactive)
330 {
331 if (!m_printPrintout)
332 return false;
333 wxWindowsPrinter printer(&m_printDialogData);
334 return printer.Print(m_previewFrame, m_printPrintout, interactive);
335 }
336
337 void wxWindowsPrintPreview::DetermineScaling()
338 {
339 ScreenHDC dc;
340 int logPPIScreenX = ::GetDeviceCaps(dc, LOGPIXELSX);
341 int logPPIScreenY = ::GetDeviceCaps(dc, LOGPIXELSY);
342 m_previewPrintout->SetPPIScreen(logPPIScreenX, logPPIScreenY);
343
344 // Get a device context for the currently selected printer
345 wxPrinterDC printerDC(m_printDialogData.GetPrintData());
346
347 int printerWidthMM;
348 int printerHeightMM;
349 int printerXRes;
350 int printerYRes;
351 int logPPIPrinterX;
352 int logPPIPrinterY;
353
354 wxRect paperRect;
355
356 if ( printerDC.IsOk() )
357 {
358 wxPrinterDCImpl *impl = (wxPrinterDCImpl*) printerDC.GetImpl();
359 HDC dc = GetHdcOf(*impl);
360 printerWidthMM = ::GetDeviceCaps(dc, HORZSIZE);
361 printerHeightMM = ::GetDeviceCaps(dc, VERTSIZE);
362 printerXRes = ::GetDeviceCaps(dc, HORZRES);
363 printerYRes = ::GetDeviceCaps(dc, VERTRES);
364 logPPIPrinterX = ::GetDeviceCaps(dc, LOGPIXELSX);
365 logPPIPrinterY = ::GetDeviceCaps(dc, LOGPIXELSY);
366
367 paperRect = printerDC.GetPaperRect();
368
369 if ( logPPIPrinterX == 0 ||
370 logPPIPrinterY == 0 ||
371 printerWidthMM == 0 ||
372 printerHeightMM == 0 )
373 {
374 m_isOk = false;
375 }
376 }
377 else
378 {
379 // use some defaults
380 printerWidthMM = 150;
381 printerHeightMM = 250;
382 printerXRes = 1500;
383 printerYRes = 2500;
384 logPPIPrinterX = 600;
385 logPPIPrinterY = 600;
386
387 paperRect = wxRect(0, 0, printerXRes, printerYRes);
388 m_isOk = false;
389 }
390 m_pageWidth = printerXRes;
391 m_pageHeight = printerYRes;
392 m_previewPrintout->SetPageSizePixels(printerXRes, printerYRes);
393 m_previewPrintout->SetPageSizeMM(printerWidthMM, printerHeightMM);
394 m_previewPrintout->SetPaperRectPixels(paperRect);
395 m_previewPrintout->SetPPIPrinter(logPPIPrinterX, logPPIPrinterY);
396
397 // At 100%, the page should look about page-size on the screen.
398 m_previewScaleX = float(logPPIScreenX) / logPPIPrinterX;
399 m_previewScaleY = float(logPPIScreenY) / logPPIPrinterY;
400 }
401
402 #if wxUSE_ENH_METAFILE
403 bool wxWindowsPrintPreview::RenderPageIntoBitmap(wxBitmap& bmp, int pageNum)
404 {
405 // The preview, as implemented in wxPrintPreviewBase (and as used prior to
406 // wx3) is inexact: it uses screen DC, which has much lower resolution and
407 // has other properties different from printer DC, so the preview is not
408 // quite right.
409 //
410 // To make matters worse, if the application depends heavily on
411 // GetTextExtent() or does text layout itself, the output in preview and on
412 // paper can be very different. In particular, wxHtmlEasyPrinting is
413 // affected and the preview can be easily off by several pages.
414 //
415 // To fix this, we render the preview into high-resolution enhanced
416 // metafile with properties identical to the printer DC. This guarantees
417 // metrics correctness while still being fast.
418
419
420 // print the preview into a metafile:
421 wxPrinterDC printerDC(m_printDialogData.GetPrintData());
422 wxEnhMetaFileDC metaDC(printerDC,
423 wxEmptyString,
424 printerDC.GetSize().x, printerDC.GetSize().y);
425
426 if ( !RenderPageIntoDC(metaDC, pageNum) )
427 return false;
428
429 wxEnhMetaFile *metafile = metaDC.Close();
430 if ( !metafile )
431 return false;
432
433 // now render the metafile:
434 wxMemoryDC bmpDC;
435 bmpDC.SelectObject(bmp);
436 bmpDC.Clear();
437
438 wxRect outRect(0, 0, bmp.GetWidth(), bmp.GetHeight());
439 metafile->Play(&bmpDC, &outRect);
440
441
442 delete metafile;
443
444 // TODO: we should keep the metafile and reuse it when changing zoom level
445
446 return true;
447 }
448 #endif // wxUSE_ENH_METAFILE
449
450 BOOL CALLBACK wxAbortProc(HDC WXUNUSED(hdc), int WXUNUSED(error))
451 {
452 MSG msg;
453
454 if (!wxPrinterBase::sm_abortWindow) /* If the abort dialog isn't up yet */
455 return(TRUE);
456
457 /* Process messages intended for the abort dialog box */
458
459 while (!wxPrinterBase::sm_abortIt && ::PeekMessage(&msg, 0, 0, 0, TRUE))
460 if (!IsDialogMessage((HWND) wxPrinterBase::sm_abortWindow->GetHWND(), &msg)) {
461 TranslateMessage(&msg);
462 DispatchMessage(&msg);
463 }
464
465 /* bAbort is TRUE (return is FALSE) if the user has aborted */
466
467 return !wxPrinterBase::sm_abortIt;
468 }
469
470 #endif
471 // wxUSE_PRINTING_ARCHITECTURE