Colour preview canvas grey to contrast with page
[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 #elif defined(__WXGTK__)
811 wxSystemColour colourIndex = wxSYS_COLOUR_BTNFACE;
812 #else
813 wxSystemColour colourIndex = wxSYS_COLOUR_APPWORKSPACE;
814 #endif
815 SetBackgroundColour(wxSystemSettings::GetColour(colourIndex));
816
817 SetScrollbars(10, 10, 100, 100);
818 }
819
820 wxPreviewCanvas::~wxPreviewCanvas()
821 {
822 }
823
824 void wxPreviewCanvas::OnPaint(wxPaintEvent& WXUNUSED(event))
825 {
826 wxPaintDC dc(this);
827 PrepareDC( dc );
828
829 /*
830 #ifdef __WXGTK__
831 if (!GetUpdateRegion().IsEmpty())
832 dc.SetClippingRegion( GetUpdateRegion() );
833 #endif
834 */
835
836 if (m_printPreview)
837 {
838 m_printPreview->PaintPage(this, dc);
839 }
840 }
841
842 // Responds to colour changes, and passes event on to children.
843 void wxPreviewCanvas::OnSysColourChanged(wxSysColourChangedEvent& event)
844 {
845 #ifdef __WXMAC__
846 // The app workspace colour is always white, but we should have
847 // a contrast with the page.
848 wxSystemColour colourIndex = wxSYS_COLOUR_3DDKSHADOW;
849 #elif defined(__WXGTK__)
850 wxSystemColour colourIndex = wxSYS_COLOUR_BTNFACE;
851 #else
852 wxSystemColour colourIndex = wxSYS_COLOUR_APPWORKSPACE;
853 #endif
854 SetBackgroundColour(wxSystemSettings::GetColour(colourIndex));
855 Refresh();
856
857 // Propagate the event to the non-top-level children
858 wxWindow::OnSysColourChanged(event);
859 }
860
861 void wxPreviewCanvas::OnChar(wxKeyEvent &event)
862 {
863 wxPreviewControlBar* controlBar = ((wxPreviewFrame*) GetParent())->GetControlBar();
864 if (event.GetKeyCode() == WXK_ESCAPE)
865 {
866 ((wxPreviewFrame*) GetParent())->Close(true);
867 return;
868 }
869 else if (event.GetKeyCode() == WXK_TAB)
870 {
871 controlBar->OnGoto();
872 return;
873 }
874 else if (event.GetKeyCode() == WXK_RETURN)
875 {
876 controlBar->OnPrint();
877 return;
878 }
879
880 if (!event.ControlDown())
881 {
882 event.Skip();
883 return;
884 }
885
886 switch(event.GetKeyCode())
887 {
888 case WXK_PAGEDOWN:
889 controlBar->OnNext(); break;
890 case WXK_PAGEUP:
891 controlBar->OnPrevious(); break;
892 case WXK_HOME:
893 controlBar->OnFirst(); break;
894 case WXK_END:
895 controlBar->OnLast(); break;
896 default:
897 event.Skip();
898 }
899 }
900
901 #if wxUSE_MOUSEWHEEL
902
903 void wxPreviewCanvas::OnMouseWheel(wxMouseEvent& event)
904 {
905 wxPreviewControlBar *
906 controlBar = wxStaticCast(GetParent(), wxPreviewFrame)->GetControlBar();
907
908 if ( controlBar )
909 {
910 if ( event.ControlDown() && event.GetWheelRotation() != 0 )
911 {
912 int currentZoom = controlBar->GetZoomControl();
913
914 int delta;
915 if ( currentZoom < 100 )
916 delta = 5;
917 else if ( currentZoom <= 120 )
918 delta = 10;
919 else
920 delta = 50;
921
922 if ( event.GetWheelRotation() > 0 )
923 delta = -delta;
924
925 int newZoom = currentZoom + delta;
926 if ( newZoom < 10 )
927 newZoom = 10;
928 if ( newZoom > 200 )
929 newZoom = 200;
930 if ( newZoom != currentZoom )
931 {
932 controlBar->SetZoomControl(newZoom);
933 m_printPreview->SetZoom(newZoom);
934 Refresh();
935 }
936 return;
937 }
938 }
939
940 event.Skip();
941 }
942
943 #endif // wxUSE_MOUSEWHEEL
944
945 //----------------------------------------------------------------------------
946 // wxPreviewControlBar
947 //----------------------------------------------------------------------------
948
949 IMPLEMENT_CLASS(wxPreviewControlBar, wxWindow)
950
951 BEGIN_EVENT_TABLE(wxPreviewControlBar, wxPanel)
952 EVT_BUTTON(wxID_PREVIEW_CLOSE, wxPreviewControlBar::OnWindowClose)
953 EVT_BUTTON(wxID_PREVIEW_PRINT, wxPreviewControlBar::OnPrintButton)
954 EVT_BUTTON(wxID_PREVIEW_PREVIOUS, wxPreviewControlBar::OnPreviousButton)
955 EVT_BUTTON(wxID_PREVIEW_NEXT, wxPreviewControlBar::OnNextButton)
956 EVT_BUTTON(wxID_PREVIEW_FIRST, wxPreviewControlBar::OnFirstButton)
957 EVT_BUTTON(wxID_PREVIEW_LAST, wxPreviewControlBar::OnLastButton)
958 EVT_BUTTON(wxID_PREVIEW_GOTO, wxPreviewControlBar::OnGotoButton)
959 EVT_CHOICE(wxID_PREVIEW_ZOOM, wxPreviewControlBar::OnZoom)
960 EVT_PAINT(wxPreviewControlBar::OnPaint)
961 END_EVENT_TABLE()
962
963 wxPreviewControlBar::wxPreviewControlBar(wxPrintPreviewBase *preview, long buttons,
964 wxWindow *parent, const wxPoint& pos, const wxSize& size,
965 long style, const wxString& name):
966 wxPanel(parent, wxID_ANY, pos, size, style, name)
967 {
968 m_printPreview = preview;
969 m_closeButton = (wxButton *) NULL;
970 m_nextPageButton = (wxButton *) NULL;
971 m_previousPageButton = (wxButton *) NULL;
972 m_printButton = (wxButton *) NULL;
973 m_zoomControl = (wxChoice *) NULL;
974 m_buttonFlags = buttons;
975 }
976
977 wxPreviewControlBar::~wxPreviewControlBar()
978 {
979 }
980
981 void wxPreviewControlBar::OnPaint(wxPaintEvent& WXUNUSED(event))
982 {
983 wxPaintDC dc(this);
984
985 int w, h;
986 GetSize(&w, &h);
987 dc.SetPen(*wxBLACK_PEN);
988 dc.SetBrush(*wxTRANSPARENT_BRUSH);
989 dc.DrawLine( 0, h-1, w, h-1 );
990 }
991
992 void wxPreviewControlBar::OnWindowClose(wxCommandEvent& WXUNUSED(event))
993 {
994 wxPreviewFrame *frame = (wxPreviewFrame *)GetParent();
995 frame->Close(true);
996 }
997
998 void wxPreviewControlBar::OnPrint(void)
999 {
1000 wxPrintPreviewBase *preview = GetPrintPreview();
1001 preview->Print(true);
1002 }
1003
1004 void wxPreviewControlBar::OnNext(void)
1005 {
1006 wxPrintPreviewBase *preview = GetPrintPreview();
1007 if (preview)
1008 {
1009 int currentPage = preview->GetCurrentPage();
1010 if ((preview->GetMaxPage() > 0) &&
1011 (currentPage < preview->GetMaxPage()) &&
1012 preview->GetPrintout()->HasPage(currentPage + 1))
1013 {
1014 preview->SetCurrentPage(currentPage + 1);
1015 }
1016 }
1017 }
1018
1019 void wxPreviewControlBar::OnPrevious(void)
1020 {
1021 wxPrintPreviewBase *preview = GetPrintPreview();
1022 if (preview)
1023 {
1024 int currentPage = preview->GetCurrentPage();
1025 if ((preview->GetMinPage() > 0) &&
1026 (currentPage > preview->GetMinPage()) &&
1027 preview->GetPrintout()->HasPage(currentPage - 1))
1028 {
1029 preview->SetCurrentPage(currentPage - 1);
1030 }
1031 }
1032 }
1033
1034 void wxPreviewControlBar::OnFirst(void)
1035 {
1036 wxPrintPreviewBase *preview = GetPrintPreview();
1037 if (preview)
1038 {
1039 int currentPage = preview->GetMinPage();
1040 if (preview->GetPrintout()->HasPage(currentPage))
1041 {
1042 preview->SetCurrentPage(currentPage);
1043 }
1044 }
1045 }
1046
1047 void wxPreviewControlBar::OnLast(void)
1048 {
1049 wxPrintPreviewBase *preview = GetPrintPreview();
1050 if (preview)
1051 {
1052 int currentPage = preview->GetMaxPage();
1053 if (preview->GetPrintout()->HasPage(currentPage))
1054 {
1055 preview->SetCurrentPage(currentPage);
1056 }
1057 }
1058 }
1059
1060 void wxPreviewControlBar::OnGoto(void)
1061 {
1062 wxPrintPreviewBase *preview = GetPrintPreview();
1063 if (preview)
1064 {
1065 long currentPage;
1066
1067 if (preview->GetMinPage() > 0)
1068 {
1069 wxString strPrompt;
1070 wxString strPage;
1071
1072 strPrompt.Printf( _("Enter a page number between %d and %d:"),
1073 preview->GetMinPage(), preview->GetMaxPage());
1074 strPage.Printf( wxT("%d"), preview->GetCurrentPage() );
1075
1076 strPage =
1077 wxGetTextFromUser( strPrompt, _("Goto Page"), strPage, GetParent());
1078
1079 if ( strPage.ToLong( &currentPage ) )
1080 if (preview->GetPrintout()->HasPage(currentPage))
1081 {
1082 preview->SetCurrentPage(currentPage);
1083 }
1084 }
1085 }
1086 }
1087
1088 void wxPreviewControlBar::OnZoom(wxCommandEvent& WXUNUSED(event))
1089 {
1090 int zoom = GetZoomControl();
1091 if (GetPrintPreview())
1092 GetPrintPreview()->SetZoom(zoom);
1093 }
1094
1095 void wxPreviewControlBar::CreateButtons()
1096 {
1097 SetSize(0, 0, 400, 40);
1098
1099 wxBoxSizer *item0 = new wxBoxSizer( wxHORIZONTAL );
1100
1101 m_closeButton = new wxButton( this, wxID_PREVIEW_CLOSE, _("&Close"), wxDefaultPosition, wxDefaultSize, 0 );
1102 item0->Add( m_closeButton, 0, wxALIGN_CENTRE|wxALL, 5 );
1103
1104 if (m_buttonFlags & wxPREVIEW_PRINT)
1105 {
1106 m_printButton = new wxButton( this, wxID_PREVIEW_PRINT, _("&Print..."), wxDefaultPosition, wxDefaultSize, 0 );
1107 item0->Add( m_printButton, 0, wxALIGN_CENTRE|wxALL, 5 );
1108 }
1109
1110 // Exact-fit buttons are too tiny on wxUniversal
1111 int navButtonStyle;
1112 wxSize navButtonSize;
1113 #ifdef __WXUNIVERSAL__
1114 navButtonStyle = 0;
1115 navButtonSize = wxSize(40, m_closeButton->GetSize().y);
1116 #else
1117 navButtonStyle = wxBU_EXACTFIT;
1118 navButtonSize = wxDefaultSize;
1119 #endif
1120
1121 if (m_buttonFlags & wxPREVIEW_FIRST)
1122 {
1123 m_firstPageButton = new wxButton( this, wxID_PREVIEW_FIRST, _("|<<"), wxDefaultPosition, navButtonSize, navButtonStyle );
1124 item0->Add( m_firstPageButton, 0, wxALIGN_CENTRE|wxALL, 5 );
1125 }
1126
1127 if (m_buttonFlags & wxPREVIEW_PREVIOUS)
1128 {
1129 m_previousPageButton = new wxButton( this, wxID_PREVIEW_PREVIOUS, _("<<"), wxDefaultPosition, navButtonSize, navButtonStyle );
1130 item0->Add( m_previousPageButton, 0, wxALIGN_CENTRE|wxRIGHT|wxTOP|wxBOTTOM, 5 );
1131 }
1132
1133 if (m_buttonFlags & wxPREVIEW_NEXT)
1134 {
1135 m_nextPageButton = new wxButton( this, wxID_PREVIEW_NEXT, _(">>"), wxDefaultPosition, navButtonSize, navButtonStyle );
1136 item0->Add( m_nextPageButton, 0, wxALIGN_CENTRE|wxRIGHT|wxTOP|wxBOTTOM, 5 );
1137 }
1138
1139 if (m_buttonFlags & wxPREVIEW_LAST)
1140 {
1141 m_lastPageButton = new wxButton( this, wxID_PREVIEW_LAST, _(">>|"), wxDefaultPosition, navButtonSize, navButtonStyle );
1142 item0->Add( m_lastPageButton, 0, wxALIGN_CENTRE|wxRIGHT|wxTOP|wxBOTTOM, 5 );
1143 }
1144
1145 if (m_buttonFlags & wxPREVIEW_GOTO)
1146 {
1147 m_gotoPageButton = new wxButton( this, wxID_PREVIEW_GOTO, _("&Goto..."), wxDefaultPosition, wxDefaultSize, 0 );
1148 item0->Add( m_gotoPageButton, 0, wxALIGN_CENTRE|wxALL, 5 );
1149 }
1150
1151 if (m_buttonFlags & wxPREVIEW_ZOOM)
1152 {
1153 wxString choices[] =
1154 {
1155 wxT("10%"), wxT("15%"), wxT("20%"), wxT("25%"), wxT("30%"), wxT("35%"), wxT("40%"), wxT("45%"), wxT("50%"), wxT("55%"),
1156 wxT("60%"), wxT("65%"), wxT("70%"), wxT("75%"), wxT("80%"), wxT("85%"), wxT("90%"), wxT("95%"), wxT("100%"), wxT("110%"),
1157 wxT("120%"), wxT("150%"), wxT("200%")
1158 };
1159 int n = WXSIZEOF(choices);
1160
1161 m_zoomControl = new wxChoice( this, wxID_PREVIEW_ZOOM, wxDefaultPosition, wxSize(70,wxDefaultCoord), n, choices, 0 );
1162 item0->Add( m_zoomControl, 0, wxALIGN_CENTRE|wxALL, 5 );
1163 SetZoomControl(m_printPreview->GetZoom());
1164 }
1165
1166 SetSizer(item0);
1167 item0->Fit(this);
1168 }
1169
1170 void wxPreviewControlBar::SetZoomControl(int zoom)
1171 {
1172 if (m_zoomControl)
1173 {
1174 int n, count = m_zoomControl->GetCount();
1175 long val;
1176 for (n=0; n<count; n++)
1177 {
1178 if (m_zoomControl->GetString(n).BeforeFirst(wxT('%')).ToLong(&val) &&
1179 (val >= long(zoom)))
1180 {
1181 m_zoomControl->SetSelection(n);
1182 return;
1183 }
1184 }
1185
1186 m_zoomControl->SetSelection(count-1);
1187 }
1188 }
1189
1190 int wxPreviewControlBar::GetZoomControl()
1191 {
1192 if (m_zoomControl && (m_zoomControl->GetStringSelection() != wxEmptyString))
1193 {
1194 long val;
1195 if (m_zoomControl->GetStringSelection().BeforeFirst(wxT('%')).ToLong(&val))
1196 return int(val);
1197 }
1198
1199 return 0;
1200 }
1201
1202
1203 /*
1204 * Preview frame
1205 */
1206
1207 IMPLEMENT_CLASS(wxPreviewFrame, wxFrame)
1208
1209 BEGIN_EVENT_TABLE(wxPreviewFrame, wxFrame)
1210 EVT_CLOSE(wxPreviewFrame::OnCloseWindow)
1211 END_EVENT_TABLE()
1212
1213 wxPreviewFrame::wxPreviewFrame(wxPrintPreviewBase *preview, wxWindow *parent, const wxString& title,
1214 const wxPoint& pos, const wxSize& size, long style, const wxString& name):
1215 wxFrame(parent, wxID_ANY, title, pos, size, style, name)
1216 {
1217 m_printPreview = preview;
1218 m_controlBar = NULL;
1219 m_previewCanvas = NULL;
1220 m_windowDisabler = NULL;
1221
1222 // Give the application icon
1223 #ifdef __WXMSW__
1224 wxFrame* topFrame = wxDynamicCast(wxTheApp->GetTopWindow(), wxFrame);
1225 if (topFrame)
1226 SetIcon(topFrame->GetIcon());
1227 #endif
1228 }
1229
1230 wxPreviewFrame::~wxPreviewFrame()
1231 {
1232 }
1233
1234 void wxPreviewFrame::OnCloseWindow(wxCloseEvent& WXUNUSED(event))
1235 {
1236 if (m_windowDisabler)
1237 delete m_windowDisabler;
1238
1239 // Need to delete the printout and the print preview
1240 wxPrintout *printout = m_printPreview->GetPrintout();
1241 if (printout)
1242 {
1243 delete printout;
1244 m_printPreview->SetPrintout(NULL);
1245 m_printPreview->SetCanvas(NULL);
1246 m_printPreview->SetFrame(NULL);
1247 }
1248 delete m_printPreview;
1249
1250 Destroy();
1251 }
1252
1253 void wxPreviewFrame::Initialize()
1254 {
1255 #if wxUSE_STATUSBAR
1256 CreateStatusBar();
1257 #endif
1258 CreateCanvas();
1259 CreateControlBar();
1260
1261 m_printPreview->SetCanvas(m_previewCanvas);
1262 m_printPreview->SetFrame(this);
1263
1264 wxBoxSizer *item0 = new wxBoxSizer( wxVERTICAL );
1265
1266 item0->Add( m_controlBar, 0, wxGROW|wxALIGN_CENTER_VERTICAL, 5 );
1267 item0->Add( m_previewCanvas, 1, wxGROW|wxALIGN_CENTER_VERTICAL, 5 );
1268
1269 SetAutoLayout( true );
1270 SetSizer( item0 );
1271
1272 m_windowDisabler = new wxWindowDisabler(this);
1273
1274 Layout();
1275
1276 m_printPreview->AdjustScrollbars(m_previewCanvas);
1277 m_previewCanvas->SetFocus();
1278 m_controlBar->SetFocus();
1279 }
1280
1281 void wxPreviewFrame::CreateCanvas()
1282 {
1283 m_previewCanvas = new wxPreviewCanvas(m_printPreview, this);
1284 }
1285
1286 void wxPreviewFrame::CreateControlBar()
1287 {
1288 long buttons = wxPREVIEW_DEFAULT;
1289 if (m_printPreview->GetPrintoutForPrinting())
1290 buttons |= wxPREVIEW_PRINT;
1291
1292 m_controlBar = new wxPreviewControlBar(m_printPreview, buttons, this, wxPoint(0,0), wxSize(400, 40));
1293 m_controlBar->CreateButtons();
1294 }
1295
1296 /*
1297 * Print preview
1298 */
1299
1300 IMPLEMENT_CLASS(wxPrintPreviewBase, wxObject)
1301
1302 wxPrintPreviewBase::wxPrintPreviewBase(wxPrintout *printout,
1303 wxPrintout *printoutForPrinting,
1304 wxPrintData *data)
1305 {
1306 if (data)
1307 m_printDialogData = (*data);
1308
1309 Init(printout, printoutForPrinting);
1310 }
1311
1312 wxPrintPreviewBase::wxPrintPreviewBase(wxPrintout *printout,
1313 wxPrintout *printoutForPrinting,
1314 wxPrintDialogData *data)
1315 {
1316 if (data)
1317 m_printDialogData = (*data);
1318
1319 Init(printout, printoutForPrinting);
1320 }
1321
1322 void wxPrintPreviewBase::Init(wxPrintout *printout,
1323 wxPrintout *printoutForPrinting)
1324 {
1325 m_isOk = true;
1326 m_previewPrintout = printout;
1327 if (m_previewPrintout)
1328 m_previewPrintout->SetIsPreview(true);
1329
1330 m_printPrintout = printoutForPrinting;
1331
1332 m_previewCanvas = NULL;
1333 m_previewFrame = NULL;
1334 m_previewBitmap = NULL;
1335 m_currentPage = 1;
1336 m_currentZoom = 70;
1337 m_topMargin = 40;
1338 m_leftMargin = 40;
1339 m_pageWidth = 0;
1340 m_pageHeight = 0;
1341 m_printingPrepared = false;
1342 m_minPage = 1;
1343 m_maxPage = 1;
1344 }
1345
1346 wxPrintPreviewBase::~wxPrintPreviewBase()
1347 {
1348 if (m_previewPrintout)
1349 delete m_previewPrintout;
1350 if (m_previewBitmap)
1351 delete m_previewBitmap;
1352 if (m_printPrintout)
1353 delete m_printPrintout;
1354 }
1355
1356 bool wxPrintPreviewBase::SetCurrentPage(int pageNum)
1357 {
1358 if (m_currentPage == pageNum)
1359 return true;
1360
1361 m_currentPage = pageNum;
1362 if (m_previewBitmap)
1363 {
1364 delete m_previewBitmap;
1365 m_previewBitmap = NULL;
1366 }
1367
1368 if (m_previewCanvas)
1369 {
1370 AdjustScrollbars(m_previewCanvas);
1371
1372 if (!RenderPage(pageNum))
1373 return false;
1374 m_previewCanvas->Refresh();
1375 m_previewCanvas->SetFocus();
1376 }
1377 return true;
1378 }
1379
1380 int wxPrintPreviewBase::GetCurrentPage() const
1381 { return m_currentPage; }
1382 void wxPrintPreviewBase::SetPrintout(wxPrintout *printout)
1383 { m_previewPrintout = printout; }
1384 wxPrintout *wxPrintPreviewBase::GetPrintout() const
1385 { return m_previewPrintout; }
1386 wxPrintout *wxPrintPreviewBase::GetPrintoutForPrinting() const
1387 { return m_printPrintout; }
1388 void wxPrintPreviewBase::SetFrame(wxFrame *frame)
1389 { m_previewFrame = frame; }
1390 void wxPrintPreviewBase::SetCanvas(wxPreviewCanvas *canvas)
1391 { m_previewCanvas = canvas; }
1392 wxFrame *wxPrintPreviewBase::GetFrame() const
1393 { return m_previewFrame; }
1394 wxPreviewCanvas *wxPrintPreviewBase::GetCanvas() const
1395 { return m_previewCanvas; }
1396
1397 void wxPrintPreviewBase::CalcRects(wxPreviewCanvas *canvas, wxRect& pageRect, wxRect& paperRect)
1398 {
1399 // Calculate the rectangles for the printable area of the page and the
1400 // entire paper as they appear on the canvas on-screen.
1401 int canvasWidth, canvasHeight;
1402 canvas->GetSize(&canvasWidth, &canvasHeight);
1403
1404 float zoomScale = float(m_currentZoom) / 100;
1405 float screenPrintableWidth = zoomScale * m_pageWidth * m_previewScaleX;
1406 float screenPrintableHeight = zoomScale * m_pageHeight * m_previewScaleY;
1407
1408 wxRect devicePaperRect = m_previewPrintout->GetPaperRectPixels();
1409 wxCoord devicePrintableWidth, devicePrintableHeight;
1410 m_previewPrintout->GetPageSizePixels(&devicePrintableWidth, &devicePrintableHeight);
1411 float scaleX = screenPrintableWidth / devicePrintableWidth;
1412 float scaleY = screenPrintableHeight / devicePrintableHeight;
1413 paperRect.width = wxCoord(scaleX * devicePaperRect.width);
1414 paperRect.height = wxCoord(scaleY * devicePaperRect.height);
1415
1416 paperRect.x = wxCoord((canvasWidth - paperRect.width)/ 2.0);
1417 if (paperRect.x < m_leftMargin)
1418 paperRect.x = m_leftMargin;
1419 paperRect.y = wxCoord((canvasHeight - paperRect.height)/ 2.0);
1420 if (paperRect.y < m_topMargin)
1421 paperRect.y = m_topMargin;
1422
1423 pageRect.x = paperRect.x - wxCoord(scaleX * devicePaperRect.x);
1424 pageRect.y = paperRect.y - wxCoord(scaleY * devicePaperRect.y);
1425 pageRect.width = wxCoord(screenPrintableWidth);
1426 pageRect.height = wxCoord(screenPrintableHeight);
1427 }
1428
1429
1430 bool wxPrintPreviewBase::PaintPage(wxPreviewCanvas *canvas, wxDC& dc)
1431 {
1432 DrawBlankPage(canvas, dc);
1433
1434 if (!m_previewBitmap)
1435 if (!RenderPage(m_currentPage))
1436 return false;
1437 if (!m_previewBitmap)
1438 return false;
1439 if (!canvas)
1440 return false;
1441
1442 wxRect pageRect, paperRect;
1443 CalcRects(canvas, pageRect, paperRect);
1444 wxMemoryDC temp_dc;
1445 temp_dc.SelectObject(*m_previewBitmap);
1446
1447 dc.Blit(pageRect.x, pageRect.y,
1448 m_previewBitmap->GetWidth(), m_previewBitmap->GetHeight(), &temp_dc, 0, 0);
1449
1450 temp_dc.SelectObject(wxNullBitmap);
1451 return true;
1452 }
1453
1454 // Adjusts the scrollbars for the current scale
1455 void wxPrintPreviewBase::AdjustScrollbars(wxPreviewCanvas *canvas)
1456 {
1457 if (!canvas)
1458 return ;
1459
1460 wxRect pageRect, paperRect;
1461 CalcRects(canvas, pageRect, paperRect);
1462 int totalWidth = paperRect.width + 2 * m_leftMargin;
1463 int totalHeight = paperRect.height + 2 * m_topMargin;
1464 int scrollUnitsX = totalWidth / 10;
1465 int scrollUnitsY = totalHeight / 10;
1466 wxSize virtualSize = canvas->GetVirtualSize();
1467 if (virtualSize.GetWidth() != totalWidth || virtualSize.GetHeight() != totalHeight)
1468 canvas->SetScrollbars(10, 10, scrollUnitsX, scrollUnitsY, 0, 0, true);
1469 }
1470
1471 bool wxPrintPreviewBase::RenderPage(int pageNum)
1472 {
1473 wxBusyCursor busy;
1474
1475 if (!m_previewCanvas)
1476 {
1477 wxFAIL_MSG(_T("wxPrintPreviewBase::RenderPage: must use wxPrintPreviewBase::SetCanvas to let me know about the canvas!"));
1478 return false;
1479 }
1480
1481 wxRect pageRect, paperRect;
1482 CalcRects(m_previewCanvas, pageRect, paperRect);
1483
1484 if (!m_previewBitmap)
1485 {
1486 m_previewBitmap = new wxBitmap(pageRect.width, pageRect.height);
1487
1488 if (!m_previewBitmap || !m_previewBitmap->Ok())
1489 {
1490 if (m_previewBitmap) {
1491 delete m_previewBitmap;
1492 m_previewBitmap = NULL;
1493 }
1494 wxMessageBox(_("Sorry, not enough memory to create a preview."), _("Print Preview Failure"), wxOK);
1495 return false;
1496 }
1497 }
1498
1499 wxMemoryDC memoryDC;
1500 memoryDC.SelectObject(*m_previewBitmap);
1501
1502 memoryDC.Clear();
1503
1504 m_previewPrintout->SetDC(&memoryDC);
1505 m_previewPrintout->SetPageSizePixels(m_pageWidth, m_pageHeight);
1506
1507 // Need to delay OnPreparePrinting until here, so we have enough information.
1508 if (!m_printingPrepared)
1509 {
1510 m_previewPrintout->OnPreparePrinting();
1511 int selFrom, selTo;
1512 m_previewPrintout->GetPageInfo(&m_minPage, &m_maxPage, &selFrom, &selTo);
1513 m_printingPrepared = true;
1514 }
1515
1516 m_previewPrintout->OnBeginPrinting();
1517
1518 if (!m_previewPrintout->OnBeginDocument(m_printDialogData.GetFromPage(), m_printDialogData.GetToPage()))
1519 {
1520 wxMessageBox(_("Could not start document preview."), _("Print Preview Failure"), wxOK);
1521
1522 memoryDC.SelectObject(wxNullBitmap);
1523
1524 delete m_previewBitmap;
1525 m_previewBitmap = NULL;
1526 return false;
1527 }
1528
1529 m_previewPrintout->OnPrintPage(pageNum);
1530 m_previewPrintout->OnEndDocument();
1531 m_previewPrintout->OnEndPrinting();
1532
1533 m_previewPrintout->SetDC(NULL);
1534
1535 memoryDC.SelectObject(wxNullBitmap);
1536
1537 #if wxUSE_STATUSBAR
1538 wxString status;
1539 if (m_maxPage != 0)
1540 status = wxString::Format(_("Page %d of %d"), pageNum, m_maxPage);
1541 else
1542 status = wxString::Format(_("Page %d"), pageNum);
1543
1544 if (m_previewFrame)
1545 m_previewFrame->SetStatusText(status);
1546 #endif
1547
1548 return true;
1549 }
1550
1551 bool wxPrintPreviewBase::DrawBlankPage(wxPreviewCanvas *canvas, wxDC& dc)
1552 {
1553 wxRect pageRect, paperRect;
1554
1555 CalcRects(canvas, pageRect, paperRect);
1556
1557 // Draw shadow, allowing for 1-pixel border AROUND the actual paper
1558 wxCoord shadowOffset = 4;
1559
1560 dc.SetPen(*wxBLACK_PEN);
1561 dc.SetBrush(*wxBLACK_BRUSH);
1562 dc.DrawRectangle(paperRect.x + shadowOffset, paperRect.y + paperRect.height + 1,
1563 paperRect.width, shadowOffset);
1564
1565 dc.DrawRectangle(paperRect.x + paperRect.width, paperRect.y + shadowOffset,
1566 shadowOffset, paperRect.height);
1567
1568 // Draw blank page allowing for 1-pixel border AROUND the actual paper
1569 dc.SetPen(*wxBLACK_PEN);
1570 dc.SetBrush(*wxWHITE_BRUSH);
1571 dc.DrawRectangle(paperRect.x - 2, paperRect.y - 1,
1572 paperRect.width + 3, paperRect.height + 2);
1573
1574 return true;
1575 }
1576
1577 void wxPrintPreviewBase::SetZoom(int percent)
1578 {
1579 if (m_currentZoom == percent)
1580 return;
1581
1582 m_currentZoom = percent;
1583 if (m_previewBitmap)
1584 {
1585 delete m_previewBitmap;
1586 m_previewBitmap = NULL;
1587 }
1588
1589 if (m_previewCanvas)
1590 {
1591 AdjustScrollbars(m_previewCanvas);
1592 RenderPage(m_currentPage);
1593 ((wxScrolledWindow *) m_previewCanvas)->Scroll(0, 0);
1594 m_previewCanvas->ClearBackground();
1595 m_previewCanvas->Refresh();
1596 m_previewCanvas->SetFocus();
1597 }
1598 }
1599
1600 wxPrintDialogData& wxPrintPreviewBase::GetPrintDialogData()
1601 {
1602 return m_printDialogData;
1603 }
1604
1605 int wxPrintPreviewBase::GetZoom() const
1606 { return m_currentZoom; }
1607 int wxPrintPreviewBase::GetMaxPage() const
1608 { return m_maxPage; }
1609 int wxPrintPreviewBase::GetMinPage() const
1610 { return m_minPage; }
1611 bool wxPrintPreviewBase::IsOk() const
1612 { return m_isOk; }
1613 void wxPrintPreviewBase::SetOk(bool ok)
1614 { m_isOk = ok; }
1615
1616 //----------------------------------------------------------------------------
1617 // wxPrintPreview
1618 //----------------------------------------------------------------------------
1619
1620 IMPLEMENT_CLASS(wxPrintPreview, wxPrintPreviewBase)
1621
1622 wxPrintPreview::wxPrintPreview(wxPrintout *printout,
1623 wxPrintout *printoutForPrinting,
1624 wxPrintDialogData *data) :
1625 wxPrintPreviewBase( printout, printoutForPrinting, data )
1626 {
1627 m_pimpl = wxPrintFactory::GetFactory()->
1628 CreatePrintPreview( printout, printoutForPrinting, data );
1629 }
1630
1631 wxPrintPreview::wxPrintPreview(wxPrintout *printout,
1632 wxPrintout *printoutForPrinting,
1633 wxPrintData *data ) :
1634 wxPrintPreviewBase( printout, printoutForPrinting, data )
1635 {
1636 m_pimpl = wxPrintFactory::GetFactory()->
1637 CreatePrintPreview( printout, printoutForPrinting, data );
1638 }
1639
1640 wxPrintPreview::~wxPrintPreview()
1641 {
1642 delete m_pimpl;
1643
1644 // don't delete twice
1645 m_printPrintout = NULL;
1646 m_previewPrintout = NULL;
1647 m_previewBitmap = NULL;
1648 }
1649
1650 bool wxPrintPreview::SetCurrentPage(int pageNum)
1651 {
1652 return m_pimpl->SetCurrentPage( pageNum );
1653 }
1654
1655 int wxPrintPreview::GetCurrentPage() const
1656 {
1657 return m_pimpl->GetCurrentPage();
1658 }
1659
1660 void wxPrintPreview::SetPrintout(wxPrintout *printout)
1661 {
1662 m_pimpl->SetPrintout( printout );
1663 }
1664
1665 wxPrintout *wxPrintPreview::GetPrintout() const
1666 {
1667 return m_pimpl->GetPrintout();
1668 }
1669
1670 wxPrintout *wxPrintPreview::GetPrintoutForPrinting() const
1671 {
1672 return m_pimpl->GetPrintoutForPrinting();
1673 }
1674
1675 void wxPrintPreview::SetFrame(wxFrame *frame)
1676 {
1677 m_pimpl->SetFrame( frame );
1678 }
1679
1680 void wxPrintPreview::SetCanvas(wxPreviewCanvas *canvas)
1681 {
1682 m_pimpl->SetCanvas( canvas );
1683 }
1684
1685 wxFrame *wxPrintPreview::GetFrame() const
1686 {
1687 return m_pimpl->GetFrame();
1688 }
1689
1690 wxPreviewCanvas *wxPrintPreview::GetCanvas() const
1691 {
1692 return m_pimpl->GetCanvas();
1693 }
1694
1695 bool wxPrintPreview::PaintPage(wxPreviewCanvas *canvas, wxDC& dc)
1696 {
1697 return m_pimpl->PaintPage( canvas, dc );
1698 }
1699
1700 bool wxPrintPreview::DrawBlankPage(wxPreviewCanvas *canvas, wxDC& dc)
1701 {
1702 return m_pimpl->DrawBlankPage( canvas, dc );
1703 }
1704
1705 void wxPrintPreview::AdjustScrollbars(wxPreviewCanvas *canvas)
1706 {
1707 m_pimpl->AdjustScrollbars( canvas );
1708 }
1709
1710 bool wxPrintPreview::RenderPage(int pageNum)
1711 {
1712 return m_pimpl->RenderPage( pageNum );
1713 }
1714
1715 void wxPrintPreview::SetZoom(int percent)
1716 {
1717 m_pimpl->SetZoom( percent );
1718 }
1719
1720 int wxPrintPreview::GetZoom() const
1721 {
1722 return m_pimpl->GetZoom();
1723 }
1724
1725 wxPrintDialogData& wxPrintPreview::GetPrintDialogData()
1726 {
1727 return m_pimpl->GetPrintDialogData();
1728 }
1729
1730 int wxPrintPreview::GetMaxPage() const
1731 {
1732 return m_pimpl->GetMaxPage();
1733 }
1734
1735 int wxPrintPreview::GetMinPage() const
1736 {
1737 return m_pimpl->GetMinPage();
1738 }
1739
1740 bool wxPrintPreview::IsOk() const
1741 {
1742 return m_pimpl->Ok();
1743 }
1744
1745 void wxPrintPreview::SetOk(bool ok)
1746 {
1747 m_pimpl->SetOk( ok );
1748 }
1749
1750 bool wxPrintPreview::Print(bool interactive)
1751 {
1752 return m_pimpl->Print( interactive );
1753 }
1754
1755 void wxPrintPreview::DetermineScaling()
1756 {
1757 m_pimpl->DetermineScaling();
1758 }
1759
1760 #endif // wxUSE_PRINTING_ARCHITECTURE