]> git.saurik.com Git - wxWidgets.git/blob - src/msw/printwin.cpp
f60b5747257b436dd4e5f0df09f4de06e44529dd
[wxWidgets.git] / src / msw / printwin.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: printwin.cpp
3 // Purpose: wxWindowsPrinter framework
4 // Author: Julian Smart
5 // Modified by:
6 // Created: 04/01/98
7 // RCS-ID: $Id$
8 // Copyright: (c) Julian Smart and Markus Holzem
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
11
12 #ifdef __GNUG__
13 #pragma implementation "printwin.h"
14 #endif
15
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
18
19 #ifdef __BORLANDC__
20 #pragma hdrstop
21 #endif
22
23 #include "wx/defs.h"
24
25 #define WINDOWS_PRINTING (wxTheApp->GetPrintMode() == wxPRINT_WINDOWS)
26
27 #ifndef WX_PRECOMP
28 #include "wx/utils.h"
29 #include "wx/dc.h"
30 #include "wx/app.h"
31 #include "wx/msgdlg.h"
32 #endif
33
34 #include "wx/msw/printwin.h"
35 #include "wx/dcprint.h"
36 #include "wx/printdlg.h"
37 #include "wx/msw/private.h"
38
39 #include <stdlib.h>
40 #include <windows.h>
41 #include <commdlg.h>
42
43 // Clash with Windows header files
44 #ifdef StartDoc
45 #undef StartDoc
46 #endif
47
48 #ifndef __WIN32__
49 #include <print.h>
50 #endif
51
52 LONG APIENTRY _EXPORT wxAbortProc(HDC hPr, int Code);
53
54 #if !USE_SHARED_LIBRARY
55 IMPLEMENT_DYNAMIC_CLASS(wxWindowsPrinter, wxPrinterBase)
56 IMPLEMENT_CLASS(wxWindowsPrintPreview, wxPrintPreviewBase)
57 #endif
58
59 /*
60 * Printer
61 */
62
63 wxWindowsPrinter::wxWindowsPrinter(wxPrintData *data):
64 wxPrinterBase(data)
65 {
66 lpAbortProc = (WXFARPROC) MakeProcInstance((FARPROC) wxAbortProc, wxGetInstance());
67 }
68
69 wxWindowsPrinter::~wxWindowsPrinter(void)
70 {
71 FreeProcInstance((FARPROC) lpAbortProc);
72 }
73
74 bool wxWindowsPrinter::Print(wxWindow *parent, wxPrintout *printout, bool prompt)
75 {
76 abortIt = FALSE;
77 abortWindow = NULL;
78
79 if (!printout)
80 return FALSE;
81
82 printout->SetIsPreview(FALSE);
83 printout->OnPreparePrinting();
84
85 // Get some parameters from the printout, if defined
86 int fromPage, toPage;
87 int minPage, maxPage;
88 printout->GetPageInfo(&minPage, &maxPage, &fromPage, &toPage);
89
90 if (maxPage == 0)
91 return FALSE;
92
93 printData.SetMinPage(minPage);
94 printData.SetMaxPage(maxPage);
95 if (fromPage != 0)
96 printData.SetFromPage(fromPage);
97 if (toPage != 0)
98 printData.SetToPage(toPage);
99
100 if (minPage != 0)
101 {
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());
111 }
112 else
113 printData.EnablePageNumbers(FALSE);
114
115 // Create a suitable device context
116 wxDC *dc = NULL;
117 if (prompt)
118 {
119 wxPrintDialog dialog(parent, & printData);
120 if (dialog.ShowModal() == wxID_OK)
121 {
122 dc = dialog.GetPrintDC();
123 printData = dialog.GetPrintData();
124 }
125 }
126 else
127 {
128 dc = new wxPrinterDC("", "", "", FALSE, printData.GetOrientation());
129 }
130
131 // May have pressed cancel.
132 if (!dc || !dc->Ok())
133 {
134 if (dc) delete dc;
135 return FALSE;
136 }
137
138 int logPPIScreenX = 0;
139 int logPPIScreenY = 0;
140 int logPPIPrinterX = 0;
141 int logPPIPrinterY = 0;
142
143 HDC hdc = ::GetDC(NULL);
144 logPPIScreenX = ::GetDeviceCaps(hdc, LOGPIXELSX);
145 logPPIScreenY = ::GetDeviceCaps(hdc, LOGPIXELSY);
146 ::ReleaseDC(NULL, hdc);
147
148 logPPIPrinterX = ::GetDeviceCaps((HDC) dc->GetHDC(), LOGPIXELSX);
149 logPPIPrinterY = ::GetDeviceCaps((HDC) dc->GetHDC(), LOGPIXELSY);
150 if (logPPIPrinterX == 0 || logPPIPrinterY == 0)
151 {
152 delete dc;
153 return FALSE;
154 }
155
156 printout->SetPPIScreen(logPPIScreenX, logPPIScreenY);
157 printout->SetPPIPrinter(logPPIPrinterX, logPPIPrinterY);
158
159 // Set printout parameters
160 printout->SetDC(dc);
161
162 int w, h;
163 long ww, hh;
164 dc->GetSize(&w, &h);
165 printout->SetPageSizePixels((int)w, (int)h);
166 dc->GetSizeMM(&ww, &hh);
167 printout->SetPageSizeMM((int)ww, (int)hh);
168
169 // Create an abort window
170 wxBeginBusyCursor();
171
172 wxWindow *win = CreateAbortWindow(parent, printout);
173 wxYield();
174
175 #if defined(__BORLANDC__) || defined(__GNUWIN32__)
176 ::SetAbortProc((HDC) dc->GetHDC(), (FARPROC) lpAbortProc);
177 #else
178 ::SetAbortProc((HDC) dc->GetHDC(), (int (_stdcall *)(HDC, int)) lpAbortProc);
179 #endif
180
181 if (!win)
182 {
183 wxEndBusyCursor();
184 wxMessageBox("Sorry, could not create an abort dialog.", "Print Error", wxOK, parent);
185 delete dc;
186 }
187 abortWindow = win;
188 abortWindow->Show(TRUE);
189 wxYield();
190
191 printout->OnBeginPrinting();
192
193 bool keepGoing = TRUE;
194
195 int copyCount;
196 for (copyCount = 1; copyCount <= printData.GetNoCopies(); copyCount ++)
197 {
198 if (!printout->OnBeginDocument(printData.GetFromPage(), printData.GetToPage()))
199 {
200 wxEndBusyCursor();
201 wxMessageBox("Could not start printing.", "Print Error", wxOK, parent);
202 break;
203 }
204 if (abortIt)
205 break;
206
207 int pn;
208 for (pn = printData.GetFromPage(); keepGoing && (pn <= printData.GetToPage()) && printout->HasPage(pn);
209 pn++)
210 {
211 if (abortIt)
212 {
213 keepGoing = FALSE;
214 break;
215 }
216 else
217 {
218 // int dcID = ::SaveDC(dc->GetHDC());
219 dc->StartPage();
220 printout->OnPrintPage(pn);
221 dc->EndPage();
222 // ::RestoreDC(dc->GetHDC(), dcID);
223 }
224 }
225 printout->OnEndDocument();
226 }
227
228 printout->OnEndPrinting();
229
230 if (abortWindow)
231 {
232 abortWindow->Show(FALSE);
233 delete abortWindow;
234 abortWindow = NULL;
235 }
236
237 wxEndBusyCursor();
238
239 delete dc;
240
241 return TRUE;
242 }
243
244 bool wxWindowsPrinter::PrintDialog(wxWindow *parent)
245 {
246 wxPrintDialog dialog(parent, & printData);
247 return (dialog.ShowModal() == wxID_OK);
248 }
249
250 bool wxWindowsPrinter::Setup(wxWindow *parent)
251 {
252 wxPrintDialog dialog(parent, & printData);
253 dialog.GetPrintData().SetSetupDialog(TRUE);
254 return (dialog.ShowModal() == wxID_OK);
255 }
256
257 /*
258 * Print preview
259 */
260
261 wxWindowsPrintPreview::wxWindowsPrintPreview(wxPrintout *printout, wxPrintout *printoutForPrinting, wxPrintData *data):
262 wxPrintPreviewBase(printout, printoutForPrinting, data)
263 {
264 DetermineScaling();
265 }
266
267 wxWindowsPrintPreview::~wxWindowsPrintPreview(void)
268 {
269 }
270
271 bool wxWindowsPrintPreview::Print(bool interactive)
272 {
273 if (!printPrintout)
274 return FALSE;
275 wxWindowsPrinter printer(&printData);
276 return printer.Print(previewFrame, printPrintout, interactive);
277 }
278
279 void wxWindowsPrintPreview::DetermineScaling(void)
280 {
281 HDC dc = ::GetDC(NULL);
282 int screenWidth = ::GetDeviceCaps(dc, HORZSIZE);
283 // int screenHeight = ::GetDeviceCaps(dc, VERTSIZE);
284 int screenXRes = ::GetDeviceCaps(dc, HORZRES);
285 // int screenYRes = ::GetDeviceCaps(dc, VERTRES);
286 int logPPIScreenX = ::GetDeviceCaps(dc, LOGPIXELSX);
287 int logPPIScreenY = ::GetDeviceCaps(dc, LOGPIXELSY);
288 previewPrintout->SetPPIScreen(logPPIScreenX, logPPIScreenY);
289
290 ::ReleaseDC(NULL, dc);
291
292 // Get a device context for the currently selected printer
293 wxPrinterDC printerDC("", "", "", FALSE, printData.GetOrientation());
294
295 int printerWidth = 150;
296 int printerHeight = 250;
297 int printerXRes = 1500;
298 int printerYRes = 2500;
299
300 if (printerDC.GetHDC())
301 {
302 printerWidth = ::GetDeviceCaps((HDC) printerDC.GetHDC(), HORZSIZE);
303 printerHeight = ::GetDeviceCaps((HDC) printerDC.GetHDC(), VERTSIZE);
304 printerXRes = ::GetDeviceCaps((HDC) printerDC.GetHDC(), HORZRES);
305 printerYRes = ::GetDeviceCaps((HDC) printerDC.GetHDC(), VERTRES);
306
307 int logPPIPrinterX = ::GetDeviceCaps((HDC) printerDC.GetHDC(), LOGPIXELSX);
308 int logPPIPrinterY = ::GetDeviceCaps((HDC) printerDC.GetHDC(), LOGPIXELSY);
309
310 previewPrintout->SetPPIPrinter(logPPIPrinterX, logPPIPrinterY);
311 previewPrintout->SetPageSizeMM(printerWidth, printerHeight);
312
313 if (logPPIPrinterX == 0 || logPPIPrinterY == 0 || printerWidth == 0 || printerHeight == 0)
314 isOk = FALSE;
315 }
316 else
317 isOk = FALSE;
318
319 pageWidth = printerXRes;
320 pageHeight = printerYRes;
321
322 // At 100%, the page should look about page-size on the screen.
323 previewScale = (float)((float)screenWidth/(float)printerWidth);
324 previewScale = previewScale * (float)((float)screenXRes/(float)printerYRes);
325 }
326
327 /****************************************************************************
328
329 FUNCTION: wxAbortProc()
330
331 PURPOSE: Processes messages for the Abort Dialog box
332
333 ****************************************************************************/
334
335 LONG APIENTRY _EXPORT wxAbortProc(HDC WXUNUSED(hPr), int WXUNUSED(Code))
336 {
337 MSG msg;
338
339 if (!wxPrinterBase::abortWindow) /* If the abort dialog isn't up yet */
340 return(TRUE);
341
342 /* Process messages intended for the abort dialog box */
343
344 while (!wxPrinterBase::abortIt && PeekMessage(&msg, 0, 0, 0, TRUE))
345 if (!IsDialogMessage((HWND) wxPrinterBase::abortWindow->GetHWND(), &msg)) {
346 TranslateMessage(&msg);
347 DispatchMessage(&msg);
348 }
349
350 /* bAbort is TRUE (return is FALSE) if the user has aborted */
351
352 return (!wxPrinterBase::abortIt);
353 }
354