1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: Printing framework base class implementation
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
13 #pragma implementation "prntbase.h"
14 #pragma implementation "printdlg.h"
17 // For compilers that support precompilation, includes "wx.h".
18 #include "wx/wxprec.h"
26 #if wxUSE_PRINTING_ARCHITECTURE
32 #include "wx/msgdlg.h"
33 #include "wx/layout.h"
34 #include "wx/choice.h"
35 #include "wx/button.h"
36 #include "wx/settings.h"
37 #include "wx/dcmemory.h"
38 #include "wx/stattext.h"
40 #include "wx/textdlg.h"
44 #include "wx/prntbase.h"
45 #include "wx/dcprint.h"
46 #include "wx/printdlg.h"
48 #include "wx/module.h"
53 #if defined(__WXMSW__) && !defined(__WXUNIVERSAL__)
54 #include "wx/msw/printdlg.h"
55 #elif defined(__WXMAC__)
56 #include "wx/mac/printdlg.h"
58 #include "wx/generic/prntdlgg.h"
62 #include "wx/msw/wrapcdlg.h"
68 //----------------------------------------------------------------------------
70 //----------------------------------------------------------------------------
72 wxPrintFactory
*wxPrintFactory::m_factory
= NULL
;
74 void wxPrintFactory::SetPrintFactory( wxPrintFactory
*factory
)
76 if (wxPrintFactory::m_factory
)
77 delete wxPrintFactory::m_factory
;
79 wxPrintFactory::m_factory
= factory
;
82 wxPrintFactory
*wxPrintFactory::GetFactory()
84 if (!wxPrintFactory::m_factory
)
85 wxPrintFactory::m_factory
= new wxNativePrintFactory
;
87 return wxPrintFactory::m_factory
;
90 //----------------------------------------------------------------------------
91 // wxNativePrintFactory
92 //----------------------------------------------------------------------------
94 wxPrinterBase
*wxNativePrintFactory::CreatePrinter( wxPrintDialogData
*data
)
96 #if defined(__WXMSW__) && !defined(__WXUNIVERSAL__)
97 return new wxWindowsPrinter( data
);
98 #elif defined(__WXMAC__)
99 return new wxMacPrinter( data
);
100 #elif defined(__WXPM__)
101 return new wxOS2Printer( data
);
103 return new wxPostScriptPrinter( data
);
107 wxPrintPreviewBase
*wxNativePrintFactory::CreatePrintPreview( wxPrintout
*preview
,
108 wxPrintout
*printout
, wxPrintDialogData
*data
)
110 #if defined(__WXMSW__) && !defined(__WXUNIVERSAL__)
111 return new wxWindowsPrintPreview( preview
, printout
, data
);
112 #elif defined(__WXMAC__)
113 return new wxMacPrintPreview( preview
, printout
, data
);
114 #elif defined(__WXPM__)
115 return new wxOS2PrintPreview( preview
, printout
, data
);
117 return new wxPostScriptPrintPreview( preview
, printout
, data
);
121 wxPrintPreviewBase
*wxNativePrintFactory::CreatePrintPreview( wxPrintout
*preview
,
122 wxPrintout
*printout
, wxPrintData
*data
)
124 #if defined(__WXMSW__) && !defined(__WXUNIVERSAL__)
125 return new wxWindowsPrintPreview( preview
, printout
, data
);
126 #elif defined(__WXMAC__)
127 return new wxMacPrintPreview( preview
, printout
, data
);
128 #elif defined(__WXPM__)
129 return new wxOS2PrintPreview( preview
, printout
, data
);
131 return new wxPostScriptPrintPreview( preview
, printout
, data
);
135 wxPrintDialogBase
*wxNativePrintFactory::CreatePrintDialog( wxWindow
*parent
,
136 wxPrintDialogData
*data
)
138 #if defined(__WXMSW__) && !defined(__WXUNIVERSAL__)
139 return new wxWindowsPrintDialog( parent
, data
);
140 #elif defined(__WXMAC__)
141 return new wxMacPrintDialog( parent
, data
);
143 return new wxGenericPrintDialog( parent
, data
);
147 wxPrintDialogBase
*wxNativePrintFactory::CreatePrintDialog( wxWindow
*parent
,
150 #if defined(__WXMSW__) && !defined(__WXUNIVERSAL__)
151 return new wxWindowsPrintDialog( parent
, data
);
152 #elif defined(__WXMAC__)
153 return new wxMacPrintDialog( parent
, data
);
155 return new wxGenericPrintDialog( parent
, data
);
159 wxPageSetupDialogBase
*wxNativePrintFactory::CreatePageSetupDialog( wxWindow
*parent
,
160 wxPageSetupDialogData
*data
)
162 #if defined(__WXMSW__) && !defined(__WXUNIVERSAL__)
163 return new wxWindowsPageSetupDialog( parent
, data
);
164 #elif defined(__WXMAC__)
165 return new wxMacPageSetupDialog( parent
, data
);
167 return new wxGenericPageSetupDialog( parent
, data
);
171 bool wxNativePrintFactory::HasPrintSetupDialog()
173 #if defined(__WXMSW__) && !defined(__WXUNIVERSAL__)
175 #elif defined(__WXMAC__)
178 // Only here do we need to provide the print setup
179 // dialog ourselves, the other platforms either have
180 // none, don't make it accessible or let you configure
181 // the printer from the wxPrintDialog anyway.
187 wxDialog
*wxNativePrintFactory::CreatePrintSetupDialog( wxWindow
*parent
,
190 #if defined(__WXMSW__) && !defined(__WXUNIVERSAL__)
194 #elif defined(__WXMAC__)
199 // Only here do we need to provide the print setup
200 // dialog ourselves, the other platforms either have
201 // none, don't make it accessible or let you configure
202 // the printer from the wxPrintDialog anyway.
203 return new wxGenericPrintSetupDialog( parent
, data
);
207 bool wxNativePrintFactory::HasOwnPrintToFile()
209 // Only relevant for PostScript and here the
210 // setup dialog provides no "print to file"
211 // option. In the GNOME setup dialog, the
212 // setup dialog has its own print to file.
216 bool wxNativePrintFactory::HasPrinterLine()
218 // Only relevant for PostScript for now
222 wxString
wxNativePrintFactory::CreatePrinterLine()
224 // Only relevant for PostScript for now
226 // We should query "lpstat -d" here
227 return _("Generic PostScript");
230 bool wxNativePrintFactory::HasStatusLine()
232 // Only relevant for PostScript for now
236 wxString
wxNativePrintFactory::CreateStatusLine()
238 // Only relevant for PostScript for now
240 // We should query "lpstat -r" or "lpstat -p" here
244 wxPrintNativeDataBase
*wxNativePrintFactory::CreatePrintNativeData()
246 #if defined(__WXMSW__) && !defined(__WXUNIVERSAL__)
247 return new wxWindowsPrintNativeData
;
248 #elif defined(__WXMAC__)
249 return new wxMacPrintNativeData
;
251 return new wxPostScriptPrintNativeData
;
255 //----------------------------------------------------------------------------
256 // wxPrintNativeDataBase
257 //----------------------------------------------------------------------------
259 IMPLEMENT_ABSTRACT_CLASS(wxPrintNativeDataBase
, wxObject
)
261 wxPrintNativeDataBase::wxPrintNativeDataBase()
266 //----------------------------------------------------------------------------
267 // wxPrintFactoryModule
268 //----------------------------------------------------------------------------
270 class wxPrintFactoryModule
: public wxModule
273 wxPrintFactoryModule() {}
274 bool OnInit() { return true; }
275 void OnExit() { wxPrintFactory::SetPrintFactory( NULL
); }
278 DECLARE_DYNAMIC_CLASS(wxPrintFactoryModule
)
281 IMPLEMENT_DYNAMIC_CLASS(wxPrintFactoryModule
, wxModule
)
283 //----------------------------------------------------------------------------
285 //----------------------------------------------------------------------------
287 IMPLEMENT_CLASS(wxPrinterBase
, wxObject
)
289 wxPrinterBase::wxPrinterBase(wxPrintDialogData
*data
)
291 m_currentPrintout
= (wxPrintout
*) NULL
;
292 sm_abortWindow
= (wxWindow
*) NULL
;
295 m_printDialogData
= (*data
);
296 sm_lastError
= wxPRINTER_NO_ERROR
;
299 wxWindow
*wxPrinterBase::sm_abortWindow
= (wxWindow
*) NULL
;
300 bool wxPrinterBase::sm_abortIt
= false;
301 wxPrinterError
wxPrinterBase::sm_lastError
= wxPRINTER_NO_ERROR
;
303 wxPrinterBase::~wxPrinterBase()
307 wxWindow
*wxPrinterBase::CreateAbortWindow(wxWindow
*parent
, wxPrintout
* printout
)
309 wxPrintAbortDialog
*dialog
= new wxPrintAbortDialog(parent
, _("Printing ") , wxDefaultPosition
, wxDefaultSize
, wxDEFAULT_DIALOG_STYLE
);
311 wxBoxSizer
*button_sizer
= new wxBoxSizer( wxVERTICAL
);
312 button_sizer
->Add( new wxStaticText(dialog
, wxID_ANY
, _("Please wait while printing\n") + printout
->GetTitle() ), 0, wxALL
, 10 );
313 button_sizer
->Add( new wxButton( dialog
, wxID_CANCEL
, wxT("Cancel") ), 0, wxALL
| wxALIGN_CENTER
, 10 );
315 dialog
->SetAutoLayout( true );
316 dialog
->SetSizer( button_sizer
);
318 button_sizer
->Fit(dialog
);
319 button_sizer
->SetSizeHints (dialog
) ;
324 void wxPrinterBase::ReportError(wxWindow
*parent
, wxPrintout
*WXUNUSED(printout
), const wxString
& message
)
326 wxMessageBox(message
, _("Printing Error"), wxOK
, parent
);
329 wxPrintDialogData
& wxPrinterBase::GetPrintDialogData() const
331 return (wxPrintDialogData
&) m_printDialogData
;
334 //----------------------------------------------------------------------------
336 //----------------------------------------------------------------------------
338 IMPLEMENT_CLASS(wxPrinter
, wxPrinterBase
)
340 wxPrinter::wxPrinter(wxPrintDialogData
*data
)
342 m_pimpl
= wxPrintFactory::GetFactory()->CreatePrinter( data
);
345 wxPrinter::~wxPrinter()
350 wxWindow
*wxPrinter::CreateAbortWindow(wxWindow
*parent
, wxPrintout
*printout
)
352 return m_pimpl
->CreateAbortWindow( parent
, printout
);
355 void wxPrinter::ReportError(wxWindow
*parent
, wxPrintout
*printout
, const wxString
& message
)
357 m_pimpl
->ReportError( parent
, printout
, message
);
360 bool wxPrinter::Setup(wxWindow
*parent
)
362 return m_pimpl
->Setup( parent
);
365 bool wxPrinter::Print(wxWindow
*parent
, wxPrintout
*printout
, bool prompt
)
367 return m_pimpl
->Print( parent
, printout
, prompt
);
370 wxDC
* wxPrinter::PrintDialog(wxWindow
*parent
)
372 return m_pimpl
->PrintDialog( parent
);
375 wxPrintDialogData
& wxPrinter::GetPrintDialogData() const
377 return m_pimpl
->GetPrintDialogData();
380 // ---------------------------------------------------------------------------
381 // wxPrintDialogBase: the dialog for printing.
382 // ---------------------------------------------------------------------------
384 IMPLEMENT_ABSTRACT_CLASS(wxPrintDialogBase
, wxDialog
)
386 wxPrintDialogBase::wxPrintDialogBase(wxWindow
*parent
,
388 const wxString
&title
,
392 : wxDialog( parent
, id
, title
.empty() ? wxString(_("Print")) : title
,
397 // ---------------------------------------------------------------------------
398 // wxPrintDialog: the dialog for printing
399 // ---------------------------------------------------------------------------
401 IMPLEMENT_CLASS(wxPrintDialog
, wxObject
)
403 wxPrintDialog::wxPrintDialog(wxWindow
*parent
, wxPrintDialogData
* data
)
405 m_pimpl
= wxPrintFactory::GetFactory()->CreatePrintDialog( parent
, data
);
408 wxPrintDialog::wxPrintDialog(wxWindow
*parent
, wxPrintData
* data
)
410 m_pimpl
= wxPrintFactory::GetFactory()->CreatePrintDialog( parent
, data
);
413 wxPrintDialog::~wxPrintDialog()
418 int wxPrintDialog::ShowModal()
420 return m_pimpl
->ShowModal();
423 wxPrintDialogData
& wxPrintDialog::GetPrintDialogData()
425 return m_pimpl
->GetPrintDialogData();
428 wxPrintData
& wxPrintDialog::GetPrintData()
430 return m_pimpl
->GetPrintData();
433 wxDC
*wxPrintDialog::GetPrintDC()
435 return m_pimpl
->GetPrintDC();
438 // ---------------------------------------------------------------------------
439 // wxPageSetupDialogBase: the page setup dialog
440 // ---------------------------------------------------------------------------
442 IMPLEMENT_ABSTRACT_CLASS(wxPageSetupDialogBase
, wxDialog
)
444 wxPageSetupDialogBase::wxPageSetupDialogBase(wxWindow
*parent
,
446 const wxString
&title
,
450 : wxDialog( parent
, id
, title
.empty() ? wxString(_("Page setup")) : title
,
455 // ---------------------------------------------------------------------------
456 // wxPageSetupDialog: the page setup dialog
457 // ---------------------------------------------------------------------------
459 IMPLEMENT_CLASS(wxPageSetupDialog
, wxObject
)
461 wxPageSetupDialog::wxPageSetupDialog(wxWindow
*parent
, wxPageSetupDialogData
*data
)
463 m_pimpl
= wxPrintFactory::GetFactory()->CreatePageSetupDialog( parent
, data
);
466 wxPageSetupDialog::~wxPageSetupDialog()
471 int wxPageSetupDialog::ShowModal()
473 return m_pimpl
->ShowModal();
476 wxPageSetupDialogData
& wxPageSetupDialog::GetPageSetupDialogData()
478 return m_pimpl
->GetPageSetupDialogData();
482 wxPageSetupDialogData
& wxPageSetupDialog::GetPageSetupData()
484 return m_pimpl
->GetPageSetupDialogData();
487 //----------------------------------------------------------------------------
488 // wxPrintAbortDialog
489 //----------------------------------------------------------------------------
491 BEGIN_EVENT_TABLE(wxPrintAbortDialog
, wxDialog
)
492 EVT_BUTTON(wxID_CANCEL
, wxPrintAbortDialog::OnCancel
)
495 void wxPrintAbortDialog::OnCancel(wxCommandEvent
& WXUNUSED(event
))
497 wxPrinterBase::sm_abortIt
= true;
498 wxPrinterBase::sm_abortWindow
->Show(false);
499 wxPrinterBase::sm_abortWindow
->Close(true);
500 wxPrinterBase::sm_abortWindow
= (wxWindow
*) NULL
;
503 //----------------------------------------------------------------------------
505 //----------------------------------------------------------------------------
507 IMPLEMENT_ABSTRACT_CLASS(wxPrintout
, wxObject
)
509 wxPrintout::wxPrintout(const wxString
& title
)
511 m_printoutTitle
= title
;
512 m_printoutDC
= (wxDC
*) NULL
;
515 m_pageWidthPixels
= 0;
516 m_pageHeightPixels
= 0;
524 wxPrintout::~wxPrintout()
528 bool wxPrintout::OnBeginDocument(int WXUNUSED(startPage
), int WXUNUSED(endPage
))
530 return GetDC()->StartDoc(_("Printing ") + m_printoutTitle
);
533 void wxPrintout::OnEndDocument()
538 void wxPrintout::OnBeginPrinting()
542 void wxPrintout::OnEndPrinting()
546 bool wxPrintout::HasPage(int page
)
551 void wxPrintout::GetPageInfo(int *minPage
, int *maxPage
, int *fromPage
, int *toPage
)
559 //----------------------------------------------------------------------------
561 //----------------------------------------------------------------------------
563 IMPLEMENT_CLASS(wxPreviewCanvas
, wxWindow
)
565 BEGIN_EVENT_TABLE(wxPreviewCanvas
, wxScrolledWindow
)
566 EVT_PAINT(wxPreviewCanvas::OnPaint
)
567 EVT_CHAR(wxPreviewCanvas::OnChar
)
568 EVT_SYS_COLOUR_CHANGED(wxPreviewCanvas::OnSysColourChanged
)
571 // VZ: the current code doesn't refresh properly without
572 // wxFULL_REPAINT_ON_RESIZE, this must be fixed as otherwise we have
573 // really horrible flicker when resizing the preview frame, but without
574 // this style it simply doesn't work correctly at all...
575 wxPreviewCanvas::wxPreviewCanvas(wxPrintPreviewBase
*preview
, wxWindow
*parent
,
576 const wxPoint
& pos
, const wxSize
& size
, long style
, const wxString
& name
):
577 wxScrolledWindow(parent
, wxID_ANY
, pos
, size
, style
| wxFULL_REPAINT_ON_RESIZE
, name
)
579 m_printPreview
= preview
;
581 // The app workspace colour is always white, but we should have
582 // a contrast with the page.
583 wxSystemColour colourIndex
= wxSYS_COLOUR_3DDKSHADOW
;
585 wxSystemColour colourIndex
= wxSYS_COLOUR_APPWORKSPACE
;
587 SetBackgroundColour(wxSystemSettings::GetColour(colourIndex
));
589 SetScrollbars(10, 10, 100, 100);
592 wxPreviewCanvas::~wxPreviewCanvas()
596 void wxPreviewCanvas::OnPaint(wxPaintEvent
& WXUNUSED(event
))
603 if (!GetUpdateRegion().IsEmpty())
604 dc.SetClippingRegion( GetUpdateRegion() );
610 m_printPreview
->PaintPage(this, dc
);
614 // Responds to colour changes, and passes event on to children.
615 void wxPreviewCanvas::OnSysColourChanged(wxSysColourChangedEvent
& event
)
618 // The app workspace colour is always white, but we should have
619 // a contrast with the page.
620 wxSystemColour colourIndex
= wxSYS_COLOUR_3DDKSHADOW
;
622 wxSystemColour colourIndex
= wxSYS_COLOUR_APPWORKSPACE
;
624 SetBackgroundColour(wxSystemSettings::GetColour(colourIndex
));
627 // Propagate the event to the non-top-level children
628 wxWindow::OnSysColourChanged(event
);
631 void wxPreviewCanvas::OnChar(wxKeyEvent
&event
)
633 wxPreviewControlBar
* controlBar
= ((wxPreviewFrame
*) GetParent())->GetControlBar();
634 if (event
.GetKeyCode() == WXK_ESCAPE
)
636 ((wxPreviewFrame
*) GetParent())->Close(true);
639 else if (event
.GetKeyCode() == WXK_TAB
)
641 controlBar
->OnGoto();
644 else if (event
.GetKeyCode() == WXK_RETURN
)
646 controlBar
->OnPrint();
650 if (!event
.ControlDown())
656 switch(event
.GetKeyCode())
659 controlBar
->OnNext(); break;
661 controlBar
->OnPrevious(); break;
663 controlBar
->OnFirst(); break;
665 controlBar
->OnLast(); break;
671 //----------------------------------------------------------------------------
672 // wxPreviewControlBar
673 //----------------------------------------------------------------------------
675 IMPLEMENT_CLASS(wxPreviewControlBar
, wxWindow
)
677 BEGIN_EVENT_TABLE(wxPreviewControlBar
, wxPanel
)
678 EVT_BUTTON(wxID_PREVIEW_CLOSE
, wxPreviewControlBar::OnWindowClose
)
679 EVT_BUTTON(wxID_PREVIEW_PRINT
, wxPreviewControlBar::OnPrintButton
)
680 EVT_BUTTON(wxID_PREVIEW_PREVIOUS
, wxPreviewControlBar::OnPreviousButton
)
681 EVT_BUTTON(wxID_PREVIEW_NEXT
, wxPreviewControlBar::OnNextButton
)
682 EVT_BUTTON(wxID_PREVIEW_FIRST
, wxPreviewControlBar::OnFirstButton
)
683 EVT_BUTTON(wxID_PREVIEW_LAST
, wxPreviewControlBar::OnLastButton
)
684 EVT_BUTTON(wxID_PREVIEW_GOTO
, wxPreviewControlBar::OnGotoButton
)
685 EVT_CHOICE(wxID_PREVIEW_ZOOM
, wxPreviewControlBar::OnZoom
)
686 EVT_PAINT(wxPreviewControlBar::OnPaint
)
689 wxPreviewControlBar::wxPreviewControlBar(wxPrintPreviewBase
*preview
, long buttons
,
690 wxWindow
*parent
, const wxPoint
& pos
, const wxSize
& size
,
691 long style
, const wxString
& name
):
692 wxPanel(parent
, wxID_ANY
, pos
, size
, style
, name
)
694 m_printPreview
= preview
;
695 m_closeButton
= (wxButton
*) NULL
;
696 m_nextPageButton
= (wxButton
*) NULL
;
697 m_previousPageButton
= (wxButton
*) NULL
;
698 m_printButton
= (wxButton
*) NULL
;
699 m_zoomControl
= (wxChoice
*) NULL
;
700 m_buttonFlags
= buttons
;
703 wxPreviewControlBar::~wxPreviewControlBar()
707 void wxPreviewControlBar::OnPaint(wxPaintEvent
& WXUNUSED(event
))
713 dc
.SetPen(*wxBLACK_PEN
);
714 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
715 dc
.DrawLine( 0, h
-1, w
, h
-1 );
718 void wxPreviewControlBar::OnWindowClose(wxCommandEvent
& WXUNUSED(event
))
720 wxPreviewFrame
*frame
= (wxPreviewFrame
*)GetParent();
724 void wxPreviewControlBar::OnPrint(void)
726 wxPrintPreviewBase
*preview
= GetPrintPreview();
727 preview
->Print(true);
730 void wxPreviewControlBar::OnNext(void)
732 wxPrintPreviewBase
*preview
= GetPrintPreview();
735 int currentPage
= preview
->GetCurrentPage();
736 if ((preview
->GetMaxPage() > 0) &&
737 (currentPage
< preview
->GetMaxPage()) &&
738 preview
->GetPrintout()->HasPage(currentPage
+ 1))
740 preview
->SetCurrentPage(currentPage
+ 1);
745 void wxPreviewControlBar::OnPrevious(void)
747 wxPrintPreviewBase
*preview
= GetPrintPreview();
750 int currentPage
= preview
->GetCurrentPage();
751 if ((preview
->GetMinPage() > 0) &&
752 (currentPage
> preview
->GetMinPage()) &&
753 preview
->GetPrintout()->HasPage(currentPage
- 1))
755 preview
->SetCurrentPage(currentPage
- 1);
760 void wxPreviewControlBar::OnFirst(void)
762 wxPrintPreviewBase
*preview
= GetPrintPreview();
765 int currentPage
= preview
->GetMinPage();
766 if (preview
->GetPrintout()->HasPage(currentPage
))
768 preview
->SetCurrentPage(currentPage
);
773 void wxPreviewControlBar::OnLast(void)
775 wxPrintPreviewBase
*preview
= GetPrintPreview();
778 int currentPage
= preview
->GetMaxPage();
779 if (preview
->GetPrintout()->HasPage(currentPage
))
781 preview
->SetCurrentPage(currentPage
);
786 void wxPreviewControlBar::OnGoto(void)
788 wxPrintPreviewBase
*preview
= GetPrintPreview();
793 if (preview
->GetMinPage() > 0)
798 strPrompt
.Printf( _("Enter a page number between %d and %d:"),
799 preview
->GetMinPage(), preview
->GetMaxPage());
800 strPage
.Printf( wxT("%d"), preview
->GetCurrentPage() );
803 wxGetTextFromUser( strPrompt
, _("Goto Page"), strPage
, GetParent());
805 if ( strPage
.ToLong( ¤tPage
) )
806 if (preview
->GetPrintout()->HasPage(currentPage
))
808 preview
->SetCurrentPage(currentPage
);
814 void wxPreviewControlBar::OnZoom(wxCommandEvent
& WXUNUSED(event
))
816 int zoom
= GetZoomControl();
817 if (GetPrintPreview())
818 GetPrintPreview()->SetZoom(zoom
);
821 void wxPreviewControlBar::CreateButtons()
823 SetSize(0, 0, 400, 40);
825 wxBoxSizer
*item0
= new wxBoxSizer( wxHORIZONTAL
);
827 m_closeButton
= new wxButton( this, wxID_PREVIEW_CLOSE
, _("&Close"), wxDefaultPosition
, wxDefaultSize
, 0 );
828 item0
->Add( m_closeButton
, 0, wxALIGN_CENTRE
|wxALL
, 5 );
830 if (m_buttonFlags
& wxPREVIEW_PRINT
)
832 m_printButton
= new wxButton( this, wxID_PREVIEW_PRINT
, _("&Print..."), wxDefaultPosition
, wxDefaultSize
, 0 );
833 item0
->Add( m_printButton
, 0, wxALIGN_CENTRE
|wxALL
, 5 );
836 if (m_buttonFlags
& wxPREVIEW_FIRST
)
838 m_firstPageButton
= new wxButton( this, wxID_PREVIEW_FIRST
, _("|<<"), wxDefaultPosition
, wxDefaultSize
, wxBU_EXACTFIT
);
839 item0
->Add( m_firstPageButton
, 0, wxALIGN_CENTRE
|wxALL
, 5 );
842 if (m_buttonFlags
& wxPREVIEW_PREVIOUS
)
844 m_previousPageButton
= new wxButton( this, wxID_PREVIEW_PREVIOUS
, _("<<"), wxDefaultPosition
, wxDefaultSize
, wxBU_EXACTFIT
);
845 item0
->Add( m_previousPageButton
, 0, wxALIGN_CENTRE
|wxRIGHT
|wxTOP
|wxBOTTOM
, 5 );
848 if (m_buttonFlags
& wxPREVIEW_NEXT
)
850 m_nextPageButton
= new wxButton( this, wxID_PREVIEW_NEXT
, _(">>"), wxDefaultPosition
, wxDefaultSize
, wxBU_EXACTFIT
);
851 item0
->Add( m_nextPageButton
, 0, wxALIGN_CENTRE
|wxRIGHT
|wxTOP
|wxBOTTOM
, 5 );
854 if (m_buttonFlags
& wxPREVIEW_LAST
)
856 m_lastPageButton
= new wxButton( this, wxID_PREVIEW_LAST
, _(">>|"), wxDefaultPosition
, wxDefaultSize
, wxBU_EXACTFIT
);
857 item0
->Add( m_lastPageButton
, 0, wxALIGN_CENTRE
|wxRIGHT
|wxTOP
|wxBOTTOM
, 5 );
860 if (m_buttonFlags
& wxPREVIEW_GOTO
)
862 m_gotoPageButton
= new wxButton( this, wxID_PREVIEW_GOTO
, _("&Goto..."), wxDefaultPosition
, wxDefaultSize
, 0 );
863 item0
->Add( m_gotoPageButton
, 0, wxALIGN_CENTRE
|wxALL
, 5 );
866 if (m_buttonFlags
& wxPREVIEW_ZOOM
)
870 wxT("10%"), wxT("15%"), wxT("20%"), wxT("25%"), wxT("30%"), wxT("35%"), wxT("40%"), wxT("45%"), wxT("50%"), wxT("55%"),
871 wxT("60%"), wxT("65%"), wxT("70%"), wxT("75%"), wxT("80%"), wxT("85%"), wxT("90%"), wxT("95%"), wxT("100%"), wxT("110%"),
872 wxT("120%"), wxT("150%"), wxT("200%")
874 int n
= WXSIZEOF(choices
);
876 m_zoomControl
= new wxChoice( this, wxID_PREVIEW_ZOOM
, wxDefaultPosition
, wxSize(70,wxDefaultCoord
), n
, choices
, 0 );
877 item0
->Add( m_zoomControl
, 0, wxALIGN_CENTRE
|wxALL
, 5 );
878 SetZoomControl(m_printPreview
->GetZoom());
885 void wxPreviewControlBar::SetZoomControl(int zoom
)
889 int n
, count
= m_zoomControl
->GetCount();
891 for (n
=0; n
<count
; n
++)
893 if (m_zoomControl
->GetString(n
).BeforeFirst(wxT('%')).ToLong(&val
) &&
896 m_zoomControl
->SetSelection(n
);
901 m_zoomControl
->SetSelection(count
-1);
905 int wxPreviewControlBar::GetZoomControl()
907 if (m_zoomControl
&& (m_zoomControl
->GetStringSelection() != wxEmptyString
))
910 if (m_zoomControl
->GetStringSelection().BeforeFirst(wxT('%')).ToLong(&val
))
922 IMPLEMENT_CLASS(wxPreviewFrame
, wxFrame
)
924 BEGIN_EVENT_TABLE(wxPreviewFrame
, wxFrame
)
925 EVT_CLOSE(wxPreviewFrame::OnCloseWindow
)
928 wxPreviewFrame::wxPreviewFrame(wxPrintPreviewBase
*preview
, wxWindow
*parent
, const wxString
& title
,
929 const wxPoint
& pos
, const wxSize
& size
, long style
, const wxString
& name
):
930 wxFrame(parent
, wxID_ANY
, title
, pos
, size
, style
, name
)
932 m_printPreview
= preview
;
934 m_previewCanvas
= NULL
;
935 m_windowDisabler
= NULL
;
937 // Give the application icon
939 wxFrame
* topFrame
= wxDynamicCast(wxTheApp
->GetTopWindow(), wxFrame
);
941 SetIcon(topFrame
->GetIcon());
945 wxPreviewFrame::~wxPreviewFrame()
949 void wxPreviewFrame::OnCloseWindow(wxCloseEvent
& WXUNUSED(event
))
951 if (m_windowDisabler
)
952 delete m_windowDisabler
;
954 // Need to delete the printout and the print preview
955 wxPrintout
*printout
= m_printPreview
->GetPrintout();
959 m_printPreview
->SetPrintout(NULL
);
960 m_printPreview
->SetCanvas(NULL
);
961 m_printPreview
->SetFrame(NULL
);
963 delete m_printPreview
;
968 void wxPreviewFrame::Initialize()
976 m_printPreview
->SetCanvas(m_previewCanvas
);
977 m_printPreview
->SetFrame(this);
979 wxBoxSizer
*item0
= new wxBoxSizer( wxVERTICAL
);
981 item0
->Add( m_controlBar
, 0, wxGROW
|wxALIGN_CENTER_VERTICAL
, 5 );
982 item0
->Add( m_previewCanvas
, 1, wxGROW
|wxALIGN_CENTER_VERTICAL
, 5 );
984 SetAutoLayout( true );
987 m_windowDisabler
= new wxWindowDisabler(this);
991 m_printPreview
->AdjustScrollbars(m_previewCanvas
);
992 m_previewCanvas
->SetFocus();
993 m_controlBar
->SetFocus();
996 void wxPreviewFrame::CreateCanvas()
998 m_previewCanvas
= new wxPreviewCanvas(m_printPreview
, this);
1001 void wxPreviewFrame::CreateControlBar()
1003 long buttons
= wxPREVIEW_DEFAULT
;
1004 if (m_printPreview
->GetPrintoutForPrinting())
1005 buttons
|= wxPREVIEW_PRINT
;
1007 m_controlBar
= new wxPreviewControlBar(m_printPreview
, buttons
, this, wxPoint(0,0), wxSize(400, 40));
1008 m_controlBar
->CreateButtons();
1015 IMPLEMENT_CLASS(wxPrintPreviewBase
, wxObject
)
1017 wxPrintPreviewBase::wxPrintPreviewBase(wxPrintout
*printout
,
1018 wxPrintout
*printoutForPrinting
,
1022 m_printDialogData
= (*data
);
1024 Init(printout
, printoutForPrinting
);
1027 wxPrintPreviewBase::wxPrintPreviewBase(wxPrintout
*printout
,
1028 wxPrintout
*printoutForPrinting
,
1029 wxPrintDialogData
*data
)
1032 m_printDialogData
= (*data
);
1034 Init(printout
, printoutForPrinting
);
1037 void wxPrintPreviewBase::Init(wxPrintout
*printout
,
1038 wxPrintout
*printoutForPrinting
)
1041 m_previewPrintout
= printout
;
1042 if (m_previewPrintout
)
1043 m_previewPrintout
->SetIsPreview(true);
1045 m_printPrintout
= printoutForPrinting
;
1047 m_previewCanvas
= NULL
;
1048 m_previewFrame
= NULL
;
1049 m_previewBitmap
= NULL
;
1056 m_printingPrepared
= false;
1061 wxPrintPreviewBase::~wxPrintPreviewBase()
1063 if (m_previewPrintout
)
1064 delete m_previewPrintout
;
1065 if (m_previewBitmap
)
1066 delete m_previewBitmap
;
1067 if (m_printPrintout
)
1068 delete m_printPrintout
;
1071 bool wxPrintPreviewBase::SetCurrentPage(int pageNum
)
1073 if (m_currentPage
== pageNum
)
1076 m_currentPage
= pageNum
;
1077 if (m_previewBitmap
)
1079 delete m_previewBitmap
;
1080 m_previewBitmap
= NULL
;
1083 if (m_previewCanvas
)
1085 AdjustScrollbars(m_previewCanvas
);
1087 if (!RenderPage(pageNum
))
1089 m_previewCanvas
->Refresh();
1090 m_previewCanvas
->SetFocus();
1095 int wxPrintPreviewBase::GetCurrentPage() const
1096 { return m_currentPage
; };
1097 void wxPrintPreviewBase::SetPrintout(wxPrintout
*printout
)
1098 { m_previewPrintout
= printout
; };
1099 wxPrintout
*wxPrintPreviewBase::GetPrintout() const
1100 { return m_previewPrintout
; };
1101 wxPrintout
*wxPrintPreviewBase::GetPrintoutForPrinting() const
1102 { return m_printPrintout
; };
1103 void wxPrintPreviewBase::SetFrame(wxFrame
*frame
)
1104 { m_previewFrame
= frame
; };
1105 void wxPrintPreviewBase::SetCanvas(wxPreviewCanvas
*canvas
)
1106 { m_previewCanvas
= canvas
; };
1107 wxFrame
*wxPrintPreviewBase::GetFrame() const
1108 { return m_previewFrame
; }
1109 wxPreviewCanvas
*wxPrintPreviewBase::GetCanvas() const
1110 { return m_previewCanvas
; }
1112 bool wxPrintPreviewBase::PaintPage(wxPreviewCanvas
*canvas
, wxDC
& dc
)
1114 DrawBlankPage(canvas
, dc
);
1116 if (!m_previewBitmap
)
1117 if (!RenderPage(m_currentPage
))
1120 if (!m_previewBitmap
)
1126 int canvasWidth
, canvasHeight
;
1127 canvas
->GetSize(&canvasWidth
, &canvasHeight
);
1129 double zoomScale
= ((float)m_currentZoom
/(float)100);
1130 double actualWidth
= (zoomScale
*m_pageWidth
*m_previewScale
);
1131 // float actualHeight = (float)(zoomScale*m_pageHeight*m_previewScale);
1133 int x
= (int) ((canvasWidth
- actualWidth
)/2.0);
1134 if (x
< m_leftMargin
)
1136 int y
= m_topMargin
;
1139 temp_dc
.SelectObject(*m_previewBitmap
);
1141 dc
.Blit(x
, y
, m_previewBitmap
->GetWidth(), m_previewBitmap
->GetHeight(), &temp_dc
, 0, 0);
1143 temp_dc
.SelectObject(wxNullBitmap
);
1148 // Adjusts the scrollbars for the current scale
1149 void wxPrintPreviewBase::AdjustScrollbars(wxPreviewCanvas
*canvas
)
1154 int canvasWidth
, canvasHeight
;
1155 canvas
->GetSize(&canvasWidth
, &canvasHeight
);
1157 double zoomScale
= ((float)m_currentZoom
/(float)100);
1158 double actualWidth
= (zoomScale
*m_pageWidth
*m_previewScale
);
1159 double actualHeight
= (zoomScale
*m_pageHeight
*m_previewScale
);
1161 // Set the scrollbars appropriately
1162 int totalWidth
= (int)(actualWidth
+ 2*m_leftMargin
);
1163 int totalHeight
= (int)(actualHeight
+ 2*m_topMargin
);
1164 int scrollUnitsX
= totalWidth
/10;
1165 int scrollUnitsY
= totalHeight
/10;
1166 wxSize virtualSize
= canvas
->GetVirtualSize();
1167 if (virtualSize
.GetWidth() != totalWidth
|| virtualSize
.GetHeight() != totalHeight
)
1168 canvas
->SetScrollbars(10, 10, scrollUnitsX
, scrollUnitsY
, 0, 0, true);
1171 bool wxPrintPreviewBase::RenderPage(int pageNum
)
1175 int canvasWidth
, canvasHeight
;
1177 if (!m_previewCanvas
)
1179 wxFAIL_MSG(_T("wxPrintPreviewBase::RenderPage: must use wxPrintPreviewBase::SetCanvas to let me know about the canvas!"));
1183 m_previewCanvas
->GetSize(&canvasWidth
, &canvasHeight
);
1185 double zoomScale
= (m_currentZoom
/100.0);
1186 int actualWidth
= (int)(zoomScale
*m_pageWidth
*m_previewScale
);
1187 int actualHeight
= (int)(zoomScale
*m_pageHeight
*m_previewScale
);
1189 if (!m_previewBitmap
)
1191 m_previewBitmap
= new wxBitmap((int)actualWidth
, (int)actualHeight
);
1192 if (!m_previewBitmap
|| !m_previewBitmap
->Ok())
1194 if (m_previewBitmap
) {
1195 delete m_previewBitmap
;
1196 m_previewBitmap
= NULL
;
1198 wxMessageBox(_("Sorry, not enough memory to create a preview."), _("Print Preview Failure"), wxOK
);
1203 wxMemoryDC memoryDC
;
1204 memoryDC
.SelectObject(*m_previewBitmap
);
1208 m_previewPrintout
->SetDC(&memoryDC
);
1209 m_previewPrintout
->SetPageSizePixels(m_pageWidth
, m_pageHeight
);
1211 // Need to delay OnPreparePrinting until here, so we have enough information.
1212 if (!m_printingPrepared
)
1214 m_previewPrintout
->OnPreparePrinting();
1216 m_previewPrintout
->GetPageInfo(&m_minPage
, &m_maxPage
, &selFrom
, &selTo
);
1217 m_printingPrepared
= true;
1220 m_previewPrintout
->OnBeginPrinting();
1222 if (!m_previewPrintout
->OnBeginDocument(m_printDialogData
.GetFromPage(), m_printDialogData
.GetToPage()))
1224 wxMessageBox(_("Could not start document preview."), _("Print Preview Failure"), wxOK
);
1226 memoryDC
.SelectObject(wxNullBitmap
);
1228 delete m_previewBitmap
;
1229 m_previewBitmap
= NULL
;
1233 m_previewPrintout
->OnPrintPage(pageNum
);
1234 m_previewPrintout
->OnEndDocument();
1235 m_previewPrintout
->OnEndPrinting();
1237 m_previewPrintout
->SetDC(NULL
);
1239 memoryDC
.SelectObject(wxNullBitmap
);
1244 status
= wxString::Format(_("Page %d of %d"), pageNum
, m_maxPage
);
1246 status
= wxString::Format(_("Page %d"), pageNum
);
1249 m_previewFrame
->SetStatusText(status
);
1256 bool wxPrintPreviewBase::DrawBlankPage(wxPreviewCanvas
*canvas
, wxDC
& dc
)
1258 int canvasWidth
, canvasHeight
;
1259 canvas
->GetSize(&canvasWidth
, &canvasHeight
);
1261 float zoomScale
= (float)((float)m_currentZoom
/(float)100);
1262 float actualWidth
= zoomScale
*m_pageWidth
*m_previewScale
;
1263 float actualHeight
= zoomScale
*m_pageHeight
*m_previewScale
;
1265 float x
= (float)((canvasWidth
- actualWidth
)/2.0);
1266 if (x
< m_leftMargin
)
1267 x
= (float)m_leftMargin
;
1268 float y
= (float)m_topMargin
;
1270 // Draw shadow, allowing for 1-pixel border AROUND the actual page
1271 int shadowOffset
= 4;
1272 dc
.SetPen(*wxBLACK_PEN
);
1273 dc
.SetBrush(*wxBLACK_BRUSH
);
1275 dc.DrawRectangle((int)(x-1 + shadowOffset), (int)(y-1 + shadowOffset), (int)(actualWidth+2), (int)(actualHeight+2));
1277 dc
.DrawRectangle((int)(x
+ shadowOffset
), (int)(y
+ actualHeight
+1), (int)(actualWidth
), shadowOffset
);
1278 dc
.DrawRectangle((int)(x
+ actualWidth
), (int)(y
+ shadowOffset
), shadowOffset
, (int)(actualHeight
));
1280 // Draw blank page allowing for 1-pixel border AROUND the actual page
1281 dc
.SetPen(*wxBLACK_PEN
);
1282 dc
.SetBrush(*wxWHITE_BRUSH
);
1285 wxRegion update_region = canvas->GetUpdateRegion();
1286 wxRect r = update_region.GetBox();
1288 printf( "x: %d y: %d w: %d h: %d.\n", (int)r.x, (int)r.y, (int)r.width, (int)r.height );
1291 dc
.DrawRectangle((int)(x
-2), (int)(y
-1), (int)(actualWidth
+3), (int)(actualHeight
+2));
1296 void wxPrintPreviewBase::SetZoom(int percent
)
1298 if (m_currentZoom
== percent
)
1301 m_currentZoom
= percent
;
1302 if (m_previewBitmap
)
1304 delete m_previewBitmap
;
1305 m_previewBitmap
= NULL
;
1308 if (m_previewCanvas
)
1310 AdjustScrollbars(m_previewCanvas
);
1311 RenderPage(m_currentPage
);
1312 ((wxScrolledWindow
*) m_previewCanvas
)->Scroll(0, 0);
1313 m_previewCanvas
->ClearBackground();
1314 m_previewCanvas
->Refresh();
1315 m_previewCanvas
->SetFocus();
1319 wxPrintDialogData
& wxPrintPreviewBase::GetPrintDialogData()
1321 return m_printDialogData
;
1324 int wxPrintPreviewBase::GetZoom() const
1325 { return m_currentZoom
; }
1326 int wxPrintPreviewBase::GetMaxPage() const
1327 { return m_maxPage
; }
1328 int wxPrintPreviewBase::GetMinPage() const
1329 { return m_minPage
; }
1330 bool wxPrintPreviewBase::Ok() const
1332 void wxPrintPreviewBase::SetOk(bool ok
)
1334 //----------------------------------------------------------------------------
1336 //----------------------------------------------------------------------------
1338 IMPLEMENT_CLASS(wxPrintPreview
, wxPrintPreviewBase
)
1340 wxPrintPreview::wxPrintPreview(wxPrintout
*printout
,
1341 wxPrintout
*printoutForPrinting
,
1342 wxPrintDialogData
*data
) :
1343 wxPrintPreviewBase( printout
, printoutForPrinting
, data
)
1345 m_pimpl
= wxPrintFactory::GetFactory()->
1346 CreatePrintPreview( printout
, printoutForPrinting
, data
);
1349 wxPrintPreview::wxPrintPreview(wxPrintout
*printout
,
1350 wxPrintout
*printoutForPrinting
,
1351 wxPrintData
*data
) :
1352 wxPrintPreviewBase( printout
, printoutForPrinting
, data
)
1354 m_pimpl
= wxPrintFactory::GetFactory()->
1355 CreatePrintPreview( printout
, printoutForPrinting
, data
);
1358 wxPrintPreview::~wxPrintPreview()
1362 // don't delete twice
1363 m_printPrintout
= NULL
;
1364 m_previewPrintout
= NULL
;
1365 m_previewBitmap
= NULL
;
1368 bool wxPrintPreview::SetCurrentPage(int pageNum
)
1370 return m_pimpl
->SetCurrentPage( pageNum
);
1373 int wxPrintPreview::GetCurrentPage() const
1375 return m_pimpl
->GetCurrentPage();
1378 void wxPrintPreview::SetPrintout(wxPrintout
*printout
)
1380 m_pimpl
->SetPrintout( printout
);
1383 wxPrintout
*wxPrintPreview::GetPrintout() const
1385 return m_pimpl
->GetPrintout();
1388 wxPrintout
*wxPrintPreview::GetPrintoutForPrinting() const
1390 return m_pimpl
->GetPrintoutForPrinting();
1393 void wxPrintPreview::SetFrame(wxFrame
*frame
)
1395 m_pimpl
->SetFrame( frame
);
1398 void wxPrintPreview::SetCanvas(wxPreviewCanvas
*canvas
)
1400 m_pimpl
->SetCanvas( canvas
);
1403 wxFrame
*wxPrintPreview::GetFrame() const
1405 return m_pimpl
->GetFrame();
1408 wxPreviewCanvas
*wxPrintPreview::GetCanvas() const
1410 return m_pimpl
->GetCanvas();
1413 bool wxPrintPreview::PaintPage(wxPreviewCanvas
*canvas
, wxDC
& dc
)
1415 return m_pimpl
->PaintPage( canvas
, dc
);
1418 bool wxPrintPreview::DrawBlankPage(wxPreviewCanvas
*canvas
, wxDC
& dc
)
1420 return m_pimpl
->DrawBlankPage( canvas
, dc
);
1423 void wxPrintPreview::AdjustScrollbars(wxPreviewCanvas
*canvas
)
1425 m_pimpl
->AdjustScrollbars( canvas
);
1428 bool wxPrintPreview::RenderPage(int pageNum
)
1430 return m_pimpl
->RenderPage( pageNum
);
1433 void wxPrintPreview::SetZoom(int percent
)
1435 m_pimpl
->SetZoom( percent
);
1438 wxPrintDialogData
& wxPrintPreview::GetPrintDialogData()
1440 return m_pimpl
->GetPrintDialogData();
1443 int wxPrintPreview::GetMaxPage() const
1445 return m_pimpl
->GetMaxPage();
1448 int wxPrintPreview::GetMinPage() const
1450 return m_pimpl
->GetMinPage();
1453 bool wxPrintPreview::Ok() const
1455 return m_pimpl
->Ok();
1458 void wxPrintPreview::SetOk(bool ok
)
1460 m_pimpl
->SetOk( ok
);
1463 bool wxPrintPreview::Print(bool interactive
)
1465 return m_pimpl
->Print( interactive
);
1468 void wxPrintPreview::DetermineScaling()
1470 m_pimpl
->DetermineScaling();
1474 #endif // wxUSE_PRINTING_ARCHITECTURE