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"
57 #include "wx/mac/private/print.h"
59 #include "wx/generic/prntdlgg.h"
63 #include "wx/msw/wrapcdlg.h"
69 //----------------------------------------------------------------------------
71 //----------------------------------------------------------------------------
73 wxPrintFactory
*wxPrintFactory::m_factory
= NULL
;
75 void wxPrintFactory::SetPrintFactory( wxPrintFactory
*factory
)
77 if (wxPrintFactory::m_factory
)
78 delete wxPrintFactory::m_factory
;
80 wxPrintFactory::m_factory
= factory
;
83 wxPrintFactory
*wxPrintFactory::GetFactory()
85 if (!wxPrintFactory::m_factory
)
86 wxPrintFactory::m_factory
= new wxNativePrintFactory
;
88 return wxPrintFactory::m_factory
;
91 //----------------------------------------------------------------------------
92 // wxNativePrintFactory
93 //----------------------------------------------------------------------------
95 wxPrinterBase
*wxNativePrintFactory::CreatePrinter( wxPrintDialogData
*data
)
97 #if defined(__WXMSW__) && !defined(__WXUNIVERSAL__)
98 return new wxWindowsPrinter( data
);
99 #elif defined(__WXMAC__)
100 return new wxMacPrinter( data
);
101 #elif defined(__WXPM__)
102 return new wxOS2Printer( data
);
104 return new wxPostScriptPrinter( data
);
108 wxPrintPreviewBase
*wxNativePrintFactory::CreatePrintPreview( wxPrintout
*preview
,
109 wxPrintout
*printout
, wxPrintDialogData
*data
)
111 #if defined(__WXMSW__) && !defined(__WXUNIVERSAL__)
112 return new wxWindowsPrintPreview( preview
, printout
, data
);
113 #elif defined(__WXMAC__)
114 return new wxMacPrintPreview( preview
, printout
, data
);
115 #elif defined(__WXPM__)
116 return new wxOS2PrintPreview( preview
, printout
, data
);
118 return new wxPostScriptPrintPreview( preview
, printout
, data
);
122 wxPrintPreviewBase
*wxNativePrintFactory::CreatePrintPreview( wxPrintout
*preview
,
123 wxPrintout
*printout
, wxPrintData
*data
)
125 #if defined(__WXMSW__) && !defined(__WXUNIVERSAL__)
126 return new wxWindowsPrintPreview( preview
, printout
, data
);
127 #elif defined(__WXMAC__)
128 return new wxMacPrintPreview( preview
, printout
, data
);
129 #elif defined(__WXPM__)
130 return new wxOS2PrintPreview( preview
, printout
, data
);
132 return new wxPostScriptPrintPreview( preview
, printout
, data
);
136 wxPrintDialogBase
*wxNativePrintFactory::CreatePrintDialog( wxWindow
*parent
,
137 wxPrintDialogData
*data
)
139 #if defined(__WXMSW__) && !defined(__WXUNIVERSAL__)
140 return new wxWindowsPrintDialog( parent
, data
);
141 #elif defined(__WXMAC__)
142 return new wxMacPrintDialog( parent
, data
);
144 return new wxGenericPrintDialog( parent
, data
);
148 wxPrintDialogBase
*wxNativePrintFactory::CreatePrintDialog( wxWindow
*parent
,
151 #if defined(__WXMSW__) && !defined(__WXUNIVERSAL__)
152 return new wxWindowsPrintDialog( parent
, data
);
153 #elif defined(__WXMAC__)
154 return new wxMacPrintDialog( parent
, data
);
156 return new wxGenericPrintDialog( parent
, data
);
160 wxPageSetupDialogBase
*wxNativePrintFactory::CreatePageSetupDialog( wxWindow
*parent
,
161 wxPageSetupDialogData
*data
)
163 #if defined(__WXMSW__) && !defined(__WXUNIVERSAL__)
164 return new wxWindowsPageSetupDialog( parent
, data
);
165 #elif defined(__WXMAC__)
166 return new wxMacPageSetupDialog( parent
, data
);
168 return new wxGenericPageSetupDialog( parent
, data
);
172 bool wxNativePrintFactory::HasPrintSetupDialog()
174 #if defined(__WXMSW__) && !defined(__WXUNIVERSAL__)
176 #elif defined(__WXMAC__)
179 // Only here do we need to provide the print setup
180 // dialog ourselves, the other platforms either have
181 // none, don't make it accessible or let you configure
182 // the printer from the wxPrintDialog anyway.
188 wxDialog
*wxNativePrintFactory::CreatePrintSetupDialog( wxWindow
*parent
,
191 #if defined(__WXMSW__) && !defined(__WXUNIVERSAL__)
195 #elif defined(__WXMAC__)
200 // Only here do we need to provide the print setup
201 // dialog ourselves, the other platforms either have
202 // none, don't make it accessible or let you configure
203 // the printer from the wxPrintDialog anyway.
204 return new wxGenericPrintSetupDialog( parent
, data
);
208 bool wxNativePrintFactory::HasOwnPrintToFile()
210 // Only relevant for PostScript and here the
211 // setup dialog provides no "print to file"
212 // option. In the GNOME setup dialog, the
213 // setup dialog has its own print to file.
217 bool wxNativePrintFactory::HasPrinterLine()
219 // Only relevant for PostScript for now
223 wxString
wxNativePrintFactory::CreatePrinterLine()
225 // Only relevant for PostScript for now
227 // We should query "lpstat -d" here
228 return _("Generic PostScript");
231 bool wxNativePrintFactory::HasStatusLine()
233 // Only relevant for PostScript for now
237 wxString
wxNativePrintFactory::CreateStatusLine()
239 // Only relevant for PostScript for now
241 // We should query "lpstat -r" or "lpstat -p" here
245 wxPrintNativeDataBase
*wxNativePrintFactory::CreatePrintNativeData()
247 #if defined(__WXMSW__) && !defined(__WXUNIVERSAL__)
248 return new wxWindowsPrintNativeData
;
249 #elif defined(__WXMAC__)
250 return new wxMacCarbonPrintData
;
252 return new wxPostScriptPrintNativeData
;
256 //----------------------------------------------------------------------------
257 // wxPrintNativeDataBase
258 //----------------------------------------------------------------------------
260 IMPLEMENT_ABSTRACT_CLASS(wxPrintNativeDataBase
, wxObject
)
262 wxPrintNativeDataBase::wxPrintNativeDataBase()
267 //----------------------------------------------------------------------------
268 // wxPrintFactoryModule
269 //----------------------------------------------------------------------------
271 class wxPrintFactoryModule
: public wxModule
274 wxPrintFactoryModule() {}
275 bool OnInit() { return true; }
276 void OnExit() { wxPrintFactory::SetPrintFactory( NULL
); }
279 DECLARE_DYNAMIC_CLASS(wxPrintFactoryModule
)
282 IMPLEMENT_DYNAMIC_CLASS(wxPrintFactoryModule
, wxModule
)
284 //----------------------------------------------------------------------------
286 //----------------------------------------------------------------------------
288 IMPLEMENT_CLASS(wxPrinterBase
, wxObject
)
290 wxPrinterBase::wxPrinterBase(wxPrintDialogData
*data
)
292 m_currentPrintout
= (wxPrintout
*) NULL
;
293 sm_abortWindow
= (wxWindow
*) NULL
;
296 m_printDialogData
= (*data
);
297 sm_lastError
= wxPRINTER_NO_ERROR
;
300 wxWindow
*wxPrinterBase::sm_abortWindow
= (wxWindow
*) NULL
;
301 bool wxPrinterBase::sm_abortIt
= false;
302 wxPrinterError
wxPrinterBase::sm_lastError
= wxPRINTER_NO_ERROR
;
304 wxPrinterBase::~wxPrinterBase()
308 wxWindow
*wxPrinterBase::CreateAbortWindow(wxWindow
*parent
, wxPrintout
* printout
)
310 wxPrintAbortDialog
*dialog
= new wxPrintAbortDialog(parent
, _("Printing ") , wxDefaultPosition
, wxDefaultSize
, wxDEFAULT_DIALOG_STYLE
);
312 wxBoxSizer
*button_sizer
= new wxBoxSizer( wxVERTICAL
);
313 button_sizer
->Add( new wxStaticText(dialog
, wxID_ANY
, _("Please wait while printing\n") + printout
->GetTitle() ), 0, wxALL
, 10 );
314 button_sizer
->Add( new wxButton( dialog
, wxID_CANCEL
, wxT("Cancel") ), 0, wxALL
| wxALIGN_CENTER
, 10 );
316 dialog
->SetAutoLayout( true );
317 dialog
->SetSizer( button_sizer
);
319 button_sizer
->Fit(dialog
);
320 button_sizer
->SetSizeHints (dialog
) ;
325 void wxPrinterBase::ReportError(wxWindow
*parent
, wxPrintout
*WXUNUSED(printout
), const wxString
& message
)
327 wxMessageBox(message
, _("Printing Error"), wxOK
, parent
);
330 wxPrintDialogData
& wxPrinterBase::GetPrintDialogData() const
332 return (wxPrintDialogData
&) m_printDialogData
;
335 //----------------------------------------------------------------------------
337 //----------------------------------------------------------------------------
339 IMPLEMENT_CLASS(wxPrinter
, wxPrinterBase
)
341 wxPrinter::wxPrinter(wxPrintDialogData
*data
)
343 m_pimpl
= wxPrintFactory::GetFactory()->CreatePrinter( data
);
346 wxPrinter::~wxPrinter()
351 wxWindow
*wxPrinter::CreateAbortWindow(wxWindow
*parent
, wxPrintout
*printout
)
353 return m_pimpl
->CreateAbortWindow( parent
, printout
);
356 void wxPrinter::ReportError(wxWindow
*parent
, wxPrintout
*printout
, const wxString
& message
)
358 m_pimpl
->ReportError( parent
, printout
, message
);
361 bool wxPrinter::Setup(wxWindow
*parent
)
363 return m_pimpl
->Setup( parent
);
366 bool wxPrinter::Print(wxWindow
*parent
, wxPrintout
*printout
, bool prompt
)
368 return m_pimpl
->Print( parent
, printout
, prompt
);
371 wxDC
* wxPrinter::PrintDialog(wxWindow
*parent
)
373 return m_pimpl
->PrintDialog( parent
);
376 wxPrintDialogData
& wxPrinter::GetPrintDialogData() const
378 return m_pimpl
->GetPrintDialogData();
381 // ---------------------------------------------------------------------------
382 // wxPrintDialogBase: the dialog for printing.
383 // ---------------------------------------------------------------------------
385 IMPLEMENT_ABSTRACT_CLASS(wxPrintDialogBase
, wxDialog
)
387 wxPrintDialogBase::wxPrintDialogBase(wxWindow
*parent
,
389 const wxString
&title
,
393 : wxDialog( parent
, id
, title
.empty() ? wxString(_("Print")) : title
,
398 // ---------------------------------------------------------------------------
399 // wxPrintDialog: the dialog for printing
400 // ---------------------------------------------------------------------------
402 IMPLEMENT_CLASS(wxPrintDialog
, wxObject
)
404 wxPrintDialog::wxPrintDialog(wxWindow
*parent
, wxPrintDialogData
* data
)
406 m_pimpl
= wxPrintFactory::GetFactory()->CreatePrintDialog( parent
, data
);
409 wxPrintDialog::wxPrintDialog(wxWindow
*parent
, wxPrintData
* data
)
411 m_pimpl
= wxPrintFactory::GetFactory()->CreatePrintDialog( parent
, data
);
414 wxPrintDialog::~wxPrintDialog()
419 int wxPrintDialog::ShowModal()
421 return m_pimpl
->ShowModal();
424 wxPrintDialogData
& wxPrintDialog::GetPrintDialogData()
426 return m_pimpl
->GetPrintDialogData();
429 wxPrintData
& wxPrintDialog::GetPrintData()
431 return m_pimpl
->GetPrintData();
434 wxDC
*wxPrintDialog::GetPrintDC()
436 return m_pimpl
->GetPrintDC();
439 // ---------------------------------------------------------------------------
440 // wxPageSetupDialogBase: the page setup dialog
441 // ---------------------------------------------------------------------------
443 IMPLEMENT_ABSTRACT_CLASS(wxPageSetupDialogBase
, wxDialog
)
445 wxPageSetupDialogBase::wxPageSetupDialogBase(wxWindow
*parent
,
447 const wxString
&title
,
451 : wxDialog( parent
, id
, title
.empty() ? wxString(_("Page setup")) : title
,
456 // ---------------------------------------------------------------------------
457 // wxPageSetupDialog: the page setup dialog
458 // ---------------------------------------------------------------------------
460 IMPLEMENT_CLASS(wxPageSetupDialog
, wxObject
)
462 wxPageSetupDialog::wxPageSetupDialog(wxWindow
*parent
, wxPageSetupDialogData
*data
)
464 m_pimpl
= wxPrintFactory::GetFactory()->CreatePageSetupDialog( parent
, data
);
467 wxPageSetupDialog::~wxPageSetupDialog()
472 int wxPageSetupDialog::ShowModal()
474 return m_pimpl
->ShowModal();
477 wxPageSetupDialogData
& wxPageSetupDialog::GetPageSetupDialogData()
479 return m_pimpl
->GetPageSetupDialogData();
483 wxPageSetupDialogData
& wxPageSetupDialog::GetPageSetupData()
485 return m_pimpl
->GetPageSetupDialogData();
488 //----------------------------------------------------------------------------
489 // wxPrintAbortDialog
490 //----------------------------------------------------------------------------
492 BEGIN_EVENT_TABLE(wxPrintAbortDialog
, wxDialog
)
493 EVT_BUTTON(wxID_CANCEL
, wxPrintAbortDialog::OnCancel
)
496 void wxPrintAbortDialog::OnCancel(wxCommandEvent
& WXUNUSED(event
))
498 wxPrinterBase::sm_abortIt
= true;
499 wxPrinterBase::sm_abortWindow
->Show(false);
500 wxPrinterBase::sm_abortWindow
->Close(true);
501 wxPrinterBase::sm_abortWindow
= (wxWindow
*) NULL
;
504 //----------------------------------------------------------------------------
506 //----------------------------------------------------------------------------
508 IMPLEMENT_ABSTRACT_CLASS(wxPrintout
, wxObject
)
510 wxPrintout::wxPrintout(const wxString
& title
)
512 m_printoutTitle
= title
;
513 m_printoutDC
= (wxDC
*) NULL
;
516 m_pageWidthPixels
= 0;
517 m_pageHeightPixels
= 0;
525 wxPrintout::~wxPrintout()
529 bool wxPrintout::OnBeginDocument(int WXUNUSED(startPage
), int WXUNUSED(endPage
))
531 return GetDC()->StartDoc(_("Printing ") + m_printoutTitle
);
534 void wxPrintout::OnEndDocument()
539 void wxPrintout::OnBeginPrinting()
543 void wxPrintout::OnEndPrinting()
547 bool wxPrintout::HasPage(int page
)
552 void wxPrintout::GetPageInfo(int *minPage
, int *maxPage
, int *fromPage
, int *toPage
)
560 //----------------------------------------------------------------------------
562 //----------------------------------------------------------------------------
564 IMPLEMENT_CLASS(wxPreviewCanvas
, wxWindow
)
566 BEGIN_EVENT_TABLE(wxPreviewCanvas
, wxScrolledWindow
)
567 EVT_PAINT(wxPreviewCanvas::OnPaint
)
568 EVT_CHAR(wxPreviewCanvas::OnChar
)
569 EVT_SYS_COLOUR_CHANGED(wxPreviewCanvas::OnSysColourChanged
)
571 EVT_MOUSEWHEEL(wxPreviewCanvas::OnMouseWheel
)
575 // VZ: the current code doesn't refresh properly without
576 // wxFULL_REPAINT_ON_RESIZE, this must be fixed as otherwise we have
577 // really horrible flicker when resizing the preview frame, but without
578 // this style it simply doesn't work correctly at all...
579 wxPreviewCanvas::wxPreviewCanvas(wxPrintPreviewBase
*preview
, wxWindow
*parent
,
580 const wxPoint
& pos
, const wxSize
& size
, long style
, const wxString
& name
):
581 wxScrolledWindow(parent
, wxID_ANY
, pos
, size
, style
| wxFULL_REPAINT_ON_RESIZE
, name
)
583 m_printPreview
= preview
;
585 // The app workspace colour is always white, but we should have
586 // a contrast with the page.
587 wxSystemColour colourIndex
= wxSYS_COLOUR_3DDKSHADOW
;
589 wxSystemColour colourIndex
= wxSYS_COLOUR_APPWORKSPACE
;
591 SetBackgroundColour(wxSystemSettings::GetColour(colourIndex
));
593 SetScrollbars(10, 10, 100, 100);
596 wxPreviewCanvas::~wxPreviewCanvas()
600 void wxPreviewCanvas::OnPaint(wxPaintEvent
& WXUNUSED(event
))
607 if (!GetUpdateRegion().IsEmpty())
608 dc.SetClippingRegion( GetUpdateRegion() );
614 m_printPreview
->PaintPage(this, dc
);
618 // Responds to colour changes, and passes event on to children.
619 void wxPreviewCanvas::OnSysColourChanged(wxSysColourChangedEvent
& event
)
622 // The app workspace colour is always white, but we should have
623 // a contrast with the page.
624 wxSystemColour colourIndex
= wxSYS_COLOUR_3DDKSHADOW
;
626 wxSystemColour colourIndex
= wxSYS_COLOUR_APPWORKSPACE
;
628 SetBackgroundColour(wxSystemSettings::GetColour(colourIndex
));
631 // Propagate the event to the non-top-level children
632 wxWindow::OnSysColourChanged(event
);
635 void wxPreviewCanvas::OnChar(wxKeyEvent
&event
)
637 wxPreviewControlBar
* controlBar
= ((wxPreviewFrame
*) GetParent())->GetControlBar();
638 if (event
.GetKeyCode() == WXK_ESCAPE
)
640 ((wxPreviewFrame
*) GetParent())->Close(true);
643 else if (event
.GetKeyCode() == WXK_TAB
)
645 controlBar
->OnGoto();
648 else if (event
.GetKeyCode() == WXK_RETURN
)
650 controlBar
->OnPrint();
654 if (!event
.ControlDown())
660 switch(event
.GetKeyCode())
663 controlBar
->OnNext(); break;
665 controlBar
->OnPrevious(); break;
667 controlBar
->OnFirst(); break;
669 controlBar
->OnLast(); break;
677 void wxPreviewCanvas::OnMouseWheel(wxMouseEvent
& event
)
679 wxPreviewControlBar
*
680 controlBar
= wxStaticCast(GetParent(), wxPreviewFrame
)->GetControlBar();
684 if ( event
.ControlDown() && event
.GetWheelRotation() != 0 )
686 int currentZoom
= controlBar
->GetZoomControl();
689 if ( currentZoom
< 100 )
691 else if ( currentZoom
<= 120 )
696 if ( event
.GetWheelRotation() > 0 )
699 int newZoom
= currentZoom
+ delta
;
704 if ( newZoom
!= currentZoom
)
706 controlBar
->SetZoomControl(newZoom
);
707 m_printPreview
->SetZoom(newZoom
);
717 #endif // wxUSE_MOUSEWHEEL
719 //----------------------------------------------------------------------------
720 // wxPreviewControlBar
721 //----------------------------------------------------------------------------
723 IMPLEMENT_CLASS(wxPreviewControlBar
, wxWindow
)
725 BEGIN_EVENT_TABLE(wxPreviewControlBar
, wxPanel
)
726 EVT_BUTTON(wxID_PREVIEW_CLOSE
, wxPreviewControlBar::OnWindowClose
)
727 EVT_BUTTON(wxID_PREVIEW_PRINT
, wxPreviewControlBar::OnPrintButton
)
728 EVT_BUTTON(wxID_PREVIEW_PREVIOUS
, wxPreviewControlBar::OnPreviousButton
)
729 EVT_BUTTON(wxID_PREVIEW_NEXT
, wxPreviewControlBar::OnNextButton
)
730 EVT_BUTTON(wxID_PREVIEW_FIRST
, wxPreviewControlBar::OnFirstButton
)
731 EVT_BUTTON(wxID_PREVIEW_LAST
, wxPreviewControlBar::OnLastButton
)
732 EVT_BUTTON(wxID_PREVIEW_GOTO
, wxPreviewControlBar::OnGotoButton
)
733 EVT_CHOICE(wxID_PREVIEW_ZOOM
, wxPreviewControlBar::OnZoom
)
734 EVT_PAINT(wxPreviewControlBar::OnPaint
)
737 wxPreviewControlBar::wxPreviewControlBar(wxPrintPreviewBase
*preview
, long buttons
,
738 wxWindow
*parent
, const wxPoint
& pos
, const wxSize
& size
,
739 long style
, const wxString
& name
):
740 wxPanel(parent
, wxID_ANY
, pos
, size
, style
, name
)
742 m_printPreview
= preview
;
743 m_closeButton
= (wxButton
*) NULL
;
744 m_nextPageButton
= (wxButton
*) NULL
;
745 m_previousPageButton
= (wxButton
*) NULL
;
746 m_printButton
= (wxButton
*) NULL
;
747 m_zoomControl
= (wxChoice
*) NULL
;
748 m_buttonFlags
= buttons
;
751 wxPreviewControlBar::~wxPreviewControlBar()
755 void wxPreviewControlBar::OnPaint(wxPaintEvent
& WXUNUSED(event
))
761 dc
.SetPen(*wxBLACK_PEN
);
762 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
763 dc
.DrawLine( 0, h
-1, w
, h
-1 );
766 void wxPreviewControlBar::OnWindowClose(wxCommandEvent
& WXUNUSED(event
))
768 wxPreviewFrame
*frame
= (wxPreviewFrame
*)GetParent();
772 void wxPreviewControlBar::OnPrint(void)
774 wxPrintPreviewBase
*preview
= GetPrintPreview();
775 preview
->Print(true);
778 void wxPreviewControlBar::OnNext(void)
780 wxPrintPreviewBase
*preview
= GetPrintPreview();
783 int currentPage
= preview
->GetCurrentPage();
784 if ((preview
->GetMaxPage() > 0) &&
785 (currentPage
< preview
->GetMaxPage()) &&
786 preview
->GetPrintout()->HasPage(currentPage
+ 1))
788 preview
->SetCurrentPage(currentPage
+ 1);
793 void wxPreviewControlBar::OnPrevious(void)
795 wxPrintPreviewBase
*preview
= GetPrintPreview();
798 int currentPage
= preview
->GetCurrentPage();
799 if ((preview
->GetMinPage() > 0) &&
800 (currentPage
> preview
->GetMinPage()) &&
801 preview
->GetPrintout()->HasPage(currentPage
- 1))
803 preview
->SetCurrentPage(currentPage
- 1);
808 void wxPreviewControlBar::OnFirst(void)
810 wxPrintPreviewBase
*preview
= GetPrintPreview();
813 int currentPage
= preview
->GetMinPage();
814 if (preview
->GetPrintout()->HasPage(currentPage
))
816 preview
->SetCurrentPage(currentPage
);
821 void wxPreviewControlBar::OnLast(void)
823 wxPrintPreviewBase
*preview
= GetPrintPreview();
826 int currentPage
= preview
->GetMaxPage();
827 if (preview
->GetPrintout()->HasPage(currentPage
))
829 preview
->SetCurrentPage(currentPage
);
834 void wxPreviewControlBar::OnGoto(void)
836 wxPrintPreviewBase
*preview
= GetPrintPreview();
841 if (preview
->GetMinPage() > 0)
846 strPrompt
.Printf( _("Enter a page number between %d and %d:"),
847 preview
->GetMinPage(), preview
->GetMaxPage());
848 strPage
.Printf( wxT("%d"), preview
->GetCurrentPage() );
851 wxGetTextFromUser( strPrompt
, _("Goto Page"), strPage
, GetParent());
853 if ( strPage
.ToLong( ¤tPage
) )
854 if (preview
->GetPrintout()->HasPage(currentPage
))
856 preview
->SetCurrentPage(currentPage
);
862 void wxPreviewControlBar::OnZoom(wxCommandEvent
& WXUNUSED(event
))
864 int zoom
= GetZoomControl();
865 if (GetPrintPreview())
866 GetPrintPreview()->SetZoom(zoom
);
869 void wxPreviewControlBar::CreateButtons()
871 SetSize(0, 0, 400, 40);
873 wxBoxSizer
*item0
= new wxBoxSizer( wxHORIZONTAL
);
875 m_closeButton
= new wxButton( this, wxID_PREVIEW_CLOSE
, _("&Close"), wxDefaultPosition
, wxDefaultSize
, 0 );
876 item0
->Add( m_closeButton
, 0, wxALIGN_CENTRE
|wxALL
, 5 );
878 if (m_buttonFlags
& wxPREVIEW_PRINT
)
880 m_printButton
= new wxButton( this, wxID_PREVIEW_PRINT
, _("&Print..."), wxDefaultPosition
, wxDefaultSize
, 0 );
881 item0
->Add( m_printButton
, 0, wxALIGN_CENTRE
|wxALL
, 5 );
884 if (m_buttonFlags
& wxPREVIEW_FIRST
)
886 m_firstPageButton
= new wxButton( this, wxID_PREVIEW_FIRST
, _("|<<"), wxDefaultPosition
, wxDefaultSize
, wxBU_EXACTFIT
);
887 item0
->Add( m_firstPageButton
, 0, wxALIGN_CENTRE
|wxALL
, 5 );
890 if (m_buttonFlags
& wxPREVIEW_PREVIOUS
)
892 m_previousPageButton
= new wxButton( this, wxID_PREVIEW_PREVIOUS
, _("<<"), wxDefaultPosition
, wxDefaultSize
, wxBU_EXACTFIT
);
893 item0
->Add( m_previousPageButton
, 0, wxALIGN_CENTRE
|wxRIGHT
|wxTOP
|wxBOTTOM
, 5 );
896 if (m_buttonFlags
& wxPREVIEW_NEXT
)
898 m_nextPageButton
= new wxButton( this, wxID_PREVIEW_NEXT
, _(">>"), wxDefaultPosition
, wxDefaultSize
, wxBU_EXACTFIT
);
899 item0
->Add( m_nextPageButton
, 0, wxALIGN_CENTRE
|wxRIGHT
|wxTOP
|wxBOTTOM
, 5 );
902 if (m_buttonFlags
& wxPREVIEW_LAST
)
904 m_lastPageButton
= new wxButton( this, wxID_PREVIEW_LAST
, _(">>|"), wxDefaultPosition
, wxDefaultSize
, wxBU_EXACTFIT
);
905 item0
->Add( m_lastPageButton
, 0, wxALIGN_CENTRE
|wxRIGHT
|wxTOP
|wxBOTTOM
, 5 );
908 if (m_buttonFlags
& wxPREVIEW_GOTO
)
910 m_gotoPageButton
= new wxButton( this, wxID_PREVIEW_GOTO
, _("&Goto..."), wxDefaultPosition
, wxDefaultSize
, 0 );
911 item0
->Add( m_gotoPageButton
, 0, wxALIGN_CENTRE
|wxALL
, 5 );
914 if (m_buttonFlags
& wxPREVIEW_ZOOM
)
918 wxT("10%"), wxT("15%"), wxT("20%"), wxT("25%"), wxT("30%"), wxT("35%"), wxT("40%"), wxT("45%"), wxT("50%"), wxT("55%"),
919 wxT("60%"), wxT("65%"), wxT("70%"), wxT("75%"), wxT("80%"), wxT("85%"), wxT("90%"), wxT("95%"), wxT("100%"), wxT("110%"),
920 wxT("120%"), wxT("150%"), wxT("200%")
922 int n
= WXSIZEOF(choices
);
924 m_zoomControl
= new wxChoice( this, wxID_PREVIEW_ZOOM
, wxDefaultPosition
, wxSize(70,wxDefaultCoord
), n
, choices
, 0 );
925 item0
->Add( m_zoomControl
, 0, wxALIGN_CENTRE
|wxALL
, 5 );
926 SetZoomControl(m_printPreview
->GetZoom());
933 void wxPreviewControlBar::SetZoomControl(int zoom
)
937 int n
, count
= m_zoomControl
->GetCount();
939 for (n
=0; n
<count
; n
++)
941 if (m_zoomControl
->GetString(n
).BeforeFirst(wxT('%')).ToLong(&val
) &&
944 m_zoomControl
->SetSelection(n
);
949 m_zoomControl
->SetSelection(count
-1);
953 int wxPreviewControlBar::GetZoomControl()
955 if (m_zoomControl
&& (m_zoomControl
->GetStringSelection() != wxEmptyString
))
958 if (m_zoomControl
->GetStringSelection().BeforeFirst(wxT('%')).ToLong(&val
))
970 IMPLEMENT_CLASS(wxPreviewFrame
, wxFrame
)
972 BEGIN_EVENT_TABLE(wxPreviewFrame
, wxFrame
)
973 EVT_CLOSE(wxPreviewFrame::OnCloseWindow
)
976 wxPreviewFrame::wxPreviewFrame(wxPrintPreviewBase
*preview
, wxWindow
*parent
, const wxString
& title
,
977 const wxPoint
& pos
, const wxSize
& size
, long style
, const wxString
& name
):
978 wxFrame(parent
, wxID_ANY
, title
, pos
, size
, style
, name
)
980 m_printPreview
= preview
;
982 m_previewCanvas
= NULL
;
983 m_windowDisabler
= NULL
;
985 // Give the application icon
987 wxFrame
* topFrame
= wxDynamicCast(wxTheApp
->GetTopWindow(), wxFrame
);
989 SetIcon(topFrame
->GetIcon());
993 wxPreviewFrame::~wxPreviewFrame()
997 void wxPreviewFrame::OnCloseWindow(wxCloseEvent
& WXUNUSED(event
))
999 if (m_windowDisabler
)
1000 delete m_windowDisabler
;
1002 // Need to delete the printout and the print preview
1003 wxPrintout
*printout
= m_printPreview
->GetPrintout();
1007 m_printPreview
->SetPrintout(NULL
);
1008 m_printPreview
->SetCanvas(NULL
);
1009 m_printPreview
->SetFrame(NULL
);
1011 delete m_printPreview
;
1016 void wxPreviewFrame::Initialize()
1024 m_printPreview
->SetCanvas(m_previewCanvas
);
1025 m_printPreview
->SetFrame(this);
1027 wxBoxSizer
*item0
= new wxBoxSizer( wxVERTICAL
);
1029 item0
->Add( m_controlBar
, 0, wxGROW
|wxALIGN_CENTER_VERTICAL
, 5 );
1030 item0
->Add( m_previewCanvas
, 1, wxGROW
|wxALIGN_CENTER_VERTICAL
, 5 );
1032 SetAutoLayout( true );
1035 m_windowDisabler
= new wxWindowDisabler(this);
1039 m_printPreview
->AdjustScrollbars(m_previewCanvas
);
1040 m_previewCanvas
->SetFocus();
1041 m_controlBar
->SetFocus();
1044 void wxPreviewFrame::CreateCanvas()
1046 m_previewCanvas
= new wxPreviewCanvas(m_printPreview
, this);
1049 void wxPreviewFrame::CreateControlBar()
1051 long buttons
= wxPREVIEW_DEFAULT
;
1052 if (m_printPreview
->GetPrintoutForPrinting())
1053 buttons
|= wxPREVIEW_PRINT
;
1055 m_controlBar
= new wxPreviewControlBar(m_printPreview
, buttons
, this, wxPoint(0,0), wxSize(400, 40));
1056 m_controlBar
->CreateButtons();
1063 IMPLEMENT_CLASS(wxPrintPreviewBase
, wxObject
)
1065 wxPrintPreviewBase::wxPrintPreviewBase(wxPrintout
*printout
,
1066 wxPrintout
*printoutForPrinting
,
1070 m_printDialogData
= (*data
);
1072 Init(printout
, printoutForPrinting
);
1075 wxPrintPreviewBase::wxPrintPreviewBase(wxPrintout
*printout
,
1076 wxPrintout
*printoutForPrinting
,
1077 wxPrintDialogData
*data
)
1080 m_printDialogData
= (*data
);
1082 Init(printout
, printoutForPrinting
);
1085 void wxPrintPreviewBase::Init(wxPrintout
*printout
,
1086 wxPrintout
*printoutForPrinting
)
1089 m_previewPrintout
= printout
;
1090 if (m_previewPrintout
)
1091 m_previewPrintout
->SetIsPreview(true);
1093 m_printPrintout
= printoutForPrinting
;
1095 m_previewCanvas
= NULL
;
1096 m_previewFrame
= NULL
;
1097 m_previewBitmap
= NULL
;
1104 m_printingPrepared
= false;
1109 wxPrintPreviewBase::~wxPrintPreviewBase()
1111 if (m_previewPrintout
)
1112 delete m_previewPrintout
;
1113 if (m_previewBitmap
)
1114 delete m_previewBitmap
;
1115 if (m_printPrintout
)
1116 delete m_printPrintout
;
1119 bool wxPrintPreviewBase::SetCurrentPage(int pageNum
)
1121 if (m_currentPage
== pageNum
)
1124 m_currentPage
= pageNum
;
1125 if (m_previewBitmap
)
1127 delete m_previewBitmap
;
1128 m_previewBitmap
= NULL
;
1131 if (m_previewCanvas
)
1133 AdjustScrollbars(m_previewCanvas
);
1135 if (!RenderPage(pageNum
))
1137 m_previewCanvas
->Refresh();
1138 m_previewCanvas
->SetFocus();
1143 int wxPrintPreviewBase::GetCurrentPage() const
1144 { return m_currentPage
; };
1145 void wxPrintPreviewBase::SetPrintout(wxPrintout
*printout
)
1146 { m_previewPrintout
= printout
; };
1147 wxPrintout
*wxPrintPreviewBase::GetPrintout() const
1148 { return m_previewPrintout
; };
1149 wxPrintout
*wxPrintPreviewBase::GetPrintoutForPrinting() const
1150 { return m_printPrintout
; };
1151 void wxPrintPreviewBase::SetFrame(wxFrame
*frame
)
1152 { m_previewFrame
= frame
; };
1153 void wxPrintPreviewBase::SetCanvas(wxPreviewCanvas
*canvas
)
1154 { m_previewCanvas
= canvas
; };
1155 wxFrame
*wxPrintPreviewBase::GetFrame() const
1156 { return m_previewFrame
; }
1157 wxPreviewCanvas
*wxPrintPreviewBase::GetCanvas() const
1158 { return m_previewCanvas
; }
1160 bool wxPrintPreviewBase::PaintPage(wxPreviewCanvas
*canvas
, wxDC
& dc
)
1162 DrawBlankPage(canvas
, dc
);
1164 if (!m_previewBitmap
)
1165 if (!RenderPage(m_currentPage
))
1168 if (!m_previewBitmap
)
1174 int canvasWidth
, canvasHeight
;
1175 canvas
->GetSize(&canvasWidth
, &canvasHeight
);
1177 double zoomScale
= ((float)m_currentZoom
/(float)100);
1178 double actualWidth
= (zoomScale
*m_pageWidth
*m_previewScale
);
1179 // float actualHeight = (float)(zoomScale*m_pageHeight*m_previewScale);
1181 int x
= (int) ((canvasWidth
- actualWidth
)/2.0);
1182 if (x
< m_leftMargin
)
1184 int y
= m_topMargin
;
1187 temp_dc
.SelectObject(*m_previewBitmap
);
1189 dc
.Blit(x
, y
, m_previewBitmap
->GetWidth(), m_previewBitmap
->GetHeight(), &temp_dc
, 0, 0);
1191 temp_dc
.SelectObject(wxNullBitmap
);
1196 // Adjusts the scrollbars for the current scale
1197 void wxPrintPreviewBase::AdjustScrollbars(wxPreviewCanvas
*canvas
)
1202 int canvasWidth
, canvasHeight
;
1203 canvas
->GetSize(&canvasWidth
, &canvasHeight
);
1205 double zoomScale
= ((float)m_currentZoom
/(float)100);
1206 double actualWidth
= (zoomScale
*m_pageWidth
*m_previewScale
);
1207 double actualHeight
= (zoomScale
*m_pageHeight
*m_previewScale
);
1209 // Set the scrollbars appropriately
1210 int totalWidth
= (int)(actualWidth
+ 2*m_leftMargin
);
1211 int totalHeight
= (int)(actualHeight
+ 2*m_topMargin
);
1212 int scrollUnitsX
= totalWidth
/10;
1213 int scrollUnitsY
= totalHeight
/10;
1214 wxSize virtualSize
= canvas
->GetVirtualSize();
1215 if (virtualSize
.GetWidth() != totalWidth
|| virtualSize
.GetHeight() != totalHeight
)
1216 canvas
->SetScrollbars(10, 10, scrollUnitsX
, scrollUnitsY
, 0, 0, true);
1219 bool wxPrintPreviewBase::RenderPage(int pageNum
)
1223 int canvasWidth
, canvasHeight
;
1225 if (!m_previewCanvas
)
1227 wxFAIL_MSG(_T("wxPrintPreviewBase::RenderPage: must use wxPrintPreviewBase::SetCanvas to let me know about the canvas!"));
1231 m_previewCanvas
->GetSize(&canvasWidth
, &canvasHeight
);
1233 double zoomScale
= (m_currentZoom
/100.0);
1234 int actualWidth
= (int)(zoomScale
*m_pageWidth
*m_previewScale
);
1235 int actualHeight
= (int)(zoomScale
*m_pageHeight
*m_previewScale
);
1237 if (!m_previewBitmap
)
1239 m_previewBitmap
= new wxBitmap((int)actualWidth
, (int)actualHeight
);
1240 if (!m_previewBitmap
|| !m_previewBitmap
->Ok())
1242 if (m_previewBitmap
) {
1243 delete m_previewBitmap
;
1244 m_previewBitmap
= NULL
;
1246 wxMessageBox(_("Sorry, not enough memory to create a preview."), _("Print Preview Failure"), wxOK
);
1251 wxMemoryDC memoryDC
;
1252 memoryDC
.SelectObject(*m_previewBitmap
);
1256 m_previewPrintout
->SetDC(&memoryDC
);
1257 m_previewPrintout
->SetPageSizePixels(m_pageWidth
, m_pageHeight
);
1259 // Need to delay OnPreparePrinting until here, so we have enough information.
1260 if (!m_printingPrepared
)
1262 m_previewPrintout
->OnPreparePrinting();
1264 m_previewPrintout
->GetPageInfo(&m_minPage
, &m_maxPage
, &selFrom
, &selTo
);
1265 m_printingPrepared
= true;
1268 m_previewPrintout
->OnBeginPrinting();
1270 if (!m_previewPrintout
->OnBeginDocument(m_printDialogData
.GetFromPage(), m_printDialogData
.GetToPage()))
1272 wxMessageBox(_("Could not start document preview."), _("Print Preview Failure"), wxOK
);
1274 memoryDC
.SelectObject(wxNullBitmap
);
1276 delete m_previewBitmap
;
1277 m_previewBitmap
= NULL
;
1281 m_previewPrintout
->OnPrintPage(pageNum
);
1282 m_previewPrintout
->OnEndDocument();
1283 m_previewPrintout
->OnEndPrinting();
1285 m_previewPrintout
->SetDC(NULL
);
1287 memoryDC
.SelectObject(wxNullBitmap
);
1292 status
= wxString::Format(_("Page %d of %d"), pageNum
, m_maxPage
);
1294 status
= wxString::Format(_("Page %d"), pageNum
);
1297 m_previewFrame
->SetStatusText(status
);
1304 bool wxPrintPreviewBase::DrawBlankPage(wxPreviewCanvas
*canvas
, wxDC
& dc
)
1306 int canvasWidth
, canvasHeight
;
1307 canvas
->GetSize(&canvasWidth
, &canvasHeight
);
1309 float zoomScale
= (float)((float)m_currentZoom
/(float)100);
1310 float actualWidth
= zoomScale
*m_pageWidth
*m_previewScale
;
1311 float actualHeight
= zoomScale
*m_pageHeight
*m_previewScale
;
1313 float x
= (float)((canvasWidth
- actualWidth
)/2.0);
1314 if (x
< m_leftMargin
)
1315 x
= (float)m_leftMargin
;
1316 float y
= (float)m_topMargin
;
1318 // Draw shadow, allowing for 1-pixel border AROUND the actual page
1319 int shadowOffset
= 4;
1320 dc
.SetPen(*wxBLACK_PEN
);
1321 dc
.SetBrush(*wxBLACK_BRUSH
);
1323 dc.DrawRectangle((int)(x-1 + shadowOffset), (int)(y-1 + shadowOffset), (int)(actualWidth+2), (int)(actualHeight+2));
1325 dc
.DrawRectangle((int)(x
+ shadowOffset
), (int)(y
+ actualHeight
+1), (int)(actualWidth
), shadowOffset
);
1326 dc
.DrawRectangle((int)(x
+ actualWidth
), (int)(y
+ shadowOffset
), shadowOffset
, (int)(actualHeight
));
1328 // Draw blank page allowing for 1-pixel border AROUND the actual page
1329 dc
.SetPen(*wxBLACK_PEN
);
1330 dc
.SetBrush(*wxWHITE_BRUSH
);
1333 wxRegion update_region = canvas->GetUpdateRegion();
1334 wxRect r = update_region.GetBox();
1336 printf( "x: %d y: %d w: %d h: %d.\n", (int)r.x, (int)r.y, (int)r.width, (int)r.height );
1339 dc
.DrawRectangle((int)(x
-2), (int)(y
-1), (int)(actualWidth
+3), (int)(actualHeight
+2));
1344 void wxPrintPreviewBase::SetZoom(int percent
)
1346 if (m_currentZoom
== percent
)
1349 m_currentZoom
= percent
;
1350 if (m_previewBitmap
)
1352 delete m_previewBitmap
;
1353 m_previewBitmap
= NULL
;
1356 if (m_previewCanvas
)
1358 AdjustScrollbars(m_previewCanvas
);
1359 RenderPage(m_currentPage
);
1360 ((wxScrolledWindow
*) m_previewCanvas
)->Scroll(0, 0);
1361 m_previewCanvas
->ClearBackground();
1362 m_previewCanvas
->Refresh();
1363 m_previewCanvas
->SetFocus();
1367 wxPrintDialogData
& wxPrintPreviewBase::GetPrintDialogData()
1369 return m_printDialogData
;
1372 int wxPrintPreviewBase::GetZoom() const
1373 { return m_currentZoom
; }
1374 int wxPrintPreviewBase::GetMaxPage() const
1375 { return m_maxPage
; }
1376 int wxPrintPreviewBase::GetMinPage() const
1377 { return m_minPage
; }
1378 bool wxPrintPreviewBase::Ok() const
1380 void wxPrintPreviewBase::SetOk(bool ok
)
1382 //----------------------------------------------------------------------------
1384 //----------------------------------------------------------------------------
1386 IMPLEMENT_CLASS(wxPrintPreview
, wxPrintPreviewBase
)
1388 wxPrintPreview::wxPrintPreview(wxPrintout
*printout
,
1389 wxPrintout
*printoutForPrinting
,
1390 wxPrintDialogData
*data
) :
1391 wxPrintPreviewBase( printout
, printoutForPrinting
, data
)
1393 m_pimpl
= wxPrintFactory::GetFactory()->
1394 CreatePrintPreview( printout
, printoutForPrinting
, data
);
1397 wxPrintPreview::wxPrintPreview(wxPrintout
*printout
,
1398 wxPrintout
*printoutForPrinting
,
1399 wxPrintData
*data
) :
1400 wxPrintPreviewBase( printout
, printoutForPrinting
, data
)
1402 m_pimpl
= wxPrintFactory::GetFactory()->
1403 CreatePrintPreview( printout
, printoutForPrinting
, data
);
1406 wxPrintPreview::~wxPrintPreview()
1410 // don't delete twice
1411 m_printPrintout
= NULL
;
1412 m_previewPrintout
= NULL
;
1413 m_previewBitmap
= NULL
;
1416 bool wxPrintPreview::SetCurrentPage(int pageNum
)
1418 return m_pimpl
->SetCurrentPage( pageNum
);
1421 int wxPrintPreview::GetCurrentPage() const
1423 return m_pimpl
->GetCurrentPage();
1426 void wxPrintPreview::SetPrintout(wxPrintout
*printout
)
1428 m_pimpl
->SetPrintout( printout
);
1431 wxPrintout
*wxPrintPreview::GetPrintout() const
1433 return m_pimpl
->GetPrintout();
1436 wxPrintout
*wxPrintPreview::GetPrintoutForPrinting() const
1438 return m_pimpl
->GetPrintoutForPrinting();
1441 void wxPrintPreview::SetFrame(wxFrame
*frame
)
1443 m_pimpl
->SetFrame( frame
);
1446 void wxPrintPreview::SetCanvas(wxPreviewCanvas
*canvas
)
1448 m_pimpl
->SetCanvas( canvas
);
1451 wxFrame
*wxPrintPreview::GetFrame() const
1453 return m_pimpl
->GetFrame();
1456 wxPreviewCanvas
*wxPrintPreview::GetCanvas() const
1458 return m_pimpl
->GetCanvas();
1461 bool wxPrintPreview::PaintPage(wxPreviewCanvas
*canvas
, wxDC
& dc
)
1463 return m_pimpl
->PaintPage( canvas
, dc
);
1466 bool wxPrintPreview::DrawBlankPage(wxPreviewCanvas
*canvas
, wxDC
& dc
)
1468 return m_pimpl
->DrawBlankPage( canvas
, dc
);
1471 void wxPrintPreview::AdjustScrollbars(wxPreviewCanvas
*canvas
)
1473 m_pimpl
->AdjustScrollbars( canvas
);
1476 bool wxPrintPreview::RenderPage(int pageNum
)
1478 return m_pimpl
->RenderPage( pageNum
);
1481 void wxPrintPreview::SetZoom(int percent
)
1483 m_pimpl
->SetZoom( percent
);
1486 wxPrintDialogData
& wxPrintPreview::GetPrintDialogData()
1488 return m_pimpl
->GetPrintDialogData();
1491 int wxPrintPreview::GetMaxPage() const
1493 return m_pimpl
->GetMaxPage();
1496 int wxPrintPreview::GetMinPage() const
1498 return m_pimpl
->GetMinPage();
1501 bool wxPrintPreview::Ok() const
1503 return m_pimpl
->Ok();
1506 void wxPrintPreview::SetOk(bool ok
)
1508 m_pimpl
->SetOk( ok
);
1511 bool wxPrintPreview::Print(bool interactive
)
1513 return m_pimpl
->Print( interactive
);
1516 void wxPrintPreview::DetermineScaling()
1518 m_pimpl
->DetermineScaling();
1522 #endif // wxUSE_PRINTING_ARCHITECTURE