]> git.saurik.com Git - wxWidgets.git/blame - src/msw/printwin.cpp
Better scrolling to cursor.
[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
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
JS
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();
103aec29 114
7bcb11d3
JS
115 // Get some parameters from the printout, if defined
116 int fromPage, toPage;
117 int minPage, maxPage;
118 printout->GetPageInfo(&minPage, &maxPage, &fromPage, &toPage);
103aec29 119
7bcb11d3 120 if (maxPage == 0)
f6bcfd97
BP
121 {
122 sm_lastError = wxPRINTER_ERROR;
7bcb11d3 123 return FALSE;
f6bcfd97 124 }
103aec29 125
7bcb11d3
JS
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);
103aec29 132
7bcb11d3
JS
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);
2bda0e17 147
103aec29 148 // Create a suitable device context
7bcb11d3
JS
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 }
103aec29 161
7bcb11d3
JS
162 // May have pressed cancel.
163 if (!dc || !dc->Ok())
164 {
165 if (dc) delete dc;
166 return FALSE;
167 }
103aec29 168
7bcb11d3
JS
169 int logPPIScreenX = 0;
170 int logPPIScreenY = 0;
171 int logPPIPrinterX = 0;
172 int logPPIPrinterY = 0;
103aec29 173
7bcb11d3
JS
174 HDC hdc = ::GetDC(NULL);
175 logPPIScreenX = ::GetDeviceCaps(hdc, LOGPIXELSX);
176 logPPIScreenY = ::GetDeviceCaps(hdc, LOGPIXELSY);
177 ::ReleaseDC(NULL, hdc);
103aec29 178
7bcb11d3
JS
179 logPPIPrinterX = ::GetDeviceCaps((HDC) dc->GetHDC(), LOGPIXELSX);
180 logPPIPrinterY = ::GetDeviceCaps((HDC) dc->GetHDC(), LOGPIXELSY);
181 if (logPPIPrinterX == 0 || logPPIPrinterY == 0)
182 {
183 delete dc;
f6bcfd97 184 sm_lastError = wxPRINTER_ERROR;
7bcb11d3
JS
185 return FALSE;
186 }
103aec29 187
7bcb11d3
JS
188 printout->SetPPIScreen(logPPIScreenX, logPPIScreenY);
189 printout->SetPPIPrinter(logPPIPrinterX, logPPIPrinterY);
103aec29
VZ
190
191 // Set printout parameters
7bcb11d3 192 printout->SetDC(dc);
103aec29 193
7bcb11d3
JS
194 int w, h;
195 dc->GetSize(&w, &h);
196 printout->SetPageSizePixels((int)w, (int)h);
d6a1743b 197
7bcb11d3
JS
198 dc->GetSizeMM(&w, &h);
199 printout->SetPageSizeMM((int)w, (int)h);
103aec29 200
7bcb11d3
JS
201 // Create an abort window
202 wxBeginBusyCursor();
103aec29 203
1044a386
JS
204 printout->OnPreparePrinting();
205
7bcb11d3
JS
206 wxWindow *win = CreateAbortWindow(parent, printout);
207 wxYield();
103aec29 208
ce3ed50d 209#if defined(__BORLANDC__) || defined(__GNUWIN32__) || defined(__SALFORDC__) || !defined(__WIN32__)
27a9bd48
PA
210#ifdef STRICT
211 ::SetAbortProc((HDC) dc->GetHDC(), (ABORTPROC) m_lpAbortProc);
212#else
7bcb11d3 213 ::SetAbortProc((HDC) dc->GetHDC(), (FARPROC) m_lpAbortProc);
27a9bd48 214#endif
7bcb11d3
JS
215#else
216 ::SetAbortProc((HDC) dc->GetHDC(), (int (_stdcall *)
217 // cast it to right type only if required
103aec29
VZ
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
7bcb11d3
JS
220#ifdef STRICT
221 (HDC, int)
d6a1743b 222#else
7bcb11d3 223 ()
d6a1743b 224#endif
7bcb11d3
JS
225 )m_lpAbortProc);
226#endif
103aec29 227
7bcb11d3 228 if (!win)
2bda0e17 229 {
7bcb11d3 230 wxEndBusyCursor();
223d09f6 231 wxLogDebug(wxT("Could not create an abort dialog."));
f6bcfd97 232 sm_lastError = wxPRINTER_ERROR;
103aec29 233
7bcb11d3 234 delete dc;
2bda0e17 235 }
7bcb11d3
JS
236 sm_abortWindow = win;
237 sm_abortWindow->Show(TRUE);
103aec29
VZ
238 wxSafeYield();
239
7bcb11d3 240 printout->OnBeginPrinting();
103aec29 241
f6bcfd97
BP
242 sm_lastError = wxPRINTER_NO_ERROR;
243
7bcb11d3 244 int copyCount;
70846f0a
VZ
245 for ( copyCount = 1;
246 copyCount <= m_printDialogData.GetNoCopies();
247 copyCount++ )
7bcb11d3
JS
248 {
249 if (!printout->OnBeginDocument(m_printDialogData.GetFromPage(), m_printDialogData.GetToPage()))
250 {
251 wxEndBusyCursor();
103aec29 252 wxLogError(_("Could not start printing."));
f6bcfd97 253 sm_lastError = wxPRINTER_ERROR;
7bcb11d3
JS
254 break;
255 }
256 if (sm_abortIt)
f6bcfd97
BP
257 {
258 sm_lastError = wxPRINTER_CANCELLED;
7bcb11d3 259 break;
f6bcfd97 260 }
103aec29 261
7bcb11d3 262 int pn;
70846f0a
VZ
263 for ( pn = m_printDialogData.GetFromPage();
264 pn <= m_printDialogData.GetToPage() && printout->HasPage(pn);
265 pn++ )
7bcb11d3 266 {
70846f0a 267 if ( sm_abortIt )
7bcb11d3 268 {
f6bcfd97 269 sm_lastError = wxPRINTER_CANCELLED;
7bcb11d3
JS
270 break;
271 }
70846f0a
VZ
272
273 dc->StartPage();
274 bool cont = printout->OnPrintPage(pn);
275 dc->EndPage();
276
277 if ( !cont )
f6bcfd97
BP
278 {
279 sm_lastError = wxPRINTER_CANCELLED;
70846f0a 280 break;
f6bcfd97 281 }
7bcb11d3 282 }
70846f0a 283
7bcb11d3
JS
284 printout->OnEndDocument();
285 }
103aec29 286
7bcb11d3 287 printout->OnEndPrinting();
103aec29 288
7bcb11d3 289 if (sm_abortWindow)
2bda0e17 290 {
7bcb11d3
JS
291 sm_abortWindow->Show(FALSE);
292 delete sm_abortWindow;
293 sm_abortWindow = NULL;
2bda0e17 294 }
103aec29 295
7bcb11d3 296 wxEndBusyCursor();
103aec29 297
7bcb11d3 298 delete dc;
103aec29 299
f6bcfd97 300 return (sm_lastError == wxPRINTER_NO_ERROR);
7bcb11d3 301}
2bda0e17 302
7bcb11d3
JS
303wxDC* wxWindowsPrinter::PrintDialog(wxWindow *parent)
304{
305 wxDC* dc = (wxDC*) NULL;
2bda0e17 306
7bcb11d3
JS
307 wxPrintDialog dialog(parent, & m_printDialogData);
308 int ret = dialog.ShowModal();
2bda0e17 309
7bcb11d3
JS
310 if (ret == wxID_OK)
311 {
312 dc = dialog.GetPrintDC();
313 m_printDialogData = dialog.GetPrintDialogData();
33ac7e6f 314 if (dc == NULL)
f6bcfd97
BP
315 sm_lastError = wxPRINTER_ERROR;
316 else
317 sm_lastError = wxPRINTER_NO_ERROR;
7bcb11d3 318 }
f6bcfd97
BP
319 else
320 sm_lastError = wxPRINTER_CANCELLED;
2bda0e17 321
7bcb11d3 322 return dc;
2bda0e17
KB
323}
324
325bool wxWindowsPrinter::Setup(wxWindow *parent)
326{
7bcb11d3
JS
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);
2bda0e17
KB
338}
339
340/*
7bcb11d3
JS
341* Print preview
342*/
2bda0e17 343
103aec29
VZ
344wxWindowsPrintPreview::wxWindowsPrintPreview(wxPrintout *printout,
345 wxPrintout *printoutForPrinting,
346 wxPrintDialogData *data)
347 : wxPrintPreviewBase(printout, printoutForPrinting, data)
2bda0e17 348{
7bcb11d3 349 DetermineScaling();
2bda0e17
KB
350}
351
103aec29
VZ
352wxWindowsPrintPreview::wxWindowsPrintPreview(wxPrintout *printout,
353 wxPrintout *printoutForPrinting,
354 wxPrintData *data)
355 : wxPrintPreviewBase(printout, printoutForPrinting, data)
356{
357 DetermineScaling();
358}
359
360wxWindowsPrintPreview::~wxWindowsPrintPreview()
2bda0e17
KB
361{
362}
363
364bool wxWindowsPrintPreview::Print(bool interactive)
365{
7bcb11d3
JS
366 if (!m_printPrintout)
367 return FALSE;
368 wxWindowsPrinter printer(&m_printDialogData);
369 return printer.Print(m_previewFrame, m_printPrintout, interactive);
2bda0e17
KB
370}
371
103aec29 372void wxWindowsPrintPreview::DetermineScaling()
2bda0e17
KB
373{
374 HDC dc = ::GetDC(NULL);
375 int screenWidth = ::GetDeviceCaps(dc, HORZSIZE);
f4fefc23 376 int screenYRes = ::GetDeviceCaps(dc, VERTRES);
2bda0e17
KB
377 int logPPIScreenX = ::GetDeviceCaps(dc, LOGPIXELSX);
378 int logPPIScreenY = ::GetDeviceCaps(dc, LOGPIXELSY);
34da0970 379 m_previewPrintout->SetPPIScreen(logPPIScreenX, logPPIScreenY);
103aec29 380
2bda0e17 381 ::ReleaseDC(NULL, dc);
103aec29 382
2bda0e17 383 // Get a device context for the currently selected printer
7bcb11d3 384 wxPrinterDC printerDC(m_printDialogData.GetPrintData());
103aec29 385
2bda0e17
KB
386 int printerWidth = 150;
387 int printerHeight = 250;
388 int printerXRes = 1500;
389 int printerYRes = 2500;
103aec29 390
f4fefc23
VZ
391 dc = GetHdcOf(printerDC);
392 if ( dc )
2bda0e17 393 {
f4fefc23
VZ
394 printerWidth = ::GetDeviceCaps(dc, HORZSIZE);
395 printerHeight = ::GetDeviceCaps(dc, VERTSIZE);
396 printerXRes = ::GetDeviceCaps(dc, HORZRES);
397 printerYRes = ::GetDeviceCaps(dc, VERTRES);
103aec29 398
f4fefc23
VZ
399 int logPPIPrinterX = ::GetDeviceCaps(dc, LOGPIXELSX);
400 int logPPIPrinterY = ::GetDeviceCaps(dc, LOGPIXELSY);
103aec29 401
7bcb11d3
JS
402 m_previewPrintout->SetPPIPrinter(logPPIPrinterX, logPPIPrinterY);
403 m_previewPrintout->SetPageSizeMM(printerWidth, printerHeight);
103aec29 404
7bcb11d3
JS
405 if (logPPIPrinterX == 0 || logPPIPrinterY == 0 || printerWidth == 0 || printerHeight == 0)
406 m_isOk = FALSE;
2bda0e17
KB
407 }
408 else
7bcb11d3 409 m_isOk = FALSE;
103aec29 410
34da0970
JS
411 m_pageWidth = printerXRes;
412 m_pageHeight = printerYRes;
103aec29 413
2bda0e17 414 // At 100%, the page should look about page-size on the screen.
34da0970 415 m_previewScale = (float)((float)screenWidth/(float)printerWidth);
f4fefc23 416 m_previewScale = m_previewScale * (float)((float)screenYRes/(float)printerYRes);
2bda0e17
KB
417}
418
419/****************************************************************************
420
7bcb11d3 421 FUNCTION: wxAbortProc()
103aec29 422
2bda0e17 423 PURPOSE: Processes messages for the Abort Dialog box
103aec29 424
2bda0e17
KB
425****************************************************************************/
426
427LONG APIENTRY _EXPORT wxAbortProc(HDC WXUNUSED(hPr), int WXUNUSED(Code))
428{
429 MSG msg;
103aec29 430
34da0970 431 if (!wxPrinterBase::sm_abortWindow) /* If the abort dialog isn't up yet */
2bda0e17 432 return(TRUE);
103aec29 433
2bda0e17 434 /* Process messages intended for the abort dialog box */
103aec29 435
34da0970
JS
436 while (!wxPrinterBase::sm_abortIt && PeekMessage(&msg, 0, 0, 0, TRUE))
437 if (!IsDialogMessage((HWND) wxPrinterBase::sm_abortWindow->GetHWND(), &msg)) {
2bda0e17
KB
438 TranslateMessage(&msg);
439 DispatchMessage(&msg);
440 }
103aec29 441
7bcb11d3 442 /* bAbort is TRUE (return is FALSE) if the user has aborted */
103aec29 443
7bcb11d3 444 return (!wxPrinterBase::sm_abortIt);
2bda0e17
KB
445}
446
f6bcfd97
BP
447#endif
448 // wxUSE_PRINTING_ARCHITECTURE