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