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