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