]> git.saurik.com Git - wxWidgets.git/blob - src/common/prntbase.cpp
Return wxPrintAbortDialog from CreateAbortWindow().
[wxWidgets.git] / src / common / prntbase.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/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 // For compilers that support precompilation, includes "wx.h".
13 #include "wx/wxprec.h"
14
15 #ifdef __BORLANDC__
16 #pragma hdrstop
17 #endif
18
19 #if wxUSE_PRINTING_ARCHITECTURE
20
21 #include "wx/dcprint.h"
22
23 #ifndef WX_PRECOMP
24 #if defined(__WXMSW__)
25 #include "wx/msw/wrapcdlg.h"
26 #endif // MSW
27 #include "wx/utils.h"
28 #include "wx/dc.h"
29 #include "wx/app.h"
30 #include "wx/math.h"
31 #include "wx/msgdlg.h"
32 #include "wx/layout.h"
33 #include "wx/choice.h"
34 #include "wx/button.h"
35 #include "wx/bmpbuttn.h"
36 #include "wx/settings.h"
37 #include "wx/dcmemory.h"
38 #include "wx/dcclient.h"
39 #include "wx/stattext.h"
40 #include "wx/intl.h"
41 #include "wx/textdlg.h"
42 #include "wx/sizer.h"
43 #include "wx/module.h"
44 #endif // !WX_PRECOMP
45
46 #include "wx/prntbase.h"
47 #include "wx/printdlg.h"
48 #include "wx/print.h"
49 #include "wx/dcprint.h"
50 #include "wx/artprov.h"
51
52 #include <stdlib.h>
53 #include <string.h>
54
55 #if defined(__WXMSW__) && !defined(__WXUNIVERSAL__)
56 #include "wx/msw/printdlg.h"
57 #include "wx/msw/dcprint.h"
58 #elif defined(__WXMAC__)
59 #include "wx/osx/printdlg.h"
60 #include "wx/osx/private/print.h"
61 #include "wx/osx/dcprint.h"
62 #elif defined(__WXPM__)
63 #include "wx/os2/dcprint.h"
64 #include "wx/generic/prntdlgg.h"
65 #else
66 #include "wx/generic/prntdlgg.h"
67 #include "wx/dcps.h"
68 #endif
69
70 #ifdef __WXMSW__
71 #ifndef __WIN32__
72 #include <print.h>
73 #endif
74 #endif // __WXMSW__
75
76 //----------------------------------------------------------------------------
77 // wxPrintFactory
78 //----------------------------------------------------------------------------
79
80 wxPrintFactory *wxPrintFactory::m_factory = NULL;
81
82 void wxPrintFactory::SetPrintFactory( wxPrintFactory *factory )
83 {
84 if (wxPrintFactory::m_factory)
85 delete wxPrintFactory::m_factory;
86
87 wxPrintFactory::m_factory = factory;
88 }
89
90 wxPrintFactory *wxPrintFactory::GetFactory()
91 {
92 if (!wxPrintFactory::m_factory)
93 wxPrintFactory::m_factory = new wxNativePrintFactory;
94
95 return wxPrintFactory::m_factory;
96 }
97
98 //----------------------------------------------------------------------------
99 // wxNativePrintFactory
100 //----------------------------------------------------------------------------
101
102 wxPrinterBase *wxNativePrintFactory::CreatePrinter( wxPrintDialogData *data )
103 {
104 #if defined(__WXMSW__) && !defined(__WXUNIVERSAL__)
105 return new wxWindowsPrinter( data );
106 #elif defined(__WXMAC__)
107 return new wxMacPrinter( data );
108 #elif defined(__WXPM__)
109 return new wxOS2Printer( data );
110 #else
111 return new wxPostScriptPrinter( data );
112 #endif
113 }
114
115 wxPrintPreviewBase *wxNativePrintFactory::CreatePrintPreview( wxPrintout *preview,
116 wxPrintout *printout, wxPrintDialogData *data )
117 {
118 #if defined(__WXMSW__) && !defined(__WXUNIVERSAL__)
119 return new wxWindowsPrintPreview( preview, printout, data );
120 #elif defined(__WXMAC__)
121 return new wxMacPrintPreview( preview, printout, data );
122 #elif defined(__WXPM__)
123 return new wxOS2PrintPreview( preview, printout, data );
124 #else
125 return new wxPostScriptPrintPreview( preview, printout, data );
126 #endif
127 }
128
129 wxPrintPreviewBase *wxNativePrintFactory::CreatePrintPreview( wxPrintout *preview,
130 wxPrintout *printout, wxPrintData *data )
131 {
132 #if defined(__WXMSW__) && !defined(__WXUNIVERSAL__)
133 return new wxWindowsPrintPreview( preview, printout, data );
134 #elif defined(__WXMAC__)
135 return new wxMacPrintPreview( preview, printout, data );
136 #elif defined(__WXPM__)
137 return new wxOS2PrintPreview( preview, printout, data );
138 #else
139 return new wxPostScriptPrintPreview( preview, printout, data );
140 #endif
141 }
142
143 wxPrintDialogBase *wxNativePrintFactory::CreatePrintDialog( wxWindow *parent,
144 wxPrintDialogData *data )
145 {
146 #if defined(__WXMSW__) && !defined(__WXUNIVERSAL__)
147 return new wxWindowsPrintDialog( parent, data );
148 #elif defined(__WXMAC__)
149 return new wxMacPrintDialog( parent, data );
150 #else
151 return new wxGenericPrintDialog( parent, data );
152 #endif
153 }
154
155 wxPrintDialogBase *wxNativePrintFactory::CreatePrintDialog( wxWindow *parent,
156 wxPrintData *data )
157 {
158 #if defined(__WXMSW__) && !defined(__WXUNIVERSAL__)
159 return new wxWindowsPrintDialog( parent, data );
160 #elif defined(__WXMAC__)
161 return new wxMacPrintDialog( parent, data );
162 #else
163 return new wxGenericPrintDialog( parent, data );
164 #endif
165 }
166
167 wxPageSetupDialogBase *wxNativePrintFactory::CreatePageSetupDialog( wxWindow *parent,
168 wxPageSetupDialogData *data )
169 {
170 #if defined(__WXMSW__) && !defined(__WXUNIVERSAL__)
171 return new wxWindowsPageSetupDialog( parent, data );
172 #elif defined(__WXMAC__)
173 return new wxMacPageSetupDialog( parent, data );
174 #else
175 return new wxGenericPageSetupDialog( parent, data );
176 #endif
177 }
178
179 bool wxNativePrintFactory::HasPrintSetupDialog()
180 {
181 #if defined(__WXMSW__) && !defined(__WXUNIVERSAL__)
182 return false;
183 #elif defined(__WXMAC__)
184 return false;
185 #else
186 // Only here do we need to provide the print setup
187 // dialog ourselves, the other platforms either have
188 // none, don't make it accessible or let you configure
189 // the printer from the wxPrintDialog anyway.
190 return true;
191 #endif
192
193 }
194
195 wxDialog *wxNativePrintFactory::CreatePrintSetupDialog( wxWindow *parent,
196 wxPrintData *data )
197 {
198 #if defined(__WXMSW__) && !defined(__WXUNIVERSAL__)
199 wxUnusedVar(parent);
200 wxUnusedVar(data);
201 return NULL;
202 #elif defined(__WXMAC__)
203 wxUnusedVar(parent);
204 wxUnusedVar(data);
205 return NULL;
206 #else
207 // Only here do we need to provide the print setup
208 // dialog ourselves, the other platforms either have
209 // none, don't make it accessible or let you configure
210 // the printer from the wxPrintDialog anyway.
211 return new wxGenericPrintSetupDialog( parent, data );
212 #endif
213 }
214
215 wxDCImpl* wxNativePrintFactory::CreatePrinterDCImpl( wxPrinterDC *owner, const wxPrintData& data )
216 {
217 #if defined(__WXGTK__) || defined(__WXMOTIF__) || ( defined(__WXUNIVERSAL__) && !defined(__WXMAC__) )
218 return new wxPostScriptDCImpl( owner, data );
219 #else
220 return new wxPrinterDCImpl( owner, data );
221 #endif
222 }
223
224 bool wxNativePrintFactory::HasOwnPrintToFile()
225 {
226 // Only relevant for PostScript and here the
227 // setup dialog provides no "print to file"
228 // option. In the GNOME setup dialog, the
229 // setup dialog has its own print to file.
230 return false;
231 }
232
233 bool wxNativePrintFactory::HasPrinterLine()
234 {
235 // Only relevant for PostScript for now
236 return true;
237 }
238
239 wxString wxNativePrintFactory::CreatePrinterLine()
240 {
241 // Only relevant for PostScript for now
242
243 // We should query "lpstat -d" here
244 return _("Generic PostScript");
245 }
246
247 bool wxNativePrintFactory::HasStatusLine()
248 {
249 // Only relevant for PostScript for now
250 return true;
251 }
252
253 wxString wxNativePrintFactory::CreateStatusLine()
254 {
255 // Only relevant for PostScript for now
256
257 // We should query "lpstat -r" or "lpstat -p" here
258 return _("Ready");
259 }
260
261 wxPrintNativeDataBase *wxNativePrintFactory::CreatePrintNativeData()
262 {
263 #if defined(__WXMSW__) && !defined(__WXUNIVERSAL__)
264 return new wxWindowsPrintNativeData;
265 #elif defined(__WXMAC__)
266 return wxOSXCreatePrintData();
267 #else
268 return new wxPostScriptPrintNativeData;
269 #endif
270 }
271
272 //----------------------------------------------------------------------------
273 // wxPrintNativeDataBase
274 //----------------------------------------------------------------------------
275
276 IMPLEMENT_ABSTRACT_CLASS(wxPrintNativeDataBase, wxObject)
277
278 wxPrintNativeDataBase::wxPrintNativeDataBase()
279 {
280 m_ref = 1;
281 }
282
283 //----------------------------------------------------------------------------
284 // wxPrintFactoryModule
285 //----------------------------------------------------------------------------
286
287 class wxPrintFactoryModule: public wxModule
288 {
289 public:
290 wxPrintFactoryModule() {}
291 bool OnInit() { return true; }
292 void OnExit() { wxPrintFactory::SetPrintFactory( NULL ); }
293
294 private:
295 DECLARE_DYNAMIC_CLASS(wxPrintFactoryModule)
296 };
297
298 IMPLEMENT_DYNAMIC_CLASS(wxPrintFactoryModule, wxModule)
299
300 //----------------------------------------------------------------------------
301 // wxPrinterBase
302 //----------------------------------------------------------------------------
303
304 IMPLEMENT_CLASS(wxPrinterBase, wxObject)
305
306 wxPrinterBase::wxPrinterBase(wxPrintDialogData *data)
307 {
308 m_currentPrintout = NULL;
309 sm_abortWindow = NULL;
310 sm_abortIt = false;
311 if (data)
312 m_printDialogData = (*data);
313 sm_lastError = wxPRINTER_NO_ERROR;
314 }
315
316 wxWindow *wxPrinterBase::sm_abortWindow = NULL;
317 bool wxPrinterBase::sm_abortIt = false;
318 wxPrinterError wxPrinterBase::sm_lastError = wxPRINTER_NO_ERROR;
319
320 wxPrinterBase::~wxPrinterBase()
321 {
322 }
323
324 wxPrintAbortDialog *wxPrinterBase::CreateAbortWindow(wxWindow *parent, wxPrintout * printout)
325 {
326 return new wxPrintAbortDialog(parent, printout->GetTitle());
327 }
328
329 void wxPrinterBase::ReportError(wxWindow *parent, wxPrintout *WXUNUSED(printout), const wxString& message)
330 {
331 wxMessageBox(message, _("Printing Error"), wxOK, parent);
332 }
333
334 wxPrintDialogData& wxPrinterBase::GetPrintDialogData() const
335 {
336 return (wxPrintDialogData&) m_printDialogData;
337 }
338
339 //----------------------------------------------------------------------------
340 // wxPrinter
341 //----------------------------------------------------------------------------
342
343 IMPLEMENT_CLASS(wxPrinter, wxPrinterBase)
344
345 wxPrinter::wxPrinter(wxPrintDialogData *data)
346 {
347 m_pimpl = wxPrintFactory::GetFactory()->CreatePrinter( data );
348 }
349
350 wxPrinter::~wxPrinter()
351 {
352 delete m_pimpl;
353 }
354
355 wxPrintAbortDialog *wxPrinter::CreateAbortWindow(wxWindow *parent, wxPrintout *printout)
356 {
357 return m_pimpl->CreateAbortWindow( parent, printout );
358 }
359
360 void wxPrinter::ReportError(wxWindow *parent, wxPrintout *printout, const wxString& message)
361 {
362 m_pimpl->ReportError( parent, printout, message );
363 }
364
365 bool wxPrinter::Setup(wxWindow *parent)
366 {
367 return m_pimpl->Setup( parent );
368 }
369
370 bool wxPrinter::Print(wxWindow *parent, wxPrintout *printout, bool prompt)
371 {
372 if ( !prompt && m_printDialogData.GetToPage() == 0 )
373 {
374 // If the dialog is not shown, set the pages range to print everything
375 // by default (as otherwise we wouldn't print anything at all which is
376 // certainly not a reasonable default behaviour).
377 int minPage, maxPage, selFrom, selTo;
378 printout->GetPageInfo(&minPage, &maxPage, &selFrom, &selTo);
379
380 wxPrintDialogData& pdd = m_pimpl->GetPrintDialogData();
381 pdd.SetFromPage(minPage);
382 pdd.SetToPage(maxPage);
383 }
384
385 return m_pimpl->Print( parent, printout, prompt );
386 }
387
388 wxDC* wxPrinter::PrintDialog(wxWindow *parent)
389 {
390 return m_pimpl->PrintDialog( parent );
391 }
392
393 wxPrintDialogData& wxPrinter::GetPrintDialogData() const
394 {
395 return m_pimpl->GetPrintDialogData();
396 }
397
398 // ---------------------------------------------------------------------------
399 // wxPrintDialogBase: the dialog for printing.
400 // ---------------------------------------------------------------------------
401
402 IMPLEMENT_ABSTRACT_CLASS(wxPrintDialogBase, wxDialog)
403
404 wxPrintDialogBase::wxPrintDialogBase(wxWindow *parent,
405 wxWindowID id,
406 const wxString &title,
407 const wxPoint &pos,
408 const wxSize &size,
409 long style)
410 : wxDialog( parent, id, title.empty() ? wxString(_("Print")) : title,
411 pos, size, style )
412 {
413 }
414
415 // ---------------------------------------------------------------------------
416 // wxPrintDialog: the dialog for printing
417 // ---------------------------------------------------------------------------
418
419 IMPLEMENT_CLASS(wxPrintDialog, wxObject)
420
421 wxPrintDialog::wxPrintDialog(wxWindow *parent, wxPrintDialogData* data)
422 {
423 m_pimpl = wxPrintFactory::GetFactory()->CreatePrintDialog( parent, data );
424 }
425
426 wxPrintDialog::wxPrintDialog(wxWindow *parent, wxPrintData* data)
427 {
428 m_pimpl = wxPrintFactory::GetFactory()->CreatePrintDialog( parent, data );
429 }
430
431 wxPrintDialog::~wxPrintDialog()
432 {
433 delete m_pimpl;
434 }
435
436 int wxPrintDialog::ShowModal()
437 {
438 return m_pimpl->ShowModal();
439 }
440
441 wxPrintDialogData& wxPrintDialog::GetPrintDialogData()
442 {
443 return m_pimpl->GetPrintDialogData();
444 }
445
446 wxPrintData& wxPrintDialog::GetPrintData()
447 {
448 return m_pimpl->GetPrintData();
449 }
450
451 wxDC *wxPrintDialog::GetPrintDC()
452 {
453 return m_pimpl->GetPrintDC();
454 }
455
456 // ---------------------------------------------------------------------------
457 // wxPageSetupDialogBase: the page setup dialog
458 // ---------------------------------------------------------------------------
459
460 IMPLEMENT_ABSTRACT_CLASS(wxPageSetupDialogBase, wxDialog)
461
462 wxPageSetupDialogBase::wxPageSetupDialogBase(wxWindow *parent,
463 wxWindowID id,
464 const wxString &title,
465 const wxPoint &pos,
466 const wxSize &size,
467 long style)
468 : wxDialog( parent, id, title.empty() ? wxString(_("Page setup")) : title,
469 pos, size, style )
470 {
471 }
472
473 // ---------------------------------------------------------------------------
474 // wxPageSetupDialog: the page setup dialog
475 // ---------------------------------------------------------------------------
476
477 IMPLEMENT_CLASS(wxPageSetupDialog, wxObject)
478
479 wxPageSetupDialog::wxPageSetupDialog(wxWindow *parent, wxPageSetupDialogData *data )
480 {
481 m_pimpl = wxPrintFactory::GetFactory()->CreatePageSetupDialog( parent, data );
482 }
483
484 wxPageSetupDialog::~wxPageSetupDialog()
485 {
486 delete m_pimpl;
487 }
488
489 int wxPageSetupDialog::ShowModal()
490 {
491 return m_pimpl->ShowModal();
492 }
493
494 wxPageSetupDialogData& wxPageSetupDialog::GetPageSetupDialogData()
495 {
496 return m_pimpl->GetPageSetupDialogData();
497 }
498
499 // old name
500 wxPageSetupDialogData& wxPageSetupDialog::GetPageSetupData()
501 {
502 return m_pimpl->GetPageSetupDialogData();
503 }
504
505 //----------------------------------------------------------------------------
506 // wxPrintAbortDialog
507 //----------------------------------------------------------------------------
508
509 BEGIN_EVENT_TABLE(wxPrintAbortDialog, wxDialog)
510 EVT_BUTTON(wxID_CANCEL, wxPrintAbortDialog::OnCancel)
511 END_EVENT_TABLE()
512
513 wxPrintAbortDialog::wxPrintAbortDialog(wxWindow *parent,
514 const wxString& documentTitle,
515 const wxPoint& pos,
516 const wxSize& size,
517 long style,
518 const wxString& name)
519 : wxDialog(parent, wxID_ANY, _("Printing"), pos, size, style, name)
520 {
521 wxBoxSizer *button_sizer = new wxBoxSizer(wxVERTICAL);
522 button_sizer->Add(new wxStaticText(this, wxID_ANY, _("Please wait while printing\n") + documentTitle), 0, wxALL, 10 );
523 button_sizer->Add(new wxButton(this, wxID_CANCEL, wxT("Cancel") ), 0, wxALL | wxALIGN_CENTER, 10);
524
525 SetAutoLayout(true);
526 SetSizer(button_sizer);
527
528 button_sizer->Fit(this);
529 button_sizer->SetSizeHints(this);
530 }
531
532 void wxPrintAbortDialog::OnCancel(wxCommandEvent& WXUNUSED(event))
533 {
534 wxPrinterBase::sm_abortIt = true;
535 wxPrinterBase::sm_abortWindow->Show(false);
536 wxPrinterBase::sm_abortWindow->Close(true);
537 wxPrinterBase::sm_abortWindow->Destroy();
538 wxPrinterBase::sm_abortWindow = NULL;
539 }
540
541 //----------------------------------------------------------------------------
542 // wxPrintout
543 //----------------------------------------------------------------------------
544
545 IMPLEMENT_ABSTRACT_CLASS(wxPrintout, wxObject)
546
547 wxPrintout::wxPrintout(const wxString& title)
548 {
549 m_printoutTitle = title ;
550 m_printoutDC = NULL;
551 m_pageWidthMM = 0;
552 m_pageHeightMM = 0;
553 m_pageWidthPixels = 0;
554 m_pageHeightPixels = 0;
555 m_PPIScreenX = 0;
556 m_PPIScreenY = 0;
557 m_PPIPrinterX = 0;
558 m_PPIPrinterY = 0;
559 m_preview = NULL;
560 }
561
562 wxPrintout::~wxPrintout()
563 {
564 }
565
566 bool wxPrintout::OnBeginDocument(int WXUNUSED(startPage), int WXUNUSED(endPage))
567 {
568 return GetDC()->StartDoc(_("Printing ") + m_printoutTitle);
569 }
570
571 void wxPrintout::OnEndDocument()
572 {
573 GetDC()->EndDoc();
574 }
575
576 void wxPrintout::OnBeginPrinting()
577 {
578 }
579
580 void wxPrintout::OnEndPrinting()
581 {
582 }
583
584 bool wxPrintout::HasPage(int page)
585 {
586 return (page == 1);
587 }
588
589 void wxPrintout::GetPageInfo(int *minPage, int *maxPage, int *fromPage, int *toPage)
590 {
591 *minPage = 1;
592 *maxPage = 32000;
593 *fromPage = 1;
594 *toPage = 1;
595 }
596
597 void wxPrintout::FitThisSizeToPaper(const wxSize& imageSize)
598 {
599 // Set the DC scale and origin so that the given image size fits within the
600 // entire page and the origin is at the top left corner of the page. Note
601 // that with most printers, portions of the page will be non-printable. Use
602 // this if you're managing your own page margins.
603 if (!m_printoutDC) return;
604 wxRect paperRect = GetPaperRectPixels();
605 wxCoord pw, ph;
606 GetPageSizePixels(&pw, &ph);
607 wxCoord w, h;
608 m_printoutDC->GetSize(&w, &h);
609 float scaleX = ((float(paperRect.width) * w) / (float(pw) * imageSize.x));
610 float scaleY = ((float(paperRect.height) * h) / (float(ph) * imageSize.y));
611 float actualScale = wxMin(scaleX, scaleY);
612 m_printoutDC->SetUserScale(actualScale, actualScale);
613 m_printoutDC->SetDeviceOrigin(0, 0);
614 wxRect logicalPaperRect = GetLogicalPaperRect();
615 SetLogicalOrigin(logicalPaperRect.x, logicalPaperRect.y);
616 }
617
618 void wxPrintout::FitThisSizeToPage(const wxSize& imageSize)
619 {
620 // Set the DC scale and origin so that the given image size fits within the
621 // printable area of the page and the origin is at the top left corner of
622 // the printable area.
623 if (!m_printoutDC) return;
624 int w, h;
625 m_printoutDC->GetSize(&w, &h);
626 float scaleX = float(w) / imageSize.x;
627 float scaleY = float(h) / imageSize.y;
628 float actualScale = wxMin(scaleX, scaleY);
629 m_printoutDC->SetUserScale(actualScale, actualScale);
630 m_printoutDC->SetDeviceOrigin(0, 0);
631 }
632
633 void wxPrintout::FitThisSizeToPageMargins(const wxSize& imageSize, const wxPageSetupDialogData& pageSetupData)
634 {
635 // Set the DC scale and origin so that the given image size fits within the
636 // page margins defined in the given wxPageSetupDialogData object and the
637 // origin is at the top left corner of the page margins.
638 if (!m_printoutDC) return;
639 wxRect paperRect = GetPaperRectPixels();
640 wxCoord pw, ph;
641 GetPageSizePixels(&pw, &ph);
642 wxPoint topLeft = pageSetupData.GetMarginTopLeft();
643 wxPoint bottomRight = pageSetupData.GetMarginBottomRight();
644 wxCoord mw, mh;
645 GetPageSizeMM(&mw, &mh);
646 float mmToDeviceX = float(pw) / mw;
647 float mmToDeviceY = float(ph) / mh;
648 wxRect pageMarginsRect(paperRect.x + wxRound(mmToDeviceX * topLeft.x),
649 paperRect.y + wxRound(mmToDeviceY * topLeft.y),
650 paperRect.width - wxRound(mmToDeviceX * (topLeft.x + bottomRight.x)),
651 paperRect.height - wxRound(mmToDeviceY * (topLeft.y + bottomRight.y)));
652 wxCoord w, h;
653 m_printoutDC->GetSize(&w, &h);
654 float scaleX = (float(pageMarginsRect.width) * w) / (float(pw) * imageSize.x);
655 float scaleY = (float(pageMarginsRect.height) * h) / (float(ph) * imageSize.y);
656 float actualScale = wxMin(scaleX, scaleY);
657 m_printoutDC->SetUserScale(actualScale, actualScale);
658 m_printoutDC->SetDeviceOrigin(0, 0);
659 wxRect logicalPageMarginsRect = GetLogicalPageMarginsRect(pageSetupData);
660 SetLogicalOrigin(logicalPageMarginsRect.x, logicalPageMarginsRect.y);
661 }
662
663 void wxPrintout::MapScreenSizeToPaper()
664 {
665 // Set the DC scale so that an image on the screen is the same size on the
666 // paper and the origin is at the top left of the paper. Note that with most
667 // printers, portions of the page will be cut off. Use this if you're
668 // managing your own page margins.
669 if (!m_printoutDC) return;
670 MapScreenSizeToPage();
671 wxRect logicalPaperRect = GetLogicalPaperRect();
672 SetLogicalOrigin(logicalPaperRect.x, logicalPaperRect.y);
673 }
674
675 void wxPrintout::MapScreenSizeToPage()
676 {
677 // Set the DC scale and origin so that an image on the screen is the same
678 // size on the paper and the origin is at the top left of the printable area.
679 if (!m_printoutDC) return;
680 int ppiScreenX, ppiScreenY;
681 GetPPIScreen(&ppiScreenX, &ppiScreenY);
682 int ppiPrinterX, ppiPrinterY;
683 GetPPIPrinter(&ppiPrinterX, &ppiPrinterY);
684 int w, h;
685 m_printoutDC->GetSize(&w, &h);
686 int pageSizePixelsX, pageSizePixelsY;
687 GetPageSizePixels(&pageSizePixelsX, &pageSizePixelsY);
688 float userScaleX = (float(ppiPrinterX) * w) / (float(ppiScreenX) * pageSizePixelsX);
689 float userScaleY = (float(ppiPrinterY) * h) / (float(ppiScreenY) * pageSizePixelsY);
690 m_printoutDC->SetUserScale(userScaleX, userScaleY);
691 m_printoutDC->SetDeviceOrigin(0, 0);
692 }
693
694 void wxPrintout::MapScreenSizeToPageMargins(const wxPageSetupDialogData& pageSetupData)
695 {
696 // Set the DC scale so that an image on the screen is the same size on the
697 // paper and the origin is at the top left of the page margins defined by
698 // the given wxPageSetupDialogData object.
699 if (!m_printoutDC) return;
700 MapScreenSizeToPage();
701 wxRect logicalPageMarginsRect = GetLogicalPageMarginsRect(pageSetupData);
702 SetLogicalOrigin(logicalPageMarginsRect.x, logicalPageMarginsRect.y);
703 }
704
705 void wxPrintout::MapScreenSizeToDevice()
706 {
707 // Set the DC scale so that a screen pixel is the same size as a device
708 // pixel and the origin is at the top left of the printable area.
709 if (!m_printoutDC) return;
710 int w, h;
711 m_printoutDC->GetSize(&w, &h);
712 int pageSizePixelsX, pageSizePixelsY;
713 GetPageSizePixels(&pageSizePixelsX, &pageSizePixelsY);
714 float userScaleX = float(w) / pageSizePixelsX;
715 float userScaleY = float(h) / pageSizePixelsY;
716 m_printoutDC->SetUserScale(userScaleX, userScaleY);
717 m_printoutDC->SetDeviceOrigin(0, 0);
718 }
719
720 wxRect wxPrintout::GetLogicalPaperRect() const
721 {
722 // Return the rectangle in logical units that corresponds to the paper
723 // rectangle.
724 wxRect paperRect = GetPaperRectPixels();
725 wxCoord pw, ph;
726 GetPageSizePixels(&pw, &ph);
727 wxCoord w, h;
728 m_printoutDC->GetSize(&w, &h);
729 if (w == pw && h == ph) {
730 // this DC matches the printed page, so no scaling
731 return wxRect(m_printoutDC->DeviceToLogicalX(paperRect.x),
732 m_printoutDC->DeviceToLogicalY(paperRect.y),
733 m_printoutDC->DeviceToLogicalXRel(paperRect.width),
734 m_printoutDC->DeviceToLogicalYRel(paperRect.height));
735 }
736 // This DC doesn't match the printed page, so we have to scale.
737 float scaleX = float(w) / pw;
738 float scaleY = float(h) / ph;
739 return wxRect(m_printoutDC->DeviceToLogicalX(wxRound(paperRect.x * scaleX)),
740 m_printoutDC->DeviceToLogicalY(wxRound(paperRect.y * scaleY)),
741 m_printoutDC->DeviceToLogicalXRel(wxRound(paperRect.width * scaleX)),
742 m_printoutDC->DeviceToLogicalYRel(wxRound(paperRect.height * scaleY)));
743 }
744
745 wxRect wxPrintout::GetLogicalPageRect() const
746 {
747 // Return the rectangle in logical units that corresponds to the printable
748 // area.
749 int w, h;
750 m_printoutDC->GetSize(&w, &h);
751 return wxRect(m_printoutDC->DeviceToLogicalX(0),
752 m_printoutDC->DeviceToLogicalY(0),
753 m_printoutDC->DeviceToLogicalXRel(w),
754 m_printoutDC->DeviceToLogicalYRel(h));
755 }
756
757 wxRect wxPrintout::GetLogicalPageMarginsRect(const wxPageSetupDialogData& pageSetupData) const
758 {
759 // Return the rectangle in logical units that corresponds to the region
760 // within the page margins as specified by the given wxPageSetupDialogData
761 // object.
762
763 // We get the paper size in device units and the margins in mm,
764 // so we need to calculate the conversion with this trick
765 wxCoord pw, ph;
766 GetPageSizePixels(&pw, &ph);
767 wxCoord mw, mh;
768 GetPageSizeMM(&mw, &mh);
769 float mmToDeviceX = float(pw) / mw;
770 float mmToDeviceY = float(ph) / mh;
771
772 // paper size in device units
773 wxRect paperRect = GetPaperRectPixels();
774
775 // margins in mm
776 wxPoint topLeft = pageSetupData.GetMarginTopLeft();
777 wxPoint bottomRight = pageSetupData.GetMarginBottomRight();
778
779 // calculate margins in device units
780 wxRect pageMarginsRect(
781 paperRect.x + wxRound(mmToDeviceX * topLeft.x),
782 paperRect.y + wxRound(mmToDeviceY * topLeft.y),
783 paperRect.width - wxRound(mmToDeviceX * (topLeft.x + bottomRight.x)),
784 paperRect.height - wxRound(mmToDeviceY * (topLeft.y + bottomRight.y)));
785
786 wxCoord w, h;
787 m_printoutDC->GetSize(&w, &h);
788 if (w == pw && h == ph)
789 {
790 // this DC matches the printed page, so no scaling
791 return wxRect(
792 m_printoutDC->DeviceToLogicalX(pageMarginsRect.x),
793 m_printoutDC->DeviceToLogicalY(pageMarginsRect.y),
794 m_printoutDC->DeviceToLogicalXRel(pageMarginsRect.width),
795 m_printoutDC->DeviceToLogicalYRel(pageMarginsRect.height));
796 }
797
798 // This DC doesn't match the printed page, so we have to scale.
799 float scaleX = float(w) / pw;
800 float scaleY = float(h) / ph;
801 return wxRect(m_printoutDC->DeviceToLogicalX(wxRound(pageMarginsRect.x * scaleX)),
802 m_printoutDC->DeviceToLogicalY(wxRound(pageMarginsRect.y * scaleY)),
803 m_printoutDC->DeviceToLogicalXRel(wxRound(pageMarginsRect.width * scaleX)),
804 m_printoutDC->DeviceToLogicalYRel(wxRound(pageMarginsRect.height * scaleY)));
805 }
806
807 void wxPrintout::SetLogicalOrigin(wxCoord x, wxCoord y)
808 {
809 // Set the device origin by specifying a point in logical coordinates.
810 m_printoutDC->SetDeviceOrigin(
811 m_printoutDC->LogicalToDeviceX(x),
812 m_printoutDC->LogicalToDeviceY(y) );
813 }
814
815 void wxPrintout::OffsetLogicalOrigin(wxCoord xoff, wxCoord yoff)
816 {
817 // Offset the device origin by a specified distance in device coordinates.
818 wxPoint dev_org = m_printoutDC->GetDeviceOrigin();
819 m_printoutDC->SetDeviceOrigin(
820 dev_org.x + m_printoutDC->LogicalToDeviceXRel(xoff),
821 dev_org.y + m_printoutDC->LogicalToDeviceYRel(yoff) );
822 }
823
824
825 //----------------------------------------------------------------------------
826 // wxPreviewCanvas
827 //----------------------------------------------------------------------------
828
829 IMPLEMENT_CLASS(wxPreviewCanvas, wxWindow)
830
831 BEGIN_EVENT_TABLE(wxPreviewCanvas, wxScrolledWindow)
832 EVT_PAINT(wxPreviewCanvas::OnPaint)
833 EVT_CHAR(wxPreviewCanvas::OnChar)
834 EVT_IDLE(wxPreviewCanvas::OnIdle)
835 EVT_SYS_COLOUR_CHANGED(wxPreviewCanvas::OnSysColourChanged)
836 #if wxUSE_MOUSEWHEEL
837 EVT_MOUSEWHEEL(wxPreviewCanvas::OnMouseWheel)
838 #endif
839 END_EVENT_TABLE()
840
841 // VZ: the current code doesn't refresh properly without
842 // wxFULL_REPAINT_ON_RESIZE, this must be fixed as otherwise we have
843 // really horrible flicker when resizing the preview frame, but without
844 // this style it simply doesn't work correctly at all...
845 wxPreviewCanvas::wxPreviewCanvas(wxPrintPreviewBase *preview, wxWindow *parent,
846 const wxPoint& pos, const wxSize& size, long style, const wxString& name):
847 wxScrolledWindow(parent, wxID_ANY, pos, size, style | wxFULL_REPAINT_ON_RESIZE, name)
848 {
849 m_printPreview = preview;
850 #ifdef __WXMAC__
851 // The app workspace colour is always white, but we should have
852 // a contrast with the page.
853 wxSystemColour colourIndex = wxSYS_COLOUR_3DDKSHADOW;
854 #elif defined(__WXGTK__)
855 wxSystemColour colourIndex = wxSYS_COLOUR_BTNFACE;
856 #else
857 wxSystemColour colourIndex = wxSYS_COLOUR_APPWORKSPACE;
858 #endif
859 SetBackgroundColour(wxSystemSettings::GetColour(colourIndex));
860
861 SetScrollbars(10, 10, 100, 100);
862 }
863
864 wxPreviewCanvas::~wxPreviewCanvas()
865 {
866 }
867
868 void wxPreviewCanvas::OnPaint(wxPaintEvent& WXUNUSED(event))
869 {
870 wxPaintDC dc(this);
871 PrepareDC( dc );
872
873 /*
874 #ifdef __WXGTK__
875 if (!GetUpdateRegion().IsEmpty())
876 dc.SetClippingRegion( GetUpdateRegion() );
877 #endif
878 */
879
880 if (m_printPreview)
881 {
882 m_printPreview->PaintPage(this, dc);
883 }
884 }
885
886 void wxPreviewCanvas::OnIdle(wxIdleEvent& event)
887 {
888 event.Skip();
889
890 // prevent UpdatePageRendering() from being called recursively:
891 static bool s_inIdle = false;
892 if ( s_inIdle )
893 return;
894 s_inIdle = true;
895
896 if ( m_printPreview )
897 {
898 if ( m_printPreview->UpdatePageRendering() )
899 Refresh();
900 }
901
902 s_inIdle = false;
903 }
904
905 // Responds to colour changes, and passes event on to children.
906 void wxPreviewCanvas::OnSysColourChanged(wxSysColourChangedEvent& event)
907 {
908 #ifdef __WXMAC__
909 // The app workspace colour is always white, but we should have
910 // a contrast with the page.
911 wxSystemColour colourIndex = wxSYS_COLOUR_3DDKSHADOW;
912 #elif defined(__WXGTK__)
913 wxSystemColour colourIndex = wxSYS_COLOUR_BTNFACE;
914 #else
915 wxSystemColour colourIndex = wxSYS_COLOUR_APPWORKSPACE;
916 #endif
917 SetBackgroundColour(wxSystemSettings::GetColour(colourIndex));
918 Refresh();
919
920 // Propagate the event to the non-top-level children
921 wxWindow::OnSysColourChanged(event);
922 }
923
924 void wxPreviewCanvas::OnChar(wxKeyEvent &event)
925 {
926 wxPreviewControlBar* controlBar = ((wxPreviewFrame*) GetParent())->GetControlBar();
927 switch (event.GetKeyCode())
928 {
929 case WXK_RETURN:
930 controlBar->OnPrint();
931 return;
932 case (int)'+':
933 case WXK_NUMPAD_ADD:
934 case WXK_ADD:
935 controlBar->DoZoomIn();
936 return;
937 case (int)'-':
938 case WXK_NUMPAD_SUBTRACT:
939 case WXK_SUBTRACT:
940 controlBar->DoZoomOut();
941 return;
942 }
943
944 if (!event.ControlDown())
945 {
946 event.Skip();
947 return;
948 }
949
950 switch(event.GetKeyCode())
951 {
952 case WXK_PAGEDOWN:
953 controlBar->OnNext(); break;
954 case WXK_PAGEUP:
955 controlBar->OnPrevious(); break;
956 case WXK_HOME:
957 controlBar->OnFirst(); break;
958 case WXK_END:
959 controlBar->OnLast(); break;
960 default:
961 event.Skip();
962 }
963 }
964
965 #if wxUSE_MOUSEWHEEL
966
967 void wxPreviewCanvas::OnMouseWheel(wxMouseEvent& event)
968 {
969 wxPreviewControlBar *
970 controlBar = wxStaticCast(GetParent(), wxPreviewFrame)->GetControlBar();
971
972 if ( controlBar )
973 {
974 if ( event.ControlDown() && event.GetWheelRotation() != 0 )
975 {
976 int currentZoom = controlBar->GetZoomControl();
977
978 int delta;
979 if ( currentZoom < 100 )
980 delta = 5;
981 else if ( currentZoom <= 120 )
982 delta = 10;
983 else
984 delta = 50;
985
986 if ( event.GetWheelRotation() > 0 )
987 delta = -delta;
988
989 int newZoom = currentZoom + delta;
990 if ( newZoom < 10 )
991 newZoom = 10;
992 if ( newZoom > 200 )
993 newZoom = 200;
994 if ( newZoom != currentZoom )
995 {
996 controlBar->SetZoomControl(newZoom);
997 m_printPreview->SetZoom(newZoom);
998 Refresh();
999 }
1000 return;
1001 }
1002 }
1003
1004 event.Skip();
1005 }
1006
1007 #endif // wxUSE_MOUSEWHEEL
1008
1009 namespace
1010 {
1011
1012 // This is by the controls in the print preview as the maximal (and hence
1013 // longest) page number we may have to display.
1014 enum { MAX_PAGE_NUMBER = 99999 };
1015
1016 } // anonymous namespace
1017
1018 // ----------------------------------------------------------------------------
1019 // wxPrintPageMaxCtrl
1020 // ----------------------------------------------------------------------------
1021
1022 // A simple static control showing the maximal number of pages.
1023 class wxPrintPageMaxCtrl : public wxStaticText
1024 {
1025 public:
1026 wxPrintPageMaxCtrl(wxWindow *parent)
1027 : wxStaticText(
1028 parent,
1029 wxID_ANY,
1030 wxString(),
1031 wxDefaultPosition,
1032 wxSize
1033 (
1034 parent->GetTextExtent(MaxAsString(MAX_PAGE_NUMBER)).x,
1035 wxDefaultCoord
1036 ),
1037 wxST_NO_AUTORESIZE | wxALIGN_CENTRE
1038 )
1039 {
1040 }
1041
1042 // Set the maximal page to display once we really know what it is.
1043 void SetMaxPage(int maxPage)
1044 {
1045 SetLabel(MaxAsString(maxPage));
1046 }
1047
1048 private:
1049 static wxString MaxAsString(int maxPage)
1050 {
1051 return wxString::Format("/ %d", maxPage);
1052 }
1053
1054 wxDECLARE_NO_COPY_CLASS(wxPrintPageMaxCtrl);
1055 };
1056
1057 // ----------------------------------------------------------------------------
1058 // wxPrintPageTextCtrl
1059 // ----------------------------------------------------------------------------
1060
1061 // This text control contains the page number in the specified interval.
1062 //
1063 // Invalid pages are not accepted and the control contents is validated when it
1064 // loses focus. Conversely, if the user changes the page to another valid one
1065 // or presses Enter, OnGotoPage() method of the preview object will be called.
1066 class wxPrintPageTextCtrl : public wxTextCtrl
1067 {
1068 public:
1069 wxPrintPageTextCtrl(wxPreviewControlBar *preview)
1070 : wxTextCtrl(preview,
1071 wxID_PREVIEW_GOTO,
1072 wxString(),
1073 wxDefaultPosition,
1074 // We use hardcoded maximal page number for the width
1075 // instead of fitting it to the values we can show because
1076 // the control looks uncomfortably narrow if the real page
1077 // number is just one or two digits.
1078 wxSize
1079 (
1080 preview->GetTextExtent(PageAsString(MAX_PAGE_NUMBER)).x,
1081 wxDefaultCoord
1082 ),
1083 wxTE_PROCESS_ENTER
1084 #if wxUSE_VALIDATORS
1085 , wxTextValidator(wxFILTER_DIGITS)
1086 #endif // wxUSE_VALIDATORS
1087 ),
1088 m_preview(preview)
1089 {
1090 m_minPage =
1091 m_maxPage =
1092 m_page = 1;
1093
1094 Connect(wxEVT_KILL_FOCUS,
1095 wxFocusEventHandler(wxPrintPageTextCtrl::OnKillFocus));
1096 Connect(wxEVT_COMMAND_TEXT_ENTER,
1097 wxCommandEventHandler(wxPrintPageTextCtrl::OnTextEnter));
1098 }
1099
1100 // Update the pages range, must be called after OnPreparePrinting() as
1101 // these values are not known before.
1102 void SetPageInfo(int minPage, int maxPage)
1103 {
1104 m_minPage = minPage;
1105 m_maxPage = maxPage;
1106
1107 // Show the first page by default.
1108 SetPageNumber(minPage);
1109 }
1110
1111 // Helpers to conveniently set or get the current page number. Return value
1112 // is 0 if the current controls contents is invalid.
1113 void SetPageNumber(int page)
1114 {
1115 wxASSERT( IsValidPage(page) );
1116
1117 SetValue(PageAsString(page));
1118 }
1119
1120 int GetPageNumber() const
1121 {
1122 long value;
1123 if ( !GetValue().ToLong(&value) || !IsValidPage(value) )
1124 return 0;
1125
1126 // Cast is safe because the value is less than (int) m_maxPage.
1127 return static_cast<int>(value);
1128 }
1129
1130 private:
1131 static wxString PageAsString(int page)
1132 {
1133 return wxString::Format("%d", page);
1134 }
1135
1136 bool IsValidPage(int page) const
1137 {
1138 return page >= m_minPage && page <= m_maxPage;
1139 }
1140
1141 bool DoChangePage()
1142 {
1143 const int page = GetPageNumber();
1144
1145 if ( !page )
1146 return false;
1147
1148 if ( page != m_page )
1149 {
1150 // We have a valid page, remember it.
1151 m_page = page;
1152
1153 // And notify the owner about the change.
1154 m_preview->OnGotoPage();
1155 }
1156 //else: Nothing really changed.
1157
1158 return true;
1159 }
1160
1161 void OnKillFocus(wxFocusEvent& event)
1162 {
1163 if ( !DoChangePage() )
1164 {
1165 // The current contents is invalid so reset it back to the last
1166 // known good page index.
1167 SetPageNumber(m_page);
1168 }
1169
1170 event.Skip();
1171 }
1172
1173 void OnTextEnter(wxCommandEvent& WXUNUSED(event))
1174 {
1175 DoChangePage();
1176 }
1177
1178
1179 wxPreviewControlBar * const m_preview;
1180
1181 int m_minPage,
1182 m_maxPage;
1183
1184 // This is the last valid page value that we had, we revert to it if an
1185 // invalid page is entered.
1186 int m_page;
1187
1188 wxDECLARE_NO_COPY_CLASS(wxPrintPageTextCtrl);
1189 };
1190
1191 //----------------------------------------------------------------------------
1192 // wxPreviewControlBar
1193 //----------------------------------------------------------------------------
1194
1195 IMPLEMENT_CLASS(wxPreviewControlBar, wxWindow)
1196
1197 BEGIN_EVENT_TABLE(wxPreviewControlBar, wxPanel)
1198 EVT_BUTTON(wxID_PREVIEW_CLOSE, wxPreviewControlBar::OnWindowClose)
1199 EVT_BUTTON(wxID_PREVIEW_PRINT, wxPreviewControlBar::OnPrintButton)
1200 EVT_BUTTON(wxID_PREVIEW_PREVIOUS, wxPreviewControlBar::OnPreviousButton)
1201 EVT_BUTTON(wxID_PREVIEW_NEXT, wxPreviewControlBar::OnNextButton)
1202 EVT_BUTTON(wxID_PREVIEW_FIRST, wxPreviewControlBar::OnFirstButton)
1203 EVT_BUTTON(wxID_PREVIEW_LAST, wxPreviewControlBar::OnLastButton)
1204 EVT_BUTTON(wxID_PREVIEW_ZOOM_IN, wxPreviewControlBar::OnZoomInButton)
1205 EVT_BUTTON(wxID_PREVIEW_ZOOM_OUT, wxPreviewControlBar::OnZoomOutButton)
1206
1207 EVT_UPDATE_UI(wxID_PREVIEW_PREVIOUS, wxPreviewControlBar::OnUpdatePreviousButton)
1208 EVT_UPDATE_UI(wxID_PREVIEW_NEXT, wxPreviewControlBar::OnUpdateNextButton)
1209 EVT_UPDATE_UI(wxID_PREVIEW_FIRST, wxPreviewControlBar::OnUpdateFirstButton)
1210 EVT_UPDATE_UI(wxID_PREVIEW_LAST, wxPreviewControlBar::OnUpdateLastButton)
1211 EVT_UPDATE_UI(wxID_PREVIEW_ZOOM_IN, wxPreviewControlBar::OnUpdateZoomInButton)
1212 EVT_UPDATE_UI(wxID_PREVIEW_ZOOM_OUT, wxPreviewControlBar::OnUpdateZoomOutButton)
1213
1214 EVT_CHOICE(wxID_PREVIEW_ZOOM, wxPreviewControlBar::OnZoomChoice)
1215 EVT_PAINT(wxPreviewControlBar::OnPaint)
1216
1217 END_EVENT_TABLE()
1218
1219 wxPreviewControlBar::wxPreviewControlBar(wxPrintPreviewBase *preview, long buttons,
1220 wxWindow *parent, const wxPoint& pos, const wxSize& size,
1221 long style, const wxString& name):
1222 wxPanel(parent, wxID_ANY, pos, size, style, name)
1223 {
1224 m_printPreview = preview;
1225 m_closeButton = NULL;
1226 m_zoomControl = NULL;
1227 m_currentPageText = NULL;
1228 m_maxPageText = NULL;
1229 m_buttonFlags = buttons;
1230 }
1231
1232 wxPreviewControlBar::~wxPreviewControlBar()
1233 {
1234 }
1235
1236 void wxPreviewControlBar::OnPaint(wxPaintEvent& WXUNUSED(event))
1237 {
1238 wxPaintDC dc(this);
1239
1240 int w, h;
1241 GetSize(&w, &h);
1242 dc.SetPen(*wxBLACK_PEN);
1243 dc.SetBrush(*wxTRANSPARENT_BRUSH);
1244 dc.DrawLine( 0, h-1, w, h-1 );
1245 }
1246
1247 void wxPreviewControlBar::OnWindowClose(wxCommandEvent& WXUNUSED(event))
1248 {
1249 wxPreviewFrame *frame = (wxPreviewFrame *)GetParent();
1250 frame->Close(true);
1251 }
1252
1253 void wxPreviewControlBar::OnPrint(void)
1254 {
1255 wxPrintPreviewBase *preview = GetPrintPreview();
1256 preview->Print(true);
1257 }
1258
1259 void wxPreviewControlBar::OnNext()
1260 {
1261 if ( IsNextEnabled() )
1262 DoGotoPage(GetPrintPreview()->GetCurrentPage() + 1);
1263 }
1264
1265 void wxPreviewControlBar::OnPrevious()
1266 {
1267 if ( IsPreviousEnabled() )
1268 DoGotoPage(GetPrintPreview()->GetCurrentPage() - 1);
1269 }
1270
1271 void wxPreviewControlBar::OnFirst()
1272 {
1273 if ( IsFirstEnabled() )
1274 DoGotoPage(GetPrintPreview()->GetMinPage());
1275 }
1276
1277 void wxPreviewControlBar::OnLast()
1278 {
1279 if ( IsLastEnabled() )
1280 DoGotoPage(GetPrintPreview()->GetMaxPage());
1281 }
1282
1283 bool wxPreviewControlBar::IsNextEnabled() const
1284 {
1285 wxPrintPreviewBase *preview = GetPrintPreview();
1286 if ( !preview )
1287 return false;
1288
1289 const int currentPage = preview->GetCurrentPage();
1290 return currentPage < preview->GetMaxPage() &&
1291 preview->GetPrintout()->HasPage(currentPage + 1);
1292 }
1293
1294 bool wxPreviewControlBar::IsPreviousEnabled() const
1295 {
1296 wxPrintPreviewBase *preview = GetPrintPreview();
1297 if ( !preview )
1298 return false;
1299
1300 const int currentPage = preview->GetCurrentPage();
1301 return currentPage > preview->GetMinPage() &&
1302 preview->GetPrintout()->HasPage(currentPage - 1);
1303 }
1304
1305 bool wxPreviewControlBar::IsFirstEnabled() const
1306 {
1307 wxPrintPreviewBase *preview = GetPrintPreview();
1308 if (!preview)
1309 return false;
1310
1311 return preview->GetPrintout()->HasPage(preview->GetMinPage());
1312 }
1313
1314 bool wxPreviewControlBar::IsLastEnabled() const
1315 {
1316 wxPrintPreviewBase *preview = GetPrintPreview();
1317 if (!preview)
1318 return false;
1319
1320 return preview->GetPrintout()->HasPage(preview->GetMaxPage());
1321 }
1322
1323 void wxPreviewControlBar::DoGotoPage(int page)
1324 {
1325 wxPrintPreviewBase *preview = GetPrintPreview();
1326 wxCHECK_RET( preview, "Shouldn't be called if there is no preview." );
1327
1328 preview->SetCurrentPage(page);
1329
1330 if ( m_currentPageText )
1331 m_currentPageText->SetPageNumber(page);
1332 }
1333
1334 void wxPreviewControlBar::OnGotoPage()
1335 {
1336 wxPrintPreviewBase *preview = GetPrintPreview();
1337 if (preview)
1338 {
1339 if (preview->GetMinPage() > 0)
1340 {
1341 long currentPage = m_currentPageText->GetPageNumber();
1342 if ( currentPage )
1343 {
1344 if (preview->GetPrintout()->HasPage(currentPage))
1345 {
1346 preview->SetCurrentPage(currentPage);
1347 }
1348 }
1349 }
1350 }
1351 }
1352
1353 void wxPreviewControlBar::DoZoom()
1354 {
1355 int zoom = GetZoomControl();
1356 if (GetPrintPreview())
1357 GetPrintPreview()->SetZoom(zoom);
1358 }
1359
1360 bool wxPreviewControlBar::IsZoomInEnabled() const
1361 {
1362 if ( !m_zoomControl )
1363 return false;
1364
1365 const unsigned sel = m_zoomControl->GetSelection();
1366 return sel < m_zoomControl->GetCount() - 1;
1367 }
1368
1369 bool wxPreviewControlBar::IsZoomOutEnabled() const
1370 {
1371 return m_zoomControl && m_zoomControl->GetSelection() > 0;
1372 }
1373
1374 void wxPreviewControlBar::DoZoomIn()
1375 {
1376 if (IsZoomInEnabled())
1377 {
1378 m_zoomControl->SetSelection(m_zoomControl->GetSelection() + 1);
1379 DoZoom();
1380 }
1381 }
1382
1383 void wxPreviewControlBar::DoZoomOut()
1384 {
1385 if (IsZoomOutEnabled())
1386 {
1387 m_zoomControl->SetSelection(m_zoomControl->GetSelection() - 1);
1388 DoZoom();
1389 }
1390 }
1391
1392 namespace
1393 {
1394
1395 // Helper class used by wxPreviewControlBar::CreateButtons() to add buttons
1396 // sequentially to it in the simplest way possible.
1397 class SizerWithButtons
1398 {
1399 public:
1400 // Constructor creates the sizer that will hold the buttons and stores the
1401 // parent that will be used for their creation.
1402 SizerWithButtons(wxWindow *parent)
1403 : m_sizer(new wxBoxSizer(wxHORIZONTAL)),
1404 m_parent(parent)
1405 {
1406 m_hasContents =
1407 m_needsSeparator = false;
1408 }
1409
1410 // Destructor associates the sizer with the parent window.
1411 ~SizerWithButtons()
1412 {
1413 m_parent->SetSizer(m_sizer);
1414 m_sizer->Fit(m_parent);
1415 }
1416
1417
1418 // Add an arbitrary window to the sizer.
1419 void Add(wxWindow *win)
1420 {
1421 if ( m_needsSeparator )
1422 {
1423 m_needsSeparator = false;
1424
1425 m_sizer->AddSpacer(2*wxSizerFlags::GetDefaultBorder());
1426 }
1427
1428 m_hasContents = true;
1429
1430 m_sizer->Add(win,
1431 wxSizerFlags().Border(wxLEFT | wxTOP | wxBOTTOM).Center());
1432 }
1433
1434 // Add a button with the specified id, bitmap and tooltip.
1435 void AddButton(wxWindowID btnId,
1436 const wxArtID& artId,
1437 const wxString& tooltip)
1438 {
1439 // We don't use (smaller) images inside a button with a text label but
1440 // rather toolbar-like bitmap buttons hence use wxART_TOOLBAR and not
1441 // wxART_BUTTON here.
1442 wxBitmap bmp = wxArtProvider::GetBitmap(artId, wxART_TOOLBAR);
1443 wxBitmapButton * const btn = new wxBitmapButton(m_parent, btnId, bmp);
1444 btn->SetToolTip(tooltip);
1445
1446 Add(btn);
1447 }
1448
1449 // Add a control at the right end of the window. This should be called last
1450 // as everything else added after it will be added on the right side too.
1451 void AddAtEnd(wxWindow *win)
1452 {
1453 m_sizer->AddStretchSpacer();
1454 m_sizer->Add(win,
1455 wxSizerFlags().Border(wxTOP | wxBOTTOM | wxRIGHT).Center());
1456 }
1457
1458 // Indicates the end of a group of buttons, a separator will be added after
1459 // it.
1460 void EndOfGroup()
1461 {
1462 if ( m_hasContents )
1463 {
1464 m_needsSeparator = true;
1465 m_hasContents = false;
1466 }
1467 }
1468
1469 private:
1470 wxSizer * const m_sizer;
1471 wxWindow * const m_parent;
1472
1473 // If true, we have some controls since the last group beginning. This is
1474 // used to avoid inserting two consecutive separators if EndOfGroup() is
1475 // called twice.
1476 bool m_hasContents;
1477
1478 // If true, a separator should be inserted before adding the next button.
1479 bool m_needsSeparator;
1480
1481 wxDECLARE_NO_COPY_CLASS(SizerWithButtons);
1482 };
1483
1484 } // anonymous namespace
1485
1486 void wxPreviewControlBar::CreateButtons()
1487 {
1488 SizerWithButtons sizer(this);
1489
1490 // Print button group (a single button).
1491 if (m_buttonFlags & wxPREVIEW_PRINT)
1492 {
1493 sizer.AddButton(wxID_PREVIEW_PRINT, wxART_PRINT, _("Print"));
1494 sizer.EndOfGroup();
1495 }
1496
1497 // Page selection buttons group.
1498 if (m_buttonFlags & wxPREVIEW_FIRST)
1499 {
1500 sizer.AddButton(wxID_PREVIEW_FIRST, wxART_GOTO_FIRST, _("First page"));
1501 }
1502
1503 if (m_buttonFlags & wxPREVIEW_PREVIOUS)
1504 {
1505 sizer.AddButton(wxID_PREVIEW_PREVIOUS, wxART_GO_BACK, _("Previous page"));
1506 }
1507
1508 if (m_buttonFlags & wxPREVIEW_GOTO)
1509 {
1510 m_currentPageText = new wxPrintPageTextCtrl(this);
1511 sizer.Add(m_currentPageText);
1512
1513 m_maxPageText = new wxPrintPageMaxCtrl(this);
1514 sizer.Add(m_maxPageText);
1515 }
1516
1517 if (m_buttonFlags & wxPREVIEW_NEXT)
1518 {
1519 sizer.AddButton(wxID_PREVIEW_NEXT, wxART_GO_FORWARD, _("Next page"));
1520 }
1521
1522 if (m_buttonFlags & wxPREVIEW_LAST)
1523 {
1524 sizer.AddButton(wxID_PREVIEW_LAST, wxART_GOTO_LAST, _("Last page"));
1525 }
1526
1527 sizer.EndOfGroup();
1528
1529 // Zoom controls group.
1530 if (m_buttonFlags & wxPREVIEW_ZOOM)
1531 {
1532 sizer.AddButton(wxID_PREVIEW_ZOOM_OUT, wxART_MINUS, _("Zoom Out"));
1533
1534 wxString choices[] =
1535 {
1536 wxT("10%"), wxT("15%"), wxT("20%"), wxT("25%"), wxT("30%"), wxT("35%"), wxT("40%"), wxT("45%"), wxT("50%"), wxT("55%"),
1537 wxT("60%"), wxT("65%"), wxT("70%"), wxT("75%"), wxT("80%"), wxT("85%"), wxT("90%"), wxT("95%"), wxT("100%"), wxT("110%"),
1538 wxT("120%"), wxT("150%"), wxT("200%")
1539 };
1540 int n = WXSIZEOF(choices);
1541
1542 m_zoomControl = new wxChoice( this, wxID_PREVIEW_ZOOM, wxDefaultPosition, wxSize(70,wxDefaultCoord), n, choices, 0 );
1543 sizer.Add(m_zoomControl);
1544 SetZoomControl(m_printPreview->GetZoom());
1545
1546 sizer.AddButton(wxID_PREVIEW_ZOOM_IN, wxART_PLUS, _("Zoom In"));
1547
1548 sizer.EndOfGroup();
1549 }
1550
1551 // Close button group (single button again).
1552 m_closeButton = new wxButton(this, wxID_PREVIEW_CLOSE, _("&Close"));
1553 sizer.AddAtEnd(m_closeButton);
1554 }
1555
1556 void wxPreviewControlBar::SetPageInfo(int minPage, int maxPage)
1557 {
1558 if ( m_currentPageText )
1559 m_currentPageText->SetPageInfo(minPage, maxPage);
1560
1561 if ( m_maxPageText )
1562 m_maxPageText->SetMaxPage(maxPage);
1563 }
1564
1565 void wxPreviewControlBar::SetZoomControl(int zoom)
1566 {
1567 if (m_zoomControl)
1568 {
1569 int n, count = m_zoomControl->GetCount();
1570 long val;
1571 for (n=0; n<count; n++)
1572 {
1573 if (m_zoomControl->GetString(n).BeforeFirst(wxT('%')).ToLong(&val) &&
1574 (val >= long(zoom)))
1575 {
1576 m_zoomControl->SetSelection(n);
1577 return;
1578 }
1579 }
1580
1581 m_zoomControl->SetSelection(count-1);
1582 }
1583 }
1584
1585 int wxPreviewControlBar::GetZoomControl()
1586 {
1587 if (m_zoomControl && (m_zoomControl->GetStringSelection() != wxEmptyString))
1588 {
1589 long val;
1590 if (m_zoomControl->GetStringSelection().BeforeFirst(wxT('%')).ToLong(&val))
1591 return int(val);
1592 }
1593
1594 return 0;
1595 }
1596
1597
1598 /*
1599 * Preview frame
1600 */
1601
1602 IMPLEMENT_CLASS(wxPreviewFrame, wxFrame)
1603
1604 BEGIN_EVENT_TABLE(wxPreviewFrame, wxFrame)
1605 EVT_CHAR_HOOK(wxPreviewFrame::OnChar)
1606 EVT_CLOSE(wxPreviewFrame::OnCloseWindow)
1607 END_EVENT_TABLE()
1608
1609 void wxPreviewFrame::OnChar(wxKeyEvent &event)
1610 {
1611 if ( event.GetKeyCode() == WXK_ESCAPE )
1612 {
1613 Close(true);
1614 }
1615 else
1616 {
1617 event.Skip();
1618 }
1619 }
1620
1621 wxPreviewFrame::wxPreviewFrame(wxPrintPreviewBase *preview, wxWindow *parent, const wxString& title,
1622 const wxPoint& pos, const wxSize& size, long style, const wxString& name):
1623 wxFrame(parent, wxID_ANY, title, pos, size, style, name)
1624 {
1625 m_printPreview = preview;
1626 m_controlBar = NULL;
1627 m_previewCanvas = NULL;
1628 m_windowDisabler = NULL;
1629 m_modalityKind = wxPreviewFrame_NonModal;
1630
1631 // Give the application icon
1632 #ifdef __WXMSW__
1633 wxFrame* topFrame = wxDynamicCast(wxTheApp->GetTopWindow(), wxFrame);
1634 if (topFrame)
1635 SetIcons(topFrame->GetIcons());
1636 #endif
1637 }
1638
1639 wxPreviewFrame::~wxPreviewFrame()
1640 {
1641 wxPrintout *printout = m_printPreview->GetPrintout();
1642 if (printout)
1643 {
1644 delete printout;
1645 m_printPreview->SetPrintout(NULL);
1646 m_printPreview->SetCanvas(NULL);
1647 m_printPreview->SetFrame(NULL);
1648 }
1649
1650 m_previewCanvas->SetPreview(NULL);
1651 delete m_printPreview;
1652 }
1653
1654 void wxPreviewFrame::OnCloseWindow(wxCloseEvent& WXUNUSED(event))
1655 {
1656 // Reenable any windows we disabled by undoing whatever we did in our
1657 // Initialize().
1658 switch ( m_modalityKind )
1659 {
1660 case wxPreviewFrame_AppModal:
1661 delete m_windowDisabler;
1662 m_windowDisabler = NULL;
1663 break;
1664
1665 case wxPreviewFrame_WindowModal:
1666 if ( GetParent() )
1667 GetParent()->Enable();
1668 break;
1669
1670 case wxPreviewFrame_NonModal:
1671 break;
1672 }
1673
1674 Destroy();
1675 }
1676
1677 void wxPreviewFrame::InitializeWithModality(wxPreviewFrameModalityKind kind)
1678 {
1679 #if wxUSE_STATUSBAR
1680 CreateStatusBar();
1681 #endif
1682 CreateCanvas();
1683 CreateControlBar();
1684
1685 m_printPreview->SetCanvas(m_previewCanvas);
1686 m_printPreview->SetFrame(this);
1687
1688 wxBoxSizer *item0 = new wxBoxSizer( wxVERTICAL );
1689
1690 item0->Add( m_controlBar, 0, wxGROW|wxALIGN_CENTER_VERTICAL, 5 );
1691 item0->Add( m_previewCanvas, 1, wxGROW|wxALIGN_CENTER_VERTICAL, 5 );
1692
1693 SetAutoLayout( true );
1694 SetSizer( item0 );
1695
1696 m_modalityKind = kind;
1697 switch ( m_modalityKind )
1698 {
1699 case wxPreviewFrame_AppModal:
1700 // Disable everything.
1701 m_windowDisabler = new wxWindowDisabler( this );
1702 break;
1703
1704 case wxPreviewFrame_WindowModal:
1705 // Disable our parent if we have one.
1706 if ( GetParent() )
1707 GetParent()->Disable();
1708 break;
1709
1710 case wxPreviewFrame_NonModal:
1711 // Nothing to do, we don't need to disable any windows.
1712 break;
1713 }
1714
1715 if ( m_modalityKind != wxPreviewFrame_NonModal )
1716 {
1717 // Behave like modal dialogs, don't show in taskbar. This implies
1718 // removing the minimize box, because minimizing windows without
1719 // taskbar entry is confusing.
1720 SetWindowStyle((GetWindowStyle() & ~wxMINIMIZE_BOX) | wxFRAME_NO_TASKBAR);
1721 }
1722
1723 Layout();
1724
1725 m_printPreview->AdjustScrollbars(m_previewCanvas);
1726 m_previewCanvas->SetFocus();
1727 m_controlBar->SetFocus();
1728 }
1729
1730 void wxPreviewFrame::CreateCanvas()
1731 {
1732 m_previewCanvas = new wxPreviewCanvas(m_printPreview, this);
1733 }
1734
1735 void wxPreviewFrame::CreateControlBar()
1736 {
1737 long buttons = wxPREVIEW_DEFAULT;
1738 if (m_printPreview->GetPrintoutForPrinting())
1739 buttons |= wxPREVIEW_PRINT;
1740
1741 m_controlBar = new wxPreviewControlBar(m_printPreview, buttons, this);
1742 m_controlBar->CreateButtons();
1743 }
1744
1745 /*
1746 * Print preview
1747 */
1748
1749 IMPLEMENT_CLASS(wxPrintPreviewBase, wxObject)
1750
1751 wxPrintPreviewBase::wxPrintPreviewBase(wxPrintout *printout,
1752 wxPrintout *printoutForPrinting,
1753 wxPrintData *data)
1754 {
1755 if (data)
1756 m_printDialogData = (*data);
1757
1758 Init(printout, printoutForPrinting);
1759 }
1760
1761 wxPrintPreviewBase::wxPrintPreviewBase(wxPrintout *printout,
1762 wxPrintout *printoutForPrinting,
1763 wxPrintDialogData *data)
1764 {
1765 if (data)
1766 m_printDialogData = (*data);
1767
1768 Init(printout, printoutForPrinting);
1769 }
1770
1771 void wxPrintPreviewBase::Init(wxPrintout *printout,
1772 wxPrintout *printoutForPrinting)
1773 {
1774 m_isOk = true;
1775 m_previewPrintout = printout;
1776 if (m_previewPrintout)
1777 m_previewPrintout->SetPreview(static_cast<wxPrintPreview *>(this));
1778
1779 m_printPrintout = printoutForPrinting;
1780
1781 m_previewCanvas = NULL;
1782 m_previewFrame = NULL;
1783 m_previewBitmap = NULL;
1784 m_previewFailed = false;
1785 m_currentPage = 1;
1786 m_currentZoom = 70;
1787 m_topMargin =
1788 m_leftMargin = 2*wxSizerFlags::GetDefaultBorder();
1789 m_pageWidth = 0;
1790 m_pageHeight = 0;
1791 m_printingPrepared = false;
1792 m_minPage = 1;
1793 m_maxPage = 1;
1794 }
1795
1796 wxPrintPreviewBase::~wxPrintPreviewBase()
1797 {
1798 if (m_previewPrintout)
1799 delete m_previewPrintout;
1800 if (m_previewBitmap)
1801 delete m_previewBitmap;
1802 if (m_printPrintout)
1803 delete m_printPrintout;
1804 }
1805
1806 bool wxPrintPreviewBase::SetCurrentPage(int pageNum)
1807 {
1808 if (m_currentPage == pageNum)
1809 return true;
1810
1811 m_currentPage = pageNum;
1812
1813 InvalidatePreviewBitmap();
1814
1815 if (m_previewCanvas)
1816 {
1817 AdjustScrollbars(m_previewCanvas);
1818
1819 m_previewCanvas->Refresh();
1820 m_previewCanvas->SetFocus();
1821 }
1822 return true;
1823 }
1824
1825 int wxPrintPreviewBase::GetCurrentPage() const
1826 { return m_currentPage; }
1827 void wxPrintPreviewBase::SetPrintout(wxPrintout *printout)
1828 { m_previewPrintout = printout; }
1829 wxPrintout *wxPrintPreviewBase::GetPrintout() const
1830 { return m_previewPrintout; }
1831 wxPrintout *wxPrintPreviewBase::GetPrintoutForPrinting() const
1832 { return m_printPrintout; }
1833 void wxPrintPreviewBase::SetFrame(wxFrame *frame)
1834 { m_previewFrame = frame; }
1835 void wxPrintPreviewBase::SetCanvas(wxPreviewCanvas *canvas)
1836 { m_previewCanvas = canvas; }
1837 wxFrame *wxPrintPreviewBase::GetFrame() const
1838 { return m_previewFrame; }
1839 wxPreviewCanvas *wxPrintPreviewBase::GetCanvas() const
1840 { return m_previewCanvas; }
1841
1842 void wxPrintPreviewBase::CalcRects(wxPreviewCanvas *canvas, wxRect& pageRect, wxRect& paperRect)
1843 {
1844 // Calculate the rectangles for the printable area of the page and the
1845 // entire paper as they appear on the canvas on-screen.
1846 int canvasWidth, canvasHeight;
1847 canvas->GetSize(&canvasWidth, &canvasHeight);
1848
1849 float zoomScale = float(m_currentZoom) / 100;
1850 float screenPrintableWidth = zoomScale * m_pageWidth * m_previewScaleX;
1851 float screenPrintableHeight = zoomScale * m_pageHeight * m_previewScaleY;
1852
1853 wxRect devicePaperRect = m_previewPrintout->GetPaperRectPixels();
1854 wxCoord devicePrintableWidth, devicePrintableHeight;
1855 m_previewPrintout->GetPageSizePixels(&devicePrintableWidth, &devicePrintableHeight);
1856 float scaleX = screenPrintableWidth / devicePrintableWidth;
1857 float scaleY = screenPrintableHeight / devicePrintableHeight;
1858 paperRect.width = wxCoord(scaleX * devicePaperRect.width);
1859 paperRect.height = wxCoord(scaleY * devicePaperRect.height);
1860
1861 paperRect.x = wxCoord((canvasWidth - paperRect.width)/ 2.0);
1862 if (paperRect.x < m_leftMargin)
1863 paperRect.x = m_leftMargin;
1864 paperRect.y = wxCoord((canvasHeight - paperRect.height)/ 2.0);
1865 if (paperRect.y < m_topMargin)
1866 paperRect.y = m_topMargin;
1867
1868 pageRect.x = paperRect.x - wxCoord(scaleX * devicePaperRect.x);
1869 pageRect.y = paperRect.y - wxCoord(scaleY * devicePaperRect.y);
1870 pageRect.width = wxCoord(screenPrintableWidth);
1871 pageRect.height = wxCoord(screenPrintableHeight);
1872 }
1873
1874
1875 void wxPrintPreviewBase::InvalidatePreviewBitmap()
1876 {
1877 wxDELETE(m_previewBitmap);
1878 // if there was a problem with rendering the preview, try again now
1879 // that it changed in some way (less memory may be needed, for example):
1880 m_previewFailed = false;
1881 }
1882
1883 bool wxPrintPreviewBase::UpdatePageRendering()
1884 {
1885 if ( m_previewBitmap )
1886 return false;
1887
1888 if ( m_previewFailed )
1889 return false;
1890
1891 if ( !RenderPage(m_currentPage) )
1892 {
1893 m_previewFailed = true; // don't waste time failing again
1894 return false;
1895 }
1896
1897 return true;
1898 }
1899
1900 bool wxPrintPreviewBase::PaintPage(wxPreviewCanvas *canvas, wxDC& dc)
1901 {
1902 DrawBlankPage(canvas, dc);
1903
1904 if (!m_previewBitmap)
1905 return false;
1906 if (!canvas)
1907 return false;
1908
1909 wxRect pageRect, paperRect;
1910 CalcRects(canvas, pageRect, paperRect);
1911 wxMemoryDC temp_dc;
1912 temp_dc.SelectObject(*m_previewBitmap);
1913
1914 dc.Blit(pageRect.x, pageRect.y,
1915 m_previewBitmap->GetWidth(), m_previewBitmap->GetHeight(), &temp_dc, 0, 0);
1916
1917 temp_dc.SelectObject(wxNullBitmap);
1918 return true;
1919 }
1920
1921 // Adjusts the scrollbars for the current scale
1922 void wxPrintPreviewBase::AdjustScrollbars(wxPreviewCanvas *canvas)
1923 {
1924 if (!canvas)
1925 return ;
1926
1927 wxRect pageRect, paperRect;
1928 CalcRects(canvas, pageRect, paperRect);
1929 int totalWidth = paperRect.width + 2 * m_leftMargin;
1930 int totalHeight = paperRect.height + 2 * m_topMargin;
1931 int scrollUnitsX = totalWidth / 10;
1932 int scrollUnitsY = totalHeight / 10;
1933 wxSize virtualSize = canvas->GetVirtualSize();
1934 if (virtualSize.GetWidth() != totalWidth || virtualSize.GetHeight() != totalHeight)
1935 canvas->SetScrollbars(10, 10, scrollUnitsX, scrollUnitsY, 0, 0, true);
1936 }
1937
1938 bool wxPrintPreviewBase::RenderPageIntoDC(wxDC& dc, int pageNum)
1939 {
1940 m_previewPrintout->SetDC(&dc);
1941 m_previewPrintout->SetPageSizePixels(m_pageWidth, m_pageHeight);
1942
1943 // Need to delay OnPreparePrinting() until here, so we have enough
1944 // information and a wxDC.
1945 if (!m_printingPrepared)
1946 {
1947 m_printingPrepared = true;
1948
1949 m_previewPrintout->OnPreparePrinting();
1950 int selFrom, selTo;
1951 m_previewPrintout->GetPageInfo(&m_minPage, &m_maxPage, &selFrom, &selTo);
1952
1953 // Update the wxPreviewControlBar page range display.
1954 if ( m_previewFrame )
1955 {
1956 wxPreviewControlBar * const
1957 controlBar = ((wxPreviewFrame*)m_previewFrame)->GetControlBar();
1958 if ( controlBar )
1959 controlBar->SetPageInfo(m_minPage, m_maxPage);
1960 }
1961 }
1962
1963 m_previewPrintout->OnBeginPrinting();
1964
1965 if (!m_previewPrintout->OnBeginDocument(m_printDialogData.GetFromPage(), m_printDialogData.GetToPage()))
1966 {
1967 wxMessageBox(_("Could not start document preview."), _("Print Preview Failure"), wxOK);
1968 return false;
1969 }
1970
1971 m_previewPrintout->OnPrintPage(pageNum);
1972 m_previewPrintout->OnEndDocument();
1973 m_previewPrintout->OnEndPrinting();
1974
1975 m_previewPrintout->SetDC(NULL);
1976
1977 return true;
1978 }
1979
1980 bool wxPrintPreviewBase::RenderPageIntoBitmap(wxBitmap& bmp, int pageNum)
1981 {
1982 wxMemoryDC memoryDC;
1983 memoryDC.SelectObject(bmp);
1984 memoryDC.Clear();
1985
1986 return RenderPageIntoDC(memoryDC, pageNum);
1987 }
1988
1989 bool wxPrintPreviewBase::RenderPage(int pageNum)
1990 {
1991 wxBusyCursor busy;
1992
1993 if (!m_previewCanvas)
1994 {
1995 wxFAIL_MSG(wxT("wxPrintPreviewBase::RenderPage: must use wxPrintPreviewBase::SetCanvas to let me know about the canvas!"));
1996 return false;
1997 }
1998
1999 wxRect pageRect, paperRect;
2000 CalcRects(m_previewCanvas, pageRect, paperRect);
2001
2002 if (!m_previewBitmap)
2003 {
2004 m_previewBitmap = new wxBitmap(pageRect.width, pageRect.height);
2005
2006 if (!m_previewBitmap || !m_previewBitmap->IsOk())
2007 {
2008 InvalidatePreviewBitmap();
2009 wxMessageBox(_("Sorry, not enough memory to create a preview."), _("Print Preview Failure"), wxOK);
2010 return false;
2011 }
2012 }
2013
2014 if ( !RenderPageIntoBitmap(*m_previewBitmap, pageNum) )
2015 {
2016 InvalidatePreviewBitmap();
2017 wxMessageBox(_("Sorry, not enough memory to create a preview."), _("Print Preview Failure"), wxOK);
2018 return false;
2019 }
2020
2021 #if wxUSE_STATUSBAR
2022 wxString status;
2023 if (m_maxPage != 0)
2024 status = wxString::Format(_("Page %d of %d"), pageNum, m_maxPage);
2025 else
2026 status = wxString::Format(_("Page %d"), pageNum);
2027
2028 if (m_previewFrame)
2029 m_previewFrame->SetStatusText(status);
2030 #endif
2031
2032 return true;
2033 }
2034
2035 bool wxPrintPreviewBase::DrawBlankPage(wxPreviewCanvas *canvas, wxDC& dc)
2036 {
2037 wxRect pageRect, paperRect;
2038
2039 CalcRects(canvas, pageRect, paperRect);
2040
2041 // Draw shadow, allowing for 1-pixel border AROUND the actual paper
2042 wxCoord shadowOffset = 4;
2043
2044 dc.SetPen(*wxBLACK_PEN);
2045 dc.SetBrush(*wxBLACK_BRUSH);
2046 dc.DrawRectangle(paperRect.x + shadowOffset, paperRect.y + paperRect.height + 1,
2047 paperRect.width, shadowOffset);
2048
2049 dc.DrawRectangle(paperRect.x + paperRect.width, paperRect.y + shadowOffset,
2050 shadowOffset, paperRect.height);
2051
2052 // Draw blank page allowing for 1-pixel border AROUND the actual paper
2053 dc.SetPen(*wxBLACK_PEN);
2054 dc.SetBrush(*wxWHITE_BRUSH);
2055 dc.DrawRectangle(paperRect.x - 2, paperRect.y - 1,
2056 paperRect.width + 3, paperRect.height + 2);
2057
2058 return true;
2059 }
2060
2061 void wxPrintPreviewBase::SetZoom(int percent)
2062 {
2063 if (m_currentZoom == percent)
2064 return;
2065
2066 m_currentZoom = percent;
2067
2068 InvalidatePreviewBitmap();
2069
2070 if (m_previewCanvas)
2071 {
2072 AdjustScrollbars(m_previewCanvas);
2073 ((wxScrolledWindow *) m_previewCanvas)->Scroll(0, 0);
2074 m_previewCanvas->ClearBackground();
2075 m_previewCanvas->Refresh();
2076 m_previewCanvas->SetFocus();
2077 }
2078 }
2079
2080 wxPrintDialogData& wxPrintPreviewBase::GetPrintDialogData()
2081 {
2082 return m_printDialogData;
2083 }
2084
2085 int wxPrintPreviewBase::GetZoom() const
2086 { return m_currentZoom; }
2087 int wxPrintPreviewBase::GetMaxPage() const
2088 { return m_maxPage; }
2089 int wxPrintPreviewBase::GetMinPage() const
2090 { return m_minPage; }
2091 bool wxPrintPreviewBase::IsOk() const
2092 { return m_isOk; }
2093 void wxPrintPreviewBase::SetOk(bool ok)
2094 { m_isOk = ok; }
2095
2096 //----------------------------------------------------------------------------
2097 // wxPrintPreview
2098 //----------------------------------------------------------------------------
2099
2100 IMPLEMENT_CLASS(wxPrintPreview, wxPrintPreviewBase)
2101
2102 wxPrintPreview::wxPrintPreview(wxPrintout *printout,
2103 wxPrintout *printoutForPrinting,
2104 wxPrintDialogData *data) :
2105 wxPrintPreviewBase( printout, printoutForPrinting, data )
2106 {
2107 m_pimpl = wxPrintFactory::GetFactory()->
2108 CreatePrintPreview( printout, printoutForPrinting, data );
2109 }
2110
2111 wxPrintPreview::wxPrintPreview(wxPrintout *printout,
2112 wxPrintout *printoutForPrinting,
2113 wxPrintData *data ) :
2114 wxPrintPreviewBase( printout, printoutForPrinting, data )
2115 {
2116 m_pimpl = wxPrintFactory::GetFactory()->
2117 CreatePrintPreview( printout, printoutForPrinting, data );
2118 }
2119
2120 wxPrintPreview::~wxPrintPreview()
2121 {
2122 delete m_pimpl;
2123
2124 // don't delete twice
2125 m_printPrintout = NULL;
2126 m_previewPrintout = NULL;
2127 m_previewBitmap = NULL;
2128 }
2129
2130 bool wxPrintPreview::SetCurrentPage(int pageNum)
2131 {
2132 return m_pimpl->SetCurrentPage( pageNum );
2133 }
2134
2135 int wxPrintPreview::GetCurrentPage() const
2136 {
2137 return m_pimpl->GetCurrentPage();
2138 }
2139
2140 void wxPrintPreview::SetPrintout(wxPrintout *printout)
2141 {
2142 m_pimpl->SetPrintout( printout );
2143 }
2144
2145 wxPrintout *wxPrintPreview::GetPrintout() const
2146 {
2147 return m_pimpl->GetPrintout();
2148 }
2149
2150 wxPrintout *wxPrintPreview::GetPrintoutForPrinting() const
2151 {
2152 return m_pimpl->GetPrintoutForPrinting();
2153 }
2154
2155 void wxPrintPreview::SetFrame(wxFrame *frame)
2156 {
2157 m_pimpl->SetFrame( frame );
2158 }
2159
2160 void wxPrintPreview::SetCanvas(wxPreviewCanvas *canvas)
2161 {
2162 m_pimpl->SetCanvas( canvas );
2163 }
2164
2165 wxFrame *wxPrintPreview::GetFrame() const
2166 {
2167 return m_pimpl->GetFrame();
2168 }
2169
2170 wxPreviewCanvas *wxPrintPreview::GetCanvas() const
2171 {
2172 return m_pimpl->GetCanvas();
2173 }
2174
2175 bool wxPrintPreview::PaintPage(wxPreviewCanvas *canvas, wxDC& dc)
2176 {
2177 return m_pimpl->PaintPage( canvas, dc );
2178 }
2179
2180 bool wxPrintPreview::UpdatePageRendering()
2181 {
2182 return m_pimpl->UpdatePageRendering();
2183 }
2184
2185 bool wxPrintPreview::DrawBlankPage(wxPreviewCanvas *canvas, wxDC& dc)
2186 {
2187 return m_pimpl->DrawBlankPage( canvas, dc );
2188 }
2189
2190 void wxPrintPreview::AdjustScrollbars(wxPreviewCanvas *canvas)
2191 {
2192 m_pimpl->AdjustScrollbars( canvas );
2193 }
2194
2195 bool wxPrintPreview::RenderPage(int pageNum)
2196 {
2197 return m_pimpl->RenderPage( pageNum );
2198 }
2199
2200 void wxPrintPreview::SetZoom(int percent)
2201 {
2202 m_pimpl->SetZoom( percent );
2203 }
2204
2205 int wxPrintPreview::GetZoom() const
2206 {
2207 return m_pimpl->GetZoom();
2208 }
2209
2210 wxPrintDialogData& wxPrintPreview::GetPrintDialogData()
2211 {
2212 return m_pimpl->GetPrintDialogData();
2213 }
2214
2215 int wxPrintPreview::GetMaxPage() const
2216 {
2217 return m_pimpl->GetMaxPage();
2218 }
2219
2220 int wxPrintPreview::GetMinPage() const
2221 {
2222 return m_pimpl->GetMinPage();
2223 }
2224
2225 bool wxPrintPreview::IsOk() const
2226 {
2227 return m_pimpl->IsOk();
2228 }
2229
2230 void wxPrintPreview::SetOk(bool ok)
2231 {
2232 m_pimpl->SetOk( ok );
2233 }
2234
2235 bool wxPrintPreview::Print(bool interactive)
2236 {
2237 return m_pimpl->Print( interactive );
2238 }
2239
2240 void wxPrintPreview::DetermineScaling()
2241 {
2242 m_pimpl->DetermineScaling();
2243 }
2244
2245 #endif // wxUSE_PRINTING_ARCHITECTURE