]>
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 and Markus Holzem
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
13 #pragma implementation "printwin.h"
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
29 #include "wx/msgdlg.h"
32 #include "wx/msw/printwin.h"
33 #include "wx/dcprint.h"
34 #include "wx/printdlg.h"
35 #include "wx/msw/private.h"
41 // Clash with Windows header files
50 LONG APIENTRY _EXPORT
wxAbortProc(HDC hPr
, int Code
);
52 #if !USE_SHARED_LIBRARY
53 IMPLEMENT_DYNAMIC_CLASS(wxWindowsPrinter
, wxPrinterBase
)
54 IMPLEMENT_CLASS(wxWindowsPrintPreview
, wxPrintPreviewBase
)
61 wxWindowsPrinter::wxWindowsPrinter(wxPrintData
*data
):
64 m_lpAbortProc
= (WXFARPROC
) MakeProcInstance((FARPROC
) wxAbortProc
, wxGetInstance());
67 wxWindowsPrinter::~wxWindowsPrinter(void)
69 FreeProcInstance((FARPROC
) m_lpAbortProc
);
72 bool wxWindowsPrinter::Print(wxWindow
*parent
, wxPrintout
*printout
, bool prompt
)
75 sm_abortWindow
= NULL
;
80 printout
->SetIsPreview(FALSE
);
81 printout
->OnPreparePrinting();
83 // Get some parameters from the printout, if defined
86 printout
->GetPageInfo(&minPage
, &maxPage
, &fromPage
, &toPage
);
91 m_printData
.SetMinPage(minPage
);
92 m_printData
.SetMaxPage(maxPage
);
94 m_printData
.SetFromPage(fromPage
);
96 m_printData
.SetToPage(toPage
);
100 m_printData
.EnablePageNumbers(TRUE
);
101 if (m_printData
.GetFromPage() < m_printData
.GetMinPage())
102 m_printData
.SetFromPage(m_printData
.GetMinPage());
103 else if (m_printData
.GetFromPage() > m_printData
.GetMaxPage())
104 m_printData
.SetFromPage(m_printData
.GetMaxPage());
105 if (m_printData
.GetToPage() > m_printData
.GetMaxPage())
106 m_printData
.SetToPage(m_printData
.GetMaxPage());
107 else if (m_printData
.GetToPage() < m_printData
.GetMinPage())
108 m_printData
.SetToPage(m_printData
.GetMinPage());
111 m_printData
.EnablePageNumbers(FALSE
);
113 // Create a suitable device context
117 wxPrintDialog
dialog(parent
, & m_printData
);
118 if (dialog
.ShowModal() == wxID_OK
)
120 dc
= dialog
.GetPrintDC();
121 m_printData
= dialog
.GetPrintData();
126 dc
= new wxPrinterDC("", "", "", FALSE
, m_printData
.GetOrientation());
129 // May have pressed cancel.
130 if (!dc
|| !dc
->Ok())
136 int logPPIScreenX
= 0;
137 int logPPIScreenY
= 0;
138 int logPPIPrinterX
= 0;
139 int logPPIPrinterY
= 0;
141 HDC hdc
= ::GetDC(NULL
);
142 logPPIScreenX
= ::GetDeviceCaps(hdc
, LOGPIXELSX
);
143 logPPIScreenY
= ::GetDeviceCaps(hdc
, LOGPIXELSY
);
144 ::ReleaseDC(NULL
, hdc
);
146 logPPIPrinterX
= ::GetDeviceCaps((HDC
) dc
->GetHDC(), LOGPIXELSX
);
147 logPPIPrinterY
= ::GetDeviceCaps((HDC
) dc
->GetHDC(), LOGPIXELSY
);
148 if (logPPIPrinterX
== 0 || logPPIPrinterY
== 0)
154 printout
->SetPPIScreen(logPPIScreenX
, logPPIScreenY
);
155 printout
->SetPPIPrinter(logPPIPrinterX
, logPPIPrinterY
);
157 // Set printout parameters
163 printout
->SetPageSizePixels((int)w
, (int)h
);
164 dc
->GetSizeMM(&ww
, &hh
);
165 printout
->SetPageSizeMM((int)ww
, (int)hh
);
167 // Create an abort window
170 wxWindow
*win
= CreateAbortWindow(parent
, printout
);
173 #if defined(__BORLANDC__) || defined(__GNUWIN32__) || defined(__SALFORDC__) || !defined(__WIN32__)
174 ::SetAbortProc((HDC
) dc
->GetHDC(), (FARPROC
) m_lpAbortProc
);
176 ::SetAbortProc((HDC
) dc
->GetHDC(), (int (_stdcall
*)
177 // cast it to right type only if required
178 // @@@ it's really cdecl and we're casting it to stdcall - either there is
179 // something I don't understand or it will crash at first usage
191 wxMessageBox("Sorry, could not create an abort dialog.", "Print Error", wxOK
, parent
);
194 sm_abortWindow
= win
;
195 sm_abortWindow
->Show(TRUE
);
198 printout
->OnBeginPrinting();
200 bool keepGoing
= TRUE
;
203 for (copyCount
= 1; copyCount
<= m_printData
.GetNoCopies(); copyCount
++)
205 if (!printout
->OnBeginDocument(m_printData
.GetFromPage(), m_printData
.GetToPage()))
208 wxMessageBox("Could not start printing.", "Print Error", wxOK
, parent
);
215 for (pn
= m_printData
.GetFromPage(); keepGoing
&& (pn
<= m_printData
.GetToPage()) && printout
->HasPage(pn
);
225 // int dcID = ::SaveDC(dc->GetHDC());
227 printout
->OnPrintPage(pn
);
229 // ::RestoreDC(dc->GetHDC(), dcID);
232 printout
->OnEndDocument();
235 printout
->OnEndPrinting();
239 sm_abortWindow
->Show(FALSE
);
240 delete sm_abortWindow
;
241 sm_abortWindow
= NULL
;
251 bool wxWindowsPrinter::PrintDialog(wxWindow
*parent
)
253 wxPrintDialog
dialog(parent
, & m_printData
);
254 return (dialog
.ShowModal() == wxID_OK
);
257 bool wxWindowsPrinter::Setup(wxWindow
*parent
)
259 wxPrintDialog
dialog(parent
, & m_printData
);
260 dialog
.GetPrintData().SetSetupDialog(TRUE
);
261 return (dialog
.ShowModal() == wxID_OK
);
268 wxWindowsPrintPreview::wxWindowsPrintPreview(wxPrintout
*printout
, wxPrintout
*printoutForPrinting
, wxPrintData
*data
):
269 wxPrintPreviewBase(printout
, printoutForPrinting
, data
)
274 wxWindowsPrintPreview::~wxWindowsPrintPreview(void)
278 bool wxWindowsPrintPreview::Print(bool interactive
)
280 if (!m_printPrintout
)
282 wxWindowsPrinter
printer(&m_printData
);
283 return printer
.Print(m_previewFrame
, m_printPrintout
, interactive
);
286 void wxWindowsPrintPreview::DetermineScaling(void)
288 HDC dc
= ::GetDC(NULL
);
289 int screenWidth
= ::GetDeviceCaps(dc
, HORZSIZE
);
290 // int screenHeight = ::GetDeviceCaps(dc, VERTSIZE);
291 int screenXRes
= ::GetDeviceCaps(dc
, HORZRES
);
292 // int screenYRes = ::GetDeviceCaps(dc, VERTRES);
293 int logPPIScreenX
= ::GetDeviceCaps(dc
, LOGPIXELSX
);
294 int logPPIScreenY
= ::GetDeviceCaps(dc
, LOGPIXELSY
);
295 m_previewPrintout
->SetPPIScreen(logPPIScreenX
, logPPIScreenY
);
297 ::ReleaseDC(NULL
, dc
);
299 // Get a device context for the currently selected printer
300 wxPrinterDC
printerDC("", "", "", FALSE
, m_printData
.GetOrientation());
302 int printerWidth
= 150;
303 int printerHeight
= 250;
304 int printerXRes
= 1500;
305 int printerYRes
= 2500;
307 if (printerDC
.GetHDC())
309 printerWidth
= ::GetDeviceCaps((HDC
) printerDC
.GetHDC(), HORZSIZE
);
310 printerHeight
= ::GetDeviceCaps((HDC
) printerDC
.GetHDC(), VERTSIZE
);
311 printerXRes
= ::GetDeviceCaps((HDC
) printerDC
.GetHDC(), HORZRES
);
312 printerYRes
= ::GetDeviceCaps((HDC
) printerDC
.GetHDC(), VERTRES
);
314 int logPPIPrinterX
= ::GetDeviceCaps((HDC
) printerDC
.GetHDC(), LOGPIXELSX
);
315 int logPPIPrinterY
= ::GetDeviceCaps((HDC
) printerDC
.GetHDC(), LOGPIXELSY
);
317 m_previewPrintout
->SetPPIPrinter(logPPIPrinterX
, logPPIPrinterY
);
318 m_previewPrintout
->SetPageSizeMM(printerWidth
, printerHeight
);
320 if (logPPIPrinterX
== 0 || logPPIPrinterY
== 0 || printerWidth
== 0 || printerHeight
== 0)
326 m_pageWidth
= printerXRes
;
327 m_pageHeight
= printerYRes
;
329 // At 100%, the page should look about page-size on the screen.
330 m_previewScale
= (float)((float)screenWidth
/(float)printerWidth
);
331 m_previewScale
= m_previewScale
* (float)((float)screenXRes
/(float)printerYRes
);
334 /****************************************************************************
336 FUNCTION: wxAbortProc()
338 PURPOSE: Processes messages for the Abort Dialog box
340 ****************************************************************************/
342 LONG APIENTRY _EXPORT
wxAbortProc(HDC
WXUNUSED(hPr
), int WXUNUSED(Code
))
346 if (!wxPrinterBase::sm_abortWindow
) /* If the abort dialog isn't up yet */
349 /* Process messages intended for the abort dialog box */
351 while (!wxPrinterBase::sm_abortIt
&& PeekMessage(&msg
, 0, 0, 0, TRUE
))
352 if (!IsDialogMessage((HWND
) wxPrinterBase::sm_abortWindow
->GetHWND(), &msg
)) {
353 TranslateMessage(&msg
);
354 DispatchMessage(&msg
);
357 /* bAbort is TRUE (return is FALSE) if the user has aborted */
359 return (!wxPrinterBase::sm_abortIt
);