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"
54 #include "wx/msw/private.h"
62 IMPLEMENT_CLASS(wxPrintPreviewBase
, wxObject
)
64 //----------------------------------------------------------------------------
66 //----------------------------------------------------------------------------
68 wxPrintFactory
*wxPrintFactory::m_factory
= NULL
;
70 void wxPrintFactory::SetPrintFactory( wxPrintFactory
*factory
)
72 if (wxPrintFactory::m_factory
)
73 delete wxPrintFactory::m_factory
;
75 wxPrintFactory::m_factory
= factory
;
78 wxPrintFactory
*wxPrintFactory::GetFactory()
80 if (!wxPrintFactory::m_factory
)
81 wxPrintFactory::m_factory
= new wxNativePrintFactory
;
83 return wxPrintFactory::m_factory
;
86 //----------------------------------------------------------------------------
87 // wxNativePrintFactory
88 //----------------------------------------------------------------------------
90 wxPrinterBase
*wxNativePrintFactory::CreatePrinter( wxPrintDialogData
*data
)
92 #if defined(__WXMSW__) && !defined(__WXUNIVERSAL__)
93 return new wxWindowsPrinter( data
);
94 #elif defined(__WXMAC__)
95 return new wxMacPrinter( data
);
96 #elif defined(__WXPM__)
97 return new wxOS2Printer( data
);
99 return new wxPostScriptPrinter( data
);
103 wxPrintPreviewBase
*wxNativePrintFactory::CreatePrintPreview( wxPrintout
*preview
,
104 wxPrintout
*printout
, wxPrintDialogData
*data
)
106 #if defined(__WXMSW__) && !defined(__WXUNIVERSAL__)
107 return new wxWindowsPrintPreview( preview
, printout
, data
);
108 #elif defined(__WXMAC__)
109 return new wxMacPrintPreview( preview
, printout
, data
);
110 #elif defined(__WXPM__)
111 return new wxOS2PrintPreview( preview
, printout
, data
);
113 return new wxPostScriptPrintPreview( preview
, printout
, data
);
117 wxPrintPreviewBase
*wxNativePrintFactory::CreatePrintPreview( wxPrintout
*preview
,
118 wxPrintout
*printout
, wxPrintData
*data
)
120 #if defined(__WXMSW__) && !defined(__WXUNIVERSAL__)
121 return new wxWindowsPrintPreview( preview
, printout
, data
);
122 #elif defined(__WXMAC__)
123 return new wxMacPrintPreview( preview
, printout
, data
);
124 #elif defined(__WXPM__)
125 return new wxOS2PrintPreview( preview
, printout
, data
);
127 return new wxPostScriptPrintPreview( preview
, printout
, data
);
131 wxPrintDialogBase
*wxNativePrintFactory::CreatePrintDialog( wxWindow
*parent
,
132 wxPrintDialogData
*data
)
134 #if defined(__WXMSW__) && !defined(__WXUNIVERSAL__)
135 return new wxWindowsPrintDialog( parent
, data
);
136 #elif defined(__WXMAC__)
137 return new wxMacPrintDialog( parent
, data
);
139 return new wxGenericPrintDialog( parent
, data
);
143 wxPrintDialogBase
*wxNativePrintFactory::CreatePrintDialog( wxWindow
*parent
,
146 #if defined(__WXMSW__) && !defined(__WXUNIVERSAL__)
147 return new wxWindowsPrintDialog( parent
, data
);
148 #elif defined(__WXMAC__)
149 return new wxMacPrintDialog( parent
, data
);
151 return new wxGenericPrintDialog( parent
, data
);
155 bool wxNativePrintFactory::HasPrintSetupDialog()
157 #if defined(__WXMSW__) && !defined(__WXUNIVERSAL__)
159 #elif defined(__WXMAC__)
162 // Only here do we need to provide the print setup
163 // dialog ourselves, the other platforms either have
164 // none, don't make it accessible or let you configure
165 // the printer from the wxPrintDialog anyway.
171 wxDialog
*wxNativePrintFactory::CreatePrintSetupDialog( wxWindow
*parent
, wxPrintData
*data
)
173 #if defined(__WXMSW__) && !defined(__WXUNIVERSAL__)
177 #elif defined(__WXMAC__)
182 // Only here do we need to provide the print setup
183 // dialog ourselves, the other platforms either have
184 // none, don't make it accessible or let you configure
185 // the printer from the wxPrintDialog anyway.
186 return new wxGenericPrintSetupDialog( parent
, data
);
190 bool wxNativePrintFactory::HasOwnPrintToFile()
192 // Only relevant for PostScript and here the
193 // setup dialog provides no "print to file"
194 // option. In the GNOME setup dialog, the
195 // setup dialog has its own print to file.
199 bool wxNativePrintFactory::HasPrinterLine()
201 // Only relevant for PostScript for now
205 wxString
wxNativePrintFactory::CreatePrinterLine()
207 // Only relevant for PostScript for now
209 // We should query "lpstat -d" here
210 return _("Generic PostScript");
213 bool wxNativePrintFactory::HasStatusLine()
215 // Only relevant for PostScript for now
219 wxString
wxNativePrintFactory::CreateStatusLine()
221 // Only relevant for PostScript for now
223 // We should query "lpstat -r" or "lpstat -p" here
227 wxPrintNativeDataBase
*wxNativePrintFactory::CreatePrintNativeData()
229 #if defined(__WXMSW__) && !defined(__WXUNIVERSAL__)
230 return new wxWindowsPrintNativeData
;
231 #elif defined(__WXMAC__)
232 return new wxMacPrintNativeData
;
234 return new wxPostScriptPrintNativeData
;
238 //----------------------------------------------------------------------------
239 // wxPrintNativeDataBase
240 //----------------------------------------------------------------------------
242 IMPLEMENT_ABSTRACT_CLASS(wxPrintNativeDataBase
, wxObject
)
244 wxPrintNativeDataBase::wxPrintNativeDataBase()
249 //----------------------------------------------------------------------------
251 //----------------------------------------------------------------------------
253 IMPLEMENT_CLASS(wxPrinterBase
, wxObject
)
255 wxPrinterBase::wxPrinterBase(wxPrintDialogData
*data
)
257 m_currentPrintout
= (wxPrintout
*) NULL
;
258 sm_abortWindow
= (wxWindow
*) NULL
;
261 m_printDialogData
= (*data
);
262 sm_lastError
= wxPRINTER_NO_ERROR
;
265 wxWindow
*wxPrinterBase::sm_abortWindow
= (wxWindow
*) NULL
;
266 bool wxPrinterBase::sm_abortIt
= false;
267 wxPrinterError
wxPrinterBase::sm_lastError
= wxPRINTER_NO_ERROR
;
269 wxPrinterBase::~wxPrinterBase()
273 wxWindow
*wxPrinterBase::CreateAbortWindow(wxWindow
*parent
, wxPrintout
* printout
)
275 wxPrintAbortDialog
*dialog
= new wxPrintAbortDialog(parent
, _("Printing ") , wxDefaultPosition
, wxDefaultSize
, wxDEFAULT_DIALOG_STYLE
);
277 wxBoxSizer
*button_sizer
= new wxBoxSizer( wxVERTICAL
);
278 button_sizer
->Add( new wxStaticText(dialog
, wxID_ANY
, _("Please wait while printing\n") + printout
->GetTitle() ), 0, wxALL
, 10 );
279 button_sizer
->Add( new wxButton( dialog
, wxID_CANCEL
, wxT("Cancel") ), 0, wxALL
| wxALIGN_CENTER
, 10 );
281 dialog
->SetAutoLayout( true );
282 dialog
->SetSizer( button_sizer
);
284 button_sizer
->Fit(dialog
);
285 button_sizer
->SetSizeHints (dialog
) ;
290 void wxPrinterBase::ReportError(wxWindow
*parent
, wxPrintout
*WXUNUSED(printout
), const wxString
& message
)
292 wxMessageBox(message
, _("Printing Error"), wxOK
, parent
);
295 wxPrintDialogData
& wxPrinterBase::GetPrintDialogData() const
297 return (wxPrintDialogData
&) m_printDialogData
;
300 //----------------------------------------------------------------------------
302 //----------------------------------------------------------------------------
304 IMPLEMENT_CLASS(wxPrinter
, wxPrinterBase
)
306 wxPrinter::wxPrinter(wxPrintDialogData
*data
)
308 m_pimpl
= wxPrintFactory::GetFactory()->CreatePrinter( data
);
311 wxPrinter::~wxPrinter()
316 wxWindow
*wxPrinter::CreateAbortWindow(wxWindow
*parent
, wxPrintout
*printout
)
318 return m_pimpl
->CreateAbortWindow( parent
, printout
);
321 void wxPrinter::ReportError(wxWindow
*parent
, wxPrintout
*printout
, const wxString
& message
)
323 m_pimpl
->ReportError( parent
, printout
, message
);
326 bool wxPrinter::Setup(wxWindow
*parent
)
328 return m_pimpl
->Setup( parent
);
331 bool wxPrinter::Print(wxWindow
*parent
, wxPrintout
*printout
, bool prompt
)
333 return m_pimpl
->Print( parent
, printout
, prompt
);
336 wxDC
* wxPrinter::PrintDialog(wxWindow
*parent
)
338 return m_pimpl
->PrintDialog( parent
);
341 wxPrintDialogData
& wxPrinter::GetPrintDialogData() const
343 return m_pimpl
->GetPrintDialogData();
346 // ---------------------------------------------------------------------------
347 // wxPrintDialogBase: the common dialog for printing.
348 // ---------------------------------------------------------------------------
350 IMPLEMENT_ABSTRACT_CLASS(wxPrintDialogBase
, wxObject
)
352 wxPrintDialogBase::wxPrintDialogBase(wxWindow
*parent
,
354 const wxString
&title
,
362 title
.empty() ? wxString(_("Print")) : title
,
370 // ---------------------------------------------------------------------------
371 // wxPrintDialog: the common dialog for printing.
372 // ---------------------------------------------------------------------------
374 IMPLEMENT_CLASS(wxPrintDialog
, wxObject
)
376 wxPrintDialog::wxPrintDialog(wxWindow
*parent
, wxPrintDialogData
* data
)
378 m_pimpl
= wxPrintFactory::GetFactory()->CreatePrintDialog( parent
, data
);
381 wxPrintDialog::wxPrintDialog(wxWindow
*parent
, wxPrintData
* data
)
383 m_pimpl
= wxPrintFactory::GetFactory()->CreatePrintDialog( parent
, data
);
386 wxPrintDialog::~wxPrintDialog()
391 int wxPrintDialog::ShowModal()
393 return m_pimpl
->ShowModal();
396 wxPrintDialogData
& wxPrintDialog::GetPrintDialogData()
398 return m_pimpl
->GetPrintDialogData();
401 wxPrintData
& wxPrintDialog::GetPrintData()
403 return m_pimpl
->GetPrintData();
405 wxDC
*wxPrintDialog::GetPrintDC()
407 return m_pimpl
->GetPrintDC();
410 //----------------------------------------------------------------------------
411 // wxPrintAbortDialog
412 //----------------------------------------------------------------------------
414 BEGIN_EVENT_TABLE(wxPrintAbortDialog
, wxDialog
)
415 EVT_BUTTON(wxID_CANCEL
, wxPrintAbortDialog::OnCancel
)
418 void wxPrintAbortDialog::OnCancel(wxCommandEvent
& WXUNUSED(event
))
420 wxPrinterBase::sm_abortIt
= true;
421 wxPrinterBase::sm_abortWindow
->Show(false);
422 wxPrinterBase::sm_abortWindow
->Close(true);
423 wxPrinterBase::sm_abortWindow
= (wxWindow
*) NULL
;
426 //----------------------------------------------------------------------------
428 //----------------------------------------------------------------------------
430 IMPLEMENT_ABSTRACT_CLASS(wxPrintout
, wxObject
)
432 wxPrintout::wxPrintout(const wxString
& title
)
434 m_printoutTitle
= title
;
435 m_printoutDC
= (wxDC
*) NULL
;
438 m_pageWidthPixels
= 0;
439 m_pageHeightPixels
= 0;
447 wxPrintout::~wxPrintout()
451 bool wxPrintout::OnBeginDocument(int WXUNUSED(startPage
), int WXUNUSED(endPage
))
453 return GetDC()->StartDoc(_("Printing ") + m_printoutTitle
);
456 void wxPrintout::OnEndDocument()
461 void wxPrintout::OnBeginPrinting()
465 void wxPrintout::OnEndPrinting()
469 bool wxPrintout::HasPage(int page
)
474 void wxPrintout::GetPageInfo(int *minPage
, int *maxPage
, int *fromPage
, int *toPage
)
482 //----------------------------------------------------------------------------
484 //----------------------------------------------------------------------------
486 IMPLEMENT_CLASS(wxPreviewCanvas
, wxWindow
)
488 BEGIN_EVENT_TABLE(wxPreviewCanvas
, wxScrolledWindow
)
489 EVT_PAINT(wxPreviewCanvas::OnPaint
)
490 EVT_CHAR(wxPreviewCanvas::OnChar
)
491 EVT_SYS_COLOUR_CHANGED(wxPreviewCanvas::OnSysColourChanged
)
494 // VZ: the current code doesn't refresh properly without
495 // wxFULL_REPAINT_ON_RESIZE, this must be fixed as otherwise we have
496 // really horrible flicker when resizing the preview frame, but without
497 // this style it simply doesn't work correctly at all...
498 wxPreviewCanvas::wxPreviewCanvas(wxPrintPreviewBase
*preview
, wxWindow
*parent
,
499 const wxPoint
& pos
, const wxSize
& size
, long style
, const wxString
& name
):
500 wxScrolledWindow(parent
, wxID_ANY
, pos
, size
, style
| wxFULL_REPAINT_ON_RESIZE
, name
)
502 m_printPreview
= preview
;
504 // The app workspace colour is always white, but we should have
505 // a contrast with the page.
506 wxSystemColour colourIndex
= wxSYS_COLOUR_3DDKSHADOW
;
508 wxSystemColour colourIndex
= wxSYS_COLOUR_APPWORKSPACE
;
510 SetBackgroundColour(wxSystemSettings::GetColour(colourIndex
));
512 SetScrollbars(10, 10, 100, 100);
515 wxPreviewCanvas::~wxPreviewCanvas()
519 void wxPreviewCanvas::OnPaint(wxPaintEvent
& WXUNUSED(event
))
526 if (!GetUpdateRegion().IsEmpty())
527 dc.SetClippingRegion( GetUpdateRegion() );
533 m_printPreview
->PaintPage(this, dc
);
537 // Responds to colour changes, and passes event on to children.
538 void wxPreviewCanvas::OnSysColourChanged(wxSysColourChangedEvent
& event
)
541 // The app workspace colour is always white, but we should have
542 // a contrast with the page.
543 wxSystemColour colourIndex
= wxSYS_COLOUR_3DDKSHADOW
;
545 wxSystemColour colourIndex
= wxSYS_COLOUR_APPWORKSPACE
;
547 SetBackgroundColour(wxSystemSettings::GetColour(colourIndex
));
550 // Propagate the event to the non-top-level children
551 wxWindow::OnSysColourChanged(event
);
554 void wxPreviewCanvas::OnChar(wxKeyEvent
&event
)
556 wxPreviewControlBar
* controlBar
= ((wxPreviewFrame
*) GetParent())->GetControlBar();
557 if (event
.GetKeyCode() == WXK_ESCAPE
)
559 ((wxPreviewFrame
*) GetParent())->Close(true);
562 else if (event
.GetKeyCode() == WXK_TAB
)
564 controlBar
->OnGoto();
567 else if (event
.GetKeyCode() == WXK_RETURN
)
569 controlBar
->OnPrint();
573 if (!event
.ControlDown())
579 switch(event
.GetKeyCode())
582 controlBar
->OnNext(); break;
584 controlBar
->OnPrevious(); break;
586 controlBar
->OnFirst(); break;
588 controlBar
->OnLast(); break;
594 //----------------------------------------------------------------------------
595 // wxPreviewControlBar
596 //----------------------------------------------------------------------------
598 IMPLEMENT_CLASS(wxPreviewControlBar
, wxWindow
)
600 BEGIN_EVENT_TABLE(wxPreviewControlBar
, wxPanel
)
601 EVT_BUTTON(wxID_PREVIEW_CLOSE
, wxPreviewControlBar::OnWindowClose
)
602 EVT_BUTTON(wxID_PREVIEW_PRINT
, wxPreviewControlBar::OnPrintButton
)
603 EVT_BUTTON(wxID_PREVIEW_PREVIOUS
, wxPreviewControlBar::OnPreviousButton
)
604 EVT_BUTTON(wxID_PREVIEW_NEXT
, wxPreviewControlBar::OnNextButton
)
605 EVT_BUTTON(wxID_PREVIEW_FIRST
, wxPreviewControlBar::OnFirstButton
)
606 EVT_BUTTON(wxID_PREVIEW_LAST
, wxPreviewControlBar::OnLastButton
)
607 EVT_BUTTON(wxID_PREVIEW_GOTO
, wxPreviewControlBar::OnGotoButton
)
608 EVT_CHOICE(wxID_PREVIEW_ZOOM
, wxPreviewControlBar::OnZoom
)
609 EVT_PAINT(wxPreviewControlBar::OnPaint
)
612 wxPreviewControlBar::wxPreviewControlBar(wxPrintPreviewBase
*preview
, long buttons
,
613 wxWindow
*parent
, const wxPoint
& pos
, const wxSize
& size
,
614 long style
, const wxString
& name
):
615 wxPanel(parent
, wxID_ANY
, pos
, size
, style
, name
)
617 m_printPreview
= preview
;
618 m_closeButton
= (wxButton
*) NULL
;
619 m_nextPageButton
= (wxButton
*) NULL
;
620 m_previousPageButton
= (wxButton
*) NULL
;
621 m_printButton
= (wxButton
*) NULL
;
622 m_zoomControl
= (wxChoice
*) NULL
;
623 m_buttonFlags
= buttons
;
626 wxPreviewControlBar::~wxPreviewControlBar()
630 void wxPreviewControlBar::OnPaint(wxPaintEvent
& WXUNUSED(event
))
636 dc
.SetPen(*wxBLACK_PEN
);
637 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
638 dc
.DrawLine( 0, h
-1, w
, h
-1 );
641 void wxPreviewControlBar::OnWindowClose(wxCommandEvent
& WXUNUSED(event
))
643 wxPreviewFrame
*frame
= (wxPreviewFrame
*)GetParent();
647 void wxPreviewControlBar::OnPrint(void)
649 wxPrintPreviewBase
*preview
= GetPrintPreview();
650 preview
->Print(true);
653 void wxPreviewControlBar::OnNext(void)
655 wxPrintPreviewBase
*preview
= GetPrintPreview();
658 int currentPage
= preview
->GetCurrentPage();
659 if ((preview
->GetMaxPage() > 0) &&
660 (currentPage
< preview
->GetMaxPage()) &&
661 preview
->GetPrintout()->HasPage(currentPage
+ 1))
663 preview
->SetCurrentPage(currentPage
+ 1);
668 void wxPreviewControlBar::OnPrevious(void)
670 wxPrintPreviewBase
*preview
= GetPrintPreview();
673 int currentPage
= preview
->GetCurrentPage();
674 if ((preview
->GetMinPage() > 0) &&
675 (currentPage
> preview
->GetMinPage()) &&
676 preview
->GetPrintout()->HasPage(currentPage
- 1))
678 preview
->SetCurrentPage(currentPage
- 1);
683 void wxPreviewControlBar::OnFirst(void)
685 wxPrintPreviewBase
*preview
= GetPrintPreview();
688 int currentPage
= preview
->GetMinPage();
689 if (preview
->GetPrintout()->HasPage(currentPage
))
691 preview
->SetCurrentPage(currentPage
);
696 void wxPreviewControlBar::OnLast(void)
698 wxPrintPreviewBase
*preview
= GetPrintPreview();
701 int currentPage
= preview
->GetMaxPage();
702 if (preview
->GetPrintout()->HasPage(currentPage
))
704 preview
->SetCurrentPage(currentPage
);
709 void wxPreviewControlBar::OnGoto(void)
711 wxPrintPreviewBase
*preview
= GetPrintPreview();
716 if (preview
->GetMinPage() > 0)
721 strPrompt
.Printf( _("Enter a page number between %d and %d:"),
722 preview
->GetMinPage(), preview
->GetMaxPage());
723 strPage
.Printf( wxT("%d"), preview
->GetCurrentPage() );
726 wxGetTextFromUser( strPrompt
, _("Goto Page"), strPage
, GetParent());
728 if ( strPage
.ToLong( ¤tPage
) )
729 if (preview
->GetPrintout()->HasPage(currentPage
))
731 preview
->SetCurrentPage(currentPage
);
737 void wxPreviewControlBar::OnZoom(wxCommandEvent
& WXUNUSED(event
))
739 int zoom
= GetZoomControl();
740 if (GetPrintPreview())
741 GetPrintPreview()->SetZoom(zoom
);
744 void wxPreviewControlBar::CreateButtons()
746 SetSize(0, 0, 400, 40);
748 wxBoxSizer
*item0
= new wxBoxSizer( wxHORIZONTAL
);
750 m_closeButton
= new wxButton( this, wxID_PREVIEW_CLOSE
, _("&Close"), wxDefaultPosition
, wxDefaultSize
, 0 );
751 item0
->Add( m_closeButton
, 0, wxALIGN_CENTRE
|wxALL
, 5 );
753 if (m_buttonFlags
& wxPREVIEW_PRINT
)
755 m_printButton
= new wxButton( this, wxID_PREVIEW_PRINT
, _("&Print..."), wxDefaultPosition
, wxDefaultSize
, 0 );
756 item0
->Add( m_printButton
, 0, wxALIGN_CENTRE
|wxALL
, 5 );
759 if (m_buttonFlags
& wxPREVIEW_FIRST
)
761 m_firstPageButton
= new wxButton( this, wxID_PREVIEW_FIRST
, _("|<<"), wxDefaultPosition
, wxDefaultSize
, wxBU_EXACTFIT
);
762 item0
->Add( m_firstPageButton
, 0, wxALIGN_CENTRE
|wxALL
, 5 );
765 if (m_buttonFlags
& wxPREVIEW_PREVIOUS
)
767 m_previousPageButton
= new wxButton( this, wxID_PREVIEW_PREVIOUS
, _("<<"), wxDefaultPosition
, wxDefaultSize
, wxBU_EXACTFIT
);
768 item0
->Add( m_previousPageButton
, 0, wxALIGN_CENTRE
|wxRIGHT
|wxTOP
|wxBOTTOM
, 5 );
771 if (m_buttonFlags
& wxPREVIEW_NEXT
)
773 m_nextPageButton
= new wxButton( this, wxID_PREVIEW_NEXT
, _(">>"), wxDefaultPosition
, wxDefaultSize
, wxBU_EXACTFIT
);
774 item0
->Add( m_nextPageButton
, 0, wxALIGN_CENTRE
|wxRIGHT
|wxTOP
|wxBOTTOM
, 5 );
777 if (m_buttonFlags
& wxPREVIEW_LAST
)
779 m_lastPageButton
= new wxButton( this, wxID_PREVIEW_LAST
, _(">>|"), wxDefaultPosition
, wxDefaultSize
, wxBU_EXACTFIT
);
780 item0
->Add( m_lastPageButton
, 0, wxALIGN_CENTRE
|wxRIGHT
|wxTOP
|wxBOTTOM
, 5 );
783 if (m_buttonFlags
& wxPREVIEW_GOTO
)
785 m_gotoPageButton
= new wxButton( this, wxID_PREVIEW_GOTO
, _("&Goto..."), wxDefaultPosition
, wxDefaultSize
, 0 );
786 item0
->Add( m_gotoPageButton
, 0, wxALIGN_CENTRE
|wxALL
, 5 );
789 if (m_buttonFlags
& wxPREVIEW_ZOOM
)
793 wxT("10%"), wxT("15%"), wxT("20%"), wxT("25%"), wxT("30%"), wxT("35%"), wxT("40%"), wxT("45%"), wxT("50%"), wxT("55%"),
794 wxT("60%"), wxT("65%"), wxT("70%"), wxT("75%"), wxT("80%"), wxT("85%"), wxT("90%"), wxT("95%"), wxT("100%"), wxT("110%"),
795 wxT("120%"), wxT("150%"), wxT("200%")
797 int n
= WXSIZEOF(choices
);
799 m_zoomControl
= new wxChoice( this, wxID_PREVIEW_ZOOM
, wxDefaultPosition
, wxSize(70,wxDefaultCoord
), n
, choices
, 0 );
800 item0
->Add( m_zoomControl
, 0, wxALIGN_CENTRE
|wxALL
, 5 );
801 SetZoomControl(m_printPreview
->GetZoom());
808 void wxPreviewControlBar::SetZoomControl(int zoom
)
812 int n
, count
= m_zoomControl
->GetCount();
814 for (n
=0; n
<count
; n
++)
816 if (m_zoomControl
->GetString(n
).BeforeFirst(wxT('%')).ToLong(&val
) &&
819 m_zoomControl
->SetSelection(n
);
824 m_zoomControl
->SetSelection(count
-1);
828 int wxPreviewControlBar::GetZoomControl()
830 if (m_zoomControl
&& (m_zoomControl
->GetStringSelection() != wxEmptyString
))
833 if (m_zoomControl
->GetStringSelection().BeforeFirst(wxT('%')).ToLong(&val
))
845 IMPLEMENT_CLASS(wxPreviewFrame
, wxFrame
)
847 BEGIN_EVENT_TABLE(wxPreviewFrame
, wxFrame
)
848 EVT_CLOSE(wxPreviewFrame::OnCloseWindow
)
851 wxPreviewFrame::wxPreviewFrame(wxPrintPreviewBase
*preview
, wxWindow
*parent
, const wxString
& title
,
852 const wxPoint
& pos
, const wxSize
& size
, long style
, const wxString
& name
):
853 wxFrame(parent
, wxID_ANY
, title
, pos
, size
, style
, name
)
855 m_printPreview
= preview
;
857 m_previewCanvas
= NULL
;
858 m_windowDisabler
= NULL
;
860 // Give the application icon
862 wxFrame
* topFrame
= wxDynamicCast(wxTheApp
->GetTopWindow(), wxFrame
);
864 SetIcon(topFrame
->GetIcon());
868 wxPreviewFrame::~wxPreviewFrame()
872 void wxPreviewFrame::OnCloseWindow(wxCloseEvent
& WXUNUSED(event
))
874 if (m_windowDisabler
)
875 delete m_windowDisabler
;
877 // Need to delete the printout and the print preview
878 wxPrintout
*printout
= m_printPreview
->GetPrintout();
882 m_printPreview
->SetPrintout(NULL
);
883 m_printPreview
->SetCanvas(NULL
);
884 m_printPreview
->SetFrame(NULL
);
886 delete m_printPreview
;
891 void wxPreviewFrame::Initialize()
899 m_printPreview
->SetCanvas(m_previewCanvas
);
900 m_printPreview
->SetFrame(this);
902 wxBoxSizer
*item0
= new wxBoxSizer( wxVERTICAL
);
904 item0
->Add( m_controlBar
, 0, wxGROW
|wxALIGN_CENTER_VERTICAL
, 5 );
905 item0
->Add( m_previewCanvas
, 1, wxGROW
|wxALIGN_CENTER_VERTICAL
, 5 );
907 SetAutoLayout( true );
910 m_windowDisabler
= new wxWindowDisabler(this);
914 m_printPreview
->AdjustScrollbars(m_previewCanvas
);
915 m_previewCanvas
->SetFocus();
916 m_controlBar
->SetFocus();
919 void wxPreviewFrame::CreateCanvas()
921 m_previewCanvas
= new wxPreviewCanvas(m_printPreview
, this);
924 void wxPreviewFrame::CreateControlBar()
926 long buttons
= wxPREVIEW_DEFAULT
;
927 if (m_printPreview
->GetPrintoutForPrinting())
928 buttons
|= wxPREVIEW_PRINT
;
930 m_controlBar
= new wxPreviewControlBar(m_printPreview
, buttons
, this, wxPoint(0, 0), wxSize(400, 40));
931 m_controlBar
->CreateButtons();
938 wxPrintPreviewBase::wxPrintPreviewBase(wxPrintout
*printout
,
939 wxPrintout
*printoutForPrinting
,
943 m_printDialogData
= (*data
);
945 Init(printout
, printoutForPrinting
);
948 wxPrintPreviewBase::wxPrintPreviewBase(wxPrintout
*printout
,
949 wxPrintout
*printoutForPrinting
,
950 wxPrintDialogData
*data
)
953 m_printDialogData
= (*data
);
955 Init(printout
, printoutForPrinting
);
958 void wxPrintPreviewBase::Init(wxPrintout
*printout
,
959 wxPrintout
*printoutForPrinting
)
962 m_previewPrintout
= printout
;
963 if (m_previewPrintout
)
964 m_previewPrintout
->SetIsPreview(true);
966 m_printPrintout
= printoutForPrinting
;
968 m_previewCanvas
= NULL
;
969 m_previewFrame
= NULL
;
970 m_previewBitmap
= NULL
;
977 m_printingPrepared
= false;
982 wxPrintPreviewBase::~wxPrintPreviewBase()
984 if (m_previewPrintout
)
985 delete m_previewPrintout
;
987 delete m_previewBitmap
;
989 delete m_printPrintout
;
992 bool wxPrintPreviewBase::SetCurrentPage(int pageNum
)
994 if (m_currentPage
== pageNum
)
997 m_currentPage
= pageNum
;
1000 delete m_previewBitmap
;
1001 m_previewBitmap
= NULL
;
1004 if (m_previewCanvas
)
1006 AdjustScrollbars(m_previewCanvas
);
1008 if (!RenderPage(pageNum
))
1010 m_previewCanvas
->Refresh();
1011 m_previewCanvas
->SetFocus();
1016 int wxPrintPreviewBase::GetCurrentPage() const
1017 { return m_currentPage
; };
1018 void wxPrintPreviewBase::SetPrintout(wxPrintout
*printout
)
1019 { m_previewPrintout
= printout
; };
1020 wxPrintout
*wxPrintPreviewBase::GetPrintout() const
1021 { return m_previewPrintout
; };
1022 wxPrintout
*wxPrintPreviewBase::GetPrintoutForPrinting() const
1023 { return m_printPrintout
; };
1024 void wxPrintPreviewBase::SetFrame(wxFrame
*frame
)
1025 { m_previewFrame
= frame
; };
1026 void wxPrintPreviewBase::SetCanvas(wxPreviewCanvas
*canvas
)
1027 { m_previewCanvas
= canvas
; };
1028 wxFrame
*wxPrintPreviewBase::GetFrame() const
1029 { return m_previewFrame
; }
1030 wxPreviewCanvas
*wxPrintPreviewBase::GetCanvas() const
1031 { return m_previewCanvas
; }
1033 bool wxPrintPreviewBase::PaintPage(wxPreviewCanvas
*canvas
, wxDC
& dc
)
1035 DrawBlankPage(canvas
, dc
);
1037 if (!m_previewBitmap
)
1038 if (!RenderPage(m_currentPage
))
1041 if (!m_previewBitmap
)
1047 int canvasWidth
, canvasHeight
;
1048 canvas
->GetSize(&canvasWidth
, &canvasHeight
);
1050 double zoomScale
= ((float)m_currentZoom
/(float)100);
1051 double actualWidth
= (zoomScale
*m_pageWidth
*m_previewScale
);
1052 // float actualHeight = (float)(zoomScale*m_pageHeight*m_previewScale);
1054 int x
= (int) ((canvasWidth
- actualWidth
)/2.0);
1055 if (x
< m_leftMargin
)
1057 int y
= m_topMargin
;
1060 temp_dc
.SelectObject(*m_previewBitmap
);
1062 dc
.Blit(x
, y
, m_previewBitmap
->GetWidth(), m_previewBitmap
->GetHeight(), &temp_dc
, 0, 0);
1064 temp_dc
.SelectObject(wxNullBitmap
);
1069 // Adjusts the scrollbars for the current scale
1070 void wxPrintPreviewBase::AdjustScrollbars(wxPreviewCanvas
*canvas
)
1075 int canvasWidth
, canvasHeight
;
1076 canvas
->GetSize(&canvasWidth
, &canvasHeight
);
1078 double zoomScale
= ((float)m_currentZoom
/(float)100);
1079 double actualWidth
= (zoomScale
*m_pageWidth
*m_previewScale
);
1080 double actualHeight
= (zoomScale
*m_pageHeight
*m_previewScale
);
1082 // Set the scrollbars appropriately
1083 int totalWidth
= (int)(actualWidth
+ 2*m_leftMargin
);
1084 int totalHeight
= (int)(actualHeight
+ 2*m_topMargin
);
1085 int scrollUnitsX
= totalWidth
/10;
1086 int scrollUnitsY
= totalHeight
/10;
1087 wxSize virtualSize
= canvas
->GetVirtualSize();
1088 if (virtualSize
.GetWidth() != totalWidth
|| virtualSize
.GetHeight() != totalHeight
)
1089 canvas
->SetScrollbars(10, 10, scrollUnitsX
, scrollUnitsY
, 0, 0, true);
1092 bool wxPrintPreviewBase::RenderPage(int pageNum
)
1096 int canvasWidth
, canvasHeight
;
1098 if (!m_previewCanvas
)
1100 wxFAIL_MSG(_T("wxPrintPreviewBase::RenderPage: must use wxPrintPreviewBase::SetCanvas to let me know about the canvas!"));
1104 m_previewCanvas
->GetSize(&canvasWidth
, &canvasHeight
);
1106 double zoomScale
= (m_currentZoom
/100.0);
1107 int actualWidth
= (int)(zoomScale
*m_pageWidth
*m_previewScale
);
1108 int actualHeight
= (int)(zoomScale
*m_pageHeight
*m_previewScale
);
1110 if (!m_previewBitmap
)
1112 m_previewBitmap
= new wxBitmap((int)actualWidth
, (int)actualHeight
);
1113 if (!m_previewBitmap
|| !m_previewBitmap
->Ok())
1115 if (m_previewBitmap
) {
1116 delete m_previewBitmap
;
1117 m_previewBitmap
= NULL
;
1119 wxMessageBox(_("Sorry, not enough memory to create a preview."), _("Print Preview Failure"), wxOK
);
1124 wxMemoryDC memoryDC
;
1125 memoryDC
.SelectObject(*m_previewBitmap
);
1129 m_previewPrintout
->SetDC(&memoryDC
);
1130 m_previewPrintout
->SetPageSizePixels(m_pageWidth
, m_pageHeight
);
1132 // Need to delay OnPreparePrinting until here, so we have enough information.
1133 if (!m_printingPrepared
)
1135 m_previewPrintout
->OnPreparePrinting();
1137 m_previewPrintout
->GetPageInfo(&m_minPage
, &m_maxPage
, &selFrom
, &selTo
);
1138 m_printingPrepared
= true;
1141 m_previewPrintout
->OnBeginPrinting();
1143 if (!m_previewPrintout
->OnBeginDocument(m_printDialogData
.GetFromPage(), m_printDialogData
.GetToPage()))
1145 wxMessageBox(_("Could not start document preview."), _("Print Preview Failure"), wxOK
);
1147 memoryDC
.SelectObject(wxNullBitmap
);
1149 delete m_previewBitmap
;
1150 m_previewBitmap
= NULL
;
1154 m_previewPrintout
->OnPrintPage(pageNum
);
1155 m_previewPrintout
->OnEndDocument();
1156 m_previewPrintout
->OnEndPrinting();
1158 m_previewPrintout
->SetDC(NULL
);
1160 memoryDC
.SelectObject(wxNullBitmap
);
1165 status
= wxString::Format(_("Page %d of %d"), pageNum
, m_maxPage
);
1167 status
= wxString::Format(_("Page %d"), pageNum
);
1170 m_previewFrame
->SetStatusText(status
);
1177 bool wxPrintPreviewBase::DrawBlankPage(wxPreviewCanvas
*canvas
, wxDC
& dc
)
1179 int canvasWidth
, canvasHeight
;
1180 canvas
->GetSize(&canvasWidth
, &canvasHeight
);
1182 float zoomScale
= (float)((float)m_currentZoom
/(float)100);
1183 float actualWidth
= zoomScale
*m_pageWidth
*m_previewScale
;
1184 float actualHeight
= zoomScale
*m_pageHeight
*m_previewScale
;
1186 float x
= (float)((canvasWidth
- actualWidth
)/2.0);
1187 if (x
< m_leftMargin
)
1188 x
= (float)m_leftMargin
;
1189 float y
= (float)m_topMargin
;
1191 // Draw shadow, allowing for 1-pixel border AROUND the actual page
1192 int shadowOffset
= 4;
1193 dc
.SetPen(*wxBLACK_PEN
);
1194 dc
.SetBrush(*wxBLACK_BRUSH
);
1196 dc.DrawRectangle((int)(x-1 + shadowOffset), (int)(y-1 + shadowOffset), (int)(actualWidth+2), (int)(actualHeight+2));
1198 dc
.DrawRectangle((int)(x
+ shadowOffset
), (int)(y
+ actualHeight
+1), (int)(actualWidth
), shadowOffset
);
1199 dc
.DrawRectangle((int)(x
+ actualWidth
), (int)(y
+ shadowOffset
), shadowOffset
, (int)(actualHeight
));
1201 // Draw blank page allowing for 1-pixel border AROUND the actual page
1202 dc
.SetPen(*wxBLACK_PEN
);
1203 dc
.SetBrush(*wxWHITE_BRUSH
);
1206 wxRegion update_region = canvas->GetUpdateRegion();
1207 wxRect r = update_region.GetBox();
1209 printf( "x: %d y: %d w: %d h: %d.\n", (int)r.x, (int)r.y, (int)r.width, (int)r.height );
1212 dc
.DrawRectangle((int)(x
-2), (int)(y
-1), (int)(actualWidth
+3), (int)(actualHeight
+2));
1217 void wxPrintPreviewBase::SetZoom(int percent
)
1219 if (m_currentZoom
== percent
)
1222 m_currentZoom
= percent
;
1223 if (m_previewBitmap
)
1225 delete m_previewBitmap
;
1226 m_previewBitmap
= NULL
;
1229 if (m_previewCanvas
)
1231 AdjustScrollbars(m_previewCanvas
);
1232 RenderPage(m_currentPage
);
1233 ((wxScrolledWindow
*) m_previewCanvas
)->Scroll(0, 0);
1234 m_previewCanvas
->ClearBackground();
1235 m_previewCanvas
->Refresh();
1236 m_previewCanvas
->SetFocus();
1240 wxPrintDialogData
& wxPrintPreviewBase::GetPrintDialogData()
1242 return m_printDialogData
;
1245 int wxPrintPreviewBase::GetZoom() const
1246 { return m_currentZoom
; }
1247 int wxPrintPreviewBase::GetMaxPage() const
1248 { return m_maxPage
; }
1249 int wxPrintPreviewBase::GetMinPage() const
1250 { return m_minPage
; }
1251 bool wxPrintPreviewBase::Ok() const
1253 void wxPrintPreviewBase::SetOk(bool ok
)
1255 //----------------------------------------------------------------------------
1257 //----------------------------------------------------------------------------
1259 IMPLEMENT_CLASS(wxPrintPreview
, wxPrintPreviewBase
)
1261 wxPrintPreview::wxPrintPreview(wxPrintout
*printout
,
1262 wxPrintout
*printoutForPrinting
,
1263 wxPrintDialogData
*data
) :
1264 wxPrintPreviewBase( printout
, printoutForPrinting
, data
)
1266 m_pimpl
= wxPrintFactory::GetFactory()->
1267 CreatePrintPreview( printout
, printoutForPrinting
, data
);
1270 wxPrintPreview::wxPrintPreview(wxPrintout
*printout
,
1271 wxPrintout
*printoutForPrinting
,
1272 wxPrintData
*data
) :
1273 wxPrintPreviewBase( printout
, printoutForPrinting
, data
)
1275 m_pimpl
= wxPrintFactory::GetFactory()->
1276 CreatePrintPreview( printout
, printoutForPrinting
, data
);
1279 wxPrintPreview::~wxPrintPreview()
1283 // don't delete twice
1284 m_printPrintout
= NULL
;
1285 m_previewPrintout
= NULL
;
1286 m_previewBitmap
= NULL
;
1289 bool wxPrintPreview::SetCurrentPage(int pageNum
)
1291 return m_pimpl
->SetCurrentPage( pageNum
);
1294 int wxPrintPreview::GetCurrentPage() const
1296 return m_pimpl
->GetCurrentPage();
1299 void wxPrintPreview::SetPrintout(wxPrintout
*printout
)
1301 m_pimpl
->SetPrintout( printout
);
1304 wxPrintout
*wxPrintPreview::GetPrintout() const
1306 return m_pimpl
->GetPrintout();
1309 wxPrintout
*wxPrintPreview::GetPrintoutForPrinting() const
1311 return m_pimpl
->GetPrintoutForPrinting();
1314 void wxPrintPreview::SetFrame(wxFrame
*frame
)
1316 m_pimpl
->SetFrame( frame
);
1319 void wxPrintPreview::SetCanvas(wxPreviewCanvas
*canvas
)
1321 m_pimpl
->SetCanvas( canvas
);
1324 wxFrame
*wxPrintPreview::GetFrame() const
1326 return m_pimpl
->GetFrame();
1329 wxPreviewCanvas
*wxPrintPreview::GetCanvas() const
1331 return m_pimpl
->GetCanvas();
1334 bool wxPrintPreview::PaintPage(wxPreviewCanvas
*canvas
, wxDC
& dc
)
1336 return m_pimpl
->PaintPage( canvas
, dc
);
1339 bool wxPrintPreview::DrawBlankPage(wxPreviewCanvas
*canvas
, wxDC
& dc
)
1341 return m_pimpl
->DrawBlankPage( canvas
, dc
);
1344 void wxPrintPreview::AdjustScrollbars(wxPreviewCanvas
*canvas
)
1346 m_pimpl
->AdjustScrollbars( canvas
);
1349 bool wxPrintPreview::RenderPage(int pageNum
)
1351 return m_pimpl
->RenderPage( pageNum
);
1354 void wxPrintPreview::SetZoom(int percent
)
1356 m_pimpl
->SetZoom( percent
);
1359 wxPrintDialogData
& wxPrintPreview::GetPrintDialogData()
1361 return m_pimpl
->GetPrintDialogData();
1364 int wxPrintPreview::GetMaxPage() const
1366 return m_pimpl
->GetMaxPage();
1369 int wxPrintPreview::GetMinPage() const
1371 return m_pimpl
->GetMinPage();
1374 bool wxPrintPreview::Ok() const
1376 return m_pimpl
->Ok();
1379 void wxPrintPreview::SetOk(bool ok
)
1381 m_pimpl
->SetOk( ok
);
1384 bool wxPrintPreview::Print(bool interactive
)
1386 return m_pimpl
->Print( interactive
);
1389 void wxPrintPreview::DetermineScaling()
1391 m_pimpl
->DetermineScaling();
1395 #endif // wxUSE_PRINTING_ARCHITECTURE