]>
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 // ---------------------------------------------------------------------------
20 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
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 if (m_printDialogData
.GetMinPage() < 1)
111 m_printDialogData
.SetMinPage(1);
112 if (m_printDialogData
.GetMaxPage() < 1)
113 m_printDialogData
.SetMaxPage(9999);
115 // Create a suitable device context
119 dc
= PrintDialog(parent
);
125 dc
= new wxPrinterDC(m_printDialogData
.GetPrintData());
128 // May have pressed cancel.
129 if (!dc
|| !dc
->Ok())
135 int logPPIScreenX
= 0;
136 int logPPIScreenY
= 0;
137 int logPPIPrinterX
= 0;
138 int logPPIPrinterY
= 0;
140 HDC hdc
= ::GetDC(NULL
);
141 logPPIScreenX
= ::GetDeviceCaps(hdc
, LOGPIXELSX
);
142 logPPIScreenY
= ::GetDeviceCaps(hdc
, LOGPIXELSY
);
143 ::ReleaseDC(NULL
, hdc
);
145 logPPIPrinterX
= ::GetDeviceCaps((HDC
) dc
->GetHDC(), LOGPIXELSX
);
146 logPPIPrinterY
= ::GetDeviceCaps((HDC
) dc
->GetHDC(), LOGPIXELSY
);
147 if (logPPIPrinterX
== 0 || logPPIPrinterY
== 0)
150 sm_lastError
= wxPRINTER_ERROR
;
154 printout
->SetPPIScreen(logPPIScreenX
, logPPIScreenY
);
155 printout
->SetPPIPrinter(logPPIPrinterX
, logPPIPrinterY
);
157 // Set printout parameters
162 printout
->SetPageSizePixels((int)w
, (int)h
);
164 dc
->GetSizeMM(&w
, &h
);
165 printout
->SetPageSizeMM((int)w
, (int)h
);
167 // Create an abort window
170 printout
->OnPreparePrinting();
172 // Get some parameters from the printout, if defined
173 int fromPage
, toPage
;
174 int minPage
, maxPage
;
175 printout
->GetPageInfo(&minPage
, &maxPage
, &fromPage
, &toPage
);
179 sm_lastError
= wxPRINTER_ERROR
;
184 // Only set min and max, because from and to have been
186 m_printDialogData
.SetMinPage(minPage
);
187 m_printDialogData
.SetMaxPage(maxPage
);
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(wxT("Could not create an abort dialog."));
215 sm_lastError
= wxPRINTER_ERROR
;
219 sm_abortWindow
= win
;
220 sm_abortWindow
->Show(TRUE
);
223 printout
->OnBeginPrinting();
225 sm_lastError
= wxPRINTER_NO_ERROR
;
229 copyCount
<= m_printDialogData
.GetNoCopies();
232 if (!printout
->OnBeginDocument(m_printDialogData
.GetFromPage(), m_printDialogData
.GetToPage()))
235 wxLogError(_("Could not start printing."));
236 sm_lastError
= wxPRINTER_ERROR
;
241 sm_lastError
= wxPRINTER_CANCELLED
;
246 for ( pn
= m_printDialogData
.GetFromPage();
247 pn
<= m_printDialogData
.GetToPage() && printout
->HasPage(pn
);
252 sm_lastError
= wxPRINTER_CANCELLED
;
257 bool cont
= printout
->OnPrintPage(pn
);
262 sm_lastError
= wxPRINTER_CANCELLED
;
267 printout
->OnEndDocument();
270 printout
->OnEndPrinting();
274 sm_abortWindow
->Show(FALSE
);
275 delete sm_abortWindow
;
276 sm_abortWindow
= NULL
;
283 return (sm_lastError
== wxPRINTER_NO_ERROR
);
286 wxDC
* wxWindowsPrinter::PrintDialog(wxWindow
*parent
)
288 wxDC
* dc
= (wxDC
*) NULL
;
290 wxPrintDialog
dialog(parent
, & m_printDialogData
);
291 int ret
= dialog
.ShowModal();
295 dc
= dialog
.GetPrintDC();
296 m_printDialogData
= dialog
.GetPrintDialogData();
298 sm_lastError
= wxPRINTER_ERROR
;
300 sm_lastError
= wxPRINTER_NO_ERROR
;
303 sm_lastError
= wxPRINTER_CANCELLED
;
308 bool wxWindowsPrinter::Setup(wxWindow
*parent
)
310 wxPrintDialog
dialog(parent
, & m_printDialogData
);
311 dialog
.GetPrintDialogData().SetSetupDialog(TRUE
);
313 int ret
= dialog
.ShowModal();
317 m_printDialogData
= dialog
.GetPrintDialogData();
320 return (ret
== wxID_OK
);
327 wxWindowsPrintPreview::wxWindowsPrintPreview(wxPrintout
*printout
,
328 wxPrintout
*printoutForPrinting
,
329 wxPrintDialogData
*data
)
330 : wxPrintPreviewBase(printout
, printoutForPrinting
, data
)
335 wxWindowsPrintPreview::wxWindowsPrintPreview(wxPrintout
*printout
,
336 wxPrintout
*printoutForPrinting
,
338 : wxPrintPreviewBase(printout
, printoutForPrinting
, data
)
343 wxWindowsPrintPreview::~wxWindowsPrintPreview()
347 bool wxWindowsPrintPreview::Print(bool interactive
)
349 if (!m_printPrintout
)
351 wxWindowsPrinter
printer(&m_printDialogData
);
352 return printer
.Print(m_previewFrame
, m_printPrintout
, interactive
);
355 void wxWindowsPrintPreview::DetermineScaling()
357 HDC dc
= ::GetDC(NULL
);
358 int screenWidth
= ::GetDeviceCaps(dc
, HORZSIZE
);
359 int screenYRes
= ::GetDeviceCaps(dc
, VERTRES
);
360 int logPPIScreenX
= ::GetDeviceCaps(dc
, LOGPIXELSX
);
361 int logPPIScreenY
= ::GetDeviceCaps(dc
, LOGPIXELSY
);
362 m_previewPrintout
->SetPPIScreen(logPPIScreenX
, logPPIScreenY
);
364 ::ReleaseDC(NULL
, dc
);
366 // Get a device context for the currently selected printer
367 wxPrinterDC
printerDC(m_printDialogData
.GetPrintData());
369 int printerWidth
= 150;
370 int printerHeight
= 250;
371 int printerXRes
= 1500;
372 int printerYRes
= 2500;
374 dc
= GetHdcOf(printerDC
);
377 printerWidth
= ::GetDeviceCaps(dc
, HORZSIZE
);
378 printerHeight
= ::GetDeviceCaps(dc
, VERTSIZE
);
379 printerXRes
= ::GetDeviceCaps(dc
, HORZRES
);
380 printerYRes
= ::GetDeviceCaps(dc
, VERTRES
);
382 int logPPIPrinterX
= ::GetDeviceCaps(dc
, LOGPIXELSX
);
383 int logPPIPrinterY
= ::GetDeviceCaps(dc
, LOGPIXELSY
);
385 m_previewPrintout
->SetPPIPrinter(logPPIPrinterX
, logPPIPrinterY
);
386 m_previewPrintout
->SetPageSizeMM(printerWidth
, printerHeight
);
388 if (logPPIPrinterX
== 0 || logPPIPrinterY
== 0 || printerWidth
== 0 || printerHeight
== 0)
394 m_pageWidth
= printerXRes
;
395 m_pageHeight
= printerYRes
;
397 // At 100%, the page should look about page-size on the screen.
398 m_previewScale
= (float)((float)screenWidth
/(float)printerWidth
);
399 m_previewScale
= m_previewScale
* (float)((float)screenYRes
/(float)printerYRes
);
402 /****************************************************************************
404 FUNCTION: wxAbortProc()
406 PURPOSE: Processes messages for the Abort Dialog box
408 ****************************************************************************/
410 LONG APIENTRY _EXPORT
wxAbortProc(HDC
WXUNUSED(hPr
), int WXUNUSED(Code
))
414 if (!wxPrinterBase::sm_abortWindow
) /* If the abort dialog isn't up yet */
417 /* Process messages intended for the abort dialog box */
419 while (!wxPrinterBase::sm_abortIt
&& PeekMessage(&msg
, 0, 0, 0, TRUE
))
420 if (!IsDialogMessage((HWND
) wxPrinterBase::sm_abortWindow
->GetHWND(), &msg
)) {
421 TranslateMessage(&msg
);
422 DispatchMessage(&msg
);
425 /* bAbort is TRUE (return is FALSE) if the user has aborted */
427 return (!wxPrinterBase::sm_abortIt
);
431 // wxUSE_PRINTING_ARCHITECTURE