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 wxPrintNativeDataBase
*wxNativePrintFactory::CreatePrintNativeData()
157 #if defined(__WXMSW__) && !defined(__WXUNIVERSAL__)
158 return new wxWindowsPrintNativeData
;
159 #elif defined(__WXMAC__)
160 return new wxMacPrintNativeData
;
162 return new wxPostScriptPrintNativeData
;
166 //----------------------------------------------------------------------------
167 // wxPrintNativeDataBase
168 //----------------------------------------------------------------------------
170 IMPLEMENT_ABSTRACT_CLASS(wxPrintNativeDataBase
, wxObject
)
172 wxPrintNativeDataBase::wxPrintNativeDataBase()
177 //----------------------------------------------------------------------------
179 //----------------------------------------------------------------------------
181 IMPLEMENT_CLASS(wxPrinterBase
, wxObject
)
183 wxPrinterBase::wxPrinterBase(wxPrintDialogData
*data
)
185 m_currentPrintout
= (wxPrintout
*) NULL
;
186 sm_abortWindow
= (wxWindow
*) NULL
;
189 m_printDialogData
= (*data
);
190 sm_lastError
= wxPRINTER_NO_ERROR
;
193 wxWindow
*wxPrinterBase::sm_abortWindow
= (wxWindow
*) NULL
;
194 bool wxPrinterBase::sm_abortIt
= false;
195 wxPrinterError
wxPrinterBase::sm_lastError
= wxPRINTER_NO_ERROR
;
197 wxPrinterBase::~wxPrinterBase()
201 wxWindow
*wxPrinterBase::CreateAbortWindow(wxWindow
*parent
, wxPrintout
* printout
)
203 wxPrintAbortDialog
*dialog
= new wxPrintAbortDialog(parent
, _("Printing ") , wxDefaultPosition
, wxDefaultSize
, wxDEFAULT_DIALOG_STYLE
);
205 wxBoxSizer
*button_sizer
= new wxBoxSizer( wxVERTICAL
);
206 button_sizer
->Add( new wxStaticText(dialog
, wxID_ANY
, _("Please wait while printing\n") + printout
->GetTitle() ), 0, wxALL
, 10 );
207 button_sizer
->Add( new wxButton( dialog
, wxID_CANCEL
, wxT("Cancel") ), 0, wxALL
| wxALIGN_CENTER
, 10 );
209 dialog
->SetAutoLayout( true );
210 dialog
->SetSizer( button_sizer
);
212 button_sizer
->Fit(dialog
);
213 button_sizer
->SetSizeHints (dialog
) ;
218 void wxPrinterBase::ReportError(wxWindow
*parent
, wxPrintout
*WXUNUSED(printout
), const wxString
& message
)
220 wxMessageBox(message
, _("Printing Error"), wxOK
, parent
);
223 wxPrintDialogData
& wxPrinterBase::GetPrintDialogData() const
225 return (wxPrintDialogData
&) m_printDialogData
;
228 //----------------------------------------------------------------------------
230 //----------------------------------------------------------------------------
232 IMPLEMENT_CLASS(wxPrinter
, wxPrinterBase
)
234 wxPrinter::wxPrinter(wxPrintDialogData
*data
)
236 m_pimpl
= wxPrintFactory::GetFactory()->CreatePrinter( data
);
239 wxPrinter::~wxPrinter()
244 wxWindow
*wxPrinter::CreateAbortWindow(wxWindow
*parent
, wxPrintout
*printout
)
246 return m_pimpl
->CreateAbortWindow( parent
, printout
);
249 void wxPrinter::ReportError(wxWindow
*parent
, wxPrintout
*printout
, const wxString
& message
)
251 m_pimpl
->ReportError( parent
, printout
, message
);
254 bool wxPrinter::Setup(wxWindow
*parent
)
256 return m_pimpl
->Setup( parent
);
259 bool wxPrinter::Print(wxWindow
*parent
, wxPrintout
*printout
, bool prompt
)
261 return m_pimpl
->Print( parent
, printout
, prompt
);
264 wxDC
* wxPrinter::PrintDialog(wxWindow
*parent
)
266 return m_pimpl
->PrintDialog( parent
);
269 wxPrintDialogData
& wxPrinter::GetPrintDialogData() const
271 return m_pimpl
->GetPrintDialogData();
274 // ---------------------------------------------------------------------------
275 // wxPrintDialogBase: the common dialog for printing.
276 // ---------------------------------------------------------------------------
278 IMPLEMENT_ABSTRACT_CLASS(wxPrintDialogBase
, wxObject
)
280 wxPrintDialogBase::wxPrintDialogBase(wxWindow
*parent
,
282 const wxString
&title
,
290 title
.empty() ? wxString(_("Print")) : title
,
298 // ---------------------------------------------------------------------------
299 // wxPrintDialog: the common dialog for printing.
300 // ---------------------------------------------------------------------------
302 IMPLEMENT_CLASS(wxPrintDialog
, wxObject
)
304 wxPrintDialog::wxPrintDialog(wxWindow
*parent
, wxPrintDialogData
* data
)
306 m_pimpl
= wxPrintFactory::GetFactory()->CreatePrintDialog( parent
, data
);
309 wxPrintDialog::wxPrintDialog(wxWindow
*parent
, wxPrintData
* data
)
311 m_pimpl
= wxPrintFactory::GetFactory()->CreatePrintDialog( parent
, data
);
314 wxPrintDialog::~wxPrintDialog()
319 int wxPrintDialog::ShowModal()
321 return m_pimpl
->ShowModal();
324 wxPrintDialogData
& wxPrintDialog::GetPrintDialogData()
326 return m_pimpl
->GetPrintDialogData();
329 wxPrintData
& wxPrintDialog::GetPrintData()
331 return m_pimpl
->GetPrintData();
333 wxDC
*wxPrintDialog::GetPrintDC()
335 return m_pimpl
->GetPrintDC();
338 //----------------------------------------------------------------------------
339 // wxPrintAbortDialog
340 //----------------------------------------------------------------------------
342 BEGIN_EVENT_TABLE(wxPrintAbortDialog
, wxDialog
)
343 EVT_BUTTON(wxID_CANCEL
, wxPrintAbortDialog::OnCancel
)
346 void wxPrintAbortDialog::OnCancel(wxCommandEvent
& WXUNUSED(event
))
348 wxPrinterBase::sm_abortIt
= true;
349 wxPrinterBase::sm_abortWindow
->Show(false);
350 wxPrinterBase::sm_abortWindow
->Close(true);
351 wxPrinterBase::sm_abortWindow
= (wxWindow
*) NULL
;
354 //----------------------------------------------------------------------------
356 //----------------------------------------------------------------------------
358 IMPLEMENT_ABSTRACT_CLASS(wxPrintout
, wxObject
)
360 wxPrintout::wxPrintout(const wxString
& title
)
362 m_printoutTitle
= title
;
363 m_printoutDC
= (wxDC
*) NULL
;
366 m_pageWidthPixels
= 0;
367 m_pageHeightPixels
= 0;
375 wxPrintout::~wxPrintout()
379 bool wxPrintout::OnBeginDocument(int WXUNUSED(startPage
), int WXUNUSED(endPage
))
381 return GetDC()->StartDoc(_("Printing ") + m_printoutTitle
);
384 void wxPrintout::OnEndDocument()
389 void wxPrintout::OnBeginPrinting()
393 void wxPrintout::OnEndPrinting()
397 bool wxPrintout::HasPage(int page
)
402 void wxPrintout::GetPageInfo(int *minPage
, int *maxPage
, int *fromPage
, int *toPage
)
410 //----------------------------------------------------------------------------
412 //----------------------------------------------------------------------------
414 IMPLEMENT_CLASS(wxPreviewCanvas
, wxWindow
)
416 BEGIN_EVENT_TABLE(wxPreviewCanvas
, wxScrolledWindow
)
417 EVT_PAINT(wxPreviewCanvas::OnPaint
)
418 EVT_CHAR(wxPreviewCanvas::OnChar
)
419 EVT_SYS_COLOUR_CHANGED(wxPreviewCanvas::OnSysColourChanged
)
422 // VZ: the current code doesn't refresh properly without
423 // wxFULL_REPAINT_ON_RESIZE, this must be fixed as otherwise we have
424 // really horrible flicker when resizing the preview frame, but without
425 // this style it simply doesn't work correctly at all...
426 wxPreviewCanvas::wxPreviewCanvas(wxPrintPreviewBase
*preview
, wxWindow
*parent
,
427 const wxPoint
& pos
, const wxSize
& size
, long style
, const wxString
& name
):
428 wxScrolledWindow(parent
, wxID_ANY
, pos
, size
, style
| wxFULL_REPAINT_ON_RESIZE
, name
)
430 m_printPreview
= preview
;
432 // The app workspace colour is always white, but we should have
433 // a contrast with the page.
434 wxSystemColour colourIndex
= wxSYS_COLOUR_3DDKSHADOW
;
436 wxSystemColour colourIndex
= wxSYS_COLOUR_APPWORKSPACE
;
438 SetBackgroundColour(wxSystemSettings::GetColour(colourIndex
));
440 SetScrollbars(10, 10, 100, 100);
443 wxPreviewCanvas::~wxPreviewCanvas()
447 void wxPreviewCanvas::OnPaint(wxPaintEvent
& WXUNUSED(event
))
454 if (!GetUpdateRegion().IsEmpty())
455 dc.SetClippingRegion( GetUpdateRegion() );
461 m_printPreview
->PaintPage(this, dc
);
465 // Responds to colour changes, and passes event on to children.
466 void wxPreviewCanvas::OnSysColourChanged(wxSysColourChangedEvent
& event
)
469 // The app workspace colour is always white, but we should have
470 // a contrast with the page.
471 wxSystemColour colourIndex
= wxSYS_COLOUR_3DDKSHADOW
;
473 wxSystemColour colourIndex
= wxSYS_COLOUR_APPWORKSPACE
;
475 SetBackgroundColour(wxSystemSettings::GetColour(colourIndex
));
478 // Propagate the event to the non-top-level children
479 wxWindow::OnSysColourChanged(event
);
482 void wxPreviewCanvas::OnChar(wxKeyEvent
&event
)
484 wxPreviewControlBar
* controlBar
= ((wxPreviewFrame
*) GetParent())->GetControlBar();
485 if (event
.GetKeyCode() == WXK_ESCAPE
)
487 ((wxPreviewFrame
*) GetParent())->Close(true);
490 else if (event
.GetKeyCode() == WXK_TAB
)
492 controlBar
->OnGoto();
495 else if (event
.GetKeyCode() == WXK_RETURN
)
497 controlBar
->OnPrint();
501 if (!event
.ControlDown())
507 switch(event
.GetKeyCode())
510 controlBar
->OnNext(); break;
512 controlBar
->OnPrevious(); break;
514 controlBar
->OnFirst(); break;
516 controlBar
->OnLast(); break;
522 //----------------------------------------------------------------------------
523 // wxPreviewControlBar
524 //----------------------------------------------------------------------------
526 IMPLEMENT_CLASS(wxPreviewControlBar
, wxWindow
)
528 BEGIN_EVENT_TABLE(wxPreviewControlBar
, wxPanel
)
529 EVT_BUTTON(wxID_PREVIEW_CLOSE
, wxPreviewControlBar::OnWindowClose
)
530 EVT_BUTTON(wxID_PREVIEW_PRINT
, wxPreviewControlBar::OnPrintButton
)
531 EVT_BUTTON(wxID_PREVIEW_PREVIOUS
, wxPreviewControlBar::OnPreviousButton
)
532 EVT_BUTTON(wxID_PREVIEW_NEXT
, wxPreviewControlBar::OnNextButton
)
533 EVT_BUTTON(wxID_PREVIEW_FIRST
, wxPreviewControlBar::OnFirstButton
)
534 EVT_BUTTON(wxID_PREVIEW_LAST
, wxPreviewControlBar::OnLastButton
)
535 EVT_BUTTON(wxID_PREVIEW_GOTO
, wxPreviewControlBar::OnGotoButton
)
536 EVT_CHOICE(wxID_PREVIEW_ZOOM
, wxPreviewControlBar::OnZoom
)
537 EVT_PAINT(wxPreviewControlBar::OnPaint
)
540 wxPreviewControlBar::wxPreviewControlBar(wxPrintPreviewBase
*preview
, long buttons
,
541 wxWindow
*parent
, const wxPoint
& pos
, const wxSize
& size
,
542 long style
, const wxString
& name
):
543 wxPanel(parent
, wxID_ANY
, pos
, size
, style
, name
)
545 m_printPreview
= preview
;
546 m_closeButton
= (wxButton
*) NULL
;
547 m_nextPageButton
= (wxButton
*) NULL
;
548 m_previousPageButton
= (wxButton
*) NULL
;
549 m_printButton
= (wxButton
*) NULL
;
550 m_zoomControl
= (wxChoice
*) NULL
;
551 m_buttonFlags
= buttons
;
554 wxPreviewControlBar::~wxPreviewControlBar()
558 void wxPreviewControlBar::OnPaint(wxPaintEvent
& WXUNUSED(event
))
564 dc
.SetPen(*wxBLACK_PEN
);
565 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
566 dc
.DrawLine( 0, h
-1, w
, h
-1 );
569 void wxPreviewControlBar::OnWindowClose(wxCommandEvent
& WXUNUSED(event
))
571 wxPreviewFrame
*frame
= (wxPreviewFrame
*)GetParent();
575 void wxPreviewControlBar::OnPrint(void)
577 wxPrintPreviewBase
*preview
= GetPrintPreview();
578 preview
->Print(true);
581 void wxPreviewControlBar::OnNext(void)
583 wxPrintPreviewBase
*preview
= GetPrintPreview();
586 int currentPage
= preview
->GetCurrentPage();
587 if ((preview
->GetMaxPage() > 0) &&
588 (currentPage
< preview
->GetMaxPage()) &&
589 preview
->GetPrintout()->HasPage(currentPage
+ 1))
591 preview
->SetCurrentPage(currentPage
+ 1);
596 void wxPreviewControlBar::OnPrevious(void)
598 wxPrintPreviewBase
*preview
= GetPrintPreview();
601 int currentPage
= preview
->GetCurrentPage();
602 if ((preview
->GetMinPage() > 0) &&
603 (currentPage
> preview
->GetMinPage()) &&
604 preview
->GetPrintout()->HasPage(currentPage
- 1))
606 preview
->SetCurrentPage(currentPage
- 1);
611 void wxPreviewControlBar::OnFirst(void)
613 wxPrintPreviewBase
*preview
= GetPrintPreview();
616 int currentPage
= preview
->GetMinPage();
617 if (preview
->GetPrintout()->HasPage(currentPage
))
619 preview
->SetCurrentPage(currentPage
);
624 void wxPreviewControlBar::OnLast(void)
626 wxPrintPreviewBase
*preview
= GetPrintPreview();
629 int currentPage
= preview
->GetMaxPage();
630 if (preview
->GetPrintout()->HasPage(currentPage
))
632 preview
->SetCurrentPage(currentPage
);
637 void wxPreviewControlBar::OnGoto(void)
639 wxPrintPreviewBase
*preview
= GetPrintPreview();
644 if (preview
->GetMinPage() > 0)
649 strPrompt
.Printf( _("Enter a page number between %d and %d:"),
650 preview
->GetMinPage(), preview
->GetMaxPage());
651 strPage
.Printf( wxT("%d"), preview
->GetCurrentPage() );
654 wxGetTextFromUser( strPrompt
, _("Goto Page"), strPage
, GetParent());
656 if ( strPage
.ToLong( ¤tPage
) )
657 if (preview
->GetPrintout()->HasPage(currentPage
))
659 preview
->SetCurrentPage(currentPage
);
665 void wxPreviewControlBar::OnZoom(wxCommandEvent
& WXUNUSED(event
))
667 int zoom
= GetZoomControl();
668 if (GetPrintPreview())
669 GetPrintPreview()->SetZoom(zoom
);
672 void wxPreviewControlBar::CreateButtons()
674 SetSize(0, 0, 400, 40);
676 wxBoxSizer
*item0
= new wxBoxSizer( wxHORIZONTAL
);
678 m_closeButton
= new wxButton( this, wxID_PREVIEW_CLOSE
, _("&Close"), wxDefaultPosition
, wxDefaultSize
, 0 );
679 item0
->Add( m_closeButton
, 0, wxALIGN_CENTRE
|wxALL
, 5 );
681 if (m_buttonFlags
& wxPREVIEW_PRINT
)
683 m_printButton
= new wxButton( this, wxID_PREVIEW_PRINT
, _("&Print..."), wxDefaultPosition
, wxDefaultSize
, 0 );
684 item0
->Add( m_printButton
, 0, wxALIGN_CENTRE
|wxALL
, 5 );
687 if (m_buttonFlags
& wxPREVIEW_FIRST
)
689 m_firstPageButton
= new wxButton( this, wxID_PREVIEW_FIRST
, _("|<<"), wxDefaultPosition
, wxDefaultSize
, wxBU_EXACTFIT
);
690 item0
->Add( m_firstPageButton
, 0, wxALIGN_CENTRE
|wxALL
, 5 );
693 if (m_buttonFlags
& wxPREVIEW_PREVIOUS
)
695 m_previousPageButton
= new wxButton( this, wxID_PREVIEW_PREVIOUS
, _("<<"), wxDefaultPosition
, wxDefaultSize
, wxBU_EXACTFIT
);
696 item0
->Add( m_previousPageButton
, 0, wxALIGN_CENTRE
|wxRIGHT
|wxTOP
|wxBOTTOM
, 5 );
699 if (m_buttonFlags
& wxPREVIEW_NEXT
)
701 m_nextPageButton
= new wxButton( this, wxID_PREVIEW_NEXT
, _(">>"), wxDefaultPosition
, wxDefaultSize
, wxBU_EXACTFIT
);
702 item0
->Add( m_nextPageButton
, 0, wxALIGN_CENTRE
|wxRIGHT
|wxTOP
|wxBOTTOM
, 5 );
705 if (m_buttonFlags
& wxPREVIEW_LAST
)
707 m_lastPageButton
= new wxButton( this, wxID_PREVIEW_LAST
, _(">>|"), wxDefaultPosition
, wxDefaultSize
, wxBU_EXACTFIT
);
708 item0
->Add( m_lastPageButton
, 0, wxALIGN_CENTRE
|wxRIGHT
|wxTOP
|wxBOTTOM
, 5 );
711 if (m_buttonFlags
& wxPREVIEW_GOTO
)
713 m_gotoPageButton
= new wxButton( this, wxID_PREVIEW_GOTO
, _("&Goto..."), wxDefaultPosition
, wxDefaultSize
, 0 );
714 item0
->Add( m_gotoPageButton
, 0, wxALIGN_CENTRE
|wxALL
, 5 );
717 if (m_buttonFlags
& wxPREVIEW_ZOOM
)
721 wxT("10%"), wxT("15%"), wxT("20%"), wxT("25%"), wxT("30%"), wxT("35%"), wxT("40%"), wxT("45%"), wxT("50%"), wxT("55%"),
722 wxT("60%"), wxT("65%"), wxT("70%"), wxT("75%"), wxT("80%"), wxT("85%"), wxT("90%"), wxT("95%"), wxT("100%"), wxT("110%"),
723 wxT("120%"), wxT("150%"), wxT("200%")
725 int n
= WXSIZEOF(choices
);
727 m_zoomControl
= new wxChoice( this, wxID_PREVIEW_ZOOM
, wxDefaultPosition
, wxSize(70,wxDefaultCoord
), n
, choices
, 0 );
728 item0
->Add( m_zoomControl
, 0, wxALIGN_CENTRE
|wxALL
, 5 );
729 SetZoomControl(m_printPreview
->GetZoom());
736 void wxPreviewControlBar::SetZoomControl(int zoom
)
740 int n
, count
= m_zoomControl
->GetCount();
742 for (n
=0; n
<count
; n
++)
744 if (m_zoomControl
->GetString(n
).BeforeFirst(wxT('%')).ToLong(&val
) &&
747 m_zoomControl
->SetSelection(n
);
752 m_zoomControl
->SetSelection(count
-1);
756 int wxPreviewControlBar::GetZoomControl()
758 if (m_zoomControl
&& (m_zoomControl
->GetStringSelection() != wxEmptyString
))
761 if (m_zoomControl
->GetStringSelection().BeforeFirst(wxT('%')).ToLong(&val
))
773 IMPLEMENT_CLASS(wxPreviewFrame
, wxFrame
)
775 BEGIN_EVENT_TABLE(wxPreviewFrame
, wxFrame
)
776 EVT_CLOSE(wxPreviewFrame::OnCloseWindow
)
779 wxPreviewFrame::wxPreviewFrame(wxPrintPreviewBase
*preview
, wxWindow
*parent
, const wxString
& title
,
780 const wxPoint
& pos
, const wxSize
& size
, long style
, const wxString
& name
):
781 wxFrame(parent
, wxID_ANY
, title
, pos
, size
, style
, name
)
783 m_printPreview
= preview
;
785 m_previewCanvas
= NULL
;
786 m_windowDisabler
= NULL
;
788 // Give the application icon
790 wxFrame
* topFrame
= wxDynamicCast(wxTheApp
->GetTopWindow(), wxFrame
);
792 SetIcon(topFrame
->GetIcon());
796 wxPreviewFrame::~wxPreviewFrame()
800 void wxPreviewFrame::OnCloseWindow(wxCloseEvent
& WXUNUSED(event
))
802 if (m_windowDisabler
)
803 delete m_windowDisabler
;
805 // Need to delete the printout and the print preview
806 wxPrintout
*printout
= m_printPreview
->GetPrintout();
810 m_printPreview
->SetPrintout(NULL
);
811 m_printPreview
->SetCanvas(NULL
);
812 m_printPreview
->SetFrame(NULL
);
814 delete m_printPreview
;
819 void wxPreviewFrame::Initialize()
827 m_printPreview
->SetCanvas(m_previewCanvas
);
828 m_printPreview
->SetFrame(this);
830 wxBoxSizer
*item0
= new wxBoxSizer( wxVERTICAL
);
832 item0
->Add( m_controlBar
, 0, wxGROW
|wxALIGN_CENTER_VERTICAL
, 5 );
833 item0
->Add( m_previewCanvas
, 1, wxGROW
|wxALIGN_CENTER_VERTICAL
, 5 );
835 SetAutoLayout( true );
838 m_windowDisabler
= new wxWindowDisabler(this);
842 m_printPreview
->AdjustScrollbars(m_previewCanvas
);
843 m_previewCanvas
->SetFocus();
844 m_controlBar
->SetFocus();
847 void wxPreviewFrame::CreateCanvas()
849 m_previewCanvas
= new wxPreviewCanvas(m_printPreview
, this);
852 void wxPreviewFrame::CreateControlBar()
854 long buttons
= wxPREVIEW_DEFAULT
;
855 if (m_printPreview
->GetPrintoutForPrinting())
856 buttons
|= wxPREVIEW_PRINT
;
858 m_controlBar
= new wxPreviewControlBar(m_printPreview
, buttons
, this, wxPoint(0, 0), wxSize(400, 40));
859 m_controlBar
->CreateButtons();
866 wxPrintPreviewBase::wxPrintPreviewBase(wxPrintout
*printout
,
867 wxPrintout
*printoutForPrinting
,
871 m_printDialogData
= (*data
);
873 Init(printout
, printoutForPrinting
);
876 wxPrintPreviewBase::wxPrintPreviewBase(wxPrintout
*printout
,
877 wxPrintout
*printoutForPrinting
,
878 wxPrintDialogData
*data
)
881 m_printDialogData
= (*data
);
883 Init(printout
, printoutForPrinting
);
886 void wxPrintPreviewBase::Init(wxPrintout
*printout
,
887 wxPrintout
*printoutForPrinting
)
890 m_previewPrintout
= printout
;
891 if (m_previewPrintout
)
892 m_previewPrintout
->SetIsPreview(true);
894 m_printPrintout
= printoutForPrinting
;
896 m_previewCanvas
= NULL
;
897 m_previewFrame
= NULL
;
898 m_previewBitmap
= NULL
;
905 m_printingPrepared
= false;
910 wxPrintPreviewBase::~wxPrintPreviewBase()
912 if (m_previewPrintout
)
913 delete m_previewPrintout
;
915 delete m_previewBitmap
;
917 delete m_printPrintout
;
920 bool wxPrintPreviewBase::SetCurrentPage(int pageNum
)
922 if (m_currentPage
== pageNum
)
925 m_currentPage
= pageNum
;
928 delete m_previewBitmap
;
929 m_previewBitmap
= NULL
;
934 AdjustScrollbars(m_previewCanvas
);
936 if (!RenderPage(pageNum
))
938 m_previewCanvas
->Refresh();
939 m_previewCanvas
->SetFocus();
944 int wxPrintPreviewBase::GetCurrentPage() const
945 { return m_currentPage
; };
946 void wxPrintPreviewBase::SetPrintout(wxPrintout
*printout
)
947 { m_previewPrintout
= printout
; };
948 wxPrintout
*wxPrintPreviewBase::GetPrintout() const
949 { return m_previewPrintout
; };
950 wxPrintout
*wxPrintPreviewBase::GetPrintoutForPrinting() const
951 { return m_printPrintout
; };
952 void wxPrintPreviewBase::SetFrame(wxFrame
*frame
)
953 { m_previewFrame
= frame
; };
954 void wxPrintPreviewBase::SetCanvas(wxPreviewCanvas
*canvas
)
955 { m_previewCanvas
= canvas
; };
956 wxFrame
*wxPrintPreviewBase::GetFrame() const
957 { return m_previewFrame
; }
958 wxPreviewCanvas
*wxPrintPreviewBase::GetCanvas() const
959 { return m_previewCanvas
; }
961 bool wxPrintPreviewBase::PaintPage(wxPreviewCanvas
*canvas
, wxDC
& dc
)
963 DrawBlankPage(canvas
, dc
);
965 if (!m_previewBitmap
)
966 if (!RenderPage(m_currentPage
))
969 if (!m_previewBitmap
)
975 int canvasWidth
, canvasHeight
;
976 canvas
->GetSize(&canvasWidth
, &canvasHeight
);
978 double zoomScale
= ((float)m_currentZoom
/(float)100);
979 double actualWidth
= (zoomScale
*m_pageWidth
*m_previewScale
);
980 // float actualHeight = (float)(zoomScale*m_pageHeight*m_previewScale);
982 int x
= (int) ((canvasWidth
- actualWidth
)/2.0);
983 if (x
< m_leftMargin
)
988 temp_dc
.SelectObject(*m_previewBitmap
);
990 dc
.Blit(x
, y
, m_previewBitmap
->GetWidth(), m_previewBitmap
->GetHeight(), &temp_dc
, 0, 0);
992 temp_dc
.SelectObject(wxNullBitmap
);
997 // Adjusts the scrollbars for the current scale
998 void wxPrintPreviewBase::AdjustScrollbars(wxPreviewCanvas
*canvas
)
1003 int canvasWidth
, canvasHeight
;
1004 canvas
->GetSize(&canvasWidth
, &canvasHeight
);
1006 double zoomScale
= ((float)m_currentZoom
/(float)100);
1007 double actualWidth
= (zoomScale
*m_pageWidth
*m_previewScale
);
1008 double actualHeight
= (zoomScale
*m_pageHeight
*m_previewScale
);
1010 // Set the scrollbars appropriately
1011 int totalWidth
= (int)(actualWidth
+ 2*m_leftMargin
);
1012 int totalHeight
= (int)(actualHeight
+ 2*m_topMargin
);
1013 int scrollUnitsX
= totalWidth
/10;
1014 int scrollUnitsY
= totalHeight
/10;
1015 wxSize virtualSize
= canvas
->GetVirtualSize();
1016 if (virtualSize
.GetWidth() != totalWidth
|| virtualSize
.GetHeight() != totalHeight
)
1017 canvas
->SetScrollbars(10, 10, scrollUnitsX
, scrollUnitsY
, 0, 0, true);
1020 bool wxPrintPreviewBase::RenderPage(int pageNum
)
1024 int canvasWidth
, canvasHeight
;
1026 if (!m_previewCanvas
)
1028 wxFAIL_MSG(_T("wxPrintPreviewBase::RenderPage: must use wxPrintPreviewBase::SetCanvas to let me know about the canvas!"));
1032 m_previewCanvas
->GetSize(&canvasWidth
, &canvasHeight
);
1034 double zoomScale
= (m_currentZoom
/100.0);
1035 int actualWidth
= (int)(zoomScale
*m_pageWidth
*m_previewScale
);
1036 int actualHeight
= (int)(zoomScale
*m_pageHeight
*m_previewScale
);
1038 if (!m_previewBitmap
)
1040 m_previewBitmap
= new wxBitmap((int)actualWidth
, (int)actualHeight
);
1041 if (!m_previewBitmap
|| !m_previewBitmap
->Ok())
1043 if (m_previewBitmap
) {
1044 delete m_previewBitmap
;
1045 m_previewBitmap
= NULL
;
1047 wxMessageBox(_("Sorry, not enough memory to create a preview."), _("Print Preview Failure"), wxOK
);
1052 wxMemoryDC memoryDC
;
1053 memoryDC
.SelectObject(*m_previewBitmap
);
1057 m_previewPrintout
->SetDC(&memoryDC
);
1058 m_previewPrintout
->SetPageSizePixels(m_pageWidth
, m_pageHeight
);
1060 // Need to delay OnPreparePrinting until here, so we have enough information.
1061 if (!m_printingPrepared
)
1063 m_previewPrintout
->OnPreparePrinting();
1065 m_previewPrintout
->GetPageInfo(&m_minPage
, &m_maxPage
, &selFrom
, &selTo
);
1066 m_printingPrepared
= true;
1069 m_previewPrintout
->OnBeginPrinting();
1071 if (!m_previewPrintout
->OnBeginDocument(m_printDialogData
.GetFromPage(), m_printDialogData
.GetToPage()))
1073 wxMessageBox(_("Could not start document preview."), _("Print Preview Failure"), wxOK
);
1075 memoryDC
.SelectObject(wxNullBitmap
);
1077 delete m_previewBitmap
;
1078 m_previewBitmap
= NULL
;
1082 m_previewPrintout
->OnPrintPage(pageNum
);
1083 m_previewPrintout
->OnEndDocument();
1084 m_previewPrintout
->OnEndPrinting();
1086 m_previewPrintout
->SetDC(NULL
);
1088 memoryDC
.SelectObject(wxNullBitmap
);
1093 status
= wxString::Format(_("Page %d of %d"), pageNum
, m_maxPage
);
1095 status
= wxString::Format(_("Page %d"), pageNum
);
1098 m_previewFrame
->SetStatusText(status
);
1105 bool wxPrintPreviewBase::DrawBlankPage(wxPreviewCanvas
*canvas
, wxDC
& dc
)
1107 int canvasWidth
, canvasHeight
;
1108 canvas
->GetSize(&canvasWidth
, &canvasHeight
);
1110 float zoomScale
= (float)((float)m_currentZoom
/(float)100);
1111 float actualWidth
= zoomScale
*m_pageWidth
*m_previewScale
;
1112 float actualHeight
= zoomScale
*m_pageHeight
*m_previewScale
;
1114 float x
= (float)((canvasWidth
- actualWidth
)/2.0);
1115 if (x
< m_leftMargin
)
1116 x
= (float)m_leftMargin
;
1117 float y
= (float)m_topMargin
;
1119 // Draw shadow, allowing for 1-pixel border AROUND the actual page
1120 int shadowOffset
= 4;
1121 dc
.SetPen(*wxBLACK_PEN
);
1122 dc
.SetBrush(*wxBLACK_BRUSH
);
1124 dc.DrawRectangle((int)(x-1 + shadowOffset), (int)(y-1 + shadowOffset), (int)(actualWidth+2), (int)(actualHeight+2));
1126 dc
.DrawRectangle((int)(x
+ shadowOffset
), (int)(y
+ actualHeight
+1), (int)(actualWidth
), shadowOffset
);
1127 dc
.DrawRectangle((int)(x
+ actualWidth
), (int)(y
+ shadowOffset
), shadowOffset
, (int)(actualHeight
));
1129 // Draw blank page allowing for 1-pixel border AROUND the actual page
1130 dc
.SetPen(*wxBLACK_PEN
);
1131 dc
.SetBrush(*wxWHITE_BRUSH
);
1134 wxRegion update_region = canvas->GetUpdateRegion();
1135 wxRect r = update_region.GetBox();
1137 printf( "x: %d y: %d w: %d h: %d.\n", (int)r.x, (int)r.y, (int)r.width, (int)r.height );
1140 dc
.DrawRectangle((int)(x
-2), (int)(y
-1), (int)(actualWidth
+3), (int)(actualHeight
+2));
1145 void wxPrintPreviewBase::SetZoom(int percent
)
1147 if (m_currentZoom
== percent
)
1150 m_currentZoom
= percent
;
1151 if (m_previewBitmap
)
1153 delete m_previewBitmap
;
1154 m_previewBitmap
= NULL
;
1157 if (m_previewCanvas
)
1159 AdjustScrollbars(m_previewCanvas
);
1160 RenderPage(m_currentPage
);
1161 ((wxScrolledWindow
*) m_previewCanvas
)->Scroll(0, 0);
1162 m_previewCanvas
->ClearBackground();
1163 m_previewCanvas
->Refresh();
1164 m_previewCanvas
->SetFocus();
1168 wxPrintDialogData
& wxPrintPreviewBase::GetPrintDialogData()
1170 return m_printDialogData
;
1173 int wxPrintPreviewBase::GetZoom() const
1174 { return m_currentZoom
; }
1175 int wxPrintPreviewBase::GetMaxPage() const
1176 { return m_maxPage
; }
1177 int wxPrintPreviewBase::GetMinPage() const
1178 { return m_minPage
; }
1179 bool wxPrintPreviewBase::Ok() const
1181 void wxPrintPreviewBase::SetOk(bool ok
)
1183 //----------------------------------------------------------------------------
1185 //----------------------------------------------------------------------------
1187 IMPLEMENT_CLASS(wxPrintPreview
, wxPrintPreviewBase
)
1189 wxPrintPreview::wxPrintPreview(wxPrintout
*printout
,
1190 wxPrintout
*printoutForPrinting
,
1191 wxPrintDialogData
*data
) :
1192 wxPrintPreviewBase( printout
, printoutForPrinting
, data
)
1194 m_pimpl
= wxPrintFactory::GetFactory()->
1195 CreatePrintPreview( printout
, printoutForPrinting
, data
);
1198 wxPrintPreview::wxPrintPreview(wxPrintout
*printout
,
1199 wxPrintout
*printoutForPrinting
,
1200 wxPrintData
*data
) :
1201 wxPrintPreviewBase( printout
, printoutForPrinting
, data
)
1203 m_pimpl
= wxPrintFactory::GetFactory()->
1204 CreatePrintPreview( printout
, printoutForPrinting
, data
);
1207 wxPrintPreview::~wxPrintPreview()
1211 // don't delete twice
1212 m_printPrintout
= NULL
;
1213 m_previewPrintout
= NULL
;
1214 m_previewBitmap
= NULL
;
1217 bool wxPrintPreview::SetCurrentPage(int pageNum
)
1219 return m_pimpl
->SetCurrentPage( pageNum
);
1222 int wxPrintPreview::GetCurrentPage() const
1224 return m_pimpl
->GetCurrentPage();
1227 void wxPrintPreview::SetPrintout(wxPrintout
*printout
)
1229 m_pimpl
->SetPrintout( printout
);
1232 wxPrintout
*wxPrintPreview::GetPrintout() const
1234 return m_pimpl
->GetPrintout();
1237 wxPrintout
*wxPrintPreview::GetPrintoutForPrinting() const
1239 return m_pimpl
->GetPrintoutForPrinting();
1242 void wxPrintPreview::SetFrame(wxFrame
*frame
)
1244 m_pimpl
->SetFrame( frame
);
1247 void wxPrintPreview::SetCanvas(wxPreviewCanvas
*canvas
)
1249 m_pimpl
->SetCanvas( canvas
);
1252 wxFrame
*wxPrintPreview::GetFrame() const
1254 return m_pimpl
->GetFrame();
1257 wxPreviewCanvas
*wxPrintPreview::GetCanvas() const
1259 return m_pimpl
->GetCanvas();
1262 bool wxPrintPreview::PaintPage(wxPreviewCanvas
*canvas
, wxDC
& dc
)
1264 return m_pimpl
->PaintPage( canvas
, dc
);
1267 bool wxPrintPreview::DrawBlankPage(wxPreviewCanvas
*canvas
, wxDC
& dc
)
1269 return m_pimpl
->DrawBlankPage( canvas
, dc
);
1272 void wxPrintPreview::AdjustScrollbars(wxPreviewCanvas
*canvas
)
1274 m_pimpl
->AdjustScrollbars( canvas
);
1277 bool wxPrintPreview::RenderPage(int pageNum
)
1279 return m_pimpl
->RenderPage( pageNum
);
1282 void wxPrintPreview::SetZoom(int percent
)
1284 m_pimpl
->SetZoom( percent
);
1287 wxPrintDialogData
& wxPrintPreview::GetPrintDialogData()
1289 return m_pimpl
->GetPrintDialogData();
1292 int wxPrintPreview::GetMaxPage() const
1294 return m_pimpl
->GetMaxPage();
1297 int wxPrintPreview::GetMinPage() const
1299 return m_pimpl
->GetMinPage();
1302 bool wxPrintPreview::Ok() const
1304 return m_pimpl
->Ok();
1307 void wxPrintPreview::SetOk(bool ok
)
1309 m_pimpl
->SetOk( ok
);
1312 bool wxPrintPreview::Print(bool interactive
)
1314 return m_pimpl
->Print( interactive
);
1317 void wxPrintPreview::DetermineScaling()
1319 m_pimpl
->DetermineScaling();
1323 #endif // wxUSE_PRINTING_ARCHITECTURE