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