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