]>
git.saurik.com Git - wxWidgets.git/blob - src/msw/printwin.cpp
1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxWindowsPrinter framework
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ===========================================================================
14 // ===========================================================================
16 // ---------------------------------------------------------------------------
18 // ---------------------------------------------------------------------------
20 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
21 #pragma implementation "printwin.h"
24 // For compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
33 // Don't use the Windows printer if we're in wxUniv mode and using
34 // the PostScript architecture
35 #if wxUSE_PRINTING_ARCHITECTURE && (!defined(__WXUNIVERSAL__) || !wxUSE_POSTSCRIPT_ARCHITECTURE_IN_MSW)
38 #include "wx/window.h"
39 #include "wx/msw/private.h"
43 #include "wx/msgdlg.h"
47 #include "wx/msw/printwin.h"
48 #include "wx/dcprint.h"
49 #include "wx/printdlg.h"
51 #include "wx/msw/private.h"
55 #include "wx/msw/private.h"
63 // ---------------------------------------------------------------------------
65 // ---------------------------------------------------------------------------
67 LONG APIENTRY _EXPORT
wxAbortProc(HDC hPr
, int Code
);
69 // ---------------------------------------------------------------------------
71 // ---------------------------------------------------------------------------
73 IMPLEMENT_DYNAMIC_CLASS(wxWindowsPrinter
, wxPrinterBase
)
74 IMPLEMENT_CLASS(wxWindowsPrintPreview
, wxPrintPreviewBase
)
76 // ===========================================================================
78 // ===========================================================================
80 // ---------------------------------------------------------------------------
82 // ---------------------------------------------------------------------------
84 wxWindowsPrinter::wxWindowsPrinter(wxPrintDialogData
*data
)
87 m_lpAbortProc
= (WXFARPROC
) MakeProcInstance((FARPROC
) wxAbortProc
, wxGetInstance());
90 wxWindowsPrinter::~wxWindowsPrinter()
92 // avoids warning about statement with no effect (FreeProcInstance
93 // doesn't do anything under Win32)
94 #if !defined(__WIN32__) && !defined(__NT__)
95 FreeProcInstance((FARPROC
) m_lpAbortProc
);
99 bool wxWindowsPrinter::Print(wxWindow
*parent
, wxPrintout
*printout
, bool prompt
)
102 sm_abortWindow
= NULL
;
106 sm_lastError
= wxPRINTER_ERROR
;
110 printout
->SetIsPreview(false);
112 if (m_printDialogData
.GetMinPage() < 1)
113 m_printDialogData
.SetMinPage(1);
114 if (m_printDialogData
.GetMaxPage() < 1)
115 m_printDialogData
.SetMaxPage(9999);
117 // Create a suitable device context
118 wxDC
*dc
wxDUMMY_INITIALIZE(NULL
);
121 dc
= PrintDialog(parent
);
127 dc
= new wxPrinterDC(m_printDialogData
.GetPrintData());
130 // May have pressed cancel.
131 if (!dc
|| !dc
->Ok())
137 HDC hdc
= ::GetDC(NULL
);
138 int logPPIScreenX
= ::GetDeviceCaps(hdc
, LOGPIXELSX
);
139 int logPPIScreenY
= ::GetDeviceCaps(hdc
, LOGPIXELSY
);
140 ::ReleaseDC(NULL
, hdc
);
142 int logPPIPrinterX
= ::GetDeviceCaps((HDC
) dc
->GetHDC(), LOGPIXELSX
);
143 int logPPIPrinterY
= ::GetDeviceCaps((HDC
) dc
->GetHDC(), LOGPIXELSY
);
144 if (logPPIPrinterX
== 0 || logPPIPrinterY
== 0)
147 sm_lastError
= wxPRINTER_ERROR
;
151 printout
->SetPPIScreen(logPPIScreenX
, logPPIScreenY
);
152 printout
->SetPPIPrinter(logPPIPrinterX
, logPPIPrinterY
);
154 // Set printout parameters
159 printout
->SetPageSizePixels((int)w
, (int)h
);
161 dc
->GetSizeMM(&w
, &h
);
162 printout
->SetPageSizeMM((int)w
, (int)h
);
164 // Create an abort window
165 wxBusyCursor busyCursor
;
167 printout
->OnPreparePrinting();
169 // Get some parameters from the printout, if defined
170 int fromPage
, toPage
;
171 int minPage
, maxPage
;
172 printout
->GetPageInfo(&minPage
, &maxPage
, &fromPage
, &toPage
);
176 sm_lastError
= wxPRINTER_ERROR
;
180 // Only set min and max, because from and to have been
182 m_printDialogData
.SetMinPage(minPage
);
183 m_printDialogData
.SetMaxPage(maxPage
);
185 wxWindow
*win
= CreateAbortWindow(parent
, printout
);
188 #if defined(__WATCOMC__) || defined(__BORLANDC__) || defined(__GNUWIN32__) || defined(__SALFORDC__) || !defined(__WIN32__)
190 ::SetAbortProc((HDC
) dc
->GetHDC(), (ABORTPROC
) m_lpAbortProc
);
192 ::SetAbortProc((HDC
) dc
->GetHDC(), (FARPROC
) m_lpAbortProc
);
195 ::SetAbortProc((HDC
) dc
->GetHDC(), (int (_stdcall
*)
196 // cast it to right type only if required
197 // FIXME it's really cdecl and we're casting it to stdcall - either there is
198 // something I don't understand or it will crash at first usage
209 wxLogDebug(wxT("Could not create an abort dialog."));
210 sm_lastError
= wxPRINTER_ERROR
;
215 sm_abortWindow
= win
;
216 sm_abortWindow
->Show();
219 printout
->OnBeginPrinting();
221 sm_lastError
= wxPRINTER_NO_ERROR
;
223 int minPageNum
= minPage
, maxPageNum
= maxPage
;
225 if ( !m_printDialogData
.GetAllPages() )
227 minPageNum
= m_printDialogData
.GetFromPage();
228 maxPageNum
= m_printDialogData
.GetToPage();
233 copyCount
<= m_printDialogData
.GetNoCopies();
236 if ( !printout
->OnBeginDocument(minPageNum
, maxPageNum
) )
238 wxLogError(_("Could not start printing."));
239 sm_lastError
= wxPRINTER_ERROR
;
244 sm_lastError
= wxPRINTER_CANCELLED
;
250 for ( pn
= minPageNum
;
251 pn
<= maxPageNum
&& printout
->HasPage(pn
);
256 sm_lastError
= wxPRINTER_CANCELLED
;
261 bool cont
= printout
->OnPrintPage(pn
);
266 sm_lastError
= wxPRINTER_CANCELLED
;
271 printout
->OnEndDocument();
274 printout
->OnEndPrinting();
278 sm_abortWindow
->Show(false);
279 delete sm_abortWindow
;
280 sm_abortWindow
= NULL
;
285 return sm_lastError
== wxPRINTER_NO_ERROR
;
288 wxDC
* wxWindowsPrinter::PrintDialog(wxWindow
*parent
)
290 wxDC
* dc
= (wxDC
*) NULL
;
292 wxPrintDialog
dialog(parent
, & m_printDialogData
);
293 int ret
= dialog
.ShowModal();
297 dc
= dialog
.GetPrintDC();
298 m_printDialogData
= dialog
.GetPrintDialogData();
300 sm_lastError
= wxPRINTER_ERROR
;
302 sm_lastError
= wxPRINTER_NO_ERROR
;
305 sm_lastError
= wxPRINTER_CANCELLED
;
310 bool wxWindowsPrinter::Setup(wxWindow
*WXUNUSED(parent
))
313 // We no longer expose that dialog
314 wxPrintDialog
dialog(parent
, & m_printDialogData
);
315 dialog
.GetPrintDialogData().SetSetupDialog(true);
317 int ret
= dialog
.ShowModal();
321 m_printDialogData
= dialog
.GetPrintDialogData();
324 return (ret
== wxID_OK
);
334 wxWindowsPrintPreview::wxWindowsPrintPreview(wxPrintout
*printout
,
335 wxPrintout
*printoutForPrinting
,
336 wxPrintDialogData
*data
)
337 : wxPrintPreviewBase(printout
, printoutForPrinting
, data
)
342 wxWindowsPrintPreview::wxWindowsPrintPreview(wxPrintout
*printout
,
343 wxPrintout
*printoutForPrinting
,
345 : wxPrintPreviewBase(printout
, printoutForPrinting
, data
)
350 wxWindowsPrintPreview::~wxWindowsPrintPreview()
354 bool wxWindowsPrintPreview::Print(bool interactive
)
356 if (!m_printPrintout
)
358 wxWindowsPrinter
printer(&m_printDialogData
);
359 return printer
.Print(m_previewFrame
, m_printPrintout
, interactive
);
362 void wxWindowsPrintPreview::DetermineScaling()
364 HDC dc
= ::GetDC(NULL
);
365 int screenWidth
= ::GetDeviceCaps(dc
, HORZSIZE
);
366 int screenYRes
= ::GetDeviceCaps(dc
, VERTRES
);
367 int logPPIScreenX
= ::GetDeviceCaps(dc
, LOGPIXELSX
);
368 int logPPIScreenY
= ::GetDeviceCaps(dc
, LOGPIXELSY
);
369 m_previewPrintout
->SetPPIScreen(logPPIScreenX
, logPPIScreenY
);
371 ::ReleaseDC(NULL
, dc
);
373 // Get a device context for the currently selected printer
374 wxPrinterDC
printerDC(m_printDialogData
.GetPrintData());
376 int printerWidth
= 150;
377 int printerHeight
wxDUMMY_INITIALIZE(250);
378 int printerXRes
= 1500;
379 int printerYRes
= 2500;
381 dc
= GetHdcOf(printerDC
);
384 printerWidth
= ::GetDeviceCaps(dc
, HORZSIZE
);
385 printerHeight
= ::GetDeviceCaps(dc
, VERTSIZE
);
386 printerXRes
= ::GetDeviceCaps(dc
, HORZRES
);
387 printerYRes
= ::GetDeviceCaps(dc
, VERTRES
);
389 int logPPIPrinterX
= ::GetDeviceCaps(dc
, LOGPIXELSX
);
390 int logPPIPrinterY
= ::GetDeviceCaps(dc
, LOGPIXELSY
);
392 m_previewPrintout
->SetPPIPrinter(logPPIPrinterX
, logPPIPrinterY
);
393 m_previewPrintout
->SetPageSizeMM(printerWidth
, printerHeight
);
395 if (logPPIPrinterX
== 0 || logPPIPrinterY
== 0 || printerWidth
== 0 || printerHeight
== 0)
401 m_pageWidth
= printerXRes
;
402 m_pageHeight
= printerYRes
;
404 // At 100%, the page should look about page-size on the screen.
405 m_previewScale
= (float)((float)screenWidth
/(float)printerWidth
);
406 m_previewScale
= m_previewScale
* (float)((float)screenYRes
/(float)printerYRes
);
409 /****************************************************************************
411 FUNCTION: wxAbortProc()
413 PURPOSE: Processes messages for the Abort Dialog box
415 ****************************************************************************/
417 LONG APIENTRY _EXPORT
wxAbortProc(HDC
WXUNUSED(hPr
), int WXUNUSED(Code
))
421 if (!wxPrinterBase::sm_abortWindow
) /* If the abort dialog isn't up yet */
424 /* Process messages intended for the abort dialog box */
426 while (!wxPrinterBase::sm_abortIt
&& ::PeekMessage(&msg
, 0, 0, 0, TRUE
))
427 if (!IsDialogMessage((HWND
) wxPrinterBase::sm_abortWindow
->GetHWND(), &msg
)) {
428 TranslateMessage(&msg
);
429 DispatchMessage(&msg
);
432 /* bAbort is TRUE (return is FALSE) if the user has aborted */
434 return !wxPrinterBase::sm_abortIt
;
438 // wxUSE_PRINTING_ARCHITECTURE