]>
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"
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/private.h"
59 // ---------------------------------------------------------------------------
61 // ---------------------------------------------------------------------------
63 LONG APIENTRY _EXPORT
wxAbortProc(HDC hPr
, int Code
);
65 // ---------------------------------------------------------------------------
67 // ---------------------------------------------------------------------------
69 #if !USE_SHARED_LIBRARY
70 IMPLEMENT_DYNAMIC_CLASS(wxWindowsPrinter
, wxPrinterBase
)
71 IMPLEMENT_CLASS(wxWindowsPrintPreview
, wxPrintPreviewBase
)
74 // ===========================================================================
76 // ===========================================================================
78 // ---------------------------------------------------------------------------
80 // ---------------------------------------------------------------------------
82 wxWindowsPrinter::wxWindowsPrinter(wxPrintDialogData
*data
)
85 m_lpAbortProc
= (WXFARPROC
) MakeProcInstance((FARPROC
) wxAbortProc
, wxGetInstance());
88 wxWindowsPrinter::~wxWindowsPrinter()
90 FreeProcInstance((FARPROC
) m_lpAbortProc
);
93 bool wxWindowsPrinter::Print(wxWindow
*parent
, wxPrintout
*printout
, bool prompt
)
96 sm_abortWindow
= NULL
;
101 printout
->SetIsPreview(FALSE
);
102 printout
->OnPreparePrinting();
104 // Get some parameters from the printout, if defined
105 int fromPage
, toPage
;
106 int minPage
, maxPage
;
107 printout
->GetPageInfo(&minPage
, &maxPage
, &fromPage
, &toPage
);
112 m_printDialogData
.SetMinPage(minPage
);
113 m_printDialogData
.SetMaxPage(maxPage
);
115 m_printDialogData
.SetFromPage(fromPage
);
117 m_printDialogData
.SetToPage(toPage
);
121 m_printDialogData
.EnablePageNumbers(TRUE
);
122 if (m_printDialogData
.GetFromPage() < m_printDialogData
.GetMinPage())
123 m_printDialogData
.SetFromPage(m_printDialogData
.GetMinPage());
124 else if (m_printDialogData
.GetFromPage() > m_printDialogData
.GetMaxPage())
125 m_printDialogData
.SetFromPage(m_printDialogData
.GetMaxPage());
126 if (m_printDialogData
.GetToPage() > m_printDialogData
.GetMaxPage())
127 m_printDialogData
.SetToPage(m_printDialogData
.GetMaxPage());
128 else if (m_printDialogData
.GetToPage() < m_printDialogData
.GetMinPage())
129 m_printDialogData
.SetToPage(m_printDialogData
.GetMinPage());
132 m_printDialogData
.EnablePageNumbers(FALSE
);
134 // Create a suitable device context
138 dc
= PrintDialog(parent
);
144 // dc = new wxPrinterDC("", "", "", FALSE, m_printData.GetOrientation());
145 dc
= new wxPrinterDC(m_printDialogData
.GetPrintData());
148 // May have pressed cancel.
149 if (!dc
|| !dc
->Ok())
155 int logPPIScreenX
= 0;
156 int logPPIScreenY
= 0;
157 int logPPIPrinterX
= 0;
158 int logPPIPrinterY
= 0;
160 HDC hdc
= ::GetDC(NULL
);
161 logPPIScreenX
= ::GetDeviceCaps(hdc
, LOGPIXELSX
);
162 logPPIScreenY
= ::GetDeviceCaps(hdc
, LOGPIXELSY
);
163 ::ReleaseDC(NULL
, hdc
);
165 logPPIPrinterX
= ::GetDeviceCaps((HDC
) dc
->GetHDC(), LOGPIXELSX
);
166 logPPIPrinterY
= ::GetDeviceCaps((HDC
) dc
->GetHDC(), LOGPIXELSY
);
167 if (logPPIPrinterX
== 0 || logPPIPrinterY
== 0)
173 printout
->SetPPIScreen(logPPIScreenX
, logPPIScreenY
);
174 printout
->SetPPIPrinter(logPPIPrinterX
, logPPIPrinterY
);
176 // Set printout parameters
181 printout
->SetPageSizePixels((int)w
, (int)h
);
183 dc
->GetSizeMM(&w
, &h
);
184 printout
->SetPageSizeMM((int)w
, (int)h
);
186 // Create an abort window
189 wxWindow
*win
= CreateAbortWindow(parent
, printout
);
192 #if defined(__BORLANDC__) || defined(__GNUWIN32__) || defined(__SALFORDC__) || !defined(__WIN32__)
194 ::SetAbortProc((HDC
) dc
->GetHDC(), (ABORTPROC
) m_lpAbortProc
);
196 ::SetAbortProc((HDC
) dc
->GetHDC(), (FARPROC
) m_lpAbortProc
);
199 ::SetAbortProc((HDC
) dc
->GetHDC(), (int (_stdcall
*)
200 // cast it to right type only if required
201 // FIXME it's really cdecl and we're casting it to stdcall - either there is
202 // something I don't understand or it will crash at first usage
214 wxLogDebug("Could not create an abort dialog.");
218 sm_abortWindow
= win
;
219 sm_abortWindow
->Show(TRUE
);
222 printout
->OnBeginPrinting();
224 bool keepGoing
= TRUE
;
227 for (copyCount
= 1; copyCount
<= m_printDialogData
.GetNoCopies(); copyCount
++)
229 if (!printout
->OnBeginDocument(m_printDialogData
.GetFromPage(), m_printDialogData
.GetToPage()))
232 wxLogError(_("Could not start printing."));
239 for (pn
= m_printDialogData
.GetFromPage(); keepGoing
&& (pn
<= m_printDialogData
.GetToPage()) && printout
->HasPage(pn
);
250 printout
->OnPrintPage(pn
);
254 printout
->OnEndDocument();
257 printout
->OnEndPrinting();
261 sm_abortWindow
->Show(FALSE
);
262 delete sm_abortWindow
;
263 sm_abortWindow
= NULL
;
273 wxDC
* wxWindowsPrinter::PrintDialog(wxWindow
*parent
)
275 wxDC
* dc
= (wxDC
*) NULL
;
277 wxPrintDialog
dialog(parent
, & m_printDialogData
);
278 int ret
= dialog
.ShowModal();
282 dc
= dialog
.GetPrintDC();
283 m_printDialogData
= dialog
.GetPrintDialogData();
289 bool wxWindowsPrinter::Setup(wxWindow
*parent
)
291 wxPrintDialog
dialog(parent
, & m_printDialogData
);
292 dialog
.GetPrintDialogData().SetSetupDialog(TRUE
);
294 int ret
= dialog
.ShowModal();
298 m_printDialogData
= dialog
.GetPrintDialogData();
301 return (ret
== wxID_OK
);
308 wxWindowsPrintPreview::wxWindowsPrintPreview(wxPrintout
*printout
,
309 wxPrintout
*printoutForPrinting
,
310 wxPrintDialogData
*data
)
311 : wxPrintPreviewBase(printout
, printoutForPrinting
, data
)
316 wxWindowsPrintPreview::wxWindowsPrintPreview(wxPrintout
*printout
,
317 wxPrintout
*printoutForPrinting
,
319 : wxPrintPreviewBase(printout
, printoutForPrinting
, data
)
324 wxWindowsPrintPreview::~wxWindowsPrintPreview()
328 bool wxWindowsPrintPreview::Print(bool interactive
)
330 if (!m_printPrintout
)
332 wxWindowsPrinter
printer(&m_printDialogData
);
333 return printer
.Print(m_previewFrame
, m_printPrintout
, interactive
);
336 void wxWindowsPrintPreview::DetermineScaling()
338 HDC dc
= ::GetDC(NULL
);
339 int screenWidth
= ::GetDeviceCaps(dc
, HORZSIZE
);
340 // int screenHeight = ::GetDeviceCaps(dc, VERTSIZE);
341 int screenXRes
= ::GetDeviceCaps(dc
, HORZRES
);
342 // int screenYRes = ::GetDeviceCaps(dc, VERTRES);
343 int logPPIScreenX
= ::GetDeviceCaps(dc
, LOGPIXELSX
);
344 int logPPIScreenY
= ::GetDeviceCaps(dc
, LOGPIXELSY
);
345 m_previewPrintout
->SetPPIScreen(logPPIScreenX
, logPPIScreenY
);
347 ::ReleaseDC(NULL
, dc
);
349 // Get a device context for the currently selected printer
350 wxPrinterDC
printerDC(m_printDialogData
.GetPrintData());
352 int printerWidth
= 150;
353 int printerHeight
= 250;
354 int printerXRes
= 1500;
355 int printerYRes
= 2500;
357 if (printerDC
.GetHDC())
359 printerWidth
= ::GetDeviceCaps((HDC
) printerDC
.GetHDC(), HORZSIZE
);
360 printerHeight
= ::GetDeviceCaps((HDC
) printerDC
.GetHDC(), VERTSIZE
);
361 printerXRes
= ::GetDeviceCaps((HDC
) printerDC
.GetHDC(), HORZRES
);
362 printerYRes
= ::GetDeviceCaps((HDC
) printerDC
.GetHDC(), VERTRES
);
364 int logPPIPrinterX
= ::GetDeviceCaps((HDC
) printerDC
.GetHDC(), LOGPIXELSX
);
365 int logPPIPrinterY
= ::GetDeviceCaps((HDC
) printerDC
.GetHDC(), LOGPIXELSY
);
367 m_previewPrintout
->SetPPIPrinter(logPPIPrinterX
, logPPIPrinterY
);
368 m_previewPrintout
->SetPageSizeMM(printerWidth
, printerHeight
);
370 if (logPPIPrinterX
== 0 || logPPIPrinterY
== 0 || printerWidth
== 0 || printerHeight
== 0)
376 m_pageWidth
= printerXRes
;
377 m_pageHeight
= printerYRes
;
379 // At 100%, the page should look about page-size on the screen.
380 m_previewScale
= (float)((float)screenWidth
/(float)printerWidth
);
381 m_previewScale
= m_previewScale
* (float)((float)screenXRes
/(float)printerYRes
);
384 /****************************************************************************
386 FUNCTION: wxAbortProc()
388 PURPOSE: Processes messages for the Abort Dialog box
390 ****************************************************************************/
392 LONG APIENTRY _EXPORT
wxAbortProc(HDC
WXUNUSED(hPr
), int WXUNUSED(Code
))
396 if (!wxPrinterBase::sm_abortWindow
) /* If the abort dialog isn't up yet */
399 /* Process messages intended for the abort dialog box */
401 while (!wxPrinterBase::sm_abortIt
&& PeekMessage(&msg
, 0, 0, 0, TRUE
))
402 if (!IsDialogMessage((HWND
) wxPrinterBase::sm_abortWindow
->GetHWND(), &msg
)) {
403 TranslateMessage(&msg
);
404 DispatchMessage(&msg
);
407 /* bAbort is TRUE (return is FALSE) if the user has aborted */
409 return (!wxPrinterBase::sm_abortIt
);