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"
23 // #define __GOOD_COMPILER__
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"
52 // Clash with Windows header files
61 #if !defined(APIENTRY) // NT defines APIENTRY, 3.x not
62 #define APIENTRY FAR PASCAL
68 #define _EXPORT _export
69 typedef signed short int SHORT
;
72 #if !defined(__WIN32__) // 3.x uses FARPROC for dialogs
73 #define DLGPROC FARPROC
76 LONG APIENTRY _EXPORT
wxAbortProc(HDC hPr
, int Code
);
80 #if !USE_SHARED_LIBRARY
81 IMPLEMENT_CLASS(wxPrinterBase
, wxObject
)
82 IMPLEMENT_ABSTRACT_CLASS(wxPrintout
, wxObject
)
83 IMPLEMENT_CLASS(wxPreviewCanvas
, wxWindow
)
84 IMPLEMENT_CLASS(wxPreviewControlBar
, wxWindow
)
85 IMPLEMENT_CLASS(wxPreviewFrame
, wxFrame
)
86 IMPLEMENT_CLASS(wxPrintPreviewBase
, wxObject
)
88 BEGIN_EVENT_TABLE(wxPrintAbortDialog
, wxDialog
)
89 EVT_BUTTON(wxID_CANCEL
, wxPrintAbortDialog::OnCancel
)
92 BEGIN_EVENT_TABLE(wxPreviewCanvas
, wxScrolledWindow
)
93 EVT_PAINT(wxPreviewCanvas::OnPaint
)
94 EVT_SYS_COLOUR_CHANGED(wxPreviewCanvas::OnSysColourChanged
)
102 wxPrinterBase::wxPrinterBase(wxPrintData
*data
)
104 currentPrintout
= NULL
;
111 wxWindow
*wxPrinterBase::abortWindow
= NULL
;
112 bool wxPrinterBase::abortIt
= FALSE
;
114 wxPrinterBase::~wxPrinterBase(void)
118 void wxPrintAbortDialog::OnCancel(wxCommandEvent
& WXUNUSED(event
))
120 wxPrinterBase::abortIt
= TRUE
;
121 wxPrinterBase::abortWindow
->Show(FALSE
);
122 wxPrinterBase::abortWindow
->Close(TRUE
);
123 wxPrinterBase::abortWindow
= NULL
;
126 wxWindow
*wxPrinterBase::CreateAbortWindow(wxWindow
*parent
, wxPrintout
*WXUNUSED(printout
))
128 wxPrintAbortDialog
*dialog
= new wxPrintAbortDialog(parent
, _("Printing"), wxPoint(0, 0), wxSize(400, 400));
129 (void) new wxStaticText(dialog
, -1, _("Please wait..."), wxPoint(5, 5));
131 wxButton
*button
= new wxButton(dialog
, wxID_CANCEL
, _("Cancel"), wxPoint(5, 30));
134 button
->Centre(wxHORIZONTAL
);
140 void wxPrinterBase::ReportError(wxWindow
*parent
, wxPrintout
*WXUNUSED(printout
), char *message
)
142 wxMessageBox(message
, "Printing Error", wxOK
, parent
);
149 wxPrintout::wxPrintout(char *title
)
151 printoutTitle
= title
? copystring(title
) : (char*)NULL
;
156 pageHeightPixels
= 0;
164 wxPrintout::~wxPrintout(void)
167 delete[] printoutTitle
;
170 bool wxPrintout::OnBeginDocument(int WXUNUSED(startPage
), int WXUNUSED(endPage
))
172 return GetDC()->StartDoc("Printing");
175 void wxPrintout::OnEndDocument(void)
180 void wxPrintout::OnBeginPrinting(void)
184 void wxPrintout::OnEndPrinting(void)
188 bool wxPrintout::HasPage(int page
)
193 void wxPrintout::GetPageInfo(int *minPage
, int *maxPage
, int *fromPage
, int *toPage
)
205 wxPreviewCanvas::wxPreviewCanvas(wxPrintPreviewBase
*preview
, wxWindow
*parent
,
206 const wxPoint
& pos
, const wxSize
& size
, long style
, const wxString
& name
):
207 wxScrolledWindow(parent
, -1, pos
, size
, style
, name
)
209 printPreview
= preview
;
210 SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_APPWORKSPACE
));
212 SetScrollbars(40, 40, 100, 100);
215 wxPreviewCanvas::~wxPreviewCanvas(void)
219 void wxPreviewCanvas::OnPaint(wxPaintEvent
& WXUNUSED(event
))
225 printPreview
->PaintPage(this, dc
);
229 // Responds to colour changes, and passes event on to children.
230 void wxPreviewCanvas::OnSysColourChanged(wxSysColourChangedEvent
& event
)
232 SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_APPWORKSPACE
));
235 // Propagate the event to the non-top-level children
236 wxWindow::OnSysColourChanged(event
);
240 * Preview control bar
243 BEGIN_EVENT_TABLE(wxPreviewControlBar
, wxPanel
)
244 EVT_BUTTON(wxID_PREVIEW_CLOSE
, wxPreviewControlBar::OnClose
)
245 EVT_BUTTON(wxID_PREVIEW_PRINT
, wxPreviewControlBar::OnPrint
)
246 EVT_BUTTON(wxID_PREVIEW_PREVIOUS
, wxPreviewControlBar::OnPrevious
)
247 EVT_BUTTON(wxID_PREVIEW_NEXT
, wxPreviewControlBar::OnNext
)
248 EVT_CHOICE(wxID_PREVIEW_ZOOM
, wxPreviewControlBar::OnZoom
)
249 EVT_PAINT(wxPreviewControlBar::OnPaint
)
252 wxPreviewControlBar::wxPreviewControlBar(wxPrintPreviewBase
*preview
, long buttons
,
253 wxWindow
*parent
, const wxPoint
& pos
, const wxSize
& size
,
254 long style
, const wxString
& name
):
255 wxPanel(parent
, -1, pos
, size
, style
, name
)
257 printPreview
= preview
;
259 nextPageButton
= NULL
;
260 previousPageButton
= NULL
;
263 buttonFlags
= buttons
;
266 wxFont
*wxPreviewControlBar::buttonFont
= NULL
;
268 wxPreviewControlBar::~wxPreviewControlBar(void)
272 void wxPreviewControlBar::OnPaint(wxPaintEvent
& WXUNUSED(event
))
278 dc
.SetPen(*wxBLACK_PEN
);
279 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
280 dc
.DrawLine( 0, h
-1, w
, h
-1 );
283 void wxPreviewControlBar::OnClose(wxCommandEvent
& WXUNUSED(event
))
285 wxPreviewFrame
*frame
= (wxPreviewFrame
*)GetParent();
289 void wxPreviewControlBar::OnPrint(wxCommandEvent
& WXUNUSED(event
))
291 wxPrintPreviewBase
*preview
= GetPrintPreview();
292 preview
->Print(TRUE
);
295 void wxPreviewControlBar::OnNext(wxCommandEvent
& WXUNUSED(event
))
297 wxPrintPreviewBase
*preview
= GetPrintPreview();
300 int currentPage
= preview
->GetCurrentPage();
301 if ((preview
->GetMaxPage() > 0) &&
302 (currentPage
< preview
->GetMaxPage()) &&
303 preview
->GetPrintout()->HasPage(currentPage
+ 1))
305 preview
->SetCurrentPage(currentPage
+ 1);
310 void wxPreviewControlBar::OnPrevious(wxCommandEvent
& WXUNUSED(event
))
312 wxPrintPreviewBase
*preview
= GetPrintPreview();
315 int currentPage
= preview
->GetCurrentPage();
316 if ((preview
->GetMinPage() > 0) &&
317 (currentPage
> preview
->GetMinPage()) &&
318 preview
->GetPrintout()->HasPage(currentPage
- 1))
320 preview
->SetCurrentPage(currentPage
- 1);
325 void wxPreviewControlBar::OnZoom(wxCommandEvent
& WXUNUSED(event
))
327 int zoom
= GetZoomControl();
328 if (GetPrintPreview())
329 GetPrintPreview()->SetZoom(zoom
);
332 void wxPreviewControlBar::CreateButtons(void)
334 #ifdef __GOOD_COMPILER__ // Robert Roebling
336 SetSize(0, 0, 400, 40);
345 buttonFont
= wxTheFontList
->FindOrCreateFont(fontSize
, wxSWISS
, wxNORMAL
, wxBOLD
);
346 SetButtonFont(*buttonFont
);
348 int buttonWidth
= 65;
349 int buttonHeight
= 24;
355 closeButton
= new wxButton(this, wxID_PREVIEW_CLOSE
, "Close",
356 wxPoint(x
, y
), wxSize(buttonWidth
, buttonHeight
));
358 x
+= gap
+ buttonWidth
;
360 if (buttonFlags
& wxPREVIEW_PRINT
)
362 printButton
= new wxButton(this, wxID_PREVIEW_PRINT
, "Print...", wxPoint(x
, y
),
363 wxSize(buttonWidth
, buttonHeight
));
364 x
+= gap
+ buttonWidth
;
367 if (buttonFlags
& wxPREVIEW_PREVIOUS
)
369 previousPageButton
= new wxButton(this, wxID_PREVIEW_PREVIOUS
, "<<", wxPoint(x
, y
),
370 wxSize(buttonWidth
, buttonHeight
));
371 x
+= gap
+ buttonWidth
;
374 if (buttonFlags
& wxPREVIEW_NEXT
)
376 nextPageButton
= new wxButton(this, wxID_PREVIEW_NEXT
, ">>",
377 wxPoint(x
, y
), wxSize(buttonWidth
, buttonHeight
));
378 x
+= gap
+ buttonWidth
;
381 static wxString choices
[] = { "10%", "20%", "25%", "30%", "35%", "40%", "45%", "50%", "55%", "60%",
382 "65%", "70%", "75%", "80%", "85%", "90%", "95%", "100%", "110%", "120%", "150%", "200%" };
384 if (buttonFlags
& wxPREVIEW_ZOOM
)
386 zoomControl
= new wxChoice(this, wxID_PREVIEW_ZOOM
, wxPoint(x
, y
),
387 wxSize(100, -1), n
, (wxString
*)choices
);
388 SetZoomControl(printPreview
->GetZoom());
391 closeButton
->SetDefault();
396 void wxPreviewControlBar::SetZoomControl(int zoom
)
398 #ifdef __GOOD_COMPILER__ // Robert Roebling
400 sprintf(buf
, "%d%%", zoom
);
402 zoomControl
->SetStringSelection(buf
);
406 int wxPreviewControlBar::GetZoomControl(void)
408 #ifdef __GOOD_COMPILER__ // Robert Roebling
410 if (zoomControl
&& zoomControl
->GetStringSelection())
412 strcpy(buf
, zoomControl
->GetStringSelection());
413 buf
[strlen(buf
) - 1] = 0;
414 return (int)atoi(buf
);
425 wxPreviewFrame::wxPreviewFrame(wxPrintPreviewBase
*preview
, wxFrame
*parent
, const wxString
& title
,
426 const wxPoint
& pos
, const wxSize
& size
, long style
, const wxString
& name
):
427 wxFrame(parent
, -1, title
, pos
, size
, style
, name
)
429 #ifdef __GOOD_COMPILER__ // Robert Roebling
431 printPreview
= preview
;
433 previewCanvas
= NULL
;
437 wxPreviewFrame::~wxPreviewFrame(void)
441 bool wxPreviewFrame::OnClose(void)
443 #ifdef __GOOD_COMPILER__ // Robert Roebling
447 // Need to delete the printout and the print preview
448 wxPrintout
*printout
= printPreview
->GetPrintout();
452 printPreview
->SetPrintout(NULL
);
453 printPreview
->SetCanvas(NULL
);
454 printPreview
->SetFrame(NULL
);
461 void wxPreviewFrame::Initialize(void)
464 #ifdef __GOOD_COMPILER__ // Robert Roebling
471 printPreview
->SetCanvas(previewCanvas
);
472 printPreview
->SetFrame(this);
474 // Set layout constraints here
476 // Control bar constraints
477 wxLayoutConstraints
*c1
= new wxLayoutConstraints
;
479 // controlBar->GetSize(&w, &h);
487 c1
->left
.SameAs (this, wxLeft
);
488 c1
->top
.SameAs (this, wxTop
);
489 c1
->right
.SameAs (this, wxRight
);
490 c1
->height
.Absolute (h
);
492 controlBar
->SetConstraints(c1
);
494 // Canvas constraints
495 wxLayoutConstraints
*c2
= new wxLayoutConstraints
;
497 c2
->left
.SameAs (this, wxLeft
);
498 c2
->top
.Below (controlBar
);
499 c2
->right
.SameAs (this, wxRight
);
500 c2
->bottom
.SameAs (this, wxBottom
);
502 previewCanvas
->SetConstraints(c2
);
513 void wxPreviewFrame::CreateCanvas(void)
515 #ifdef __GOOD_COMPILER__ // Robert Roebling
517 previewCanvas
= new wxPreviewCanvas(printPreview
, this);
522 void wxPreviewFrame::CreateControlBar(void)
524 #ifdef __GOOD_COMPILER__ // Robert Roebling
526 long buttons
= wxPREVIEW_DEFAULT
;
527 if (printPreview
->GetPrintoutForPrinting())
528 buttons
|= wxPREVIEW_PRINT
;
530 controlBar
= new wxPreviewControlBar(printPreview
, buttons
, this, wxPoint(0, 0), wxSize(400, 40));
531 controlBar
->CreateButtons();
539 wxPrintPreviewBase::wxPrintPreviewBase(wxPrintout
*printout
, wxPrintout
*printoutForPrinting
, wxPrintData
*data
)
542 #ifdef __GOOD_COMPILER__ // Robert Roebling
545 previewPrintout
= printout
;
547 previewPrintout
->SetIsPreview(TRUE
);
549 printPrintout
= printoutForPrinting
;
553 previewCanvas
= NULL
;
555 previewBitmap
= NULL
;
563 printout
->OnPreparePrinting();
565 // Get some parameters from the printout, if defined
567 printout
->GetPageInfo(&minPage
, &maxPage
, &selFrom
, &selTo
);
572 wxPrintPreviewBase::~wxPrintPreviewBase(void)
574 #ifdef __GOOD_COMPILER__ // Robert Roebling
577 delete previewPrintout
;
579 delete previewBitmap
;
581 delete printPrintout
;
586 bool wxPrintPreviewBase::SetCurrentPage(int pageNum
)
588 #ifdef __GOOD_COMPILER__ // Robert Roebling
589 if (currentPage
== pageNum
)
592 currentPage
= pageNum
;
595 delete previewBitmap
;
596 previewBitmap
= NULL
;
602 previewCanvas
->Refresh();
609 bool wxPrintPreviewBase::PaintPage(wxWindow
*canvas
, wxDC
& dc
)
612 #ifdef __GOOD_COMPILER__ // Robert Roebling
614 DrawBlankPage(canvas
, dc
);
617 RenderPage(currentPage
);
625 int canvasWidth
, canvasHeight
;
626 canvas
->GetSize(&canvasWidth
, &canvasHeight
);
628 float zoomScale
= (float)((float)currentZoom
/(float)100);
629 float actualWidth
= (float)(zoomScale
*pageWidth
*previewScale
);
630 // float actualHeight = (float)(zoomScale*pageHeight*previewScale);
632 float x
= (float)((canvasWidth
- actualWidth
)/2.0);
634 x
= (float)leftMargin
;
635 float y
= (float)topMargin
;
638 temp_dc
.SelectObject(*previewBitmap
);
640 dc
.Blit((float)x
, (float)y
, (float)previewBitmap
->GetWidth(), (float)previewBitmap
->GetHeight(), &temp_dc
, (float)0, (float)0);
642 temp_dc
.SelectObject(wxNullBitmap
);
649 bool wxPrintPreviewBase::RenderPage(int pageNum
)
651 int canvasWidth
, canvasHeight
;
653 #ifdef __GOOD_COMPILER__ // Robert Roebling
657 wxMessageBox("wxPrintPreviewBase::RenderPage: must use wxPrintPreviewBase::SetCanvas to let me know about the canvas!",
658 "Print Preview Failure", wxOK
);
661 previewCanvas
->GetSize(&canvasWidth
, &canvasHeight
);
663 float zoomScale
= (float)((float)currentZoom
/(float)100);
664 float actualWidth
= (float)(zoomScale
*pageWidth
*previewScale
);
665 float actualHeight
= (float)(zoomScale
*pageHeight
*previewScale
);
667 float x
= (float)((canvasWidth
- actualWidth
)/2.0);
669 x
= (float)leftMargin
;
670 // float y = topMargin;
675 previewBitmap
= new wxBitmap((int)actualWidth
, (int)actualHeight
);
676 if (!previewBitmap
|| !previewBitmap
->Ok())
679 delete previewBitmap
;
680 wxMessageBox("Sorry, not enough memory to create a preview.", "Print Preview Failure", wxOK
);
686 memoryDC
.SelectObject(*previewBitmap
);
690 previewPrintout
->SetDC(&memoryDC
);
691 previewPrintout
->SetPageSizePixels(pageWidth
, pageHeight
);
693 previewPrintout
->OnBeginPrinting();
696 if (!previewPrintout
->OnBeginDocument(printData
.GetFromPage(), printData
.GetToPage()))
698 wxMessageBox("Could not start document preview.", "Print Preview Failure", wxOK
);
700 memoryDC
.SelectObject(wxNullBitmap
);
702 delete previewBitmap
;
706 previewPrintout
->OnPrintPage(pageNum
);
707 previewPrintout
->OnEndDocument();
708 previewPrintout
->OnEndPrinting();
710 previewPrintout
->SetDC(NULL
);
712 memoryDC
.SelectObject(wxNullBitmap
);
717 sprintf(buf
, "Page %d of %d", pageNum
, maxPage
);
719 sprintf(buf
, "Page %d", pageNum
);
722 previewFrame
->SetStatusText(buf
);
728 bool wxPrintPreviewBase::DrawBlankPage(wxWindow
*canvas
, wxDC
& dc
)
731 #ifdef __GOOD_COMPILER__ // Robert Roebling
733 int canvasWidth
, canvasHeight
;
734 canvas
->GetSize(&canvasWidth
, &canvasHeight
);
736 float zoomScale
= (float)((float)currentZoom
/(float)100);
737 float actualWidth
= zoomScale
*pageWidth
*previewScale
;
738 float actualHeight
= zoomScale
*pageHeight
*previewScale
;
740 float x
= (float)((canvasWidth
- actualWidth
)/2.0);
742 x
= (float)leftMargin
;
743 float y
= (float)topMargin
;
745 // Draw shadow, allowing for 1-pixel border AROUND the actual page
746 int shadowOffset
= 4;
747 dc
.SetPen(*wxBLACK_PEN
);
748 dc
.SetBrush(*wxBLACK_BRUSH
);
749 dc
.DrawRectangle(x
-1 + shadowOffset
, y
-1 + shadowOffset
, actualWidth
+2, actualHeight
+2);
751 // Draw blank page allowing for 1-pixel border AROUND the actual page
752 dc
.SetPen(*wxBLACK_PEN
);
753 dc
.SetBrush(*wxWHITE_BRUSH
);
756 dc
.DrawRectangle(x
-1, y
-1, actualWidth
+2, actualHeight
+2);
763 void wxPrintPreviewBase::SetZoom(int percent
)
765 #ifdef __GOOD_COMPILER__ // Robert Roebling
766 if (currentZoom
== percent
)
769 currentZoom
= percent
;
772 delete previewBitmap
;
773 previewBitmap
= NULL
;
775 RenderPage(currentPage
);
779 previewCanvas
->Clear();
780 previewCanvas
->Refresh();