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