]>
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"
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(_WIN32) && !defined(__WIN32__) && !defined(__NT__) && !defined(__GNUWIN32__)
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 screenHeight = ::GetDeviceCaps(dc, VERTSIZE);
377 int screenXRes
= ::GetDeviceCaps(dc
, HORZRES
);
378 // int screenYRes = ::GetDeviceCaps(dc, VERTRES);
379 int logPPIScreenX
= ::GetDeviceCaps(dc
, LOGPIXELSX
);
380 int logPPIScreenY
= ::GetDeviceCaps(dc
, LOGPIXELSY
);
381 m_previewPrintout
->SetPPIScreen(logPPIScreenX
, logPPIScreenY
);
383 ::ReleaseDC(NULL
, dc
);
385 // Get a device context for the currently selected printer
386 wxPrinterDC
printerDC(m_printDialogData
.GetPrintData());
388 int printerWidth
= 150;
389 int printerHeight
= 250;
390 int printerXRes
= 1500;
391 int printerYRes
= 2500;
393 if (printerDC
.GetHDC())
395 printerWidth
= ::GetDeviceCaps((HDC
) printerDC
.GetHDC(), HORZSIZE
);
396 printerHeight
= ::GetDeviceCaps((HDC
) printerDC
.GetHDC(), VERTSIZE
);
397 printerXRes
= ::GetDeviceCaps((HDC
) printerDC
.GetHDC(), HORZRES
);
398 printerYRes
= ::GetDeviceCaps((HDC
) printerDC
.GetHDC(), VERTRES
);
400 int logPPIPrinterX
= ::GetDeviceCaps((HDC
) printerDC
.GetHDC(), LOGPIXELSX
);
401 int logPPIPrinterY
= ::GetDeviceCaps((HDC
) printerDC
.GetHDC(), LOGPIXELSY
);
403 m_previewPrintout
->SetPPIPrinter(logPPIPrinterX
, logPPIPrinterY
);
404 m_previewPrintout
->SetPageSizeMM(printerWidth
, printerHeight
);
406 if (logPPIPrinterX
== 0 || logPPIPrinterY
== 0 || printerWidth
== 0 || printerHeight
== 0)
412 m_pageWidth
= printerXRes
;
413 m_pageHeight
= printerYRes
;
415 // At 100%, the page should look about page-size on the screen.
416 m_previewScale
= (float)((float)screenWidth
/(float)printerWidth
);
417 m_previewScale
= m_previewScale
* (float)((float)screenXRes
/(float)printerYRes
);
420 /****************************************************************************
422 FUNCTION: wxAbortProc()
424 PURPOSE: Processes messages for the Abort Dialog box
426 ****************************************************************************/
428 LONG APIENTRY _EXPORT
wxAbortProc(HDC
WXUNUSED(hPr
), int WXUNUSED(Code
))
432 if (!wxPrinterBase::sm_abortWindow
) /* If the abort dialog isn't up yet */
435 /* Process messages intended for the abort dialog box */
437 while (!wxPrinterBase::sm_abortIt
&& PeekMessage(&msg
, 0, 0, 0, TRUE
))
438 if (!IsDialogMessage((HWND
) wxPrinterBase::sm_abortWindow
->GetHWND(), &msg
)) {
439 TranslateMessage(&msg
);
440 DispatchMessage(&msg
);
443 /* bAbort is TRUE (return is FALSE) if the user has aborted */
445 return (!wxPrinterBase::sm_abortIt
);
449 // wxUSE_PRINTING_ARCHITECTURE