]> git.saurik.com Git - wxWidgets.git/blame - src/msw/printwin.cpp
Had missed one LPSTR.
[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{
34da0970 90 FreeProcInstance((FARPROC) m_lpAbortProc);
2bda0e17
KB
91}
92
93bool wxWindowsPrinter::Print(wxWindow *parent, wxPrintout *printout, bool prompt)
94{
7bcb11d3
JS
95 sm_abortIt = FALSE;
96 sm_abortWindow = NULL;
103aec29 97
7bcb11d3
JS
98 if (!printout)
99 return FALSE;
103aec29 100
7bcb11d3
JS
101 printout->SetIsPreview(FALSE);
102 printout->OnPreparePrinting();
103aec29 103
7bcb11d3
JS
104 // Get some parameters from the printout, if defined
105 int fromPage, toPage;
106 int minPage, maxPage;
107 printout->GetPageInfo(&minPage, &maxPage, &fromPage, &toPage);
103aec29 108
7bcb11d3
JS
109 if (maxPage == 0)
110 return FALSE;
103aec29 111
7bcb11d3
JS
112 m_printDialogData.SetMinPage(minPage);
113 m_printDialogData.SetMaxPage(maxPage);
114 if (fromPage != 0)
115 m_printDialogData.SetFromPage(fromPage);
116 if (toPage != 0)
117 m_printDialogData.SetToPage(toPage);
103aec29 118
7bcb11d3
JS
119 if (minPage != 0)
120 {
121 m_printDialogData.EnablePageNumbers(TRUE);
122 if (m_printDialogData.GetFromPage() < m_printDialogData.GetMinPage())
123 m_printDialogData.SetFromPage(m_printDialogData.GetMinPage());
124 else if (m_printDialogData.GetFromPage() > m_printDialogData.GetMaxPage())
125 m_printDialogData.SetFromPage(m_printDialogData.GetMaxPage());
126 if (m_printDialogData.GetToPage() > m_printDialogData.GetMaxPage())
127 m_printDialogData.SetToPage(m_printDialogData.GetMaxPage());
128 else if (m_printDialogData.GetToPage() < m_printDialogData.GetMinPage())
129 m_printDialogData.SetToPage(m_printDialogData.GetMinPage());
130 }
131 else
132 m_printDialogData.EnablePageNumbers(FALSE);
2bda0e17 133
103aec29 134 // Create a suitable device context
7bcb11d3
JS
135 wxDC *dc = NULL;
136 if (prompt)
137 {
138 dc = PrintDialog(parent);
139 if (!dc)
140 return FALSE;
141 }
142 else
143 {
144 // dc = new wxPrinterDC("", "", "", FALSE, m_printData.GetOrientation());
145 dc = new wxPrinterDC(m_printDialogData.GetPrintData());
146 }
103aec29 147
7bcb11d3
JS
148 // May have pressed cancel.
149 if (!dc || !dc->Ok())
150 {
151 if (dc) delete dc;
152 return FALSE;
153 }
103aec29 154
7bcb11d3
JS
155 int logPPIScreenX = 0;
156 int logPPIScreenY = 0;
157 int logPPIPrinterX = 0;
158 int logPPIPrinterY = 0;
103aec29 159
7bcb11d3
JS
160 HDC hdc = ::GetDC(NULL);
161 logPPIScreenX = ::GetDeviceCaps(hdc, LOGPIXELSX);
162 logPPIScreenY = ::GetDeviceCaps(hdc, LOGPIXELSY);
163 ::ReleaseDC(NULL, hdc);
103aec29 164
7bcb11d3
JS
165 logPPIPrinterX = ::GetDeviceCaps((HDC) dc->GetHDC(), LOGPIXELSX);
166 logPPIPrinterY = ::GetDeviceCaps((HDC) dc->GetHDC(), LOGPIXELSY);
167 if (logPPIPrinterX == 0 || logPPIPrinterY == 0)
168 {
169 delete dc;
170 return FALSE;
171 }
103aec29 172
7bcb11d3
JS
173 printout->SetPPIScreen(logPPIScreenX, logPPIScreenY);
174 printout->SetPPIPrinter(logPPIPrinterX, logPPIPrinterY);
103aec29
VZ
175
176 // Set printout parameters
7bcb11d3 177 printout->SetDC(dc);
103aec29 178
7bcb11d3
JS
179 int w, h;
180 dc->GetSize(&w, &h);
181 printout->SetPageSizePixels((int)w, (int)h);
d6a1743b 182
7bcb11d3
JS
183 dc->GetSizeMM(&w, &h);
184 printout->SetPageSizeMM((int)w, (int)h);
103aec29 185
7bcb11d3
JS
186 // Create an abort window
187 wxBeginBusyCursor();
103aec29 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();
837e5743 214 wxLogDebug(_T("Could not create an abort dialog."));
103aec29 215
7bcb11d3 216 delete dc;
2bda0e17 217 }
7bcb11d3
JS
218 sm_abortWindow = win;
219 sm_abortWindow->Show(TRUE);
103aec29
VZ
220 wxSafeYield();
221
7bcb11d3 222 printout->OnBeginPrinting();
103aec29 223
7bcb11d3 224 bool keepGoing = TRUE;
103aec29 225
7bcb11d3
JS
226 int copyCount;
227 for (copyCount = 1; copyCount <= m_printDialogData.GetNoCopies(); copyCount ++)
228 {
229 if (!printout->OnBeginDocument(m_printDialogData.GetFromPage(), m_printDialogData.GetToPage()))
230 {
231 wxEndBusyCursor();
103aec29 232 wxLogError(_("Could not start printing."));
7bcb11d3
JS
233 break;
234 }
235 if (sm_abortIt)
236 break;
103aec29 237
7bcb11d3
JS
238 int pn;
239 for (pn = m_printDialogData.GetFromPage(); keepGoing && (pn <= m_printDialogData.GetToPage()) && printout->HasPage(pn);
240 pn++)
241 {
242 if (sm_abortIt)
243 {
244 keepGoing = FALSE;
245 break;
246 }
247 else
248 {
249 dc->StartPage();
250 printout->OnPrintPage(pn);
251 dc->EndPage();
252 }
253 }
254 printout->OnEndDocument();
255 }
103aec29 256
7bcb11d3 257 printout->OnEndPrinting();
103aec29 258
7bcb11d3 259 if (sm_abortWindow)
2bda0e17 260 {
7bcb11d3
JS
261 sm_abortWindow->Show(FALSE);
262 delete sm_abortWindow;
263 sm_abortWindow = NULL;
2bda0e17 264 }
103aec29 265
7bcb11d3 266 wxEndBusyCursor();
103aec29 267
7bcb11d3 268 delete dc;
103aec29 269
7bcb11d3
JS
270 return TRUE;
271}
2bda0e17 272
7bcb11d3
JS
273wxDC* wxWindowsPrinter::PrintDialog(wxWindow *parent)
274{
275 wxDC* dc = (wxDC*) NULL;
2bda0e17 276
7bcb11d3
JS
277 wxPrintDialog dialog(parent, & m_printDialogData);
278 int ret = dialog.ShowModal();
2bda0e17 279
7bcb11d3
JS
280 if (ret == wxID_OK)
281 {
282 dc = dialog.GetPrintDC();
283 m_printDialogData = dialog.GetPrintDialogData();
284 }
2bda0e17 285
7bcb11d3 286 return dc;
2bda0e17
KB
287}
288
289bool wxWindowsPrinter::Setup(wxWindow *parent)
290{
7bcb11d3
JS
291 wxPrintDialog dialog(parent, & m_printDialogData);
292 dialog.GetPrintDialogData().SetSetupDialog(TRUE);
293
294 int ret = dialog.ShowModal();
295
296 if (ret == wxID_OK)
297 {
298 m_printDialogData = dialog.GetPrintDialogData();
299 }
300
301 return (ret == wxID_OK);
2bda0e17
KB
302}
303
304/*
7bcb11d3
JS
305* Print preview
306*/
2bda0e17 307
103aec29
VZ
308wxWindowsPrintPreview::wxWindowsPrintPreview(wxPrintout *printout,
309 wxPrintout *printoutForPrinting,
310 wxPrintDialogData *data)
311 : wxPrintPreviewBase(printout, printoutForPrinting, data)
2bda0e17 312{
7bcb11d3 313 DetermineScaling();
2bda0e17
KB
314}
315
103aec29
VZ
316wxWindowsPrintPreview::wxWindowsPrintPreview(wxPrintout *printout,
317 wxPrintout *printoutForPrinting,
318 wxPrintData *data)
319 : wxPrintPreviewBase(printout, printoutForPrinting, data)
320{
321 DetermineScaling();
322}
323
324wxWindowsPrintPreview::~wxWindowsPrintPreview()
2bda0e17
KB
325{
326}
327
328bool wxWindowsPrintPreview::Print(bool interactive)
329{
7bcb11d3
JS
330 if (!m_printPrintout)
331 return FALSE;
332 wxWindowsPrinter printer(&m_printDialogData);
333 return printer.Print(m_previewFrame, m_printPrintout, interactive);
2bda0e17
KB
334}
335
103aec29 336void wxWindowsPrintPreview::DetermineScaling()
2bda0e17
KB
337{
338 HDC dc = ::GetDC(NULL);
339 int screenWidth = ::GetDeviceCaps(dc, HORZSIZE);
7bcb11d3 340 // int screenHeight = ::GetDeviceCaps(dc, VERTSIZE);
2bda0e17 341 int screenXRes = ::GetDeviceCaps(dc, HORZRES);
7bcb11d3 342 // int screenYRes = ::GetDeviceCaps(dc, VERTRES);
2bda0e17
KB
343 int logPPIScreenX = ::GetDeviceCaps(dc, LOGPIXELSX);
344 int logPPIScreenY = ::GetDeviceCaps(dc, LOGPIXELSY);
34da0970 345 m_previewPrintout->SetPPIScreen(logPPIScreenX, logPPIScreenY);
103aec29 346
2bda0e17 347 ::ReleaseDC(NULL, dc);
103aec29 348
2bda0e17 349 // Get a device context for the currently selected printer
7bcb11d3 350 wxPrinterDC printerDC(m_printDialogData.GetPrintData());
103aec29 351
2bda0e17
KB
352 int printerWidth = 150;
353 int printerHeight = 250;
354 int printerXRes = 1500;
355 int printerYRes = 2500;
103aec29 356
2bda0e17
KB
357 if (printerDC.GetHDC())
358 {
7bcb11d3
JS
359 printerWidth = ::GetDeviceCaps((HDC) printerDC.GetHDC(), HORZSIZE);
360 printerHeight = ::GetDeviceCaps((HDC) printerDC.GetHDC(), VERTSIZE);
361 printerXRes = ::GetDeviceCaps((HDC) printerDC.GetHDC(), HORZRES);
362 printerYRes = ::GetDeviceCaps((HDC) printerDC.GetHDC(), VERTRES);
103aec29 363
7bcb11d3
JS
364 int logPPIPrinterX = ::GetDeviceCaps((HDC) printerDC.GetHDC(), LOGPIXELSX);
365 int logPPIPrinterY = ::GetDeviceCaps((HDC) printerDC.GetHDC(), LOGPIXELSY);
103aec29 366
7bcb11d3
JS
367 m_previewPrintout->SetPPIPrinter(logPPIPrinterX, logPPIPrinterY);
368 m_previewPrintout->SetPageSizeMM(printerWidth, printerHeight);
103aec29 369
7bcb11d3
JS
370 if (logPPIPrinterX == 0 || logPPIPrinterY == 0 || printerWidth == 0 || printerHeight == 0)
371 m_isOk = FALSE;
2bda0e17
KB
372 }
373 else
7bcb11d3 374 m_isOk = FALSE;
103aec29 375
34da0970
JS
376 m_pageWidth = printerXRes;
377 m_pageHeight = printerYRes;
103aec29 378
2bda0e17 379 // At 100%, the page should look about page-size on the screen.
34da0970
JS
380 m_previewScale = (float)((float)screenWidth/(float)printerWidth);
381 m_previewScale = m_previewScale * (float)((float)screenXRes/(float)printerYRes);
2bda0e17
KB
382}
383
384/****************************************************************************
385
7bcb11d3 386 FUNCTION: wxAbortProc()
103aec29 387
2bda0e17 388 PURPOSE: Processes messages for the Abort Dialog box
103aec29 389
2bda0e17
KB
390****************************************************************************/
391
392LONG APIENTRY _EXPORT wxAbortProc(HDC WXUNUSED(hPr), int WXUNUSED(Code))
393{
394 MSG msg;
103aec29 395
34da0970 396 if (!wxPrinterBase::sm_abortWindow) /* If the abort dialog isn't up yet */
2bda0e17 397 return(TRUE);
103aec29 398
2bda0e17 399 /* Process messages intended for the abort dialog box */
103aec29 400
34da0970
JS
401 while (!wxPrinterBase::sm_abortIt && PeekMessage(&msg, 0, 0, 0, TRUE))
402 if (!IsDialogMessage((HWND) wxPrinterBase::sm_abortWindow->GetHWND(), &msg)) {
2bda0e17
KB
403 TranslateMessage(&msg);
404 DispatchMessage(&msg);
405 }
103aec29 406
7bcb11d3 407 /* bAbort is TRUE (return is FALSE) if the user has aborted */
103aec29 408
7bcb11d3 409 return (!wxPrinterBase::sm_abortIt);
2bda0e17
KB
410}
411