1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/prntbase.cpp
3 // Purpose: Printing framework base class implementation
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // For compilers that support precompilation, includes "wx.h".
13 #include "wx/wxprec.h"
19 #if wxUSE_PRINTING_ARCHITECTURE
25 #include "wx/msgdlg.h"
26 #include "wx/layout.h"
27 #include "wx/choice.h"
28 #include "wx/button.h"
29 #include "wx/settings.h"
30 #include "wx/dcmemory.h"
31 #include "wx/stattext.h"
33 #include "wx/textdlg.h"
37 #include "wx/prntbase.h"
38 #include "wx/dcprint.h"
39 #include "wx/printdlg.h"
41 #include "wx/module.h"
46 #if defined(__WXMSW__) && !defined(__WXUNIVERSAL__)
47 #include "wx/msw/printdlg.h"
48 #elif defined(__WXMAC__)
49 #include "wx/mac/printdlg.h"
50 #include "wx/mac/private/print.h"
52 #include "wx/generic/prntdlgg.h"
56 #include "wx/msw/wrapcdlg.h"
62 //----------------------------------------------------------------------------
64 //----------------------------------------------------------------------------
66 wxPrintFactory
*wxPrintFactory::m_factory
= NULL
;
68 void wxPrintFactory::SetPrintFactory( wxPrintFactory
*factory
)
70 if (wxPrintFactory::m_factory
)
71 delete wxPrintFactory::m_factory
;
73 wxPrintFactory::m_factory
= factory
;
76 wxPrintFactory
*wxPrintFactory::GetFactory()
78 if (!wxPrintFactory::m_factory
)
79 wxPrintFactory::m_factory
= new wxNativePrintFactory
;
81 return wxPrintFactory::m_factory
;
84 //----------------------------------------------------------------------------
85 // wxNativePrintFactory
86 //----------------------------------------------------------------------------
88 wxPrinterBase
*wxNativePrintFactory::CreatePrinter( wxPrintDialogData
*data
)
90 #if defined(__WXMSW__) && !defined(__WXUNIVERSAL__)
91 return new wxWindowsPrinter( data
);
92 #elif defined(__WXMAC__)
93 return new wxMacPrinter( data
);
94 #elif defined(__WXPM__)
95 return new wxOS2Printer( data
);
97 return new wxPostScriptPrinter( data
);
101 wxPrintPreviewBase
*wxNativePrintFactory::CreatePrintPreview( wxPrintout
*preview
,
102 wxPrintout
*printout
, wxPrintDialogData
*data
)
104 #if defined(__WXMSW__) && !defined(__WXUNIVERSAL__)
105 return new wxWindowsPrintPreview( preview
, printout
, data
);
106 #elif defined(__WXMAC__)
107 return new wxMacPrintPreview( preview
, printout
, data
);
108 #elif defined(__WXPM__)
109 return new wxOS2PrintPreview( preview
, printout
, data
);
111 return new wxPostScriptPrintPreview( preview
, printout
, data
);
115 wxPrintPreviewBase
*wxNativePrintFactory::CreatePrintPreview( wxPrintout
*preview
,
116 wxPrintout
*printout
, wxPrintData
*data
)
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
);
125 return new wxPostScriptPrintPreview( preview
, printout
, data
);
129 wxPrintDialogBase
*wxNativePrintFactory::CreatePrintDialog( wxWindow
*parent
,
130 wxPrintDialogData
*data
)
132 #if defined(__WXMSW__) && !defined(__WXUNIVERSAL__)
133 return new wxWindowsPrintDialog( parent
, data
);
134 #elif defined(__WXMAC__)
135 return new wxMacPrintDialog( parent
, data
);
137 return new wxGenericPrintDialog( parent
, data
);
141 wxPrintDialogBase
*wxNativePrintFactory::CreatePrintDialog( wxWindow
*parent
,
144 #if defined(__WXMSW__) && !defined(__WXUNIVERSAL__)
145 return new wxWindowsPrintDialog( parent
, data
);
146 #elif defined(__WXMAC__)
147 return new wxMacPrintDialog( parent
, data
);
149 return new wxGenericPrintDialog( parent
, data
);
153 wxPageSetupDialogBase
*wxNativePrintFactory::CreatePageSetupDialog( wxWindow
*parent
,
154 wxPageSetupDialogData
*data
)
156 #if defined(__WXMSW__) && !defined(__WXUNIVERSAL__)
157 return new wxWindowsPageSetupDialog( parent
, data
);
158 #elif defined(__WXMAC__)
159 return new wxMacPageSetupDialog( parent
, data
);
161 return new wxGenericPageSetupDialog( parent
, data
);
165 bool wxNativePrintFactory::HasPrintSetupDialog()
167 #if defined(__WXMSW__) && !defined(__WXUNIVERSAL__)
169 #elif defined(__WXMAC__)
172 // Only here do we need to provide the print setup
173 // dialog ourselves, the other platforms either have
174 // none, don't make it accessible or let you configure
175 // the printer from the wxPrintDialog anyway.
181 wxDialog
*wxNativePrintFactory::CreatePrintSetupDialog( wxWindow
*parent
,
184 #if defined(__WXMSW__) && !defined(__WXUNIVERSAL__)
188 #elif defined(__WXMAC__)
193 // Only here do we need to provide the print setup
194 // dialog ourselves, the other platforms either have
195 // none, don't make it accessible or let you configure
196 // the printer from the wxPrintDialog anyway.
197 return new wxGenericPrintSetupDialog( parent
, data
);
201 bool wxNativePrintFactory::HasOwnPrintToFile()
203 // Only relevant for PostScript and here the
204 // setup dialog provides no "print to file"
205 // option. In the GNOME setup dialog, the
206 // setup dialog has its own print to file.
210 bool wxNativePrintFactory::HasPrinterLine()
212 // Only relevant for PostScript for now
216 wxString
wxNativePrintFactory::CreatePrinterLine()
218 // Only relevant for PostScript for now
220 // We should query "lpstat -d" here
221 return _("Generic PostScript");
224 bool wxNativePrintFactory::HasStatusLine()
226 // Only relevant for PostScript for now
230 wxString
wxNativePrintFactory::CreateStatusLine()
232 // Only relevant for PostScript for now
234 // We should query "lpstat -r" or "lpstat -p" here
238 wxPrintNativeDataBase
*wxNativePrintFactory::CreatePrintNativeData()
240 #if defined(__WXMSW__) && !defined(__WXUNIVERSAL__)
241 return new wxWindowsPrintNativeData
;
242 #elif defined(__WXMAC__)
243 return new wxMacCarbonPrintData
;
245 return new wxPostScriptPrintNativeData
;
249 //----------------------------------------------------------------------------
250 // wxPrintNativeDataBase
251 //----------------------------------------------------------------------------
253 IMPLEMENT_ABSTRACT_CLASS(wxPrintNativeDataBase
, wxObject
)
255 wxPrintNativeDataBase::wxPrintNativeDataBase()
260 //----------------------------------------------------------------------------
261 // wxPrintFactoryModule
262 //----------------------------------------------------------------------------
264 class wxPrintFactoryModule
: public wxModule
267 wxPrintFactoryModule() {}
268 bool OnInit() { return true; }
269 void OnExit() { wxPrintFactory::SetPrintFactory( NULL
); }
272 DECLARE_DYNAMIC_CLASS(wxPrintFactoryModule
)
275 IMPLEMENT_DYNAMIC_CLASS(wxPrintFactoryModule
, wxModule
)
277 //----------------------------------------------------------------------------
279 //----------------------------------------------------------------------------
281 IMPLEMENT_CLASS(wxPrinterBase
, wxObject
)
283 wxPrinterBase::wxPrinterBase(wxPrintDialogData
*data
)
285 m_currentPrintout
= (wxPrintout
*) NULL
;
286 sm_abortWindow
= (wxWindow
*) NULL
;
289 m_printDialogData
= (*data
);
290 sm_lastError
= wxPRINTER_NO_ERROR
;
293 wxWindow
*wxPrinterBase::sm_abortWindow
= (wxWindow
*) NULL
;
294 bool wxPrinterBase::sm_abortIt
= false;
295 wxPrinterError
wxPrinterBase::sm_lastError
= wxPRINTER_NO_ERROR
;
297 wxPrinterBase::~wxPrinterBase()
301 wxWindow
*wxPrinterBase::CreateAbortWindow(wxWindow
*parent
, wxPrintout
* printout
)
303 wxPrintAbortDialog
*dialog
= new wxPrintAbortDialog(parent
, _("Printing ") , wxDefaultPosition
, wxDefaultSize
, wxDEFAULT_DIALOG_STYLE
);
305 wxBoxSizer
*button_sizer
= new wxBoxSizer( wxVERTICAL
);
306 button_sizer
->Add( new wxStaticText(dialog
, wxID_ANY
, _("Please wait while printing\n") + printout
->GetTitle() ), 0, wxALL
, 10 );
307 button_sizer
->Add( new wxButton( dialog
, wxID_CANCEL
, wxT("Cancel") ), 0, wxALL
| wxALIGN_CENTER
, 10 );
309 dialog
->SetAutoLayout( true );
310 dialog
->SetSizer( button_sizer
);
312 button_sizer
->Fit(dialog
);
313 button_sizer
->SetSizeHints (dialog
) ;
318 void wxPrinterBase::ReportError(wxWindow
*parent
, wxPrintout
*WXUNUSED(printout
), const wxString
& message
)
320 wxMessageBox(message
, _("Printing Error"), wxOK
, parent
);
323 wxPrintDialogData
& wxPrinterBase::GetPrintDialogData() const
325 return (wxPrintDialogData
&) m_printDialogData
;
328 //----------------------------------------------------------------------------
330 //----------------------------------------------------------------------------
332 IMPLEMENT_CLASS(wxPrinter
, wxPrinterBase
)
334 wxPrinter::wxPrinter(wxPrintDialogData
*data
)
336 m_pimpl
= wxPrintFactory::GetFactory()->CreatePrinter( data
);
339 wxPrinter::~wxPrinter()
344 wxWindow
*wxPrinter::CreateAbortWindow(wxWindow
*parent
, wxPrintout
*printout
)
346 return m_pimpl
->CreateAbortWindow( parent
, printout
);
349 void wxPrinter::ReportError(wxWindow
*parent
, wxPrintout
*printout
, const wxString
& message
)
351 m_pimpl
->ReportError( parent
, printout
, message
);
354 bool wxPrinter::Setup(wxWindow
*parent
)
356 return m_pimpl
->Setup( parent
);
359 bool wxPrinter::Print(wxWindow
*parent
, wxPrintout
*printout
, bool prompt
)
361 return m_pimpl
->Print( parent
, printout
, prompt
);
364 wxDC
* wxPrinter::PrintDialog(wxWindow
*parent
)
366 return m_pimpl
->PrintDialog( parent
);
369 wxPrintDialogData
& wxPrinter::GetPrintDialogData() const
371 return m_pimpl
->GetPrintDialogData();
374 // ---------------------------------------------------------------------------
375 // wxPrintDialogBase: the dialog for printing.
376 // ---------------------------------------------------------------------------
378 IMPLEMENT_ABSTRACT_CLASS(wxPrintDialogBase
, wxDialog
)
380 wxPrintDialogBase::wxPrintDialogBase(wxWindow
*parent
,
382 const wxString
&title
,
386 : wxDialog( parent
, id
, title
.empty() ? wxString(_("Print")) : title
,
391 // ---------------------------------------------------------------------------
392 // wxPrintDialog: the dialog for printing
393 // ---------------------------------------------------------------------------
395 IMPLEMENT_CLASS(wxPrintDialog
, wxObject
)
397 wxPrintDialog::wxPrintDialog(wxWindow
*parent
, wxPrintDialogData
* data
)
399 m_pimpl
= wxPrintFactory::GetFactory()->CreatePrintDialog( parent
, data
);
402 wxPrintDialog::wxPrintDialog(wxWindow
*parent
, wxPrintData
* data
)
404 m_pimpl
= wxPrintFactory::GetFactory()->CreatePrintDialog( parent
, data
);
407 wxPrintDialog::~wxPrintDialog()
412 int wxPrintDialog::ShowModal()
414 return m_pimpl
->ShowModal();
417 wxPrintDialogData
& wxPrintDialog::GetPrintDialogData()
419 return m_pimpl
->GetPrintDialogData();
422 wxPrintData
& wxPrintDialog::GetPrintData()
424 return m_pimpl
->GetPrintData();
427 wxDC
*wxPrintDialog::GetPrintDC()
429 return m_pimpl
->GetPrintDC();
432 // ---------------------------------------------------------------------------
433 // wxPageSetupDialogBase: the page setup dialog
434 // ---------------------------------------------------------------------------
436 IMPLEMENT_ABSTRACT_CLASS(wxPageSetupDialogBase
, wxDialog
)
438 wxPageSetupDialogBase::wxPageSetupDialogBase(wxWindow
*parent
,
440 const wxString
&title
,
444 : wxDialog( parent
, id
, title
.empty() ? wxString(_("Page setup")) : title
,
449 // ---------------------------------------------------------------------------
450 // wxPageSetupDialog: the page setup dialog
451 // ---------------------------------------------------------------------------
453 IMPLEMENT_CLASS(wxPageSetupDialog
, wxObject
)
455 wxPageSetupDialog::wxPageSetupDialog(wxWindow
*parent
, wxPageSetupDialogData
*data
)
457 m_pimpl
= wxPrintFactory::GetFactory()->CreatePageSetupDialog( parent
, data
);
460 wxPageSetupDialog::~wxPageSetupDialog()
465 int wxPageSetupDialog::ShowModal()
467 return m_pimpl
->ShowModal();
470 wxPageSetupDialogData
& wxPageSetupDialog::GetPageSetupDialogData()
472 return m_pimpl
->GetPageSetupDialogData();
476 wxPageSetupDialogData
& wxPageSetupDialog::GetPageSetupData()
478 return m_pimpl
->GetPageSetupDialogData();
481 //----------------------------------------------------------------------------
482 // wxPrintAbortDialog
483 //----------------------------------------------------------------------------
485 BEGIN_EVENT_TABLE(wxPrintAbortDialog
, wxDialog
)
486 EVT_BUTTON(wxID_CANCEL
, wxPrintAbortDialog::OnCancel
)
489 void wxPrintAbortDialog::OnCancel(wxCommandEvent
& WXUNUSED(event
))
491 wxPrinterBase::sm_abortIt
= true;
492 wxPrinterBase::sm_abortWindow
->Show(false);
493 wxPrinterBase::sm_abortWindow
->Close(true);
494 wxPrinterBase::sm_abortWindow
= (wxWindow
*) NULL
;
497 //----------------------------------------------------------------------------
499 //----------------------------------------------------------------------------
501 IMPLEMENT_ABSTRACT_CLASS(wxPrintout
, wxObject
)
503 wxPrintout::wxPrintout(const wxString
& title
)
505 m_printoutTitle
= title
;
506 m_printoutDC
= (wxDC
*) NULL
;
509 m_pageWidthPixels
= 0;
510 m_pageHeightPixels
= 0;
518 wxPrintout::~wxPrintout()
522 bool wxPrintout::OnBeginDocument(int WXUNUSED(startPage
), int WXUNUSED(endPage
))
524 return GetDC()->StartDoc(_("Printing ") + m_printoutTitle
);
527 void wxPrintout::OnEndDocument()
532 void wxPrintout::OnBeginPrinting()
536 void wxPrintout::OnEndPrinting()
540 bool wxPrintout::HasPage(int page
)
545 void wxPrintout::GetPageInfo(int *minPage
, int *maxPage
, int *fromPage
, int *toPage
)
553 //----------------------------------------------------------------------------
555 //----------------------------------------------------------------------------
557 IMPLEMENT_CLASS(wxPreviewCanvas
, wxWindow
)
559 BEGIN_EVENT_TABLE(wxPreviewCanvas
, wxScrolledWindow
)
560 EVT_PAINT(wxPreviewCanvas::OnPaint
)
561 EVT_CHAR(wxPreviewCanvas::OnChar
)
562 EVT_SYS_COLOUR_CHANGED(wxPreviewCanvas::OnSysColourChanged
)
564 EVT_MOUSEWHEEL(wxPreviewCanvas::OnMouseWheel
)
568 // VZ: the current code doesn't refresh properly without
569 // wxFULL_REPAINT_ON_RESIZE, this must be fixed as otherwise we have
570 // really horrible flicker when resizing the preview frame, but without
571 // this style it simply doesn't work correctly at all...
572 wxPreviewCanvas::wxPreviewCanvas(wxPrintPreviewBase
*preview
, wxWindow
*parent
,
573 const wxPoint
& pos
, const wxSize
& size
, long style
, const wxString
& name
):
574 wxScrolledWindow(parent
, wxID_ANY
, pos
, size
, style
| wxFULL_REPAINT_ON_RESIZE
, name
)
576 m_printPreview
= preview
;
578 // The app workspace colour is always white, but we should have
579 // a contrast with the page.
580 wxSystemColour colourIndex
= wxSYS_COLOUR_3DDKSHADOW
;
582 wxSystemColour colourIndex
= wxSYS_COLOUR_APPWORKSPACE
;
584 SetBackgroundColour(wxSystemSettings::GetColour(colourIndex
));
586 SetScrollbars(10, 10, 100, 100);
589 wxPreviewCanvas::~wxPreviewCanvas()
593 void wxPreviewCanvas::OnPaint(wxPaintEvent
& WXUNUSED(event
))
600 if (!GetUpdateRegion().IsEmpty())
601 dc.SetClippingRegion( GetUpdateRegion() );
607 m_printPreview
->PaintPage(this, dc
);
611 // Responds to colour changes, and passes event on to children.
612 void wxPreviewCanvas::OnSysColourChanged(wxSysColourChangedEvent
& event
)
615 // The app workspace colour is always white, but we should have
616 // a contrast with the page.
617 wxSystemColour colourIndex
= wxSYS_COLOUR_3DDKSHADOW
;
619 wxSystemColour colourIndex
= wxSYS_COLOUR_APPWORKSPACE
;
621 SetBackgroundColour(wxSystemSettings::GetColour(colourIndex
));
624 // Propagate the event to the non-top-level children
625 wxWindow::OnSysColourChanged(event
);
628 void wxPreviewCanvas::OnChar(wxKeyEvent
&event
)
630 wxPreviewControlBar
* controlBar
= ((wxPreviewFrame
*) GetParent())->GetControlBar();
631 if (event
.GetKeyCode() == WXK_ESCAPE
)
633 ((wxPreviewFrame
*) GetParent())->Close(true);
636 else if (event
.GetKeyCode() == WXK_TAB
)
638 controlBar
->OnGoto();
641 else if (event
.GetKeyCode() == WXK_RETURN
)
643 controlBar
->OnPrint();
647 if (!event
.ControlDown())
653 switch(event
.GetKeyCode())
656 controlBar
->OnNext(); break;
658 controlBar
->OnPrevious(); break;
660 controlBar
->OnFirst(); break;
662 controlBar
->OnLast(); break;
670 void wxPreviewCanvas::OnMouseWheel(wxMouseEvent
& event
)
672 wxPreviewControlBar
*
673 controlBar
= wxStaticCast(GetParent(), wxPreviewFrame
)->GetControlBar();
677 if ( event
.ControlDown() && event
.GetWheelRotation() != 0 )
679 int currentZoom
= controlBar
->GetZoomControl();
682 if ( currentZoom
< 100 )
684 else if ( currentZoom
<= 120 )
689 if ( event
.GetWheelRotation() > 0 )
692 int newZoom
= currentZoom
+ delta
;
697 if ( newZoom
!= currentZoom
)
699 controlBar
->SetZoomControl(newZoom
);
700 m_printPreview
->SetZoom(newZoom
);
710 #endif // wxUSE_MOUSEWHEEL
712 //----------------------------------------------------------------------------
713 // wxPreviewControlBar
714 //----------------------------------------------------------------------------
716 IMPLEMENT_CLASS(wxPreviewControlBar
, wxWindow
)
718 BEGIN_EVENT_TABLE(wxPreviewControlBar
, wxPanel
)
719 EVT_BUTTON(wxID_PREVIEW_CLOSE
, wxPreviewControlBar::OnWindowClose
)
720 EVT_BUTTON(wxID_PREVIEW_PRINT
, wxPreviewControlBar::OnPrintButton
)
721 EVT_BUTTON(wxID_PREVIEW_PREVIOUS
, wxPreviewControlBar::OnPreviousButton
)
722 EVT_BUTTON(wxID_PREVIEW_NEXT
, wxPreviewControlBar::OnNextButton
)
723 EVT_BUTTON(wxID_PREVIEW_FIRST
, wxPreviewControlBar::OnFirstButton
)
724 EVT_BUTTON(wxID_PREVIEW_LAST
, wxPreviewControlBar::OnLastButton
)
725 EVT_BUTTON(wxID_PREVIEW_GOTO
, wxPreviewControlBar::OnGotoButton
)
726 EVT_CHOICE(wxID_PREVIEW_ZOOM
, wxPreviewControlBar::OnZoom
)
727 EVT_PAINT(wxPreviewControlBar::OnPaint
)
730 wxPreviewControlBar::wxPreviewControlBar(wxPrintPreviewBase
*preview
, long buttons
,
731 wxWindow
*parent
, const wxPoint
& pos
, const wxSize
& size
,
732 long style
, const wxString
& name
):
733 wxPanel(parent
, wxID_ANY
, pos
, size
, style
, name
)
735 m_printPreview
= preview
;
736 m_closeButton
= (wxButton
*) NULL
;
737 m_nextPageButton
= (wxButton
*) NULL
;
738 m_previousPageButton
= (wxButton
*) NULL
;
739 m_printButton
= (wxButton
*) NULL
;
740 m_zoomControl
= (wxChoice
*) NULL
;
741 m_buttonFlags
= buttons
;
744 wxPreviewControlBar::~wxPreviewControlBar()
748 void wxPreviewControlBar::OnPaint(wxPaintEvent
& WXUNUSED(event
))
754 dc
.SetPen(*wxBLACK_PEN
);
755 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
756 dc
.DrawLine( 0, h
-1, w
, h
-1 );
759 void wxPreviewControlBar::OnWindowClose(wxCommandEvent
& WXUNUSED(event
))
761 wxPreviewFrame
*frame
= (wxPreviewFrame
*)GetParent();
765 void wxPreviewControlBar::OnPrint(void)
767 wxPrintPreviewBase
*preview
= GetPrintPreview();
768 preview
->Print(true);
771 void wxPreviewControlBar::OnNext(void)
773 wxPrintPreviewBase
*preview
= GetPrintPreview();
776 int currentPage
= preview
->GetCurrentPage();
777 if ((preview
->GetMaxPage() > 0) &&
778 (currentPage
< preview
->GetMaxPage()) &&
779 preview
->GetPrintout()->HasPage(currentPage
+ 1))
781 preview
->SetCurrentPage(currentPage
+ 1);
786 void wxPreviewControlBar::OnPrevious(void)
788 wxPrintPreviewBase
*preview
= GetPrintPreview();
791 int currentPage
= preview
->GetCurrentPage();
792 if ((preview
->GetMinPage() > 0) &&
793 (currentPage
> preview
->GetMinPage()) &&
794 preview
->GetPrintout()->HasPage(currentPage
- 1))
796 preview
->SetCurrentPage(currentPage
- 1);
801 void wxPreviewControlBar::OnFirst(void)
803 wxPrintPreviewBase
*preview
= GetPrintPreview();
806 int currentPage
= preview
->GetMinPage();
807 if (preview
->GetPrintout()->HasPage(currentPage
))
809 preview
->SetCurrentPage(currentPage
);
814 void wxPreviewControlBar::OnLast(void)
816 wxPrintPreviewBase
*preview
= GetPrintPreview();
819 int currentPage
= preview
->GetMaxPage();
820 if (preview
->GetPrintout()->HasPage(currentPage
))
822 preview
->SetCurrentPage(currentPage
);
827 void wxPreviewControlBar::OnGoto(void)
829 wxPrintPreviewBase
*preview
= GetPrintPreview();
834 if (preview
->GetMinPage() > 0)
839 strPrompt
.Printf( _("Enter a page number between %d and %d:"),
840 preview
->GetMinPage(), preview
->GetMaxPage());
841 strPage
.Printf( wxT("%d"), preview
->GetCurrentPage() );
844 wxGetTextFromUser( strPrompt
, _("Goto Page"), strPage
, GetParent());
846 if ( strPage
.ToLong( ¤tPage
) )
847 if (preview
->GetPrintout()->HasPage(currentPage
))
849 preview
->SetCurrentPage(currentPage
);
855 void wxPreviewControlBar::OnZoom(wxCommandEvent
& WXUNUSED(event
))
857 int zoom
= GetZoomControl();
858 if (GetPrintPreview())
859 GetPrintPreview()->SetZoom(zoom
);
862 void wxPreviewControlBar::CreateButtons()
864 SetSize(0, 0, 400, 40);
866 wxBoxSizer
*item0
= new wxBoxSizer( wxHORIZONTAL
);
868 m_closeButton
= new wxButton( this, wxID_PREVIEW_CLOSE
, _("&Close"), wxDefaultPosition
, wxDefaultSize
, 0 );
869 item0
->Add( m_closeButton
, 0, wxALIGN_CENTRE
|wxALL
, 5 );
871 if (m_buttonFlags
& wxPREVIEW_PRINT
)
873 m_printButton
= new wxButton( this, wxID_PREVIEW_PRINT
, _("&Print..."), wxDefaultPosition
, wxDefaultSize
, 0 );
874 item0
->Add( m_printButton
, 0, wxALIGN_CENTRE
|wxALL
, 5 );
877 if (m_buttonFlags
& wxPREVIEW_FIRST
)
879 m_firstPageButton
= new wxButton( this, wxID_PREVIEW_FIRST
, _("|<<"), wxDefaultPosition
, wxDefaultSize
, wxBU_EXACTFIT
);
880 item0
->Add( m_firstPageButton
, 0, wxALIGN_CENTRE
|wxALL
, 5 );
883 if (m_buttonFlags
& wxPREVIEW_PREVIOUS
)
885 m_previousPageButton
= new wxButton( this, wxID_PREVIEW_PREVIOUS
, _("<<"), wxDefaultPosition
, wxDefaultSize
, wxBU_EXACTFIT
);
886 item0
->Add( m_previousPageButton
, 0, wxALIGN_CENTRE
|wxRIGHT
|wxTOP
|wxBOTTOM
, 5 );
889 if (m_buttonFlags
& wxPREVIEW_NEXT
)
891 m_nextPageButton
= new wxButton( this, wxID_PREVIEW_NEXT
, _(">>"), wxDefaultPosition
, wxDefaultSize
, wxBU_EXACTFIT
);
892 item0
->Add( m_nextPageButton
, 0, wxALIGN_CENTRE
|wxRIGHT
|wxTOP
|wxBOTTOM
, 5 );
895 if (m_buttonFlags
& wxPREVIEW_LAST
)
897 m_lastPageButton
= new wxButton( this, wxID_PREVIEW_LAST
, _(">>|"), wxDefaultPosition
, wxDefaultSize
, wxBU_EXACTFIT
);
898 item0
->Add( m_lastPageButton
, 0, wxALIGN_CENTRE
|wxRIGHT
|wxTOP
|wxBOTTOM
, 5 );
901 if (m_buttonFlags
& wxPREVIEW_GOTO
)
903 m_gotoPageButton
= new wxButton( this, wxID_PREVIEW_GOTO
, _("&Goto..."), wxDefaultPosition
, wxDefaultSize
, 0 );
904 item0
->Add( m_gotoPageButton
, 0, wxALIGN_CENTRE
|wxALL
, 5 );
907 if (m_buttonFlags
& wxPREVIEW_ZOOM
)
911 wxT("10%"), wxT("15%"), wxT("20%"), wxT("25%"), wxT("30%"), wxT("35%"), wxT("40%"), wxT("45%"), wxT("50%"), wxT("55%"),
912 wxT("60%"), wxT("65%"), wxT("70%"), wxT("75%"), wxT("80%"), wxT("85%"), wxT("90%"), wxT("95%"), wxT("100%"), wxT("110%"),
913 wxT("120%"), wxT("150%"), wxT("200%")
915 int n
= WXSIZEOF(choices
);
917 m_zoomControl
= new wxChoice( this, wxID_PREVIEW_ZOOM
, wxDefaultPosition
, wxSize(70,wxDefaultCoord
), n
, choices
, 0 );
918 item0
->Add( m_zoomControl
, 0, wxALIGN_CENTRE
|wxALL
, 5 );
919 SetZoomControl(m_printPreview
->GetZoom());
926 void wxPreviewControlBar::SetZoomControl(int zoom
)
930 int n
, count
= m_zoomControl
->GetCount();
932 for (n
=0; n
<count
; n
++)
934 if (m_zoomControl
->GetString(n
).BeforeFirst(wxT('%')).ToLong(&val
) &&
937 m_zoomControl
->SetSelection(n
);
942 m_zoomControl
->SetSelection(count
-1);
946 int wxPreviewControlBar::GetZoomControl()
948 if (m_zoomControl
&& (m_zoomControl
->GetStringSelection() != wxEmptyString
))
951 if (m_zoomControl
->GetStringSelection().BeforeFirst(wxT('%')).ToLong(&val
))
963 IMPLEMENT_CLASS(wxPreviewFrame
, wxFrame
)
965 BEGIN_EVENT_TABLE(wxPreviewFrame
, wxFrame
)
966 EVT_CLOSE(wxPreviewFrame::OnCloseWindow
)
969 wxPreviewFrame::wxPreviewFrame(wxPrintPreviewBase
*preview
, wxWindow
*parent
, const wxString
& title
,
970 const wxPoint
& pos
, const wxSize
& size
, long style
, const wxString
& name
):
971 wxFrame(parent
, wxID_ANY
, title
, pos
, size
, style
, name
)
973 m_printPreview
= preview
;
975 m_previewCanvas
= NULL
;
976 m_windowDisabler
= NULL
;
978 // Give the application icon
980 wxFrame
* topFrame
= wxDynamicCast(wxTheApp
->GetTopWindow(), wxFrame
);
982 SetIcon(topFrame
->GetIcon());
986 wxPreviewFrame::~wxPreviewFrame()
990 void wxPreviewFrame::OnCloseWindow(wxCloseEvent
& WXUNUSED(event
))
992 if (m_windowDisabler
)
993 delete m_windowDisabler
;
995 // Need to delete the printout and the print preview
996 wxPrintout
*printout
= m_printPreview
->GetPrintout();
1000 m_printPreview
->SetPrintout(NULL
);
1001 m_printPreview
->SetCanvas(NULL
);
1002 m_printPreview
->SetFrame(NULL
);
1004 delete m_printPreview
;
1009 void wxPreviewFrame::Initialize()
1017 m_printPreview
->SetCanvas(m_previewCanvas
);
1018 m_printPreview
->SetFrame(this);
1020 wxBoxSizer
*item0
= new wxBoxSizer( wxVERTICAL
);
1022 item0
->Add( m_controlBar
, 0, wxGROW
|wxALIGN_CENTER_VERTICAL
, 5 );
1023 item0
->Add( m_previewCanvas
, 1, wxGROW
|wxALIGN_CENTER_VERTICAL
, 5 );
1025 SetAutoLayout( true );
1028 m_windowDisabler
= new wxWindowDisabler(this);
1032 m_printPreview
->AdjustScrollbars(m_previewCanvas
);
1033 m_previewCanvas
->SetFocus();
1034 m_controlBar
->SetFocus();
1037 void wxPreviewFrame::CreateCanvas()
1039 m_previewCanvas
= new wxPreviewCanvas(m_printPreview
, this);
1042 void wxPreviewFrame::CreateControlBar()
1044 long buttons
= wxPREVIEW_DEFAULT
;
1045 if (m_printPreview
->GetPrintoutForPrinting())
1046 buttons
|= wxPREVIEW_PRINT
;
1048 m_controlBar
= new wxPreviewControlBar(m_printPreview
, buttons
, this, wxPoint(0,0), wxSize(400, 40));
1049 m_controlBar
->CreateButtons();
1056 IMPLEMENT_CLASS(wxPrintPreviewBase
, wxObject
)
1058 wxPrintPreviewBase::wxPrintPreviewBase(wxPrintout
*printout
,
1059 wxPrintout
*printoutForPrinting
,
1063 m_printDialogData
= (*data
);
1065 Init(printout
, printoutForPrinting
);
1068 wxPrintPreviewBase::wxPrintPreviewBase(wxPrintout
*printout
,
1069 wxPrintout
*printoutForPrinting
,
1070 wxPrintDialogData
*data
)
1073 m_printDialogData
= (*data
);
1075 Init(printout
, printoutForPrinting
);
1078 void wxPrintPreviewBase::Init(wxPrintout
*printout
,
1079 wxPrintout
*printoutForPrinting
)
1082 m_previewPrintout
= printout
;
1083 if (m_previewPrintout
)
1084 m_previewPrintout
->SetIsPreview(true);
1086 m_printPrintout
= printoutForPrinting
;
1088 m_previewCanvas
= NULL
;
1089 m_previewFrame
= NULL
;
1090 m_previewBitmap
= NULL
;
1097 m_printingPrepared
= false;
1102 wxPrintPreviewBase::~wxPrintPreviewBase()
1104 if (m_previewPrintout
)
1105 delete m_previewPrintout
;
1106 if (m_previewBitmap
)
1107 delete m_previewBitmap
;
1108 if (m_printPrintout
)
1109 delete m_printPrintout
;
1112 bool wxPrintPreviewBase::SetCurrentPage(int pageNum
)
1114 if (m_currentPage
== pageNum
)
1117 m_currentPage
= pageNum
;
1118 if (m_previewBitmap
)
1120 delete m_previewBitmap
;
1121 m_previewBitmap
= NULL
;
1124 if (m_previewCanvas
)
1126 AdjustScrollbars(m_previewCanvas
);
1128 if (!RenderPage(pageNum
))
1130 m_previewCanvas
->Refresh();
1131 m_previewCanvas
->SetFocus();
1136 int wxPrintPreviewBase::GetCurrentPage() const
1137 { return m_currentPage
; }
1138 void wxPrintPreviewBase::SetPrintout(wxPrintout
*printout
)
1139 { m_previewPrintout
= printout
; }
1140 wxPrintout
*wxPrintPreviewBase::GetPrintout() const
1141 { return m_previewPrintout
; }
1142 wxPrintout
*wxPrintPreviewBase::GetPrintoutForPrinting() const
1143 { return m_printPrintout
; }
1144 void wxPrintPreviewBase::SetFrame(wxFrame
*frame
)
1145 { m_previewFrame
= frame
; }
1146 void wxPrintPreviewBase::SetCanvas(wxPreviewCanvas
*canvas
)
1147 { m_previewCanvas
= canvas
; }
1148 wxFrame
*wxPrintPreviewBase::GetFrame() const
1149 { return m_previewFrame
; }
1150 wxPreviewCanvas
*wxPrintPreviewBase::GetCanvas() const
1151 { return m_previewCanvas
; }
1153 bool wxPrintPreviewBase::PaintPage(wxPreviewCanvas
*canvas
, wxDC
& dc
)
1155 DrawBlankPage(canvas
, dc
);
1157 if (!m_previewBitmap
)
1158 if (!RenderPage(m_currentPage
))
1161 if (!m_previewBitmap
)
1167 int canvasWidth
, canvasHeight
;
1168 canvas
->GetSize(&canvasWidth
, &canvasHeight
);
1170 double zoomScale
= ((float)m_currentZoom
/(float)100);
1171 double actualWidth
= (zoomScale
*m_pageWidth
*m_previewScale
);
1172 // float actualHeight = (float)(zoomScale*m_pageHeight*m_previewScale);
1174 int x
= (int) ((canvasWidth
- actualWidth
)/2.0);
1175 if (x
< m_leftMargin
)
1177 int y
= m_topMargin
;
1180 temp_dc
.SelectObject(*m_previewBitmap
);
1182 dc
.Blit(x
, y
, m_previewBitmap
->GetWidth(), m_previewBitmap
->GetHeight(), &temp_dc
, 0, 0);
1184 temp_dc
.SelectObject(wxNullBitmap
);
1189 // Adjusts the scrollbars for the current scale
1190 void wxPrintPreviewBase::AdjustScrollbars(wxPreviewCanvas
*canvas
)
1195 int canvasWidth
, canvasHeight
;
1196 canvas
->GetSize(&canvasWidth
, &canvasHeight
);
1198 double zoomScale
= ((float)m_currentZoom
/(float)100);
1199 double actualWidth
= (zoomScale
*m_pageWidth
*m_previewScale
);
1200 double actualHeight
= (zoomScale
*m_pageHeight
*m_previewScale
);
1202 // Set the scrollbars appropriately
1203 int totalWidth
= (int)(actualWidth
+ 2*m_leftMargin
);
1204 int totalHeight
= (int)(actualHeight
+ 2*m_topMargin
);
1205 int scrollUnitsX
= totalWidth
/10;
1206 int scrollUnitsY
= totalHeight
/10;
1207 wxSize virtualSize
= canvas
->GetVirtualSize();
1208 if (virtualSize
.GetWidth() != totalWidth
|| virtualSize
.GetHeight() != totalHeight
)
1209 canvas
->SetScrollbars(10, 10, scrollUnitsX
, scrollUnitsY
, 0, 0, true);
1212 bool wxPrintPreviewBase::RenderPage(int pageNum
)
1216 int canvasWidth
, canvasHeight
;
1218 if (!m_previewCanvas
)
1220 wxFAIL_MSG(_T("wxPrintPreviewBase::RenderPage: must use wxPrintPreviewBase::SetCanvas to let me know about the canvas!"));
1224 m_previewCanvas
->GetSize(&canvasWidth
, &canvasHeight
);
1226 double zoomScale
= (m_currentZoom
/100.0);
1227 int actualWidth
= (int)(zoomScale
*m_pageWidth
*m_previewScale
);
1228 int actualHeight
= (int)(zoomScale
*m_pageHeight
*m_previewScale
);
1230 if (!m_previewBitmap
)
1232 m_previewBitmap
= new wxBitmap((int)actualWidth
, (int)actualHeight
);
1233 if (!m_previewBitmap
|| !m_previewBitmap
->Ok())
1235 if (m_previewBitmap
) {
1236 delete m_previewBitmap
;
1237 m_previewBitmap
= NULL
;
1239 wxMessageBox(_("Sorry, not enough memory to create a preview."), _("Print Preview Failure"), wxOK
);
1244 wxMemoryDC memoryDC
;
1245 memoryDC
.SelectObject(*m_previewBitmap
);
1249 m_previewPrintout
->SetDC(&memoryDC
);
1250 m_previewPrintout
->SetPageSizePixels(m_pageWidth
, m_pageHeight
);
1252 // Need to delay OnPreparePrinting until here, so we have enough information.
1253 if (!m_printingPrepared
)
1255 m_previewPrintout
->OnPreparePrinting();
1257 m_previewPrintout
->GetPageInfo(&m_minPage
, &m_maxPage
, &selFrom
, &selTo
);
1258 m_printingPrepared
= true;
1261 m_previewPrintout
->OnBeginPrinting();
1263 if (!m_previewPrintout
->OnBeginDocument(m_printDialogData
.GetFromPage(), m_printDialogData
.GetToPage()))
1265 wxMessageBox(_("Could not start document preview."), _("Print Preview Failure"), wxOK
);
1267 memoryDC
.SelectObject(wxNullBitmap
);
1269 delete m_previewBitmap
;
1270 m_previewBitmap
= NULL
;
1274 m_previewPrintout
->OnPrintPage(pageNum
);
1275 m_previewPrintout
->OnEndDocument();
1276 m_previewPrintout
->OnEndPrinting();
1278 m_previewPrintout
->SetDC(NULL
);
1280 memoryDC
.SelectObject(wxNullBitmap
);
1285 status
= wxString::Format(_("Page %d of %d"), pageNum
, m_maxPage
);
1287 status
= wxString::Format(_("Page %d"), pageNum
);
1290 m_previewFrame
->SetStatusText(status
);
1297 bool wxPrintPreviewBase::DrawBlankPage(wxPreviewCanvas
*canvas
, wxDC
& dc
)
1299 int canvasWidth
, canvasHeight
;
1300 canvas
->GetSize(&canvasWidth
, &canvasHeight
);
1302 float zoomScale
= (float)((float)m_currentZoom
/(float)100);
1303 float actualWidth
= zoomScale
*m_pageWidth
*m_previewScale
;
1304 float actualHeight
= zoomScale
*m_pageHeight
*m_previewScale
;
1306 float x
= (float)((canvasWidth
- actualWidth
)/2.0);
1307 if (x
< m_leftMargin
)
1308 x
= (float)m_leftMargin
;
1309 float y
= (float)m_topMargin
;
1311 // Draw shadow, allowing for 1-pixel border AROUND the actual page
1312 int shadowOffset
= 4;
1313 dc
.SetPen(*wxBLACK_PEN
);
1314 dc
.SetBrush(*wxBLACK_BRUSH
);
1316 dc.DrawRectangle((int)(x-1 + shadowOffset), (int)(y-1 + shadowOffset), (int)(actualWidth+2), (int)(actualHeight+2));
1318 dc
.DrawRectangle((int)(x
+ shadowOffset
), (int)(y
+ actualHeight
+1), (int)(actualWidth
), shadowOffset
);
1319 dc
.DrawRectangle((int)(x
+ actualWidth
), (int)(y
+ shadowOffset
), shadowOffset
, (int)(actualHeight
));
1321 // Draw blank page allowing for 1-pixel border AROUND the actual page
1322 dc
.SetPen(*wxBLACK_PEN
);
1323 dc
.SetBrush(*wxWHITE_BRUSH
);
1326 wxRegion update_region = canvas->GetUpdateRegion();
1327 wxRect r = update_region.GetBox();
1329 printf( "x: %d y: %d w: %d h: %d.\n", (int)r.x, (int)r.y, (int)r.width, (int)r.height );
1332 dc
.DrawRectangle((int)(x
-2), (int)(y
-1), (int)(actualWidth
+3), (int)(actualHeight
+2));
1337 void wxPrintPreviewBase::SetZoom(int percent
)
1339 if (m_currentZoom
== percent
)
1342 m_currentZoom
= percent
;
1343 if (m_previewBitmap
)
1345 delete m_previewBitmap
;
1346 m_previewBitmap
= NULL
;
1349 if (m_previewCanvas
)
1351 AdjustScrollbars(m_previewCanvas
);
1352 RenderPage(m_currentPage
);
1353 ((wxScrolledWindow
*) m_previewCanvas
)->Scroll(0, 0);
1354 m_previewCanvas
->ClearBackground();
1355 m_previewCanvas
->Refresh();
1356 m_previewCanvas
->SetFocus();
1360 wxPrintDialogData
& wxPrintPreviewBase::GetPrintDialogData()
1362 return m_printDialogData
;
1365 int wxPrintPreviewBase::GetZoom() const
1366 { return m_currentZoom
; }
1367 int wxPrintPreviewBase::GetMaxPage() const
1368 { return m_maxPage
; }
1369 int wxPrintPreviewBase::GetMinPage() const
1370 { return m_minPage
; }
1371 bool wxPrintPreviewBase::Ok() const
1373 void wxPrintPreviewBase::SetOk(bool ok
)
1375 //----------------------------------------------------------------------------
1377 //----------------------------------------------------------------------------
1379 IMPLEMENT_CLASS(wxPrintPreview
, wxPrintPreviewBase
)
1381 wxPrintPreview::wxPrintPreview(wxPrintout
*printout
,
1382 wxPrintout
*printoutForPrinting
,
1383 wxPrintDialogData
*data
) :
1384 wxPrintPreviewBase( printout
, printoutForPrinting
, data
)
1386 m_pimpl
= wxPrintFactory::GetFactory()->
1387 CreatePrintPreview( printout
, printoutForPrinting
, data
);
1390 wxPrintPreview::wxPrintPreview(wxPrintout
*printout
,
1391 wxPrintout
*printoutForPrinting
,
1392 wxPrintData
*data
) :
1393 wxPrintPreviewBase( printout
, printoutForPrinting
, data
)
1395 m_pimpl
= wxPrintFactory::GetFactory()->
1396 CreatePrintPreview( printout
, printoutForPrinting
, data
);
1399 wxPrintPreview::~wxPrintPreview()
1403 // don't delete twice
1404 m_printPrintout
= NULL
;
1405 m_previewPrintout
= NULL
;
1406 m_previewBitmap
= NULL
;
1409 bool wxPrintPreview::SetCurrentPage(int pageNum
)
1411 return m_pimpl
->SetCurrentPage( pageNum
);
1414 int wxPrintPreview::GetCurrentPage() const
1416 return m_pimpl
->GetCurrentPage();
1419 void wxPrintPreview::SetPrintout(wxPrintout
*printout
)
1421 m_pimpl
->SetPrintout( printout
);
1424 wxPrintout
*wxPrintPreview::GetPrintout() const
1426 return m_pimpl
->GetPrintout();
1429 wxPrintout
*wxPrintPreview::GetPrintoutForPrinting() const
1431 return m_pimpl
->GetPrintoutForPrinting();
1434 void wxPrintPreview::SetFrame(wxFrame
*frame
)
1436 m_pimpl
->SetFrame( frame
);
1439 void wxPrintPreview::SetCanvas(wxPreviewCanvas
*canvas
)
1441 m_pimpl
->SetCanvas( canvas
);
1444 wxFrame
*wxPrintPreview::GetFrame() const
1446 return m_pimpl
->GetFrame();
1449 wxPreviewCanvas
*wxPrintPreview::GetCanvas() const
1451 return m_pimpl
->GetCanvas();
1454 bool wxPrintPreview::PaintPage(wxPreviewCanvas
*canvas
, wxDC
& dc
)
1456 return m_pimpl
->PaintPage( canvas
, dc
);
1459 bool wxPrintPreview::DrawBlankPage(wxPreviewCanvas
*canvas
, wxDC
& dc
)
1461 return m_pimpl
->DrawBlankPage( canvas
, dc
);
1464 void wxPrintPreview::AdjustScrollbars(wxPreviewCanvas
*canvas
)
1466 m_pimpl
->AdjustScrollbars( canvas
);
1469 bool wxPrintPreview::RenderPage(int pageNum
)
1471 return m_pimpl
->RenderPage( pageNum
);
1474 void wxPrintPreview::SetZoom(int percent
)
1476 m_pimpl
->SetZoom( percent
);
1479 wxPrintDialogData
& wxPrintPreview::GetPrintDialogData()
1481 return m_pimpl
->GetPrintDialogData();
1484 int wxPrintPreview::GetMaxPage() const
1486 return m_pimpl
->GetMaxPage();
1489 int wxPrintPreview::GetMinPage() const
1491 return m_pimpl
->GetMinPage();
1494 bool wxPrintPreview::Ok() const
1496 return m_pimpl
->Ok();
1499 void wxPrintPreview::SetOk(bool ok
)
1501 m_pimpl
->SetOk( ok
);
1504 bool wxPrintPreview::Print(bool interactive
)
1506 return m_pimpl
->Print( interactive
);
1509 void wxPrintPreview::DetermineScaling()
1511 m_pimpl
->DetermineScaling();
1514 #endif // wxUSE_PRINTING_ARCHITECTURE