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 static const char *choices
[] =
371 "10%", "15%", "20%", "25%", "30%", "35%", "40%", "45%", "50%", "55%",
372 "60%", "65%", "70%", "75%", "80%", "85%", "90%", "95%", "100%", "110%",
373 "120%", "150%", "200%"
376 if (m_buttonFlags
& wxPREVIEW_ZOOM
)
378 m_zoomControl
= new wxChoice(this, wxID_PREVIEW_ZOOM
,
379 wxPoint(x
, y
), wxSize(100, -1));
380 SetZoomControl(m_printPreview
->GetZoom());
383 // Yes, this look stupid, but this is because gcc gives up otherwise.
384 int n
= WXSIZEOF(choices
);
385 for ( int i
= 0; i
< n
; i
++ )
386 m_zoomControl
->Append(choices
[i
]);
388 // m_closeButton->SetDefault();
391 void wxPreviewControlBar::SetZoomControl(int zoom
)
394 sprintf(buf
, "%d%%", zoom
);
396 m_zoomControl
->SetStringSelection(buf
);
399 int wxPreviewControlBar::GetZoomControl()
402 if (m_zoomControl
&& (m_zoomControl
->GetStringSelection() != ""))
404 wxStrcpy(buf
, m_zoomControl
->GetStringSelection());
405 buf
[wxStrlen(buf
) - 1] = 0;
406 return (int)wxAtoi(buf
);
416 BEGIN_EVENT_TABLE(wxPreviewFrame
, wxFrame
)
417 EVT_CLOSE(wxPreviewFrame::OnCloseWindow
)
420 wxPreviewFrame::wxPreviewFrame(wxPrintPreviewBase
*preview
, wxFrame
*parent
, const wxString
& title
,
421 const wxPoint
& pos
, const wxSize
& size
, long style
, const wxString
& name
):
422 wxFrame(parent
, -1, title
, pos
, size
, style
, name
)
424 m_printPreview
= preview
;
426 m_previewCanvas
= NULL
;
429 wxPreviewFrame::~wxPreviewFrame()
433 void wxPreviewFrame::OnCloseWindow(wxCloseEvent
& event
)
437 // Need to delete the printout and the print preview
438 wxPrintout
*printout
= m_printPreview
->GetPrintout();
442 m_printPreview
->SetPrintout(NULL
);
443 m_printPreview
->SetCanvas(NULL
);
444 m_printPreview
->SetFrame(NULL
);
446 delete m_printPreview
;
451 void wxPreviewFrame::Initialize()
458 m_printPreview
->SetCanvas(m_previewCanvas
);
459 m_printPreview
->SetFrame(this);
461 // Set layout constraints here
463 // Control bar constraints
464 wxLayoutConstraints
*c1
= new wxLayoutConstraints
;
466 // m_controlBar->GetSize(&w, &h);
468 #if (defined(__WXMSW__) || defined(__WXGTK__))
474 c1
->left
.SameAs (this, wxLeft
);
475 c1
->top
.SameAs (this, wxTop
);
476 c1
->right
.SameAs (this, wxRight
);
477 c1
->height
.Absolute (h
);
479 m_controlBar
->SetConstraints(c1
);
481 // Canvas constraints
482 wxLayoutConstraints
*c2
= new wxLayoutConstraints
;
484 c2
->left
.SameAs (this, wxLeft
);
485 c2
->top
.Below (m_controlBar
);
486 c2
->right
.SameAs (this, wxRight
);
487 c2
->bottom
.SameAs (this, wxBottom
);
489 m_previewCanvas
->SetConstraints(c2
);
498 void wxPreviewFrame::CreateCanvas()
500 m_previewCanvas
= new wxPreviewCanvas(m_printPreview
, this);
503 void wxPreviewFrame::CreateControlBar()
505 long buttons
= wxPREVIEW_DEFAULT
;
506 if (m_printPreview
->GetPrintoutForPrinting())
507 buttons
|= wxPREVIEW_PRINT
;
509 m_controlBar
= new wxPreviewControlBar(m_printPreview
, buttons
, this, wxPoint(0, 0), wxSize(400, 40));
510 m_controlBar
->CreateButtons();
517 wxPrintPreviewBase::wxPrintPreviewBase(wxPrintout
*printout
,
518 wxPrintout
*printoutForPrinting
,
522 m_printDialogData
= (*data
);
524 Init(printout
, printoutForPrinting
);
527 wxPrintPreviewBase::wxPrintPreviewBase(wxPrintout
*printout
,
528 wxPrintout
*printoutForPrinting
,
529 wxPrintDialogData
*data
)
532 m_printDialogData
= (*data
);
534 Init(printout
, printoutForPrinting
);
537 void wxPrintPreviewBase::Init(wxPrintout
*printout
,
538 wxPrintout
*printoutForPrinting
)
541 m_previewPrintout
= printout
;
542 if (m_previewPrintout
)
543 m_previewPrintout
->SetIsPreview(TRUE
);
545 m_printPrintout
= printoutForPrinting
;
547 m_previewCanvas
= NULL
;
548 m_previewFrame
= NULL
;
549 m_previewBitmap
= NULL
;
557 printout
->OnPreparePrinting();
559 // Get some parameters from the printout, if defined
561 printout
->GetPageInfo(&m_minPage
, &m_maxPage
, &selFrom
, &selTo
);
564 wxPrintPreviewBase::~wxPrintPreviewBase()
566 if (m_previewPrintout
)
567 delete m_previewPrintout
;
569 delete m_previewBitmap
;
571 delete m_printPrintout
;
574 bool wxPrintPreviewBase::SetCurrentPage(int pageNum
)
576 if (m_currentPage
== pageNum
)
579 m_currentPage
= pageNum
;
582 delete m_previewBitmap
;
583 m_previewBitmap
= NULL
;
589 m_previewCanvas
->Refresh();
594 bool wxPrintPreviewBase::PaintPage(wxWindow
*canvas
, wxDC
& dc
)
596 DrawBlankPage(canvas
, dc
);
598 if (!m_previewBitmap
)
599 RenderPage(m_currentPage
);
601 if (!m_previewBitmap
)
607 int canvasWidth
, canvasHeight
;
608 canvas
->GetSize(&canvasWidth
, &canvasHeight
);
610 double zoomScale
= ((float)m_currentZoom
/(float)100);
611 double actualWidth
= (zoomScale
*m_pageWidth
*m_previewScale
);
612 // float actualHeight = (float)(zoomScale*m_pageHeight*m_previewScale);
614 int x
= (int) ((canvasWidth
- actualWidth
)/2.0);
615 if (x
< m_leftMargin
)
620 temp_dc
.SelectObject(*m_previewBitmap
);
622 dc
.Blit(x
, y
, m_previewBitmap
->GetWidth(), m_previewBitmap
->GetHeight(), &temp_dc
, 0, 0);
624 temp_dc
.SelectObject(wxNullBitmap
);
629 bool wxPrintPreviewBase::RenderPage(int pageNum
)
631 int canvasWidth
, canvasHeight
;
633 if (!m_previewCanvas
)
635 wxMessageBox(_("wxPrintPreviewBase::RenderPage: must use wxPrintPreviewBase::SetCanvas to let me know about the canvas!"),
636 _("Print Preview Failure"), wxOK
);
639 m_previewCanvas
->GetSize(&canvasWidth
, &canvasHeight
);
641 double zoomScale
= (m_currentZoom
/100.0);
642 int actualWidth
= (int)(zoomScale
*m_pageWidth
*m_previewScale
);
643 int actualHeight
= (int)(zoomScale
*m_pageHeight
*m_previewScale
);
645 int x
= (int)((canvasWidth
- actualWidth
)/2.0);
646 if (x
< m_leftMargin
)
648 // int y = m_topMargin;
651 if (!m_previewBitmap
)
653 m_previewBitmap
= new wxBitmap((int)actualWidth
, (int)actualHeight
);
654 if (!m_previewBitmap
|| !m_previewBitmap
->Ok())
657 delete m_previewBitmap
;
658 wxMessageBox(_("Sorry, not enough memory to create a preview."), _("Print Preview Failure"), wxOK
);
664 memoryDC
.SelectObject(*m_previewBitmap
);
668 m_previewPrintout
->SetDC(&memoryDC
);
669 m_previewPrintout
->SetPageSizePixels(m_pageWidth
, m_pageHeight
);
671 m_previewPrintout
->OnBeginPrinting();
673 if (!m_previewPrintout
->OnBeginDocument(m_printDialogData
.GetFromPage(), m_printDialogData
.GetToPage()))
675 wxMessageBox(_("Could not start document preview."), _("Print Preview Failure"), wxOK
);
677 memoryDC
.SelectObject(wxNullBitmap
);
679 delete m_previewBitmap
;
683 m_previewPrintout
->OnPrintPage(pageNum
);
684 m_previewPrintout
->OnEndDocument();
685 m_previewPrintout
->OnEndPrinting();
687 m_previewPrintout
->SetDC(NULL
);
689 memoryDC
.SelectObject(wxNullBitmap
);
693 wxSprintf(buf
, _("Page %d of %d"), pageNum
, m_maxPage
);
695 wxSprintf(buf
, _("Page %d"), pageNum
);
698 m_previewFrame
->SetStatusText(buf
);
704 bool wxPrintPreviewBase::DrawBlankPage(wxWindow
*canvas
, wxDC
& dc
)
706 int canvasWidth
, canvasHeight
;
707 canvas
->GetSize(&canvasWidth
, &canvasHeight
);
709 float zoomScale
= (float)((float)m_currentZoom
/(float)100);
710 float actualWidth
= zoomScale
*m_pageWidth
*m_previewScale
;
711 float actualHeight
= zoomScale
*m_pageHeight
*m_previewScale
;
713 float x
= (float)((canvasWidth
- actualWidth
)/2.0);
714 if (x
< m_leftMargin
)
715 x
= (float)m_leftMargin
;
716 float y
= (float)m_topMargin
;
718 // Draw shadow, allowing for 1-pixel border AROUND the actual page
719 int shadowOffset
= 4;
720 dc
.SetPen(*wxBLACK_PEN
);
721 dc
.SetBrush(*wxBLACK_BRUSH
);
723 dc.DrawRectangle((int)(x-1 + shadowOffset), (int)(y-1 + shadowOffset), (int)(actualWidth+2), (int)(actualHeight+2));
725 dc
.DrawRectangle((int)(x
+ shadowOffset
), (int)(y
+ actualHeight
+1), (int)(actualWidth
), shadowOffset
);
726 dc
.DrawRectangle((int)(x
+ actualWidth
), (int)(y
+ shadowOffset
), shadowOffset
, (int)(actualHeight
));
728 // Draw blank page allowing for 1-pixel border AROUND the actual page
729 dc
.SetPen(*wxBLACK_PEN
);
730 dc
.SetBrush(*wxWHITE_BRUSH
);
733 wxRegion update_region = canvas->GetUpdateRegion();
734 wxRect r = update_region.GetBox();
736 printf( "x: %d y: %d w: %d h: %d.\n", (int)r.x, (int)r.y, (int)r.width, (int)r.height );
739 dc
.DrawRectangle((int)(x
-2), (int)(y
-1), (int)(actualWidth
+3), (int)(actualHeight
+2));
744 void wxPrintPreviewBase::SetZoom(int percent
)
746 if (m_currentZoom
== percent
)
749 m_currentZoom
= percent
;
752 delete m_previewBitmap
;
753 m_previewBitmap
= NULL
;
755 RenderPage(m_currentPage
);
759 m_previewCanvas
->Clear();
760 m_previewCanvas
->Refresh();