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
))
199 if (!GetUpdateRegion().IsEmpty())
200 dc
.SetClippingRegion( GetUpdateRegion() );
205 m_printPreview
->PaintPage(this, dc
);
209 // Responds to colour changes, and passes event on to children.
210 void wxPreviewCanvas::OnSysColourChanged(wxSysColourChangedEvent
& event
)
212 SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_APPWORKSPACE
));
215 // Propagate the event to the non-top-level children
216 wxWindow::OnSysColourChanged(event
);
220 * Preview control bar
223 BEGIN_EVENT_TABLE(wxPreviewControlBar
, wxPanel
)
224 EVT_BUTTON(wxID_PREVIEW_CLOSE
, wxPreviewControlBar::OnWindowClose
)
225 EVT_BUTTON(wxID_PREVIEW_PRINT
, wxPreviewControlBar::OnPrint
)
226 EVT_BUTTON(wxID_PREVIEW_PREVIOUS
, wxPreviewControlBar::OnPreviousButton
)
227 EVT_BUTTON(wxID_PREVIEW_NEXT
, wxPreviewControlBar::OnNextButton
)
228 EVT_CHAR(wxPreviewControlBar::OnChar
)
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::OnChar(wxKeyEvent
&event
)
276 switch(event
.KeyCode())
287 void wxPreviewControlBar::OnNext(void)
289 wxPrintPreviewBase
*preview
= GetPrintPreview();
292 int currentPage
= preview
->GetCurrentPage();
293 if ((preview
->GetMaxPage() > 0) &&
294 (currentPage
< preview
->GetMaxPage()) &&
295 preview
->GetPrintout()->HasPage(currentPage
+ 1))
297 preview
->SetCurrentPage(currentPage
+ 1);
302 void wxPreviewControlBar::OnPrevious(void)
304 wxPrintPreviewBase
*preview
= GetPrintPreview();
307 int currentPage
= preview
->GetCurrentPage();
308 if ((preview
->GetMinPage() > 0) &&
309 (currentPage
> preview
->GetMinPage()) &&
310 preview
->GetPrintout()->HasPage(currentPage
- 1))
312 preview
->SetCurrentPage(currentPage
- 1);
317 void wxPreviewControlBar::OnZoom(wxCommandEvent
& WXUNUSED(event
))
319 int zoom
= GetZoomControl();
320 if (GetPrintPreview())
321 GetPrintPreview()->SetZoom(zoom
);
324 void wxPreviewControlBar::CreateButtons()
326 SetSize(0, 0, 400, 40);
335 wxFont buttonFont(fontSize, wxSWISS, wxNORMAL, wxBOLD);
339 int buttonWidth
= 65;
341 int buttonHeight
= -1;
343 int buttonHeight
= 24;
355 m_closeButton
= new wxButton(this, wxID_PREVIEW_CLOSE
, _("Close"),
356 wxPoint(x
, y
), wxSize(buttonWidth
, buttonHeight
));
358 x
+= gap
+ buttonWidth
;
360 if (m_buttonFlags
& wxPREVIEW_PRINT
)
362 m_printButton
= new wxButton(this, wxID_PREVIEW_PRINT
, _("Print..."), wxPoint(x
, y
),
363 wxSize(buttonWidth
, buttonHeight
));
364 x
+= gap
+ buttonWidth
;
367 if (m_buttonFlags
& wxPREVIEW_PREVIOUS
)
369 m_previousPageButton
= new wxButton(this, wxID_PREVIEW_PREVIOUS
, wxT("<<"), wxPoint(x
, y
),
370 wxSize(buttonWidth
, buttonHeight
));
371 x
+= gap
+ buttonWidth
;
374 if (m_buttonFlags
& wxPREVIEW_NEXT
)
376 m_nextPageButton
= new wxButton(this, wxID_PREVIEW_NEXT
, wxT(">>"),
377 wxPoint(x
, y
), wxSize(buttonWidth
, buttonHeight
));
378 x
+= gap
+ buttonWidth
;
381 if (m_buttonFlags
& wxPREVIEW_ZOOM
)
383 static const char *choices
[] =
385 "10%", "15%", "20%", "25%", "30%", "35%", "40%", "45%", "50%", "55%",
386 "60%", "65%", "70%", "75%", "80%", "85%", "90%", "95%", "100%", "110%",
387 "120%", "150%", "200%"
390 int n
= WXSIZEOF(choices
);
392 wxString
* strings
= new wxString
[n
];
394 for (i
= 0; i
< n
; i
++ )
395 strings
[i
] = choices
[i
];
397 m_zoomControl
= new wxChoice(this, wxID_PREVIEW_ZOOM
,
405 SetZoomControl(m_printPreview
->GetZoom());
408 // m_closeButton->SetDefault();
411 void wxPreviewControlBar::SetZoomControl(int zoom
)
414 sprintf(buf
, "%d%%", zoom
);
415 // Someone is calling methods that do no exist in wxChoice!! So I'll just comment out for VA for now
417 m_zoomControl
->SetStringSelection(buf
);
420 int wxPreviewControlBar::GetZoomControl()
423 if (m_zoomControl
&& (m_zoomControl
->GetStringSelection() != wxT("")))
425 wxStrcpy(buf
, m_zoomControl
->GetStringSelection());
426 buf
[wxStrlen(buf
) - 1] = 0;
427 return (int)wxAtoi(buf
);
437 BEGIN_EVENT_TABLE(wxPreviewFrame
, wxFrame
)
438 EVT_CLOSE(wxPreviewFrame::OnCloseWindow
)
441 wxPreviewFrame::wxPreviewFrame(wxPrintPreviewBase
*preview
, wxFrame
*parent
, const wxString
& title
,
442 const wxPoint
& pos
, const wxSize
& size
, long style
, const wxString
& name
):
443 wxFrame(parent
, -1, title
, pos
, size
, style
, name
)
445 m_printPreview
= preview
;
447 m_previewCanvas
= NULL
;
450 wxPreviewFrame::~wxPreviewFrame()
454 void wxPreviewFrame::OnCloseWindow(wxCloseEvent
& WXUNUSED(event
))
458 // Need to delete the printout and the print preview
459 wxPrintout
*printout
= m_printPreview
->GetPrintout();
463 m_printPreview
->SetPrintout(NULL
);
464 m_printPreview
->SetCanvas(NULL
);
465 m_printPreview
->SetFrame(NULL
);
467 delete m_printPreview
;
472 void wxPreviewFrame::Initialize()
479 m_printPreview
->SetCanvas(m_previewCanvas
);
480 m_printPreview
->SetFrame(this);
482 // Set layout constraints here
484 // Control bar constraints
485 wxLayoutConstraints
*c1
= new wxLayoutConstraints
;
487 // m_controlBar->GetSize(&w, &h);
489 #if (defined(__WXMSW__) || defined(__WXGTK__))
495 c1
->left
.SameAs (this, wxLeft
);
496 c1
->top
.SameAs (this, wxTop
);
497 c1
->right
.SameAs (this, wxRight
);
498 c1
->height
.Absolute (h
);
500 m_controlBar
->SetConstraints(c1
);
502 // Canvas constraints
503 wxLayoutConstraints
*c2
= new wxLayoutConstraints
;
505 c2
->left
.SameAs (this, wxLeft
);
506 c2
->top
.Below (m_controlBar
);
507 c2
->right
.SameAs (this, wxRight
);
508 c2
->bottom
.SameAs (this, wxBottom
);
510 m_previewCanvas
->SetConstraints(c2
);
519 void wxPreviewFrame::CreateCanvas()
521 m_previewCanvas
= new wxPreviewCanvas(m_printPreview
, this);
524 void wxPreviewFrame::CreateControlBar()
526 long buttons
= wxPREVIEW_DEFAULT
;
527 if (m_printPreview
->GetPrintoutForPrinting())
528 buttons
|= wxPREVIEW_PRINT
;
530 m_controlBar
= new wxPreviewControlBar(m_printPreview
, buttons
, this, wxPoint(0, 0), wxSize(400, 40));
531 m_controlBar
->CreateButtons();
538 wxPrintPreviewBase::wxPrintPreviewBase(wxPrintout
*printout
,
539 wxPrintout
*printoutForPrinting
,
543 m_printDialogData
= (*data
);
545 Init(printout
, printoutForPrinting
);
548 wxPrintPreviewBase::wxPrintPreviewBase(wxPrintout
*printout
,
549 wxPrintout
*printoutForPrinting
,
550 wxPrintDialogData
*data
)
553 m_printDialogData
= (*data
);
555 Init(printout
, printoutForPrinting
);
558 void wxPrintPreviewBase::Init(wxPrintout
*printout
,
559 wxPrintout
*printoutForPrinting
)
562 m_previewPrintout
= printout
;
563 if (m_previewPrintout
)
564 m_previewPrintout
->SetIsPreview(TRUE
);
566 m_printPrintout
= printoutForPrinting
;
568 m_previewCanvas
= NULL
;
569 m_previewFrame
= NULL
;
570 m_previewBitmap
= NULL
;
577 m_printingPrepared
= FALSE
;
579 // Too soon! Moved to RenderPage.
580 // printout->OnPreparePrinting();
582 // Get some parameters from the printout, if defined
584 printout
->GetPageInfo(&m_minPage
, &m_maxPage
, &selFrom
, &selTo
);
587 wxPrintPreviewBase::~wxPrintPreviewBase()
589 if (m_previewPrintout
)
590 delete m_previewPrintout
;
592 delete m_previewBitmap
;
594 delete m_printPrintout
;
597 bool wxPrintPreviewBase::SetCurrentPage(int pageNum
)
599 if (m_currentPage
== pageNum
)
602 m_currentPage
= pageNum
;
605 delete m_previewBitmap
;
606 m_previewBitmap
= NULL
;
612 m_previewCanvas
->Refresh();
617 bool wxPrintPreviewBase::PaintPage(wxWindow
*canvas
, wxDC
& dc
)
619 DrawBlankPage(canvas
, dc
);
621 if (!m_previewBitmap
)
622 RenderPage(m_currentPage
);
624 if (!m_previewBitmap
)
630 int canvasWidth
, canvasHeight
;
631 canvas
->GetSize(&canvasWidth
, &canvasHeight
);
633 double zoomScale
= ((float)m_currentZoom
/(float)100);
634 double actualWidth
= (zoomScale
*m_pageWidth
*m_previewScale
);
635 // float actualHeight = (float)(zoomScale*m_pageHeight*m_previewScale);
637 int x
= (int) ((canvasWidth
- actualWidth
)/2.0);
638 if (x
< m_leftMargin
)
643 temp_dc
.SelectObject(*m_previewBitmap
);
645 dc
.Blit(x
, y
, m_previewBitmap
->GetWidth(), m_previewBitmap
->GetHeight(), &temp_dc
, 0, 0);
647 temp_dc
.SelectObject(wxNullBitmap
);
652 bool wxPrintPreviewBase::RenderPage(int pageNum
)
654 int canvasWidth
, canvasHeight
;
656 if (!m_previewCanvas
)
658 wxMessageBox(_("wxPrintPreviewBase::RenderPage: must use wxPrintPreviewBase::SetCanvas to let me know about the canvas!"),
659 _("Print Preview Failure"), wxOK
);
662 m_previewCanvas
->GetSize(&canvasWidth
, &canvasHeight
);
664 double zoomScale
= (m_currentZoom
/100.0);
665 int actualWidth
= (int)(zoomScale
*m_pageWidth
*m_previewScale
);
666 int actualHeight
= (int)(zoomScale
*m_pageHeight
*m_previewScale
);
668 int x
= (int)((canvasWidth
- actualWidth
)/2.0);
669 if (x
< m_leftMargin
)
671 // int y = m_topMargin;
674 if (!m_previewBitmap
)
676 m_previewBitmap
= new wxBitmap((int)actualWidth
, (int)actualHeight
);
677 if (!m_previewBitmap
|| !m_previewBitmap
->Ok())
680 delete m_previewBitmap
;
681 wxMessageBox(_("Sorry, not enough memory to create a preview."), _("Print Preview Failure"), wxOK
);
687 memoryDC
.SelectObject(*m_previewBitmap
);
691 m_previewPrintout
->SetDC(&memoryDC
);
692 m_previewPrintout
->SetPageSizePixels(m_pageWidth
, m_pageHeight
);
694 // Need to delay OnPreparePrinting until here, so we have enough information.
695 if (!m_printingPrepared
)
697 m_previewPrintout
->OnPreparePrinting();
698 m_printingPrepared
= TRUE
;
701 m_previewPrintout
->OnBeginPrinting();
703 if (!m_previewPrintout
->OnBeginDocument(m_printDialogData
.GetFromPage(), m_printDialogData
.GetToPage()))
705 wxMessageBox(_("Could not start document preview."), _("Print Preview Failure"), wxOK
);
707 memoryDC
.SelectObject(wxNullBitmap
);
709 delete m_previewBitmap
;
713 m_previewPrintout
->OnPrintPage(pageNum
);
714 m_previewPrintout
->OnEndDocument();
715 m_previewPrintout
->OnEndPrinting();
717 m_previewPrintout
->SetDC(NULL
);
719 memoryDC
.SelectObject(wxNullBitmap
);
723 wxSprintf(buf
, _("Page %d of %d"), pageNum
, m_maxPage
);
725 wxSprintf(buf
, _("Page %d"), pageNum
);
728 m_previewFrame
->SetStatusText(buf
);
734 bool wxPrintPreviewBase::DrawBlankPage(wxWindow
*canvas
, wxDC
& dc
)
736 int canvasWidth
, canvasHeight
;
737 canvas
->GetSize(&canvasWidth
, &canvasHeight
);
739 float zoomScale
= (float)((float)m_currentZoom
/(float)100);
740 float actualWidth
= zoomScale
*m_pageWidth
*m_previewScale
;
741 float actualHeight
= zoomScale
*m_pageHeight
*m_previewScale
;
743 float x
= (float)((canvasWidth
- actualWidth
)/2.0);
744 if (x
< m_leftMargin
)
745 x
= (float)m_leftMargin
;
746 float y
= (float)m_topMargin
;
748 // Draw shadow, allowing for 1-pixel border AROUND the actual page
749 int shadowOffset
= 4;
750 dc
.SetPen(*wxBLACK_PEN
);
751 dc
.SetBrush(*wxBLACK_BRUSH
);
753 dc.DrawRectangle((int)(x-1 + shadowOffset), (int)(y-1 + shadowOffset), (int)(actualWidth+2), (int)(actualHeight+2));
755 dc
.DrawRectangle((int)(x
+ shadowOffset
), (int)(y
+ actualHeight
+1), (int)(actualWidth
), shadowOffset
);
756 dc
.DrawRectangle((int)(x
+ actualWidth
), (int)(y
+ shadowOffset
), shadowOffset
, (int)(actualHeight
));
758 // Draw blank page allowing for 1-pixel border AROUND the actual page
759 dc
.SetPen(*wxBLACK_PEN
);
760 dc
.SetBrush(*wxWHITE_BRUSH
);
763 wxRegion update_region = canvas->GetUpdateRegion();
764 wxRect r = update_region.GetBox();
766 printf( "x: %d y: %d w: %d h: %d.\n", (int)r.x, (int)r.y, (int)r.width, (int)r.height );
769 dc
.DrawRectangle((int)(x
-2), (int)(y
-1), (int)(actualWidth
+3), (int)(actualHeight
+2));
774 void wxPrintPreviewBase::SetZoom(int percent
)
776 if (m_currentZoom
== percent
)
779 m_currentZoom
= percent
;
782 delete m_previewBitmap
;
783 m_previewBitmap
= NULL
;
788 RenderPage(m_currentPage
);
789 ((wxScrolledWindow
*) m_previewCanvas
)->Scroll(0, 0);
790 m_previewCanvas
->Clear();
791 m_previewCanvas
->Refresh();
795 #endif // wxUSE_PRINTING_ARCHITECTURE