]>
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 IMPLEMENT_DYNAMIC_CLASS(wxWindowsPrinter
, wxPrinterBase
)
70 IMPLEMENT_CLASS(wxWindowsPrintPreview
, wxPrintPreviewBase
)
72 // ===========================================================================
74 // ===========================================================================
76 // ---------------------------------------------------------------------------
78 // ---------------------------------------------------------------------------
80 wxWindowsPrinter::wxWindowsPrinter(wxPrintDialogData
*data
)
83 m_lpAbortProc
= (WXFARPROC
) MakeProcInstance((FARPROC
) wxAbortProc
, wxGetInstance());
86 wxWindowsPrinter::~wxWindowsPrinter()
88 // avoids mingw warning about statement with no effect (FreeProcInstance
89 // doesn't do anything under Win32)
91 FreeProcInstance((FARPROC
) m_lpAbortProc
);
95 bool wxWindowsPrinter::Print(wxWindow
*parent
, wxPrintout
*printout
, bool prompt
)
98 sm_abortWindow
= NULL
;
103 printout
->SetIsPreview(FALSE
);
105 // 4/9/99, JACS: this is a silly place to allow preparation, considering
106 // the DC and no parameters have been set in the printout object.
107 // Moved further down.
108 // printout->OnPreparePrinting();
110 // Get some parameters from the printout, if defined
111 int fromPage
, toPage
;
112 int minPage
, maxPage
;
113 printout
->GetPageInfo(&minPage
, &maxPage
, &fromPage
, &toPage
);
118 m_printDialogData
.SetMinPage(minPage
);
119 m_printDialogData
.SetMaxPage(maxPage
);
121 m_printDialogData
.SetFromPage(fromPage
);
123 m_printDialogData
.SetToPage(toPage
);
127 m_printDialogData
.EnablePageNumbers(TRUE
);
128 if (m_printDialogData
.GetFromPage() < m_printDialogData
.GetMinPage())
129 m_printDialogData
.SetFromPage(m_printDialogData
.GetMinPage());
130 else if (m_printDialogData
.GetFromPage() > m_printDialogData
.GetMaxPage())
131 m_printDialogData
.SetFromPage(m_printDialogData
.GetMaxPage());
132 if (m_printDialogData
.GetToPage() > m_printDialogData
.GetMaxPage())
133 m_printDialogData
.SetToPage(m_printDialogData
.GetMaxPage());
134 else if (m_printDialogData
.GetToPage() < m_printDialogData
.GetMinPage())
135 m_printDialogData
.SetToPage(m_printDialogData
.GetMinPage());
138 m_printDialogData
.EnablePageNumbers(FALSE
);
140 // Create a suitable device context
144 dc
= PrintDialog(parent
);
150 // dc = new wxPrinterDC("", "", "", FALSE, m_printData.GetOrientation());
151 dc
= new wxPrinterDC(m_printDialogData
.GetPrintData());
154 // May have pressed cancel.
155 if (!dc
|| !dc
->Ok())
161 int logPPIScreenX
= 0;
162 int logPPIScreenY
= 0;
163 int logPPIPrinterX
= 0;
164 int logPPIPrinterY
= 0;
166 HDC hdc
= ::GetDC(NULL
);
167 logPPIScreenX
= ::GetDeviceCaps(hdc
, LOGPIXELSX
);
168 logPPIScreenY
= ::GetDeviceCaps(hdc
, LOGPIXELSY
);
169 ::ReleaseDC(NULL
, hdc
);
171 logPPIPrinterX
= ::GetDeviceCaps((HDC
) dc
->GetHDC(), LOGPIXELSX
);
172 logPPIPrinterY
= ::GetDeviceCaps((HDC
) dc
->GetHDC(), LOGPIXELSY
);
173 if (logPPIPrinterX
== 0 || logPPIPrinterY
== 0)
179 printout
->SetPPIScreen(logPPIScreenX
, logPPIScreenY
);
180 printout
->SetPPIPrinter(logPPIPrinterX
, logPPIPrinterY
);
182 // Set printout parameters
187 printout
->SetPageSizePixels((int)w
, (int)h
);
189 dc
->GetSizeMM(&w
, &h
);
190 printout
->SetPageSizeMM((int)w
, (int)h
);
192 // Create an abort window
195 printout
->OnPreparePrinting();
197 wxWindow
*win
= CreateAbortWindow(parent
, printout
);
200 #if defined(__BORLANDC__) || defined(__GNUWIN32__) || defined(__SALFORDC__) || !defined(__WIN32__)
202 ::SetAbortProc((HDC
) dc
->GetHDC(), (ABORTPROC
) m_lpAbortProc
);
204 ::SetAbortProc((HDC
) dc
->GetHDC(), (FARPROC
) m_lpAbortProc
);
207 ::SetAbortProc((HDC
) dc
->GetHDC(), (int (_stdcall
*)
208 // cast it to right type only if required
209 // FIXME it's really cdecl and we're casting it to stdcall - either there is
210 // something I don't understand or it will crash at first usage
222 wxLogDebug(wxT("Could not create an abort dialog."));
226 sm_abortWindow
= win
;
227 sm_abortWindow
->Show(TRUE
);
230 printout
->OnBeginPrinting();
234 copyCount
<= m_printDialogData
.GetNoCopies();
237 if (!printout
->OnBeginDocument(m_printDialogData
.GetFromPage(), m_printDialogData
.GetToPage()))
240 wxLogError(_("Could not start printing."));
247 for ( pn
= m_printDialogData
.GetFromPage();
248 pn
<= m_printDialogData
.GetToPage() && printout
->HasPage(pn
);
257 bool cont
= printout
->OnPrintPage(pn
);
264 printout
->OnEndDocument();
267 printout
->OnEndPrinting();
271 sm_abortWindow
->Show(FALSE
);
272 delete sm_abortWindow
;
273 sm_abortWindow
= NULL
;
283 wxDC
* wxWindowsPrinter::PrintDialog(wxWindow
*parent
)
285 wxDC
* dc
= (wxDC
*) NULL
;
287 wxPrintDialog
dialog(parent
, & m_printDialogData
);
288 int ret
= dialog
.ShowModal();
292 dc
= dialog
.GetPrintDC();
293 m_printDialogData
= dialog
.GetPrintDialogData();
299 bool wxWindowsPrinter::Setup(wxWindow
*parent
)
301 wxPrintDialog
dialog(parent
, & m_printDialogData
);
302 dialog
.GetPrintDialogData().SetSetupDialog(TRUE
);
304 int ret
= dialog
.ShowModal();
308 m_printDialogData
= dialog
.GetPrintDialogData();
311 return (ret
== wxID_OK
);
318 wxWindowsPrintPreview::wxWindowsPrintPreview(wxPrintout
*printout
,
319 wxPrintout
*printoutForPrinting
,
320 wxPrintDialogData
*data
)
321 : wxPrintPreviewBase(printout
, printoutForPrinting
, data
)
326 wxWindowsPrintPreview::wxWindowsPrintPreview(wxPrintout
*printout
,
327 wxPrintout
*printoutForPrinting
,
329 : wxPrintPreviewBase(printout
, printoutForPrinting
, data
)
334 wxWindowsPrintPreview::~wxWindowsPrintPreview()
338 bool wxWindowsPrintPreview::Print(bool interactive
)
340 if (!m_printPrintout
)
342 wxWindowsPrinter
printer(&m_printDialogData
);
343 return printer
.Print(m_previewFrame
, m_printPrintout
, interactive
);
346 void wxWindowsPrintPreview::DetermineScaling()
348 HDC dc
= ::GetDC(NULL
);
349 int screenWidth
= ::GetDeviceCaps(dc
, HORZSIZE
);
350 // int screenHeight = ::GetDeviceCaps(dc, VERTSIZE);
351 int screenXRes
= ::GetDeviceCaps(dc
, HORZRES
);
352 // int screenYRes = ::GetDeviceCaps(dc, VERTRES);
353 int logPPIScreenX
= ::GetDeviceCaps(dc
, LOGPIXELSX
);
354 int logPPIScreenY
= ::GetDeviceCaps(dc
, LOGPIXELSY
);
355 m_previewPrintout
->SetPPIScreen(logPPIScreenX
, logPPIScreenY
);
357 ::ReleaseDC(NULL
, dc
);
359 // Get a device context for the currently selected printer
360 wxPrinterDC
printerDC(m_printDialogData
.GetPrintData());
362 int printerWidth
= 150;
363 int printerHeight
= 250;
364 int printerXRes
= 1500;
365 int printerYRes
= 2500;
367 if (printerDC
.GetHDC())
369 printerWidth
= ::GetDeviceCaps((HDC
) printerDC
.GetHDC(), HORZSIZE
);
370 printerHeight
= ::GetDeviceCaps((HDC
) printerDC
.GetHDC(), VERTSIZE
);
371 printerXRes
= ::GetDeviceCaps((HDC
) printerDC
.GetHDC(), HORZRES
);
372 printerYRes
= ::GetDeviceCaps((HDC
) printerDC
.GetHDC(), VERTRES
);
374 int logPPIPrinterX
= ::GetDeviceCaps((HDC
) printerDC
.GetHDC(), LOGPIXELSX
);
375 int logPPIPrinterY
= ::GetDeviceCaps((HDC
) printerDC
.GetHDC(), LOGPIXELSY
);
377 m_previewPrintout
->SetPPIPrinter(logPPIPrinterX
, logPPIPrinterY
);
378 m_previewPrintout
->SetPageSizeMM(printerWidth
, printerHeight
);
380 if (logPPIPrinterX
== 0 || logPPIPrinterY
== 0 || printerWidth
== 0 || printerHeight
== 0)
386 m_pageWidth
= printerXRes
;
387 m_pageHeight
= printerYRes
;
389 // At 100%, the page should look about page-size on the screen.
390 m_previewScale
= (float)((float)screenWidth
/(float)printerWidth
);
391 m_previewScale
= m_previewScale
* (float)((float)screenXRes
/(float)printerYRes
);
394 /****************************************************************************
396 FUNCTION: wxAbortProc()
398 PURPOSE: Processes messages for the Abort Dialog box
400 ****************************************************************************/
402 LONG APIENTRY _EXPORT
wxAbortProc(HDC
WXUNUSED(hPr
), int WXUNUSED(Code
))
406 if (!wxPrinterBase::sm_abortWindow
) /* If the abort dialog isn't up yet */
409 /* Process messages intended for the abort dialog box */
411 while (!wxPrinterBase::sm_abortIt
&& PeekMessage(&msg
, 0, 0, 0, TRUE
))
412 if (!IsDialogMessage((HWND
) wxPrinterBase::sm_abortWindow
->GetHWND(), &msg
)) {
413 TranslateMessage(&msg
);
414 DispatchMessage(&msg
);
417 /* bAbort is TRUE (return is FALSE) if the user has aborted */
419 return (!wxPrinterBase::sm_abortIt
);