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