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 #if !USE_SHARED_LIBRARY
59 IMPLEMENT_CLASS(wxPrinterBase
, wxObject
)
60 IMPLEMENT_ABSTRACT_CLASS(wxPrintout
, wxObject
)
61 IMPLEMENT_CLASS(wxPreviewCanvas
, wxWindow
)
62 IMPLEMENT_CLASS(wxPreviewControlBar
, wxWindow
)
63 IMPLEMENT_CLASS(wxPreviewFrame
, wxFrame
)
64 IMPLEMENT_CLASS(wxPrintPreviewBase
, wxObject
)
66 BEGIN_EVENT_TABLE(wxPrintAbortDialog
, wxDialog
)
67 EVT_BUTTON(wxID_CANCEL
, wxPrintAbortDialog::OnCancel
)
70 BEGIN_EVENT_TABLE(wxPreviewCanvas
, wxScrolledWindow
)
71 EVT_PAINT(wxPreviewCanvas::OnPaint
)
72 EVT_SYS_COLOUR_CHANGED(wxPreviewCanvas::OnSysColourChanged
)
80 wxPrinterBase::wxPrinterBase(wxPrintDialogData
*data
)
82 m_currentPrintout
= (wxPrintout
*) NULL
;
83 sm_abortWindow
= (wxWindow
*) NULL
;
86 m_printDialogData
= (*data
);
89 wxWindow
*wxPrinterBase::sm_abortWindow
= (wxWindow
*) NULL
;
90 bool wxPrinterBase::sm_abortIt
= FALSE
;
92 wxPrinterBase::~wxPrinterBase()
96 void wxPrintAbortDialog::OnCancel(wxCommandEvent
& WXUNUSED(event
))
98 wxPrinterBase::sm_abortIt
= TRUE
;
99 wxPrinterBase::sm_abortWindow
->Show(FALSE
);
100 wxPrinterBase::sm_abortWindow
->Close(TRUE
);
101 wxPrinterBase::sm_abortWindow
= (wxWindow
*) NULL
;
104 wxWindow
*wxPrinterBase::CreateAbortWindow(wxWindow
*parent
, wxPrintout
*WXUNUSED(printout
))
106 wxPrintAbortDialog
*dialog
= new wxPrintAbortDialog(parent
, _("Printing"), wxPoint(0, 0), wxSize(400, 400), wxDEFAULT_DIALOG_STYLE
);
107 (void) new wxStaticText(dialog
, -1, _("Please wait..."), wxPoint(5, 5));
109 wxButton
*button
= new wxButton(dialog
, wxID_CANCEL
, _("Cancel"), wxPoint(5, 30));
112 button
->Centre(wxHORIZONTAL
);
118 void wxPrinterBase::ReportError(wxWindow
*parent
, wxPrintout
*WXUNUSED(printout
), char *message
)
120 wxMessageBox(message
, _("Printing Error"), wxOK
, parent
);
127 wxPrintout::wxPrintout(const wxString
& title
)
129 m_printoutTitle
= title
;
130 m_printoutDC
= (wxDC
*) NULL
;
133 m_pageWidthPixels
= 0;
134 m_pageHeightPixels
= 0;
142 wxPrintout::~wxPrintout()
146 bool wxPrintout::OnBeginDocument(int WXUNUSED(startPage
), int WXUNUSED(endPage
))
148 return GetDC()->StartDoc(_("Printing"));
151 void wxPrintout::OnEndDocument()
156 void wxPrintout::OnBeginPrinting()
160 void wxPrintout::OnEndPrinting()
164 bool wxPrintout::HasPage(int page
)
169 void wxPrintout::GetPageInfo(int *minPage
, int *maxPage
, int *fromPage
, int *toPage
)
181 wxPreviewCanvas::wxPreviewCanvas(wxPrintPreviewBase
*preview
, wxWindow
*parent
,
182 const wxPoint
& pos
, const wxSize
& size
, long style
, const wxString
& name
):
183 wxScrolledWindow(parent
, -1, pos
, size
, style
, name
)
185 m_printPreview
= preview
;
186 SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_APPWORKSPACE
));
188 SetScrollbars(15, 18, 100, 100);
191 wxPreviewCanvas::~wxPreviewCanvas()
195 void wxPreviewCanvas::OnPaint(wxPaintEvent
& WXUNUSED(event
))
202 m_printPreview
->PaintPage(this, dc
);
206 // Responds to colour changes, and passes event on to children.
207 void wxPreviewCanvas::OnSysColourChanged(wxSysColourChangedEvent
& event
)
209 SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_APPWORKSPACE
));
212 // Propagate the event to the non-top-level children
213 wxWindow::OnSysColourChanged(event
);
217 * Preview control bar
220 BEGIN_EVENT_TABLE(wxPreviewControlBar
, wxPanel
)
221 EVT_BUTTON(wxID_PREVIEW_CLOSE
, wxPreviewControlBar::OnWindowClose
)
222 EVT_BUTTON(wxID_PREVIEW_PRINT
, wxPreviewControlBar::OnPrint
)
223 EVT_BUTTON(wxID_PREVIEW_PREVIOUS
, wxPreviewControlBar::OnPreviousButton
)
224 EVT_BUTTON(wxID_PREVIEW_NEXT
, wxPreviewControlBar::OnNextButton
)
225 EVT_CHAR(wxPreviewControlBar::OnChar
)
226 EVT_CHOICE(wxID_PREVIEW_ZOOM
, wxPreviewControlBar::OnZoom
)
227 EVT_PAINT(wxPreviewControlBar::OnPaint
)
230 wxPreviewControlBar::wxPreviewControlBar(wxPrintPreviewBase
*preview
, long buttons
,
231 wxWindow
*parent
, const wxPoint
& pos
, const wxSize
& size
,
232 long style
, const wxString
& name
):
233 wxPanel(parent
, -1, pos
, size
, style
, name
)
235 m_printPreview
= preview
;
236 m_closeButton
= (wxButton
*) NULL
;
237 m_nextPageButton
= (wxButton
*) NULL
;
238 m_previousPageButton
= (wxButton
*) NULL
;
239 m_printButton
= (wxButton
*) NULL
;
240 m_zoomControl
= (wxChoice
*) NULL
;
241 m_buttonFlags
= buttons
;
244 wxPreviewControlBar::~wxPreviewControlBar()
248 void wxPreviewControlBar::OnPaint(wxPaintEvent
& WXUNUSED(event
))
254 dc
.SetPen(*wxBLACK_PEN
);
255 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
256 dc
.DrawLine( 0, h
-1, w
, h
-1 );
259 void wxPreviewControlBar::OnWindowClose(wxCommandEvent
& WXUNUSED(event
))
261 wxPreviewFrame
*frame
= (wxPreviewFrame
*)GetParent();
265 void wxPreviewControlBar::OnPrint(wxCommandEvent
& WXUNUSED(event
))
267 wxPrintPreviewBase
*preview
= GetPrintPreview();
268 preview
->Print(TRUE
);
271 void wxPreviewControlBar::OnChar(wxKeyEvent
&event
)
273 switch(event
.KeyCode())
284 void wxPreviewControlBar::OnNext(void)
286 wxPrintPreviewBase
*preview
= GetPrintPreview();
289 int currentPage
= preview
->GetCurrentPage();
290 if ((preview
->GetMaxPage() > 0) &&
291 (currentPage
< preview
->GetMaxPage()) &&
292 preview
->GetPrintout()->HasPage(currentPage
+ 1))
294 preview
->SetCurrentPage(currentPage
+ 1);
299 void wxPreviewControlBar::OnPrevious(void)
301 wxPrintPreviewBase
*preview
= GetPrintPreview();
304 int currentPage
= preview
->GetCurrentPage();
305 if ((preview
->GetMinPage() > 0) &&
306 (currentPage
> preview
->GetMinPage()) &&
307 preview
->GetPrintout()->HasPage(currentPage
- 1))
309 preview
->SetCurrentPage(currentPage
- 1);
314 void wxPreviewControlBar::OnZoom(wxCommandEvent
& WXUNUSED(event
))
316 int zoom
= GetZoomControl();
317 if (GetPrintPreview())
318 GetPrintPreview()->SetZoom(zoom
);
321 void wxPreviewControlBar::CreateButtons()
323 SetSize(0, 0, 400, 40);
332 wxFont buttonFont(fontSize, wxSWISS, wxNORMAL, wxBOLD);
336 int buttonWidth
= 65;
338 int buttonHeight
= -1;
340 int buttonHeight
= 24;
352 m_closeButton
= new wxButton(this, wxID_PREVIEW_CLOSE
, _("Close"),
353 wxPoint(x
, y
), wxSize(buttonWidth
, buttonHeight
));
355 x
+= gap
+ buttonWidth
;
357 if (m_buttonFlags
& wxPREVIEW_PRINT
)
359 m_printButton
= new wxButton(this, wxID_PREVIEW_PRINT
, _("Print..."), wxPoint(x
, y
),
360 wxSize(buttonWidth
, buttonHeight
));
361 x
+= gap
+ buttonWidth
;
364 if (m_buttonFlags
& wxPREVIEW_PREVIOUS
)
366 m_previousPageButton
= new wxButton(this, wxID_PREVIEW_PREVIOUS
, "<<", wxPoint(x
, y
),
367 wxSize(buttonWidth
, buttonHeight
));
368 x
+= gap
+ buttonWidth
;
371 if (m_buttonFlags
& wxPREVIEW_NEXT
)
373 m_nextPageButton
= new wxButton(this, wxID_PREVIEW_NEXT
, ">>",
374 wxPoint(x
, y
), wxSize(buttonWidth
, buttonHeight
));
375 x
+= gap
+ buttonWidth
;
378 if (m_buttonFlags
& wxPREVIEW_ZOOM
)
380 static const char *choices
[] =
382 "10%", "15%", "20%", "25%", "30%", "35%", "40%", "45%", "50%", "55%",
383 "60%", "65%", "70%", "75%", "80%", "85%", "90%", "95%", "100%", "110%",
384 "120%", "150%", "200%"
387 m_zoomControl
= new wxChoice(this, wxID_PREVIEW_ZOOM
,
388 wxPoint(x
, y
), wxSize(100, -1));
390 // Yes, this look stupid, but this is because gcc gives up otherwise.
391 int n
= WXSIZEOF(choices
);
392 for ( int i
= 0; i
< n
; i
++ )
393 m_zoomControl
->Append(choices
[i
]);
395 SetZoomControl(m_printPreview
->GetZoom());
398 // m_closeButton->SetDefault();
401 void wxPreviewControlBar::SetZoomControl(int zoom
)
404 sprintf(buf
, "%d%%", zoom
);
406 m_zoomControl
->SetStringSelection(buf
);
409 int wxPreviewControlBar::GetZoomControl()
412 if (m_zoomControl
&& (m_zoomControl
->GetStringSelection() != _T("")))
414 wxStrcpy(buf
, m_zoomControl
->GetStringSelection());
415 buf
[wxStrlen(buf
) - 1] = 0;
416 return (int)wxAtoi(buf
);
426 BEGIN_EVENT_TABLE(wxPreviewFrame
, wxFrame
)
427 EVT_CLOSE(wxPreviewFrame::OnCloseWindow
)
430 wxPreviewFrame::wxPreviewFrame(wxPrintPreviewBase
*preview
, wxFrame
*parent
, const wxString
& title
,
431 const wxPoint
& pos
, const wxSize
& size
, long style
, const wxString
& name
):
432 wxFrame(parent
, -1, title
, pos
, size
, style
, name
)
434 m_printPreview
= preview
;
436 m_previewCanvas
= NULL
;
439 wxPreviewFrame::~wxPreviewFrame()
443 void wxPreviewFrame::OnCloseWindow(wxCloseEvent
& WXUNUSED(event
))
447 // Need to delete the printout and the print preview
448 wxPrintout
*printout
= m_printPreview
->GetPrintout();
452 m_printPreview
->SetPrintout(NULL
);
453 m_printPreview
->SetCanvas(NULL
);
454 m_printPreview
->SetFrame(NULL
);
456 delete m_printPreview
;
461 void wxPreviewFrame::Initialize()
468 m_printPreview
->SetCanvas(m_previewCanvas
);
469 m_printPreview
->SetFrame(this);
471 // Set layout constraints here
473 // Control bar constraints
474 wxLayoutConstraints
*c1
= new wxLayoutConstraints
;
476 // m_controlBar->GetSize(&w, &h);
478 #if (defined(__WXMSW__) || defined(__WXGTK__))
484 c1
->left
.SameAs (this, wxLeft
);
485 c1
->top
.SameAs (this, wxTop
);
486 c1
->right
.SameAs (this, wxRight
);
487 c1
->height
.Absolute (h
);
489 m_controlBar
->SetConstraints(c1
);
491 // Canvas constraints
492 wxLayoutConstraints
*c2
= new wxLayoutConstraints
;
494 c2
->left
.SameAs (this, wxLeft
);
495 c2
->top
.Below (m_controlBar
);
496 c2
->right
.SameAs (this, wxRight
);
497 c2
->bottom
.SameAs (this, wxBottom
);
499 m_previewCanvas
->SetConstraints(c2
);
508 void wxPreviewFrame::CreateCanvas()
510 m_previewCanvas
= new wxPreviewCanvas(m_printPreview
, this);
513 void wxPreviewFrame::CreateControlBar()
515 long buttons
= wxPREVIEW_DEFAULT
;
516 if (m_printPreview
->GetPrintoutForPrinting())
517 buttons
|= wxPREVIEW_PRINT
;
519 m_controlBar
= new wxPreviewControlBar(m_printPreview
, buttons
, this, wxPoint(0, 0), wxSize(400, 40));
520 m_controlBar
->CreateButtons();
527 wxPrintPreviewBase::wxPrintPreviewBase(wxPrintout
*printout
,
528 wxPrintout
*printoutForPrinting
,
532 m_printDialogData
= (*data
);
534 Init(printout
, printoutForPrinting
);
537 wxPrintPreviewBase::wxPrintPreviewBase(wxPrintout
*printout
,
538 wxPrintout
*printoutForPrinting
,
539 wxPrintDialogData
*data
)
542 m_printDialogData
= (*data
);
544 Init(printout
, printoutForPrinting
);
547 void wxPrintPreviewBase::Init(wxPrintout
*printout
,
548 wxPrintout
*printoutForPrinting
)
551 m_previewPrintout
= printout
;
552 if (m_previewPrintout
)
553 m_previewPrintout
->SetIsPreview(TRUE
);
555 m_printPrintout
= printoutForPrinting
;
557 m_previewCanvas
= NULL
;
558 m_previewFrame
= NULL
;
559 m_previewBitmap
= NULL
;
567 printout
->OnPreparePrinting();
569 // Get some parameters from the printout, if defined
571 printout
->GetPageInfo(&m_minPage
, &m_maxPage
, &selFrom
, &selTo
);
574 wxPrintPreviewBase::~wxPrintPreviewBase()
576 if (m_previewPrintout
)
577 delete m_previewPrintout
;
579 delete m_previewBitmap
;
581 delete m_printPrintout
;
584 bool wxPrintPreviewBase::SetCurrentPage(int pageNum
)
586 if (m_currentPage
== pageNum
)
589 m_currentPage
= pageNum
;
592 delete m_previewBitmap
;
593 m_previewBitmap
= NULL
;
599 m_previewCanvas
->Refresh();
604 bool wxPrintPreviewBase::PaintPage(wxWindow
*canvas
, wxDC
& dc
)
606 DrawBlankPage(canvas
, dc
);
608 if (!m_previewBitmap
)
609 RenderPage(m_currentPage
);
611 if (!m_previewBitmap
)
617 int canvasWidth
, canvasHeight
;
618 canvas
->GetSize(&canvasWidth
, &canvasHeight
);
620 double zoomScale
= ((float)m_currentZoom
/(float)100);
621 double actualWidth
= (zoomScale
*m_pageWidth
*m_previewScale
);
622 // float actualHeight = (float)(zoomScale*m_pageHeight*m_previewScale);
624 int x
= (int) ((canvasWidth
- actualWidth
)/2.0);
625 if (x
< m_leftMargin
)
630 temp_dc
.SelectObject(*m_previewBitmap
);
632 dc
.Blit(x
, y
, m_previewBitmap
->GetWidth(), m_previewBitmap
->GetHeight(), &temp_dc
, 0, 0);
634 temp_dc
.SelectObject(wxNullBitmap
);
639 bool wxPrintPreviewBase::RenderPage(int pageNum
)
641 int canvasWidth
, canvasHeight
;
643 if (!m_previewCanvas
)
645 wxMessageBox(_("wxPrintPreviewBase::RenderPage: must use wxPrintPreviewBase::SetCanvas to let me know about the canvas!"),
646 _("Print Preview Failure"), wxOK
);
649 m_previewCanvas
->GetSize(&canvasWidth
, &canvasHeight
);
651 double zoomScale
= (m_currentZoom
/100.0);
652 int actualWidth
= (int)(zoomScale
*m_pageWidth
*m_previewScale
);
653 int actualHeight
= (int)(zoomScale
*m_pageHeight
*m_previewScale
);
655 int x
= (int)((canvasWidth
- actualWidth
)/2.0);
656 if (x
< m_leftMargin
)
658 // int y = m_topMargin;
661 if (!m_previewBitmap
)
663 m_previewBitmap
= new wxBitmap((int)actualWidth
, (int)actualHeight
);
664 if (!m_previewBitmap
|| !m_previewBitmap
->Ok())
667 delete m_previewBitmap
;
668 wxMessageBox(_("Sorry, not enough memory to create a preview."), _("Print Preview Failure"), wxOK
);
674 memoryDC
.SelectObject(*m_previewBitmap
);
678 m_previewPrintout
->SetDC(&memoryDC
);
679 m_previewPrintout
->SetPageSizePixels(m_pageWidth
, m_pageHeight
);
681 m_previewPrintout
->OnBeginPrinting();
683 if (!m_previewPrintout
->OnBeginDocument(m_printDialogData
.GetFromPage(), m_printDialogData
.GetToPage()))
685 wxMessageBox(_("Could not start document preview."), _("Print Preview Failure"), wxOK
);
687 memoryDC
.SelectObject(wxNullBitmap
);
689 delete m_previewBitmap
;
693 m_previewPrintout
->OnPrintPage(pageNum
);
694 m_previewPrintout
->OnEndDocument();
695 m_previewPrintout
->OnEndPrinting();
697 m_previewPrintout
->SetDC(NULL
);
699 memoryDC
.SelectObject(wxNullBitmap
);
703 wxSprintf(buf
, _("Page %d of %d"), pageNum
, m_maxPage
);
705 wxSprintf(buf
, _("Page %d"), pageNum
);
708 m_previewFrame
->SetStatusText(buf
);
714 bool wxPrintPreviewBase::DrawBlankPage(wxWindow
*canvas
, wxDC
& dc
)
716 int canvasWidth
, canvasHeight
;
717 canvas
->GetSize(&canvasWidth
, &canvasHeight
);
719 float zoomScale
= (float)((float)m_currentZoom
/(float)100);
720 float actualWidth
= zoomScale
*m_pageWidth
*m_previewScale
;
721 float actualHeight
= zoomScale
*m_pageHeight
*m_previewScale
;
723 float x
= (float)((canvasWidth
- actualWidth
)/2.0);
724 if (x
< m_leftMargin
)
725 x
= (float)m_leftMargin
;
726 float y
= (float)m_topMargin
;
728 // Draw shadow, allowing for 1-pixel border AROUND the actual page
729 int shadowOffset
= 4;
730 dc
.SetPen(*wxBLACK_PEN
);
731 dc
.SetBrush(*wxBLACK_BRUSH
);
733 dc.DrawRectangle((int)(x-1 + shadowOffset), (int)(y-1 + shadowOffset), (int)(actualWidth+2), (int)(actualHeight+2));
735 dc
.DrawRectangle((int)(x
+ shadowOffset
), (int)(y
+ actualHeight
+1), (int)(actualWidth
), shadowOffset
);
736 dc
.DrawRectangle((int)(x
+ actualWidth
), (int)(y
+ shadowOffset
), shadowOffset
, (int)(actualHeight
));
738 // Draw blank page allowing for 1-pixel border AROUND the actual page
739 dc
.SetPen(*wxBLACK_PEN
);
740 dc
.SetBrush(*wxWHITE_BRUSH
);
743 wxRegion update_region = canvas->GetUpdateRegion();
744 wxRect r = update_region.GetBox();
746 printf( "x: %d y: %d w: %d h: %d.\n", (int)r.x, (int)r.y, (int)r.width, (int)r.height );
749 dc
.DrawRectangle((int)(x
-2), (int)(y
-1), (int)(actualWidth
+3), (int)(actualHeight
+2));
754 void wxPrintPreviewBase::SetZoom(int percent
)
756 if (m_currentZoom
== percent
)
759 m_currentZoom
= percent
;
762 delete m_previewBitmap
;
763 m_previewBitmap
= NULL
;
769 RenderPage(m_currentPage
);
770 m_previewCanvas
->Clear();
771 m_previewCanvas
->Refresh();
775 #endif // wxUSE_PRINTING_ARCHITECTURE