]>
git.saurik.com Git - wxWidgets.git/blob - src/msw/printwin.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/msw/printwin.cpp
3 // Purpose: wxWindowsPrinter framework
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ===========================================================================
14 // ===========================================================================
16 // ---------------------------------------------------------------------------
18 // ---------------------------------------------------------------------------
20 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
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)
32 #include "wx/msw/wrapcdlg.h"
33 #include "wx/window.h"
34 #include "wx/msw/private.h"
38 #include "wx/msgdlg.h"
41 #include "wx/dcprint.h"
42 #include "wx/dcmemory.h"
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"
56 // ---------------------------------------------------------------------------
58 // ---------------------------------------------------------------------------
60 BOOL CALLBACK
wxAbortProc(HDC hdc
, int error
);
62 // ---------------------------------------------------------------------------
64 // ---------------------------------------------------------------------------
66 IMPLEMENT_DYNAMIC_CLASS(wxWindowsPrinter
, wxPrinterBase
)
67 IMPLEMENT_CLASS(wxWindowsPrintPreview
, wxPrintPreviewBase
)
69 // ===========================================================================
71 // ===========================================================================
73 // ---------------------------------------------------------------------------
75 // ---------------------------------------------------------------------------
77 wxWindowsPrinter::wxWindowsPrinter(wxPrintDialogData
*data
)
82 bool wxWindowsPrinter::Print(wxWindow
*parent
, wxPrintout
*printout
, bool prompt
)
85 sm_abortWindow
= NULL
;
89 sm_lastError
= wxPRINTER_ERROR
;
93 if (m_printDialogData
.GetMinPage() < 1)
94 m_printDialogData
.SetMinPage(1);
95 if (m_printDialogData
.GetMaxPage() < 1)
96 m_printDialogData
.SetMaxPage(9999);
98 // Create a suitable device context
99 wxPrinterDC
*dc
wxDUMMY_INITIALIZE(NULL
);
102 dc
= wxDynamicCast(PrintDialog(parent
), wxPrinterDC
);
108 dc
= new wxPrinterDC(m_printDialogData
.GetPrintData());
111 // May have pressed cancel.
112 if (!dc
|| !dc
->IsOk())
118 wxPrinterDCImpl
*impl
= (wxPrinterDCImpl
*) dc
->GetImpl();
120 HDC hdc
= ::GetDC(NULL
);
121 int logPPIScreenX
= ::GetDeviceCaps(hdc
, LOGPIXELSX
);
122 int logPPIScreenY
= ::GetDeviceCaps(hdc
, LOGPIXELSY
);
123 ::ReleaseDC(NULL
, hdc
);
125 int logPPIPrinterX
= ::GetDeviceCaps((HDC
) impl
->GetHDC(), LOGPIXELSX
);
126 int logPPIPrinterY
= ::GetDeviceCaps((HDC
) impl
->GetHDC(), LOGPIXELSY
);
127 if (logPPIPrinterX
== 0 || logPPIPrinterY
== 0)
130 sm_lastError
= wxPRINTER_ERROR
;
134 printout
->SetPPIScreen(logPPIScreenX
, logPPIScreenY
);
135 printout
->SetPPIPrinter(logPPIPrinterX
, logPPIPrinterY
);
137 // Set printout parameters
142 printout
->SetPageSizePixels((int)w
, (int)h
);
143 printout
->SetPaperRectPixels(dc
->GetPaperRect());
145 dc
->GetSizeMM(&w
, &h
);
146 printout
->SetPageSizeMM((int)w
, (int)h
);
148 // Create an abort window
149 wxBusyCursor busyCursor
;
151 printout
->OnPreparePrinting();
153 // Get some parameters from the printout, if defined
154 int fromPage
, toPage
;
155 int minPage
, maxPage
;
156 printout
->GetPageInfo(&minPage
, &maxPage
, &fromPage
, &toPage
);
160 sm_lastError
= wxPRINTER_ERROR
;
164 // Only set min and max, because from and to have been
166 m_printDialogData
.SetMinPage(minPage
);
167 m_printDialogData
.SetMaxPage(maxPage
);
169 wxWindow
*win
= CreateAbortWindow(parent
, printout
);
172 ::SetAbortProc(GetHdcOf(*impl
), wxAbortProc
);
176 wxLogDebug(wxT("Could not create an abort dialog."));
177 sm_lastError
= wxPRINTER_ERROR
;
182 sm_abortWindow
= win
;
183 sm_abortWindow
->Show();
186 printout
->OnBeginPrinting();
188 sm_lastError
= wxPRINTER_NO_ERROR
;
190 int minPageNum
= minPage
, maxPageNum
= maxPage
;
192 if ( !m_printDialogData
.GetAllPages() )
194 minPageNum
= m_printDialogData
.GetFromPage();
195 maxPageNum
= m_printDialogData
.GetToPage();
200 copyCount
<= m_printDialogData
.GetNoCopies();
203 if ( !printout
->OnBeginDocument(minPageNum
, maxPageNum
) )
205 wxLogError(_("Could not start printing."));
206 sm_lastError
= wxPRINTER_ERROR
;
211 sm_lastError
= wxPRINTER_CANCELLED
;
217 for ( pn
= minPageNum
;
218 pn
<= maxPageNum
&& printout
->HasPage(pn
);
223 sm_lastError
= wxPRINTER_CANCELLED
;
228 bool cont
= printout
->OnPrintPage(pn
);
233 sm_lastError
= wxPRINTER_CANCELLED
;
238 printout
->OnEndDocument();
241 printout
->OnEndPrinting();
245 sm_abortWindow
->Show(false);
246 wxDELETE(sm_abortWindow
);
251 return sm_lastError
== wxPRINTER_NO_ERROR
;
254 wxDC
*wxWindowsPrinter::PrintDialog(wxWindow
*parent
)
258 wxWindowsPrintDialog
dialog(parent
, & m_printDialogData
);
259 int ret
= dialog
.ShowModal();
263 dc
= dialog
.GetPrintDC();
264 m_printDialogData
= dialog
.GetPrintDialogData();
266 sm_lastError
= wxPRINTER_ERROR
;
268 sm_lastError
= wxPRINTER_NO_ERROR
;
271 sm_lastError
= wxPRINTER_CANCELLED
;
276 bool wxWindowsPrinter::Setup(wxWindow
*WXUNUSED(parent
))
279 // We no longer expose that dialog
280 wxPrintDialog
dialog(parent
, & m_printDialogData
);
281 dialog
.GetPrintDialogData().SetSetupDialog(true);
283 int ret
= dialog
.ShowModal();
287 m_printDialogData
= dialog
.GetPrintDialogData();
290 return (ret
== wxID_OK
);
300 wxWindowsPrintPreview::wxWindowsPrintPreview(wxPrintout
*printout
,
301 wxPrintout
*printoutForPrinting
,
302 wxPrintDialogData
*data
)
303 : wxPrintPreviewBase(printout
, printoutForPrinting
, data
)
308 wxWindowsPrintPreview::wxWindowsPrintPreview(wxPrintout
*printout
,
309 wxPrintout
*printoutForPrinting
,
311 : wxPrintPreviewBase(printout
, printoutForPrinting
, data
)
316 wxWindowsPrintPreview::~wxWindowsPrintPreview()
320 bool wxWindowsPrintPreview::Print(bool interactive
)
322 if (!m_printPrintout
)
324 wxWindowsPrinter
printer(&m_printDialogData
);
325 return printer
.Print(m_previewFrame
, m_printPrintout
, interactive
);
328 void wxWindowsPrintPreview::DetermineScaling()
331 int logPPIScreenX
= ::GetDeviceCaps(dc
, LOGPIXELSX
);
332 int logPPIScreenY
= ::GetDeviceCaps(dc
, LOGPIXELSY
);
333 m_previewPrintout
->SetPPIScreen(logPPIScreenX
, logPPIScreenY
);
335 // Get a device context for the currently selected printer
336 wxPrinterDC
printerDC(m_printDialogData
.GetPrintData());
347 if ( printerDC
.IsOk() )
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
);
358 paperRect
= printerDC
.GetPaperRect();
360 if ( logPPIPrinterX
== 0 ||
361 logPPIPrinterY
== 0 ||
362 printerWidthMM
== 0 ||
363 printerHeightMM
== 0 )
371 printerWidthMM
= 150;
372 printerHeightMM
= 250;
375 logPPIPrinterX
= 600;
376 logPPIPrinterY
= 600;
378 paperRect
= wxRect(0, 0, printerXRes
, printerYRes
);
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
);
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
;
393 bool wxWindowsPrintPreview::RenderPageIntoBitmap(wxBitmap
& bmp
, int pageNum
)
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
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.
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.
410 // print the preview into a metafile:
411 wxPrinterDC
printerDC(m_printDialogData
.GetPrintData());
412 wxEnhMetaFileDC
metaDC(printerDC
,
414 printerDC
.GetSize().x
, printerDC
.GetSize().y
);
416 if ( !RenderPageIntoDC(metaDC
, pageNum
) )
419 wxEnhMetaFile
*metafile
= metaDC
.Close();
423 // now render the metafile:
425 bmpDC
.SelectObject(bmp
);
428 wxRect
outRect(0, 0, bmp
.GetWidth(), bmp
.GetHeight());
429 metafile
->Play(&bmpDC
, &outRect
);
434 // TODO: we should keep the metafile and reuse it when changing zoom level
440 BOOL CALLBACK
wxAbortProc(HDC
WXUNUSED(hdc
), int WXUNUSED(error
))
444 if (!wxPrinterBase::sm_abortWindow
) /* If the abort dialog isn't up yet */
447 /* Process messages intended for the abort dialog box */
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
);
455 /* bAbort is TRUE (return is FALSE) if the user has aborted */
457 return !wxPrinterBase::sm_abortIt
;
461 // wxUSE_PRINTING_ARCHITECTURE