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