Robert Lang's patch [ 1583183 ] Fixes printing/print preview inconsistencies
[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/msgdlg.h"
31 #include "wx/layout.h"
32 #include "wx/choice.h"
33 #include "wx/button.h"
34 #include "wx/settings.h"
35 #include "wx/dcmemory.h"
36 #include "wx/stattext.h"
37 #include "wx/intl.h"
38 #include "wx/textdlg.h"
39 #include "wx/sizer.h"
40 #include "wx/module.h"
41 #endif // !WX_PRECOMP
42
43 #include "wx/prntbase.h"
44 #include "wx/printdlg.h"
45 #include "wx/print.h"
46 #include "wx/dcprint.h"
47
48 #include <stdlib.h>
49 #include <string.h>
50
51 #if defined(__WXMSW__) && !defined(__WXUNIVERSAL__)
52 #include "wx/msw/printdlg.h"
53 #elif defined(__WXMAC__)
54 #include "wx/mac/printdlg.h"
55 #include "wx/mac/private/print.h"
56 #else
57 #include "wx/generic/prntdlgg.h"
58 #include "wx/dcps.h"
59 #endif
60
61 #ifdef __WXMSW__
62 #ifndef __WIN32__
63 #include <print.h>
64 #endif
65 #endif // __WXMSW__
66
67 //----------------------------------------------------------------------------
68 // wxPrintFactory
69 //----------------------------------------------------------------------------
70
71 wxPrintFactory *wxPrintFactory::m_factory = NULL;
72
73 void wxPrintFactory::SetPrintFactory( wxPrintFactory *factory )
74 {
75 if (wxPrintFactory::m_factory)
76 delete wxPrintFactory::m_factory;
77
78 wxPrintFactory::m_factory = factory;
79 }
80
81 wxPrintFactory *wxPrintFactory::GetFactory()
82 {
83 if (!wxPrintFactory::m_factory)
84 wxPrintFactory::m_factory = new wxNativePrintFactory;
85
86 return wxPrintFactory::m_factory;
87 }
88
89 //----------------------------------------------------------------------------
90 // wxNativePrintFactory
91 //----------------------------------------------------------------------------
92
93 wxPrinterBase *wxNativePrintFactory::CreatePrinter( wxPrintDialogData *data )
94 {
95 #if defined(__WXMSW__) && !defined(__WXUNIVERSAL__)
96 return new wxWindowsPrinter( data );
97 #elif defined(__WXMAC__)
98 return new wxMacPrinter( data );
99 #elif defined(__WXPM__)
100 return new wxOS2Printer( data );
101 #else
102 return new wxPostScriptPrinter( data );
103 #endif
104 }
105
106 wxPrintPreviewBase *wxNativePrintFactory::CreatePrintPreview( wxPrintout *preview,
107 wxPrintout *printout, wxPrintDialogData *data )
108 {
109 #if defined(__WXMSW__) && !defined(__WXUNIVERSAL__)
110 return new wxWindowsPrintPreview( preview, printout, data );
111 #elif defined(__WXMAC__)
112 return new wxMacPrintPreview( preview, printout, data );
113 #elif defined(__WXPM__)
114 return new wxOS2PrintPreview( preview, printout, data );
115 #else
116 return new wxPostScriptPrintPreview( preview, printout, data );
117 #endif
118 }
119
120 wxPrintPreviewBase *wxNativePrintFactory::CreatePrintPreview( wxPrintout *preview,
121 wxPrintout *printout, wxPrintData *data )
122 {
123 #if defined(__WXMSW__) && !defined(__WXUNIVERSAL__)
124 return new wxWindowsPrintPreview( preview, printout, data );
125 #elif defined(__WXMAC__)
126 return new wxMacPrintPreview( preview, printout, data );
127 #elif defined(__WXPM__)
128 return new wxOS2PrintPreview( preview, printout, data );
129 #else
130 return new wxPostScriptPrintPreview( preview, printout, data );
131 #endif
132 }
133
134 wxPrintDialogBase *wxNativePrintFactory::CreatePrintDialog( wxWindow *parent,
135 wxPrintDialogData *data )
136 {
137 #if defined(__WXMSW__) && !defined(__WXUNIVERSAL__)
138 return new wxWindowsPrintDialog( parent, data );
139 #elif defined(__WXMAC__)
140 return new wxMacPrintDialog( parent, data );
141 #else
142 return new wxGenericPrintDialog( parent, data );
143 #endif
144 }
145
146 wxPrintDialogBase *wxNativePrintFactory::CreatePrintDialog( wxWindow *parent,
147 wxPrintData *data )
148 {
149 #if defined(__WXMSW__) && !defined(__WXUNIVERSAL__)
150 return new wxWindowsPrintDialog( parent, data );
151 #elif defined(__WXMAC__)
152 return new wxMacPrintDialog( parent, data );
153 #else
154 return new wxGenericPrintDialog( parent, data );
155 #endif
156 }
157
158 wxPageSetupDialogBase *wxNativePrintFactory::CreatePageSetupDialog( wxWindow *parent,
159 wxPageSetupDialogData *data )
160 {
161 #if defined(__WXMSW__) && !defined(__WXUNIVERSAL__)
162 return new wxWindowsPageSetupDialog( parent, data );
163 #elif defined(__WXMAC__)
164 return new wxMacPageSetupDialog( parent, data );
165 #else
166 return new wxGenericPageSetupDialog( parent, data );
167 #endif
168 }
169
170 bool wxNativePrintFactory::HasPrintSetupDialog()
171 {
172 #if defined(__WXMSW__) && !defined(__WXUNIVERSAL__)
173 return false;
174 #elif defined(__WXMAC__)
175 return false;
176 #else
177 // Only here do we need to provide the print setup
178 // dialog ourselves, the other platforms either have
179 // none, don't make it accessible or let you configure
180 // the printer from the wxPrintDialog anyway.
181 return true;
182 #endif
183
184 }
185
186 wxDialog *wxNativePrintFactory::CreatePrintSetupDialog( wxWindow *parent,
187 wxPrintData *data )
188 {
189 #if defined(__WXMSW__) && !defined(__WXUNIVERSAL__)
190 wxUnusedVar(parent);
191 wxUnusedVar(data);
192 return NULL;
193 #elif defined(__WXMAC__)
194 wxUnusedVar(parent);
195 wxUnusedVar(data);
196 return NULL;
197 #else
198 // Only here do we need to provide the print setup
199 // dialog ourselves, the other platforms either have
200 // none, don't make it accessible or let you configure
201 // the printer from the wxPrintDialog anyway.
202 return new wxGenericPrintSetupDialog( parent, data );
203 #endif
204 }
205
206 wxDC* wxNativePrintFactory::CreatePrinterDC( const wxPrintData& data )
207 {
208 #if defined(__WXMSW__) && !defined(__WXUNIVERSAL__)
209 return new wxPrinterDC(data);
210 #elif defined(__WXMAC__)
211 return new wxPrinterDC(data);
212 #else
213 return new wxPostScriptDC(data);
214 #endif
215 }
216
217 bool wxNativePrintFactory::HasOwnPrintToFile()
218 {
219 // Only relevant for PostScript and here the
220 // setup dialog provides no "print to file"
221 // option. In the GNOME setup dialog, the
222 // setup dialog has its own print to file.
223 return false;
224 }
225
226 bool wxNativePrintFactory::HasPrinterLine()
227 {
228 // Only relevant for PostScript for now
229 return true;
230 }
231
232 wxString wxNativePrintFactory::CreatePrinterLine()
233 {
234 // Only relevant for PostScript for now
235
236 // We should query "lpstat -d" here
237 return _("Generic PostScript");
238 }
239
240 bool wxNativePrintFactory::HasStatusLine()
241 {
242 // Only relevant for PostScript for now
243 return true;
244 }
245
246 wxString wxNativePrintFactory::CreateStatusLine()
247 {
248 // Only relevant for PostScript for now
249
250 // We should query "lpstat -r" or "lpstat -p" here
251 return _("Ready");
252 }
253
254 wxPrintNativeDataBase *wxNativePrintFactory::CreatePrintNativeData()
255 {
256 #if defined(__WXMSW__) && !defined(__WXUNIVERSAL__)
257 return new wxWindowsPrintNativeData;
258 #elif defined(__WXMAC__)
259 return new wxMacCarbonPrintData;
260 #else
261 return new wxPostScriptPrintNativeData;
262 #endif
263 }
264
265 //----------------------------------------------------------------------------
266 // wxPrintNativeDataBase
267 //----------------------------------------------------------------------------
268
269 IMPLEMENT_ABSTRACT_CLASS(wxPrintNativeDataBase, wxObject)
270
271 wxPrintNativeDataBase::wxPrintNativeDataBase()
272 {
273 m_ref = 1;
274 }
275
276 //----------------------------------------------------------------------------
277 // wxPrintFactoryModule
278 //----------------------------------------------------------------------------
279
280 class wxPrintFactoryModule: public wxModule
281 {
282 public:
283 wxPrintFactoryModule() {}
284 bool OnInit() { return true; }
285 void OnExit() { wxPrintFactory::SetPrintFactory( NULL ); }
286
287 private:
288 DECLARE_DYNAMIC_CLASS(wxPrintFactoryModule)
289 };
290
291 IMPLEMENT_DYNAMIC_CLASS(wxPrintFactoryModule, wxModule)
292
293 //----------------------------------------------------------------------------
294 // wxPrinterBase
295 //----------------------------------------------------------------------------
296
297 IMPLEMENT_CLASS(wxPrinterBase, wxObject)
298
299 wxPrinterBase::wxPrinterBase(wxPrintDialogData *data)
300 {
301 m_currentPrintout = (wxPrintout *) NULL;
302 sm_abortWindow = (wxWindow *) NULL;
303 sm_abortIt = false;
304 if (data)
305 m_printDialogData = (*data);
306 sm_lastError = wxPRINTER_NO_ERROR;
307 }
308
309 wxWindow *wxPrinterBase::sm_abortWindow = (wxWindow *) NULL;
310 bool wxPrinterBase::sm_abortIt = false;
311 wxPrinterError wxPrinterBase::sm_lastError = wxPRINTER_NO_ERROR;
312
313 wxPrinterBase::~wxPrinterBase()
314 {
315 }
316
317 wxWindow *wxPrinterBase::CreateAbortWindow(wxWindow *parent, wxPrintout * printout)
318 {
319 wxPrintAbortDialog *dialog = new wxPrintAbortDialog(parent, _("Printing ") , wxDefaultPosition, wxDefaultSize, wxDEFAULT_DIALOG_STYLE);
320
321 wxBoxSizer *button_sizer = new wxBoxSizer( wxVERTICAL );
322 button_sizer->Add( new wxStaticText(dialog, wxID_ANY, _("Please wait while printing\n") + printout->GetTitle() ), 0, wxALL, 10 );
323 button_sizer->Add( new wxButton( dialog, wxID_CANCEL, wxT("Cancel") ), 0, wxALL | wxALIGN_CENTER, 10 );
324
325 dialog->SetAutoLayout( true );
326 dialog->SetSizer( button_sizer );
327
328 button_sizer->Fit(dialog);
329 button_sizer->SetSizeHints (dialog) ;
330
331 return dialog;
332 }
333
334 void wxPrinterBase::ReportError(wxWindow *parent, wxPrintout *WXUNUSED(printout), const wxString& message)
335 {
336 wxMessageBox(message, _("Printing Error"), wxOK, parent);
337 }
338
339 wxPrintDialogData& wxPrinterBase::GetPrintDialogData() const
340 {
341 return (wxPrintDialogData&) m_printDialogData;
342 }
343
344 //----------------------------------------------------------------------------
345 // wxPrinter
346 //----------------------------------------------------------------------------
347
348 IMPLEMENT_CLASS(wxPrinter, wxPrinterBase)
349
350 wxPrinter::wxPrinter(wxPrintDialogData *data)
351 {
352 m_pimpl = wxPrintFactory::GetFactory()->CreatePrinter( data );
353 }
354
355 wxPrinter::~wxPrinter()
356 {
357 delete m_pimpl;
358 }
359
360 wxWindow *wxPrinter::CreateAbortWindow(wxWindow *parent, wxPrintout *printout)
361 {
362 return m_pimpl->CreateAbortWindow( parent, printout );
363 }
364
365 void wxPrinter::ReportError(wxWindow *parent, wxPrintout *printout, const wxString& message)
366 {
367 m_pimpl->ReportError( parent, printout, message );
368 }
369
370 bool wxPrinter::Setup(wxWindow *parent)
371 {
372 return m_pimpl->Setup( parent );
373 }
374
375 bool wxPrinter::Print(wxWindow *parent, wxPrintout *printout, bool prompt)
376 {
377 return m_pimpl->Print( parent, printout, prompt );
378 }
379
380 wxDC* wxPrinter::PrintDialog(wxWindow *parent)
381 {
382 return m_pimpl->PrintDialog( parent );
383 }
384
385 wxPrintDialogData& wxPrinter::GetPrintDialogData() const
386 {
387 return m_pimpl->GetPrintDialogData();
388 }
389
390 // ---------------------------------------------------------------------------
391 // wxPrintDialogBase: the dialog for printing.
392 // ---------------------------------------------------------------------------
393
394 IMPLEMENT_ABSTRACT_CLASS(wxPrintDialogBase, wxDialog)
395
396 wxPrintDialogBase::wxPrintDialogBase(wxWindow *parent,
397 wxWindowID id,
398 const wxString &title,
399 const wxPoint &pos,
400 const wxSize &size,
401 long style)
402 : wxDialog( parent, id, title.empty() ? wxString(_("Print")) : title,
403 pos, size, style )
404 {
405 }
406
407 // ---------------------------------------------------------------------------
408 // wxPrintDialog: the dialog for printing
409 // ---------------------------------------------------------------------------
410
411 IMPLEMENT_CLASS(wxPrintDialog, wxObject)
412
413 wxPrintDialog::wxPrintDialog(wxWindow *parent, wxPrintDialogData* data)
414 {
415 m_pimpl = wxPrintFactory::GetFactory()->CreatePrintDialog( parent, data );
416 }
417
418 wxPrintDialog::wxPrintDialog(wxWindow *parent, wxPrintData* data)
419 {
420 m_pimpl = wxPrintFactory::GetFactory()->CreatePrintDialog( parent, data );
421 }
422
423 wxPrintDialog::~wxPrintDialog()
424 {
425 delete m_pimpl;
426 }
427
428 int wxPrintDialog::ShowModal()
429 {
430 return m_pimpl->ShowModal();
431 }
432
433 wxPrintDialogData& wxPrintDialog::GetPrintDialogData()
434 {
435 return m_pimpl->GetPrintDialogData();
436 }
437
438 wxPrintData& wxPrintDialog::GetPrintData()
439 {
440 return m_pimpl->GetPrintData();
441 }
442
443 wxDC *wxPrintDialog::GetPrintDC()
444 {
445 return m_pimpl->GetPrintDC();
446 }
447
448 // ---------------------------------------------------------------------------
449 // wxPageSetupDialogBase: the page setup dialog
450 // ---------------------------------------------------------------------------
451
452 IMPLEMENT_ABSTRACT_CLASS(wxPageSetupDialogBase, wxDialog)
453
454 wxPageSetupDialogBase::wxPageSetupDialogBase(wxWindow *parent,
455 wxWindowID id,
456 const wxString &title,
457 const wxPoint &pos,
458 const wxSize &size,
459 long style)
460 : wxDialog( parent, id, title.empty() ? wxString(_("Page setup")) : title,
461 pos, size, style )
462 {
463 }
464
465 // ---------------------------------------------------------------------------
466 // wxPageSetupDialog: the page setup dialog
467 // ---------------------------------------------------------------------------
468
469 IMPLEMENT_CLASS(wxPageSetupDialog, wxObject)
470
471 wxPageSetupDialog::wxPageSetupDialog(wxWindow *parent, wxPageSetupDialogData *data )
472 {
473 m_pimpl = wxPrintFactory::GetFactory()->CreatePageSetupDialog( parent, data );
474 }
475
476 wxPageSetupDialog::~wxPageSetupDialog()
477 {
478 delete m_pimpl;
479 }
480
481 int wxPageSetupDialog::ShowModal()
482 {
483 return m_pimpl->ShowModal();
484 }
485
486 wxPageSetupDialogData& wxPageSetupDialog::GetPageSetupDialogData()
487 {
488 return m_pimpl->GetPageSetupDialogData();
489 }
490
491 // old name
492 wxPageSetupDialogData& wxPageSetupDialog::GetPageSetupData()
493 {
494 return m_pimpl->GetPageSetupDialogData();
495 }
496
497 //----------------------------------------------------------------------------
498 // wxPrintAbortDialog
499 //----------------------------------------------------------------------------
500
501 BEGIN_EVENT_TABLE(wxPrintAbortDialog, wxDialog)
502 EVT_BUTTON(wxID_CANCEL, wxPrintAbortDialog::OnCancel)
503 END_EVENT_TABLE()
504
505 void wxPrintAbortDialog::OnCancel(wxCommandEvent& WXUNUSED(event))
506 {
507 wxPrinterBase::sm_abortIt = true;
508 wxPrinterBase::sm_abortWindow->Show(false);
509 wxPrinterBase::sm_abortWindow->Close(true);
510 wxPrinterBase::sm_abortWindow = (wxWindow *) NULL;
511 }
512
513 //----------------------------------------------------------------------------
514 // wxPrintout
515 //----------------------------------------------------------------------------
516
517 IMPLEMENT_ABSTRACT_CLASS(wxPrintout, wxObject)
518
519 wxPrintout::wxPrintout(const wxString& title)
520 {
521 m_printoutTitle = title ;
522 m_printoutDC = (wxDC *) NULL;
523 m_pageWidthMM = 0;
524 m_pageHeightMM = 0;
525 m_pageWidthPixels = 0;
526 m_pageHeightPixels = 0;
527 m_PPIScreenX = 0;
528 m_PPIScreenY = 0;
529 m_PPIPrinterX = 0;
530 m_PPIPrinterY = 0;
531 m_isPreview = false;
532 }
533
534 wxPrintout::~wxPrintout()
535 {
536 }
537
538 bool wxPrintout::OnBeginDocument(int WXUNUSED(startPage), int WXUNUSED(endPage))
539 {
540 return GetDC()->StartDoc(_("Printing ") + m_printoutTitle);
541 }
542
543 void wxPrintout::OnEndDocument()
544 {
545 GetDC()->EndDoc();
546 }
547
548 void wxPrintout::OnBeginPrinting()
549 {
550 }
551
552 void wxPrintout::OnEndPrinting()
553 {
554 }
555
556 bool wxPrintout::HasPage(int page)
557 {
558 return (page == 1);
559 }
560
561 void wxPrintout::GetPageInfo(int *minPage, int *maxPage, int *fromPage, int *toPage)
562 {
563 *minPage = 1;
564 *maxPage = 32000;
565 *fromPage = 1;
566 *toPage = 1;
567 }
568
569 void wxPrintout::FitThisSizeToPaper(const wxSize& imageSize)
570 {
571 // Set the DC scale and origin so that the given image size fits within the
572 // entire page and the origin is at the top left corner of the page. Note
573 // that with most printers, portions of the page will be non-printable. Use
574 // this if you're managing your own page margins.
575 if (!m_printoutDC) return;
576 wxRect paperRect = GetPaperRectPixels();
577 wxCoord pw, ph;
578 GetPageSizePixels(&pw, &ph);
579 wxCoord w, h;
580 m_printoutDC->GetSize(&w, &h);
581 float scaleX = ((float(paperRect.width) * w) / (float(pw) * imageSize.x));
582 float scaleY = ((float(paperRect.height) * h) / (float(ph) * imageSize.y));
583 float actualScale = wxMin(scaleX, scaleY);
584 m_printoutDC->SetUserScale(actualScale, actualScale);
585 m_printoutDC->SetDeviceOrigin(0, 0);
586 wxRect logicalPaperRect = GetLogicalPaperRect();
587 SetLogicalOrigin(logicalPaperRect.x, logicalPaperRect.y);
588 }
589
590 void wxPrintout::FitThisSizeToPage(const wxSize& imageSize)
591 {
592 // Set the DC scale and origin so that the given image size fits within the
593 // printable area of the page and the origin is at the top left corner of
594 // the printable area.
595 if (!m_printoutDC) return;
596 int w, h;
597 m_printoutDC->GetSize(&w, &h);
598 float scaleX = float(w) / imageSize.x;
599 float scaleY = float(h) / imageSize.y;
600 float actualScale = wxMin(scaleX, scaleY);
601 m_printoutDC->SetUserScale(actualScale, actualScale);
602 m_printoutDC->SetDeviceOrigin(0, 0);
603 }
604
605 void wxPrintout::FitThisSizeToPageMargins(const wxSize& imageSize, const wxPageSetupDialogData& pageSetupData)
606 {
607 // Set the DC scale and origin so that the given image size fits within the
608 // page margins defined in the given wxPageSetupDialogData object and the
609 // origin is at the top left corner of the page margins.
610 if (!m_printoutDC) return;
611 wxRect paperRect = GetPaperRectPixels();
612 wxCoord pw, ph;
613 GetPageSizePixels(&pw, &ph);
614 wxPoint topLeft = pageSetupData.GetMarginTopLeft();
615 wxPoint bottomRight = pageSetupData.GetMarginBottomRight();
616 wxCoord mw, mh;
617 GetPageSizeMM(&mw, &mh);
618 float mmToDeviceX = float(pw) / mw;
619 float mmToDeviceY = float(ph) / mh;
620 wxRect pageMarginsRect(paperRect.x + wxCoord(mmToDeviceX * topLeft.x),
621 paperRect.y + wxCoord(mmToDeviceY * topLeft.y),
622 paperRect.width - wxCoord(mmToDeviceX * (topLeft.x + bottomRight.x)),
623 paperRect.height - wxCoord(mmToDeviceY * (topLeft.y + bottomRight.y)));
624 wxCoord w, h;
625 m_printoutDC->GetSize(&w, &h);
626 float scaleX = (float(pageMarginsRect.width) * w) / (float(pw) * imageSize.x);
627 float scaleY = (float(pageMarginsRect.height) * h) / (float(ph) * imageSize.y);
628 float actualScale = wxMin(scaleX, scaleY);
629 m_printoutDC->SetUserScale(actualScale, actualScale);
630 m_printoutDC->SetDeviceOrigin(0, 0);
631 wxRect logicalPageMarginsRect = GetLogicalPageMarginsRect(pageSetupData);
632 SetLogicalOrigin(logicalPageMarginsRect.x, logicalPageMarginsRect.y);
633 }
634
635 void wxPrintout::MapScreenSizeToPaper()
636 {
637 // Set the DC scale so that an image on the screen is the same size on the
638 // paper and the origin is at the top left of the paper. Note that with most
639 // printers, portions of the page will be cut off. Use this if you're
640 // managing your own page margins.
641 if (!m_printoutDC) return;
642 MapScreenSizeToPage();
643 wxRect logicalPaperRect = GetLogicalPaperRect();
644 SetLogicalOrigin(logicalPaperRect.x, logicalPaperRect.y);
645 }
646
647 void wxPrintout::MapScreenSizeToPage()
648 {
649 // Set the DC scale and origin so that an image on the screen is the same
650 // size on the paper and the origin is at the top left of the printable area.
651 if (!m_printoutDC) return;
652 int ppiScreenX, ppiScreenY;
653 GetPPIScreen(&ppiScreenX, &ppiScreenY);
654 int ppiPrinterX, ppiPrinterY;
655 GetPPIPrinter(&ppiPrinterX, &ppiPrinterY);
656 int w, h;
657 m_printoutDC->GetSize(&w, &h);
658 int pageSizePixelsX, pageSizePixelsY;
659 GetPageSizePixels(&pageSizePixelsX, &pageSizePixelsY);
660 float userScaleX = (float(ppiPrinterX) * w) / (float(ppiScreenX) * pageSizePixelsX);
661 float userScaleY = (float(ppiPrinterY) * h) / (float(ppiScreenY) * pageSizePixelsY);
662 m_printoutDC->SetUserScale(userScaleX, userScaleY);
663 m_printoutDC->SetDeviceOrigin(0, 0);
664 }
665
666 void wxPrintout::MapScreenSizeToPageMargins(const wxPageSetupDialogData& pageSetupData)
667 {
668 // Set the DC scale so that an image on the screen is the same size on the
669 // paper and the origin is at the top left of the page margins defined by
670 // the given wxPageSetupDialogData object.
671 if (!m_printoutDC) return;
672 MapScreenSizeToPage();
673 wxRect logicalPageMarginsRect = GetLogicalPageMarginsRect(pageSetupData);
674 SetLogicalOrigin(logicalPageMarginsRect.x, logicalPageMarginsRect.y);
675 }
676
677 void wxPrintout::MapScreenSizeToDevice()
678 {
679 // Set the DC scale so that a screen pixel is the same size as a device
680 // pixel and the origin is at the top left of the printable area.
681 if (!m_printoutDC) return;
682 int w, h;
683 m_printoutDC->GetSize(&w, &h);
684 int pageSizePixelsX, pageSizePixelsY;
685 GetPageSizePixels(&pageSizePixelsX, &pageSizePixelsY);
686 float userScaleX = float(w) / pageSizePixelsX;
687 float userScaleY = float(h) / pageSizePixelsY;
688 m_printoutDC->SetUserScale(userScaleX, userScaleY);
689 m_printoutDC->SetDeviceOrigin(0, 0);
690 }
691
692 wxRect wxPrintout::GetLogicalPaperRect() const
693 {
694 // Return the rectangle in logical units that corresponds to the paper
695 // rectangle.
696 wxRect paperRect = GetPaperRectPixels();
697 wxCoord pw, ph;
698 GetPageSizePixels(&pw, &ph);
699 wxCoord w, h;
700 m_printoutDC->GetSize(&w, &h);
701 if (w == pw && h == ph) {
702 // this DC matches the printed page, so no scaling
703 return wxRect(m_printoutDC->DeviceToLogicalX(paperRect.x),
704 m_printoutDC->DeviceToLogicalY(paperRect.y),
705 m_printoutDC->DeviceToLogicalXRel(paperRect.width),
706 m_printoutDC->DeviceToLogicalYRel(paperRect.height));
707 }
708 // This DC doesn't match the printed page, so we have to scale.
709 float scaleX = float(w) / pw;
710 float scaleY = float(h) / ph;
711 return wxRect(m_printoutDC->DeviceToLogicalX(wxCoord(paperRect.x * scaleX)),
712 m_printoutDC->DeviceToLogicalY(wxCoord(paperRect.y * scaleY)),
713 m_printoutDC->DeviceToLogicalXRel(wxCoord(paperRect.width * scaleX)),
714 m_printoutDC->DeviceToLogicalYRel(wxCoord(paperRect.height * scaleY)));
715 }
716
717 wxRect wxPrintout::GetLogicalPageRect() const
718 {
719 // Return the rectangle in logical units that corresponds to the printable
720 // area.
721 int w, h;
722 m_printoutDC->GetSize(&w, &h);
723 return wxRect(m_printoutDC->DeviceToLogicalX(0),
724 m_printoutDC->DeviceToLogicalY(0),
725 m_printoutDC->DeviceToLogicalXRel(w),
726 m_printoutDC->DeviceToLogicalYRel(h));
727 }
728
729 wxRect wxPrintout::GetLogicalPageMarginsRect(const wxPageSetupDialogData& pageSetupData) const
730 {
731 // Return the rectangle in logical units that corresponds to the region
732 // within the page margins as specified by the given wxPageSetupDialogData
733 // object.
734 wxRect paperRect = GetPaperRectPixels();
735 wxCoord pw, ph;
736 GetPageSizePixels(&pw, &ph);
737 wxPoint topLeft = pageSetupData.GetMarginTopLeft();
738 wxPoint bottomRight = pageSetupData.GetMarginBottomRight();
739 wxCoord mw, mh;
740 GetPageSizeMM(&mw, &mh);
741 float mmToDeviceX = float(pw) / mw;
742 float mmToDeviceY = float(ph) / mh;
743 wxRect pageMarginsRect(paperRect.x + wxCoord(mmToDeviceX * topLeft.x),
744 paperRect.y + wxCoord(mmToDeviceY * topLeft.y),
745 paperRect.width - wxCoord(mmToDeviceX * (topLeft.x + bottomRight.x)),
746 paperRect.height - wxCoord(mmToDeviceY * (topLeft.y + bottomRight.y)));
747 wxCoord w, h;
748 m_printoutDC->GetSize(&w, &h);
749 if (w == pw && h == ph) {
750 // this DC matches the printed page, so no scaling
751 return wxRect(m_printoutDC->DeviceToLogicalX(pageMarginsRect.x),
752 m_printoutDC->DeviceToLogicalY(pageMarginsRect.y),
753 m_printoutDC->DeviceToLogicalXRel(pageMarginsRect.width),
754 m_printoutDC->DeviceToLogicalYRel(pageMarginsRect.height));
755 }
756 // This DC doesn't match the printed page, so we have to scale.
757 float scaleX = float(w) / pw;
758 float scaleY = float(h) / ph;
759 return wxRect(m_printoutDC->DeviceToLogicalX(wxCoord(pageMarginsRect.x * scaleX)),
760 m_printoutDC->DeviceToLogicalY(wxCoord(pageMarginsRect.y * scaleY)),
761 m_printoutDC->DeviceToLogicalXRel(wxCoord(pageMarginsRect.width * scaleX)),
762 m_printoutDC->DeviceToLogicalYRel(wxCoord(pageMarginsRect.height * scaleY)));
763 }
764
765 void wxPrintout::SetLogicalOrigin(wxCoord x, wxCoord y)
766 {
767 // Set the device origin by specifying a point in logical coordinates.
768 m_printoutDC->SetDeviceOrigin(m_printoutDC->LogicalToDeviceX(x),
769 m_printoutDC->LogicalToDeviceY(y));
770 }
771
772 void wxPrintout::OffsetLogicalOrigin(wxCoord xoff, wxCoord yoff)
773 {
774 // Offset the device origin by a specified distance in device coordinates.
775 wxCoord x = m_printoutDC->LogicalToDeviceX(0);
776 wxCoord y = m_printoutDC->LogicalToDeviceY(0);
777 m_printoutDC->SetDeviceOrigin(x + m_printoutDC->LogicalToDeviceXRel(xoff),
778 y + m_printoutDC->LogicalToDeviceYRel(yoff));
779 }
780
781
782 //----------------------------------------------------------------------------
783 // wxPreviewCanvas
784 //----------------------------------------------------------------------------
785
786 IMPLEMENT_CLASS(wxPreviewCanvas, wxWindow)
787
788 BEGIN_EVENT_TABLE(wxPreviewCanvas, wxScrolledWindow)
789 EVT_PAINT(wxPreviewCanvas::OnPaint)
790 EVT_CHAR(wxPreviewCanvas::OnChar)
791 EVT_SYS_COLOUR_CHANGED(wxPreviewCanvas::OnSysColourChanged)
792 #if wxUSE_MOUSEWHEEL
793 EVT_MOUSEWHEEL(wxPreviewCanvas::OnMouseWheel)
794 #endif
795 END_EVENT_TABLE()
796
797 // VZ: the current code doesn't refresh properly without
798 // wxFULL_REPAINT_ON_RESIZE, this must be fixed as otherwise we have
799 // really horrible flicker when resizing the preview frame, but without
800 // this style it simply doesn't work correctly at all...
801 wxPreviewCanvas::wxPreviewCanvas(wxPrintPreviewBase *preview, wxWindow *parent,
802 const wxPoint& pos, const wxSize& size, long style, const wxString& name):
803 wxScrolledWindow(parent, wxID_ANY, pos, size, style | wxFULL_REPAINT_ON_RESIZE, name)
804 {
805 m_printPreview = preview;
806 #ifdef __WXMAC__
807 // The app workspace colour is always white, but we should have
808 // a contrast with the page.
809 wxSystemColour colourIndex = wxSYS_COLOUR_3DDKSHADOW;
810 #else
811 wxSystemColour colourIndex = wxSYS_COLOUR_APPWORKSPACE;
812 #endif
813 SetBackgroundColour(wxSystemSettings::GetColour(colourIndex));
814
815 SetScrollbars(10, 10, 100, 100);
816 }
817
818 wxPreviewCanvas::~wxPreviewCanvas()
819 {
820 }
821
822 void wxPreviewCanvas::OnPaint(wxPaintEvent& WXUNUSED(event))
823 {
824 wxPaintDC dc(this);
825 PrepareDC( dc );
826
827 /*
828 #ifdef __WXGTK__
829 if (!GetUpdateRegion().IsEmpty())
830 dc.SetClippingRegion( GetUpdateRegion() );
831 #endif
832 */
833
834 if (m_printPreview)
835 {
836 m_printPreview->PaintPage(this, dc);
837 }
838 }
839
840 // Responds to colour changes, and passes event on to children.
841 void wxPreviewCanvas::OnSysColourChanged(wxSysColourChangedEvent& event)
842 {
843 #ifdef __WXMAC__
844 // The app workspace colour is always white, but we should have
845 // a contrast with the page.
846 wxSystemColour colourIndex = wxSYS_COLOUR_3DDKSHADOW;
847 #else
848 wxSystemColour colourIndex = wxSYS_COLOUR_APPWORKSPACE;
849 #endif
850 SetBackgroundColour(wxSystemSettings::GetColour(colourIndex));
851 Refresh();
852
853 // Propagate the event to the non-top-level children
854 wxWindow::OnSysColourChanged(event);
855 }
856
857 void wxPreviewCanvas::OnChar(wxKeyEvent &event)
858 {
859 wxPreviewControlBar* controlBar = ((wxPreviewFrame*) GetParent())->GetControlBar();
860 if (event.GetKeyCode() == WXK_ESCAPE)
861 {
862 ((wxPreviewFrame*) GetParent())->Close(true);
863 return;
864 }
865 else if (event.GetKeyCode() == WXK_TAB)
866 {
867 controlBar->OnGoto();
868 return;
869 }
870 else if (event.GetKeyCode() == WXK_RETURN)
871 {
872 controlBar->OnPrint();
873 return;
874 }
875
876 if (!event.ControlDown())
877 {
878 event.Skip();
879 return;
880 }
881
882 switch(event.GetKeyCode())
883 {
884 case WXK_PAGEDOWN:
885 controlBar->OnNext(); break;
886 case WXK_PAGEUP:
887 controlBar->OnPrevious(); break;
888 case WXK_HOME:
889 controlBar->OnFirst(); break;
890 case WXK_END:
891 controlBar->OnLast(); break;
892 default:
893 event.Skip();
894 }
895 }
896
897 #if wxUSE_MOUSEWHEEL
898
899 void wxPreviewCanvas::OnMouseWheel(wxMouseEvent& event)
900 {
901 wxPreviewControlBar *
902 controlBar = wxStaticCast(GetParent(), wxPreviewFrame)->GetControlBar();
903
904 if ( controlBar )
905 {
906 if ( event.ControlDown() && event.GetWheelRotation() != 0 )
907 {
908 int currentZoom = controlBar->GetZoomControl();
909
910 int delta;
911 if ( currentZoom < 100 )
912 delta = 5;
913 else if ( currentZoom <= 120 )
914 delta = 10;
915 else
916 delta = 50;
917
918 if ( event.GetWheelRotation() > 0 )
919 delta = -delta;
920
921 int newZoom = currentZoom + delta;
922 if ( newZoom < 10 )
923 newZoom = 10;
924 if ( newZoom > 200 )
925 newZoom = 200;
926 if ( newZoom != currentZoom )
927 {
928 controlBar->SetZoomControl(newZoom);
929 m_printPreview->SetZoom(newZoom);
930 Refresh();
931 }
932 return;
933 }
934 }
935
936 event.Skip();
937 }
938
939 #endif // wxUSE_MOUSEWHEEL
940
941 //----------------------------------------------------------------------------
942 // wxPreviewControlBar
943 //----------------------------------------------------------------------------
944
945 IMPLEMENT_CLASS(wxPreviewControlBar, wxWindow)
946
947 BEGIN_EVENT_TABLE(wxPreviewControlBar, wxPanel)
948 EVT_BUTTON(wxID_PREVIEW_CLOSE, wxPreviewControlBar::OnWindowClose)
949 EVT_BUTTON(wxID_PREVIEW_PRINT, wxPreviewControlBar::OnPrintButton)
950 EVT_BUTTON(wxID_PREVIEW_PREVIOUS, wxPreviewControlBar::OnPreviousButton)
951 EVT_BUTTON(wxID_PREVIEW_NEXT, wxPreviewControlBar::OnNextButton)
952 EVT_BUTTON(wxID_PREVIEW_FIRST, wxPreviewControlBar::OnFirstButton)
953 EVT_BUTTON(wxID_PREVIEW_LAST, wxPreviewControlBar::OnLastButton)
954 EVT_BUTTON(wxID_PREVIEW_GOTO, wxPreviewControlBar::OnGotoButton)
955 EVT_CHOICE(wxID_PREVIEW_ZOOM, wxPreviewControlBar::OnZoom)
956 EVT_PAINT(wxPreviewControlBar::OnPaint)
957 END_EVENT_TABLE()
958
959 wxPreviewControlBar::wxPreviewControlBar(wxPrintPreviewBase *preview, long buttons,
960 wxWindow *parent, const wxPoint& pos, const wxSize& size,
961 long style, const wxString& name):
962 wxPanel(parent, wxID_ANY, pos, size, style, name)
963 {
964 m_printPreview = preview;
965 m_closeButton = (wxButton *) NULL;
966 m_nextPageButton = (wxButton *) NULL;
967 m_previousPageButton = (wxButton *) NULL;
968 m_printButton = (wxButton *) NULL;
969 m_zoomControl = (wxChoice *) NULL;
970 m_buttonFlags = buttons;
971 }
972
973 wxPreviewControlBar::~wxPreviewControlBar()
974 {
975 }
976
977 void wxPreviewControlBar::OnPaint(wxPaintEvent& WXUNUSED(event))
978 {
979 wxPaintDC dc(this);
980
981 int w, h;
982 GetSize(&w, &h);
983 dc.SetPen(*wxBLACK_PEN);
984 dc.SetBrush(*wxTRANSPARENT_BRUSH);
985 dc.DrawLine( 0, h-1, w, h-1 );
986 }
987
988 void wxPreviewControlBar::OnWindowClose(wxCommandEvent& WXUNUSED(event))
989 {
990 wxPreviewFrame *frame = (wxPreviewFrame *)GetParent();
991 frame->Close(true);
992 }
993
994 void wxPreviewControlBar::OnPrint(void)
995 {
996 wxPrintPreviewBase *preview = GetPrintPreview();
997 preview->Print(true);
998 }
999
1000 void wxPreviewControlBar::OnNext(void)
1001 {
1002 wxPrintPreviewBase *preview = GetPrintPreview();
1003 if (preview)
1004 {
1005 int currentPage = preview->GetCurrentPage();
1006 if ((preview->GetMaxPage() > 0) &&
1007 (currentPage < preview->GetMaxPage()) &&
1008 preview->GetPrintout()->HasPage(currentPage + 1))
1009 {
1010 preview->SetCurrentPage(currentPage + 1);
1011 }
1012 }
1013 }
1014
1015 void wxPreviewControlBar::OnPrevious(void)
1016 {
1017 wxPrintPreviewBase *preview = GetPrintPreview();
1018 if (preview)
1019 {
1020 int currentPage = preview->GetCurrentPage();
1021 if ((preview->GetMinPage() > 0) &&
1022 (currentPage > preview->GetMinPage()) &&
1023 preview->GetPrintout()->HasPage(currentPage - 1))
1024 {
1025 preview->SetCurrentPage(currentPage - 1);
1026 }
1027 }
1028 }
1029
1030 void wxPreviewControlBar::OnFirst(void)
1031 {
1032 wxPrintPreviewBase *preview = GetPrintPreview();
1033 if (preview)
1034 {
1035 int currentPage = preview->GetMinPage();
1036 if (preview->GetPrintout()->HasPage(currentPage))
1037 {
1038 preview->SetCurrentPage(currentPage);
1039 }
1040 }
1041 }
1042
1043 void wxPreviewControlBar::OnLast(void)
1044 {
1045 wxPrintPreviewBase *preview = GetPrintPreview();
1046 if (preview)
1047 {
1048 int currentPage = preview->GetMaxPage();
1049 if (preview->GetPrintout()->HasPage(currentPage))
1050 {
1051 preview->SetCurrentPage(currentPage);
1052 }
1053 }
1054 }
1055
1056 void wxPreviewControlBar::OnGoto(void)
1057 {
1058 wxPrintPreviewBase *preview = GetPrintPreview();
1059 if (preview)
1060 {
1061 long currentPage;
1062
1063 if (preview->GetMinPage() > 0)
1064 {
1065 wxString strPrompt;
1066 wxString strPage;
1067
1068 strPrompt.Printf( _("Enter a page number between %d and %d:"),
1069 preview->GetMinPage(), preview->GetMaxPage());
1070 strPage.Printf( wxT("%d"), preview->GetCurrentPage() );
1071
1072 strPage =
1073 wxGetTextFromUser( strPrompt, _("Goto Page"), strPage, GetParent());
1074
1075 if ( strPage.ToLong( &currentPage ) )
1076 if (preview->GetPrintout()->HasPage(currentPage))
1077 {
1078 preview->SetCurrentPage(currentPage);
1079 }
1080 }
1081 }
1082 }
1083
1084 void wxPreviewControlBar::OnZoom(wxCommandEvent& WXUNUSED(event))
1085 {
1086 int zoom = GetZoomControl();
1087 if (GetPrintPreview())
1088 GetPrintPreview()->SetZoom(zoom);
1089 }
1090
1091 void wxPreviewControlBar::CreateButtons()
1092 {
1093 SetSize(0, 0, 400, 40);
1094
1095 wxBoxSizer *item0 = new wxBoxSizer( wxHORIZONTAL );
1096
1097 m_closeButton = new wxButton( this, wxID_PREVIEW_CLOSE, _("&Close"), wxDefaultPosition, wxDefaultSize, 0 );
1098 item0->Add( m_closeButton, 0, wxALIGN_CENTRE|wxALL, 5 );
1099
1100 if (m_buttonFlags & wxPREVIEW_PRINT)
1101 {
1102 m_printButton = new wxButton( this, wxID_PREVIEW_PRINT, _("&Print..."), wxDefaultPosition, wxDefaultSize, 0 );
1103 item0->Add( m_printButton, 0, wxALIGN_CENTRE|wxALL, 5 );
1104 }
1105
1106 // Exact-fit buttons are too tiny on wxUniversal
1107 int navButtonStyle;
1108 wxSize navButtonSize;
1109 #ifdef __WXUNIVERSAL__
1110 navButtonStyle = 0;
1111 navButtonSize = wxSize(40, m_closeButton->GetSize().y);
1112 #else
1113 navButtonStyle = wxBU_EXACTFIT;
1114 navButtonSize = wxDefaultSize;
1115 #endif
1116
1117 if (m_buttonFlags & wxPREVIEW_FIRST)
1118 {
1119 m_firstPageButton = new wxButton( this, wxID_PREVIEW_FIRST, _("|<<"), wxDefaultPosition, navButtonSize, navButtonStyle );
1120 item0->Add( m_firstPageButton, 0, wxALIGN_CENTRE|wxALL, 5 );
1121 }
1122
1123 if (m_buttonFlags & wxPREVIEW_PREVIOUS)
1124 {
1125 m_previousPageButton = new wxButton( this, wxID_PREVIEW_PREVIOUS, _("<<"), wxDefaultPosition, navButtonSize, navButtonStyle );
1126 item0->Add( m_previousPageButton, 0, wxALIGN_CENTRE|wxRIGHT|wxTOP|wxBOTTOM, 5 );
1127 }
1128
1129 if (m_buttonFlags & wxPREVIEW_NEXT)
1130 {
1131 m_nextPageButton = new wxButton( this, wxID_PREVIEW_NEXT, _(">>"), wxDefaultPosition, navButtonSize, navButtonStyle );
1132 item0->Add( m_nextPageButton, 0, wxALIGN_CENTRE|wxRIGHT|wxTOP|wxBOTTOM, 5 );
1133 }
1134
1135 if (m_buttonFlags & wxPREVIEW_LAST)
1136 {
1137 m_lastPageButton = new wxButton( this, wxID_PREVIEW_LAST, _(">>|"), wxDefaultPosition, navButtonSize, navButtonStyle );
1138 item0->Add( m_lastPageButton, 0, wxALIGN_CENTRE|wxRIGHT|wxTOP|wxBOTTOM, 5 );
1139 }
1140
1141 if (m_buttonFlags & wxPREVIEW_GOTO)
1142 {
1143 m_gotoPageButton = new wxButton( this, wxID_PREVIEW_GOTO, _("&Goto..."), wxDefaultPosition, wxDefaultSize, 0 );
1144 item0->Add( m_gotoPageButton, 0, wxALIGN_CENTRE|wxALL, 5 );
1145 }
1146
1147 if (m_buttonFlags & wxPREVIEW_ZOOM)
1148 {
1149 wxString choices[] =
1150 {
1151 wxT("10%"), wxT("15%"), wxT("20%"), wxT("25%"), wxT("30%"), wxT("35%"), wxT("40%"), wxT("45%"), wxT("50%"), wxT("55%"),
1152 wxT("60%"), wxT("65%"), wxT("70%"), wxT("75%"), wxT("80%"), wxT("85%"), wxT("90%"), wxT("95%"), wxT("100%"), wxT("110%"),
1153 wxT("120%"), wxT("150%"), wxT("200%")
1154 };
1155 int n = WXSIZEOF(choices);
1156
1157 m_zoomControl = new wxChoice( this, wxID_PREVIEW_ZOOM, wxDefaultPosition, wxSize(70,wxDefaultCoord), n, choices, 0 );
1158 item0->Add( m_zoomControl, 0, wxALIGN_CENTRE|wxALL, 5 );
1159 SetZoomControl(m_printPreview->GetZoom());
1160 }
1161
1162 SetSizer(item0);
1163 item0->Fit(this);
1164 }
1165
1166 void wxPreviewControlBar::SetZoomControl(int zoom)
1167 {
1168 if (m_zoomControl)
1169 {
1170 int n, count = m_zoomControl->GetCount();
1171 long val;
1172 for (n=0; n<count; n++)
1173 {
1174 if (m_zoomControl->GetString(n).BeforeFirst(wxT('%')).ToLong(&val) &&
1175 (val >= long(zoom)))
1176 {
1177 m_zoomControl->SetSelection(n);
1178 return;
1179 }
1180 }
1181
1182 m_zoomControl->SetSelection(count-1);
1183 }
1184 }
1185
1186 int wxPreviewControlBar::GetZoomControl()
1187 {
1188 if (m_zoomControl && (m_zoomControl->GetStringSelection() != wxEmptyString))
1189 {
1190 long val;
1191 if (m_zoomControl->GetStringSelection().BeforeFirst(wxT('%')).ToLong(&val))
1192 return int(val);
1193 }
1194
1195 return 0;
1196 }
1197
1198
1199 /*
1200 * Preview frame
1201 */
1202
1203 IMPLEMENT_CLASS(wxPreviewFrame, wxFrame)
1204
1205 BEGIN_EVENT_TABLE(wxPreviewFrame, wxFrame)
1206 EVT_CLOSE(wxPreviewFrame::OnCloseWindow)
1207 END_EVENT_TABLE()
1208
1209 wxPreviewFrame::wxPreviewFrame(wxPrintPreviewBase *preview, wxWindow *parent, const wxString& title,
1210 const wxPoint& pos, const wxSize& size, long style, const wxString& name):
1211 wxFrame(parent, wxID_ANY, title, pos, size, style, name)
1212 {
1213 m_printPreview = preview;
1214 m_controlBar = NULL;
1215 m_previewCanvas = NULL;
1216 m_windowDisabler = NULL;
1217
1218 // Give the application icon
1219 #ifdef __WXMSW__
1220 wxFrame* topFrame = wxDynamicCast(wxTheApp->GetTopWindow(), wxFrame);
1221 if (topFrame)
1222 SetIcon(topFrame->GetIcon());
1223 #endif
1224 }
1225
1226 wxPreviewFrame::~wxPreviewFrame()
1227 {
1228 }
1229
1230 void wxPreviewFrame::OnCloseWindow(wxCloseEvent& WXUNUSED(event))
1231 {
1232 if (m_windowDisabler)
1233 delete m_windowDisabler;
1234
1235 // Need to delete the printout and the print preview
1236 wxPrintout *printout = m_printPreview->GetPrintout();
1237 if (printout)
1238 {
1239 delete printout;
1240 m_printPreview->SetPrintout(NULL);
1241 m_printPreview->SetCanvas(NULL);
1242 m_printPreview->SetFrame(NULL);
1243 }
1244 delete m_printPreview;
1245
1246 Destroy();
1247 }
1248
1249 void wxPreviewFrame::Initialize()
1250 {
1251 #if wxUSE_STATUSBAR
1252 CreateStatusBar();
1253 #endif
1254 CreateCanvas();
1255 CreateControlBar();
1256
1257 m_printPreview->SetCanvas(m_previewCanvas);
1258 m_printPreview->SetFrame(this);
1259
1260 wxBoxSizer *item0 = new wxBoxSizer( wxVERTICAL );
1261
1262 item0->Add( m_controlBar, 0, wxGROW|wxALIGN_CENTER_VERTICAL, 5 );
1263 item0->Add( m_previewCanvas, 1, wxGROW|wxALIGN_CENTER_VERTICAL, 5 );
1264
1265 SetAutoLayout( true );
1266 SetSizer( item0 );
1267
1268 m_windowDisabler = new wxWindowDisabler(this);
1269
1270 Layout();
1271
1272 m_printPreview->AdjustScrollbars(m_previewCanvas);
1273 m_previewCanvas->SetFocus();
1274 m_controlBar->SetFocus();
1275 }
1276
1277 void wxPreviewFrame::CreateCanvas()
1278 {
1279 m_previewCanvas = new wxPreviewCanvas(m_printPreview, this);
1280 }
1281
1282 void wxPreviewFrame::CreateControlBar()
1283 {
1284 long buttons = wxPREVIEW_DEFAULT;
1285 if (m_printPreview->GetPrintoutForPrinting())
1286 buttons |= wxPREVIEW_PRINT;
1287
1288 m_controlBar = new wxPreviewControlBar(m_printPreview, buttons, this, wxPoint(0,0), wxSize(400, 40));
1289 m_controlBar->CreateButtons();
1290 }
1291
1292 /*
1293 * Print preview
1294 */
1295
1296 IMPLEMENT_CLASS(wxPrintPreviewBase, wxObject)
1297
1298 wxPrintPreviewBase::wxPrintPreviewBase(wxPrintout *printout,
1299 wxPrintout *printoutForPrinting,
1300 wxPrintData *data)
1301 {
1302 if (data)
1303 m_printDialogData = (*data);
1304
1305 Init(printout, printoutForPrinting);
1306 }
1307
1308 wxPrintPreviewBase::wxPrintPreviewBase(wxPrintout *printout,
1309 wxPrintout *printoutForPrinting,
1310 wxPrintDialogData *data)
1311 {
1312 if (data)
1313 m_printDialogData = (*data);
1314
1315 Init(printout, printoutForPrinting);
1316 }
1317
1318 void wxPrintPreviewBase::Init(wxPrintout *printout,
1319 wxPrintout *printoutForPrinting)
1320 {
1321 m_isOk = true;
1322 m_previewPrintout = printout;
1323 if (m_previewPrintout)
1324 m_previewPrintout->SetIsPreview(true);
1325
1326 m_printPrintout = printoutForPrinting;
1327
1328 m_previewCanvas = NULL;
1329 m_previewFrame = NULL;
1330 m_previewBitmap = NULL;
1331 m_currentPage = 1;
1332 m_currentZoom = 70;
1333 m_topMargin = 40;
1334 m_leftMargin = 40;
1335 m_pageWidth = 0;
1336 m_pageHeight = 0;
1337 m_printingPrepared = false;
1338 m_minPage = 1;
1339 m_maxPage = 1;
1340 }
1341
1342 wxPrintPreviewBase::~wxPrintPreviewBase()
1343 {
1344 if (m_previewPrintout)
1345 delete m_previewPrintout;
1346 if (m_previewBitmap)
1347 delete m_previewBitmap;
1348 if (m_printPrintout)
1349 delete m_printPrintout;
1350 }
1351
1352 bool wxPrintPreviewBase::SetCurrentPage(int pageNum)
1353 {
1354 if (m_currentPage == pageNum)
1355 return true;
1356
1357 m_currentPage = pageNum;
1358 if (m_previewBitmap)
1359 {
1360 delete m_previewBitmap;
1361 m_previewBitmap = NULL;
1362 }
1363
1364 if (m_previewCanvas)
1365 {
1366 AdjustScrollbars(m_previewCanvas);
1367
1368 if (!RenderPage(pageNum))
1369 return false;
1370 m_previewCanvas->Refresh();
1371 m_previewCanvas->SetFocus();
1372 }
1373 return true;
1374 }
1375
1376 int wxPrintPreviewBase::GetCurrentPage() const
1377 { return m_currentPage; }
1378 void wxPrintPreviewBase::SetPrintout(wxPrintout *printout)
1379 { m_previewPrintout = printout; }
1380 wxPrintout *wxPrintPreviewBase::GetPrintout() const
1381 { return m_previewPrintout; }
1382 wxPrintout *wxPrintPreviewBase::GetPrintoutForPrinting() const
1383 { return m_printPrintout; }
1384 void wxPrintPreviewBase::SetFrame(wxFrame *frame)
1385 { m_previewFrame = frame; }
1386 void wxPrintPreviewBase::SetCanvas(wxPreviewCanvas *canvas)
1387 { m_previewCanvas = canvas; }
1388 wxFrame *wxPrintPreviewBase::GetFrame() const
1389 { return m_previewFrame; }
1390 wxPreviewCanvas *wxPrintPreviewBase::GetCanvas() const
1391 { return m_previewCanvas; }
1392
1393 void wxPrintPreviewBase::CalcRects(wxPreviewCanvas *canvas, wxRect& pageRect, wxRect& paperRect)
1394 {
1395 // Calculate the rectangles for the printable area of the page and the
1396 // entire paper as they appear on the canvas on-screen.
1397 int canvasWidth, canvasHeight;
1398 canvas->GetSize(&canvasWidth, &canvasHeight);
1399
1400 float zoomScale = float(m_currentZoom) / 100;
1401 float screenPrintableWidth = zoomScale * m_pageWidth * m_previewScaleX;
1402 float screenPrintableHeight = zoomScale * m_pageHeight * m_previewScaleY;
1403
1404 wxRect devicePaperRect = m_previewPrintout->GetPaperRectPixels();
1405 wxCoord devicePrintableWidth, devicePrintableHeight;
1406 m_previewPrintout->GetPageSizePixels(&devicePrintableWidth, &devicePrintableHeight);
1407 float scaleX = screenPrintableWidth / devicePrintableWidth;
1408 float scaleY = screenPrintableHeight / devicePrintableHeight;
1409 paperRect.width = wxCoord(scaleX * devicePaperRect.width);
1410 paperRect.height = wxCoord(scaleY * devicePaperRect.height);
1411
1412 paperRect.x = wxCoord((canvasWidth - paperRect.width)/ 2.0);
1413 if (paperRect.x < m_leftMargin)
1414 paperRect.x = m_leftMargin;
1415 paperRect.y = wxCoord((canvasHeight - paperRect.height)/ 2.0);
1416 if (paperRect.y < m_topMargin)
1417 paperRect.y = m_topMargin;
1418
1419 pageRect.x = paperRect.x - wxCoord(scaleX * devicePaperRect.x);
1420 pageRect.y = paperRect.y - wxCoord(scaleY * devicePaperRect.y);
1421 pageRect.width = wxCoord(screenPrintableWidth);
1422 pageRect.height = wxCoord(screenPrintableHeight);
1423 }
1424
1425
1426 bool wxPrintPreviewBase::PaintPage(wxPreviewCanvas *canvas, wxDC& dc)
1427 {
1428 DrawBlankPage(canvas, dc);
1429
1430 if (!m_previewBitmap)
1431 if (!RenderPage(m_currentPage))
1432 return false;
1433 if (!m_previewBitmap)
1434 return false;
1435 if (!canvas)
1436 return false;
1437
1438 wxRect pageRect, paperRect;
1439 CalcRects(canvas, pageRect, paperRect);
1440 wxMemoryDC temp_dc;
1441 temp_dc.SelectObject(*m_previewBitmap);
1442
1443 dc.Blit(pageRect.x, pageRect.y,
1444 m_previewBitmap->GetWidth(), m_previewBitmap->GetHeight(), &temp_dc, 0, 0);
1445
1446 temp_dc.SelectObject(wxNullBitmap);
1447 return true;
1448 }
1449
1450 // Adjusts the scrollbars for the current scale
1451 void wxPrintPreviewBase::AdjustScrollbars(wxPreviewCanvas *canvas)
1452 {
1453 if (!canvas)
1454 return ;
1455
1456 wxRect pageRect, paperRect;
1457 CalcRects(canvas, pageRect, paperRect);
1458 int totalWidth = paperRect.width + 2 * m_leftMargin;
1459 int totalHeight = paperRect.height + 2 * m_topMargin;
1460 int scrollUnitsX = totalWidth / 10;
1461 int scrollUnitsY = totalHeight / 10;
1462 wxSize virtualSize = canvas->GetVirtualSize();
1463 if (virtualSize.GetWidth() != totalWidth || virtualSize.GetHeight() != totalHeight)
1464 canvas->SetScrollbars(10, 10, scrollUnitsX, scrollUnitsY, 0, 0, true);
1465 }
1466
1467 bool wxPrintPreviewBase::RenderPage(int pageNum)
1468 {
1469 wxBusyCursor busy;
1470
1471 if (!m_previewCanvas)
1472 {
1473 wxFAIL_MSG(_T("wxPrintPreviewBase::RenderPage: must use wxPrintPreviewBase::SetCanvas to let me know about the canvas!"));
1474 return false;
1475 }
1476
1477 wxRect pageRect, paperRect;
1478 CalcRects(m_previewCanvas, pageRect, paperRect);
1479
1480 if (!m_previewBitmap)
1481 {
1482 m_previewBitmap = new wxBitmap(pageRect.width, pageRect.height);
1483
1484 if (!m_previewBitmap || !m_previewBitmap->Ok())
1485 {
1486 if (m_previewBitmap) {
1487 delete m_previewBitmap;
1488 m_previewBitmap = NULL;
1489 }
1490 wxMessageBox(_("Sorry, not enough memory to create a preview."), _("Print Preview Failure"), wxOK);
1491 return false;
1492 }
1493 }
1494
1495 wxMemoryDC memoryDC;
1496 memoryDC.SelectObject(*m_previewBitmap);
1497
1498 memoryDC.Clear();
1499
1500 m_previewPrintout->SetDC(&memoryDC);
1501 m_previewPrintout->SetPageSizePixels(m_pageWidth, m_pageHeight);
1502
1503 // Need to delay OnPreparePrinting until here, so we have enough information.
1504 if (!m_printingPrepared)
1505 {
1506 m_previewPrintout->OnPreparePrinting();
1507 int selFrom, selTo;
1508 m_previewPrintout->GetPageInfo(&m_minPage, &m_maxPage, &selFrom, &selTo);
1509 m_printingPrepared = true;
1510 }
1511
1512 m_previewPrintout->OnBeginPrinting();
1513
1514 if (!m_previewPrintout->OnBeginDocument(m_printDialogData.GetFromPage(), m_printDialogData.GetToPage()))
1515 {
1516 wxMessageBox(_("Could not start document preview."), _("Print Preview Failure"), wxOK);
1517
1518 memoryDC.SelectObject(wxNullBitmap);
1519
1520 delete m_previewBitmap;
1521 m_previewBitmap = NULL;
1522 return false;
1523 }
1524
1525 m_previewPrintout->OnPrintPage(pageNum);
1526 m_previewPrintout->OnEndDocument();
1527 m_previewPrintout->OnEndPrinting();
1528
1529 m_previewPrintout->SetDC(NULL);
1530
1531 memoryDC.SelectObject(wxNullBitmap);
1532
1533 #if wxUSE_STATUSBAR
1534 wxString status;
1535 if (m_maxPage != 0)
1536 status = wxString::Format(_("Page %d of %d"), pageNum, m_maxPage);
1537 else
1538 status = wxString::Format(_("Page %d"), pageNum);
1539
1540 if (m_previewFrame)
1541 m_previewFrame->SetStatusText(status);
1542 #endif
1543
1544 return true;
1545 }
1546
1547 bool wxPrintPreviewBase::DrawBlankPage(wxPreviewCanvas *canvas, wxDC& dc)
1548 {
1549 wxRect pageRect, paperRect;
1550
1551 CalcRects(canvas, pageRect, paperRect);
1552
1553 // Draw shadow, allowing for 1-pixel border AROUND the actual paper
1554 wxCoord shadowOffset = 4;
1555
1556 dc.SetPen(*wxBLACK_PEN);
1557 dc.SetBrush(*wxBLACK_BRUSH);
1558 dc.DrawRectangle(paperRect.x + shadowOffset, paperRect.y + paperRect.height + 1,
1559 paperRect.width, shadowOffset);
1560
1561 dc.DrawRectangle(paperRect.x + paperRect.width, paperRect.y + shadowOffset,
1562 shadowOffset, paperRect.height);
1563
1564 // Draw blank page allowing for 1-pixel border AROUND the actual paper
1565 dc.SetPen(*wxBLACK_PEN);
1566 dc.SetBrush(*wxWHITE_BRUSH);
1567 dc.DrawRectangle(paperRect.x - 2, paperRect.y - 1,
1568 paperRect.width + 3, paperRect.height + 2);
1569
1570 return true;
1571 }
1572
1573 void wxPrintPreviewBase::SetZoom(int percent)
1574 {
1575 if (m_currentZoom == percent)
1576 return;
1577
1578 m_currentZoom = percent;
1579 if (m_previewBitmap)
1580 {
1581 delete m_previewBitmap;
1582 m_previewBitmap = NULL;
1583 }
1584
1585 if (m_previewCanvas)
1586 {
1587 AdjustScrollbars(m_previewCanvas);
1588 RenderPage(m_currentPage);
1589 ((wxScrolledWindow *) m_previewCanvas)->Scroll(0, 0);
1590 m_previewCanvas->ClearBackground();
1591 m_previewCanvas->Refresh();
1592 m_previewCanvas->SetFocus();
1593 }
1594 }
1595
1596 wxPrintDialogData& wxPrintPreviewBase::GetPrintDialogData()
1597 {
1598 return m_printDialogData;
1599 }
1600
1601 int wxPrintPreviewBase::GetZoom() const
1602 { return m_currentZoom; }
1603 int wxPrintPreviewBase::GetMaxPage() const
1604 { return m_maxPage; }
1605 int wxPrintPreviewBase::GetMinPage() const
1606 { return m_minPage; }
1607 bool wxPrintPreviewBase::IsOk() const
1608 { return m_isOk; }
1609 void wxPrintPreviewBase::SetOk(bool ok)
1610 { m_isOk = ok; }
1611
1612 //----------------------------------------------------------------------------
1613 // wxPrintPreview
1614 //----------------------------------------------------------------------------
1615
1616 IMPLEMENT_CLASS(wxPrintPreview, wxPrintPreviewBase)
1617
1618 wxPrintPreview::wxPrintPreview(wxPrintout *printout,
1619 wxPrintout *printoutForPrinting,
1620 wxPrintDialogData *data) :
1621 wxPrintPreviewBase( printout, printoutForPrinting, data )
1622 {
1623 m_pimpl = wxPrintFactory::GetFactory()->
1624 CreatePrintPreview( printout, printoutForPrinting, data );
1625 }
1626
1627 wxPrintPreview::wxPrintPreview(wxPrintout *printout,
1628 wxPrintout *printoutForPrinting,
1629 wxPrintData *data ) :
1630 wxPrintPreviewBase( printout, printoutForPrinting, data )
1631 {
1632 m_pimpl = wxPrintFactory::GetFactory()->
1633 CreatePrintPreview( printout, printoutForPrinting, data );
1634 }
1635
1636 wxPrintPreview::~wxPrintPreview()
1637 {
1638 delete m_pimpl;
1639
1640 // don't delete twice
1641 m_printPrintout = NULL;
1642 m_previewPrintout = NULL;
1643 m_previewBitmap = NULL;
1644 }
1645
1646 bool wxPrintPreview::SetCurrentPage(int pageNum)
1647 {
1648 return m_pimpl->SetCurrentPage( pageNum );
1649 }
1650
1651 int wxPrintPreview::GetCurrentPage() const
1652 {
1653 return m_pimpl->GetCurrentPage();
1654 }
1655
1656 void wxPrintPreview::SetPrintout(wxPrintout *printout)
1657 {
1658 m_pimpl->SetPrintout( printout );
1659 }
1660
1661 wxPrintout *wxPrintPreview::GetPrintout() const
1662 {
1663 return m_pimpl->GetPrintout();
1664 }
1665
1666 wxPrintout *wxPrintPreview::GetPrintoutForPrinting() const
1667 {
1668 return m_pimpl->GetPrintoutForPrinting();
1669 }
1670
1671 void wxPrintPreview::SetFrame(wxFrame *frame)
1672 {
1673 m_pimpl->SetFrame( frame );
1674 }
1675
1676 void wxPrintPreview::SetCanvas(wxPreviewCanvas *canvas)
1677 {
1678 m_pimpl->SetCanvas( canvas );
1679 }
1680
1681 wxFrame *wxPrintPreview::GetFrame() const
1682 {
1683 return m_pimpl->GetFrame();
1684 }
1685
1686 wxPreviewCanvas *wxPrintPreview::GetCanvas() const
1687 {
1688 return m_pimpl->GetCanvas();
1689 }
1690
1691 bool wxPrintPreview::PaintPage(wxPreviewCanvas *canvas, wxDC& dc)
1692 {
1693 return m_pimpl->PaintPage( canvas, dc );
1694 }
1695
1696 bool wxPrintPreview::DrawBlankPage(wxPreviewCanvas *canvas, wxDC& dc)
1697 {
1698 return m_pimpl->DrawBlankPage( canvas, dc );
1699 }
1700
1701 void wxPrintPreview::AdjustScrollbars(wxPreviewCanvas *canvas)
1702 {
1703 m_pimpl->AdjustScrollbars( canvas );
1704 }
1705
1706 bool wxPrintPreview::RenderPage(int pageNum)
1707 {
1708 return m_pimpl->RenderPage( pageNum );
1709 }
1710
1711 void wxPrintPreview::SetZoom(int percent)
1712 {
1713 m_pimpl->SetZoom( percent );
1714 }
1715
1716 int wxPrintPreview::GetZoom() const
1717 {
1718 return m_pimpl->GetZoom();
1719 }
1720
1721 wxPrintDialogData& wxPrintPreview::GetPrintDialogData()
1722 {
1723 return m_pimpl->GetPrintDialogData();
1724 }
1725
1726 int wxPrintPreview::GetMaxPage() const
1727 {
1728 return m_pimpl->GetMaxPage();
1729 }
1730
1731 int wxPrintPreview::GetMinPage() const
1732 {
1733 return m_pimpl->GetMinPage();
1734 }
1735
1736 bool wxPrintPreview::IsOk() const
1737 {
1738 return m_pimpl->Ok();
1739 }
1740
1741 void wxPrintPreview::SetOk(bool ok)
1742 {
1743 m_pimpl->SetOk( ok );
1744 }
1745
1746 bool wxPrintPreview::Print(bool interactive)
1747 {
1748 return m_pimpl->Print( interactive );
1749 }
1750
1751 void wxPrintPreview::DetermineScaling()
1752 {
1753 m_pimpl->DetermineScaling();
1754 }
1755
1756 #endif // wxUSE_PRINTING_ARCHITECTURE