]>
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 // Get some parameters from the printout, if defined
97 printout
->GetPageInfo(&minPage
, &maxPage
, &fromPage
, &toPage
);
99 m_printDialogData
.SetFromPage(fromPage
);
100 m_printDialogData
.SetToPage(toPage
);
101 m_printDialogData
.SetMinPage(minPage
);
102 m_printDialogData
.SetMaxPage(maxPage
);
103 m_printDialogData
.SetAllPages((fromPage
== minPage
) && (toPage
== maxPage
));
105 // Create a suitable device context
106 wxPrinterDC
*dc
wxDUMMY_INITIALIZE(NULL
);
109 dc
= wxDynamicCast(PrintDialog(parent
), wxPrinterDC
);
115 dc
= new wxPrinterDC(m_printDialogData
.GetPrintData());
118 // May have pressed cancel.
119 if (!dc
|| !dc
->IsOk())
125 wxPrinterDCImpl
*impl
= (wxPrinterDCImpl
*) dc
->GetImpl();
127 HDC hdc
= ::GetDC(NULL
);
128 int logPPIScreenX
= ::GetDeviceCaps(hdc
, LOGPIXELSX
);
129 int logPPIScreenY
= ::GetDeviceCaps(hdc
, LOGPIXELSY
);
130 ::ReleaseDC(NULL
, hdc
);
132 int logPPIPrinterX
= ::GetDeviceCaps((HDC
) impl
->GetHDC(), LOGPIXELSX
);
133 int logPPIPrinterY
= ::GetDeviceCaps((HDC
) impl
->GetHDC(), LOGPIXELSY
);
134 if (logPPIPrinterX
== 0 || logPPIPrinterY
== 0)
137 sm_lastError
= wxPRINTER_ERROR
;
141 printout
->SetPPIScreen(logPPIScreenX
, logPPIScreenY
);
142 printout
->SetPPIPrinter(logPPIPrinterX
, logPPIPrinterY
);
144 // Set printout parameters
149 printout
->SetPageSizePixels((int)w
, (int)h
);
150 printout
->SetPaperRectPixels(dc
->GetPaperRect());
152 dc
->GetSizeMM(&w
, &h
);
153 printout
->SetPageSizeMM((int)w
, (int)h
);
155 // Create an abort window
156 wxBusyCursor busyCursor
;
158 printout
->OnPreparePrinting();
162 sm_lastError
= wxPRINTER_ERROR
;
166 // Only set min and max, because from and to have been
168 m_printDialogData
.SetMinPage(minPage
);
169 m_printDialogData
.SetMaxPage(maxPage
);
171 wxWindow
*win
= CreateAbortWindow(parent
, printout
);
174 ::SetAbortProc(GetHdcOf(*impl
), wxAbortProc
);
178 wxLogDebug(wxT("Could not create an abort dialog."));
179 sm_lastError
= wxPRINTER_ERROR
;
184 sm_abortWindow
= win
;
185 sm_abortWindow
->Show();
188 printout
->OnBeginPrinting();
190 sm_lastError
= wxPRINTER_NO_ERROR
;
192 int minPageNum
= minPage
, maxPageNum
= maxPage
;
194 if ( !m_printDialogData
.GetAllPages() )
196 minPageNum
= m_printDialogData
.GetFromPage();
197 maxPageNum
= m_printDialogData
.GetToPage();
202 copyCount
<= m_printDialogData
.GetNoCopies();
205 if ( !printout
->OnBeginDocument(minPageNum
, maxPageNum
) )
207 wxLogError(_("Could not start printing."));
208 sm_lastError
= wxPRINTER_ERROR
;
213 sm_lastError
= wxPRINTER_CANCELLED
;
219 for ( pn
= minPageNum
;
220 pn
<= maxPageNum
&& printout
->HasPage(pn
);
225 sm_lastError
= wxPRINTER_CANCELLED
;
230 bool cont
= printout
->OnPrintPage(pn
);
235 sm_lastError
= wxPRINTER_CANCELLED
;
240 printout
->OnEndDocument();
243 printout
->OnEndPrinting();
247 sm_abortWindow
->Show(false);
248 wxDELETE(sm_abortWindow
);
253 return sm_lastError
== wxPRINTER_NO_ERROR
;
256 wxDC
*wxWindowsPrinter::PrintDialog(wxWindow
*parent
)
260 wxWindowsPrintDialog
dialog(parent
, & m_printDialogData
);
261 int ret
= dialog
.ShowModal();
265 dc
= dialog
.GetPrintDC();
266 m_printDialogData
= dialog
.GetPrintDialogData();
268 sm_lastError
= wxPRINTER_ERROR
;
270 sm_lastError
= wxPRINTER_NO_ERROR
;
273 sm_lastError
= wxPRINTER_CANCELLED
;
278 bool wxWindowsPrinter::Setup(wxWindow
*WXUNUSED(parent
))
281 // We no longer expose that dialog
282 wxPrintDialog
dialog(parent
, & m_printDialogData
);
283 dialog
.GetPrintDialogData().SetSetupDialog(true);
285 int ret
= dialog
.ShowModal();
289 m_printDialogData
= dialog
.GetPrintDialogData();
292 return (ret
== wxID_OK
);
302 wxWindowsPrintPreview::wxWindowsPrintPreview(wxPrintout
*printout
,
303 wxPrintout
*printoutForPrinting
,
304 wxPrintDialogData
*data
)
305 : wxPrintPreviewBase(printout
, printoutForPrinting
, data
)
310 wxWindowsPrintPreview::wxWindowsPrintPreview(wxPrintout
*printout
,
311 wxPrintout
*printoutForPrinting
,
313 : wxPrintPreviewBase(printout
, printoutForPrinting
, data
)
318 wxWindowsPrintPreview::~wxWindowsPrintPreview()
322 bool wxWindowsPrintPreview::Print(bool interactive
)
324 if (!m_printPrintout
)
326 wxWindowsPrinter
printer(&m_printDialogData
);
327 return printer
.Print(m_previewFrame
, m_printPrintout
, interactive
);
330 void wxWindowsPrintPreview::DetermineScaling()
333 int logPPIScreenX
= ::GetDeviceCaps(dc
, LOGPIXELSX
);
334 int logPPIScreenY
= ::GetDeviceCaps(dc
, LOGPIXELSY
);
335 m_previewPrintout
->SetPPIScreen(logPPIScreenX
, logPPIScreenY
);
337 // Get a device context for the currently selected printer
338 wxPrinterDC
printerDC(m_printDialogData
.GetPrintData());
349 if ( printerDC
.IsOk() )
351 wxPrinterDCImpl
*impl
= (wxPrinterDCImpl
*) printerDC
.GetImpl();
352 HDC dc
= GetHdcOf(*impl
);
353 printerWidthMM
= ::GetDeviceCaps(dc
, HORZSIZE
);
354 printerHeightMM
= ::GetDeviceCaps(dc
, VERTSIZE
);
355 printerXRes
= ::GetDeviceCaps(dc
, HORZRES
);
356 printerYRes
= ::GetDeviceCaps(dc
, VERTRES
);
357 logPPIPrinterX
= ::GetDeviceCaps(dc
, LOGPIXELSX
);
358 logPPIPrinterY
= ::GetDeviceCaps(dc
, LOGPIXELSY
);
360 paperRect
= printerDC
.GetPaperRect();
362 if ( logPPIPrinterX
== 0 ||
363 logPPIPrinterY
== 0 ||
364 printerWidthMM
== 0 ||
365 printerHeightMM
== 0 )
373 printerWidthMM
= 150;
374 printerHeightMM
= 250;
377 logPPIPrinterX
= 600;
378 logPPIPrinterY
= 600;
380 paperRect
= wxRect(0, 0, printerXRes
, printerYRes
);
383 m_pageWidth
= printerXRes
;
384 m_pageHeight
= printerYRes
;
385 m_previewPrintout
->SetPageSizePixels(printerXRes
, printerYRes
);
386 m_previewPrintout
->SetPageSizeMM(printerWidthMM
, printerHeightMM
);
387 m_previewPrintout
->SetPaperRectPixels(paperRect
);
388 m_previewPrintout
->SetPPIPrinter(logPPIPrinterX
, logPPIPrinterY
);
390 // At 100%, the page should look about page-size on the screen.
391 m_previewScaleX
= float(logPPIScreenX
) / logPPIPrinterX
;
392 m_previewScaleY
= float(logPPIScreenY
) / logPPIPrinterY
;
395 #if wxUSE_ENH_METAFILE
396 bool wxWindowsPrintPreview::RenderPageIntoBitmap(wxBitmap
& bmp
, int pageNum
)
398 // The preview, as implemented in wxPrintPreviewBase (and as used prior to
399 // wx3) is inexact: it uses screen DC, which has much lower resolution and
400 // has other properties different from printer DC, so the preview is not
403 // To make matters worse, if the application depends heavily on
404 // GetTextExtent() or does text layout itself, the output in preview and on
405 // paper can be very different. In particular, wxHtmlEasyPrinting is
406 // affected and the preview can be easily off by several pages.
408 // To fix this, we render the preview into high-resolution enhanced
409 // metafile with properties identical to the printer DC. This guarantees
410 // metrics correctness while still being fast.
413 // print the preview into a metafile:
414 wxPrinterDC
printerDC(m_printDialogData
.GetPrintData());
415 wxEnhMetaFileDC
metaDC(printerDC
,
417 printerDC
.GetSize().x
, printerDC
.GetSize().y
);
419 if ( !RenderPageIntoDC(metaDC
, pageNum
) )
422 wxEnhMetaFile
*metafile
= metaDC
.Close();
426 // now render the metafile:
428 bmpDC
.SelectObject(bmp
);
431 wxRect
outRect(0, 0, bmp
.GetWidth(), bmp
.GetHeight());
432 metafile
->Play(&bmpDC
, &outRect
);
437 // TODO: we should keep the metafile and reuse it when changing zoom level
441 #endif // wxUSE_ENH_METAFILE
443 BOOL CALLBACK
wxAbortProc(HDC
WXUNUSED(hdc
), int WXUNUSED(error
))
447 if (!wxPrinterBase::sm_abortWindow
) /* If the abort dialog isn't up yet */
450 /* Process messages intended for the abort dialog box */
452 while (!wxPrinterBase::sm_abortIt
&& ::PeekMessage(&msg
, 0, 0, 0, TRUE
))
453 if (!IsDialogMessage((HWND
) wxPrinterBase::sm_abortWindow
->GetHWND(), &msg
)) {
454 TranslateMessage(&msg
);
455 DispatchMessage(&msg
);
458 /* bAbort is TRUE (return is FALSE) if the user has aborted */
460 return !wxPrinterBase::sm_abortIt
;
464 // wxUSE_PRINTING_ARCHITECTURE