]>
Commit | Line | Data |
---|---|---|
2bda0e17 KB |
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$ | |
6c9a19aa JS |
8 | // Copyright: (c) Julian Smart |
9 | // Licence: wxWindows licence | |
2bda0e17 KB |
10 | ///////////////////////////////////////////////////////////////////////////// |
11 | ||
103aec29 VZ |
12 | // =========================================================================== |
13 | // declarations | |
14 | // =========================================================================== | |
15 | ||
16 | // --------------------------------------------------------------------------- | |
17 | // headers | |
18 | // --------------------------------------------------------------------------- | |
19 | ||
14f355c2 | 20 | #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA) |
103aec29 | 21 | #pragma implementation "printwin.h" |
2bda0e17 KB |
22 | #endif |
23 | ||
24 | // For compilers that support precompilation, includes "wx.h". | |
25 | #include "wx/wxprec.h" | |
26 | ||
27 | #ifdef __BORLANDC__ | |
103aec29 | 28 | #pragma hdrstop |
2bda0e17 KB |
29 | #endif |
30 | ||
31 | #include "wx/defs.h" | |
32 | ||
12bdd77c JS |
33 | // Don't use the Windows printer if we're in wxUniv mode and using |
34 | // the PostScript architecture | |
35 | #if wxUSE_PRINTING_ARCHITECTURE && (!defined(__WXUNIVERSAL__) || !wxUSE_POSTSCRIPT_ARCHITECTURE_IN_MSW) | |
f6bcfd97 | 36 | |
2bda0e17 | 37 | #ifndef WX_PRECOMP |
0c589ad0 BM |
38 | #include "wx/window.h" |
39 | #include "wx/msw/private.h" | |
103aec29 VZ |
40 | #include "wx/utils.h" |
41 | #include "wx/dc.h" | |
42 | #include "wx/app.h" | |
43 | #include "wx/msgdlg.h" | |
0c589ad0 | 44 | #include "wx/intl.h" |
2bda0e17 KB |
45 | #endif |
46 | ||
47 | #include "wx/msw/printwin.h" | |
48 | #include "wx/dcprint.h" | |
49 | #include "wx/printdlg.h" | |
6776a0b2 | 50 | #include "wx/log.h" |
2bda0e17 KB |
51 | #include "wx/msw/private.h" |
52 | ||
53 | #include <stdlib.h> | |
2bda0e17 | 54 | |
42e69d6b VZ |
55 | #include "wx/msw/private.h" |
56 | ||
57 | #include <commdlg.h> | |
2bda0e17 KB |
58 | |
59 | #ifndef __WIN32__ | |
103aec29 | 60 | #include <print.h> |
2bda0e17 KB |
61 | #endif |
62 | ||
103aec29 VZ |
63 | // --------------------------------------------------------------------------- |
64 | // private functions | |
65 | // --------------------------------------------------------------------------- | |
66 | ||
2bda0e17 KB |
67 | LONG APIENTRY _EXPORT wxAbortProc(HDC hPr, int Code); |
68 | ||
103aec29 VZ |
69 | // --------------------------------------------------------------------------- |
70 | // wxWin macros | |
71 | // --------------------------------------------------------------------------- | |
72 | ||
103aec29 VZ |
73 | IMPLEMENT_DYNAMIC_CLASS(wxWindowsPrinter, wxPrinterBase) |
74 | IMPLEMENT_CLASS(wxWindowsPrintPreview, wxPrintPreviewBase) | |
2bda0e17 | 75 | |
103aec29 VZ |
76 | // =========================================================================== |
77 | // implementation | |
78 | // =========================================================================== | |
79 | ||
80 | // --------------------------------------------------------------------------- | |
81 | // Printer | |
82 | // --------------------------------------------------------------------------- | |
7bcb11d3 | 83 | |
103aec29 VZ |
84 | wxWindowsPrinter::wxWindowsPrinter(wxPrintDialogData *data) |
85 | : wxPrinterBase(data) | |
2bda0e17 | 86 | { |
34da0970 | 87 | m_lpAbortProc = (WXFARPROC) MakeProcInstance((FARPROC) wxAbortProc, wxGetInstance()); |
2bda0e17 KB |
88 | } |
89 | ||
103aec29 | 90 | wxWindowsPrinter::~wxWindowsPrinter() |
2bda0e17 | 91 | { |
33ac7e6f | 92 | // avoids warning about statement with no effect (FreeProcInstance |
e41e579f | 93 | // doesn't do anything under Win32) |
d66dcb60 | 94 | #if !defined(__WIN32__) && !defined(__NT__) |
34da0970 | 95 | FreeProcInstance((FARPROC) m_lpAbortProc); |
a17e237f | 96 | #endif |
2bda0e17 KB |
97 | } |
98 | ||
99 | bool wxWindowsPrinter::Print(wxWindow *parent, wxPrintout *printout, bool prompt) | |
100 | { | |
e41e579f | 101 | sm_abortIt = false; |
7bcb11d3 | 102 | sm_abortWindow = NULL; |
103aec29 | 103 | |
7bcb11d3 | 104 | if (!printout) |
f6bcfd97 BP |
105 | { |
106 | sm_lastError = wxPRINTER_ERROR; | |
e41e579f | 107 | return false; |
f6bcfd97 | 108 | } |
103aec29 | 109 | |
e41e579f | 110 | printout->SetIsPreview(false); |
1044a386 | 111 | |
d2b354f9 JS |
112 | if (m_printDialogData.GetMinPage() < 1) |
113 | m_printDialogData.SetMinPage(1); | |
114 | if (m_printDialogData.GetMaxPage() < 1) | |
115 | m_printDialogData.SetMaxPage(9999); | |
2bda0e17 | 116 | |
103aec29 | 117 | // Create a suitable device context |
7bcb11d3 JS |
118 | wxDC *dc = NULL; |
119 | if (prompt) | |
120 | { | |
121 | dc = PrintDialog(parent); | |
122 | if (!dc) | |
e41e579f | 123 | return false; |
7bcb11d3 JS |
124 | } |
125 | else | |
126 | { | |
7bcb11d3 JS |
127 | dc = new wxPrinterDC(m_printDialogData.GetPrintData()); |
128 | } | |
103aec29 | 129 | |
7bcb11d3 JS |
130 | // May have pressed cancel. |
131 | if (!dc || !dc->Ok()) | |
132 | { | |
133 | if (dc) delete dc; | |
e41e579f | 134 | return false; |
7bcb11d3 | 135 | } |
103aec29 | 136 | |
7bcb11d3 JS |
137 | int logPPIScreenX = 0; |
138 | int logPPIScreenY = 0; | |
139 | int logPPIPrinterX = 0; | |
140 | int logPPIPrinterY = 0; | |
103aec29 | 141 | |
7bcb11d3 JS |
142 | HDC hdc = ::GetDC(NULL); |
143 | logPPIScreenX = ::GetDeviceCaps(hdc, LOGPIXELSX); | |
144 | logPPIScreenY = ::GetDeviceCaps(hdc, LOGPIXELSY); | |
145 | ::ReleaseDC(NULL, hdc); | |
103aec29 | 146 | |
7bcb11d3 JS |
147 | logPPIPrinterX = ::GetDeviceCaps((HDC) dc->GetHDC(), LOGPIXELSX); |
148 | logPPIPrinterY = ::GetDeviceCaps((HDC) dc->GetHDC(), LOGPIXELSY); | |
149 | if (logPPIPrinterX == 0 || logPPIPrinterY == 0) | |
150 | { | |
151 | delete dc; | |
f6bcfd97 | 152 | sm_lastError = wxPRINTER_ERROR; |
e41e579f | 153 | return false; |
7bcb11d3 | 154 | } |
103aec29 | 155 | |
7bcb11d3 JS |
156 | printout->SetPPIScreen(logPPIScreenX, logPPIScreenY); |
157 | printout->SetPPIPrinter(logPPIPrinterX, logPPIPrinterY); | |
103aec29 VZ |
158 | |
159 | // Set printout parameters | |
7bcb11d3 | 160 | printout->SetDC(dc); |
103aec29 | 161 | |
7bcb11d3 JS |
162 | int w, h; |
163 | dc->GetSize(&w, &h); | |
164 | printout->SetPageSizePixels((int)w, (int)h); | |
d6a1743b | 165 | |
7bcb11d3 JS |
166 | dc->GetSizeMM(&w, &h); |
167 | printout->SetPageSizeMM((int)w, (int)h); | |
103aec29 | 168 | |
7bcb11d3 JS |
169 | // Create an abort window |
170 | wxBeginBusyCursor(); | |
103aec29 | 171 | |
1044a386 JS |
172 | printout->OnPreparePrinting(); |
173 | ||
d2b354f9 JS |
174 | // Get some parameters from the printout, if defined |
175 | int fromPage, toPage; | |
176 | int minPage, maxPage; | |
177 | printout->GetPageInfo(&minPage, &maxPage, &fromPage, &toPage); | |
178 | ||
179 | if (maxPage == 0) | |
180 | { | |
181 | sm_lastError = wxPRINTER_ERROR; | |
182 | wxEndBusyCursor(); | |
e41e579f | 183 | return false; |
d2b354f9 JS |
184 | } |
185 | ||
186 | // Only set min and max, because from and to have been | |
187 | // set by the user | |
188 | m_printDialogData.SetMinPage(minPage); | |
189 | m_printDialogData.SetMaxPage(maxPage); | |
190 | ||
7bcb11d3 JS |
191 | wxWindow *win = CreateAbortWindow(parent, printout); |
192 | wxYield(); | |
103aec29 | 193 | |
ce3ed50d | 194 | #if defined(__BORLANDC__) || defined(__GNUWIN32__) || defined(__SALFORDC__) || !defined(__WIN32__) |
27a9bd48 PA |
195 | #ifdef STRICT |
196 | ::SetAbortProc((HDC) dc->GetHDC(), (ABORTPROC) m_lpAbortProc); | |
197 | #else | |
7bcb11d3 | 198 | ::SetAbortProc((HDC) dc->GetHDC(), (FARPROC) m_lpAbortProc); |
27a9bd48 | 199 | #endif |
7bcb11d3 JS |
200 | #else |
201 | ::SetAbortProc((HDC) dc->GetHDC(), (int (_stdcall *) | |
202 | // cast it to right type only if required | |
103aec29 VZ |
203 | // FIXME it's really cdecl and we're casting it to stdcall - either there is |
204 | // something I don't understand or it will crash at first usage | |
7bcb11d3 JS |
205 | #ifdef STRICT |
206 | (HDC, int) | |
d6a1743b | 207 | #else |
7bcb11d3 | 208 | () |
d6a1743b | 209 | #endif |
7bcb11d3 JS |
210 | )m_lpAbortProc); |
211 | #endif | |
103aec29 | 212 | |
7bcb11d3 | 213 | if (!win) |
2bda0e17 | 214 | { |
7bcb11d3 | 215 | wxEndBusyCursor(); |
223d09f6 | 216 | wxLogDebug(wxT("Could not create an abort dialog.")); |
f6bcfd97 | 217 | sm_lastError = wxPRINTER_ERROR; |
103aec29 | 218 | |
7bcb11d3 | 219 | delete dc; |
2bda0e17 | 220 | } |
7bcb11d3 | 221 | sm_abortWindow = win; |
e41e579f | 222 | sm_abortWindow->Show(); |
103aec29 VZ |
223 | wxSafeYield(); |
224 | ||
7bcb11d3 | 225 | printout->OnBeginPrinting(); |
103aec29 | 226 | |
f6bcfd97 BP |
227 | sm_lastError = wxPRINTER_NO_ERROR; |
228 | ||
e41e579f DS |
229 | int minPageNum = minPage, maxPageNum = maxPage; |
230 | ||
231 | if ( !m_printDialogData.GetAllPages() ) | |
e41e579f DS |
232 | { |
233 | minPageNum = m_printDialogData.GetFromPage(); | |
234 | maxPageNum = m_printDialogData.GetToPage(); | |
235 | } | |
236 | ||
7bcb11d3 | 237 | int copyCount; |
70846f0a VZ |
238 | for ( copyCount = 1; |
239 | copyCount <= m_printDialogData.GetNoCopies(); | |
240 | copyCount++ ) | |
7bcb11d3 | 241 | { |
e41e579f | 242 | if ( !printout->OnBeginDocument(minPageNum, maxPageNum) ) |
7bcb11d3 JS |
243 | { |
244 | wxEndBusyCursor(); | |
103aec29 | 245 | wxLogError(_("Could not start printing.")); |
f6bcfd97 | 246 | sm_lastError = wxPRINTER_ERROR; |
7bcb11d3 JS |
247 | break; |
248 | } | |
249 | if (sm_abortIt) | |
f6bcfd97 BP |
250 | { |
251 | sm_lastError = wxPRINTER_CANCELLED; | |
7bcb11d3 | 252 | break; |
f6bcfd97 | 253 | } |
103aec29 | 254 | |
7bcb11d3 | 255 | int pn; |
e41e579f DS |
256 | |
257 | for ( pn = minPageNum; | |
258 | pn <= maxPageNum && printout->HasPage(pn); | |
70846f0a | 259 | pn++ ) |
7bcb11d3 | 260 | { |
70846f0a | 261 | if ( sm_abortIt ) |
7bcb11d3 | 262 | { |
f6bcfd97 | 263 | sm_lastError = wxPRINTER_CANCELLED; |
7bcb11d3 JS |
264 | break; |
265 | } | |
70846f0a VZ |
266 | |
267 | dc->StartPage(); | |
268 | bool cont = printout->OnPrintPage(pn); | |
269 | dc->EndPage(); | |
270 | ||
271 | if ( !cont ) | |
f6bcfd97 BP |
272 | { |
273 | sm_lastError = wxPRINTER_CANCELLED; | |
70846f0a | 274 | break; |
f6bcfd97 | 275 | } |
7bcb11d3 | 276 | } |
70846f0a | 277 | |
7bcb11d3 JS |
278 | printout->OnEndDocument(); |
279 | } | |
103aec29 | 280 | |
7bcb11d3 | 281 | printout->OnEndPrinting(); |
103aec29 | 282 | |
7bcb11d3 | 283 | if (sm_abortWindow) |
2bda0e17 | 284 | { |
e41e579f | 285 | sm_abortWindow->Show(false); |
7bcb11d3 JS |
286 | delete sm_abortWindow; |
287 | sm_abortWindow = NULL; | |
2bda0e17 | 288 | } |
103aec29 | 289 | |
7bcb11d3 | 290 | wxEndBusyCursor(); |
103aec29 | 291 | |
7bcb11d3 | 292 | delete dc; |
103aec29 | 293 | |
f6bcfd97 | 294 | return (sm_lastError == wxPRINTER_NO_ERROR); |
7bcb11d3 | 295 | } |
2bda0e17 | 296 | |
7bcb11d3 JS |
297 | wxDC* wxWindowsPrinter::PrintDialog(wxWindow *parent) |
298 | { | |
299 | wxDC* dc = (wxDC*) NULL; | |
2bda0e17 | 300 | |
7bcb11d3 JS |
301 | wxPrintDialog dialog(parent, & m_printDialogData); |
302 | int ret = dialog.ShowModal(); | |
2bda0e17 | 303 | |
7bcb11d3 JS |
304 | if (ret == wxID_OK) |
305 | { | |
306 | dc = dialog.GetPrintDC(); | |
307 | m_printDialogData = dialog.GetPrintDialogData(); | |
33ac7e6f | 308 | if (dc == NULL) |
f6bcfd97 BP |
309 | sm_lastError = wxPRINTER_ERROR; |
310 | else | |
311 | sm_lastError = wxPRINTER_NO_ERROR; | |
7bcb11d3 | 312 | } |
f6bcfd97 BP |
313 | else |
314 | sm_lastError = wxPRINTER_CANCELLED; | |
2bda0e17 | 315 | |
7bcb11d3 | 316 | return dc; |
2bda0e17 KB |
317 | } |
318 | ||
319 | bool wxWindowsPrinter::Setup(wxWindow *parent) | |
320 | { | |
7bcb11d3 | 321 | wxPrintDialog dialog(parent, & m_printDialogData); |
e41e579f | 322 | dialog.GetPrintDialogData().SetSetupDialog(true); |
7bcb11d3 JS |
323 | |
324 | int ret = dialog.ShowModal(); | |
325 | ||
326 | if (ret == wxID_OK) | |
327 | { | |
328 | m_printDialogData = dialog.GetPrintDialogData(); | |
329 | } | |
330 | ||
331 | return (ret == wxID_OK); | |
2bda0e17 KB |
332 | } |
333 | ||
334 | /* | |
7bcb11d3 JS |
335 | * Print preview |
336 | */ | |
2bda0e17 | 337 | |
103aec29 VZ |
338 | wxWindowsPrintPreview::wxWindowsPrintPreview(wxPrintout *printout, |
339 | wxPrintout *printoutForPrinting, | |
340 | wxPrintDialogData *data) | |
341 | : wxPrintPreviewBase(printout, printoutForPrinting, data) | |
2bda0e17 | 342 | { |
7bcb11d3 | 343 | DetermineScaling(); |
2bda0e17 KB |
344 | } |
345 | ||
103aec29 VZ |
346 | wxWindowsPrintPreview::wxWindowsPrintPreview(wxPrintout *printout, |
347 | wxPrintout *printoutForPrinting, | |
348 | wxPrintData *data) | |
349 | : wxPrintPreviewBase(printout, printoutForPrinting, data) | |
350 | { | |
351 | DetermineScaling(); | |
352 | } | |
353 | ||
354 | wxWindowsPrintPreview::~wxWindowsPrintPreview() | |
2bda0e17 KB |
355 | { |
356 | } | |
357 | ||
358 | bool wxWindowsPrintPreview::Print(bool interactive) | |
359 | { | |
7bcb11d3 | 360 | if (!m_printPrintout) |
e41e579f | 361 | return false; |
7bcb11d3 JS |
362 | wxWindowsPrinter printer(&m_printDialogData); |
363 | return printer.Print(m_previewFrame, m_printPrintout, interactive); | |
2bda0e17 KB |
364 | } |
365 | ||
103aec29 | 366 | void wxWindowsPrintPreview::DetermineScaling() |
2bda0e17 KB |
367 | { |
368 | HDC dc = ::GetDC(NULL); | |
369 | int screenWidth = ::GetDeviceCaps(dc, HORZSIZE); | |
f4fefc23 | 370 | int screenYRes = ::GetDeviceCaps(dc, VERTRES); |
2bda0e17 KB |
371 | int logPPIScreenX = ::GetDeviceCaps(dc, LOGPIXELSX); |
372 | int logPPIScreenY = ::GetDeviceCaps(dc, LOGPIXELSY); | |
34da0970 | 373 | m_previewPrintout->SetPPIScreen(logPPIScreenX, logPPIScreenY); |
103aec29 | 374 | |
2bda0e17 | 375 | ::ReleaseDC(NULL, dc); |
103aec29 | 376 | |
2bda0e17 | 377 | // Get a device context for the currently selected printer |
7bcb11d3 | 378 | wxPrinterDC printerDC(m_printDialogData.GetPrintData()); |
103aec29 | 379 | |
2bda0e17 KB |
380 | int printerWidth = 150; |
381 | int printerHeight = 250; | |
382 | int printerXRes = 1500; | |
383 | int printerYRes = 2500; | |
103aec29 | 384 | |
f4fefc23 VZ |
385 | dc = GetHdcOf(printerDC); |
386 | if ( dc ) | |
2bda0e17 | 387 | { |
f4fefc23 VZ |
388 | printerWidth = ::GetDeviceCaps(dc, HORZSIZE); |
389 | printerHeight = ::GetDeviceCaps(dc, VERTSIZE); | |
390 | printerXRes = ::GetDeviceCaps(dc, HORZRES); | |
391 | printerYRes = ::GetDeviceCaps(dc, VERTRES); | |
103aec29 | 392 | |
f4fefc23 VZ |
393 | int logPPIPrinterX = ::GetDeviceCaps(dc, LOGPIXELSX); |
394 | int logPPIPrinterY = ::GetDeviceCaps(dc, LOGPIXELSY); | |
103aec29 | 395 | |
7bcb11d3 JS |
396 | m_previewPrintout->SetPPIPrinter(logPPIPrinterX, logPPIPrinterY); |
397 | m_previewPrintout->SetPageSizeMM(printerWidth, printerHeight); | |
103aec29 | 398 | |
7bcb11d3 | 399 | if (logPPIPrinterX == 0 || logPPIPrinterY == 0 || printerWidth == 0 || printerHeight == 0) |
e41e579f | 400 | m_isOk = false; |
2bda0e17 KB |
401 | } |
402 | else | |
e41e579f | 403 | m_isOk = false; |
103aec29 | 404 | |
34da0970 JS |
405 | m_pageWidth = printerXRes; |
406 | m_pageHeight = printerYRes; | |
103aec29 | 407 | |
2bda0e17 | 408 | // At 100%, the page should look about page-size on the screen. |
34da0970 | 409 | m_previewScale = (float)((float)screenWidth/(float)printerWidth); |
f4fefc23 | 410 | m_previewScale = m_previewScale * (float)((float)screenYRes/(float)printerYRes); |
2bda0e17 KB |
411 | } |
412 | ||
413 | /**************************************************************************** | |
414 | ||
7bcb11d3 | 415 | FUNCTION: wxAbortProc() |
103aec29 | 416 | |
2bda0e17 | 417 | PURPOSE: Processes messages for the Abort Dialog box |
103aec29 | 418 | |
2bda0e17 KB |
419 | ****************************************************************************/ |
420 | ||
421 | LONG APIENTRY _EXPORT wxAbortProc(HDC WXUNUSED(hPr), int WXUNUSED(Code)) | |
422 | { | |
423 | MSG msg; | |
103aec29 | 424 | |
34da0970 | 425 | if (!wxPrinterBase::sm_abortWindow) /* If the abort dialog isn't up yet */ |
2bda0e17 | 426 | return(TRUE); |
103aec29 | 427 | |
2bda0e17 | 428 | /* Process messages intended for the abort dialog box */ |
103aec29 | 429 | |
34da0970 JS |
430 | while (!wxPrinterBase::sm_abortIt && PeekMessage(&msg, 0, 0, 0, TRUE)) |
431 | if (!IsDialogMessage((HWND) wxPrinterBase::sm_abortWindow->GetHWND(), &msg)) { | |
2bda0e17 KB |
432 | TranslateMessage(&msg); |
433 | DispatchMessage(&msg); | |
434 | } | |
103aec29 | 435 | |
7bcb11d3 | 436 | /* bAbort is TRUE (return is FALSE) if the user has aborted */ |
103aec29 | 437 | |
7bcb11d3 | 438 | return (!wxPrinterBase::sm_abortIt); |
2bda0e17 KB |
439 | } |
440 | ||
f6bcfd97 BP |
441 | #endif |
442 | // wxUSE_PRINTING_ARCHITECTURE |