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