]>
git.saurik.com Git - wxWidgets.git/blob - src/msw/printwin.cpp
f65d3e74317b21e637434964fc082468b9e804fb
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(wxPrintDialogData
*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_printDialogData
.SetMinPage(minPage
);
92 m_printDialogData
.SetMaxPage(maxPage
);
94 m_printDialogData
.SetFromPage(fromPage
);
96 m_printDialogData
.SetToPage(toPage
);
100 m_printDialogData
.EnablePageNumbers(TRUE
);
101 if (m_printDialogData
.GetFromPage() < m_printDialogData
.GetMinPage())
102 m_printDialogData
.SetFromPage(m_printDialogData
.GetMinPage());
103 else if (m_printDialogData
.GetFromPage() > m_printDialogData
.GetMaxPage())
104 m_printDialogData
.SetFromPage(m_printDialogData
.GetMaxPage());
105 if (m_printDialogData
.GetToPage() > m_printDialogData
.GetMaxPage())
106 m_printDialogData
.SetToPage(m_printDialogData
.GetMaxPage());
107 else if (m_printDialogData
.GetToPage() < m_printDialogData
.GetMinPage())
108 m_printDialogData
.SetToPage(m_printDialogData
.GetMinPage());
111 m_printDialogData
.EnablePageNumbers(FALSE
);
113 // Create a suitable device context
117 dc
= PrintDialog(parent
);
123 // dc = new wxPrinterDC("", "", "", FALSE, m_printData.GetOrientation());
124 dc
= new wxPrinterDC(m_printDialogData
.GetPrintData());
127 // May have pressed cancel.
128 if (!dc
|| !dc
->Ok())
134 int logPPIScreenX
= 0;
135 int logPPIScreenY
= 0;
136 int logPPIPrinterX
= 0;
137 int logPPIPrinterY
= 0;
139 HDC hdc
= ::GetDC(NULL
);
140 logPPIScreenX
= ::GetDeviceCaps(hdc
, LOGPIXELSX
);
141 logPPIScreenY
= ::GetDeviceCaps(hdc
, LOGPIXELSY
);
142 ::ReleaseDC(NULL
, hdc
);
144 logPPIPrinterX
= ::GetDeviceCaps((HDC
) dc
->GetHDC(), LOGPIXELSX
);
145 logPPIPrinterY
= ::GetDeviceCaps((HDC
) dc
->GetHDC(), LOGPIXELSY
);
146 if (logPPIPrinterX
== 0 || logPPIPrinterY
== 0)
152 printout
->SetPPIScreen(logPPIScreenX
, logPPIScreenY
);
153 printout
->SetPPIPrinter(logPPIPrinterX
, logPPIPrinterY
);
155 // Set printout parameters
160 printout
->SetPageSizePixels((int)w
, (int)h
);
162 dc
->GetSizeMM(&w
, &h
);
163 printout
->SetPageSizeMM((int)w
, (int)h
);
165 // Create an abort window
168 wxWindow
*win
= CreateAbortWindow(parent
, printout
);
171 #if defined(__BORLANDC__) || defined(__GNUWIN32__) || defined(__SALFORDC__) || !defined(__WIN32__)
172 ::SetAbortProc((HDC
) dc
->GetHDC(), (FARPROC
) m_lpAbortProc
);
174 ::SetAbortProc((HDC
) dc
->GetHDC(), (int (_stdcall
*)
175 // cast it to right type only if required
176 // @@@ it's really cdecl and we're casting it to stdcall - either there is
177 // something I don't understand or it will crash at first usage
189 wxMessageBox("Sorry, could not create an abort dialog.", "Print Error", wxOK
, parent
);
192 sm_abortWindow
= win
;
193 sm_abortWindow
->Show(TRUE
);
196 printout
->OnBeginPrinting();
198 bool keepGoing
= TRUE
;
201 for (copyCount
= 1; copyCount
<= m_printDialogData
.GetNoCopies(); copyCount
++)
203 if (!printout
->OnBeginDocument(m_printDialogData
.GetFromPage(), m_printDialogData
.GetToPage()))
206 wxMessageBox("Could not start printing.", "Print Error", wxOK
, parent
);
213 for (pn
= m_printDialogData
.GetFromPage(); keepGoing
&& (pn
<= m_printDialogData
.GetToPage()) && printout
->HasPage(pn
);
224 printout
->OnPrintPage(pn
);
228 printout
->OnEndDocument();
231 printout
->OnEndPrinting();
235 sm_abortWindow
->Show(FALSE
);
236 delete sm_abortWindow
;
237 sm_abortWindow
= NULL
;
247 wxDC
* wxWindowsPrinter::PrintDialog(wxWindow
*parent
)
249 wxDC
* dc
= (wxDC
*) NULL
;
251 wxPrintDialog
dialog(parent
, & m_printDialogData
);
252 int ret
= dialog
.ShowModal();
256 dc
= dialog
.GetPrintDC();
257 m_printDialogData
= dialog
.GetPrintDialogData();
263 bool wxWindowsPrinter::Setup(wxWindow
*parent
)
265 wxPrintDialog
dialog(parent
, & m_printDialogData
);
266 dialog
.GetPrintDialogData().SetSetupDialog(TRUE
);
268 int ret
= dialog
.ShowModal();
272 m_printDialogData
= dialog
.GetPrintDialogData();
275 return (ret
== wxID_OK
);
282 wxWindowsPrintPreview::wxWindowsPrintPreview(wxPrintout
*printout
, wxPrintout
*printoutForPrinting
, wxPrintDialogData
*data
):
283 wxPrintPreviewBase(printout
, printoutForPrinting
, data
)
288 wxWindowsPrintPreview::~wxWindowsPrintPreview(void)
292 bool wxWindowsPrintPreview::Print(bool interactive
)
294 if (!m_printPrintout
)
296 wxWindowsPrinter
printer(&m_printDialogData
);
297 return printer
.Print(m_previewFrame
, m_printPrintout
, interactive
);
300 void wxWindowsPrintPreview::DetermineScaling(void)
302 HDC dc
= ::GetDC(NULL
);
303 int screenWidth
= ::GetDeviceCaps(dc
, HORZSIZE
);
304 // int screenHeight = ::GetDeviceCaps(dc, VERTSIZE);
305 int screenXRes
= ::GetDeviceCaps(dc
, HORZRES
);
306 // int screenYRes = ::GetDeviceCaps(dc, VERTRES);
307 int logPPIScreenX
= ::GetDeviceCaps(dc
, LOGPIXELSX
);
308 int logPPIScreenY
= ::GetDeviceCaps(dc
, LOGPIXELSY
);
309 m_previewPrintout
->SetPPIScreen(logPPIScreenX
, logPPIScreenY
);
311 ::ReleaseDC(NULL
, dc
);
313 // Get a device context for the currently selected printer
314 wxPrinterDC
printerDC(m_printDialogData
.GetPrintData());
316 int printerWidth
= 150;
317 int printerHeight
= 250;
318 int printerXRes
= 1500;
319 int printerYRes
= 2500;
321 if (printerDC
.GetHDC())
323 printerWidth
= ::GetDeviceCaps((HDC
) printerDC
.GetHDC(), HORZSIZE
);
324 printerHeight
= ::GetDeviceCaps((HDC
) printerDC
.GetHDC(), VERTSIZE
);
325 printerXRes
= ::GetDeviceCaps((HDC
) printerDC
.GetHDC(), HORZRES
);
326 printerYRes
= ::GetDeviceCaps((HDC
) printerDC
.GetHDC(), VERTRES
);
328 int logPPIPrinterX
= ::GetDeviceCaps((HDC
) printerDC
.GetHDC(), LOGPIXELSX
);
329 int logPPIPrinterY
= ::GetDeviceCaps((HDC
) printerDC
.GetHDC(), LOGPIXELSY
);
331 m_previewPrintout
->SetPPIPrinter(logPPIPrinterX
, logPPIPrinterY
);
332 m_previewPrintout
->SetPageSizeMM(printerWidth
, printerHeight
);
334 if (logPPIPrinterX
== 0 || logPPIPrinterY
== 0 || printerWidth
== 0 || printerHeight
== 0)
340 m_pageWidth
= printerXRes
;
341 m_pageHeight
= printerYRes
;
343 // At 100%, the page should look about page-size on the screen.
344 m_previewScale
= (float)((float)screenWidth
/(float)printerWidth
);
345 m_previewScale
= m_previewScale
* (float)((float)screenXRes
/(float)printerYRes
);
348 /****************************************************************************
350 FUNCTION: wxAbortProc()
352 PURPOSE: Processes messages for the Abort Dialog box
354 ****************************************************************************/
356 LONG APIENTRY _EXPORT
wxAbortProc(HDC
WXUNUSED(hPr
), int WXUNUSED(Code
))
360 if (!wxPrinterBase::sm_abortWindow
) /* If the abort dialog isn't up yet */
363 /* Process messages intended for the abort dialog box */
365 while (!wxPrinterBase::sm_abortIt
&& PeekMessage(&msg
, 0, 0, 0, TRUE
))
366 if (!IsDialogMessage((HWND
) wxPrinterBase::sm_abortWindow
->GetHWND(), &msg
)) {
367 TranslateMessage(&msg
);
368 DispatchMessage(&msg
);
371 /* bAbort is TRUE (return is FALSE) if the user has aborted */
373 return (!wxPrinterBase::sm_abortIt
);