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