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"
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
25 #if wxUSE_PRINTING_ARCHITECTURE
31 #include "wx/msgdlg.h"
32 #include "wx/layout.h"
33 #include "wx/choice.h"
34 #include "wx/button.h"
35 #include "wx/settings.h"
36 #include "wx/dcmemory.h"
37 #include "wx/stattext.h"
39 #include "wx/textdlg.h"
43 #include "wx/prntbase.h"
44 #include "wx/dcprint.h"
45 #include "wx/printdlg.h"
47 #include "wx/module.h"
53 #include "wx/msw/private.h"
61 IMPLEMENT_CLASS(wxPrintPreviewBase
, wxObject
)
63 //----------------------------------------------------------------------------
65 //----------------------------------------------------------------------------
67 wxPrintFactory
*wxPrintFactory::m_factory
= NULL
;
69 void wxPrintFactory::SetPrintFactory( wxPrintFactory
*factory
)
71 if (wxPrintFactory::m_factory
)
72 delete wxPrintFactory::m_factory
;
74 wxPrintFactory::m_factory
= factory
;
77 wxPrintFactory
*wxPrintFactory::GetFactory()
79 if (!wxPrintFactory::m_factory
)
80 wxPrintFactory::m_factory
= new wxNativePrintFactory
;
82 return wxPrintFactory::m_factory
;
85 //----------------------------------------------------------------------------
86 // wxNativePrintFactory
87 //----------------------------------------------------------------------------
89 wxPrinterBase
*wxNativePrintFactory::CreatePrinter( wxPrintDialogData
*data
)
91 #if defined(__WXMSW__) && !defined(__WXUNIVERSAL__)
92 return new wxWindowsPrinter( data
);
93 #elif defined(__WXMAC__)
94 return new wxMacPrinter( data
);
96 return new wxPostScriptPrinter( data
);
100 wxPrintPreviewBase
*wxNativePrintFactory::CreatePrintPreview( wxPrintout
*preview
,
101 wxPrintout
*printout
, wxPrintDialogData
*data
)
103 #if defined(__WXMSW__) && !defined(__WXUNIVERSAL__)
104 return new wxWindowsPrintPreview( preview
, printout
, data
);
105 #elif defined(__WXMAC__)
106 return new wxMacPrintPreview( preview
, printout
, data
);
108 return new wxPostScriptPrintPreview( preview
, printout
, data
);
112 wxPrintPreviewBase
*wxNativePrintFactory::CreatePrintPreview( wxPrintout
*preview
,
113 wxPrintout
*printout
, wxPrintData
*data
)
115 #if defined(__WXMSW__) && !defined(__WXUNIVERSAL__)
116 return new wxWindowsPrintPreview( preview
, printout
, data
);
117 #elif defined(__WXMAC__)
118 return new wxMacPrintPreview( preview
, printout
, data
);
120 return new wxPostScriptPrintPreview( preview
, printout
, data
);
124 wxPrintDialogBase
*wxNativePrintFactory::CreatePrintDialog( wxWindow
*parent
,
125 wxPrintDialogData
*data
)
127 #if defined(__WXMSW__) && !defined(__WXUNIVERSAL__)
128 return new wxWindowsPrintDialog( parent
, data
);
129 #elif defined(__WXMAC__)
130 return new wxMacPrintDialog( parent
, data
);
132 return new wxGenericPrintDialog( parent
, data
);
136 wxPrintDialogBase
*wxNativePrintFactory::CreatePrintDialog( wxWindow
*parent
,
139 #if defined(__WXMSW__) && !defined(__WXUNIVERSAL__)
140 return new wxWindowsPrintDialog( parent
, data
);
141 #elif defined(__WXMAC__)
142 return new wxMacPrintDialog( parent
, data
);
144 return new wxGenericPrintDialog( parent
, data
);
148 //----------------------------------------------------------------------------
150 //----------------------------------------------------------------------------
152 IMPLEMENT_CLASS(wxPrinterBase
, wxObject
)
154 wxPrinterBase::wxPrinterBase(wxPrintDialogData
*data
)
156 m_currentPrintout
= (wxPrintout
*) NULL
;
157 sm_abortWindow
= (wxWindow
*) NULL
;
160 m_printDialogData
= (*data
);
161 sm_lastError
= wxPRINTER_NO_ERROR
;
164 wxWindow
*wxPrinterBase::sm_abortWindow
= (wxWindow
*) NULL
;
165 bool wxPrinterBase::sm_abortIt
= false;
166 wxPrinterError
wxPrinterBase::sm_lastError
= wxPRINTER_NO_ERROR
;
168 wxPrinterBase::~wxPrinterBase()
172 wxWindow
*wxPrinterBase::CreateAbortWindow(wxWindow
*parent
, wxPrintout
* printout
)
174 wxPrintAbortDialog
*dialog
= new wxPrintAbortDialog(parent
, _("Printing ") , wxDefaultPosition
, wxDefaultSize
, wxDEFAULT_DIALOG_STYLE
);
176 wxBoxSizer
*button_sizer
= new wxBoxSizer( wxVERTICAL
);
177 button_sizer
->Add( new wxStaticText(dialog
, wxID_ANY
, _("Please wait while printing\n") + printout
->GetTitle() ), 0, wxALL
, 10 );
178 button_sizer
->Add( new wxButton( dialog
, wxID_CANCEL
, wxT("Cancel") ), 0, wxALL
| wxALIGN_CENTER
, 10 );
180 dialog
->SetAutoLayout( true );
181 dialog
->SetSizer( button_sizer
);
183 button_sizer
->Fit(dialog
);
184 button_sizer
->SetSizeHints (dialog
) ;
189 void wxPrinterBase::ReportError(wxWindow
*parent
, wxPrintout
*WXUNUSED(printout
), const wxString
& message
)
191 wxMessageBox(message
, _("Printing Error"), wxOK
, parent
);
194 wxPrintDialogData
& wxPrinterBase::GetPrintDialogData() const
196 return (wxPrintDialogData
&) m_printDialogData
;
199 //----------------------------------------------------------------------------
201 //----------------------------------------------------------------------------
203 IMPLEMENT_CLASS(wxPrinter
, wxPrinterBase
)
205 wxPrinter::wxPrinter(wxPrintDialogData
*data
)
207 m_pimpl
= wxPrintFactory::GetFactory()->CreatePrinter( data
);
210 wxPrinter::~wxPrinter()
215 wxWindow
*wxPrinter::CreateAbortWindow(wxWindow
*parent
, wxPrintout
*printout
)
217 return m_pimpl
->CreateAbortWindow( parent
, printout
);
220 void wxPrinter::ReportError(wxWindow
*parent
, wxPrintout
*printout
, const wxString
& message
)
222 m_pimpl
->ReportError( parent
, printout
, message
);
225 bool wxPrinter::Setup(wxWindow
*parent
)
227 return m_pimpl
->Setup( parent
);
230 bool wxPrinter::Print(wxWindow
*parent
, wxPrintout
*printout
, bool prompt
)
232 return m_pimpl
->Print( parent
, printout
, prompt
);
235 wxDC
* wxPrinter::PrintDialog(wxWindow
*parent
)
237 return m_pimpl
->PrintDialog( parent
);
240 wxPrintDialogData
& wxPrinter::GetPrintDialogData() const
242 return m_pimpl
->GetPrintDialogData();
245 // ---------------------------------------------------------------------------
246 // wxPrintDialogBase: the common dialog for printing.
247 // ---------------------------------------------------------------------------
249 IMPLEMENT_ABSTRACT_CLASS(wxPrintDialogBase
, wxObject
)
251 wxPrintDialogBase::wxPrintDialogBase(wxWindow
*parent
, wxWindowID id
,
252 const wxString
&title
, const wxPoint
&pos
, const wxSize
&size
, long style
) :
253 wxDialog( parent
, id
, title
, pos
, size
, style
)
257 // ---------------------------------------------------------------------------
258 // wxPrintDialog: the common dialog for printing.
259 // ---------------------------------------------------------------------------
261 IMPLEMENT_CLASS(wxPrintDialog
, wxObject
)
263 wxPrintDialog::wxPrintDialog(wxWindow
*parent
, wxPrintDialogData
* data
)
265 m_pimpl
= wxPrintFactory::GetFactory()->CreatePrintDialog( parent
, data
);
268 wxPrintDialog::wxPrintDialog(wxWindow
*parent
, wxPrintData
* data
)
270 m_pimpl
= wxPrintFactory::GetFactory()->CreatePrintDialog( parent
, data
);
273 wxPrintDialog::~wxPrintDialog()
278 int wxPrintDialog::ShowModal()
280 return m_pimpl
->ShowModal();
283 wxPrintDialogData
& wxPrintDialog::GetPrintDialogData()
285 return m_pimpl
->GetPrintDialogData();
288 wxPrintData
& wxPrintDialog::GetPrintData()
290 return m_pimpl
->GetPrintData();
292 wxDC
*wxPrintDialog::GetPrintDC()
294 return m_pimpl
->GetPrintDC();
297 //----------------------------------------------------------------------------
298 // wxPrintAbortDialog
299 //----------------------------------------------------------------------------
301 BEGIN_EVENT_TABLE(wxPrintAbortDialog
, wxDialog
)
302 EVT_BUTTON(wxID_CANCEL
, wxPrintAbortDialog::OnCancel
)
305 void wxPrintAbortDialog::OnCancel(wxCommandEvent
& WXUNUSED(event
))
307 wxPrinterBase::sm_abortIt
= true;
308 wxPrinterBase::sm_abortWindow
->Show(false);
309 wxPrinterBase::sm_abortWindow
->Close(true);
310 wxPrinterBase::sm_abortWindow
= (wxWindow
*) NULL
;
313 //----------------------------------------------------------------------------
315 //----------------------------------------------------------------------------
317 IMPLEMENT_ABSTRACT_CLASS(wxPrintout
, wxObject
)
319 wxPrintout::wxPrintout(const wxString
& title
)
321 m_printoutTitle
= title
;
322 m_printoutDC
= (wxDC
*) NULL
;
325 m_pageWidthPixels
= 0;
326 m_pageHeightPixels
= 0;
334 wxPrintout::~wxPrintout()
338 bool wxPrintout::OnBeginDocument(int WXUNUSED(startPage
), int WXUNUSED(endPage
))
340 return GetDC()->StartDoc(_("Printing ") + m_printoutTitle
);
343 void wxPrintout::OnEndDocument()
348 void wxPrintout::OnBeginPrinting()
352 void wxPrintout::OnEndPrinting()
356 bool wxPrintout::HasPage(int page
)
361 void wxPrintout::GetPageInfo(int *minPage
, int *maxPage
, int *fromPage
, int *toPage
)
369 //----------------------------------------------------------------------------
371 //----------------------------------------------------------------------------
373 IMPLEMENT_CLASS(wxPreviewCanvas
, wxWindow
)
375 BEGIN_EVENT_TABLE(wxPreviewCanvas
, wxScrolledWindow
)
376 EVT_PAINT(wxPreviewCanvas::OnPaint
)
377 EVT_CHAR(wxPreviewCanvas::OnChar
)
378 EVT_SYS_COLOUR_CHANGED(wxPreviewCanvas::OnSysColourChanged
)
381 // VZ: the current code doesn't refresh properly without
382 // wxFULL_REPAINT_ON_RESIZE, this must be fixed as otherwise we have
383 // really horrible flicker when resizing the preview frame, but without
384 // this style it simply doesn't work correctly at all...
385 wxPreviewCanvas::wxPreviewCanvas(wxPrintPreviewBase
*preview
, wxWindow
*parent
,
386 const wxPoint
& pos
, const wxSize
& size
, long style
, const wxString
& name
):
387 wxScrolledWindow(parent
, wxID_ANY
, pos
, size
, style
| wxFULL_REPAINT_ON_RESIZE
, name
)
389 m_printPreview
= preview
;
391 // The app workspace colour is always white, but we should have
392 // a contrast with the page.
393 wxSystemColour colourIndex
= wxSYS_COLOUR_3DDKSHADOW
;
395 wxSystemColour colourIndex
= wxSYS_COLOUR_APPWORKSPACE
;
397 SetBackgroundColour(wxSystemSettings::GetColour(colourIndex
));
399 SetScrollbars(10, 10, 100, 100);
402 wxPreviewCanvas::~wxPreviewCanvas()
406 void wxPreviewCanvas::OnPaint(wxPaintEvent
& WXUNUSED(event
))
413 if (!GetUpdateRegion().IsEmpty())
414 dc.SetClippingRegion( GetUpdateRegion() );
420 m_printPreview
->PaintPage(this, dc
);
424 // Responds to colour changes, and passes event on to children.
425 void wxPreviewCanvas::OnSysColourChanged(wxSysColourChangedEvent
& event
)
428 // The app workspace colour is always white, but we should have
429 // a contrast with the page.
430 wxSystemColour colourIndex
= wxSYS_COLOUR_3DDKSHADOW
;
432 wxSystemColour colourIndex
= wxSYS_COLOUR_APPWORKSPACE
;
434 SetBackgroundColour(wxSystemSettings::GetColour(colourIndex
));
437 // Propagate the event to the non-top-level children
438 wxWindow::OnSysColourChanged(event
);
441 void wxPreviewCanvas::OnChar(wxKeyEvent
&event
)
443 wxPreviewControlBar
* controlBar
= ((wxPreviewFrame
*) GetParent())->GetControlBar();
444 if (event
.GetKeyCode() == WXK_ESCAPE
)
446 ((wxPreviewFrame
*) GetParent())->Close(true);
449 else if (event
.GetKeyCode() == WXK_TAB
)
451 controlBar
->OnGoto();
454 else if (event
.GetKeyCode() == WXK_RETURN
)
456 controlBar
->OnPrint();
460 if (!event
.ControlDown())
466 switch(event
.GetKeyCode())
469 controlBar
->OnNext(); break;
471 controlBar
->OnPrevious(); break;
473 controlBar
->OnFirst(); break;
475 controlBar
->OnLast(); break;
481 //----------------------------------------------------------------------------
482 // wxPreviewControlBar
483 //----------------------------------------------------------------------------
485 IMPLEMENT_CLASS(wxPreviewControlBar
, wxWindow
)
487 BEGIN_EVENT_TABLE(wxPreviewControlBar
, wxPanel
)
488 EVT_BUTTON(wxID_PREVIEW_CLOSE
, wxPreviewControlBar::OnWindowClose
)
489 EVT_BUTTON(wxID_PREVIEW_PRINT
, wxPreviewControlBar::OnPrintButton
)
490 EVT_BUTTON(wxID_PREVIEW_PREVIOUS
, wxPreviewControlBar::OnPreviousButton
)
491 EVT_BUTTON(wxID_PREVIEW_NEXT
, wxPreviewControlBar::OnNextButton
)
492 EVT_BUTTON(wxID_PREVIEW_FIRST
, wxPreviewControlBar::OnFirstButton
)
493 EVT_BUTTON(wxID_PREVIEW_LAST
, wxPreviewControlBar::OnLastButton
)
494 EVT_BUTTON(wxID_PREVIEW_GOTO
, wxPreviewControlBar::OnGotoButton
)
495 EVT_CHOICE(wxID_PREVIEW_ZOOM
, wxPreviewControlBar::OnZoom
)
496 EVT_PAINT(wxPreviewControlBar::OnPaint
)
499 wxPreviewControlBar::wxPreviewControlBar(wxPrintPreviewBase
*preview
, long buttons
,
500 wxWindow
*parent
, const wxPoint
& pos
, const wxSize
& size
,
501 long style
, const wxString
& name
):
502 wxPanel(parent
, wxID_ANY
, pos
, size
, style
, name
)
504 m_printPreview
= preview
;
505 m_closeButton
= (wxButton
*) NULL
;
506 m_nextPageButton
= (wxButton
*) NULL
;
507 m_previousPageButton
= (wxButton
*) NULL
;
508 m_printButton
= (wxButton
*) NULL
;
509 m_zoomControl
= (wxChoice
*) NULL
;
510 m_buttonFlags
= buttons
;
513 wxPreviewControlBar::~wxPreviewControlBar()
517 void wxPreviewControlBar::OnPaint(wxPaintEvent
& WXUNUSED(event
))
523 dc
.SetPen(*wxBLACK_PEN
);
524 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
525 dc
.DrawLine( 0, h
-1, w
, h
-1 );
528 void wxPreviewControlBar::OnWindowClose(wxCommandEvent
& WXUNUSED(event
))
530 wxPreviewFrame
*frame
= (wxPreviewFrame
*)GetParent();
534 void wxPreviewControlBar::OnPrint(void)
536 wxPrintPreviewBase
*preview
= GetPrintPreview();
537 preview
->Print(true);
540 void wxPreviewControlBar::OnNext(void)
542 wxPrintPreviewBase
*preview
= GetPrintPreview();
545 int currentPage
= preview
->GetCurrentPage();
546 if ((preview
->GetMaxPage() > 0) &&
547 (currentPage
< preview
->GetMaxPage()) &&
548 preview
->GetPrintout()->HasPage(currentPage
+ 1))
550 preview
->SetCurrentPage(currentPage
+ 1);
555 void wxPreviewControlBar::OnPrevious(void)
557 wxPrintPreviewBase
*preview
= GetPrintPreview();
560 int currentPage
= preview
->GetCurrentPage();
561 if ((preview
->GetMinPage() > 0) &&
562 (currentPage
> preview
->GetMinPage()) &&
563 preview
->GetPrintout()->HasPage(currentPage
- 1))
565 preview
->SetCurrentPage(currentPage
- 1);
570 void wxPreviewControlBar::OnFirst(void)
572 wxPrintPreviewBase
*preview
= GetPrintPreview();
575 int currentPage
= preview
->GetMinPage();
576 if (preview
->GetPrintout()->HasPage(currentPage
))
578 preview
->SetCurrentPage(currentPage
);
583 void wxPreviewControlBar::OnLast(void)
585 wxPrintPreviewBase
*preview
= GetPrintPreview();
588 int currentPage
= preview
->GetMaxPage();
589 if (preview
->GetPrintout()->HasPage(currentPage
))
591 preview
->SetCurrentPage(currentPage
);
596 void wxPreviewControlBar::OnGoto(void)
598 wxPrintPreviewBase
*preview
= GetPrintPreview();
603 if (preview
->GetMinPage() > 0)
608 strPrompt
.Printf( _("Enter a page number between %d and %d:"),
609 preview
->GetMinPage(), preview
->GetMaxPage());
610 strPage
.Printf( wxT("%d"), preview
->GetCurrentPage() );
613 wxGetTextFromUser( strPrompt
, _("Goto Page"), strPage
, GetParent());
615 if ( strPage
.ToLong( ¤tPage
) )
616 if (preview
->GetPrintout()->HasPage(currentPage
))
618 preview
->SetCurrentPage(currentPage
);
624 void wxPreviewControlBar::OnZoom(wxCommandEvent
& WXUNUSED(event
))
626 int zoom
= GetZoomControl();
627 if (GetPrintPreview())
628 GetPrintPreview()->SetZoom(zoom
);
631 void wxPreviewControlBar::CreateButtons()
633 SetSize(0, 0, 400, 40);
635 wxBoxSizer
*item0
= new wxBoxSizer( wxHORIZONTAL
);
637 m_closeButton
= new wxButton( this, wxID_PREVIEW_CLOSE
, _("&Close"), wxDefaultPosition
, wxDefaultSize
, 0 );
638 item0
->Add( m_closeButton
, 0, wxALIGN_CENTRE
|wxALL
, 5 );
640 if (m_buttonFlags
& wxPREVIEW_PRINT
)
642 m_printButton
= new wxButton( this, wxID_PREVIEW_PRINT
, _("&Print..."), wxDefaultPosition
, wxDefaultSize
, 0 );
643 item0
->Add( m_printButton
, 0, wxALIGN_CENTRE
|wxALL
, 5 );
646 if (m_buttonFlags
& wxPREVIEW_FIRST
)
648 m_firstPageButton
= new wxButton( this, wxID_PREVIEW_FIRST
, _("|<<"), wxDefaultPosition
, wxDefaultSize
, wxBU_EXACTFIT
);
649 item0
->Add( m_firstPageButton
, 0, wxALIGN_CENTRE
|wxALL
, 5 );
652 if (m_buttonFlags
& wxPREVIEW_PREVIOUS
)
654 m_previousPageButton
= new wxButton( this, wxID_PREVIEW_PREVIOUS
, _("<<"), wxDefaultPosition
, wxDefaultSize
, wxBU_EXACTFIT
);
655 item0
->Add( m_previousPageButton
, 0, wxALIGN_CENTRE
|wxRIGHT
|wxTOP
|wxBOTTOM
, 5 );
658 if (m_buttonFlags
& wxPREVIEW_NEXT
)
660 m_nextPageButton
= new wxButton( this, wxID_PREVIEW_NEXT
, _(">>"), wxDefaultPosition
, wxDefaultSize
, wxBU_EXACTFIT
);
661 item0
->Add( m_nextPageButton
, 0, wxALIGN_CENTRE
|wxRIGHT
|wxTOP
|wxBOTTOM
, 5 );
664 if (m_buttonFlags
& wxPREVIEW_LAST
)
666 m_lastPageButton
= new wxButton( this, wxID_PREVIEW_LAST
, _(">>|"), wxDefaultPosition
, wxDefaultSize
, wxBU_EXACTFIT
);
667 item0
->Add( m_lastPageButton
, 0, wxALIGN_CENTRE
|wxRIGHT
|wxTOP
|wxBOTTOM
, 5 );
670 if (m_buttonFlags
& wxPREVIEW_GOTO
)
672 m_gotoPageButton
= new wxButton( this, wxID_PREVIEW_GOTO
, _("&Goto..."), wxDefaultPosition
, wxDefaultSize
, 0 );
673 item0
->Add( m_gotoPageButton
, 0, wxALIGN_CENTRE
|wxALL
, 5 );
676 if (m_buttonFlags
& wxPREVIEW_ZOOM
)
680 wxT("10%"), wxT("15%"), wxT("20%"), wxT("25%"), wxT("30%"), wxT("35%"), wxT("40%"), wxT("45%"), wxT("50%"), wxT("55%"),
681 wxT("60%"), wxT("65%"), wxT("70%"), wxT("75%"), wxT("80%"), wxT("85%"), wxT("90%"), wxT("95%"), wxT("100%"), wxT("110%"),
682 wxT("120%"), wxT("150%"), wxT("200%")
684 int n
= WXSIZEOF(choices
);
686 m_zoomControl
= new wxChoice( this, wxID_PREVIEW_ZOOM
, wxDefaultPosition
, wxSize(70,wxDefaultCoord
), n
, choices
, 0 );
687 item0
->Add( m_zoomControl
, 0, wxALIGN_CENTRE
|wxALL
, 5 );
688 SetZoomControl(m_printPreview
->GetZoom());
695 void wxPreviewControlBar::SetZoomControl(int zoom
)
699 int n
, count
= m_zoomControl
->GetCount();
701 for (n
=0; n
<count
; n
++)
703 if (m_zoomControl
->GetString(n
).BeforeFirst(wxT('%')).ToLong(&val
) &&
706 m_zoomControl
->SetSelection(n
);
711 m_zoomControl
->SetSelection(count
-1);
715 int wxPreviewControlBar::GetZoomControl()
717 if (m_zoomControl
&& (m_zoomControl
->GetStringSelection() != wxEmptyString
))
720 if (m_zoomControl
->GetStringSelection().BeforeFirst(wxT('%')).ToLong(&val
))
732 IMPLEMENT_CLASS(wxPreviewFrame
, wxFrame
)
734 BEGIN_EVENT_TABLE(wxPreviewFrame
, wxFrame
)
735 EVT_CLOSE(wxPreviewFrame::OnCloseWindow
)
738 wxPreviewFrame::wxPreviewFrame(wxPrintPreviewBase
*preview
, wxWindow
*parent
, const wxString
& title
,
739 const wxPoint
& pos
, const wxSize
& size
, long style
, const wxString
& name
):
740 wxFrame(parent
, wxID_ANY
, title
, pos
, size
, style
, name
)
742 m_printPreview
= preview
;
744 m_previewCanvas
= NULL
;
745 m_windowDisabler
= NULL
;
747 // Give the application icon
749 wxFrame
* topFrame
= wxDynamicCast(wxTheApp
->GetTopWindow(), wxFrame
);
751 SetIcon(topFrame
->GetIcon());
755 wxPreviewFrame::~wxPreviewFrame()
759 void wxPreviewFrame::OnCloseWindow(wxCloseEvent
& WXUNUSED(event
))
761 if (m_windowDisabler
)
762 delete m_windowDisabler
;
764 // Need to delete the printout and the print preview
765 wxPrintout
*printout
= m_printPreview
->GetPrintout();
769 m_printPreview
->SetPrintout(NULL
);
770 m_printPreview
->SetCanvas(NULL
);
771 m_printPreview
->SetFrame(NULL
);
773 delete m_printPreview
;
778 void wxPreviewFrame::Initialize()
786 m_printPreview
->SetCanvas(m_previewCanvas
);
787 m_printPreview
->SetFrame(this);
789 wxBoxSizer
*item0
= new wxBoxSizer( wxVERTICAL
);
791 item0
->Add( m_controlBar
, 0, wxGROW
|wxALIGN_CENTER_VERTICAL
, 5 );
792 item0
->Add( m_previewCanvas
, 1, wxGROW
|wxALIGN_CENTER_VERTICAL
, 5 );
794 SetAutoLayout( true );
797 m_windowDisabler
= new wxWindowDisabler(this);
801 m_printPreview
->AdjustScrollbars(m_previewCanvas
);
802 m_previewCanvas
->SetFocus();
803 m_controlBar
->SetFocus();
806 void wxPreviewFrame::CreateCanvas()
808 m_previewCanvas
= new wxPreviewCanvas(m_printPreview
, this);
811 void wxPreviewFrame::CreateControlBar()
813 long buttons
= wxPREVIEW_DEFAULT
;
814 if (m_printPreview
->GetPrintoutForPrinting())
815 buttons
|= wxPREVIEW_PRINT
;
817 m_controlBar
= new wxPreviewControlBar(m_printPreview
, buttons
, this, wxPoint(0, 0), wxSize(400, 40));
818 m_controlBar
->CreateButtons();
825 wxPrintPreviewBase::wxPrintPreviewBase(wxPrintout
*printout
,
826 wxPrintout
*printoutForPrinting
,
830 m_printDialogData
= (*data
);
832 Init(printout
, printoutForPrinting
);
835 wxPrintPreviewBase::wxPrintPreviewBase(wxPrintout
*printout
,
836 wxPrintout
*printoutForPrinting
,
837 wxPrintDialogData
*data
)
840 m_printDialogData
= (*data
);
842 Init(printout
, printoutForPrinting
);
845 void wxPrintPreviewBase::Init(wxPrintout
*printout
,
846 wxPrintout
*printoutForPrinting
)
849 m_previewPrintout
= printout
;
850 if (m_previewPrintout
)
851 m_previewPrintout
->SetIsPreview(true);
853 m_printPrintout
= printoutForPrinting
;
855 m_previewCanvas
= NULL
;
856 m_previewFrame
= NULL
;
857 m_previewBitmap
= NULL
;
864 m_printingPrepared
= false;
869 wxPrintPreviewBase::~wxPrintPreviewBase()
871 if (m_previewPrintout
)
872 delete m_previewPrintout
;
874 delete m_previewBitmap
;
876 delete m_printPrintout
;
879 bool wxPrintPreviewBase::SetCurrentPage(int pageNum
)
881 if (m_currentPage
== pageNum
)
884 m_currentPage
= pageNum
;
887 delete m_previewBitmap
;
888 m_previewBitmap
= NULL
;
893 AdjustScrollbars(m_previewCanvas
);
895 if (!RenderPage(pageNum
))
897 m_previewCanvas
->Refresh();
898 m_previewCanvas
->SetFocus();
903 int wxPrintPreviewBase::GetCurrentPage() const
904 { return m_currentPage
; };
905 void wxPrintPreviewBase::SetPrintout(wxPrintout
*printout
)
906 { m_previewPrintout
= printout
; };
907 wxPrintout
*wxPrintPreviewBase::GetPrintout() const
908 { return m_previewPrintout
; };
909 wxPrintout
*wxPrintPreviewBase::GetPrintoutForPrinting() const
910 { return m_printPrintout
; };
911 void wxPrintPreviewBase::SetFrame(wxFrame
*frame
)
912 { m_previewFrame
= frame
; };
913 void wxPrintPreviewBase::SetCanvas(wxPreviewCanvas
*canvas
)
914 { m_previewCanvas
= canvas
; };
915 wxFrame
*wxPrintPreviewBase::GetFrame() const
916 { return m_previewFrame
; }
917 wxPreviewCanvas
*wxPrintPreviewBase::GetCanvas() const
918 { return m_previewCanvas
; }
920 bool wxPrintPreviewBase::PaintPage(wxPreviewCanvas
*canvas
, wxDC
& dc
)
922 DrawBlankPage(canvas
, dc
);
924 if (!m_previewBitmap
)
925 if (!RenderPage(m_currentPage
))
928 if (!m_previewBitmap
)
934 int canvasWidth
, canvasHeight
;
935 canvas
->GetSize(&canvasWidth
, &canvasHeight
);
937 double zoomScale
= ((float)m_currentZoom
/(float)100);
938 double actualWidth
= (zoomScale
*m_pageWidth
*m_previewScale
);
939 // float actualHeight = (float)(zoomScale*m_pageHeight*m_previewScale);
941 int x
= (int) ((canvasWidth
- actualWidth
)/2.0);
942 if (x
< m_leftMargin
)
947 temp_dc
.SelectObject(*m_previewBitmap
);
949 dc
.Blit(x
, y
, m_previewBitmap
->GetWidth(), m_previewBitmap
->GetHeight(), &temp_dc
, 0, 0);
951 temp_dc
.SelectObject(wxNullBitmap
);
956 // Adjusts the scrollbars for the current scale
957 void wxPrintPreviewBase::AdjustScrollbars(wxPreviewCanvas
*canvas
)
962 int canvasWidth
, canvasHeight
;
963 canvas
->GetSize(&canvasWidth
, &canvasHeight
);
965 double zoomScale
= ((float)m_currentZoom
/(float)100);
966 double actualWidth
= (zoomScale
*m_pageWidth
*m_previewScale
);
967 double actualHeight
= (zoomScale
*m_pageHeight
*m_previewScale
);
969 // Set the scrollbars appropriately
970 int totalWidth
= (int)(actualWidth
+ 2*m_leftMargin
);
971 int totalHeight
= (int)(actualHeight
+ 2*m_topMargin
);
972 int scrollUnitsX
= totalWidth
/10;
973 int scrollUnitsY
= totalHeight
/10;
974 wxSize virtualSize
= canvas
->GetVirtualSize();
975 if (virtualSize
.GetWidth() != totalWidth
|| virtualSize
.GetHeight() != totalHeight
)
976 canvas
->SetScrollbars(10, 10, scrollUnitsX
, scrollUnitsY
, 0, 0, true);
979 bool wxPrintPreviewBase::RenderPage(int pageNum
)
983 int canvasWidth
, canvasHeight
;
985 if (!m_previewCanvas
)
987 wxFAIL_MSG(_T("wxPrintPreviewBase::RenderPage: must use wxPrintPreviewBase::SetCanvas to let me know about the canvas!"));
991 m_previewCanvas
->GetSize(&canvasWidth
, &canvasHeight
);
993 double zoomScale
= (m_currentZoom
/100.0);
994 int actualWidth
= (int)(zoomScale
*m_pageWidth
*m_previewScale
);
995 int actualHeight
= (int)(zoomScale
*m_pageHeight
*m_previewScale
);
997 if (!m_previewBitmap
)
999 m_previewBitmap
= new wxBitmap((int)actualWidth
, (int)actualHeight
);
1000 if (!m_previewBitmap
|| !m_previewBitmap
->Ok())
1002 if (m_previewBitmap
) {
1003 delete m_previewBitmap
;
1004 m_previewBitmap
= NULL
;
1006 wxMessageBox(_("Sorry, not enough memory to create a preview."), _("Print Preview Failure"), wxOK
);
1011 wxMemoryDC memoryDC
;
1012 memoryDC
.SelectObject(*m_previewBitmap
);
1016 m_previewPrintout
->SetDC(&memoryDC
);
1017 m_previewPrintout
->SetPageSizePixels(m_pageWidth
, m_pageHeight
);
1019 // Need to delay OnPreparePrinting until here, so we have enough information.
1020 if (!m_printingPrepared
)
1022 m_previewPrintout
->OnPreparePrinting();
1024 m_previewPrintout
->GetPageInfo(&m_minPage
, &m_maxPage
, &selFrom
, &selTo
);
1025 m_printingPrepared
= true;
1028 m_previewPrintout
->OnBeginPrinting();
1030 if (!m_previewPrintout
->OnBeginDocument(m_printDialogData
.GetFromPage(), m_printDialogData
.GetToPage()))
1032 wxMessageBox(_("Could not start document preview."), _("Print Preview Failure"), wxOK
);
1034 memoryDC
.SelectObject(wxNullBitmap
);
1036 delete m_previewBitmap
;
1037 m_previewBitmap
= NULL
;
1041 m_previewPrintout
->OnPrintPage(pageNum
);
1042 m_previewPrintout
->OnEndDocument();
1043 m_previewPrintout
->OnEndPrinting();
1045 m_previewPrintout
->SetDC(NULL
);
1047 memoryDC
.SelectObject(wxNullBitmap
);
1052 status
= wxString::Format(_("Page %d of %d"), pageNum
, m_maxPage
);
1054 status
= wxString::Format(_("Page %d"), pageNum
);
1057 m_previewFrame
->SetStatusText(status
);
1064 bool wxPrintPreviewBase::DrawBlankPage(wxPreviewCanvas
*canvas
, wxDC
& dc
)
1066 int canvasWidth
, canvasHeight
;
1067 canvas
->GetSize(&canvasWidth
, &canvasHeight
);
1069 float zoomScale
= (float)((float)m_currentZoom
/(float)100);
1070 float actualWidth
= zoomScale
*m_pageWidth
*m_previewScale
;
1071 float actualHeight
= zoomScale
*m_pageHeight
*m_previewScale
;
1073 float x
= (float)((canvasWidth
- actualWidth
)/2.0);
1074 if (x
< m_leftMargin
)
1075 x
= (float)m_leftMargin
;
1076 float y
= (float)m_topMargin
;
1078 // Draw shadow, allowing for 1-pixel border AROUND the actual page
1079 int shadowOffset
= 4;
1080 dc
.SetPen(*wxBLACK_PEN
);
1081 dc
.SetBrush(*wxBLACK_BRUSH
);
1083 dc.DrawRectangle((int)(x-1 + shadowOffset), (int)(y-1 + shadowOffset), (int)(actualWidth+2), (int)(actualHeight+2));
1085 dc
.DrawRectangle((int)(x
+ shadowOffset
), (int)(y
+ actualHeight
+1), (int)(actualWidth
), shadowOffset
);
1086 dc
.DrawRectangle((int)(x
+ actualWidth
), (int)(y
+ shadowOffset
), shadowOffset
, (int)(actualHeight
));
1088 // Draw blank page allowing for 1-pixel border AROUND the actual page
1089 dc
.SetPen(*wxBLACK_PEN
);
1090 dc
.SetBrush(*wxWHITE_BRUSH
);
1093 wxRegion update_region = canvas->GetUpdateRegion();
1094 wxRect r = update_region.GetBox();
1096 printf( "x: %d y: %d w: %d h: %d.\n", (int)r.x, (int)r.y, (int)r.width, (int)r.height );
1099 dc
.DrawRectangle((int)(x
-2), (int)(y
-1), (int)(actualWidth
+3), (int)(actualHeight
+2));
1104 void wxPrintPreviewBase::SetZoom(int percent
)
1106 if (m_currentZoom
== percent
)
1109 m_currentZoom
= percent
;
1110 if (m_previewBitmap
)
1112 delete m_previewBitmap
;
1113 m_previewBitmap
= NULL
;
1116 if (m_previewCanvas
)
1118 AdjustScrollbars(m_previewCanvas
);
1119 RenderPage(m_currentPage
);
1120 ((wxScrolledWindow
*) m_previewCanvas
)->Scroll(0, 0);
1121 m_previewCanvas
->ClearBackground();
1122 m_previewCanvas
->Refresh();
1123 m_previewCanvas
->SetFocus();
1127 wxPrintDialogData
& wxPrintPreviewBase::GetPrintDialogData()
1129 return m_printDialogData
;
1132 int wxPrintPreviewBase::GetZoom() const
1133 { return m_currentZoom
; }
1134 int wxPrintPreviewBase::GetMaxPage() const
1135 { return m_maxPage
; }
1136 int wxPrintPreviewBase::GetMinPage() const
1137 { return m_minPage
; }
1138 bool wxPrintPreviewBase::Ok() const
1140 void wxPrintPreviewBase::SetOk(bool ok
)
1142 //----------------------------------------------------------------------------
1144 //----------------------------------------------------------------------------
1146 IMPLEMENT_CLASS(wxPrintPreview
, wxPrintPreviewBase
)
1148 wxPrintPreview::wxPrintPreview(wxPrintout
*printout
,
1149 wxPrintout
*printoutForPrinting
,
1150 wxPrintDialogData
*data
) :
1151 wxPrintPreviewBase( printout
, printoutForPrinting
, data
)
1153 m_pimpl
= wxPrintFactory::GetFactory()->
1154 CreatePrintPreview( printout
, printoutForPrinting
, data
);
1157 wxPrintPreview::wxPrintPreview(wxPrintout
*printout
,
1158 wxPrintout
*printoutForPrinting
,
1159 wxPrintData
*data
) :
1160 wxPrintPreviewBase( printout
, printoutForPrinting
, data
)
1162 m_pimpl
= wxPrintFactory::GetFactory()->
1163 CreatePrintPreview( printout
, printoutForPrinting
, data
);
1166 wxPrintPreview::~wxPrintPreview()
1170 // don't delete twice
1171 m_printPrintout
= NULL
;
1172 m_previewPrintout
= NULL
;
1173 m_previewBitmap
= NULL
;
1176 bool wxPrintPreview::SetCurrentPage(int pageNum
)
1178 return m_pimpl
->SetCurrentPage( pageNum
);
1181 int wxPrintPreview::GetCurrentPage() const
1183 return m_pimpl
->GetCurrentPage();
1186 void wxPrintPreview::SetPrintout(wxPrintout
*printout
)
1188 m_pimpl
->SetPrintout( printout
);
1191 wxPrintout
*wxPrintPreview::GetPrintout() const
1193 return m_pimpl
->GetPrintout();
1196 wxPrintout
*wxPrintPreview::GetPrintoutForPrinting() const
1198 return m_pimpl
->GetPrintoutForPrinting();
1201 void wxPrintPreview::SetFrame(wxFrame
*frame
)
1203 m_pimpl
->SetFrame( frame
);
1206 void wxPrintPreview::SetCanvas(wxPreviewCanvas
*canvas
)
1208 m_pimpl
->SetCanvas( canvas
);
1211 wxFrame
*wxPrintPreview::GetFrame() const
1213 return m_pimpl
->GetFrame();
1216 wxPreviewCanvas
*wxPrintPreview::GetCanvas() const
1218 return m_pimpl
->GetCanvas();
1221 bool wxPrintPreview::PaintPage(wxPreviewCanvas
*canvas
, wxDC
& dc
)
1223 return m_pimpl
->PaintPage( canvas
, dc
);
1226 bool wxPrintPreview::DrawBlankPage(wxPreviewCanvas
*canvas
, wxDC
& dc
)
1228 return m_pimpl
->DrawBlankPage( canvas
, dc
);
1231 void wxPrintPreview::AdjustScrollbars(wxPreviewCanvas
*canvas
)
1233 m_pimpl
->AdjustScrollbars( canvas
);
1236 bool wxPrintPreview::RenderPage(int pageNum
)
1238 return m_pimpl
->RenderPage( pageNum
);
1241 void wxPrintPreview::SetZoom(int percent
)
1243 m_pimpl
->SetZoom( percent
);
1246 wxPrintDialogData
& wxPrintPreview::GetPrintDialogData()
1248 return m_pimpl
->GetPrintDialogData();
1251 int wxPrintPreview::GetMaxPage() const
1253 return m_pimpl
->GetMaxPage();
1256 int wxPrintPreview::GetMinPage() const
1258 return m_pimpl
->GetMinPage();
1261 bool wxPrintPreview::Ok() const
1263 return m_pimpl
->Ok();
1266 void wxPrintPreview::SetOk(bool ok
)
1268 m_pimpl
->SetOk( ok
);
1271 bool wxPrintPreview::Print(bool interactive
)
1273 return m_pimpl
->Print( interactive
);
1276 void wxPrintPreview::DetermineScaling()
1278 m_pimpl
->DetermineScaling();
1282 #endif // wxUSE_PRINTING_ARCHITECTURE