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"
29 #include "wx/msgdlg.h"
30 #include "wx/layout.h"
31 #include "wx/choice.h"
32 #include "wx/button.h"
33 #include "wx/settings.h"
34 #include "wx/dcmemory.h"
35 #include "wx/stattext.h"
39 #include "wx/prntbase.h"
40 #include "wx/dcprint.h"
41 #include "wx/printdlg.h"
50 // Clash with Windows header files
62 #if !USE_SHARED_LIBRARY
63 IMPLEMENT_CLASS(wxPrinterBase
, wxObject
)
64 IMPLEMENT_ABSTRACT_CLASS(wxPrintout
, wxObject
)
65 IMPLEMENT_CLASS(wxPreviewCanvas
, wxWindow
)
66 IMPLEMENT_CLASS(wxPreviewControlBar
, wxWindow
)
67 IMPLEMENT_CLASS(wxPreviewFrame
, wxFrame
)
68 IMPLEMENT_CLASS(wxPrintPreviewBase
, wxObject
)
70 BEGIN_EVENT_TABLE(wxPrintAbortDialog
, wxDialog
)
71 EVT_BUTTON(wxID_CANCEL
, wxPrintAbortDialog::OnCancel
)
74 BEGIN_EVENT_TABLE(wxPreviewCanvas
, wxScrolledWindow
)
75 EVT_PAINT(wxPreviewCanvas::OnPaint
)
76 EVT_SYS_COLOUR_CHANGED(wxPreviewCanvas::OnSysColourChanged
)
84 wxPrinterBase::wxPrinterBase(wxPrintData
*data
)
86 m_currentPrintout
= (wxPrintout
*) NULL
;
87 sm_abortWindow
= (wxWindow
*) NULL
;
90 m_printData
= (*data
);
93 wxWindow
*wxPrinterBase::sm_abortWindow
= (wxWindow
*) NULL
;
94 bool wxPrinterBase::sm_abortIt
= FALSE
;
96 wxPrinterBase::~wxPrinterBase()
100 void wxPrintAbortDialog::OnCancel(wxCommandEvent
& WXUNUSED(event
))
102 wxPrinterBase::sm_abortIt
= TRUE
;
103 wxPrinterBase::sm_abortWindow
->Show(FALSE
);
104 wxPrinterBase::sm_abortWindow
->Close(TRUE
);
105 wxPrinterBase::sm_abortWindow
= (wxWindow
*) NULL
;
108 wxWindow
*wxPrinterBase::CreateAbortWindow(wxWindow
*parent
, wxPrintout
*WXUNUSED(printout
))
110 wxPrintAbortDialog
*dialog
= new wxPrintAbortDialog(parent
, _("Printing"), wxPoint(0, 0), wxSize(400, 400), wxDEFAULT_DIALOG_STYLE
);
111 (void) new wxStaticText(dialog
, -1, _("Please wait..."), wxPoint(5, 5));
113 wxButton
*button
= new wxButton(dialog
, wxID_CANCEL
, _("Cancel"), wxPoint(5, 30));
116 button
->Centre(wxHORIZONTAL
);
122 void wxPrinterBase::ReportError(wxWindow
*parent
, wxPrintout
*WXUNUSED(printout
), char *message
)
124 wxMessageBox(message
, _("Printing Error"), wxOK
, parent
);
131 wxPrintout::wxPrintout(const wxString
& title
)
133 m_printoutTitle
= title
;
134 m_printoutDC
= (wxDC
*) NULL
;
137 m_pageWidthPixels
= 0;
138 m_pageHeightPixels
= 0;
146 wxPrintout::~wxPrintout()
150 bool wxPrintout::OnBeginDocument(int WXUNUSED(startPage
), int WXUNUSED(endPage
))
152 return GetDC()->StartDoc(_("Printing"));
155 void wxPrintout::OnEndDocument()
160 void wxPrintout::OnBeginPrinting()
164 void wxPrintout::OnEndPrinting()
168 bool wxPrintout::HasPage(int page
)
173 void wxPrintout::GetPageInfo(int *minPage
, int *maxPage
, int *fromPage
, int *toPage
)
185 wxPreviewCanvas::wxPreviewCanvas(wxPrintPreviewBase
*preview
, wxWindow
*parent
,
186 const wxPoint
& pos
, const wxSize
& size
, long style
, const wxString
& name
):
187 wxScrolledWindow(parent
, -1, pos
, size
, style
, name
)
189 m_printPreview
= preview
;
190 SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_APPWORKSPACE
));
192 SetScrollbars(15, 18, 100, 100);
195 wxPreviewCanvas::~wxPreviewCanvas()
199 void wxPreviewCanvas::OnPaint(wxPaintEvent
& WXUNUSED(event
))
206 m_printPreview
->PaintPage(this, dc
);
210 // Responds to colour changes, and passes event on to children.
211 void wxPreviewCanvas::OnSysColourChanged(wxSysColourChangedEvent
& event
)
213 SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_APPWORKSPACE
));
216 // Propagate the event to the non-top-level children
217 wxWindow::OnSysColourChanged(event
);
221 * Preview control bar
224 BEGIN_EVENT_TABLE(wxPreviewControlBar
, wxPanel
)
225 EVT_BUTTON(wxID_PREVIEW_CLOSE
, wxPreviewControlBar::OnWindowClose
)
226 EVT_BUTTON(wxID_PREVIEW_PRINT
, wxPreviewControlBar::OnPrint
)
227 EVT_BUTTON(wxID_PREVIEW_PREVIOUS
, wxPreviewControlBar::OnPrevious
)
228 EVT_BUTTON(wxID_PREVIEW_NEXT
, wxPreviewControlBar::OnNext
)
229 EVT_CHOICE(wxID_PREVIEW_ZOOM
, wxPreviewControlBar::OnZoom
)
230 EVT_PAINT(wxPreviewControlBar::OnPaint
)
233 wxPreviewControlBar::wxPreviewControlBar(wxPrintPreviewBase
*preview
, long buttons
,
234 wxWindow
*parent
, const wxPoint
& pos
, const wxSize
& size
,
235 long style
, const wxString
& name
):
236 wxPanel(parent
, -1, pos
, size
, style
, name
)
238 m_printPreview
= preview
;
239 m_closeButton
= (wxButton
*) NULL
;
240 m_nextPageButton
= (wxButton
*) NULL
;
241 m_previousPageButton
= (wxButton
*) NULL
;
242 m_printButton
= (wxButton
*) NULL
;
243 m_zoomControl
= (wxChoice
*) NULL
;
244 m_buttonFlags
= buttons
;
247 wxPreviewControlBar::~wxPreviewControlBar()
251 void wxPreviewControlBar::OnPaint(wxPaintEvent
& WXUNUSED(event
))
257 dc
.SetPen(*wxBLACK_PEN
);
258 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
259 dc
.DrawLine( 0, h
-1, w
, h
-1 );
262 void wxPreviewControlBar::OnWindowClose(wxCommandEvent
& WXUNUSED(event
))
264 wxPreviewFrame
*frame
= (wxPreviewFrame
*)GetParent();
268 void wxPreviewControlBar::OnPrint(wxCommandEvent
& WXUNUSED(event
))
270 wxPrintPreviewBase
*preview
= GetPrintPreview();
271 preview
->Print(TRUE
);
274 void wxPreviewControlBar::OnNext(wxCommandEvent
& WXUNUSED(event
))
276 wxPrintPreviewBase
*preview
= GetPrintPreview();
279 int currentPage
= preview
->GetCurrentPage();
280 if ((preview
->GetMaxPage() > 0) &&
281 (currentPage
< preview
->GetMaxPage()) &&
282 preview
->GetPrintout()->HasPage(currentPage
+ 1))
284 preview
->SetCurrentPage(currentPage
+ 1);
289 void wxPreviewControlBar::OnPrevious(wxCommandEvent
& WXUNUSED(event
))
291 wxPrintPreviewBase
*preview
= GetPrintPreview();
294 int currentPage
= preview
->GetCurrentPage();
295 if ((preview
->GetMinPage() > 0) &&
296 (currentPage
> preview
->GetMinPage()) &&
297 preview
->GetPrintout()->HasPage(currentPage
- 1))
299 preview
->SetCurrentPage(currentPage
- 1);
304 void wxPreviewControlBar::OnZoom(wxCommandEvent
& WXUNUSED(event
))
306 int zoom
= GetZoomControl();
307 if (GetPrintPreview())
308 GetPrintPreview()->SetZoom(zoom
);
311 void wxPreviewControlBar::CreateButtons()
313 SetSize(0, 0, 400, 40);
322 wxFont buttonFont(fontSize, wxSWISS, wxNORMAL, wxBOLD);
326 int buttonWidth
= 65;
328 int buttonHeight
= -1;
330 int buttonHeight
= 24;
342 m_closeButton
= new wxButton(this, wxID_PREVIEW_CLOSE
, _("Close"),
343 wxPoint(x
, y
), wxSize(buttonWidth
, buttonHeight
));
345 x
+= gap
+ buttonWidth
;
347 if (m_buttonFlags
& wxPREVIEW_PRINT
)
349 m_printButton
= new wxButton(this, wxID_PREVIEW_PRINT
, _("Print..."), wxPoint(x
, y
),
350 wxSize(buttonWidth
, buttonHeight
));
351 x
+= gap
+ buttonWidth
;
354 if (m_buttonFlags
& wxPREVIEW_PREVIOUS
)
356 m_previousPageButton
= new wxButton(this, wxID_PREVIEW_PREVIOUS
, "<<", wxPoint(x
, y
),
357 wxSize(buttonWidth
, buttonHeight
));
358 x
+= gap
+ buttonWidth
;
361 if (m_buttonFlags
& wxPREVIEW_NEXT
)
363 m_nextPageButton
= new wxButton(this, wxID_PREVIEW_NEXT
, ">>",
364 wxPoint(x
, y
), wxSize(buttonWidth
, buttonHeight
));
365 x
+= gap
+ buttonWidth
;
368 // Yes, this look stupid, but this is because gcc gives up otherwise.
369 wxString
*choices
= new wxString
[23];
388 choices
[18] = "100%";
389 choices
[19] = "110%";
390 choices
[20] = "120%";
391 choices
[21] = "150%";
392 choices
[22] = "200%";
395 if (m_buttonFlags
& wxPREVIEW_ZOOM
)
397 m_zoomControl
= new wxChoice(this, wxID_PREVIEW_ZOOM
, wxPoint(x
, y
),
398 wxSize(100, -1), n
, (wxString
*)choices
);
399 SetZoomControl(m_printPreview
->GetZoom());
404 // m_closeButton->SetDefault();
407 void wxPreviewControlBar::SetZoomControl(int zoom
)
410 sprintf(buf
, "%d%%", zoom
);
412 m_zoomControl
->SetStringSelection(buf
);
415 int wxPreviewControlBar::GetZoomControl()
418 if (m_zoomControl
&& (m_zoomControl
->GetStringSelection() != ""))
420 strcpy(buf
, m_zoomControl
->GetStringSelection());
421 buf
[strlen(buf
) - 1] = 0;
422 return (int)atoi(buf
);
432 BEGIN_EVENT_TABLE(wxPreviewFrame
, wxFrame
)
433 EVT_CLOSE(wxPreviewFrame::OnCloseWindow
)
436 wxPreviewFrame::wxPreviewFrame(wxPrintPreviewBase
*preview
, wxFrame
*parent
, const wxString
& title
,
437 const wxPoint
& pos
, const wxSize
& size
, long style
, const wxString
& name
):
438 wxFrame(parent
, -1, title
, pos
, size
, style
, name
)
440 m_printPreview
= preview
;
442 m_previewCanvas
= NULL
;
445 wxPreviewFrame::~wxPreviewFrame()
449 void wxPreviewFrame::OnCloseWindow(wxCloseEvent
& event
)
453 // Need to delete the printout and the print preview
454 wxPrintout
*printout
= m_printPreview
->GetPrintout();
458 m_printPreview
->SetPrintout(NULL
);
459 m_printPreview
->SetCanvas(NULL
);
460 m_printPreview
->SetFrame(NULL
);
462 delete m_printPreview
;
467 void wxPreviewFrame::Initialize()
474 m_printPreview
->SetCanvas(m_previewCanvas
);
475 m_printPreview
->SetFrame(this);
477 // Set layout constraints here
479 // Control bar constraints
480 wxLayoutConstraints
*c1
= new wxLayoutConstraints
;
482 // m_controlBar->GetSize(&w, &h);
484 #if (defined(__WXMSW__) || defined(__WXGTK__))
490 c1
->left
.SameAs (this, wxLeft
);
491 c1
->top
.SameAs (this, wxTop
);
492 c1
->right
.SameAs (this, wxRight
);
493 c1
->height
.Absolute (h
);
495 m_controlBar
->SetConstraints(c1
);
497 // Canvas constraints
498 wxLayoutConstraints
*c2
= new wxLayoutConstraints
;
500 c2
->left
.SameAs (this, wxLeft
);
501 c2
->top
.Below (m_controlBar
);
502 c2
->right
.SameAs (this, wxRight
);
503 c2
->bottom
.SameAs (this, wxBottom
);
505 m_previewCanvas
->SetConstraints(c2
);
514 void wxPreviewFrame::CreateCanvas()
516 m_previewCanvas
= new wxPreviewCanvas(m_printPreview
, this);
519 void wxPreviewFrame::CreateControlBar()
521 long buttons
= wxPREVIEW_DEFAULT
;
522 if (m_printPreview
->GetPrintoutForPrinting())
523 buttons
|= wxPREVIEW_PRINT
;
525 m_controlBar
= new wxPreviewControlBar(m_printPreview
, buttons
, this, wxPoint(0, 0), wxSize(400, 40));
526 m_controlBar
->CreateButtons();
533 wxPrintPreviewBase::wxPrintPreviewBase(wxPrintout
*printout
, wxPrintout
*printoutForPrinting
, wxPrintData
*data
)
536 m_previewPrintout
= printout
;
537 if (m_previewPrintout
)
538 m_previewPrintout
->SetIsPreview(TRUE
);
540 m_printPrintout
= printoutForPrinting
;
542 m_printData
= (*data
);
544 m_previewCanvas
= NULL
;
545 m_previewFrame
= NULL
;
546 m_previewBitmap
= NULL
;
554 printout
->OnPreparePrinting();
556 // Get some parameters from the printout, if defined
558 printout
->GetPageInfo(&m_minPage
, &m_maxPage
, &selFrom
, &selTo
);
561 wxPrintPreviewBase::~wxPrintPreviewBase()
563 if (m_previewPrintout
)
564 delete m_previewPrintout
;
566 delete m_previewBitmap
;
568 delete m_printPrintout
;
571 bool wxPrintPreviewBase::SetCurrentPage(int pageNum
)
573 if (m_currentPage
== pageNum
)
576 m_currentPage
= pageNum
;
579 delete m_previewBitmap
;
580 m_previewBitmap
= NULL
;
586 m_previewCanvas
->Refresh();
591 bool wxPrintPreviewBase::PaintPage(wxWindow
*canvas
, wxDC
& dc
)
593 DrawBlankPage(canvas
, dc
);
595 if (!m_previewBitmap
)
596 RenderPage(m_currentPage
);
598 if (!m_previewBitmap
)
604 int canvasWidth
, canvasHeight
;
605 canvas
->GetSize(&canvasWidth
, &canvasHeight
);
607 double zoomScale
= ((float)m_currentZoom
/(float)100);
608 double actualWidth
= (zoomScale
*m_pageWidth
*m_previewScale
);
609 // float actualHeight = (float)(zoomScale*m_pageHeight*m_previewScale);
611 int x
= (int) ((canvasWidth
- actualWidth
)/2.0);
612 if (x
< m_leftMargin
)
617 temp_dc
.SelectObject(*m_previewBitmap
);
619 dc
.Blit(x
, y
, m_previewBitmap
->GetWidth(), m_previewBitmap
->GetHeight(), &temp_dc
, 0, 0);
621 temp_dc
.SelectObject(wxNullBitmap
);
626 bool wxPrintPreviewBase::RenderPage(int pageNum
)
628 int canvasWidth
, canvasHeight
;
630 if (!m_previewCanvas
)
632 wxMessageBox(_("wxPrintPreviewBase::RenderPage: must use wxPrintPreviewBase::SetCanvas to let me know about the canvas!"),
633 _("Print Preview Failure"), wxOK
);
636 m_previewCanvas
->GetSize(&canvasWidth
, &canvasHeight
);
638 double zoomScale
= (m_currentZoom
/100.0);
639 int actualWidth
= (int)(zoomScale
*m_pageWidth
*m_previewScale
);
640 int actualHeight
= (int)(zoomScale
*m_pageHeight
*m_previewScale
);
642 int x
= (int)((canvasWidth
- actualWidth
)/2.0);
643 if (x
< m_leftMargin
)
645 // int y = m_topMargin;
648 if (!m_previewBitmap
)
650 m_previewBitmap
= new wxBitmap((int)actualWidth
, (int)actualHeight
);
651 if (!m_previewBitmap
|| !m_previewBitmap
->Ok())
654 delete m_previewBitmap
;
655 wxMessageBox(_("Sorry, not enough memory to create a preview."), _("Print Preview Failure"), wxOK
);
661 memoryDC
.SelectObject(*m_previewBitmap
);
665 m_previewPrintout
->SetDC(&memoryDC
);
666 m_previewPrintout
->SetPageSizePixels(m_pageWidth
, m_pageHeight
);
668 m_previewPrintout
->OnBeginPrinting();
671 if (!m_previewPrintout
->OnBeginDocument(m_printData
.GetFromPage(), m_printData
.GetToPage()))
673 wxMessageBox(_("Could not start document preview."), _("Print Preview Failure"), wxOK
);
675 memoryDC
.SelectObject(wxNullBitmap
);
677 delete m_previewBitmap
;
681 m_previewPrintout
->OnPrintPage(pageNum
);
682 m_previewPrintout
->OnEndDocument();
683 m_previewPrintout
->OnEndPrinting();
685 m_previewPrintout
->SetDC(NULL
);
687 memoryDC
.SelectObject(wxNullBitmap
);
691 sprintf(buf
, _("Page %d of %d"), pageNum
, m_maxPage
);
693 sprintf(buf
, _("Page %d"), pageNum
);
696 m_previewFrame
->SetStatusText(buf
);
702 bool wxPrintPreviewBase::DrawBlankPage(wxWindow
*canvas
, wxDC
& dc
)
704 int canvasWidth
, canvasHeight
;
705 canvas
->GetSize(&canvasWidth
, &canvasHeight
);
707 float zoomScale
= (float)((float)m_currentZoom
/(float)100);
708 float actualWidth
= zoomScale
*m_pageWidth
*m_previewScale
;
709 float actualHeight
= zoomScale
*m_pageHeight
*m_previewScale
;
711 float x
= (float)((canvasWidth
- actualWidth
)/2.0);
712 if (x
< m_leftMargin
)
713 x
= (float)m_leftMargin
;
714 float y
= (float)m_topMargin
;
716 // Draw shadow, allowing for 1-pixel border AROUND the actual page
717 int shadowOffset
= 4;
718 dc
.SetPen(*wxBLACK_PEN
);
719 dc
.SetBrush(*wxBLACK_BRUSH
);
721 dc.DrawRectangle((int)(x-1 + shadowOffset), (int)(y-1 + shadowOffset), (int)(actualWidth+2), (int)(actualHeight+2));
723 dc
.DrawRectangle((int)(x
+ shadowOffset
), (int)(y
+ actualHeight
+1), (int)(actualWidth
), shadowOffset
);
724 dc
.DrawRectangle((int)(x
+ actualWidth
), (int)(y
+ shadowOffset
), shadowOffset
, (int)(actualHeight
));
726 // Draw blank page allowing for 1-pixel border AROUND the actual page
727 dc
.SetPen(*wxBLACK_PEN
);
728 dc
.SetBrush(*wxWHITE_BRUSH
);
731 wxRegion update_region = canvas->GetUpdateRegion();
732 wxRect r = update_region.GetBox();
734 printf( "x: %d y: %d w: %d h: %d.\n", (int)r.x, (int)r.y, (int)r.width, (int)r.height );
737 dc
.DrawRectangle((int)(x
-2), (int)(y
-1), (int)(actualWidth
+3), (int)(actualHeight
+2));
742 void wxPrintPreviewBase::SetZoom(int percent
)
744 if (m_currentZoom
== percent
)
747 m_currentZoom
= percent
;
750 delete m_previewBitmap
;
751 m_previewBitmap
= NULL
;
753 RenderPage(m_currentPage
);
757 m_previewCanvas
->Clear();
758 m_previewCanvas
->Refresh();