]>
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/wrapcdlg.h"
60 // ---------------------------------------------------------------------------
62 // ---------------------------------------------------------------------------
64 LONG APIENTRY _EXPORT
wxAbortProc(HDC hPr
, int Code
);
66 // ---------------------------------------------------------------------------
68 // ---------------------------------------------------------------------------
70 IMPLEMENT_DYNAMIC_CLASS(wxWindowsPrinter
, wxPrinterBase
)
71 IMPLEMENT_CLASS(wxWindowsPrintPreview
, wxPrintPreviewBase
)
73 // ===========================================================================
75 // ===========================================================================
77 // ---------------------------------------------------------------------------
79 // ---------------------------------------------------------------------------
81 wxWindowsPrinter::wxWindowsPrinter(wxPrintDialogData
*data
)
84 m_lpAbortProc
= (WXFARPROC
) MakeProcInstance((FARPROC
) wxAbortProc
, wxGetInstance());
87 wxWindowsPrinter::~wxWindowsPrinter()
89 // avoids warning about statement with no effect (FreeProcInstance
90 // doesn't do anything under Win32)
91 #if !defined(__WIN32__) && !defined(__NT__)
92 FreeProcInstance((FARPROC
) m_lpAbortProc
);
96 bool wxWindowsPrinter::Print(wxWindow
*parent
, wxPrintout
*printout
, bool prompt
)
99 sm_abortWindow
= NULL
;
103 sm_lastError
= wxPRINTER_ERROR
;
107 printout
->SetIsPreview(false);
109 if (m_printDialogData
.GetMinPage() < 1)
110 m_printDialogData
.SetMinPage(1);
111 if (m_printDialogData
.GetMaxPage() < 1)
112 m_printDialogData
.SetMaxPage(9999);
114 // Create a suitable device context
115 wxDC
*dc
wxDUMMY_INITIALIZE(NULL
);
118 dc
= PrintDialog(parent
);
124 dc
= new wxPrinterDC(m_printDialogData
.GetPrintData());
127 // May have pressed cancel.
128 if (!dc
|| !dc
->Ok())
134 HDC hdc
= ::GetDC(NULL
);
135 int logPPIScreenX
= ::GetDeviceCaps(hdc
, LOGPIXELSX
);
136 int logPPIScreenY
= ::GetDeviceCaps(hdc
, LOGPIXELSY
);
137 ::ReleaseDC(NULL
, hdc
);
139 int logPPIPrinterX
= ::GetDeviceCaps((HDC
) dc
->GetHDC(), LOGPIXELSX
);
140 int logPPIPrinterY
= ::GetDeviceCaps((HDC
) dc
->GetHDC(), LOGPIXELSY
);
141 if (logPPIPrinterX
== 0 || logPPIPrinterY
== 0)
144 sm_lastError
= wxPRINTER_ERROR
;
148 printout
->SetPPIScreen(logPPIScreenX
, logPPIScreenY
);
149 printout
->SetPPIPrinter(logPPIPrinterX
, logPPIPrinterY
);
151 // Set printout parameters
156 printout
->SetPageSizePixels((int)w
, (int)h
);
158 dc
->GetSizeMM(&w
, &h
);
159 printout
->SetPageSizeMM((int)w
, (int)h
);
161 // Create an abort window
162 wxBusyCursor busyCursor
;
164 printout
->OnPreparePrinting();
166 // Get some parameters from the printout, if defined
167 int fromPage
, toPage
;
168 int minPage
, maxPage
;
169 printout
->GetPageInfo(&minPage
, &maxPage
, &fromPage
, &toPage
);
173 sm_lastError
= wxPRINTER_ERROR
;
177 // Only set min and max, because from and to have been
179 m_printDialogData
.SetMinPage(minPage
);
180 m_printDialogData
.SetMaxPage(maxPage
);
182 wxWindow
*win
= CreateAbortWindow(parent
, printout
);
185 #if defined(__WATCOMC__) || defined(__BORLANDC__) || defined(__GNUWIN32__) || defined(__SALFORDC__) || !defined(__WIN32__)
187 ::SetAbortProc((HDC
) dc
->GetHDC(), (ABORTPROC
) m_lpAbortProc
);
189 ::SetAbortProc((HDC
) dc
->GetHDC(), (FARPROC
) m_lpAbortProc
);
192 ::SetAbortProc((HDC
) dc
->GetHDC(), (int (_stdcall
*)
193 // cast it to right type only if required
194 // FIXME it's really cdecl and we're casting it to stdcall - either there is
195 // something I don't understand or it will crash at first usage
206 wxLogDebug(wxT("Could not create an abort dialog."));
207 sm_lastError
= wxPRINTER_ERROR
;
212 sm_abortWindow
= win
;
213 sm_abortWindow
->Show();
216 printout
->OnBeginPrinting();
218 sm_lastError
= wxPRINTER_NO_ERROR
;
220 int minPageNum
= minPage
, maxPageNum
= maxPage
;
222 if ( !m_printDialogData
.GetAllPages() )
224 minPageNum
= m_printDialogData
.GetFromPage();
225 maxPageNum
= m_printDialogData
.GetToPage();
230 copyCount
<= m_printDialogData
.GetNoCopies();
233 if ( !printout
->OnBeginDocument(minPageNum
, maxPageNum
) )
235 wxLogError(_("Could not start printing."));
236 sm_lastError
= wxPRINTER_ERROR
;
241 sm_lastError
= wxPRINTER_CANCELLED
;
247 for ( pn
= minPageNum
;
248 pn
<= maxPageNum
&& printout
->HasPage(pn
);
253 sm_lastError
= wxPRINTER_CANCELLED
;
258 bool cont
= printout
->OnPrintPage(pn
);
263 sm_lastError
= wxPRINTER_CANCELLED
;
268 printout
->OnEndDocument();
271 printout
->OnEndPrinting();
275 sm_abortWindow
->Show(false);
276 delete sm_abortWindow
;
277 sm_abortWindow
= NULL
;
282 return sm_lastError
== wxPRINTER_NO_ERROR
;
285 wxDC
* wxWindowsPrinter::PrintDialog(wxWindow
*parent
)
287 wxDC
* dc
= (wxDC
*) NULL
;
289 wxPrintDialog
dialog(parent
, & m_printDialogData
);
290 int ret
= dialog
.ShowModal();
294 dc
= dialog
.GetPrintDC();
295 m_printDialogData
= dialog
.GetPrintDialogData();
297 sm_lastError
= wxPRINTER_ERROR
;
299 sm_lastError
= wxPRINTER_NO_ERROR
;
302 sm_lastError
= wxPRINTER_CANCELLED
;
307 bool wxWindowsPrinter::Setup(wxWindow
*WXUNUSED(parent
))
310 // We no longer expose that dialog
311 wxPrintDialog
dialog(parent
, & m_printDialogData
);
312 dialog
.GetPrintDialogData().SetSetupDialog(true);
314 int ret
= dialog
.ShowModal();
318 m_printDialogData
= dialog
.GetPrintDialogData();
321 return (ret
== wxID_OK
);
331 wxWindowsPrintPreview::wxWindowsPrintPreview(wxPrintout
*printout
,
332 wxPrintout
*printoutForPrinting
,
333 wxPrintDialogData
*data
)
334 : wxPrintPreviewBase(printout
, printoutForPrinting
, data
)
339 wxWindowsPrintPreview::wxWindowsPrintPreview(wxPrintout
*printout
,
340 wxPrintout
*printoutForPrinting
,
342 : wxPrintPreviewBase(printout
, printoutForPrinting
, data
)
347 wxWindowsPrintPreview::~wxWindowsPrintPreview()
351 bool wxWindowsPrintPreview::Print(bool interactive
)
353 if (!m_printPrintout
)
355 wxWindowsPrinter
printer(&m_printDialogData
);
356 return printer
.Print(m_previewFrame
, m_printPrintout
, interactive
);
359 void wxWindowsPrintPreview::DetermineScaling()
361 HDC dc
= ::GetDC(NULL
);
362 int screenWidth
= ::GetDeviceCaps(dc
, HORZSIZE
);
363 int screenYRes
= ::GetDeviceCaps(dc
, VERTRES
);
364 int logPPIScreenX
= ::GetDeviceCaps(dc
, LOGPIXELSX
);
365 int logPPIScreenY
= ::GetDeviceCaps(dc
, LOGPIXELSY
);
366 m_previewPrintout
->SetPPIScreen(logPPIScreenX
, logPPIScreenY
);
368 ::ReleaseDC(NULL
, dc
);
370 // Get a device context for the currently selected printer
371 wxPrinterDC
printerDC(m_printDialogData
.GetPrintData());
373 int printerWidth
= 150;
374 int printerHeight
wxDUMMY_INITIALIZE(250);
375 int printerXRes
= 1500;
376 int printerYRes
= 2500;
378 dc
= GetHdcOf(printerDC
);
381 printerWidth
= ::GetDeviceCaps(dc
, HORZSIZE
);
382 printerHeight
= ::GetDeviceCaps(dc
, VERTSIZE
);
383 printerXRes
= ::GetDeviceCaps(dc
, HORZRES
);
384 printerYRes
= ::GetDeviceCaps(dc
, VERTRES
);
386 int logPPIPrinterX
= ::GetDeviceCaps(dc
, LOGPIXELSX
);
387 int logPPIPrinterY
= ::GetDeviceCaps(dc
, LOGPIXELSY
);
389 m_previewPrintout
->SetPPIPrinter(logPPIPrinterX
, logPPIPrinterY
);
390 m_previewPrintout
->SetPageSizeMM(printerWidth
, printerHeight
);
392 if (logPPIPrinterX
== 0 || logPPIPrinterY
== 0 || printerWidth
== 0 || printerHeight
== 0)
398 m_pageWidth
= printerXRes
;
399 m_pageHeight
= printerYRes
;
401 // At 100%, the page should look about page-size on the screen.
402 m_previewScale
= (float)((float)screenWidth
/(float)printerWidth
);
403 m_previewScale
= m_previewScale
* (float)((float)screenYRes
/(float)printerYRes
);
406 /****************************************************************************
408 FUNCTION: wxAbortProc()
410 PURPOSE: Processes messages for the Abort Dialog box
412 ****************************************************************************/
414 LONG APIENTRY _EXPORT
wxAbortProc(HDC
WXUNUSED(hPr
), int WXUNUSED(Code
))
418 if (!wxPrinterBase::sm_abortWindow
) /* If the abort dialog isn't up yet */
421 /* Process messages intended for the abort dialog box */
423 while (!wxPrinterBase::sm_abortIt
&& ::PeekMessage(&msg
, 0, 0, 0, TRUE
))
424 if (!IsDialogMessage((HWND
) wxPrinterBase::sm_abortWindow
->GetHWND(), &msg
)) {
425 TranslateMessage(&msg
);
426 DispatchMessage(&msg
);
429 /* bAbort is TRUE (return is FALSE) if the user has aborted */
431 return !wxPrinterBase::sm_abortIt
;
435 // wxUSE_PRINTING_ARCHITECTURE