]>
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
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ===========================================================================
14 // ===========================================================================
16 // ---------------------------------------------------------------------------
18 // ---------------------------------------------------------------------------
21 #pragma implementation "printwin.h"
24 // For compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
33 #if wxUSE_PRINTING_ARCHITECTURE
36 #include "wx/window.h"
37 #include "wx/msw/private.h"
41 #include "wx/msgdlg.h"
45 #include "wx/msw/printwin.h"
46 #include "wx/dcprint.h"
47 #include "wx/printdlg.h"
49 #include "wx/msw/private.h"
53 #include "wx/msw/private.h"
61 // ---------------------------------------------------------------------------
63 // ---------------------------------------------------------------------------
65 LONG APIENTRY _EXPORT
wxAbortProc(HDC hPr
, int Code
);
67 // ---------------------------------------------------------------------------
69 // ---------------------------------------------------------------------------
71 IMPLEMENT_DYNAMIC_CLASS(wxWindowsPrinter
, wxPrinterBase
)
72 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 // avoids warning about statement with no effect (FreeProcInstance
91 // doesn't do anything under Win32)
92 #if !defined(__WIN32__) && !defined(__NT__)
93 FreeProcInstance((FARPROC
) m_lpAbortProc
);
97 bool wxWindowsPrinter::Print(wxWindow
*parent
, wxPrintout
*printout
, bool prompt
)
100 sm_abortWindow
= NULL
;
104 sm_lastError
= wxPRINTER_ERROR
;
108 printout
->SetIsPreview(FALSE
);
110 // 4/9/99, JACS: this is a silly place to allow preparation, considering
111 // the DC and no parameters have been set in the printout object.
112 // Moved further down.
113 // printout->OnPreparePrinting();
115 // Get some parameters from the printout, if defined
116 int fromPage
, toPage
;
117 int minPage
, maxPage
;
118 printout
->GetPageInfo(&minPage
, &maxPage
, &fromPage
, &toPage
);
122 sm_lastError
= wxPRINTER_ERROR
;
126 m_printDialogData
.SetMinPage(minPage
);
127 m_printDialogData
.SetMaxPage(maxPage
);
129 m_printDialogData
.SetFromPage(fromPage
);
131 m_printDialogData
.SetToPage(toPage
);
135 m_printDialogData
.EnablePageNumbers(TRUE
);
136 if (m_printDialogData
.GetFromPage() < m_printDialogData
.GetMinPage())
137 m_printDialogData
.SetFromPage(m_printDialogData
.GetMinPage());
138 else if (m_printDialogData
.GetFromPage() > m_printDialogData
.GetMaxPage())
139 m_printDialogData
.SetFromPage(m_printDialogData
.GetMaxPage());
140 if (m_printDialogData
.GetToPage() > m_printDialogData
.GetMaxPage())
141 m_printDialogData
.SetToPage(m_printDialogData
.GetMaxPage());
142 else if (m_printDialogData
.GetToPage() < m_printDialogData
.GetMinPage())
143 m_printDialogData
.SetToPage(m_printDialogData
.GetMinPage());
146 m_printDialogData
.EnablePageNumbers(FALSE
);
148 // Create a suitable device context
152 dc
= PrintDialog(parent
);
158 // dc = new wxPrinterDC("", "", "", FALSE, m_printData.GetOrientation());
159 dc
= new wxPrinterDC(m_printDialogData
.GetPrintData());
162 // May have pressed cancel.
163 if (!dc
|| !dc
->Ok())
169 int logPPIScreenX
= 0;
170 int logPPIScreenY
= 0;
171 int logPPIPrinterX
= 0;
172 int logPPIPrinterY
= 0;
174 HDC hdc
= ::GetDC(NULL
);
175 logPPIScreenX
= ::GetDeviceCaps(hdc
, LOGPIXELSX
);
176 logPPIScreenY
= ::GetDeviceCaps(hdc
, LOGPIXELSY
);
177 ::ReleaseDC(NULL
, hdc
);
179 logPPIPrinterX
= ::GetDeviceCaps((HDC
) dc
->GetHDC(), LOGPIXELSX
);
180 logPPIPrinterY
= ::GetDeviceCaps((HDC
) dc
->GetHDC(), LOGPIXELSY
);
181 if (logPPIPrinterX
== 0 || logPPIPrinterY
== 0)
184 sm_lastError
= wxPRINTER_ERROR
;
188 printout
->SetPPIScreen(logPPIScreenX
, logPPIScreenY
);
189 printout
->SetPPIPrinter(logPPIPrinterX
, logPPIPrinterY
);
191 // Set printout parameters
196 printout
->SetPageSizePixels((int)w
, (int)h
);
198 dc
->GetSizeMM(&w
, &h
);
199 printout
->SetPageSizeMM((int)w
, (int)h
);
201 // Create an abort window
204 printout
->OnPreparePrinting();
206 wxWindow
*win
= CreateAbortWindow(parent
, printout
);
209 #if defined(__BORLANDC__) || defined(__GNUWIN32__) || defined(__SALFORDC__) || !defined(__WIN32__)
211 ::SetAbortProc((HDC
) dc
->GetHDC(), (ABORTPROC
) m_lpAbortProc
);
213 ::SetAbortProc((HDC
) dc
->GetHDC(), (FARPROC
) m_lpAbortProc
);
216 ::SetAbortProc((HDC
) dc
->GetHDC(), (int (_stdcall
*)
217 // cast it to right type only if required
218 // FIXME it's really cdecl and we're casting it to stdcall - either there is
219 // something I don't understand or it will crash at first usage
231 wxLogDebug(wxT("Could not create an abort dialog."));
232 sm_lastError
= wxPRINTER_ERROR
;
236 sm_abortWindow
= win
;
237 sm_abortWindow
->Show(TRUE
);
240 printout
->OnBeginPrinting();
242 sm_lastError
= wxPRINTER_NO_ERROR
;
246 copyCount
<= m_printDialogData
.GetNoCopies();
249 if (!printout
->OnBeginDocument(m_printDialogData
.GetFromPage(), m_printDialogData
.GetToPage()))
252 wxLogError(_("Could not start printing."));
253 sm_lastError
= wxPRINTER_ERROR
;
258 sm_lastError
= wxPRINTER_CANCELLED
;
263 for ( pn
= m_printDialogData
.GetFromPage();
264 pn
<= m_printDialogData
.GetToPage() && printout
->HasPage(pn
);
269 sm_lastError
= wxPRINTER_CANCELLED
;
274 bool cont
= printout
->OnPrintPage(pn
);
279 sm_lastError
= wxPRINTER_CANCELLED
;
284 printout
->OnEndDocument();
287 printout
->OnEndPrinting();
291 sm_abortWindow
->Show(FALSE
);
292 delete sm_abortWindow
;
293 sm_abortWindow
= NULL
;
300 return (sm_lastError
== wxPRINTER_NO_ERROR
);
303 wxDC
* wxWindowsPrinter::PrintDialog(wxWindow
*parent
)
305 wxDC
* dc
= (wxDC
*) NULL
;
307 wxPrintDialog
dialog(parent
, & m_printDialogData
);
308 int ret
= dialog
.ShowModal();
312 dc
= dialog
.GetPrintDC();
313 m_printDialogData
= dialog
.GetPrintDialogData();
315 sm_lastError
= wxPRINTER_ERROR
;
317 sm_lastError
= wxPRINTER_NO_ERROR
;
320 sm_lastError
= wxPRINTER_CANCELLED
;
325 bool wxWindowsPrinter::Setup(wxWindow
*parent
)
327 wxPrintDialog
dialog(parent
, & m_printDialogData
);
328 dialog
.GetPrintDialogData().SetSetupDialog(TRUE
);
330 int ret
= dialog
.ShowModal();
334 m_printDialogData
= dialog
.GetPrintDialogData();
337 return (ret
== wxID_OK
);
344 wxWindowsPrintPreview::wxWindowsPrintPreview(wxPrintout
*printout
,
345 wxPrintout
*printoutForPrinting
,
346 wxPrintDialogData
*data
)
347 : wxPrintPreviewBase(printout
, printoutForPrinting
, data
)
352 wxWindowsPrintPreview::wxWindowsPrintPreview(wxPrintout
*printout
,
353 wxPrintout
*printoutForPrinting
,
355 : wxPrintPreviewBase(printout
, printoutForPrinting
, data
)
360 wxWindowsPrintPreview::~wxWindowsPrintPreview()
364 bool wxWindowsPrintPreview::Print(bool interactive
)
366 if (!m_printPrintout
)
368 wxWindowsPrinter
printer(&m_printDialogData
);
369 return printer
.Print(m_previewFrame
, m_printPrintout
, interactive
);
372 void wxWindowsPrintPreview::DetermineScaling()
374 HDC dc
= ::GetDC(NULL
);
375 int screenWidth
= ::GetDeviceCaps(dc
, HORZSIZE
);
376 int screenYRes
= ::GetDeviceCaps(dc
, VERTRES
);
377 int logPPIScreenX
= ::GetDeviceCaps(dc
, LOGPIXELSX
);
378 int logPPIScreenY
= ::GetDeviceCaps(dc
, LOGPIXELSY
);
379 m_previewPrintout
->SetPPIScreen(logPPIScreenX
, logPPIScreenY
);
381 ::ReleaseDC(NULL
, dc
);
383 // Get a device context for the currently selected printer
384 wxPrinterDC
printerDC(m_printDialogData
.GetPrintData());
386 int printerWidth
= 150;
387 int printerHeight
= 250;
388 int printerXRes
= 1500;
389 int printerYRes
= 2500;
391 dc
= GetHdcOf(printerDC
);
394 printerWidth
= ::GetDeviceCaps(dc
, HORZSIZE
);
395 printerHeight
= ::GetDeviceCaps(dc
, VERTSIZE
);
396 printerXRes
= ::GetDeviceCaps(dc
, HORZRES
);
397 printerYRes
= ::GetDeviceCaps(dc
, VERTRES
);
399 int logPPIPrinterX
= ::GetDeviceCaps(dc
, LOGPIXELSX
);
400 int logPPIPrinterY
= ::GetDeviceCaps(dc
, LOGPIXELSY
);
402 m_previewPrintout
->SetPPIPrinter(logPPIPrinterX
, logPPIPrinterY
);
403 m_previewPrintout
->SetPageSizeMM(printerWidth
, printerHeight
);
405 if (logPPIPrinterX
== 0 || logPPIPrinterY
== 0 || printerWidth
== 0 || printerHeight
== 0)
411 m_pageWidth
= printerXRes
;
412 m_pageHeight
= printerYRes
;
414 // At 100%, the page should look about page-size on the screen.
415 m_previewScale
= (float)((float)screenWidth
/(float)printerWidth
);
416 m_previewScale
= m_previewScale
* (float)((float)screenYRes
/(float)printerYRes
);
419 /****************************************************************************
421 FUNCTION: wxAbortProc()
423 PURPOSE: Processes messages for the Abort Dialog box
425 ****************************************************************************/
427 LONG APIENTRY _EXPORT
wxAbortProc(HDC
WXUNUSED(hPr
), int WXUNUSED(Code
))
431 if (!wxPrinterBase::sm_abortWindow
) /* If the abort dialog isn't up yet */
434 /* Process messages intended for the abort dialog box */
436 while (!wxPrinterBase::sm_abortIt
&& PeekMessage(&msg
, 0, 0, 0, TRUE
))
437 if (!IsDialogMessage((HWND
) wxPrinterBase::sm_abortWindow
->GetHWND(), &msg
)) {
438 TranslateMessage(&msg
);
439 DispatchMessage(&msg
);
442 /* bAbort is TRUE (return is FALSE) if the user has aborted */
444 return (!wxPrinterBase::sm_abortIt
);
448 // wxUSE_PRINTING_ARCHITECTURE