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