]>
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 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
29 // Don't use the Windows printer if we're in wxUniv mode and using
30 // the PostScript architecture
31 #if wxUSE_PRINTING_ARCHITECTURE && (!defined(__WXUNIVERSAL__) || !wxUSE_POSTSCRIPT_ARCHITECTURE_IN_MSW)
34 #include "wx/window.h"
35 #include "wx/msw/private.h"
39 #include "wx/msgdlg.h"
43 #include "wx/msw/printwin.h"
44 #include "wx/dcprint.h"
45 #include "wx/printdlg.h"
47 #include "wx/msw/private.h"
51 #include "wx/msw/wrapcdlg.h"
56 // ---------------------------------------------------------------------------
58 // ---------------------------------------------------------------------------
60 LONG APIENTRY _EXPORT
wxAbortProc(HDC hPr
, int Code
);
62 // ---------------------------------------------------------------------------
64 // ---------------------------------------------------------------------------
66 IMPLEMENT_DYNAMIC_CLASS(wxWindowsPrinter
, wxPrinterBase
)
67 IMPLEMENT_CLASS(wxWindowsPrintPreview
, wxPrintPreviewBase
)
69 // ===========================================================================
71 // ===========================================================================
73 // ---------------------------------------------------------------------------
75 // ---------------------------------------------------------------------------
77 wxWindowsPrinter::wxWindowsPrinter(wxPrintDialogData
*data
)
80 m_lpAbortProc
= (WXFARPROC
) MakeProcInstance((FARPROC
) wxAbortProc
, wxGetInstance());
83 wxWindowsPrinter::~wxWindowsPrinter()
85 // avoids warning about statement with no effect (FreeProcInstance
86 // doesn't do anything under Win32)
87 #if !defined(__WIN32__) && !defined(__NT__)
88 FreeProcInstance((FARPROC
) m_lpAbortProc
);
92 bool wxWindowsPrinter::Print(wxWindow
*parent
, wxPrintout
*printout
, bool prompt
)
95 sm_abortWindow
= NULL
;
99 sm_lastError
= wxPRINTER_ERROR
;
103 printout
->SetIsPreview(false);
105 if (m_printDialogData
.GetMinPage() < 1)
106 m_printDialogData
.SetMinPage(1);
107 if (m_printDialogData
.GetMaxPage() < 1)
108 m_printDialogData
.SetMaxPage(9999);
110 // Create a suitable device context
111 wxDC
*dc
wxDUMMY_INITIALIZE(NULL
);
114 dc
= PrintDialog(parent
);
120 dc
= new wxPrinterDC(m_printDialogData
.GetPrintData());
123 // May have pressed cancel.
124 if (!dc
|| !dc
->Ok())
130 HDC hdc
= ::GetDC(NULL
);
131 int logPPIScreenX
= ::GetDeviceCaps(hdc
, LOGPIXELSX
);
132 int logPPIScreenY
= ::GetDeviceCaps(hdc
, LOGPIXELSY
);
133 ::ReleaseDC(NULL
, hdc
);
135 int logPPIPrinterX
= ::GetDeviceCaps((HDC
) dc
->GetHDC(), LOGPIXELSX
);
136 int logPPIPrinterY
= ::GetDeviceCaps((HDC
) dc
->GetHDC(), LOGPIXELSY
);
137 if (logPPIPrinterX
== 0 || logPPIPrinterY
== 0)
140 sm_lastError
= wxPRINTER_ERROR
;
144 printout
->SetPPIScreen(logPPIScreenX
, logPPIScreenY
);
145 printout
->SetPPIPrinter(logPPIPrinterX
, logPPIPrinterY
);
147 // Set printout parameters
152 printout
->SetPageSizePixels((int)w
, (int)h
);
154 dc
->GetSizeMM(&w
, &h
);
155 printout
->SetPageSizeMM((int)w
, (int)h
);
157 // Create an abort window
158 wxBusyCursor busyCursor
;
160 printout
->OnPreparePrinting();
162 // Get some parameters from the printout, if defined
163 int fromPage
, toPage
;
164 int minPage
, maxPage
;
165 printout
->GetPageInfo(&minPage
, &maxPage
, &fromPage
, &toPage
);
169 sm_lastError
= wxPRINTER_ERROR
;
173 // Only set min and max, because from and to have been
175 m_printDialogData
.SetMinPage(minPage
);
176 m_printDialogData
.SetMaxPage(maxPage
);
178 wxWindow
*win
= CreateAbortWindow(parent
, printout
);
181 #if defined(__WATCOMC__) || defined(__BORLANDC__) || defined(__GNUWIN32__) || defined(__SALFORDC__) || !defined(__WIN32__)
183 ::SetAbortProc((HDC
) dc
->GetHDC(), (ABORTPROC
) m_lpAbortProc
);
185 ::SetAbortProc((HDC
) dc
->GetHDC(), (FARPROC
) m_lpAbortProc
);
188 ::SetAbortProc((HDC
) dc
->GetHDC(), (int (_stdcall
*)
189 // cast it to right type only if required
190 // FIXME it's really cdecl and we're casting it to stdcall - either there is
191 // something I don't understand or it will crash at first usage
202 wxLogDebug(wxT("Could not create an abort dialog."));
203 sm_lastError
= wxPRINTER_ERROR
;
208 sm_abortWindow
= win
;
209 sm_abortWindow
->Show();
212 printout
->OnBeginPrinting();
214 sm_lastError
= wxPRINTER_NO_ERROR
;
216 int minPageNum
= minPage
, maxPageNum
= maxPage
;
218 if ( !m_printDialogData
.GetAllPages() )
220 minPageNum
= m_printDialogData
.GetFromPage();
221 maxPageNum
= m_printDialogData
.GetToPage();
226 copyCount
<= m_printDialogData
.GetNoCopies();
229 if ( !printout
->OnBeginDocument(minPageNum
, maxPageNum
) )
231 wxLogError(_("Could not start printing."));
232 sm_lastError
= wxPRINTER_ERROR
;
237 sm_lastError
= wxPRINTER_CANCELLED
;
243 for ( pn
= minPageNum
;
244 pn
<= maxPageNum
&& printout
->HasPage(pn
);
249 sm_lastError
= wxPRINTER_CANCELLED
;
254 bool cont
= printout
->OnPrintPage(pn
);
259 sm_lastError
= wxPRINTER_CANCELLED
;
264 printout
->OnEndDocument();
267 printout
->OnEndPrinting();
271 sm_abortWindow
->Show(false);
272 delete sm_abortWindow
;
273 sm_abortWindow
= NULL
;
278 return sm_lastError
== wxPRINTER_NO_ERROR
;
281 wxDC
* wxWindowsPrinter::PrintDialog(wxWindow
*parent
)
283 wxDC
* dc
= (wxDC
*) NULL
;
285 wxPrintDialog
dialog(parent
, & m_printDialogData
);
286 int ret
= dialog
.ShowModal();
290 dc
= dialog
.GetPrintDC();
291 m_printDialogData
= dialog
.GetPrintDialogData();
293 sm_lastError
= wxPRINTER_ERROR
;
295 sm_lastError
= wxPRINTER_NO_ERROR
;
298 sm_lastError
= wxPRINTER_CANCELLED
;
303 bool wxWindowsPrinter::Setup(wxWindow
*WXUNUSED(parent
))
306 // We no longer expose that dialog
307 wxPrintDialog
dialog(parent
, & m_printDialogData
);
308 dialog
.GetPrintDialogData().SetSetupDialog(true);
310 int ret
= dialog
.ShowModal();
314 m_printDialogData
= dialog
.GetPrintDialogData();
317 return (ret
== wxID_OK
);
327 wxWindowsPrintPreview::wxWindowsPrintPreview(wxPrintout
*printout
,
328 wxPrintout
*printoutForPrinting
,
329 wxPrintDialogData
*data
)
330 : wxPrintPreviewBase(printout
, printoutForPrinting
, data
)
335 wxWindowsPrintPreview::wxWindowsPrintPreview(wxPrintout
*printout
,
336 wxPrintout
*printoutForPrinting
,
338 : wxPrintPreviewBase(printout
, printoutForPrinting
, data
)
343 wxWindowsPrintPreview::~wxWindowsPrintPreview()
347 bool wxWindowsPrintPreview::Print(bool interactive
)
349 if (!m_printPrintout
)
351 wxWindowsPrinter
printer(&m_printDialogData
);
352 return printer
.Print(m_previewFrame
, m_printPrintout
, interactive
);
355 void wxWindowsPrintPreview::DetermineScaling()
357 HDC dc
= ::GetDC(NULL
);
358 int screenWidth
= ::GetDeviceCaps(dc
, HORZSIZE
);
359 int screenYRes
= ::GetDeviceCaps(dc
, VERTRES
);
360 int logPPIScreenX
= ::GetDeviceCaps(dc
, LOGPIXELSX
);
361 int logPPIScreenY
= ::GetDeviceCaps(dc
, LOGPIXELSY
);
362 m_previewPrintout
->SetPPIScreen(logPPIScreenX
, logPPIScreenY
);
364 ::ReleaseDC(NULL
, dc
);
366 // Get a device context for the currently selected printer
367 wxPrinterDC
printerDC(m_printDialogData
.GetPrintData());
369 int printerWidth
= 150;
370 int printerHeight
wxDUMMY_INITIALIZE(250);
371 int printerXRes
= 1500;
372 int printerYRes
= 2500;
374 dc
= GetHdcOf(printerDC
);
377 printerWidth
= ::GetDeviceCaps(dc
, HORZSIZE
);
378 printerHeight
= ::GetDeviceCaps(dc
, VERTSIZE
);
379 printerXRes
= ::GetDeviceCaps(dc
, HORZRES
);
380 printerYRes
= ::GetDeviceCaps(dc
, VERTRES
);
382 int logPPIPrinterX
= ::GetDeviceCaps(dc
, LOGPIXELSX
);
383 int logPPIPrinterY
= ::GetDeviceCaps(dc
, LOGPIXELSY
);
385 m_previewPrintout
->SetPPIPrinter(logPPIPrinterX
, logPPIPrinterY
);
386 m_previewPrintout
->SetPageSizeMM(printerWidth
, printerHeight
);
388 if (logPPIPrinterX
== 0 || logPPIPrinterY
== 0 || printerWidth
== 0 || printerHeight
== 0)
394 m_pageWidth
= printerXRes
;
395 m_pageHeight
= printerYRes
;
397 // At 100%, the page should look about page-size on the screen.
398 m_previewScale
= (float)((float)screenWidth
/(float)printerWidth
);
399 m_previewScale
= m_previewScale
* (float)((float)screenYRes
/(float)printerYRes
);
402 /****************************************************************************
404 FUNCTION: wxAbortProc()
406 PURPOSE: Processes messages for the Abort Dialog box
408 ****************************************************************************/
410 LONG APIENTRY _EXPORT
wxAbortProc(HDC
WXUNUSED(hPr
), int WXUNUSED(Code
))
414 if (!wxPrinterBase::sm_abortWindow
) /* If the abort dialog isn't up yet */
417 /* Process messages intended for the abort dialog box */
419 while (!wxPrinterBase::sm_abortIt
&& ::PeekMessage(&msg
, 0, 0, 0, TRUE
))
420 if (!IsDialogMessage((HWND
) wxPrinterBase::sm_abortWindow
->GetHWND(), &msg
)) {
421 TranslateMessage(&msg
);
422 DispatchMessage(&msg
);
425 /* bAbort is TRUE (return is FALSE) if the user has aborted */
427 return !wxPrinterBase::sm_abortIt
;
431 // wxUSE_PRINTING_ARCHITECTURE