]>
Commit | Line | Data |
---|---|---|
c801d85f | 1 | ///////////////////////////////////////////////////////////////////////////// |
cb7d7375 | 2 | // Name: src/common/prntbase.cpp |
c801d85f KB |
3 | // Purpose: Printing framework base class implementation |
4 | // Author: Julian Smart | |
5 | // Modified by: | |
6 | // Created: 04/01/98 | |
7 | // RCS-ID: $Id$ | |
55d99c7a | 8 | // Copyright: (c) Julian Smart |
65571936 | 9 | // Licence: wxWindows licence |
c801d85f KB |
10 | ///////////////////////////////////////////////////////////////////////////// |
11 | ||
c801d85f KB |
12 | // For compilers that support precompilation, includes "wx.h". |
13 | #include "wx/wxprec.h" | |
14 | ||
15 | #ifdef __BORLANDC__ | |
8898456d | 16 | #pragma hdrstop |
c801d85f KB |
17 | #endif |
18 | ||
d427503c VZ |
19 | #if wxUSE_PRINTING_ARCHITECTURE |
20 | ||
6d50343d WS |
21 | #include "wx/dcprint.h" |
22 | ||
c801d85f | 23 | #ifndef WX_PRECOMP |
57bd4c60 WS |
24 | #if defined(__WXMSW__) |
25 | #include "wx/msw/wrapcdlg.h" | |
26 | #endif // MSW | |
8898456d WS |
27 | #include "wx/utils.h" |
28 | #include "wx/dc.h" | |
29 | #include "wx/app.h" | |
5a70d3f5 | 30 | #include "wx/math.h" |
8898456d WS |
31 | #include "wx/msgdlg.h" |
32 | #include "wx/layout.h" | |
33 | #include "wx/choice.h" | |
34 | #include "wx/button.h" | |
35 | #include "wx/settings.h" | |
36 | #include "wx/dcmemory.h" | |
23205be8 | 37 | #include "wx/dcclient.h" |
8898456d WS |
38 | #include "wx/stattext.h" |
39 | #include "wx/intl.h" | |
40 | #include "wx/textdlg.h" | |
41 | #include "wx/sizer.h" | |
02761f6c | 42 | #include "wx/module.h" |
2b5f62a0 | 43 | #endif // !WX_PRECOMP |
c801d85f KB |
44 | |
45 | #include "wx/prntbase.h" | |
c801d85f | 46 | #include "wx/printdlg.h" |
e81e3883 | 47 | #include "wx/print.h" |
147bf263 | 48 | #include "wx/dcprint.h" |
c801d85f KB |
49 | |
50 | #include <stdlib.h> | |
51 | #include <string.h> | |
52 | ||
891daf8c RR |
53 | #if defined(__WXMSW__) && !defined(__WXUNIVERSAL__) |
54 | #include "wx/msw/printdlg.h" | |
888dde65 | 55 | #include "wx/msw/dcprint.h" |
891daf8c | 56 | #elif defined(__WXMAC__) |
c933e267 SC |
57 | #include "wx/osx/printdlg.h" |
58 | #include "wx/osx/private/print.h" | |
59 | #include "wx/osx/dcprint.h" | |
7c71eb6a SN |
60 | #elif defined(__WXPM__) |
61 | #include "wx/os2/dcprint.h" | |
62 | #include "wx/generic/prntdlgg.h" | |
891daf8c RR |
63 | #else |
64 | #include "wx/generic/prntdlgg.h" | |
147bf263 | 65 | #include "wx/dcps.h" |
891daf8c RR |
66 | #endif |
67 | ||
2049ba38 | 68 | #ifdef __WXMSW__ |
d427503c VZ |
69 | #ifndef __WIN32__ |
70 | #include <print.h> | |
71 | #endif | |
72 | #endif // __WXMSW__ | |
c801d85f | 73 | |
e81e3883 RR |
74 | //---------------------------------------------------------------------------- |
75 | // wxPrintFactory | |
76 | //---------------------------------------------------------------------------- | |
77 | ||
78 | wxPrintFactory *wxPrintFactory::m_factory = NULL; | |
383f6abd | 79 | |
e81e3883 | 80 | void wxPrintFactory::SetPrintFactory( wxPrintFactory *factory ) |
383f6abd WS |
81 | { |
82 | if (wxPrintFactory::m_factory) | |
e81e3883 | 83 | delete wxPrintFactory::m_factory; |
383f6abd WS |
84 | |
85 | wxPrintFactory::m_factory = factory; | |
e81e3883 | 86 | } |
c801d85f | 87 | |
e81e3883 RR |
88 | wxPrintFactory *wxPrintFactory::GetFactory() |
89 | { | |
90 | if (!wxPrintFactory::m_factory) | |
383f6abd | 91 | wxPrintFactory::m_factory = new wxNativePrintFactory; |
c801d85f | 92 | |
e81e3883 RR |
93 | return wxPrintFactory::m_factory; |
94 | } | |
95 | ||
96 | //---------------------------------------------------------------------------- | |
97 | // wxNativePrintFactory | |
98 | //---------------------------------------------------------------------------- | |
99 | ||
100 | wxPrinterBase *wxNativePrintFactory::CreatePrinter( wxPrintDialogData *data ) | |
383f6abd WS |
101 | { |
102 | #if defined(__WXMSW__) && !defined(__WXUNIVERSAL__) | |
e81e3883 RR |
103 | return new wxWindowsPrinter( data ); |
104 | #elif defined(__WXMAC__) | |
105 | return new wxMacPrinter( data ); | |
21708c73 SN |
106 | #elif defined(__WXPM__) |
107 | return new wxOS2Printer( data ); | |
e81e3883 RR |
108 | #else |
109 | return new wxPostScriptPrinter( data ); | |
110 | #endif | |
259c43f6 | 111 | } |
e81e3883 | 112 | |
383f6abd | 113 | wxPrintPreviewBase *wxNativePrintFactory::CreatePrintPreview( wxPrintout *preview, |
e81e3883 RR |
114 | wxPrintout *printout, wxPrintDialogData *data ) |
115 | { | |
383f6abd | 116 | #if defined(__WXMSW__) && !defined(__WXUNIVERSAL__) |
e81e3883 RR |
117 | return new wxWindowsPrintPreview( preview, printout, data ); |
118 | #elif defined(__WXMAC__) | |
119 | return new wxMacPrintPreview( preview, printout, data ); | |
21708c73 SN |
120 | #elif defined(__WXPM__) |
121 | return new wxOS2PrintPreview( preview, printout, data ); | |
e81e3883 RR |
122 | #else |
123 | return new wxPostScriptPrintPreview( preview, printout, data ); | |
124 | #endif | |
125 | } | |
126 | ||
383f6abd | 127 | wxPrintPreviewBase *wxNativePrintFactory::CreatePrintPreview( wxPrintout *preview, |
e81e3883 RR |
128 | wxPrintout *printout, wxPrintData *data ) |
129 | { | |
383f6abd | 130 | #if defined(__WXMSW__) && !defined(__WXUNIVERSAL__) |
e81e3883 RR |
131 | return new wxWindowsPrintPreview( preview, printout, data ); |
132 | #elif defined(__WXMAC__) | |
133 | return new wxMacPrintPreview( preview, printout, data ); | |
21708c73 SN |
134 | #elif defined(__WXPM__) |
135 | return new wxOS2PrintPreview( preview, printout, data ); | |
e81e3883 RR |
136 | #else |
137 | return new wxPostScriptPrintPreview( preview, printout, data ); | |
138 | #endif | |
139 | } | |
140 | ||
2997ca30 | 141 | wxPrintDialogBase *wxNativePrintFactory::CreatePrintDialog( wxWindow *parent, |
c061373d RR |
142 | wxPrintDialogData *data ) |
143 | { | |
144 | #if defined(__WXMSW__) && !defined(__WXUNIVERSAL__) | |
145 | return new wxWindowsPrintDialog( parent, data ); | |
146 | #elif defined(__WXMAC__) | |
147 | return new wxMacPrintDialog( parent, data ); | |
148 | #else | |
149 | return new wxGenericPrintDialog( parent, data ); | |
150 | #endif | |
151 | } | |
152 | ||
2997ca30 | 153 | wxPrintDialogBase *wxNativePrintFactory::CreatePrintDialog( wxWindow *parent, |
c061373d RR |
154 | wxPrintData *data ) |
155 | { | |
156 | #if defined(__WXMSW__) && !defined(__WXUNIVERSAL__) | |
157 | return new wxWindowsPrintDialog( parent, data ); | |
158 | #elif defined(__WXMAC__) | |
159 | return new wxMacPrintDialog( parent, data ); | |
160 | #else | |
161 | return new wxGenericPrintDialog( parent, data ); | |
162 | #endif | |
163 | } | |
164 | ||
2997ca30 | 165 | wxPageSetupDialogBase *wxNativePrintFactory::CreatePageSetupDialog( wxWindow *parent, |
891daf8c RR |
166 | wxPageSetupDialogData *data ) |
167 | { | |
168 | #if defined(__WXMSW__) && !defined(__WXUNIVERSAL__) | |
169 | return new wxWindowsPageSetupDialog( parent, data ); | |
170 | #elif defined(__WXMAC__) | |
171 | return new wxMacPageSetupDialog( parent, data ); | |
172 | #else | |
173 | return new wxGenericPageSetupDialog( parent, data ); | |
174 | #endif | |
175 | } | |
176 | ||
6038ec8e RR |
177 | bool wxNativePrintFactory::HasPrintSetupDialog() |
178 | { | |
179 | #if defined(__WXMSW__) && !defined(__WXUNIVERSAL__) | |
180 | return false; | |
181 | #elif defined(__WXMAC__) | |
182 | return false; | |
183 | #else | |
184 | // Only here do we need to provide the print setup | |
185 | // dialog ourselves, the other platforms either have | |
186 | // none, don't make it accessible or let you configure | |
187 | // the printer from the wxPrintDialog anyway. | |
188 | return true; | |
189 | #endif | |
2997ca30 | 190 | |
6038ec8e RR |
191 | } |
192 | ||
2997ca30 | 193 | wxDialog *wxNativePrintFactory::CreatePrintSetupDialog( wxWindow *parent, |
891daf8c | 194 | wxPrintData *data ) |
6038ec8e RR |
195 | { |
196 | #if defined(__WXMSW__) && !defined(__WXUNIVERSAL__) | |
ce403057 WS |
197 | wxUnusedVar(parent); |
198 | wxUnusedVar(data); | |
6038ec8e RR |
199 | return NULL; |
200 | #elif defined(__WXMAC__) | |
ce403057 WS |
201 | wxUnusedVar(parent); |
202 | wxUnusedVar(data); | |
6038ec8e RR |
203 | return NULL; |
204 | #else | |
205 | // Only here do we need to provide the print setup | |
206 | // dialog ourselves, the other platforms either have | |
207 | // none, don't make it accessible or let you configure | |
208 | // the printer from the wxPrintDialog anyway. | |
209 | return new wxGenericPrintSetupDialog( parent, data ); | |
210 | #endif | |
211 | } | |
212 | ||
888dde65 | 213 | wxDCImpl* wxNativePrintFactory::CreatePrinterDCImpl( wxPrinterDC *owner, const wxPrintData& data ) |
147bf263 | 214 | { |
26eef304 | 215 | #if defined(__WXGTK__) || defined(__WXMOTIF__) || ( defined(__WXUNIVERSAL__) && !defined(__WXMAC__) ) |
888dde65 | 216 | return new wxPostScriptDCImpl( owner, data ); |
147bf263 | 217 | #else |
888dde65 | 218 | return new wxPrinterDCImpl( owner, data ); |
147bf263 JS |
219 | #endif |
220 | } | |
221 | ||
6038ec8e RR |
222 | bool wxNativePrintFactory::HasOwnPrintToFile() |
223 | { | |
224 | // Only relevant for PostScript and here the | |
2997ca30 | 225 | // setup dialog provides no "print to file" |
6038ec8e RR |
226 | // option. In the GNOME setup dialog, the |
227 | // setup dialog has its own print to file. | |
228 | return false; | |
229 | } | |
230 | ||
231 | bool wxNativePrintFactory::HasPrinterLine() | |
232 | { | |
233 | // Only relevant for PostScript for now | |
234 | return true; | |
235 | } | |
236 | ||
237 | wxString wxNativePrintFactory::CreatePrinterLine() | |
238 | { | |
239 | // Only relevant for PostScript for now | |
2997ca30 | 240 | |
6038ec8e RR |
241 | // We should query "lpstat -d" here |
242 | return _("Generic PostScript"); | |
243 | } | |
244 | ||
245 | bool wxNativePrintFactory::HasStatusLine() | |
246 | { | |
247 | // Only relevant for PostScript for now | |
2997ca30 | 248 | return true; |
6038ec8e RR |
249 | } |
250 | ||
251 | wxString wxNativePrintFactory::CreateStatusLine() | |
252 | { | |
253 | // Only relevant for PostScript for now | |
2997ca30 | 254 | |
6038ec8e RR |
255 | // We should query "lpstat -r" or "lpstat -p" here |
256 | return _("Ready"); | |
257 | } | |
258 | ||
8850cbd3 RR |
259 | wxPrintNativeDataBase *wxNativePrintFactory::CreatePrintNativeData() |
260 | { | |
261 | #if defined(__WXMSW__) && !defined(__WXUNIVERSAL__) | |
262 | return new wxWindowsPrintNativeData; | |
263 | #elif defined(__WXMAC__) | |
c347101b | 264 | return wxOSXCreatePrintData(); |
8850cbd3 RR |
265 | #else |
266 | return new wxPostScriptPrintNativeData; | |
267 | #endif | |
268 | } | |
269 | ||
270 | //---------------------------------------------------------------------------- | |
271 | // wxPrintNativeDataBase | |
272 | //---------------------------------------------------------------------------- | |
273 | ||
274 | IMPLEMENT_ABSTRACT_CLASS(wxPrintNativeDataBase, wxObject) | |
275 | ||
276 | wxPrintNativeDataBase::wxPrintNativeDataBase() | |
2997ca30 WS |
277 | { |
278 | m_ref = 1; | |
8850cbd3 RR |
279 | } |
280 | ||
0ab2de15 RR |
281 | //---------------------------------------------------------------------------- |
282 | // wxPrintFactoryModule | |
283 | //---------------------------------------------------------------------------- | |
284 | ||
285 | class wxPrintFactoryModule: public wxModule | |
286 | { | |
287 | public: | |
288 | wxPrintFactoryModule() {} | |
289 | bool OnInit() { return true; } | |
290 | void OnExit() { wxPrintFactory::SetPrintFactory( NULL ); } | |
2997ca30 | 291 | |
0ab2de15 RR |
292 | private: |
293 | DECLARE_DYNAMIC_CLASS(wxPrintFactoryModule) | |
294 | }; | |
295 | ||
296 | IMPLEMENT_DYNAMIC_CLASS(wxPrintFactoryModule, wxModule) | |
297 | ||
e81e3883 RR |
298 | //---------------------------------------------------------------------------- |
299 | // wxPrinterBase | |
300 | //---------------------------------------------------------------------------- | |
301 | ||
302 | IMPLEMENT_CLASS(wxPrinterBase, wxObject) | |
7bcb11d3 JS |
303 | |
304 | wxPrinterBase::wxPrinterBase(wxPrintDialogData *data) | |
c801d85f | 305 | { |
d3b9f782 VZ |
306 | m_currentPrintout = NULL; |
307 | sm_abortWindow = NULL; | |
7e548f6b | 308 | sm_abortIt = false; |
7bcb11d3 JS |
309 | if (data) |
310 | m_printDialogData = (*data); | |
f6bcfd97 | 311 | sm_lastError = wxPRINTER_NO_ERROR; |
c801d85f KB |
312 | } |
313 | ||
d3b9f782 | 314 | wxWindow *wxPrinterBase::sm_abortWindow = NULL; |
7e548f6b | 315 | bool wxPrinterBase::sm_abortIt = false; |
f6bcfd97 | 316 | wxPrinterError wxPrinterBase::sm_lastError = wxPRINTER_NO_ERROR; |
c801d85f | 317 | |
34da0970 | 318 | wxPrinterBase::~wxPrinterBase() |
c801d85f KB |
319 | { |
320 | } | |
321 | ||
fc799548 | 322 | wxWindow *wxPrinterBase::CreateAbortWindow(wxWindow *parent, wxPrintout * printout) |
c801d85f | 323 | { |
fc799548 | 324 | wxPrintAbortDialog *dialog = new wxPrintAbortDialog(parent, _("Printing ") , wxDefaultPosition, wxDefaultSize, wxDEFAULT_DIALOG_STYLE); |
8826f46f | 325 | |
fc799548 | 326 | wxBoxSizer *button_sizer = new wxBoxSizer( wxVERTICAL ); |
7e548f6b | 327 | button_sizer->Add( new wxStaticText(dialog, wxID_ANY, _("Please wait while printing\n") + printout->GetTitle() ), 0, wxALL, 10 ); |
fc799548 | 328 | button_sizer->Add( new wxButton( dialog, wxID_CANCEL, wxT("Cancel") ), 0, wxALL | wxALIGN_CENTER, 10 ); |
8826f46f | 329 | |
7e548f6b | 330 | dialog->SetAutoLayout( true ); |
fc799548 JS |
331 | dialog->SetSizer( button_sizer ); |
332 | ||
333 | button_sizer->Fit(dialog); | |
334 | button_sizer->SetSizeHints (dialog) ; | |
8826f46f | 335 | |
7bcb11d3 | 336 | return dialog; |
c801d85f KB |
337 | } |
338 | ||
161f4f73 | 339 | void wxPrinterBase::ReportError(wxWindow *parent, wxPrintout *WXUNUSED(printout), const wxString& message) |
c801d85f | 340 | { |
7bcb11d3 | 341 | wxMessageBox(message, _("Printing Error"), wxOK, parent); |
c801d85f KB |
342 | } |
343 | ||
c061373d RR |
344 | wxPrintDialogData& wxPrinterBase::GetPrintDialogData() const |
345 | { | |
346 | return (wxPrintDialogData&) m_printDialogData; | |
347 | } | |
348 | ||
e81e3883 RR |
349 | //---------------------------------------------------------------------------- |
350 | // wxPrinter | |
351 | //---------------------------------------------------------------------------- | |
352 | ||
353 | IMPLEMENT_CLASS(wxPrinter, wxPrinterBase) | |
354 | ||
355 | wxPrinter::wxPrinter(wxPrintDialogData *data) | |
356 | { | |
357 | m_pimpl = wxPrintFactory::GetFactory()->CreatePrinter( data ); | |
358 | } | |
359 | ||
360 | wxPrinter::~wxPrinter() | |
361 | { | |
362 | delete m_pimpl; | |
363 | } | |
364 | ||
365 | wxWindow *wxPrinter::CreateAbortWindow(wxWindow *parent, wxPrintout *printout) | |
366 | { | |
367 | return m_pimpl->CreateAbortWindow( parent, printout ); | |
368 | } | |
369 | ||
370 | void wxPrinter::ReportError(wxWindow *parent, wxPrintout *printout, const wxString& message) | |
371 | { | |
372 | m_pimpl->ReportError( parent, printout, message ); | |
373 | } | |
374 | ||
375 | bool wxPrinter::Setup(wxWindow *parent) | |
376 | { | |
377 | return m_pimpl->Setup( parent ); | |
378 | } | |
379 | ||
380 | bool wxPrinter::Print(wxWindow *parent, wxPrintout *printout, bool prompt) | |
381 | { | |
382 | return m_pimpl->Print( parent, printout, prompt ); | |
383 | } | |
384 | ||
385 | wxDC* wxPrinter::PrintDialog(wxWindow *parent) | |
386 | { | |
387 | return m_pimpl->PrintDialog( parent ); | |
388 | } | |
389 | ||
c061373d RR |
390 | wxPrintDialogData& wxPrinter::GetPrintDialogData() const |
391 | { | |
392 | return m_pimpl->GetPrintDialogData(); | |
393 | } | |
394 | ||
395 | // --------------------------------------------------------------------------- | |
891daf8c | 396 | // wxPrintDialogBase: the dialog for printing. |
c061373d RR |
397 | // --------------------------------------------------------------------------- |
398 | ||
891daf8c | 399 | IMPLEMENT_ABSTRACT_CLASS(wxPrintDialogBase, wxDialog) |
c061373d | 400 | |
6ce8c562 | 401 | wxPrintDialogBase::wxPrintDialogBase(wxWindow *parent, |
2997ca30 | 402 | wxWindowID id, |
6ce8c562 VZ |
403 | const wxString &title, |
404 | const wxPoint &pos, | |
405 | const wxSize &size, | |
406 | long style) | |
891daf8c RR |
407 | : wxDialog( parent, id, title.empty() ? wxString(_("Print")) : title, |
408 | pos, size, style ) | |
c061373d RR |
409 | { |
410 | } | |
411 | ||
412 | // --------------------------------------------------------------------------- | |
891daf8c | 413 | // wxPrintDialog: the dialog for printing |
c061373d RR |
414 | // --------------------------------------------------------------------------- |
415 | ||
416 | IMPLEMENT_CLASS(wxPrintDialog, wxObject) | |
417 | ||
418 | wxPrintDialog::wxPrintDialog(wxWindow *parent, wxPrintDialogData* data) | |
419 | { | |
420 | m_pimpl = wxPrintFactory::GetFactory()->CreatePrintDialog( parent, data ); | |
421 | } | |
422 | ||
423 | wxPrintDialog::wxPrintDialog(wxWindow *parent, wxPrintData* data) | |
424 | { | |
425 | m_pimpl = wxPrintFactory::GetFactory()->CreatePrintDialog( parent, data ); | |
426 | } | |
427 | ||
428 | wxPrintDialog::~wxPrintDialog() | |
429 | { | |
430 | delete m_pimpl; | |
431 | } | |
432 | ||
433 | int wxPrintDialog::ShowModal() | |
434 | { | |
435 | return m_pimpl->ShowModal(); | |
436 | } | |
437 | ||
438 | wxPrintDialogData& wxPrintDialog::GetPrintDialogData() | |
439 | { | |
440 | return m_pimpl->GetPrintDialogData(); | |
441 | } | |
442 | ||
443 | wxPrintData& wxPrintDialog::GetPrintData() | |
444 | { | |
445 | return m_pimpl->GetPrintData(); | |
446 | } | |
891daf8c | 447 | |
c061373d RR |
448 | wxDC *wxPrintDialog::GetPrintDC() |
449 | { | |
450 | return m_pimpl->GetPrintDC(); | |
451 | } | |
452 | ||
891daf8c RR |
453 | // --------------------------------------------------------------------------- |
454 | // wxPageSetupDialogBase: the page setup dialog | |
455 | // --------------------------------------------------------------------------- | |
456 | ||
457 | IMPLEMENT_ABSTRACT_CLASS(wxPageSetupDialogBase, wxDialog) | |
458 | ||
459 | wxPageSetupDialogBase::wxPageSetupDialogBase(wxWindow *parent, | |
2997ca30 | 460 | wxWindowID id, |
891daf8c RR |
461 | const wxString &title, |
462 | const wxPoint &pos, | |
463 | const wxSize &size, | |
464 | long style) | |
465 | : wxDialog( parent, id, title.empty() ? wxString(_("Page setup")) : title, | |
466 | pos, size, style ) | |
467 | { | |
468 | } | |
469 | ||
470 | // --------------------------------------------------------------------------- | |
471 | // wxPageSetupDialog: the page setup dialog | |
472 | // --------------------------------------------------------------------------- | |
473 | ||
474 | IMPLEMENT_CLASS(wxPageSetupDialog, wxObject) | |
475 | ||
476 | wxPageSetupDialog::wxPageSetupDialog(wxWindow *parent, wxPageSetupDialogData *data ) | |
477 | { | |
478 | m_pimpl = wxPrintFactory::GetFactory()->CreatePageSetupDialog( parent, data ); | |
479 | } | |
480 | ||
481 | wxPageSetupDialog::~wxPageSetupDialog() | |
482 | { | |
483 | delete m_pimpl; | |
484 | } | |
485 | ||
486 | int wxPageSetupDialog::ShowModal() | |
487 | { | |
488 | return m_pimpl->ShowModal(); | |
489 | } | |
490 | ||
491 | wxPageSetupDialogData& wxPageSetupDialog::GetPageSetupDialogData() | |
492 | { | |
493 | return m_pimpl->GetPageSetupDialogData(); | |
494 | } | |
495 | ||
496 | // old name | |
497 | wxPageSetupDialogData& wxPageSetupDialog::GetPageSetupData() | |
498 | { | |
499 | return m_pimpl->GetPageSetupDialogData(); | |
500 | } | |
501 | ||
e81e3883 RR |
502 | //---------------------------------------------------------------------------- |
503 | // wxPrintAbortDialog | |
504 | //---------------------------------------------------------------------------- | |
505 | ||
506 | BEGIN_EVENT_TABLE(wxPrintAbortDialog, wxDialog) | |
507 | EVT_BUTTON(wxID_CANCEL, wxPrintAbortDialog::OnCancel) | |
508 | END_EVENT_TABLE() | |
509 | ||
510 | void wxPrintAbortDialog::OnCancel(wxCommandEvent& WXUNUSED(event)) | |
511 | { | |
512 | wxPrinterBase::sm_abortIt = true; | |
513 | wxPrinterBase::sm_abortWindow->Show(false); | |
514 | wxPrinterBase::sm_abortWindow->Close(true); | |
861eef72 | 515 | wxPrinterBase::sm_abortWindow->Destroy(); |
d3b9f782 | 516 | wxPrinterBase::sm_abortWindow = NULL; |
e81e3883 RR |
517 | } |
518 | ||
519 | //---------------------------------------------------------------------------- | |
520 | // wxPrintout | |
521 | //---------------------------------------------------------------------------- | |
522 | ||
523 | IMPLEMENT_ABSTRACT_CLASS(wxPrintout, wxObject) | |
7bcb11d3 | 524 | |
34da0970 | 525 | wxPrintout::wxPrintout(const wxString& title) |
c801d85f | 526 | { |
7bcb11d3 | 527 | m_printoutTitle = title ; |
d3b9f782 | 528 | m_printoutDC = NULL; |
7bcb11d3 JS |
529 | m_pageWidthMM = 0; |
530 | m_pageHeightMM = 0; | |
531 | m_pageWidthPixels = 0; | |
532 | m_pageHeightPixels = 0; | |
533 | m_PPIScreenX = 0; | |
534 | m_PPIScreenY = 0; | |
535 | m_PPIPrinterX = 0; | |
536 | m_PPIPrinterY = 0; | |
1bd122dd | 537 | m_preview = NULL; |
c801d85f KB |
538 | } |
539 | ||
34da0970 | 540 | wxPrintout::~wxPrintout() |
c801d85f | 541 | { |
c801d85f KB |
542 | } |
543 | ||
544 | bool wxPrintout::OnBeginDocument(int WXUNUSED(startPage), int WXUNUSED(endPage)) | |
545 | { | |
fc799548 | 546 | return GetDC()->StartDoc(_("Printing ") + m_printoutTitle); |
c801d85f KB |
547 | } |
548 | ||
34da0970 | 549 | void wxPrintout::OnEndDocument() |
c801d85f | 550 | { |
7bcb11d3 | 551 | GetDC()->EndDoc(); |
c801d85f KB |
552 | } |
553 | ||
34da0970 | 554 | void wxPrintout::OnBeginPrinting() |
c801d85f KB |
555 | { |
556 | } | |
557 | ||
34da0970 | 558 | void wxPrintout::OnEndPrinting() |
c801d85f KB |
559 | { |
560 | } | |
561 | ||
562 | bool wxPrintout::HasPage(int page) | |
563 | { | |
7bcb11d3 | 564 | return (page == 1); |
c801d85f KB |
565 | } |
566 | ||
567 | void wxPrintout::GetPageInfo(int *minPage, int *maxPage, int *fromPage, int *toPage) | |
568 | { | |
7bcb11d3 JS |
569 | *minPage = 1; |
570 | *maxPage = 32000; | |
571 | *fromPage = 1; | |
572 | *toPage = 1; | |
c801d85f KB |
573 | } |
574 | ||
f415cab9 JS |
575 | void wxPrintout::FitThisSizeToPaper(const wxSize& imageSize) |
576 | { | |
577 | // Set the DC scale and origin so that the given image size fits within the | |
578 | // entire page and the origin is at the top left corner of the page. Note | |
579 | // that with most printers, portions of the page will be non-printable. Use | |
580 | // this if you're managing your own page margins. | |
581 | if (!m_printoutDC) return; | |
582 | wxRect paperRect = GetPaperRectPixels(); | |
583 | wxCoord pw, ph; | |
584 | GetPageSizePixels(&pw, &ph); | |
585 | wxCoord w, h; | |
586 | m_printoutDC->GetSize(&w, &h); | |
587 | float scaleX = ((float(paperRect.width) * w) / (float(pw) * imageSize.x)); | |
588 | float scaleY = ((float(paperRect.height) * h) / (float(ph) * imageSize.y)); | |
589 | float actualScale = wxMin(scaleX, scaleY); | |
03647350 | 590 | m_printoutDC->SetUserScale(actualScale, actualScale); |
f415cab9 JS |
591 | m_printoutDC->SetDeviceOrigin(0, 0); |
592 | wxRect logicalPaperRect = GetLogicalPaperRect(); | |
593 | SetLogicalOrigin(logicalPaperRect.x, logicalPaperRect.y); | |
594 | } | |
595 | ||
596 | void wxPrintout::FitThisSizeToPage(const wxSize& imageSize) | |
597 | { | |
598 | // Set the DC scale and origin so that the given image size fits within the | |
599 | // printable area of the page and the origin is at the top left corner of | |
600 | // the printable area. | |
601 | if (!m_printoutDC) return; | |
602 | int w, h; | |
603 | m_printoutDC->GetSize(&w, &h); | |
604 | float scaleX = float(w) / imageSize.x; | |
605 | float scaleY = float(h) / imageSize.y; | |
606 | float actualScale = wxMin(scaleX, scaleY); | |
03647350 | 607 | m_printoutDC->SetUserScale(actualScale, actualScale); |
f415cab9 JS |
608 | m_printoutDC->SetDeviceOrigin(0, 0); |
609 | } | |
610 | ||
611 | void wxPrintout::FitThisSizeToPageMargins(const wxSize& imageSize, const wxPageSetupDialogData& pageSetupData) | |
612 | { | |
613 | // Set the DC scale and origin so that the given image size fits within the | |
614 | // page margins defined in the given wxPageSetupDialogData object and the | |
615 | // origin is at the top left corner of the page margins. | |
616 | if (!m_printoutDC) return; | |
617 | wxRect paperRect = GetPaperRectPixels(); | |
618 | wxCoord pw, ph; | |
619 | GetPageSizePixels(&pw, &ph); | |
620 | wxPoint topLeft = pageSetupData.GetMarginTopLeft(); | |
621 | wxPoint bottomRight = pageSetupData.GetMarginBottomRight(); | |
622 | wxCoord mw, mh; | |
623 | GetPageSizeMM(&mw, &mh); | |
624 | float mmToDeviceX = float(pw) / mw; | |
625 | float mmToDeviceY = float(ph) / mh; | |
5a70d3f5 VZ |
626 | wxRect pageMarginsRect(paperRect.x + wxRound(mmToDeviceX * topLeft.x), |
627 | paperRect.y + wxRound(mmToDeviceY * topLeft.y), | |
628 | paperRect.width - wxRound(mmToDeviceX * (topLeft.x + bottomRight.x)), | |
629 | paperRect.height - wxRound(mmToDeviceY * (topLeft.y + bottomRight.y))); | |
f415cab9 JS |
630 | wxCoord w, h; |
631 | m_printoutDC->GetSize(&w, &h); | |
632 | float scaleX = (float(pageMarginsRect.width) * w) / (float(pw) * imageSize.x); | |
633 | float scaleY = (float(pageMarginsRect.height) * h) / (float(ph) * imageSize.y); | |
634 | float actualScale = wxMin(scaleX, scaleY); | |
03647350 | 635 | m_printoutDC->SetUserScale(actualScale, actualScale); |
f415cab9 JS |
636 | m_printoutDC->SetDeviceOrigin(0, 0); |
637 | wxRect logicalPageMarginsRect = GetLogicalPageMarginsRect(pageSetupData); | |
638 | SetLogicalOrigin(logicalPageMarginsRect.x, logicalPageMarginsRect.y); | |
639 | } | |
640 | ||
641 | void wxPrintout::MapScreenSizeToPaper() | |
642 | { | |
643 | // Set the DC scale so that an image on the screen is the same size on the | |
644 | // paper and the origin is at the top left of the paper. Note that with most | |
645 | // printers, portions of the page will be cut off. Use this if you're | |
646 | // managing your own page margins. | |
647 | if (!m_printoutDC) return; | |
648 | MapScreenSizeToPage(); | |
649 | wxRect logicalPaperRect = GetLogicalPaperRect(); | |
650 | SetLogicalOrigin(logicalPaperRect.x, logicalPaperRect.y); | |
651 | } | |
652 | ||
653 | void wxPrintout::MapScreenSizeToPage() | |
654 | { | |
655 | // Set the DC scale and origin so that an image on the screen is the same | |
656 | // size on the paper and the origin is at the top left of the printable area. | |
657 | if (!m_printoutDC) return; | |
03647350 VZ |
658 | int ppiScreenX, ppiScreenY; |
659 | GetPPIScreen(&ppiScreenX, &ppiScreenY); | |
660 | int ppiPrinterX, ppiPrinterY; | |
661 | GetPPIPrinter(&ppiPrinterX, &ppiPrinterY); | |
662 | int w, h; | |
663 | m_printoutDC->GetSize(&w, &h); | |
664 | int pageSizePixelsX, pageSizePixelsY; | |
665 | GetPageSizePixels(&pageSizePixelsX, &pageSizePixelsY); | |
666 | float userScaleX = (float(ppiPrinterX) * w) / (float(ppiScreenX) * pageSizePixelsX); | |
667 | float userScaleY = (float(ppiPrinterY) * h) / (float(ppiScreenY) * pageSizePixelsY); | |
f415cab9 JS |
668 | m_printoutDC->SetUserScale(userScaleX, userScaleY); |
669 | m_printoutDC->SetDeviceOrigin(0, 0); | |
670 | } | |
671 | ||
672 | void wxPrintout::MapScreenSizeToPageMargins(const wxPageSetupDialogData& pageSetupData) | |
673 | { | |
674 | // Set the DC scale so that an image on the screen is the same size on the | |
675 | // paper and the origin is at the top left of the page margins defined by | |
676 | // the given wxPageSetupDialogData object. | |
677 | if (!m_printoutDC) return; | |
678 | MapScreenSizeToPage(); | |
679 | wxRect logicalPageMarginsRect = GetLogicalPageMarginsRect(pageSetupData); | |
680 | SetLogicalOrigin(logicalPageMarginsRect.x, logicalPageMarginsRect.y); | |
681 | } | |
682 | ||
683 | void wxPrintout::MapScreenSizeToDevice() | |
684 | { | |
685 | // Set the DC scale so that a screen pixel is the same size as a device | |
686 | // pixel and the origin is at the top left of the printable area. | |
687 | if (!m_printoutDC) return; | |
03647350 VZ |
688 | int w, h; |
689 | m_printoutDC->GetSize(&w, &h); | |
690 | int pageSizePixelsX, pageSizePixelsY; | |
691 | GetPageSizePixels(&pageSizePixelsX, &pageSizePixelsY); | |
692 | float userScaleX = float(w) / pageSizePixelsX; | |
693 | float userScaleY = float(h) / pageSizePixelsY; | |
f415cab9 JS |
694 | m_printoutDC->SetUserScale(userScaleX, userScaleY); |
695 | m_printoutDC->SetDeviceOrigin(0, 0); | |
696 | } | |
697 | ||
698 | wxRect wxPrintout::GetLogicalPaperRect() const | |
699 | { | |
700 | // Return the rectangle in logical units that corresponds to the paper | |
701 | // rectangle. | |
702 | wxRect paperRect = GetPaperRectPixels(); | |
703 | wxCoord pw, ph; | |
704 | GetPageSizePixels(&pw, &ph); | |
705 | wxCoord w, h; | |
706 | m_printoutDC->GetSize(&w, &h); | |
707 | if (w == pw && h == ph) { | |
708 | // this DC matches the printed page, so no scaling | |
03647350 VZ |
709 | return wxRect(m_printoutDC->DeviceToLogicalX(paperRect.x), |
710 | m_printoutDC->DeviceToLogicalY(paperRect.y), | |
711 | m_printoutDC->DeviceToLogicalXRel(paperRect.width), | |
f415cab9 JS |
712 | m_printoutDC->DeviceToLogicalYRel(paperRect.height)); |
713 | } | |
714 | // This DC doesn't match the printed page, so we have to scale. | |
715 | float scaleX = float(w) / pw; | |
716 | float scaleY = float(h) / ph; | |
03647350 VZ |
717 | return wxRect(m_printoutDC->DeviceToLogicalX(wxRound(paperRect.x * scaleX)), |
718 | m_printoutDC->DeviceToLogicalY(wxRound(paperRect.y * scaleY)), | |
719 | m_printoutDC->DeviceToLogicalXRel(wxRound(paperRect.width * scaleX)), | |
5a70d3f5 | 720 | m_printoutDC->DeviceToLogicalYRel(wxRound(paperRect.height * scaleY))); |
f415cab9 JS |
721 | } |
722 | ||
723 | wxRect wxPrintout::GetLogicalPageRect() const | |
724 | { | |
725 | // Return the rectangle in logical units that corresponds to the printable | |
726 | // area. | |
727 | int w, h; | |
728 | m_printoutDC->GetSize(&w, &h); | |
03647350 VZ |
729 | return wxRect(m_printoutDC->DeviceToLogicalX(0), |
730 | m_printoutDC->DeviceToLogicalY(0), | |
731 | m_printoutDC->DeviceToLogicalXRel(w), | |
f415cab9 JS |
732 | m_printoutDC->DeviceToLogicalYRel(h)); |
733 | } | |
734 | ||
735 | wxRect wxPrintout::GetLogicalPageMarginsRect(const wxPageSetupDialogData& pageSetupData) const | |
736 | { | |
737 | // Return the rectangle in logical units that corresponds to the region | |
738 | // within the page margins as specified by the given wxPageSetupDialogData | |
739 | // object. | |
03647350 | 740 | |
02255e07 RR |
741 | // We get the paper size in device units and the margins in mm, |
742 | // so we need to calculate the conversion with this trick | |
f415cab9 JS |
743 | wxCoord pw, ph; |
744 | GetPageSizePixels(&pw, &ph); | |
f415cab9 JS |
745 | wxCoord mw, mh; |
746 | GetPageSizeMM(&mw, &mh); | |
747 | float mmToDeviceX = float(pw) / mw; | |
748 | float mmToDeviceY = float(ph) / mh; | |
02255e07 | 749 | |
03647350 | 750 | // paper size in device units |
02255e07 | 751 | wxRect paperRect = GetPaperRectPixels(); |
03647350 | 752 | |
02255e07 RR |
753 | // margins in mm |
754 | wxPoint topLeft = pageSetupData.GetMarginTopLeft(); | |
755 | wxPoint bottomRight = pageSetupData.GetMarginBottomRight(); | |
03647350 | 756 | |
02255e07 RR |
757 | // calculate margins in device units |
758 | wxRect pageMarginsRect( | |
759 | paperRect.x + wxRound(mmToDeviceX * topLeft.x), | |
760 | paperRect.y + wxRound(mmToDeviceY * topLeft.y), | |
761 | paperRect.width - wxRound(mmToDeviceX * (topLeft.x + bottomRight.x)), | |
5a70d3f5 | 762 | paperRect.height - wxRound(mmToDeviceY * (topLeft.y + bottomRight.y))); |
03647350 | 763 | |
f415cab9 JS |
764 | wxCoord w, h; |
765 | m_printoutDC->GetSize(&w, &h); | |
03647350 | 766 | if (w == pw && h == ph) |
02255e07 | 767 | { |
f415cab9 | 768 | // this DC matches the printed page, so no scaling |
02255e07 | 769 | return wxRect( |
03647350 VZ |
770 | m_printoutDC->DeviceToLogicalX(pageMarginsRect.x), |
771 | m_printoutDC->DeviceToLogicalY(pageMarginsRect.y), | |
772 | m_printoutDC->DeviceToLogicalXRel(pageMarginsRect.width), | |
f415cab9 JS |
773 | m_printoutDC->DeviceToLogicalYRel(pageMarginsRect.height)); |
774 | } | |
03647350 | 775 | |
f415cab9 JS |
776 | // This DC doesn't match the printed page, so we have to scale. |
777 | float scaleX = float(w) / pw; | |
778 | float scaleY = float(h) / ph; | |
03647350 VZ |
779 | return wxRect(m_printoutDC->DeviceToLogicalX(wxRound(pageMarginsRect.x * scaleX)), |
780 | m_printoutDC->DeviceToLogicalY(wxRound(pageMarginsRect.y * scaleY)), | |
781 | m_printoutDC->DeviceToLogicalXRel(wxRound(pageMarginsRect.width * scaleX)), | |
5a70d3f5 | 782 | m_printoutDC->DeviceToLogicalYRel(wxRound(pageMarginsRect.height * scaleY))); |
f415cab9 JS |
783 | } |
784 | ||
785 | void wxPrintout::SetLogicalOrigin(wxCoord x, wxCoord y) | |
786 | { | |
787 | // Set the device origin by specifying a point in logical coordinates. | |
02255e07 | 788 | m_printoutDC->SetDeviceOrigin( |
03647350 | 789 | m_printoutDC->LogicalToDeviceX(x), |
02255e07 | 790 | m_printoutDC->LogicalToDeviceY(y) ); |
f415cab9 | 791 | } |
03647350 | 792 | |
f415cab9 JS |
793 | void wxPrintout::OffsetLogicalOrigin(wxCoord xoff, wxCoord yoff) |
794 | { | |
795 | // Offset the device origin by a specified distance in device coordinates. | |
02255e07 RR |
796 | wxPoint dev_org = m_printoutDC->GetDeviceOrigin(); |
797 | m_printoutDC->SetDeviceOrigin( | |
03647350 | 798 | dev_org.x + m_printoutDC->LogicalToDeviceXRel(xoff), |
02255e07 | 799 | dev_org.y + m_printoutDC->LogicalToDeviceYRel(yoff) ); |
f415cab9 | 800 | } |
03647350 | 801 | |
f415cab9 | 802 | |
e81e3883 RR |
803 | //---------------------------------------------------------------------------- |
804 | // wxPreviewCanvas | |
805 | //---------------------------------------------------------------------------- | |
806 | ||
807 | IMPLEMENT_CLASS(wxPreviewCanvas, wxWindow) | |
808 | ||
809 | BEGIN_EVENT_TABLE(wxPreviewCanvas, wxScrolledWindow) | |
810 | EVT_PAINT(wxPreviewCanvas::OnPaint) | |
811 | EVT_CHAR(wxPreviewCanvas::OnChar) | |
b88bf073 | 812 | EVT_IDLE(wxPreviewCanvas::OnIdle) |
e81e3883 | 813 | EVT_SYS_COLOUR_CHANGED(wxPreviewCanvas::OnSysColourChanged) |
fb6efdf2 VZ |
814 | #if wxUSE_MOUSEWHEEL |
815 | EVT_MOUSEWHEEL(wxPreviewCanvas::OnMouseWheel) | |
816 | #endif | |
e81e3883 | 817 | END_EVENT_TABLE() |
7bcb11d3 | 818 | |
e9cafd42 VZ |
819 | // VZ: the current code doesn't refresh properly without |
820 | // wxFULL_REPAINT_ON_RESIZE, this must be fixed as otherwise we have | |
821 | // really horrible flicker when resizing the preview frame, but without | |
822 | // this style it simply doesn't work correctly at all... | |
c801d85f | 823 | wxPreviewCanvas::wxPreviewCanvas(wxPrintPreviewBase *preview, wxWindow *parent, |
7bcb11d3 | 824 | const wxPoint& pos, const wxSize& size, long style, const wxString& name): |
7e548f6b | 825 | wxScrolledWindow(parent, wxID_ANY, pos, size, style | wxFULL_REPAINT_ON_RESIZE, name) |
c801d85f | 826 | { |
7bcb11d3 | 827 | m_printPreview = preview; |
46b24ef9 JS |
828 | #ifdef __WXMAC__ |
829 | // The app workspace colour is always white, but we should have | |
830 | // a contrast with the page. | |
ca5020c2 | 831 | wxSystemColour colourIndex = wxSYS_COLOUR_3DDKSHADOW; |
92b799e0 JS |
832 | #elif defined(__WXGTK__) |
833 | wxSystemColour colourIndex = wxSYS_COLOUR_BTNFACE; | |
46b24ef9 JS |
834 | #else |
835 | wxSystemColour colourIndex = wxSYS_COLOUR_APPWORKSPACE; | |
e71fd398 | 836 | #endif |
46b24ef9 | 837 | SetBackgroundColour(wxSystemSettings::GetColour(colourIndex)); |
8826f46f | 838 | |
d2b354f9 | 839 | SetScrollbars(10, 10, 100, 100); |
c801d85f KB |
840 | } |
841 | ||
34da0970 | 842 | wxPreviewCanvas::~wxPreviewCanvas() |
c801d85f KB |
843 | { |
844 | } | |
845 | ||
846 | void wxPreviewCanvas::OnPaint(wxPaintEvent& WXUNUSED(event)) | |
847 | { | |
7bcb11d3 JS |
848 | wxPaintDC dc(this); |
849 | PrepareDC( dc ); | |
8826f46f | 850 | |
a56fcaaf | 851 | /* |
809934d2 RR |
852 | #ifdef __WXGTK__ |
853 | if (!GetUpdateRegion().IsEmpty()) | |
854 | dc.SetClippingRegion( GetUpdateRegion() ); | |
855 | #endif | |
a56fcaaf | 856 | */ |
809934d2 | 857 | |
7bcb11d3 JS |
858 | if (m_printPreview) |
859 | { | |
860 | m_printPreview->PaintPage(this, dc); | |
861 | } | |
c801d85f KB |
862 | } |
863 | ||
b88bf073 VS |
864 | void wxPreviewCanvas::OnIdle(wxIdleEvent& event) |
865 | { | |
88b98787 VS |
866 | event.Skip(); |
867 | ||
868 | // prevent UpdatePageRendering() from being called recursively: | |
869 | static bool s_inIdle = false; | |
870 | if ( s_inIdle ) | |
871 | return; | |
872 | s_inIdle = true; | |
873 | ||
b88bf073 VS |
874 | if ( m_printPreview ) |
875 | { | |
876 | if ( m_printPreview->UpdatePageRendering() ) | |
877 | Refresh(); | |
878 | } | |
88b98787 VS |
879 | |
880 | s_inIdle = false; | |
b88bf073 VS |
881 | } |
882 | ||
c801d85f KB |
883 | // Responds to colour changes, and passes event on to children. |
884 | void wxPreviewCanvas::OnSysColourChanged(wxSysColourChangedEvent& event) | |
885 | { | |
46b24ef9 JS |
886 | #ifdef __WXMAC__ |
887 | // The app workspace colour is always white, but we should have | |
888 | // a contrast with the page. | |
ca5020c2 | 889 | wxSystemColour colourIndex = wxSYS_COLOUR_3DDKSHADOW; |
92b799e0 JS |
890 | #elif defined(__WXGTK__) |
891 | wxSystemColour colourIndex = wxSYS_COLOUR_BTNFACE; | |
46b24ef9 JS |
892 | #else |
893 | wxSystemColour colourIndex = wxSYS_COLOUR_APPWORKSPACE; | |
e71fd398 | 894 | #endif |
46b24ef9 | 895 | SetBackgroundColour(wxSystemSettings::GetColour(colourIndex)); |
7bcb11d3 | 896 | Refresh(); |
8826f46f | 897 | |
7bcb11d3 JS |
898 | // Propagate the event to the non-top-level children |
899 | wxWindow::OnSysColourChanged(event); | |
c801d85f KB |
900 | } |
901 | ||
d2b354f9 JS |
902 | void wxPreviewCanvas::OnChar(wxKeyEvent &event) |
903 | { | |
b38b0d22 | 904 | wxPreviewControlBar* controlBar = ((wxPreviewFrame*) GetParent())->GetControlBar(); |
043c6705 | 905 | switch (event.GetKeyCode()) |
d2b354f9 | 906 | { |
043c6705 VZ |
907 | case WXK_TAB: |
908 | controlBar->OnGoto(); | |
909 | return; | |
910 | case WXK_RETURN: | |
911 | controlBar->OnPrint(); | |
912 | return; | |
b38b0d22 JS |
913 | } |
914 | ||
d2b354f9 JS |
915 | if (!event.ControlDown()) |
916 | { | |
917 | event.Skip(); | |
918 | return; | |
919 | } | |
e71fd398 | 920 | |
b38b0d22 JS |
921 | switch(event.GetKeyCode()) |
922 | { | |
faa94f3e | 923 | case WXK_PAGEDOWN: |
b38b0d22 | 924 | controlBar->OnNext(); break; |
faa94f3e | 925 | case WXK_PAGEUP: |
b38b0d22 JS |
926 | controlBar->OnPrevious(); break; |
927 | case WXK_HOME: | |
928 | controlBar->OnFirst(); break; | |
929 | case WXK_END: | |
930 | controlBar->OnLast(); break; | |
931 | default: | |
932 | event.Skip(); | |
933 | } | |
d2b354f9 JS |
934 | } |
935 | ||
fb6efdf2 VZ |
936 | #if wxUSE_MOUSEWHEEL |
937 | ||
938 | void wxPreviewCanvas::OnMouseWheel(wxMouseEvent& event) | |
939 | { | |
940 | wxPreviewControlBar * | |
941 | controlBar = wxStaticCast(GetParent(), wxPreviewFrame)->GetControlBar(); | |
942 | ||
943 | if ( controlBar ) | |
944 | { | |
945 | if ( event.ControlDown() && event.GetWheelRotation() != 0 ) | |
946 | { | |
947 | int currentZoom = controlBar->GetZoomControl(); | |
948 | ||
949 | int delta; | |
950 | if ( currentZoom < 100 ) | |
951 | delta = 5; | |
952 | else if ( currentZoom <= 120 ) | |
953 | delta = 10; | |
954 | else | |
955 | delta = 50; | |
956 | ||
957 | if ( event.GetWheelRotation() > 0 ) | |
958 | delta = -delta; | |
959 | ||
960 | int newZoom = currentZoom + delta; | |
961 | if ( newZoom < 10 ) | |
962 | newZoom = 10; | |
963 | if ( newZoom > 200 ) | |
964 | newZoom = 200; | |
965 | if ( newZoom != currentZoom ) | |
966 | { | |
967 | controlBar->SetZoomControl(newZoom); | |
968 | m_printPreview->SetZoom(newZoom); | |
969 | Refresh(); | |
970 | } | |
971 | return; | |
972 | } | |
973 | } | |
974 | ||
975 | event.Skip(); | |
976 | } | |
977 | ||
978 | #endif // wxUSE_MOUSEWHEEL | |
979 | ||
e81e3883 RR |
980 | //---------------------------------------------------------------------------- |
981 | // wxPreviewControlBar | |
982 | //---------------------------------------------------------------------------- | |
983 | ||
984 | IMPLEMENT_CLASS(wxPreviewControlBar, wxWindow) | |
c801d85f KB |
985 | |
986 | BEGIN_EVENT_TABLE(wxPreviewControlBar, wxPanel) | |
8826f46f | 987 | EVT_BUTTON(wxID_PREVIEW_CLOSE, wxPreviewControlBar::OnWindowClose) |
90b6b974 | 988 | EVT_BUTTON(wxID_PREVIEW_PRINT, wxPreviewControlBar::OnPrintButton) |
0f90ec93 KB |
989 | EVT_BUTTON(wxID_PREVIEW_PREVIOUS, wxPreviewControlBar::OnPreviousButton) |
990 | EVT_BUTTON(wxID_PREVIEW_NEXT, wxPreviewControlBar::OnNextButton) | |
bf89b538 JS |
991 | EVT_BUTTON(wxID_PREVIEW_FIRST, wxPreviewControlBar::OnFirstButton) |
992 | EVT_BUTTON(wxID_PREVIEW_LAST, wxPreviewControlBar::OnLastButton) | |
993 | EVT_BUTTON(wxID_PREVIEW_GOTO, wxPreviewControlBar::OnGotoButton) | |
8826f46f VZ |
994 | EVT_CHOICE(wxID_PREVIEW_ZOOM, wxPreviewControlBar::OnZoom) |
995 | EVT_PAINT(wxPreviewControlBar::OnPaint) | |
c801d85f | 996 | END_EVENT_TABLE() |
7bcb11d3 | 997 | |
c801d85f | 998 | wxPreviewControlBar::wxPreviewControlBar(wxPrintPreviewBase *preview, long buttons, |
7bcb11d3 JS |
999 | wxWindow *parent, const wxPoint& pos, const wxSize& size, |
1000 | long style, const wxString& name): | |
7e548f6b | 1001 | wxPanel(parent, wxID_ANY, pos, size, style, name) |
c801d85f | 1002 | { |
7bcb11d3 | 1003 | m_printPreview = preview; |
d3b9f782 VZ |
1004 | m_closeButton = NULL; |
1005 | m_nextPageButton = NULL; | |
1006 | m_previousPageButton = NULL; | |
1007 | m_printButton = NULL; | |
1008 | m_zoomControl = NULL; | |
7bcb11d3 | 1009 | m_buttonFlags = buttons; |
c801d85f KB |
1010 | } |
1011 | ||
34da0970 | 1012 | wxPreviewControlBar::~wxPreviewControlBar() |
c801d85f KB |
1013 | { |
1014 | } | |
1015 | ||
1016 | void wxPreviewControlBar::OnPaint(wxPaintEvent& WXUNUSED(event)) | |
1017 | { | |
7bcb11d3 | 1018 | wxPaintDC dc(this); |
8826f46f | 1019 | |
7bcb11d3 JS |
1020 | int w, h; |
1021 | GetSize(&w, &h); | |
1022 | dc.SetPen(*wxBLACK_PEN); | |
1023 | dc.SetBrush(*wxTRANSPARENT_BRUSH); | |
1024 | dc.DrawLine( 0, h-1, w, h-1 ); | |
c801d85f KB |
1025 | } |
1026 | ||
c330a2cf | 1027 | void wxPreviewControlBar::OnWindowClose(wxCommandEvent& WXUNUSED(event)) |
c801d85f | 1028 | { |
7bcb11d3 | 1029 | wxPreviewFrame *frame = (wxPreviewFrame *)GetParent(); |
7e548f6b | 1030 | frame->Close(true); |
c801d85f KB |
1031 | } |
1032 | ||
b38b0d22 | 1033 | void wxPreviewControlBar::OnPrint(void) |
c801d85f | 1034 | { |
7bcb11d3 | 1035 | wxPrintPreviewBase *preview = GetPrintPreview(); |
7e548f6b | 1036 | preview->Print(true); |
c801d85f KB |
1037 | } |
1038 | ||
0f90ec93 | 1039 | void wxPreviewControlBar::OnNext(void) |
c801d85f | 1040 | { |
7bcb11d3 JS |
1041 | wxPrintPreviewBase *preview = GetPrintPreview(); |
1042 | if (preview) | |
c801d85f | 1043 | { |
7bcb11d3 JS |
1044 | int currentPage = preview->GetCurrentPage(); |
1045 | if ((preview->GetMaxPage() > 0) && | |
1046 | (currentPage < preview->GetMaxPage()) && | |
1047 | preview->GetPrintout()->HasPage(currentPage + 1)) | |
1048 | { | |
1049 | preview->SetCurrentPage(currentPage + 1); | |
1050 | } | |
c801d85f | 1051 | } |
c801d85f KB |
1052 | } |
1053 | ||
0f90ec93 | 1054 | void wxPreviewControlBar::OnPrevious(void) |
c801d85f | 1055 | { |
7bcb11d3 JS |
1056 | wxPrintPreviewBase *preview = GetPrintPreview(); |
1057 | if (preview) | |
c801d85f | 1058 | { |
7bcb11d3 JS |
1059 | int currentPage = preview->GetCurrentPage(); |
1060 | if ((preview->GetMinPage() > 0) && | |
1061 | (currentPage > preview->GetMinPage()) && | |
1062 | preview->GetPrintout()->HasPage(currentPage - 1)) | |
1063 | { | |
1064 | preview->SetCurrentPage(currentPage - 1); | |
1065 | } | |
c801d85f | 1066 | } |
c801d85f KB |
1067 | } |
1068 | ||
bf89b538 JS |
1069 | void wxPreviewControlBar::OnFirst(void) |
1070 | { | |
1071 | wxPrintPreviewBase *preview = GetPrintPreview(); | |
1072 | if (preview) | |
1073 | { | |
1074 | int currentPage = preview->GetMinPage(); | |
1075 | if (preview->GetPrintout()->HasPage(currentPage)) | |
1076 | { | |
1077 | preview->SetCurrentPage(currentPage); | |
1078 | } | |
1079 | } | |
1080 | } | |
1081 | ||
1082 | void wxPreviewControlBar::OnLast(void) | |
1083 | { | |
1084 | wxPrintPreviewBase *preview = GetPrintPreview(); | |
1085 | if (preview) | |
1086 | { | |
1087 | int currentPage = preview->GetMaxPage(); | |
1088 | if (preview->GetPrintout()->HasPage(currentPage)) | |
1089 | { | |
1090 | preview->SetCurrentPage(currentPage); | |
1091 | } | |
1092 | } | |
1093 | } | |
1094 | ||
1095 | void wxPreviewControlBar::OnGoto(void) | |
1096 | { | |
1097 | wxPrintPreviewBase *preview = GetPrintPreview(); | |
1098 | if (preview) | |
1099 | { | |
1100 | long currentPage; | |
1101 | ||
1102 | if (preview->GetMinPage() > 0) | |
1103 | { | |
1104 | wxString strPrompt; | |
1105 | wxString strPage; | |
1106 | ||
d2b354f9 | 1107 | strPrompt.Printf( _("Enter a page number between %d and %d:"), |
bf89b538 | 1108 | preview->GetMinPage(), preview->GetMaxPage()); |
7e99eddf | 1109 | strPage.Printf( wxT("%d"), preview->GetCurrentPage() ); |
bf89b538 JS |
1110 | |
1111 | strPage = | |
d2b354f9 | 1112 | wxGetTextFromUser( strPrompt, _("Goto Page"), strPage, GetParent()); |
bf89b538 JS |
1113 | |
1114 | if ( strPage.ToLong( ¤tPage ) ) | |
1115 | if (preview->GetPrintout()->HasPage(currentPage)) | |
1116 | { | |
1117 | preview->SetCurrentPage(currentPage); | |
1118 | } | |
1119 | } | |
1120 | } | |
1121 | } | |
1122 | ||
c801d85f KB |
1123 | void wxPreviewControlBar::OnZoom(wxCommandEvent& WXUNUSED(event)) |
1124 | { | |
7bcb11d3 JS |
1125 | int zoom = GetZoomControl(); |
1126 | if (GetPrintPreview()) | |
1127 | GetPrintPreview()->SetZoom(zoom); | |
c801d85f KB |
1128 | } |
1129 | ||
34da0970 | 1130 | void wxPreviewControlBar::CreateButtons() |
c801d85f | 1131 | { |
7bcb11d3 | 1132 | SetSize(0, 0, 400, 40); |
8826f46f | 1133 | |
9dff8515 | 1134 | wxBoxSizer *item0 = new wxBoxSizer( wxHORIZONTAL ); |
e71fd398 | 1135 | |
9dff8515 JS |
1136 | m_closeButton = new wxButton( this, wxID_PREVIEW_CLOSE, _("&Close"), wxDefaultPosition, wxDefaultSize, 0 ); |
1137 | item0->Add( m_closeButton, 0, wxALIGN_CENTRE|wxALL, 5 ); | |
e71fd398 | 1138 | |
7bcb11d3 JS |
1139 | if (m_buttonFlags & wxPREVIEW_PRINT) |
1140 | { | |
9dff8515 JS |
1141 | m_printButton = new wxButton( this, wxID_PREVIEW_PRINT, _("&Print..."), wxDefaultPosition, wxDefaultSize, 0 ); |
1142 | item0->Add( m_printButton, 0, wxALIGN_CENTRE|wxALL, 5 ); | |
7bcb11d3 | 1143 | } |
e71fd398 | 1144 | |
892a76f7 JS |
1145 | // Exact-fit buttons are too tiny on wxUniversal |
1146 | int navButtonStyle; | |
1147 | wxSize navButtonSize; | |
1148 | #ifdef __WXUNIVERSAL__ | |
1149 | navButtonStyle = 0; | |
2981fc83 | 1150 | navButtonSize = wxSize(40, m_closeButton->GetSize().y); |
892a76f7 JS |
1151 | #else |
1152 | navButtonStyle = wxBU_EXACTFIT; | |
1153 | navButtonSize = wxDefaultSize; | |
1154 | #endif | |
1155 | ||
bf89b538 JS |
1156 | if (m_buttonFlags & wxPREVIEW_FIRST) |
1157 | { | |
892a76f7 | 1158 | m_firstPageButton = new wxButton( this, wxID_PREVIEW_FIRST, _("|<<"), wxDefaultPosition, navButtonSize, navButtonStyle ); |
9dff8515 | 1159 | item0->Add( m_firstPageButton, 0, wxALIGN_CENTRE|wxALL, 5 ); |
bf89b538 | 1160 | } |
e71fd398 | 1161 | |
7bcb11d3 JS |
1162 | if (m_buttonFlags & wxPREVIEW_PREVIOUS) |
1163 | { | |
892a76f7 | 1164 | m_previousPageButton = new wxButton( this, wxID_PREVIEW_PREVIOUS, _("<<"), wxDefaultPosition, navButtonSize, navButtonStyle ); |
9dff8515 | 1165 | item0->Add( m_previousPageButton, 0, wxALIGN_CENTRE|wxRIGHT|wxTOP|wxBOTTOM, 5 ); |
7bcb11d3 | 1166 | } |
e71fd398 | 1167 | |
7bcb11d3 JS |
1168 | if (m_buttonFlags & wxPREVIEW_NEXT) |
1169 | { | |
892a76f7 | 1170 | m_nextPageButton = new wxButton( this, wxID_PREVIEW_NEXT, _(">>"), wxDefaultPosition, navButtonSize, navButtonStyle ); |
9dff8515 | 1171 | item0->Add( m_nextPageButton, 0, wxALIGN_CENTRE|wxRIGHT|wxTOP|wxBOTTOM, 5 ); |
bf89b538 | 1172 | } |
e71fd398 | 1173 | |
bf89b538 JS |
1174 | if (m_buttonFlags & wxPREVIEW_LAST) |
1175 | { | |
892a76f7 | 1176 | m_lastPageButton = new wxButton( this, wxID_PREVIEW_LAST, _(">>|"), wxDefaultPosition, navButtonSize, navButtonStyle ); |
9dff8515 | 1177 | item0->Add( m_lastPageButton, 0, wxALIGN_CENTRE|wxRIGHT|wxTOP|wxBOTTOM, 5 ); |
bf89b538 | 1178 | } |
e71fd398 | 1179 | |
bf89b538 JS |
1180 | if (m_buttonFlags & wxPREVIEW_GOTO) |
1181 | { | |
9dff8515 JS |
1182 | m_gotoPageButton = new wxButton( this, wxID_PREVIEW_GOTO, _("&Goto..."), wxDefaultPosition, wxDefaultSize, 0 ); |
1183 | item0->Add( m_gotoPageButton, 0, wxALIGN_CENTRE|wxALL, 5 ); | |
7bcb11d3 | 1184 | } |
e71fd398 | 1185 | |
7bcb11d3 JS |
1186 | if (m_buttonFlags & wxPREVIEW_ZOOM) |
1187 | { | |
e71fd398 | 1188 | wxString choices[] = |
c25ccf85 | 1189 | { |
2b5f62a0 | 1190 | wxT("10%"), wxT("15%"), wxT("20%"), wxT("25%"), wxT("30%"), wxT("35%"), wxT("40%"), wxT("45%"), wxT("50%"), wxT("55%"), |
9dff8515 JS |
1191 | wxT("60%"), wxT("65%"), wxT("70%"), wxT("75%"), wxT("80%"), wxT("85%"), wxT("90%"), wxT("95%"), wxT("100%"), wxT("110%"), |
1192 | wxT("120%"), wxT("150%"), wxT("200%") | |
c25ccf85 | 1193 | }; |
c25ccf85 | 1194 | int n = WXSIZEOF(choices); |
e71fd398 | 1195 | |
7e548f6b | 1196 | m_zoomControl = new wxChoice( this, wxID_PREVIEW_ZOOM, wxDefaultPosition, wxSize(70,wxDefaultCoord), n, choices, 0 ); |
9dff8515 | 1197 | item0->Add( m_zoomControl, 0, wxALIGN_CENTRE|wxALL, 5 ); |
7bcb11d3 JS |
1198 | SetZoomControl(m_printPreview->GetZoom()); |
1199 | } | |
8826f46f | 1200 | |
9dff8515 JS |
1201 | SetSizer(item0); |
1202 | item0->Fit(this); | |
c801d85f KB |
1203 | } |
1204 | ||
1205 | void wxPreviewControlBar::SetZoomControl(int zoom) | |
1206 | { | |
7bcb11d3 | 1207 | if (m_zoomControl) |
963907fa RR |
1208 | { |
1209 | int n, count = m_zoomControl->GetCount(); | |
1210 | long val; | |
1211 | for (n=0; n<count; n++) | |
1212 | { | |
1213 | if (m_zoomControl->GetString(n).BeforeFirst(wxT('%')).ToLong(&val) && | |
1214 | (val >= long(zoom))) | |
1215 | { | |
1216 | m_zoomControl->SetSelection(n); | |
1217 | return; | |
1218 | } | |
1219 | } | |
7e548f6b | 1220 | |
963907fa RR |
1221 | m_zoomControl->SetSelection(count-1); |
1222 | } | |
c801d85f KB |
1223 | } |
1224 | ||
34da0970 | 1225 | int wxPreviewControlBar::GetZoomControl() |
c801d85f | 1226 | { |
963907fa | 1227 | if (m_zoomControl && (m_zoomControl->GetStringSelection() != wxEmptyString)) |
7bcb11d3 | 1228 | { |
963907fa RR |
1229 | long val; |
1230 | if (m_zoomControl->GetStringSelection().BeforeFirst(wxT('%')).ToLong(&val)) | |
1231 | return int(val); | |
7bcb11d3 | 1232 | } |
7e548f6b | 1233 | |
963907fa | 1234 | return 0; |
c801d85f KB |
1235 | } |
1236 | ||
1237 | ||
1238 | /* | |
7bcb11d3 JS |
1239 | * Preview frame |
1240 | */ | |
c801d85f | 1241 | |
e81e3883 RR |
1242 | IMPLEMENT_CLASS(wxPreviewFrame, wxFrame) |
1243 | ||
e3065973 | 1244 | BEGIN_EVENT_TABLE(wxPreviewFrame, wxFrame) |
043c6705 | 1245 | EVT_CHAR_HOOK(wxPreviewFrame::OnChar) |
0f90ec93 | 1246 | EVT_CLOSE(wxPreviewFrame::OnCloseWindow) |
e3065973 JS |
1247 | END_EVENT_TABLE() |
1248 | ||
043c6705 VZ |
1249 | void wxPreviewFrame::OnChar(wxKeyEvent &event) |
1250 | { | |
1251 | if ( event.GetKeyCode() == WXK_ESCAPE ) | |
1252 | { | |
1253 | Close(true); | |
1254 | } | |
1255 | else | |
1256 | { | |
1257 | event.Skip(); | |
1258 | } | |
1259 | } | |
1260 | ||
a5ae8241 | 1261 | wxPreviewFrame::wxPreviewFrame(wxPrintPreviewBase *preview, wxWindow *parent, const wxString& title, |
7bcb11d3 | 1262 | const wxPoint& pos, const wxSize& size, long style, const wxString& name): |
7e548f6b | 1263 | wxFrame(parent, wxID_ANY, title, pos, size, style, name) |
c801d85f | 1264 | { |
7bcb11d3 JS |
1265 | m_printPreview = preview; |
1266 | m_controlBar = NULL; | |
1267 | m_previewCanvas = NULL; | |
7c995553 | 1268 | m_windowDisabler = NULL; |
46b24ef9 | 1269 | |
a5ae8241 | 1270 | // Give the application icon |
46b24ef9 JS |
1271 | #ifdef __WXMSW__ |
1272 | wxFrame* topFrame = wxDynamicCast(wxTheApp->GetTopWindow(), wxFrame); | |
1273 | if (topFrame) | |
7ccf316e | 1274 | SetIcons(topFrame->GetIcons()); |
e71fd398 | 1275 | #endif |
c801d85f KB |
1276 | } |
1277 | ||
34da0970 | 1278 | wxPreviewFrame::~wxPreviewFrame() |
c801d85f KB |
1279 | { |
1280 | } | |
1281 | ||
74e3313b | 1282 | void wxPreviewFrame::OnCloseWindow(wxCloseEvent& WXUNUSED(event)) |
c801d85f | 1283 | { |
7c995553 VZ |
1284 | if (m_windowDisabler) |
1285 | delete m_windowDisabler; | |
8826f46f | 1286 | |
7bcb11d3 JS |
1287 | // Need to delete the printout and the print preview |
1288 | wxPrintout *printout = m_printPreview->GetPrintout(); | |
1289 | if (printout) | |
1290 | { | |
1291 | delete printout; | |
1292 | m_printPreview->SetPrintout(NULL); | |
1293 | m_printPreview->SetCanvas(NULL); | |
1294 | m_printPreview->SetFrame(NULL); | |
1295 | } | |
b88bf073 VS |
1296 | |
1297 | m_previewCanvas->SetPreview(NULL); | |
1298 | wxDELETE(m_printPreview); | |
8826f46f | 1299 | |
7bcb11d3 | 1300 | Destroy(); |
c801d85f KB |
1301 | } |
1302 | ||
34da0970 | 1303 | void wxPreviewFrame::Initialize() |
c801d85f | 1304 | { |
3080bf59 | 1305 | #if wxUSE_STATUSBAR |
7bcb11d3 | 1306 | CreateStatusBar(); |
3080bf59 | 1307 | #endif |
7bcb11d3 JS |
1308 | CreateCanvas(); |
1309 | CreateControlBar(); | |
8826f46f | 1310 | |
7bcb11d3 JS |
1311 | m_printPreview->SetCanvas(m_previewCanvas); |
1312 | m_printPreview->SetFrame(this); | |
8826f46f | 1313 | |
9dff8515 | 1314 | wxBoxSizer *item0 = new wxBoxSizer( wxVERTICAL ); |
8826f46f | 1315 | |
9dff8515 JS |
1316 | item0->Add( m_controlBar, 0, wxGROW|wxALIGN_CENTER_VERTICAL, 5 ); |
1317 | item0->Add( m_previewCanvas, 1, wxGROW|wxALIGN_CENTER_VERTICAL, 5 ); | |
8826f46f | 1318 | |
7e548f6b | 1319 | SetAutoLayout( true ); |
9dff8515 | 1320 | SetSizer( item0 ); |
8826f46f | 1321 | |
7c995553 | 1322 | m_windowDisabler = new wxWindowDisabler(this); |
8826f46f | 1323 | |
7bcb11d3 | 1324 | Layout(); |
e71fd398 | 1325 | |
d2b354f9 JS |
1326 | m_printPreview->AdjustScrollbars(m_previewCanvas); |
1327 | m_previewCanvas->SetFocus(); | |
1328 | m_controlBar->SetFocus(); | |
c801d85f KB |
1329 | } |
1330 | ||
34da0970 | 1331 | void wxPreviewFrame::CreateCanvas() |
c801d85f | 1332 | { |
7bcb11d3 | 1333 | m_previewCanvas = new wxPreviewCanvas(m_printPreview, this); |
c801d85f KB |
1334 | } |
1335 | ||
34da0970 | 1336 | void wxPreviewFrame::CreateControlBar() |
c801d85f | 1337 | { |
7bcb11d3 JS |
1338 | long buttons = wxPREVIEW_DEFAULT; |
1339 | if (m_printPreview->GetPrintoutForPrinting()) | |
1340 | buttons |= wxPREVIEW_PRINT; | |
8826f46f | 1341 | |
c47addef | 1342 | m_controlBar = new wxPreviewControlBar(m_printPreview, buttons, this, wxPoint(0,0), wxSize(400, 40)); |
7bcb11d3 | 1343 | m_controlBar->CreateButtons(); |
c801d85f | 1344 | } |
7bcb11d3 | 1345 | |
c801d85f | 1346 | /* |
7bcb11d3 JS |
1347 | * Print preview |
1348 | */ | |
c801d85f | 1349 | |
891daf8c RR |
1350 | IMPLEMENT_CLASS(wxPrintPreviewBase, wxObject) |
1351 | ||
8826f46f VZ |
1352 | wxPrintPreviewBase::wxPrintPreviewBase(wxPrintout *printout, |
1353 | wxPrintout *printoutForPrinting, | |
1354 | wxPrintData *data) | |
1355 | { | |
1356 | if (data) | |
1357 | m_printDialogData = (*data); | |
1358 | ||
1359 | Init(printout, printoutForPrinting); | |
1360 | } | |
1361 | ||
1362 | wxPrintPreviewBase::wxPrintPreviewBase(wxPrintout *printout, | |
1363 | wxPrintout *printoutForPrinting, | |
1364 | wxPrintDialogData *data) | |
1365 | { | |
1366 | if (data) | |
1367 | m_printDialogData = (*data); | |
1368 | ||
1369 | Init(printout, printoutForPrinting); | |
1370 | } | |
1371 | ||
1372 | void wxPrintPreviewBase::Init(wxPrintout *printout, | |
1373 | wxPrintout *printoutForPrinting) | |
c801d85f | 1374 | { |
7e548f6b | 1375 | m_isOk = true; |
7bcb11d3 JS |
1376 | m_previewPrintout = printout; |
1377 | if (m_previewPrintout) | |
1bd122dd | 1378 | m_previewPrintout->SetPreview(static_cast<wxPrintPreview *>(this)); |
8826f46f | 1379 | |
7bcb11d3 | 1380 | m_printPrintout = printoutForPrinting; |
8826f46f | 1381 | |
7bcb11d3 JS |
1382 | m_previewCanvas = NULL; |
1383 | m_previewFrame = NULL; | |
1384 | m_previewBitmap = NULL; | |
88b98787 | 1385 | m_previewFailed = false; |
7bcb11d3 | 1386 | m_currentPage = 1; |
c25ccf85 | 1387 | m_currentZoom = 70; |
7bcb11d3 JS |
1388 | m_topMargin = 40; |
1389 | m_leftMargin = 40; | |
1390 | m_pageWidth = 0; | |
1391 | m_pageHeight = 0; | |
7e548f6b | 1392 | m_printingPrepared = false; |
d2b354f9 JS |
1393 | m_minPage = 1; |
1394 | m_maxPage = 1; | |
c801d85f KB |
1395 | } |
1396 | ||
34da0970 | 1397 | wxPrintPreviewBase::~wxPrintPreviewBase() |
c801d85f | 1398 | { |
7bcb11d3 JS |
1399 | if (m_previewPrintout) |
1400 | delete m_previewPrintout; | |
1401 | if (m_previewBitmap) | |
1402 | delete m_previewBitmap; | |
1403 | if (m_printPrintout) | |
1404 | delete m_printPrintout; | |
c801d85f KB |
1405 | } |
1406 | ||
1407 | bool wxPrintPreviewBase::SetCurrentPage(int pageNum) | |
1408 | { | |
7bcb11d3 | 1409 | if (m_currentPage == pageNum) |
7e548f6b | 1410 | return true; |
8826f46f | 1411 | |
7bcb11d3 | 1412 | m_currentPage = pageNum; |
88b98787 VS |
1413 | |
1414 | InvalidatePreviewBitmap(); | |
e71fd398 | 1415 | |
7bcb11d3 JS |
1416 | if (m_previewCanvas) |
1417 | { | |
d2b354f9 | 1418 | AdjustScrollbars(m_previewCanvas); |
e71fd398 | 1419 | |
7bcb11d3 | 1420 | m_previewCanvas->Refresh(); |
d2b354f9 | 1421 | m_previewCanvas->SetFocus(); |
7bcb11d3 | 1422 | } |
7e548f6b | 1423 | return true; |
c801d85f KB |
1424 | } |
1425 | ||
383f6abd | 1426 | int wxPrintPreviewBase::GetCurrentPage() const |
259c43f6 | 1427 | { return m_currentPage; } |
383f6abd | 1428 | void wxPrintPreviewBase::SetPrintout(wxPrintout *printout) |
259c43f6 | 1429 | { m_previewPrintout = printout; } |
383f6abd | 1430 | wxPrintout *wxPrintPreviewBase::GetPrintout() const |
259c43f6 | 1431 | { return m_previewPrintout; } |
383f6abd | 1432 | wxPrintout *wxPrintPreviewBase::GetPrintoutForPrinting() const |
259c43f6 | 1433 | { return m_printPrintout; } |
383f6abd | 1434 | void wxPrintPreviewBase::SetFrame(wxFrame *frame) |
259c43f6 | 1435 | { m_previewFrame = frame; } |
383f6abd | 1436 | void wxPrintPreviewBase::SetCanvas(wxPreviewCanvas *canvas) |
259c43f6 | 1437 | { m_previewCanvas = canvas; } |
383f6abd | 1438 | wxFrame *wxPrintPreviewBase::GetFrame() const |
e81e3883 | 1439 | { return m_previewFrame; } |
383f6abd | 1440 | wxPreviewCanvas *wxPrintPreviewBase::GetCanvas() const |
e81e3883 RR |
1441 | { return m_previewCanvas; } |
1442 | ||
f415cab9 JS |
1443 | void wxPrintPreviewBase::CalcRects(wxPreviewCanvas *canvas, wxRect& pageRect, wxRect& paperRect) |
1444 | { | |
1445 | // Calculate the rectangles for the printable area of the page and the | |
1446 | // entire paper as they appear on the canvas on-screen. | |
1447 | int canvasWidth, canvasHeight; | |
1448 | canvas->GetSize(&canvasWidth, &canvasHeight); | |
1449 | ||
1450 | float zoomScale = float(m_currentZoom) / 100; | |
1451 | float screenPrintableWidth = zoomScale * m_pageWidth * m_previewScaleX; | |
1452 | float screenPrintableHeight = zoomScale * m_pageHeight * m_previewScaleY; | |
1453 | ||
1454 | wxRect devicePaperRect = m_previewPrintout->GetPaperRectPixels(); | |
1455 | wxCoord devicePrintableWidth, devicePrintableHeight; | |
1456 | m_previewPrintout->GetPageSizePixels(&devicePrintableWidth, &devicePrintableHeight); | |
1457 | float scaleX = screenPrintableWidth / devicePrintableWidth; | |
1458 | float scaleY = screenPrintableHeight / devicePrintableHeight; | |
1459 | paperRect.width = wxCoord(scaleX * devicePaperRect.width); | |
1460 | paperRect.height = wxCoord(scaleY * devicePaperRect.height); | |
1461 | ||
1462 | paperRect.x = wxCoord((canvasWidth - paperRect.width)/ 2.0); | |
1463 | if (paperRect.x < m_leftMargin) | |
1464 | paperRect.x = m_leftMargin; | |
1465 | paperRect.y = wxCoord((canvasHeight - paperRect.height)/ 2.0); | |
1466 | if (paperRect.y < m_topMargin) | |
1467 | paperRect.y = m_topMargin; | |
1468 | ||
1469 | pageRect.x = paperRect.x - wxCoord(scaleX * devicePaperRect.x); | |
1470 | pageRect.y = paperRect.y - wxCoord(scaleY * devicePaperRect.y); | |
1471 | pageRect.width = wxCoord(screenPrintableWidth); | |
1472 | pageRect.height = wxCoord(screenPrintableHeight); | |
1473 | } | |
1474 | ||
1475 | ||
88b98787 VS |
1476 | void wxPrintPreviewBase::InvalidatePreviewBitmap() |
1477 | { | |
1478 | wxDELETE(m_previewBitmap); | |
1479 | // if there was a problem with rendering the preview, try again now | |
1480 | // that it changed in some way (less memory may be needed, for example): | |
1481 | m_previewFailed = false; | |
1482 | } | |
1483 | ||
b88bf073 VS |
1484 | bool wxPrintPreviewBase::UpdatePageRendering() |
1485 | { | |
1486 | if ( m_previewBitmap ) | |
1487 | return false; | |
1488 | ||
88b98787 VS |
1489 | if ( m_previewFailed ) |
1490 | return false; | |
1491 | ||
b88bf073 | 1492 | if ( !RenderPage(m_currentPage) ) |
88b98787 VS |
1493 | { |
1494 | m_previewFailed = true; // don't waste time failing again | |
b88bf073 | 1495 | return false; |
88b98787 | 1496 | } |
b88bf073 VS |
1497 | |
1498 | return true; | |
1499 | } | |
1500 | ||
d2b354f9 | 1501 | bool wxPrintPreviewBase::PaintPage(wxPreviewCanvas *canvas, wxDC& dc) |
c801d85f | 1502 | { |
7bcb11d3 | 1503 | DrawBlankPage(canvas, dc); |
8826f46f | 1504 | |
7bcb11d3 | 1505 | if (!m_previewBitmap) |
7e548f6b | 1506 | return false; |
7bcb11d3 | 1507 | if (!canvas) |
7e548f6b | 1508 | return false; |
8826f46f | 1509 | |
f415cab9 JS |
1510 | wxRect pageRect, paperRect; |
1511 | CalcRects(canvas, pageRect, paperRect); | |
7bcb11d3 JS |
1512 | wxMemoryDC temp_dc; |
1513 | temp_dc.SelectObject(*m_previewBitmap); | |
8826f46f | 1514 | |
03647350 | 1515 | dc.Blit(pageRect.x, pageRect.y, |
f415cab9 | 1516 | m_previewBitmap->GetWidth(), m_previewBitmap->GetHeight(), &temp_dc, 0, 0); |
8826f46f | 1517 | |
7bcb11d3 | 1518 | temp_dc.SelectObject(wxNullBitmap); |
7e548f6b | 1519 | return true; |
c801d85f KB |
1520 | } |
1521 | ||
d2b354f9 JS |
1522 | // Adjusts the scrollbars for the current scale |
1523 | void wxPrintPreviewBase::AdjustScrollbars(wxPreviewCanvas *canvas) | |
1524 | { | |
1525 | if (!canvas) | |
1526 | return ; | |
1527 | ||
f415cab9 JS |
1528 | wxRect pageRect, paperRect; |
1529 | CalcRects(canvas, pageRect, paperRect); | |
1530 | int totalWidth = paperRect.width + 2 * m_leftMargin; | |
1531 | int totalHeight = paperRect.height + 2 * m_topMargin; | |
1532 | int scrollUnitsX = totalWidth / 10; | |
1533 | int scrollUnitsY = totalHeight / 10; | |
d2b354f9 JS |
1534 | wxSize virtualSize = canvas->GetVirtualSize(); |
1535 | if (virtualSize.GetWidth() != totalWidth || virtualSize.GetHeight() != totalHeight) | |
7e548f6b | 1536 | canvas->SetScrollbars(10, 10, scrollUnitsX, scrollUnitsY, 0, 0, true); |
d2b354f9 JS |
1537 | } |
1538 | ||
25a3fca2 VS |
1539 | bool wxPrintPreviewBase::RenderPageIntoDC(wxDC& dc, int pageNum) |
1540 | { | |
1541 | m_previewPrintout->SetDC(&dc); | |
1542 | m_previewPrintout->SetPageSizePixels(m_pageWidth, m_pageHeight); | |
1543 | ||
1544 | // Need to delay OnPreparePrinting() until here, so we have enough | |
1545 | // information. | |
1546 | if (!m_printingPrepared) | |
1547 | { | |
1548 | m_previewPrintout->OnPreparePrinting(); | |
1549 | int selFrom, selTo; | |
1550 | m_previewPrintout->GetPageInfo(&m_minPage, &m_maxPage, &selFrom, &selTo); | |
1551 | m_printingPrepared = true; | |
1552 | } | |
1553 | ||
1554 | m_previewPrintout->OnBeginPrinting(); | |
1555 | ||
1556 | if (!m_previewPrintout->OnBeginDocument(m_printDialogData.GetFromPage(), m_printDialogData.GetToPage())) | |
1557 | { | |
1558 | wxMessageBox(_("Could not start document preview."), _("Print Preview Failure"), wxOK); | |
1559 | return false; | |
1560 | } | |
1561 | ||
1562 | m_previewPrintout->OnPrintPage(pageNum); | |
1563 | m_previewPrintout->OnEndDocument(); | |
1564 | m_previewPrintout->OnEndPrinting(); | |
1565 | ||
1566 | m_previewPrintout->SetDC(NULL); | |
1567 | ||
1568 | return true; | |
1569 | } | |
1570 | ||
1571 | bool wxPrintPreviewBase::RenderPageIntoBitmap(wxBitmap& bmp, int pageNum) | |
1572 | { | |
1573 | wxMemoryDC memoryDC; | |
1574 | memoryDC.SelectObject(bmp); | |
1575 | memoryDC.Clear(); | |
1576 | ||
1577 | return RenderPageIntoDC(memoryDC, pageNum); | |
1578 | } | |
1579 | ||
c801d85f KB |
1580 | bool wxPrintPreviewBase::RenderPage(int pageNum) |
1581 | { | |
f6bcfd97 BP |
1582 | wxBusyCursor busy; |
1583 | ||
7bcb11d3 | 1584 | if (!m_previewCanvas) |
c801d85f | 1585 | { |
9a83f860 | 1586 | wxFAIL_MSG(wxT("wxPrintPreviewBase::RenderPage: must use wxPrintPreviewBase::SetCanvas to let me know about the canvas!")); |
7e548f6b | 1587 | return false; |
c801d85f | 1588 | } |
8826f46f | 1589 | |
f415cab9 JS |
1590 | wxRect pageRect, paperRect; |
1591 | CalcRects(m_previewCanvas, pageRect, paperRect); | |
8826f46f | 1592 | |
7bcb11d3 JS |
1593 | if (!m_previewBitmap) |
1594 | { | |
f415cab9 JS |
1595 | m_previewBitmap = new wxBitmap(pageRect.width, pageRect.height); |
1596 | ||
7bcb11d3 JS |
1597 | if (!m_previewBitmap || !m_previewBitmap->Ok()) |
1598 | { | |
88b98787 | 1599 | InvalidatePreviewBitmap(); |
7bcb11d3 | 1600 | wxMessageBox(_("Sorry, not enough memory to create a preview."), _("Print Preview Failure"), wxOK); |
7e548f6b | 1601 | return false; |
7bcb11d3 JS |
1602 | } |
1603 | } | |
8826f46f | 1604 | |
25a3fca2 | 1605 | if ( !RenderPageIntoBitmap(*m_previewBitmap, pageNum) ) |
7bcb11d3 | 1606 | { |
88b98787 | 1607 | InvalidatePreviewBitmap(); |
25a3fca2 | 1608 | wxMessageBox(_("Sorry, not enough memory to create a preview."), _("Print Preview Failure"), wxOK); |
7e548f6b | 1609 | return false; |
7bcb11d3 | 1610 | } |
8826f46f | 1611 | |
3080bf59 VZ |
1612 | #if wxUSE_STATUSBAR |
1613 | wxString status; | |
7bcb11d3 | 1614 | if (m_maxPage != 0) |
7e548f6b | 1615 | status = wxString::Format(_("Page %d of %d"), pageNum, m_maxPage); |
7bcb11d3 | 1616 | else |
7e548f6b | 1617 | status = wxString::Format(_("Page %d"), pageNum); |
8826f46f | 1618 | |
7bcb11d3 | 1619 | if (m_previewFrame) |
3080bf59 VZ |
1620 | m_previewFrame->SetStatusText(status); |
1621 | #endif | |
8826f46f | 1622 | |
7e548f6b | 1623 | return true; |
c801d85f KB |
1624 | } |
1625 | ||
d2b354f9 | 1626 | bool wxPrintPreviewBase::DrawBlankPage(wxPreviewCanvas *canvas, wxDC& dc) |
c801d85f | 1627 | { |
f415cab9 | 1628 | wxRect pageRect, paperRect; |
8826f46f | 1629 | |
f415cab9 | 1630 | CalcRects(canvas, pageRect, paperRect); |
8826f46f | 1631 | |
f415cab9 JS |
1632 | // Draw shadow, allowing for 1-pixel border AROUND the actual paper |
1633 | wxCoord shadowOffset = 4; | |
8826f46f | 1634 | |
7bcb11d3 JS |
1635 | dc.SetPen(*wxBLACK_PEN); |
1636 | dc.SetBrush(*wxBLACK_BRUSH); | |
f415cab9 JS |
1637 | dc.DrawRectangle(paperRect.x + shadowOffset, paperRect.y + paperRect.height + 1, |
1638 | paperRect.width, shadowOffset); | |
1639 | ||
1640 | dc.DrawRectangle(paperRect.x + paperRect.width, paperRect.y + shadowOffset, | |
1641 | shadowOffset, paperRect.height); | |
8826f46f | 1642 | |
f415cab9 | 1643 | // Draw blank page allowing for 1-pixel border AROUND the actual paper |
7bcb11d3 JS |
1644 | dc.SetPen(*wxBLACK_PEN); |
1645 | dc.SetBrush(*wxWHITE_BRUSH); | |
03647350 | 1646 | dc.DrawRectangle(paperRect.x - 2, paperRect.y - 1, |
f415cab9 | 1647 | paperRect.width + 3, paperRect.height + 2); |
8826f46f | 1648 | |
7e548f6b | 1649 | return true; |
2a47d3c1 JS |
1650 | } |
1651 | ||
7bcb11d3 | 1652 | void wxPrintPreviewBase::SetZoom(int percent) |
2a47d3c1 | 1653 | { |
7bcb11d3 JS |
1654 | if (m_currentZoom == percent) |
1655 | return; | |
8826f46f | 1656 | |
7bcb11d3 | 1657 | m_currentZoom = percent; |
88b98787 VS |
1658 | |
1659 | InvalidatePreviewBitmap(); | |
aff37a19 | 1660 | |
7bcb11d3 JS |
1661 | if (m_previewCanvas) |
1662 | { | |
d2b354f9 | 1663 | AdjustScrollbars(m_previewCanvas); |
95724b1a | 1664 | ((wxScrolledWindow *) m_previewCanvas)->Scroll(0, 0); |
e71fd398 | 1665 | m_previewCanvas->ClearBackground(); |
7bcb11d3 | 1666 | m_previewCanvas->Refresh(); |
d2b354f9 | 1667 | m_previewCanvas->SetFocus(); |
7bcb11d3 | 1668 | } |
2a47d3c1 JS |
1669 | } |
1670 | ||
383f6abd WS |
1671 | wxPrintDialogData& wxPrintPreviewBase::GetPrintDialogData() |
1672 | { | |
e81e3883 RR |
1673 | return m_printDialogData; |
1674 | } | |
1675 | ||
383f6abd | 1676 | int wxPrintPreviewBase::GetZoom() const |
e81e3883 | 1677 | { return m_currentZoom; } |
383f6abd | 1678 | int wxPrintPreviewBase::GetMaxPage() const |
e81e3883 | 1679 | { return m_maxPage; } |
383f6abd | 1680 | int wxPrintPreviewBase::GetMinPage() const |
e81e3883 | 1681 | { return m_minPage; } |
b7cacb43 | 1682 | bool wxPrintPreviewBase::IsOk() const |
e81e3883 | 1683 | { return m_isOk; } |
383f6abd | 1684 | void wxPrintPreviewBase::SetOk(bool ok) |
e81e3883 | 1685 | { m_isOk = ok; } |
f415cab9 | 1686 | |
e81e3883 RR |
1687 | //---------------------------------------------------------------------------- |
1688 | // wxPrintPreview | |
1689 | //---------------------------------------------------------------------------- | |
1690 | ||
1691 | IMPLEMENT_CLASS(wxPrintPreview, wxPrintPreviewBase) | |
1692 | ||
1693 | wxPrintPreview::wxPrintPreview(wxPrintout *printout, | |
1694 | wxPrintout *printoutForPrinting, | |
1695 | wxPrintDialogData *data) : | |
1696 | wxPrintPreviewBase( printout, printoutForPrinting, data ) | |
1697 | { | |
1698 | m_pimpl = wxPrintFactory::GetFactory()-> | |
1699 | CreatePrintPreview( printout, printoutForPrinting, data ); | |
1700 | } | |
1701 | ||
1702 | wxPrintPreview::wxPrintPreview(wxPrintout *printout, | |
1703 | wxPrintout *printoutForPrinting, | |
1704 | wxPrintData *data ) : | |
1705 | wxPrintPreviewBase( printout, printoutForPrinting, data ) | |
1706 | { | |
1707 | m_pimpl = wxPrintFactory::GetFactory()-> | |
1708 | CreatePrintPreview( printout, printoutForPrinting, data ); | |
1709 | } | |
1710 | ||
1711 | wxPrintPreview::~wxPrintPreview() | |
1712 | { | |
1713 | delete m_pimpl; | |
383f6abd | 1714 | |
e81e3883 RR |
1715 | // don't delete twice |
1716 | m_printPrintout = NULL; | |
1717 | m_previewPrintout = NULL; | |
1718 | m_previewBitmap = NULL; | |
1719 | } | |
1720 | ||
1721 | bool wxPrintPreview::SetCurrentPage(int pageNum) | |
1722 | { | |
1723 | return m_pimpl->SetCurrentPage( pageNum ); | |
1724 | } | |
1725 | ||
383f6abd WS |
1726 | int wxPrintPreview::GetCurrentPage() const |
1727 | { | |
e81e3883 RR |
1728 | return m_pimpl->GetCurrentPage(); |
1729 | } | |
1730 | ||
383f6abd WS |
1731 | void wxPrintPreview::SetPrintout(wxPrintout *printout) |
1732 | { | |
e81e3883 RR |
1733 | m_pimpl->SetPrintout( printout ); |
1734 | } | |
1735 | ||
383f6abd WS |
1736 | wxPrintout *wxPrintPreview::GetPrintout() const |
1737 | { | |
e81e3883 RR |
1738 | return m_pimpl->GetPrintout(); |
1739 | } | |
1740 | ||
383f6abd WS |
1741 | wxPrintout *wxPrintPreview::GetPrintoutForPrinting() const |
1742 | { | |
e81e3883 RR |
1743 | return m_pimpl->GetPrintoutForPrinting(); |
1744 | } | |
1745 | ||
383f6abd WS |
1746 | void wxPrintPreview::SetFrame(wxFrame *frame) |
1747 | { | |
e81e3883 RR |
1748 | m_pimpl->SetFrame( frame ); |
1749 | } | |
1750 | ||
383f6abd WS |
1751 | void wxPrintPreview::SetCanvas(wxPreviewCanvas *canvas) |
1752 | { | |
e81e3883 RR |
1753 | m_pimpl->SetCanvas( canvas ); |
1754 | } | |
1755 | ||
383f6abd | 1756 | wxFrame *wxPrintPreview::GetFrame() const |
e81e3883 RR |
1757 | { |
1758 | return m_pimpl->GetFrame(); | |
1759 | } | |
1760 | ||
383f6abd WS |
1761 | wxPreviewCanvas *wxPrintPreview::GetCanvas() const |
1762 | { | |
e81e3883 RR |
1763 | return m_pimpl->GetCanvas(); |
1764 | } | |
1765 | ||
1766 | bool wxPrintPreview::PaintPage(wxPreviewCanvas *canvas, wxDC& dc) | |
1767 | { | |
1768 | return m_pimpl->PaintPage( canvas, dc ); | |
1769 | } | |
1770 | ||
b88bf073 VS |
1771 | bool wxPrintPreview::UpdatePageRendering() |
1772 | { | |
1773 | return m_pimpl->UpdatePageRendering(); | |
1774 | } | |
1775 | ||
e81e3883 RR |
1776 | bool wxPrintPreview::DrawBlankPage(wxPreviewCanvas *canvas, wxDC& dc) |
1777 | { | |
1778 | return m_pimpl->DrawBlankPage( canvas, dc ); | |
1779 | } | |
1780 | ||
1781 | void wxPrintPreview::AdjustScrollbars(wxPreviewCanvas *canvas) | |
1782 | { | |
1783 | m_pimpl->AdjustScrollbars( canvas ); | |
1784 | } | |
1785 | ||
1786 | bool wxPrintPreview::RenderPage(int pageNum) | |
1787 | { | |
1788 | return m_pimpl->RenderPage( pageNum ); | |
1789 | } | |
1790 | ||
1791 | void wxPrintPreview::SetZoom(int percent) | |
1792 | { | |
1793 | m_pimpl->SetZoom( percent ); | |
1794 | } | |
1795 | ||
d8766675 JS |
1796 | int wxPrintPreview::GetZoom() const |
1797 | { | |
1798 | return m_pimpl->GetZoom(); | |
1799 | } | |
1800 | ||
e81e3883 RR |
1801 | wxPrintDialogData& wxPrintPreview::GetPrintDialogData() |
1802 | { | |
1803 | return m_pimpl->GetPrintDialogData(); | |
1804 | } | |
1805 | ||
1806 | int wxPrintPreview::GetMaxPage() const | |
1807 | { | |
1808 | return m_pimpl->GetMaxPage(); | |
1809 | } | |
1810 | ||
1811 | int wxPrintPreview::GetMinPage() const | |
1812 | { | |
1813 | return m_pimpl->GetMinPage(); | |
1814 | } | |
1815 | ||
b7cacb43 | 1816 | bool wxPrintPreview::IsOk() const |
e81e3883 RR |
1817 | { |
1818 | return m_pimpl->Ok(); | |
1819 | } | |
1820 | ||
1821 | void wxPrintPreview::SetOk(bool ok) | |
1822 | { | |
1823 | m_pimpl->SetOk( ok ); | |
1824 | } | |
1825 | ||
1826 | bool wxPrintPreview::Print(bool interactive) | |
1827 | { | |
1828 | return m_pimpl->Print( interactive ); | |
1829 | } | |
1830 | ||
1831 | void wxPrintPreview::DetermineScaling() | |
1832 | { | |
1833 | m_pimpl->DetermineScaling(); | |
1834 | } | |
1835 | ||
d427503c | 1836 | #endif // wxUSE_PRINTING_ARCHITECTURE |