]>
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 /////////////////////////////////////////////////////////////////////////////
13 #pragma implementation "printwin.h"
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
25 #define WINDOWS_PRINTING (wxTheApp->GetPrintMode() == wxPRINT_WINDOWS)
31 #include "wx/msgdlg.h"
34 #include "wx/msw/printwin.h"
35 #include "wx/dcprint.h"
36 #include "wx/printdlg.h"
37 #include "wx/msw/private.h"
43 // Clash with Windows header files
52 LONG APIENTRY _EXPORT
wxAbortProc(HDC hPr
, int Code
);
54 #if !USE_SHARED_LIBRARY
55 IMPLEMENT_DYNAMIC_CLASS(wxWindowsPrinter
, wxPrinterBase
)
56 IMPLEMENT_CLASS(wxWindowsPrintPreview
, wxPrintPreviewBase
)
63 wxWindowsPrinter::wxWindowsPrinter(wxPrintData
*data
):
66 lpAbortProc
= (WXFARPROC
) MakeProcInstance((FARPROC
) wxAbortProc
, wxGetInstance());
69 wxWindowsPrinter::~wxWindowsPrinter(void)
71 FreeProcInstance((FARPROC
) lpAbortProc
);
74 bool wxWindowsPrinter::Print(wxWindow
*parent
, wxPrintout
*printout
, bool prompt
)
82 printout
->SetIsPreview(FALSE
);
83 printout
->OnPreparePrinting();
85 // Get some parameters from the printout, if defined
88 printout
->GetPageInfo(&minPage
, &maxPage
, &fromPage
, &toPage
);
93 printData
.SetMinPage(minPage
);
94 printData
.SetMaxPage(maxPage
);
96 printData
.SetFromPage(fromPage
);
98 printData
.SetToPage(toPage
);
102 printData
.EnablePageNumbers(TRUE
);
103 if (printData
.GetFromPage() < printData
.GetMinPage())
104 printData
.SetFromPage(printData
.GetMinPage());
105 else if (printData
.GetFromPage() > printData
.GetMaxPage())
106 printData
.SetFromPage(printData
.GetMaxPage());
107 if (printData
.GetToPage() > printData
.GetMaxPage())
108 printData
.SetToPage(printData
.GetMaxPage());
109 else if (printData
.GetToPage() < printData
.GetMinPage())
110 printData
.SetToPage(printData
.GetMinPage());
113 printData
.EnablePageNumbers(FALSE
);
115 // Create a suitable device context
119 wxPrintDialog
dialog(parent
, & printData
);
120 if (dialog
.ShowModal() == wxID_OK
)
122 dc
= dialog
.GetPrintDC();
123 printData
= dialog
.GetPrintData();
128 dc
= new wxPrinterDC("", "", "", FALSE
, printData
.GetOrientation());
131 // May have pressed cancel.
132 if (!dc
|| !dc
->Ok())
138 int logPPIScreenX
= 0;
139 int logPPIScreenY
= 0;
140 int logPPIPrinterX
= 0;
141 int logPPIPrinterY
= 0;
143 HDC hdc
= ::GetDC(NULL
);
144 logPPIScreenX
= ::GetDeviceCaps(hdc
, LOGPIXELSX
);
145 logPPIScreenY
= ::GetDeviceCaps(hdc
, LOGPIXELSY
);
146 ::ReleaseDC(NULL
, hdc
);
148 logPPIPrinterX
= ::GetDeviceCaps((HDC
) dc
->GetHDC(), LOGPIXELSX
);
149 logPPIPrinterY
= ::GetDeviceCaps((HDC
) dc
->GetHDC(), LOGPIXELSY
);
150 if (logPPIPrinterX
== 0 || logPPIPrinterY
== 0)
156 printout
->SetPPIScreen(logPPIScreenX
, logPPIScreenY
);
157 printout
->SetPPIPrinter(logPPIPrinterX
, logPPIPrinterY
);
159 // Set printout parameters
165 printout
->SetPageSizePixels((int)w
, (int)h
);
166 dc
->GetSizeMM(&ww
, &hh
);
167 printout
->SetPageSizeMM((int)ww
, (int)hh
);
169 // Create an abort window
172 wxWindow
*win
= CreateAbortWindow(parent
, printout
);
174 ::SetAbortProc((HDC
) dc
->GetHDC(), (FARPROC
) lpAbortProc
);
179 wxMessageBox("Sorry, could not create an abort dialog.", "Print Error", wxOK
, parent
);
183 abortWindow
->Show(TRUE
);
186 printout
->OnBeginPrinting();
188 bool keepGoing
= TRUE
;
191 for (copyCount
= 1; copyCount
<= printData
.GetNoCopies(); copyCount
++)
193 if (!printout
->OnBeginDocument(printData
.GetFromPage(), printData
.GetToPage()))
196 wxMessageBox("Could not start printing.", "Print Error", wxOK
, parent
);
203 for (pn
= printData
.GetFromPage(); keepGoing
&& (pn
<= printData
.GetToPage()) && printout
->HasPage(pn
);
213 // int dcID = ::SaveDC(dc->GetHDC());
215 printout
->OnPrintPage(pn
);
217 // ::RestoreDC(dc->GetHDC(), dcID);
220 printout
->OnEndDocument();
223 printout
->OnEndPrinting();
227 abortWindow
->Show(FALSE
);
239 bool wxWindowsPrinter::PrintDialog(wxWindow
*parent
)
241 wxPrintDialog
dialog(parent
, & printData
);
242 return (dialog
.ShowModal() == wxID_OK
);
245 bool wxWindowsPrinter::Setup(wxWindow
*parent
)
247 wxPrintDialog
dialog(parent
, & printData
);
248 dialog
.GetPrintData().SetSetupDialog(TRUE
);
249 return (dialog
.ShowModal() == wxID_OK
);
256 wxWindowsPrintPreview::wxWindowsPrintPreview(wxPrintout
*printout
, wxPrintout
*printoutForPrinting
, wxPrintData
*data
):
257 wxPrintPreviewBase(printout
, printoutForPrinting
, data
)
262 wxWindowsPrintPreview::~wxWindowsPrintPreview(void)
266 bool wxWindowsPrintPreview::Print(bool interactive
)
270 wxWindowsPrinter
printer(&printData
);
271 return printer
.Print(previewFrame
, printPrintout
, interactive
);
274 void wxWindowsPrintPreview::DetermineScaling(void)
276 HDC dc
= ::GetDC(NULL
);
277 int screenWidth
= ::GetDeviceCaps(dc
, HORZSIZE
);
278 // int screenHeight = ::GetDeviceCaps(dc, VERTSIZE);
279 int screenXRes
= ::GetDeviceCaps(dc
, HORZRES
);
280 // int screenYRes = ::GetDeviceCaps(dc, VERTRES);
281 int logPPIScreenX
= ::GetDeviceCaps(dc
, LOGPIXELSX
);
282 int logPPIScreenY
= ::GetDeviceCaps(dc
, LOGPIXELSY
);
283 previewPrintout
->SetPPIScreen(logPPIScreenX
, logPPIScreenY
);
285 ::ReleaseDC(NULL
, dc
);
287 // Get a device context for the currently selected printer
288 wxPrinterDC
printerDC("", "", "", FALSE
, printData
.GetOrientation());
290 int printerWidth
= 150;
291 int printerHeight
= 250;
292 int printerXRes
= 1500;
293 int printerYRes
= 2500;
295 if (printerDC
.GetHDC())
297 printerWidth
= ::GetDeviceCaps((HDC
) printerDC
.GetHDC(), HORZSIZE
);
298 printerHeight
= ::GetDeviceCaps((HDC
) printerDC
.GetHDC(), VERTSIZE
);
299 printerXRes
= ::GetDeviceCaps((HDC
) printerDC
.GetHDC(), HORZRES
);
300 printerYRes
= ::GetDeviceCaps((HDC
) printerDC
.GetHDC(), VERTRES
);
302 int logPPIPrinterX
= ::GetDeviceCaps((HDC
) printerDC
.GetHDC(), LOGPIXELSX
);
303 int logPPIPrinterY
= ::GetDeviceCaps((HDC
) printerDC
.GetHDC(), LOGPIXELSY
);
305 previewPrintout
->SetPPIPrinter(logPPIPrinterX
, logPPIPrinterY
);
306 previewPrintout
->SetPageSizeMM(printerWidth
, printerHeight
);
308 if (logPPIPrinterX
== 0 || logPPIPrinterY
== 0 || printerWidth
== 0 || printerHeight
== 0)
314 pageWidth
= printerXRes
;
315 pageHeight
= printerYRes
;
317 // At 100%, the page should look about page-size on the screen.
318 previewScale
= (float)((float)screenWidth
/(float)printerWidth
);
319 previewScale
= previewScale
* (float)((float)screenXRes
/(float)printerYRes
);
322 /****************************************************************************
324 FUNCTION: wxAbortProc()
326 PURPOSE: Processes messages for the Abort Dialog box
328 ****************************************************************************/
330 LONG APIENTRY _EXPORT
wxAbortProc(HDC
WXUNUSED(hPr
), int WXUNUSED(Code
))
334 if (!wxPrinterBase::abortWindow
) /* If the abort dialog isn't up yet */
337 /* Process messages intended for the abort dialog box */
339 while (!wxPrinterBase::abortIt
&& PeekMessage(&msg
, 0, 0, 0, TRUE
))
340 if (!IsDialogMessage((HWND
) wxPrinterBase::abortWindow
->GetHWND(), &msg
)) {
341 TranslateMessage(&msg
);
342 DispatchMessage(&msg
);
345 /* bAbort is TRUE (return is FALSE) if the user has aborted */
347 return (!wxPrinterBase::abortIt
);