]>
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
121 dc
= PrintDialog(parent
);
127 dc
= new wxPrinterDC(m_printDialogData
.GetPrintData());
130 // May have pressed cancel.
131 if (!dc
|| !dc
->Ok())
137 int logPPIScreenX
= 0;
138 int logPPIScreenY
= 0;
139 int logPPIPrinterX
= 0;
140 int logPPIPrinterY
= 0;
142 HDC hdc
= ::GetDC(NULL
);
143 logPPIScreenX
= ::GetDeviceCaps(hdc
, LOGPIXELSX
);
144 logPPIScreenY
= ::GetDeviceCaps(hdc
, LOGPIXELSY
);
145 ::ReleaseDC(NULL
, hdc
);
147 logPPIPrinterX
= ::GetDeviceCaps((HDC
) dc
->GetHDC(), LOGPIXELSX
);
148 logPPIPrinterY
= ::GetDeviceCaps((HDC
) dc
->GetHDC(), LOGPIXELSY
);
149 if (logPPIPrinterX
== 0 || logPPIPrinterY
== 0)
152 sm_lastError
= wxPRINTER_ERROR
;
156 printout
->SetPPIScreen(logPPIScreenX
, logPPIScreenY
);
157 printout
->SetPPIPrinter(logPPIPrinterX
, logPPIPrinterY
);
159 // Set printout parameters
164 printout
->SetPageSizePixels((int)w
, (int)h
);
166 dc
->GetSizeMM(&w
, &h
);
167 printout
->SetPageSizeMM((int)w
, (int)h
);
169 // Create an abort window
172 printout
->OnPreparePrinting();
174 // Get some parameters from the printout, if defined
175 int fromPage
, toPage
;
176 int minPage
, maxPage
;
177 printout
->GetPageInfo(&minPage
, &maxPage
, &fromPage
, &toPage
);
181 sm_lastError
= wxPRINTER_ERROR
;
186 // Only set min and max, because from and to have been
188 m_printDialogData
.SetMinPage(minPage
);
189 m_printDialogData
.SetMaxPage(maxPage
);
191 wxWindow
*win
= CreateAbortWindow(parent
, printout
);
194 #if defined(__BORLANDC__) || defined(__GNUWIN32__) || defined(__SALFORDC__) || !defined(__WIN32__)
196 ::SetAbortProc((HDC
) dc
->GetHDC(), (ABORTPROC
) m_lpAbortProc
);
198 ::SetAbortProc((HDC
) dc
->GetHDC(), (FARPROC
) m_lpAbortProc
);
201 ::SetAbortProc((HDC
) dc
->GetHDC(), (int (_stdcall
*)
202 // cast it to right type only if required
203 // FIXME it's really cdecl and we're casting it to stdcall - either there is
204 // something I don't understand or it will crash at first usage
216 wxLogDebug(wxT("Could not create an abort dialog."));
217 sm_lastError
= wxPRINTER_ERROR
;
221 sm_abortWindow
= win
;
222 sm_abortWindow
->Show();
225 printout
->OnBeginPrinting();
227 sm_lastError
= wxPRINTER_NO_ERROR
;
229 int minPageNum
= minPage
, maxPageNum
= maxPage
;
231 if ( !m_printDialogData
.GetAllPages() )
233 minPageNum
= m_printDialogData
.GetFromPage();
234 maxPageNum
= m_printDialogData
.GetToPage();
239 copyCount
<= m_printDialogData
.GetNoCopies();
242 if ( !printout
->OnBeginDocument(minPageNum
, maxPageNum
) )
245 wxLogError(_("Could not start printing."));
246 sm_lastError
= wxPRINTER_ERROR
;
251 sm_lastError
= wxPRINTER_CANCELLED
;
257 for ( pn
= minPageNum
;
258 pn
<= maxPageNum
&& printout
->HasPage(pn
);
263 sm_lastError
= wxPRINTER_CANCELLED
;
268 bool cont
= printout
->OnPrintPage(pn
);
273 sm_lastError
= wxPRINTER_CANCELLED
;
278 printout
->OnEndDocument();
281 printout
->OnEndPrinting();
285 sm_abortWindow
->Show(false);
286 delete sm_abortWindow
;
287 sm_abortWindow
= NULL
;
294 return (sm_lastError
== wxPRINTER_NO_ERROR
);
297 wxDC
* wxWindowsPrinter::PrintDialog(wxWindow
*parent
)
299 wxDC
* dc
= (wxDC
*) NULL
;
301 wxPrintDialog
dialog(parent
, & m_printDialogData
);
302 int ret
= dialog
.ShowModal();
306 dc
= dialog
.GetPrintDC();
307 m_printDialogData
= dialog
.GetPrintDialogData();
309 sm_lastError
= wxPRINTER_ERROR
;
311 sm_lastError
= wxPRINTER_NO_ERROR
;
314 sm_lastError
= wxPRINTER_CANCELLED
;
319 bool wxWindowsPrinter::Setup(wxWindow
*parent
)
321 wxPrintDialog
dialog(parent
, & m_printDialogData
);
322 dialog
.GetPrintDialogData().SetSetupDialog(true);
324 int ret
= dialog
.ShowModal();
328 m_printDialogData
= dialog
.GetPrintDialogData();
331 return (ret
== wxID_OK
);
338 wxWindowsPrintPreview::wxWindowsPrintPreview(wxPrintout
*printout
,
339 wxPrintout
*printoutForPrinting
,
340 wxPrintDialogData
*data
)
341 : wxPrintPreviewBase(printout
, printoutForPrinting
, data
)
346 wxWindowsPrintPreview::wxWindowsPrintPreview(wxPrintout
*printout
,
347 wxPrintout
*printoutForPrinting
,
349 : wxPrintPreviewBase(printout
, printoutForPrinting
, data
)
354 wxWindowsPrintPreview::~wxWindowsPrintPreview()
358 bool wxWindowsPrintPreview::Print(bool interactive
)
360 if (!m_printPrintout
)
362 wxWindowsPrinter
printer(&m_printDialogData
);
363 return printer
.Print(m_previewFrame
, m_printPrintout
, interactive
);
366 void wxWindowsPrintPreview::DetermineScaling()
368 HDC dc
= ::GetDC(NULL
);
369 int screenWidth
= ::GetDeviceCaps(dc
, HORZSIZE
);
370 int screenYRes
= ::GetDeviceCaps(dc
, VERTRES
);
371 int logPPIScreenX
= ::GetDeviceCaps(dc
, LOGPIXELSX
);
372 int logPPIScreenY
= ::GetDeviceCaps(dc
, LOGPIXELSY
);
373 m_previewPrintout
->SetPPIScreen(logPPIScreenX
, logPPIScreenY
);
375 ::ReleaseDC(NULL
, dc
);
377 // Get a device context for the currently selected printer
378 wxPrinterDC
printerDC(m_printDialogData
.GetPrintData());
380 int printerWidth
= 150;
381 int printerHeight
= 250;
382 int printerXRes
= 1500;
383 int printerYRes
= 2500;
385 dc
= GetHdcOf(printerDC
);
388 printerWidth
= ::GetDeviceCaps(dc
, HORZSIZE
);
389 printerHeight
= ::GetDeviceCaps(dc
, VERTSIZE
);
390 printerXRes
= ::GetDeviceCaps(dc
, HORZRES
);
391 printerYRes
= ::GetDeviceCaps(dc
, VERTRES
);
393 int logPPIPrinterX
= ::GetDeviceCaps(dc
, LOGPIXELSX
);
394 int logPPIPrinterY
= ::GetDeviceCaps(dc
, LOGPIXELSY
);
396 m_previewPrintout
->SetPPIPrinter(logPPIPrinterX
, logPPIPrinterY
);
397 m_previewPrintout
->SetPageSizeMM(printerWidth
, printerHeight
);
399 if (logPPIPrinterX
== 0 || logPPIPrinterY
== 0 || printerWidth
== 0 || printerHeight
== 0)
405 m_pageWidth
= printerXRes
;
406 m_pageHeight
= printerYRes
;
408 // At 100%, the page should look about page-size on the screen.
409 m_previewScale
= (float)((float)screenWidth
/(float)printerWidth
);
410 m_previewScale
= m_previewScale
* (float)((float)screenYRes
/(float)printerYRes
);
413 /****************************************************************************
415 FUNCTION: wxAbortProc()
417 PURPOSE: Processes messages for the Abort Dialog box
419 ****************************************************************************/
421 LONG APIENTRY _EXPORT
wxAbortProc(HDC
WXUNUSED(hPr
), int WXUNUSED(Code
))
425 if (!wxPrinterBase::sm_abortWindow
) /* If the abort dialog isn't up yet */
428 /* Process messages intended for the abort dialog box */
430 while (!wxPrinterBase::sm_abortIt
&& PeekMessage(&msg
, 0, 0, 0, TRUE
))
431 if (!IsDialogMessage((HWND
) wxPrinterBase::sm_abortWindow
->GetHWND(), &msg
)) {
432 TranslateMessage(&msg
);
433 DispatchMessage(&msg
);
436 /* bAbort is TRUE (return is FALSE) if the user has aborted */
438 return (!wxPrinterBase::sm_abortIt
);
442 // wxUSE_PRINTING_ARCHITECTURE