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
);
97 return new wxPostScriptPrinter( data
);
101 wxPrintPreviewBase
*wxNativePrintFactory::CreatePrintPreview( wxPrintout
*preview
,
102 wxPrintout
*printout
, wxPrintDialogData
*data
)
104 #if defined(__WXMSW__) && !defined(__WXUNIVERSAL__)
105 return new wxWindowsPrintPreview( preview
, printout
, data
);
106 #elif defined(__WXMAC__)
107 return new wxMacPrintPreview( preview
, printout
, data
);
109 return new wxPostScriptPrintPreview( preview
, printout
, data
);
113 wxPrintPreviewBase
*wxNativePrintFactory::CreatePrintPreview( wxPrintout
*preview
,
114 wxPrintout
*printout
, wxPrintData
*data
)
116 #if defined(__WXMSW__) && !defined(__WXUNIVERSAL__)
117 return new wxWindowsPrintPreview( preview
, printout
, data
);
118 #elif defined(__WXMAC__)
119 return new wxMacPrintPreview( preview
, printout
, data
);
121 return new wxPostScriptPrintPreview( preview
, printout
, data
);
125 wxPrintDialogBase
*wxNativePrintFactory::CreatePrintDialog( wxWindow
*parent
,
126 wxPrintDialogData
*data
)
128 #if defined(__WXMSW__) && !defined(__WXUNIVERSAL__)
129 return new wxWindowsPrintDialog( parent
, data
);
130 #elif defined(__WXMAC__)
131 return new wxMacPrintDialog( parent
, data
);
133 return new wxGenericPrintDialog( parent
, data
);
137 wxPrintDialogBase
*wxNativePrintFactory::CreatePrintDialog( wxWindow
*parent
,
140 #if defined(__WXMSW__) && !defined(__WXUNIVERSAL__)
141 return new wxWindowsPrintDialog( parent
, data
);
142 #elif defined(__WXMAC__)
143 return new wxMacPrintDialog( parent
, data
);
145 return new wxGenericPrintDialog( parent
, data
);
149 //----------------------------------------------------------------------------
151 //----------------------------------------------------------------------------
153 IMPLEMENT_CLASS(wxPrinterBase
, wxObject
)
155 wxPrinterBase::wxPrinterBase(wxPrintDialogData
*data
)
157 m_currentPrintout
= (wxPrintout
*) NULL
;
158 sm_abortWindow
= (wxWindow
*) NULL
;
161 m_printDialogData
= (*data
);
162 sm_lastError
= wxPRINTER_NO_ERROR
;
165 wxWindow
*wxPrinterBase::sm_abortWindow
= (wxWindow
*) NULL
;
166 bool wxPrinterBase::sm_abortIt
= false;
167 wxPrinterError
wxPrinterBase::sm_lastError
= wxPRINTER_NO_ERROR
;
169 wxPrinterBase::~wxPrinterBase()
173 wxWindow
*wxPrinterBase::CreateAbortWindow(wxWindow
*parent
, wxPrintout
* printout
)
175 wxPrintAbortDialog
*dialog
= new wxPrintAbortDialog(parent
, _("Printing ") , wxDefaultPosition
, wxDefaultSize
, wxDEFAULT_DIALOG_STYLE
);
177 wxBoxSizer
*button_sizer
= new wxBoxSizer( wxVERTICAL
);
178 button_sizer
->Add( new wxStaticText(dialog
, wxID_ANY
, _("Please wait while printing\n") + printout
->GetTitle() ), 0, wxALL
, 10 );
179 button_sizer
->Add( new wxButton( dialog
, wxID_CANCEL
, wxT("Cancel") ), 0, wxALL
| wxALIGN_CENTER
, 10 );
181 dialog
->SetAutoLayout( true );
182 dialog
->SetSizer( button_sizer
);
184 button_sizer
->Fit(dialog
);
185 button_sizer
->SetSizeHints (dialog
) ;
190 void wxPrinterBase::ReportError(wxWindow
*parent
, wxPrintout
*WXUNUSED(printout
), const wxString
& message
)
192 wxMessageBox(message
, _("Printing Error"), wxOK
, parent
);
195 wxPrintDialogData
& wxPrinterBase::GetPrintDialogData() const
197 return (wxPrintDialogData
&) m_printDialogData
;
200 //----------------------------------------------------------------------------
202 //----------------------------------------------------------------------------
204 IMPLEMENT_CLASS(wxPrinter
, wxPrinterBase
)
206 wxPrinter::wxPrinter(wxPrintDialogData
*data
)
208 m_pimpl
= wxPrintFactory::GetFactory()->CreatePrinter( data
);
211 wxPrinter::~wxPrinter()
216 wxWindow
*wxPrinter::CreateAbortWindow(wxWindow
*parent
, wxPrintout
*printout
)
218 return m_pimpl
->CreateAbortWindow( parent
, printout
);
221 void wxPrinter::ReportError(wxWindow
*parent
, wxPrintout
*printout
, const wxString
& message
)
223 m_pimpl
->ReportError( parent
, printout
, message
);
226 bool wxPrinter::Setup(wxWindow
*parent
)
228 return m_pimpl
->Setup( parent
);
231 bool wxPrinter::Print(wxWindow
*parent
, wxPrintout
*printout
, bool prompt
)
233 return m_pimpl
->Print( parent
, printout
, prompt
);
236 wxDC
* wxPrinter::PrintDialog(wxWindow
*parent
)
238 return m_pimpl
->PrintDialog( parent
);
241 wxPrintDialogData
& wxPrinter::GetPrintDialogData() const
243 return m_pimpl
->GetPrintDialogData();
246 // ---------------------------------------------------------------------------
247 // wxPrintDialogBase: the common dialog for printing.
248 // ---------------------------------------------------------------------------
250 IMPLEMENT_ABSTRACT_CLASS(wxPrintDialogBase
, wxObject
)
252 wxPrintDialogBase::wxPrintDialogBase(wxWindow
*parent
, wxWindowID id
,
253 const wxString
&title
, const wxPoint
&pos
, const wxSize
&size
, long style
) :
254 wxDialog( parent
, id
, title
, pos
, size
, style
)
258 // ---------------------------------------------------------------------------
259 // wxPrintDialog: the common dialog for printing.
260 // ---------------------------------------------------------------------------
262 IMPLEMENT_CLASS(wxPrintDialog
, wxObject
)
264 wxPrintDialog::wxPrintDialog(wxWindow
*parent
, wxPrintDialogData
* data
)
266 m_pimpl
= wxPrintFactory::GetFactory()->CreatePrintDialog( parent
, data
);
269 wxPrintDialog::wxPrintDialog(wxWindow
*parent
, wxPrintData
* data
)
271 m_pimpl
= wxPrintFactory::GetFactory()->CreatePrintDialog( parent
, data
);
274 wxPrintDialog::~wxPrintDialog()
279 int wxPrintDialog::ShowModal()
281 return m_pimpl
->ShowModal();
284 wxPrintDialogData
& wxPrintDialog::GetPrintDialogData()
286 return m_pimpl
->GetPrintDialogData();
289 wxPrintData
& wxPrintDialog::GetPrintData()
291 return m_pimpl
->GetPrintData();
293 wxDC
*wxPrintDialog::GetPrintDC()
295 return m_pimpl
->GetPrintDC();
298 //----------------------------------------------------------------------------
299 // wxPrintAbortDialog
300 //----------------------------------------------------------------------------
302 BEGIN_EVENT_TABLE(wxPrintAbortDialog
, wxDialog
)
303 EVT_BUTTON(wxID_CANCEL
, wxPrintAbortDialog::OnCancel
)
306 void wxPrintAbortDialog::OnCancel(wxCommandEvent
& WXUNUSED(event
))
308 wxPrinterBase::sm_abortIt
= true;
309 wxPrinterBase::sm_abortWindow
->Show(false);
310 wxPrinterBase::sm_abortWindow
->Close(true);
311 wxPrinterBase::sm_abortWindow
= (wxWindow
*) NULL
;
314 //----------------------------------------------------------------------------
316 //----------------------------------------------------------------------------
318 IMPLEMENT_ABSTRACT_CLASS(wxPrintout
, wxObject
)
320 wxPrintout::wxPrintout(const wxString
& title
)
322 m_printoutTitle
= title
;
323 m_printoutDC
= (wxDC
*) NULL
;
326 m_pageWidthPixels
= 0;
327 m_pageHeightPixels
= 0;
335 wxPrintout::~wxPrintout()
339 bool wxPrintout::OnBeginDocument(int WXUNUSED(startPage
), int WXUNUSED(endPage
))
341 return GetDC()->StartDoc(_("Printing ") + m_printoutTitle
);
344 void wxPrintout::OnEndDocument()
349 void wxPrintout::OnBeginPrinting()
353 void wxPrintout::OnEndPrinting()
357 bool wxPrintout::HasPage(int page
)
362 void wxPrintout::GetPageInfo(int *minPage
, int *maxPage
, int *fromPage
, int *toPage
)
370 //----------------------------------------------------------------------------
372 //----------------------------------------------------------------------------
374 IMPLEMENT_CLASS(wxPreviewCanvas
, wxWindow
)
376 BEGIN_EVENT_TABLE(wxPreviewCanvas
, wxScrolledWindow
)
377 EVT_PAINT(wxPreviewCanvas::OnPaint
)
378 EVT_CHAR(wxPreviewCanvas::OnChar
)
379 EVT_SYS_COLOUR_CHANGED(wxPreviewCanvas::OnSysColourChanged
)
382 // VZ: the current code doesn't refresh properly without
383 // wxFULL_REPAINT_ON_RESIZE, this must be fixed as otherwise we have
384 // really horrible flicker when resizing the preview frame, but without
385 // this style it simply doesn't work correctly at all...
386 wxPreviewCanvas::wxPreviewCanvas(wxPrintPreviewBase
*preview
, wxWindow
*parent
,
387 const wxPoint
& pos
, const wxSize
& size
, long style
, const wxString
& name
):
388 wxScrolledWindow(parent
, wxID_ANY
, pos
, size
, style
| wxFULL_REPAINT_ON_RESIZE
, name
)
390 m_printPreview
= preview
;
392 // The app workspace colour is always white, but we should have
393 // a contrast with the page.
394 wxSystemColour colourIndex
= wxSYS_COLOUR_3DDKSHADOW
;
396 wxSystemColour colourIndex
= wxSYS_COLOUR_APPWORKSPACE
;
398 SetBackgroundColour(wxSystemSettings::GetColour(colourIndex
));
400 SetScrollbars(10, 10, 100, 100);
403 wxPreviewCanvas::~wxPreviewCanvas()
407 void wxPreviewCanvas::OnPaint(wxPaintEvent
& WXUNUSED(event
))
414 if (!GetUpdateRegion().IsEmpty())
415 dc.SetClippingRegion( GetUpdateRegion() );
421 m_printPreview
->PaintPage(this, dc
);
425 // Responds to colour changes, and passes event on to children.
426 void wxPreviewCanvas::OnSysColourChanged(wxSysColourChangedEvent
& event
)
429 // The app workspace colour is always white, but we should have
430 // a contrast with the page.
431 wxSystemColour colourIndex
= wxSYS_COLOUR_3DDKSHADOW
;
433 wxSystemColour colourIndex
= wxSYS_COLOUR_APPWORKSPACE
;
435 SetBackgroundColour(wxSystemSettings::GetColour(colourIndex
));
438 // Propagate the event to the non-top-level children
439 wxWindow::OnSysColourChanged(event
);
442 void wxPreviewCanvas::OnChar(wxKeyEvent
&event
)
444 wxPreviewControlBar
* controlBar
= ((wxPreviewFrame
*) GetParent())->GetControlBar();
445 if (event
.GetKeyCode() == WXK_ESCAPE
)
447 ((wxPreviewFrame
*) GetParent())->Close(true);
450 else if (event
.GetKeyCode() == WXK_TAB
)
452 controlBar
->OnGoto();
455 else if (event
.GetKeyCode() == WXK_RETURN
)
457 controlBar
->OnPrint();
461 if (!event
.ControlDown())
467 switch(event
.GetKeyCode())
470 controlBar
->OnNext(); break;
472 controlBar
->OnPrevious(); break;
474 controlBar
->OnFirst(); break;
476 controlBar
->OnLast(); break;
482 //----------------------------------------------------------------------------
483 // wxPreviewControlBar
484 //----------------------------------------------------------------------------
486 IMPLEMENT_CLASS(wxPreviewControlBar
, wxWindow
)
488 BEGIN_EVENT_TABLE(wxPreviewControlBar
, wxPanel
)
489 EVT_BUTTON(wxID_PREVIEW_CLOSE
, wxPreviewControlBar::OnWindowClose
)
490 EVT_BUTTON(wxID_PREVIEW_PRINT
, wxPreviewControlBar::OnPrintButton
)
491 EVT_BUTTON(wxID_PREVIEW_PREVIOUS
, wxPreviewControlBar::OnPreviousButton
)
492 EVT_BUTTON(wxID_PREVIEW_NEXT
, wxPreviewControlBar::OnNextButton
)
493 EVT_BUTTON(wxID_PREVIEW_FIRST
, wxPreviewControlBar::OnFirstButton
)
494 EVT_BUTTON(wxID_PREVIEW_LAST
, wxPreviewControlBar::OnLastButton
)
495 EVT_BUTTON(wxID_PREVIEW_GOTO
, wxPreviewControlBar::OnGotoButton
)
496 EVT_CHOICE(wxID_PREVIEW_ZOOM
, wxPreviewControlBar::OnZoom
)
497 EVT_PAINT(wxPreviewControlBar::OnPaint
)
500 wxPreviewControlBar::wxPreviewControlBar(wxPrintPreviewBase
*preview
, long buttons
,
501 wxWindow
*parent
, const wxPoint
& pos
, const wxSize
& size
,
502 long style
, const wxString
& name
):
503 wxPanel(parent
, wxID_ANY
, pos
, size
, style
, name
)
505 m_printPreview
= preview
;
506 m_closeButton
= (wxButton
*) NULL
;
507 m_nextPageButton
= (wxButton
*) NULL
;
508 m_previousPageButton
= (wxButton
*) NULL
;
509 m_printButton
= (wxButton
*) NULL
;
510 m_zoomControl
= (wxChoice
*) NULL
;
511 m_buttonFlags
= buttons
;
514 wxPreviewControlBar::~wxPreviewControlBar()
518 void wxPreviewControlBar::OnPaint(wxPaintEvent
& WXUNUSED(event
))
524 dc
.SetPen(*wxBLACK_PEN
);
525 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
526 dc
.DrawLine( 0, h
-1, w
, h
-1 );
529 void wxPreviewControlBar::OnWindowClose(wxCommandEvent
& WXUNUSED(event
))
531 wxPreviewFrame
*frame
= (wxPreviewFrame
*)GetParent();
535 void wxPreviewControlBar::OnPrint(void)
537 wxPrintPreviewBase
*preview
= GetPrintPreview();
538 preview
->Print(true);
541 void wxPreviewControlBar::OnNext(void)
543 wxPrintPreviewBase
*preview
= GetPrintPreview();
546 int currentPage
= preview
->GetCurrentPage();
547 if ((preview
->GetMaxPage() > 0) &&
548 (currentPage
< preview
->GetMaxPage()) &&
549 preview
->GetPrintout()->HasPage(currentPage
+ 1))
551 preview
->SetCurrentPage(currentPage
+ 1);
556 void wxPreviewControlBar::OnPrevious(void)
558 wxPrintPreviewBase
*preview
= GetPrintPreview();
561 int currentPage
= preview
->GetCurrentPage();
562 if ((preview
->GetMinPage() > 0) &&
563 (currentPage
> preview
->GetMinPage()) &&
564 preview
->GetPrintout()->HasPage(currentPage
- 1))
566 preview
->SetCurrentPage(currentPage
- 1);
571 void wxPreviewControlBar::OnFirst(void)
573 wxPrintPreviewBase
*preview
= GetPrintPreview();
576 int currentPage
= preview
->GetMinPage();
577 if (preview
->GetPrintout()->HasPage(currentPage
))
579 preview
->SetCurrentPage(currentPage
);
584 void wxPreviewControlBar::OnLast(void)
586 wxPrintPreviewBase
*preview
= GetPrintPreview();
589 int currentPage
= preview
->GetMaxPage();
590 if (preview
->GetPrintout()->HasPage(currentPage
))
592 preview
->SetCurrentPage(currentPage
);
597 void wxPreviewControlBar::OnGoto(void)
599 wxPrintPreviewBase
*preview
= GetPrintPreview();
604 if (preview
->GetMinPage() > 0)
609 strPrompt
.Printf( _("Enter a page number between %d and %d:"),
610 preview
->GetMinPage(), preview
->GetMaxPage());
611 strPage
.Printf( wxT("%d"), preview
->GetCurrentPage() );
614 wxGetTextFromUser( strPrompt
, _("Goto Page"), strPage
, GetParent());
616 if ( strPage
.ToLong( ¤tPage
) )
617 if (preview
->GetPrintout()->HasPage(currentPage
))
619 preview
->SetCurrentPage(currentPage
);
625 void wxPreviewControlBar::OnZoom(wxCommandEvent
& WXUNUSED(event
))
627 int zoom
= GetZoomControl();
628 if (GetPrintPreview())
629 GetPrintPreview()->SetZoom(zoom
);
632 void wxPreviewControlBar::CreateButtons()
634 SetSize(0, 0, 400, 40);
636 wxBoxSizer
*item0
= new wxBoxSizer( wxHORIZONTAL
);
638 m_closeButton
= new wxButton( this, wxID_PREVIEW_CLOSE
, _("&Close"), wxDefaultPosition
, wxDefaultSize
, 0 );
639 item0
->Add( m_closeButton
, 0, wxALIGN_CENTRE
|wxALL
, 5 );
641 if (m_buttonFlags
& wxPREVIEW_PRINT
)
643 m_printButton
= new wxButton( this, wxID_PREVIEW_PRINT
, _("&Print..."), wxDefaultPosition
, wxDefaultSize
, 0 );
644 item0
->Add( m_printButton
, 0, wxALIGN_CENTRE
|wxALL
, 5 );
647 if (m_buttonFlags
& wxPREVIEW_FIRST
)
649 m_firstPageButton
= new wxButton( this, wxID_PREVIEW_FIRST
, _("|<<"), wxDefaultPosition
, wxDefaultSize
, wxBU_EXACTFIT
);
650 item0
->Add( m_firstPageButton
, 0, wxALIGN_CENTRE
|wxALL
, 5 );
653 if (m_buttonFlags
& wxPREVIEW_PREVIOUS
)
655 m_previousPageButton
= new wxButton( this, wxID_PREVIEW_PREVIOUS
, _("<<"), wxDefaultPosition
, wxDefaultSize
, wxBU_EXACTFIT
);
656 item0
->Add( m_previousPageButton
, 0, wxALIGN_CENTRE
|wxRIGHT
|wxTOP
|wxBOTTOM
, 5 );
659 if (m_buttonFlags
& wxPREVIEW_NEXT
)
661 m_nextPageButton
= new wxButton( this, wxID_PREVIEW_NEXT
, _(">>"), wxDefaultPosition
, wxDefaultSize
, wxBU_EXACTFIT
);
662 item0
->Add( m_nextPageButton
, 0, wxALIGN_CENTRE
|wxRIGHT
|wxTOP
|wxBOTTOM
, 5 );
665 if (m_buttonFlags
& wxPREVIEW_LAST
)
667 m_lastPageButton
= new wxButton( this, wxID_PREVIEW_LAST
, _(">>|"), wxDefaultPosition
, wxDefaultSize
, wxBU_EXACTFIT
);
668 item0
->Add( m_lastPageButton
, 0, wxALIGN_CENTRE
|wxRIGHT
|wxTOP
|wxBOTTOM
, 5 );
671 if (m_buttonFlags
& wxPREVIEW_GOTO
)
673 m_gotoPageButton
= new wxButton( this, wxID_PREVIEW_GOTO
, _("&Goto..."), wxDefaultPosition
, wxDefaultSize
, 0 );
674 item0
->Add( m_gotoPageButton
, 0, wxALIGN_CENTRE
|wxALL
, 5 );
677 if (m_buttonFlags
& wxPREVIEW_ZOOM
)
681 wxT("10%"), wxT("15%"), wxT("20%"), wxT("25%"), wxT("30%"), wxT("35%"), wxT("40%"), wxT("45%"), wxT("50%"), wxT("55%"),
682 wxT("60%"), wxT("65%"), wxT("70%"), wxT("75%"), wxT("80%"), wxT("85%"), wxT("90%"), wxT("95%"), wxT("100%"), wxT("110%"),
683 wxT("120%"), wxT("150%"), wxT("200%")
685 int n
= WXSIZEOF(choices
);
687 m_zoomControl
= new wxChoice( this, wxID_PREVIEW_ZOOM
, wxDefaultPosition
, wxSize(70,wxDefaultCoord
), n
, choices
, 0 );
688 item0
->Add( m_zoomControl
, 0, wxALIGN_CENTRE
|wxALL
, 5 );
689 SetZoomControl(m_printPreview
->GetZoom());
696 void wxPreviewControlBar::SetZoomControl(int zoom
)
700 int n
, count
= m_zoomControl
->GetCount();
702 for (n
=0; n
<count
; n
++)
704 if (m_zoomControl
->GetString(n
).BeforeFirst(wxT('%')).ToLong(&val
) &&
707 m_zoomControl
->SetSelection(n
);
712 m_zoomControl
->SetSelection(count
-1);
716 int wxPreviewControlBar::GetZoomControl()
718 if (m_zoomControl
&& (m_zoomControl
->GetStringSelection() != wxEmptyString
))
721 if (m_zoomControl
->GetStringSelection().BeforeFirst(wxT('%')).ToLong(&val
))
733 IMPLEMENT_CLASS(wxPreviewFrame
, wxFrame
)
735 BEGIN_EVENT_TABLE(wxPreviewFrame
, wxFrame
)
736 EVT_CLOSE(wxPreviewFrame::OnCloseWindow
)
739 wxPreviewFrame::wxPreviewFrame(wxPrintPreviewBase
*preview
, wxWindow
*parent
, const wxString
& title
,
740 const wxPoint
& pos
, const wxSize
& size
, long style
, const wxString
& name
):
741 wxFrame(parent
, wxID_ANY
, title
, pos
, size
, style
, name
)
743 m_printPreview
= preview
;
745 m_previewCanvas
= NULL
;
746 m_windowDisabler
= NULL
;
748 // Give the application icon
750 wxFrame
* topFrame
= wxDynamicCast(wxTheApp
->GetTopWindow(), wxFrame
);
752 SetIcon(topFrame
->GetIcon());
756 wxPreviewFrame::~wxPreviewFrame()
760 void wxPreviewFrame::OnCloseWindow(wxCloseEvent
& WXUNUSED(event
))
762 if (m_windowDisabler
)
763 delete m_windowDisabler
;
765 // Need to delete the printout and the print preview
766 wxPrintout
*printout
= m_printPreview
->GetPrintout();
770 m_printPreview
->SetPrintout(NULL
);
771 m_printPreview
->SetCanvas(NULL
);
772 m_printPreview
->SetFrame(NULL
);
774 delete m_printPreview
;
779 void wxPreviewFrame::Initialize()
787 m_printPreview
->SetCanvas(m_previewCanvas
);
788 m_printPreview
->SetFrame(this);
790 wxBoxSizer
*item0
= new wxBoxSizer( wxVERTICAL
);
792 item0
->Add( m_controlBar
, 0, wxGROW
|wxALIGN_CENTER_VERTICAL
, 5 );
793 item0
->Add( m_previewCanvas
, 1, wxGROW
|wxALIGN_CENTER_VERTICAL
, 5 );
795 SetAutoLayout( true );
798 m_windowDisabler
= new wxWindowDisabler(this);
802 m_printPreview
->AdjustScrollbars(m_previewCanvas
);
803 m_previewCanvas
->SetFocus();
804 m_controlBar
->SetFocus();
807 void wxPreviewFrame::CreateCanvas()
809 m_previewCanvas
= new wxPreviewCanvas(m_printPreview
, this);
812 void wxPreviewFrame::CreateControlBar()
814 long buttons
= wxPREVIEW_DEFAULT
;
815 if (m_printPreview
->GetPrintoutForPrinting())
816 buttons
|= wxPREVIEW_PRINT
;
818 m_controlBar
= new wxPreviewControlBar(m_printPreview
, buttons
, this, wxPoint(0, 0), wxSize(400, 40));
819 m_controlBar
->CreateButtons();
826 wxPrintPreviewBase::wxPrintPreviewBase(wxPrintout
*printout
,
827 wxPrintout
*printoutForPrinting
,
831 m_printDialogData
= (*data
);
833 Init(printout
, printoutForPrinting
);
836 wxPrintPreviewBase::wxPrintPreviewBase(wxPrintout
*printout
,
837 wxPrintout
*printoutForPrinting
,
838 wxPrintDialogData
*data
)
841 m_printDialogData
= (*data
);
843 Init(printout
, printoutForPrinting
);
846 void wxPrintPreviewBase::Init(wxPrintout
*printout
,
847 wxPrintout
*printoutForPrinting
)
850 m_previewPrintout
= printout
;
851 if (m_previewPrintout
)
852 m_previewPrintout
->SetIsPreview(true);
854 m_printPrintout
= printoutForPrinting
;
856 m_previewCanvas
= NULL
;
857 m_previewFrame
= NULL
;
858 m_previewBitmap
= NULL
;
865 m_printingPrepared
= false;
870 wxPrintPreviewBase::~wxPrintPreviewBase()
872 if (m_previewPrintout
)
873 delete m_previewPrintout
;
875 delete m_previewBitmap
;
877 delete m_printPrintout
;
880 bool wxPrintPreviewBase::SetCurrentPage(int pageNum
)
882 if (m_currentPage
== pageNum
)
885 m_currentPage
= pageNum
;
888 delete m_previewBitmap
;
889 m_previewBitmap
= NULL
;
894 AdjustScrollbars(m_previewCanvas
);
896 if (!RenderPage(pageNum
))
898 m_previewCanvas
->Refresh();
899 m_previewCanvas
->SetFocus();
904 int wxPrintPreviewBase::GetCurrentPage() const
905 { return m_currentPage
; };
906 void wxPrintPreviewBase::SetPrintout(wxPrintout
*printout
)
907 { m_previewPrintout
= printout
; };
908 wxPrintout
*wxPrintPreviewBase::GetPrintout() const
909 { return m_previewPrintout
; };
910 wxPrintout
*wxPrintPreviewBase::GetPrintoutForPrinting() const
911 { return m_printPrintout
; };
912 void wxPrintPreviewBase::SetFrame(wxFrame
*frame
)
913 { m_previewFrame
= frame
; };
914 void wxPrintPreviewBase::SetCanvas(wxPreviewCanvas
*canvas
)
915 { m_previewCanvas
= canvas
; };
916 wxFrame
*wxPrintPreviewBase::GetFrame() const
917 { return m_previewFrame
; }
918 wxPreviewCanvas
*wxPrintPreviewBase::GetCanvas() const
919 { return m_previewCanvas
; }
921 bool wxPrintPreviewBase::PaintPage(wxPreviewCanvas
*canvas
, wxDC
& dc
)
923 DrawBlankPage(canvas
, dc
);
925 if (!m_previewBitmap
)
926 if (!RenderPage(m_currentPage
))
929 if (!m_previewBitmap
)
935 int canvasWidth
, canvasHeight
;
936 canvas
->GetSize(&canvasWidth
, &canvasHeight
);
938 double zoomScale
= ((float)m_currentZoom
/(float)100);
939 double actualWidth
= (zoomScale
*m_pageWidth
*m_previewScale
);
940 // float actualHeight = (float)(zoomScale*m_pageHeight*m_previewScale);
942 int x
= (int) ((canvasWidth
- actualWidth
)/2.0);
943 if (x
< m_leftMargin
)
948 temp_dc
.SelectObject(*m_previewBitmap
);
950 dc
.Blit(x
, y
, m_previewBitmap
->GetWidth(), m_previewBitmap
->GetHeight(), &temp_dc
, 0, 0);
952 temp_dc
.SelectObject(wxNullBitmap
);
957 // Adjusts the scrollbars for the current scale
958 void wxPrintPreviewBase::AdjustScrollbars(wxPreviewCanvas
*canvas
)
963 int canvasWidth
, canvasHeight
;
964 canvas
->GetSize(&canvasWidth
, &canvasHeight
);
966 double zoomScale
= ((float)m_currentZoom
/(float)100);
967 double actualWidth
= (zoomScale
*m_pageWidth
*m_previewScale
);
968 double actualHeight
= (zoomScale
*m_pageHeight
*m_previewScale
);
970 // Set the scrollbars appropriately
971 int totalWidth
= (int)(actualWidth
+ 2*m_leftMargin
);
972 int totalHeight
= (int)(actualHeight
+ 2*m_topMargin
);
973 int scrollUnitsX
= totalWidth
/10;
974 int scrollUnitsY
= totalHeight
/10;
975 wxSize virtualSize
= canvas
->GetVirtualSize();
976 if (virtualSize
.GetWidth() != totalWidth
|| virtualSize
.GetHeight() != totalHeight
)
977 canvas
->SetScrollbars(10, 10, scrollUnitsX
, scrollUnitsY
, 0, 0, true);
980 bool wxPrintPreviewBase::RenderPage(int pageNum
)
984 int canvasWidth
, canvasHeight
;
986 if (!m_previewCanvas
)
988 wxFAIL_MSG(_T("wxPrintPreviewBase::RenderPage: must use wxPrintPreviewBase::SetCanvas to let me know about the canvas!"));
992 m_previewCanvas
->GetSize(&canvasWidth
, &canvasHeight
);
994 double zoomScale
= (m_currentZoom
/100.0);
995 int actualWidth
= (int)(zoomScale
*m_pageWidth
*m_previewScale
);
996 int actualHeight
= (int)(zoomScale
*m_pageHeight
*m_previewScale
);
998 if (!m_previewBitmap
)
1000 m_previewBitmap
= new wxBitmap((int)actualWidth
, (int)actualHeight
);
1001 if (!m_previewBitmap
|| !m_previewBitmap
->Ok())
1003 if (m_previewBitmap
) {
1004 delete m_previewBitmap
;
1005 m_previewBitmap
= NULL
;
1007 wxMessageBox(_("Sorry, not enough memory to create a preview."), _("Print Preview Failure"), wxOK
);
1012 wxMemoryDC memoryDC
;
1013 memoryDC
.SelectObject(*m_previewBitmap
);
1017 m_previewPrintout
->SetDC(&memoryDC
);
1018 m_previewPrintout
->SetPageSizePixels(m_pageWidth
, m_pageHeight
);
1020 // Need to delay OnPreparePrinting until here, so we have enough information.
1021 if (!m_printingPrepared
)
1023 m_previewPrintout
->OnPreparePrinting();
1025 m_previewPrintout
->GetPageInfo(&m_minPage
, &m_maxPage
, &selFrom
, &selTo
);
1026 m_printingPrepared
= true;
1029 m_previewPrintout
->OnBeginPrinting();
1031 if (!m_previewPrintout
->OnBeginDocument(m_printDialogData
.GetFromPage(), m_printDialogData
.GetToPage()))
1033 wxMessageBox(_("Could not start document preview."), _("Print Preview Failure"), wxOK
);
1035 memoryDC
.SelectObject(wxNullBitmap
);
1037 delete m_previewBitmap
;
1038 m_previewBitmap
= NULL
;
1042 m_previewPrintout
->OnPrintPage(pageNum
);
1043 m_previewPrintout
->OnEndDocument();
1044 m_previewPrintout
->OnEndPrinting();
1046 m_previewPrintout
->SetDC(NULL
);
1048 memoryDC
.SelectObject(wxNullBitmap
);
1053 status
= wxString::Format(_("Page %d of %d"), pageNum
, m_maxPage
);
1055 status
= wxString::Format(_("Page %d"), pageNum
);
1058 m_previewFrame
->SetStatusText(status
);
1065 bool wxPrintPreviewBase::DrawBlankPage(wxPreviewCanvas
*canvas
, wxDC
& dc
)
1067 int canvasWidth
, canvasHeight
;
1068 canvas
->GetSize(&canvasWidth
, &canvasHeight
);
1070 float zoomScale
= (float)((float)m_currentZoom
/(float)100);
1071 float actualWidth
= zoomScale
*m_pageWidth
*m_previewScale
;
1072 float actualHeight
= zoomScale
*m_pageHeight
*m_previewScale
;
1074 float x
= (float)((canvasWidth
- actualWidth
)/2.0);
1075 if (x
< m_leftMargin
)
1076 x
= (float)m_leftMargin
;
1077 float y
= (float)m_topMargin
;
1079 // Draw shadow, allowing for 1-pixel border AROUND the actual page
1080 int shadowOffset
= 4;
1081 dc
.SetPen(*wxBLACK_PEN
);
1082 dc
.SetBrush(*wxBLACK_BRUSH
);
1084 dc.DrawRectangle((int)(x-1 + shadowOffset), (int)(y-1 + shadowOffset), (int)(actualWidth+2), (int)(actualHeight+2));
1086 dc
.DrawRectangle((int)(x
+ shadowOffset
), (int)(y
+ actualHeight
+1), (int)(actualWidth
), shadowOffset
);
1087 dc
.DrawRectangle((int)(x
+ actualWidth
), (int)(y
+ shadowOffset
), shadowOffset
, (int)(actualHeight
));
1089 // Draw blank page allowing for 1-pixel border AROUND the actual page
1090 dc
.SetPen(*wxBLACK_PEN
);
1091 dc
.SetBrush(*wxWHITE_BRUSH
);
1094 wxRegion update_region = canvas->GetUpdateRegion();
1095 wxRect r = update_region.GetBox();
1097 printf( "x: %d y: %d w: %d h: %d.\n", (int)r.x, (int)r.y, (int)r.width, (int)r.height );
1100 dc
.DrawRectangle((int)(x
-2), (int)(y
-1), (int)(actualWidth
+3), (int)(actualHeight
+2));
1105 void wxPrintPreviewBase::SetZoom(int percent
)
1107 if (m_currentZoom
== percent
)
1110 m_currentZoom
= percent
;
1111 if (m_previewBitmap
)
1113 delete m_previewBitmap
;
1114 m_previewBitmap
= NULL
;
1117 if (m_previewCanvas
)
1119 AdjustScrollbars(m_previewCanvas
);
1120 RenderPage(m_currentPage
);
1121 ((wxScrolledWindow
*) m_previewCanvas
)->Scroll(0, 0);
1122 m_previewCanvas
->ClearBackground();
1123 m_previewCanvas
->Refresh();
1124 m_previewCanvas
->SetFocus();
1128 wxPrintDialogData
& wxPrintPreviewBase::GetPrintDialogData()
1130 return m_printDialogData
;
1133 int wxPrintPreviewBase::GetZoom() const
1134 { return m_currentZoom
; }
1135 int wxPrintPreviewBase::GetMaxPage() const
1136 { return m_maxPage
; }
1137 int wxPrintPreviewBase::GetMinPage() const
1138 { return m_minPage
; }
1139 bool wxPrintPreviewBase::Ok() const
1141 void wxPrintPreviewBase::SetOk(bool ok
)
1143 //----------------------------------------------------------------------------
1145 //----------------------------------------------------------------------------
1147 IMPLEMENT_CLASS(wxPrintPreview
, wxPrintPreviewBase
)
1149 wxPrintPreview::wxPrintPreview(wxPrintout
*printout
,
1150 wxPrintout
*printoutForPrinting
,
1151 wxPrintDialogData
*data
) :
1152 wxPrintPreviewBase( printout
, printoutForPrinting
, data
)
1154 m_pimpl
= wxPrintFactory::GetFactory()->
1155 CreatePrintPreview( printout
, printoutForPrinting
, data
);
1158 wxPrintPreview::wxPrintPreview(wxPrintout
*printout
,
1159 wxPrintout
*printoutForPrinting
,
1160 wxPrintData
*data
) :
1161 wxPrintPreviewBase( printout
, printoutForPrinting
, data
)
1163 m_pimpl
= wxPrintFactory::GetFactory()->
1164 CreatePrintPreview( printout
, printoutForPrinting
, data
);
1167 wxPrintPreview::~wxPrintPreview()
1171 // don't delete twice
1172 m_printPrintout
= NULL
;
1173 m_previewPrintout
= NULL
;
1174 m_previewBitmap
= NULL
;
1177 bool wxPrintPreview::SetCurrentPage(int pageNum
)
1179 return m_pimpl
->SetCurrentPage( pageNum
);
1182 int wxPrintPreview::GetCurrentPage() const
1184 return m_pimpl
->GetCurrentPage();
1187 void wxPrintPreview::SetPrintout(wxPrintout
*printout
)
1189 m_pimpl
->SetPrintout( printout
);
1192 wxPrintout
*wxPrintPreview::GetPrintout() const
1194 return m_pimpl
->GetPrintout();
1197 wxPrintout
*wxPrintPreview::GetPrintoutForPrinting() const
1199 return m_pimpl
->GetPrintoutForPrinting();
1202 void wxPrintPreview::SetFrame(wxFrame
*frame
)
1204 m_pimpl
->SetFrame( frame
);
1207 void wxPrintPreview::SetCanvas(wxPreviewCanvas
*canvas
)
1209 m_pimpl
->SetCanvas( canvas
);
1212 wxFrame
*wxPrintPreview::GetFrame() const
1214 return m_pimpl
->GetFrame();
1217 wxPreviewCanvas
*wxPrintPreview::GetCanvas() const
1219 return m_pimpl
->GetCanvas();
1222 bool wxPrintPreview::PaintPage(wxPreviewCanvas
*canvas
, wxDC
& dc
)
1224 return m_pimpl
->PaintPage( canvas
, dc
);
1227 bool wxPrintPreview::DrawBlankPage(wxPreviewCanvas
*canvas
, wxDC
& dc
)
1229 return m_pimpl
->DrawBlankPage( canvas
, dc
);
1232 void wxPrintPreview::AdjustScrollbars(wxPreviewCanvas
*canvas
)
1234 m_pimpl
->AdjustScrollbars( canvas
);
1237 bool wxPrintPreview::RenderPage(int pageNum
)
1239 return m_pimpl
->RenderPage( pageNum
);
1242 void wxPrintPreview::SetZoom(int percent
)
1244 m_pimpl
->SetZoom( percent
);
1247 wxPrintDialogData
& wxPrintPreview::GetPrintDialogData()
1249 return m_pimpl
->GetPrintDialogData();
1252 int wxPrintPreview::GetMaxPage() const
1254 return m_pimpl
->GetMaxPage();
1257 int wxPrintPreview::GetMinPage() const
1259 return m_pimpl
->GetMinPage();
1262 bool wxPrintPreview::Ok() const
1264 return m_pimpl
->Ok();
1267 void wxPrintPreview::SetOk(bool ok
)
1269 m_pimpl
->SetOk( ok
);
1272 bool wxPrintPreview::Print(bool interactive
)
1274 return m_pimpl
->Print( interactive
);
1277 void wxPrintPreview::DetermineScaling()
1279 m_pimpl
->DetermineScaling();
1283 #endif // wxUSE_PRINTING_ARCHITECTURE