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