1 /////////////////////////////////////////////////////////////////////////////
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"
21 #if wxUSE_PRINTING_ARCHITECTURE
27 #include "wx/msgdlg.h"
28 #include "wx/layout.h"
29 #include "wx/choice.h"
30 #include "wx/button.h"
31 #include "wx/settings.h"
32 #include "wx/dcmemory.h"
33 #include "wx/stattext.h"
35 #include "wx/textdlg.h"
39 #include "wx/prntbase.h"
40 #include "wx/dcprint.h"
41 #include "wx/printdlg.h"
43 #include "wx/module.h"
48 #if defined(__WXMSW__) && !defined(__WXUNIVERSAL__)
49 #include "wx/msw/printdlg.h"
50 #elif defined(__WXMAC__)
51 #include "wx/mac/printdlg.h"
52 #include "wx/mac/private/print.h"
54 #include "wx/generic/prntdlgg.h"
58 #include "wx/msw/wrapcdlg.h"
64 //----------------------------------------------------------------------------
66 //----------------------------------------------------------------------------
68 wxPrintFactory
*wxPrintFactory::m_factory
= NULL
;
70 void wxPrintFactory::SetPrintFactory( wxPrintFactory
*factory
)
72 if (wxPrintFactory::m_factory
)
73 delete wxPrintFactory::m_factory
;
75 wxPrintFactory::m_factory
= factory
;
78 wxPrintFactory
*wxPrintFactory::GetFactory()
80 if (!wxPrintFactory::m_factory
)
81 wxPrintFactory::m_factory
= new wxNativePrintFactory
;
83 return wxPrintFactory::m_factory
;
86 //----------------------------------------------------------------------------
87 // wxNativePrintFactory
88 //----------------------------------------------------------------------------
90 wxPrinterBase
*wxNativePrintFactory::CreatePrinter( wxPrintDialogData
*data
)
92 #if defined(__WXMSW__) && !defined(__WXUNIVERSAL__)
93 return new wxWindowsPrinter( data
);
94 #elif defined(__WXMAC__)
95 return new wxMacPrinter( data
);
96 #elif defined(__WXPM__)
97 return new wxOS2Printer( data
);
99 return new wxPostScriptPrinter( data
);
103 wxPrintPreviewBase
*wxNativePrintFactory::CreatePrintPreview( wxPrintout
*preview
,
104 wxPrintout
*printout
, wxPrintDialogData
*data
)
106 #if defined(__WXMSW__) && !defined(__WXUNIVERSAL__)
107 return new wxWindowsPrintPreview( preview
, printout
, data
);
108 #elif defined(__WXMAC__)
109 return new wxMacPrintPreview( preview
, printout
, data
);
110 #elif defined(__WXPM__)
111 return new wxOS2PrintPreview( preview
, printout
, data
);
113 return new wxPostScriptPrintPreview( preview
, printout
, data
);
117 wxPrintPreviewBase
*wxNativePrintFactory::CreatePrintPreview( wxPrintout
*preview
,
118 wxPrintout
*printout
, wxPrintData
*data
)
120 #if defined(__WXMSW__) && !defined(__WXUNIVERSAL__)
121 return new wxWindowsPrintPreview( preview
, printout
, data
);
122 #elif defined(__WXMAC__)
123 return new wxMacPrintPreview( preview
, printout
, data
);
124 #elif defined(__WXPM__)
125 return new wxOS2PrintPreview( preview
, printout
, data
);
127 return new wxPostScriptPrintPreview( preview
, printout
, data
);
131 wxPrintDialogBase
*wxNativePrintFactory::CreatePrintDialog( wxWindow
*parent
,
132 wxPrintDialogData
*data
)
134 #if defined(__WXMSW__) && !defined(__WXUNIVERSAL__)
135 return new wxWindowsPrintDialog( parent
, data
);
136 #elif defined(__WXMAC__)
137 return new wxMacPrintDialog( parent
, data
);
139 return new wxGenericPrintDialog( parent
, data
);
143 wxPrintDialogBase
*wxNativePrintFactory::CreatePrintDialog( wxWindow
*parent
,
146 #if defined(__WXMSW__) && !defined(__WXUNIVERSAL__)
147 return new wxWindowsPrintDialog( parent
, data
);
148 #elif defined(__WXMAC__)
149 return new wxMacPrintDialog( parent
, data
);
151 return new wxGenericPrintDialog( parent
, data
);
155 wxPageSetupDialogBase
*wxNativePrintFactory::CreatePageSetupDialog( wxWindow
*parent
,
156 wxPageSetupDialogData
*data
)
158 #if defined(__WXMSW__) && !defined(__WXUNIVERSAL__)
159 return new wxWindowsPageSetupDialog( parent
, data
);
160 #elif defined(__WXMAC__)
161 return new wxMacPageSetupDialog( parent
, data
);
163 return new wxGenericPageSetupDialog( parent
, data
);
167 bool wxNativePrintFactory::HasPrintSetupDialog()
169 #if defined(__WXMSW__) && !defined(__WXUNIVERSAL__)
171 #elif defined(__WXMAC__)
174 // Only here do we need to provide the print setup
175 // dialog ourselves, the other platforms either have
176 // none, don't make it accessible or let you configure
177 // the printer from the wxPrintDialog anyway.
183 wxDialog
*wxNativePrintFactory::CreatePrintSetupDialog( wxWindow
*parent
,
186 #if defined(__WXMSW__) && !defined(__WXUNIVERSAL__)
190 #elif defined(__WXMAC__)
195 // Only here do we need to provide the print setup
196 // dialog ourselves, the other platforms either have
197 // none, don't make it accessible or let you configure
198 // the printer from the wxPrintDialog anyway.
199 return new wxGenericPrintSetupDialog( parent
, data
);
203 bool wxNativePrintFactory::HasOwnPrintToFile()
205 // Only relevant for PostScript and here the
206 // setup dialog provides no "print to file"
207 // option. In the GNOME setup dialog, the
208 // setup dialog has its own print to file.
212 bool wxNativePrintFactory::HasPrinterLine()
214 // Only relevant for PostScript for now
218 wxString
wxNativePrintFactory::CreatePrinterLine()
220 // Only relevant for PostScript for now
222 // We should query "lpstat -d" here
223 return _("Generic PostScript");
226 bool wxNativePrintFactory::HasStatusLine()
228 // Only relevant for PostScript for now
232 wxString
wxNativePrintFactory::CreateStatusLine()
234 // Only relevant for PostScript for now
236 // We should query "lpstat -r" or "lpstat -p" here
240 wxPrintNativeDataBase
*wxNativePrintFactory::CreatePrintNativeData()
242 #if defined(__WXMSW__) && !defined(__WXUNIVERSAL__)
243 return new wxWindowsPrintNativeData
;
244 #elif defined(__WXMAC__)
245 return new wxMacCarbonPrintData
;
247 return new wxPostScriptPrintNativeData
;
251 //----------------------------------------------------------------------------
252 // wxPrintNativeDataBase
253 //----------------------------------------------------------------------------
255 IMPLEMENT_ABSTRACT_CLASS(wxPrintNativeDataBase
, wxObject
)
257 wxPrintNativeDataBase::wxPrintNativeDataBase()
262 //----------------------------------------------------------------------------
263 // wxPrintFactoryModule
264 //----------------------------------------------------------------------------
266 class wxPrintFactoryModule
: public wxModule
269 wxPrintFactoryModule() {}
270 bool OnInit() { return true; }
271 void OnExit() { wxPrintFactory::SetPrintFactory( NULL
); }
274 DECLARE_DYNAMIC_CLASS(wxPrintFactoryModule
)
277 IMPLEMENT_DYNAMIC_CLASS(wxPrintFactoryModule
, wxModule
)
279 //----------------------------------------------------------------------------
281 //----------------------------------------------------------------------------
283 IMPLEMENT_CLASS(wxPrinterBase
, wxObject
)
285 wxPrinterBase::wxPrinterBase(wxPrintDialogData
*data
)
287 m_currentPrintout
= (wxPrintout
*) NULL
;
288 sm_abortWindow
= (wxWindow
*) NULL
;
291 m_printDialogData
= (*data
);
292 sm_lastError
= wxPRINTER_NO_ERROR
;
295 wxWindow
*wxPrinterBase::sm_abortWindow
= (wxWindow
*) NULL
;
296 bool wxPrinterBase::sm_abortIt
= false;
297 wxPrinterError
wxPrinterBase::sm_lastError
= wxPRINTER_NO_ERROR
;
299 wxPrinterBase::~wxPrinterBase()
303 wxWindow
*wxPrinterBase::CreateAbortWindow(wxWindow
*parent
, wxPrintout
* printout
)
305 wxPrintAbortDialog
*dialog
= new wxPrintAbortDialog(parent
, _("Printing ") , wxDefaultPosition
, wxDefaultSize
, wxDEFAULT_DIALOG_STYLE
);
307 wxBoxSizer
*button_sizer
= new wxBoxSizer( wxVERTICAL
);
308 button_sizer
->Add( new wxStaticText(dialog
, wxID_ANY
, _("Please wait while printing\n") + printout
->GetTitle() ), 0, wxALL
, 10 );
309 button_sizer
->Add( new wxButton( dialog
, wxID_CANCEL
, wxT("Cancel") ), 0, wxALL
| wxALIGN_CENTER
, 10 );
311 dialog
->SetAutoLayout( true );
312 dialog
->SetSizer( button_sizer
);
314 button_sizer
->Fit(dialog
);
315 button_sizer
->SetSizeHints (dialog
) ;
320 void wxPrinterBase::ReportError(wxWindow
*parent
, wxPrintout
*WXUNUSED(printout
), const wxString
& message
)
322 wxMessageBox(message
, _("Printing Error"), wxOK
, parent
);
325 wxPrintDialogData
& wxPrinterBase::GetPrintDialogData() const
327 return (wxPrintDialogData
&) m_printDialogData
;
330 //----------------------------------------------------------------------------
332 //----------------------------------------------------------------------------
334 IMPLEMENT_CLASS(wxPrinter
, wxPrinterBase
)
336 wxPrinter::wxPrinter(wxPrintDialogData
*data
)
338 m_pimpl
= wxPrintFactory::GetFactory()->CreatePrinter( data
);
341 wxPrinter::~wxPrinter()
346 wxWindow
*wxPrinter::CreateAbortWindow(wxWindow
*parent
, wxPrintout
*printout
)
348 return m_pimpl
->CreateAbortWindow( parent
, printout
);
351 void wxPrinter::ReportError(wxWindow
*parent
, wxPrintout
*printout
, const wxString
& message
)
353 m_pimpl
->ReportError( parent
, printout
, message
);
356 bool wxPrinter::Setup(wxWindow
*parent
)
358 return m_pimpl
->Setup( parent
);
361 bool wxPrinter::Print(wxWindow
*parent
, wxPrintout
*printout
, bool prompt
)
363 return m_pimpl
->Print( parent
, printout
, prompt
);
366 wxDC
* wxPrinter::PrintDialog(wxWindow
*parent
)
368 return m_pimpl
->PrintDialog( parent
);
371 wxPrintDialogData
& wxPrinter::GetPrintDialogData() const
373 return m_pimpl
->GetPrintDialogData();
376 // ---------------------------------------------------------------------------
377 // wxPrintDialogBase: the dialog for printing.
378 // ---------------------------------------------------------------------------
380 IMPLEMENT_ABSTRACT_CLASS(wxPrintDialogBase
, wxDialog
)
382 wxPrintDialogBase::wxPrintDialogBase(wxWindow
*parent
,
384 const wxString
&title
,
388 : wxDialog( parent
, id
, title
.empty() ? wxString(_("Print")) : title
,
393 // ---------------------------------------------------------------------------
394 // wxPrintDialog: the dialog for printing
395 // ---------------------------------------------------------------------------
397 IMPLEMENT_CLASS(wxPrintDialog
, wxObject
)
399 wxPrintDialog::wxPrintDialog(wxWindow
*parent
, wxPrintDialogData
* data
)
401 m_pimpl
= wxPrintFactory::GetFactory()->CreatePrintDialog( parent
, data
);
404 wxPrintDialog::wxPrintDialog(wxWindow
*parent
, wxPrintData
* data
)
406 m_pimpl
= wxPrintFactory::GetFactory()->CreatePrintDialog( parent
, data
);
409 wxPrintDialog::~wxPrintDialog()
414 int wxPrintDialog::ShowModal()
416 return m_pimpl
->ShowModal();
419 wxPrintDialogData
& wxPrintDialog::GetPrintDialogData()
421 return m_pimpl
->GetPrintDialogData();
424 wxPrintData
& wxPrintDialog::GetPrintData()
426 return m_pimpl
->GetPrintData();
429 wxDC
*wxPrintDialog::GetPrintDC()
431 return m_pimpl
->GetPrintDC();
434 // ---------------------------------------------------------------------------
435 // wxPageSetupDialogBase: the page setup dialog
436 // ---------------------------------------------------------------------------
438 IMPLEMENT_ABSTRACT_CLASS(wxPageSetupDialogBase
, wxDialog
)
440 wxPageSetupDialogBase::wxPageSetupDialogBase(wxWindow
*parent
,
442 const wxString
&title
,
446 : wxDialog( parent
, id
, title
.empty() ? wxString(_("Page setup")) : title
,
451 // ---------------------------------------------------------------------------
452 // wxPageSetupDialog: the page setup dialog
453 // ---------------------------------------------------------------------------
455 IMPLEMENT_CLASS(wxPageSetupDialog
, wxObject
)
457 wxPageSetupDialog::wxPageSetupDialog(wxWindow
*parent
, wxPageSetupDialogData
*data
)
459 m_pimpl
= wxPrintFactory::GetFactory()->CreatePageSetupDialog( parent
, data
);
462 wxPageSetupDialog::~wxPageSetupDialog()
467 int wxPageSetupDialog::ShowModal()
469 return m_pimpl
->ShowModal();
472 wxPageSetupDialogData
& wxPageSetupDialog::GetPageSetupDialogData()
474 return m_pimpl
->GetPageSetupDialogData();
478 wxPageSetupDialogData
& wxPageSetupDialog::GetPageSetupData()
480 return m_pimpl
->GetPageSetupDialogData();
483 //----------------------------------------------------------------------------
484 // wxPrintAbortDialog
485 //----------------------------------------------------------------------------
487 BEGIN_EVENT_TABLE(wxPrintAbortDialog
, wxDialog
)
488 EVT_BUTTON(wxID_CANCEL
, wxPrintAbortDialog::OnCancel
)
491 void wxPrintAbortDialog::OnCancel(wxCommandEvent
& WXUNUSED(event
))
493 wxPrinterBase::sm_abortIt
= true;
494 wxPrinterBase::sm_abortWindow
->Show(false);
495 wxPrinterBase::sm_abortWindow
->Close(true);
496 wxPrinterBase::sm_abortWindow
= (wxWindow
*) NULL
;
499 //----------------------------------------------------------------------------
501 //----------------------------------------------------------------------------
503 IMPLEMENT_ABSTRACT_CLASS(wxPrintout
, wxObject
)
505 wxPrintout::wxPrintout(const wxString
& title
)
507 m_printoutTitle
= title
;
508 m_printoutDC
= (wxDC
*) NULL
;
511 m_pageWidthPixels
= 0;
512 m_pageHeightPixels
= 0;
520 wxPrintout::~wxPrintout()
524 bool wxPrintout::OnBeginDocument(int WXUNUSED(startPage
), int WXUNUSED(endPage
))
526 return GetDC()->StartDoc(_("Printing ") + m_printoutTitle
);
529 void wxPrintout::OnEndDocument()
534 void wxPrintout::OnBeginPrinting()
538 void wxPrintout::OnEndPrinting()
542 bool wxPrintout::HasPage(int page
)
547 void wxPrintout::GetPageInfo(int *minPage
, int *maxPage
, int *fromPage
, int *toPage
)
555 //----------------------------------------------------------------------------
557 //----------------------------------------------------------------------------
559 IMPLEMENT_CLASS(wxPreviewCanvas
, wxWindow
)
561 BEGIN_EVENT_TABLE(wxPreviewCanvas
, wxScrolledWindow
)
562 EVT_PAINT(wxPreviewCanvas::OnPaint
)
563 EVT_CHAR(wxPreviewCanvas::OnChar
)
564 EVT_SYS_COLOUR_CHANGED(wxPreviewCanvas::OnSysColourChanged
)
566 EVT_MOUSEWHEEL(wxPreviewCanvas::OnMouseWheel
)
570 // VZ: the current code doesn't refresh properly without
571 // wxFULL_REPAINT_ON_RESIZE, this must be fixed as otherwise we have
572 // really horrible flicker when resizing the preview frame, but without
573 // this style it simply doesn't work correctly at all...
574 wxPreviewCanvas::wxPreviewCanvas(wxPrintPreviewBase
*preview
, wxWindow
*parent
,
575 const wxPoint
& pos
, const wxSize
& size
, long style
, const wxString
& name
):
576 wxScrolledWindow(parent
, wxID_ANY
, pos
, size
, style
| wxFULL_REPAINT_ON_RESIZE
, name
)
578 m_printPreview
= preview
;
580 // The app workspace colour is always white, but we should have
581 // a contrast with the page.
582 wxSystemColour colourIndex
= wxSYS_COLOUR_3DDKSHADOW
;
584 wxSystemColour colourIndex
= wxSYS_COLOUR_APPWORKSPACE
;
586 SetBackgroundColour(wxSystemSettings::GetColour(colourIndex
));
588 SetScrollbars(10, 10, 100, 100);
591 wxPreviewCanvas::~wxPreviewCanvas()
595 void wxPreviewCanvas::OnPaint(wxPaintEvent
& WXUNUSED(event
))
602 if (!GetUpdateRegion().IsEmpty())
603 dc.SetClippingRegion( GetUpdateRegion() );
609 m_printPreview
->PaintPage(this, dc
);
613 // Responds to colour changes, and passes event on to children.
614 void wxPreviewCanvas::OnSysColourChanged(wxSysColourChangedEvent
& event
)
617 // The app workspace colour is always white, but we should have
618 // a contrast with the page.
619 wxSystemColour colourIndex
= wxSYS_COLOUR_3DDKSHADOW
;
621 wxSystemColour colourIndex
= wxSYS_COLOUR_APPWORKSPACE
;
623 SetBackgroundColour(wxSystemSettings::GetColour(colourIndex
));
626 // Propagate the event to the non-top-level children
627 wxWindow::OnSysColourChanged(event
);
630 void wxPreviewCanvas::OnChar(wxKeyEvent
&event
)
632 wxPreviewControlBar
* controlBar
= ((wxPreviewFrame
*) GetParent())->GetControlBar();
633 if (event
.GetKeyCode() == WXK_ESCAPE
)
635 ((wxPreviewFrame
*) GetParent())->Close(true);
638 else if (event
.GetKeyCode() == WXK_TAB
)
640 controlBar
->OnGoto();
643 else if (event
.GetKeyCode() == WXK_RETURN
)
645 controlBar
->OnPrint();
649 if (!event
.ControlDown())
655 switch(event
.GetKeyCode())
658 controlBar
->OnNext(); break;
660 controlBar
->OnPrevious(); break;
662 controlBar
->OnFirst(); break;
664 controlBar
->OnLast(); break;
672 void wxPreviewCanvas::OnMouseWheel(wxMouseEvent
& event
)
674 wxPreviewControlBar
*
675 controlBar
= wxStaticCast(GetParent(), wxPreviewFrame
)->GetControlBar();
679 if ( event
.ControlDown() && event
.GetWheelRotation() != 0 )
681 int currentZoom
= controlBar
->GetZoomControl();
684 if ( currentZoom
< 100 )
686 else if ( currentZoom
<= 120 )
691 if ( event
.GetWheelRotation() > 0 )
694 int newZoom
= currentZoom
+ delta
;
699 if ( newZoom
!= currentZoom
)
701 controlBar
->SetZoomControl(newZoom
);
702 m_printPreview
->SetZoom(newZoom
);
712 #endif // wxUSE_MOUSEWHEEL
714 //----------------------------------------------------------------------------
715 // wxPreviewControlBar
716 //----------------------------------------------------------------------------
718 IMPLEMENT_CLASS(wxPreviewControlBar
, wxWindow
)
720 BEGIN_EVENT_TABLE(wxPreviewControlBar
, wxPanel
)
721 EVT_BUTTON(wxID_PREVIEW_CLOSE
, wxPreviewControlBar::OnWindowClose
)
722 EVT_BUTTON(wxID_PREVIEW_PRINT
, wxPreviewControlBar::OnPrintButton
)
723 EVT_BUTTON(wxID_PREVIEW_PREVIOUS
, wxPreviewControlBar::OnPreviousButton
)
724 EVT_BUTTON(wxID_PREVIEW_NEXT
, wxPreviewControlBar::OnNextButton
)
725 EVT_BUTTON(wxID_PREVIEW_FIRST
, wxPreviewControlBar::OnFirstButton
)
726 EVT_BUTTON(wxID_PREVIEW_LAST
, wxPreviewControlBar::OnLastButton
)
727 EVT_BUTTON(wxID_PREVIEW_GOTO
, wxPreviewControlBar::OnGotoButton
)
728 EVT_CHOICE(wxID_PREVIEW_ZOOM
, wxPreviewControlBar::OnZoom
)
729 EVT_PAINT(wxPreviewControlBar::OnPaint
)
732 wxPreviewControlBar::wxPreviewControlBar(wxPrintPreviewBase
*preview
, long buttons
,
733 wxWindow
*parent
, const wxPoint
& pos
, const wxSize
& size
,
734 long style
, const wxString
& name
):
735 wxPanel(parent
, wxID_ANY
, pos
, size
, style
, name
)
737 m_printPreview
= preview
;
738 m_closeButton
= (wxButton
*) NULL
;
739 m_nextPageButton
= (wxButton
*) NULL
;
740 m_previousPageButton
= (wxButton
*) NULL
;
741 m_printButton
= (wxButton
*) NULL
;
742 m_zoomControl
= (wxChoice
*) NULL
;
743 m_buttonFlags
= buttons
;
746 wxPreviewControlBar::~wxPreviewControlBar()
750 void wxPreviewControlBar::OnPaint(wxPaintEvent
& WXUNUSED(event
))
756 dc
.SetPen(*wxBLACK_PEN
);
757 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
758 dc
.DrawLine( 0, h
-1, w
, h
-1 );
761 void wxPreviewControlBar::OnWindowClose(wxCommandEvent
& WXUNUSED(event
))
763 wxPreviewFrame
*frame
= (wxPreviewFrame
*)GetParent();
767 void wxPreviewControlBar::OnPrint(void)
769 wxPrintPreviewBase
*preview
= GetPrintPreview();
770 preview
->Print(true);
773 void wxPreviewControlBar::OnNext(void)
775 wxPrintPreviewBase
*preview
= GetPrintPreview();
778 int currentPage
= preview
->GetCurrentPage();
779 if ((preview
->GetMaxPage() > 0) &&
780 (currentPage
< preview
->GetMaxPage()) &&
781 preview
->GetPrintout()->HasPage(currentPage
+ 1))
783 preview
->SetCurrentPage(currentPage
+ 1);
788 void wxPreviewControlBar::OnPrevious(void)
790 wxPrintPreviewBase
*preview
= GetPrintPreview();
793 int currentPage
= preview
->GetCurrentPage();
794 if ((preview
->GetMinPage() > 0) &&
795 (currentPage
> preview
->GetMinPage()) &&
796 preview
->GetPrintout()->HasPage(currentPage
- 1))
798 preview
->SetCurrentPage(currentPage
- 1);
803 void wxPreviewControlBar::OnFirst(void)
805 wxPrintPreviewBase
*preview
= GetPrintPreview();
808 int currentPage
= preview
->GetMinPage();
809 if (preview
->GetPrintout()->HasPage(currentPage
))
811 preview
->SetCurrentPage(currentPage
);
816 void wxPreviewControlBar::OnLast(void)
818 wxPrintPreviewBase
*preview
= GetPrintPreview();
821 int currentPage
= preview
->GetMaxPage();
822 if (preview
->GetPrintout()->HasPage(currentPage
))
824 preview
->SetCurrentPage(currentPage
);
829 void wxPreviewControlBar::OnGoto(void)
831 wxPrintPreviewBase
*preview
= GetPrintPreview();
836 if (preview
->GetMinPage() > 0)
841 strPrompt
.Printf( _("Enter a page number between %d and %d:"),
842 preview
->GetMinPage(), preview
->GetMaxPage());
843 strPage
.Printf( wxT("%d"), preview
->GetCurrentPage() );
846 wxGetTextFromUser( strPrompt
, _("Goto Page"), strPage
, GetParent());
848 if ( strPage
.ToLong( ¤tPage
) )
849 if (preview
->GetPrintout()->HasPage(currentPage
))
851 preview
->SetCurrentPage(currentPage
);
857 void wxPreviewControlBar::OnZoom(wxCommandEvent
& WXUNUSED(event
))
859 int zoom
= GetZoomControl();
860 if (GetPrintPreview())
861 GetPrintPreview()->SetZoom(zoom
);
864 void wxPreviewControlBar::CreateButtons()
866 SetSize(0, 0, 400, 40);
868 wxBoxSizer
*item0
= new wxBoxSizer( wxHORIZONTAL
);
870 m_closeButton
= new wxButton( this, wxID_PREVIEW_CLOSE
, _("&Close"), wxDefaultPosition
, wxDefaultSize
, 0 );
871 item0
->Add( m_closeButton
, 0, wxALIGN_CENTRE
|wxALL
, 5 );
873 if (m_buttonFlags
& wxPREVIEW_PRINT
)
875 m_printButton
= new wxButton( this, wxID_PREVIEW_PRINT
, _("&Print..."), wxDefaultPosition
, wxDefaultSize
, 0 );
876 item0
->Add( m_printButton
, 0, wxALIGN_CENTRE
|wxALL
, 5 );
879 if (m_buttonFlags
& wxPREVIEW_FIRST
)
881 m_firstPageButton
= new wxButton( this, wxID_PREVIEW_FIRST
, _("|<<"), wxDefaultPosition
, wxDefaultSize
, wxBU_EXACTFIT
);
882 item0
->Add( m_firstPageButton
, 0, wxALIGN_CENTRE
|wxALL
, 5 );
885 if (m_buttonFlags
& wxPREVIEW_PREVIOUS
)
887 m_previousPageButton
= new wxButton( this, wxID_PREVIEW_PREVIOUS
, _("<<"), wxDefaultPosition
, wxDefaultSize
, wxBU_EXACTFIT
);
888 item0
->Add( m_previousPageButton
, 0, wxALIGN_CENTRE
|wxRIGHT
|wxTOP
|wxBOTTOM
, 5 );
891 if (m_buttonFlags
& wxPREVIEW_NEXT
)
893 m_nextPageButton
= new wxButton( this, wxID_PREVIEW_NEXT
, _(">>"), wxDefaultPosition
, wxDefaultSize
, wxBU_EXACTFIT
);
894 item0
->Add( m_nextPageButton
, 0, wxALIGN_CENTRE
|wxRIGHT
|wxTOP
|wxBOTTOM
, 5 );
897 if (m_buttonFlags
& wxPREVIEW_LAST
)
899 m_lastPageButton
= new wxButton( this, wxID_PREVIEW_LAST
, _(">>|"), wxDefaultPosition
, wxDefaultSize
, wxBU_EXACTFIT
);
900 item0
->Add( m_lastPageButton
, 0, wxALIGN_CENTRE
|wxRIGHT
|wxTOP
|wxBOTTOM
, 5 );
903 if (m_buttonFlags
& wxPREVIEW_GOTO
)
905 m_gotoPageButton
= new wxButton( this, wxID_PREVIEW_GOTO
, _("&Goto..."), wxDefaultPosition
, wxDefaultSize
, 0 );
906 item0
->Add( m_gotoPageButton
, 0, wxALIGN_CENTRE
|wxALL
, 5 );
909 if (m_buttonFlags
& wxPREVIEW_ZOOM
)
913 wxT("10%"), wxT("15%"), wxT("20%"), wxT("25%"), wxT("30%"), wxT("35%"), wxT("40%"), wxT("45%"), wxT("50%"), wxT("55%"),
914 wxT("60%"), wxT("65%"), wxT("70%"), wxT("75%"), wxT("80%"), wxT("85%"), wxT("90%"), wxT("95%"), wxT("100%"), wxT("110%"),
915 wxT("120%"), wxT("150%"), wxT("200%")
917 int n
= WXSIZEOF(choices
);
919 m_zoomControl
= new wxChoice( this, wxID_PREVIEW_ZOOM
, wxDefaultPosition
, wxSize(70,wxDefaultCoord
), n
, choices
, 0 );
920 item0
->Add( m_zoomControl
, 0, wxALIGN_CENTRE
|wxALL
, 5 );
921 SetZoomControl(m_printPreview
->GetZoom());
928 void wxPreviewControlBar::SetZoomControl(int zoom
)
932 int n
, count
= m_zoomControl
->GetCount();
934 for (n
=0; n
<count
; n
++)
936 if (m_zoomControl
->GetString(n
).BeforeFirst(wxT('%')).ToLong(&val
) &&
939 m_zoomControl
->SetSelection(n
);
944 m_zoomControl
->SetSelection(count
-1);
948 int wxPreviewControlBar::GetZoomControl()
950 if (m_zoomControl
&& (m_zoomControl
->GetStringSelection() != wxEmptyString
))
953 if (m_zoomControl
->GetStringSelection().BeforeFirst(wxT('%')).ToLong(&val
))
965 IMPLEMENT_CLASS(wxPreviewFrame
, wxFrame
)
967 BEGIN_EVENT_TABLE(wxPreviewFrame
, wxFrame
)
968 EVT_CLOSE(wxPreviewFrame::OnCloseWindow
)
971 wxPreviewFrame::wxPreviewFrame(wxPrintPreviewBase
*preview
, wxWindow
*parent
, const wxString
& title
,
972 const wxPoint
& pos
, const wxSize
& size
, long style
, const wxString
& name
):
973 wxFrame(parent
, wxID_ANY
, title
, pos
, size
, style
, name
)
975 m_printPreview
= preview
;
977 m_previewCanvas
= NULL
;
978 m_windowDisabler
= NULL
;
980 // Give the application icon
982 wxFrame
* topFrame
= wxDynamicCast(wxTheApp
->GetTopWindow(), wxFrame
);
984 SetIcon(topFrame
->GetIcon());
988 wxPreviewFrame::~wxPreviewFrame()
992 void wxPreviewFrame::OnCloseWindow(wxCloseEvent
& WXUNUSED(event
))
994 if (m_windowDisabler
)
995 delete m_windowDisabler
;
997 // Need to delete the printout and the print preview
998 wxPrintout
*printout
= m_printPreview
->GetPrintout();
1002 m_printPreview
->SetPrintout(NULL
);
1003 m_printPreview
->SetCanvas(NULL
);
1004 m_printPreview
->SetFrame(NULL
);
1006 delete m_printPreview
;
1011 void wxPreviewFrame::Initialize()
1019 m_printPreview
->SetCanvas(m_previewCanvas
);
1020 m_printPreview
->SetFrame(this);
1022 wxBoxSizer
*item0
= new wxBoxSizer( wxVERTICAL
);
1024 item0
->Add( m_controlBar
, 0, wxGROW
|wxALIGN_CENTER_VERTICAL
, 5 );
1025 item0
->Add( m_previewCanvas
, 1, wxGROW
|wxALIGN_CENTER_VERTICAL
, 5 );
1027 SetAutoLayout( true );
1030 m_windowDisabler
= new wxWindowDisabler(this);
1034 m_printPreview
->AdjustScrollbars(m_previewCanvas
);
1035 m_previewCanvas
->SetFocus();
1036 m_controlBar
->SetFocus();
1039 void wxPreviewFrame::CreateCanvas()
1041 m_previewCanvas
= new wxPreviewCanvas(m_printPreview
, this);
1044 void wxPreviewFrame::CreateControlBar()
1046 long buttons
= wxPREVIEW_DEFAULT
;
1047 if (m_printPreview
->GetPrintoutForPrinting())
1048 buttons
|= wxPREVIEW_PRINT
;
1050 m_controlBar
= new wxPreviewControlBar(m_printPreview
, buttons
, this, wxPoint(0,0), wxSize(400, 40));
1051 m_controlBar
->CreateButtons();
1058 IMPLEMENT_CLASS(wxPrintPreviewBase
, wxObject
)
1060 wxPrintPreviewBase::wxPrintPreviewBase(wxPrintout
*printout
,
1061 wxPrintout
*printoutForPrinting
,
1065 m_printDialogData
= (*data
);
1067 Init(printout
, printoutForPrinting
);
1070 wxPrintPreviewBase::wxPrintPreviewBase(wxPrintout
*printout
,
1071 wxPrintout
*printoutForPrinting
,
1072 wxPrintDialogData
*data
)
1075 m_printDialogData
= (*data
);
1077 Init(printout
, printoutForPrinting
);
1080 void wxPrintPreviewBase::Init(wxPrintout
*printout
,
1081 wxPrintout
*printoutForPrinting
)
1084 m_previewPrintout
= printout
;
1085 if (m_previewPrintout
)
1086 m_previewPrintout
->SetIsPreview(true);
1088 m_printPrintout
= printoutForPrinting
;
1090 m_previewCanvas
= NULL
;
1091 m_previewFrame
= NULL
;
1092 m_previewBitmap
= NULL
;
1099 m_printingPrepared
= false;
1104 wxPrintPreviewBase::~wxPrintPreviewBase()
1106 if (m_previewPrintout
)
1107 delete m_previewPrintout
;
1108 if (m_previewBitmap
)
1109 delete m_previewBitmap
;
1110 if (m_printPrintout
)
1111 delete m_printPrintout
;
1114 bool wxPrintPreviewBase::SetCurrentPage(int pageNum
)
1116 if (m_currentPage
== pageNum
)
1119 m_currentPage
= pageNum
;
1120 if (m_previewBitmap
)
1122 delete m_previewBitmap
;
1123 m_previewBitmap
= NULL
;
1126 if (m_previewCanvas
)
1128 AdjustScrollbars(m_previewCanvas
);
1130 if (!RenderPage(pageNum
))
1132 m_previewCanvas
->Refresh();
1133 m_previewCanvas
->SetFocus();
1138 int wxPrintPreviewBase::GetCurrentPage() const
1139 { return m_currentPage
; };
1140 void wxPrintPreviewBase::SetPrintout(wxPrintout
*printout
)
1141 { m_previewPrintout
= printout
; };
1142 wxPrintout
*wxPrintPreviewBase::GetPrintout() const
1143 { return m_previewPrintout
; };
1144 wxPrintout
*wxPrintPreviewBase::GetPrintoutForPrinting() const
1145 { return m_printPrintout
; };
1146 void wxPrintPreviewBase::SetFrame(wxFrame
*frame
)
1147 { m_previewFrame
= frame
; };
1148 void wxPrintPreviewBase::SetCanvas(wxPreviewCanvas
*canvas
)
1149 { m_previewCanvas
= canvas
; };
1150 wxFrame
*wxPrintPreviewBase::GetFrame() const
1151 { return m_previewFrame
; }
1152 wxPreviewCanvas
*wxPrintPreviewBase::GetCanvas() const
1153 { return m_previewCanvas
; }
1155 bool wxPrintPreviewBase::PaintPage(wxPreviewCanvas
*canvas
, wxDC
& dc
)
1157 DrawBlankPage(canvas
, dc
);
1159 if (!m_previewBitmap
)
1160 if (!RenderPage(m_currentPage
))
1163 if (!m_previewBitmap
)
1169 int canvasWidth
, canvasHeight
;
1170 canvas
->GetSize(&canvasWidth
, &canvasHeight
);
1172 double zoomScale
= ((float)m_currentZoom
/(float)100);
1173 double actualWidth
= (zoomScale
*m_pageWidth
*m_previewScale
);
1174 // float actualHeight = (float)(zoomScale*m_pageHeight*m_previewScale);
1176 int x
= (int) ((canvasWidth
- actualWidth
)/2.0);
1177 if (x
< m_leftMargin
)
1179 int y
= m_topMargin
;
1182 temp_dc
.SelectObject(*m_previewBitmap
);
1184 dc
.Blit(x
, y
, m_previewBitmap
->GetWidth(), m_previewBitmap
->GetHeight(), &temp_dc
, 0, 0);
1186 temp_dc
.SelectObject(wxNullBitmap
);
1191 // Adjusts the scrollbars for the current scale
1192 void wxPrintPreviewBase::AdjustScrollbars(wxPreviewCanvas
*canvas
)
1197 int canvasWidth
, canvasHeight
;
1198 canvas
->GetSize(&canvasWidth
, &canvasHeight
);
1200 double zoomScale
= ((float)m_currentZoom
/(float)100);
1201 double actualWidth
= (zoomScale
*m_pageWidth
*m_previewScale
);
1202 double actualHeight
= (zoomScale
*m_pageHeight
*m_previewScale
);
1204 // Set the scrollbars appropriately
1205 int totalWidth
= (int)(actualWidth
+ 2*m_leftMargin
);
1206 int totalHeight
= (int)(actualHeight
+ 2*m_topMargin
);
1207 int scrollUnitsX
= totalWidth
/10;
1208 int scrollUnitsY
= totalHeight
/10;
1209 wxSize virtualSize
= canvas
->GetVirtualSize();
1210 if (virtualSize
.GetWidth() != totalWidth
|| virtualSize
.GetHeight() != totalHeight
)
1211 canvas
->SetScrollbars(10, 10, scrollUnitsX
, scrollUnitsY
, 0, 0, true);
1214 bool wxPrintPreviewBase::RenderPage(int pageNum
)
1218 int canvasWidth
, canvasHeight
;
1220 if (!m_previewCanvas
)
1222 wxFAIL_MSG(_T("wxPrintPreviewBase::RenderPage: must use wxPrintPreviewBase::SetCanvas to let me know about the canvas!"));
1226 m_previewCanvas
->GetSize(&canvasWidth
, &canvasHeight
);
1228 double zoomScale
= (m_currentZoom
/100.0);
1229 int actualWidth
= (int)(zoomScale
*m_pageWidth
*m_previewScale
);
1230 int actualHeight
= (int)(zoomScale
*m_pageHeight
*m_previewScale
);
1232 if (!m_previewBitmap
)
1234 m_previewBitmap
= new wxBitmap((int)actualWidth
, (int)actualHeight
);
1235 if (!m_previewBitmap
|| !m_previewBitmap
->Ok())
1237 if (m_previewBitmap
) {
1238 delete m_previewBitmap
;
1239 m_previewBitmap
= NULL
;
1241 wxMessageBox(_("Sorry, not enough memory to create a preview."), _("Print Preview Failure"), wxOK
);
1246 wxMemoryDC memoryDC
;
1247 memoryDC
.SelectObject(*m_previewBitmap
);
1251 m_previewPrintout
->SetDC(&memoryDC
);
1252 m_previewPrintout
->SetPageSizePixels(m_pageWidth
, m_pageHeight
);
1254 // Need to delay OnPreparePrinting until here, so we have enough information.
1255 if (!m_printingPrepared
)
1257 m_previewPrintout
->OnPreparePrinting();
1259 m_previewPrintout
->GetPageInfo(&m_minPage
, &m_maxPage
, &selFrom
, &selTo
);
1260 m_printingPrepared
= true;
1263 m_previewPrintout
->OnBeginPrinting();
1265 if (!m_previewPrintout
->OnBeginDocument(m_printDialogData
.GetFromPage(), m_printDialogData
.GetToPage()))
1267 wxMessageBox(_("Could not start document preview."), _("Print Preview Failure"), wxOK
);
1269 memoryDC
.SelectObject(wxNullBitmap
);
1271 delete m_previewBitmap
;
1272 m_previewBitmap
= NULL
;
1276 m_previewPrintout
->OnPrintPage(pageNum
);
1277 m_previewPrintout
->OnEndDocument();
1278 m_previewPrintout
->OnEndPrinting();
1280 m_previewPrintout
->SetDC(NULL
);
1282 memoryDC
.SelectObject(wxNullBitmap
);
1287 status
= wxString::Format(_("Page %d of %d"), pageNum
, m_maxPage
);
1289 status
= wxString::Format(_("Page %d"), pageNum
);
1292 m_previewFrame
->SetStatusText(status
);
1299 bool wxPrintPreviewBase::DrawBlankPage(wxPreviewCanvas
*canvas
, wxDC
& dc
)
1301 int canvasWidth
, canvasHeight
;
1302 canvas
->GetSize(&canvasWidth
, &canvasHeight
);
1304 float zoomScale
= (float)((float)m_currentZoom
/(float)100);
1305 float actualWidth
= zoomScale
*m_pageWidth
*m_previewScale
;
1306 float actualHeight
= zoomScale
*m_pageHeight
*m_previewScale
;
1308 float x
= (float)((canvasWidth
- actualWidth
)/2.0);
1309 if (x
< m_leftMargin
)
1310 x
= (float)m_leftMargin
;
1311 float y
= (float)m_topMargin
;
1313 // Draw shadow, allowing for 1-pixel border AROUND the actual page
1314 int shadowOffset
= 4;
1315 dc
.SetPen(*wxBLACK_PEN
);
1316 dc
.SetBrush(*wxBLACK_BRUSH
);
1318 dc.DrawRectangle((int)(x-1 + shadowOffset), (int)(y-1 + shadowOffset), (int)(actualWidth+2), (int)(actualHeight+2));
1320 dc
.DrawRectangle((int)(x
+ shadowOffset
), (int)(y
+ actualHeight
+1), (int)(actualWidth
), shadowOffset
);
1321 dc
.DrawRectangle((int)(x
+ actualWidth
), (int)(y
+ shadowOffset
), shadowOffset
, (int)(actualHeight
));
1323 // Draw blank page allowing for 1-pixel border AROUND the actual page
1324 dc
.SetPen(*wxBLACK_PEN
);
1325 dc
.SetBrush(*wxWHITE_BRUSH
);
1328 wxRegion update_region = canvas->GetUpdateRegion();
1329 wxRect r = update_region.GetBox();
1331 printf( "x: %d y: %d w: %d h: %d.\n", (int)r.x, (int)r.y, (int)r.width, (int)r.height );
1334 dc
.DrawRectangle((int)(x
-2), (int)(y
-1), (int)(actualWidth
+3), (int)(actualHeight
+2));
1339 void wxPrintPreviewBase::SetZoom(int percent
)
1341 if (m_currentZoom
== percent
)
1344 m_currentZoom
= percent
;
1345 if (m_previewBitmap
)
1347 delete m_previewBitmap
;
1348 m_previewBitmap
= NULL
;
1351 if (m_previewCanvas
)
1353 AdjustScrollbars(m_previewCanvas
);
1354 RenderPage(m_currentPage
);
1355 ((wxScrolledWindow
*) m_previewCanvas
)->Scroll(0, 0);
1356 m_previewCanvas
->ClearBackground();
1357 m_previewCanvas
->Refresh();
1358 m_previewCanvas
->SetFocus();
1362 wxPrintDialogData
& wxPrintPreviewBase::GetPrintDialogData()
1364 return m_printDialogData
;
1367 int wxPrintPreviewBase::GetZoom() const
1368 { return m_currentZoom
; }
1369 int wxPrintPreviewBase::GetMaxPage() const
1370 { return m_maxPage
; }
1371 int wxPrintPreviewBase::GetMinPage() const
1372 { return m_minPage
; }
1373 bool wxPrintPreviewBase::Ok() const
1375 void wxPrintPreviewBase::SetOk(bool ok
)
1377 //----------------------------------------------------------------------------
1379 //----------------------------------------------------------------------------
1381 IMPLEMENT_CLASS(wxPrintPreview
, wxPrintPreviewBase
)
1383 wxPrintPreview::wxPrintPreview(wxPrintout
*printout
,
1384 wxPrintout
*printoutForPrinting
,
1385 wxPrintDialogData
*data
) :
1386 wxPrintPreviewBase( printout
, printoutForPrinting
, data
)
1388 m_pimpl
= wxPrintFactory::GetFactory()->
1389 CreatePrintPreview( printout
, printoutForPrinting
, data
);
1392 wxPrintPreview::wxPrintPreview(wxPrintout
*printout
,
1393 wxPrintout
*printoutForPrinting
,
1394 wxPrintData
*data
) :
1395 wxPrintPreviewBase( printout
, printoutForPrinting
, data
)
1397 m_pimpl
= wxPrintFactory::GetFactory()->
1398 CreatePrintPreview( printout
, printoutForPrinting
, data
);
1401 wxPrintPreview::~wxPrintPreview()
1405 // don't delete twice
1406 m_printPrintout
= NULL
;
1407 m_previewPrintout
= NULL
;
1408 m_previewBitmap
= NULL
;
1411 bool wxPrintPreview::SetCurrentPage(int pageNum
)
1413 return m_pimpl
->SetCurrentPage( pageNum
);
1416 int wxPrintPreview::GetCurrentPage() const
1418 return m_pimpl
->GetCurrentPage();
1421 void wxPrintPreview::SetPrintout(wxPrintout
*printout
)
1423 m_pimpl
->SetPrintout( printout
);
1426 wxPrintout
*wxPrintPreview::GetPrintout() const
1428 return m_pimpl
->GetPrintout();
1431 wxPrintout
*wxPrintPreview::GetPrintoutForPrinting() const
1433 return m_pimpl
->GetPrintoutForPrinting();
1436 void wxPrintPreview::SetFrame(wxFrame
*frame
)
1438 m_pimpl
->SetFrame( frame
);
1441 void wxPrintPreview::SetCanvas(wxPreviewCanvas
*canvas
)
1443 m_pimpl
->SetCanvas( canvas
);
1446 wxFrame
*wxPrintPreview::GetFrame() const
1448 return m_pimpl
->GetFrame();
1451 wxPreviewCanvas
*wxPrintPreview::GetCanvas() const
1453 return m_pimpl
->GetCanvas();
1456 bool wxPrintPreview::PaintPage(wxPreviewCanvas
*canvas
, wxDC
& dc
)
1458 return m_pimpl
->PaintPage( canvas
, dc
);
1461 bool wxPrintPreview::DrawBlankPage(wxPreviewCanvas
*canvas
, wxDC
& dc
)
1463 return m_pimpl
->DrawBlankPage( canvas
, dc
);
1466 void wxPrintPreview::AdjustScrollbars(wxPreviewCanvas
*canvas
)
1468 m_pimpl
->AdjustScrollbars( canvas
);
1471 bool wxPrintPreview::RenderPage(int pageNum
)
1473 return m_pimpl
->RenderPage( pageNum
);
1476 void wxPrintPreview::SetZoom(int percent
)
1478 m_pimpl
->SetZoom( percent
);
1481 wxPrintDialogData
& wxPrintPreview::GetPrintDialogData()
1483 return m_pimpl
->GetPrintDialogData();
1486 int wxPrintPreview::GetMaxPage() const
1488 return m_pimpl
->GetMaxPage();
1491 int wxPrintPreview::GetMinPage() const
1493 return m_pimpl
->GetMinPage();
1496 bool wxPrintPreview::Ok() const
1498 return m_pimpl
->Ok();
1501 void wxPrintPreview::SetOk(bool ok
)
1503 m_pimpl
->SetOk( ok
);
1506 bool wxPrintPreview::Print(bool interactive
)
1508 return m_pimpl
->Print( interactive
);
1511 void wxPrintPreview::DetermineScaling()
1513 m_pimpl
->DetermineScaling();
1517 #endif // wxUSE_PRINTING_ARCHITECTURE