1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: Printing framework base class implementation
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
13 #pragma implementation "prntbase.h"
14 #pragma implementation "printdlg.h"
17 // For compilers that support precompilation, includes "wx.h".
18 #include "wx/wxprec.h"
26 #if wxUSE_PRINTING_ARCHITECTURE
32 #include "wx/msgdlg.h"
33 #include "wx/layout.h"
34 #include "wx/choice.h"
35 #include "wx/button.h"
36 #include "wx/settings.h"
37 #include "wx/dcmemory.h"
38 #include "wx/stattext.h"
40 #include "wx/textdlg.h"
44 #include "wx/prntbase.h"
45 #include "wx/dcprint.h"
46 #include "wx/printdlg.h"
48 #include "wx/module.h"
54 #include "wx/msw/private.h"
62 IMPLEMENT_CLASS(wxPrintPreviewBase
, wxObject
)
64 //----------------------------------------------------------------------------
66 //----------------------------------------------------------------------------
68 wxPrintFactory
*wxPrintFactory::m_factory
= NULL
;
70 void wxPrintFactory::SetPrintFactory( wxPrintFactory
*factory
)
72 if (wxPrintFactory::m_factory
)
73 delete wxPrintFactory::m_factory
;
75 wxPrintFactory::m_factory
= factory
;
78 wxPrintFactory
*wxPrintFactory::GetFactory()
80 if (!wxPrintFactory::m_factory
)
81 wxPrintFactory::m_factory
= new wxNativePrintFactory
;
83 return wxPrintFactory::m_factory
;
86 //----------------------------------------------------------------------------
87 // wxNativePrintFactory
88 //----------------------------------------------------------------------------
90 wxPrinterBase
*wxNativePrintFactory::CreatePrinter( wxPrintDialogData
*data
)
92 #if defined(__WXMSW__) && !defined(__WXUNIVERSAL__)
93 return new wxWindowsPrinter( data
);
94 #elif defined(__WXMAC__)
95 return new wxMacPrinter( data
);
96 #elif defined(__WXPM__)
97 return new wxOS2Printer( data
);
99 return new wxPostScriptPrinter( data
);
103 wxPrintPreviewBase
*wxNativePrintFactory::CreatePrintPreview( wxPrintout
*preview
,
104 wxPrintout
*printout
, wxPrintDialogData
*data
)
106 #if defined(__WXMSW__) && !defined(__WXUNIVERSAL__)
107 return new wxWindowsPrintPreview( preview
, printout
, data
);
108 #elif defined(__WXMAC__)
109 return new wxMacPrintPreview( preview
, printout
, data
);
110 #elif defined(__WXPM__)
111 return new wxOS2PrintPreview( preview
, printout
, data
);
113 return new wxPostScriptPrintPreview( preview
, printout
, data
);
117 wxPrintPreviewBase
*wxNativePrintFactory::CreatePrintPreview( wxPrintout
*preview
,
118 wxPrintout
*printout
, wxPrintData
*data
)
120 #if defined(__WXMSW__) && !defined(__WXUNIVERSAL__)
121 return new wxWindowsPrintPreview( preview
, printout
, data
);
122 #elif defined(__WXMAC__)
123 return new wxMacPrintPreview( preview
, printout
, data
);
124 #elif defined(__WXPM__)
125 return new wxOS2PrintPreview( preview
, printout
, data
);
127 return new wxPostScriptPrintPreview( preview
, printout
, data
);
131 wxPrintDialogBase
*wxNativePrintFactory::CreatePrintDialog( wxWindow
*parent
,
132 wxPrintDialogData
*data
)
134 #if defined(__WXMSW__) && !defined(__WXUNIVERSAL__)
135 return new wxWindowsPrintDialog( parent
, data
);
136 #elif defined(__WXMAC__)
137 return new wxMacPrintDialog( parent
, data
);
139 return new wxGenericPrintDialog( parent
, data
);
143 wxPrintDialogBase
*wxNativePrintFactory::CreatePrintDialog( wxWindow
*parent
,
146 #if defined(__WXMSW__) && !defined(__WXUNIVERSAL__)
147 return new wxWindowsPrintDialog( parent
, data
);
148 #elif defined(__WXMAC__)
149 return new wxMacPrintDialog( parent
, data
);
151 return new wxGenericPrintDialog( parent
, data
);
155 bool wxNativePrintFactory::HasPrintSetupDialog()
157 #if defined(__WXMSW__) && !defined(__WXUNIVERSAL__)
159 #elif defined(__WXMAC__)
162 // Only here do we need to provide the print setup
163 // dialog ourselves, the other platforms either have
164 // none, don't make it accessible or let you configure
165 // the printer from the wxPrintDialog anyway.
171 wxDialog
*wxNativePrintFactory::CreatePrintSetupDialog( wxWindow
*parent
, wxPrintData
*data
)
173 #if defined(__WXMSW__) && !defined(__WXUNIVERSAL__)
175 #elif defined(__WXMAC__)
178 // Only here do we need to provide the print setup
179 // dialog ourselves, the other platforms either have
180 // none, don't make it accessible or let you configure
181 // the printer from the wxPrintDialog anyway.
182 return new wxGenericPrintSetupDialog( parent
, data
);
186 bool wxNativePrintFactory::HasOwnPrintToFile()
188 // Only relevant for PostScript and here the
189 // setup dialog provides no "print to file"
190 // option. In the GNOME setup dialog, the
191 // setup dialog has its own print to file.
195 bool wxNativePrintFactory::HasPrinterLine()
197 // Only relevant for PostScript for now
201 wxString
wxNativePrintFactory::CreatePrinterLine()
203 // Only relevant for PostScript for now
205 // We should query "lpstat -d" here
206 return _("Generic PostScript");
209 bool wxNativePrintFactory::HasStatusLine()
211 // Only relevant for PostScript for now
215 wxString
wxNativePrintFactory::CreateStatusLine()
217 // Only relevant for PostScript for now
219 // We should query "lpstat -r" or "lpstat -p" here
223 wxPrintNativeDataBase
*wxNativePrintFactory::CreatePrintNativeData()
225 #if defined(__WXMSW__) && !defined(__WXUNIVERSAL__)
226 return new wxWindowsPrintNativeData
;
227 #elif defined(__WXMAC__)
228 return new wxMacPrintNativeData
;
230 return new wxPostScriptPrintNativeData
;
234 //----------------------------------------------------------------------------
235 // wxPrintNativeDataBase
236 //----------------------------------------------------------------------------
238 IMPLEMENT_ABSTRACT_CLASS(wxPrintNativeDataBase
, wxObject
)
240 wxPrintNativeDataBase::wxPrintNativeDataBase()
245 //----------------------------------------------------------------------------
247 //----------------------------------------------------------------------------
249 IMPLEMENT_CLASS(wxPrinterBase
, wxObject
)
251 wxPrinterBase::wxPrinterBase(wxPrintDialogData
*data
)
253 m_currentPrintout
= (wxPrintout
*) NULL
;
254 sm_abortWindow
= (wxWindow
*) NULL
;
257 m_printDialogData
= (*data
);
258 sm_lastError
= wxPRINTER_NO_ERROR
;
261 wxWindow
*wxPrinterBase::sm_abortWindow
= (wxWindow
*) NULL
;
262 bool wxPrinterBase::sm_abortIt
= false;
263 wxPrinterError
wxPrinterBase::sm_lastError
= wxPRINTER_NO_ERROR
;
265 wxPrinterBase::~wxPrinterBase()
269 wxWindow
*wxPrinterBase::CreateAbortWindow(wxWindow
*parent
, wxPrintout
* printout
)
271 wxPrintAbortDialog
*dialog
= new wxPrintAbortDialog(parent
, _("Printing ") , wxDefaultPosition
, wxDefaultSize
, wxDEFAULT_DIALOG_STYLE
);
273 wxBoxSizer
*button_sizer
= new wxBoxSizer( wxVERTICAL
);
274 button_sizer
->Add( new wxStaticText(dialog
, wxID_ANY
, _("Please wait while printing\n") + printout
->GetTitle() ), 0, wxALL
, 10 );
275 button_sizer
->Add( new wxButton( dialog
, wxID_CANCEL
, wxT("Cancel") ), 0, wxALL
| wxALIGN_CENTER
, 10 );
277 dialog
->SetAutoLayout( true );
278 dialog
->SetSizer( button_sizer
);
280 button_sizer
->Fit(dialog
);
281 button_sizer
->SetSizeHints (dialog
) ;
286 void wxPrinterBase::ReportError(wxWindow
*parent
, wxPrintout
*WXUNUSED(printout
), const wxString
& message
)
288 wxMessageBox(message
, _("Printing Error"), wxOK
, parent
);
291 wxPrintDialogData
& wxPrinterBase::GetPrintDialogData() const
293 return (wxPrintDialogData
&) m_printDialogData
;
296 //----------------------------------------------------------------------------
298 //----------------------------------------------------------------------------
300 IMPLEMENT_CLASS(wxPrinter
, wxPrinterBase
)
302 wxPrinter::wxPrinter(wxPrintDialogData
*data
)
304 m_pimpl
= wxPrintFactory::GetFactory()->CreatePrinter( data
);
307 wxPrinter::~wxPrinter()
312 wxWindow
*wxPrinter::CreateAbortWindow(wxWindow
*parent
, wxPrintout
*printout
)
314 return m_pimpl
->CreateAbortWindow( parent
, printout
);
317 void wxPrinter::ReportError(wxWindow
*parent
, wxPrintout
*printout
, const wxString
& message
)
319 m_pimpl
->ReportError( parent
, printout
, message
);
322 bool wxPrinter::Setup(wxWindow
*parent
)
324 return m_pimpl
->Setup( parent
);
327 bool wxPrinter::Print(wxWindow
*parent
, wxPrintout
*printout
, bool prompt
)
329 return m_pimpl
->Print( parent
, printout
, prompt
);
332 wxDC
* wxPrinter::PrintDialog(wxWindow
*parent
)
334 return m_pimpl
->PrintDialog( parent
);
337 wxPrintDialogData
& wxPrinter::GetPrintDialogData() const
339 return m_pimpl
->GetPrintDialogData();
342 // ---------------------------------------------------------------------------
343 // wxPrintDialogBase: the common dialog for printing.
344 // ---------------------------------------------------------------------------
346 IMPLEMENT_ABSTRACT_CLASS(wxPrintDialogBase
, wxObject
)
348 wxPrintDialogBase::wxPrintDialogBase(wxWindow
*parent
,
350 const wxString
&title
,
358 title
.empty() ? wxString(_("Print")) : title
,
366 // ---------------------------------------------------------------------------
367 // wxPrintDialog: the common dialog for printing.
368 // ---------------------------------------------------------------------------
370 IMPLEMENT_CLASS(wxPrintDialog
, wxObject
)
372 wxPrintDialog::wxPrintDialog(wxWindow
*parent
, wxPrintDialogData
* data
)
374 m_pimpl
= wxPrintFactory::GetFactory()->CreatePrintDialog( parent
, data
);
377 wxPrintDialog::wxPrintDialog(wxWindow
*parent
, wxPrintData
* data
)
379 m_pimpl
= wxPrintFactory::GetFactory()->CreatePrintDialog( parent
, data
);
382 wxPrintDialog::~wxPrintDialog()
387 int wxPrintDialog::ShowModal()
389 return m_pimpl
->ShowModal();
392 wxPrintDialogData
& wxPrintDialog::GetPrintDialogData()
394 return m_pimpl
->GetPrintDialogData();
397 wxPrintData
& wxPrintDialog::GetPrintData()
399 return m_pimpl
->GetPrintData();
401 wxDC
*wxPrintDialog::GetPrintDC()
403 return m_pimpl
->GetPrintDC();
406 //----------------------------------------------------------------------------
407 // wxPrintAbortDialog
408 //----------------------------------------------------------------------------
410 BEGIN_EVENT_TABLE(wxPrintAbortDialog
, wxDialog
)
411 EVT_BUTTON(wxID_CANCEL
, wxPrintAbortDialog::OnCancel
)
414 void wxPrintAbortDialog::OnCancel(wxCommandEvent
& WXUNUSED(event
))
416 wxPrinterBase::sm_abortIt
= true;
417 wxPrinterBase::sm_abortWindow
->Show(false);
418 wxPrinterBase::sm_abortWindow
->Close(true);
419 wxPrinterBase::sm_abortWindow
= (wxWindow
*) NULL
;
422 //----------------------------------------------------------------------------
424 //----------------------------------------------------------------------------
426 IMPLEMENT_ABSTRACT_CLASS(wxPrintout
, wxObject
)
428 wxPrintout::wxPrintout(const wxString
& title
)
430 m_printoutTitle
= title
;
431 m_printoutDC
= (wxDC
*) NULL
;
434 m_pageWidthPixels
= 0;
435 m_pageHeightPixels
= 0;
443 wxPrintout::~wxPrintout()
447 bool wxPrintout::OnBeginDocument(int WXUNUSED(startPage
), int WXUNUSED(endPage
))
449 return GetDC()->StartDoc(_("Printing ") + m_printoutTitle
);
452 void wxPrintout::OnEndDocument()
457 void wxPrintout::OnBeginPrinting()
461 void wxPrintout::OnEndPrinting()
465 bool wxPrintout::HasPage(int page
)
470 void wxPrintout::GetPageInfo(int *minPage
, int *maxPage
, int *fromPage
, int *toPage
)
478 //----------------------------------------------------------------------------
480 //----------------------------------------------------------------------------
482 IMPLEMENT_CLASS(wxPreviewCanvas
, wxWindow
)
484 BEGIN_EVENT_TABLE(wxPreviewCanvas
, wxScrolledWindow
)
485 EVT_PAINT(wxPreviewCanvas::OnPaint
)
486 EVT_CHAR(wxPreviewCanvas::OnChar
)
487 EVT_SYS_COLOUR_CHANGED(wxPreviewCanvas::OnSysColourChanged
)
490 // VZ: the current code doesn't refresh properly without
491 // wxFULL_REPAINT_ON_RESIZE, this must be fixed as otherwise we have
492 // really horrible flicker when resizing the preview frame, but without
493 // this style it simply doesn't work correctly at all...
494 wxPreviewCanvas::wxPreviewCanvas(wxPrintPreviewBase
*preview
, wxWindow
*parent
,
495 const wxPoint
& pos
, const wxSize
& size
, long style
, const wxString
& name
):
496 wxScrolledWindow(parent
, wxID_ANY
, pos
, size
, style
| wxFULL_REPAINT_ON_RESIZE
, name
)
498 m_printPreview
= preview
;
500 // The app workspace colour is always white, but we should have
501 // a contrast with the page.
502 wxSystemColour colourIndex
= wxSYS_COLOUR_3DDKSHADOW
;
504 wxSystemColour colourIndex
= wxSYS_COLOUR_APPWORKSPACE
;
506 SetBackgroundColour(wxSystemSettings::GetColour(colourIndex
));
508 SetScrollbars(10, 10, 100, 100);
511 wxPreviewCanvas::~wxPreviewCanvas()
515 void wxPreviewCanvas::OnPaint(wxPaintEvent
& WXUNUSED(event
))
522 if (!GetUpdateRegion().IsEmpty())
523 dc.SetClippingRegion( GetUpdateRegion() );
529 m_printPreview
->PaintPage(this, dc
);
533 // Responds to colour changes, and passes event on to children.
534 void wxPreviewCanvas::OnSysColourChanged(wxSysColourChangedEvent
& event
)
537 // The app workspace colour is always white, but we should have
538 // a contrast with the page.
539 wxSystemColour colourIndex
= wxSYS_COLOUR_3DDKSHADOW
;
541 wxSystemColour colourIndex
= wxSYS_COLOUR_APPWORKSPACE
;
543 SetBackgroundColour(wxSystemSettings::GetColour(colourIndex
));
546 // Propagate the event to the non-top-level children
547 wxWindow::OnSysColourChanged(event
);
550 void wxPreviewCanvas::OnChar(wxKeyEvent
&event
)
552 wxPreviewControlBar
* controlBar
= ((wxPreviewFrame
*) GetParent())->GetControlBar();
553 if (event
.GetKeyCode() == WXK_ESCAPE
)
555 ((wxPreviewFrame
*) GetParent())->Close(true);
558 else if (event
.GetKeyCode() == WXK_TAB
)
560 controlBar
->OnGoto();
563 else if (event
.GetKeyCode() == WXK_RETURN
)
565 controlBar
->OnPrint();
569 if (!event
.ControlDown())
575 switch(event
.GetKeyCode())
578 controlBar
->OnNext(); break;
580 controlBar
->OnPrevious(); break;
582 controlBar
->OnFirst(); break;
584 controlBar
->OnLast(); break;
590 //----------------------------------------------------------------------------
591 // wxPreviewControlBar
592 //----------------------------------------------------------------------------
594 IMPLEMENT_CLASS(wxPreviewControlBar
, wxWindow
)
596 BEGIN_EVENT_TABLE(wxPreviewControlBar
, wxPanel
)
597 EVT_BUTTON(wxID_PREVIEW_CLOSE
, wxPreviewControlBar::OnWindowClose
)
598 EVT_BUTTON(wxID_PREVIEW_PRINT
, wxPreviewControlBar::OnPrintButton
)
599 EVT_BUTTON(wxID_PREVIEW_PREVIOUS
, wxPreviewControlBar::OnPreviousButton
)
600 EVT_BUTTON(wxID_PREVIEW_NEXT
, wxPreviewControlBar::OnNextButton
)
601 EVT_BUTTON(wxID_PREVIEW_FIRST
, wxPreviewControlBar::OnFirstButton
)
602 EVT_BUTTON(wxID_PREVIEW_LAST
, wxPreviewControlBar::OnLastButton
)
603 EVT_BUTTON(wxID_PREVIEW_GOTO
, wxPreviewControlBar::OnGotoButton
)
604 EVT_CHOICE(wxID_PREVIEW_ZOOM
, wxPreviewControlBar::OnZoom
)
605 EVT_PAINT(wxPreviewControlBar::OnPaint
)
608 wxPreviewControlBar::wxPreviewControlBar(wxPrintPreviewBase
*preview
, long buttons
,
609 wxWindow
*parent
, const wxPoint
& pos
, const wxSize
& size
,
610 long style
, const wxString
& name
):
611 wxPanel(parent
, wxID_ANY
, pos
, size
, style
, name
)
613 m_printPreview
= preview
;
614 m_closeButton
= (wxButton
*) NULL
;
615 m_nextPageButton
= (wxButton
*) NULL
;
616 m_previousPageButton
= (wxButton
*) NULL
;
617 m_printButton
= (wxButton
*) NULL
;
618 m_zoomControl
= (wxChoice
*) NULL
;
619 m_buttonFlags
= buttons
;
622 wxPreviewControlBar::~wxPreviewControlBar()
626 void wxPreviewControlBar::OnPaint(wxPaintEvent
& WXUNUSED(event
))
632 dc
.SetPen(*wxBLACK_PEN
);
633 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
634 dc
.DrawLine( 0, h
-1, w
, h
-1 );
637 void wxPreviewControlBar::OnWindowClose(wxCommandEvent
& WXUNUSED(event
))
639 wxPreviewFrame
*frame
= (wxPreviewFrame
*)GetParent();
643 void wxPreviewControlBar::OnPrint(void)
645 wxPrintPreviewBase
*preview
= GetPrintPreview();
646 preview
->Print(true);
649 void wxPreviewControlBar::OnNext(void)
651 wxPrintPreviewBase
*preview
= GetPrintPreview();
654 int currentPage
= preview
->GetCurrentPage();
655 if ((preview
->GetMaxPage() > 0) &&
656 (currentPage
< preview
->GetMaxPage()) &&
657 preview
->GetPrintout()->HasPage(currentPage
+ 1))
659 preview
->SetCurrentPage(currentPage
+ 1);
664 void wxPreviewControlBar::OnPrevious(void)
666 wxPrintPreviewBase
*preview
= GetPrintPreview();
669 int currentPage
= preview
->GetCurrentPage();
670 if ((preview
->GetMinPage() > 0) &&
671 (currentPage
> preview
->GetMinPage()) &&
672 preview
->GetPrintout()->HasPage(currentPage
- 1))
674 preview
->SetCurrentPage(currentPage
- 1);
679 void wxPreviewControlBar::OnFirst(void)
681 wxPrintPreviewBase
*preview
= GetPrintPreview();
684 int currentPage
= preview
->GetMinPage();
685 if (preview
->GetPrintout()->HasPage(currentPage
))
687 preview
->SetCurrentPage(currentPage
);
692 void wxPreviewControlBar::OnLast(void)
694 wxPrintPreviewBase
*preview
= GetPrintPreview();
697 int currentPage
= preview
->GetMaxPage();
698 if (preview
->GetPrintout()->HasPage(currentPage
))
700 preview
->SetCurrentPage(currentPage
);
705 void wxPreviewControlBar::OnGoto(void)
707 wxPrintPreviewBase
*preview
= GetPrintPreview();
712 if (preview
->GetMinPage() > 0)
717 strPrompt
.Printf( _("Enter a page number between %d and %d:"),
718 preview
->GetMinPage(), preview
->GetMaxPage());
719 strPage
.Printf( wxT("%d"), preview
->GetCurrentPage() );
722 wxGetTextFromUser( strPrompt
, _("Goto Page"), strPage
, GetParent());
724 if ( strPage
.ToLong( ¤tPage
) )
725 if (preview
->GetPrintout()->HasPage(currentPage
))
727 preview
->SetCurrentPage(currentPage
);
733 void wxPreviewControlBar::OnZoom(wxCommandEvent
& WXUNUSED(event
))
735 int zoom
= GetZoomControl();
736 if (GetPrintPreview())
737 GetPrintPreview()->SetZoom(zoom
);
740 void wxPreviewControlBar::CreateButtons()
742 SetSize(0, 0, 400, 40);
744 wxBoxSizer
*item0
= new wxBoxSizer( wxHORIZONTAL
);
746 m_closeButton
= new wxButton( this, wxID_PREVIEW_CLOSE
, _("&Close"), wxDefaultPosition
, wxDefaultSize
, 0 );
747 item0
->Add( m_closeButton
, 0, wxALIGN_CENTRE
|wxALL
, 5 );
749 if (m_buttonFlags
& wxPREVIEW_PRINT
)
751 m_printButton
= new wxButton( this, wxID_PREVIEW_PRINT
, _("&Print..."), wxDefaultPosition
, wxDefaultSize
, 0 );
752 item0
->Add( m_printButton
, 0, wxALIGN_CENTRE
|wxALL
, 5 );
755 if (m_buttonFlags
& wxPREVIEW_FIRST
)
757 m_firstPageButton
= new wxButton( this, wxID_PREVIEW_FIRST
, _("|<<"), wxDefaultPosition
, wxDefaultSize
, wxBU_EXACTFIT
);
758 item0
->Add( m_firstPageButton
, 0, wxALIGN_CENTRE
|wxALL
, 5 );
761 if (m_buttonFlags
& wxPREVIEW_PREVIOUS
)
763 m_previousPageButton
= new wxButton( this, wxID_PREVIEW_PREVIOUS
, _("<<"), wxDefaultPosition
, wxDefaultSize
, wxBU_EXACTFIT
);
764 item0
->Add( m_previousPageButton
, 0, wxALIGN_CENTRE
|wxRIGHT
|wxTOP
|wxBOTTOM
, 5 );
767 if (m_buttonFlags
& wxPREVIEW_NEXT
)
769 m_nextPageButton
= new wxButton( this, wxID_PREVIEW_NEXT
, _(">>"), wxDefaultPosition
, wxDefaultSize
, wxBU_EXACTFIT
);
770 item0
->Add( m_nextPageButton
, 0, wxALIGN_CENTRE
|wxRIGHT
|wxTOP
|wxBOTTOM
, 5 );
773 if (m_buttonFlags
& wxPREVIEW_LAST
)
775 m_lastPageButton
= new wxButton( this, wxID_PREVIEW_LAST
, _(">>|"), wxDefaultPosition
, wxDefaultSize
, wxBU_EXACTFIT
);
776 item0
->Add( m_lastPageButton
, 0, wxALIGN_CENTRE
|wxRIGHT
|wxTOP
|wxBOTTOM
, 5 );
779 if (m_buttonFlags
& wxPREVIEW_GOTO
)
781 m_gotoPageButton
= new wxButton( this, wxID_PREVIEW_GOTO
, _("&Goto..."), wxDefaultPosition
, wxDefaultSize
, 0 );
782 item0
->Add( m_gotoPageButton
, 0, wxALIGN_CENTRE
|wxALL
, 5 );
785 if (m_buttonFlags
& wxPREVIEW_ZOOM
)
789 wxT("10%"), wxT("15%"), wxT("20%"), wxT("25%"), wxT("30%"), wxT("35%"), wxT("40%"), wxT("45%"), wxT("50%"), wxT("55%"),
790 wxT("60%"), wxT("65%"), wxT("70%"), wxT("75%"), wxT("80%"), wxT("85%"), wxT("90%"), wxT("95%"), wxT("100%"), wxT("110%"),
791 wxT("120%"), wxT("150%"), wxT("200%")
793 int n
= WXSIZEOF(choices
);
795 m_zoomControl
= new wxChoice( this, wxID_PREVIEW_ZOOM
, wxDefaultPosition
, wxSize(70,wxDefaultCoord
), n
, choices
, 0 );
796 item0
->Add( m_zoomControl
, 0, wxALIGN_CENTRE
|wxALL
, 5 );
797 SetZoomControl(m_printPreview
->GetZoom());
804 void wxPreviewControlBar::SetZoomControl(int zoom
)
808 int n
, count
= m_zoomControl
->GetCount();
810 for (n
=0; n
<count
; n
++)
812 if (m_zoomControl
->GetString(n
).BeforeFirst(wxT('%')).ToLong(&val
) &&
815 m_zoomControl
->SetSelection(n
);
820 m_zoomControl
->SetSelection(count
-1);
824 int wxPreviewControlBar::GetZoomControl()
826 if (m_zoomControl
&& (m_zoomControl
->GetStringSelection() != wxEmptyString
))
829 if (m_zoomControl
->GetStringSelection().BeforeFirst(wxT('%')).ToLong(&val
))
841 IMPLEMENT_CLASS(wxPreviewFrame
, wxFrame
)
843 BEGIN_EVENT_TABLE(wxPreviewFrame
, wxFrame
)
844 EVT_CLOSE(wxPreviewFrame::OnCloseWindow
)
847 wxPreviewFrame::wxPreviewFrame(wxPrintPreviewBase
*preview
, wxWindow
*parent
, const wxString
& title
,
848 const wxPoint
& pos
, const wxSize
& size
, long style
, const wxString
& name
):
849 wxFrame(parent
, wxID_ANY
, title
, pos
, size
, style
, name
)
851 m_printPreview
= preview
;
853 m_previewCanvas
= NULL
;
854 m_windowDisabler
= NULL
;
856 // Give the application icon
858 wxFrame
* topFrame
= wxDynamicCast(wxTheApp
->GetTopWindow(), wxFrame
);
860 SetIcon(topFrame
->GetIcon());
864 wxPreviewFrame::~wxPreviewFrame()
868 void wxPreviewFrame::OnCloseWindow(wxCloseEvent
& WXUNUSED(event
))
870 if (m_windowDisabler
)
871 delete m_windowDisabler
;
873 // Need to delete the printout and the print preview
874 wxPrintout
*printout
= m_printPreview
->GetPrintout();
878 m_printPreview
->SetPrintout(NULL
);
879 m_printPreview
->SetCanvas(NULL
);
880 m_printPreview
->SetFrame(NULL
);
882 delete m_printPreview
;
887 void wxPreviewFrame::Initialize()
895 m_printPreview
->SetCanvas(m_previewCanvas
);
896 m_printPreview
->SetFrame(this);
898 wxBoxSizer
*item0
= new wxBoxSizer( wxVERTICAL
);
900 item0
->Add( m_controlBar
, 0, wxGROW
|wxALIGN_CENTER_VERTICAL
, 5 );
901 item0
->Add( m_previewCanvas
, 1, wxGROW
|wxALIGN_CENTER_VERTICAL
, 5 );
903 SetAutoLayout( true );
906 m_windowDisabler
= new wxWindowDisabler(this);
910 m_printPreview
->AdjustScrollbars(m_previewCanvas
);
911 m_previewCanvas
->SetFocus();
912 m_controlBar
->SetFocus();
915 void wxPreviewFrame::CreateCanvas()
917 m_previewCanvas
= new wxPreviewCanvas(m_printPreview
, this);
920 void wxPreviewFrame::CreateControlBar()
922 long buttons
= wxPREVIEW_DEFAULT
;
923 if (m_printPreview
->GetPrintoutForPrinting())
924 buttons
|= wxPREVIEW_PRINT
;
926 m_controlBar
= new wxPreviewControlBar(m_printPreview
, buttons
, this, wxPoint(0, 0), wxSize(400, 40));
927 m_controlBar
->CreateButtons();
934 wxPrintPreviewBase::wxPrintPreviewBase(wxPrintout
*printout
,
935 wxPrintout
*printoutForPrinting
,
939 m_printDialogData
= (*data
);
941 Init(printout
, printoutForPrinting
);
944 wxPrintPreviewBase::wxPrintPreviewBase(wxPrintout
*printout
,
945 wxPrintout
*printoutForPrinting
,
946 wxPrintDialogData
*data
)
949 m_printDialogData
= (*data
);
951 Init(printout
, printoutForPrinting
);
954 void wxPrintPreviewBase::Init(wxPrintout
*printout
,
955 wxPrintout
*printoutForPrinting
)
958 m_previewPrintout
= printout
;
959 if (m_previewPrintout
)
960 m_previewPrintout
->SetIsPreview(true);
962 m_printPrintout
= printoutForPrinting
;
964 m_previewCanvas
= NULL
;
965 m_previewFrame
= NULL
;
966 m_previewBitmap
= NULL
;
973 m_printingPrepared
= false;
978 wxPrintPreviewBase::~wxPrintPreviewBase()
980 if (m_previewPrintout
)
981 delete m_previewPrintout
;
983 delete m_previewBitmap
;
985 delete m_printPrintout
;
988 bool wxPrintPreviewBase::SetCurrentPage(int pageNum
)
990 if (m_currentPage
== pageNum
)
993 m_currentPage
= pageNum
;
996 delete m_previewBitmap
;
997 m_previewBitmap
= NULL
;
1000 if (m_previewCanvas
)
1002 AdjustScrollbars(m_previewCanvas
);
1004 if (!RenderPage(pageNum
))
1006 m_previewCanvas
->Refresh();
1007 m_previewCanvas
->SetFocus();
1012 int wxPrintPreviewBase::GetCurrentPage() const
1013 { return m_currentPage
; };
1014 void wxPrintPreviewBase::SetPrintout(wxPrintout
*printout
)
1015 { m_previewPrintout
= printout
; };
1016 wxPrintout
*wxPrintPreviewBase::GetPrintout() const
1017 { return m_previewPrintout
; };
1018 wxPrintout
*wxPrintPreviewBase::GetPrintoutForPrinting() const
1019 { return m_printPrintout
; };
1020 void wxPrintPreviewBase::SetFrame(wxFrame
*frame
)
1021 { m_previewFrame
= frame
; };
1022 void wxPrintPreviewBase::SetCanvas(wxPreviewCanvas
*canvas
)
1023 { m_previewCanvas
= canvas
; };
1024 wxFrame
*wxPrintPreviewBase::GetFrame() const
1025 { return m_previewFrame
; }
1026 wxPreviewCanvas
*wxPrintPreviewBase::GetCanvas() const
1027 { return m_previewCanvas
; }
1029 bool wxPrintPreviewBase::PaintPage(wxPreviewCanvas
*canvas
, wxDC
& dc
)
1031 DrawBlankPage(canvas
, dc
);
1033 if (!m_previewBitmap
)
1034 if (!RenderPage(m_currentPage
))
1037 if (!m_previewBitmap
)
1043 int canvasWidth
, canvasHeight
;
1044 canvas
->GetSize(&canvasWidth
, &canvasHeight
);
1046 double zoomScale
= ((float)m_currentZoom
/(float)100);
1047 double actualWidth
= (zoomScale
*m_pageWidth
*m_previewScale
);
1048 // float actualHeight = (float)(zoomScale*m_pageHeight*m_previewScale);
1050 int x
= (int) ((canvasWidth
- actualWidth
)/2.0);
1051 if (x
< m_leftMargin
)
1053 int y
= m_topMargin
;
1056 temp_dc
.SelectObject(*m_previewBitmap
);
1058 dc
.Blit(x
, y
, m_previewBitmap
->GetWidth(), m_previewBitmap
->GetHeight(), &temp_dc
, 0, 0);
1060 temp_dc
.SelectObject(wxNullBitmap
);
1065 // Adjusts the scrollbars for the current scale
1066 void wxPrintPreviewBase::AdjustScrollbars(wxPreviewCanvas
*canvas
)
1071 int canvasWidth
, canvasHeight
;
1072 canvas
->GetSize(&canvasWidth
, &canvasHeight
);
1074 double zoomScale
= ((float)m_currentZoom
/(float)100);
1075 double actualWidth
= (zoomScale
*m_pageWidth
*m_previewScale
);
1076 double actualHeight
= (zoomScale
*m_pageHeight
*m_previewScale
);
1078 // Set the scrollbars appropriately
1079 int totalWidth
= (int)(actualWidth
+ 2*m_leftMargin
);
1080 int totalHeight
= (int)(actualHeight
+ 2*m_topMargin
);
1081 int scrollUnitsX
= totalWidth
/10;
1082 int scrollUnitsY
= totalHeight
/10;
1083 wxSize virtualSize
= canvas
->GetVirtualSize();
1084 if (virtualSize
.GetWidth() != totalWidth
|| virtualSize
.GetHeight() != totalHeight
)
1085 canvas
->SetScrollbars(10, 10, scrollUnitsX
, scrollUnitsY
, 0, 0, true);
1088 bool wxPrintPreviewBase::RenderPage(int pageNum
)
1092 int canvasWidth
, canvasHeight
;
1094 if (!m_previewCanvas
)
1096 wxFAIL_MSG(_T("wxPrintPreviewBase::RenderPage: must use wxPrintPreviewBase::SetCanvas to let me know about the canvas!"));
1100 m_previewCanvas
->GetSize(&canvasWidth
, &canvasHeight
);
1102 double zoomScale
= (m_currentZoom
/100.0);
1103 int actualWidth
= (int)(zoomScale
*m_pageWidth
*m_previewScale
);
1104 int actualHeight
= (int)(zoomScale
*m_pageHeight
*m_previewScale
);
1106 if (!m_previewBitmap
)
1108 m_previewBitmap
= new wxBitmap((int)actualWidth
, (int)actualHeight
);
1109 if (!m_previewBitmap
|| !m_previewBitmap
->Ok())
1111 if (m_previewBitmap
) {
1112 delete m_previewBitmap
;
1113 m_previewBitmap
= NULL
;
1115 wxMessageBox(_("Sorry, not enough memory to create a preview."), _("Print Preview Failure"), wxOK
);
1120 wxMemoryDC memoryDC
;
1121 memoryDC
.SelectObject(*m_previewBitmap
);
1125 m_previewPrintout
->SetDC(&memoryDC
);
1126 m_previewPrintout
->SetPageSizePixels(m_pageWidth
, m_pageHeight
);
1128 // Need to delay OnPreparePrinting until here, so we have enough information.
1129 if (!m_printingPrepared
)
1131 m_previewPrintout
->OnPreparePrinting();
1133 m_previewPrintout
->GetPageInfo(&m_minPage
, &m_maxPage
, &selFrom
, &selTo
);
1134 m_printingPrepared
= true;
1137 m_previewPrintout
->OnBeginPrinting();
1139 if (!m_previewPrintout
->OnBeginDocument(m_printDialogData
.GetFromPage(), m_printDialogData
.GetToPage()))
1141 wxMessageBox(_("Could not start document preview."), _("Print Preview Failure"), wxOK
);
1143 memoryDC
.SelectObject(wxNullBitmap
);
1145 delete m_previewBitmap
;
1146 m_previewBitmap
= NULL
;
1150 m_previewPrintout
->OnPrintPage(pageNum
);
1151 m_previewPrintout
->OnEndDocument();
1152 m_previewPrintout
->OnEndPrinting();
1154 m_previewPrintout
->SetDC(NULL
);
1156 memoryDC
.SelectObject(wxNullBitmap
);
1161 status
= wxString::Format(_("Page %d of %d"), pageNum
, m_maxPage
);
1163 status
= wxString::Format(_("Page %d"), pageNum
);
1166 m_previewFrame
->SetStatusText(status
);
1173 bool wxPrintPreviewBase::DrawBlankPage(wxPreviewCanvas
*canvas
, wxDC
& dc
)
1175 int canvasWidth
, canvasHeight
;
1176 canvas
->GetSize(&canvasWidth
, &canvasHeight
);
1178 float zoomScale
= (float)((float)m_currentZoom
/(float)100);
1179 float actualWidth
= zoomScale
*m_pageWidth
*m_previewScale
;
1180 float actualHeight
= zoomScale
*m_pageHeight
*m_previewScale
;
1182 float x
= (float)((canvasWidth
- actualWidth
)/2.0);
1183 if (x
< m_leftMargin
)
1184 x
= (float)m_leftMargin
;
1185 float y
= (float)m_topMargin
;
1187 // Draw shadow, allowing for 1-pixel border AROUND the actual page
1188 int shadowOffset
= 4;
1189 dc
.SetPen(*wxBLACK_PEN
);
1190 dc
.SetBrush(*wxBLACK_BRUSH
);
1192 dc.DrawRectangle((int)(x-1 + shadowOffset), (int)(y-1 + shadowOffset), (int)(actualWidth+2), (int)(actualHeight+2));
1194 dc
.DrawRectangle((int)(x
+ shadowOffset
), (int)(y
+ actualHeight
+1), (int)(actualWidth
), shadowOffset
);
1195 dc
.DrawRectangle((int)(x
+ actualWidth
), (int)(y
+ shadowOffset
), shadowOffset
, (int)(actualHeight
));
1197 // Draw blank page allowing for 1-pixel border AROUND the actual page
1198 dc
.SetPen(*wxBLACK_PEN
);
1199 dc
.SetBrush(*wxWHITE_BRUSH
);
1202 wxRegion update_region = canvas->GetUpdateRegion();
1203 wxRect r = update_region.GetBox();
1205 printf( "x: %d y: %d w: %d h: %d.\n", (int)r.x, (int)r.y, (int)r.width, (int)r.height );
1208 dc
.DrawRectangle((int)(x
-2), (int)(y
-1), (int)(actualWidth
+3), (int)(actualHeight
+2));
1213 void wxPrintPreviewBase::SetZoom(int percent
)
1215 if (m_currentZoom
== percent
)
1218 m_currentZoom
= percent
;
1219 if (m_previewBitmap
)
1221 delete m_previewBitmap
;
1222 m_previewBitmap
= NULL
;
1225 if (m_previewCanvas
)
1227 AdjustScrollbars(m_previewCanvas
);
1228 RenderPage(m_currentPage
);
1229 ((wxScrolledWindow
*) m_previewCanvas
)->Scroll(0, 0);
1230 m_previewCanvas
->ClearBackground();
1231 m_previewCanvas
->Refresh();
1232 m_previewCanvas
->SetFocus();
1236 wxPrintDialogData
& wxPrintPreviewBase::GetPrintDialogData()
1238 return m_printDialogData
;
1241 int wxPrintPreviewBase::GetZoom() const
1242 { return m_currentZoom
; }
1243 int wxPrintPreviewBase::GetMaxPage() const
1244 { return m_maxPage
; }
1245 int wxPrintPreviewBase::GetMinPage() const
1246 { return m_minPage
; }
1247 bool wxPrintPreviewBase::Ok() const
1249 void wxPrintPreviewBase::SetOk(bool ok
)
1251 //----------------------------------------------------------------------------
1253 //----------------------------------------------------------------------------
1255 IMPLEMENT_CLASS(wxPrintPreview
, wxPrintPreviewBase
)
1257 wxPrintPreview::wxPrintPreview(wxPrintout
*printout
,
1258 wxPrintout
*printoutForPrinting
,
1259 wxPrintDialogData
*data
) :
1260 wxPrintPreviewBase( printout
, printoutForPrinting
, data
)
1262 m_pimpl
= wxPrintFactory::GetFactory()->
1263 CreatePrintPreview( printout
, printoutForPrinting
, data
);
1266 wxPrintPreview::wxPrintPreview(wxPrintout
*printout
,
1267 wxPrintout
*printoutForPrinting
,
1268 wxPrintData
*data
) :
1269 wxPrintPreviewBase( printout
, printoutForPrinting
, data
)
1271 m_pimpl
= wxPrintFactory::GetFactory()->
1272 CreatePrintPreview( printout
, printoutForPrinting
, data
);
1275 wxPrintPreview::~wxPrintPreview()
1279 // don't delete twice
1280 m_printPrintout
= NULL
;
1281 m_previewPrintout
= NULL
;
1282 m_previewBitmap
= NULL
;
1285 bool wxPrintPreview::SetCurrentPage(int pageNum
)
1287 return m_pimpl
->SetCurrentPage( pageNum
);
1290 int wxPrintPreview::GetCurrentPage() const
1292 return m_pimpl
->GetCurrentPage();
1295 void wxPrintPreview::SetPrintout(wxPrintout
*printout
)
1297 m_pimpl
->SetPrintout( printout
);
1300 wxPrintout
*wxPrintPreview::GetPrintout() const
1302 return m_pimpl
->GetPrintout();
1305 wxPrintout
*wxPrintPreview::GetPrintoutForPrinting() const
1307 return m_pimpl
->GetPrintoutForPrinting();
1310 void wxPrintPreview::SetFrame(wxFrame
*frame
)
1312 m_pimpl
->SetFrame( frame
);
1315 void wxPrintPreview::SetCanvas(wxPreviewCanvas
*canvas
)
1317 m_pimpl
->SetCanvas( canvas
);
1320 wxFrame
*wxPrintPreview::GetFrame() const
1322 return m_pimpl
->GetFrame();
1325 wxPreviewCanvas
*wxPrintPreview::GetCanvas() const
1327 return m_pimpl
->GetCanvas();
1330 bool wxPrintPreview::PaintPage(wxPreviewCanvas
*canvas
, wxDC
& dc
)
1332 return m_pimpl
->PaintPage( canvas
, dc
);
1335 bool wxPrintPreview::DrawBlankPage(wxPreviewCanvas
*canvas
, wxDC
& dc
)
1337 return m_pimpl
->DrawBlankPage( canvas
, dc
);
1340 void wxPrintPreview::AdjustScrollbars(wxPreviewCanvas
*canvas
)
1342 m_pimpl
->AdjustScrollbars( canvas
);
1345 bool wxPrintPreview::RenderPage(int pageNum
)
1347 return m_pimpl
->RenderPage( pageNum
);
1350 void wxPrintPreview::SetZoom(int percent
)
1352 m_pimpl
->SetZoom( percent
);
1355 wxPrintDialogData
& wxPrintPreview::GetPrintDialogData()
1357 return m_pimpl
->GetPrintDialogData();
1360 int wxPrintPreview::GetMaxPage() const
1362 return m_pimpl
->GetMaxPage();
1365 int wxPrintPreview::GetMinPage() const
1367 return m_pimpl
->GetMinPage();
1370 bool wxPrintPreview::Ok() const
1372 return m_pimpl
->Ok();
1375 void wxPrintPreview::SetOk(bool ok
)
1377 m_pimpl
->SetOk( ok
);
1380 bool wxPrintPreview::Print(bool interactive
)
1382 return m_pimpl
->Print( interactive
);
1385 void wxPrintPreview::DetermineScaling()
1387 m_pimpl
->DetermineScaling();
1391 #endif // wxUSE_PRINTING_ARCHITECTURE