]>
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 /////////////////////////////////////////////////////////////////////////////
12 // ===========================================================================
14 // ===========================================================================
16 // ---------------------------------------------------------------------------
18 // ---------------------------------------------------------------------------
21 #pragma implementation "printwin.h"
24 // For compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
37 #include "wx/msgdlg.h"
40 #include "wx/msw/printwin.h"
41 #include "wx/dcprint.h"
42 #include "wx/printdlg.h"
43 #include "wx/msw/private.h"
49 // Clash with Windows header files
58 // ---------------------------------------------------------------------------
60 // ---------------------------------------------------------------------------
62 LONG APIENTRY _EXPORT
wxAbortProc(HDC hPr
, int Code
);
64 // ---------------------------------------------------------------------------
66 // ---------------------------------------------------------------------------
68 #if !USE_SHARED_LIBRARY
69 IMPLEMENT_DYNAMIC_CLASS(wxWindowsPrinter
, wxPrinterBase
)
70 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 FreeProcInstance((FARPROC
) m_lpAbortProc
);
92 bool wxWindowsPrinter::Print(wxWindow
*parent
, wxPrintout
*printout
, bool prompt
)
95 sm_abortWindow
= NULL
;
100 printout
->SetIsPreview(FALSE
);
101 printout
->OnPreparePrinting();
103 // Get some parameters from the printout, if defined
104 int fromPage
, toPage
;
105 int minPage
, maxPage
;
106 printout
->GetPageInfo(&minPage
, &maxPage
, &fromPage
, &toPage
);
111 m_printDialogData
.SetMinPage(minPage
);
112 m_printDialogData
.SetMaxPage(maxPage
);
114 m_printDialogData
.SetFromPage(fromPage
);
116 m_printDialogData
.SetToPage(toPage
);
120 m_printDialogData
.EnablePageNumbers(TRUE
);
121 if (m_printDialogData
.GetFromPage() < m_printDialogData
.GetMinPage())
122 m_printDialogData
.SetFromPage(m_printDialogData
.GetMinPage());
123 else if (m_printDialogData
.GetFromPage() > m_printDialogData
.GetMaxPage())
124 m_printDialogData
.SetFromPage(m_printDialogData
.GetMaxPage());
125 if (m_printDialogData
.GetToPage() > m_printDialogData
.GetMaxPage())
126 m_printDialogData
.SetToPage(m_printDialogData
.GetMaxPage());
127 else if (m_printDialogData
.GetToPage() < m_printDialogData
.GetMinPage())
128 m_printDialogData
.SetToPage(m_printDialogData
.GetMinPage());
131 m_printDialogData
.EnablePageNumbers(FALSE
);
133 // Create a suitable device context
137 dc
= PrintDialog(parent
);
143 // dc = new wxPrinterDC("", "", "", FALSE, m_printData.GetOrientation());
144 dc
= new wxPrinterDC(m_printDialogData
.GetPrintData());
147 // May have pressed cancel.
148 if (!dc
|| !dc
->Ok())
154 int logPPIScreenX
= 0;
155 int logPPIScreenY
= 0;
156 int logPPIPrinterX
= 0;
157 int logPPIPrinterY
= 0;
159 HDC hdc
= ::GetDC(NULL
);
160 logPPIScreenX
= ::GetDeviceCaps(hdc
, LOGPIXELSX
);
161 logPPIScreenY
= ::GetDeviceCaps(hdc
, LOGPIXELSY
);
162 ::ReleaseDC(NULL
, hdc
);
164 logPPIPrinterX
= ::GetDeviceCaps((HDC
) dc
->GetHDC(), LOGPIXELSX
);
165 logPPIPrinterY
= ::GetDeviceCaps((HDC
) dc
->GetHDC(), LOGPIXELSY
);
166 if (logPPIPrinterX
== 0 || logPPIPrinterY
== 0)
172 printout
->SetPPIScreen(logPPIScreenX
, logPPIScreenY
);
173 printout
->SetPPIPrinter(logPPIPrinterX
, logPPIPrinterY
);
175 // Set printout parameters
180 printout
->SetPageSizePixels((int)w
, (int)h
);
182 dc
->GetSizeMM(&w
, &h
);
183 printout
->SetPageSizeMM((int)w
, (int)h
);
185 // Create an abort window
188 wxWindow
*win
= CreateAbortWindow(parent
, printout
);
191 #if defined(__BORLANDC__) || defined(__GNUWIN32__) || defined(__SALFORDC__) || !defined(__WIN32__)
192 ::SetAbortProc((HDC
) dc
->GetHDC(), (FARPROC
) m_lpAbortProc
);
194 ::SetAbortProc((HDC
) dc
->GetHDC(), (int (_stdcall
*)
195 // cast it to right type only if required
196 // FIXME it's really cdecl and we're casting it to stdcall - either there is
197 // something I don't understand or it will crash at first usage
209 wxLogDebug("Could not create an abort dialog.");
213 sm_abortWindow
= win
;
214 sm_abortWindow
->Show(TRUE
);
217 printout
->OnBeginPrinting();
219 bool keepGoing
= TRUE
;
222 for (copyCount
= 1; copyCount
<= m_printDialogData
.GetNoCopies(); copyCount
++)
224 if (!printout
->OnBeginDocument(m_printDialogData
.GetFromPage(), m_printDialogData
.GetToPage()))
227 wxLogError(_("Could not start printing."));
234 for (pn
= m_printDialogData
.GetFromPage(); keepGoing
&& (pn
<= m_printDialogData
.GetToPage()) && printout
->HasPage(pn
);
245 printout
->OnPrintPage(pn
);
249 printout
->OnEndDocument();
252 printout
->OnEndPrinting();
256 sm_abortWindow
->Show(FALSE
);
257 delete sm_abortWindow
;
258 sm_abortWindow
= NULL
;
268 wxDC
* wxWindowsPrinter::PrintDialog(wxWindow
*parent
)
270 wxDC
* dc
= (wxDC
*) NULL
;
272 wxPrintDialog
dialog(parent
, & m_printDialogData
);
273 int ret
= dialog
.ShowModal();
277 dc
= dialog
.GetPrintDC();
278 m_printDialogData
= dialog
.GetPrintDialogData();
284 bool wxWindowsPrinter::Setup(wxWindow
*parent
)
286 wxPrintDialog
dialog(parent
, & m_printDialogData
);
287 dialog
.GetPrintDialogData().SetSetupDialog(TRUE
);
289 int ret
= dialog
.ShowModal();
293 m_printDialogData
= dialog
.GetPrintDialogData();
296 return (ret
== wxID_OK
);
303 wxWindowsPrintPreview::wxWindowsPrintPreview(wxPrintout
*printout
,
304 wxPrintout
*printoutForPrinting
,
305 wxPrintDialogData
*data
)
306 : wxPrintPreviewBase(printout
, printoutForPrinting
, data
)
311 wxWindowsPrintPreview::wxWindowsPrintPreview(wxPrintout
*printout
,
312 wxPrintout
*printoutForPrinting
,
314 : wxPrintPreviewBase(printout
, printoutForPrinting
, data
)
319 wxWindowsPrintPreview::~wxWindowsPrintPreview()
323 bool wxWindowsPrintPreview::Print(bool interactive
)
325 if (!m_printPrintout
)
327 wxWindowsPrinter
printer(&m_printDialogData
);
328 return printer
.Print(m_previewFrame
, m_printPrintout
, interactive
);
331 void wxWindowsPrintPreview::DetermineScaling()
333 HDC dc
= ::GetDC(NULL
);
334 int screenWidth
= ::GetDeviceCaps(dc
, HORZSIZE
);
335 // int screenHeight = ::GetDeviceCaps(dc, VERTSIZE);
336 int screenXRes
= ::GetDeviceCaps(dc
, HORZRES
);
337 // int screenYRes = ::GetDeviceCaps(dc, VERTRES);
338 int logPPIScreenX
= ::GetDeviceCaps(dc
, LOGPIXELSX
);
339 int logPPIScreenY
= ::GetDeviceCaps(dc
, LOGPIXELSY
);
340 m_previewPrintout
->SetPPIScreen(logPPIScreenX
, logPPIScreenY
);
342 ::ReleaseDC(NULL
, dc
);
344 // Get a device context for the currently selected printer
345 wxPrinterDC
printerDC(m_printDialogData
.GetPrintData());
347 int printerWidth
= 150;
348 int printerHeight
= 250;
349 int printerXRes
= 1500;
350 int printerYRes
= 2500;
352 if (printerDC
.GetHDC())
354 printerWidth
= ::GetDeviceCaps((HDC
) printerDC
.GetHDC(), HORZSIZE
);
355 printerHeight
= ::GetDeviceCaps((HDC
) printerDC
.GetHDC(), VERTSIZE
);
356 printerXRes
= ::GetDeviceCaps((HDC
) printerDC
.GetHDC(), HORZRES
);
357 printerYRes
= ::GetDeviceCaps((HDC
) printerDC
.GetHDC(), VERTRES
);
359 int logPPIPrinterX
= ::GetDeviceCaps((HDC
) printerDC
.GetHDC(), LOGPIXELSX
);
360 int logPPIPrinterY
= ::GetDeviceCaps((HDC
) printerDC
.GetHDC(), LOGPIXELSY
);
362 m_previewPrintout
->SetPPIPrinter(logPPIPrinterX
, logPPIPrinterY
);
363 m_previewPrintout
->SetPageSizeMM(printerWidth
, printerHeight
);
365 if (logPPIPrinterX
== 0 || logPPIPrinterY
== 0 || printerWidth
== 0 || printerHeight
== 0)
371 m_pageWidth
= printerXRes
;
372 m_pageHeight
= printerYRes
;
374 // At 100%, the page should look about page-size on the screen.
375 m_previewScale
= (float)((float)screenWidth
/(float)printerWidth
);
376 m_previewScale
= m_previewScale
* (float)((float)screenXRes
/(float)printerYRes
);
379 /****************************************************************************
381 FUNCTION: wxAbortProc()
383 PURPOSE: Processes messages for the Abort Dialog box
385 ****************************************************************************/
387 LONG APIENTRY _EXPORT
wxAbortProc(HDC
WXUNUSED(hPr
), int WXUNUSED(Code
))
391 if (!wxPrinterBase::sm_abortWindow
) /* If the abort dialog isn't up yet */
394 /* Process messages intended for the abort dialog box */
396 while (!wxPrinterBase::sm_abortIt
&& PeekMessage(&msg
, 0, 0, 0, TRUE
))
397 if (!IsDialogMessage((HWND
) wxPrinterBase::sm_abortWindow
->GetHWND(), &msg
)) {
398 TranslateMessage(&msg
);
399 DispatchMessage(&msg
);
402 /* bAbort is TRUE (return is FALSE) if the user has aborted */
404 return (!wxPrinterBase::sm_abortIt
);