1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: Printing framework base class implementation
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart and Markus Holzem
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
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"
41 #include "wx/prntbase.h"
42 #include "wx/dcprint.h"
43 #include "wx/printdlg.h"
44 #include "wx/module.h"
50 #include "wx/msw/private.h"
58 IMPLEMENT_CLASS(wxPrinterBase
, wxObject
)
59 IMPLEMENT_ABSTRACT_CLASS(wxPrintout
, wxObject
)
60 IMPLEMENT_CLASS(wxPreviewCanvas
, wxWindow
)
61 IMPLEMENT_CLASS(wxPreviewControlBar
, wxWindow
)
62 IMPLEMENT_CLASS(wxPreviewFrame
, wxFrame
)
63 IMPLEMENT_CLASS(wxPrintPreviewBase
, wxObject
)
65 BEGIN_EVENT_TABLE(wxPrintAbortDialog
, wxDialog
)
66 EVT_BUTTON(wxID_CANCEL
, wxPrintAbortDialog::OnCancel
)
69 BEGIN_EVENT_TABLE(wxPreviewCanvas
, wxScrolledWindow
)
70 EVT_PAINT(wxPreviewCanvas::OnPaint
)
71 EVT_SYS_COLOUR_CHANGED(wxPreviewCanvas::OnSysColourChanged
)
78 wxPrinterBase::wxPrinterBase(wxPrintDialogData
*data
)
80 m_currentPrintout
= (wxPrintout
*) NULL
;
81 sm_abortWindow
= (wxWindow
*) NULL
;
84 m_printDialogData
= (*data
);
85 sm_lastError
= wxPRINTER_NO_ERROR
;
88 wxWindow
*wxPrinterBase::sm_abortWindow
= (wxWindow
*) NULL
;
89 bool wxPrinterBase::sm_abortIt
= FALSE
;
90 wxPrinterError
wxPrinterBase::sm_lastError
= wxPRINTER_NO_ERROR
;
92 wxPrinterBase::~wxPrinterBase()
96 void wxPrintAbortDialog::OnCancel(wxCommandEvent
& WXUNUSED(event
))
98 wxPrinterBase::sm_abortIt
= TRUE
;
99 wxPrinterBase::sm_abortWindow
->Show(FALSE
);
100 wxPrinterBase::sm_abortWindow
->Close(TRUE
);
101 wxPrinterBase::sm_abortWindow
= (wxWindow
*) NULL
;
104 wxWindow
*wxPrinterBase::CreateAbortWindow(wxWindow
*parent
, wxPrintout
*WXUNUSED(printout
))
106 wxPrintAbortDialog
*dialog
= new wxPrintAbortDialog(parent
, _("Printing"), wxPoint(0, 0), wxSize(400, 400), wxDEFAULT_DIALOG_STYLE
);
107 (void) new wxStaticText(dialog
, -1, _("Please wait..."), wxPoint(5, 5));
109 wxButton
*button
= new wxButton(dialog
, wxID_CANCEL
, _("Cancel"), wxPoint(5, 30));
112 button
->Centre(wxHORIZONTAL
);
118 void wxPrinterBase::ReportError(wxWindow
*parent
, wxPrintout
*WXUNUSED(printout
), char *message
)
120 wxMessageBox(message
, _("Printing Error"), wxOK
, parent
);
127 wxPrintout::wxPrintout(const wxString
& title
)
129 m_printoutTitle
= title
;
130 m_printoutDC
= (wxDC
*) NULL
;
133 m_pageWidthPixels
= 0;
134 m_pageHeightPixels
= 0;
142 wxPrintout::~wxPrintout()
146 bool wxPrintout::OnBeginDocument(int WXUNUSED(startPage
), int WXUNUSED(endPage
))
148 return GetDC()->StartDoc(_("Printing"));
151 void wxPrintout::OnEndDocument()
156 void wxPrintout::OnBeginPrinting()
160 void wxPrintout::OnEndPrinting()
164 bool wxPrintout::HasPage(int page
)
169 void wxPrintout::GetPageInfo(int *minPage
, int *maxPage
, int *fromPage
, int *toPage
)
181 wxPreviewCanvas::wxPreviewCanvas(wxPrintPreviewBase
*preview
, wxWindow
*parent
,
182 const wxPoint
& pos
, const wxSize
& size
, long style
, const wxString
& name
):
183 wxScrolledWindow(parent
, -1, pos
, size
, style
, name
)
185 m_printPreview
= preview
;
186 SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_APPWORKSPACE
));
188 SetScrollbars(15, 18, 100, 100);
191 wxPreviewCanvas::~wxPreviewCanvas()
195 void wxPreviewCanvas::OnPaint(wxPaintEvent
& WXUNUSED(event
))
202 if (!GetUpdateRegion().IsEmpty())
203 dc.SetClippingRegion( GetUpdateRegion() );
209 m_printPreview
->PaintPage(this, dc
);
213 // Responds to colour changes, and passes event on to children.
214 void wxPreviewCanvas::OnSysColourChanged(wxSysColourChangedEvent
& event
)
216 SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_APPWORKSPACE
));
219 // Propagate the event to the non-top-level children
220 wxWindow::OnSysColourChanged(event
);
224 * Preview control bar
227 BEGIN_EVENT_TABLE(wxPreviewControlBar
, wxPanel
)
228 EVT_BUTTON(wxID_PREVIEW_CLOSE
, wxPreviewControlBar::OnWindowClose
)
229 EVT_BUTTON(wxID_PREVIEW_PRINT
, wxPreviewControlBar::OnPrint
)
230 EVT_BUTTON(wxID_PREVIEW_PREVIOUS
, wxPreviewControlBar::OnPreviousButton
)
231 EVT_BUTTON(wxID_PREVIEW_NEXT
, wxPreviewControlBar::OnNextButton
)
232 EVT_CHAR(wxPreviewControlBar::OnChar
)
233 EVT_CHOICE(wxID_PREVIEW_ZOOM
, wxPreviewControlBar::OnZoom
)
234 EVT_PAINT(wxPreviewControlBar::OnPaint
)
237 wxPreviewControlBar::wxPreviewControlBar(wxPrintPreviewBase
*preview
, long buttons
,
238 wxWindow
*parent
, const wxPoint
& pos
, const wxSize
& size
,
239 long style
, const wxString
& name
):
240 wxPanel(parent
, -1, pos
, size
, style
, name
)
242 m_printPreview
= preview
;
243 m_closeButton
= (wxButton
*) NULL
;
244 m_nextPageButton
= (wxButton
*) NULL
;
245 m_previousPageButton
= (wxButton
*) NULL
;
246 m_printButton
= (wxButton
*) NULL
;
247 m_zoomControl
= (wxChoice
*) NULL
;
248 m_buttonFlags
= buttons
;
251 wxPreviewControlBar::~wxPreviewControlBar()
255 void wxPreviewControlBar::OnPaint(wxPaintEvent
& WXUNUSED(event
))
261 dc
.SetPen(*wxBLACK_PEN
);
262 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
263 dc
.DrawLine( 0, h
-1, w
, h
-1 );
266 void wxPreviewControlBar::OnWindowClose(wxCommandEvent
& WXUNUSED(event
))
268 wxPreviewFrame
*frame
= (wxPreviewFrame
*)GetParent();
272 void wxPreviewControlBar::OnPrint(wxCommandEvent
& WXUNUSED(event
))
274 wxPrintPreviewBase
*preview
= GetPrintPreview();
275 preview
->Print(TRUE
);
278 void wxPreviewControlBar::OnChar(wxKeyEvent
&event
)
280 switch(event
.KeyCode())
291 void wxPreviewControlBar::OnNext(void)
293 wxPrintPreviewBase
*preview
= GetPrintPreview();
296 int currentPage
= preview
->GetCurrentPage();
297 if ((preview
->GetMaxPage() > 0) &&
298 (currentPage
< preview
->GetMaxPage()) &&
299 preview
->GetPrintout()->HasPage(currentPage
+ 1))
301 preview
->SetCurrentPage(currentPage
+ 1);
306 void wxPreviewControlBar::OnPrevious(void)
308 wxPrintPreviewBase
*preview
= GetPrintPreview();
311 int currentPage
= preview
->GetCurrentPage();
312 if ((preview
->GetMinPage() > 0) &&
313 (currentPage
> preview
->GetMinPage()) &&
314 preview
->GetPrintout()->HasPage(currentPage
- 1))
316 preview
->SetCurrentPage(currentPage
- 1);
321 void wxPreviewControlBar::OnZoom(wxCommandEvent
& WXUNUSED(event
))
323 int zoom
= GetZoomControl();
324 if (GetPrintPreview())
325 GetPrintPreview()->SetZoom(zoom
);
328 void wxPreviewControlBar::CreateButtons()
330 SetSize(0, 0, 400, 40);
339 wxFont buttonFont(fontSize, wxSWISS, wxNORMAL, wxBOLD);
343 int buttonWidth
= 65;
345 int buttonHeight
= -1;
347 int buttonHeight
= 24;
359 m_closeButton
= new wxButton(this, wxID_PREVIEW_CLOSE
, _("Close"),
360 wxPoint(x
, y
), wxSize(buttonWidth
, buttonHeight
));
362 x
+= gap
+ buttonWidth
;
364 if (m_buttonFlags
& wxPREVIEW_PRINT
)
366 m_printButton
= new wxButton(this, wxID_PREVIEW_PRINT
, _("Print..."), wxPoint(x
, y
),
367 wxSize(buttonWidth
, buttonHeight
));
368 x
+= gap
+ buttonWidth
;
371 if (m_buttonFlags
& wxPREVIEW_PREVIOUS
)
373 m_previousPageButton
= new wxButton(this, wxID_PREVIEW_PREVIOUS
, wxT("<<"), wxPoint(x
, y
),
374 wxSize(buttonWidth
, buttonHeight
));
375 x
+= gap
+ buttonWidth
;
378 if (m_buttonFlags
& wxPREVIEW_NEXT
)
380 m_nextPageButton
= new wxButton(this, wxID_PREVIEW_NEXT
, wxT(">>"),
381 wxPoint(x
, y
), wxSize(buttonWidth
, buttonHeight
));
382 x
+= gap
+ buttonWidth
;
385 if (m_buttonFlags
& wxPREVIEW_ZOOM
)
387 static const char *choices
[] =
389 "10%", "15%", "20%", "25%", "30%", "35%", "40%", "45%", "50%", "55%",
390 "60%", "65%", "70%", "75%", "80%", "85%", "90%", "95%", "100%", "110%",
391 "120%", "150%", "200%"
394 int n
= WXSIZEOF(choices
);
396 wxString
* strings
= new wxString
[n
];
398 for (i
= 0; i
< n
; i
++ )
399 strings
[i
] = choices
[i
];
401 m_zoomControl
= new wxChoice(this, wxID_PREVIEW_ZOOM
,
409 SetZoomControl(m_printPreview
->GetZoom());
412 // m_closeButton->SetDefault();
415 void wxPreviewControlBar::SetZoomControl(int zoom
)
418 sprintf(buf
, "%d%%", zoom
);
419 // Someone is calling methods that do no exist in wxChoice!! So I'll just comment out for VA for now
421 m_zoomControl
->SetStringSelection(buf
);
424 int wxPreviewControlBar::GetZoomControl()
427 if (m_zoomControl
&& (m_zoomControl
->GetStringSelection() != wxT("")))
429 wxStrcpy(buf
, m_zoomControl
->GetStringSelection());
430 buf
[wxStrlen(buf
) - 1] = 0;
431 return (int)wxAtoi(buf
);
441 BEGIN_EVENT_TABLE(wxPreviewFrame
, wxFrame
)
442 EVT_CLOSE(wxPreviewFrame::OnCloseWindow
)
445 wxPreviewFrame::wxPreviewFrame(wxPrintPreviewBase
*preview
, wxFrame
*parent
, const wxString
& title
,
446 const wxPoint
& pos
, const wxSize
& size
, long style
, const wxString
& name
):
447 wxFrame(parent
, -1, title
, pos
, size
, style
, name
)
449 m_printPreview
= preview
;
451 m_previewCanvas
= NULL
;
454 wxPreviewFrame::~wxPreviewFrame()
458 void wxPreviewFrame::OnCloseWindow(wxCloseEvent
& WXUNUSED(event
))
462 // Need to delete the printout and the print preview
463 wxPrintout
*printout
= m_printPreview
->GetPrintout();
467 m_printPreview
->SetPrintout(NULL
);
468 m_printPreview
->SetCanvas(NULL
);
469 m_printPreview
->SetFrame(NULL
);
471 delete m_printPreview
;
476 void wxPreviewFrame::Initialize()
483 m_printPreview
->SetCanvas(m_previewCanvas
);
484 m_printPreview
->SetFrame(this);
486 // Set layout constraints here
488 // Control bar constraints
489 wxLayoutConstraints
*c1
= new wxLayoutConstraints
;
491 // m_controlBar->GetSize(&w, &h);
493 #if (defined(__WXMSW__) || defined(__WXGTK__))
499 c1
->left
.SameAs (this, wxLeft
);
500 c1
->top
.SameAs (this, wxTop
);
501 c1
->right
.SameAs (this, wxRight
);
502 c1
->height
.Absolute (h
);
504 m_controlBar
->SetConstraints(c1
);
506 // Canvas constraints
507 wxLayoutConstraints
*c2
= new wxLayoutConstraints
;
509 c2
->left
.SameAs (this, wxLeft
);
510 c2
->top
.Below (m_controlBar
);
511 c2
->right
.SameAs (this, wxRight
);
512 c2
->bottom
.SameAs (this, wxBottom
);
514 m_previewCanvas
->SetConstraints(c2
);
523 void wxPreviewFrame::CreateCanvas()
525 m_previewCanvas
= new wxPreviewCanvas(m_printPreview
, this);
528 void wxPreviewFrame::CreateControlBar()
530 long buttons
= wxPREVIEW_DEFAULT
;
531 if (m_printPreview
->GetPrintoutForPrinting())
532 buttons
|= wxPREVIEW_PRINT
;
534 m_controlBar
= new wxPreviewControlBar(m_printPreview
, buttons
, this, wxPoint(0, 0), wxSize(400, 40));
535 m_controlBar
->CreateButtons();
542 wxPrintPreviewBase::wxPrintPreviewBase(wxPrintout
*printout
,
543 wxPrintout
*printoutForPrinting
,
547 m_printDialogData
= (*data
);
549 Init(printout
, printoutForPrinting
);
552 wxPrintPreviewBase::wxPrintPreviewBase(wxPrintout
*printout
,
553 wxPrintout
*printoutForPrinting
,
554 wxPrintDialogData
*data
)
557 m_printDialogData
= (*data
);
559 Init(printout
, printoutForPrinting
);
562 void wxPrintPreviewBase::Init(wxPrintout
*printout
,
563 wxPrintout
*printoutForPrinting
)
566 m_previewPrintout
= printout
;
567 if (m_previewPrintout
)
568 m_previewPrintout
->SetIsPreview(TRUE
);
570 m_printPrintout
= printoutForPrinting
;
572 m_previewCanvas
= NULL
;
573 m_previewFrame
= NULL
;
574 m_previewBitmap
= NULL
;
581 m_printingPrepared
= FALSE
;
583 // Too soon! Moved to RenderPage.
584 // printout->OnPreparePrinting();
586 // Get some parameters from the printout, if defined
588 printout
->GetPageInfo(&m_minPage
, &m_maxPage
, &selFrom
, &selTo
);
591 wxPrintPreviewBase::~wxPrintPreviewBase()
593 if (m_previewPrintout
)
594 delete m_previewPrintout
;
596 delete m_previewBitmap
;
598 delete m_printPrintout
;
601 bool wxPrintPreviewBase::SetCurrentPage(int pageNum
)
603 if (m_currentPage
== pageNum
)
606 m_currentPage
= pageNum
;
609 delete m_previewBitmap
;
610 m_previewBitmap
= NULL
;
616 m_previewCanvas
->Refresh();
621 bool wxPrintPreviewBase::PaintPage(wxWindow
*canvas
, wxDC
& dc
)
623 DrawBlankPage(canvas
, dc
);
625 if (!m_previewBitmap
)
626 RenderPage(m_currentPage
);
628 if (!m_previewBitmap
)
634 int canvasWidth
, canvasHeight
;
635 canvas
->GetSize(&canvasWidth
, &canvasHeight
);
637 double zoomScale
= ((float)m_currentZoom
/(float)100);
638 double actualWidth
= (zoomScale
*m_pageWidth
*m_previewScale
);
639 // float actualHeight = (float)(zoomScale*m_pageHeight*m_previewScale);
641 int x
= (int) ((canvasWidth
- actualWidth
)/2.0);
642 if (x
< m_leftMargin
)
647 temp_dc
.SelectObject(*m_previewBitmap
);
649 dc
.Blit(x
, y
, m_previewBitmap
->GetWidth(), m_previewBitmap
->GetHeight(), &temp_dc
, 0, 0);
651 temp_dc
.SelectObject(wxNullBitmap
);
656 bool wxPrintPreviewBase::RenderPage(int pageNum
)
660 int canvasWidth
, canvasHeight
;
662 if (!m_previewCanvas
)
664 wxFAIL_MSG(_T("wxPrintPreviewBase::RenderPage: must use wxPrintPreviewBase::SetCanvas to let me know about the canvas!"));
668 m_previewCanvas
->GetSize(&canvasWidth
, &canvasHeight
);
670 double zoomScale
= (m_currentZoom
/100.0);
671 int actualWidth
= (int)(zoomScale
*m_pageWidth
*m_previewScale
);
672 int actualHeight
= (int)(zoomScale
*m_pageHeight
*m_previewScale
);
674 int x
= (int)((canvasWidth
- actualWidth
)/2.0);
675 if (x
< m_leftMargin
)
677 // int y = m_topMargin;
680 if (!m_previewBitmap
)
682 m_previewBitmap
= new wxBitmap((int)actualWidth
, (int)actualHeight
);
683 if (!m_previewBitmap
|| !m_previewBitmap
->Ok())
686 delete m_previewBitmap
;
687 wxMessageBox(_("Sorry, not enough memory to create a preview."), _("Print Preview Failure"), wxOK
);
693 memoryDC
.SelectObject(*m_previewBitmap
);
697 m_previewPrintout
->SetDC(&memoryDC
);
698 m_previewPrintout
->SetPageSizePixels(m_pageWidth
, m_pageHeight
);
700 // Need to delay OnPreparePrinting until here, so we have enough information.
701 if (!m_printingPrepared
)
703 m_previewPrintout
->OnPreparePrinting();
704 m_printingPrepared
= TRUE
;
707 m_previewPrintout
->OnBeginPrinting();
709 if (!m_previewPrintout
->OnBeginDocument(m_printDialogData
.GetFromPage(), m_printDialogData
.GetToPage()))
711 wxMessageBox(_("Could not start document preview."), _("Print Preview Failure"), wxOK
);
713 memoryDC
.SelectObject(wxNullBitmap
);
715 delete m_previewBitmap
;
719 m_previewPrintout
->OnPrintPage(pageNum
);
720 m_previewPrintout
->OnEndDocument();
721 m_previewPrintout
->OnEndPrinting();
723 m_previewPrintout
->SetDC(NULL
);
725 memoryDC
.SelectObject(wxNullBitmap
);
729 wxSprintf(buf
, _("Page %d of %d"), pageNum
, m_maxPage
);
731 wxSprintf(buf
, _("Page %d"), pageNum
);
734 m_previewFrame
->SetStatusText(buf
);
740 bool wxPrintPreviewBase::DrawBlankPage(wxWindow
*canvas
, wxDC
& dc
)
742 int canvasWidth
, canvasHeight
;
743 canvas
->GetSize(&canvasWidth
, &canvasHeight
);
745 float zoomScale
= (float)((float)m_currentZoom
/(float)100);
746 float actualWidth
= zoomScale
*m_pageWidth
*m_previewScale
;
747 float actualHeight
= zoomScale
*m_pageHeight
*m_previewScale
;
749 float x
= (float)((canvasWidth
- actualWidth
)/2.0);
750 if (x
< m_leftMargin
)
751 x
= (float)m_leftMargin
;
752 float y
= (float)m_topMargin
;
754 // Draw shadow, allowing for 1-pixel border AROUND the actual page
755 int shadowOffset
= 4;
756 dc
.SetPen(*wxBLACK_PEN
);
757 dc
.SetBrush(*wxBLACK_BRUSH
);
759 dc.DrawRectangle((int)(x-1 + shadowOffset), (int)(y-1 + shadowOffset), (int)(actualWidth+2), (int)(actualHeight+2));
761 dc
.DrawRectangle((int)(x
+ shadowOffset
), (int)(y
+ actualHeight
+1), (int)(actualWidth
), shadowOffset
);
762 dc
.DrawRectangle((int)(x
+ actualWidth
), (int)(y
+ shadowOffset
), shadowOffset
, (int)(actualHeight
));
764 // Draw blank page allowing for 1-pixel border AROUND the actual page
765 dc
.SetPen(*wxBLACK_PEN
);
766 dc
.SetBrush(*wxWHITE_BRUSH
);
769 wxRegion update_region = canvas->GetUpdateRegion();
770 wxRect r = update_region.GetBox();
772 printf( "x: %d y: %d w: %d h: %d.\n", (int)r.x, (int)r.y, (int)r.width, (int)r.height );
775 dc
.DrawRectangle((int)(x
-2), (int)(y
-1), (int)(actualWidth
+3), (int)(actualHeight
+2));
780 void wxPrintPreviewBase::SetZoom(int percent
)
782 if (m_currentZoom
== percent
)
785 m_currentZoom
= percent
;
788 delete m_previewBitmap
;
789 m_previewBitmap
= NULL
;
794 RenderPage(m_currentPage
);
795 ((wxScrolledWindow
*) m_previewCanvas
)->Scroll(0, 0);
796 m_previewCanvas
->Clear();
797 m_previewCanvas
->Refresh();
801 #endif // wxUSE_PRINTING_ARCHITECTURE