]>
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"
44 #include "wx/msw/printwin.h"
45 #include "wx/msw/printdlg.h"
46 #include "wx/msw/private.h"
47 #include "wx/msw/dcprint.h"
55 // ---------------------------------------------------------------------------
57 // ---------------------------------------------------------------------------
59 LONG APIENTRY _EXPORT
wxAbortProc(HDC hPr
, int Code
);
61 // ---------------------------------------------------------------------------
63 // ---------------------------------------------------------------------------
65 IMPLEMENT_DYNAMIC_CLASS(wxWindowsPrinter
, wxPrinterBase
)
66 IMPLEMENT_CLASS(wxWindowsPrintPreview
, wxPrintPreviewBase
)
68 // ===========================================================================
70 // ===========================================================================
72 // ---------------------------------------------------------------------------
74 // ---------------------------------------------------------------------------
76 wxWindowsPrinter::wxWindowsPrinter(wxPrintDialogData
*data
)
79 m_lpAbortProc
= (WXFARPROC
)wxAbortProc
;
82 wxWindowsPrinter::~wxWindowsPrinter()
84 // avoids warning about statement with no effect (FreeProcInstance
85 // doesn't do anything under Win32)
86 #if !defined(__WIN32__) && !defined(__NT__)
87 FreeProcInstance((FARPROC
) m_lpAbortProc
);
91 bool wxWindowsPrinter::Print(wxWindow
*parent
, wxPrintout
*printout
, bool prompt
)
94 sm_abortWindow
= NULL
;
98 sm_lastError
= wxPRINTER_ERROR
;
102 printout
->SetIsPreview(false);
104 if (m_printDialogData
.GetMinPage() < 1)
105 m_printDialogData
.SetMinPage(1);
106 if (m_printDialogData
.GetMaxPage() < 1)
107 m_printDialogData
.SetMaxPage(9999);
109 // Create a suitable device context
110 wxPrinterDC
*dc
wxDUMMY_INITIALIZE(NULL
);
113 dc
= wxDynamicCast(PrintDialog(parent
), wxPrinterDC
);
119 dc
= new wxPrinterDC(m_printDialogData
.GetPrintData());
122 // May have pressed cancel.
123 if (!dc
|| !dc
->IsOk())
129 wxPrinterDCImpl
*impl
= (wxPrinterDCImpl
*) dc
->GetImpl();
131 HDC hdc
= ::GetDC(NULL
);
132 int logPPIScreenX
= ::GetDeviceCaps(hdc
, LOGPIXELSX
);
133 int logPPIScreenY
= ::GetDeviceCaps(hdc
, LOGPIXELSY
);
134 ::ReleaseDC(NULL
, hdc
);
136 int logPPIPrinterX
= ::GetDeviceCaps((HDC
) impl
->GetHDC(), LOGPIXELSX
);
137 int logPPIPrinterY
= ::GetDeviceCaps((HDC
) impl
->GetHDC(), LOGPIXELSY
);
138 if (logPPIPrinterX
== 0 || logPPIPrinterY
== 0)
141 sm_lastError
= wxPRINTER_ERROR
;
145 printout
->SetPPIScreen(logPPIScreenX
, logPPIScreenY
);
146 printout
->SetPPIPrinter(logPPIPrinterX
, logPPIPrinterY
);
148 // Set printout parameters
153 printout
->SetPageSizePixels((int)w
, (int)h
);
154 printout
->SetPaperRectPixels(dc
->GetPaperRect());
156 dc
->GetSizeMM(&w
, &h
);
157 printout
->SetPageSizeMM((int)w
, (int)h
);
159 // Create an abort window
160 wxBusyCursor busyCursor
;
162 printout
->OnPreparePrinting();
164 // Get some parameters from the printout, if defined
165 int fromPage
, toPage
;
166 int minPage
, maxPage
;
167 printout
->GetPageInfo(&minPage
, &maxPage
, &fromPage
, &toPage
);
171 sm_lastError
= wxPRINTER_ERROR
;
175 // Only set min and max, because from and to have been
177 m_printDialogData
.SetMinPage(minPage
);
178 m_printDialogData
.SetMaxPage(maxPage
);
180 wxWindow
*win
= CreateAbortWindow(parent
, printout
);
183 #if defined(__WATCOMC__) || defined(__BORLANDC__) || defined(__GNUWIN32__) || defined(__SALFORDC__) || !defined(__WIN32__)
185 ::SetAbortProc((HDC
) impl
->GetHDC(), (ABORTPROC
) m_lpAbortProc
);
187 ::SetAbortProc((HDC
) impl
->GetHDC(), (FARPROC
) m_lpAbortProc
);
190 ::SetAbortProc((HDC
) impl
->GetHDC(), (int (_stdcall
*)
191 // cast it to right type only if required
192 // FIXME it's really cdecl and we're casting it to stdcall - either there is
193 // something I don't understand or it will crash at first usage
204 wxLogDebug(wxT("Could not create an abort dialog."));
205 sm_lastError
= wxPRINTER_ERROR
;
210 sm_abortWindow
= win
;
211 sm_abortWindow
->Show();
214 printout
->OnBeginPrinting();
216 sm_lastError
= wxPRINTER_NO_ERROR
;
218 int minPageNum
= minPage
, maxPageNum
= maxPage
;
220 if ( !m_printDialogData
.GetAllPages() )
222 minPageNum
= m_printDialogData
.GetFromPage();
223 maxPageNum
= m_printDialogData
.GetToPage();
228 copyCount
<= m_printDialogData
.GetNoCopies();
231 if ( !printout
->OnBeginDocument(minPageNum
, maxPageNum
) )
233 wxLogError(_("Could not start printing."));
234 sm_lastError
= wxPRINTER_ERROR
;
239 sm_lastError
= wxPRINTER_CANCELLED
;
245 for ( pn
= minPageNum
;
246 pn
<= maxPageNum
&& printout
->HasPage(pn
);
251 sm_lastError
= wxPRINTER_CANCELLED
;
256 bool cont
= printout
->OnPrintPage(pn
);
261 sm_lastError
= wxPRINTER_CANCELLED
;
266 printout
->OnEndDocument();
269 printout
->OnEndPrinting();
273 sm_abortWindow
->Show(false);
274 delete sm_abortWindow
;
275 sm_abortWindow
= NULL
;
280 return sm_lastError
== wxPRINTER_NO_ERROR
;
283 wxDC
*wxWindowsPrinter::PrintDialog(wxWindow
*parent
)
285 wxDC
*dc
= (wxPrinterDC
*) NULL
;
287 wxWindowsPrintDialog
dialog(parent
, & m_printDialogData
);
288 int ret
= dialog
.ShowModal();
292 dc
= dialog
.GetPrintDC();
293 m_printDialogData
= dialog
.GetPrintDialogData();
295 sm_lastError
= wxPRINTER_ERROR
;
297 sm_lastError
= wxPRINTER_NO_ERROR
;
300 sm_lastError
= wxPRINTER_CANCELLED
;
305 bool wxWindowsPrinter::Setup(wxWindow
*WXUNUSED(parent
))
308 // We no longer expose that dialog
309 wxPrintDialog
dialog(parent
, & m_printDialogData
);
310 dialog
.GetPrintDialogData().SetSetupDialog(true);
312 int ret
= dialog
.ShowModal();
316 m_printDialogData
= dialog
.GetPrintDialogData();
319 return (ret
== wxID_OK
);
329 wxWindowsPrintPreview::wxWindowsPrintPreview(wxPrintout
*printout
,
330 wxPrintout
*printoutForPrinting
,
331 wxPrintDialogData
*data
)
332 : wxPrintPreviewBase(printout
, printoutForPrinting
, data
)
337 wxWindowsPrintPreview::wxWindowsPrintPreview(wxPrintout
*printout
,
338 wxPrintout
*printoutForPrinting
,
340 : wxPrintPreviewBase(printout
, printoutForPrinting
, data
)
345 wxWindowsPrintPreview::~wxWindowsPrintPreview()
349 bool wxWindowsPrintPreview::Print(bool interactive
)
351 if (!m_printPrintout
)
353 wxWindowsPrinter
printer(&m_printDialogData
);
354 return printer
.Print(m_previewFrame
, m_printPrintout
, interactive
);
357 void wxWindowsPrintPreview::DetermineScaling()
360 int logPPIScreenX
= ::GetDeviceCaps(dc
, LOGPIXELSX
);
361 int logPPIScreenY
= ::GetDeviceCaps(dc
, LOGPIXELSY
);
362 m_previewPrintout
->SetPPIScreen(logPPIScreenX
, logPPIScreenY
);
364 // Get a device context for the currently selected printer
365 wxPrinterDC
printerDC(m_printDialogData
.GetPrintData());
376 if ( printerDC
.IsOk() )
378 wxPrinterDCImpl
*impl
= (wxPrinterDCImpl
*) printerDC
.GetImpl();
379 HDC dc
= GetHdcOf(*impl
);
380 printerWidthMM
= ::GetDeviceCaps(dc
, HORZSIZE
);
381 printerHeightMM
= ::GetDeviceCaps(dc
, VERTSIZE
);
382 printerXRes
= ::GetDeviceCaps(dc
, HORZRES
);
383 printerYRes
= ::GetDeviceCaps(dc
, VERTRES
);
384 logPPIPrinterX
= ::GetDeviceCaps(dc
, LOGPIXELSX
);
385 logPPIPrinterY
= ::GetDeviceCaps(dc
, LOGPIXELSY
);
387 paperRect
= printerDC
.GetPaperRect();
389 if ( logPPIPrinterX
== 0 ||
390 logPPIPrinterY
== 0 ||
391 printerWidthMM
== 0 ||
392 printerHeightMM
== 0 )
400 printerWidthMM
= 150;
401 printerHeightMM
= 250;
404 logPPIPrinterX
= 600;
405 logPPIPrinterY
= 600;
407 paperRect
= wxRect(0, 0, printerXRes
, printerYRes
);
410 m_pageWidth
= printerXRes
;
411 m_pageHeight
= printerYRes
;
412 m_previewPrintout
->SetPageSizePixels(printerXRes
, printerYRes
);
413 m_previewPrintout
->SetPageSizeMM(printerWidthMM
, printerHeightMM
);
414 m_previewPrintout
->SetPaperRectPixels(paperRect
);
415 m_previewPrintout
->SetPPIPrinter(logPPIPrinterX
, logPPIPrinterY
);
417 // At 100%, the page should look about page-size on the screen.
418 m_previewScaleX
= float(logPPIScreenX
) / logPPIPrinterX
;
419 m_previewScaleY
= float(logPPIScreenY
) / logPPIPrinterY
;
422 /****************************************************************************
424 FUNCTION: wxAbortProc()
426 PURPOSE: Processes messages for the Abort Dialog box
428 ****************************************************************************/
430 LONG APIENTRY _EXPORT
wxAbortProc(HDC
WXUNUSED(hPr
), int WXUNUSED(Code
))
434 if (!wxPrinterBase::sm_abortWindow
) /* If the abort dialog isn't up yet */
437 /* Process messages intended for the abort dialog box */
439 while (!wxPrinterBase::sm_abortIt
&& ::PeekMessage(&msg
, 0, 0, 0, TRUE
))
440 if (!IsDialogMessage((HWND
) wxPrinterBase::sm_abortWindow
->GetHWND(), &msg
)) {
441 TranslateMessage(&msg
);
442 DispatchMessage(&msg
);
445 /* bAbort is TRUE (return is FALSE) if the user has aborted */
447 return !wxPrinterBase::sm_abortIt
;
451 // wxUSE_PRINTING_ARCHITECTURE