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"
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/printdlg.h"
42 #include "wx/module.h"
47 #if defined(__WXMSW__) && !defined(__WXUNIVERSAL__)
48 #include "wx/msw/printdlg.h"
49 #elif defined(__WXMAC__)
50 #include "wx/mac/printdlg.h"
51 #include "wx/mac/private/print.h"
53 #include "wx/generic/prntdlgg.h"
57 #include "wx/msw/wrapcdlg.h"
63 //----------------------------------------------------------------------------
65 //----------------------------------------------------------------------------
67 wxPrintFactory
*wxPrintFactory::m_factory
= NULL
;
69 void wxPrintFactory::SetPrintFactory( wxPrintFactory
*factory
)
71 if (wxPrintFactory::m_factory
)
72 delete wxPrintFactory::m_factory
;
74 wxPrintFactory::m_factory
= factory
;
77 wxPrintFactory
*wxPrintFactory::GetFactory()
79 if (!wxPrintFactory::m_factory
)
80 wxPrintFactory::m_factory
= new wxNativePrintFactory
;
82 return wxPrintFactory::m_factory
;
85 //----------------------------------------------------------------------------
86 // wxNativePrintFactory
87 //----------------------------------------------------------------------------
89 wxPrinterBase
*wxNativePrintFactory::CreatePrinter( wxPrintDialogData
*data
)
91 #if defined(__WXMSW__) && !defined(__WXUNIVERSAL__)
92 return new wxWindowsPrinter( data
);
93 #elif defined(__WXMAC__)
94 return new wxMacPrinter( data
);
95 #elif defined(__WXPM__)
96 return new wxOS2Printer( data
);
98 return new wxPostScriptPrinter( data
);
102 wxPrintPreviewBase
*wxNativePrintFactory::CreatePrintPreview( wxPrintout
*preview
,
103 wxPrintout
*printout
, wxPrintDialogData
*data
)
105 #if defined(__WXMSW__) && !defined(__WXUNIVERSAL__)
106 return new wxWindowsPrintPreview( preview
, printout
, data
);
107 #elif defined(__WXMAC__)
108 return new wxMacPrintPreview( preview
, printout
, data
);
109 #elif defined(__WXPM__)
110 return new wxOS2PrintPreview( preview
, printout
, data
);
112 return new wxPostScriptPrintPreview( preview
, printout
, data
);
116 wxPrintPreviewBase
*wxNativePrintFactory::CreatePrintPreview( wxPrintout
*preview
,
117 wxPrintout
*printout
, wxPrintData
*data
)
119 #if defined(__WXMSW__) && !defined(__WXUNIVERSAL__)
120 return new wxWindowsPrintPreview( preview
, printout
, data
);
121 #elif defined(__WXMAC__)
122 return new wxMacPrintPreview( preview
, printout
, data
);
123 #elif defined(__WXPM__)
124 return new wxOS2PrintPreview( preview
, printout
, data
);
126 return new wxPostScriptPrintPreview( preview
, printout
, data
);
130 wxPrintDialogBase
*wxNativePrintFactory::CreatePrintDialog( wxWindow
*parent
,
131 wxPrintDialogData
*data
)
133 #if defined(__WXMSW__) && !defined(__WXUNIVERSAL__)
134 return new wxWindowsPrintDialog( parent
, data
);
135 #elif defined(__WXMAC__)
136 return new wxMacPrintDialog( parent
, data
);
138 return new wxGenericPrintDialog( parent
, data
);
142 wxPrintDialogBase
*wxNativePrintFactory::CreatePrintDialog( wxWindow
*parent
,
145 #if defined(__WXMSW__) && !defined(__WXUNIVERSAL__)
146 return new wxWindowsPrintDialog( parent
, data
);
147 #elif defined(__WXMAC__)
148 return new wxMacPrintDialog( parent
, data
);
150 return new wxGenericPrintDialog( parent
, data
);
154 wxPageSetupDialogBase
*wxNativePrintFactory::CreatePageSetupDialog( wxWindow
*parent
,
155 wxPageSetupDialogData
*data
)
157 #if defined(__WXMSW__) && !defined(__WXUNIVERSAL__)
158 return new wxWindowsPageSetupDialog( parent
, data
);
159 #elif defined(__WXMAC__)
160 return new wxMacPageSetupDialog( parent
, data
);
162 return new wxGenericPageSetupDialog( parent
, data
);
166 bool wxNativePrintFactory::HasPrintSetupDialog()
168 #if defined(__WXMSW__) && !defined(__WXUNIVERSAL__)
170 #elif defined(__WXMAC__)
173 // Only here do we need to provide the print setup
174 // dialog ourselves, the other platforms either have
175 // none, don't make it accessible or let you configure
176 // the printer from the wxPrintDialog anyway.
182 wxDialog
*wxNativePrintFactory::CreatePrintSetupDialog( wxWindow
*parent
,
185 #if defined(__WXMSW__) && !defined(__WXUNIVERSAL__)
189 #elif defined(__WXMAC__)
194 // Only here do we need to provide the print setup
195 // dialog ourselves, the other platforms either have
196 // none, don't make it accessible or let you configure
197 // the printer from the wxPrintDialog anyway.
198 return new wxGenericPrintSetupDialog( parent
, data
);
202 bool wxNativePrintFactory::HasOwnPrintToFile()
204 // Only relevant for PostScript and here the
205 // setup dialog provides no "print to file"
206 // option. In the GNOME setup dialog, the
207 // setup dialog has its own print to file.
211 bool wxNativePrintFactory::HasPrinterLine()
213 // Only relevant for PostScript for now
217 wxString
wxNativePrintFactory::CreatePrinterLine()
219 // Only relevant for PostScript for now
221 // We should query "lpstat -d" here
222 return _("Generic PostScript");
225 bool wxNativePrintFactory::HasStatusLine()
227 // Only relevant for PostScript for now
231 wxString
wxNativePrintFactory::CreateStatusLine()
233 // Only relevant for PostScript for now
235 // We should query "lpstat -r" or "lpstat -p" here
239 wxPrintNativeDataBase
*wxNativePrintFactory::CreatePrintNativeData()
241 #if defined(__WXMSW__) && !defined(__WXUNIVERSAL__)
242 return new wxWindowsPrintNativeData
;
243 #elif defined(__WXMAC__)
244 return new wxMacCarbonPrintData
;
246 return new wxPostScriptPrintNativeData
;
250 //----------------------------------------------------------------------------
251 // wxPrintNativeDataBase
252 //----------------------------------------------------------------------------
254 IMPLEMENT_ABSTRACT_CLASS(wxPrintNativeDataBase
, wxObject
)
256 wxPrintNativeDataBase::wxPrintNativeDataBase()
261 //----------------------------------------------------------------------------
262 // wxPrintFactoryModule
263 //----------------------------------------------------------------------------
265 class wxPrintFactoryModule
: public wxModule
268 wxPrintFactoryModule() {}
269 bool OnInit() { return true; }
270 void OnExit() { wxPrintFactory::SetPrintFactory( NULL
); }
273 DECLARE_DYNAMIC_CLASS(wxPrintFactoryModule
)
276 IMPLEMENT_DYNAMIC_CLASS(wxPrintFactoryModule
, wxModule
)
278 //----------------------------------------------------------------------------
280 //----------------------------------------------------------------------------
282 IMPLEMENT_CLASS(wxPrinterBase
, wxObject
)
284 wxPrinterBase::wxPrinterBase(wxPrintDialogData
*data
)
286 m_currentPrintout
= (wxPrintout
*) NULL
;
287 sm_abortWindow
= (wxWindow
*) NULL
;
290 m_printDialogData
= (*data
);
291 sm_lastError
= wxPRINTER_NO_ERROR
;
294 wxWindow
*wxPrinterBase::sm_abortWindow
= (wxWindow
*) NULL
;
295 bool wxPrinterBase::sm_abortIt
= false;
296 wxPrinterError
wxPrinterBase::sm_lastError
= wxPRINTER_NO_ERROR
;
298 wxPrinterBase::~wxPrinterBase()
302 wxWindow
*wxPrinterBase::CreateAbortWindow(wxWindow
*parent
, wxPrintout
* printout
)
304 wxPrintAbortDialog
*dialog
= new wxPrintAbortDialog(parent
, _("Printing ") , wxDefaultPosition
, wxDefaultSize
, wxDEFAULT_DIALOG_STYLE
);
306 wxBoxSizer
*button_sizer
= new wxBoxSizer( wxVERTICAL
);
307 button_sizer
->Add( new wxStaticText(dialog
, wxID_ANY
, _("Please wait while printing\n") + printout
->GetTitle() ), 0, wxALL
, 10 );
308 button_sizer
->Add( new wxButton( dialog
, wxID_CANCEL
, wxT("Cancel") ), 0, wxALL
| wxALIGN_CENTER
, 10 );
310 dialog
->SetAutoLayout( true );
311 dialog
->SetSizer( button_sizer
);
313 button_sizer
->Fit(dialog
);
314 button_sizer
->SetSizeHints (dialog
) ;
319 void wxPrinterBase::ReportError(wxWindow
*parent
, wxPrintout
*WXUNUSED(printout
), const wxString
& message
)
321 wxMessageBox(message
, _("Printing Error"), wxOK
, parent
);
324 wxPrintDialogData
& wxPrinterBase::GetPrintDialogData() const
326 return (wxPrintDialogData
&) m_printDialogData
;
329 //----------------------------------------------------------------------------
331 //----------------------------------------------------------------------------
333 IMPLEMENT_CLASS(wxPrinter
, wxPrinterBase
)
335 wxPrinter::wxPrinter(wxPrintDialogData
*data
)
337 m_pimpl
= wxPrintFactory::GetFactory()->CreatePrinter( data
);
340 wxPrinter::~wxPrinter()
345 wxWindow
*wxPrinter::CreateAbortWindow(wxWindow
*parent
, wxPrintout
*printout
)
347 return m_pimpl
->CreateAbortWindow( parent
, printout
);
350 void wxPrinter::ReportError(wxWindow
*parent
, wxPrintout
*printout
, const wxString
& message
)
352 m_pimpl
->ReportError( parent
, printout
, message
);
355 bool wxPrinter::Setup(wxWindow
*parent
)
357 return m_pimpl
->Setup( parent
);
360 bool wxPrinter::Print(wxWindow
*parent
, wxPrintout
*printout
, bool prompt
)
362 return m_pimpl
->Print( parent
, printout
, prompt
);
365 wxDC
* wxPrinter::PrintDialog(wxWindow
*parent
)
367 return m_pimpl
->PrintDialog( parent
);
370 wxPrintDialogData
& wxPrinter::GetPrintDialogData() const
372 return m_pimpl
->GetPrintDialogData();
375 // ---------------------------------------------------------------------------
376 // wxPrintDialogBase: the dialog for printing.
377 // ---------------------------------------------------------------------------
379 IMPLEMENT_ABSTRACT_CLASS(wxPrintDialogBase
, wxDialog
)
381 wxPrintDialogBase::wxPrintDialogBase(wxWindow
*parent
,
383 const wxString
&title
,
387 : wxDialog( parent
, id
, title
.empty() ? wxString(_("Print")) : title
,
392 // ---------------------------------------------------------------------------
393 // wxPrintDialog: the dialog for printing
394 // ---------------------------------------------------------------------------
396 IMPLEMENT_CLASS(wxPrintDialog
, wxObject
)
398 wxPrintDialog::wxPrintDialog(wxWindow
*parent
, wxPrintDialogData
* data
)
400 m_pimpl
= wxPrintFactory::GetFactory()->CreatePrintDialog( parent
, data
);
403 wxPrintDialog::wxPrintDialog(wxWindow
*parent
, wxPrintData
* data
)
405 m_pimpl
= wxPrintFactory::GetFactory()->CreatePrintDialog( parent
, data
);
408 wxPrintDialog::~wxPrintDialog()
413 int wxPrintDialog::ShowModal()
415 return m_pimpl
->ShowModal();
418 wxPrintDialogData
& wxPrintDialog::GetPrintDialogData()
420 return m_pimpl
->GetPrintDialogData();
423 wxPrintData
& wxPrintDialog::GetPrintData()
425 return m_pimpl
->GetPrintData();
428 wxDC
*wxPrintDialog::GetPrintDC()
430 return m_pimpl
->GetPrintDC();
433 // ---------------------------------------------------------------------------
434 // wxPageSetupDialogBase: the page setup dialog
435 // ---------------------------------------------------------------------------
437 IMPLEMENT_ABSTRACT_CLASS(wxPageSetupDialogBase
, wxDialog
)
439 wxPageSetupDialogBase::wxPageSetupDialogBase(wxWindow
*parent
,
441 const wxString
&title
,
445 : wxDialog( parent
, id
, title
.empty() ? wxString(_("Page setup")) : title
,
450 // ---------------------------------------------------------------------------
451 // wxPageSetupDialog: the page setup dialog
452 // ---------------------------------------------------------------------------
454 IMPLEMENT_CLASS(wxPageSetupDialog
, wxObject
)
456 wxPageSetupDialog::wxPageSetupDialog(wxWindow
*parent
, wxPageSetupDialogData
*data
)
458 m_pimpl
= wxPrintFactory::GetFactory()->CreatePageSetupDialog( parent
, data
);
461 wxPageSetupDialog::~wxPageSetupDialog()
466 int wxPageSetupDialog::ShowModal()
468 return m_pimpl
->ShowModal();
471 wxPageSetupDialogData
& wxPageSetupDialog::GetPageSetupDialogData()
473 return m_pimpl
->GetPageSetupDialogData();
477 wxPageSetupDialogData
& wxPageSetupDialog::GetPageSetupData()
479 return m_pimpl
->GetPageSetupDialogData();
482 //----------------------------------------------------------------------------
483 // wxPrintAbortDialog
484 //----------------------------------------------------------------------------
486 BEGIN_EVENT_TABLE(wxPrintAbortDialog
, wxDialog
)
487 EVT_BUTTON(wxID_CANCEL
, wxPrintAbortDialog::OnCancel
)
490 void wxPrintAbortDialog::OnCancel(wxCommandEvent
& WXUNUSED(event
))
492 wxPrinterBase::sm_abortIt
= true;
493 wxPrinterBase::sm_abortWindow
->Show(false);
494 wxPrinterBase::sm_abortWindow
->Close(true);
495 wxPrinterBase::sm_abortWindow
= (wxWindow
*) NULL
;
498 //----------------------------------------------------------------------------
500 //----------------------------------------------------------------------------
502 IMPLEMENT_ABSTRACT_CLASS(wxPrintout
, wxObject
)
504 wxPrintout::wxPrintout(const wxString
& title
)
506 m_printoutTitle
= title
;
507 m_printoutDC
= (wxDC
*) NULL
;
510 m_pageWidthPixels
= 0;
511 m_pageHeightPixels
= 0;
519 wxPrintout::~wxPrintout()
523 bool wxPrintout::OnBeginDocument(int WXUNUSED(startPage
), int WXUNUSED(endPage
))
525 return GetDC()->StartDoc(_("Printing ") + m_printoutTitle
);
528 void wxPrintout::OnEndDocument()
533 void wxPrintout::OnBeginPrinting()
537 void wxPrintout::OnEndPrinting()
541 bool wxPrintout::HasPage(int page
)
546 void wxPrintout::GetPageInfo(int *minPage
, int *maxPage
, int *fromPage
, int *toPage
)
554 //----------------------------------------------------------------------------
556 //----------------------------------------------------------------------------
558 IMPLEMENT_CLASS(wxPreviewCanvas
, wxWindow
)
560 BEGIN_EVENT_TABLE(wxPreviewCanvas
, wxScrolledWindow
)
561 EVT_PAINT(wxPreviewCanvas::OnPaint
)
562 EVT_CHAR(wxPreviewCanvas::OnChar
)
563 EVT_SYS_COLOUR_CHANGED(wxPreviewCanvas::OnSysColourChanged
)
565 EVT_MOUSEWHEEL(wxPreviewCanvas::OnMouseWheel
)
569 // VZ: the current code doesn't refresh properly without
570 // wxFULL_REPAINT_ON_RESIZE, this must be fixed as otherwise we have
571 // really horrible flicker when resizing the preview frame, but without
572 // this style it simply doesn't work correctly at all...
573 wxPreviewCanvas::wxPreviewCanvas(wxPrintPreviewBase
*preview
, wxWindow
*parent
,
574 const wxPoint
& pos
, const wxSize
& size
, long style
, const wxString
& name
):
575 wxScrolledWindow(parent
, wxID_ANY
, pos
, size
, style
| wxFULL_REPAINT_ON_RESIZE
, name
)
577 m_printPreview
= preview
;
579 // The app workspace colour is always white, but we should have
580 // a contrast with the page.
581 wxSystemColour colourIndex
= wxSYS_COLOUR_3DDKSHADOW
;
583 wxSystemColour colourIndex
= wxSYS_COLOUR_APPWORKSPACE
;
585 SetBackgroundColour(wxSystemSettings::GetColour(colourIndex
));
587 SetScrollbars(10, 10, 100, 100);
590 wxPreviewCanvas::~wxPreviewCanvas()
594 void wxPreviewCanvas::OnPaint(wxPaintEvent
& WXUNUSED(event
))
601 if (!GetUpdateRegion().IsEmpty())
602 dc.SetClippingRegion( GetUpdateRegion() );
608 m_printPreview
->PaintPage(this, dc
);
612 // Responds to colour changes, and passes event on to children.
613 void wxPreviewCanvas::OnSysColourChanged(wxSysColourChangedEvent
& event
)
616 // The app workspace colour is always white, but we should have
617 // a contrast with the page.
618 wxSystemColour colourIndex
= wxSYS_COLOUR_3DDKSHADOW
;
620 wxSystemColour colourIndex
= wxSYS_COLOUR_APPWORKSPACE
;
622 SetBackgroundColour(wxSystemSettings::GetColour(colourIndex
));
625 // Propagate the event to the non-top-level children
626 wxWindow::OnSysColourChanged(event
);
629 void wxPreviewCanvas::OnChar(wxKeyEvent
&event
)
631 wxPreviewControlBar
* controlBar
= ((wxPreviewFrame
*) GetParent())->GetControlBar();
632 if (event
.GetKeyCode() == WXK_ESCAPE
)
634 ((wxPreviewFrame
*) GetParent())->Close(true);
637 else if (event
.GetKeyCode() == WXK_TAB
)
639 controlBar
->OnGoto();
642 else if (event
.GetKeyCode() == WXK_RETURN
)
644 controlBar
->OnPrint();
648 if (!event
.ControlDown())
654 switch(event
.GetKeyCode())
657 controlBar
->OnNext(); break;
659 controlBar
->OnPrevious(); break;
661 controlBar
->OnFirst(); break;
663 controlBar
->OnLast(); break;
671 void wxPreviewCanvas::OnMouseWheel(wxMouseEvent
& event
)
673 wxPreviewControlBar
*
674 controlBar
= wxStaticCast(GetParent(), wxPreviewFrame
)->GetControlBar();
678 if ( event
.ControlDown() && event
.GetWheelRotation() != 0 )
680 int currentZoom
= controlBar
->GetZoomControl();
683 if ( currentZoom
< 100 )
685 else if ( currentZoom
<= 120 )
690 if ( event
.GetWheelRotation() > 0 )
693 int newZoom
= currentZoom
+ delta
;
698 if ( newZoom
!= currentZoom
)
700 controlBar
->SetZoomControl(newZoom
);
701 m_printPreview
->SetZoom(newZoom
);
711 #endif // wxUSE_MOUSEWHEEL
713 //----------------------------------------------------------------------------
714 // wxPreviewControlBar
715 //----------------------------------------------------------------------------
717 IMPLEMENT_CLASS(wxPreviewControlBar
, wxWindow
)
719 BEGIN_EVENT_TABLE(wxPreviewControlBar
, wxPanel
)
720 EVT_BUTTON(wxID_PREVIEW_CLOSE
, wxPreviewControlBar::OnWindowClose
)
721 EVT_BUTTON(wxID_PREVIEW_PRINT
, wxPreviewControlBar::OnPrintButton
)
722 EVT_BUTTON(wxID_PREVIEW_PREVIOUS
, wxPreviewControlBar::OnPreviousButton
)
723 EVT_BUTTON(wxID_PREVIEW_NEXT
, wxPreviewControlBar::OnNextButton
)
724 EVT_BUTTON(wxID_PREVIEW_FIRST
, wxPreviewControlBar::OnFirstButton
)
725 EVT_BUTTON(wxID_PREVIEW_LAST
, wxPreviewControlBar::OnLastButton
)
726 EVT_BUTTON(wxID_PREVIEW_GOTO
, wxPreviewControlBar::OnGotoButton
)
727 EVT_CHOICE(wxID_PREVIEW_ZOOM
, wxPreviewControlBar::OnZoom
)
728 EVT_PAINT(wxPreviewControlBar::OnPaint
)
731 wxPreviewControlBar::wxPreviewControlBar(wxPrintPreviewBase
*preview
, long buttons
,
732 wxWindow
*parent
, const wxPoint
& pos
, const wxSize
& size
,
733 long style
, const wxString
& name
):
734 wxPanel(parent
, wxID_ANY
, pos
, size
, style
, name
)
736 m_printPreview
= preview
;
737 m_closeButton
= (wxButton
*) NULL
;
738 m_nextPageButton
= (wxButton
*) NULL
;
739 m_previousPageButton
= (wxButton
*) NULL
;
740 m_printButton
= (wxButton
*) NULL
;
741 m_zoomControl
= (wxChoice
*) NULL
;
742 m_buttonFlags
= buttons
;
745 wxPreviewControlBar::~wxPreviewControlBar()
749 void wxPreviewControlBar::OnPaint(wxPaintEvent
& WXUNUSED(event
))
755 dc
.SetPen(*wxBLACK_PEN
);
756 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
757 dc
.DrawLine( 0, h
-1, w
, h
-1 );
760 void wxPreviewControlBar::OnWindowClose(wxCommandEvent
& WXUNUSED(event
))
762 wxPreviewFrame
*frame
= (wxPreviewFrame
*)GetParent();
766 void wxPreviewControlBar::OnPrint(void)
768 wxPrintPreviewBase
*preview
= GetPrintPreview();
769 preview
->Print(true);
772 void wxPreviewControlBar::OnNext(void)
774 wxPrintPreviewBase
*preview
= GetPrintPreview();
777 int currentPage
= preview
->GetCurrentPage();
778 if ((preview
->GetMaxPage() > 0) &&
779 (currentPage
< preview
->GetMaxPage()) &&
780 preview
->GetPrintout()->HasPage(currentPage
+ 1))
782 preview
->SetCurrentPage(currentPage
+ 1);
787 void wxPreviewControlBar::OnPrevious(void)
789 wxPrintPreviewBase
*preview
= GetPrintPreview();
792 int currentPage
= preview
->GetCurrentPage();
793 if ((preview
->GetMinPage() > 0) &&
794 (currentPage
> preview
->GetMinPage()) &&
795 preview
->GetPrintout()->HasPage(currentPage
- 1))
797 preview
->SetCurrentPage(currentPage
- 1);
802 void wxPreviewControlBar::OnFirst(void)
804 wxPrintPreviewBase
*preview
= GetPrintPreview();
807 int currentPage
= preview
->GetMinPage();
808 if (preview
->GetPrintout()->HasPage(currentPage
))
810 preview
->SetCurrentPage(currentPage
);
815 void wxPreviewControlBar::OnLast(void)
817 wxPrintPreviewBase
*preview
= GetPrintPreview();
820 int currentPage
= preview
->GetMaxPage();
821 if (preview
->GetPrintout()->HasPage(currentPage
))
823 preview
->SetCurrentPage(currentPage
);
828 void wxPreviewControlBar::OnGoto(void)
830 wxPrintPreviewBase
*preview
= GetPrintPreview();
835 if (preview
->GetMinPage() > 0)
840 strPrompt
.Printf( _("Enter a page number between %d and %d:"),
841 preview
->GetMinPage(), preview
->GetMaxPage());
842 strPage
.Printf( wxT("%d"), preview
->GetCurrentPage() );
845 wxGetTextFromUser( strPrompt
, _("Goto Page"), strPage
, GetParent());
847 if ( strPage
.ToLong( ¤tPage
) )
848 if (preview
->GetPrintout()->HasPage(currentPage
))
850 preview
->SetCurrentPage(currentPage
);
856 void wxPreviewControlBar::OnZoom(wxCommandEvent
& WXUNUSED(event
))
858 int zoom
= GetZoomControl();
859 if (GetPrintPreview())
860 GetPrintPreview()->SetZoom(zoom
);
863 void wxPreviewControlBar::CreateButtons()
865 SetSize(0, 0, 400, 40);
867 wxBoxSizer
*item0
= new wxBoxSizer( wxHORIZONTAL
);
869 m_closeButton
= new wxButton( this, wxID_PREVIEW_CLOSE
, _("&Close"), wxDefaultPosition
, wxDefaultSize
, 0 );
870 item0
->Add( m_closeButton
, 0, wxALIGN_CENTRE
|wxALL
, 5 );
872 if (m_buttonFlags
& wxPREVIEW_PRINT
)
874 m_printButton
= new wxButton( this, wxID_PREVIEW_PRINT
, _("&Print..."), wxDefaultPosition
, wxDefaultSize
, 0 );
875 item0
->Add( m_printButton
, 0, wxALIGN_CENTRE
|wxALL
, 5 );
878 if (m_buttonFlags
& wxPREVIEW_FIRST
)
880 m_firstPageButton
= new wxButton( this, wxID_PREVIEW_FIRST
, _("|<<"), wxDefaultPosition
, wxDefaultSize
, wxBU_EXACTFIT
);
881 item0
->Add( m_firstPageButton
, 0, wxALIGN_CENTRE
|wxALL
, 5 );
884 if (m_buttonFlags
& wxPREVIEW_PREVIOUS
)
886 m_previousPageButton
= new wxButton( this, wxID_PREVIEW_PREVIOUS
, _("<<"), wxDefaultPosition
, wxDefaultSize
, wxBU_EXACTFIT
);
887 item0
->Add( m_previousPageButton
, 0, wxALIGN_CENTRE
|wxRIGHT
|wxTOP
|wxBOTTOM
, 5 );
890 if (m_buttonFlags
& wxPREVIEW_NEXT
)
892 m_nextPageButton
= new wxButton( this, wxID_PREVIEW_NEXT
, _(">>"), wxDefaultPosition
, wxDefaultSize
, wxBU_EXACTFIT
);
893 item0
->Add( m_nextPageButton
, 0, wxALIGN_CENTRE
|wxRIGHT
|wxTOP
|wxBOTTOM
, 5 );
896 if (m_buttonFlags
& wxPREVIEW_LAST
)
898 m_lastPageButton
= new wxButton( this, wxID_PREVIEW_LAST
, _(">>|"), wxDefaultPosition
, wxDefaultSize
, wxBU_EXACTFIT
);
899 item0
->Add( m_lastPageButton
, 0, wxALIGN_CENTRE
|wxRIGHT
|wxTOP
|wxBOTTOM
, 5 );
902 if (m_buttonFlags
& wxPREVIEW_GOTO
)
904 m_gotoPageButton
= new wxButton( this, wxID_PREVIEW_GOTO
, _("&Goto..."), wxDefaultPosition
, wxDefaultSize
, 0 );
905 item0
->Add( m_gotoPageButton
, 0, wxALIGN_CENTRE
|wxALL
, 5 );
908 if (m_buttonFlags
& wxPREVIEW_ZOOM
)
912 wxT("10%"), wxT("15%"), wxT("20%"), wxT("25%"), wxT("30%"), wxT("35%"), wxT("40%"), wxT("45%"), wxT("50%"), wxT("55%"),
913 wxT("60%"), wxT("65%"), wxT("70%"), wxT("75%"), wxT("80%"), wxT("85%"), wxT("90%"), wxT("95%"), wxT("100%"), wxT("110%"),
914 wxT("120%"), wxT("150%"), wxT("200%")
916 int n
= WXSIZEOF(choices
);
918 m_zoomControl
= new wxChoice( this, wxID_PREVIEW_ZOOM
, wxDefaultPosition
, wxSize(70,wxDefaultCoord
), n
, choices
, 0 );
919 item0
->Add( m_zoomControl
, 0, wxALIGN_CENTRE
|wxALL
, 5 );
920 SetZoomControl(m_printPreview
->GetZoom());
927 void wxPreviewControlBar::SetZoomControl(int zoom
)
931 int n
, count
= m_zoomControl
->GetCount();
933 for (n
=0; n
<count
; n
++)
935 if (m_zoomControl
->GetString(n
).BeforeFirst(wxT('%')).ToLong(&val
) &&
938 m_zoomControl
->SetSelection(n
);
943 m_zoomControl
->SetSelection(count
-1);
947 int wxPreviewControlBar::GetZoomControl()
949 if (m_zoomControl
&& (m_zoomControl
->GetStringSelection() != wxEmptyString
))
952 if (m_zoomControl
->GetStringSelection().BeforeFirst(wxT('%')).ToLong(&val
))
964 IMPLEMENT_CLASS(wxPreviewFrame
, wxFrame
)
966 BEGIN_EVENT_TABLE(wxPreviewFrame
, wxFrame
)
967 EVT_CLOSE(wxPreviewFrame::OnCloseWindow
)
970 wxPreviewFrame::wxPreviewFrame(wxPrintPreviewBase
*preview
, wxWindow
*parent
, const wxString
& title
,
971 const wxPoint
& pos
, const wxSize
& size
, long style
, const wxString
& name
):
972 wxFrame(parent
, wxID_ANY
, title
, pos
, size
, style
, name
)
974 m_printPreview
= preview
;
976 m_previewCanvas
= NULL
;
977 m_windowDisabler
= NULL
;
979 // Give the application icon
981 wxFrame
* topFrame
= wxDynamicCast(wxTheApp
->GetTopWindow(), wxFrame
);
983 SetIcon(topFrame
->GetIcon());
987 wxPreviewFrame::~wxPreviewFrame()
991 void wxPreviewFrame::OnCloseWindow(wxCloseEvent
& WXUNUSED(event
))
993 if (m_windowDisabler
)
994 delete m_windowDisabler
;
996 // Need to delete the printout and the print preview
997 wxPrintout
*printout
= m_printPreview
->GetPrintout();
1001 m_printPreview
->SetPrintout(NULL
);
1002 m_printPreview
->SetCanvas(NULL
);
1003 m_printPreview
->SetFrame(NULL
);
1005 delete m_printPreview
;
1010 void wxPreviewFrame::Initialize()
1018 m_printPreview
->SetCanvas(m_previewCanvas
);
1019 m_printPreview
->SetFrame(this);
1021 wxBoxSizer
*item0
= new wxBoxSizer( wxVERTICAL
);
1023 item0
->Add( m_controlBar
, 0, wxGROW
|wxALIGN_CENTER_VERTICAL
, 5 );
1024 item0
->Add( m_previewCanvas
, 1, wxGROW
|wxALIGN_CENTER_VERTICAL
, 5 );
1026 SetAutoLayout( true );
1029 m_windowDisabler
= new wxWindowDisabler(this);
1033 m_printPreview
->AdjustScrollbars(m_previewCanvas
);
1034 m_previewCanvas
->SetFocus();
1035 m_controlBar
->SetFocus();
1038 void wxPreviewFrame::CreateCanvas()
1040 m_previewCanvas
= new wxPreviewCanvas(m_printPreview
, this);
1043 void wxPreviewFrame::CreateControlBar()
1045 long buttons
= wxPREVIEW_DEFAULT
;
1046 if (m_printPreview
->GetPrintoutForPrinting())
1047 buttons
|= wxPREVIEW_PRINT
;
1049 m_controlBar
= new wxPreviewControlBar(m_printPreview
, buttons
, this, wxPoint(0,0), wxSize(400, 40));
1050 m_controlBar
->CreateButtons();
1057 IMPLEMENT_CLASS(wxPrintPreviewBase
, wxObject
)
1059 wxPrintPreviewBase::wxPrintPreviewBase(wxPrintout
*printout
,
1060 wxPrintout
*printoutForPrinting
,
1064 m_printDialogData
= (*data
);
1066 Init(printout
, printoutForPrinting
);
1069 wxPrintPreviewBase::wxPrintPreviewBase(wxPrintout
*printout
,
1070 wxPrintout
*printoutForPrinting
,
1071 wxPrintDialogData
*data
)
1074 m_printDialogData
= (*data
);
1076 Init(printout
, printoutForPrinting
);
1079 void wxPrintPreviewBase::Init(wxPrintout
*printout
,
1080 wxPrintout
*printoutForPrinting
)
1083 m_previewPrintout
= printout
;
1084 if (m_previewPrintout
)
1085 m_previewPrintout
->SetIsPreview(true);
1087 m_printPrintout
= printoutForPrinting
;
1089 m_previewCanvas
= NULL
;
1090 m_previewFrame
= NULL
;
1091 m_previewBitmap
= NULL
;
1098 m_printingPrepared
= false;
1103 wxPrintPreviewBase::~wxPrintPreviewBase()
1105 if (m_previewPrintout
)
1106 delete m_previewPrintout
;
1107 if (m_previewBitmap
)
1108 delete m_previewBitmap
;
1109 if (m_printPrintout
)
1110 delete m_printPrintout
;
1113 bool wxPrintPreviewBase::SetCurrentPage(int pageNum
)
1115 if (m_currentPage
== pageNum
)
1118 m_currentPage
= pageNum
;
1119 if (m_previewBitmap
)
1121 delete m_previewBitmap
;
1122 m_previewBitmap
= NULL
;
1125 if (m_previewCanvas
)
1127 AdjustScrollbars(m_previewCanvas
);
1129 if (!RenderPage(pageNum
))
1131 m_previewCanvas
->Refresh();
1132 m_previewCanvas
->SetFocus();
1137 int wxPrintPreviewBase::GetCurrentPage() const
1138 { return m_currentPage
; }
1139 void wxPrintPreviewBase::SetPrintout(wxPrintout
*printout
)
1140 { m_previewPrintout
= printout
; }
1141 wxPrintout
*wxPrintPreviewBase::GetPrintout() const
1142 { return m_previewPrintout
; }
1143 wxPrintout
*wxPrintPreviewBase::GetPrintoutForPrinting() const
1144 { return m_printPrintout
; }
1145 void wxPrintPreviewBase::SetFrame(wxFrame
*frame
)
1146 { m_previewFrame
= frame
; }
1147 void wxPrintPreviewBase::SetCanvas(wxPreviewCanvas
*canvas
)
1148 { m_previewCanvas
= canvas
; }
1149 wxFrame
*wxPrintPreviewBase::GetFrame() const
1150 { return m_previewFrame
; }
1151 wxPreviewCanvas
*wxPrintPreviewBase::GetCanvas() const
1152 { return m_previewCanvas
; }
1154 bool wxPrintPreviewBase::PaintPage(wxPreviewCanvas
*canvas
, wxDC
& dc
)
1156 DrawBlankPage(canvas
, dc
);
1158 if (!m_previewBitmap
)
1159 if (!RenderPage(m_currentPage
))
1162 if (!m_previewBitmap
)
1168 int canvasWidth
, canvasHeight
;
1169 canvas
->GetSize(&canvasWidth
, &canvasHeight
);
1171 double zoomScale
= ((float)m_currentZoom
/(float)100);
1172 double actualWidth
= (zoomScale
*m_pageWidth
*m_previewScale
);
1173 // float actualHeight = (float)(zoomScale*m_pageHeight*m_previewScale);
1175 int x
= (int) ((canvasWidth
- actualWidth
)/2.0);
1176 if (x
< m_leftMargin
)
1178 int y
= m_topMargin
;
1181 temp_dc
.SelectObject(*m_previewBitmap
);
1183 dc
.Blit(x
, y
, m_previewBitmap
->GetWidth(), m_previewBitmap
->GetHeight(), &temp_dc
, 0, 0);
1185 temp_dc
.SelectObject(wxNullBitmap
);
1190 // Adjusts the scrollbars for the current scale
1191 void wxPrintPreviewBase::AdjustScrollbars(wxPreviewCanvas
*canvas
)
1196 int canvasWidth
, canvasHeight
;
1197 canvas
->GetSize(&canvasWidth
, &canvasHeight
);
1199 double zoomScale
= ((float)m_currentZoom
/(float)100);
1200 double actualWidth
= (zoomScale
*m_pageWidth
*m_previewScale
);
1201 double actualHeight
= (zoomScale
*m_pageHeight
*m_previewScale
);
1203 // Set the scrollbars appropriately
1204 int totalWidth
= (int)(actualWidth
+ 2*m_leftMargin
);
1205 int totalHeight
= (int)(actualHeight
+ 2*m_topMargin
);
1206 int scrollUnitsX
= totalWidth
/10;
1207 int scrollUnitsY
= totalHeight
/10;
1208 wxSize virtualSize
= canvas
->GetVirtualSize();
1209 if (virtualSize
.GetWidth() != totalWidth
|| virtualSize
.GetHeight() != totalHeight
)
1210 canvas
->SetScrollbars(10, 10, scrollUnitsX
, scrollUnitsY
, 0, 0, true);
1213 bool wxPrintPreviewBase::RenderPage(int pageNum
)
1217 int canvasWidth
, canvasHeight
;
1219 if (!m_previewCanvas
)
1221 wxFAIL_MSG(_T("wxPrintPreviewBase::RenderPage: must use wxPrintPreviewBase::SetCanvas to let me know about the canvas!"));
1225 m_previewCanvas
->GetSize(&canvasWidth
, &canvasHeight
);
1227 double zoomScale
= (m_currentZoom
/100.0);
1228 int actualWidth
= (int)(zoomScale
*m_pageWidth
*m_previewScale
);
1229 int actualHeight
= (int)(zoomScale
*m_pageHeight
*m_previewScale
);
1231 if (!m_previewBitmap
)
1233 m_previewBitmap
= new wxBitmap((int)actualWidth
, (int)actualHeight
);
1234 if (!m_previewBitmap
|| !m_previewBitmap
->Ok())
1236 if (m_previewBitmap
) {
1237 delete m_previewBitmap
;
1238 m_previewBitmap
= NULL
;
1240 wxMessageBox(_("Sorry, not enough memory to create a preview."), _("Print Preview Failure"), wxOK
);
1245 wxMemoryDC memoryDC
;
1246 memoryDC
.SelectObject(*m_previewBitmap
);
1250 m_previewPrintout
->SetDC(&memoryDC
);
1251 m_previewPrintout
->SetPageSizePixels(m_pageWidth
, m_pageHeight
);
1253 // Need to delay OnPreparePrinting until here, so we have enough information.
1254 if (!m_printingPrepared
)
1256 m_previewPrintout
->OnPreparePrinting();
1258 m_previewPrintout
->GetPageInfo(&m_minPage
, &m_maxPage
, &selFrom
, &selTo
);
1259 m_printingPrepared
= true;
1262 m_previewPrintout
->OnBeginPrinting();
1264 if (!m_previewPrintout
->OnBeginDocument(m_printDialogData
.GetFromPage(), m_printDialogData
.GetToPage()))
1266 wxMessageBox(_("Could not start document preview."), _("Print Preview Failure"), wxOK
);
1268 memoryDC
.SelectObject(wxNullBitmap
);
1270 delete m_previewBitmap
;
1271 m_previewBitmap
= NULL
;
1275 m_previewPrintout
->OnPrintPage(pageNum
);
1276 m_previewPrintout
->OnEndDocument();
1277 m_previewPrintout
->OnEndPrinting();
1279 m_previewPrintout
->SetDC(NULL
);
1281 memoryDC
.SelectObject(wxNullBitmap
);
1286 status
= wxString::Format(_("Page %d of %d"), pageNum
, m_maxPage
);
1288 status
= wxString::Format(_("Page %d"), pageNum
);
1291 m_previewFrame
->SetStatusText(status
);
1298 bool wxPrintPreviewBase::DrawBlankPage(wxPreviewCanvas
*canvas
, wxDC
& dc
)
1300 int canvasWidth
, canvasHeight
;
1301 canvas
->GetSize(&canvasWidth
, &canvasHeight
);
1303 float zoomScale
= (float)((float)m_currentZoom
/(float)100);
1304 float actualWidth
= zoomScale
*m_pageWidth
*m_previewScale
;
1305 float actualHeight
= zoomScale
*m_pageHeight
*m_previewScale
;
1307 float x
= (float)((canvasWidth
- actualWidth
)/2.0);
1308 if (x
< m_leftMargin
)
1309 x
= (float)m_leftMargin
;
1310 float y
= (float)m_topMargin
;
1312 // Draw shadow, allowing for 1-pixel border AROUND the actual page
1313 int shadowOffset
= 4;
1314 dc
.SetPen(*wxBLACK_PEN
);
1315 dc
.SetBrush(*wxBLACK_BRUSH
);
1317 dc.DrawRectangle((int)(x-1 + shadowOffset), (int)(y-1 + shadowOffset), (int)(actualWidth+2), (int)(actualHeight+2));
1319 dc
.DrawRectangle((int)(x
+ shadowOffset
), (int)(y
+ actualHeight
+1), (int)(actualWidth
), shadowOffset
);
1320 dc
.DrawRectangle((int)(x
+ actualWidth
), (int)(y
+ shadowOffset
), shadowOffset
, (int)(actualHeight
));
1322 // Draw blank page allowing for 1-pixel border AROUND the actual page
1323 dc
.SetPen(*wxBLACK_PEN
);
1324 dc
.SetBrush(*wxWHITE_BRUSH
);
1327 wxRegion update_region = canvas->GetUpdateRegion();
1328 wxRect r = update_region.GetBox();
1330 printf( "x: %d y: %d w: %d h: %d.\n", (int)r.x, (int)r.y, (int)r.width, (int)r.height );
1333 dc
.DrawRectangle((int)(x
-2), (int)(y
-1), (int)(actualWidth
+3), (int)(actualHeight
+2));
1338 void wxPrintPreviewBase::SetZoom(int percent
)
1340 if (m_currentZoom
== percent
)
1343 m_currentZoom
= percent
;
1344 if (m_previewBitmap
)
1346 delete m_previewBitmap
;
1347 m_previewBitmap
= NULL
;
1350 if (m_previewCanvas
)
1352 AdjustScrollbars(m_previewCanvas
);
1353 RenderPage(m_currentPage
);
1354 ((wxScrolledWindow
*) m_previewCanvas
)->Scroll(0, 0);
1355 m_previewCanvas
->ClearBackground();
1356 m_previewCanvas
->Refresh();
1357 m_previewCanvas
->SetFocus();
1361 wxPrintDialogData
& wxPrintPreviewBase::GetPrintDialogData()
1363 return m_printDialogData
;
1366 int wxPrintPreviewBase::GetZoom() const
1367 { return m_currentZoom
; }
1368 int wxPrintPreviewBase::GetMaxPage() const
1369 { return m_maxPage
; }
1370 int wxPrintPreviewBase::GetMinPage() const
1371 { return m_minPage
; }
1372 bool wxPrintPreviewBase::Ok() const
1374 void wxPrintPreviewBase::SetOk(bool ok
)
1376 //----------------------------------------------------------------------------
1378 //----------------------------------------------------------------------------
1380 IMPLEMENT_CLASS(wxPrintPreview
, wxPrintPreviewBase
)
1382 wxPrintPreview::wxPrintPreview(wxPrintout
*printout
,
1383 wxPrintout
*printoutForPrinting
,
1384 wxPrintDialogData
*data
) :
1385 wxPrintPreviewBase( printout
, printoutForPrinting
, data
)
1387 m_pimpl
= wxPrintFactory::GetFactory()->
1388 CreatePrintPreview( printout
, printoutForPrinting
, data
);
1391 wxPrintPreview::wxPrintPreview(wxPrintout
*printout
,
1392 wxPrintout
*printoutForPrinting
,
1393 wxPrintData
*data
) :
1394 wxPrintPreviewBase( printout
, printoutForPrinting
, data
)
1396 m_pimpl
= wxPrintFactory::GetFactory()->
1397 CreatePrintPreview( printout
, printoutForPrinting
, data
);
1400 wxPrintPreview::~wxPrintPreview()
1404 // don't delete twice
1405 m_printPrintout
= NULL
;
1406 m_previewPrintout
= NULL
;
1407 m_previewBitmap
= NULL
;
1410 bool wxPrintPreview::SetCurrentPage(int pageNum
)
1412 return m_pimpl
->SetCurrentPage( pageNum
);
1415 int wxPrintPreview::GetCurrentPage() const
1417 return m_pimpl
->GetCurrentPage();
1420 void wxPrintPreview::SetPrintout(wxPrintout
*printout
)
1422 m_pimpl
->SetPrintout( printout
);
1425 wxPrintout
*wxPrintPreview::GetPrintout() const
1427 return m_pimpl
->GetPrintout();
1430 wxPrintout
*wxPrintPreview::GetPrintoutForPrinting() const
1432 return m_pimpl
->GetPrintoutForPrinting();
1435 void wxPrintPreview::SetFrame(wxFrame
*frame
)
1437 m_pimpl
->SetFrame( frame
);
1440 void wxPrintPreview::SetCanvas(wxPreviewCanvas
*canvas
)
1442 m_pimpl
->SetCanvas( canvas
);
1445 wxFrame
*wxPrintPreview::GetFrame() const
1447 return m_pimpl
->GetFrame();
1450 wxPreviewCanvas
*wxPrintPreview::GetCanvas() const
1452 return m_pimpl
->GetCanvas();
1455 bool wxPrintPreview::PaintPage(wxPreviewCanvas
*canvas
, wxDC
& dc
)
1457 return m_pimpl
->PaintPage( canvas
, dc
);
1460 bool wxPrintPreview::DrawBlankPage(wxPreviewCanvas
*canvas
, wxDC
& dc
)
1462 return m_pimpl
->DrawBlankPage( canvas
, dc
);
1465 void wxPrintPreview::AdjustScrollbars(wxPreviewCanvas
*canvas
)
1467 m_pimpl
->AdjustScrollbars( canvas
);
1470 bool wxPrintPreview::RenderPage(int pageNum
)
1472 return m_pimpl
->RenderPage( pageNum
);
1475 void wxPrintPreview::SetZoom(int percent
)
1477 m_pimpl
->SetZoom( percent
);
1480 int wxPrintPreview::GetZoom() const
1482 return m_pimpl
->GetZoom();
1485 wxPrintDialogData
& wxPrintPreview::GetPrintDialogData()
1487 return m_pimpl
->GetPrintDialogData();
1490 int wxPrintPreview::GetMaxPage() const
1492 return m_pimpl
->GetMaxPage();
1495 int wxPrintPreview::GetMinPage() const
1497 return m_pimpl
->GetMinPage();
1500 bool wxPrintPreview::Ok() const
1502 return m_pimpl
->Ok();
1505 void wxPrintPreview::SetOk(bool ok
)
1507 m_pimpl
->SetOk( ok
);
1510 bool wxPrintPreview::Print(bool interactive
)
1512 return m_pimpl
->Print( interactive
);
1515 void wxPrintPreview::DetermineScaling()
1517 m_pimpl
->DetermineScaling();
1520 #endif // wxUSE_PRINTING_ARCHITECTURE