]>
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"
44 #include "wx/msw/private.h"
48 #include "wx/msw/private.h"
56 // ---------------------------------------------------------------------------
58 // ---------------------------------------------------------------------------
60 LONG APIENTRY _EXPORT
wxAbortProc(HDC hPr
, int Code
);
62 // ---------------------------------------------------------------------------
64 // ---------------------------------------------------------------------------
66 #if !USE_SHARED_LIBRARY
67 IMPLEMENT_DYNAMIC_CLASS(wxWindowsPrinter
, wxPrinterBase
)
68 IMPLEMENT_CLASS(wxWindowsPrintPreview
, wxPrintPreviewBase
)
71 // ===========================================================================
73 // ===========================================================================
75 // ---------------------------------------------------------------------------
77 // ---------------------------------------------------------------------------
79 wxWindowsPrinter::wxWindowsPrinter(wxPrintDialogData
*data
)
82 m_lpAbortProc
= (WXFARPROC
) MakeProcInstance((FARPROC
) wxAbortProc
, wxGetInstance());
85 wxWindowsPrinter::~wxWindowsPrinter()
87 FreeProcInstance((FARPROC
) m_lpAbortProc
);
90 bool wxWindowsPrinter::Print(wxWindow
*parent
, wxPrintout
*printout
, bool prompt
)
93 sm_abortWindow
= NULL
;
98 printout
->SetIsPreview(FALSE
);
99 printout
->OnPreparePrinting();
101 // Get some parameters from the printout, if defined
102 int fromPage
, toPage
;
103 int minPage
, maxPage
;
104 printout
->GetPageInfo(&minPage
, &maxPage
, &fromPage
, &toPage
);
109 m_printDialogData
.SetMinPage(minPage
);
110 m_printDialogData
.SetMaxPage(maxPage
);
112 m_printDialogData
.SetFromPage(fromPage
);
114 m_printDialogData
.SetToPage(toPage
);
118 m_printDialogData
.EnablePageNumbers(TRUE
);
119 if (m_printDialogData
.GetFromPage() < m_printDialogData
.GetMinPage())
120 m_printDialogData
.SetFromPage(m_printDialogData
.GetMinPage());
121 else if (m_printDialogData
.GetFromPage() > m_printDialogData
.GetMaxPage())
122 m_printDialogData
.SetFromPage(m_printDialogData
.GetMaxPage());
123 if (m_printDialogData
.GetToPage() > m_printDialogData
.GetMaxPage())
124 m_printDialogData
.SetToPage(m_printDialogData
.GetMaxPage());
125 else if (m_printDialogData
.GetToPage() < m_printDialogData
.GetMinPage())
126 m_printDialogData
.SetToPage(m_printDialogData
.GetMinPage());
129 m_printDialogData
.EnablePageNumbers(FALSE
);
131 // Create a suitable device context
135 dc
= PrintDialog(parent
);
141 // dc = new wxPrinterDC("", "", "", FALSE, m_printData.GetOrientation());
142 dc
= new wxPrinterDC(m_printDialogData
.GetPrintData());
145 // May have pressed cancel.
146 if (!dc
|| !dc
->Ok())
152 int logPPIScreenX
= 0;
153 int logPPIScreenY
= 0;
154 int logPPIPrinterX
= 0;
155 int logPPIPrinterY
= 0;
157 HDC hdc
= ::GetDC(NULL
);
158 logPPIScreenX
= ::GetDeviceCaps(hdc
, LOGPIXELSX
);
159 logPPIScreenY
= ::GetDeviceCaps(hdc
, LOGPIXELSY
);
160 ::ReleaseDC(NULL
, hdc
);
162 logPPIPrinterX
= ::GetDeviceCaps((HDC
) dc
->GetHDC(), LOGPIXELSX
);
163 logPPIPrinterY
= ::GetDeviceCaps((HDC
) dc
->GetHDC(), LOGPIXELSY
);
164 if (logPPIPrinterX
== 0 || logPPIPrinterY
== 0)
170 printout
->SetPPIScreen(logPPIScreenX
, logPPIScreenY
);
171 printout
->SetPPIPrinter(logPPIPrinterX
, logPPIPrinterY
);
173 // Set printout parameters
178 printout
->SetPageSizePixels((int)w
, (int)h
);
180 dc
->GetSizeMM(&w
, &h
);
181 printout
->SetPageSizeMM((int)w
, (int)h
);
183 // Create an abort window
186 wxWindow
*win
= CreateAbortWindow(parent
, printout
);
189 #if defined(__BORLANDC__) || defined(__GNUWIN32__) || defined(__SALFORDC__) || !defined(__WIN32__)
190 ::SetAbortProc((HDC
) dc
->GetHDC(), (FARPROC
) m_lpAbortProc
);
192 ::SetAbortProc((HDC
) dc
->GetHDC(), (int (_stdcall
*)
193 // cast it to right type only if required
194 // FIXME it's really cdecl and we're casting it to stdcall - either there is
195 // something I don't understand or it will crash at first usage
207 wxLogDebug("Could not create an abort dialog.");
211 sm_abortWindow
= win
;
212 sm_abortWindow
->Show(TRUE
);
215 printout
->OnBeginPrinting();
217 bool keepGoing
= TRUE
;
220 for (copyCount
= 1; copyCount
<= m_printDialogData
.GetNoCopies(); copyCount
++)
222 if (!printout
->OnBeginDocument(m_printDialogData
.GetFromPage(), m_printDialogData
.GetToPage()))
225 wxLogError(_("Could not start printing."));
232 for (pn
= m_printDialogData
.GetFromPage(); keepGoing
&& (pn
<= m_printDialogData
.GetToPage()) && printout
->HasPage(pn
);
243 printout
->OnPrintPage(pn
);
247 printout
->OnEndDocument();
250 printout
->OnEndPrinting();
254 sm_abortWindow
->Show(FALSE
);
255 delete sm_abortWindow
;
256 sm_abortWindow
= NULL
;
266 wxDC
* wxWindowsPrinter::PrintDialog(wxWindow
*parent
)
268 wxDC
* dc
= (wxDC
*) NULL
;
270 wxPrintDialog
dialog(parent
, & m_printDialogData
);
271 int ret
= dialog
.ShowModal();
275 dc
= dialog
.GetPrintDC();
276 m_printDialogData
= dialog
.GetPrintDialogData();
282 bool wxWindowsPrinter::Setup(wxWindow
*parent
)
284 wxPrintDialog
dialog(parent
, & m_printDialogData
);
285 dialog
.GetPrintDialogData().SetSetupDialog(TRUE
);
287 int ret
= dialog
.ShowModal();
291 m_printDialogData
= dialog
.GetPrintDialogData();
294 return (ret
== wxID_OK
);
301 wxWindowsPrintPreview::wxWindowsPrintPreview(wxPrintout
*printout
,
302 wxPrintout
*printoutForPrinting
,
303 wxPrintDialogData
*data
)
304 : wxPrintPreviewBase(printout
, printoutForPrinting
, data
)
309 wxWindowsPrintPreview::wxWindowsPrintPreview(wxPrintout
*printout
,
310 wxPrintout
*printoutForPrinting
,
312 : wxPrintPreviewBase(printout
, printoutForPrinting
, data
)
317 wxWindowsPrintPreview::~wxWindowsPrintPreview()
321 bool wxWindowsPrintPreview::Print(bool interactive
)
323 if (!m_printPrintout
)
325 wxWindowsPrinter
printer(&m_printDialogData
);
326 return printer
.Print(m_previewFrame
, m_printPrintout
, interactive
);
329 void wxWindowsPrintPreview::DetermineScaling()
331 HDC dc
= ::GetDC(NULL
);
332 int screenWidth
= ::GetDeviceCaps(dc
, HORZSIZE
);
333 // int screenHeight = ::GetDeviceCaps(dc, VERTSIZE);
334 int screenXRes
= ::GetDeviceCaps(dc
, HORZRES
);
335 // int screenYRes = ::GetDeviceCaps(dc, VERTRES);
336 int logPPIScreenX
= ::GetDeviceCaps(dc
, LOGPIXELSX
);
337 int logPPIScreenY
= ::GetDeviceCaps(dc
, LOGPIXELSY
);
338 m_previewPrintout
->SetPPIScreen(logPPIScreenX
, logPPIScreenY
);
340 ::ReleaseDC(NULL
, dc
);
342 // Get a device context for the currently selected printer
343 wxPrinterDC
printerDC(m_printDialogData
.GetPrintData());
345 int printerWidth
= 150;
346 int printerHeight
= 250;
347 int printerXRes
= 1500;
348 int printerYRes
= 2500;
350 if (printerDC
.GetHDC())
352 printerWidth
= ::GetDeviceCaps((HDC
) printerDC
.GetHDC(), HORZSIZE
);
353 printerHeight
= ::GetDeviceCaps((HDC
) printerDC
.GetHDC(), VERTSIZE
);
354 printerXRes
= ::GetDeviceCaps((HDC
) printerDC
.GetHDC(), HORZRES
);
355 printerYRes
= ::GetDeviceCaps((HDC
) printerDC
.GetHDC(), VERTRES
);
357 int logPPIPrinterX
= ::GetDeviceCaps((HDC
) printerDC
.GetHDC(), LOGPIXELSX
);
358 int logPPIPrinterY
= ::GetDeviceCaps((HDC
) printerDC
.GetHDC(), LOGPIXELSY
);
360 m_previewPrintout
->SetPPIPrinter(logPPIPrinterX
, logPPIPrinterY
);
361 m_previewPrintout
->SetPageSizeMM(printerWidth
, printerHeight
);
363 if (logPPIPrinterX
== 0 || logPPIPrinterY
== 0 || printerWidth
== 0 || printerHeight
== 0)
369 m_pageWidth
= printerXRes
;
370 m_pageHeight
= printerYRes
;
372 // At 100%, the page should look about page-size on the screen.
373 m_previewScale
= (float)((float)screenWidth
/(float)printerWidth
);
374 m_previewScale
= m_previewScale
* (float)((float)screenXRes
/(float)printerYRes
);
377 /****************************************************************************
379 FUNCTION: wxAbortProc()
381 PURPOSE: Processes messages for the Abort Dialog box
383 ****************************************************************************/
385 LONG APIENTRY _EXPORT
wxAbortProc(HDC
WXUNUSED(hPr
), int WXUNUSED(Code
))
389 if (!wxPrinterBase::sm_abortWindow
) /* If the abort dialog isn't up yet */
392 /* Process messages intended for the abort dialog box */
394 while (!wxPrinterBase::sm_abortIt
&& PeekMessage(&msg
, 0, 0, 0, TRUE
))
395 if (!IsDialogMessage((HWND
) wxPrinterBase::sm_abortWindow
->GetHWND(), &msg
)) {
396 TranslateMessage(&msg
);
397 DispatchMessage(&msg
);
400 /* bAbort is TRUE (return is FALSE) if the user has aborted */
402 return (!wxPrinterBase::sm_abortIt
);