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