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