]>
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"
54 // ---------------------------------------------------------------------------
56 // ---------------------------------------------------------------------------
58 LONG APIENTRY _EXPORT
wxAbortProc(HDC hPr
, int Code
);
60 // ---------------------------------------------------------------------------
62 // ---------------------------------------------------------------------------
64 IMPLEMENT_DYNAMIC_CLASS(wxWindowsPrinter
, wxPrinterBase
)
65 IMPLEMENT_CLASS(wxWindowsPrintPreview
, wxPrintPreviewBase
)
67 // ===========================================================================
69 // ===========================================================================
71 // ---------------------------------------------------------------------------
73 // ---------------------------------------------------------------------------
75 wxWindowsPrinter::wxWindowsPrinter(wxPrintDialogData
*data
)
78 m_lpAbortProc
= (WXFARPROC
)wxAbortProc
;
81 wxWindowsPrinter::~wxWindowsPrinter()
83 // avoids warning about statement with no effect (FreeProcInstance
84 // doesn't do anything under Win32)
85 #if !defined(__WIN32__) && !defined(__NT__)
86 FreeProcInstance((FARPROC
) m_lpAbortProc
);
90 bool wxWindowsPrinter::Print(wxWindow
*parent
, wxPrintout
*printout
, bool prompt
)
93 sm_abortWindow
= NULL
;
97 sm_lastError
= wxPRINTER_ERROR
;
101 printout
->SetIsPreview(false);
103 if (m_printDialogData
.GetMinPage() < 1)
104 m_printDialogData
.SetMinPage(1);
105 if (m_printDialogData
.GetMaxPage() < 1)
106 m_printDialogData
.SetMaxPage(9999);
108 // Create a suitable device context
109 wxPrinterDC
*dc
wxDUMMY_INITIALIZE(NULL
);
112 dc
= wxDynamicCast(PrintDialog(parent
), wxPrinterDC
);
118 dc
= new wxPrinterDC(m_printDialogData
.GetPrintData());
121 // May have pressed cancel.
122 if (!dc
|| !dc
->Ok())
128 HDC hdc
= ::GetDC(NULL
);
129 int logPPIScreenX
= ::GetDeviceCaps(hdc
, LOGPIXELSX
);
130 int logPPIScreenY
= ::GetDeviceCaps(hdc
, LOGPIXELSY
);
131 ::ReleaseDC(NULL
, hdc
);
133 int logPPIPrinterX
= ::GetDeviceCaps((HDC
) dc
->GetHDC(), LOGPIXELSX
);
134 int logPPIPrinterY
= ::GetDeviceCaps((HDC
) dc
->GetHDC(), LOGPIXELSY
);
135 if (logPPIPrinterX
== 0 || logPPIPrinterY
== 0)
138 sm_lastError
= wxPRINTER_ERROR
;
142 printout
->SetPPIScreen(logPPIScreenX
, logPPIScreenY
);
143 printout
->SetPPIPrinter(logPPIPrinterX
, logPPIPrinterY
);
145 // Set printout parameters
150 printout
->SetPageSizePixels((int)w
, (int)h
);
151 printout
->SetPaperRectPixels(dc
->GetPaperRect());
153 dc
->GetSizeMM(&w
, &h
);
154 printout
->SetPageSizeMM((int)w
, (int)h
);
156 // Create an abort window
157 wxBusyCursor busyCursor
;
159 printout
->OnPreparePrinting();
161 // Get some parameters from the printout, if defined
162 int fromPage
, toPage
;
163 int minPage
, maxPage
;
164 printout
->GetPageInfo(&minPage
, &maxPage
, &fromPage
, &toPage
);
168 sm_lastError
= wxPRINTER_ERROR
;
172 // Only set min and max, because from and to have been
174 m_printDialogData
.SetMinPage(minPage
);
175 m_printDialogData
.SetMaxPage(maxPage
);
177 wxWindow
*win
= CreateAbortWindow(parent
, printout
);
180 #if defined(__WATCOMC__) || defined(__BORLANDC__) || defined(__GNUWIN32__) || defined(__SALFORDC__) || !defined(__WIN32__)
182 ::SetAbortProc((HDC
) dc
->GetHDC(), (ABORTPROC
) m_lpAbortProc
);
184 ::SetAbortProc((HDC
) dc
->GetHDC(), (FARPROC
) m_lpAbortProc
);
187 ::SetAbortProc((HDC
) dc
->GetHDC(), (int (_stdcall
*)
188 // cast it to right type only if required
189 // FIXME it's really cdecl and we're casting it to stdcall - either there is
190 // something I don't understand or it will crash at first usage
201 wxLogDebug(wxT("Could not create an abort dialog."));
202 sm_lastError
= wxPRINTER_ERROR
;
207 sm_abortWindow
= win
;
208 sm_abortWindow
->Show();
211 printout
->OnBeginPrinting();
213 sm_lastError
= wxPRINTER_NO_ERROR
;
215 int minPageNum
= minPage
, maxPageNum
= maxPage
;
217 if ( !m_printDialogData
.GetAllPages() )
219 minPageNum
= m_printDialogData
.GetFromPage();
220 maxPageNum
= m_printDialogData
.GetToPage();
225 copyCount
<= m_printDialogData
.GetNoCopies();
228 if ( !printout
->OnBeginDocument(minPageNum
, maxPageNum
) )
230 wxLogError(_("Could not start printing."));
231 sm_lastError
= wxPRINTER_ERROR
;
236 sm_lastError
= wxPRINTER_CANCELLED
;
242 for ( pn
= minPageNum
;
243 pn
<= maxPageNum
&& printout
->HasPage(pn
);
248 sm_lastError
= wxPRINTER_CANCELLED
;
253 bool cont
= printout
->OnPrintPage(pn
);
258 sm_lastError
= wxPRINTER_CANCELLED
;
263 printout
->OnEndDocument();
266 printout
->OnEndPrinting();
270 sm_abortWindow
->Show(false);
271 delete sm_abortWindow
;
272 sm_abortWindow
= NULL
;
277 return sm_lastError
== wxPRINTER_NO_ERROR
;
280 wxDC
*wxWindowsPrinter::PrintDialog(wxWindow
*parent
)
282 wxDC
*dc
= (wxPrinterDC
*) NULL
;
284 wxWindowsPrintDialog
dialog(parent
, & m_printDialogData
);
285 int ret
= dialog
.ShowModal();
289 dc
= dialog
.GetPrintDC();
290 m_printDialogData
= dialog
.GetPrintDialogData();
292 sm_lastError
= wxPRINTER_ERROR
;
294 sm_lastError
= wxPRINTER_NO_ERROR
;
297 sm_lastError
= wxPRINTER_CANCELLED
;
302 bool wxWindowsPrinter::Setup(wxWindow
*WXUNUSED(parent
))
305 // We no longer expose that dialog
306 wxPrintDialog
dialog(parent
, & m_printDialogData
);
307 dialog
.GetPrintDialogData().SetSetupDialog(true);
309 int ret
= dialog
.ShowModal();
313 m_printDialogData
= dialog
.GetPrintDialogData();
316 return (ret
== wxID_OK
);
326 wxWindowsPrintPreview::wxWindowsPrintPreview(wxPrintout
*printout
,
327 wxPrintout
*printoutForPrinting
,
328 wxPrintDialogData
*data
)
329 : wxPrintPreviewBase(printout
, printoutForPrinting
, data
)
334 wxWindowsPrintPreview::wxWindowsPrintPreview(wxPrintout
*printout
,
335 wxPrintout
*printoutForPrinting
,
337 : wxPrintPreviewBase(printout
, printoutForPrinting
, data
)
342 wxWindowsPrintPreview::~wxWindowsPrintPreview()
346 bool wxWindowsPrintPreview::Print(bool interactive
)
348 if (!m_printPrintout
)
350 wxWindowsPrinter
printer(&m_printDialogData
);
351 return printer
.Print(m_previewFrame
, m_printPrintout
, interactive
);
354 void wxWindowsPrintPreview::DetermineScaling()
357 int logPPIScreenX
= ::GetDeviceCaps(dc
, LOGPIXELSX
);
358 int logPPIScreenY
= ::GetDeviceCaps(dc
, LOGPIXELSY
);
359 m_previewPrintout
->SetPPIScreen(logPPIScreenX
, logPPIScreenY
);
361 // Get a device context for the currently selected printer
362 wxPrinterDC
printerDC(m_printDialogData
.GetPrintData());
373 if ( printerDC
.Ok() )
375 HDC dc
= GetHdcOf(printerDC
);
376 printerWidthMM
= ::GetDeviceCaps(dc
, HORZSIZE
);
377 printerHeightMM
= ::GetDeviceCaps(dc
, VERTSIZE
);
378 printerXRes
= ::GetDeviceCaps(dc
, HORZRES
);
379 printerYRes
= ::GetDeviceCaps(dc
, VERTRES
);
380 logPPIPrinterX
= ::GetDeviceCaps(dc
, LOGPIXELSX
);
381 logPPIPrinterY
= ::GetDeviceCaps(dc
, LOGPIXELSY
);
383 paperRect
= printerDC
.GetPaperRect();
385 if ( logPPIPrinterX
== 0 ||
386 logPPIPrinterY
== 0 ||
387 printerWidthMM
== 0 ||
388 printerHeightMM
== 0 )
396 printerWidthMM
= 150;
397 printerHeightMM
= 250;
400 logPPIPrinterX
= 600;
401 logPPIPrinterY
= 600;
403 paperRect
= wxRect(0, 0, printerXRes
, printerYRes
);
406 m_pageWidth
= printerXRes
;
407 m_pageHeight
= printerYRes
;
408 m_previewPrintout
->SetPageSizePixels(printerXRes
, printerYRes
);
409 m_previewPrintout
->SetPageSizeMM(printerWidthMM
, printerHeightMM
);
410 m_previewPrintout
->SetPaperRectPixels(paperRect
);
411 m_previewPrintout
->SetPPIPrinter(logPPIPrinterX
, logPPIPrinterY
);
413 // At 100%, the page should look about page-size on the screen.
414 m_previewScaleX
= float(logPPIScreenX
) / logPPIPrinterX
;
415 m_previewScaleY
= float(logPPIScreenY
) / logPPIPrinterY
;
418 /****************************************************************************
420 FUNCTION: wxAbortProc()
422 PURPOSE: Processes messages for the Abort Dialog box
424 ****************************************************************************/
426 LONG APIENTRY _EXPORT
wxAbortProc(HDC
WXUNUSED(hPr
), int WXUNUSED(Code
))
430 if (!wxPrinterBase::sm_abortWindow
) /* If the abort dialog isn't up yet */
433 /* Process messages intended for the abort dialog box */
435 while (!wxPrinterBase::sm_abortIt
&& ::PeekMessage(&msg
, 0, 0, 0, TRUE
))
436 if (!IsDialogMessage((HWND
) wxPrinterBase::sm_abortWindow
->GetHWND(), &msg
)) {
437 TranslateMessage(&msg
);
438 DispatchMessage(&msg
);
441 /* bAbort is TRUE (return is FALSE) if the user has aborted */
443 return !wxPrinterBase::sm_abortIt
;
447 // wxUSE_PRINTING_ARCHITECTURE