]> git.saurik.com Git - wxWidgets.git/blob - src/common/prntbase.cpp
Applied #15375 to stop event-sending in generic wxSpinCtrl ctor (eco)
[wxWidgets.git] / src / common / prntbase.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/prntbase.cpp
3 // Purpose: Printing framework base class implementation
4 // Author: Julian Smart
5 // Modified by:
6 // Created: 04/01/98
7 // Copyright: (c) Julian Smart
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
10
11 // For compilers that support precompilation, includes "wx.h".
12 #include "wx/wxprec.h"
13
14 #ifdef __BORLANDC__
15 #pragma hdrstop
16 #endif
17
18 #if wxUSE_PRINTING_ARCHITECTURE
19
20 #include "wx/dcprint.h"
21
22 #ifndef WX_PRECOMP
23 #if defined(__WXMSW__)
24 #include "wx/msw/wrapcdlg.h"
25 #endif // MSW
26 #include "wx/utils.h"
27 #include "wx/dc.h"
28 #include "wx/app.h"
29 #include "wx/math.h"
30 #include "wx/msgdlg.h"
31 #include "wx/layout.h"
32 #include "wx/choice.h"
33 #include "wx/button.h"
34 #include "wx/bmpbuttn.h"
35 #include "wx/settings.h"
36 #include "wx/dcmemory.h"
37 #include "wx/dcclient.h"
38 #include "wx/stattext.h"
39 #include "wx/intl.h"
40 #include "wx/textdlg.h"
41 #include "wx/sizer.h"
42 #include "wx/module.h"
43 #endif // !WX_PRECOMP
44
45 #include "wx/prntbase.h"
46 #include "wx/printdlg.h"
47 #include "wx/print.h"
48 #include "wx/dcprint.h"
49 #include "wx/artprov.h"
50
51 #include <stdlib.h>
52 #include <string.h>
53
54 #if defined(__WXMSW__) && !defined(__WXUNIVERSAL__)
55 #include "wx/msw/printdlg.h"
56 #include "wx/msw/dcprint.h"
57 #elif defined(__WXMAC__)
58 #include "wx/osx/printdlg.h"
59 #include "wx/osx/private/print.h"
60 #include "wx/osx/dcprint.h"
61 #elif defined(__WXPM__)
62 #include "wx/os2/dcprint.h"
63 #include "wx/generic/prntdlgg.h"
64 #else
65 #include "wx/generic/prntdlgg.h"
66 #include "wx/dcps.h"
67 #endif
68
69 #ifdef __WXMSW__
70 #ifndef __WIN32__
71 #include <print.h>
72 #endif
73 #endif // __WXMSW__
74
75 //----------------------------------------------------------------------------
76 // wxPrintFactory
77 //----------------------------------------------------------------------------
78
79 wxPrintFactory *wxPrintFactory::m_factory = NULL;
80
81 void wxPrintFactory::SetPrintFactory( wxPrintFactory *factory )
82 {
83 if (wxPrintFactory::m_factory)
84 delete wxPrintFactory::m_factory;
85
86 wxPrintFactory::m_factory = factory;
87 }
88
89 wxPrintFactory *wxPrintFactory::GetFactory()
90 {
91 if (!wxPrintFactory::m_factory)
92 wxPrintFactory::m_factory = new wxNativePrintFactory;
93
94 return wxPrintFactory::m_factory;
95 }
96
97 //----------------------------------------------------------------------------
98 // wxNativePrintFactory
99 //----------------------------------------------------------------------------
100
101 wxPrinterBase *wxNativePrintFactory::CreatePrinter( wxPrintDialogData *data )
102 {
103 #if defined(__WXMSW__) && !defined(__WXUNIVERSAL__)
104 return new wxWindowsPrinter( data );
105 #elif defined(__WXMAC__)
106 return new wxMacPrinter( data );
107 #elif defined(__WXPM__)
108 return new wxOS2Printer( data );
109 #else
110 return new wxPostScriptPrinter( data );
111 #endif
112 }
113
114 wxPrintPreviewBase *wxNativePrintFactory::CreatePrintPreview( wxPrintout *preview,
115 wxPrintout *printout, wxPrintDialogData *data )
116 {
117 #if defined(__WXMSW__) && !defined(__WXUNIVERSAL__)
118 return new wxWindowsPrintPreview( preview, printout, data );
119 #elif defined(__WXMAC__)
120 return new wxMacPrintPreview( preview, printout, data );
121 #elif defined(__WXPM__)
122 return new wxOS2PrintPreview( preview, printout, data );
123 #else
124 return new wxPostScriptPrintPreview( preview, printout, data );
125 #endif
126 }
127
128 wxPrintPreviewBase *wxNativePrintFactory::CreatePrintPreview( wxPrintout *preview,
129 wxPrintout *printout, wxPrintData *data )
130 {
131 #if defined(__WXMSW__) && !defined(__WXUNIVERSAL__)
132 return new wxWindowsPrintPreview( preview, printout, data );
133 #elif defined(__WXMAC__)
134 return new wxMacPrintPreview( preview, printout, data );
135 #elif defined(__WXPM__)
136 return new wxOS2PrintPreview( preview, printout, data );
137 #else
138 return new wxPostScriptPrintPreview( preview, printout, data );
139 #endif
140 }
141
142 wxPrintDialogBase *wxNativePrintFactory::CreatePrintDialog( wxWindow *parent,
143 wxPrintDialogData *data )
144 {
145 #if defined(__WXMSW__) && !defined(__WXUNIVERSAL__)
146 return new wxWindowsPrintDialog( parent, data );
147 #elif defined(__WXMAC__)
148 return new wxMacPrintDialog( parent, data );
149 #else
150 return new wxGenericPrintDialog( parent, data );
151 #endif
152 }
153
154 wxPrintDialogBase *wxNativePrintFactory::CreatePrintDialog( wxWindow *parent,
155 wxPrintData *data )
156 {
157 #if defined(__WXMSW__) && !defined(__WXUNIVERSAL__)
158 return new wxWindowsPrintDialog( parent, data );
159 #elif defined(__WXMAC__)
160 return new wxMacPrintDialog( parent, data );
161 #else
162 return new wxGenericPrintDialog( parent, data );
163 #endif
164 }
165
166 wxPageSetupDialogBase *wxNativePrintFactory::CreatePageSetupDialog( wxWindow *parent,
167 wxPageSetupDialogData *data )
168 {
169 #if defined(__WXMSW__) && !defined(__WXUNIVERSAL__)
170 return new wxWindowsPageSetupDialog( parent, data );
171 #elif defined(__WXMAC__)
172 return new wxMacPageSetupDialog( parent, data );
173 #else
174 return new wxGenericPageSetupDialog( parent, data );
175 #endif
176 }
177
178 bool wxNativePrintFactory::HasPrintSetupDialog()
179 {
180 #if defined(__WXMSW__) && !defined(__WXUNIVERSAL__)
181 return false;
182 #elif defined(__WXMAC__)
183 return false;
184 #else
185 // Only here do we need to provide the print setup
186 // dialog ourselves, the other platforms either have
187 // none, don't make it accessible or let you configure
188 // the printer from the wxPrintDialog anyway.
189 return true;
190 #endif
191
192 }
193
194 wxDialog *wxNativePrintFactory::CreatePrintSetupDialog( wxWindow *parent,
195 wxPrintData *data )
196 {
197 #if defined(__WXMSW__) && !defined(__WXUNIVERSAL__)
198 wxUnusedVar(parent);
199 wxUnusedVar(data);
200 return NULL;
201 #elif defined(__WXMAC__)
202 wxUnusedVar(parent);
203 wxUnusedVar(data);
204 return NULL;
205 #else
206 // Only here do we need to provide the print setup
207 // dialog ourselves, the other platforms either have
208 // none, don't make it accessible or let you configure
209 // the printer from the wxPrintDialog anyway.
210 return new wxGenericPrintSetupDialog( parent, data );
211 #endif
212 }
213
214 wxDCImpl* wxNativePrintFactory::CreatePrinterDCImpl( wxPrinterDC *owner, const wxPrintData& data )
215 {
216 #if defined(__WXGTK__) || defined(__WXMOTIF__) || ( defined(__WXUNIVERSAL__) && !defined(__WXMAC__) )
217 return new wxPostScriptDCImpl( owner, data );
218 #else
219 return new wxPrinterDCImpl( owner, data );
220 #endif
221 }
222
223 bool wxNativePrintFactory::HasOwnPrintToFile()
224 {
225 // Only relevant for PostScript and here the
226 // setup dialog provides no "print to file"
227 // option. In the GNOME setup dialog, the
228 // setup dialog has its own print to file.
229 return false;
230 }
231
232 bool wxNativePrintFactory::HasPrinterLine()
233 {
234 // Only relevant for PostScript for now
235 return true;
236 }
237
238 wxString wxNativePrintFactory::CreatePrinterLine()
239 {
240 // Only relevant for PostScript for now
241
242 // We should query "lpstat -d" here
243 return _("Generic PostScript");
244 }
245
246 bool wxNativePrintFactory::HasStatusLine()
247 {
248 // Only relevant for PostScript for now
249 return true;
250 }
251
252 wxString wxNativePrintFactory::CreateStatusLine()
253 {
254 // Only relevant for PostScript for now
255
256 // We should query "lpstat -r" or "lpstat -p" here
257 return _("Ready");
258 }
259
260 wxPrintNativeDataBase *wxNativePrintFactory::CreatePrintNativeData()
261 {
262 #if defined(__WXMSW__) && !defined(__WXUNIVERSAL__)
263 return new wxWindowsPrintNativeData;
264 #elif defined(__WXMAC__)
265 return wxOSXCreatePrintData();
266 #else
267 return new wxPostScriptPrintNativeData;
268 #endif
269 }
270
271 //----------------------------------------------------------------------------
272 // wxPrintNativeDataBase
273 //----------------------------------------------------------------------------
274
275 IMPLEMENT_ABSTRACT_CLASS(wxPrintNativeDataBase, wxObject)
276
277 wxPrintNativeDataBase::wxPrintNativeDataBase()
278 {
279 m_ref = 1;
280 }
281
282 //----------------------------------------------------------------------------
283 // wxPrintFactoryModule
284 //----------------------------------------------------------------------------
285
286 class wxPrintFactoryModule: public wxModule
287 {
288 public:
289 wxPrintFactoryModule() {}
290 bool OnInit() { return true; }
291 void OnExit() { wxPrintFactory::SetPrintFactory( NULL ); }
292
293 private:
294 DECLARE_DYNAMIC_CLASS(wxPrintFactoryModule)
295 };
296
297 IMPLEMENT_DYNAMIC_CLASS(wxPrintFactoryModule, wxModule)
298
299 //----------------------------------------------------------------------------
300 // wxPrinterBase
301 //----------------------------------------------------------------------------
302
303 IMPLEMENT_CLASS(wxPrinterBase, wxObject)
304
305 wxPrinterBase::wxPrinterBase(wxPrintDialogData *data)
306 {
307 m_currentPrintout = NULL;
308 sm_abortWindow = NULL;
309 sm_abortIt = false;
310 if (data)
311 m_printDialogData = (*data);
312 sm_lastError = wxPRINTER_NO_ERROR;
313 }
314
315 wxWindow *wxPrinterBase::sm_abortWindow = NULL;
316 bool wxPrinterBase::sm_abortIt = false;
317 wxPrinterError wxPrinterBase::sm_lastError = wxPRINTER_NO_ERROR;
318
319 wxPrinterBase::~wxPrinterBase()
320 {
321 }
322
323 wxPrintAbortDialog *wxPrinterBase::CreateAbortWindow(wxWindow *parent, wxPrintout * printout)
324 {
325 return new wxPrintAbortDialog(parent, printout->GetTitle());
326 }
327
328 void wxPrinterBase::ReportError(wxWindow *parent, wxPrintout *WXUNUSED(printout), const wxString& message)
329 {
330 wxMessageBox(message, _("Printing Error"), wxOK, parent);
331 }
332
333 wxPrintDialogData& wxPrinterBase::GetPrintDialogData() const
334 {
335 return (wxPrintDialogData&) m_printDialogData;
336 }
337
338 //----------------------------------------------------------------------------
339 // wxPrinter
340 //----------------------------------------------------------------------------
341
342 IMPLEMENT_CLASS(wxPrinter, wxPrinterBase)
343
344 wxPrinter::wxPrinter(wxPrintDialogData *data)
345 {
346 m_pimpl = wxPrintFactory::GetFactory()->CreatePrinter( data );
347 }
348
349 wxPrinter::~wxPrinter()
350 {
351 delete m_pimpl;
352 }
353
354 wxPrintAbortDialog *wxPrinter::CreateAbortWindow(wxWindow *parent, wxPrintout *printout)
355 {
356 return m_pimpl->CreateAbortWindow( parent, printout );
357 }
358
359 void wxPrinter::ReportError(wxWindow *parent, wxPrintout *printout, const wxString& message)
360 {
361 m_pimpl->ReportError( parent, printout, message );
362 }
363
364 bool wxPrinter::Setup(wxWindow *parent)
365 {
366 return m_pimpl->Setup( parent );
367 }
368
369 bool wxPrinter::Print(wxWindow *parent, wxPrintout *printout, bool prompt)
370 {
371 if ( !prompt && m_printDialogData.GetToPage() == 0 )
372 {
373 // If the dialog is not shown, set the pages range to print everything
374 // by default (as otherwise we wouldn't print anything at all which is
375 // certainly not a reasonable default behaviour).
376 int minPage, maxPage, selFrom, selTo;
377 printout->GetPageInfo(&minPage, &maxPage, &selFrom, &selTo);
378
379 wxPrintDialogData& pdd = m_pimpl->GetPrintDialogData();
380 pdd.SetFromPage(minPage);
381 pdd.SetToPage(maxPage);
382 }
383
384 return m_pimpl->Print( parent, printout, prompt );
385 }
386
387 wxDC* wxPrinter::PrintDialog(wxWindow *parent)
388 {
389 return m_pimpl->PrintDialog( parent );
390 }
391
392 wxPrintDialogData& wxPrinter::GetPrintDialogData() const
393 {
394 return m_pimpl->GetPrintDialogData();
395 }
396
397 // ---------------------------------------------------------------------------
398 // wxPrintDialogBase: the dialog for printing.
399 // ---------------------------------------------------------------------------
400
401 IMPLEMENT_ABSTRACT_CLASS(wxPrintDialogBase, wxDialog)
402
403 wxPrintDialogBase::wxPrintDialogBase(wxWindow *parent,
404 wxWindowID id,
405 const wxString &title,
406 const wxPoint &pos,
407 const wxSize &size,
408 long style)
409 : wxDialog( parent, id, title.empty() ? wxString(_("Print")) : title,
410 pos, size, style )
411 {
412 }
413
414 // ---------------------------------------------------------------------------
415 // wxPrintDialog: the dialog for printing
416 // ---------------------------------------------------------------------------
417
418 IMPLEMENT_CLASS(wxPrintDialog, wxObject)
419
420 wxPrintDialog::wxPrintDialog(wxWindow *parent, wxPrintDialogData* data)
421 {
422 m_pimpl = wxPrintFactory::GetFactory()->CreatePrintDialog( parent, data );
423 }
424
425 wxPrintDialog::wxPrintDialog(wxWindow *parent, wxPrintData* data)
426 {
427 m_pimpl = wxPrintFactory::GetFactory()->CreatePrintDialog( parent, data );
428 }
429
430 wxPrintDialog::~wxPrintDialog()
431 {
432 delete m_pimpl;
433 }
434
435 int wxPrintDialog::ShowModal()
436 {
437 return m_pimpl->ShowModal();
438 }
439
440 wxPrintDialogData& wxPrintDialog::GetPrintDialogData()
441 {
442 return m_pimpl->GetPrintDialogData();
443 }
444
445 wxPrintData& wxPrintDialog::GetPrintData()
446 {
447 return m_pimpl->GetPrintData();
448 }
449
450 wxDC *wxPrintDialog::GetPrintDC()
451 {
452 return m_pimpl->GetPrintDC();
453 }
454
455 // ---------------------------------------------------------------------------
456 // wxPageSetupDialogBase: the page setup dialog
457 // ---------------------------------------------------------------------------
458
459 IMPLEMENT_ABSTRACT_CLASS(wxPageSetupDialogBase, wxDialog)
460
461 wxPageSetupDialogBase::wxPageSetupDialogBase(wxWindow *parent,
462 wxWindowID id,
463 const wxString &title,
464 const wxPoint &pos,
465 const wxSize &size,
466 long style)
467 : wxDialog( parent, id, title.empty() ? wxString(_("Page setup")) : title,
468 pos, size, style )
469 {
470 }
471
472 // ---------------------------------------------------------------------------
473 // wxPageSetupDialog: the page setup dialog
474 // ---------------------------------------------------------------------------
475
476 IMPLEMENT_CLASS(wxPageSetupDialog, wxObject)
477
478 wxPageSetupDialog::wxPageSetupDialog(wxWindow *parent, wxPageSetupDialogData *data )
479 {
480 m_pimpl = wxPrintFactory::GetFactory()->CreatePageSetupDialog( parent, data );
481 }
482
483 wxPageSetupDialog::~wxPageSetupDialog()
484 {
485 delete m_pimpl;
486 }
487
488 int wxPageSetupDialog::ShowModal()
489 {
490 return m_pimpl->ShowModal();
491 }
492
493 wxPageSetupDialogData& wxPageSetupDialog::GetPageSetupDialogData()
494 {
495 return m_pimpl->GetPageSetupDialogData();
496 }
497
498 // old name
499 wxPageSetupDialogData& wxPageSetupDialog::GetPageSetupData()
500 {
501 return m_pimpl->GetPageSetupDialogData();
502 }
503
504 //----------------------------------------------------------------------------
505 // wxPrintAbortDialog
506 //----------------------------------------------------------------------------
507
508 BEGIN_EVENT_TABLE(wxPrintAbortDialog, wxDialog)
509 EVT_BUTTON(wxID_CANCEL, wxPrintAbortDialog::OnCancel)
510 END_EVENT_TABLE()
511
512 wxPrintAbortDialog::wxPrintAbortDialog(wxWindow *parent,
513 const wxString& documentTitle,
514 const wxPoint& pos,
515 const wxSize& size,
516 long style,
517 const wxString& name)
518 : wxDialog(parent, wxID_ANY, _("Printing"), pos, size, style, name)
519 {
520 wxBoxSizer *mainSizer = new wxBoxSizer(wxVERTICAL);
521 mainSizer->Add(new wxStaticText(this, wxID_ANY, _("Please wait while printing...")),
522 wxSizerFlags().Expand().DoubleBorder());
523
524 wxFlexGridSizer *gridSizer = new wxFlexGridSizer(2, wxSize(20, 0));
525 gridSizer->Add(new wxStaticText(this, wxID_ANY, _("Document:")));
526 gridSizer->AddGrowableCol(1);
527 gridSizer->Add(new wxStaticText(this, wxID_ANY, documentTitle));
528 gridSizer->Add(new wxStaticText(this, wxID_ANY, _("Progress:")));
529 m_progress = new wxStaticText(this, wxID_ANY, _("Preparing"));
530 m_progress->SetMinSize(wxSize(250, -1));
531 gridSizer->Add(m_progress);
532 mainSizer->Add(gridSizer, wxSizerFlags().Expand().DoubleBorder(wxLEFT | wxRIGHT));
533
534 mainSizer->Add(CreateStdDialogButtonSizer(wxCANCEL),
535 wxSizerFlags().Expand().DoubleBorder());
536
537 SetSizerAndFit(mainSizer);
538 }
539
540 void wxPrintAbortDialog::SetProgress(int currentPage, int totalPages,
541 int currentCopy, int totalCopies)
542 {
543 wxString text;
544 text.Printf(_("Printing page %d of %d"), currentPage, totalPages);
545 if ( totalCopies > 1 )
546 text += wxString::Format(_(" (copy %d of %d)"), currentCopy, totalCopies);
547 m_progress->SetLabel(text);
548 }
549
550 void wxPrintAbortDialog::OnCancel(wxCommandEvent& WXUNUSED(event))
551 {
552 wxCHECK_RET( wxPrinterBase::sm_abortWindow != NULL, "OnCancel called twice" );
553
554 wxPrinterBase::sm_abortIt = true;
555 wxPrinterBase::sm_abortWindow->Destroy();
556 wxPrinterBase::sm_abortWindow = NULL;
557 }
558
559 //----------------------------------------------------------------------------
560 // wxPrintout
561 //----------------------------------------------------------------------------
562
563 IMPLEMENT_ABSTRACT_CLASS(wxPrintout, wxObject)
564
565 wxPrintout::wxPrintout(const wxString& title)
566 {
567 m_printoutTitle = title ;
568 m_printoutDC = NULL;
569 m_pageWidthMM = 0;
570 m_pageHeightMM = 0;
571 m_pageWidthPixels = 0;
572 m_pageHeightPixels = 0;
573 m_PPIScreenX = 0;
574 m_PPIScreenY = 0;
575 m_PPIPrinterX = 0;
576 m_PPIPrinterY = 0;
577 m_preview = NULL;
578 }
579
580 wxPrintout::~wxPrintout()
581 {
582 }
583
584 bool wxPrintout::OnBeginDocument(int WXUNUSED(startPage), int WXUNUSED(endPage))
585 {
586 return GetDC()->StartDoc(_("Printing ") + m_printoutTitle);
587 }
588
589 void wxPrintout::OnEndDocument()
590 {
591 GetDC()->EndDoc();
592 }
593
594 void wxPrintout::OnBeginPrinting()
595 {
596 }
597
598 void wxPrintout::OnEndPrinting()
599 {
600 }
601
602 bool wxPrintout::HasPage(int page)
603 {
604 return (page == 1);
605 }
606
607 void wxPrintout::GetPageInfo(int *minPage, int *maxPage, int *fromPage, int *toPage)
608 {
609 *minPage = 1;
610 *maxPage = 32000;
611 *fromPage = 1;
612 *toPage = 1;
613 }
614
615 void wxPrintout::FitThisSizeToPaper(const wxSize& imageSize)
616 {
617 // Set the DC scale and origin so that the given image size fits within the
618 // entire page and the origin is at the top left corner of the page. Note
619 // that with most printers, portions of the page will be non-printable. Use
620 // this if you're managing your own page margins.
621 if (!m_printoutDC) return;
622 wxRect paperRect = GetPaperRectPixels();
623 wxCoord pw, ph;
624 GetPageSizePixels(&pw, &ph);
625 wxCoord w, h;
626 m_printoutDC->GetSize(&w, &h);
627 float scaleX = ((float(paperRect.width) * w) / (float(pw) * imageSize.x));
628 float scaleY = ((float(paperRect.height) * h) / (float(ph) * imageSize.y));
629 float actualScale = wxMin(scaleX, scaleY);
630 m_printoutDC->SetUserScale(actualScale, actualScale);
631 m_printoutDC->SetDeviceOrigin(0, 0);
632 wxRect logicalPaperRect = GetLogicalPaperRect();
633 SetLogicalOrigin(logicalPaperRect.x, logicalPaperRect.y);
634 }
635
636 void wxPrintout::FitThisSizeToPage(const wxSize& imageSize)
637 {
638 // Set the DC scale and origin so that the given image size fits within the
639 // printable area of the page and the origin is at the top left corner of
640 // the printable area.
641 if (!m_printoutDC) return;
642 int w, h;
643 m_printoutDC->GetSize(&w, &h);
644 float scaleX = float(w) / imageSize.x;
645 float scaleY = float(h) / imageSize.y;
646 float actualScale = wxMin(scaleX, scaleY);
647 m_printoutDC->SetUserScale(actualScale, actualScale);
648 m_printoutDC->SetDeviceOrigin(0, 0);
649 }
650
651 void wxPrintout::FitThisSizeToPageMargins(const wxSize& imageSize, const wxPageSetupDialogData& pageSetupData)
652 {
653 // Set the DC scale and origin so that the given image size fits within the
654 // page margins defined in the given wxPageSetupDialogData object and the
655 // origin is at the top left corner of the page margins.
656 if (!m_printoutDC) return;
657 wxRect paperRect = GetPaperRectPixels();
658 wxCoord pw, ph;
659 GetPageSizePixels(&pw, &ph);
660 wxPoint topLeft = pageSetupData.GetMarginTopLeft();
661 wxPoint bottomRight = pageSetupData.GetMarginBottomRight();
662 wxCoord mw, mh;
663 GetPageSizeMM(&mw, &mh);
664 float mmToDeviceX = float(pw) / mw;
665 float mmToDeviceY = float(ph) / mh;
666 wxRect pageMarginsRect(paperRect.x + wxRound(mmToDeviceX * topLeft.x),
667 paperRect.y + wxRound(mmToDeviceY * topLeft.y),
668 paperRect.width - wxRound(mmToDeviceX * (topLeft.x + bottomRight.x)),
669 paperRect.height - wxRound(mmToDeviceY * (topLeft.y + bottomRight.y)));
670 wxCoord w, h;
671 m_printoutDC->GetSize(&w, &h);
672 float scaleX = (float(pageMarginsRect.width) * w) / (float(pw) * imageSize.x);
673 float scaleY = (float(pageMarginsRect.height) * h) / (float(ph) * imageSize.y);
674 float actualScale = wxMin(scaleX, scaleY);
675 m_printoutDC->SetUserScale(actualScale, actualScale);
676 m_printoutDC->SetDeviceOrigin(0, 0);
677 wxRect logicalPageMarginsRect = GetLogicalPageMarginsRect(pageSetupData);
678 SetLogicalOrigin(logicalPageMarginsRect.x, logicalPageMarginsRect.y);
679 }
680
681 void wxPrintout::MapScreenSizeToPaper()
682 {
683 // Set the DC scale so that an image on the screen is the same size on the
684 // paper and the origin is at the top left of the paper. Note that with most
685 // printers, portions of the page will be cut off. Use this if you're
686 // managing your own page margins.
687 if (!m_printoutDC) return;
688 MapScreenSizeToPage();
689 wxRect logicalPaperRect = GetLogicalPaperRect();
690 SetLogicalOrigin(logicalPaperRect.x, logicalPaperRect.y);
691 }
692
693 void wxPrintout::MapScreenSizeToPage()
694 {
695 // Set the DC scale and origin so that an image on the screen is the same
696 // size on the paper and the origin is at the top left of the printable area.
697 if (!m_printoutDC) return;
698 int ppiScreenX, ppiScreenY;
699 GetPPIScreen(&ppiScreenX, &ppiScreenY);
700 int ppiPrinterX, ppiPrinterY;
701 GetPPIPrinter(&ppiPrinterX, &ppiPrinterY);
702 int w, h;
703 m_printoutDC->GetSize(&w, &h);
704 int pageSizePixelsX, pageSizePixelsY;
705 GetPageSizePixels(&pageSizePixelsX, &pageSizePixelsY);
706 float userScaleX = (float(ppiPrinterX) * w) / (float(ppiScreenX) * pageSizePixelsX);
707 float userScaleY = (float(ppiPrinterY) * h) / (float(ppiScreenY) * pageSizePixelsY);
708 m_printoutDC->SetUserScale(userScaleX, userScaleY);
709 m_printoutDC->SetDeviceOrigin(0, 0);
710 }
711
712 void wxPrintout::MapScreenSizeToPageMargins(const wxPageSetupDialogData& pageSetupData)
713 {
714 // Set the DC scale so that an image on the screen is the same size on the
715 // paper and the origin is at the top left of the page margins defined by
716 // the given wxPageSetupDialogData object.
717 if (!m_printoutDC) return;
718 MapScreenSizeToPage();
719 wxRect logicalPageMarginsRect = GetLogicalPageMarginsRect(pageSetupData);
720 SetLogicalOrigin(logicalPageMarginsRect.x, logicalPageMarginsRect.y);
721 }
722
723 void wxPrintout::MapScreenSizeToDevice()
724 {
725 // Set the DC scale so that a screen pixel is the same size as a device
726 // pixel and the origin is at the top left of the printable area.
727 if (!m_printoutDC) return;
728 int w, h;
729 m_printoutDC->GetSize(&w, &h);
730 int pageSizePixelsX, pageSizePixelsY;
731 GetPageSizePixels(&pageSizePixelsX, &pageSizePixelsY);
732 float userScaleX = float(w) / pageSizePixelsX;
733 float userScaleY = float(h) / pageSizePixelsY;
734 m_printoutDC->SetUserScale(userScaleX, userScaleY);
735 m_printoutDC->SetDeviceOrigin(0, 0);
736 }
737
738 wxRect wxPrintout::GetLogicalPaperRect() const
739 {
740 // Return the rectangle in logical units that corresponds to the paper
741 // rectangle.
742 wxRect paperRect = GetPaperRectPixels();
743 wxCoord pw, ph;
744 GetPageSizePixels(&pw, &ph);
745 wxCoord w, h;
746 m_printoutDC->GetSize(&w, &h);
747 if (w == pw && h == ph) {
748 // this DC matches the printed page, so no scaling
749 return wxRect(m_printoutDC->DeviceToLogicalX(paperRect.x),
750 m_printoutDC->DeviceToLogicalY(paperRect.y),
751 m_printoutDC->DeviceToLogicalXRel(paperRect.width),
752 m_printoutDC->DeviceToLogicalYRel(paperRect.height));
753 }
754 // This DC doesn't match the printed page, so we have to scale.
755 float scaleX = float(w) / pw;
756 float scaleY = float(h) / ph;
757 return wxRect(m_printoutDC->DeviceToLogicalX(wxRound(paperRect.x * scaleX)),
758 m_printoutDC->DeviceToLogicalY(wxRound(paperRect.y * scaleY)),
759 m_printoutDC->DeviceToLogicalXRel(wxRound(paperRect.width * scaleX)),
760 m_printoutDC->DeviceToLogicalYRel(wxRound(paperRect.height * scaleY)));
761 }
762
763 wxRect wxPrintout::GetLogicalPageRect() const
764 {
765 // Return the rectangle in logical units that corresponds to the printable
766 // area.
767 int w, h;
768 m_printoutDC->GetSize(&w, &h);
769 return wxRect(m_printoutDC->DeviceToLogicalX(0),
770 m_printoutDC->DeviceToLogicalY(0),
771 m_printoutDC->DeviceToLogicalXRel(w),
772 m_printoutDC->DeviceToLogicalYRel(h));
773 }
774
775 wxRect wxPrintout::GetLogicalPageMarginsRect(const wxPageSetupDialogData& pageSetupData) const
776 {
777 // Return the rectangle in logical units that corresponds to the region
778 // within the page margins as specified by the given wxPageSetupDialogData
779 // object.
780
781 // We get the paper size in device units and the margins in mm,
782 // so we need to calculate the conversion with this trick
783 wxCoord pw, ph;
784 GetPageSizePixels(&pw, &ph);
785 wxCoord mw, mh;
786 GetPageSizeMM(&mw, &mh);
787 float mmToDeviceX = float(pw) / mw;
788 float mmToDeviceY = float(ph) / mh;
789
790 // paper size in device units
791 wxRect paperRect = GetPaperRectPixels();
792
793 // margins in mm
794 wxPoint topLeft = pageSetupData.GetMarginTopLeft();
795 wxPoint bottomRight = pageSetupData.GetMarginBottomRight();
796
797 // calculate margins in device units
798 wxRect pageMarginsRect(
799 paperRect.x + wxRound(mmToDeviceX * topLeft.x),
800 paperRect.y + wxRound(mmToDeviceY * topLeft.y),
801 paperRect.width - wxRound(mmToDeviceX * (topLeft.x + bottomRight.x)),
802 paperRect.height - wxRound(mmToDeviceY * (topLeft.y + bottomRight.y)));
803
804 wxCoord w, h;
805 m_printoutDC->GetSize(&w, &h);
806 if (w == pw && h == ph)
807 {
808 // this DC matches the printed page, so no scaling
809 return wxRect(
810 m_printoutDC->DeviceToLogicalX(pageMarginsRect.x),
811 m_printoutDC->DeviceToLogicalY(pageMarginsRect.y),
812 m_printoutDC->DeviceToLogicalXRel(pageMarginsRect.width),
813 m_printoutDC->DeviceToLogicalYRel(pageMarginsRect.height));
814 }
815
816 // This DC doesn't match the printed page, so we have to scale.
817 float scaleX = float(w) / pw;
818 float scaleY = float(h) / ph;
819 return wxRect(m_printoutDC->DeviceToLogicalX(wxRound(pageMarginsRect.x * scaleX)),
820 m_printoutDC->DeviceToLogicalY(wxRound(pageMarginsRect.y * scaleY)),
821 m_printoutDC->DeviceToLogicalXRel(wxRound(pageMarginsRect.width * scaleX)),
822 m_printoutDC->DeviceToLogicalYRel(wxRound(pageMarginsRect.height * scaleY)));
823 }
824
825 void wxPrintout::SetLogicalOrigin(wxCoord x, wxCoord y)
826 {
827 // Set the device origin by specifying a point in logical coordinates.
828 m_printoutDC->SetDeviceOrigin(
829 m_printoutDC->LogicalToDeviceX(x),
830 m_printoutDC->LogicalToDeviceY(y) );
831 }
832
833 void wxPrintout::OffsetLogicalOrigin(wxCoord xoff, wxCoord yoff)
834 {
835 // Offset the device origin by a specified distance in device coordinates.
836 wxPoint dev_org = m_printoutDC->GetDeviceOrigin();
837 m_printoutDC->SetDeviceOrigin(
838 dev_org.x + m_printoutDC->LogicalToDeviceXRel(xoff),
839 dev_org.y + m_printoutDC->LogicalToDeviceYRel(yoff) );
840 }
841
842
843 //----------------------------------------------------------------------------
844 // wxPreviewCanvas
845 //----------------------------------------------------------------------------
846
847 IMPLEMENT_CLASS(wxPreviewCanvas, wxWindow)
848
849 BEGIN_EVENT_TABLE(wxPreviewCanvas, wxScrolledWindow)
850 EVT_PAINT(wxPreviewCanvas::OnPaint)
851 EVT_CHAR(wxPreviewCanvas::OnChar)
852 EVT_IDLE(wxPreviewCanvas::OnIdle)
853 EVT_SYS_COLOUR_CHANGED(wxPreviewCanvas::OnSysColourChanged)
854 #if wxUSE_MOUSEWHEEL
855 EVT_MOUSEWHEEL(wxPreviewCanvas::OnMouseWheel)
856 #endif
857 END_EVENT_TABLE()
858
859 // VZ: the current code doesn't refresh properly without
860 // wxFULL_REPAINT_ON_RESIZE, this must be fixed as otherwise we have
861 // really horrible flicker when resizing the preview frame, but without
862 // this style it simply doesn't work correctly at all...
863 wxPreviewCanvas::wxPreviewCanvas(wxPrintPreviewBase *preview, wxWindow *parent,
864 const wxPoint& pos, const wxSize& size, long style, const wxString& name):
865 wxScrolledWindow(parent, wxID_ANY, pos, size, style | wxFULL_REPAINT_ON_RESIZE, name)
866 {
867 m_printPreview = preview;
868 #ifdef __WXMAC__
869 // The app workspace colour is always white, but we should have
870 // a contrast with the page.
871 wxSystemColour colourIndex = wxSYS_COLOUR_3DDKSHADOW;
872 #elif defined(__WXGTK__)
873 wxSystemColour colourIndex = wxSYS_COLOUR_BTNFACE;
874 #else
875 wxSystemColour colourIndex = wxSYS_COLOUR_APPWORKSPACE;
876 #endif
877 SetBackgroundColour(wxSystemSettings::GetColour(colourIndex));
878
879 SetScrollbars(10, 10, 100, 100);
880 }
881
882 wxPreviewCanvas::~wxPreviewCanvas()
883 {
884 }
885
886 void wxPreviewCanvas::OnPaint(wxPaintEvent& WXUNUSED(event))
887 {
888 wxPaintDC dc(this);
889 PrepareDC( dc );
890
891 /*
892 #ifdef __WXGTK__
893 if (!GetUpdateRegion().IsEmpty())
894 dc.SetClippingRegion( GetUpdateRegion() );
895 #endif
896 */
897
898 if (m_printPreview)
899 {
900 m_printPreview->PaintPage(this, dc);
901 }
902 }
903
904 void wxPreviewCanvas::OnIdle(wxIdleEvent& event)
905 {
906 event.Skip();
907
908 // prevent UpdatePageRendering() from being called recursively:
909 static bool s_inIdle = false;
910 if ( s_inIdle )
911 return;
912 s_inIdle = true;
913
914 if ( m_printPreview )
915 {
916 if ( m_printPreview->UpdatePageRendering() )
917 Refresh();
918 }
919
920 s_inIdle = false;
921 }
922
923 // Responds to colour changes, and passes event on to children.
924 void wxPreviewCanvas::OnSysColourChanged(wxSysColourChangedEvent& event)
925 {
926 #ifdef __WXMAC__
927 // The app workspace colour is always white, but we should have
928 // a contrast with the page.
929 wxSystemColour colourIndex = wxSYS_COLOUR_3DDKSHADOW;
930 #elif defined(__WXGTK__)
931 wxSystemColour colourIndex = wxSYS_COLOUR_BTNFACE;
932 #else
933 wxSystemColour colourIndex = wxSYS_COLOUR_APPWORKSPACE;
934 #endif
935 SetBackgroundColour(wxSystemSettings::GetColour(colourIndex));
936 Refresh();
937
938 // Propagate the event to the non-top-level children
939 wxWindow::OnSysColourChanged(event);
940 }
941
942 void wxPreviewCanvas::OnChar(wxKeyEvent &event)
943 {
944 wxPreviewControlBar* controlBar = ((wxPreviewFrame*) GetParent())->GetControlBar();
945 switch (event.GetKeyCode())
946 {
947 case WXK_RETURN:
948 controlBar->OnPrint();
949 return;
950 case (int)'+':
951 case WXK_NUMPAD_ADD:
952 case WXK_ADD:
953 controlBar->DoZoomIn();
954 return;
955 case (int)'-':
956 case WXK_NUMPAD_SUBTRACT:
957 case WXK_SUBTRACT:
958 controlBar->DoZoomOut();
959 return;
960 }
961
962 if (!event.ControlDown())
963 {
964 event.Skip();
965 return;
966 }
967
968 switch(event.GetKeyCode())
969 {
970 case WXK_PAGEDOWN:
971 controlBar->OnNext(); break;
972 case WXK_PAGEUP:
973 controlBar->OnPrevious(); break;
974 case WXK_HOME:
975 controlBar->OnFirst(); break;
976 case WXK_END:
977 controlBar->OnLast(); break;
978 default:
979 event.Skip();
980 }
981 }
982
983 #if wxUSE_MOUSEWHEEL
984
985 void wxPreviewCanvas::OnMouseWheel(wxMouseEvent& event)
986 {
987 wxPreviewControlBar *
988 controlBar = wxStaticCast(GetParent(), wxPreviewFrame)->GetControlBar();
989
990 if ( controlBar )
991 {
992 if ( event.ControlDown() && event.GetWheelRotation() != 0 )
993 {
994 int currentZoom = controlBar->GetZoomControl();
995
996 int delta;
997 if ( currentZoom < 100 )
998 delta = 5;
999 else if ( currentZoom <= 120 )
1000 delta = 10;
1001 else
1002 delta = 50;
1003
1004 if ( event.GetWheelRotation() > 0 )
1005 delta = -delta;
1006
1007 int newZoom = currentZoom + delta;
1008 if ( newZoom < 10 )
1009 newZoom = 10;
1010 if ( newZoom > 200 )
1011 newZoom = 200;
1012 if ( newZoom != currentZoom )
1013 {
1014 controlBar->SetZoomControl(newZoom);
1015 m_printPreview->SetZoom(newZoom);
1016 Refresh();
1017 }
1018 return;
1019 }
1020 }
1021
1022 event.Skip();
1023 }
1024
1025 #endif // wxUSE_MOUSEWHEEL
1026
1027 namespace
1028 {
1029
1030 // This is by the controls in the print preview as the maximal (and hence
1031 // longest) page number we may have to display.
1032 enum { MAX_PAGE_NUMBER = 99999 };
1033
1034 } // anonymous namespace
1035
1036 // ----------------------------------------------------------------------------
1037 // wxPrintPageMaxCtrl
1038 // ----------------------------------------------------------------------------
1039
1040 // A simple static control showing the maximal number of pages.
1041 class wxPrintPageMaxCtrl : public wxStaticText
1042 {
1043 public:
1044 wxPrintPageMaxCtrl(wxWindow *parent)
1045 : wxStaticText(
1046 parent,
1047 wxID_ANY,
1048 wxString(),
1049 wxDefaultPosition,
1050 wxSize
1051 (
1052 parent->GetTextExtent(MaxAsString(MAX_PAGE_NUMBER)).x,
1053 wxDefaultCoord
1054 ),
1055 wxST_NO_AUTORESIZE | wxALIGN_CENTRE
1056 )
1057 {
1058 }
1059
1060 // Set the maximal page to display once we really know what it is.
1061 void SetMaxPage(int maxPage)
1062 {
1063 SetLabel(MaxAsString(maxPage));
1064 }
1065
1066 private:
1067 static wxString MaxAsString(int maxPage)
1068 {
1069 return wxString::Format("/ %d", maxPage);
1070 }
1071
1072 wxDECLARE_NO_COPY_CLASS(wxPrintPageMaxCtrl);
1073 };
1074
1075 // ----------------------------------------------------------------------------
1076 // wxPrintPageTextCtrl
1077 // ----------------------------------------------------------------------------
1078
1079 // This text control contains the page number in the specified interval.
1080 //
1081 // Invalid pages are not accepted and the control contents is validated when it
1082 // loses focus. Conversely, if the user changes the page to another valid one
1083 // or presses Enter, OnGotoPage() method of the preview object will be called.
1084 class wxPrintPageTextCtrl : public wxTextCtrl
1085 {
1086 public:
1087 wxPrintPageTextCtrl(wxPreviewControlBar *preview)
1088 : wxTextCtrl(preview,
1089 wxID_PREVIEW_GOTO,
1090 wxString(),
1091 wxDefaultPosition,
1092 // We use hardcoded maximal page number for the width
1093 // instead of fitting it to the values we can show because
1094 // the control looks uncomfortably narrow if the real page
1095 // number is just one or two digits.
1096 wxSize
1097 (
1098 preview->GetTextExtent(PageAsString(MAX_PAGE_NUMBER)).x,
1099 wxDefaultCoord
1100 ),
1101 wxTE_PROCESS_ENTER
1102 #if wxUSE_VALIDATORS
1103 , wxTextValidator(wxFILTER_DIGITS)
1104 #endif // wxUSE_VALIDATORS
1105 ),
1106 m_preview(preview)
1107 {
1108 m_minPage =
1109 m_maxPage =
1110 m_page = 1;
1111
1112 Connect(wxEVT_KILL_FOCUS,
1113 wxFocusEventHandler(wxPrintPageTextCtrl::OnKillFocus));
1114 Connect(wxEVT_TEXT_ENTER,
1115 wxCommandEventHandler(wxPrintPageTextCtrl::OnTextEnter));
1116 }
1117
1118 // Update the pages range, must be called after OnPreparePrinting() as
1119 // these values are not known before.
1120 void SetPageInfo(int minPage, int maxPage)
1121 {
1122 m_minPage = minPage;
1123 m_maxPage = maxPage;
1124
1125 // Show the first page by default.
1126 SetPageNumber(minPage);
1127 }
1128
1129 // Helpers to conveniently set or get the current page number. Return value
1130 // is 0 if the current controls contents is invalid.
1131 void SetPageNumber(int page)
1132 {
1133 wxASSERT( IsValidPage(page) );
1134
1135 SetValue(PageAsString(page));
1136 }
1137
1138 int GetPageNumber() const
1139 {
1140 long value;
1141 if ( !GetValue().ToLong(&value) || !IsValidPage(value) )
1142 return 0;
1143
1144 // Cast is safe because the value is less than (int) m_maxPage.
1145 return static_cast<int>(value);
1146 }
1147
1148 private:
1149 static wxString PageAsString(int page)
1150 {
1151 return wxString::Format("%d", page);
1152 }
1153
1154 bool IsValidPage(int page) const
1155 {
1156 return page >= m_minPage && page <= m_maxPage;
1157 }
1158
1159 bool DoChangePage()
1160 {
1161 const int page = GetPageNumber();
1162
1163 if ( !page )
1164 return false;
1165
1166 if ( page != m_page )
1167 {
1168 // We have a valid page, remember it.
1169 m_page = page;
1170
1171 // And notify the owner about the change.
1172 m_preview->OnGotoPage();
1173 }
1174 //else: Nothing really changed.
1175
1176 return true;
1177 }
1178
1179 void OnKillFocus(wxFocusEvent& event)
1180 {
1181 if ( !DoChangePage() )
1182 {
1183 // The current contents is invalid so reset it back to the last
1184 // known good page index.
1185 SetPageNumber(m_page);
1186 }
1187
1188 event.Skip();
1189 }
1190
1191 void OnTextEnter(wxCommandEvent& WXUNUSED(event))
1192 {
1193 DoChangePage();
1194 }
1195
1196
1197 wxPreviewControlBar * const m_preview;
1198
1199 int m_minPage,
1200 m_maxPage;
1201
1202 // This is the last valid page value that we had, we revert to it if an
1203 // invalid page is entered.
1204 int m_page;
1205
1206 wxDECLARE_NO_COPY_CLASS(wxPrintPageTextCtrl);
1207 };
1208
1209 //----------------------------------------------------------------------------
1210 // wxPreviewControlBar
1211 //----------------------------------------------------------------------------
1212
1213 IMPLEMENT_CLASS(wxPreviewControlBar, wxWindow)
1214
1215 BEGIN_EVENT_TABLE(wxPreviewControlBar, wxPanel)
1216 EVT_BUTTON(wxID_PREVIEW_CLOSE, wxPreviewControlBar::OnWindowClose)
1217 EVT_BUTTON(wxID_PREVIEW_PRINT, wxPreviewControlBar::OnPrintButton)
1218 EVT_BUTTON(wxID_PREVIEW_PREVIOUS, wxPreviewControlBar::OnPreviousButton)
1219 EVT_BUTTON(wxID_PREVIEW_NEXT, wxPreviewControlBar::OnNextButton)
1220 EVT_BUTTON(wxID_PREVIEW_FIRST, wxPreviewControlBar::OnFirstButton)
1221 EVT_BUTTON(wxID_PREVIEW_LAST, wxPreviewControlBar::OnLastButton)
1222 EVT_BUTTON(wxID_PREVIEW_ZOOM_IN, wxPreviewControlBar::OnZoomInButton)
1223 EVT_BUTTON(wxID_PREVIEW_ZOOM_OUT, wxPreviewControlBar::OnZoomOutButton)
1224
1225 EVT_UPDATE_UI(wxID_PREVIEW_PREVIOUS, wxPreviewControlBar::OnUpdatePreviousButton)
1226 EVT_UPDATE_UI(wxID_PREVIEW_NEXT, wxPreviewControlBar::OnUpdateNextButton)
1227 EVT_UPDATE_UI(wxID_PREVIEW_FIRST, wxPreviewControlBar::OnUpdateFirstButton)
1228 EVT_UPDATE_UI(wxID_PREVIEW_LAST, wxPreviewControlBar::OnUpdateLastButton)
1229 EVT_UPDATE_UI(wxID_PREVIEW_ZOOM_IN, wxPreviewControlBar::OnUpdateZoomInButton)
1230 EVT_UPDATE_UI(wxID_PREVIEW_ZOOM_OUT, wxPreviewControlBar::OnUpdateZoomOutButton)
1231
1232 EVT_CHOICE(wxID_PREVIEW_ZOOM, wxPreviewControlBar::OnZoomChoice)
1233 EVT_PAINT(wxPreviewControlBar::OnPaint)
1234
1235 END_EVENT_TABLE()
1236
1237 wxPreviewControlBar::wxPreviewControlBar(wxPrintPreviewBase *preview, long buttons,
1238 wxWindow *parent, const wxPoint& pos, const wxSize& size,
1239 long style, const wxString& name):
1240 wxPanel(parent, wxID_ANY, pos, size, style, name)
1241 {
1242 m_printPreview = preview;
1243 m_closeButton = NULL;
1244 m_zoomControl = NULL;
1245 m_currentPageText = NULL;
1246 m_maxPageText = NULL;
1247 m_buttonFlags = buttons;
1248 }
1249
1250 wxPreviewControlBar::~wxPreviewControlBar()
1251 {
1252 }
1253
1254 void wxPreviewControlBar::OnPaint(wxPaintEvent& WXUNUSED(event))
1255 {
1256 wxPaintDC dc(this);
1257
1258 int w, h;
1259 GetSize(&w, &h);
1260 dc.SetPen(*wxBLACK_PEN);
1261 dc.SetBrush(*wxTRANSPARENT_BRUSH);
1262 dc.DrawLine( 0, h-1, w, h-1 );
1263 }
1264
1265 void wxPreviewControlBar::OnWindowClose(wxCommandEvent& WXUNUSED(event))
1266 {
1267 wxPreviewFrame *frame = (wxPreviewFrame *)GetParent();
1268 frame->Close(true);
1269 }
1270
1271 void wxPreviewControlBar::OnPrint(void)
1272 {
1273 wxPrintPreviewBase *preview = GetPrintPreview();
1274 preview->Print(true);
1275 }
1276
1277 void wxPreviewControlBar::OnNext()
1278 {
1279 if ( IsNextEnabled() )
1280 DoGotoPage(GetPrintPreview()->GetCurrentPage() + 1);
1281 }
1282
1283 void wxPreviewControlBar::OnPrevious()
1284 {
1285 if ( IsPreviousEnabled() )
1286 DoGotoPage(GetPrintPreview()->GetCurrentPage() - 1);
1287 }
1288
1289 void wxPreviewControlBar::OnFirst()
1290 {
1291 if ( IsFirstEnabled() )
1292 DoGotoPage(GetPrintPreview()->GetMinPage());
1293 }
1294
1295 void wxPreviewControlBar::OnLast()
1296 {
1297 if ( IsLastEnabled() )
1298 DoGotoPage(GetPrintPreview()->GetMaxPage());
1299 }
1300
1301 bool wxPreviewControlBar::IsNextEnabled() const
1302 {
1303 wxPrintPreviewBase *preview = GetPrintPreview();
1304 if ( !preview )
1305 return false;
1306
1307 const int currentPage = preview->GetCurrentPage();
1308 return currentPage < preview->GetMaxPage() &&
1309 preview->GetPrintout()->HasPage(currentPage + 1);
1310 }
1311
1312 bool wxPreviewControlBar::IsPreviousEnabled() const
1313 {
1314 wxPrintPreviewBase *preview = GetPrintPreview();
1315 if ( !preview )
1316 return false;
1317
1318 const int currentPage = preview->GetCurrentPage();
1319 return currentPage > preview->GetMinPage() &&
1320 preview->GetPrintout()->HasPage(currentPage - 1);
1321 }
1322
1323 bool wxPreviewControlBar::IsFirstEnabled() const
1324 {
1325 wxPrintPreviewBase *preview = GetPrintPreview();
1326 if (!preview)
1327 return false;
1328
1329 return preview->GetPrintout()->HasPage(preview->GetMinPage());
1330 }
1331
1332 bool wxPreviewControlBar::IsLastEnabled() const
1333 {
1334 wxPrintPreviewBase *preview = GetPrintPreview();
1335 if (!preview)
1336 return false;
1337
1338 return preview->GetPrintout()->HasPage(preview->GetMaxPage());
1339 }
1340
1341 void wxPreviewControlBar::DoGotoPage(int page)
1342 {
1343 wxPrintPreviewBase *preview = GetPrintPreview();
1344 wxCHECK_RET( preview, "Shouldn't be called if there is no preview." );
1345
1346 preview->SetCurrentPage(page);
1347
1348 if ( m_currentPageText )
1349 m_currentPageText->SetPageNumber(page);
1350 }
1351
1352 void wxPreviewControlBar::OnGotoPage()
1353 {
1354 wxPrintPreviewBase *preview = GetPrintPreview();
1355 if (preview)
1356 {
1357 if (preview->GetMinPage() > 0)
1358 {
1359 long currentPage = m_currentPageText->GetPageNumber();
1360 if ( currentPage )
1361 {
1362 if (preview->GetPrintout()->HasPage(currentPage))
1363 {
1364 preview->SetCurrentPage(currentPage);
1365 }
1366 }
1367 }
1368 }
1369 }
1370
1371 void wxPreviewControlBar::DoZoom()
1372 {
1373 int zoom = GetZoomControl();
1374 if (GetPrintPreview())
1375 GetPrintPreview()->SetZoom(zoom);
1376 }
1377
1378 bool wxPreviewControlBar::IsZoomInEnabled() const
1379 {
1380 if ( !m_zoomControl )
1381 return false;
1382
1383 const unsigned sel = m_zoomControl->GetSelection();
1384 return sel < m_zoomControl->GetCount() - 1;
1385 }
1386
1387 bool wxPreviewControlBar::IsZoomOutEnabled() const
1388 {
1389 return m_zoomControl && m_zoomControl->GetSelection() > 0;
1390 }
1391
1392 void wxPreviewControlBar::DoZoomIn()
1393 {
1394 if (IsZoomInEnabled())
1395 {
1396 m_zoomControl->SetSelection(m_zoomControl->GetSelection() + 1);
1397 DoZoom();
1398 }
1399 }
1400
1401 void wxPreviewControlBar::DoZoomOut()
1402 {
1403 if (IsZoomOutEnabled())
1404 {
1405 m_zoomControl->SetSelection(m_zoomControl->GetSelection() - 1);
1406 DoZoom();
1407 }
1408 }
1409
1410 namespace
1411 {
1412
1413 // Helper class used by wxPreviewControlBar::CreateButtons() to add buttons
1414 // sequentially to it in the simplest way possible.
1415 class SizerWithButtons
1416 {
1417 public:
1418 // Constructor creates the sizer that will hold the buttons and stores the
1419 // parent that will be used for their creation.
1420 SizerWithButtons(wxWindow *parent)
1421 : m_sizer(new wxBoxSizer(wxHORIZONTAL)),
1422 m_parent(parent)
1423 {
1424 m_hasContents =
1425 m_needsSeparator = false;
1426 }
1427
1428 // Destructor associates the sizer with the parent window.
1429 ~SizerWithButtons()
1430 {
1431 m_parent->SetSizer(m_sizer);
1432 m_sizer->Fit(m_parent);
1433 }
1434
1435
1436 // Add an arbitrary window to the sizer.
1437 void Add(wxWindow *win)
1438 {
1439 if ( m_needsSeparator )
1440 {
1441 m_needsSeparator = false;
1442
1443 m_sizer->AddSpacer(2*wxSizerFlags::GetDefaultBorder());
1444 }
1445
1446 m_hasContents = true;
1447
1448 m_sizer->Add(win,
1449 wxSizerFlags().Border(wxLEFT | wxTOP | wxBOTTOM).Center());
1450 }
1451
1452 // Add a button with the specified id, bitmap and tooltip.
1453 void AddButton(wxWindowID btnId,
1454 const wxArtID& artId,
1455 const wxString& tooltip)
1456 {
1457 // We don't use (smaller) images inside a button with a text label but
1458 // rather toolbar-like bitmap buttons hence use wxART_TOOLBAR and not
1459 // wxART_BUTTON here.
1460 wxBitmap bmp = wxArtProvider::GetBitmap(artId, wxART_TOOLBAR);
1461 wxBitmapButton * const btn = new wxBitmapButton(m_parent, btnId, bmp);
1462 btn->SetToolTip(tooltip);
1463
1464 Add(btn);
1465 }
1466
1467 // Add a control at the right end of the window. This should be called last
1468 // as everything else added after it will be added on the right side too.
1469 void AddAtEnd(wxWindow *win)
1470 {
1471 m_sizer->AddStretchSpacer();
1472 m_sizer->Add(win,
1473 wxSizerFlags().Border(wxTOP | wxBOTTOM | wxRIGHT).Center());
1474 }
1475
1476 // Indicates the end of a group of buttons, a separator will be added after
1477 // it.
1478 void EndOfGroup()
1479 {
1480 if ( m_hasContents )
1481 {
1482 m_needsSeparator = true;
1483 m_hasContents = false;
1484 }
1485 }
1486
1487 private:
1488 wxSizer * const m_sizer;
1489 wxWindow * const m_parent;
1490
1491 // If true, we have some controls since the last group beginning. This is
1492 // used to avoid inserting two consecutive separators if EndOfGroup() is
1493 // called twice.
1494 bool m_hasContents;
1495
1496 // If true, a separator should be inserted before adding the next button.
1497 bool m_needsSeparator;
1498
1499 wxDECLARE_NO_COPY_CLASS(SizerWithButtons);
1500 };
1501
1502 } // anonymous namespace
1503
1504 void wxPreviewControlBar::CreateButtons()
1505 {
1506 SizerWithButtons sizer(this);
1507
1508 // Print button group (a single button).
1509 if (m_buttonFlags & wxPREVIEW_PRINT)
1510 {
1511 sizer.AddButton(wxID_PREVIEW_PRINT, wxART_PRINT, _("Print"));
1512 sizer.EndOfGroup();
1513 }
1514
1515 // Page selection buttons group.
1516 if (m_buttonFlags & wxPREVIEW_FIRST)
1517 {
1518 sizer.AddButton(wxID_PREVIEW_FIRST, wxART_GOTO_FIRST, _("First page"));
1519 }
1520
1521 if (m_buttonFlags & wxPREVIEW_PREVIOUS)
1522 {
1523 sizer.AddButton(wxID_PREVIEW_PREVIOUS, wxART_GO_BACK, _("Previous page"));
1524 }
1525
1526 if (m_buttonFlags & wxPREVIEW_GOTO)
1527 {
1528 m_currentPageText = new wxPrintPageTextCtrl(this);
1529 sizer.Add(m_currentPageText);
1530
1531 m_maxPageText = new wxPrintPageMaxCtrl(this);
1532 sizer.Add(m_maxPageText);
1533 }
1534
1535 if (m_buttonFlags & wxPREVIEW_NEXT)
1536 {
1537 sizer.AddButton(wxID_PREVIEW_NEXT, wxART_GO_FORWARD, _("Next page"));
1538 }
1539
1540 if (m_buttonFlags & wxPREVIEW_LAST)
1541 {
1542 sizer.AddButton(wxID_PREVIEW_LAST, wxART_GOTO_LAST, _("Last page"));
1543 }
1544
1545 sizer.EndOfGroup();
1546
1547 // Zoom controls group.
1548 if (m_buttonFlags & wxPREVIEW_ZOOM)
1549 {
1550 sizer.AddButton(wxID_PREVIEW_ZOOM_OUT, wxART_MINUS, _("Zoom Out"));
1551
1552 wxString choices[] =
1553 {
1554 wxT("10%"), wxT("15%"), wxT("20%"), wxT("25%"), wxT("30%"), wxT("35%"), wxT("40%"), wxT("45%"), wxT("50%"), wxT("55%"),
1555 wxT("60%"), wxT("65%"), wxT("70%"), wxT("75%"), wxT("80%"), wxT("85%"), wxT("90%"), wxT("95%"), wxT("100%"), wxT("110%"),
1556 wxT("120%"), wxT("150%"), wxT("200%")
1557 };
1558 int n = WXSIZEOF(choices);
1559
1560 m_zoomControl = new wxChoice( this, wxID_PREVIEW_ZOOM, wxDefaultPosition, wxSize(70,wxDefaultCoord), n, choices, 0 );
1561 sizer.Add(m_zoomControl);
1562 SetZoomControl(m_printPreview->GetZoom());
1563
1564 sizer.AddButton(wxID_PREVIEW_ZOOM_IN, wxART_PLUS, _("Zoom In"));
1565
1566 sizer.EndOfGroup();
1567 }
1568
1569 // Close button group (single button again).
1570 m_closeButton = new wxButton(this, wxID_PREVIEW_CLOSE, _("&Close"));
1571 sizer.AddAtEnd(m_closeButton);
1572 }
1573
1574 void wxPreviewControlBar::SetPageInfo(int minPage, int maxPage)
1575 {
1576 if ( m_currentPageText )
1577 m_currentPageText->SetPageInfo(minPage, maxPage);
1578
1579 if ( m_maxPageText )
1580 m_maxPageText->SetMaxPage(maxPage);
1581 }
1582
1583 void wxPreviewControlBar::SetZoomControl(int zoom)
1584 {
1585 if (m_zoomControl)
1586 {
1587 int n, count = m_zoomControl->GetCount();
1588 long val;
1589 for (n=0; n<count; n++)
1590 {
1591 if (m_zoomControl->GetString(n).BeforeFirst(wxT('%')).ToLong(&val) &&
1592 (val >= long(zoom)))
1593 {
1594 m_zoomControl->SetSelection(n);
1595 return;
1596 }
1597 }
1598
1599 m_zoomControl->SetSelection(count-1);
1600 }
1601 }
1602
1603 int wxPreviewControlBar::GetZoomControl()
1604 {
1605 if (m_zoomControl && (m_zoomControl->GetStringSelection() != wxEmptyString))
1606 {
1607 long val;
1608 if (m_zoomControl->GetStringSelection().BeforeFirst(wxT('%')).ToLong(&val))
1609 return int(val);
1610 }
1611
1612 return 0;
1613 }
1614
1615
1616 /*
1617 * Preview frame
1618 */
1619
1620 IMPLEMENT_CLASS(wxPreviewFrame, wxFrame)
1621
1622 BEGIN_EVENT_TABLE(wxPreviewFrame, wxFrame)
1623 EVT_CHAR_HOOK(wxPreviewFrame::OnChar)
1624 EVT_CLOSE(wxPreviewFrame::OnCloseWindow)
1625 END_EVENT_TABLE()
1626
1627 void wxPreviewFrame::OnChar(wxKeyEvent &event)
1628 {
1629 if ( event.GetKeyCode() == WXK_ESCAPE )
1630 {
1631 Close(true);
1632 }
1633 else
1634 {
1635 event.Skip();
1636 }
1637 }
1638
1639 wxPreviewFrame::wxPreviewFrame(wxPrintPreviewBase *preview, wxWindow *parent, const wxString& title,
1640 const wxPoint& pos, const wxSize& size, long style, const wxString& name):
1641 wxFrame(parent, wxID_ANY, title, pos, size, style, name)
1642 {
1643 m_printPreview = preview;
1644 m_controlBar = NULL;
1645 m_previewCanvas = NULL;
1646 m_windowDisabler = NULL;
1647 m_modalityKind = wxPreviewFrame_NonModal;
1648
1649 // Give the application icon
1650 #ifdef __WXMSW__
1651 wxFrame* topFrame = wxDynamicCast(wxTheApp->GetTopWindow(), wxFrame);
1652 if (topFrame)
1653 SetIcons(topFrame->GetIcons());
1654 #endif
1655 }
1656
1657 wxPreviewFrame::~wxPreviewFrame()
1658 {
1659 wxPrintout *printout = m_printPreview->GetPrintout();
1660 if (printout)
1661 {
1662 delete printout;
1663 m_printPreview->SetPrintout(NULL);
1664 m_printPreview->SetCanvas(NULL);
1665 m_printPreview->SetFrame(NULL);
1666 }
1667
1668 m_previewCanvas->SetPreview(NULL);
1669 delete m_printPreview;
1670 }
1671
1672 void wxPreviewFrame::OnCloseWindow(wxCloseEvent& WXUNUSED(event))
1673 {
1674 // Reenable any windows we disabled by undoing whatever we did in our
1675 // Initialize().
1676 switch ( m_modalityKind )
1677 {
1678 case wxPreviewFrame_AppModal:
1679 delete m_windowDisabler;
1680 m_windowDisabler = NULL;
1681 break;
1682
1683 case wxPreviewFrame_WindowModal:
1684 if ( GetParent() )
1685 GetParent()->Enable();
1686 break;
1687
1688 case wxPreviewFrame_NonModal:
1689 break;
1690 }
1691
1692 Destroy();
1693 }
1694
1695 void wxPreviewFrame::InitializeWithModality(wxPreviewFrameModalityKind kind)
1696 {
1697 #if wxUSE_STATUSBAR
1698 CreateStatusBar();
1699 #endif
1700 CreateCanvas();
1701 CreateControlBar();
1702
1703 m_printPreview->SetCanvas(m_previewCanvas);
1704 m_printPreview->SetFrame(this);
1705
1706 wxBoxSizer *item0 = new wxBoxSizer( wxVERTICAL );
1707
1708 item0->Add( m_controlBar, 0, wxGROW|wxALIGN_CENTER_VERTICAL, 5 );
1709 item0->Add( m_previewCanvas, 1, wxGROW|wxALIGN_CENTER_VERTICAL, 5 );
1710
1711 SetAutoLayout( true );
1712 SetSizer( item0 );
1713
1714 m_modalityKind = kind;
1715 switch ( m_modalityKind )
1716 {
1717 case wxPreviewFrame_AppModal:
1718 // Disable everything.
1719 m_windowDisabler = new wxWindowDisabler( this );
1720 break;
1721
1722 case wxPreviewFrame_WindowModal:
1723 // Disable our parent if we have one.
1724 if ( GetParent() )
1725 GetParent()->Disable();
1726 break;
1727
1728 case wxPreviewFrame_NonModal:
1729 // Nothing to do, we don't need to disable any windows.
1730 break;
1731 }
1732
1733 if ( m_modalityKind != wxPreviewFrame_NonModal )
1734 {
1735 // Behave like modal dialogs, don't show in taskbar. This implies
1736 // removing the minimize box, because minimizing windows without
1737 // taskbar entry is confusing.
1738 SetWindowStyle((GetWindowStyle() & ~wxMINIMIZE_BOX) | wxFRAME_NO_TASKBAR);
1739 }
1740
1741 Layout();
1742
1743 m_printPreview->AdjustScrollbars(m_previewCanvas);
1744 m_previewCanvas->SetFocus();
1745 m_controlBar->SetFocus();
1746 }
1747
1748 void wxPreviewFrame::CreateCanvas()
1749 {
1750 m_previewCanvas = new wxPreviewCanvas(m_printPreview, this);
1751 }
1752
1753 void wxPreviewFrame::CreateControlBar()
1754 {
1755 long buttons = wxPREVIEW_DEFAULT;
1756 if (m_printPreview->GetPrintoutForPrinting())
1757 buttons |= wxPREVIEW_PRINT;
1758
1759 m_controlBar = new wxPreviewControlBar(m_printPreview, buttons, this);
1760 m_controlBar->CreateButtons();
1761 }
1762
1763 /*
1764 * Print preview
1765 */
1766
1767 IMPLEMENT_CLASS(wxPrintPreviewBase, wxObject)
1768
1769 wxPrintPreviewBase::wxPrintPreviewBase(wxPrintout *printout,
1770 wxPrintout *printoutForPrinting,
1771 wxPrintData *data)
1772 {
1773 if (data)
1774 m_printDialogData = (*data);
1775
1776 Init(printout, printoutForPrinting);
1777 }
1778
1779 wxPrintPreviewBase::wxPrintPreviewBase(wxPrintout *printout,
1780 wxPrintout *printoutForPrinting,
1781 wxPrintDialogData *data)
1782 {
1783 if (data)
1784 m_printDialogData = (*data);
1785
1786 Init(printout, printoutForPrinting);
1787 }
1788
1789 void wxPrintPreviewBase::Init(wxPrintout *printout,
1790 wxPrintout *printoutForPrinting)
1791 {
1792 m_isOk = true;
1793 m_previewPrintout = printout;
1794 if (m_previewPrintout)
1795 m_previewPrintout->SetPreview(static_cast<wxPrintPreview *>(this));
1796
1797 m_printPrintout = printoutForPrinting;
1798
1799 m_previewCanvas = NULL;
1800 m_previewFrame = NULL;
1801 m_previewBitmap = NULL;
1802 m_previewFailed = false;
1803 m_currentPage = 1;
1804 m_currentZoom = 70;
1805 m_topMargin =
1806 m_leftMargin = 2*wxSizerFlags::GetDefaultBorder();
1807 m_pageWidth = 0;
1808 m_pageHeight = 0;
1809 m_printingPrepared = false;
1810 m_minPage = 1;
1811 m_maxPage = 1;
1812 }
1813
1814 wxPrintPreviewBase::~wxPrintPreviewBase()
1815 {
1816 if (m_previewPrintout)
1817 delete m_previewPrintout;
1818 if (m_previewBitmap)
1819 delete m_previewBitmap;
1820 if (m_printPrintout)
1821 delete m_printPrintout;
1822 }
1823
1824 bool wxPrintPreviewBase::SetCurrentPage(int pageNum)
1825 {
1826 if (m_currentPage == pageNum)
1827 return true;
1828
1829 m_currentPage = pageNum;
1830
1831 InvalidatePreviewBitmap();
1832
1833 if (m_previewCanvas)
1834 {
1835 AdjustScrollbars(m_previewCanvas);
1836
1837 m_previewCanvas->Refresh();
1838 m_previewCanvas->SetFocus();
1839 }
1840 return true;
1841 }
1842
1843 int wxPrintPreviewBase::GetCurrentPage() const
1844 { return m_currentPage; }
1845 void wxPrintPreviewBase::SetPrintout(wxPrintout *printout)
1846 { m_previewPrintout = printout; }
1847 wxPrintout *wxPrintPreviewBase::GetPrintout() const
1848 { return m_previewPrintout; }
1849 wxPrintout *wxPrintPreviewBase::GetPrintoutForPrinting() const
1850 { return m_printPrintout; }
1851 void wxPrintPreviewBase::SetFrame(wxFrame *frame)
1852 { m_previewFrame = frame; }
1853 void wxPrintPreviewBase::SetCanvas(wxPreviewCanvas *canvas)
1854 { m_previewCanvas = canvas; }
1855 wxFrame *wxPrintPreviewBase::GetFrame() const
1856 { return m_previewFrame; }
1857 wxPreviewCanvas *wxPrintPreviewBase::GetCanvas() const
1858 { return m_previewCanvas; }
1859
1860 void wxPrintPreviewBase::CalcRects(wxPreviewCanvas *canvas, wxRect& pageRect, wxRect& paperRect)
1861 {
1862 // Calculate the rectangles for the printable area of the page and the
1863 // entire paper as they appear on the canvas on-screen.
1864 int canvasWidth, canvasHeight;
1865 canvas->GetSize(&canvasWidth, &canvasHeight);
1866
1867 float zoomScale = float(m_currentZoom) / 100;
1868 float screenPrintableWidth = zoomScale * m_pageWidth * m_previewScaleX;
1869 float screenPrintableHeight = zoomScale * m_pageHeight * m_previewScaleY;
1870
1871 wxRect devicePaperRect = m_previewPrintout->GetPaperRectPixels();
1872 wxCoord devicePrintableWidth, devicePrintableHeight;
1873 m_previewPrintout->GetPageSizePixels(&devicePrintableWidth, &devicePrintableHeight);
1874 float scaleX = screenPrintableWidth / devicePrintableWidth;
1875 float scaleY = screenPrintableHeight / devicePrintableHeight;
1876 paperRect.width = wxCoord(scaleX * devicePaperRect.width);
1877 paperRect.height = wxCoord(scaleY * devicePaperRect.height);
1878
1879 paperRect.x = wxCoord((canvasWidth - paperRect.width)/ 2.0);
1880 if (paperRect.x < m_leftMargin)
1881 paperRect.x = m_leftMargin;
1882 paperRect.y = wxCoord((canvasHeight - paperRect.height)/ 2.0);
1883 if (paperRect.y < m_topMargin)
1884 paperRect.y = m_topMargin;
1885
1886 pageRect.x = paperRect.x - wxCoord(scaleX * devicePaperRect.x);
1887 pageRect.y = paperRect.y - wxCoord(scaleY * devicePaperRect.y);
1888 pageRect.width = wxCoord(screenPrintableWidth);
1889 pageRect.height = wxCoord(screenPrintableHeight);
1890 }
1891
1892
1893 void wxPrintPreviewBase::InvalidatePreviewBitmap()
1894 {
1895 wxDELETE(m_previewBitmap);
1896 // if there was a problem with rendering the preview, try again now
1897 // that it changed in some way (less memory may be needed, for example):
1898 m_previewFailed = false;
1899 }
1900
1901 bool wxPrintPreviewBase::UpdatePageRendering()
1902 {
1903 if ( m_previewBitmap )
1904 return false;
1905
1906 if ( m_previewFailed )
1907 return false;
1908
1909 if ( !RenderPage(m_currentPage) )
1910 {
1911 m_previewFailed = true; // don't waste time failing again
1912 return false;
1913 }
1914
1915 return true;
1916 }
1917
1918 bool wxPrintPreviewBase::PaintPage(wxPreviewCanvas *canvas, wxDC& dc)
1919 {
1920 DrawBlankPage(canvas, dc);
1921
1922 if (!m_previewBitmap)
1923 return false;
1924 if (!canvas)
1925 return false;
1926
1927 wxRect pageRect, paperRect;
1928 CalcRects(canvas, pageRect, paperRect);
1929 wxMemoryDC temp_dc;
1930 temp_dc.SelectObject(*m_previewBitmap);
1931
1932 dc.Blit(pageRect.x, pageRect.y,
1933 m_previewBitmap->GetWidth(), m_previewBitmap->GetHeight(), &temp_dc, 0, 0);
1934
1935 temp_dc.SelectObject(wxNullBitmap);
1936 return true;
1937 }
1938
1939 // Adjusts the scrollbars for the current scale
1940 void wxPrintPreviewBase::AdjustScrollbars(wxPreviewCanvas *canvas)
1941 {
1942 if (!canvas)
1943 return ;
1944
1945 wxRect pageRect, paperRect;
1946 CalcRects(canvas, pageRect, paperRect);
1947 int totalWidth = paperRect.width + 2 * m_leftMargin;
1948 int totalHeight = paperRect.height + 2 * m_topMargin;
1949 int scrollUnitsX = totalWidth / 10;
1950 int scrollUnitsY = totalHeight / 10;
1951 wxSize virtualSize = canvas->GetVirtualSize();
1952 if (virtualSize.GetWidth() != totalWidth || virtualSize.GetHeight() != totalHeight)
1953 canvas->SetScrollbars(10, 10, scrollUnitsX, scrollUnitsY, 0, 0, true);
1954 }
1955
1956 bool wxPrintPreviewBase::RenderPageIntoDC(wxDC& dc, int pageNum)
1957 {
1958 m_previewPrintout->SetDC(&dc);
1959 m_previewPrintout->SetPageSizePixels(m_pageWidth, m_pageHeight);
1960
1961 // Need to delay OnPreparePrinting() until here, so we have enough
1962 // information and a wxDC.
1963 if (!m_printingPrepared)
1964 {
1965 m_printingPrepared = true;
1966
1967 m_previewPrintout->OnPreparePrinting();
1968 int selFrom, selTo;
1969 m_previewPrintout->GetPageInfo(&m_minPage, &m_maxPage, &selFrom, &selTo);
1970
1971 // Update the wxPreviewControlBar page range display.
1972 if ( m_previewFrame )
1973 {
1974 wxPreviewControlBar * const
1975 controlBar = ((wxPreviewFrame*)m_previewFrame)->GetControlBar();
1976 if ( controlBar )
1977 controlBar->SetPageInfo(m_minPage, m_maxPage);
1978 }
1979 }
1980
1981 m_previewPrintout->OnBeginPrinting();
1982
1983 if (!m_previewPrintout->OnBeginDocument(m_printDialogData.GetFromPage(), m_printDialogData.GetToPage()))
1984 {
1985 wxMessageBox(_("Could not start document preview."), _("Print Preview Failure"), wxOK);
1986 return false;
1987 }
1988
1989 m_previewPrintout->OnPrintPage(pageNum);
1990 m_previewPrintout->OnEndDocument();
1991 m_previewPrintout->OnEndPrinting();
1992
1993 m_previewPrintout->SetDC(NULL);
1994
1995 return true;
1996 }
1997
1998 bool wxPrintPreviewBase::RenderPageIntoBitmap(wxBitmap& bmp, int pageNum)
1999 {
2000 wxMemoryDC memoryDC;
2001 memoryDC.SelectObject(bmp);
2002 memoryDC.Clear();
2003
2004 return RenderPageIntoDC(memoryDC, pageNum);
2005 }
2006
2007 bool wxPrintPreviewBase::RenderPage(int pageNum)
2008 {
2009 wxBusyCursor busy;
2010
2011 if (!m_previewCanvas)
2012 {
2013 wxFAIL_MSG(wxT("wxPrintPreviewBase::RenderPage: must use wxPrintPreviewBase::SetCanvas to let me know about the canvas!"));
2014 return false;
2015 }
2016
2017 wxRect pageRect, paperRect;
2018 CalcRects(m_previewCanvas, pageRect, paperRect);
2019
2020 if (!m_previewBitmap)
2021 {
2022 m_previewBitmap = new wxBitmap(pageRect.width, pageRect.height);
2023
2024 if (!m_previewBitmap || !m_previewBitmap->IsOk())
2025 {
2026 InvalidatePreviewBitmap();
2027 wxMessageBox(_("Sorry, not enough memory to create a preview."), _("Print Preview Failure"), wxOK);
2028 return false;
2029 }
2030 }
2031
2032 if ( !RenderPageIntoBitmap(*m_previewBitmap, pageNum) )
2033 {
2034 InvalidatePreviewBitmap();
2035 wxMessageBox(_("Sorry, not enough memory to create a preview."), _("Print Preview Failure"), wxOK);
2036 return false;
2037 }
2038
2039 #if wxUSE_STATUSBAR
2040 wxString status;
2041 if (m_maxPage != 0)
2042 status = wxString::Format(_("Page %d of %d"), pageNum, m_maxPage);
2043 else
2044 status = wxString::Format(_("Page %d"), pageNum);
2045
2046 if (m_previewFrame)
2047 m_previewFrame->SetStatusText(status);
2048 #endif
2049
2050 return true;
2051 }
2052
2053 bool wxPrintPreviewBase::DrawBlankPage(wxPreviewCanvas *canvas, wxDC& dc)
2054 {
2055 wxRect pageRect, paperRect;
2056
2057 CalcRects(canvas, pageRect, paperRect);
2058
2059 // Draw shadow, allowing for 1-pixel border AROUND the actual paper
2060 wxCoord shadowOffset = 4;
2061
2062 dc.SetPen(*wxBLACK_PEN);
2063 dc.SetBrush(*wxBLACK_BRUSH);
2064 dc.DrawRectangle(paperRect.x + shadowOffset, paperRect.y + paperRect.height + 1,
2065 paperRect.width, shadowOffset);
2066
2067 dc.DrawRectangle(paperRect.x + paperRect.width, paperRect.y + shadowOffset,
2068 shadowOffset, paperRect.height);
2069
2070 // Draw blank page allowing for 1-pixel border AROUND the actual paper
2071 dc.SetPen(*wxBLACK_PEN);
2072 dc.SetBrush(*wxWHITE_BRUSH);
2073 dc.DrawRectangle(paperRect.x - 2, paperRect.y - 1,
2074 paperRect.width + 3, paperRect.height + 2);
2075
2076 return true;
2077 }
2078
2079 void wxPrintPreviewBase::SetZoom(int percent)
2080 {
2081 if (m_currentZoom == percent)
2082 return;
2083
2084 m_currentZoom = percent;
2085
2086 InvalidatePreviewBitmap();
2087
2088 if (m_previewCanvas)
2089 {
2090 AdjustScrollbars(m_previewCanvas);
2091 ((wxScrolledWindow *) m_previewCanvas)->Scroll(0, 0);
2092 m_previewCanvas->ClearBackground();
2093 m_previewCanvas->Refresh();
2094 m_previewCanvas->SetFocus();
2095 }
2096 }
2097
2098 wxPrintDialogData& wxPrintPreviewBase::GetPrintDialogData()
2099 {
2100 return m_printDialogData;
2101 }
2102
2103 int wxPrintPreviewBase::GetZoom() const
2104 { return m_currentZoom; }
2105 int wxPrintPreviewBase::GetMaxPage() const
2106 { return m_maxPage; }
2107 int wxPrintPreviewBase::GetMinPage() const
2108 { return m_minPage; }
2109 bool wxPrintPreviewBase::IsOk() const
2110 { return m_isOk; }
2111 void wxPrintPreviewBase::SetOk(bool ok)
2112 { m_isOk = ok; }
2113
2114 //----------------------------------------------------------------------------
2115 // wxPrintPreview
2116 //----------------------------------------------------------------------------
2117
2118 IMPLEMENT_CLASS(wxPrintPreview, wxPrintPreviewBase)
2119
2120 wxPrintPreview::wxPrintPreview(wxPrintout *printout,
2121 wxPrintout *printoutForPrinting,
2122 wxPrintDialogData *data) :
2123 wxPrintPreviewBase( printout, printoutForPrinting, data )
2124 {
2125 m_pimpl = wxPrintFactory::GetFactory()->
2126 CreatePrintPreview( printout, printoutForPrinting, data );
2127 }
2128
2129 wxPrintPreview::wxPrintPreview(wxPrintout *printout,
2130 wxPrintout *printoutForPrinting,
2131 wxPrintData *data ) :
2132 wxPrintPreviewBase( printout, printoutForPrinting, data )
2133 {
2134 m_pimpl = wxPrintFactory::GetFactory()->
2135 CreatePrintPreview( printout, printoutForPrinting, data );
2136 }
2137
2138 wxPrintPreview::~wxPrintPreview()
2139 {
2140 delete m_pimpl;
2141
2142 // don't delete twice
2143 m_printPrintout = NULL;
2144 m_previewPrintout = NULL;
2145 m_previewBitmap = NULL;
2146 }
2147
2148 bool wxPrintPreview::SetCurrentPage(int pageNum)
2149 {
2150 return m_pimpl->SetCurrentPage( pageNum );
2151 }
2152
2153 int wxPrintPreview::GetCurrentPage() const
2154 {
2155 return m_pimpl->GetCurrentPage();
2156 }
2157
2158 void wxPrintPreview::SetPrintout(wxPrintout *printout)
2159 {
2160 m_pimpl->SetPrintout( printout );
2161 }
2162
2163 wxPrintout *wxPrintPreview::GetPrintout() const
2164 {
2165 return m_pimpl->GetPrintout();
2166 }
2167
2168 wxPrintout *wxPrintPreview::GetPrintoutForPrinting() const
2169 {
2170 return m_pimpl->GetPrintoutForPrinting();
2171 }
2172
2173 void wxPrintPreview::SetFrame(wxFrame *frame)
2174 {
2175 m_pimpl->SetFrame( frame );
2176 }
2177
2178 void wxPrintPreview::SetCanvas(wxPreviewCanvas *canvas)
2179 {
2180 m_pimpl->SetCanvas( canvas );
2181 }
2182
2183 wxFrame *wxPrintPreview::GetFrame() const
2184 {
2185 return m_pimpl->GetFrame();
2186 }
2187
2188 wxPreviewCanvas *wxPrintPreview::GetCanvas() const
2189 {
2190 return m_pimpl->GetCanvas();
2191 }
2192
2193 bool wxPrintPreview::PaintPage(wxPreviewCanvas *canvas, wxDC& dc)
2194 {
2195 return m_pimpl->PaintPage( canvas, dc );
2196 }
2197
2198 bool wxPrintPreview::UpdatePageRendering()
2199 {
2200 return m_pimpl->UpdatePageRendering();
2201 }
2202
2203 bool wxPrintPreview::DrawBlankPage(wxPreviewCanvas *canvas, wxDC& dc)
2204 {
2205 return m_pimpl->DrawBlankPage( canvas, dc );
2206 }
2207
2208 void wxPrintPreview::AdjustScrollbars(wxPreviewCanvas *canvas)
2209 {
2210 m_pimpl->AdjustScrollbars( canvas );
2211 }
2212
2213 bool wxPrintPreview::RenderPage(int pageNum)
2214 {
2215 return m_pimpl->RenderPage( pageNum );
2216 }
2217
2218 void wxPrintPreview::SetZoom(int percent)
2219 {
2220 m_pimpl->SetZoom( percent );
2221 }
2222
2223 int wxPrintPreview::GetZoom() const
2224 {
2225 return m_pimpl->GetZoom();
2226 }
2227
2228 wxPrintDialogData& wxPrintPreview::GetPrintDialogData()
2229 {
2230 return m_pimpl->GetPrintDialogData();
2231 }
2232
2233 int wxPrintPreview::GetMaxPage() const
2234 {
2235 return m_pimpl->GetMaxPage();
2236 }
2237
2238 int wxPrintPreview::GetMinPage() const
2239 {
2240 return m_pimpl->GetMinPage();
2241 }
2242
2243 bool wxPrintPreview::IsOk() const
2244 {
2245 return m_pimpl->IsOk();
2246 }
2247
2248 void wxPrintPreview::SetOk(bool ok)
2249 {
2250 m_pimpl->SetOk( ok );
2251 }
2252
2253 bool wxPrintPreview::Print(bool interactive)
2254 {
2255 return m_pimpl->Print( interactive );
2256 }
2257
2258 void wxPrintPreview::DetermineScaling()
2259 {
2260 m_pimpl->DetermineScaling();
2261 }
2262
2263 #endif // wxUSE_PRINTING_ARCHITECTURE