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