use wxBU_EXACTFIT for the small buttons
[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 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
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 // VZ: the current code doesn't refresh properly without
188 // wxFULL_REPAINT_ON_RESIZE, this must be fixed as otherwise we have
189 // really horrible flicker when resizing the preview frame, but without
190 // this style it simply doesn't work correctly at all...
191 wxPreviewCanvas::wxPreviewCanvas(wxPrintPreviewBase *preview, wxWindow *parent,
192 const wxPoint& pos, const wxSize& size, long style, const wxString& name):
193 wxScrolledWindow(parent, -1, pos, size, style | wxFULL_REPAINT_ON_RESIZE, name)
194 {
195 m_printPreview = preview;
196 #ifdef __WXMAC__
197 // The app workspace colour is always white, but we should have
198 // a contrast with the page.
199 wxSystemColour colourIndex = wxSYS_COLOUR_3DDKSHADOW;
200 #else
201 wxSystemColour colourIndex = wxSYS_COLOUR_APPWORKSPACE;
202 #endif
203 SetBackgroundColour(wxSystemSettings::GetColour(colourIndex));
204
205 SetScrollbars(10, 10, 100, 100);
206 }
207
208 wxPreviewCanvas::~wxPreviewCanvas()
209 {
210 }
211
212 void wxPreviewCanvas::OnPaint(wxPaintEvent& WXUNUSED(event))
213 {
214 wxPaintDC dc(this);
215 PrepareDC( dc );
216
217 /*
218 #ifdef __WXGTK__
219 if (!GetUpdateRegion().IsEmpty())
220 dc.SetClippingRegion( GetUpdateRegion() );
221 #endif
222 */
223
224 if (m_printPreview)
225 {
226 m_printPreview->PaintPage(this, dc);
227 }
228 }
229
230 // Responds to colour changes, and passes event on to children.
231 void wxPreviewCanvas::OnSysColourChanged(wxSysColourChangedEvent& event)
232 {
233 #ifdef __WXMAC__
234 // The app workspace colour is always white, but we should have
235 // a contrast with the page.
236 wxSystemColour colourIndex = wxSYS_COLOUR_3DDKSHADOW;
237 #else
238 wxSystemColour colourIndex = wxSYS_COLOUR_APPWORKSPACE;
239 #endif
240 SetBackgroundColour(wxSystemSettings::GetColour(colourIndex));
241 Refresh();
242
243 // Propagate the event to the non-top-level children
244 wxWindow::OnSysColourChanged(event);
245 }
246
247 void wxPreviewCanvas::OnChar(wxKeyEvent &event)
248 {
249 wxPreviewControlBar* controlBar = ((wxPreviewFrame*) GetParent())->GetControlBar();
250 if (event.GetKeyCode() == WXK_ESCAPE)
251 {
252 ((wxPreviewFrame*) GetParent())->Close(TRUE);
253 return;
254 }
255 else if (event.GetKeyCode() == WXK_TAB)
256 {
257 controlBar->OnGoto();
258 return;
259 }
260 else if (event.GetKeyCode() == WXK_RETURN)
261 {
262 controlBar->OnPrint();
263 return;
264 }
265
266 if (!event.ControlDown())
267 {
268 event.Skip();
269 return;
270 }
271
272 switch(event.GetKeyCode())
273 {
274 case WXK_NEXT:
275 controlBar->OnNext(); break;
276 case WXK_PRIOR:
277 controlBar->OnPrevious(); break;
278 case WXK_HOME:
279 controlBar->OnFirst(); break;
280 case WXK_END:
281 controlBar->OnLast(); break;
282 default:
283 event.Skip();
284 }
285 }
286
287 /*
288 * Preview control bar
289 */
290
291 BEGIN_EVENT_TABLE(wxPreviewControlBar, wxPanel)
292 EVT_BUTTON(wxID_PREVIEW_CLOSE, wxPreviewControlBar::OnWindowClose)
293 EVT_BUTTON(wxID_PREVIEW_PRINT, wxPreviewControlBar::OnPrintButton)
294 EVT_BUTTON(wxID_PREVIEW_PREVIOUS, wxPreviewControlBar::OnPreviousButton)
295 EVT_BUTTON(wxID_PREVIEW_NEXT, wxPreviewControlBar::OnNextButton)
296 EVT_BUTTON(wxID_PREVIEW_FIRST, wxPreviewControlBar::OnFirstButton)
297 EVT_BUTTON(wxID_PREVIEW_LAST, wxPreviewControlBar::OnLastButton)
298 EVT_BUTTON(wxID_PREVIEW_GOTO, wxPreviewControlBar::OnGotoButton)
299 EVT_CHOICE(wxID_PREVIEW_ZOOM, wxPreviewControlBar::OnZoom)
300 EVT_PAINT(wxPreviewControlBar::OnPaint)
301 END_EVENT_TABLE()
302
303 wxPreviewControlBar::wxPreviewControlBar(wxPrintPreviewBase *preview, long buttons,
304 wxWindow *parent, const wxPoint& pos, const wxSize& size,
305 long style, const wxString& name):
306 wxPanel(parent, -1, pos, size, style, name)
307 {
308 m_printPreview = preview;
309 m_closeButton = (wxButton *) NULL;
310 m_nextPageButton = (wxButton *) NULL;
311 m_previousPageButton = (wxButton *) NULL;
312 m_printButton = (wxButton *) NULL;
313 m_zoomControl = (wxChoice *) NULL;
314 m_buttonFlags = buttons;
315 }
316
317 wxPreviewControlBar::~wxPreviewControlBar()
318 {
319 }
320
321 void wxPreviewControlBar::OnPaint(wxPaintEvent& WXUNUSED(event))
322 {
323 wxPaintDC dc(this);
324
325 int w, h;
326 GetSize(&w, &h);
327 dc.SetPen(*wxBLACK_PEN);
328 dc.SetBrush(*wxTRANSPARENT_BRUSH);
329 dc.DrawLine( 0, h-1, w, h-1 );
330 }
331
332 void wxPreviewControlBar::OnWindowClose(wxCommandEvent& WXUNUSED(event))
333 {
334 wxPreviewFrame *frame = (wxPreviewFrame *)GetParent();
335 frame->Close(TRUE);
336 }
337
338 void wxPreviewControlBar::OnPrint(void)
339 {
340 wxPrintPreviewBase *preview = GetPrintPreview();
341 preview->Print(TRUE);
342 }
343
344 void wxPreviewControlBar::OnNext(void)
345 {
346 wxPrintPreviewBase *preview = GetPrintPreview();
347 if (preview)
348 {
349 int currentPage = preview->GetCurrentPage();
350 if ((preview->GetMaxPage() > 0) &&
351 (currentPage < preview->GetMaxPage()) &&
352 preview->GetPrintout()->HasPage(currentPage + 1))
353 {
354 preview->SetCurrentPage(currentPage + 1);
355 }
356 }
357 }
358
359 void wxPreviewControlBar::OnPrevious(void)
360 {
361 wxPrintPreviewBase *preview = GetPrintPreview();
362 if (preview)
363 {
364 int currentPage = preview->GetCurrentPage();
365 if ((preview->GetMinPage() > 0) &&
366 (currentPage > preview->GetMinPage()) &&
367 preview->GetPrintout()->HasPage(currentPage - 1))
368 {
369 preview->SetCurrentPage(currentPage - 1);
370 }
371 }
372 }
373
374 void wxPreviewControlBar::OnFirst(void)
375 {
376 wxPrintPreviewBase *preview = GetPrintPreview();
377 if (preview)
378 {
379 int currentPage = preview->GetMinPage();
380 if (preview->GetPrintout()->HasPage(currentPage))
381 {
382 preview->SetCurrentPage(currentPage);
383 }
384 }
385 }
386
387 void wxPreviewControlBar::OnLast(void)
388 {
389 wxPrintPreviewBase *preview = GetPrintPreview();
390 if (preview)
391 {
392 int currentPage = preview->GetMaxPage();
393 if (preview->GetPrintout()->HasPage(currentPage))
394 {
395 preview->SetCurrentPage(currentPage);
396 }
397 }
398 }
399
400 void wxPreviewControlBar::OnGoto(void)
401 {
402 wxPrintPreviewBase *preview = GetPrintPreview();
403 if (preview)
404 {
405 long currentPage;
406
407 if (preview->GetMinPage() > 0)
408 {
409 wxString strPrompt;
410 wxString strPage;
411
412 strPrompt.Printf( _("Enter a page number between %d and %d:"),
413 preview->GetMinPage(), preview->GetMaxPage());
414 strPage.Printf( wxT("%d"), preview->GetCurrentPage() );
415
416 strPage =
417 wxGetTextFromUser( strPrompt, _("Goto Page"), strPage, GetParent());
418
419 if ( strPage.ToLong( &currentPage ) )
420 if (preview->GetPrintout()->HasPage(currentPage))
421 {
422 preview->SetCurrentPage(currentPage);
423 }
424 }
425 }
426 }
427
428 void wxPreviewControlBar::OnZoom(wxCommandEvent& WXUNUSED(event))
429 {
430 int zoom = GetZoomControl();
431 if (GetPrintPreview())
432 GetPrintPreview()->SetZoom(zoom);
433 }
434
435 void wxPreviewControlBar::CreateButtons()
436 {
437 SetSize(0, 0, 400, 40);
438
439 wxBoxSizer *item0 = new wxBoxSizer( wxHORIZONTAL );
440
441 m_closeButton = new wxButton( this, wxID_PREVIEW_CLOSE, _("&Close"), wxDefaultPosition, wxDefaultSize, 0 );
442 item0->Add( m_closeButton, 0, wxALIGN_CENTRE|wxALL, 5 );
443
444 if (m_buttonFlags & wxPREVIEW_PRINT)
445 {
446 m_printButton = new wxButton( this, wxID_PREVIEW_PRINT, _("&Print..."), wxDefaultPosition, wxDefaultSize, 0 );
447 item0->Add( m_printButton, 0, wxALIGN_CENTRE|wxALL, 5 );
448 }
449
450 if (m_buttonFlags & wxPREVIEW_FIRST)
451 {
452 m_firstPageButton = new wxButton( this, wxID_PREVIEW_FIRST, _("|<<"), wxDefaultPosition, wxDefaultSize, wxBU_EXACTFIT );
453 item0->Add( m_firstPageButton, 0, wxALIGN_CENTRE|wxALL, 5 );
454 }
455
456 if (m_buttonFlags & wxPREVIEW_PREVIOUS)
457 {
458 m_previousPageButton = new wxButton( this, wxID_PREVIEW_PREVIOUS, _("<<"), wxDefaultPosition, wxDefaultSize, wxBU_EXACTFIT );
459 item0->Add( m_previousPageButton, 0, wxALIGN_CENTRE|wxRIGHT|wxTOP|wxBOTTOM, 5 );
460 }
461
462 if (m_buttonFlags & wxPREVIEW_NEXT)
463 {
464 m_nextPageButton = new wxButton( this, wxID_PREVIEW_NEXT, _(">>"), wxDefaultPosition, wxDefaultSize, wxBU_EXACTFIT );
465 item0->Add( m_nextPageButton, 0, wxALIGN_CENTRE|wxRIGHT|wxTOP|wxBOTTOM, 5 );
466 }
467
468 if (m_buttonFlags & wxPREVIEW_LAST)
469 {
470 m_lastPageButton = new wxButton( this, wxID_PREVIEW_LAST, _(">>|"), wxDefaultPosition, wxDefaultSize, wxBU_EXACTFIT );
471 item0->Add( m_lastPageButton, 0, wxALIGN_CENTRE|wxRIGHT|wxTOP|wxBOTTOM, 5 );
472 }
473
474 if (m_buttonFlags & wxPREVIEW_GOTO)
475 {
476 m_gotoPageButton = new wxButton( this, wxID_PREVIEW_GOTO, _("&Goto..."), wxDefaultPosition, wxDefaultSize, 0 );
477 item0->Add( m_gotoPageButton, 0, wxALIGN_CENTRE|wxALL, 5 );
478 }
479
480 if (m_buttonFlags & wxPREVIEW_ZOOM)
481 {
482 wxString choices[] =
483 {
484 wxT("10%"), wxT("15%"), wxT("20%"), wxT("25%"), wxT("30%"), wxT("35%"), wxT("40%"), wxT("45%"), wxT("50%"), wxT("55%"),
485 wxT("60%"), wxT("65%"), wxT("70%"), wxT("75%"), wxT("80%"), wxT("85%"), wxT("90%"), wxT("95%"), wxT("100%"), wxT("110%"),
486 wxT("120%"), wxT("150%"), wxT("200%")
487 };
488 int n = WXSIZEOF(choices);
489
490 m_zoomControl = new wxChoice( this, wxID_PREVIEW_ZOOM, wxDefaultPosition, wxSize(70,-1), n, choices, 0 );
491 item0->Add( m_zoomControl, 0, wxALIGN_CENTRE|wxALL, 5 );
492 SetZoomControl(m_printPreview->GetZoom());
493 }
494
495 SetSizer(item0);
496 item0->Fit(this);
497 }
498
499 void wxPreviewControlBar::SetZoomControl(int zoom)
500 {
501 if (m_zoomControl)
502 {
503 int n, count = m_zoomControl->GetCount();
504 long val;
505 for (n=0; n<count; n++)
506 {
507 if (m_zoomControl->GetString(n).BeforeFirst(wxT('%')).ToLong(&val) &&
508 (val >= long(zoom)))
509 {
510 m_zoomControl->SetSelection(n);
511 return;
512 }
513 }
514
515 m_zoomControl->SetSelection(count-1);
516 }
517 }
518
519 int wxPreviewControlBar::GetZoomControl()
520 {
521 if (m_zoomControl && (m_zoomControl->GetStringSelection() != wxEmptyString))
522 {
523 long val;
524 if (m_zoomControl->GetStringSelection().BeforeFirst(wxT('%')).ToLong(&val))
525 return int(val);
526 }
527
528 return 0;
529 }
530
531
532 /*
533 * Preview frame
534 */
535
536 BEGIN_EVENT_TABLE(wxPreviewFrame, wxFrame)
537 EVT_CLOSE(wxPreviewFrame::OnCloseWindow)
538 END_EVENT_TABLE()
539
540 wxPreviewFrame::wxPreviewFrame(wxPrintPreviewBase *preview, wxWindow *parent, const wxString& title,
541 const wxPoint& pos, const wxSize& size, long style, const wxString& name):
542 wxFrame(parent, -1, title, pos, size, style, name)
543 {
544 m_printPreview = preview;
545 m_controlBar = NULL;
546 m_previewCanvas = NULL;
547
548 // Give the application icon
549 #ifdef __WXMSW__
550 wxFrame* topFrame = wxDynamicCast(wxTheApp->GetTopWindow(), wxFrame);
551 if (topFrame)
552 SetIcon(topFrame->GetIcon());
553 #endif
554 }
555
556 wxPreviewFrame::~wxPreviewFrame()
557 {
558 }
559
560 void wxPreviewFrame::OnCloseWindow(wxCloseEvent& WXUNUSED(event))
561 {
562 // MakeModal doesn't work on wxMac, especially when there
563 // are multiple top-level windows.
564 #ifndef __WXMAC__
565 MakeModal(FALSE);
566 #endif
567
568 // Need to delete the printout and the print preview
569 wxPrintout *printout = m_printPreview->GetPrintout();
570 if (printout)
571 {
572 delete printout;
573 m_printPreview->SetPrintout(NULL);
574 m_printPreview->SetCanvas(NULL);
575 m_printPreview->SetFrame(NULL);
576 }
577 delete m_printPreview;
578
579 Destroy();
580 }
581
582 void wxPreviewFrame::Initialize()
583 {
584 #if wxUSE_STATUSBAR
585 CreateStatusBar();
586 #endif
587 CreateCanvas();
588 CreateControlBar();
589
590 m_printPreview->SetCanvas(m_previewCanvas);
591 m_printPreview->SetFrame(this);
592
593 wxBoxSizer *item0 = new wxBoxSizer( wxVERTICAL );
594
595 item0->Add( m_controlBar, 0, wxGROW|wxALIGN_CENTER_VERTICAL, 5 );
596 item0->Add( m_previewCanvas, 1, wxGROW|wxALIGN_CENTER_VERTICAL, 5 );
597
598 SetAutoLayout( TRUE );
599 SetSizer( item0 );
600
601 // MakeModal doesn't work on wxMac, especially when there
602 // are multiple top-level windows.
603 #ifndef __WXMAC__
604 MakeModal(TRUE);
605 #endif
606
607 Layout();
608
609 m_printPreview->AdjustScrollbars(m_previewCanvas);
610 m_previewCanvas->SetFocus();
611 m_controlBar->SetFocus();
612 }
613
614 void wxPreviewFrame::CreateCanvas()
615 {
616 m_previewCanvas = new wxPreviewCanvas(m_printPreview, this);
617 }
618
619 void wxPreviewFrame::CreateControlBar()
620 {
621 long buttons = wxPREVIEW_DEFAULT;
622 if (m_printPreview->GetPrintoutForPrinting())
623 buttons |= wxPREVIEW_PRINT;
624
625 m_controlBar = new wxPreviewControlBar(m_printPreview, buttons, this, wxPoint(0, 0), wxSize(400, 40));
626 m_controlBar->CreateButtons();
627 }
628
629 /*
630 * Print preview
631 */
632
633 wxPrintPreviewBase::wxPrintPreviewBase(wxPrintout *printout,
634 wxPrintout *printoutForPrinting,
635 wxPrintData *data)
636 {
637 if (data)
638 m_printDialogData = (*data);
639
640 Init(printout, printoutForPrinting);
641 }
642
643 wxPrintPreviewBase::wxPrintPreviewBase(wxPrintout *printout,
644 wxPrintout *printoutForPrinting,
645 wxPrintDialogData *data)
646 {
647 if (data)
648 m_printDialogData = (*data);
649
650 Init(printout, printoutForPrinting);
651 }
652
653 void wxPrintPreviewBase::Init(wxPrintout *printout,
654 wxPrintout *printoutForPrinting)
655 {
656 m_isOk = TRUE;
657 m_previewPrintout = printout;
658 if (m_previewPrintout)
659 m_previewPrintout->SetIsPreview(TRUE);
660
661 m_printPrintout = printoutForPrinting;
662
663 m_previewCanvas = NULL;
664 m_previewFrame = NULL;
665 m_previewBitmap = NULL;
666 m_currentPage = 1;
667 m_currentZoom = 70;
668 m_topMargin = 40;
669 m_leftMargin = 40;
670 m_pageWidth = 0;
671 m_pageHeight = 0;
672 m_printingPrepared = FALSE;
673 m_minPage = 1;
674 m_maxPage = 1;
675 }
676
677 wxPrintPreviewBase::~wxPrintPreviewBase()
678 {
679 if (m_previewPrintout)
680 delete m_previewPrintout;
681 if (m_previewBitmap)
682 delete m_previewBitmap;
683 if (m_printPrintout)
684 delete m_printPrintout;
685 }
686
687 bool wxPrintPreviewBase::SetCurrentPage(int pageNum)
688 {
689 if (m_currentPage == pageNum)
690 return TRUE;
691
692 m_currentPage = pageNum;
693 if (m_previewBitmap)
694 {
695 delete m_previewBitmap;
696 m_previewBitmap = NULL;
697 }
698
699 if (m_previewCanvas)
700 {
701 AdjustScrollbars(m_previewCanvas);
702
703 if (!RenderPage(pageNum))
704 return FALSE;
705 m_previewCanvas->Refresh();
706 m_previewCanvas->SetFocus();
707 }
708 return TRUE;
709 }
710
711 bool wxPrintPreviewBase::PaintPage(wxPreviewCanvas *canvas, wxDC& dc)
712 {
713 DrawBlankPage(canvas, dc);
714
715 if (!m_previewBitmap)
716 if (!RenderPage(m_currentPage))
717 return FALSE;
718
719 if (!m_previewBitmap)
720 return FALSE;
721
722 if (!canvas)
723 return FALSE;
724
725 int canvasWidth, canvasHeight;
726 canvas->GetSize(&canvasWidth, &canvasHeight);
727
728 double zoomScale = ((float)m_currentZoom/(float)100);
729 double actualWidth = (zoomScale*m_pageWidth*m_previewScale);
730 // float actualHeight = (float)(zoomScale*m_pageHeight*m_previewScale);
731
732 int x = (int) ((canvasWidth - actualWidth)/2.0);
733 if (x < m_leftMargin)
734 x = m_leftMargin;
735 int y = m_topMargin;
736
737 wxMemoryDC temp_dc;
738 temp_dc.SelectObject(*m_previewBitmap);
739
740 dc.Blit(x, y, m_previewBitmap->GetWidth(), m_previewBitmap->GetHeight(), &temp_dc, 0, 0);
741
742 temp_dc.SelectObject(wxNullBitmap);
743
744 return TRUE;
745 }
746
747 // Adjusts the scrollbars for the current scale
748 void wxPrintPreviewBase::AdjustScrollbars(wxPreviewCanvas *canvas)
749 {
750 if (!canvas)
751 return ;
752
753 int canvasWidth, canvasHeight;
754 canvas->GetSize(&canvasWidth, &canvasHeight);
755
756 double zoomScale = ((float)m_currentZoom/(float)100);
757 double actualWidth = (zoomScale*m_pageWidth*m_previewScale);
758 double actualHeight = (zoomScale*m_pageHeight*m_previewScale);
759
760 // Set the scrollbars appropriately
761 int totalWidth = (int)(actualWidth + 2*m_leftMargin);
762 int totalHeight = (int)(actualHeight + 2*m_topMargin);
763 int scrollUnitsX = totalWidth/10;
764 int scrollUnitsY = totalHeight/10;
765 wxSize virtualSize = canvas->GetVirtualSize();
766 if (virtualSize.GetWidth() != totalWidth || virtualSize.GetHeight() != totalHeight)
767 canvas->SetScrollbars(10, 10, scrollUnitsX, scrollUnitsY, 0, 0, TRUE);
768 }
769
770 bool wxPrintPreviewBase::RenderPage(int pageNum)
771 {
772 wxBusyCursor busy;
773
774 int canvasWidth, canvasHeight;
775
776 if (!m_previewCanvas)
777 {
778 wxFAIL_MSG(_T("wxPrintPreviewBase::RenderPage: must use wxPrintPreviewBase::SetCanvas to let me know about the canvas!"));
779
780 return FALSE;
781 }
782 m_previewCanvas->GetSize(&canvasWidth, &canvasHeight);
783
784 double zoomScale = (m_currentZoom/100.0);
785 int actualWidth = (int)(zoomScale*m_pageWidth*m_previewScale);
786 int actualHeight = (int)(zoomScale*m_pageHeight*m_previewScale);
787
788 if (!m_previewBitmap)
789 {
790 m_previewBitmap = new wxBitmap((int)actualWidth, (int)actualHeight);
791 if (!m_previewBitmap || !m_previewBitmap->Ok())
792 {
793 if (m_previewBitmap) {
794 delete m_previewBitmap;
795 m_previewBitmap = NULL;
796 }
797 wxMessageBox(_("Sorry, not enough memory to create a preview."), _("Print Preview Failure"), wxOK);
798 return FALSE;
799 }
800 }
801
802 wxMemoryDC memoryDC;
803 memoryDC.SelectObject(*m_previewBitmap);
804
805 memoryDC.Clear();
806
807 m_previewPrintout->SetDC(&memoryDC);
808 m_previewPrintout->SetPageSizePixels(m_pageWidth, m_pageHeight);
809
810 // Need to delay OnPreparePrinting until here, so we have enough information.
811 if (!m_printingPrepared)
812 {
813 m_previewPrintout->OnPreparePrinting();
814 int selFrom, selTo;
815 m_previewPrintout->GetPageInfo(&m_minPage, &m_maxPage, &selFrom, &selTo);
816 m_printingPrepared = TRUE;
817 }
818
819 m_previewPrintout->OnBeginPrinting();
820
821 if (!m_previewPrintout->OnBeginDocument(m_printDialogData.GetFromPage(), m_printDialogData.GetToPage()))
822 {
823 wxMessageBox(_("Could not start document preview."), _("Print Preview Failure"), wxOK);
824
825 memoryDC.SelectObject(wxNullBitmap);
826
827 delete m_previewBitmap;
828 m_previewBitmap = NULL;
829 return FALSE;
830 }
831
832 m_previewPrintout->OnPrintPage(pageNum);
833 m_previewPrintout->OnEndDocument();
834 m_previewPrintout->OnEndPrinting();
835
836 m_previewPrintout->SetDC(NULL);
837
838 memoryDC.SelectObject(wxNullBitmap);
839
840 #if wxUSE_STATUSBAR
841 wxString status;
842 if (m_maxPage != 0)
843 status = wxString::Format(_("Page %d of %d"), pageNum, m_maxPage);
844 else
845 status = wxString::Format(_("Page %d"), pageNum);
846
847 if (m_previewFrame)
848 m_previewFrame->SetStatusText(status);
849 #endif
850
851 return TRUE;
852 }
853
854
855 bool wxPrintPreviewBase::DrawBlankPage(wxPreviewCanvas *canvas, wxDC& dc)
856 {
857 int canvasWidth, canvasHeight;
858 canvas->GetSize(&canvasWidth, &canvasHeight);
859
860 float zoomScale = (float)((float)m_currentZoom/(float)100);
861 float actualWidth = zoomScale*m_pageWidth*m_previewScale;
862 float actualHeight = zoomScale*m_pageHeight*m_previewScale;
863
864 float x = (float)((canvasWidth - actualWidth)/2.0);
865 if (x < m_leftMargin)
866 x = (float)m_leftMargin;
867 float y = (float)m_topMargin;
868
869 // Draw shadow, allowing for 1-pixel border AROUND the actual page
870 int shadowOffset = 4;
871 dc.SetPen(*wxBLACK_PEN);
872 dc.SetBrush(*wxBLACK_BRUSH);
873 /*
874 dc.DrawRectangle((int)(x-1 + shadowOffset), (int)(y-1 + shadowOffset), (int)(actualWidth+2), (int)(actualHeight+2));
875 */
876 dc.DrawRectangle((int)(x + shadowOffset), (int)(y + actualHeight+1), (int)(actualWidth), shadowOffset);
877 dc.DrawRectangle((int)(x + actualWidth), (int)(y + shadowOffset), shadowOffset, (int)(actualHeight));
878
879 // Draw blank page allowing for 1-pixel border AROUND the actual page
880 dc.SetPen(*wxBLACK_PEN);
881 dc.SetBrush(*wxWHITE_BRUSH);
882
883 /*
884 wxRegion update_region = canvas->GetUpdateRegion();
885 wxRect r = update_region.GetBox();
886
887 printf( "x: %d y: %d w: %d h: %d.\n", (int)r.x, (int)r.y, (int)r.width, (int)r.height );
888 */
889
890 dc.DrawRectangle((int)(x-2), (int)(y-1), (int)(actualWidth+3), (int)(actualHeight+2));
891
892 return TRUE;
893 }
894
895 void wxPrintPreviewBase::SetZoom(int percent)
896 {
897 if (m_currentZoom == percent)
898 return;
899
900 m_currentZoom = percent;
901 if (m_previewBitmap)
902 {
903 delete m_previewBitmap;
904 m_previewBitmap = NULL;
905 }
906
907 if (m_previewCanvas)
908 {
909 AdjustScrollbars(m_previewCanvas);
910 RenderPage(m_currentPage);
911 ((wxScrolledWindow *) m_previewCanvas)->Scroll(0, 0);
912 m_previewCanvas->ClearBackground();
913 m_previewCanvas->Refresh();
914 m_previewCanvas->SetFocus();
915 }
916 }
917
918 #endif // wxUSE_PRINTING_ARCHITECTURE