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::OnPrevious
)
224 EVT_BUTTON(wxID_PREVIEW_NEXT
, wxPreviewControlBar::OnNext
)
225 EVT_CHOICE(wxID_PREVIEW_ZOOM
, wxPreviewControlBar::OnZoom
)
226 EVT_PAINT(wxPreviewControlBar::OnPaint
)
229 wxPreviewControlBar::wxPreviewControlBar(wxPrintPreviewBase
*preview
, long buttons
,
230 wxWindow
*parent
, const wxPoint
& pos
, const wxSize
& size
,
231 long style
, const wxString
& name
):
232 wxPanel(parent
, -1, pos
, size
, style
, name
)
234 m_printPreview
= preview
;
235 m_closeButton
= (wxButton
*) NULL
;
236 m_nextPageButton
= (wxButton
*) NULL
;
237 m_previousPageButton
= (wxButton
*) NULL
;
238 m_printButton
= (wxButton
*) NULL
;
239 m_zoomControl
= (wxChoice
*) NULL
;
240 m_buttonFlags
= buttons
;
243 wxPreviewControlBar::~wxPreviewControlBar()
247 void wxPreviewControlBar::OnPaint(wxPaintEvent
& WXUNUSED(event
))
253 dc
.SetPen(*wxBLACK_PEN
);
254 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
255 dc
.DrawLine( 0, h
-1, w
, h
-1 );
258 void wxPreviewControlBar::OnWindowClose(wxCommandEvent
& WXUNUSED(event
))
260 wxPreviewFrame
*frame
= (wxPreviewFrame
*)GetParent();
264 void wxPreviewControlBar::OnPrint(wxCommandEvent
& WXUNUSED(event
))
266 wxPrintPreviewBase
*preview
= GetPrintPreview();
267 preview
->Print(TRUE
);
270 void wxPreviewControlBar::OnNext(wxCommandEvent
& WXUNUSED(event
))
272 wxPrintPreviewBase
*preview
= GetPrintPreview();
275 int currentPage
= preview
->GetCurrentPage();
276 if ((preview
->GetMaxPage() > 0) &&
277 (currentPage
< preview
->GetMaxPage()) &&
278 preview
->GetPrintout()->HasPage(currentPage
+ 1))
280 preview
->SetCurrentPage(currentPage
+ 1);
285 void wxPreviewControlBar::OnPrevious(wxCommandEvent
& WXUNUSED(event
))
287 wxPrintPreviewBase
*preview
= GetPrintPreview();
290 int currentPage
= preview
->GetCurrentPage();
291 if ((preview
->GetMinPage() > 0) &&
292 (currentPage
> preview
->GetMinPage()) &&
293 preview
->GetPrintout()->HasPage(currentPage
- 1))
295 preview
->SetCurrentPage(currentPage
- 1);
300 void wxPreviewControlBar::OnZoom(wxCommandEvent
& WXUNUSED(event
))
302 int zoom
= GetZoomControl();
303 if (GetPrintPreview())
304 GetPrintPreview()->SetZoom(zoom
);
307 void wxPreviewControlBar::CreateButtons()
309 SetSize(0, 0, 400, 40);
318 wxFont buttonFont(fontSize, wxSWISS, wxNORMAL, wxBOLD);
322 int buttonWidth
= 65;
324 int buttonHeight
= -1;
326 int buttonHeight
= 24;
338 m_closeButton
= new wxButton(this, wxID_PREVIEW_CLOSE
, _("Close"),
339 wxPoint(x
, y
), wxSize(buttonWidth
, buttonHeight
));
341 x
+= gap
+ buttonWidth
;
343 if (m_buttonFlags
& wxPREVIEW_PRINT
)
345 m_printButton
= new wxButton(this, wxID_PREVIEW_PRINT
, _("Print..."), wxPoint(x
, y
),
346 wxSize(buttonWidth
, buttonHeight
));
347 x
+= gap
+ buttonWidth
;
350 if (m_buttonFlags
& wxPREVIEW_PREVIOUS
)
352 m_previousPageButton
= new wxButton(this, wxID_PREVIEW_PREVIOUS
, "<<", wxPoint(x
, y
),
353 wxSize(buttonWidth
, buttonHeight
));
354 x
+= gap
+ buttonWidth
;
357 if (m_buttonFlags
& wxPREVIEW_NEXT
)
359 m_nextPageButton
= new wxButton(this, wxID_PREVIEW_NEXT
, ">>",
360 wxPoint(x
, y
), wxSize(buttonWidth
, buttonHeight
));
361 x
+= gap
+ buttonWidth
;
364 if (m_buttonFlags
& wxPREVIEW_ZOOM
)
366 static const char *choices
[] =
368 "10%", "15%", "20%", "25%", "30%", "35%", "40%", "45%", "50%", "55%",
369 "60%", "65%", "70%", "75%", "80%", "85%", "90%", "95%", "100%", "110%",
370 "120%", "150%", "200%"
373 m_zoomControl
= new wxChoice(this, wxID_PREVIEW_ZOOM
,
374 wxPoint(x
, y
), wxSize(100, -1));
376 // Yes, this look stupid, but this is because gcc gives up otherwise.
377 int n
= WXSIZEOF(choices
);
378 for ( int i
= 0; i
< n
; i
++ )
379 m_zoomControl
->Append(choices
[i
]);
381 SetZoomControl(m_printPreview
->GetZoom());
384 // m_closeButton->SetDefault();
387 void wxPreviewControlBar::SetZoomControl(int zoom
)
390 sprintf(buf
, "%d%%", zoom
);
392 m_zoomControl
->SetStringSelection(buf
);
395 int wxPreviewControlBar::GetZoomControl()
398 if (m_zoomControl
&& (m_zoomControl
->GetStringSelection() != _T("")))
400 wxStrcpy(buf
, m_zoomControl
->GetStringSelection());
401 buf
[wxStrlen(buf
) - 1] = 0;
402 return (int)wxAtoi(buf
);
412 BEGIN_EVENT_TABLE(wxPreviewFrame
, wxFrame
)
413 EVT_CLOSE(wxPreviewFrame::OnCloseWindow
)
416 wxPreviewFrame::wxPreviewFrame(wxPrintPreviewBase
*preview
, wxFrame
*parent
, const wxString
& title
,
417 const wxPoint
& pos
, const wxSize
& size
, long style
, const wxString
& name
):
418 wxFrame(parent
, -1, title
, pos
, size
, style
, name
)
420 m_printPreview
= preview
;
422 m_previewCanvas
= NULL
;
425 wxPreviewFrame::~wxPreviewFrame()
429 void wxPreviewFrame::OnCloseWindow(wxCloseEvent
& WXUNUSED(event
))
433 // Need to delete the printout and the print preview
434 wxPrintout
*printout
= m_printPreview
->GetPrintout();
438 m_printPreview
->SetPrintout(NULL
);
439 m_printPreview
->SetCanvas(NULL
);
440 m_printPreview
->SetFrame(NULL
);
442 delete m_printPreview
;
447 void wxPreviewFrame::Initialize()
454 m_printPreview
->SetCanvas(m_previewCanvas
);
455 m_printPreview
->SetFrame(this);
457 // Set layout constraints here
459 // Control bar constraints
460 wxLayoutConstraints
*c1
= new wxLayoutConstraints
;
462 // m_controlBar->GetSize(&w, &h);
464 #if (defined(__WXMSW__) || defined(__WXGTK__))
470 c1
->left
.SameAs (this, wxLeft
);
471 c1
->top
.SameAs (this, wxTop
);
472 c1
->right
.SameAs (this, wxRight
);
473 c1
->height
.Absolute (h
);
475 m_controlBar
->SetConstraints(c1
);
477 // Canvas constraints
478 wxLayoutConstraints
*c2
= new wxLayoutConstraints
;
480 c2
->left
.SameAs (this, wxLeft
);
481 c2
->top
.Below (m_controlBar
);
482 c2
->right
.SameAs (this, wxRight
);
483 c2
->bottom
.SameAs (this, wxBottom
);
485 m_previewCanvas
->SetConstraints(c2
);
494 void wxPreviewFrame::CreateCanvas()
496 m_previewCanvas
= new wxPreviewCanvas(m_printPreview
, this);
499 void wxPreviewFrame::CreateControlBar()
501 long buttons
= wxPREVIEW_DEFAULT
;
502 if (m_printPreview
->GetPrintoutForPrinting())
503 buttons
|= wxPREVIEW_PRINT
;
505 m_controlBar
= new wxPreviewControlBar(m_printPreview
, buttons
, this, wxPoint(0, 0), wxSize(400, 40));
506 m_controlBar
->CreateButtons();
513 wxPrintPreviewBase::wxPrintPreviewBase(wxPrintout
*printout
,
514 wxPrintout
*printoutForPrinting
,
518 m_printDialogData
= (*data
);
520 Init(printout
, printoutForPrinting
);
523 wxPrintPreviewBase::wxPrintPreviewBase(wxPrintout
*printout
,
524 wxPrintout
*printoutForPrinting
,
525 wxPrintDialogData
*data
)
528 m_printDialogData
= (*data
);
530 Init(printout
, printoutForPrinting
);
533 void wxPrintPreviewBase::Init(wxPrintout
*printout
,
534 wxPrintout
*printoutForPrinting
)
537 m_previewPrintout
= printout
;
538 if (m_previewPrintout
)
539 m_previewPrintout
->SetIsPreview(TRUE
);
541 m_printPrintout
= printoutForPrinting
;
543 m_previewCanvas
= NULL
;
544 m_previewFrame
= NULL
;
545 m_previewBitmap
= NULL
;
553 printout
->OnPreparePrinting();
555 // Get some parameters from the printout, if defined
557 printout
->GetPageInfo(&m_minPage
, &m_maxPage
, &selFrom
, &selTo
);
560 wxPrintPreviewBase::~wxPrintPreviewBase()
562 if (m_previewPrintout
)
563 delete m_previewPrintout
;
565 delete m_previewBitmap
;
567 delete m_printPrintout
;
570 bool wxPrintPreviewBase::SetCurrentPage(int pageNum
)
572 if (m_currentPage
== pageNum
)
575 m_currentPage
= pageNum
;
578 delete m_previewBitmap
;
579 m_previewBitmap
= NULL
;
585 m_previewCanvas
->Refresh();
590 bool wxPrintPreviewBase::PaintPage(wxWindow
*canvas
, wxDC
& dc
)
592 DrawBlankPage(canvas
, dc
);
594 if (!m_previewBitmap
)
595 RenderPage(m_currentPage
);
597 if (!m_previewBitmap
)
603 int canvasWidth
, canvasHeight
;
604 canvas
->GetSize(&canvasWidth
, &canvasHeight
);
606 double zoomScale
= ((float)m_currentZoom
/(float)100);
607 double actualWidth
= (zoomScale
*m_pageWidth
*m_previewScale
);
608 // float actualHeight = (float)(zoomScale*m_pageHeight*m_previewScale);
610 int x
= (int) ((canvasWidth
- actualWidth
)/2.0);
611 if (x
< m_leftMargin
)
616 temp_dc
.SelectObject(*m_previewBitmap
);
618 dc
.Blit(x
, y
, m_previewBitmap
->GetWidth(), m_previewBitmap
->GetHeight(), &temp_dc
, 0, 0);
620 temp_dc
.SelectObject(wxNullBitmap
);
625 bool wxPrintPreviewBase::RenderPage(int pageNum
)
627 int canvasWidth
, canvasHeight
;
629 if (!m_previewCanvas
)
631 wxMessageBox(_("wxPrintPreviewBase::RenderPage: must use wxPrintPreviewBase::SetCanvas to let me know about the canvas!"),
632 _("Print Preview Failure"), wxOK
);
635 m_previewCanvas
->GetSize(&canvasWidth
, &canvasHeight
);
637 double zoomScale
= (m_currentZoom
/100.0);
638 int actualWidth
= (int)(zoomScale
*m_pageWidth
*m_previewScale
);
639 int actualHeight
= (int)(zoomScale
*m_pageHeight
*m_previewScale
);
641 int x
= (int)((canvasWidth
- actualWidth
)/2.0);
642 if (x
< m_leftMargin
)
644 // int y = m_topMargin;
647 if (!m_previewBitmap
)
649 m_previewBitmap
= new wxBitmap((int)actualWidth
, (int)actualHeight
);
650 if (!m_previewBitmap
|| !m_previewBitmap
->Ok())
653 delete m_previewBitmap
;
654 wxMessageBox(_("Sorry, not enough memory to create a preview."), _("Print Preview Failure"), wxOK
);
660 memoryDC
.SelectObject(*m_previewBitmap
);
664 m_previewPrintout
->SetDC(&memoryDC
);
665 m_previewPrintout
->SetPageSizePixels(m_pageWidth
, m_pageHeight
);
667 m_previewPrintout
->OnBeginPrinting();
669 if (!m_previewPrintout
->OnBeginDocument(m_printDialogData
.GetFromPage(), m_printDialogData
.GetToPage()))
671 wxMessageBox(_("Could not start document preview."), _("Print Preview Failure"), wxOK
);
673 memoryDC
.SelectObject(wxNullBitmap
);
675 delete m_previewBitmap
;
679 m_previewPrintout
->OnPrintPage(pageNum
);
680 m_previewPrintout
->OnEndDocument();
681 m_previewPrintout
->OnEndPrinting();
683 m_previewPrintout
->SetDC(NULL
);
685 memoryDC
.SelectObject(wxNullBitmap
);
689 wxSprintf(buf
, _("Page %d of %d"), pageNum
, m_maxPage
);
691 wxSprintf(buf
, _("Page %d"), pageNum
);
694 m_previewFrame
->SetStatusText(buf
);
700 bool wxPrintPreviewBase::DrawBlankPage(wxWindow
*canvas
, wxDC
& dc
)
702 int canvasWidth
, canvasHeight
;
703 canvas
->GetSize(&canvasWidth
, &canvasHeight
);
705 float zoomScale
= (float)((float)m_currentZoom
/(float)100);
706 float actualWidth
= zoomScale
*m_pageWidth
*m_previewScale
;
707 float actualHeight
= zoomScale
*m_pageHeight
*m_previewScale
;
709 float x
= (float)((canvasWidth
- actualWidth
)/2.0);
710 if (x
< m_leftMargin
)
711 x
= (float)m_leftMargin
;
712 float y
= (float)m_topMargin
;
714 // Draw shadow, allowing for 1-pixel border AROUND the actual page
715 int shadowOffset
= 4;
716 dc
.SetPen(*wxBLACK_PEN
);
717 dc
.SetBrush(*wxBLACK_BRUSH
);
719 dc.DrawRectangle((int)(x-1 + shadowOffset), (int)(y-1 + shadowOffset), (int)(actualWidth+2), (int)(actualHeight+2));
721 dc
.DrawRectangle((int)(x
+ shadowOffset
), (int)(y
+ actualHeight
+1), (int)(actualWidth
), shadowOffset
);
722 dc
.DrawRectangle((int)(x
+ actualWidth
), (int)(y
+ shadowOffset
), shadowOffset
, (int)(actualHeight
));
724 // Draw blank page allowing for 1-pixel border AROUND the actual page
725 dc
.SetPen(*wxBLACK_PEN
);
726 dc
.SetBrush(*wxWHITE_BRUSH
);
729 wxRegion update_region = canvas->GetUpdateRegion();
730 wxRect r = update_region.GetBox();
732 printf( "x: %d y: %d w: %d h: %d.\n", (int)r.x, (int)r.y, (int)r.width, (int)r.height );
735 dc
.DrawRectangle((int)(x
-2), (int)(y
-1), (int)(actualWidth
+3), (int)(actualHeight
+2));
740 void wxPrintPreviewBase::SetZoom(int percent
)
742 if (m_currentZoom
== percent
)
745 m_currentZoom
= percent
;
748 delete m_previewBitmap
;
749 m_previewBitmap
= NULL
;
755 RenderPage(m_currentPage
);
756 m_previewCanvas
->Clear();
757 m_previewCanvas
->Refresh();
761 #endif // wxUSE_PRINTING_ARCHITECTURE