]> git.saurik.com Git - wxWidgets.git/blob - src/common/prntbase.cpp
ee3ec90dad6a507348899ed946e8dc427f70a5df
[wxWidgets.git] / src / common / prntbase.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: prntbase.cpp
3 // Purpose: Printing framework base class implementation
4 // Author: Julian Smart
5 // Modified by:
6 // Created: 04/01/98
7 // RCS-ID: $Id$
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 #ifdef __GNUG__
13 #pragma implementation "prntbase.h"
14 #endif
15
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
18
19 #ifdef __BORLANDC__
20 #pragma hdrstop
21 #endif
22
23 #include "wx/defs.h"
24
25 #if wxUSE_PRINTING_ARCHITECTURE
26
27 #ifndef WX_PRECOMP
28 #include "wx/utils.h"
29 #include "wx/dc.h"
30 #include "wx/app.h"
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"
38 #include "wx/intl.h"
39 #include "wx/textdlg.h"
40 #include "wx/sizer.h"
41 #endif // !WX_PRECOMP
42
43 #include "wx/prntbase.h"
44 #include "wx/dcprint.h"
45 #include "wx/printdlg.h"
46 #include "wx/module.h"
47
48 #include <stdlib.h>
49 #include <string.h>
50
51 #ifdef __WXMSW__
52 #include "wx/msw/private.h"
53 #include <commdlg.h>
54
55 #ifndef __WIN32__
56 #include <print.h>
57 #endif
58 #endif // __WXMSW__
59
60 IMPLEMENT_CLASS(wxPrinterBase, wxObject)
61 IMPLEMENT_ABSTRACT_CLASS(wxPrintout, wxObject)
62 IMPLEMENT_CLASS(wxPreviewCanvas, wxWindow)
63 IMPLEMENT_CLASS(wxPreviewControlBar, wxWindow)
64 IMPLEMENT_CLASS(wxPreviewFrame, wxFrame)
65 IMPLEMENT_CLASS(wxPrintPreviewBase, wxObject)
66
67 BEGIN_EVENT_TABLE(wxPrintAbortDialog, wxDialog)
68 EVT_BUTTON(wxID_CANCEL, wxPrintAbortDialog::OnCancel)
69 END_EVENT_TABLE()
70
71 BEGIN_EVENT_TABLE(wxPreviewCanvas, wxScrolledWindow)
72 EVT_PAINT(wxPreviewCanvas::OnPaint)
73 EVT_CHAR(wxPreviewCanvas::OnChar)
74 EVT_SYS_COLOUR_CHANGED(wxPreviewCanvas::OnSysColourChanged)
75 END_EVENT_TABLE()
76
77 /*
78 * Printer
79 */
80
81 wxPrinterBase::wxPrinterBase(wxPrintDialogData *data)
82 {
83 m_currentPrintout = (wxPrintout *) NULL;
84 sm_abortWindow = (wxWindow *) NULL;
85 sm_abortIt = FALSE;
86 if (data)
87 m_printDialogData = (*data);
88 sm_lastError = wxPRINTER_NO_ERROR;
89 }
90
91 wxWindow *wxPrinterBase::sm_abortWindow = (wxWindow *) NULL;
92 bool wxPrinterBase::sm_abortIt = FALSE;
93 wxPrinterError wxPrinterBase::sm_lastError = wxPRINTER_NO_ERROR;
94
95 wxPrinterBase::~wxPrinterBase()
96 {
97 }
98
99 void wxPrintAbortDialog::OnCancel(wxCommandEvent& WXUNUSED(event))
100 {
101 wxPrinterBase::sm_abortIt = TRUE;
102 wxPrinterBase::sm_abortWindow->Show(FALSE);
103 wxPrinterBase::sm_abortWindow->Close(TRUE);
104 wxPrinterBase::sm_abortWindow = (wxWindow *) NULL;
105 }
106
107 wxWindow *wxPrinterBase::CreateAbortWindow(wxWindow *parent, wxPrintout * printout)
108 {
109 wxPrintAbortDialog *dialog = new wxPrintAbortDialog(parent, _("Printing ") , wxDefaultPosition, wxDefaultSize, wxDEFAULT_DIALOG_STYLE);
110
111 wxBoxSizer *button_sizer = new wxBoxSizer( wxVERTICAL );
112 button_sizer->Add( new wxStaticText(dialog, -1, _("Please wait while printing\n") + printout->GetTitle() ), 0, wxALL, 10 );
113 button_sizer->Add( new wxButton( dialog, wxID_CANCEL, wxT("Cancel") ), 0, wxALL | wxALIGN_CENTER, 10 );
114
115 dialog->SetAutoLayout( TRUE );
116 dialog->SetSizer( button_sizer );
117
118 button_sizer->Fit(dialog);
119 button_sizer->SetSizeHints (dialog) ;
120
121 return dialog;
122 }
123
124 void wxPrinterBase::ReportError(wxWindow *parent, wxPrintout *WXUNUSED(printout), const wxString& message)
125 {
126 wxMessageBox(message, _("Printing Error"), wxOK, parent);
127 }
128
129 /*
130 * Printout class
131 */
132
133 wxPrintout::wxPrintout(const wxString& title)
134 {
135 m_printoutTitle = title ;
136 m_printoutDC = (wxDC *) NULL;
137 m_pageWidthMM = 0;
138 m_pageHeightMM = 0;
139 m_pageWidthPixels = 0;
140 m_pageHeightPixels = 0;
141 m_PPIScreenX = 0;
142 m_PPIScreenY = 0;
143 m_PPIPrinterX = 0;
144 m_PPIPrinterY = 0;
145 m_isPreview = FALSE;
146 }
147
148 wxPrintout::~wxPrintout()
149 {
150 }
151
152 bool wxPrintout::OnBeginDocument(int WXUNUSED(startPage), int WXUNUSED(endPage))
153 {
154 return GetDC()->StartDoc(_("Printing ") + m_printoutTitle);
155 }
156
157 void wxPrintout::OnEndDocument()
158 {
159 GetDC()->EndDoc();
160 }
161
162 void wxPrintout::OnBeginPrinting()
163 {
164 }
165
166 void wxPrintout::OnEndPrinting()
167 {
168 }
169
170 bool wxPrintout::HasPage(int page)
171 {
172 return (page == 1);
173 }
174
175 void wxPrintout::GetPageInfo(int *minPage, int *maxPage, int *fromPage, int *toPage)
176 {
177 *minPage = 1;
178 *maxPage = 32000;
179 *fromPage = 1;
180 *toPage = 1;
181 }
182
183 /*
184 * Preview canvas
185 */
186
187 wxPreviewCanvas::wxPreviewCanvas(wxPrintPreviewBase *preview, wxWindow *parent,
188 const wxPoint& pos, const wxSize& size, long style, const wxString& name):
189 wxScrolledWindow(parent, -1, pos, size, style, name)
190 {
191 m_printPreview = preview;
192 #ifdef __WXMAC__
193 // The app workspace colour is always white, but we should have
194 // a contrast with the page.
195 wxSystemColour colourIndex = wxSYS_COLOUR_3DDKSHADOW;
196 #else
197 wxSystemColour colourIndex = wxSYS_COLOUR_APPWORKSPACE;
198 #endif
199 SetBackgroundColour(wxSystemSettings::GetColour(colourIndex));
200
201 SetScrollbars(10, 10, 100, 100);
202 }
203
204 wxPreviewCanvas::~wxPreviewCanvas()
205 {
206 }
207
208 void wxPreviewCanvas::OnPaint(wxPaintEvent& WXUNUSED(event))
209 {
210 wxPaintDC dc(this);
211 PrepareDC( dc );
212
213 /*
214 #ifdef __WXGTK__
215 if (!GetUpdateRegion().IsEmpty())
216 dc.SetClippingRegion( GetUpdateRegion() );
217 #endif
218 */
219
220 if (m_printPreview)
221 {
222 m_printPreview->PaintPage(this, dc);
223 }
224 }
225
226 // Responds to colour changes, and passes event on to children.
227 void wxPreviewCanvas::OnSysColourChanged(wxSysColourChangedEvent& event)
228 {
229 #ifdef __WXMAC__
230 // The app workspace colour is always white, but we should have
231 // a contrast with the page.
232 wxSystemColour colourIndex = wxSYS_COLOUR_3DDKSHADOW;
233 #else
234 wxSystemColour colourIndex = wxSYS_COLOUR_APPWORKSPACE;
235 #endif
236 SetBackgroundColour(wxSystemSettings::GetColour(colourIndex));
237 Refresh();
238
239 // Propagate the event to the non-top-level children
240 wxWindow::OnSysColourChanged(event);
241 }
242
243 void wxPreviewCanvas::OnChar(wxKeyEvent &event)
244 {
245 if (event.GetKeyCode() == WXK_ESCAPE)
246 {
247 ((wxPreviewFrame*) GetParent())->Close(TRUE);
248 return;
249 }
250
251 if (!event.ControlDown())
252 {
253 event.Skip();
254 return;
255 }
256
257 wxPreviewControlBar* controlBar = ((wxPreviewFrame*) GetParent())->GetControlBar();
258 switch(event.GetKeyCode())
259 {
260 case WXK_NEXT:
261 controlBar->OnNext(); break;
262 case WXK_PRIOR:
263 controlBar->OnPrevious(); break;
264 case WXK_HOME:
265 controlBar->OnFirst(); break;
266 case WXK_END:
267 controlBar->OnLast(); break;
268 case WXK_TAB:
269 controlBar->OnGoto(); break;
270 default:
271 event.Skip();
272 }
273 }
274
275 /*
276 * Preview control bar
277 */
278
279 BEGIN_EVENT_TABLE(wxPreviewControlBar, wxPanel)
280 EVT_BUTTON(wxID_PREVIEW_CLOSE, wxPreviewControlBar::OnWindowClose)
281 EVT_BUTTON(wxID_PREVIEW_PRINT, wxPreviewControlBar::OnPrint)
282 EVT_BUTTON(wxID_PREVIEW_PREVIOUS, wxPreviewControlBar::OnPreviousButton)
283 EVT_BUTTON(wxID_PREVIEW_NEXT, wxPreviewControlBar::OnNextButton)
284 EVT_BUTTON(wxID_PREVIEW_FIRST, wxPreviewControlBar::OnFirstButton)
285 EVT_BUTTON(wxID_PREVIEW_LAST, wxPreviewControlBar::OnLastButton)
286 EVT_BUTTON(wxID_PREVIEW_GOTO, wxPreviewControlBar::OnGotoButton)
287 EVT_CHOICE(wxID_PREVIEW_ZOOM, wxPreviewControlBar::OnZoom)
288 EVT_PAINT(wxPreviewControlBar::OnPaint)
289 END_EVENT_TABLE()
290
291 wxPreviewControlBar::wxPreviewControlBar(wxPrintPreviewBase *preview, long buttons,
292 wxWindow *parent, const wxPoint& pos, const wxSize& size,
293 long style, const wxString& name):
294 wxPanel(parent, -1, pos, size, style, name)
295 {
296 m_printPreview = preview;
297 m_closeButton = (wxButton *) NULL;
298 m_nextPageButton = (wxButton *) NULL;
299 m_previousPageButton = (wxButton *) NULL;
300 m_printButton = (wxButton *) NULL;
301 m_zoomControl = (wxChoice *) NULL;
302 m_buttonFlags = buttons;
303 }
304
305 wxPreviewControlBar::~wxPreviewControlBar()
306 {
307 }
308
309 void wxPreviewControlBar::OnPaint(wxPaintEvent& WXUNUSED(event))
310 {
311 wxPaintDC dc(this);
312
313 int w, h;
314 GetSize(&w, &h);
315 dc.SetPen(*wxBLACK_PEN);
316 dc.SetBrush(*wxTRANSPARENT_BRUSH);
317 dc.DrawLine( 0, h-1, w, h-1 );
318 }
319
320 void wxPreviewControlBar::OnWindowClose(wxCommandEvent& WXUNUSED(event))
321 {
322 wxPreviewFrame *frame = (wxPreviewFrame *)GetParent();
323 frame->Close(TRUE);
324 }
325
326 void wxPreviewControlBar::OnPrint(wxCommandEvent& WXUNUSED(event))
327 {
328 wxPrintPreviewBase *preview = GetPrintPreview();
329 preview->Print(TRUE);
330 }
331
332 void wxPreviewControlBar::OnNext(void)
333 {
334 wxPrintPreviewBase *preview = GetPrintPreview();
335 if (preview)
336 {
337 int currentPage = preview->GetCurrentPage();
338 if ((preview->GetMaxPage() > 0) &&
339 (currentPage < preview->GetMaxPage()) &&
340 preview->GetPrintout()->HasPage(currentPage + 1))
341 {
342 preview->SetCurrentPage(currentPage + 1);
343 }
344 }
345 }
346
347 void wxPreviewControlBar::OnPrevious(void)
348 {
349 wxPrintPreviewBase *preview = GetPrintPreview();
350 if (preview)
351 {
352 int currentPage = preview->GetCurrentPage();
353 if ((preview->GetMinPage() > 0) &&
354 (currentPage > preview->GetMinPage()) &&
355 preview->GetPrintout()->HasPage(currentPage - 1))
356 {
357 preview->SetCurrentPage(currentPage - 1);
358 }
359 }
360 }
361
362 void wxPreviewControlBar::OnFirst(void)
363 {
364 wxPrintPreviewBase *preview = GetPrintPreview();
365 if (preview)
366 {
367 int currentPage = preview->GetMinPage();
368 if (preview->GetPrintout()->HasPage(currentPage))
369 {
370 preview->SetCurrentPage(currentPage);
371 }
372 }
373 }
374
375 void wxPreviewControlBar::OnLast(void)
376 {
377 wxPrintPreviewBase *preview = GetPrintPreview();
378 if (preview)
379 {
380 int currentPage = preview->GetMaxPage();
381 if (preview->GetPrintout()->HasPage(currentPage))
382 {
383 preview->SetCurrentPage(currentPage);
384 }
385 }
386 }
387
388 void wxPreviewControlBar::OnGoto(void)
389 {
390 wxPrintPreviewBase *preview = GetPrintPreview();
391 if (preview)
392 {
393 long currentPage;
394
395 if (preview->GetMinPage() > 0)
396 {
397 wxString strPrompt;
398 wxString strPage;
399
400 strPrompt.Printf( _("Enter a page number between %d and %d:"),
401 preview->GetMinPage(), preview->GetMaxPage());
402 strPage.Printf( wxT("%d"), preview->GetCurrentPage() );
403
404 strPage =
405 wxGetTextFromUser( strPrompt, _("Goto Page"), strPage, GetParent());
406
407 if ( strPage.ToLong( &currentPage ) )
408 if (preview->GetPrintout()->HasPage(currentPage))
409 {
410 preview->SetCurrentPage(currentPage);
411 }
412 }
413 }
414 }
415
416 void wxPreviewControlBar::OnZoom(wxCommandEvent& WXUNUSED(event))
417 {
418 int zoom = GetZoomControl();
419 if (GetPrintPreview())
420 GetPrintPreview()->SetZoom(zoom);
421 }
422
423 void wxPreviewControlBar::CreateButtons()
424 {
425 SetSize(0, 0, 400, 40);
426
427 wxBoxSizer *item0 = new wxBoxSizer( wxHORIZONTAL );
428
429 int smallButtonWidth = 45;
430
431 m_closeButton = new wxButton( this, wxID_PREVIEW_CLOSE, _("&Close"), wxDefaultPosition, wxDefaultSize, 0 );
432 item0->Add( m_closeButton, 0, wxALIGN_CENTRE|wxALL, 5 );
433
434 if (m_buttonFlags & wxPREVIEW_PRINT)
435 {
436 m_printButton = new wxButton( this, wxID_PREVIEW_PRINT, _("&Print..."), wxDefaultPosition, wxDefaultSize, 0 );
437 item0->Add( m_printButton, 0, wxALIGN_CENTRE|wxALL, 5 );
438 }
439
440 if (m_buttonFlags & wxPREVIEW_FIRST)
441 {
442 m_firstPageButton = new wxButton( this, wxID_PREVIEW_FIRST, _("|<<"), wxDefaultPosition, wxSize(smallButtonWidth,-1), 0 );
443 item0->Add( m_firstPageButton, 0, wxALIGN_CENTRE|wxALL, 5 );
444 }
445
446 if (m_buttonFlags & wxPREVIEW_PREVIOUS)
447 {
448 m_previousPageButton = new wxButton( this, wxID_PREVIEW_PREVIOUS, _("<<"), wxDefaultPosition, wxSize(smallButtonWidth,-1), 0 );
449 item0->Add( m_previousPageButton, 0, wxALIGN_CENTRE|wxRIGHT|wxTOP|wxBOTTOM, 5 );
450 }
451
452 if (m_buttonFlags & wxPREVIEW_NEXT)
453 {
454 m_nextPageButton = new wxButton( this, wxID_PREVIEW_NEXT, _(">>"), wxDefaultPosition, wxSize(smallButtonWidth,-1), 0 );
455 item0->Add( m_nextPageButton, 0, wxALIGN_CENTRE|wxRIGHT|wxTOP|wxBOTTOM, 5 );
456 }
457
458 if (m_buttonFlags & wxPREVIEW_LAST)
459 {
460 m_lastPageButton = new wxButton( this, wxID_PREVIEW_LAST, _(">>|"), wxDefaultPosition, wxSize(smallButtonWidth,-1), 0 );
461 item0->Add( m_lastPageButton, 0, wxALIGN_CENTRE|wxRIGHT|wxTOP|wxBOTTOM, 5 );
462 }
463
464 if (m_buttonFlags & wxPREVIEW_GOTO)
465 {
466 m_gotoPageButton = new wxButton( this, wxID_PREVIEW_GOTO, _("&Goto..."), wxDefaultPosition, wxDefaultSize, 0 );
467 item0->Add( m_gotoPageButton, 0, wxALIGN_CENTRE|wxALL, 5 );
468 }
469
470 if (m_buttonFlags & wxPREVIEW_ZOOM)
471 {
472 wxString choices[] =
473 {
474 wxT("10%"), wxT("15%"), wxT("20%"), wxT("25%"), wxT("30%"), wxT("35%"), wxT("40%"), wxT("45%"), wxT("50%"), wxT("55%"),
475 wxT("60%"), wxT("65%"), wxT("70%"), wxT("75%"), wxT("80%"), wxT("85%"), wxT("90%"), wxT("95%"), wxT("100%"), wxT("110%"),
476 wxT("120%"), wxT("150%"), wxT("200%")
477 };
478 int n = WXSIZEOF(choices);
479
480 m_zoomControl = new wxChoice( this, wxID_PREVIEW_ZOOM, wxDefaultPosition, wxSize(70,-1), n, choices, 0 );
481 item0->Add( m_zoomControl, 0, wxALIGN_CENTRE|wxALL, 5 );
482 SetZoomControl(m_printPreview->GetZoom());
483 }
484
485 SetSizer(item0);
486 item0->Fit(this);
487 }
488
489 void wxPreviewControlBar::SetZoomControl(int zoom)
490 {
491 wxChar buf[20];
492 wxSprintf( buf, wxT("%d%%"), zoom );
493
494 if (m_zoomControl)
495 m_zoomControl->SetStringSelection(buf);
496 }
497
498 int wxPreviewControlBar::GetZoomControl()
499 {
500 wxChar buf[20];
501 if (m_zoomControl && (m_zoomControl->GetStringSelection() != wxT("")))
502 {
503 wxStrcpy(buf, m_zoomControl->GetStringSelection());
504 buf[wxStrlen(buf) - 1] = 0;
505 return (int)wxAtoi(buf);
506 }
507 else return 0;
508 }
509
510
511 /*
512 * Preview frame
513 */
514
515 BEGIN_EVENT_TABLE(wxPreviewFrame, wxFrame)
516 EVT_CLOSE(wxPreviewFrame::OnCloseWindow)
517 END_EVENT_TABLE()
518
519 wxPreviewFrame::wxPreviewFrame(wxPrintPreviewBase *preview, wxWindow *parent, const wxString& title,
520 const wxPoint& pos, const wxSize& size, long style, const wxString& name):
521 wxFrame(parent, -1, title, pos, size, style, name)
522 {
523 m_printPreview = preview;
524 m_controlBar = NULL;
525 m_previewCanvas = NULL;
526
527 // Give the application icon
528 #ifdef __WXMSW__
529 wxFrame* topFrame = wxDynamicCast(wxTheApp->GetTopWindow(), wxFrame);
530 if (topFrame)
531 SetIcon(topFrame->GetIcon());
532 #endif
533 }
534
535 wxPreviewFrame::~wxPreviewFrame()
536 {
537 }
538
539 void wxPreviewFrame::OnCloseWindow(wxCloseEvent& WXUNUSED(event))
540 {
541 // MakeModal doesn't work on wxMac, especially when there
542 // are multiple top-level windows.
543 #ifndef __WXMAC__
544 MakeModal(FALSE);
545 #endif
546
547 // Need to delete the printout and the print preview
548 wxPrintout *printout = m_printPreview->GetPrintout();
549 if (printout)
550 {
551 delete printout;
552 m_printPreview->SetPrintout(NULL);
553 m_printPreview->SetCanvas(NULL);
554 m_printPreview->SetFrame(NULL);
555 }
556 delete m_printPreview;
557
558 Destroy();
559 }
560
561 void wxPreviewFrame::Initialize()
562 {
563 #if wxUSE_STATUSBAR
564 CreateStatusBar();
565 #endif
566 CreateCanvas();
567 CreateControlBar();
568
569 m_printPreview->SetCanvas(m_previewCanvas);
570 m_printPreview->SetFrame(this);
571
572 wxBoxSizer *item0 = new wxBoxSizer( wxVERTICAL );
573
574 item0->Add( m_controlBar, 0, wxGROW|wxALIGN_CENTER_VERTICAL, 5 );
575 item0->Add( m_previewCanvas, 1, wxGROW|wxALIGN_CENTER_VERTICAL, 5 );
576
577 SetAutoLayout( TRUE );
578 SetSizer( item0 );
579
580 // MakeModal doesn't work on wxMac, especially when there
581 // are multiple top-level windows.
582 #ifndef __WXMAC__
583 MakeModal(TRUE);
584 #endif
585
586 Layout();
587
588 m_printPreview->AdjustScrollbars(m_previewCanvas);
589 m_previewCanvas->SetFocus();
590 m_controlBar->SetFocus();
591 }
592
593 void wxPreviewFrame::CreateCanvas()
594 {
595 m_previewCanvas = new wxPreviewCanvas(m_printPreview, this);
596 }
597
598 void wxPreviewFrame::CreateControlBar()
599 {
600 long buttons = wxPREVIEW_DEFAULT;
601 if (m_printPreview->GetPrintoutForPrinting())
602 buttons |= wxPREVIEW_PRINT;
603
604 m_controlBar = new wxPreviewControlBar(m_printPreview, buttons, this, wxPoint(0, 0), wxSize(400, 40));
605 m_controlBar->CreateButtons();
606 }
607
608 /*
609 * Print preview
610 */
611
612 wxPrintPreviewBase::wxPrintPreviewBase(wxPrintout *printout,
613 wxPrintout *printoutForPrinting,
614 wxPrintData *data)
615 {
616 if (data)
617 m_printDialogData = (*data);
618
619 Init(printout, printoutForPrinting);
620 }
621
622 wxPrintPreviewBase::wxPrintPreviewBase(wxPrintout *printout,
623 wxPrintout *printoutForPrinting,
624 wxPrintDialogData *data)
625 {
626 if (data)
627 m_printDialogData = (*data);
628
629 Init(printout, printoutForPrinting);
630 }
631
632 void wxPrintPreviewBase::Init(wxPrintout *printout,
633 wxPrintout *printoutForPrinting)
634 {
635 m_isOk = TRUE;
636 m_previewPrintout = printout;
637 if (m_previewPrintout)
638 m_previewPrintout->SetIsPreview(TRUE);
639
640 m_printPrintout = printoutForPrinting;
641
642 m_previewCanvas = NULL;
643 m_previewFrame = NULL;
644 m_previewBitmap = NULL;
645 m_currentPage = 1;
646 m_currentZoom = 70;
647 m_topMargin = 40;
648 m_leftMargin = 40;
649 m_pageWidth = 0;
650 m_pageHeight = 0;
651 m_printingPrepared = FALSE;
652 m_minPage = 1;
653 m_maxPage = 1;
654 }
655
656 wxPrintPreviewBase::~wxPrintPreviewBase()
657 {
658 if (m_previewPrintout)
659 delete m_previewPrintout;
660 if (m_previewBitmap)
661 delete m_previewBitmap;
662 if (m_printPrintout)
663 delete m_printPrintout;
664 }
665
666 bool wxPrintPreviewBase::SetCurrentPage(int pageNum)
667 {
668 if (m_currentPage == pageNum)
669 return TRUE;
670
671 m_currentPage = pageNum;
672 if (m_previewBitmap)
673 {
674 delete m_previewBitmap;
675 m_previewBitmap = NULL;
676 }
677
678 if (m_previewCanvas)
679 {
680 AdjustScrollbars(m_previewCanvas);
681
682 if (!RenderPage(pageNum))
683 return FALSE;
684 m_previewCanvas->Refresh();
685 m_previewCanvas->SetFocus();
686 }
687 return TRUE;
688 }
689
690 bool wxPrintPreviewBase::PaintPage(wxPreviewCanvas *canvas, wxDC& dc)
691 {
692 DrawBlankPage(canvas, dc);
693
694 if (!m_previewBitmap)
695 if (!RenderPage(m_currentPage))
696 return FALSE;
697
698 if (!m_previewBitmap)
699 return FALSE;
700
701 if (!canvas)
702 return FALSE;
703
704 int canvasWidth, canvasHeight;
705 canvas->GetSize(&canvasWidth, &canvasHeight);
706
707 double zoomScale = ((float)m_currentZoom/(float)100);
708 double actualWidth = (zoomScale*m_pageWidth*m_previewScale);
709 // float actualHeight = (float)(zoomScale*m_pageHeight*m_previewScale);
710
711 int x = (int) ((canvasWidth - actualWidth)/2.0);
712 if (x < m_leftMargin)
713 x = m_leftMargin;
714 int y = m_topMargin;
715
716 wxMemoryDC temp_dc;
717 temp_dc.SelectObject(*m_previewBitmap);
718
719 dc.Blit(x, y, m_previewBitmap->GetWidth(), m_previewBitmap->GetHeight(), &temp_dc, 0, 0);
720
721 temp_dc.SelectObject(wxNullBitmap);
722
723 return TRUE;
724 }
725
726 // Adjusts the scrollbars for the current scale
727 void wxPrintPreviewBase::AdjustScrollbars(wxPreviewCanvas *canvas)
728 {
729 if (!canvas)
730 return ;
731
732 int canvasWidth, canvasHeight;
733 canvas->GetSize(&canvasWidth, &canvasHeight);
734
735 double zoomScale = ((float)m_currentZoom/(float)100);
736 double actualWidth = (zoomScale*m_pageWidth*m_previewScale);
737 double actualHeight = (zoomScale*m_pageHeight*m_previewScale);
738
739 // Set the scrollbars appropriately
740 int totalWidth = actualWidth + 2*m_leftMargin;
741 int totalHeight = actualHeight + 2*m_topMargin;
742 int scrollUnitsX = totalWidth/10;
743 int scrollUnitsY = totalHeight/10;
744 wxSize virtualSize = canvas->GetVirtualSize();
745 if (virtualSize.GetWidth() != totalWidth || virtualSize.GetHeight() != totalHeight)
746 canvas->SetScrollbars(10, 10, scrollUnitsX, scrollUnitsY, 0, 0, TRUE);
747 }
748
749 bool wxPrintPreviewBase::RenderPage(int pageNum)
750 {
751 wxBusyCursor busy;
752
753 int canvasWidth, canvasHeight;
754
755 if (!m_previewCanvas)
756 {
757 wxFAIL_MSG(_T("wxPrintPreviewBase::RenderPage: must use wxPrintPreviewBase::SetCanvas to let me know about the canvas!"));
758
759 return FALSE;
760 }
761 m_previewCanvas->GetSize(&canvasWidth, &canvasHeight);
762
763 double zoomScale = (m_currentZoom/100.0);
764 int actualWidth = (int)(zoomScale*m_pageWidth*m_previewScale);
765 int actualHeight = (int)(zoomScale*m_pageHeight*m_previewScale);
766
767 int x = (int)((canvasWidth - actualWidth)/2.0);
768 if (x < m_leftMargin)
769 x = m_leftMargin;
770 // int y = m_topMargin;
771
772
773 if (!m_previewBitmap)
774 {
775 m_previewBitmap = new wxBitmap((int)actualWidth, (int)actualHeight);
776 if (!m_previewBitmap || !m_previewBitmap->Ok())
777 {
778 if (m_previewBitmap) {
779 delete m_previewBitmap;
780 m_previewBitmap = NULL;
781 }
782 wxMessageBox(_("Sorry, not enough memory to create a preview."), _("Print Preview Failure"), wxOK);
783 return FALSE;
784 }
785 }
786
787 wxMemoryDC memoryDC;
788 memoryDC.SelectObject(*m_previewBitmap);
789
790 memoryDC.Clear();
791
792 m_previewPrintout->SetDC(&memoryDC);
793 m_previewPrintout->SetPageSizePixels(m_pageWidth, m_pageHeight);
794
795 // Need to delay OnPreparePrinting until here, so we have enough information.
796 if (!m_printingPrepared)
797 {
798 m_previewPrintout->OnPreparePrinting();
799 int selFrom, selTo;
800 m_previewPrintout->GetPageInfo(&m_minPage, &m_maxPage, &selFrom, &selTo);
801 m_printingPrepared = TRUE;
802 }
803
804 m_previewPrintout->OnBeginPrinting();
805
806 if (!m_previewPrintout->OnBeginDocument(m_printDialogData.GetFromPage(), m_printDialogData.GetToPage()))
807 {
808 wxMessageBox(_("Could not start document preview."), _("Print Preview Failure"), wxOK);
809
810 memoryDC.SelectObject(wxNullBitmap);
811
812 delete m_previewBitmap;
813 m_previewBitmap = NULL;
814 return FALSE;
815 }
816
817 m_previewPrintout->OnPrintPage(pageNum);
818 m_previewPrintout->OnEndDocument();
819 m_previewPrintout->OnEndPrinting();
820
821 m_previewPrintout->SetDC(NULL);
822
823 memoryDC.SelectObject(wxNullBitmap);
824
825 #if wxUSE_STATUSBAR
826 wxString status;
827 if (m_maxPage != 0)
828 status = wxString::Format(_("Page %d of %d"), pageNum, m_maxPage);
829 else
830 status = wxString::Format(_("Page %d"), pageNum);
831
832 if (m_previewFrame)
833 m_previewFrame->SetStatusText(status);
834 #endif
835
836 return TRUE;
837 }
838
839
840 bool wxPrintPreviewBase::DrawBlankPage(wxPreviewCanvas *canvas, wxDC& dc)
841 {
842 int canvasWidth, canvasHeight;
843 canvas->GetSize(&canvasWidth, &canvasHeight);
844
845 float zoomScale = (float)((float)m_currentZoom/(float)100);
846 float actualWidth = zoomScale*m_pageWidth*m_previewScale;
847 float actualHeight = zoomScale*m_pageHeight*m_previewScale;
848
849 float x = (float)((canvasWidth - actualWidth)/2.0);
850 if (x < m_leftMargin)
851 x = (float)m_leftMargin;
852 float y = (float)m_topMargin;
853
854 // Draw shadow, allowing for 1-pixel border AROUND the actual page
855 int shadowOffset = 4;
856 dc.SetPen(*wxBLACK_PEN);
857 dc.SetBrush(*wxBLACK_BRUSH);
858 /*
859 dc.DrawRectangle((int)(x-1 + shadowOffset), (int)(y-1 + shadowOffset), (int)(actualWidth+2), (int)(actualHeight+2));
860 */
861 dc.DrawRectangle((int)(x + shadowOffset), (int)(y + actualHeight+1), (int)(actualWidth), shadowOffset);
862 dc.DrawRectangle((int)(x + actualWidth), (int)(y + shadowOffset), shadowOffset, (int)(actualHeight));
863
864 // Draw blank page allowing for 1-pixel border AROUND the actual page
865 dc.SetPen(*wxBLACK_PEN);
866 dc.SetBrush(*wxWHITE_BRUSH);
867
868 /*
869 wxRegion update_region = canvas->GetUpdateRegion();
870 wxRect r = update_region.GetBox();
871
872 printf( "x: %d y: %d w: %d h: %d.\n", (int)r.x, (int)r.y, (int)r.width, (int)r.height );
873 */
874
875 dc.DrawRectangle((int)(x-2), (int)(y-1), (int)(actualWidth+3), (int)(actualHeight+2));
876
877 return TRUE;
878 }
879
880 void wxPrintPreviewBase::SetZoom(int percent)
881 {
882 if (m_currentZoom == percent)
883 return;
884
885 m_currentZoom = percent;
886 if (m_previewBitmap)
887 {
888 delete m_previewBitmap;
889 m_previewBitmap = NULL;
890 }
891
892 if (m_previewCanvas)
893 {
894 AdjustScrollbars(m_previewCanvas);
895 RenderPage(m_currentPage);
896 ((wxScrolledWindow *) m_previewCanvas)->Scroll(0, 0);
897 m_previewCanvas->Clear();
898 m_previewCanvas->Refresh();
899 m_previewCanvas->SetFocus();
900 }
901 }
902
903 #endif // wxUSE_PRINTING_ARCHITECTURE