]>
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 #if wxUSE_ENH_METAFILE
53 #include "wx/msw/enhmeta.h"
57 // ---------------------------------------------------------------------------
59 // ---------------------------------------------------------------------------
61 BOOL CALLBACK
wxAbortProc(HDC hdc
, int error
);
63 // ---------------------------------------------------------------------------
65 // ---------------------------------------------------------------------------
67 IMPLEMENT_DYNAMIC_CLASS(wxWindowsPrinter
, wxPrinterBase
)
68 IMPLEMENT_CLASS(wxWindowsPrintPreview
, wxPrintPreviewBase
)
70 // ===========================================================================
72 // ===========================================================================
74 // ---------------------------------------------------------------------------
76 // ---------------------------------------------------------------------------
78 wxWindowsPrinter::wxWindowsPrinter(wxPrintDialogData
*data
)
83 bool wxWindowsPrinter::Print(wxWindow
*parent
, wxPrintout
*printout
, bool prompt
)
86 sm_abortWindow
= NULL
;
90 sm_lastError
= wxPRINTER_ERROR
;
94 if (m_printDialogData
.GetMinPage() < 1)
95 m_printDialogData
.SetMinPage(1);
96 if (m_printDialogData
.GetMaxPage() < 1)
97 m_printDialogData
.SetMaxPage(9999);
99 // Create a suitable device context
100 wxPrinterDC
*dc
wxDUMMY_INITIALIZE(NULL
);
103 dc
= wxDynamicCast(PrintDialog(parent
), wxPrinterDC
);
109 dc
= new wxPrinterDC(m_printDialogData
.GetPrintData());
112 // May have pressed cancel.
113 if (!dc
|| !dc
->IsOk())
119 wxPrinterDCImpl
*impl
= (wxPrinterDCImpl
*) dc
->GetImpl();
121 HDC hdc
= ::GetDC(NULL
);
122 int logPPIScreenX
= ::GetDeviceCaps(hdc
, LOGPIXELSX
);
123 int logPPIScreenY
= ::GetDeviceCaps(hdc
, LOGPIXELSY
);
124 ::ReleaseDC(NULL
, hdc
);
126 int logPPIPrinterX
= ::GetDeviceCaps((HDC
) impl
->GetHDC(), LOGPIXELSX
);
127 int logPPIPrinterY
= ::GetDeviceCaps((HDC
) impl
->GetHDC(), LOGPIXELSY
);
128 if (logPPIPrinterX
== 0 || logPPIPrinterY
== 0)
131 sm_lastError
= wxPRINTER_ERROR
;
135 printout
->SetPPIScreen(logPPIScreenX
, logPPIScreenY
);
136 printout
->SetPPIPrinter(logPPIPrinterX
, logPPIPrinterY
);
138 // Set printout parameters
143 printout
->SetPageSizePixels((int)w
, (int)h
);
144 printout
->SetPaperRectPixels(dc
->GetPaperRect());
146 dc
->GetSizeMM(&w
, &h
);
147 printout
->SetPageSizeMM((int)w
, (int)h
);
149 // Create an abort window
150 wxBusyCursor busyCursor
;
152 printout
->OnPreparePrinting();
154 // Get some parameters from the printout, if defined
155 int fromPage
, toPage
;
156 int minPage
, maxPage
;
157 printout
->GetPageInfo(&minPage
, &maxPage
, &fromPage
, &toPage
);
161 sm_lastError
= wxPRINTER_ERROR
;
165 // Only set min and max, because from and to have been
167 m_printDialogData
.SetMinPage(minPage
);
168 m_printDialogData
.SetMaxPage(maxPage
);
170 wxPrintAbortDialog
*win
= CreateAbortWindow(parent
, printout
);
173 ::SetAbortProc(GetHdcOf(*impl
), wxAbortProc
);
177 wxLogDebug(wxT("Could not create an abort dialog."));
178 sm_lastError
= wxPRINTER_ERROR
;
183 sm_abortWindow
= win
;
184 sm_abortWindow
->Show();
187 printout
->OnBeginPrinting();
189 sm_lastError
= wxPRINTER_NO_ERROR
;
191 int minPageNum
= minPage
, maxPageNum
= maxPage
;
193 if ( !m_printDialogData
.GetAllPages() )
195 minPageNum
= m_printDialogData
.GetFromPage();
196 maxPageNum
= m_printDialogData
.GetToPage();
199 const int maxCopyCount
= m_printDialogData
.GetNoCopies();
200 for ( int copyCount
= 1; copyCount
<= maxCopyCount
; copyCount
++ )
202 if ( !printout
->OnBeginDocument(minPageNum
, maxPageNum
) )
204 wxLogError(_("Could not start printing."));
205 sm_lastError
= wxPRINTER_ERROR
;
210 sm_lastError
= wxPRINTER_CANCELLED
;
216 for ( pn
= minPageNum
;
217 pn
<= maxPageNum
&& printout
->HasPage(pn
);
220 win
->SetProgress(pn
- minPageNum
+ 1,
221 maxPageNum
- minPageNum
+ 1,
222 copyCount
, maxCopyCount
);
226 sm_lastError
= wxPRINTER_CANCELLED
;
231 bool cont
= printout
->OnPrintPage(pn
);
236 sm_lastError
= wxPRINTER_CANCELLED
;
241 printout
->OnEndDocument();
244 printout
->OnEndPrinting();
248 sm_abortWindow
->Show(false);
249 wxDELETE(sm_abortWindow
);
254 return sm_lastError
== wxPRINTER_NO_ERROR
;
257 wxDC
*wxWindowsPrinter::PrintDialog(wxWindow
*parent
)
261 wxWindowsPrintDialog
dialog(parent
, & m_printDialogData
);
262 int ret
= dialog
.ShowModal();
266 dc
= dialog
.GetPrintDC();
267 m_printDialogData
= dialog
.GetPrintDialogData();
269 sm_lastError
= wxPRINTER_ERROR
;
271 sm_lastError
= wxPRINTER_NO_ERROR
;
274 sm_lastError
= wxPRINTER_CANCELLED
;
279 bool wxWindowsPrinter::Setup(wxWindow
*WXUNUSED(parent
))
282 // We no longer expose that dialog
283 wxPrintDialog
dialog(parent
, & m_printDialogData
);
284 dialog
.GetPrintDialogData().SetSetupDialog(true);
286 int ret
= dialog
.ShowModal();
290 m_printDialogData
= dialog
.GetPrintDialogData();
293 return (ret
== wxID_OK
);
303 wxWindowsPrintPreview::wxWindowsPrintPreview(wxPrintout
*printout
,
304 wxPrintout
*printoutForPrinting
,
305 wxPrintDialogData
*data
)
306 : wxPrintPreviewBase(printout
, printoutForPrinting
, data
)
311 wxWindowsPrintPreview::wxWindowsPrintPreview(wxPrintout
*printout
,
312 wxPrintout
*printoutForPrinting
,
314 : wxPrintPreviewBase(printout
, printoutForPrinting
, data
)
319 wxWindowsPrintPreview::~wxWindowsPrintPreview()
323 bool wxWindowsPrintPreview::Print(bool interactive
)
325 if (!m_printPrintout
)
327 wxWindowsPrinter
printer(&m_printDialogData
);
328 return printer
.Print(m_previewFrame
, m_printPrintout
, interactive
);
331 void wxWindowsPrintPreview::DetermineScaling()
334 int logPPIScreenX
= ::GetDeviceCaps(dc
, LOGPIXELSX
);
335 int logPPIScreenY
= ::GetDeviceCaps(dc
, LOGPIXELSY
);
336 m_previewPrintout
->SetPPIScreen(logPPIScreenX
, logPPIScreenY
);
338 // Get a device context for the currently selected printer
339 wxPrinterDC
printerDC(m_printDialogData
.GetPrintData());
350 if ( printerDC
.IsOk() )
352 wxPrinterDCImpl
*impl
= (wxPrinterDCImpl
*) printerDC
.GetImpl();
353 HDC dc
= GetHdcOf(*impl
);
354 printerWidthMM
= ::GetDeviceCaps(dc
, HORZSIZE
);
355 printerHeightMM
= ::GetDeviceCaps(dc
, VERTSIZE
);
356 printerXRes
= ::GetDeviceCaps(dc
, HORZRES
);
357 printerYRes
= ::GetDeviceCaps(dc
, VERTRES
);
358 logPPIPrinterX
= ::GetDeviceCaps(dc
, LOGPIXELSX
);
359 logPPIPrinterY
= ::GetDeviceCaps(dc
, LOGPIXELSY
);
361 paperRect
= printerDC
.GetPaperRect();
363 if ( logPPIPrinterX
== 0 ||
364 logPPIPrinterY
== 0 ||
365 printerWidthMM
== 0 ||
366 printerHeightMM
== 0 )
374 printerWidthMM
= 150;
375 printerHeightMM
= 250;
378 logPPIPrinterX
= 600;
379 logPPIPrinterY
= 600;
381 paperRect
= wxRect(0, 0, printerXRes
, printerYRes
);
384 m_pageWidth
= printerXRes
;
385 m_pageHeight
= printerYRes
;
386 m_previewPrintout
->SetPageSizePixels(printerXRes
, printerYRes
);
387 m_previewPrintout
->SetPageSizeMM(printerWidthMM
, printerHeightMM
);
388 m_previewPrintout
->SetPaperRectPixels(paperRect
);
389 m_previewPrintout
->SetPPIPrinter(logPPIPrinterX
, logPPIPrinterY
);
391 // At 100%, the page should look about page-size on the screen.
392 m_previewScaleX
= float(logPPIScreenX
) / logPPIPrinterX
;
393 m_previewScaleY
= float(logPPIScreenY
) / logPPIPrinterY
;
396 #if wxUSE_ENH_METAFILE
397 bool wxWindowsPrintPreview::RenderPageIntoBitmap(wxBitmap
& bmp
, int pageNum
)
399 // The preview, as implemented in wxPrintPreviewBase (and as used prior to
400 // wx3) is inexact: it uses screen DC, which has much lower resolution and
401 // has other properties different from printer DC, so the preview is not
404 // To make matters worse, if the application depends heavily on
405 // GetTextExtent() or does text layout itself, the output in preview and on
406 // paper can be very different. In particular, wxHtmlEasyPrinting is
407 // affected and the preview can be easily off by several pages.
409 // To fix this, we render the preview into high-resolution enhanced
410 // metafile with properties identical to the printer DC. This guarantees
411 // metrics correctness while still being fast.
414 // print the preview into a metafile:
415 wxPrinterDC
printerDC(m_printDialogData
.GetPrintData());
416 wxEnhMetaFileDC
metaDC(printerDC
,
418 printerDC
.GetSize().x
, printerDC
.GetSize().y
);
420 if ( !RenderPageIntoDC(metaDC
, pageNum
) )
423 wxEnhMetaFile
*metafile
= metaDC
.Close();
427 // now render the metafile:
429 bmpDC
.SelectObject(bmp
);
432 wxRect
outRect(0, 0, bmp
.GetWidth(), bmp
.GetHeight());
433 metafile
->Play(&bmpDC
, &outRect
);
438 // TODO: we should keep the metafile and reuse it when changing zoom level
442 #endif // wxUSE_ENH_METAFILE
444 BOOL CALLBACK
wxAbortProc(HDC
WXUNUSED(hdc
), int WXUNUSED(error
))
448 if (!wxPrinterBase::sm_abortWindow
) /* If the abort dialog isn't up yet */
451 /* Process messages intended for the abort dialog box */
453 while (!wxPrinterBase::sm_abortIt
&& ::PeekMessage(&msg
, 0, 0, 0, TRUE
))
454 if (!IsDialogMessage((HWND
) wxPrinterBase::sm_abortWindow
->GetHWND(), &msg
)) {
455 TranslateMessage(&msg
);
456 DispatchMessage(&msg
);
459 /* bAbort is TRUE (return is FALSE) if the user has aborted */
461 return !wxPrinterBase::sm_abortIt
;
465 // wxUSE_PRINTING_ARCHITECTURE