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__)
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__)
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__)
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 //----------------------------------------------------------------------------
126 //----------------------------------------------------------------------------
128 IMPLEMENT_CLASS(wxPrinterBase
, wxObject
)
130 wxPrinterBase::wxPrinterBase(wxPrintDialogData
*data
)
132 m_currentPrintout
= (wxPrintout
*) NULL
;
133 sm_abortWindow
= (wxWindow
*) NULL
;
136 m_printDialogData
= (*data
);
137 sm_lastError
= wxPRINTER_NO_ERROR
;
140 wxWindow
*wxPrinterBase::sm_abortWindow
= (wxWindow
*) NULL
;
141 bool wxPrinterBase::sm_abortIt
= false;
142 wxPrinterError
wxPrinterBase::sm_lastError
= wxPRINTER_NO_ERROR
;
144 wxPrinterBase::~wxPrinterBase()
148 wxWindow
*wxPrinterBase::CreateAbortWindow(wxWindow
*parent
, wxPrintout
* printout
)
150 wxPrintAbortDialog
*dialog
= new wxPrintAbortDialog(parent
, _("Printing ") , wxDefaultPosition
, wxDefaultSize
, wxDEFAULT_DIALOG_STYLE
);
152 wxBoxSizer
*button_sizer
= new wxBoxSizer( wxVERTICAL
);
153 button_sizer
->Add( new wxStaticText(dialog
, wxID_ANY
, _("Please wait while printing\n") + printout
->GetTitle() ), 0, wxALL
, 10 );
154 button_sizer
->Add( new wxButton( dialog
, wxID_CANCEL
, wxT("Cancel") ), 0, wxALL
| wxALIGN_CENTER
, 10 );
156 dialog
->SetAutoLayout( true );
157 dialog
->SetSizer( button_sizer
);
159 button_sizer
->Fit(dialog
);
160 button_sizer
->SetSizeHints (dialog
) ;
165 void wxPrinterBase::ReportError(wxWindow
*parent
, wxPrintout
*WXUNUSED(printout
), const wxString
& message
)
167 wxMessageBox(message
, _("Printing Error"), wxOK
, parent
);
170 //----------------------------------------------------------------------------
172 //----------------------------------------------------------------------------
174 IMPLEMENT_CLASS(wxPrinter
, wxPrinterBase
)
176 wxPrinter::wxPrinter(wxPrintDialogData
*data
)
178 m_pimpl
= wxPrintFactory::GetFactory()->CreatePrinter( data
);
181 wxPrinter::~wxPrinter()
186 wxWindow
*wxPrinter::CreateAbortWindow(wxWindow
*parent
, wxPrintout
*printout
)
188 return m_pimpl
->CreateAbortWindow( parent
, printout
);
191 void wxPrinter::ReportError(wxWindow
*parent
, wxPrintout
*printout
, const wxString
& message
)
193 m_pimpl
->ReportError( parent
, printout
, message
);
196 bool wxPrinter::Setup(wxWindow
*parent
)
198 return m_pimpl
->Setup( parent
);
201 bool wxPrinter::Print(wxWindow
*parent
, wxPrintout
*printout
, bool prompt
)
203 return m_pimpl
->Print( parent
, printout
, prompt
);
206 wxDC
* wxPrinter::PrintDialog(wxWindow
*parent
)
208 return m_pimpl
->PrintDialog( parent
);
211 //----------------------------------------------------------------------------
212 // wxPrintAbortDialog
213 //----------------------------------------------------------------------------
215 BEGIN_EVENT_TABLE(wxPrintAbortDialog
, wxDialog
)
216 EVT_BUTTON(wxID_CANCEL
, wxPrintAbortDialog::OnCancel
)
219 void wxPrintAbortDialog::OnCancel(wxCommandEvent
& WXUNUSED(event
))
221 wxPrinterBase::sm_abortIt
= true;
222 wxPrinterBase::sm_abortWindow
->Show(false);
223 wxPrinterBase::sm_abortWindow
->Close(true);
224 wxPrinterBase::sm_abortWindow
= (wxWindow
*) NULL
;
227 //----------------------------------------------------------------------------
229 //----------------------------------------------------------------------------
231 IMPLEMENT_ABSTRACT_CLASS(wxPrintout
, wxObject
)
233 wxPrintout::wxPrintout(const wxString
& title
)
235 m_printoutTitle
= title
;
236 m_printoutDC
= (wxDC
*) NULL
;
239 m_pageWidthPixels
= 0;
240 m_pageHeightPixels
= 0;
248 wxPrintout::~wxPrintout()
252 bool wxPrintout::OnBeginDocument(int WXUNUSED(startPage
), int WXUNUSED(endPage
))
254 return GetDC()->StartDoc(_("Printing ") + m_printoutTitle
);
257 void wxPrintout::OnEndDocument()
262 void wxPrintout::OnBeginPrinting()
266 void wxPrintout::OnEndPrinting()
270 bool wxPrintout::HasPage(int page
)
275 void wxPrintout::GetPageInfo(int *minPage
, int *maxPage
, int *fromPage
, int *toPage
)
283 //----------------------------------------------------------------------------
285 //----------------------------------------------------------------------------
287 IMPLEMENT_CLASS(wxPreviewCanvas
, wxWindow
)
289 BEGIN_EVENT_TABLE(wxPreviewCanvas
, wxScrolledWindow
)
290 EVT_PAINT(wxPreviewCanvas::OnPaint
)
291 EVT_CHAR(wxPreviewCanvas::OnChar
)
292 EVT_SYS_COLOUR_CHANGED(wxPreviewCanvas::OnSysColourChanged
)
295 // VZ: the current code doesn't refresh properly without
296 // wxFULL_REPAINT_ON_RESIZE, this must be fixed as otherwise we have
297 // really horrible flicker when resizing the preview frame, but without
298 // this style it simply doesn't work correctly at all...
299 wxPreviewCanvas::wxPreviewCanvas(wxPrintPreviewBase
*preview
, wxWindow
*parent
,
300 const wxPoint
& pos
, const wxSize
& size
, long style
, const wxString
& name
):
301 wxScrolledWindow(parent
, wxID_ANY
, pos
, size
, style
| wxFULL_REPAINT_ON_RESIZE
, name
)
303 m_printPreview
= preview
;
305 // The app workspace colour is always white, but we should have
306 // a contrast with the page.
307 wxSystemColour colourIndex
= wxSYS_COLOUR_3DDKSHADOW
;
309 wxSystemColour colourIndex
= wxSYS_COLOUR_APPWORKSPACE
;
311 SetBackgroundColour(wxSystemSettings::GetColour(colourIndex
));
313 SetScrollbars(10, 10, 100, 100);
316 wxPreviewCanvas::~wxPreviewCanvas()
320 void wxPreviewCanvas::OnPaint(wxPaintEvent
& WXUNUSED(event
))
327 if (!GetUpdateRegion().IsEmpty())
328 dc.SetClippingRegion( GetUpdateRegion() );
334 m_printPreview
->PaintPage(this, dc
);
338 // Responds to colour changes, and passes event on to children.
339 void wxPreviewCanvas::OnSysColourChanged(wxSysColourChangedEvent
& event
)
342 // The app workspace colour is always white, but we should have
343 // a contrast with the page.
344 wxSystemColour colourIndex
= wxSYS_COLOUR_3DDKSHADOW
;
346 wxSystemColour colourIndex
= wxSYS_COLOUR_APPWORKSPACE
;
348 SetBackgroundColour(wxSystemSettings::GetColour(colourIndex
));
351 // Propagate the event to the non-top-level children
352 wxWindow::OnSysColourChanged(event
);
355 void wxPreviewCanvas::OnChar(wxKeyEvent
&event
)
357 wxPreviewControlBar
* controlBar
= ((wxPreviewFrame
*) GetParent())->GetControlBar();
358 if (event
.GetKeyCode() == WXK_ESCAPE
)
360 ((wxPreviewFrame
*) GetParent())->Close(true);
363 else if (event
.GetKeyCode() == WXK_TAB
)
365 controlBar
->OnGoto();
368 else if (event
.GetKeyCode() == WXK_RETURN
)
370 controlBar
->OnPrint();
374 if (!event
.ControlDown())
380 switch(event
.GetKeyCode())
383 controlBar
->OnNext(); break;
385 controlBar
->OnPrevious(); break;
387 controlBar
->OnFirst(); break;
389 controlBar
->OnLast(); break;
395 //----------------------------------------------------------------------------
396 // wxPreviewControlBar
397 //----------------------------------------------------------------------------
399 IMPLEMENT_CLASS(wxPreviewControlBar
, wxWindow
)
401 BEGIN_EVENT_TABLE(wxPreviewControlBar
, wxPanel
)
402 EVT_BUTTON(wxID_PREVIEW_CLOSE
, wxPreviewControlBar::OnWindowClose
)
403 EVT_BUTTON(wxID_PREVIEW_PRINT
, wxPreviewControlBar::OnPrintButton
)
404 EVT_BUTTON(wxID_PREVIEW_PREVIOUS
, wxPreviewControlBar::OnPreviousButton
)
405 EVT_BUTTON(wxID_PREVIEW_NEXT
, wxPreviewControlBar::OnNextButton
)
406 EVT_BUTTON(wxID_PREVIEW_FIRST
, wxPreviewControlBar::OnFirstButton
)
407 EVT_BUTTON(wxID_PREVIEW_LAST
, wxPreviewControlBar::OnLastButton
)
408 EVT_BUTTON(wxID_PREVIEW_GOTO
, wxPreviewControlBar::OnGotoButton
)
409 EVT_CHOICE(wxID_PREVIEW_ZOOM
, wxPreviewControlBar::OnZoom
)
410 EVT_PAINT(wxPreviewControlBar::OnPaint
)
413 wxPreviewControlBar::wxPreviewControlBar(wxPrintPreviewBase
*preview
, long buttons
,
414 wxWindow
*parent
, const wxPoint
& pos
, const wxSize
& size
,
415 long style
, const wxString
& name
):
416 wxPanel(parent
, wxID_ANY
, pos
, size
, style
, name
)
418 m_printPreview
= preview
;
419 m_closeButton
= (wxButton
*) NULL
;
420 m_nextPageButton
= (wxButton
*) NULL
;
421 m_previousPageButton
= (wxButton
*) NULL
;
422 m_printButton
= (wxButton
*) NULL
;
423 m_zoomControl
= (wxChoice
*) NULL
;
424 m_buttonFlags
= buttons
;
427 wxPreviewControlBar::~wxPreviewControlBar()
431 void wxPreviewControlBar::OnPaint(wxPaintEvent
& WXUNUSED(event
))
437 dc
.SetPen(*wxBLACK_PEN
);
438 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
439 dc
.DrawLine( 0, h
-1, w
, h
-1 );
442 void wxPreviewControlBar::OnWindowClose(wxCommandEvent
& WXUNUSED(event
))
444 wxPreviewFrame
*frame
= (wxPreviewFrame
*)GetParent();
448 void wxPreviewControlBar::OnPrint(void)
450 wxPrintPreviewBase
*preview
= GetPrintPreview();
451 preview
->Print(true);
454 void wxPreviewControlBar::OnNext(void)
456 wxPrintPreviewBase
*preview
= GetPrintPreview();
459 int currentPage
= preview
->GetCurrentPage();
460 if ((preview
->GetMaxPage() > 0) &&
461 (currentPage
< preview
->GetMaxPage()) &&
462 preview
->GetPrintout()->HasPage(currentPage
+ 1))
464 preview
->SetCurrentPage(currentPage
+ 1);
469 void wxPreviewControlBar::OnPrevious(void)
471 wxPrintPreviewBase
*preview
= GetPrintPreview();
474 int currentPage
= preview
->GetCurrentPage();
475 if ((preview
->GetMinPage() > 0) &&
476 (currentPage
> preview
->GetMinPage()) &&
477 preview
->GetPrintout()->HasPage(currentPage
- 1))
479 preview
->SetCurrentPage(currentPage
- 1);
484 void wxPreviewControlBar::OnFirst(void)
486 wxPrintPreviewBase
*preview
= GetPrintPreview();
489 int currentPage
= preview
->GetMinPage();
490 if (preview
->GetPrintout()->HasPage(currentPage
))
492 preview
->SetCurrentPage(currentPage
);
497 void wxPreviewControlBar::OnLast(void)
499 wxPrintPreviewBase
*preview
= GetPrintPreview();
502 int currentPage
= preview
->GetMaxPage();
503 if (preview
->GetPrintout()->HasPage(currentPage
))
505 preview
->SetCurrentPage(currentPage
);
510 void wxPreviewControlBar::OnGoto(void)
512 wxPrintPreviewBase
*preview
= GetPrintPreview();
517 if (preview
->GetMinPage() > 0)
522 strPrompt
.Printf( _("Enter a page number between %d and %d:"),
523 preview
->GetMinPage(), preview
->GetMaxPage());
524 strPage
.Printf( wxT("%d"), preview
->GetCurrentPage() );
527 wxGetTextFromUser( strPrompt
, _("Goto Page"), strPage
, GetParent());
529 if ( strPage
.ToLong( ¤tPage
) )
530 if (preview
->GetPrintout()->HasPage(currentPage
))
532 preview
->SetCurrentPage(currentPage
);
538 void wxPreviewControlBar::OnZoom(wxCommandEvent
& WXUNUSED(event
))
540 int zoom
= GetZoomControl();
541 if (GetPrintPreview())
542 GetPrintPreview()->SetZoom(zoom
);
545 void wxPreviewControlBar::CreateButtons()
547 SetSize(0, 0, 400, 40);
549 wxBoxSizer
*item0
= new wxBoxSizer( wxHORIZONTAL
);
551 m_closeButton
= new wxButton( this, wxID_PREVIEW_CLOSE
, _("&Close"), wxDefaultPosition
, wxDefaultSize
, 0 );
552 item0
->Add( m_closeButton
, 0, wxALIGN_CENTRE
|wxALL
, 5 );
554 if (m_buttonFlags
& wxPREVIEW_PRINT
)
556 m_printButton
= new wxButton( this, wxID_PREVIEW_PRINT
, _("&Print..."), wxDefaultPosition
, wxDefaultSize
, 0 );
557 item0
->Add( m_printButton
, 0, wxALIGN_CENTRE
|wxALL
, 5 );
560 if (m_buttonFlags
& wxPREVIEW_FIRST
)
562 m_firstPageButton
= new wxButton( this, wxID_PREVIEW_FIRST
, _("|<<"), wxDefaultPosition
, wxDefaultSize
, wxBU_EXACTFIT
);
563 item0
->Add( m_firstPageButton
, 0, wxALIGN_CENTRE
|wxALL
, 5 );
566 if (m_buttonFlags
& wxPREVIEW_PREVIOUS
)
568 m_previousPageButton
= new wxButton( this, wxID_PREVIEW_PREVIOUS
, _("<<"), wxDefaultPosition
, wxDefaultSize
, wxBU_EXACTFIT
);
569 item0
->Add( m_previousPageButton
, 0, wxALIGN_CENTRE
|wxRIGHT
|wxTOP
|wxBOTTOM
, 5 );
572 if (m_buttonFlags
& wxPREVIEW_NEXT
)
574 m_nextPageButton
= new wxButton( this, wxID_PREVIEW_NEXT
, _(">>"), wxDefaultPosition
, wxDefaultSize
, wxBU_EXACTFIT
);
575 item0
->Add( m_nextPageButton
, 0, wxALIGN_CENTRE
|wxRIGHT
|wxTOP
|wxBOTTOM
, 5 );
578 if (m_buttonFlags
& wxPREVIEW_LAST
)
580 m_lastPageButton
= new wxButton( this, wxID_PREVIEW_LAST
, _(">>|"), wxDefaultPosition
, wxDefaultSize
, wxBU_EXACTFIT
);
581 item0
->Add( m_lastPageButton
, 0, wxALIGN_CENTRE
|wxRIGHT
|wxTOP
|wxBOTTOM
, 5 );
584 if (m_buttonFlags
& wxPREVIEW_GOTO
)
586 m_gotoPageButton
= new wxButton( this, wxID_PREVIEW_GOTO
, _("&Goto..."), wxDefaultPosition
, wxDefaultSize
, 0 );
587 item0
->Add( m_gotoPageButton
, 0, wxALIGN_CENTRE
|wxALL
, 5 );
590 if (m_buttonFlags
& wxPREVIEW_ZOOM
)
594 wxT("10%"), wxT("15%"), wxT("20%"), wxT("25%"), wxT("30%"), wxT("35%"), wxT("40%"), wxT("45%"), wxT("50%"), wxT("55%"),
595 wxT("60%"), wxT("65%"), wxT("70%"), wxT("75%"), wxT("80%"), wxT("85%"), wxT("90%"), wxT("95%"), wxT("100%"), wxT("110%"),
596 wxT("120%"), wxT("150%"), wxT("200%")
598 int n
= WXSIZEOF(choices
);
600 m_zoomControl
= new wxChoice( this, wxID_PREVIEW_ZOOM
, wxDefaultPosition
, wxSize(70,wxDefaultCoord
), n
, choices
, 0 );
601 item0
->Add( m_zoomControl
, 0, wxALIGN_CENTRE
|wxALL
, 5 );
602 SetZoomControl(m_printPreview
->GetZoom());
609 void wxPreviewControlBar::SetZoomControl(int zoom
)
613 int n
, count
= m_zoomControl
->GetCount();
615 for (n
=0; n
<count
; n
++)
617 if (m_zoomControl
->GetString(n
).BeforeFirst(wxT('%')).ToLong(&val
) &&
620 m_zoomControl
->SetSelection(n
);
625 m_zoomControl
->SetSelection(count
-1);
629 int wxPreviewControlBar::GetZoomControl()
631 if (m_zoomControl
&& (m_zoomControl
->GetStringSelection() != wxEmptyString
))
634 if (m_zoomControl
->GetStringSelection().BeforeFirst(wxT('%')).ToLong(&val
))
646 IMPLEMENT_CLASS(wxPreviewFrame
, wxFrame
)
648 BEGIN_EVENT_TABLE(wxPreviewFrame
, wxFrame
)
649 EVT_CLOSE(wxPreviewFrame::OnCloseWindow
)
652 wxPreviewFrame::wxPreviewFrame(wxPrintPreviewBase
*preview
, wxWindow
*parent
, const wxString
& title
,
653 const wxPoint
& pos
, const wxSize
& size
, long style
, const wxString
& name
):
654 wxFrame(parent
, wxID_ANY
, title
, pos
, size
, style
, name
)
656 m_printPreview
= preview
;
658 m_previewCanvas
= NULL
;
659 m_windowDisabler
= NULL
;
661 // Give the application icon
663 wxFrame
* topFrame
= wxDynamicCast(wxTheApp
->GetTopWindow(), wxFrame
);
665 SetIcon(topFrame
->GetIcon());
669 wxPreviewFrame::~wxPreviewFrame()
673 void wxPreviewFrame::OnCloseWindow(wxCloseEvent
& WXUNUSED(event
))
675 if (m_windowDisabler
)
676 delete m_windowDisabler
;
678 // Need to delete the printout and the print preview
679 wxPrintout
*printout
= m_printPreview
->GetPrintout();
683 m_printPreview
->SetPrintout(NULL
);
684 m_printPreview
->SetCanvas(NULL
);
685 m_printPreview
->SetFrame(NULL
);
687 delete m_printPreview
;
692 void wxPreviewFrame::Initialize()
700 m_printPreview
->SetCanvas(m_previewCanvas
);
701 m_printPreview
->SetFrame(this);
703 wxBoxSizer
*item0
= new wxBoxSizer( wxVERTICAL
);
705 item0
->Add( m_controlBar
, 0, wxGROW
|wxALIGN_CENTER_VERTICAL
, 5 );
706 item0
->Add( m_previewCanvas
, 1, wxGROW
|wxALIGN_CENTER_VERTICAL
, 5 );
708 SetAutoLayout( true );
711 m_windowDisabler
= new wxWindowDisabler(this);
715 m_printPreview
->AdjustScrollbars(m_previewCanvas
);
716 m_previewCanvas
->SetFocus();
717 m_controlBar
->SetFocus();
720 void wxPreviewFrame::CreateCanvas()
722 m_previewCanvas
= new wxPreviewCanvas(m_printPreview
, this);
725 void wxPreviewFrame::CreateControlBar()
727 long buttons
= wxPREVIEW_DEFAULT
;
728 if (m_printPreview
->GetPrintoutForPrinting())
729 buttons
|= wxPREVIEW_PRINT
;
731 m_controlBar
= new wxPreviewControlBar(m_printPreview
, buttons
, this, wxPoint(0, 0), wxSize(400, 40));
732 m_controlBar
->CreateButtons();
739 wxPrintPreviewBase::wxPrintPreviewBase(wxPrintout
*printout
,
740 wxPrintout
*printoutForPrinting
,
744 m_printDialogData
= (*data
);
746 Init(printout
, printoutForPrinting
);
749 wxPrintPreviewBase::wxPrintPreviewBase(wxPrintout
*printout
,
750 wxPrintout
*printoutForPrinting
,
751 wxPrintDialogData
*data
)
754 m_printDialogData
= (*data
);
756 Init(printout
, printoutForPrinting
);
759 void wxPrintPreviewBase::Init(wxPrintout
*printout
,
760 wxPrintout
*printoutForPrinting
)
763 m_previewPrintout
= printout
;
764 if (m_previewPrintout
)
765 m_previewPrintout
->SetIsPreview(true);
767 m_printPrintout
= printoutForPrinting
;
769 m_previewCanvas
= NULL
;
770 m_previewFrame
= NULL
;
771 m_previewBitmap
= NULL
;
778 m_printingPrepared
= false;
783 wxPrintPreviewBase::~wxPrintPreviewBase()
785 if (m_previewPrintout
)
786 delete m_previewPrintout
;
788 delete m_previewBitmap
;
790 delete m_printPrintout
;
793 bool wxPrintPreviewBase::SetCurrentPage(int pageNum
)
795 if (m_currentPage
== pageNum
)
798 m_currentPage
= pageNum
;
801 delete m_previewBitmap
;
802 m_previewBitmap
= NULL
;
807 AdjustScrollbars(m_previewCanvas
);
809 if (!RenderPage(pageNum
))
811 m_previewCanvas
->Refresh();
812 m_previewCanvas
->SetFocus();
817 int wxPrintPreviewBase::GetCurrentPage() const
818 { return m_currentPage
; };
819 void wxPrintPreviewBase::SetPrintout(wxPrintout
*printout
)
820 { m_previewPrintout
= printout
; };
821 wxPrintout
*wxPrintPreviewBase::GetPrintout() const
822 { return m_previewPrintout
; };
823 wxPrintout
*wxPrintPreviewBase::GetPrintoutForPrinting() const
824 { return m_printPrintout
; };
825 void wxPrintPreviewBase::SetFrame(wxFrame
*frame
)
826 { m_previewFrame
= frame
; };
827 void wxPrintPreviewBase::SetCanvas(wxPreviewCanvas
*canvas
)
828 { m_previewCanvas
= canvas
; };
829 wxFrame
*wxPrintPreviewBase::GetFrame() const
830 { return m_previewFrame
; }
831 wxPreviewCanvas
*wxPrintPreviewBase::GetCanvas() const
832 { return m_previewCanvas
; }
834 bool wxPrintPreviewBase::PaintPage(wxPreviewCanvas
*canvas
, wxDC
& dc
)
836 DrawBlankPage(canvas
, dc
);
838 if (!m_previewBitmap
)
839 if (!RenderPage(m_currentPage
))
842 if (!m_previewBitmap
)
848 int canvasWidth
, canvasHeight
;
849 canvas
->GetSize(&canvasWidth
, &canvasHeight
);
851 double zoomScale
= ((float)m_currentZoom
/(float)100);
852 double actualWidth
= (zoomScale
*m_pageWidth
*m_previewScale
);
853 // float actualHeight = (float)(zoomScale*m_pageHeight*m_previewScale);
855 int x
= (int) ((canvasWidth
- actualWidth
)/2.0);
856 if (x
< m_leftMargin
)
861 temp_dc
.SelectObject(*m_previewBitmap
);
863 dc
.Blit(x
, y
, m_previewBitmap
->GetWidth(), m_previewBitmap
->GetHeight(), &temp_dc
, 0, 0);
865 temp_dc
.SelectObject(wxNullBitmap
);
870 // Adjusts the scrollbars for the current scale
871 void wxPrintPreviewBase::AdjustScrollbars(wxPreviewCanvas
*canvas
)
876 int canvasWidth
, canvasHeight
;
877 canvas
->GetSize(&canvasWidth
, &canvasHeight
);
879 double zoomScale
= ((float)m_currentZoom
/(float)100);
880 double actualWidth
= (zoomScale
*m_pageWidth
*m_previewScale
);
881 double actualHeight
= (zoomScale
*m_pageHeight
*m_previewScale
);
883 // Set the scrollbars appropriately
884 int totalWidth
= (int)(actualWidth
+ 2*m_leftMargin
);
885 int totalHeight
= (int)(actualHeight
+ 2*m_topMargin
);
886 int scrollUnitsX
= totalWidth
/10;
887 int scrollUnitsY
= totalHeight
/10;
888 wxSize virtualSize
= canvas
->GetVirtualSize();
889 if (virtualSize
.GetWidth() != totalWidth
|| virtualSize
.GetHeight() != totalHeight
)
890 canvas
->SetScrollbars(10, 10, scrollUnitsX
, scrollUnitsY
, 0, 0, true);
893 bool wxPrintPreviewBase::RenderPage(int pageNum
)
897 int canvasWidth
, canvasHeight
;
899 if (!m_previewCanvas
)
901 wxFAIL_MSG(_T("wxPrintPreviewBase::RenderPage: must use wxPrintPreviewBase::SetCanvas to let me know about the canvas!"));
905 m_previewCanvas
->GetSize(&canvasWidth
, &canvasHeight
);
907 double zoomScale
= (m_currentZoom
/100.0);
908 int actualWidth
= (int)(zoomScale
*m_pageWidth
*m_previewScale
);
909 int actualHeight
= (int)(zoomScale
*m_pageHeight
*m_previewScale
);
911 if (!m_previewBitmap
)
913 m_previewBitmap
= new wxBitmap((int)actualWidth
, (int)actualHeight
);
914 if (!m_previewBitmap
|| !m_previewBitmap
->Ok())
916 if (m_previewBitmap
) {
917 delete m_previewBitmap
;
918 m_previewBitmap
= NULL
;
920 wxMessageBox(_("Sorry, not enough memory to create a preview."), _("Print Preview Failure"), wxOK
);
926 memoryDC
.SelectObject(*m_previewBitmap
);
930 m_previewPrintout
->SetDC(&memoryDC
);
931 m_previewPrintout
->SetPageSizePixels(m_pageWidth
, m_pageHeight
);
933 // Need to delay OnPreparePrinting until here, so we have enough information.
934 if (!m_printingPrepared
)
936 m_previewPrintout
->OnPreparePrinting();
938 m_previewPrintout
->GetPageInfo(&m_minPage
, &m_maxPage
, &selFrom
, &selTo
);
939 m_printingPrepared
= true;
942 m_previewPrintout
->OnBeginPrinting();
944 if (!m_previewPrintout
->OnBeginDocument(m_printDialogData
.GetFromPage(), m_printDialogData
.GetToPage()))
946 wxMessageBox(_("Could not start document preview."), _("Print Preview Failure"), wxOK
);
948 memoryDC
.SelectObject(wxNullBitmap
);
950 delete m_previewBitmap
;
951 m_previewBitmap
= NULL
;
955 m_previewPrintout
->OnPrintPage(pageNum
);
956 m_previewPrintout
->OnEndDocument();
957 m_previewPrintout
->OnEndPrinting();
959 m_previewPrintout
->SetDC(NULL
);
961 memoryDC
.SelectObject(wxNullBitmap
);
966 status
= wxString::Format(_("Page %d of %d"), pageNum
, m_maxPage
);
968 status
= wxString::Format(_("Page %d"), pageNum
);
971 m_previewFrame
->SetStatusText(status
);
978 bool wxPrintPreviewBase::DrawBlankPage(wxPreviewCanvas
*canvas
, wxDC
& dc
)
980 int canvasWidth
, canvasHeight
;
981 canvas
->GetSize(&canvasWidth
, &canvasHeight
);
983 float zoomScale
= (float)((float)m_currentZoom
/(float)100);
984 float actualWidth
= zoomScale
*m_pageWidth
*m_previewScale
;
985 float actualHeight
= zoomScale
*m_pageHeight
*m_previewScale
;
987 float x
= (float)((canvasWidth
- actualWidth
)/2.0);
988 if (x
< m_leftMargin
)
989 x
= (float)m_leftMargin
;
990 float y
= (float)m_topMargin
;
992 // Draw shadow, allowing for 1-pixel border AROUND the actual page
993 int shadowOffset
= 4;
994 dc
.SetPen(*wxBLACK_PEN
);
995 dc
.SetBrush(*wxBLACK_BRUSH
);
997 dc.DrawRectangle((int)(x-1 + shadowOffset), (int)(y-1 + shadowOffset), (int)(actualWidth+2), (int)(actualHeight+2));
999 dc
.DrawRectangle((int)(x
+ shadowOffset
), (int)(y
+ actualHeight
+1), (int)(actualWidth
), shadowOffset
);
1000 dc
.DrawRectangle((int)(x
+ actualWidth
), (int)(y
+ shadowOffset
), shadowOffset
, (int)(actualHeight
));
1002 // Draw blank page allowing for 1-pixel border AROUND the actual page
1003 dc
.SetPen(*wxBLACK_PEN
);
1004 dc
.SetBrush(*wxWHITE_BRUSH
);
1007 wxRegion update_region = canvas->GetUpdateRegion();
1008 wxRect r = update_region.GetBox();
1010 printf( "x: %d y: %d w: %d h: %d.\n", (int)r.x, (int)r.y, (int)r.width, (int)r.height );
1013 dc
.DrawRectangle((int)(x
-2), (int)(y
-1), (int)(actualWidth
+3), (int)(actualHeight
+2));
1018 void wxPrintPreviewBase::SetZoom(int percent
)
1020 if (m_currentZoom
== percent
)
1023 m_currentZoom
= percent
;
1024 if (m_previewBitmap
)
1026 delete m_previewBitmap
;
1027 m_previewBitmap
= NULL
;
1030 if (m_previewCanvas
)
1032 AdjustScrollbars(m_previewCanvas
);
1033 RenderPage(m_currentPage
);
1034 ((wxScrolledWindow
*) m_previewCanvas
)->Scroll(0, 0);
1035 m_previewCanvas
->ClearBackground();
1036 m_previewCanvas
->Refresh();
1037 m_previewCanvas
->SetFocus();
1041 wxPrintDialogData
& wxPrintPreviewBase::GetPrintDialogData()
1043 return m_printDialogData
;
1046 int wxPrintPreviewBase::GetZoom() const
1047 { return m_currentZoom
; }
1048 int wxPrintPreviewBase::GetMaxPage() const
1049 { return m_maxPage
; }
1050 int wxPrintPreviewBase::GetMinPage() const
1051 { return m_minPage
; }
1052 bool wxPrintPreviewBase::Ok() const
1054 void wxPrintPreviewBase::SetOk(bool ok
)
1056 //----------------------------------------------------------------------------
1058 //----------------------------------------------------------------------------
1060 IMPLEMENT_CLASS(wxPrintPreview
, wxPrintPreviewBase
)
1062 wxPrintPreview::wxPrintPreview(wxPrintout
*printout
,
1063 wxPrintout
*printoutForPrinting
,
1064 wxPrintDialogData
*data
) :
1065 wxPrintPreviewBase( printout
, printoutForPrinting
, data
)
1067 m_pimpl
= wxPrintFactory::GetFactory()->
1068 CreatePrintPreview( printout
, printoutForPrinting
, data
);
1071 wxPrintPreview::wxPrintPreview(wxPrintout
*printout
,
1072 wxPrintout
*printoutForPrinting
,
1073 wxPrintData
*data
) :
1074 wxPrintPreviewBase( printout
, printoutForPrinting
, data
)
1076 m_pimpl
= wxPrintFactory::GetFactory()->
1077 CreatePrintPreview( printout
, printoutForPrinting
, data
);
1080 wxPrintPreview::~wxPrintPreview()
1084 // don't delete twice
1085 m_printPrintout
= NULL
;
1086 m_previewPrintout
= NULL
;
1087 m_previewBitmap
= NULL
;
1090 bool wxPrintPreview::SetCurrentPage(int pageNum
)
1092 return m_pimpl
->SetCurrentPage( pageNum
);
1095 int wxPrintPreview::GetCurrentPage() const
1097 return m_pimpl
->GetCurrentPage();
1100 void wxPrintPreview::SetPrintout(wxPrintout
*printout
)
1102 m_pimpl
->SetPrintout( printout
);
1105 wxPrintout
*wxPrintPreview::GetPrintout() const
1107 return m_pimpl
->GetPrintout();
1110 wxPrintout
*wxPrintPreview::GetPrintoutForPrinting() const
1112 return m_pimpl
->GetPrintoutForPrinting();
1115 void wxPrintPreview::SetFrame(wxFrame
*frame
)
1117 m_pimpl
->SetFrame( frame
);
1120 void wxPrintPreview::SetCanvas(wxPreviewCanvas
*canvas
)
1122 m_pimpl
->SetCanvas( canvas
);
1125 wxFrame
*wxPrintPreview::GetFrame() const
1127 return m_pimpl
->GetFrame();
1130 wxPreviewCanvas
*wxPrintPreview::GetCanvas() const
1132 return m_pimpl
->GetCanvas();
1135 bool wxPrintPreview::PaintPage(wxPreviewCanvas
*canvas
, wxDC
& dc
)
1137 return m_pimpl
->PaintPage( canvas
, dc
);
1140 bool wxPrintPreview::DrawBlankPage(wxPreviewCanvas
*canvas
, wxDC
& dc
)
1142 return m_pimpl
->DrawBlankPage( canvas
, dc
);
1145 void wxPrintPreview::AdjustScrollbars(wxPreviewCanvas
*canvas
)
1147 m_pimpl
->AdjustScrollbars( canvas
);
1150 bool wxPrintPreview::RenderPage(int pageNum
)
1152 return m_pimpl
->RenderPage( pageNum
);
1155 void wxPrintPreview::SetZoom(int percent
)
1157 m_pimpl
->SetZoom( percent
);
1160 wxPrintDialogData
& wxPrintPreview::GetPrintDialogData()
1162 return m_pimpl
->GetPrintDialogData();
1165 int wxPrintPreview::GetMaxPage() const
1167 return m_pimpl
->GetMaxPage();
1170 int wxPrintPreview::GetMinPage() const
1172 return m_pimpl
->GetMinPage();
1175 bool wxPrintPreview::Ok() const
1177 return m_pimpl
->Ok();
1180 void wxPrintPreview::SetOk(bool ok
)
1182 m_pimpl
->SetOk( ok
);
1185 bool wxPrintPreview::Print(bool interactive
)
1187 return m_pimpl
->Print( interactive
);
1190 void wxPrintPreview::DetermineScaling()
1192 m_pimpl
->DetermineScaling();
1196 #endif // wxUSE_PRINTING_ARCHITECTURE