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 m_printPreview
->PaintPage(this, dc
);
204 // Responds to colour changes, and passes event on to children.
205 void wxPreviewCanvas::OnSysColourChanged(wxSysColourChangedEvent
& event
)
207 SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_APPWORKSPACE
));
210 // Propagate the event to the non-top-level children
211 wxWindow::OnSysColourChanged(event
);
215 * Preview control bar
218 BEGIN_EVENT_TABLE(wxPreviewControlBar
, wxPanel
)
219 EVT_BUTTON(wxID_PREVIEW_CLOSE
, wxPreviewControlBar::OnWindowClose
)
220 EVT_BUTTON(wxID_PREVIEW_PRINT
, wxPreviewControlBar::OnPrint
)
221 EVT_BUTTON(wxID_PREVIEW_PREVIOUS
, wxPreviewControlBar::OnPreviousButton
)
222 EVT_BUTTON(wxID_PREVIEW_NEXT
, wxPreviewControlBar::OnNextButton
)
223 EVT_CHAR(wxPreviewControlBar::OnChar
)
224 EVT_CHOICE(wxID_PREVIEW_ZOOM
, wxPreviewControlBar::OnZoom
)
225 EVT_PAINT(wxPreviewControlBar::OnPaint
)
228 wxPreviewControlBar::wxPreviewControlBar(wxPrintPreviewBase
*preview
, long buttons
,
229 wxWindow
*parent
, const wxPoint
& pos
, const wxSize
& size
,
230 long style
, const wxString
& name
):
231 wxPanel(parent
, -1, pos
, size
, style
, name
)
233 m_printPreview
= preview
;
234 m_closeButton
= (wxButton
*) NULL
;
235 m_nextPageButton
= (wxButton
*) NULL
;
236 m_previousPageButton
= (wxButton
*) NULL
;
237 m_printButton
= (wxButton
*) NULL
;
238 m_zoomControl
= (wxChoice
*) NULL
;
239 m_buttonFlags
= buttons
;
242 wxPreviewControlBar::~wxPreviewControlBar()
246 void wxPreviewControlBar::OnPaint(wxPaintEvent
& WXUNUSED(event
))
252 dc
.SetPen(*wxBLACK_PEN
);
253 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
254 dc
.DrawLine( 0, h
-1, w
, h
-1 );
257 void wxPreviewControlBar::OnWindowClose(wxCommandEvent
& WXUNUSED(event
))
259 wxPreviewFrame
*frame
= (wxPreviewFrame
*)GetParent();
263 void wxPreviewControlBar::OnPrint(wxCommandEvent
& WXUNUSED(event
))
265 wxPrintPreviewBase
*preview
= GetPrintPreview();
266 preview
->Print(TRUE
);
269 void wxPreviewControlBar::OnChar(wxKeyEvent
&event
)
271 switch(event
.KeyCode())
282 void wxPreviewControlBar::OnNext(void)
284 wxPrintPreviewBase
*preview
= GetPrintPreview();
287 int currentPage
= preview
->GetCurrentPage();
288 if ((preview
->GetMaxPage() > 0) &&
289 (currentPage
< preview
->GetMaxPage()) &&
290 preview
->GetPrintout()->HasPage(currentPage
+ 1))
292 preview
->SetCurrentPage(currentPage
+ 1);
297 void wxPreviewControlBar::OnPrevious(void)
299 wxPrintPreviewBase
*preview
= GetPrintPreview();
302 int currentPage
= preview
->GetCurrentPage();
303 if ((preview
->GetMinPage() > 0) &&
304 (currentPage
> preview
->GetMinPage()) &&
305 preview
->GetPrintout()->HasPage(currentPage
- 1))
307 preview
->SetCurrentPage(currentPage
- 1);
312 void wxPreviewControlBar::OnZoom(wxCommandEvent
& WXUNUSED(event
))
314 int zoom
= GetZoomControl();
315 if (GetPrintPreview())
316 GetPrintPreview()->SetZoom(zoom
);
319 void wxPreviewControlBar::CreateButtons()
321 SetSize(0, 0, 400, 40);
330 wxFont buttonFont(fontSize, wxSWISS, wxNORMAL, wxBOLD);
334 int buttonWidth
= 65;
336 int buttonHeight
= -1;
338 int buttonHeight
= 24;
350 m_closeButton
= new wxButton(this, wxID_PREVIEW_CLOSE
, _("Close"),
351 wxPoint(x
, y
), wxSize(buttonWidth
, buttonHeight
));
353 x
+= gap
+ buttonWidth
;
355 if (m_buttonFlags
& wxPREVIEW_PRINT
)
357 m_printButton
= new wxButton(this, wxID_PREVIEW_PRINT
, _("Print..."), wxPoint(x
, y
),
358 wxSize(buttonWidth
, buttonHeight
));
359 x
+= gap
+ buttonWidth
;
362 if (m_buttonFlags
& wxPREVIEW_PREVIOUS
)
364 m_previousPageButton
= new wxButton(this, wxID_PREVIEW_PREVIOUS
, wxT("<<"), wxPoint(x
, y
),
365 wxSize(buttonWidth
, buttonHeight
));
366 x
+= gap
+ buttonWidth
;
369 if (m_buttonFlags
& wxPREVIEW_NEXT
)
371 m_nextPageButton
= new wxButton(this, wxID_PREVIEW_NEXT
, wxT(">>"),
372 wxPoint(x
, y
), wxSize(buttonWidth
, buttonHeight
));
373 x
+= gap
+ buttonWidth
;
376 if (m_buttonFlags
& wxPREVIEW_ZOOM
)
378 static const char *choices
[] =
380 "10%", "15%", "20%", "25%", "30%", "35%", "40%", "45%", "50%", "55%",
381 "60%", "65%", "70%", "75%", "80%", "85%", "90%", "95%", "100%", "110%",
382 "120%", "150%", "200%"
385 int n
= WXSIZEOF(choices
);
387 wxString
* strings
= new wxString
[n
];
389 for (i
= 0; i
< n
; i
++ )
390 strings
[i
] = choices
[i
];
392 m_zoomControl
= new wxChoice(this, wxID_PREVIEW_ZOOM
,
400 SetZoomControl(m_printPreview
->GetZoom());
403 // m_closeButton->SetDefault();
406 void wxPreviewControlBar::SetZoomControl(int zoom
)
409 sprintf(buf
, "%d%%", zoom
);
410 // Someone is calling methods that do no exist in wxChoice!! So I'll just comment out for VA for now
412 m_zoomControl
->SetStringSelection(buf
);
415 int wxPreviewControlBar::GetZoomControl()
418 if (m_zoomControl
&& (m_zoomControl
->GetStringSelection() != wxT("")))
420 wxStrcpy(buf
, m_zoomControl
->GetStringSelection());
421 buf
[wxStrlen(buf
) - 1] = 0;
422 return (int)wxAtoi(buf
);
432 BEGIN_EVENT_TABLE(wxPreviewFrame
, wxFrame
)
433 EVT_CLOSE(wxPreviewFrame::OnCloseWindow
)
436 wxPreviewFrame::wxPreviewFrame(wxPrintPreviewBase
*preview
, wxFrame
*parent
, const wxString
& title
,
437 const wxPoint
& pos
, const wxSize
& size
, long style
, const wxString
& name
):
438 wxFrame(parent
, -1, title
, pos
, size
, style
, name
)
440 m_printPreview
= preview
;
442 m_previewCanvas
= NULL
;
445 wxPreviewFrame::~wxPreviewFrame()
449 void wxPreviewFrame::OnCloseWindow(wxCloseEvent
& WXUNUSED(event
))
453 // Need to delete the printout and the print preview
454 wxPrintout
*printout
= m_printPreview
->GetPrintout();
458 m_printPreview
->SetPrintout(NULL
);
459 m_printPreview
->SetCanvas(NULL
);
460 m_printPreview
->SetFrame(NULL
);
462 delete m_printPreview
;
467 void wxPreviewFrame::Initialize()
474 m_printPreview
->SetCanvas(m_previewCanvas
);
475 m_printPreview
->SetFrame(this);
477 // Set layout constraints here
479 // Control bar constraints
480 wxLayoutConstraints
*c1
= new wxLayoutConstraints
;
482 // m_controlBar->GetSize(&w, &h);
484 #if (defined(__WXMSW__) || defined(__WXGTK__))
490 c1
->left
.SameAs (this, wxLeft
);
491 c1
->top
.SameAs (this, wxTop
);
492 c1
->right
.SameAs (this, wxRight
);
493 c1
->height
.Absolute (h
);
495 m_controlBar
->SetConstraints(c1
);
497 // Canvas constraints
498 wxLayoutConstraints
*c2
= new wxLayoutConstraints
;
500 c2
->left
.SameAs (this, wxLeft
);
501 c2
->top
.Below (m_controlBar
);
502 c2
->right
.SameAs (this, wxRight
);
503 c2
->bottom
.SameAs (this, wxBottom
);
505 m_previewCanvas
->SetConstraints(c2
);
514 void wxPreviewFrame::CreateCanvas()
516 m_previewCanvas
= new wxPreviewCanvas(m_printPreview
, this);
519 void wxPreviewFrame::CreateControlBar()
521 long buttons
= wxPREVIEW_DEFAULT
;
522 if (m_printPreview
->GetPrintoutForPrinting())
523 buttons
|= wxPREVIEW_PRINT
;
525 m_controlBar
= new wxPreviewControlBar(m_printPreview
, buttons
, this, wxPoint(0, 0), wxSize(400, 40));
526 m_controlBar
->CreateButtons();
533 wxPrintPreviewBase::wxPrintPreviewBase(wxPrintout
*printout
,
534 wxPrintout
*printoutForPrinting
,
538 m_printDialogData
= (*data
);
540 Init(printout
, printoutForPrinting
);
543 wxPrintPreviewBase::wxPrintPreviewBase(wxPrintout
*printout
,
544 wxPrintout
*printoutForPrinting
,
545 wxPrintDialogData
*data
)
548 m_printDialogData
= (*data
);
550 Init(printout
, printoutForPrinting
);
553 void wxPrintPreviewBase::Init(wxPrintout
*printout
,
554 wxPrintout
*printoutForPrinting
)
557 m_previewPrintout
= printout
;
558 if (m_previewPrintout
)
559 m_previewPrintout
->SetIsPreview(TRUE
);
561 m_printPrintout
= printoutForPrinting
;
563 m_previewCanvas
= NULL
;
564 m_previewFrame
= NULL
;
565 m_previewBitmap
= NULL
;
572 m_printingPrepared
= FALSE
;
574 // Too soon! Moved to RenderPage.
575 // printout->OnPreparePrinting();
577 // Get some parameters from the printout, if defined
579 printout
->GetPageInfo(&m_minPage
, &m_maxPage
, &selFrom
, &selTo
);
582 wxPrintPreviewBase::~wxPrintPreviewBase()
584 if (m_previewPrintout
)
585 delete m_previewPrintout
;
587 delete m_previewBitmap
;
589 delete m_printPrintout
;
592 bool wxPrintPreviewBase::SetCurrentPage(int pageNum
)
594 if (m_currentPage
== pageNum
)
597 m_currentPage
= pageNum
;
600 delete m_previewBitmap
;
601 m_previewBitmap
= NULL
;
607 m_previewCanvas
->Refresh();
612 bool wxPrintPreviewBase::PaintPage(wxWindow
*canvas
, wxDC
& dc
)
614 DrawBlankPage(canvas
, dc
);
616 if (!m_previewBitmap
)
617 RenderPage(m_currentPage
);
619 if (!m_previewBitmap
)
625 int canvasWidth
, canvasHeight
;
626 canvas
->GetSize(&canvasWidth
, &canvasHeight
);
628 double zoomScale
= ((float)m_currentZoom
/(float)100);
629 double actualWidth
= (zoomScale
*m_pageWidth
*m_previewScale
);
630 // float actualHeight = (float)(zoomScale*m_pageHeight*m_previewScale);
632 int x
= (int) ((canvasWidth
- actualWidth
)/2.0);
633 if (x
< m_leftMargin
)
638 temp_dc
.SelectObject(*m_previewBitmap
);
640 dc
.Blit(x
, y
, m_previewBitmap
->GetWidth(), m_previewBitmap
->GetHeight(), &temp_dc
, 0, 0);
642 temp_dc
.SelectObject(wxNullBitmap
);
647 bool wxPrintPreviewBase::RenderPage(int pageNum
)
649 int canvasWidth
, canvasHeight
;
651 if (!m_previewCanvas
)
653 wxMessageBox(_("wxPrintPreviewBase::RenderPage: must use wxPrintPreviewBase::SetCanvas to let me know about the canvas!"),
654 _("Print Preview Failure"), wxOK
);
657 m_previewCanvas
->GetSize(&canvasWidth
, &canvasHeight
);
659 double zoomScale
= (m_currentZoom
/100.0);
660 int actualWidth
= (int)(zoomScale
*m_pageWidth
*m_previewScale
);
661 int actualHeight
= (int)(zoomScale
*m_pageHeight
*m_previewScale
);
663 int x
= (int)((canvasWidth
- actualWidth
)/2.0);
664 if (x
< m_leftMargin
)
666 // int y = m_topMargin;
669 if (!m_previewBitmap
)
671 m_previewBitmap
= new wxBitmap((int)actualWidth
, (int)actualHeight
);
672 if (!m_previewBitmap
|| !m_previewBitmap
->Ok())
675 delete m_previewBitmap
;
676 wxMessageBox(_("Sorry, not enough memory to create a preview."), _("Print Preview Failure"), wxOK
);
682 memoryDC
.SelectObject(*m_previewBitmap
);
686 m_previewPrintout
->SetDC(&memoryDC
);
687 m_previewPrintout
->SetPageSizePixels(m_pageWidth
, m_pageHeight
);
689 // Need to delay OnPreparePrinting until here, so we have enough information.
690 if (!m_printingPrepared
)
692 m_previewPrintout
->OnPreparePrinting();
693 m_printingPrepared
= TRUE
;
696 m_previewPrintout
->OnBeginPrinting();
698 if (!m_previewPrintout
->OnBeginDocument(m_printDialogData
.GetFromPage(), m_printDialogData
.GetToPage()))
700 wxMessageBox(_("Could not start document preview."), _("Print Preview Failure"), wxOK
);
702 memoryDC
.SelectObject(wxNullBitmap
);
704 delete m_previewBitmap
;
708 m_previewPrintout
->OnPrintPage(pageNum
);
709 m_previewPrintout
->OnEndDocument();
710 m_previewPrintout
->OnEndPrinting();
712 m_previewPrintout
->SetDC(NULL
);
714 memoryDC
.SelectObject(wxNullBitmap
);
718 wxSprintf(buf
, _("Page %d of %d"), pageNum
, m_maxPage
);
720 wxSprintf(buf
, _("Page %d"), pageNum
);
723 m_previewFrame
->SetStatusText(buf
);
729 bool wxPrintPreviewBase::DrawBlankPage(wxWindow
*canvas
, wxDC
& dc
)
731 int canvasWidth
, canvasHeight
;
732 canvas
->GetSize(&canvasWidth
, &canvasHeight
);
734 float zoomScale
= (float)((float)m_currentZoom
/(float)100);
735 float actualWidth
= zoomScale
*m_pageWidth
*m_previewScale
;
736 float actualHeight
= zoomScale
*m_pageHeight
*m_previewScale
;
738 float x
= (float)((canvasWidth
- actualWidth
)/2.0);
739 if (x
< m_leftMargin
)
740 x
= (float)m_leftMargin
;
741 float y
= (float)m_topMargin
;
743 // Draw shadow, allowing for 1-pixel border AROUND the actual page
744 int shadowOffset
= 4;
745 dc
.SetPen(*wxBLACK_PEN
);
746 dc
.SetBrush(*wxBLACK_BRUSH
);
748 dc.DrawRectangle((int)(x-1 + shadowOffset), (int)(y-1 + shadowOffset), (int)(actualWidth+2), (int)(actualHeight+2));
750 dc
.DrawRectangle((int)(x
+ shadowOffset
), (int)(y
+ actualHeight
+1), (int)(actualWidth
), shadowOffset
);
751 dc
.DrawRectangle((int)(x
+ actualWidth
), (int)(y
+ shadowOffset
), shadowOffset
, (int)(actualHeight
));
753 // Draw blank page allowing for 1-pixel border AROUND the actual page
754 dc
.SetPen(*wxBLACK_PEN
);
755 dc
.SetBrush(*wxWHITE_BRUSH
);
758 wxRegion update_region = canvas->GetUpdateRegion();
759 wxRect r = update_region.GetBox();
761 printf( "x: %d y: %d w: %d h: %d.\n", (int)r.x, (int)r.y, (int)r.width, (int)r.height );
764 dc
.DrawRectangle((int)(x
-2), (int)(y
-1), (int)(actualWidth
+3), (int)(actualHeight
+2));
769 void wxPrintPreviewBase::SetZoom(int percent
)
771 if (m_currentZoom
== percent
)
774 m_currentZoom
= percent
;
777 delete m_previewBitmap
;
778 m_previewBitmap
= NULL
;
783 RenderPage(m_currentPage
);
784 ((wxScrolledWindow
*) m_previewCanvas
)->Scroll(0, 0);
785 m_previewCanvas
->Clear();
786 m_previewCanvas
->Refresh();
790 #endif // wxUSE_PRINTING_ARCHITECTURE