]>
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 FreeProcInstance((FARPROC
) m_lpAbortProc
);
93 bool wxWindowsPrinter::Print(wxWindow
*parent
, wxPrintout
*printout
, bool prompt
)
96 sm_abortWindow
= NULL
;
101 printout
->SetIsPreview(FALSE
);
103 // 4/9/99, JACS: this is a silly place to allow preparation, considering
104 // the DC and no parameters have been set in the printout object.
105 // Moved further down.
106 // printout->OnPreparePrinting();
108 // Get some parameters from the printout, if defined
109 int fromPage
, toPage
;
110 int minPage
, maxPage
;
111 printout
->GetPageInfo(&minPage
, &maxPage
, &fromPage
, &toPage
);
116 m_printDialogData
.SetMinPage(minPage
);
117 m_printDialogData
.SetMaxPage(maxPage
);
119 m_printDialogData
.SetFromPage(fromPage
);
121 m_printDialogData
.SetToPage(toPage
);
125 m_printDialogData
.EnablePageNumbers(TRUE
);
126 if (m_printDialogData
.GetFromPage() < m_printDialogData
.GetMinPage())
127 m_printDialogData
.SetFromPage(m_printDialogData
.GetMinPage());
128 else if (m_printDialogData
.GetFromPage() > m_printDialogData
.GetMaxPage())
129 m_printDialogData
.SetFromPage(m_printDialogData
.GetMaxPage());
130 if (m_printDialogData
.GetToPage() > m_printDialogData
.GetMaxPage())
131 m_printDialogData
.SetToPage(m_printDialogData
.GetMaxPage());
132 else if (m_printDialogData
.GetToPage() < m_printDialogData
.GetMinPage())
133 m_printDialogData
.SetToPage(m_printDialogData
.GetMinPage());
136 m_printDialogData
.EnablePageNumbers(FALSE
);
138 // Create a suitable device context
142 dc
= PrintDialog(parent
);
148 // dc = new wxPrinterDC("", "", "", FALSE, m_printData.GetOrientation());
149 dc
= new wxPrinterDC(m_printDialogData
.GetPrintData());
152 // May have pressed cancel.
153 if (!dc
|| !dc
->Ok())
159 int logPPIScreenX
= 0;
160 int logPPIScreenY
= 0;
161 int logPPIPrinterX
= 0;
162 int logPPIPrinterY
= 0;
164 HDC hdc
= ::GetDC(NULL
);
165 logPPIScreenX
= ::GetDeviceCaps(hdc
, LOGPIXELSX
);
166 logPPIScreenY
= ::GetDeviceCaps(hdc
, LOGPIXELSY
);
167 ::ReleaseDC(NULL
, hdc
);
169 logPPIPrinterX
= ::GetDeviceCaps((HDC
) dc
->GetHDC(), LOGPIXELSX
);
170 logPPIPrinterY
= ::GetDeviceCaps((HDC
) dc
->GetHDC(), LOGPIXELSY
);
171 if (logPPIPrinterX
== 0 || logPPIPrinterY
== 0)
177 printout
->SetPPIScreen(logPPIScreenX
, logPPIScreenY
);
178 printout
->SetPPIPrinter(logPPIPrinterX
, logPPIPrinterY
);
180 // Set printout parameters
185 printout
->SetPageSizePixels((int)w
, (int)h
);
187 dc
->GetSizeMM(&w
, &h
);
188 printout
->SetPageSizeMM((int)w
, (int)h
);
190 // Create an abort window
193 printout
->OnPreparePrinting();
195 wxWindow
*win
= CreateAbortWindow(parent
, printout
);
198 #if defined(__BORLANDC__) || defined(__GNUWIN32__) || defined(__SALFORDC__) || !defined(__WIN32__)
200 ::SetAbortProc((HDC
) dc
->GetHDC(), (ABORTPROC
) m_lpAbortProc
);
202 ::SetAbortProc((HDC
) dc
->GetHDC(), (FARPROC
) m_lpAbortProc
);
205 ::SetAbortProc((HDC
) dc
->GetHDC(), (int (_stdcall
*)
206 // cast it to right type only if required
207 // FIXME it's really cdecl and we're casting it to stdcall - either there is
208 // something I don't understand or it will crash at first usage
220 wxLogDebug(_T("Could not create an abort dialog."));
224 sm_abortWindow
= win
;
225 sm_abortWindow
->Show(TRUE
);
228 printout
->OnBeginPrinting();
230 bool keepGoing
= TRUE
;
233 for (copyCount
= 1; copyCount
<= m_printDialogData
.GetNoCopies(); copyCount
++)
235 if (!printout
->OnBeginDocument(m_printDialogData
.GetFromPage(), m_printDialogData
.GetToPage()))
238 wxLogError(_("Could not start printing."));
245 for (pn
= m_printDialogData
.GetFromPage(); keepGoing
&& (pn
<= m_printDialogData
.GetToPage()) && printout
->HasPage(pn
);
256 printout
->OnPrintPage(pn
);
260 printout
->OnEndDocument();
263 printout
->OnEndPrinting();
267 sm_abortWindow
->Show(FALSE
);
268 delete sm_abortWindow
;
269 sm_abortWindow
= NULL
;
279 wxDC
* wxWindowsPrinter::PrintDialog(wxWindow
*parent
)
281 wxDC
* dc
= (wxDC
*) NULL
;
283 wxPrintDialog
dialog(parent
, & m_printDialogData
);
284 int ret
= dialog
.ShowModal();
288 dc
= dialog
.GetPrintDC();
289 m_printDialogData
= dialog
.GetPrintDialogData();
295 bool wxWindowsPrinter::Setup(wxWindow
*parent
)
297 wxPrintDialog
dialog(parent
, & m_printDialogData
);
298 dialog
.GetPrintDialogData().SetSetupDialog(TRUE
);
300 int ret
= dialog
.ShowModal();
304 m_printDialogData
= dialog
.GetPrintDialogData();
307 return (ret
== wxID_OK
);
314 wxWindowsPrintPreview::wxWindowsPrintPreview(wxPrintout
*printout
,
315 wxPrintout
*printoutForPrinting
,
316 wxPrintDialogData
*data
)
317 : wxPrintPreviewBase(printout
, printoutForPrinting
, data
)
322 wxWindowsPrintPreview::wxWindowsPrintPreview(wxPrintout
*printout
,
323 wxPrintout
*printoutForPrinting
,
325 : wxPrintPreviewBase(printout
, printoutForPrinting
, data
)
330 wxWindowsPrintPreview::~wxWindowsPrintPreview()
334 bool wxWindowsPrintPreview::Print(bool interactive
)
336 if (!m_printPrintout
)
338 wxWindowsPrinter
printer(&m_printDialogData
);
339 return printer
.Print(m_previewFrame
, m_printPrintout
, interactive
);
342 void wxWindowsPrintPreview::DetermineScaling()
344 HDC dc
= ::GetDC(NULL
);
345 int screenWidth
= ::GetDeviceCaps(dc
, HORZSIZE
);
346 // int screenHeight = ::GetDeviceCaps(dc, VERTSIZE);
347 int screenXRes
= ::GetDeviceCaps(dc
, HORZRES
);
348 // int screenYRes = ::GetDeviceCaps(dc, VERTRES);
349 int logPPIScreenX
= ::GetDeviceCaps(dc
, LOGPIXELSX
);
350 int logPPIScreenY
= ::GetDeviceCaps(dc
, LOGPIXELSY
);
351 m_previewPrintout
->SetPPIScreen(logPPIScreenX
, logPPIScreenY
);
353 ::ReleaseDC(NULL
, dc
);
355 // Get a device context for the currently selected printer
356 wxPrinterDC
printerDC(m_printDialogData
.GetPrintData());
358 int printerWidth
= 150;
359 int printerHeight
= 250;
360 int printerXRes
= 1500;
361 int printerYRes
= 2500;
363 if (printerDC
.GetHDC())
365 printerWidth
= ::GetDeviceCaps((HDC
) printerDC
.GetHDC(), HORZSIZE
);
366 printerHeight
= ::GetDeviceCaps((HDC
) printerDC
.GetHDC(), VERTSIZE
);
367 printerXRes
= ::GetDeviceCaps((HDC
) printerDC
.GetHDC(), HORZRES
);
368 printerYRes
= ::GetDeviceCaps((HDC
) printerDC
.GetHDC(), VERTRES
);
370 int logPPIPrinterX
= ::GetDeviceCaps((HDC
) printerDC
.GetHDC(), LOGPIXELSX
);
371 int logPPIPrinterY
= ::GetDeviceCaps((HDC
) printerDC
.GetHDC(), LOGPIXELSY
);
373 m_previewPrintout
->SetPPIPrinter(logPPIPrinterX
, logPPIPrinterY
);
374 m_previewPrintout
->SetPageSizeMM(printerWidth
, printerHeight
);
376 if (logPPIPrinterX
== 0 || logPPIPrinterY
== 0 || printerWidth
== 0 || printerHeight
== 0)
382 m_pageWidth
= printerXRes
;
383 m_pageHeight
= printerYRes
;
385 // At 100%, the page should look about page-size on the screen.
386 m_previewScale
= (float)((float)screenWidth
/(float)printerWidth
);
387 m_previewScale
= m_previewScale
* (float)((float)screenXRes
/(float)printerYRes
);
390 /****************************************************************************
392 FUNCTION: wxAbortProc()
394 PURPOSE: Processes messages for the Abort Dialog box
396 ****************************************************************************/
398 LONG APIENTRY _EXPORT
wxAbortProc(HDC
WXUNUSED(hPr
), int WXUNUSED(Code
))
402 if (!wxPrinterBase::sm_abortWindow
) /* If the abort dialog isn't up yet */
405 /* Process messages intended for the abort dialog box */
407 while (!wxPrinterBase::sm_abortIt
&& PeekMessage(&msg
, 0, 0, 0, TRUE
))
408 if (!IsDialogMessage((HWND
) wxPrinterBase::sm_abortWindow
->GetHWND(), &msg
)) {
409 TranslateMessage(&msg
);
410 DispatchMessage(&msg
);
413 /* bAbort is TRUE (return is FALSE) if the user has aborted */
415 return (!wxPrinterBase::sm_abortIt
);