1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: Printing framework base class implementation
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
13 #pragma implementation "prntbase.h"
14 #pragma implementation "printdlg.h"
17 // For compilers that support precompilation, includes "wx.h".
18 #include "wx/wxprec.h"
26 #if wxUSE_PRINTING_ARCHITECTURE
32 #include "wx/msgdlg.h"
33 #include "wx/layout.h"
34 #include "wx/choice.h"
35 #include "wx/button.h"
36 #include "wx/settings.h"
37 #include "wx/dcmemory.h"
38 #include "wx/stattext.h"
40 #include "wx/textdlg.h"
44 #include "wx/prntbase.h"
45 #include "wx/dcprint.h"
46 #include "wx/printdlg.h"
48 #include "wx/module.h"
54 #include "wx/msw/private.h"
62 IMPLEMENT_CLASS(wxPrintPreviewBase
, wxObject
)
64 //----------------------------------------------------------------------------
66 //----------------------------------------------------------------------------
68 wxPrintFactory
*wxPrintFactory::m_factory
= NULL
;
70 void wxPrintFactory::SetPrintFactory( wxPrintFactory
*factory
)
72 if (wxPrintFactory::m_factory
)
73 delete wxPrintFactory::m_factory
;
75 wxPrintFactory::m_factory
= factory
;
78 wxPrintFactory
*wxPrintFactory::GetFactory()
80 if (!wxPrintFactory::m_factory
)
81 wxPrintFactory::m_factory
= new wxNativePrintFactory
;
83 return wxPrintFactory::m_factory
;
86 //----------------------------------------------------------------------------
87 // wxNativePrintFactory
88 //----------------------------------------------------------------------------
90 wxPrinterBase
*wxNativePrintFactory::CreatePrinter( wxPrintDialogData
*data
)
92 #if defined(__WXMSW__) && !defined(__WXUNIVERSAL__)
93 return new wxWindowsPrinter( data
);
94 #elif defined(__WXMAC__)
95 return new wxMacPrinter( data
);
96 #elif defined(__WXPM__)
97 return new wxOS2Printer( data
);
99 return new wxPostScriptPrinter( data
);
103 wxPrintPreviewBase
*wxNativePrintFactory::CreatePrintPreview( wxPrintout
*preview
,
104 wxPrintout
*printout
, wxPrintDialogData
*data
)
106 #if defined(__WXMSW__) && !defined(__WXUNIVERSAL__)
107 return new wxWindowsPrintPreview( preview
, printout
, data
);
108 #elif defined(__WXMAC__)
109 return new wxMacPrintPreview( preview
, printout
, data
);
110 #elif defined(__WXPM__)
111 return new wxOS2PrintPreview( preview
, printout
, data
);
113 return new wxPostScriptPrintPreview( preview
, printout
, data
);
117 wxPrintPreviewBase
*wxNativePrintFactory::CreatePrintPreview( wxPrintout
*preview
,
118 wxPrintout
*printout
, wxPrintData
*data
)
120 #if defined(__WXMSW__) && !defined(__WXUNIVERSAL__)
121 return new wxWindowsPrintPreview( preview
, printout
, data
);
122 #elif defined(__WXMAC__)
123 return new wxMacPrintPreview( preview
, printout
, data
);
124 #elif defined(__WXPM__)
125 return new wxOS2PrintPreview( preview
, printout
, data
);
127 return new wxPostScriptPrintPreview( preview
, printout
, data
);
131 wxPrintDialogBase
*wxNativePrintFactory::CreatePrintDialog( wxWindow
*parent
,
132 wxPrintDialogData
*data
)
134 #if defined(__WXMSW__) && !defined(__WXUNIVERSAL__)
135 return new wxWindowsPrintDialog( parent
, data
);
136 #elif defined(__WXMAC__)
137 return new wxMacPrintDialog( parent
, data
);
139 return new wxGenericPrintDialog( parent
, data
);
143 wxPrintDialogBase
*wxNativePrintFactory::CreatePrintDialog( wxWindow
*parent
,
146 #if defined(__WXMSW__) && !defined(__WXUNIVERSAL__)
147 return new wxWindowsPrintDialog( parent
, data
);
148 #elif defined(__WXMAC__)
149 return new wxMacPrintDialog( parent
, data
);
151 return new wxGenericPrintDialog( parent
, data
);
155 wxPrintNativeDataBase
*wxNativePrintFactory::CreatePrintNativeData()
157 #if defined(__WXMSW__) && !defined(__WXUNIVERSAL__)
158 return new wxWindowsPrintNativeData
;
159 #elif defined(__WXMAC__)
160 return new wxMacPrintNativeData
;
162 return new wxPostScriptPrintNativeData
;
166 //----------------------------------------------------------------------------
167 // wxPrintNativeDataBase
168 //----------------------------------------------------------------------------
170 IMPLEMENT_ABSTRACT_CLASS(wxPrintNativeDataBase
, wxObject
)
172 wxPrintNativeDataBase::wxPrintNativeDataBase()
177 //----------------------------------------------------------------------------
179 //----------------------------------------------------------------------------
181 IMPLEMENT_CLASS(wxPrinterBase
, wxObject
)
183 wxPrinterBase::wxPrinterBase(wxPrintDialogData
*data
)
185 m_currentPrintout
= (wxPrintout
*) NULL
;
186 sm_abortWindow
= (wxWindow
*) NULL
;
189 m_printDialogData
= (*data
);
190 sm_lastError
= wxPRINTER_NO_ERROR
;
193 wxWindow
*wxPrinterBase::sm_abortWindow
= (wxWindow
*) NULL
;
194 bool wxPrinterBase::sm_abortIt
= false;
195 wxPrinterError
wxPrinterBase::sm_lastError
= wxPRINTER_NO_ERROR
;
197 wxPrinterBase::~wxPrinterBase()
201 wxWindow
*wxPrinterBase::CreateAbortWindow(wxWindow
*parent
, wxPrintout
* printout
)
203 wxPrintAbortDialog
*dialog
= new wxPrintAbortDialog(parent
, _("Printing ") , wxDefaultPosition
, wxDefaultSize
, wxDEFAULT_DIALOG_STYLE
);
205 wxBoxSizer
*button_sizer
= new wxBoxSizer( wxVERTICAL
);
206 button_sizer
->Add( new wxStaticText(dialog
, wxID_ANY
, _("Please wait while printing\n") + printout
->GetTitle() ), 0, wxALL
, 10 );
207 button_sizer
->Add( new wxButton( dialog
, wxID_CANCEL
, wxT("Cancel") ), 0, wxALL
| wxALIGN_CENTER
, 10 );
209 dialog
->SetAutoLayout( true );
210 dialog
->SetSizer( button_sizer
);
212 button_sizer
->Fit(dialog
);
213 button_sizer
->SetSizeHints (dialog
) ;
218 void wxPrinterBase::ReportError(wxWindow
*parent
, wxPrintout
*WXUNUSED(printout
), const wxString
& message
)
220 wxMessageBox(message
, _("Printing Error"), wxOK
, parent
);
223 wxPrintDialogData
& wxPrinterBase::GetPrintDialogData() const
225 return (wxPrintDialogData
&) m_printDialogData
;
228 //----------------------------------------------------------------------------
230 //----------------------------------------------------------------------------
232 IMPLEMENT_CLASS(wxPrinter
, wxPrinterBase
)
234 wxPrinter::wxPrinter(wxPrintDialogData
*data
)
236 m_pimpl
= wxPrintFactory::GetFactory()->CreatePrinter( data
);
239 wxPrinter::~wxPrinter()
244 wxWindow
*wxPrinter::CreateAbortWindow(wxWindow
*parent
, wxPrintout
*printout
)
246 return m_pimpl
->CreateAbortWindow( parent
, printout
);
249 void wxPrinter::ReportError(wxWindow
*parent
, wxPrintout
*printout
, const wxString
& message
)
251 m_pimpl
->ReportError( parent
, printout
, message
);
254 bool wxPrinter::Setup(wxWindow
*parent
)
256 return m_pimpl
->Setup( parent
);
259 bool wxPrinter::Print(wxWindow
*parent
, wxPrintout
*printout
, bool prompt
)
261 return m_pimpl
->Print( parent
, printout
, prompt
);
264 wxDC
* wxPrinter::PrintDialog(wxWindow
*parent
)
266 return m_pimpl
->PrintDialog( parent
);
269 wxPrintDialogData
& wxPrinter::GetPrintDialogData() const
271 return m_pimpl
->GetPrintDialogData();
274 // ---------------------------------------------------------------------------
275 // wxPrintDialogBase: the common dialog for printing.
276 // ---------------------------------------------------------------------------
278 IMPLEMENT_ABSTRACT_CLASS(wxPrintDialogBase
, wxObject
)
280 wxPrintDialogBase::wxPrintDialogBase(wxWindow
*parent
, wxWindowID id
,
281 const wxString
&title
, const wxPoint
&pos
, const wxSize
&size
, long style
) :
282 wxDialog( parent
, id
, title
, pos
, size
, style
)
286 // ---------------------------------------------------------------------------
287 // wxPrintDialog: the common dialog for printing.
288 // ---------------------------------------------------------------------------
290 IMPLEMENT_CLASS(wxPrintDialog
, wxObject
)
292 wxPrintDialog::wxPrintDialog(wxWindow
*parent
, wxPrintDialogData
* data
)
294 m_pimpl
= wxPrintFactory::GetFactory()->CreatePrintDialog( parent
, data
);
297 wxPrintDialog::wxPrintDialog(wxWindow
*parent
, wxPrintData
* data
)
299 m_pimpl
= wxPrintFactory::GetFactory()->CreatePrintDialog( parent
, data
);
302 wxPrintDialog::~wxPrintDialog()
307 int wxPrintDialog::ShowModal()
309 return m_pimpl
->ShowModal();
312 wxPrintDialogData
& wxPrintDialog::GetPrintDialogData()
314 return m_pimpl
->GetPrintDialogData();
317 wxPrintData
& wxPrintDialog::GetPrintData()
319 return m_pimpl
->GetPrintData();
321 wxDC
*wxPrintDialog::GetPrintDC()
323 return m_pimpl
->GetPrintDC();
326 //----------------------------------------------------------------------------
327 // wxPrintAbortDialog
328 //----------------------------------------------------------------------------
330 BEGIN_EVENT_TABLE(wxPrintAbortDialog
, wxDialog
)
331 EVT_BUTTON(wxID_CANCEL
, wxPrintAbortDialog::OnCancel
)
334 void wxPrintAbortDialog::OnCancel(wxCommandEvent
& WXUNUSED(event
))
336 wxPrinterBase::sm_abortIt
= true;
337 wxPrinterBase::sm_abortWindow
->Show(false);
338 wxPrinterBase::sm_abortWindow
->Close(true);
339 wxPrinterBase::sm_abortWindow
= (wxWindow
*) NULL
;
342 //----------------------------------------------------------------------------
344 //----------------------------------------------------------------------------
346 IMPLEMENT_ABSTRACT_CLASS(wxPrintout
, wxObject
)
348 wxPrintout::wxPrintout(const wxString
& title
)
350 m_printoutTitle
= title
;
351 m_printoutDC
= (wxDC
*) NULL
;
354 m_pageWidthPixels
= 0;
355 m_pageHeightPixels
= 0;
363 wxPrintout::~wxPrintout()
367 bool wxPrintout::OnBeginDocument(int WXUNUSED(startPage
), int WXUNUSED(endPage
))
369 return GetDC()->StartDoc(_("Printing ") + m_printoutTitle
);
372 void wxPrintout::OnEndDocument()
377 void wxPrintout::OnBeginPrinting()
381 void wxPrintout::OnEndPrinting()
385 bool wxPrintout::HasPage(int page
)
390 void wxPrintout::GetPageInfo(int *minPage
, int *maxPage
, int *fromPage
, int *toPage
)
398 //----------------------------------------------------------------------------
400 //----------------------------------------------------------------------------
402 IMPLEMENT_CLASS(wxPreviewCanvas
, wxWindow
)
404 BEGIN_EVENT_TABLE(wxPreviewCanvas
, wxScrolledWindow
)
405 EVT_PAINT(wxPreviewCanvas::OnPaint
)
406 EVT_CHAR(wxPreviewCanvas::OnChar
)
407 EVT_SYS_COLOUR_CHANGED(wxPreviewCanvas::OnSysColourChanged
)
410 // VZ: the current code doesn't refresh properly without
411 // wxFULL_REPAINT_ON_RESIZE, this must be fixed as otherwise we have
412 // really horrible flicker when resizing the preview frame, but without
413 // this style it simply doesn't work correctly at all...
414 wxPreviewCanvas::wxPreviewCanvas(wxPrintPreviewBase
*preview
, wxWindow
*parent
,
415 const wxPoint
& pos
, const wxSize
& size
, long style
, const wxString
& name
):
416 wxScrolledWindow(parent
, wxID_ANY
, pos
, size
, style
| wxFULL_REPAINT_ON_RESIZE
, name
)
418 m_printPreview
= preview
;
420 // The app workspace colour is always white, but we should have
421 // a contrast with the page.
422 wxSystemColour colourIndex
= wxSYS_COLOUR_3DDKSHADOW
;
424 wxSystemColour colourIndex
= wxSYS_COLOUR_APPWORKSPACE
;
426 SetBackgroundColour(wxSystemSettings::GetColour(colourIndex
));
428 SetScrollbars(10, 10, 100, 100);
431 wxPreviewCanvas::~wxPreviewCanvas()
435 void wxPreviewCanvas::OnPaint(wxPaintEvent
& WXUNUSED(event
))
442 if (!GetUpdateRegion().IsEmpty())
443 dc.SetClippingRegion( GetUpdateRegion() );
449 m_printPreview
->PaintPage(this, dc
);
453 // Responds to colour changes, and passes event on to children.
454 void wxPreviewCanvas::OnSysColourChanged(wxSysColourChangedEvent
& event
)
457 // The app workspace colour is always white, but we should have
458 // a contrast with the page.
459 wxSystemColour colourIndex
= wxSYS_COLOUR_3DDKSHADOW
;
461 wxSystemColour colourIndex
= wxSYS_COLOUR_APPWORKSPACE
;
463 SetBackgroundColour(wxSystemSettings::GetColour(colourIndex
));
466 // Propagate the event to the non-top-level children
467 wxWindow::OnSysColourChanged(event
);
470 void wxPreviewCanvas::OnChar(wxKeyEvent
&event
)
472 wxPreviewControlBar
* controlBar
= ((wxPreviewFrame
*) GetParent())->GetControlBar();
473 if (event
.GetKeyCode() == WXK_ESCAPE
)
475 ((wxPreviewFrame
*) GetParent())->Close(true);
478 else if (event
.GetKeyCode() == WXK_TAB
)
480 controlBar
->OnGoto();
483 else if (event
.GetKeyCode() == WXK_RETURN
)
485 controlBar
->OnPrint();
489 if (!event
.ControlDown())
495 switch(event
.GetKeyCode())
498 controlBar
->OnNext(); break;
500 controlBar
->OnPrevious(); break;
502 controlBar
->OnFirst(); break;
504 controlBar
->OnLast(); break;
510 //----------------------------------------------------------------------------
511 // wxPreviewControlBar
512 //----------------------------------------------------------------------------
514 IMPLEMENT_CLASS(wxPreviewControlBar
, wxWindow
)
516 BEGIN_EVENT_TABLE(wxPreviewControlBar
, wxPanel
)
517 EVT_BUTTON(wxID_PREVIEW_CLOSE
, wxPreviewControlBar::OnWindowClose
)
518 EVT_BUTTON(wxID_PREVIEW_PRINT
, wxPreviewControlBar::OnPrintButton
)
519 EVT_BUTTON(wxID_PREVIEW_PREVIOUS
, wxPreviewControlBar::OnPreviousButton
)
520 EVT_BUTTON(wxID_PREVIEW_NEXT
, wxPreviewControlBar::OnNextButton
)
521 EVT_BUTTON(wxID_PREVIEW_FIRST
, wxPreviewControlBar::OnFirstButton
)
522 EVT_BUTTON(wxID_PREVIEW_LAST
, wxPreviewControlBar::OnLastButton
)
523 EVT_BUTTON(wxID_PREVIEW_GOTO
, wxPreviewControlBar::OnGotoButton
)
524 EVT_CHOICE(wxID_PREVIEW_ZOOM
, wxPreviewControlBar::OnZoom
)
525 EVT_PAINT(wxPreviewControlBar::OnPaint
)
528 wxPreviewControlBar::wxPreviewControlBar(wxPrintPreviewBase
*preview
, long buttons
,
529 wxWindow
*parent
, const wxPoint
& pos
, const wxSize
& size
,
530 long style
, const wxString
& name
):
531 wxPanel(parent
, wxID_ANY
, pos
, size
, style
, name
)
533 m_printPreview
= preview
;
534 m_closeButton
= (wxButton
*) NULL
;
535 m_nextPageButton
= (wxButton
*) NULL
;
536 m_previousPageButton
= (wxButton
*) NULL
;
537 m_printButton
= (wxButton
*) NULL
;
538 m_zoomControl
= (wxChoice
*) NULL
;
539 m_buttonFlags
= buttons
;
542 wxPreviewControlBar::~wxPreviewControlBar()
546 void wxPreviewControlBar::OnPaint(wxPaintEvent
& WXUNUSED(event
))
552 dc
.SetPen(*wxBLACK_PEN
);
553 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
554 dc
.DrawLine( 0, h
-1, w
, h
-1 );
557 void wxPreviewControlBar::OnWindowClose(wxCommandEvent
& WXUNUSED(event
))
559 wxPreviewFrame
*frame
= (wxPreviewFrame
*)GetParent();
563 void wxPreviewControlBar::OnPrint(void)
565 wxPrintPreviewBase
*preview
= GetPrintPreview();
566 preview
->Print(true);
569 void wxPreviewControlBar::OnNext(void)
571 wxPrintPreviewBase
*preview
= GetPrintPreview();
574 int currentPage
= preview
->GetCurrentPage();
575 if ((preview
->GetMaxPage() > 0) &&
576 (currentPage
< preview
->GetMaxPage()) &&
577 preview
->GetPrintout()->HasPage(currentPage
+ 1))
579 preview
->SetCurrentPage(currentPage
+ 1);
584 void wxPreviewControlBar::OnPrevious(void)
586 wxPrintPreviewBase
*preview
= GetPrintPreview();
589 int currentPage
= preview
->GetCurrentPage();
590 if ((preview
->GetMinPage() > 0) &&
591 (currentPage
> preview
->GetMinPage()) &&
592 preview
->GetPrintout()->HasPage(currentPage
- 1))
594 preview
->SetCurrentPage(currentPage
- 1);
599 void wxPreviewControlBar::OnFirst(void)
601 wxPrintPreviewBase
*preview
= GetPrintPreview();
604 int currentPage
= preview
->GetMinPage();
605 if (preview
->GetPrintout()->HasPage(currentPage
))
607 preview
->SetCurrentPage(currentPage
);
612 void wxPreviewControlBar::OnLast(void)
614 wxPrintPreviewBase
*preview
= GetPrintPreview();
617 int currentPage
= preview
->GetMaxPage();
618 if (preview
->GetPrintout()->HasPage(currentPage
))
620 preview
->SetCurrentPage(currentPage
);
625 void wxPreviewControlBar::OnGoto(void)
627 wxPrintPreviewBase
*preview
= GetPrintPreview();
632 if (preview
->GetMinPage() > 0)
637 strPrompt
.Printf( _("Enter a page number between %d and %d:"),
638 preview
->GetMinPage(), preview
->GetMaxPage());
639 strPage
.Printf( wxT("%d"), preview
->GetCurrentPage() );
642 wxGetTextFromUser( strPrompt
, _("Goto Page"), strPage
, GetParent());
644 if ( strPage
.ToLong( ¤tPage
) )
645 if (preview
->GetPrintout()->HasPage(currentPage
))
647 preview
->SetCurrentPage(currentPage
);
653 void wxPreviewControlBar::OnZoom(wxCommandEvent
& WXUNUSED(event
))
655 int zoom
= GetZoomControl();
656 if (GetPrintPreview())
657 GetPrintPreview()->SetZoom(zoom
);
660 void wxPreviewControlBar::CreateButtons()
662 SetSize(0, 0, 400, 40);
664 wxBoxSizer
*item0
= new wxBoxSizer( wxHORIZONTAL
);
666 m_closeButton
= new wxButton( this, wxID_PREVIEW_CLOSE
, _("&Close"), wxDefaultPosition
, wxDefaultSize
, 0 );
667 item0
->Add( m_closeButton
, 0, wxALIGN_CENTRE
|wxALL
, 5 );
669 if (m_buttonFlags
& wxPREVIEW_PRINT
)
671 m_printButton
= new wxButton( this, wxID_PREVIEW_PRINT
, _("&Print..."), wxDefaultPosition
, wxDefaultSize
, 0 );
672 item0
->Add( m_printButton
, 0, wxALIGN_CENTRE
|wxALL
, 5 );
675 if (m_buttonFlags
& wxPREVIEW_FIRST
)
677 m_firstPageButton
= new wxButton( this, wxID_PREVIEW_FIRST
, _("|<<"), wxDefaultPosition
, wxDefaultSize
, wxBU_EXACTFIT
);
678 item0
->Add( m_firstPageButton
, 0, wxALIGN_CENTRE
|wxALL
, 5 );
681 if (m_buttonFlags
& wxPREVIEW_PREVIOUS
)
683 m_previousPageButton
= new wxButton( this, wxID_PREVIEW_PREVIOUS
, _("<<"), wxDefaultPosition
, wxDefaultSize
, wxBU_EXACTFIT
);
684 item0
->Add( m_previousPageButton
, 0, wxALIGN_CENTRE
|wxRIGHT
|wxTOP
|wxBOTTOM
, 5 );
687 if (m_buttonFlags
& wxPREVIEW_NEXT
)
689 m_nextPageButton
= new wxButton( this, wxID_PREVIEW_NEXT
, _(">>"), wxDefaultPosition
, wxDefaultSize
, wxBU_EXACTFIT
);
690 item0
->Add( m_nextPageButton
, 0, wxALIGN_CENTRE
|wxRIGHT
|wxTOP
|wxBOTTOM
, 5 );
693 if (m_buttonFlags
& wxPREVIEW_LAST
)
695 m_lastPageButton
= new wxButton( this, wxID_PREVIEW_LAST
, _(">>|"), wxDefaultPosition
, wxDefaultSize
, wxBU_EXACTFIT
);
696 item0
->Add( m_lastPageButton
, 0, wxALIGN_CENTRE
|wxRIGHT
|wxTOP
|wxBOTTOM
, 5 );
699 if (m_buttonFlags
& wxPREVIEW_GOTO
)
701 m_gotoPageButton
= new wxButton( this, wxID_PREVIEW_GOTO
, _("&Goto..."), wxDefaultPosition
, wxDefaultSize
, 0 );
702 item0
->Add( m_gotoPageButton
, 0, wxALIGN_CENTRE
|wxALL
, 5 );
705 if (m_buttonFlags
& wxPREVIEW_ZOOM
)
709 wxT("10%"), wxT("15%"), wxT("20%"), wxT("25%"), wxT("30%"), wxT("35%"), wxT("40%"), wxT("45%"), wxT("50%"), wxT("55%"),
710 wxT("60%"), wxT("65%"), wxT("70%"), wxT("75%"), wxT("80%"), wxT("85%"), wxT("90%"), wxT("95%"), wxT("100%"), wxT("110%"),
711 wxT("120%"), wxT("150%"), wxT("200%")
713 int n
= WXSIZEOF(choices
);
715 m_zoomControl
= new wxChoice( this, wxID_PREVIEW_ZOOM
, wxDefaultPosition
, wxSize(70,wxDefaultCoord
), n
, choices
, 0 );
716 item0
->Add( m_zoomControl
, 0, wxALIGN_CENTRE
|wxALL
, 5 );
717 SetZoomControl(m_printPreview
->GetZoom());
724 void wxPreviewControlBar::SetZoomControl(int zoom
)
728 int n
, count
= m_zoomControl
->GetCount();
730 for (n
=0; n
<count
; n
++)
732 if (m_zoomControl
->GetString(n
).BeforeFirst(wxT('%')).ToLong(&val
) &&
735 m_zoomControl
->SetSelection(n
);
740 m_zoomControl
->SetSelection(count
-1);
744 int wxPreviewControlBar::GetZoomControl()
746 if (m_zoomControl
&& (m_zoomControl
->GetStringSelection() != wxEmptyString
))
749 if (m_zoomControl
->GetStringSelection().BeforeFirst(wxT('%')).ToLong(&val
))
761 IMPLEMENT_CLASS(wxPreviewFrame
, wxFrame
)
763 BEGIN_EVENT_TABLE(wxPreviewFrame
, wxFrame
)
764 EVT_CLOSE(wxPreviewFrame::OnCloseWindow
)
767 wxPreviewFrame::wxPreviewFrame(wxPrintPreviewBase
*preview
, wxWindow
*parent
, const wxString
& title
,
768 const wxPoint
& pos
, const wxSize
& size
, long style
, const wxString
& name
):
769 wxFrame(parent
, wxID_ANY
, title
, pos
, size
, style
, name
)
771 m_printPreview
= preview
;
773 m_previewCanvas
= NULL
;
774 m_windowDisabler
= NULL
;
776 // Give the application icon
778 wxFrame
* topFrame
= wxDynamicCast(wxTheApp
->GetTopWindow(), wxFrame
);
780 SetIcon(topFrame
->GetIcon());
784 wxPreviewFrame::~wxPreviewFrame()
788 void wxPreviewFrame::OnCloseWindow(wxCloseEvent
& WXUNUSED(event
))
790 if (m_windowDisabler
)
791 delete m_windowDisabler
;
793 // Need to delete the printout and the print preview
794 wxPrintout
*printout
= m_printPreview
->GetPrintout();
798 m_printPreview
->SetPrintout(NULL
);
799 m_printPreview
->SetCanvas(NULL
);
800 m_printPreview
->SetFrame(NULL
);
802 delete m_printPreview
;
807 void wxPreviewFrame::Initialize()
815 m_printPreview
->SetCanvas(m_previewCanvas
);
816 m_printPreview
->SetFrame(this);
818 wxBoxSizer
*item0
= new wxBoxSizer( wxVERTICAL
);
820 item0
->Add( m_controlBar
, 0, wxGROW
|wxALIGN_CENTER_VERTICAL
, 5 );
821 item0
->Add( m_previewCanvas
, 1, wxGROW
|wxALIGN_CENTER_VERTICAL
, 5 );
823 SetAutoLayout( true );
826 m_windowDisabler
= new wxWindowDisabler(this);
830 m_printPreview
->AdjustScrollbars(m_previewCanvas
);
831 m_previewCanvas
->SetFocus();
832 m_controlBar
->SetFocus();
835 void wxPreviewFrame::CreateCanvas()
837 m_previewCanvas
= new wxPreviewCanvas(m_printPreview
, this);
840 void wxPreviewFrame::CreateControlBar()
842 long buttons
= wxPREVIEW_DEFAULT
;
843 if (m_printPreview
->GetPrintoutForPrinting())
844 buttons
|= wxPREVIEW_PRINT
;
846 m_controlBar
= new wxPreviewControlBar(m_printPreview
, buttons
, this, wxPoint(0, 0), wxSize(400, 40));
847 m_controlBar
->CreateButtons();
854 wxPrintPreviewBase::wxPrintPreviewBase(wxPrintout
*printout
,
855 wxPrintout
*printoutForPrinting
,
859 m_printDialogData
= (*data
);
861 Init(printout
, printoutForPrinting
);
864 wxPrintPreviewBase::wxPrintPreviewBase(wxPrintout
*printout
,
865 wxPrintout
*printoutForPrinting
,
866 wxPrintDialogData
*data
)
869 m_printDialogData
= (*data
);
871 Init(printout
, printoutForPrinting
);
874 void wxPrintPreviewBase::Init(wxPrintout
*printout
,
875 wxPrintout
*printoutForPrinting
)
878 m_previewPrintout
= printout
;
879 if (m_previewPrintout
)
880 m_previewPrintout
->SetIsPreview(true);
882 m_printPrintout
= printoutForPrinting
;
884 m_previewCanvas
= NULL
;
885 m_previewFrame
= NULL
;
886 m_previewBitmap
= NULL
;
893 m_printingPrepared
= false;
898 wxPrintPreviewBase::~wxPrintPreviewBase()
900 if (m_previewPrintout
)
901 delete m_previewPrintout
;
903 delete m_previewBitmap
;
905 delete m_printPrintout
;
908 bool wxPrintPreviewBase::SetCurrentPage(int pageNum
)
910 if (m_currentPage
== pageNum
)
913 m_currentPage
= pageNum
;
916 delete m_previewBitmap
;
917 m_previewBitmap
= NULL
;
922 AdjustScrollbars(m_previewCanvas
);
924 if (!RenderPage(pageNum
))
926 m_previewCanvas
->Refresh();
927 m_previewCanvas
->SetFocus();
932 int wxPrintPreviewBase::GetCurrentPage() const
933 { return m_currentPage
; };
934 void wxPrintPreviewBase::SetPrintout(wxPrintout
*printout
)
935 { m_previewPrintout
= printout
; };
936 wxPrintout
*wxPrintPreviewBase::GetPrintout() const
937 { return m_previewPrintout
; };
938 wxPrintout
*wxPrintPreviewBase::GetPrintoutForPrinting() const
939 { return m_printPrintout
; };
940 void wxPrintPreviewBase::SetFrame(wxFrame
*frame
)
941 { m_previewFrame
= frame
; };
942 void wxPrintPreviewBase::SetCanvas(wxPreviewCanvas
*canvas
)
943 { m_previewCanvas
= canvas
; };
944 wxFrame
*wxPrintPreviewBase::GetFrame() const
945 { return m_previewFrame
; }
946 wxPreviewCanvas
*wxPrintPreviewBase::GetCanvas() const
947 { return m_previewCanvas
; }
949 bool wxPrintPreviewBase::PaintPage(wxPreviewCanvas
*canvas
, wxDC
& dc
)
951 DrawBlankPage(canvas
, dc
);
953 if (!m_previewBitmap
)
954 if (!RenderPage(m_currentPage
))
957 if (!m_previewBitmap
)
963 int canvasWidth
, canvasHeight
;
964 canvas
->GetSize(&canvasWidth
, &canvasHeight
);
966 double zoomScale
= ((float)m_currentZoom
/(float)100);
967 double actualWidth
= (zoomScale
*m_pageWidth
*m_previewScale
);
968 // float actualHeight = (float)(zoomScale*m_pageHeight*m_previewScale);
970 int x
= (int) ((canvasWidth
- actualWidth
)/2.0);
971 if (x
< m_leftMargin
)
976 temp_dc
.SelectObject(*m_previewBitmap
);
978 dc
.Blit(x
, y
, m_previewBitmap
->GetWidth(), m_previewBitmap
->GetHeight(), &temp_dc
, 0, 0);
980 temp_dc
.SelectObject(wxNullBitmap
);
985 // Adjusts the scrollbars for the current scale
986 void wxPrintPreviewBase::AdjustScrollbars(wxPreviewCanvas
*canvas
)
991 int canvasWidth
, canvasHeight
;
992 canvas
->GetSize(&canvasWidth
, &canvasHeight
);
994 double zoomScale
= ((float)m_currentZoom
/(float)100);
995 double actualWidth
= (zoomScale
*m_pageWidth
*m_previewScale
);
996 double actualHeight
= (zoomScale
*m_pageHeight
*m_previewScale
);
998 // Set the scrollbars appropriately
999 int totalWidth
= (int)(actualWidth
+ 2*m_leftMargin
);
1000 int totalHeight
= (int)(actualHeight
+ 2*m_topMargin
);
1001 int scrollUnitsX
= totalWidth
/10;
1002 int scrollUnitsY
= totalHeight
/10;
1003 wxSize virtualSize
= canvas
->GetVirtualSize();
1004 if (virtualSize
.GetWidth() != totalWidth
|| virtualSize
.GetHeight() != totalHeight
)
1005 canvas
->SetScrollbars(10, 10, scrollUnitsX
, scrollUnitsY
, 0, 0, true);
1008 bool wxPrintPreviewBase::RenderPage(int pageNum
)
1012 int canvasWidth
, canvasHeight
;
1014 if (!m_previewCanvas
)
1016 wxFAIL_MSG(_T("wxPrintPreviewBase::RenderPage: must use wxPrintPreviewBase::SetCanvas to let me know about the canvas!"));
1020 m_previewCanvas
->GetSize(&canvasWidth
, &canvasHeight
);
1022 double zoomScale
= (m_currentZoom
/100.0);
1023 int actualWidth
= (int)(zoomScale
*m_pageWidth
*m_previewScale
);
1024 int actualHeight
= (int)(zoomScale
*m_pageHeight
*m_previewScale
);
1026 if (!m_previewBitmap
)
1028 m_previewBitmap
= new wxBitmap((int)actualWidth
, (int)actualHeight
);
1029 if (!m_previewBitmap
|| !m_previewBitmap
->Ok())
1031 if (m_previewBitmap
) {
1032 delete m_previewBitmap
;
1033 m_previewBitmap
= NULL
;
1035 wxMessageBox(_("Sorry, not enough memory to create a preview."), _("Print Preview Failure"), wxOK
);
1040 wxMemoryDC memoryDC
;
1041 memoryDC
.SelectObject(*m_previewBitmap
);
1045 m_previewPrintout
->SetDC(&memoryDC
);
1046 m_previewPrintout
->SetPageSizePixels(m_pageWidth
, m_pageHeight
);
1048 // Need to delay OnPreparePrinting until here, so we have enough information.
1049 if (!m_printingPrepared
)
1051 m_previewPrintout
->OnPreparePrinting();
1053 m_previewPrintout
->GetPageInfo(&m_minPage
, &m_maxPage
, &selFrom
, &selTo
);
1054 m_printingPrepared
= true;
1057 m_previewPrintout
->OnBeginPrinting();
1059 if (!m_previewPrintout
->OnBeginDocument(m_printDialogData
.GetFromPage(), m_printDialogData
.GetToPage()))
1061 wxMessageBox(_("Could not start document preview."), _("Print Preview Failure"), wxOK
);
1063 memoryDC
.SelectObject(wxNullBitmap
);
1065 delete m_previewBitmap
;
1066 m_previewBitmap
= NULL
;
1070 m_previewPrintout
->OnPrintPage(pageNum
);
1071 m_previewPrintout
->OnEndDocument();
1072 m_previewPrintout
->OnEndPrinting();
1074 m_previewPrintout
->SetDC(NULL
);
1076 memoryDC
.SelectObject(wxNullBitmap
);
1081 status
= wxString::Format(_("Page %d of %d"), pageNum
, m_maxPage
);
1083 status
= wxString::Format(_("Page %d"), pageNum
);
1086 m_previewFrame
->SetStatusText(status
);
1093 bool wxPrintPreviewBase::DrawBlankPage(wxPreviewCanvas
*canvas
, wxDC
& dc
)
1095 int canvasWidth
, canvasHeight
;
1096 canvas
->GetSize(&canvasWidth
, &canvasHeight
);
1098 float zoomScale
= (float)((float)m_currentZoom
/(float)100);
1099 float actualWidth
= zoomScale
*m_pageWidth
*m_previewScale
;
1100 float actualHeight
= zoomScale
*m_pageHeight
*m_previewScale
;
1102 float x
= (float)((canvasWidth
- actualWidth
)/2.0);
1103 if (x
< m_leftMargin
)
1104 x
= (float)m_leftMargin
;
1105 float y
= (float)m_topMargin
;
1107 // Draw shadow, allowing for 1-pixel border AROUND the actual page
1108 int shadowOffset
= 4;
1109 dc
.SetPen(*wxBLACK_PEN
);
1110 dc
.SetBrush(*wxBLACK_BRUSH
);
1112 dc.DrawRectangle((int)(x-1 + shadowOffset), (int)(y-1 + shadowOffset), (int)(actualWidth+2), (int)(actualHeight+2));
1114 dc
.DrawRectangle((int)(x
+ shadowOffset
), (int)(y
+ actualHeight
+1), (int)(actualWidth
), shadowOffset
);
1115 dc
.DrawRectangle((int)(x
+ actualWidth
), (int)(y
+ shadowOffset
), shadowOffset
, (int)(actualHeight
));
1117 // Draw blank page allowing for 1-pixel border AROUND the actual page
1118 dc
.SetPen(*wxBLACK_PEN
);
1119 dc
.SetBrush(*wxWHITE_BRUSH
);
1122 wxRegion update_region = canvas->GetUpdateRegion();
1123 wxRect r = update_region.GetBox();
1125 printf( "x: %d y: %d w: %d h: %d.\n", (int)r.x, (int)r.y, (int)r.width, (int)r.height );
1128 dc
.DrawRectangle((int)(x
-2), (int)(y
-1), (int)(actualWidth
+3), (int)(actualHeight
+2));
1133 void wxPrintPreviewBase::SetZoom(int percent
)
1135 if (m_currentZoom
== percent
)
1138 m_currentZoom
= percent
;
1139 if (m_previewBitmap
)
1141 delete m_previewBitmap
;
1142 m_previewBitmap
= NULL
;
1145 if (m_previewCanvas
)
1147 AdjustScrollbars(m_previewCanvas
);
1148 RenderPage(m_currentPage
);
1149 ((wxScrolledWindow
*) m_previewCanvas
)->Scroll(0, 0);
1150 m_previewCanvas
->ClearBackground();
1151 m_previewCanvas
->Refresh();
1152 m_previewCanvas
->SetFocus();
1156 wxPrintDialogData
& wxPrintPreviewBase::GetPrintDialogData()
1158 return m_printDialogData
;
1161 int wxPrintPreviewBase::GetZoom() const
1162 { return m_currentZoom
; }
1163 int wxPrintPreviewBase::GetMaxPage() const
1164 { return m_maxPage
; }
1165 int wxPrintPreviewBase::GetMinPage() const
1166 { return m_minPage
; }
1167 bool wxPrintPreviewBase::Ok() const
1169 void wxPrintPreviewBase::SetOk(bool ok
)
1171 //----------------------------------------------------------------------------
1173 //----------------------------------------------------------------------------
1175 IMPLEMENT_CLASS(wxPrintPreview
, wxPrintPreviewBase
)
1177 wxPrintPreview::wxPrintPreview(wxPrintout
*printout
,
1178 wxPrintout
*printoutForPrinting
,
1179 wxPrintDialogData
*data
) :
1180 wxPrintPreviewBase( printout
, printoutForPrinting
, data
)
1182 m_pimpl
= wxPrintFactory::GetFactory()->
1183 CreatePrintPreview( printout
, printoutForPrinting
, data
);
1186 wxPrintPreview::wxPrintPreview(wxPrintout
*printout
,
1187 wxPrintout
*printoutForPrinting
,
1188 wxPrintData
*data
) :
1189 wxPrintPreviewBase( printout
, printoutForPrinting
, data
)
1191 m_pimpl
= wxPrintFactory::GetFactory()->
1192 CreatePrintPreview( printout
, printoutForPrinting
, data
);
1195 wxPrintPreview::~wxPrintPreview()
1199 // don't delete twice
1200 m_printPrintout
= NULL
;
1201 m_previewPrintout
= NULL
;
1202 m_previewBitmap
= NULL
;
1205 bool wxPrintPreview::SetCurrentPage(int pageNum
)
1207 return m_pimpl
->SetCurrentPage( pageNum
);
1210 int wxPrintPreview::GetCurrentPage() const
1212 return m_pimpl
->GetCurrentPage();
1215 void wxPrintPreview::SetPrintout(wxPrintout
*printout
)
1217 m_pimpl
->SetPrintout( printout
);
1220 wxPrintout
*wxPrintPreview::GetPrintout() const
1222 return m_pimpl
->GetPrintout();
1225 wxPrintout
*wxPrintPreview::GetPrintoutForPrinting() const
1227 return m_pimpl
->GetPrintoutForPrinting();
1230 void wxPrintPreview::SetFrame(wxFrame
*frame
)
1232 m_pimpl
->SetFrame( frame
);
1235 void wxPrintPreview::SetCanvas(wxPreviewCanvas
*canvas
)
1237 m_pimpl
->SetCanvas( canvas
);
1240 wxFrame
*wxPrintPreview::GetFrame() const
1242 return m_pimpl
->GetFrame();
1245 wxPreviewCanvas
*wxPrintPreview::GetCanvas() const
1247 return m_pimpl
->GetCanvas();
1250 bool wxPrintPreview::PaintPage(wxPreviewCanvas
*canvas
, wxDC
& dc
)
1252 return m_pimpl
->PaintPage( canvas
, dc
);
1255 bool wxPrintPreview::DrawBlankPage(wxPreviewCanvas
*canvas
, wxDC
& dc
)
1257 return m_pimpl
->DrawBlankPage( canvas
, dc
);
1260 void wxPrintPreview::AdjustScrollbars(wxPreviewCanvas
*canvas
)
1262 m_pimpl
->AdjustScrollbars( canvas
);
1265 bool wxPrintPreview::RenderPage(int pageNum
)
1267 return m_pimpl
->RenderPage( pageNum
);
1270 void wxPrintPreview::SetZoom(int percent
)
1272 m_pimpl
->SetZoom( percent
);
1275 wxPrintDialogData
& wxPrintPreview::GetPrintDialogData()
1277 return m_pimpl
->GetPrintDialogData();
1280 int wxPrintPreview::GetMaxPage() const
1282 return m_pimpl
->GetMaxPage();
1285 int wxPrintPreview::GetMinPage() const
1287 return m_pimpl
->GetMinPage();
1290 bool wxPrintPreview::Ok() const
1292 return m_pimpl
->Ok();
1295 void wxPrintPreview::SetOk(bool ok
)
1297 m_pimpl
->SetOk( ok
);
1300 bool wxPrintPreview::Print(bool interactive
)
1302 return m_pimpl
->Print( interactive
);
1305 void wxPrintPreview::DetermineScaling()
1307 m_pimpl
->DetermineScaling();
1311 #endif // wxUSE_PRINTING_ARCHITECTURE