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