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
21 #include "wx/dcprint.h"
24 #if defined(__WXMSW__)
25 #include "wx/msw/wrapcdlg.h"
30 #include "wx/msgdlg.h"
31 #include "wx/layout.h"
32 #include "wx/choice.h"
33 #include "wx/button.h"
34 #include "wx/settings.h"
35 #include "wx/dcmemory.h"
36 #include "wx/stattext.h"
38 #include "wx/textdlg.h"
40 #include "wx/module.h"
43 #include "wx/prntbase.h"
44 #include "wx/printdlg.h"
46 #include "wx/dcprint.h"
51 #if defined(__WXMSW__) && !defined(__WXUNIVERSAL__)
52 #include "wx/msw/printdlg.h"
53 #elif defined(__WXMAC__)
54 #include "wx/mac/printdlg.h"
55 #include "wx/mac/private/print.h"
57 #include "wx/generic/prntdlgg.h"
67 //----------------------------------------------------------------------------
69 //----------------------------------------------------------------------------
71 wxPrintFactory
*wxPrintFactory::m_factory
= NULL
;
73 void wxPrintFactory::SetPrintFactory( wxPrintFactory
*factory
)
75 if (wxPrintFactory::m_factory
)
76 delete wxPrintFactory::m_factory
;
78 wxPrintFactory::m_factory
= factory
;
81 wxPrintFactory
*wxPrintFactory::GetFactory()
83 if (!wxPrintFactory::m_factory
)
84 wxPrintFactory::m_factory
= new wxNativePrintFactory
;
86 return wxPrintFactory::m_factory
;
89 //----------------------------------------------------------------------------
90 // wxNativePrintFactory
91 //----------------------------------------------------------------------------
93 wxPrinterBase
*wxNativePrintFactory::CreatePrinter( wxPrintDialogData
*data
)
95 #if defined(__WXMSW__) && !defined(__WXUNIVERSAL__)
96 return new wxWindowsPrinter( data
);
97 #elif defined(__WXMAC__)
98 return new wxMacPrinter( data
);
99 #elif defined(__WXPM__)
100 return new wxOS2Printer( data
);
102 return new wxPostScriptPrinter( data
);
106 wxPrintPreviewBase
*wxNativePrintFactory::CreatePrintPreview( wxPrintout
*preview
,
107 wxPrintout
*printout
, wxPrintDialogData
*data
)
109 #if defined(__WXMSW__) && !defined(__WXUNIVERSAL__)
110 return new wxWindowsPrintPreview( preview
, printout
, data
);
111 #elif defined(__WXMAC__)
112 return new wxMacPrintPreview( preview
, printout
, data
);
113 #elif defined(__WXPM__)
114 return new wxOS2PrintPreview( preview
, printout
, data
);
116 return new wxPostScriptPrintPreview( preview
, printout
, data
);
120 wxPrintPreviewBase
*wxNativePrintFactory::CreatePrintPreview( wxPrintout
*preview
,
121 wxPrintout
*printout
, wxPrintData
*data
)
123 #if defined(__WXMSW__) && !defined(__WXUNIVERSAL__)
124 return new wxWindowsPrintPreview( preview
, printout
, data
);
125 #elif defined(__WXMAC__)
126 return new wxMacPrintPreview( preview
, printout
, data
);
127 #elif defined(__WXPM__)
128 return new wxOS2PrintPreview( preview
, printout
, data
);
130 return new wxPostScriptPrintPreview( preview
, printout
, data
);
134 wxPrintDialogBase
*wxNativePrintFactory::CreatePrintDialog( wxWindow
*parent
,
135 wxPrintDialogData
*data
)
137 #if defined(__WXMSW__) && !defined(__WXUNIVERSAL__)
138 return new wxWindowsPrintDialog( parent
, data
);
139 #elif defined(__WXMAC__)
140 return new wxMacPrintDialog( parent
, data
);
142 return new wxGenericPrintDialog( parent
, data
);
146 wxPrintDialogBase
*wxNativePrintFactory::CreatePrintDialog( wxWindow
*parent
,
149 #if defined(__WXMSW__) && !defined(__WXUNIVERSAL__)
150 return new wxWindowsPrintDialog( parent
, data
);
151 #elif defined(__WXMAC__)
152 return new wxMacPrintDialog( parent
, data
);
154 return new wxGenericPrintDialog( parent
, data
);
158 wxPageSetupDialogBase
*wxNativePrintFactory::CreatePageSetupDialog( wxWindow
*parent
,
159 wxPageSetupDialogData
*data
)
161 #if defined(__WXMSW__) && !defined(__WXUNIVERSAL__)
162 return new wxWindowsPageSetupDialog( parent
, data
);
163 #elif defined(__WXMAC__)
164 return new wxMacPageSetupDialog( parent
, data
);
166 return new wxGenericPageSetupDialog( parent
, data
);
170 bool wxNativePrintFactory::HasPrintSetupDialog()
172 #if defined(__WXMSW__) && !defined(__WXUNIVERSAL__)
174 #elif defined(__WXMAC__)
177 // Only here do we need to provide the print setup
178 // dialog ourselves, the other platforms either have
179 // none, don't make it accessible or let you configure
180 // the printer from the wxPrintDialog anyway.
186 wxDialog
*wxNativePrintFactory::CreatePrintSetupDialog( wxWindow
*parent
,
189 #if defined(__WXMSW__) && !defined(__WXUNIVERSAL__)
193 #elif defined(__WXMAC__)
198 // Only here do we need to provide the print setup
199 // dialog ourselves, the other platforms either have
200 // none, don't make it accessible or let you configure
201 // the printer from the wxPrintDialog anyway.
202 return new wxGenericPrintSetupDialog( parent
, data
);
206 wxDC
* wxNativePrintFactory::CreatePrinterDC( const wxPrintData
& data
)
208 #if defined(__WXMSW__) && !defined(__WXUNIVERSAL__)
209 return new wxPrinterDC(data
);
210 #elif defined(__WXMAC__)
211 return new wxPrinterDC(data
);
213 return new wxPostScriptDC(data
);
217 bool wxNativePrintFactory::HasOwnPrintToFile()
219 // Only relevant for PostScript and here the
220 // setup dialog provides no "print to file"
221 // option. In the GNOME setup dialog, the
222 // setup dialog has its own print to file.
226 bool wxNativePrintFactory::HasPrinterLine()
228 // Only relevant for PostScript for now
232 wxString
wxNativePrintFactory::CreatePrinterLine()
234 // Only relevant for PostScript for now
236 // We should query "lpstat -d" here
237 return _("Generic PostScript");
240 bool wxNativePrintFactory::HasStatusLine()
242 // Only relevant for PostScript for now
246 wxString
wxNativePrintFactory::CreateStatusLine()
248 // Only relevant for PostScript for now
250 // We should query "lpstat -r" or "lpstat -p" here
254 wxPrintNativeDataBase
*wxNativePrintFactory::CreatePrintNativeData()
256 #if defined(__WXMSW__) && !defined(__WXUNIVERSAL__)
257 return new wxWindowsPrintNativeData
;
258 #elif defined(__WXMAC__)
259 return new wxMacCarbonPrintData
;
261 return new wxPostScriptPrintNativeData
;
265 //----------------------------------------------------------------------------
266 // wxPrintNativeDataBase
267 //----------------------------------------------------------------------------
269 IMPLEMENT_ABSTRACT_CLASS(wxPrintNativeDataBase
, wxObject
)
271 wxPrintNativeDataBase::wxPrintNativeDataBase()
276 //----------------------------------------------------------------------------
277 // wxPrintFactoryModule
278 //----------------------------------------------------------------------------
280 class wxPrintFactoryModule
: public wxModule
283 wxPrintFactoryModule() {}
284 bool OnInit() { return true; }
285 void OnExit() { wxPrintFactory::SetPrintFactory( NULL
); }
288 DECLARE_DYNAMIC_CLASS(wxPrintFactoryModule
)
291 IMPLEMENT_DYNAMIC_CLASS(wxPrintFactoryModule
, wxModule
)
293 //----------------------------------------------------------------------------
295 //----------------------------------------------------------------------------
297 IMPLEMENT_CLASS(wxPrinterBase
, wxObject
)
299 wxPrinterBase::wxPrinterBase(wxPrintDialogData
*data
)
301 m_currentPrintout
= (wxPrintout
*) NULL
;
302 sm_abortWindow
= (wxWindow
*) NULL
;
305 m_printDialogData
= (*data
);
306 sm_lastError
= wxPRINTER_NO_ERROR
;
309 wxWindow
*wxPrinterBase::sm_abortWindow
= (wxWindow
*) NULL
;
310 bool wxPrinterBase::sm_abortIt
= false;
311 wxPrinterError
wxPrinterBase::sm_lastError
= wxPRINTER_NO_ERROR
;
313 wxPrinterBase::~wxPrinterBase()
317 wxWindow
*wxPrinterBase::CreateAbortWindow(wxWindow
*parent
, wxPrintout
* printout
)
319 wxPrintAbortDialog
*dialog
= new wxPrintAbortDialog(parent
, _("Printing ") , wxDefaultPosition
, wxDefaultSize
, wxDEFAULT_DIALOG_STYLE
);
321 wxBoxSizer
*button_sizer
= new wxBoxSizer( wxVERTICAL
);
322 button_sizer
->Add( new wxStaticText(dialog
, wxID_ANY
, _("Please wait while printing\n") + printout
->GetTitle() ), 0, wxALL
, 10 );
323 button_sizer
->Add( new wxButton( dialog
, wxID_CANCEL
, wxT("Cancel") ), 0, wxALL
| wxALIGN_CENTER
, 10 );
325 dialog
->SetAutoLayout( true );
326 dialog
->SetSizer( button_sizer
);
328 button_sizer
->Fit(dialog
);
329 button_sizer
->SetSizeHints (dialog
) ;
334 void wxPrinterBase::ReportError(wxWindow
*parent
, wxPrintout
*WXUNUSED(printout
), const wxString
& message
)
336 wxMessageBox(message
, _("Printing Error"), wxOK
, parent
);
339 wxPrintDialogData
& wxPrinterBase::GetPrintDialogData() const
341 return (wxPrintDialogData
&) m_printDialogData
;
344 //----------------------------------------------------------------------------
346 //----------------------------------------------------------------------------
348 IMPLEMENT_CLASS(wxPrinter
, wxPrinterBase
)
350 wxPrinter::wxPrinter(wxPrintDialogData
*data
)
352 m_pimpl
= wxPrintFactory::GetFactory()->CreatePrinter( data
);
355 wxPrinter::~wxPrinter()
360 wxWindow
*wxPrinter::CreateAbortWindow(wxWindow
*parent
, wxPrintout
*printout
)
362 return m_pimpl
->CreateAbortWindow( parent
, printout
);
365 void wxPrinter::ReportError(wxWindow
*parent
, wxPrintout
*printout
, const wxString
& message
)
367 m_pimpl
->ReportError( parent
, printout
, message
);
370 bool wxPrinter::Setup(wxWindow
*parent
)
372 return m_pimpl
->Setup( parent
);
375 bool wxPrinter::Print(wxWindow
*parent
, wxPrintout
*printout
, bool prompt
)
377 return m_pimpl
->Print( parent
, printout
, prompt
);
380 wxDC
* wxPrinter::PrintDialog(wxWindow
*parent
)
382 return m_pimpl
->PrintDialog( parent
);
385 wxPrintDialogData
& wxPrinter::GetPrintDialogData() const
387 return m_pimpl
->GetPrintDialogData();
390 // ---------------------------------------------------------------------------
391 // wxPrintDialogBase: the dialog for printing.
392 // ---------------------------------------------------------------------------
394 IMPLEMENT_ABSTRACT_CLASS(wxPrintDialogBase
, wxDialog
)
396 wxPrintDialogBase::wxPrintDialogBase(wxWindow
*parent
,
398 const wxString
&title
,
402 : wxDialog( parent
, id
, title
.empty() ? wxString(_("Print")) : title
,
407 // ---------------------------------------------------------------------------
408 // wxPrintDialog: the dialog for printing
409 // ---------------------------------------------------------------------------
411 IMPLEMENT_CLASS(wxPrintDialog
, wxObject
)
413 wxPrintDialog::wxPrintDialog(wxWindow
*parent
, wxPrintDialogData
* data
)
415 m_pimpl
= wxPrintFactory::GetFactory()->CreatePrintDialog( parent
, data
);
418 wxPrintDialog::wxPrintDialog(wxWindow
*parent
, wxPrintData
* data
)
420 m_pimpl
= wxPrintFactory::GetFactory()->CreatePrintDialog( parent
, data
);
423 wxPrintDialog::~wxPrintDialog()
428 int wxPrintDialog::ShowModal()
430 return m_pimpl
->ShowModal();
433 wxPrintDialogData
& wxPrintDialog::GetPrintDialogData()
435 return m_pimpl
->GetPrintDialogData();
438 wxPrintData
& wxPrintDialog::GetPrintData()
440 return m_pimpl
->GetPrintData();
443 wxDC
*wxPrintDialog::GetPrintDC()
445 return m_pimpl
->GetPrintDC();
448 // ---------------------------------------------------------------------------
449 // wxPageSetupDialogBase: the page setup dialog
450 // ---------------------------------------------------------------------------
452 IMPLEMENT_ABSTRACT_CLASS(wxPageSetupDialogBase
, wxDialog
)
454 wxPageSetupDialogBase::wxPageSetupDialogBase(wxWindow
*parent
,
456 const wxString
&title
,
460 : wxDialog( parent
, id
, title
.empty() ? wxString(_("Page setup")) : title
,
465 // ---------------------------------------------------------------------------
466 // wxPageSetupDialog: the page setup dialog
467 // ---------------------------------------------------------------------------
469 IMPLEMENT_CLASS(wxPageSetupDialog
, wxObject
)
471 wxPageSetupDialog::wxPageSetupDialog(wxWindow
*parent
, wxPageSetupDialogData
*data
)
473 m_pimpl
= wxPrintFactory::GetFactory()->CreatePageSetupDialog( parent
, data
);
476 wxPageSetupDialog::~wxPageSetupDialog()
481 int wxPageSetupDialog::ShowModal()
483 return m_pimpl
->ShowModal();
486 wxPageSetupDialogData
& wxPageSetupDialog::GetPageSetupDialogData()
488 return m_pimpl
->GetPageSetupDialogData();
492 wxPageSetupDialogData
& wxPageSetupDialog::GetPageSetupData()
494 return m_pimpl
->GetPageSetupDialogData();
497 //----------------------------------------------------------------------------
498 // wxPrintAbortDialog
499 //----------------------------------------------------------------------------
501 BEGIN_EVENT_TABLE(wxPrintAbortDialog
, wxDialog
)
502 EVT_BUTTON(wxID_CANCEL
, wxPrintAbortDialog::OnCancel
)
505 void wxPrintAbortDialog::OnCancel(wxCommandEvent
& WXUNUSED(event
))
507 wxPrinterBase::sm_abortIt
= true;
508 wxPrinterBase::sm_abortWindow
->Show(false);
509 wxPrinterBase::sm_abortWindow
->Close(true);
510 wxPrinterBase::sm_abortWindow
= (wxWindow
*) NULL
;
513 //----------------------------------------------------------------------------
515 //----------------------------------------------------------------------------
517 IMPLEMENT_ABSTRACT_CLASS(wxPrintout
, wxObject
)
519 wxPrintout::wxPrintout(const wxString
& title
)
521 m_printoutTitle
= title
;
522 m_printoutDC
= (wxDC
*) NULL
;
525 m_pageWidthPixels
= 0;
526 m_pageHeightPixels
= 0;
534 wxPrintout::~wxPrintout()
538 bool wxPrintout::OnBeginDocument(int WXUNUSED(startPage
), int WXUNUSED(endPage
))
540 return GetDC()->StartDoc(_("Printing ") + m_printoutTitle
);
543 void wxPrintout::OnEndDocument()
548 void wxPrintout::OnBeginPrinting()
552 void wxPrintout::OnEndPrinting()
556 bool wxPrintout::HasPage(int page
)
561 void wxPrintout::GetPageInfo(int *minPage
, int *maxPage
, int *fromPage
, int *toPage
)
569 //----------------------------------------------------------------------------
571 //----------------------------------------------------------------------------
573 IMPLEMENT_CLASS(wxPreviewCanvas
, wxWindow
)
575 BEGIN_EVENT_TABLE(wxPreviewCanvas
, wxScrolledWindow
)
576 EVT_PAINT(wxPreviewCanvas::OnPaint
)
577 EVT_CHAR(wxPreviewCanvas::OnChar
)
578 EVT_SYS_COLOUR_CHANGED(wxPreviewCanvas::OnSysColourChanged
)
580 EVT_MOUSEWHEEL(wxPreviewCanvas::OnMouseWheel
)
584 // VZ: the current code doesn't refresh properly without
585 // wxFULL_REPAINT_ON_RESIZE, this must be fixed as otherwise we have
586 // really horrible flicker when resizing the preview frame, but without
587 // this style it simply doesn't work correctly at all...
588 wxPreviewCanvas::wxPreviewCanvas(wxPrintPreviewBase
*preview
, wxWindow
*parent
,
589 const wxPoint
& pos
, const wxSize
& size
, long style
, const wxString
& name
):
590 wxScrolledWindow(parent
, wxID_ANY
, pos
, size
, style
| wxFULL_REPAINT_ON_RESIZE
, name
)
592 m_printPreview
= preview
;
594 // The app workspace colour is always white, but we should have
595 // a contrast with the page.
596 wxSystemColour colourIndex
= wxSYS_COLOUR_3DDKSHADOW
;
598 wxSystemColour colourIndex
= wxSYS_COLOUR_APPWORKSPACE
;
600 SetBackgroundColour(wxSystemSettings::GetColour(colourIndex
));
602 SetScrollbars(10, 10, 100, 100);
605 wxPreviewCanvas::~wxPreviewCanvas()
609 void wxPreviewCanvas::OnPaint(wxPaintEvent
& WXUNUSED(event
))
616 if (!GetUpdateRegion().IsEmpty())
617 dc.SetClippingRegion( GetUpdateRegion() );
623 m_printPreview
->PaintPage(this, dc
);
627 // Responds to colour changes, and passes event on to children.
628 void wxPreviewCanvas::OnSysColourChanged(wxSysColourChangedEvent
& event
)
631 // The app workspace colour is always white, but we should have
632 // a contrast with the page.
633 wxSystemColour colourIndex
= wxSYS_COLOUR_3DDKSHADOW
;
635 wxSystemColour colourIndex
= wxSYS_COLOUR_APPWORKSPACE
;
637 SetBackgroundColour(wxSystemSettings::GetColour(colourIndex
));
640 // Propagate the event to the non-top-level children
641 wxWindow::OnSysColourChanged(event
);
644 void wxPreviewCanvas::OnChar(wxKeyEvent
&event
)
646 wxPreviewControlBar
* controlBar
= ((wxPreviewFrame
*) GetParent())->GetControlBar();
647 if (event
.GetKeyCode() == WXK_ESCAPE
)
649 ((wxPreviewFrame
*) GetParent())->Close(true);
652 else if (event
.GetKeyCode() == WXK_TAB
)
654 controlBar
->OnGoto();
657 else if (event
.GetKeyCode() == WXK_RETURN
)
659 controlBar
->OnPrint();
663 if (!event
.ControlDown())
669 switch(event
.GetKeyCode())
672 controlBar
->OnNext(); break;
674 controlBar
->OnPrevious(); break;
676 controlBar
->OnFirst(); break;
678 controlBar
->OnLast(); break;
686 void wxPreviewCanvas::OnMouseWheel(wxMouseEvent
& event
)
688 wxPreviewControlBar
*
689 controlBar
= wxStaticCast(GetParent(), wxPreviewFrame
)->GetControlBar();
693 if ( event
.ControlDown() && event
.GetWheelRotation() != 0 )
695 int currentZoom
= controlBar
->GetZoomControl();
698 if ( currentZoom
< 100 )
700 else if ( currentZoom
<= 120 )
705 if ( event
.GetWheelRotation() > 0 )
708 int newZoom
= currentZoom
+ delta
;
713 if ( newZoom
!= currentZoom
)
715 controlBar
->SetZoomControl(newZoom
);
716 m_printPreview
->SetZoom(newZoom
);
726 #endif // wxUSE_MOUSEWHEEL
728 //----------------------------------------------------------------------------
729 // wxPreviewControlBar
730 //----------------------------------------------------------------------------
732 IMPLEMENT_CLASS(wxPreviewControlBar
, wxWindow
)
734 BEGIN_EVENT_TABLE(wxPreviewControlBar
, wxPanel
)
735 EVT_BUTTON(wxID_PREVIEW_CLOSE
, wxPreviewControlBar::OnWindowClose
)
736 EVT_BUTTON(wxID_PREVIEW_PRINT
, wxPreviewControlBar::OnPrintButton
)
737 EVT_BUTTON(wxID_PREVIEW_PREVIOUS
, wxPreviewControlBar::OnPreviousButton
)
738 EVT_BUTTON(wxID_PREVIEW_NEXT
, wxPreviewControlBar::OnNextButton
)
739 EVT_BUTTON(wxID_PREVIEW_FIRST
, wxPreviewControlBar::OnFirstButton
)
740 EVT_BUTTON(wxID_PREVIEW_LAST
, wxPreviewControlBar::OnLastButton
)
741 EVT_BUTTON(wxID_PREVIEW_GOTO
, wxPreviewControlBar::OnGotoButton
)
742 EVT_CHOICE(wxID_PREVIEW_ZOOM
, wxPreviewControlBar::OnZoom
)
743 EVT_PAINT(wxPreviewControlBar::OnPaint
)
746 wxPreviewControlBar::wxPreviewControlBar(wxPrintPreviewBase
*preview
, long buttons
,
747 wxWindow
*parent
, const wxPoint
& pos
, const wxSize
& size
,
748 long style
, const wxString
& name
):
749 wxPanel(parent
, wxID_ANY
, pos
, size
, style
, name
)
751 m_printPreview
= preview
;
752 m_closeButton
= (wxButton
*) NULL
;
753 m_nextPageButton
= (wxButton
*) NULL
;
754 m_previousPageButton
= (wxButton
*) NULL
;
755 m_printButton
= (wxButton
*) NULL
;
756 m_zoomControl
= (wxChoice
*) NULL
;
757 m_buttonFlags
= buttons
;
760 wxPreviewControlBar::~wxPreviewControlBar()
764 void wxPreviewControlBar::OnPaint(wxPaintEvent
& WXUNUSED(event
))
770 dc
.SetPen(*wxBLACK_PEN
);
771 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
772 dc
.DrawLine( 0, h
-1, w
, h
-1 );
775 void wxPreviewControlBar::OnWindowClose(wxCommandEvent
& WXUNUSED(event
))
777 wxPreviewFrame
*frame
= (wxPreviewFrame
*)GetParent();
781 void wxPreviewControlBar::OnPrint(void)
783 wxPrintPreviewBase
*preview
= GetPrintPreview();
784 preview
->Print(true);
787 void wxPreviewControlBar::OnNext(void)
789 wxPrintPreviewBase
*preview
= GetPrintPreview();
792 int currentPage
= preview
->GetCurrentPage();
793 if ((preview
->GetMaxPage() > 0) &&
794 (currentPage
< preview
->GetMaxPage()) &&
795 preview
->GetPrintout()->HasPage(currentPage
+ 1))
797 preview
->SetCurrentPage(currentPage
+ 1);
802 void wxPreviewControlBar::OnPrevious(void)
804 wxPrintPreviewBase
*preview
= GetPrintPreview();
807 int currentPage
= preview
->GetCurrentPage();
808 if ((preview
->GetMinPage() > 0) &&
809 (currentPage
> preview
->GetMinPage()) &&
810 preview
->GetPrintout()->HasPage(currentPage
- 1))
812 preview
->SetCurrentPage(currentPage
- 1);
817 void wxPreviewControlBar::OnFirst(void)
819 wxPrintPreviewBase
*preview
= GetPrintPreview();
822 int currentPage
= preview
->GetMinPage();
823 if (preview
->GetPrintout()->HasPage(currentPage
))
825 preview
->SetCurrentPage(currentPage
);
830 void wxPreviewControlBar::OnLast(void)
832 wxPrintPreviewBase
*preview
= GetPrintPreview();
835 int currentPage
= preview
->GetMaxPage();
836 if (preview
->GetPrintout()->HasPage(currentPage
))
838 preview
->SetCurrentPage(currentPage
);
843 void wxPreviewControlBar::OnGoto(void)
845 wxPrintPreviewBase
*preview
= GetPrintPreview();
850 if (preview
->GetMinPage() > 0)
855 strPrompt
.Printf( _("Enter a page number between %d and %d:"),
856 preview
->GetMinPage(), preview
->GetMaxPage());
857 strPage
.Printf( wxT("%d"), preview
->GetCurrentPage() );
860 wxGetTextFromUser( strPrompt
, _("Goto Page"), strPage
, GetParent());
862 if ( strPage
.ToLong( ¤tPage
) )
863 if (preview
->GetPrintout()->HasPage(currentPage
))
865 preview
->SetCurrentPage(currentPage
);
871 void wxPreviewControlBar::OnZoom(wxCommandEvent
& WXUNUSED(event
))
873 int zoom
= GetZoomControl();
874 if (GetPrintPreview())
875 GetPrintPreview()->SetZoom(zoom
);
878 void wxPreviewControlBar::CreateButtons()
880 SetSize(0, 0, 400, 40);
882 wxBoxSizer
*item0
= new wxBoxSizer( wxHORIZONTAL
);
884 m_closeButton
= new wxButton( this, wxID_PREVIEW_CLOSE
, _("&Close"), wxDefaultPosition
, wxDefaultSize
, 0 );
885 item0
->Add( m_closeButton
, 0, wxALIGN_CENTRE
|wxALL
, 5 );
887 if (m_buttonFlags
& wxPREVIEW_PRINT
)
889 m_printButton
= new wxButton( this, wxID_PREVIEW_PRINT
, _("&Print..."), wxDefaultPosition
, wxDefaultSize
, 0 );
890 item0
->Add( m_printButton
, 0, wxALIGN_CENTRE
|wxALL
, 5 );
893 // Exact-fit buttons are too tiny on wxUniversal
895 wxSize navButtonSize
;
896 #ifdef __WXUNIVERSAL__
898 navButtonSize
= wxSize(40, m_closeButton
->GetSize().y
);
900 navButtonStyle
= wxBU_EXACTFIT
;
901 navButtonSize
= wxDefaultSize
;
904 if (m_buttonFlags
& wxPREVIEW_FIRST
)
906 m_firstPageButton
= new wxButton( this, wxID_PREVIEW_FIRST
, _("|<<"), wxDefaultPosition
, navButtonSize
, navButtonStyle
);
907 item0
->Add( m_firstPageButton
, 0, wxALIGN_CENTRE
|wxALL
, 5 );
910 if (m_buttonFlags
& wxPREVIEW_PREVIOUS
)
912 m_previousPageButton
= new wxButton( this, wxID_PREVIEW_PREVIOUS
, _("<<"), wxDefaultPosition
, navButtonSize
, navButtonStyle
);
913 item0
->Add( m_previousPageButton
, 0, wxALIGN_CENTRE
|wxRIGHT
|wxTOP
|wxBOTTOM
, 5 );
916 if (m_buttonFlags
& wxPREVIEW_NEXT
)
918 m_nextPageButton
= new wxButton( this, wxID_PREVIEW_NEXT
, _(">>"), wxDefaultPosition
, navButtonSize
, navButtonStyle
);
919 item0
->Add( m_nextPageButton
, 0, wxALIGN_CENTRE
|wxRIGHT
|wxTOP
|wxBOTTOM
, 5 );
922 if (m_buttonFlags
& wxPREVIEW_LAST
)
924 m_lastPageButton
= new wxButton( this, wxID_PREVIEW_LAST
, _(">>|"), wxDefaultPosition
, navButtonSize
, navButtonStyle
);
925 item0
->Add( m_lastPageButton
, 0, wxALIGN_CENTRE
|wxRIGHT
|wxTOP
|wxBOTTOM
, 5 );
928 if (m_buttonFlags
& wxPREVIEW_GOTO
)
930 m_gotoPageButton
= new wxButton( this, wxID_PREVIEW_GOTO
, _("&Goto..."), wxDefaultPosition
, wxDefaultSize
, 0 );
931 item0
->Add( m_gotoPageButton
, 0, wxALIGN_CENTRE
|wxALL
, 5 );
934 if (m_buttonFlags
& wxPREVIEW_ZOOM
)
938 wxT("10%"), wxT("15%"), wxT("20%"), wxT("25%"), wxT("30%"), wxT("35%"), wxT("40%"), wxT("45%"), wxT("50%"), wxT("55%"),
939 wxT("60%"), wxT("65%"), wxT("70%"), wxT("75%"), wxT("80%"), wxT("85%"), wxT("90%"), wxT("95%"), wxT("100%"), wxT("110%"),
940 wxT("120%"), wxT("150%"), wxT("200%")
942 int n
= WXSIZEOF(choices
);
944 m_zoomControl
= new wxChoice( this, wxID_PREVIEW_ZOOM
, wxDefaultPosition
, wxSize(70,wxDefaultCoord
), n
, choices
, 0 );
945 item0
->Add( m_zoomControl
, 0, wxALIGN_CENTRE
|wxALL
, 5 );
946 SetZoomControl(m_printPreview
->GetZoom());
953 void wxPreviewControlBar::SetZoomControl(int zoom
)
957 int n
, count
= m_zoomControl
->GetCount();
959 for (n
=0; n
<count
; n
++)
961 if (m_zoomControl
->GetString(n
).BeforeFirst(wxT('%')).ToLong(&val
) &&
964 m_zoomControl
->SetSelection(n
);
969 m_zoomControl
->SetSelection(count
-1);
973 int wxPreviewControlBar::GetZoomControl()
975 if (m_zoomControl
&& (m_zoomControl
->GetStringSelection() != wxEmptyString
))
978 if (m_zoomControl
->GetStringSelection().BeforeFirst(wxT('%')).ToLong(&val
))
990 IMPLEMENT_CLASS(wxPreviewFrame
, wxFrame
)
992 BEGIN_EVENT_TABLE(wxPreviewFrame
, wxFrame
)
993 EVT_CLOSE(wxPreviewFrame::OnCloseWindow
)
996 wxPreviewFrame::wxPreviewFrame(wxPrintPreviewBase
*preview
, wxWindow
*parent
, const wxString
& title
,
997 const wxPoint
& pos
, const wxSize
& size
, long style
, const wxString
& name
):
998 wxFrame(parent
, wxID_ANY
, title
, pos
, size
, style
, name
)
1000 m_printPreview
= preview
;
1001 m_controlBar
= NULL
;
1002 m_previewCanvas
= NULL
;
1003 m_windowDisabler
= NULL
;
1005 // Give the application icon
1007 wxFrame
* topFrame
= wxDynamicCast(wxTheApp
->GetTopWindow(), wxFrame
);
1009 SetIcon(topFrame
->GetIcon());
1013 wxPreviewFrame::~wxPreviewFrame()
1017 void wxPreviewFrame::OnCloseWindow(wxCloseEvent
& WXUNUSED(event
))
1019 if (m_windowDisabler
)
1020 delete m_windowDisabler
;
1022 // Need to delete the printout and the print preview
1023 wxPrintout
*printout
= m_printPreview
->GetPrintout();
1027 m_printPreview
->SetPrintout(NULL
);
1028 m_printPreview
->SetCanvas(NULL
);
1029 m_printPreview
->SetFrame(NULL
);
1031 delete m_printPreview
;
1036 void wxPreviewFrame::Initialize()
1044 m_printPreview
->SetCanvas(m_previewCanvas
);
1045 m_printPreview
->SetFrame(this);
1047 wxBoxSizer
*item0
= new wxBoxSizer( wxVERTICAL
);
1049 item0
->Add( m_controlBar
, 0, wxGROW
|wxALIGN_CENTER_VERTICAL
, 5 );
1050 item0
->Add( m_previewCanvas
, 1, wxGROW
|wxALIGN_CENTER_VERTICAL
, 5 );
1052 SetAutoLayout( true );
1055 m_windowDisabler
= new wxWindowDisabler(this);
1059 m_printPreview
->AdjustScrollbars(m_previewCanvas
);
1060 m_previewCanvas
->SetFocus();
1061 m_controlBar
->SetFocus();
1064 void wxPreviewFrame::CreateCanvas()
1066 m_previewCanvas
= new wxPreviewCanvas(m_printPreview
, this);
1069 void wxPreviewFrame::CreateControlBar()
1071 long buttons
= wxPREVIEW_DEFAULT
;
1072 if (m_printPreview
->GetPrintoutForPrinting())
1073 buttons
|= wxPREVIEW_PRINT
;
1075 m_controlBar
= new wxPreviewControlBar(m_printPreview
, buttons
, this, wxPoint(0,0), wxSize(400, 40));
1076 m_controlBar
->CreateButtons();
1083 IMPLEMENT_CLASS(wxPrintPreviewBase
, wxObject
)
1085 wxPrintPreviewBase::wxPrintPreviewBase(wxPrintout
*printout
,
1086 wxPrintout
*printoutForPrinting
,
1090 m_printDialogData
= (*data
);
1092 Init(printout
, printoutForPrinting
);
1095 wxPrintPreviewBase::wxPrintPreviewBase(wxPrintout
*printout
,
1096 wxPrintout
*printoutForPrinting
,
1097 wxPrintDialogData
*data
)
1100 m_printDialogData
= (*data
);
1102 Init(printout
, printoutForPrinting
);
1105 void wxPrintPreviewBase::Init(wxPrintout
*printout
,
1106 wxPrintout
*printoutForPrinting
)
1109 m_previewPrintout
= printout
;
1110 if (m_previewPrintout
)
1111 m_previewPrintout
->SetIsPreview(true);
1113 m_printPrintout
= printoutForPrinting
;
1115 m_previewCanvas
= NULL
;
1116 m_previewFrame
= NULL
;
1117 m_previewBitmap
= NULL
;
1124 m_printingPrepared
= false;
1129 wxPrintPreviewBase::~wxPrintPreviewBase()
1131 if (m_previewPrintout
)
1132 delete m_previewPrintout
;
1133 if (m_previewBitmap
)
1134 delete m_previewBitmap
;
1135 if (m_printPrintout
)
1136 delete m_printPrintout
;
1139 bool wxPrintPreviewBase::SetCurrentPage(int pageNum
)
1141 if (m_currentPage
== pageNum
)
1144 m_currentPage
= pageNum
;
1145 if (m_previewBitmap
)
1147 delete m_previewBitmap
;
1148 m_previewBitmap
= NULL
;
1151 if (m_previewCanvas
)
1153 AdjustScrollbars(m_previewCanvas
);
1155 if (!RenderPage(pageNum
))
1157 m_previewCanvas
->Refresh();
1158 m_previewCanvas
->SetFocus();
1163 int wxPrintPreviewBase::GetCurrentPage() const
1164 { return m_currentPage
; }
1165 void wxPrintPreviewBase::SetPrintout(wxPrintout
*printout
)
1166 { m_previewPrintout
= printout
; }
1167 wxPrintout
*wxPrintPreviewBase::GetPrintout() const
1168 { return m_previewPrintout
; }
1169 wxPrintout
*wxPrintPreviewBase::GetPrintoutForPrinting() const
1170 { return m_printPrintout
; }
1171 void wxPrintPreviewBase::SetFrame(wxFrame
*frame
)
1172 { m_previewFrame
= frame
; }
1173 void wxPrintPreviewBase::SetCanvas(wxPreviewCanvas
*canvas
)
1174 { m_previewCanvas
= canvas
; }
1175 wxFrame
*wxPrintPreviewBase::GetFrame() const
1176 { return m_previewFrame
; }
1177 wxPreviewCanvas
*wxPrintPreviewBase::GetCanvas() const
1178 { return m_previewCanvas
; }
1180 bool wxPrintPreviewBase::PaintPage(wxPreviewCanvas
*canvas
, wxDC
& dc
)
1182 DrawBlankPage(canvas
, dc
);
1184 if (!m_previewBitmap
)
1185 if (!RenderPage(m_currentPage
))
1188 if (!m_previewBitmap
)
1194 int canvasWidth
, canvasHeight
;
1195 canvas
->GetSize(&canvasWidth
, &canvasHeight
);
1197 double zoomScale
= ((float)m_currentZoom
/(float)100);
1198 double actualWidth
= (zoomScale
*m_pageWidth
*m_previewScale
);
1199 // float actualHeight = (float)(zoomScale*m_pageHeight*m_previewScale);
1201 int x
= (int) ((canvasWidth
- actualWidth
)/2.0);
1202 if (x
< m_leftMargin
)
1204 int y
= m_topMargin
;
1207 temp_dc
.SelectObject(*m_previewBitmap
);
1209 dc
.Blit(x
, y
, m_previewBitmap
->GetWidth(), m_previewBitmap
->GetHeight(), &temp_dc
, 0, 0);
1211 temp_dc
.SelectObject(wxNullBitmap
);
1216 // Adjusts the scrollbars for the current scale
1217 void wxPrintPreviewBase::AdjustScrollbars(wxPreviewCanvas
*canvas
)
1222 int canvasWidth
, canvasHeight
;
1223 canvas
->GetSize(&canvasWidth
, &canvasHeight
);
1225 double zoomScale
= ((float)m_currentZoom
/(float)100);
1226 double actualWidth
= (zoomScale
*m_pageWidth
*m_previewScale
);
1227 double actualHeight
= (zoomScale
*m_pageHeight
*m_previewScale
);
1229 // Set the scrollbars appropriately
1230 int totalWidth
= (int)(actualWidth
+ 2*m_leftMargin
);
1231 int totalHeight
= (int)(actualHeight
+ 2*m_topMargin
);
1232 int scrollUnitsX
= totalWidth
/10;
1233 int scrollUnitsY
= totalHeight
/10;
1234 wxSize virtualSize
= canvas
->GetVirtualSize();
1235 if (virtualSize
.GetWidth() != totalWidth
|| virtualSize
.GetHeight() != totalHeight
)
1236 canvas
->SetScrollbars(10, 10, scrollUnitsX
, scrollUnitsY
, 0, 0, true);
1239 bool wxPrintPreviewBase::RenderPage(int pageNum
)
1243 int canvasWidth
, canvasHeight
;
1245 if (!m_previewCanvas
)
1247 wxFAIL_MSG(_T("wxPrintPreviewBase::RenderPage: must use wxPrintPreviewBase::SetCanvas to let me know about the canvas!"));
1251 m_previewCanvas
->GetSize(&canvasWidth
, &canvasHeight
);
1253 double zoomScale
= (m_currentZoom
/100.0);
1254 int actualWidth
= (int)(zoomScale
*m_pageWidth
*m_previewScale
);
1255 int actualHeight
= (int)(zoomScale
*m_pageHeight
*m_previewScale
);
1257 if (!m_previewBitmap
)
1259 m_previewBitmap
= new wxBitmap((int)actualWidth
, (int)actualHeight
);
1260 if (!m_previewBitmap
|| !m_previewBitmap
->Ok())
1262 if (m_previewBitmap
) {
1263 delete m_previewBitmap
;
1264 m_previewBitmap
= NULL
;
1266 wxMessageBox(_("Sorry, not enough memory to create a preview."), _("Print Preview Failure"), wxOK
);
1271 wxMemoryDC memoryDC
;
1272 memoryDC
.SelectObject(*m_previewBitmap
);
1276 m_previewPrintout
->SetDC(&memoryDC
);
1277 m_previewPrintout
->SetPageSizePixels(m_pageWidth
, m_pageHeight
);
1279 // Need to delay OnPreparePrinting until here, so we have enough information.
1280 if (!m_printingPrepared
)
1282 m_previewPrintout
->OnPreparePrinting();
1284 m_previewPrintout
->GetPageInfo(&m_minPage
, &m_maxPage
, &selFrom
, &selTo
);
1285 m_printingPrepared
= true;
1288 m_previewPrintout
->OnBeginPrinting();
1290 if (!m_previewPrintout
->OnBeginDocument(m_printDialogData
.GetFromPage(), m_printDialogData
.GetToPage()))
1292 wxMessageBox(_("Could not start document preview."), _("Print Preview Failure"), wxOK
);
1294 memoryDC
.SelectObject(wxNullBitmap
);
1296 delete m_previewBitmap
;
1297 m_previewBitmap
= NULL
;
1301 m_previewPrintout
->OnPrintPage(pageNum
);
1302 m_previewPrintout
->OnEndDocument();
1303 m_previewPrintout
->OnEndPrinting();
1305 m_previewPrintout
->SetDC(NULL
);
1307 memoryDC
.SelectObject(wxNullBitmap
);
1312 status
= wxString::Format(_("Page %d of %d"), pageNum
, m_maxPage
);
1314 status
= wxString::Format(_("Page %d"), pageNum
);
1317 m_previewFrame
->SetStatusText(status
);
1324 bool wxPrintPreviewBase::DrawBlankPage(wxPreviewCanvas
*canvas
, wxDC
& dc
)
1326 int canvasWidth
, canvasHeight
;
1327 canvas
->GetSize(&canvasWidth
, &canvasHeight
);
1329 float zoomScale
= (float)((float)m_currentZoom
/(float)100);
1330 float actualWidth
= zoomScale
*m_pageWidth
*m_previewScale
;
1331 float actualHeight
= zoomScale
*m_pageHeight
*m_previewScale
;
1333 float x
= (float)((canvasWidth
- actualWidth
)/2.0);
1334 if (x
< m_leftMargin
)
1335 x
= (float)m_leftMargin
;
1336 float y
= (float)m_topMargin
;
1338 // Draw shadow, allowing for 1-pixel border AROUND the actual page
1339 int shadowOffset
= 4;
1340 dc
.SetPen(*wxBLACK_PEN
);
1341 dc
.SetBrush(*wxBLACK_BRUSH
);
1343 dc.DrawRectangle((int)(x-1 + shadowOffset), (int)(y-1 + shadowOffset), (int)(actualWidth+2), (int)(actualHeight+2));
1345 dc
.DrawRectangle((int)(x
+ shadowOffset
), (int)(y
+ actualHeight
+1), (int)(actualWidth
), shadowOffset
);
1346 dc
.DrawRectangle((int)(x
+ actualWidth
), (int)(y
+ shadowOffset
), shadowOffset
, (int)(actualHeight
));
1348 // Draw blank page allowing for 1-pixel border AROUND the actual page
1349 dc
.SetPen(*wxBLACK_PEN
);
1350 dc
.SetBrush(*wxWHITE_BRUSH
);
1353 wxRegion update_region = canvas->GetUpdateRegion();
1354 wxRect r = update_region.GetBox();
1356 printf( "x: %d y: %d w: %d h: %d.\n", (int)r.x, (int)r.y, (int)r.width, (int)r.height );
1359 dc
.DrawRectangle((int)(x
-2), (int)(y
-1), (int)(actualWidth
+3), (int)(actualHeight
+2));
1364 void wxPrintPreviewBase::SetZoom(int percent
)
1366 if (m_currentZoom
== percent
)
1369 m_currentZoom
= percent
;
1370 if (m_previewBitmap
)
1372 delete m_previewBitmap
;
1373 m_previewBitmap
= NULL
;
1376 if (m_previewCanvas
)
1378 AdjustScrollbars(m_previewCanvas
);
1379 RenderPage(m_currentPage
);
1380 ((wxScrolledWindow
*) m_previewCanvas
)->Scroll(0, 0);
1381 m_previewCanvas
->ClearBackground();
1382 m_previewCanvas
->Refresh();
1383 m_previewCanvas
->SetFocus();
1387 wxPrintDialogData
& wxPrintPreviewBase::GetPrintDialogData()
1389 return m_printDialogData
;
1392 int wxPrintPreviewBase::GetZoom() const
1393 { return m_currentZoom
; }
1394 int wxPrintPreviewBase::GetMaxPage() const
1395 { return m_maxPage
; }
1396 int wxPrintPreviewBase::GetMinPage() const
1397 { return m_minPage
; }
1398 bool wxPrintPreviewBase::IsOk() const
1400 void wxPrintPreviewBase::SetOk(bool ok
)
1402 //----------------------------------------------------------------------------
1404 //----------------------------------------------------------------------------
1406 IMPLEMENT_CLASS(wxPrintPreview
, wxPrintPreviewBase
)
1408 wxPrintPreview::wxPrintPreview(wxPrintout
*printout
,
1409 wxPrintout
*printoutForPrinting
,
1410 wxPrintDialogData
*data
) :
1411 wxPrintPreviewBase( printout
, printoutForPrinting
, data
)
1413 m_pimpl
= wxPrintFactory::GetFactory()->
1414 CreatePrintPreview( printout
, printoutForPrinting
, data
);
1417 wxPrintPreview::wxPrintPreview(wxPrintout
*printout
,
1418 wxPrintout
*printoutForPrinting
,
1419 wxPrintData
*data
) :
1420 wxPrintPreviewBase( printout
, printoutForPrinting
, data
)
1422 m_pimpl
= wxPrintFactory::GetFactory()->
1423 CreatePrintPreview( printout
, printoutForPrinting
, data
);
1426 wxPrintPreview::~wxPrintPreview()
1430 // don't delete twice
1431 m_printPrintout
= NULL
;
1432 m_previewPrintout
= NULL
;
1433 m_previewBitmap
= NULL
;
1436 bool wxPrintPreview::SetCurrentPage(int pageNum
)
1438 return m_pimpl
->SetCurrentPage( pageNum
);
1441 int wxPrintPreview::GetCurrentPage() const
1443 return m_pimpl
->GetCurrentPage();
1446 void wxPrintPreview::SetPrintout(wxPrintout
*printout
)
1448 m_pimpl
->SetPrintout( printout
);
1451 wxPrintout
*wxPrintPreview::GetPrintout() const
1453 return m_pimpl
->GetPrintout();
1456 wxPrintout
*wxPrintPreview::GetPrintoutForPrinting() const
1458 return m_pimpl
->GetPrintoutForPrinting();
1461 void wxPrintPreview::SetFrame(wxFrame
*frame
)
1463 m_pimpl
->SetFrame( frame
);
1466 void wxPrintPreview::SetCanvas(wxPreviewCanvas
*canvas
)
1468 m_pimpl
->SetCanvas( canvas
);
1471 wxFrame
*wxPrintPreview::GetFrame() const
1473 return m_pimpl
->GetFrame();
1476 wxPreviewCanvas
*wxPrintPreview::GetCanvas() const
1478 return m_pimpl
->GetCanvas();
1481 bool wxPrintPreview::PaintPage(wxPreviewCanvas
*canvas
, wxDC
& dc
)
1483 return m_pimpl
->PaintPage( canvas
, dc
);
1486 bool wxPrintPreview::DrawBlankPage(wxPreviewCanvas
*canvas
, wxDC
& dc
)
1488 return m_pimpl
->DrawBlankPage( canvas
, dc
);
1491 void wxPrintPreview::AdjustScrollbars(wxPreviewCanvas
*canvas
)
1493 m_pimpl
->AdjustScrollbars( canvas
);
1496 bool wxPrintPreview::RenderPage(int pageNum
)
1498 return m_pimpl
->RenderPage( pageNum
);
1501 void wxPrintPreview::SetZoom(int percent
)
1503 m_pimpl
->SetZoom( percent
);
1506 int wxPrintPreview::GetZoom() const
1508 return m_pimpl
->GetZoom();
1511 wxPrintDialogData
& wxPrintPreview::GetPrintDialogData()
1513 return m_pimpl
->GetPrintDialogData();
1516 int wxPrintPreview::GetMaxPage() const
1518 return m_pimpl
->GetMaxPage();
1521 int wxPrintPreview::GetMinPage() const
1523 return m_pimpl
->GetMinPage();
1526 bool wxPrintPreview::IsOk() const
1528 return m_pimpl
->Ok();
1531 void wxPrintPreview::SetOk(bool ok
)
1533 m_pimpl
->SetOk( ok
);
1536 bool wxPrintPreview::Print(bool interactive
)
1538 return m_pimpl
->Print( interactive
);
1541 void wxPrintPreview::DetermineScaling()
1543 m_pimpl
->DetermineScaling();
1546 #endif // wxUSE_PRINTING_ARCHITECTURE