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