]>
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 and Markus Holzem | |
9 | // Licence: wxWindows license | |
10 | ///////////////////////////////////////////////////////////////////////////// | |
11 | ||
12 | // =========================================================================== | |
13 | // declarations | |
14 | // =========================================================================== | |
15 | ||
16 | // --------------------------------------------------------------------------- | |
17 | // headers | |
18 | // --------------------------------------------------------------------------- | |
19 | ||
20 | #ifdef __GNUG__ | |
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 | #ifndef WX_PRECOMP | |
34 | #include "wx/window.h" | |
35 | #include "wx/msw/private.h" | |
36 | #include "wx/utils.h" | |
37 | #include "wx/dc.h" | |
38 | #include "wx/app.h" | |
39 | #include "wx/msgdlg.h" | |
40 | #include "wx/intl.h" | |
41 | #endif | |
42 | ||
43 | #include "wx/msw/printwin.h" | |
44 | #include "wx/dcprint.h" | |
45 | #include "wx/printdlg.h" | |
46 | #include "wx/log.h" | |
47 | #include "wx/msw/private.h" | |
48 | ||
49 | #include <stdlib.h> | |
50 | ||
51 | #include "wx/msw/private.h" | |
52 | ||
53 | #include <commdlg.h> | |
54 | ||
55 | #ifndef __WIN32__ | |
56 | #include <print.h> | |
57 | #endif | |
58 | ||
59 | // --------------------------------------------------------------------------- | |
60 | // private functions | |
61 | // --------------------------------------------------------------------------- | |
62 | ||
63 | LONG APIENTRY _EXPORT wxAbortProc(HDC hPr, int Code); | |
64 | ||
65 | // --------------------------------------------------------------------------- | |
66 | // wxWin macros | |
67 | // --------------------------------------------------------------------------- | |
68 | ||
69 | #if !USE_SHARED_LIBRARY | |
70 | IMPLEMENT_DYNAMIC_CLASS(wxWindowsPrinter, wxPrinterBase) | |
71 | IMPLEMENT_CLASS(wxWindowsPrintPreview, wxPrintPreviewBase) | |
72 | #endif | |
73 | ||
74 | // =========================================================================== | |
75 | // implementation | |
76 | // =========================================================================== | |
77 | ||
78 | // --------------------------------------------------------------------------- | |
79 | // Printer | |
80 | // --------------------------------------------------------------------------- | |
81 | ||
82 | wxWindowsPrinter::wxWindowsPrinter(wxPrintDialogData *data) | |
83 | : wxPrinterBase(data) | |
84 | { | |
85 | m_lpAbortProc = (WXFARPROC) MakeProcInstance((FARPROC) wxAbortProc, wxGetInstance()); | |
86 | } | |
87 | ||
88 | wxWindowsPrinter::~wxWindowsPrinter() | |
89 | { | |
90 | FreeProcInstance((FARPROC) m_lpAbortProc); | |
91 | } | |
92 | ||
93 | bool wxWindowsPrinter::Print(wxWindow *parent, wxPrintout *printout, bool prompt) | |
94 | { | |
95 | sm_abortIt = FALSE; | |
96 | sm_abortWindow = NULL; | |
97 | ||
98 | if (!printout) | |
99 | return FALSE; | |
100 | ||
101 | printout->SetIsPreview(FALSE); | |
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(); | |
107 | ||
108 | // Get some parameters from the printout, if defined | |
109 | int fromPage, toPage; | |
110 | int minPage, maxPage; | |
111 | printout->GetPageInfo(&minPage, &maxPage, &fromPage, &toPage); | |
112 | ||
113 | if (maxPage == 0) | |
114 | return FALSE; | |
115 | ||
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); | |
122 | ||
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); | |
137 | ||
138 | // Create a suitable device context | |
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 | } | |
151 | ||
152 | // May have pressed cancel. | |
153 | if (!dc || !dc->Ok()) | |
154 | { | |
155 | if (dc) delete dc; | |
156 | return FALSE; | |
157 | } | |
158 | ||
159 | int logPPIScreenX = 0; | |
160 | int logPPIScreenY = 0; | |
161 | int logPPIPrinterX = 0; | |
162 | int logPPIPrinterY = 0; | |
163 | ||
164 | HDC hdc = ::GetDC(NULL); | |
165 | logPPIScreenX = ::GetDeviceCaps(hdc, LOGPIXELSX); | |
166 | logPPIScreenY = ::GetDeviceCaps(hdc, LOGPIXELSY); | |
167 | ::ReleaseDC(NULL, hdc); | |
168 | ||
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 | } | |
176 | ||
177 | printout->SetPPIScreen(logPPIScreenX, logPPIScreenY); | |
178 | printout->SetPPIPrinter(logPPIPrinterX, logPPIPrinterY); | |
179 | ||
180 | // Set printout parameters | |
181 | printout->SetDC(dc); | |
182 | ||
183 | int w, h; | |
184 | dc->GetSize(&w, &h); | |
185 | printout->SetPageSizePixels((int)w, (int)h); | |
186 | ||
187 | dc->GetSizeMM(&w, &h); | |
188 | printout->SetPageSizeMM((int)w, (int)h); | |
189 | ||
190 | // Create an abort window | |
191 | wxBeginBusyCursor(); | |
192 | ||
193 | printout->OnPreparePrinting(); | |
194 | ||
195 | wxWindow *win = CreateAbortWindow(parent, printout); | |
196 | wxYield(); | |
197 | ||
198 | #if defined(__BORLANDC__) || defined(__GNUWIN32__) || defined(__SALFORDC__) || !defined(__WIN32__) | |
199 | #ifdef STRICT | |
200 | ::SetAbortProc((HDC) dc->GetHDC(), (ABORTPROC) m_lpAbortProc); | |
201 | #else | |
202 | ::SetAbortProc((HDC) dc->GetHDC(), (FARPROC) m_lpAbortProc); | |
203 | #endif | |
204 | #else | |
205 | ::SetAbortProc((HDC) dc->GetHDC(), (int (_stdcall *) | |
206 | // cast it to right type only if required | |
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 | |
209 | #ifdef STRICT | |
210 | (HDC, int) | |
211 | #else | |
212 | () | |
213 | #endif | |
214 | )m_lpAbortProc); | |
215 | #endif | |
216 | ||
217 | if (!win) | |
218 | { | |
219 | wxEndBusyCursor(); | |
220 | wxLogDebug(_T("Could not create an abort dialog.")); | |
221 | ||
222 | delete dc; | |
223 | } | |
224 | sm_abortWindow = win; | |
225 | sm_abortWindow->Show(TRUE); | |
226 | wxSafeYield(); | |
227 | ||
228 | printout->OnBeginPrinting(); | |
229 | ||
230 | bool keepGoing = TRUE; | |
231 | ||
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(); | |
238 | wxLogError(_("Could not start printing.")); | |
239 | break; | |
240 | } | |
241 | if (sm_abortIt) | |
242 | break; | |
243 | ||
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 | } | |
262 | ||
263 | printout->OnEndPrinting(); | |
264 | ||
265 | if (sm_abortWindow) | |
266 | { | |
267 | sm_abortWindow->Show(FALSE); | |
268 | delete sm_abortWindow; | |
269 | sm_abortWindow = NULL; | |
270 | } | |
271 | ||
272 | wxEndBusyCursor(); | |
273 | ||
274 | delete dc; | |
275 | ||
276 | return TRUE; | |
277 | } | |
278 | ||
279 | wxDC* wxWindowsPrinter::PrintDialog(wxWindow *parent) | |
280 | { | |
281 | wxDC* dc = (wxDC*) NULL; | |
282 | ||
283 | wxPrintDialog dialog(parent, & m_printDialogData); | |
284 | int ret = dialog.ShowModal(); | |
285 | ||
286 | if (ret == wxID_OK) | |
287 | { | |
288 | dc = dialog.GetPrintDC(); | |
289 | m_printDialogData = dialog.GetPrintDialogData(); | |
290 | } | |
291 | ||
292 | return dc; | |
293 | } | |
294 | ||
295 | bool wxWindowsPrinter::Setup(wxWindow *parent) | |
296 | { | |
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); | |
308 | } | |
309 | ||
310 | /* | |
311 | * Print preview | |
312 | */ | |
313 | ||
314 | wxWindowsPrintPreview::wxWindowsPrintPreview(wxPrintout *printout, | |
315 | wxPrintout *printoutForPrinting, | |
316 | wxPrintDialogData *data) | |
317 | : wxPrintPreviewBase(printout, printoutForPrinting, data) | |
318 | { | |
319 | DetermineScaling(); | |
320 | } | |
321 | ||
322 | wxWindowsPrintPreview::wxWindowsPrintPreview(wxPrintout *printout, | |
323 | wxPrintout *printoutForPrinting, | |
324 | wxPrintData *data) | |
325 | : wxPrintPreviewBase(printout, printoutForPrinting, data) | |
326 | { | |
327 | DetermineScaling(); | |
328 | } | |
329 | ||
330 | wxWindowsPrintPreview::~wxWindowsPrintPreview() | |
331 | { | |
332 | } | |
333 | ||
334 | bool wxWindowsPrintPreview::Print(bool interactive) | |
335 | { | |
336 | if (!m_printPrintout) | |
337 | return FALSE; | |
338 | wxWindowsPrinter printer(&m_printDialogData); | |
339 | return printer.Print(m_previewFrame, m_printPrintout, interactive); | |
340 | } | |
341 | ||
342 | void wxWindowsPrintPreview::DetermineScaling() | |
343 | { | |
344 | HDC dc = ::GetDC(NULL); | |
345 | int screenWidth = ::GetDeviceCaps(dc, HORZSIZE); | |
346 | // int screenHeight = ::GetDeviceCaps(dc, VERTSIZE); | |
347 | int screenXRes = ::GetDeviceCaps(dc, HORZRES); | |
348 | // int screenYRes = ::GetDeviceCaps(dc, VERTRES); | |
349 | int logPPIScreenX = ::GetDeviceCaps(dc, LOGPIXELSX); | |
350 | int logPPIScreenY = ::GetDeviceCaps(dc, LOGPIXELSY); | |
351 | m_previewPrintout->SetPPIScreen(logPPIScreenX, logPPIScreenY); | |
352 | ||
353 | ::ReleaseDC(NULL, dc); | |
354 | ||
355 | // Get a device context for the currently selected printer | |
356 | wxPrinterDC printerDC(m_printDialogData.GetPrintData()); | |
357 | ||
358 | int printerWidth = 150; | |
359 | int printerHeight = 250; | |
360 | int printerXRes = 1500; | |
361 | int printerYRes = 2500; | |
362 | ||
363 | if (printerDC.GetHDC()) | |
364 | { | |
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); | |
369 | ||
370 | int logPPIPrinterX = ::GetDeviceCaps((HDC) printerDC.GetHDC(), LOGPIXELSX); | |
371 | int logPPIPrinterY = ::GetDeviceCaps((HDC) printerDC.GetHDC(), LOGPIXELSY); | |
372 | ||
373 | m_previewPrintout->SetPPIPrinter(logPPIPrinterX, logPPIPrinterY); | |
374 | m_previewPrintout->SetPageSizeMM(printerWidth, printerHeight); | |
375 | ||
376 | if (logPPIPrinterX == 0 || logPPIPrinterY == 0 || printerWidth == 0 || printerHeight == 0) | |
377 | m_isOk = FALSE; | |
378 | } | |
379 | else | |
380 | m_isOk = FALSE; | |
381 | ||
382 | m_pageWidth = printerXRes; | |
383 | m_pageHeight = printerYRes; | |
384 | ||
385 | // At 100%, the page should look about page-size on the screen. | |
386 | m_previewScale = (float)((float)screenWidth/(float)printerWidth); | |
387 | m_previewScale = m_previewScale * (float)((float)screenXRes/(float)printerYRes); | |
388 | } | |
389 | ||
390 | /**************************************************************************** | |
391 | ||
392 | FUNCTION: wxAbortProc() | |
393 | ||
394 | PURPOSE: Processes messages for the Abort Dialog box | |
395 | ||
396 | ****************************************************************************/ | |
397 | ||
398 | LONG APIENTRY _EXPORT wxAbortProc(HDC WXUNUSED(hPr), int WXUNUSED(Code)) | |
399 | { | |
400 | MSG msg; | |
401 | ||
402 | if (!wxPrinterBase::sm_abortWindow) /* If the abort dialog isn't up yet */ | |
403 | return(TRUE); | |
404 | ||
405 | /* Process messages intended for the abort dialog box */ | |
406 | ||
407 | while (!wxPrinterBase::sm_abortIt && PeekMessage(&msg, 0, 0, 0, TRUE)) | |
408 | if (!IsDialogMessage((HWND) wxPrinterBase::sm_abortWindow->GetHWND(), &msg)) { | |
409 | TranslateMessage(&msg); | |
410 | DispatchMessage(&msg); | |
411 | } | |
412 | ||
413 | /* bAbort is TRUE (return is FALSE) if the user has aborted */ | |
414 | ||
415 | return (!wxPrinterBase::sm_abortIt); | |
416 | } | |
417 |