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 if (m_buttonFlags
& wxPREVIEW_ZOOM
)
371 static const char *choices
[] =
373 "10%", "15%", "20%", "25%", "30%", "35%", "40%", "45%", "50%", "55%",
374 "60%", "65%", "70%", "75%", "80%", "85%", "90%", "95%", "100%", "110%",
375 "120%", "150%", "200%"
378 m_zoomControl
= new wxChoice(this, wxID_PREVIEW_ZOOM
,
379 wxPoint(x
, y
), wxSize(100, -1));
381 // Yes, this look stupid, but this is because gcc gives up otherwise.
382 int n
= WXSIZEOF(choices
);
383 for ( int i
= 0; i
< n
; i
++ )
384 m_zoomControl
->Append(choices
[i
]);
386 SetZoomControl(m_printPreview
->GetZoom());
389 // m_closeButton->SetDefault();
392 void wxPreviewControlBar::SetZoomControl(int zoom
)
395 sprintf(buf
, "%d%%", zoom
);
397 m_zoomControl
->SetStringSelection(buf
);
400 int wxPreviewControlBar::GetZoomControl()
403 if (m_zoomControl
&& (m_zoomControl
->GetStringSelection() != _T("")))
405 wxStrcpy(buf
, m_zoomControl
->GetStringSelection());
406 buf
[wxStrlen(buf
) - 1] = 0;
407 return (int)wxAtoi(buf
);
417 BEGIN_EVENT_TABLE(wxPreviewFrame
, wxFrame
)
418 EVT_CLOSE(wxPreviewFrame::OnCloseWindow
)
421 wxPreviewFrame::wxPreviewFrame(wxPrintPreviewBase
*preview
, wxFrame
*parent
, const wxString
& title
,
422 const wxPoint
& pos
, const wxSize
& size
, long style
, const wxString
& name
):
423 wxFrame(parent
, -1, title
, pos
, size
, style
, name
)
425 m_printPreview
= preview
;
427 m_previewCanvas
= NULL
;
430 wxPreviewFrame::~wxPreviewFrame()
434 void wxPreviewFrame::OnCloseWindow(wxCloseEvent
& WXUNUSED(event
))
438 // Need to delete the printout and the print preview
439 wxPrintout
*printout
= m_printPreview
->GetPrintout();
443 m_printPreview
->SetPrintout(NULL
);
444 m_printPreview
->SetCanvas(NULL
);
445 m_printPreview
->SetFrame(NULL
);
447 delete m_printPreview
;
452 void wxPreviewFrame::Initialize()
459 m_printPreview
->SetCanvas(m_previewCanvas
);
460 m_printPreview
->SetFrame(this);
462 // Set layout constraints here
464 // Control bar constraints
465 wxLayoutConstraints
*c1
= new wxLayoutConstraints
;
467 // m_controlBar->GetSize(&w, &h);
469 #if (defined(__WXMSW__) || defined(__WXGTK__))
475 c1
->left
.SameAs (this, wxLeft
);
476 c1
->top
.SameAs (this, wxTop
);
477 c1
->right
.SameAs (this, wxRight
);
478 c1
->height
.Absolute (h
);
480 m_controlBar
->SetConstraints(c1
);
482 // Canvas constraints
483 wxLayoutConstraints
*c2
= new wxLayoutConstraints
;
485 c2
->left
.SameAs (this, wxLeft
);
486 c2
->top
.Below (m_controlBar
);
487 c2
->right
.SameAs (this, wxRight
);
488 c2
->bottom
.SameAs (this, wxBottom
);
490 m_previewCanvas
->SetConstraints(c2
);
499 void wxPreviewFrame::CreateCanvas()
501 m_previewCanvas
= new wxPreviewCanvas(m_printPreview
, this);
504 void wxPreviewFrame::CreateControlBar()
506 long buttons
= wxPREVIEW_DEFAULT
;
507 if (m_printPreview
->GetPrintoutForPrinting())
508 buttons
|= wxPREVIEW_PRINT
;
510 m_controlBar
= new wxPreviewControlBar(m_printPreview
, buttons
, this, wxPoint(0, 0), wxSize(400, 40));
511 m_controlBar
->CreateButtons();
518 wxPrintPreviewBase::wxPrintPreviewBase(wxPrintout
*printout
,
519 wxPrintout
*printoutForPrinting
,
523 m_printDialogData
= (*data
);
525 Init(printout
, printoutForPrinting
);
528 wxPrintPreviewBase::wxPrintPreviewBase(wxPrintout
*printout
,
529 wxPrintout
*printoutForPrinting
,
530 wxPrintDialogData
*data
)
533 m_printDialogData
= (*data
);
535 Init(printout
, printoutForPrinting
);
538 void wxPrintPreviewBase::Init(wxPrintout
*printout
,
539 wxPrintout
*printoutForPrinting
)
542 m_previewPrintout
= printout
;
543 if (m_previewPrintout
)
544 m_previewPrintout
->SetIsPreview(TRUE
);
546 m_printPrintout
= printoutForPrinting
;
548 m_previewCanvas
= NULL
;
549 m_previewFrame
= NULL
;
550 m_previewBitmap
= NULL
;
558 printout
->OnPreparePrinting();
560 // Get some parameters from the printout, if defined
562 printout
->GetPageInfo(&m_minPage
, &m_maxPage
, &selFrom
, &selTo
);
565 wxPrintPreviewBase::~wxPrintPreviewBase()
567 if (m_previewPrintout
)
568 delete m_previewPrintout
;
570 delete m_previewBitmap
;
572 delete m_printPrintout
;
575 bool wxPrintPreviewBase::SetCurrentPage(int pageNum
)
577 if (m_currentPage
== pageNum
)
580 m_currentPage
= pageNum
;
583 delete m_previewBitmap
;
584 m_previewBitmap
= NULL
;
590 m_previewCanvas
->Refresh();
595 bool wxPrintPreviewBase::PaintPage(wxWindow
*canvas
, wxDC
& dc
)
597 DrawBlankPage(canvas
, dc
);
599 if (!m_previewBitmap
)
600 RenderPage(m_currentPage
);
602 if (!m_previewBitmap
)
608 int canvasWidth
, canvasHeight
;
609 canvas
->GetSize(&canvasWidth
, &canvasHeight
);
611 double zoomScale
= ((float)m_currentZoom
/(float)100);
612 double actualWidth
= (zoomScale
*m_pageWidth
*m_previewScale
);
613 // float actualHeight = (float)(zoomScale*m_pageHeight*m_previewScale);
615 int x
= (int) ((canvasWidth
- actualWidth
)/2.0);
616 if (x
< m_leftMargin
)
621 temp_dc
.SelectObject(*m_previewBitmap
);
623 dc
.Blit(x
, y
, m_previewBitmap
->GetWidth(), m_previewBitmap
->GetHeight(), &temp_dc
, 0, 0);
625 temp_dc
.SelectObject(wxNullBitmap
);
630 bool wxPrintPreviewBase::RenderPage(int pageNum
)
632 int canvasWidth
, canvasHeight
;
634 if (!m_previewCanvas
)
636 wxMessageBox(_("wxPrintPreviewBase::RenderPage: must use wxPrintPreviewBase::SetCanvas to let me know about the canvas!"),
637 _("Print Preview Failure"), wxOK
);
640 m_previewCanvas
->GetSize(&canvasWidth
, &canvasHeight
);
642 double zoomScale
= (m_currentZoom
/100.0);
643 int actualWidth
= (int)(zoomScale
*m_pageWidth
*m_previewScale
);
644 int actualHeight
= (int)(zoomScale
*m_pageHeight
*m_previewScale
);
646 int x
= (int)((canvasWidth
- actualWidth
)/2.0);
647 if (x
< m_leftMargin
)
649 // int y = m_topMargin;
652 if (!m_previewBitmap
)
654 m_previewBitmap
= new wxBitmap((int)actualWidth
, (int)actualHeight
);
655 if (!m_previewBitmap
|| !m_previewBitmap
->Ok())
658 delete m_previewBitmap
;
659 wxMessageBox(_("Sorry, not enough memory to create a preview."), _("Print Preview Failure"), wxOK
);
665 memoryDC
.SelectObject(*m_previewBitmap
);
669 m_previewPrintout
->SetDC(&memoryDC
);
670 m_previewPrintout
->SetPageSizePixels(m_pageWidth
, m_pageHeight
);
672 m_previewPrintout
->OnBeginPrinting();
674 if (!m_previewPrintout
->OnBeginDocument(m_printDialogData
.GetFromPage(), m_printDialogData
.GetToPage()))
676 wxMessageBox(_("Could not start document preview."), _("Print Preview Failure"), wxOK
);
678 memoryDC
.SelectObject(wxNullBitmap
);
680 delete m_previewBitmap
;
684 m_previewPrintout
->OnPrintPage(pageNum
);
685 m_previewPrintout
->OnEndDocument();
686 m_previewPrintout
->OnEndPrinting();
688 m_previewPrintout
->SetDC(NULL
);
690 memoryDC
.SelectObject(wxNullBitmap
);
694 wxSprintf(buf
, _("Page %d of %d"), pageNum
, m_maxPage
);
696 wxSprintf(buf
, _("Page %d"), pageNum
);
699 m_previewFrame
->SetStatusText(buf
);
705 bool wxPrintPreviewBase::DrawBlankPage(wxWindow
*canvas
, wxDC
& dc
)
707 int canvasWidth
, canvasHeight
;
708 canvas
->GetSize(&canvasWidth
, &canvasHeight
);
710 float zoomScale
= (float)((float)m_currentZoom
/(float)100);
711 float actualWidth
= zoomScale
*m_pageWidth
*m_previewScale
;
712 float actualHeight
= zoomScale
*m_pageHeight
*m_previewScale
;
714 float x
= (float)((canvasWidth
- actualWidth
)/2.0);
715 if (x
< m_leftMargin
)
716 x
= (float)m_leftMargin
;
717 float y
= (float)m_topMargin
;
719 // Draw shadow, allowing for 1-pixel border AROUND the actual page
720 int shadowOffset
= 4;
721 dc
.SetPen(*wxBLACK_PEN
);
722 dc
.SetBrush(*wxBLACK_BRUSH
);
724 dc.DrawRectangle((int)(x-1 + shadowOffset), (int)(y-1 + shadowOffset), (int)(actualWidth+2), (int)(actualHeight+2));
726 dc
.DrawRectangle((int)(x
+ shadowOffset
), (int)(y
+ actualHeight
+1), (int)(actualWidth
), shadowOffset
);
727 dc
.DrawRectangle((int)(x
+ actualWidth
), (int)(y
+ shadowOffset
), shadowOffset
, (int)(actualHeight
));
729 // Draw blank page allowing for 1-pixel border AROUND the actual page
730 dc
.SetPen(*wxBLACK_PEN
);
731 dc
.SetBrush(*wxWHITE_BRUSH
);
734 wxRegion update_region = canvas->GetUpdateRegion();
735 wxRect r = update_region.GetBox();
737 printf( "x: %d y: %d w: %d h: %d.\n", (int)r.x, (int)r.y, (int)r.width, (int)r.height );
740 dc
.DrawRectangle((int)(x
-2), (int)(y
-1), (int)(actualWidth
+3), (int)(actualHeight
+2));
745 void wxPrintPreviewBase::SetZoom(int percent
)
747 if (m_currentZoom
== percent
)
750 m_currentZoom
= percent
;
753 delete m_previewBitmap
;
754 m_previewBitmap
= NULL
;
760 RenderPage(m_currentPage
);
761 m_previewCanvas
->Clear();
762 m_previewCanvas
->Refresh();