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 wxPrintNativeDataBase
*wxNativePrintFactory::CreatePrintNativeData()
151 #if defined(__WXMSW__) && !defined(__WXUNIVERSAL__)
152 return new wxWindowsPrintNativeData
;
153 #elif defined(__WXMAC__)
154 return new wxMacPrintNativeData
;
156 return new wxPostScriptPrintNativeData
;
160 //----------------------------------------------------------------------------
161 // wxPrintNativeDataBase
162 //----------------------------------------------------------------------------
164 IMPLEMENT_ABSTRACT_CLASS(wxPrintNativeDataBase
, wxObject
)
166 wxPrintNativeDataBase::wxPrintNativeDataBase()
171 //----------------------------------------------------------------------------
173 //----------------------------------------------------------------------------
175 IMPLEMENT_CLASS(wxPrinterBase
, wxObject
)
177 wxPrinterBase::wxPrinterBase(wxPrintDialogData
*data
)
179 m_currentPrintout
= (wxPrintout
*) NULL
;
180 sm_abortWindow
= (wxWindow
*) NULL
;
183 m_printDialogData
= (*data
);
184 sm_lastError
= wxPRINTER_NO_ERROR
;
187 wxWindow
*wxPrinterBase::sm_abortWindow
= (wxWindow
*) NULL
;
188 bool wxPrinterBase::sm_abortIt
= false;
189 wxPrinterError
wxPrinterBase::sm_lastError
= wxPRINTER_NO_ERROR
;
191 wxPrinterBase::~wxPrinterBase()
195 wxWindow
*wxPrinterBase::CreateAbortWindow(wxWindow
*parent
, wxPrintout
* printout
)
197 wxPrintAbortDialog
*dialog
= new wxPrintAbortDialog(parent
, _("Printing ") , wxDefaultPosition
, wxDefaultSize
, wxDEFAULT_DIALOG_STYLE
);
199 wxBoxSizer
*button_sizer
= new wxBoxSizer( wxVERTICAL
);
200 button_sizer
->Add( new wxStaticText(dialog
, wxID_ANY
, _("Please wait while printing\n") + printout
->GetTitle() ), 0, wxALL
, 10 );
201 button_sizer
->Add( new wxButton( dialog
, wxID_CANCEL
, wxT("Cancel") ), 0, wxALL
| wxALIGN_CENTER
, 10 );
203 dialog
->SetAutoLayout( true );
204 dialog
->SetSizer( button_sizer
);
206 button_sizer
->Fit(dialog
);
207 button_sizer
->SetSizeHints (dialog
) ;
212 void wxPrinterBase::ReportError(wxWindow
*parent
, wxPrintout
*WXUNUSED(printout
), const wxString
& message
)
214 wxMessageBox(message
, _("Printing Error"), wxOK
, parent
);
217 wxPrintDialogData
& wxPrinterBase::GetPrintDialogData() const
219 return (wxPrintDialogData
&) m_printDialogData
;
222 //----------------------------------------------------------------------------
224 //----------------------------------------------------------------------------
226 IMPLEMENT_CLASS(wxPrinter
, wxPrinterBase
)
228 wxPrinter::wxPrinter(wxPrintDialogData
*data
)
230 m_pimpl
= wxPrintFactory::GetFactory()->CreatePrinter( data
);
233 wxPrinter::~wxPrinter()
238 wxWindow
*wxPrinter::CreateAbortWindow(wxWindow
*parent
, wxPrintout
*printout
)
240 return m_pimpl
->CreateAbortWindow( parent
, printout
);
243 void wxPrinter::ReportError(wxWindow
*parent
, wxPrintout
*printout
, const wxString
& message
)
245 m_pimpl
->ReportError( parent
, printout
, message
);
248 bool wxPrinter::Setup(wxWindow
*parent
)
250 return m_pimpl
->Setup( parent
);
253 bool wxPrinter::Print(wxWindow
*parent
, wxPrintout
*printout
, bool prompt
)
255 return m_pimpl
->Print( parent
, printout
, prompt
);
258 wxDC
* wxPrinter::PrintDialog(wxWindow
*parent
)
260 return m_pimpl
->PrintDialog( parent
);
263 wxPrintDialogData
& wxPrinter::GetPrintDialogData() const
265 return m_pimpl
->GetPrintDialogData();
268 // ---------------------------------------------------------------------------
269 // wxPrintDialogBase: the common dialog for printing.
270 // ---------------------------------------------------------------------------
272 IMPLEMENT_ABSTRACT_CLASS(wxPrintDialogBase
, wxObject
)
274 wxPrintDialogBase::wxPrintDialogBase(wxWindow
*parent
, wxWindowID id
,
275 const wxString
&title
, const wxPoint
&pos
, const wxSize
&size
, long style
) :
276 wxDialog( parent
, id
, title
, pos
, size
, style
)
280 // ---------------------------------------------------------------------------
281 // wxPrintDialog: the common dialog for printing.
282 // ---------------------------------------------------------------------------
284 IMPLEMENT_CLASS(wxPrintDialog
, wxObject
)
286 wxPrintDialog::wxPrintDialog(wxWindow
*parent
, wxPrintDialogData
* data
)
288 m_pimpl
= wxPrintFactory::GetFactory()->CreatePrintDialog( parent
, data
);
291 wxPrintDialog::wxPrintDialog(wxWindow
*parent
, wxPrintData
* data
)
293 m_pimpl
= wxPrintFactory::GetFactory()->CreatePrintDialog( parent
, data
);
296 wxPrintDialog::~wxPrintDialog()
301 int wxPrintDialog::ShowModal()
303 return m_pimpl
->ShowModal();
306 wxPrintDialogData
& wxPrintDialog::GetPrintDialogData()
308 return m_pimpl
->GetPrintDialogData();
311 wxPrintData
& wxPrintDialog::GetPrintData()
313 return m_pimpl
->GetPrintData();
315 wxDC
*wxPrintDialog::GetPrintDC()
317 return m_pimpl
->GetPrintDC();
320 //----------------------------------------------------------------------------
321 // wxPrintAbortDialog
322 //----------------------------------------------------------------------------
324 BEGIN_EVENT_TABLE(wxPrintAbortDialog
, wxDialog
)
325 EVT_BUTTON(wxID_CANCEL
, wxPrintAbortDialog::OnCancel
)
328 void wxPrintAbortDialog::OnCancel(wxCommandEvent
& WXUNUSED(event
))
330 wxPrinterBase::sm_abortIt
= true;
331 wxPrinterBase::sm_abortWindow
->Show(false);
332 wxPrinterBase::sm_abortWindow
->Close(true);
333 wxPrinterBase::sm_abortWindow
= (wxWindow
*) NULL
;
336 //----------------------------------------------------------------------------
338 //----------------------------------------------------------------------------
340 IMPLEMENT_ABSTRACT_CLASS(wxPrintout
, wxObject
)
342 wxPrintout::wxPrintout(const wxString
& title
)
344 m_printoutTitle
= title
;
345 m_printoutDC
= (wxDC
*) NULL
;
348 m_pageWidthPixels
= 0;
349 m_pageHeightPixels
= 0;
357 wxPrintout::~wxPrintout()
361 bool wxPrintout::OnBeginDocument(int WXUNUSED(startPage
), int WXUNUSED(endPage
))
363 return GetDC()->StartDoc(_("Printing ") + m_printoutTitle
);
366 void wxPrintout::OnEndDocument()
371 void wxPrintout::OnBeginPrinting()
375 void wxPrintout::OnEndPrinting()
379 bool wxPrintout::HasPage(int page
)
384 void wxPrintout::GetPageInfo(int *minPage
, int *maxPage
, int *fromPage
, int *toPage
)
392 //----------------------------------------------------------------------------
394 //----------------------------------------------------------------------------
396 IMPLEMENT_CLASS(wxPreviewCanvas
, wxWindow
)
398 BEGIN_EVENT_TABLE(wxPreviewCanvas
, wxScrolledWindow
)
399 EVT_PAINT(wxPreviewCanvas::OnPaint
)
400 EVT_CHAR(wxPreviewCanvas::OnChar
)
401 EVT_SYS_COLOUR_CHANGED(wxPreviewCanvas::OnSysColourChanged
)
404 // VZ: the current code doesn't refresh properly without
405 // wxFULL_REPAINT_ON_RESIZE, this must be fixed as otherwise we have
406 // really horrible flicker when resizing the preview frame, but without
407 // this style it simply doesn't work correctly at all...
408 wxPreviewCanvas::wxPreviewCanvas(wxPrintPreviewBase
*preview
, wxWindow
*parent
,
409 const wxPoint
& pos
, const wxSize
& size
, long style
, const wxString
& name
):
410 wxScrolledWindow(parent
, wxID_ANY
, pos
, size
, style
| wxFULL_REPAINT_ON_RESIZE
, name
)
412 m_printPreview
= preview
;
414 // The app workspace colour is always white, but we should have
415 // a contrast with the page.
416 wxSystemColour colourIndex
= wxSYS_COLOUR_3DDKSHADOW
;
418 wxSystemColour colourIndex
= wxSYS_COLOUR_APPWORKSPACE
;
420 SetBackgroundColour(wxSystemSettings::GetColour(colourIndex
));
422 SetScrollbars(10, 10, 100, 100);
425 wxPreviewCanvas::~wxPreviewCanvas()
429 void wxPreviewCanvas::OnPaint(wxPaintEvent
& WXUNUSED(event
))
436 if (!GetUpdateRegion().IsEmpty())
437 dc.SetClippingRegion( GetUpdateRegion() );
443 m_printPreview
->PaintPage(this, dc
);
447 // Responds to colour changes, and passes event on to children.
448 void wxPreviewCanvas::OnSysColourChanged(wxSysColourChangedEvent
& event
)
451 // The app workspace colour is always white, but we should have
452 // a contrast with the page.
453 wxSystemColour colourIndex
= wxSYS_COLOUR_3DDKSHADOW
;
455 wxSystemColour colourIndex
= wxSYS_COLOUR_APPWORKSPACE
;
457 SetBackgroundColour(wxSystemSettings::GetColour(colourIndex
));
460 // Propagate the event to the non-top-level children
461 wxWindow::OnSysColourChanged(event
);
464 void wxPreviewCanvas::OnChar(wxKeyEvent
&event
)
466 wxPreviewControlBar
* controlBar
= ((wxPreviewFrame
*) GetParent())->GetControlBar();
467 if (event
.GetKeyCode() == WXK_ESCAPE
)
469 ((wxPreviewFrame
*) GetParent())->Close(true);
472 else if (event
.GetKeyCode() == WXK_TAB
)
474 controlBar
->OnGoto();
477 else if (event
.GetKeyCode() == WXK_RETURN
)
479 controlBar
->OnPrint();
483 if (!event
.ControlDown())
489 switch(event
.GetKeyCode())
492 controlBar
->OnNext(); break;
494 controlBar
->OnPrevious(); break;
496 controlBar
->OnFirst(); break;
498 controlBar
->OnLast(); break;
504 //----------------------------------------------------------------------------
505 // wxPreviewControlBar
506 //----------------------------------------------------------------------------
508 IMPLEMENT_CLASS(wxPreviewControlBar
, wxWindow
)
510 BEGIN_EVENT_TABLE(wxPreviewControlBar
, wxPanel
)
511 EVT_BUTTON(wxID_PREVIEW_CLOSE
, wxPreviewControlBar::OnWindowClose
)
512 EVT_BUTTON(wxID_PREVIEW_PRINT
, wxPreviewControlBar::OnPrintButton
)
513 EVT_BUTTON(wxID_PREVIEW_PREVIOUS
, wxPreviewControlBar::OnPreviousButton
)
514 EVT_BUTTON(wxID_PREVIEW_NEXT
, wxPreviewControlBar::OnNextButton
)
515 EVT_BUTTON(wxID_PREVIEW_FIRST
, wxPreviewControlBar::OnFirstButton
)
516 EVT_BUTTON(wxID_PREVIEW_LAST
, wxPreviewControlBar::OnLastButton
)
517 EVT_BUTTON(wxID_PREVIEW_GOTO
, wxPreviewControlBar::OnGotoButton
)
518 EVT_CHOICE(wxID_PREVIEW_ZOOM
, wxPreviewControlBar::OnZoom
)
519 EVT_PAINT(wxPreviewControlBar::OnPaint
)
522 wxPreviewControlBar::wxPreviewControlBar(wxPrintPreviewBase
*preview
, long buttons
,
523 wxWindow
*parent
, const wxPoint
& pos
, const wxSize
& size
,
524 long style
, const wxString
& name
):
525 wxPanel(parent
, wxID_ANY
, pos
, size
, style
, name
)
527 m_printPreview
= preview
;
528 m_closeButton
= (wxButton
*) NULL
;
529 m_nextPageButton
= (wxButton
*) NULL
;
530 m_previousPageButton
= (wxButton
*) NULL
;
531 m_printButton
= (wxButton
*) NULL
;
532 m_zoomControl
= (wxChoice
*) NULL
;
533 m_buttonFlags
= buttons
;
536 wxPreviewControlBar::~wxPreviewControlBar()
540 void wxPreviewControlBar::OnPaint(wxPaintEvent
& WXUNUSED(event
))
546 dc
.SetPen(*wxBLACK_PEN
);
547 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
548 dc
.DrawLine( 0, h
-1, w
, h
-1 );
551 void wxPreviewControlBar::OnWindowClose(wxCommandEvent
& WXUNUSED(event
))
553 wxPreviewFrame
*frame
= (wxPreviewFrame
*)GetParent();
557 void wxPreviewControlBar::OnPrint(void)
559 wxPrintPreviewBase
*preview
= GetPrintPreview();
560 preview
->Print(true);
563 void wxPreviewControlBar::OnNext(void)
565 wxPrintPreviewBase
*preview
= GetPrintPreview();
568 int currentPage
= preview
->GetCurrentPage();
569 if ((preview
->GetMaxPage() > 0) &&
570 (currentPage
< preview
->GetMaxPage()) &&
571 preview
->GetPrintout()->HasPage(currentPage
+ 1))
573 preview
->SetCurrentPage(currentPage
+ 1);
578 void wxPreviewControlBar::OnPrevious(void)
580 wxPrintPreviewBase
*preview
= GetPrintPreview();
583 int currentPage
= preview
->GetCurrentPage();
584 if ((preview
->GetMinPage() > 0) &&
585 (currentPage
> preview
->GetMinPage()) &&
586 preview
->GetPrintout()->HasPage(currentPage
- 1))
588 preview
->SetCurrentPage(currentPage
- 1);
593 void wxPreviewControlBar::OnFirst(void)
595 wxPrintPreviewBase
*preview
= GetPrintPreview();
598 int currentPage
= preview
->GetMinPage();
599 if (preview
->GetPrintout()->HasPage(currentPage
))
601 preview
->SetCurrentPage(currentPage
);
606 void wxPreviewControlBar::OnLast(void)
608 wxPrintPreviewBase
*preview
= GetPrintPreview();
611 int currentPage
= preview
->GetMaxPage();
612 if (preview
->GetPrintout()->HasPage(currentPage
))
614 preview
->SetCurrentPage(currentPage
);
619 void wxPreviewControlBar::OnGoto(void)
621 wxPrintPreviewBase
*preview
= GetPrintPreview();
626 if (preview
->GetMinPage() > 0)
631 strPrompt
.Printf( _("Enter a page number between %d and %d:"),
632 preview
->GetMinPage(), preview
->GetMaxPage());
633 strPage
.Printf( wxT("%d"), preview
->GetCurrentPage() );
636 wxGetTextFromUser( strPrompt
, _("Goto Page"), strPage
, GetParent());
638 if ( strPage
.ToLong( ¤tPage
) )
639 if (preview
->GetPrintout()->HasPage(currentPage
))
641 preview
->SetCurrentPage(currentPage
);
647 void wxPreviewControlBar::OnZoom(wxCommandEvent
& WXUNUSED(event
))
649 int zoom
= GetZoomControl();
650 if (GetPrintPreview())
651 GetPrintPreview()->SetZoom(zoom
);
654 void wxPreviewControlBar::CreateButtons()
656 SetSize(0, 0, 400, 40);
658 wxBoxSizer
*item0
= new wxBoxSizer( wxHORIZONTAL
);
660 m_closeButton
= new wxButton( this, wxID_PREVIEW_CLOSE
, _("&Close"), wxDefaultPosition
, wxDefaultSize
, 0 );
661 item0
->Add( m_closeButton
, 0, wxALIGN_CENTRE
|wxALL
, 5 );
663 if (m_buttonFlags
& wxPREVIEW_PRINT
)
665 m_printButton
= new wxButton( this, wxID_PREVIEW_PRINT
, _("&Print..."), wxDefaultPosition
, wxDefaultSize
, 0 );
666 item0
->Add( m_printButton
, 0, wxALIGN_CENTRE
|wxALL
, 5 );
669 if (m_buttonFlags
& wxPREVIEW_FIRST
)
671 m_firstPageButton
= new wxButton( this, wxID_PREVIEW_FIRST
, _("|<<"), wxDefaultPosition
, wxDefaultSize
, wxBU_EXACTFIT
);
672 item0
->Add( m_firstPageButton
, 0, wxALIGN_CENTRE
|wxALL
, 5 );
675 if (m_buttonFlags
& wxPREVIEW_PREVIOUS
)
677 m_previousPageButton
= new wxButton( this, wxID_PREVIEW_PREVIOUS
, _("<<"), wxDefaultPosition
, wxDefaultSize
, wxBU_EXACTFIT
);
678 item0
->Add( m_previousPageButton
, 0, wxALIGN_CENTRE
|wxRIGHT
|wxTOP
|wxBOTTOM
, 5 );
681 if (m_buttonFlags
& wxPREVIEW_NEXT
)
683 m_nextPageButton
= new wxButton( this, wxID_PREVIEW_NEXT
, _(">>"), wxDefaultPosition
, wxDefaultSize
, wxBU_EXACTFIT
);
684 item0
->Add( m_nextPageButton
, 0, wxALIGN_CENTRE
|wxRIGHT
|wxTOP
|wxBOTTOM
, 5 );
687 if (m_buttonFlags
& wxPREVIEW_LAST
)
689 m_lastPageButton
= new wxButton( this, wxID_PREVIEW_LAST
, _(">>|"), wxDefaultPosition
, wxDefaultSize
, wxBU_EXACTFIT
);
690 item0
->Add( m_lastPageButton
, 0, wxALIGN_CENTRE
|wxRIGHT
|wxTOP
|wxBOTTOM
, 5 );
693 if (m_buttonFlags
& wxPREVIEW_GOTO
)
695 m_gotoPageButton
= new wxButton( this, wxID_PREVIEW_GOTO
, _("&Goto..."), wxDefaultPosition
, wxDefaultSize
, 0 );
696 item0
->Add( m_gotoPageButton
, 0, wxALIGN_CENTRE
|wxALL
, 5 );
699 if (m_buttonFlags
& wxPREVIEW_ZOOM
)
703 wxT("10%"), wxT("15%"), wxT("20%"), wxT("25%"), wxT("30%"), wxT("35%"), wxT("40%"), wxT("45%"), wxT("50%"), wxT("55%"),
704 wxT("60%"), wxT("65%"), wxT("70%"), wxT("75%"), wxT("80%"), wxT("85%"), wxT("90%"), wxT("95%"), wxT("100%"), wxT("110%"),
705 wxT("120%"), wxT("150%"), wxT("200%")
707 int n
= WXSIZEOF(choices
);
709 m_zoomControl
= new wxChoice( this, wxID_PREVIEW_ZOOM
, wxDefaultPosition
, wxSize(70,wxDefaultCoord
), n
, choices
, 0 );
710 item0
->Add( m_zoomControl
, 0, wxALIGN_CENTRE
|wxALL
, 5 );
711 SetZoomControl(m_printPreview
->GetZoom());
718 void wxPreviewControlBar::SetZoomControl(int zoom
)
722 int n
, count
= m_zoomControl
->GetCount();
724 for (n
=0; n
<count
; n
++)
726 if (m_zoomControl
->GetString(n
).BeforeFirst(wxT('%')).ToLong(&val
) &&
729 m_zoomControl
->SetSelection(n
);
734 m_zoomControl
->SetSelection(count
-1);
738 int wxPreviewControlBar::GetZoomControl()
740 if (m_zoomControl
&& (m_zoomControl
->GetStringSelection() != wxEmptyString
))
743 if (m_zoomControl
->GetStringSelection().BeforeFirst(wxT('%')).ToLong(&val
))
755 IMPLEMENT_CLASS(wxPreviewFrame
, wxFrame
)
757 BEGIN_EVENT_TABLE(wxPreviewFrame
, wxFrame
)
758 EVT_CLOSE(wxPreviewFrame::OnCloseWindow
)
761 wxPreviewFrame::wxPreviewFrame(wxPrintPreviewBase
*preview
, wxWindow
*parent
, const wxString
& title
,
762 const wxPoint
& pos
, const wxSize
& size
, long style
, const wxString
& name
):
763 wxFrame(parent
, wxID_ANY
, title
, pos
, size
, style
, name
)
765 m_printPreview
= preview
;
767 m_previewCanvas
= NULL
;
768 m_windowDisabler
= NULL
;
770 // Give the application icon
772 wxFrame
* topFrame
= wxDynamicCast(wxTheApp
->GetTopWindow(), wxFrame
);
774 SetIcon(topFrame
->GetIcon());
778 wxPreviewFrame::~wxPreviewFrame()
782 void wxPreviewFrame::OnCloseWindow(wxCloseEvent
& WXUNUSED(event
))
784 if (m_windowDisabler
)
785 delete m_windowDisabler
;
787 // Need to delete the printout and the print preview
788 wxPrintout
*printout
= m_printPreview
->GetPrintout();
792 m_printPreview
->SetPrintout(NULL
);
793 m_printPreview
->SetCanvas(NULL
);
794 m_printPreview
->SetFrame(NULL
);
796 delete m_printPreview
;
801 void wxPreviewFrame::Initialize()
809 m_printPreview
->SetCanvas(m_previewCanvas
);
810 m_printPreview
->SetFrame(this);
812 wxBoxSizer
*item0
= new wxBoxSizer( wxVERTICAL
);
814 item0
->Add( m_controlBar
, 0, wxGROW
|wxALIGN_CENTER_VERTICAL
, 5 );
815 item0
->Add( m_previewCanvas
, 1, wxGROW
|wxALIGN_CENTER_VERTICAL
, 5 );
817 SetAutoLayout( true );
820 m_windowDisabler
= new wxWindowDisabler(this);
824 m_printPreview
->AdjustScrollbars(m_previewCanvas
);
825 m_previewCanvas
->SetFocus();
826 m_controlBar
->SetFocus();
829 void wxPreviewFrame::CreateCanvas()
831 m_previewCanvas
= new wxPreviewCanvas(m_printPreview
, this);
834 void wxPreviewFrame::CreateControlBar()
836 long buttons
= wxPREVIEW_DEFAULT
;
837 if (m_printPreview
->GetPrintoutForPrinting())
838 buttons
|= wxPREVIEW_PRINT
;
840 m_controlBar
= new wxPreviewControlBar(m_printPreview
, buttons
, this, wxPoint(0, 0), wxSize(400, 40));
841 m_controlBar
->CreateButtons();
848 wxPrintPreviewBase::wxPrintPreviewBase(wxPrintout
*printout
,
849 wxPrintout
*printoutForPrinting
,
853 m_printDialogData
= (*data
);
855 Init(printout
, printoutForPrinting
);
858 wxPrintPreviewBase::wxPrintPreviewBase(wxPrintout
*printout
,
859 wxPrintout
*printoutForPrinting
,
860 wxPrintDialogData
*data
)
863 m_printDialogData
= (*data
);
865 Init(printout
, printoutForPrinting
);
868 void wxPrintPreviewBase::Init(wxPrintout
*printout
,
869 wxPrintout
*printoutForPrinting
)
872 m_previewPrintout
= printout
;
873 if (m_previewPrintout
)
874 m_previewPrintout
->SetIsPreview(true);
876 m_printPrintout
= printoutForPrinting
;
878 m_previewCanvas
= NULL
;
879 m_previewFrame
= NULL
;
880 m_previewBitmap
= NULL
;
887 m_printingPrepared
= false;
892 wxPrintPreviewBase::~wxPrintPreviewBase()
894 if (m_previewPrintout
)
895 delete m_previewPrintout
;
897 delete m_previewBitmap
;
899 delete m_printPrintout
;
902 bool wxPrintPreviewBase::SetCurrentPage(int pageNum
)
904 if (m_currentPage
== pageNum
)
907 m_currentPage
= pageNum
;
910 delete m_previewBitmap
;
911 m_previewBitmap
= NULL
;
916 AdjustScrollbars(m_previewCanvas
);
918 if (!RenderPage(pageNum
))
920 m_previewCanvas
->Refresh();
921 m_previewCanvas
->SetFocus();
926 int wxPrintPreviewBase::GetCurrentPage() const
927 { return m_currentPage
; };
928 void wxPrintPreviewBase::SetPrintout(wxPrintout
*printout
)
929 { m_previewPrintout
= printout
; };
930 wxPrintout
*wxPrintPreviewBase::GetPrintout() const
931 { return m_previewPrintout
; };
932 wxPrintout
*wxPrintPreviewBase::GetPrintoutForPrinting() const
933 { return m_printPrintout
; };
934 void wxPrintPreviewBase::SetFrame(wxFrame
*frame
)
935 { m_previewFrame
= frame
; };
936 void wxPrintPreviewBase::SetCanvas(wxPreviewCanvas
*canvas
)
937 { m_previewCanvas
= canvas
; };
938 wxFrame
*wxPrintPreviewBase::GetFrame() const
939 { return m_previewFrame
; }
940 wxPreviewCanvas
*wxPrintPreviewBase::GetCanvas() const
941 { return m_previewCanvas
; }
943 bool wxPrintPreviewBase::PaintPage(wxPreviewCanvas
*canvas
, wxDC
& dc
)
945 DrawBlankPage(canvas
, dc
);
947 if (!m_previewBitmap
)
948 if (!RenderPage(m_currentPage
))
951 if (!m_previewBitmap
)
957 int canvasWidth
, canvasHeight
;
958 canvas
->GetSize(&canvasWidth
, &canvasHeight
);
960 double zoomScale
= ((float)m_currentZoom
/(float)100);
961 double actualWidth
= (zoomScale
*m_pageWidth
*m_previewScale
);
962 // float actualHeight = (float)(zoomScale*m_pageHeight*m_previewScale);
964 int x
= (int) ((canvasWidth
- actualWidth
)/2.0);
965 if (x
< m_leftMargin
)
970 temp_dc
.SelectObject(*m_previewBitmap
);
972 dc
.Blit(x
, y
, m_previewBitmap
->GetWidth(), m_previewBitmap
->GetHeight(), &temp_dc
, 0, 0);
974 temp_dc
.SelectObject(wxNullBitmap
);
979 // Adjusts the scrollbars for the current scale
980 void wxPrintPreviewBase::AdjustScrollbars(wxPreviewCanvas
*canvas
)
985 int canvasWidth
, canvasHeight
;
986 canvas
->GetSize(&canvasWidth
, &canvasHeight
);
988 double zoomScale
= ((float)m_currentZoom
/(float)100);
989 double actualWidth
= (zoomScale
*m_pageWidth
*m_previewScale
);
990 double actualHeight
= (zoomScale
*m_pageHeight
*m_previewScale
);
992 // Set the scrollbars appropriately
993 int totalWidth
= (int)(actualWidth
+ 2*m_leftMargin
);
994 int totalHeight
= (int)(actualHeight
+ 2*m_topMargin
);
995 int scrollUnitsX
= totalWidth
/10;
996 int scrollUnitsY
= totalHeight
/10;
997 wxSize virtualSize
= canvas
->GetVirtualSize();
998 if (virtualSize
.GetWidth() != totalWidth
|| virtualSize
.GetHeight() != totalHeight
)
999 canvas
->SetScrollbars(10, 10, scrollUnitsX
, scrollUnitsY
, 0, 0, true);
1002 bool wxPrintPreviewBase::RenderPage(int pageNum
)
1006 int canvasWidth
, canvasHeight
;
1008 if (!m_previewCanvas
)
1010 wxFAIL_MSG(_T("wxPrintPreviewBase::RenderPage: must use wxPrintPreviewBase::SetCanvas to let me know about the canvas!"));
1014 m_previewCanvas
->GetSize(&canvasWidth
, &canvasHeight
);
1016 double zoomScale
= (m_currentZoom
/100.0);
1017 int actualWidth
= (int)(zoomScale
*m_pageWidth
*m_previewScale
);
1018 int actualHeight
= (int)(zoomScale
*m_pageHeight
*m_previewScale
);
1020 if (!m_previewBitmap
)
1022 m_previewBitmap
= new wxBitmap((int)actualWidth
, (int)actualHeight
);
1023 if (!m_previewBitmap
|| !m_previewBitmap
->Ok())
1025 if (m_previewBitmap
) {
1026 delete m_previewBitmap
;
1027 m_previewBitmap
= NULL
;
1029 wxMessageBox(_("Sorry, not enough memory to create a preview."), _("Print Preview Failure"), wxOK
);
1034 wxMemoryDC memoryDC
;
1035 memoryDC
.SelectObject(*m_previewBitmap
);
1039 m_previewPrintout
->SetDC(&memoryDC
);
1040 m_previewPrintout
->SetPageSizePixels(m_pageWidth
, m_pageHeight
);
1042 // Need to delay OnPreparePrinting until here, so we have enough information.
1043 if (!m_printingPrepared
)
1045 m_previewPrintout
->OnPreparePrinting();
1047 m_previewPrintout
->GetPageInfo(&m_minPage
, &m_maxPage
, &selFrom
, &selTo
);
1048 m_printingPrepared
= true;
1051 m_previewPrintout
->OnBeginPrinting();
1053 if (!m_previewPrintout
->OnBeginDocument(m_printDialogData
.GetFromPage(), m_printDialogData
.GetToPage()))
1055 wxMessageBox(_("Could not start document preview."), _("Print Preview Failure"), wxOK
);
1057 memoryDC
.SelectObject(wxNullBitmap
);
1059 delete m_previewBitmap
;
1060 m_previewBitmap
= NULL
;
1064 m_previewPrintout
->OnPrintPage(pageNum
);
1065 m_previewPrintout
->OnEndDocument();
1066 m_previewPrintout
->OnEndPrinting();
1068 m_previewPrintout
->SetDC(NULL
);
1070 memoryDC
.SelectObject(wxNullBitmap
);
1075 status
= wxString::Format(_("Page %d of %d"), pageNum
, m_maxPage
);
1077 status
= wxString::Format(_("Page %d"), pageNum
);
1080 m_previewFrame
->SetStatusText(status
);
1087 bool wxPrintPreviewBase::DrawBlankPage(wxPreviewCanvas
*canvas
, wxDC
& dc
)
1089 int canvasWidth
, canvasHeight
;
1090 canvas
->GetSize(&canvasWidth
, &canvasHeight
);
1092 float zoomScale
= (float)((float)m_currentZoom
/(float)100);
1093 float actualWidth
= zoomScale
*m_pageWidth
*m_previewScale
;
1094 float actualHeight
= zoomScale
*m_pageHeight
*m_previewScale
;
1096 float x
= (float)((canvasWidth
- actualWidth
)/2.0);
1097 if (x
< m_leftMargin
)
1098 x
= (float)m_leftMargin
;
1099 float y
= (float)m_topMargin
;
1101 // Draw shadow, allowing for 1-pixel border AROUND the actual page
1102 int shadowOffset
= 4;
1103 dc
.SetPen(*wxBLACK_PEN
);
1104 dc
.SetBrush(*wxBLACK_BRUSH
);
1106 dc.DrawRectangle((int)(x-1 + shadowOffset), (int)(y-1 + shadowOffset), (int)(actualWidth+2), (int)(actualHeight+2));
1108 dc
.DrawRectangle((int)(x
+ shadowOffset
), (int)(y
+ actualHeight
+1), (int)(actualWidth
), shadowOffset
);
1109 dc
.DrawRectangle((int)(x
+ actualWidth
), (int)(y
+ shadowOffset
), shadowOffset
, (int)(actualHeight
));
1111 // Draw blank page allowing for 1-pixel border AROUND the actual page
1112 dc
.SetPen(*wxBLACK_PEN
);
1113 dc
.SetBrush(*wxWHITE_BRUSH
);
1116 wxRegion update_region = canvas->GetUpdateRegion();
1117 wxRect r = update_region.GetBox();
1119 printf( "x: %d y: %d w: %d h: %d.\n", (int)r.x, (int)r.y, (int)r.width, (int)r.height );
1122 dc
.DrawRectangle((int)(x
-2), (int)(y
-1), (int)(actualWidth
+3), (int)(actualHeight
+2));
1127 void wxPrintPreviewBase::SetZoom(int percent
)
1129 if (m_currentZoom
== percent
)
1132 m_currentZoom
= percent
;
1133 if (m_previewBitmap
)
1135 delete m_previewBitmap
;
1136 m_previewBitmap
= NULL
;
1139 if (m_previewCanvas
)
1141 AdjustScrollbars(m_previewCanvas
);
1142 RenderPage(m_currentPage
);
1143 ((wxScrolledWindow
*) m_previewCanvas
)->Scroll(0, 0);
1144 m_previewCanvas
->ClearBackground();
1145 m_previewCanvas
->Refresh();
1146 m_previewCanvas
->SetFocus();
1150 wxPrintDialogData
& wxPrintPreviewBase::GetPrintDialogData()
1152 return m_printDialogData
;
1155 int wxPrintPreviewBase::GetZoom() const
1156 { return m_currentZoom
; }
1157 int wxPrintPreviewBase::GetMaxPage() const
1158 { return m_maxPage
; }
1159 int wxPrintPreviewBase::GetMinPage() const
1160 { return m_minPage
; }
1161 bool wxPrintPreviewBase::Ok() const
1163 void wxPrintPreviewBase::SetOk(bool ok
)
1165 //----------------------------------------------------------------------------
1167 //----------------------------------------------------------------------------
1169 IMPLEMENT_CLASS(wxPrintPreview
, wxPrintPreviewBase
)
1171 wxPrintPreview::wxPrintPreview(wxPrintout
*printout
,
1172 wxPrintout
*printoutForPrinting
,
1173 wxPrintDialogData
*data
) :
1174 wxPrintPreviewBase( printout
, printoutForPrinting
, data
)
1176 m_pimpl
= wxPrintFactory::GetFactory()->
1177 CreatePrintPreview( printout
, printoutForPrinting
, data
);
1180 wxPrintPreview::wxPrintPreview(wxPrintout
*printout
,
1181 wxPrintout
*printoutForPrinting
,
1182 wxPrintData
*data
) :
1183 wxPrintPreviewBase( printout
, printoutForPrinting
, data
)
1185 m_pimpl
= wxPrintFactory::GetFactory()->
1186 CreatePrintPreview( printout
, printoutForPrinting
, data
);
1189 wxPrintPreview::~wxPrintPreview()
1193 // don't delete twice
1194 m_printPrintout
= NULL
;
1195 m_previewPrintout
= NULL
;
1196 m_previewBitmap
= NULL
;
1199 bool wxPrintPreview::SetCurrentPage(int pageNum
)
1201 return m_pimpl
->SetCurrentPage( pageNum
);
1204 int wxPrintPreview::GetCurrentPage() const
1206 return m_pimpl
->GetCurrentPage();
1209 void wxPrintPreview::SetPrintout(wxPrintout
*printout
)
1211 m_pimpl
->SetPrintout( printout
);
1214 wxPrintout
*wxPrintPreview::GetPrintout() const
1216 return m_pimpl
->GetPrintout();
1219 wxPrintout
*wxPrintPreview::GetPrintoutForPrinting() const
1221 return m_pimpl
->GetPrintoutForPrinting();
1224 void wxPrintPreview::SetFrame(wxFrame
*frame
)
1226 m_pimpl
->SetFrame( frame
);
1229 void wxPrintPreview::SetCanvas(wxPreviewCanvas
*canvas
)
1231 m_pimpl
->SetCanvas( canvas
);
1234 wxFrame
*wxPrintPreview::GetFrame() const
1236 return m_pimpl
->GetFrame();
1239 wxPreviewCanvas
*wxPrintPreview::GetCanvas() const
1241 return m_pimpl
->GetCanvas();
1244 bool wxPrintPreview::PaintPage(wxPreviewCanvas
*canvas
, wxDC
& dc
)
1246 return m_pimpl
->PaintPage( canvas
, dc
);
1249 bool wxPrintPreview::DrawBlankPage(wxPreviewCanvas
*canvas
, wxDC
& dc
)
1251 return m_pimpl
->DrawBlankPage( canvas
, dc
);
1254 void wxPrintPreview::AdjustScrollbars(wxPreviewCanvas
*canvas
)
1256 m_pimpl
->AdjustScrollbars( canvas
);
1259 bool wxPrintPreview::RenderPage(int pageNum
)
1261 return m_pimpl
->RenderPage( pageNum
);
1264 void wxPrintPreview::SetZoom(int percent
)
1266 m_pimpl
->SetZoom( percent
);
1269 wxPrintDialogData
& wxPrintPreview::GetPrintDialogData()
1271 return m_pimpl
->GetPrintDialogData();
1274 int wxPrintPreview::GetMaxPage() const
1276 return m_pimpl
->GetMaxPage();
1279 int wxPrintPreview::GetMinPage() const
1281 return m_pimpl
->GetMinPage();
1284 bool wxPrintPreview::Ok() const
1286 return m_pimpl
->Ok();
1289 void wxPrintPreview::SetOk(bool ok
)
1291 m_pimpl
->SetOk( ok
);
1294 bool wxPrintPreview::Print(bool interactive
)
1296 return m_pimpl
->Print( interactive
);
1299 void wxPrintPreview::DetermineScaling()
1301 m_pimpl
->DetermineScaling();
1305 #endif // wxUSE_PRINTING_ARCHITECTURE