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