]>
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 // avoids mingw warning about statement with no effect (FreeProcInstance
91 // doesn't do anything under Win32)
93 FreeProcInstance((FARPROC
) m_lpAbortProc
);
97 bool wxWindowsPrinter::Print(wxWindow
*parent
, wxPrintout
*printout
, bool prompt
)
100 sm_abortWindow
= NULL
;
105 printout
->SetIsPreview(FALSE
);
107 // 4/9/99, JACS: this is a silly place to allow preparation, considering
108 // the DC and no parameters have been set in the printout object.
109 // Moved further down.
110 // printout->OnPreparePrinting();
112 // Get some parameters from the printout, if defined
113 int fromPage
, toPage
;
114 int minPage
, maxPage
;
115 printout
->GetPageInfo(&minPage
, &maxPage
, &fromPage
, &toPage
);
120 m_printDialogData
.SetMinPage(minPage
);
121 m_printDialogData
.SetMaxPage(maxPage
);
123 m_printDialogData
.SetFromPage(fromPage
);
125 m_printDialogData
.SetToPage(toPage
);
129 m_printDialogData
.EnablePageNumbers(TRUE
);
130 if (m_printDialogData
.GetFromPage() < m_printDialogData
.GetMinPage())
131 m_printDialogData
.SetFromPage(m_printDialogData
.GetMinPage());
132 else if (m_printDialogData
.GetFromPage() > m_printDialogData
.GetMaxPage())
133 m_printDialogData
.SetFromPage(m_printDialogData
.GetMaxPage());
134 if (m_printDialogData
.GetToPage() > m_printDialogData
.GetMaxPage())
135 m_printDialogData
.SetToPage(m_printDialogData
.GetMaxPage());
136 else if (m_printDialogData
.GetToPage() < m_printDialogData
.GetMinPage())
137 m_printDialogData
.SetToPage(m_printDialogData
.GetMinPage());
140 m_printDialogData
.EnablePageNumbers(FALSE
);
142 // Create a suitable device context
146 dc
= PrintDialog(parent
);
152 // dc = new wxPrinterDC("", "", "", FALSE, m_printData.GetOrientation());
153 dc
= new wxPrinterDC(m_printDialogData
.GetPrintData());
156 // May have pressed cancel.
157 if (!dc
|| !dc
->Ok())
163 int logPPIScreenX
= 0;
164 int logPPIScreenY
= 0;
165 int logPPIPrinterX
= 0;
166 int logPPIPrinterY
= 0;
168 HDC hdc
= ::GetDC(NULL
);
169 logPPIScreenX
= ::GetDeviceCaps(hdc
, LOGPIXELSX
);
170 logPPIScreenY
= ::GetDeviceCaps(hdc
, LOGPIXELSY
);
171 ::ReleaseDC(NULL
, hdc
);
173 logPPIPrinterX
= ::GetDeviceCaps((HDC
) dc
->GetHDC(), LOGPIXELSX
);
174 logPPIPrinterY
= ::GetDeviceCaps((HDC
) dc
->GetHDC(), LOGPIXELSY
);
175 if (logPPIPrinterX
== 0 || logPPIPrinterY
== 0)
181 printout
->SetPPIScreen(logPPIScreenX
, logPPIScreenY
);
182 printout
->SetPPIPrinter(logPPIPrinterX
, logPPIPrinterY
);
184 // Set printout parameters
189 printout
->SetPageSizePixels((int)w
, (int)h
);
191 dc
->GetSizeMM(&w
, &h
);
192 printout
->SetPageSizeMM((int)w
, (int)h
);
194 // Create an abort window
197 printout
->OnPreparePrinting();
199 wxWindow
*win
= CreateAbortWindow(parent
, printout
);
202 #if defined(__BORLANDC__) || defined(__GNUWIN32__) || defined(__SALFORDC__) || !defined(__WIN32__)
204 ::SetAbortProc((HDC
) dc
->GetHDC(), (ABORTPROC
) m_lpAbortProc
);
206 ::SetAbortProc((HDC
) dc
->GetHDC(), (FARPROC
) m_lpAbortProc
);
209 ::SetAbortProc((HDC
) dc
->GetHDC(), (int (_stdcall
*)
210 // cast it to right type only if required
211 // FIXME it's really cdecl and we're casting it to stdcall - either there is
212 // something I don't understand or it will crash at first usage
224 wxLogDebug(wxT("Could not create an abort dialog."));
228 sm_abortWindow
= win
;
229 sm_abortWindow
->Show(TRUE
);
232 printout
->OnBeginPrinting();
236 copyCount
<= m_printDialogData
.GetNoCopies();
239 if (!printout
->OnBeginDocument(m_printDialogData
.GetFromPage(), m_printDialogData
.GetToPage()))
242 wxLogError(_("Could not start printing."));
249 for ( pn
= m_printDialogData
.GetFromPage();
250 pn
<= m_printDialogData
.GetToPage() && printout
->HasPage(pn
);
259 bool cont
= printout
->OnPrintPage(pn
);
266 printout
->OnEndDocument();
269 printout
->OnEndPrinting();
273 sm_abortWindow
->Show(FALSE
);
274 delete sm_abortWindow
;
275 sm_abortWindow
= NULL
;
285 wxDC
* wxWindowsPrinter::PrintDialog(wxWindow
*parent
)
287 wxDC
* dc
= (wxDC
*) NULL
;
289 wxPrintDialog
dialog(parent
, & m_printDialogData
);
290 int ret
= dialog
.ShowModal();
294 dc
= dialog
.GetPrintDC();
295 m_printDialogData
= dialog
.GetPrintDialogData();
301 bool wxWindowsPrinter::Setup(wxWindow
*parent
)
303 wxPrintDialog
dialog(parent
, & m_printDialogData
);
304 dialog
.GetPrintDialogData().SetSetupDialog(TRUE
);
306 int ret
= dialog
.ShowModal();
310 m_printDialogData
= dialog
.GetPrintDialogData();
313 return (ret
== wxID_OK
);
320 wxWindowsPrintPreview::wxWindowsPrintPreview(wxPrintout
*printout
,
321 wxPrintout
*printoutForPrinting
,
322 wxPrintDialogData
*data
)
323 : wxPrintPreviewBase(printout
, printoutForPrinting
, data
)
328 wxWindowsPrintPreview::wxWindowsPrintPreview(wxPrintout
*printout
,
329 wxPrintout
*printoutForPrinting
,
331 : wxPrintPreviewBase(printout
, printoutForPrinting
, data
)
336 wxWindowsPrintPreview::~wxWindowsPrintPreview()
340 bool wxWindowsPrintPreview::Print(bool interactive
)
342 if (!m_printPrintout
)
344 wxWindowsPrinter
printer(&m_printDialogData
);
345 return printer
.Print(m_previewFrame
, m_printPrintout
, interactive
);
348 void wxWindowsPrintPreview::DetermineScaling()
350 HDC dc
= ::GetDC(NULL
);
351 int screenWidth
= ::GetDeviceCaps(dc
, HORZSIZE
);
352 // int screenHeight = ::GetDeviceCaps(dc, VERTSIZE);
353 int screenXRes
= ::GetDeviceCaps(dc
, HORZRES
);
354 // int screenYRes = ::GetDeviceCaps(dc, VERTRES);
355 int logPPIScreenX
= ::GetDeviceCaps(dc
, LOGPIXELSX
);
356 int logPPIScreenY
= ::GetDeviceCaps(dc
, LOGPIXELSY
);
357 m_previewPrintout
->SetPPIScreen(logPPIScreenX
, logPPIScreenY
);
359 ::ReleaseDC(NULL
, dc
);
361 // Get a device context for the currently selected printer
362 wxPrinterDC
printerDC(m_printDialogData
.GetPrintData());
364 int printerWidth
= 150;
365 int printerHeight
= 250;
366 int printerXRes
= 1500;
367 int printerYRes
= 2500;
369 if (printerDC
.GetHDC())
371 printerWidth
= ::GetDeviceCaps((HDC
) printerDC
.GetHDC(), HORZSIZE
);
372 printerHeight
= ::GetDeviceCaps((HDC
) printerDC
.GetHDC(), VERTSIZE
);
373 printerXRes
= ::GetDeviceCaps((HDC
) printerDC
.GetHDC(), HORZRES
);
374 printerYRes
= ::GetDeviceCaps((HDC
) printerDC
.GetHDC(), VERTRES
);
376 int logPPIPrinterX
= ::GetDeviceCaps((HDC
) printerDC
.GetHDC(), LOGPIXELSX
);
377 int logPPIPrinterY
= ::GetDeviceCaps((HDC
) printerDC
.GetHDC(), LOGPIXELSY
);
379 m_previewPrintout
->SetPPIPrinter(logPPIPrinterX
, logPPIPrinterY
);
380 m_previewPrintout
->SetPageSizeMM(printerWidth
, printerHeight
);
382 if (logPPIPrinterX
== 0 || logPPIPrinterY
== 0 || printerWidth
== 0 || printerHeight
== 0)
388 m_pageWidth
= printerXRes
;
389 m_pageHeight
= printerYRes
;
391 // At 100%, the page should look about page-size on the screen.
392 m_previewScale
= (float)((float)screenWidth
/(float)printerWidth
);
393 m_previewScale
= m_previewScale
* (float)((float)screenXRes
/(float)printerYRes
);
396 /****************************************************************************
398 FUNCTION: wxAbortProc()
400 PURPOSE: Processes messages for the Abort Dialog box
402 ****************************************************************************/
404 LONG APIENTRY _EXPORT
wxAbortProc(HDC
WXUNUSED(hPr
), int WXUNUSED(Code
))
408 if (!wxPrinterBase::sm_abortWindow
) /* If the abort dialog isn't up yet */
411 /* Process messages intended for the abort dialog box */
413 while (!wxPrinterBase::sm_abortIt
&& PeekMessage(&msg
, 0, 0, 0, TRUE
))
414 if (!IsDialogMessage((HWND
) wxPrinterBase::sm_abortWindow
->GetHWND(), &msg
)) {
415 TranslateMessage(&msg
);
416 DispatchMessage(&msg
);
419 /* bAbort is TRUE (return is FALSE) if the user has aborted */
421 return (!wxPrinterBase::sm_abortIt
);