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"
42 #include "wx/module.h"
51 // Clash with Windows header files
63 #if !USE_SHARED_LIBRARY
64 IMPLEMENT_CLASS(wxPrinterBase
, wxObject
)
65 IMPLEMENT_ABSTRACT_CLASS(wxPrintout
, wxObject
)
66 IMPLEMENT_CLASS(wxPreviewCanvas
, wxWindow
)
67 IMPLEMENT_CLASS(wxPreviewControlBar
, wxWindow
)
68 IMPLEMENT_CLASS(wxPreviewFrame
, wxFrame
)
69 IMPLEMENT_CLASS(wxPrintPreviewBase
, wxObject
)
71 BEGIN_EVENT_TABLE(wxPrintAbortDialog
, wxDialog
)
72 EVT_BUTTON(wxID_CANCEL
, wxPrintAbortDialog::OnCancel
)
75 BEGIN_EVENT_TABLE(wxPreviewCanvas
, wxScrolledWindow
)
76 EVT_PAINT(wxPreviewCanvas::OnPaint
)
77 EVT_SYS_COLOUR_CHANGED(wxPreviewCanvas::OnSysColourChanged
)
85 wxPrinterBase::wxPrinterBase(wxPrintDialogData
*data
)
87 m_currentPrintout
= (wxPrintout
*) NULL
;
88 sm_abortWindow
= (wxWindow
*) NULL
;
91 m_printDialogData
= (*data
);
94 wxWindow
*wxPrinterBase::sm_abortWindow
= (wxWindow
*) NULL
;
95 bool wxPrinterBase::sm_abortIt
= FALSE
;
97 wxPrinterBase::~wxPrinterBase()
101 void wxPrintAbortDialog::OnCancel(wxCommandEvent
& WXUNUSED(event
))
103 wxPrinterBase::sm_abortIt
= TRUE
;
104 wxPrinterBase::sm_abortWindow
->Show(FALSE
);
105 wxPrinterBase::sm_abortWindow
->Close(TRUE
);
106 wxPrinterBase::sm_abortWindow
= (wxWindow
*) NULL
;
109 wxWindow
*wxPrinterBase::CreateAbortWindow(wxWindow
*parent
, wxPrintout
*WXUNUSED(printout
))
111 wxPrintAbortDialog
*dialog
= new wxPrintAbortDialog(parent
, _("Printing"), wxPoint(0, 0), wxSize(400, 400), wxDEFAULT_DIALOG_STYLE
);
112 (void) new wxStaticText(dialog
, -1, _("Please wait..."), wxPoint(5, 5));
114 wxButton
*button
= new wxButton(dialog
, wxID_CANCEL
, _("Cancel"), wxPoint(5, 30));
117 button
->Centre(wxHORIZONTAL
);
123 void wxPrinterBase::ReportError(wxWindow
*parent
, wxPrintout
*WXUNUSED(printout
), char *message
)
125 wxMessageBox(message
, _("Printing Error"), wxOK
, parent
);
132 wxPrintout::wxPrintout(const wxString
& title
)
134 m_printoutTitle
= title
;
135 m_printoutDC
= (wxDC
*) NULL
;
138 m_pageWidthPixels
= 0;
139 m_pageHeightPixels
= 0;
147 wxPrintout::~wxPrintout()
151 bool wxPrintout::OnBeginDocument(int WXUNUSED(startPage
), int WXUNUSED(endPage
))
153 return GetDC()->StartDoc(_("Printing"));
156 void wxPrintout::OnEndDocument()
161 void wxPrintout::OnBeginPrinting()
165 void wxPrintout::OnEndPrinting()
169 bool wxPrintout::HasPage(int page
)
174 void wxPrintout::GetPageInfo(int *minPage
, int *maxPage
, int *fromPage
, int *toPage
)
186 wxPreviewCanvas::wxPreviewCanvas(wxPrintPreviewBase
*preview
, wxWindow
*parent
,
187 const wxPoint
& pos
, const wxSize
& size
, long style
, const wxString
& name
):
188 wxScrolledWindow(parent
, -1, pos
, size
, style
, name
)
190 m_printPreview
= preview
;
191 SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_APPWORKSPACE
));
193 SetScrollbars(15, 18, 100, 100);
196 wxPreviewCanvas::~wxPreviewCanvas()
200 void wxPreviewCanvas::OnPaint(wxPaintEvent
& WXUNUSED(event
))
207 m_printPreview
->PaintPage(this, dc
);
211 // Responds to colour changes, and passes event on to children.
212 void wxPreviewCanvas::OnSysColourChanged(wxSysColourChangedEvent
& event
)
214 SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_APPWORKSPACE
));
217 // Propagate the event to the non-top-level children
218 wxWindow::OnSysColourChanged(event
);
222 * Preview control bar
225 BEGIN_EVENT_TABLE(wxPreviewControlBar
, wxPanel
)
226 EVT_BUTTON(wxID_PREVIEW_CLOSE
, wxPreviewControlBar::OnWindowClose
)
227 EVT_BUTTON(wxID_PREVIEW_PRINT
, wxPreviewControlBar::OnPrint
)
228 EVT_BUTTON(wxID_PREVIEW_PREVIOUS
, wxPreviewControlBar::OnPrevious
)
229 EVT_BUTTON(wxID_PREVIEW_NEXT
, wxPreviewControlBar::OnNext
)
230 EVT_CHOICE(wxID_PREVIEW_ZOOM
, wxPreviewControlBar::OnZoom
)
231 EVT_PAINT(wxPreviewControlBar::OnPaint
)
234 wxPreviewControlBar::wxPreviewControlBar(wxPrintPreviewBase
*preview
, long buttons
,
235 wxWindow
*parent
, const wxPoint
& pos
, const wxSize
& size
,
236 long style
, const wxString
& name
):
237 wxPanel(parent
, -1, pos
, size
, style
, name
)
239 m_printPreview
= preview
;
240 m_closeButton
= (wxButton
*) NULL
;
241 m_nextPageButton
= (wxButton
*) NULL
;
242 m_previousPageButton
= (wxButton
*) NULL
;
243 m_printButton
= (wxButton
*) NULL
;
244 m_zoomControl
= (wxChoice
*) NULL
;
245 m_buttonFlags
= buttons
;
248 wxPreviewControlBar::~wxPreviewControlBar()
252 void wxPreviewControlBar::OnPaint(wxPaintEvent
& WXUNUSED(event
))
258 dc
.SetPen(*wxBLACK_PEN
);
259 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
260 dc
.DrawLine( 0, h
-1, w
, h
-1 );
263 void wxPreviewControlBar::OnWindowClose(wxCommandEvent
& WXUNUSED(event
))
265 wxPreviewFrame
*frame
= (wxPreviewFrame
*)GetParent();
269 void wxPreviewControlBar::OnPrint(wxCommandEvent
& WXUNUSED(event
))
271 wxPrintPreviewBase
*preview
= GetPrintPreview();
272 preview
->Print(TRUE
);
275 void wxPreviewControlBar::OnNext(wxCommandEvent
& WXUNUSED(event
))
277 wxPrintPreviewBase
*preview
= GetPrintPreview();
280 int currentPage
= preview
->GetCurrentPage();
281 if ((preview
->GetMaxPage() > 0) &&
282 (currentPage
< preview
->GetMaxPage()) &&
283 preview
->GetPrintout()->HasPage(currentPage
+ 1))
285 preview
->SetCurrentPage(currentPage
+ 1);
290 void wxPreviewControlBar::OnPrevious(wxCommandEvent
& WXUNUSED(event
))
292 wxPrintPreviewBase
*preview
= GetPrintPreview();
295 int currentPage
= preview
->GetCurrentPage();
296 if ((preview
->GetMinPage() > 0) &&
297 (currentPage
> preview
->GetMinPage()) &&
298 preview
->GetPrintout()->HasPage(currentPage
- 1))
300 preview
->SetCurrentPage(currentPage
- 1);
305 void wxPreviewControlBar::OnZoom(wxCommandEvent
& WXUNUSED(event
))
307 int zoom
= GetZoomControl();
308 if (GetPrintPreview())
309 GetPrintPreview()->SetZoom(zoom
);
312 void wxPreviewControlBar::CreateButtons()
314 SetSize(0, 0, 400, 40);
323 wxFont buttonFont(fontSize, wxSWISS, wxNORMAL, wxBOLD);
327 int buttonWidth
= 65;
329 int buttonHeight
= -1;
331 int buttonHeight
= 24;
343 m_closeButton
= new wxButton(this, wxID_PREVIEW_CLOSE
, _("Close"),
344 wxPoint(x
, y
), wxSize(buttonWidth
, buttonHeight
));
346 x
+= gap
+ buttonWidth
;
348 if (m_buttonFlags
& wxPREVIEW_PRINT
)
350 m_printButton
= new wxButton(this, wxID_PREVIEW_PRINT
, _("Print..."), wxPoint(x
, y
),
351 wxSize(buttonWidth
, buttonHeight
));
352 x
+= gap
+ buttonWidth
;
355 if (m_buttonFlags
& wxPREVIEW_PREVIOUS
)
357 m_previousPageButton
= new wxButton(this, wxID_PREVIEW_PREVIOUS
, "<<", wxPoint(x
, y
),
358 wxSize(buttonWidth
, buttonHeight
));
359 x
+= gap
+ buttonWidth
;
362 if (m_buttonFlags
& wxPREVIEW_NEXT
)
364 m_nextPageButton
= new wxButton(this, wxID_PREVIEW_NEXT
, ">>",
365 wxPoint(x
, y
), wxSize(buttonWidth
, buttonHeight
));
366 x
+= gap
+ buttonWidth
;
369 // Yes, this look stupid, but this is because gcc gives up otherwise.
370 wxString
*choices
= new wxString
[23];
389 choices
[18] = "100%";
390 choices
[19] = "110%";
391 choices
[20] = "120%";
392 choices
[21] = "150%";
393 choices
[22] = "200%";
396 if (m_buttonFlags
& wxPREVIEW_ZOOM
)
398 m_zoomControl
= new wxChoice(this, wxID_PREVIEW_ZOOM
, wxPoint(x
, y
),
399 wxSize(100, -1), n
, (wxString
*)choices
);
400 SetZoomControl(m_printPreview
->GetZoom());
405 // m_closeButton->SetDefault();
408 void wxPreviewControlBar::SetZoomControl(int zoom
)
411 sprintf(buf
, "%d%%", zoom
);
413 m_zoomControl
->SetStringSelection(buf
);
416 int wxPreviewControlBar::GetZoomControl()
419 if (m_zoomControl
&& (m_zoomControl
->GetStringSelection() != ""))
421 strcpy(buf
, m_zoomControl
->GetStringSelection());
422 buf
[strlen(buf
) - 1] = 0;
423 return (int)atoi(buf
);
433 BEGIN_EVENT_TABLE(wxPreviewFrame
, wxFrame
)
434 EVT_CLOSE(wxPreviewFrame::OnCloseWindow
)
437 wxPreviewFrame::wxPreviewFrame(wxPrintPreviewBase
*preview
, wxFrame
*parent
, const wxString
& title
,
438 const wxPoint
& pos
, const wxSize
& size
, long style
, const wxString
& name
):
439 wxFrame(parent
, -1, title
, pos
, size
, style
, name
)
441 m_printPreview
= preview
;
443 m_previewCanvas
= NULL
;
446 wxPreviewFrame::~wxPreviewFrame()
450 void wxPreviewFrame::OnCloseWindow(wxCloseEvent
& event
)
454 // Need to delete the printout and the print preview
455 wxPrintout
*printout
= m_printPreview
->GetPrintout();
459 m_printPreview
->SetPrintout(NULL
);
460 m_printPreview
->SetCanvas(NULL
);
461 m_printPreview
->SetFrame(NULL
);
463 delete m_printPreview
;
468 void wxPreviewFrame::Initialize()
475 m_printPreview
->SetCanvas(m_previewCanvas
);
476 m_printPreview
->SetFrame(this);
478 // Set layout constraints here
480 // Control bar constraints
481 wxLayoutConstraints
*c1
= new wxLayoutConstraints
;
483 // m_controlBar->GetSize(&w, &h);
485 #if (defined(__WXMSW__) || defined(__WXGTK__))
491 c1
->left
.SameAs (this, wxLeft
);
492 c1
->top
.SameAs (this, wxTop
);
493 c1
->right
.SameAs (this, wxRight
);
494 c1
->height
.Absolute (h
);
496 m_controlBar
->SetConstraints(c1
);
498 // Canvas constraints
499 wxLayoutConstraints
*c2
= new wxLayoutConstraints
;
501 c2
->left
.SameAs (this, wxLeft
);
502 c2
->top
.Below (m_controlBar
);
503 c2
->right
.SameAs (this, wxRight
);
504 c2
->bottom
.SameAs (this, wxBottom
);
506 m_previewCanvas
->SetConstraints(c2
);
515 void wxPreviewFrame::CreateCanvas()
517 m_previewCanvas
= new wxPreviewCanvas(m_printPreview
, this);
520 void wxPreviewFrame::CreateControlBar()
522 long buttons
= wxPREVIEW_DEFAULT
;
523 if (m_printPreview
->GetPrintoutForPrinting())
524 buttons
|= wxPREVIEW_PRINT
;
526 m_controlBar
= new wxPreviewControlBar(m_printPreview
, buttons
, this, wxPoint(0, 0), wxSize(400, 40));
527 m_controlBar
->CreateButtons();
534 wxPrintPreviewBase::wxPrintPreviewBase(wxPrintout
*printout
,
535 wxPrintout
*printoutForPrinting
,
539 m_printDialogData
= (*data
);
541 Init(printout
, printoutForPrinting
);
544 wxPrintPreviewBase::wxPrintPreviewBase(wxPrintout
*printout
,
545 wxPrintout
*printoutForPrinting
,
546 wxPrintDialogData
*data
)
549 m_printDialogData
= (*data
);
551 Init(printout
, printoutForPrinting
);
554 void wxPrintPreviewBase::Init(wxPrintout
*printout
,
555 wxPrintout
*printoutForPrinting
)
558 m_previewPrintout
= printout
;
559 if (m_previewPrintout
)
560 m_previewPrintout
->SetIsPreview(TRUE
);
562 m_printPrintout
= printoutForPrinting
;
564 m_previewCanvas
= NULL
;
565 m_previewFrame
= NULL
;
566 m_previewBitmap
= NULL
;
574 printout
->OnPreparePrinting();
576 // Get some parameters from the printout, if defined
578 printout
->GetPageInfo(&m_minPage
, &m_maxPage
, &selFrom
, &selTo
);
581 wxPrintPreviewBase::~wxPrintPreviewBase()
583 if (m_previewPrintout
)
584 delete m_previewPrintout
;
586 delete m_previewBitmap
;
588 delete m_printPrintout
;
591 bool wxPrintPreviewBase::SetCurrentPage(int pageNum
)
593 if (m_currentPage
== pageNum
)
596 m_currentPage
= pageNum
;
599 delete m_previewBitmap
;
600 m_previewBitmap
= NULL
;
606 m_previewCanvas
->Refresh();
611 bool wxPrintPreviewBase::PaintPage(wxWindow
*canvas
, wxDC
& dc
)
613 DrawBlankPage(canvas
, dc
);
615 if (!m_previewBitmap
)
616 RenderPage(m_currentPage
);
618 if (!m_previewBitmap
)
624 int canvasWidth
, canvasHeight
;
625 canvas
->GetSize(&canvasWidth
, &canvasHeight
);
627 double zoomScale
= ((float)m_currentZoom
/(float)100);
628 double actualWidth
= (zoomScale
*m_pageWidth
*m_previewScale
);
629 // float actualHeight = (float)(zoomScale*m_pageHeight*m_previewScale);
631 int x
= (int) ((canvasWidth
- actualWidth
)/2.0);
632 if (x
< m_leftMargin
)
637 temp_dc
.SelectObject(*m_previewBitmap
);
639 dc
.Blit(x
, y
, m_previewBitmap
->GetWidth(), m_previewBitmap
->GetHeight(), &temp_dc
, 0, 0);
641 temp_dc
.SelectObject(wxNullBitmap
);
646 bool wxPrintPreviewBase::RenderPage(int pageNum
)
648 int canvasWidth
, canvasHeight
;
650 if (!m_previewCanvas
)
652 wxMessageBox(_("wxPrintPreviewBase::RenderPage: must use wxPrintPreviewBase::SetCanvas to let me know about the canvas!"),
653 _("Print Preview Failure"), wxOK
);
656 m_previewCanvas
->GetSize(&canvasWidth
, &canvasHeight
);
658 double zoomScale
= (m_currentZoom
/100.0);
659 int actualWidth
= (int)(zoomScale
*m_pageWidth
*m_previewScale
);
660 int actualHeight
= (int)(zoomScale
*m_pageHeight
*m_previewScale
);
662 int x
= (int)((canvasWidth
- actualWidth
)/2.0);
663 if (x
< m_leftMargin
)
665 // int y = m_topMargin;
668 if (!m_previewBitmap
)
670 m_previewBitmap
= new wxBitmap((int)actualWidth
, (int)actualHeight
);
671 if (!m_previewBitmap
|| !m_previewBitmap
->Ok())
674 delete m_previewBitmap
;
675 wxMessageBox(_("Sorry, not enough memory to create a preview."), _("Print Preview Failure"), wxOK
);
681 memoryDC
.SelectObject(*m_previewBitmap
);
685 m_previewPrintout
->SetDC(&memoryDC
);
686 m_previewPrintout
->SetPageSizePixels(m_pageWidth
, m_pageHeight
);
688 m_previewPrintout
->OnBeginPrinting();
690 if (!m_previewPrintout
->OnBeginDocument(m_printDialogData
.GetFromPage(), m_printDialogData
.GetToPage()))
692 wxMessageBox(_("Could not start document preview."), _("Print Preview Failure"), wxOK
);
694 memoryDC
.SelectObject(wxNullBitmap
);
696 delete m_previewBitmap
;
700 m_previewPrintout
->OnPrintPage(pageNum
);
701 m_previewPrintout
->OnEndDocument();
702 m_previewPrintout
->OnEndPrinting();
704 m_previewPrintout
->SetDC(NULL
);
706 memoryDC
.SelectObject(wxNullBitmap
);
710 sprintf(buf
, _("Page %d of %d"), pageNum
, m_maxPage
);
712 sprintf(buf
, _("Page %d"), pageNum
);
715 m_previewFrame
->SetStatusText(buf
);
721 bool wxPrintPreviewBase::DrawBlankPage(wxWindow
*canvas
, wxDC
& dc
)
723 int canvasWidth
, canvasHeight
;
724 canvas
->GetSize(&canvasWidth
, &canvasHeight
);
726 float zoomScale
= (float)((float)m_currentZoom
/(float)100);
727 float actualWidth
= zoomScale
*m_pageWidth
*m_previewScale
;
728 float actualHeight
= zoomScale
*m_pageHeight
*m_previewScale
;
730 float x
= (float)((canvasWidth
- actualWidth
)/2.0);
731 if (x
< m_leftMargin
)
732 x
= (float)m_leftMargin
;
733 float y
= (float)m_topMargin
;
735 // Draw shadow, allowing for 1-pixel border AROUND the actual page
736 int shadowOffset
= 4;
737 dc
.SetPen(*wxBLACK_PEN
);
738 dc
.SetBrush(*wxBLACK_BRUSH
);
740 dc.DrawRectangle((int)(x-1 + shadowOffset), (int)(y-1 + shadowOffset), (int)(actualWidth+2), (int)(actualHeight+2));
742 dc
.DrawRectangle((int)(x
+ shadowOffset
), (int)(y
+ actualHeight
+1), (int)(actualWidth
), shadowOffset
);
743 dc
.DrawRectangle((int)(x
+ actualWidth
), (int)(y
+ shadowOffset
), shadowOffset
, (int)(actualHeight
));
745 // Draw blank page allowing for 1-pixel border AROUND the actual page
746 dc
.SetPen(*wxBLACK_PEN
);
747 dc
.SetBrush(*wxWHITE_BRUSH
);
750 wxRegion update_region = canvas->GetUpdateRegion();
751 wxRect r = update_region.GetBox();
753 printf( "x: %d y: %d w: %d h: %d.\n", (int)r.x, (int)r.y, (int)r.width, (int)r.height );
756 dc
.DrawRectangle((int)(x
-2), (int)(y
-1), (int)(actualWidth
+3), (int)(actualHeight
+2));
761 void wxPrintPreviewBase::SetZoom(int percent
)
763 if (m_currentZoom
== percent
)
766 m_currentZoom
= percent
;
769 delete m_previewBitmap
;
770 m_previewBitmap
= NULL
;
772 RenderPage(m_currentPage
);
776 m_previewCanvas
->Clear();
777 m_previewCanvas
->Refresh();