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