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