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