]> git.saurik.com Git - wxWidgets.git/blob - src/common/prntbase.cpp
OS/2 must have positive control IDs.
[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 and Markus Holzem
9 // Licence: wxWindows license
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 #endif
41
42 #include "wx/prntbase.h"
43 #include "wx/dcprint.h"
44 #include "wx/printdlg.h"
45 #include "wx/module.h"
46
47 #include <stdlib.h>
48 #include <string.h>
49
50 #include <wx/sizer.h>
51
52 #ifdef __WXMSW__
53 #include "wx/msw/private.h"
54 #include <commdlg.h>
55
56 #ifndef __WIN32__
57 #include <print.h>
58 #endif
59 #endif // __WXMSW__
60
61 IMPLEMENT_CLASS(wxPrinterBase, wxObject)
62 IMPLEMENT_ABSTRACT_CLASS(wxPrintout, wxObject)
63 IMPLEMENT_CLASS(wxPreviewCanvas, wxWindow)
64 IMPLEMENT_CLASS(wxPreviewControlBar, wxWindow)
65 IMPLEMENT_CLASS(wxPreviewFrame, wxFrame)
66 IMPLEMENT_CLASS(wxPrintPreviewBase, wxObject)
67
68 BEGIN_EVENT_TABLE(wxPrintAbortDialog, wxDialog)
69 EVT_BUTTON(wxID_CANCEL, wxPrintAbortDialog::OnCancel)
70 END_EVENT_TABLE()
71
72 BEGIN_EVENT_TABLE(wxPreviewCanvas, wxScrolledWindow)
73 EVT_PAINT(wxPreviewCanvas::OnPaint)
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 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_APPWORKSPACE));
193
194 SetScrollbars(15, 18, 100, 100);
195 }
196
197 wxPreviewCanvas::~wxPreviewCanvas()
198 {
199 }
200
201 void wxPreviewCanvas::OnPaint(wxPaintEvent& WXUNUSED(event))
202 {
203 wxPaintDC dc(this);
204 PrepareDC( dc );
205
206 /*
207 #ifdef __WXGTK__
208 if (!GetUpdateRegion().IsEmpty())
209 dc.SetClippingRegion( GetUpdateRegion() );
210 #endif
211 */
212
213 if (m_printPreview)
214 {
215 m_printPreview->PaintPage(this, dc);
216 }
217 }
218
219 // Responds to colour changes, and passes event on to children.
220 void wxPreviewCanvas::OnSysColourChanged(wxSysColourChangedEvent& event)
221 {
222 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_APPWORKSPACE));
223 Refresh();
224
225 // Propagate the event to the non-top-level children
226 wxWindow::OnSysColourChanged(event);
227 }
228
229 /*
230 * Preview control bar
231 */
232
233 BEGIN_EVENT_TABLE(wxPreviewControlBar, wxPanel)
234 EVT_BUTTON(wxID_PREVIEW_CLOSE, wxPreviewControlBar::OnWindowClose)
235 EVT_BUTTON(wxID_PREVIEW_PRINT, wxPreviewControlBar::OnPrint)
236 EVT_BUTTON(wxID_PREVIEW_PREVIOUS, wxPreviewControlBar::OnPreviousButton)
237 EVT_BUTTON(wxID_PREVIEW_NEXT, wxPreviewControlBar::OnNextButton)
238 EVT_BUTTON(wxID_PREVIEW_FIRST, wxPreviewControlBar::OnFirstButton)
239 EVT_BUTTON(wxID_PREVIEW_LAST, wxPreviewControlBar::OnLastButton)
240 EVT_BUTTON(wxID_PREVIEW_GOTO, wxPreviewControlBar::OnGotoButton)
241 EVT_CHAR(wxPreviewControlBar::OnChar)
242 EVT_CHOICE(wxID_PREVIEW_ZOOM, wxPreviewControlBar::OnZoom)
243 EVT_PAINT(wxPreviewControlBar::OnPaint)
244 END_EVENT_TABLE()
245
246 wxPreviewControlBar::wxPreviewControlBar(wxPrintPreviewBase *preview, long buttons,
247 wxWindow *parent, const wxPoint& pos, const wxSize& size,
248 long style, const wxString& name):
249 wxPanel(parent, -1, pos, size, style, name)
250 {
251 m_printPreview = preview;
252 m_closeButton = (wxButton *) NULL;
253 m_nextPageButton = (wxButton *) NULL;
254 m_previousPageButton = (wxButton *) NULL;
255 m_printButton = (wxButton *) NULL;
256 m_zoomControl = (wxChoice *) NULL;
257 m_buttonFlags = buttons;
258 }
259
260 wxPreviewControlBar::~wxPreviewControlBar()
261 {
262 }
263
264 void wxPreviewControlBar::OnPaint(wxPaintEvent& WXUNUSED(event))
265 {
266 wxPaintDC dc(this);
267
268 int w, h;
269 GetSize(&w, &h);
270 dc.SetPen(*wxBLACK_PEN);
271 dc.SetBrush(*wxTRANSPARENT_BRUSH);
272 dc.DrawLine( 0, h-1, w, h-1 );
273 }
274
275 void wxPreviewControlBar::OnWindowClose(wxCommandEvent& WXUNUSED(event))
276 {
277 wxPreviewFrame *frame = (wxPreviewFrame *)GetParent();
278 frame->Close(TRUE);
279 }
280
281 void wxPreviewControlBar::OnPrint(wxCommandEvent& WXUNUSED(event))
282 {
283 wxPrintPreviewBase *preview = GetPrintPreview();
284 preview->Print(TRUE);
285 }
286
287 void wxPreviewControlBar::OnChar(wxKeyEvent &event)
288 {
289 switch(event.KeyCode())
290 {
291 case WXK_NEXT:
292 OnNext(); break;
293 case WXK_PRIOR:
294 OnPrevious(); break;
295 case WXK_HOME:
296 OnFirst(); break;
297 case WXK_END:
298 OnLast(); break;
299 case WXK_TAB:
300 OnGoto(); break;
301 default:
302 event.Skip();
303 }
304 }
305
306 void wxPreviewControlBar::OnNext(void)
307 {
308 wxPrintPreviewBase *preview = GetPrintPreview();
309 if (preview)
310 {
311 int currentPage = preview->GetCurrentPage();
312 if ((preview->GetMaxPage() > 0) &&
313 (currentPage < preview->GetMaxPage()) &&
314 preview->GetPrintout()->HasPage(currentPage + 1))
315 {
316 preview->SetCurrentPage(currentPage + 1);
317 }
318 }
319 }
320
321 void wxPreviewControlBar::OnPrevious(void)
322 {
323 wxPrintPreviewBase *preview = GetPrintPreview();
324 if (preview)
325 {
326 int currentPage = preview->GetCurrentPage();
327 if ((preview->GetMinPage() > 0) &&
328 (currentPage > preview->GetMinPage()) &&
329 preview->GetPrintout()->HasPage(currentPage - 1))
330 {
331 preview->SetCurrentPage(currentPage - 1);
332 }
333 }
334 }
335
336 void wxPreviewControlBar::OnFirst(void)
337 {
338 wxPrintPreviewBase *preview = GetPrintPreview();
339 if (preview)
340 {
341 int currentPage = preview->GetMinPage();
342 if (preview->GetPrintout()->HasPage(currentPage))
343 {
344 preview->SetCurrentPage(currentPage);
345 }
346 }
347 }
348
349 void wxPreviewControlBar::OnLast(void)
350 {
351 wxPrintPreviewBase *preview = GetPrintPreview();
352 if (preview)
353 {
354 int currentPage = preview->GetMaxPage();
355 if (preview->GetPrintout()->HasPage(currentPage))
356 {
357 preview->SetCurrentPage(currentPage);
358 }
359 }
360 }
361
362 void wxPreviewControlBar::OnGoto(void)
363 {
364 wxPrintPreviewBase *preview = GetPrintPreview();
365 if (preview)
366 {
367 long currentPage;
368
369 if (preview->GetMinPage() > 0)
370 {
371 wxString strPrompt;
372 wxString strPage;
373
374 strPrompt.Printf( _("%d...%d"),
375 preview->GetMinPage(), preview->GetMaxPage());
376 strPage.Printf( _("%d"), preview->GetCurrentPage() );
377
378 strPage =
379 wxGetTextFromUser( strPrompt, _("Goto Page"), strPage);
380
381 if ( strPage.ToLong( &currentPage ) )
382 if (preview->GetPrintout()->HasPage(currentPage))
383 {
384 preview->SetCurrentPage(currentPage);
385 }
386 }
387 }
388 }
389
390 void wxPreviewControlBar::OnZoom(wxCommandEvent& WXUNUSED(event))
391 {
392 int zoom = GetZoomControl();
393 if (GetPrintPreview())
394 GetPrintPreview()->SetZoom(zoom);
395 }
396
397 void wxPreviewControlBar::CreateButtons()
398 {
399 SetSize(0, 0, 400, 40);
400
401 /*
402 #ifdef __WXMSW__
403 int fontSize = 9;
404 #else
405 int fontSize = 10;
406 #endif
407
408 wxFont buttonFont(fontSize, wxSWISS, wxNORMAL, wxBOLD);
409 SetFont(buttonFont);
410 */
411
412 int buttonWidth = 60;
413 int buttonNavigation = 30;
414 #ifdef __WXGTK__
415 int buttonHeight = -1;
416 #else
417 int buttonHeight = 24;
418 #endif
419
420 int x = 5;
421 int y = 5;
422
423 #ifdef __WXMOTIF__
424 int gap = 15;
425 #else
426 int gap = 5;
427 #endif
428
429 m_closeButton = new wxButton(this, wxID_PREVIEW_CLOSE, _("Close"),
430 wxPoint(x, y), wxSize(buttonWidth, buttonHeight));
431
432 x += gap + buttonWidth;
433
434 if (m_buttonFlags & wxPREVIEW_PRINT)
435 {
436 m_printButton = new wxButton(this, wxID_PREVIEW_PRINT, _("Print..."), wxPoint(x, y),
437 wxSize(buttonWidth, buttonHeight));
438 x += gap + buttonWidth;
439 }
440
441 if (m_buttonFlags & wxPREVIEW_FIRST)
442 {
443 m_firstPageButton = new wxButton(this, wxID_PREVIEW_FIRST, wxT("|<<"), wxPoint(x, y),
444 wxSize(buttonNavigation, buttonHeight));
445 x += gap + buttonNavigation;
446 }
447
448 if (m_buttonFlags & wxPREVIEW_PREVIOUS)
449 {
450 m_previousPageButton = new wxButton(this, wxID_PREVIEW_PREVIOUS, wxT("<<"), wxPoint(x, y),
451 wxSize(buttonNavigation, buttonHeight));
452 x += gap + buttonNavigation;
453 }
454
455 if (m_buttonFlags & wxPREVIEW_NEXT)
456 {
457 m_nextPageButton = new wxButton(this, wxID_PREVIEW_NEXT, wxT(">>"),
458 wxPoint(x, y), wxSize(buttonNavigation, buttonHeight));
459 x += gap + buttonNavigation;
460 }
461
462 if (m_buttonFlags & wxPREVIEW_LAST)
463 {
464 m_lastPageButton = new wxButton(this, wxID_PREVIEW_LAST, wxT(">>|"), wxPoint(x, y),
465 wxSize(buttonNavigation, buttonHeight));
466 x += gap + buttonNavigation;
467 }
468
469 if (m_buttonFlags & wxPREVIEW_GOTO)
470 {
471 m_gotoPageButton = new wxButton(this, wxID_PREVIEW_GOTO, wxT("Goto..."), wxPoint(x, y),
472 wxSize(buttonWidth, buttonHeight));
473 x += gap + buttonWidth;
474 }
475
476 if (m_buttonFlags & wxPREVIEW_ZOOM)
477 {
478 static const char *choices[] =
479 {
480 "10%", "15%", "20%", "25%", "30%", "35%", "40%", "45%", "50%", "55%",
481 "60%", "65%", "70%", "75%", "80%", "85%", "90%", "95%", "100%", "110%",
482 "120%", "150%", "200%"
483 };
484
485 int n = WXSIZEOF(choices);
486
487 wxString* strings = new wxString[n];
488 int i;
489 for (i = 0; i < n; i++ )
490 strings[i] = choices[i];
491
492 m_zoomControl = new wxChoice(this, wxID_PREVIEW_ZOOM,
493 wxPoint(x, y),
494 wxSize(100, -1),
495 n,
496 strings
497 );
498 delete[] strings;
499
500 SetZoomControl(m_printPreview->GetZoom());
501 }
502
503 // m_closeButton->SetDefault();
504 }
505
506 void wxPreviewControlBar::SetZoomControl(int zoom)
507 {
508 char buf[20];
509 sprintf(buf, "%d%%", zoom);
510 // Someone is calling methods that do no exist in wxChoice!! So I'll just comment out for VA for now
511 if (m_zoomControl)
512 m_zoomControl->SetStringSelection(buf);
513 }
514
515 int wxPreviewControlBar::GetZoomControl()
516 {
517 wxChar buf[20];
518 if (m_zoomControl && (m_zoomControl->GetStringSelection() != wxT("")))
519 {
520 wxStrcpy(buf, m_zoomControl->GetStringSelection());
521 buf[wxStrlen(buf) - 1] = 0;
522 return (int)wxAtoi(buf);
523 }
524 else return 0;
525 }
526
527
528 /*
529 * Preview frame
530 */
531
532 BEGIN_EVENT_TABLE(wxPreviewFrame, wxFrame)
533 EVT_CLOSE(wxPreviewFrame::OnCloseWindow)
534 END_EVENT_TABLE()
535
536 wxPreviewFrame::wxPreviewFrame(wxPrintPreviewBase *preview, wxFrame *parent, const wxString& title,
537 const wxPoint& pos, const wxSize& size, long style, const wxString& name):
538 wxFrame(parent, -1, title, pos, size, style, name)
539 {
540 m_printPreview = preview;
541 m_controlBar = NULL;
542 m_previewCanvas = NULL;
543 }
544
545 wxPreviewFrame::~wxPreviewFrame()
546 {
547 }
548
549 void wxPreviewFrame::OnCloseWindow(wxCloseEvent& WXUNUSED(event))
550 {
551 MakeModal(FALSE);
552
553 // Need to delete the printout and the print preview
554 wxPrintout *printout = m_printPreview->GetPrintout();
555 if (printout)
556 {
557 delete printout;
558 m_printPreview->SetPrintout(NULL);
559 m_printPreview->SetCanvas(NULL);
560 m_printPreview->SetFrame(NULL);
561 }
562 delete m_printPreview;
563
564 Destroy();
565 }
566
567 void wxPreviewFrame::Initialize()
568 {
569 CreateStatusBar();
570
571 CreateCanvas();
572 CreateControlBar();
573
574 m_printPreview->SetCanvas(m_previewCanvas);
575 m_printPreview->SetFrame(this);
576
577 // Set layout constraints here
578
579 // Control bar constraints
580 wxLayoutConstraints *c1 = new wxLayoutConstraints;
581 // int w, h;
582 // m_controlBar->GetSize(&w, &h);
583 int h;
584 #if (defined(__WXMSW__) || defined(__WXGTK__))
585 h = 40;
586 #else
587 h = 60;
588 #endif
589
590 c1->left.SameAs (this, wxLeft);
591 c1->top.SameAs (this, wxTop);
592 c1->right.SameAs (this, wxRight);
593 c1->height.Absolute (h);
594
595 m_controlBar->SetConstraints(c1);
596
597 // Canvas constraints
598 wxLayoutConstraints *c2 = new wxLayoutConstraints;
599
600 c2->left.SameAs (this, wxLeft);
601 c2->top.Below (m_controlBar);
602 c2->right.SameAs (this, wxRight);
603 c2->bottom.SameAs (this, wxBottom);
604
605 m_previewCanvas->SetConstraints(c2);
606
607 SetAutoLayout(TRUE);
608
609 MakeModal(TRUE);
610
611 Layout();
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
674 // Too soon! Moved to RenderPage.
675 // printout->OnPreparePrinting();
676
677 // Get some parameters from the printout, if defined
678 int selFrom, selTo;
679 printout->GetPageInfo(&m_minPage, &m_maxPage, &selFrom, &selTo);
680 }
681
682 wxPrintPreviewBase::~wxPrintPreviewBase()
683 {
684 if (m_previewPrintout)
685 delete m_previewPrintout;
686 if (m_previewBitmap)
687 delete m_previewBitmap;
688 if (m_printPrintout)
689 delete m_printPrintout;
690 }
691
692 bool wxPrintPreviewBase::SetCurrentPage(int pageNum)
693 {
694 if (m_currentPage == pageNum)
695 return TRUE;
696
697 m_currentPage = pageNum;
698 if (m_previewBitmap)
699 {
700 delete m_previewBitmap;
701 m_previewBitmap = NULL;
702 }
703
704 if (m_previewCanvas)
705 {
706 if (!RenderPage(pageNum))
707 return FALSE;
708 m_previewCanvas->Refresh();
709 }
710 return TRUE;
711 }
712
713 bool wxPrintPreviewBase::PaintPage(wxWindow *canvas, wxDC& dc)
714 {
715 DrawBlankPage(canvas, dc);
716
717 if (!m_previewBitmap)
718 if (!RenderPage(m_currentPage))
719 return FALSE;
720
721 if (!m_previewBitmap)
722 return FALSE;
723
724 if (!canvas)
725 return FALSE;
726
727 int canvasWidth, canvasHeight;
728 canvas->GetSize(&canvasWidth, &canvasHeight);
729
730 double zoomScale = ((float)m_currentZoom/(float)100);
731 double actualWidth = (zoomScale*m_pageWidth*m_previewScale);
732 // float actualHeight = (float)(zoomScale*m_pageHeight*m_previewScale);
733
734 int x = (int) ((canvasWidth - actualWidth)/2.0);
735 if (x < m_leftMargin)
736 x = m_leftMargin;
737 int y = m_topMargin;
738
739 wxMemoryDC temp_dc;
740 temp_dc.SelectObject(*m_previewBitmap);
741
742 dc.Blit(x, y, m_previewBitmap->GetWidth(), m_previewBitmap->GetHeight(), &temp_dc, 0, 0);
743
744 temp_dc.SelectObject(wxNullBitmap);
745
746 return 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 m_printingPrepared = TRUE;
800 }
801
802 m_previewPrintout->OnBeginPrinting();
803
804 if (!m_previewPrintout->OnBeginDocument(m_printDialogData.GetFromPage(), m_printDialogData.GetToPage()))
805 {
806 wxMessageBox(_("Could not start document preview."), _("Print Preview Failure"), wxOK);
807
808 memoryDC.SelectObject(wxNullBitmap);
809
810 delete m_previewBitmap;
811 m_previewBitmap = NULL;
812 return FALSE;
813 }
814
815 m_previewPrintout->OnPrintPage(pageNum);
816 m_previewPrintout->OnEndDocument();
817 m_previewPrintout->OnEndPrinting();
818
819 m_previewPrintout->SetDC(NULL);
820
821 memoryDC.SelectObject(wxNullBitmap);
822
823 wxChar buf[200];
824 if (m_maxPage != 0)
825 wxSprintf(buf, _("Page %d of %d"), pageNum, m_maxPage);
826 else
827 wxSprintf(buf, _("Page %d"), pageNum);
828
829 if (m_previewFrame)
830 m_previewFrame->SetStatusText(buf);
831
832 return TRUE;
833 }
834
835
836 bool wxPrintPreviewBase::DrawBlankPage(wxWindow *canvas, wxDC& dc)
837 {
838 int canvasWidth, canvasHeight;
839 canvas->GetSize(&canvasWidth, &canvasHeight);
840
841 float zoomScale = (float)((float)m_currentZoom/(float)100);
842 float actualWidth = zoomScale*m_pageWidth*m_previewScale;
843 float actualHeight = zoomScale*m_pageHeight*m_previewScale;
844
845 float x = (float)((canvasWidth - actualWidth)/2.0);
846 if (x < m_leftMargin)
847 x = (float)m_leftMargin;
848 float y = (float)m_topMargin;
849
850 // Draw shadow, allowing for 1-pixel border AROUND the actual page
851 int shadowOffset = 4;
852 dc.SetPen(*wxBLACK_PEN);
853 dc.SetBrush(*wxBLACK_BRUSH);
854 /*
855 dc.DrawRectangle((int)(x-1 + shadowOffset), (int)(y-1 + shadowOffset), (int)(actualWidth+2), (int)(actualHeight+2));
856 */
857 dc.DrawRectangle((int)(x + shadowOffset), (int)(y + actualHeight+1), (int)(actualWidth), shadowOffset);
858 dc.DrawRectangle((int)(x + actualWidth), (int)(y + shadowOffset), shadowOffset, (int)(actualHeight));
859
860 // Draw blank page allowing for 1-pixel border AROUND the actual page
861 dc.SetPen(*wxBLACK_PEN);
862 dc.SetBrush(*wxWHITE_BRUSH);
863
864 /*
865 wxRegion update_region = canvas->GetUpdateRegion();
866 wxRect r = update_region.GetBox();
867
868 printf( "x: %d y: %d w: %d h: %d.\n", (int)r.x, (int)r.y, (int)r.width, (int)r.height );
869 */
870
871 dc.DrawRectangle((int)(x-2), (int)(y-1), (int)(actualWidth+3), (int)(actualHeight+2));
872
873 return TRUE;
874 }
875
876 void wxPrintPreviewBase::SetZoom(int percent)
877 {
878 if (m_currentZoom == percent)
879 return;
880
881 m_currentZoom = percent;
882 if (m_previewBitmap)
883 {
884 delete m_previewBitmap;
885 m_previewBitmap = NULL;
886 }
887
888 if (m_previewCanvas)
889 {
890 RenderPage(m_currentPage);
891 ((wxScrolledWindow *) m_previewCanvas)->Scroll(0, 0);
892 m_previewCanvas->Clear();
893 m_previewCanvas->Refresh();
894 }
895 }
896
897 #endif // wxUSE_PRINTING_ARCHITECTURE