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
);
87 wxWindow
*wxPrinterBase::sm_abortWindow
= (wxWindow
*) NULL
;
88 bool wxPrinterBase::sm_abortIt
= FALSE
;
90 wxPrinterBase::~wxPrinterBase()
94 void wxPrintAbortDialog::OnCancel(wxCommandEvent
& WXUNUSED(event
))
96 wxPrinterBase::sm_abortIt
= TRUE
;
97 wxPrinterBase::sm_abortWindow
->Show(FALSE
);
98 wxPrinterBase::sm_abortWindow
->Close(TRUE
);
99 wxPrinterBase::sm_abortWindow
= (wxWindow
*) NULL
;
102 wxWindow
*wxPrinterBase::CreateAbortWindow(wxWindow
*parent
, wxPrintout
*WXUNUSED(printout
))
104 wxPrintAbortDialog
*dialog
= new wxPrintAbortDialog(parent
, _("Printing"), wxPoint(0, 0), wxSize(400, 400), wxDEFAULT_DIALOG_STYLE
);
105 (void) new wxStaticText(dialog
, -1, _("Please wait..."), wxPoint(5, 5));
107 wxButton
*button
= new wxButton(dialog
, wxID_CANCEL
, _("Cancel"), wxPoint(5, 30));
110 button
->Centre(wxHORIZONTAL
);
116 void wxPrinterBase::ReportError(wxWindow
*parent
, wxPrintout
*WXUNUSED(printout
), char *message
)
118 wxMessageBox(message
, _("Printing Error"), wxOK
, parent
);
125 wxPrintout::wxPrintout(const wxString
& title
)
127 m_printoutTitle
= title
;
128 m_printoutDC
= (wxDC
*) NULL
;
131 m_pageWidthPixels
= 0;
132 m_pageHeightPixels
= 0;
140 wxPrintout::~wxPrintout()
144 bool wxPrintout::OnBeginDocument(int WXUNUSED(startPage
), int WXUNUSED(endPage
))
146 return GetDC()->StartDoc(_("Printing"));
149 void wxPrintout::OnEndDocument()
154 void wxPrintout::OnBeginPrinting()
158 void wxPrintout::OnEndPrinting()
162 bool wxPrintout::HasPage(int page
)
167 void wxPrintout::GetPageInfo(int *minPage
, int *maxPage
, int *fromPage
, int *toPage
)
179 wxPreviewCanvas::wxPreviewCanvas(wxPrintPreviewBase
*preview
, wxWindow
*parent
,
180 const wxPoint
& pos
, const wxSize
& size
, long style
, const wxString
& name
):
181 wxScrolledWindow(parent
, -1, pos
, size
, style
, name
)
183 m_printPreview
= preview
;
184 SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_APPWORKSPACE
));
186 SetScrollbars(15, 18, 100, 100);
189 wxPreviewCanvas::~wxPreviewCanvas()
193 void wxPreviewCanvas::OnPaint(wxPaintEvent
& WXUNUSED(event
))
200 if (!GetUpdateRegion().IsEmpty())
201 dc.SetClippingRegion( GetUpdateRegion() );
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::OnPreviousButton
)
229 EVT_BUTTON(wxID_PREVIEW_NEXT
, wxPreviewControlBar::OnNextButton
)
230 EVT_CHAR(wxPreviewControlBar::OnChar
)
231 EVT_CHOICE(wxID_PREVIEW_ZOOM
, wxPreviewControlBar::OnZoom
)
232 EVT_PAINT(wxPreviewControlBar::OnPaint
)
235 wxPreviewControlBar::wxPreviewControlBar(wxPrintPreviewBase
*preview
, long buttons
,
236 wxWindow
*parent
, const wxPoint
& pos
, const wxSize
& size
,
237 long style
, const wxString
& name
):
238 wxPanel(parent
, -1, pos
, size
, style
, name
)
240 m_printPreview
= preview
;
241 m_closeButton
= (wxButton
*) NULL
;
242 m_nextPageButton
= (wxButton
*) NULL
;
243 m_previousPageButton
= (wxButton
*) NULL
;
244 m_printButton
= (wxButton
*) NULL
;
245 m_zoomControl
= (wxChoice
*) NULL
;
246 m_buttonFlags
= buttons
;
249 wxPreviewControlBar::~wxPreviewControlBar()
253 void wxPreviewControlBar::OnPaint(wxPaintEvent
& WXUNUSED(event
))
259 dc
.SetPen(*wxBLACK_PEN
);
260 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
261 dc
.DrawLine( 0, h
-1, w
, h
-1 );
264 void wxPreviewControlBar::OnWindowClose(wxCommandEvent
& WXUNUSED(event
))
266 wxPreviewFrame
*frame
= (wxPreviewFrame
*)GetParent();
270 void wxPreviewControlBar::OnPrint(wxCommandEvent
& WXUNUSED(event
))
272 wxPrintPreviewBase
*preview
= GetPrintPreview();
273 preview
->Print(TRUE
);
276 void wxPreviewControlBar::OnChar(wxKeyEvent
&event
)
278 switch(event
.KeyCode())
289 void wxPreviewControlBar::OnNext(void)
291 wxPrintPreviewBase
*preview
= GetPrintPreview();
294 int currentPage
= preview
->GetCurrentPage();
295 if ((preview
->GetMaxPage() > 0) &&
296 (currentPage
< preview
->GetMaxPage()) &&
297 preview
->GetPrintout()->HasPage(currentPage
+ 1))
299 preview
->SetCurrentPage(currentPage
+ 1);
304 void wxPreviewControlBar::OnPrevious(void)
306 wxPrintPreviewBase
*preview
= GetPrintPreview();
309 int currentPage
= preview
->GetCurrentPage();
310 if ((preview
->GetMinPage() > 0) &&
311 (currentPage
> preview
->GetMinPage()) &&
312 preview
->GetPrintout()->HasPage(currentPage
- 1))
314 preview
->SetCurrentPage(currentPage
- 1);
319 void wxPreviewControlBar::OnZoom(wxCommandEvent
& WXUNUSED(event
))
321 int zoom
= GetZoomControl();
322 if (GetPrintPreview())
323 GetPrintPreview()->SetZoom(zoom
);
326 void wxPreviewControlBar::CreateButtons()
328 SetSize(0, 0, 400, 40);
337 wxFont buttonFont(fontSize, wxSWISS, wxNORMAL, wxBOLD);
341 int buttonWidth
= 65;
343 int buttonHeight
= -1;
345 int buttonHeight
= 24;
357 m_closeButton
= new wxButton(this, wxID_PREVIEW_CLOSE
, _("Close"),
358 wxPoint(x
, y
), wxSize(buttonWidth
, buttonHeight
));
360 x
+= gap
+ buttonWidth
;
362 if (m_buttonFlags
& wxPREVIEW_PRINT
)
364 m_printButton
= new wxButton(this, wxID_PREVIEW_PRINT
, _("Print..."), wxPoint(x
, y
),
365 wxSize(buttonWidth
, buttonHeight
));
366 x
+= gap
+ buttonWidth
;
369 if (m_buttonFlags
& wxPREVIEW_PREVIOUS
)
371 m_previousPageButton
= new wxButton(this, wxID_PREVIEW_PREVIOUS
, wxT("<<"), wxPoint(x
, y
),
372 wxSize(buttonWidth
, buttonHeight
));
373 x
+= gap
+ buttonWidth
;
376 if (m_buttonFlags
& wxPREVIEW_NEXT
)
378 m_nextPageButton
= new wxButton(this, wxID_PREVIEW_NEXT
, wxT(">>"),
379 wxPoint(x
, y
), wxSize(buttonWidth
, buttonHeight
));
380 x
+= gap
+ buttonWidth
;
383 if (m_buttonFlags
& wxPREVIEW_ZOOM
)
385 static const char *choices
[] =
387 "10%", "15%", "20%", "25%", "30%", "35%", "40%", "45%", "50%", "55%",
388 "60%", "65%", "70%", "75%", "80%", "85%", "90%", "95%", "100%", "110%",
389 "120%", "150%", "200%"
392 int n
= WXSIZEOF(choices
);
394 wxString
* strings
= new wxString
[n
];
396 for (i
= 0; i
< n
; i
++ )
397 strings
[i
] = choices
[i
];
399 m_zoomControl
= new wxChoice(this, wxID_PREVIEW_ZOOM
,
407 SetZoomControl(m_printPreview
->GetZoom());
410 // m_closeButton->SetDefault();
413 void wxPreviewControlBar::SetZoomControl(int zoom
)
416 sprintf(buf
, "%d%%", zoom
);
417 // Someone is calling methods that do no exist in wxChoice!! So I'll just comment out for VA for now
419 m_zoomControl
->SetStringSelection(buf
);
422 int wxPreviewControlBar::GetZoomControl()
425 if (m_zoomControl
&& (m_zoomControl
->GetStringSelection() != wxT("")))
427 wxStrcpy(buf
, m_zoomControl
->GetStringSelection());
428 buf
[wxStrlen(buf
) - 1] = 0;
429 return (int)wxAtoi(buf
);
439 BEGIN_EVENT_TABLE(wxPreviewFrame
, wxFrame
)
440 EVT_CLOSE(wxPreviewFrame::OnCloseWindow
)
443 wxPreviewFrame::wxPreviewFrame(wxPrintPreviewBase
*preview
, wxFrame
*parent
, const wxString
& title
,
444 const wxPoint
& pos
, const wxSize
& size
, long style
, const wxString
& name
):
445 wxFrame(parent
, -1, title
, pos
, size
, style
, name
)
447 m_printPreview
= preview
;
449 m_previewCanvas
= NULL
;
452 wxPreviewFrame::~wxPreviewFrame()
456 void wxPreviewFrame::OnCloseWindow(wxCloseEvent
& WXUNUSED(event
))
460 // Need to delete the printout and the print preview
461 wxPrintout
*printout
= m_printPreview
->GetPrintout();
465 m_printPreview
->SetPrintout(NULL
);
466 m_printPreview
->SetCanvas(NULL
);
467 m_printPreview
->SetFrame(NULL
);
469 delete m_printPreview
;
474 void wxPreviewFrame::Initialize()
481 m_printPreview
->SetCanvas(m_previewCanvas
);
482 m_printPreview
->SetFrame(this);
484 // Set layout constraints here
486 // Control bar constraints
487 wxLayoutConstraints
*c1
= new wxLayoutConstraints
;
489 // m_controlBar->GetSize(&w, &h);
491 #if (defined(__WXMSW__) || defined(__WXGTK__))
497 c1
->left
.SameAs (this, wxLeft
);
498 c1
->top
.SameAs (this, wxTop
);
499 c1
->right
.SameAs (this, wxRight
);
500 c1
->height
.Absolute (h
);
502 m_controlBar
->SetConstraints(c1
);
504 // Canvas constraints
505 wxLayoutConstraints
*c2
= new wxLayoutConstraints
;
507 c2
->left
.SameAs (this, wxLeft
);
508 c2
->top
.Below (m_controlBar
);
509 c2
->right
.SameAs (this, wxRight
);
510 c2
->bottom
.SameAs (this, wxBottom
);
512 m_previewCanvas
->SetConstraints(c2
);
521 void wxPreviewFrame::CreateCanvas()
523 m_previewCanvas
= new wxPreviewCanvas(m_printPreview
, this);
526 void wxPreviewFrame::CreateControlBar()
528 long buttons
= wxPREVIEW_DEFAULT
;
529 if (m_printPreview
->GetPrintoutForPrinting())
530 buttons
|= wxPREVIEW_PRINT
;
532 m_controlBar
= new wxPreviewControlBar(m_printPreview
, buttons
, this, wxPoint(0, 0), wxSize(400, 40));
533 m_controlBar
->CreateButtons();
540 wxPrintPreviewBase::wxPrintPreviewBase(wxPrintout
*printout
,
541 wxPrintout
*printoutForPrinting
,
545 m_printDialogData
= (*data
);
547 Init(printout
, printoutForPrinting
);
550 wxPrintPreviewBase::wxPrintPreviewBase(wxPrintout
*printout
,
551 wxPrintout
*printoutForPrinting
,
552 wxPrintDialogData
*data
)
555 m_printDialogData
= (*data
);
557 Init(printout
, printoutForPrinting
);
560 void wxPrintPreviewBase::Init(wxPrintout
*printout
,
561 wxPrintout
*printoutForPrinting
)
564 m_previewPrintout
= printout
;
565 if (m_previewPrintout
)
566 m_previewPrintout
->SetIsPreview(TRUE
);
568 m_printPrintout
= printoutForPrinting
;
570 m_previewCanvas
= NULL
;
571 m_previewFrame
= NULL
;
572 m_previewBitmap
= NULL
;
579 m_printingPrepared
= FALSE
;
581 // Too soon! Moved to RenderPage.
582 // printout->OnPreparePrinting();
584 // Get some parameters from the printout, if defined
586 printout
->GetPageInfo(&m_minPage
, &m_maxPage
, &selFrom
, &selTo
);
589 wxPrintPreviewBase::~wxPrintPreviewBase()
591 if (m_previewPrintout
)
592 delete m_previewPrintout
;
594 delete m_previewBitmap
;
596 delete m_printPrintout
;
599 bool wxPrintPreviewBase::SetCurrentPage(int pageNum
)
601 if (m_currentPage
== pageNum
)
604 m_currentPage
= pageNum
;
607 delete m_previewBitmap
;
608 m_previewBitmap
= NULL
;
614 m_previewCanvas
->Refresh();
619 bool wxPrintPreviewBase::PaintPage(wxWindow
*canvas
, wxDC
& dc
)
621 DrawBlankPage(canvas
, dc
);
623 if (!m_previewBitmap
)
624 RenderPage(m_currentPage
);
626 if (!m_previewBitmap
)
632 int canvasWidth
, canvasHeight
;
633 canvas
->GetSize(&canvasWidth
, &canvasHeight
);
635 double zoomScale
= ((float)m_currentZoom
/(float)100);
636 double actualWidth
= (zoomScale
*m_pageWidth
*m_previewScale
);
637 // float actualHeight = (float)(zoomScale*m_pageHeight*m_previewScale);
639 int x
= (int) ((canvasWidth
- actualWidth
)/2.0);
640 if (x
< m_leftMargin
)
645 temp_dc
.SelectObject(*m_previewBitmap
);
647 dc
.Blit(x
, y
, m_previewBitmap
->GetWidth(), m_previewBitmap
->GetHeight(), &temp_dc
, 0, 0);
649 temp_dc
.SelectObject(wxNullBitmap
);
654 bool wxPrintPreviewBase::RenderPage(int pageNum
)
656 int canvasWidth
, canvasHeight
;
658 if (!m_previewCanvas
)
660 wxFAIL_MSG(_T("wxPrintPreviewBase::RenderPage: must use "
661 "wxPrintPreviewBase::SetCanvas to let me "
662 "know about the canvas!"));
666 m_previewCanvas
->GetSize(&canvasWidth
, &canvasHeight
);
668 double zoomScale
= (m_currentZoom
/100.0);
669 int actualWidth
= (int)(zoomScale
*m_pageWidth
*m_previewScale
);
670 int actualHeight
= (int)(zoomScale
*m_pageHeight
*m_previewScale
);
672 int x
= (int)((canvasWidth
- actualWidth
)/2.0);
673 if (x
< m_leftMargin
)
675 // int y = m_topMargin;
678 if (!m_previewBitmap
)
680 m_previewBitmap
= new wxBitmap((int)actualWidth
, (int)actualHeight
);
681 if (!m_previewBitmap
|| !m_previewBitmap
->Ok())
684 delete m_previewBitmap
;
685 wxMessageBox(_("Sorry, not enough memory to create a preview."), _("Print Preview Failure"), wxOK
);
691 memoryDC
.SelectObject(*m_previewBitmap
);
695 m_previewPrintout
->SetDC(&memoryDC
);
696 m_previewPrintout
->SetPageSizePixels(m_pageWidth
, m_pageHeight
);
698 // Need to delay OnPreparePrinting until here, so we have enough information.
699 if (!m_printingPrepared
)
701 m_previewPrintout
->OnPreparePrinting();
702 m_printingPrepared
= TRUE
;
705 m_previewPrintout
->OnBeginPrinting();
707 if (!m_previewPrintout
->OnBeginDocument(m_printDialogData
.GetFromPage(), m_printDialogData
.GetToPage()))
709 wxMessageBox(_("Could not start document preview."), _("Print Preview Failure"), wxOK
);
711 memoryDC
.SelectObject(wxNullBitmap
);
713 delete m_previewBitmap
;
717 m_previewPrintout
->OnPrintPage(pageNum
);
718 m_previewPrintout
->OnEndDocument();
719 m_previewPrintout
->OnEndPrinting();
721 m_previewPrintout
->SetDC(NULL
);
723 memoryDC
.SelectObject(wxNullBitmap
);
727 wxSprintf(buf
, _("Page %d of %d"), pageNum
, m_maxPage
);
729 wxSprintf(buf
, _("Page %d"), pageNum
);
732 m_previewFrame
->SetStatusText(buf
);
738 bool wxPrintPreviewBase::DrawBlankPage(wxWindow
*canvas
, wxDC
& dc
)
740 int canvasWidth
, canvasHeight
;
741 canvas
->GetSize(&canvasWidth
, &canvasHeight
);
743 float zoomScale
= (float)((float)m_currentZoom
/(float)100);
744 float actualWidth
= zoomScale
*m_pageWidth
*m_previewScale
;
745 float actualHeight
= zoomScale
*m_pageHeight
*m_previewScale
;
747 float x
= (float)((canvasWidth
- actualWidth
)/2.0);
748 if (x
< m_leftMargin
)
749 x
= (float)m_leftMargin
;
750 float y
= (float)m_topMargin
;
752 // Draw shadow, allowing for 1-pixel border AROUND the actual page
753 int shadowOffset
= 4;
754 dc
.SetPen(*wxBLACK_PEN
);
755 dc
.SetBrush(*wxBLACK_BRUSH
);
757 dc.DrawRectangle((int)(x-1 + shadowOffset), (int)(y-1 + shadowOffset), (int)(actualWidth+2), (int)(actualHeight+2));
759 dc
.DrawRectangle((int)(x
+ shadowOffset
), (int)(y
+ actualHeight
+1), (int)(actualWidth
), shadowOffset
);
760 dc
.DrawRectangle((int)(x
+ actualWidth
), (int)(y
+ shadowOffset
), shadowOffset
, (int)(actualHeight
));
762 // Draw blank page allowing for 1-pixel border AROUND the actual page
763 dc
.SetPen(*wxBLACK_PEN
);
764 dc
.SetBrush(*wxWHITE_BRUSH
);
767 wxRegion update_region = canvas->GetUpdateRegion();
768 wxRect r = update_region.GetBox();
770 printf( "x: %d y: %d w: %d h: %d.\n", (int)r.x, (int)r.y, (int)r.width, (int)r.height );
773 dc
.DrawRectangle((int)(x
-2), (int)(y
-1), (int)(actualWidth
+3), (int)(actualHeight
+2));
778 void wxPrintPreviewBase::SetZoom(int percent
)
780 if (m_currentZoom
== percent
)
783 m_currentZoom
= percent
;
786 delete m_previewBitmap
;
787 m_previewBitmap
= NULL
;
792 RenderPage(m_currentPage
);
793 ((wxScrolledWindow
*) m_previewCanvas
)->Scroll(0, 0);
794 m_previewCanvas
->Clear();
795 m_previewCanvas
->Refresh();
799 #endif // wxUSE_PRINTING_ARCHITECTURE