]> git.saurik.com Git - wxWidgets.git/blame - src/common/prntbase.cpp
Fix for #15520: wxRichTextCtrl: Drawing the selection doesn't respect its container...
[wxWidgets.git] / src / common / prntbase.cpp
CommitLineData
c801d85f 1/////////////////////////////////////////////////////////////////////////////
cb7d7375 2// Name: src/common/prntbase.cpp
c801d85f
KB
3// Purpose: Printing framework base class implementation
4// Author: Julian Smart
5// Modified by:
6// Created: 04/01/98
55d99c7a 7// Copyright: (c) Julian Smart
65571936 8// Licence: wxWindows licence
c801d85f
KB
9/////////////////////////////////////////////////////////////////////////////
10
c801d85f
KB
11// For compilers that support precompilation, includes "wx.h".
12#include "wx/wxprec.h"
13
14#ifdef __BORLANDC__
8898456d 15 #pragma hdrstop
c801d85f
KB
16#endif
17
d427503c
VZ
18#if wxUSE_PRINTING_ARCHITECTURE
19
6d50343d
WS
20#include "wx/dcprint.h"
21
c801d85f 22#ifndef WX_PRECOMP
57bd4c60
WS
23 #if defined(__WXMSW__)
24 #include "wx/msw/wrapcdlg.h"
25 #endif // MSW
8898456d
WS
26 #include "wx/utils.h"
27 #include "wx/dc.h"
28 #include "wx/app.h"
5a70d3f5 29 #include "wx/math.h"
8898456d
WS
30 #include "wx/msgdlg.h"
31 #include "wx/layout.h"
32 #include "wx/choice.h"
33 #include "wx/button.h"
424da8bd 34 #include "wx/bmpbuttn.h"
8898456d
WS
35 #include "wx/settings.h"
36 #include "wx/dcmemory.h"
23205be8 37 #include "wx/dcclient.h"
8898456d
WS
38 #include "wx/stattext.h"
39 #include "wx/intl.h"
40 #include "wx/textdlg.h"
41 #include "wx/sizer.h"
02761f6c 42 #include "wx/module.h"
2b5f62a0 43#endif // !WX_PRECOMP
c801d85f
KB
44
45#include "wx/prntbase.h"
c801d85f 46#include "wx/printdlg.h"
e81e3883 47#include "wx/print.h"
147bf263 48#include "wx/dcprint.h"
82579461 49#include "wx/artprov.h"
c801d85f
KB
50
51#include <stdlib.h>
52#include <string.h>
53
891daf8c
RR
54#if defined(__WXMSW__) && !defined(__WXUNIVERSAL__)
55#include "wx/msw/printdlg.h"
888dde65 56#include "wx/msw/dcprint.h"
891daf8c 57#elif defined(__WXMAC__)
c933e267
SC
58#include "wx/osx/printdlg.h"
59#include "wx/osx/private/print.h"
60#include "wx/osx/dcprint.h"
7c71eb6a
SN
61#elif defined(__WXPM__)
62#include "wx/os2/dcprint.h"
63#include "wx/generic/prntdlgg.h"
891daf8c
RR
64#else
65#include "wx/generic/prntdlgg.h"
147bf263 66#include "wx/dcps.h"
891daf8c
RR
67#endif
68
2049ba38 69#ifdef __WXMSW__
d427503c
VZ
70 #ifndef __WIN32__
71 #include <print.h>
72 #endif
73#endif // __WXMSW__
c801d85f 74
e81e3883
RR
75//----------------------------------------------------------------------------
76// wxPrintFactory
77//----------------------------------------------------------------------------
78
79wxPrintFactory *wxPrintFactory::m_factory = NULL;
383f6abd 80
e81e3883 81void wxPrintFactory::SetPrintFactory( wxPrintFactory *factory )
383f6abd
WS
82{
83 if (wxPrintFactory::m_factory)
e81e3883 84 delete wxPrintFactory::m_factory;
383f6abd
WS
85
86 wxPrintFactory::m_factory = factory;
e81e3883 87}
c801d85f 88
e81e3883
RR
89wxPrintFactory *wxPrintFactory::GetFactory()
90{
91 if (!wxPrintFactory::m_factory)
383f6abd 92 wxPrintFactory::m_factory = new wxNativePrintFactory;
c801d85f 93
e81e3883
RR
94 return wxPrintFactory::m_factory;
95}
96
97//----------------------------------------------------------------------------
98// wxNativePrintFactory
99//----------------------------------------------------------------------------
100
101wxPrinterBase *wxNativePrintFactory::CreatePrinter( wxPrintDialogData *data )
383f6abd
WS
102{
103#if defined(__WXMSW__) && !defined(__WXUNIVERSAL__)
e81e3883
RR
104 return new wxWindowsPrinter( data );
105#elif defined(__WXMAC__)
106 return new wxMacPrinter( data );
21708c73
SN
107#elif defined(__WXPM__)
108 return new wxOS2Printer( data );
e81e3883
RR
109#else
110 return new wxPostScriptPrinter( data );
111#endif
259c43f6 112}
e81e3883 113
383f6abd 114wxPrintPreviewBase *wxNativePrintFactory::CreatePrintPreview( wxPrintout *preview,
e81e3883
RR
115 wxPrintout *printout, wxPrintDialogData *data )
116{
383f6abd 117#if defined(__WXMSW__) && !defined(__WXUNIVERSAL__)
e81e3883
RR
118 return new wxWindowsPrintPreview( preview, printout, data );
119#elif defined(__WXMAC__)
120 return new wxMacPrintPreview( preview, printout, data );
21708c73
SN
121#elif defined(__WXPM__)
122 return new wxOS2PrintPreview( preview, printout, data );
e81e3883
RR
123#else
124 return new wxPostScriptPrintPreview( preview, printout, data );
125#endif
126}
127
383f6abd 128wxPrintPreviewBase *wxNativePrintFactory::CreatePrintPreview( wxPrintout *preview,
e81e3883
RR
129 wxPrintout *printout, wxPrintData *data )
130{
383f6abd 131#if defined(__WXMSW__) && !defined(__WXUNIVERSAL__)
e81e3883
RR
132 return new wxWindowsPrintPreview( preview, printout, data );
133#elif defined(__WXMAC__)
134 return new wxMacPrintPreview( preview, printout, data );
21708c73
SN
135#elif defined(__WXPM__)
136 return new wxOS2PrintPreview( preview, printout, data );
e81e3883
RR
137#else
138 return new wxPostScriptPrintPreview( preview, printout, data );
139#endif
140}
141
2997ca30 142wxPrintDialogBase *wxNativePrintFactory::CreatePrintDialog( wxWindow *parent,
c061373d
RR
143 wxPrintDialogData *data )
144{
145#if defined(__WXMSW__) && !defined(__WXUNIVERSAL__)
146 return new wxWindowsPrintDialog( parent, data );
147#elif defined(__WXMAC__)
148 return new wxMacPrintDialog( parent, data );
149#else
150 return new wxGenericPrintDialog( parent, data );
151#endif
152}
153
2997ca30 154wxPrintDialogBase *wxNativePrintFactory::CreatePrintDialog( wxWindow *parent,
c061373d
RR
155 wxPrintData *data )
156{
157#if defined(__WXMSW__) && !defined(__WXUNIVERSAL__)
158 return new wxWindowsPrintDialog( parent, data );
159#elif defined(__WXMAC__)
160 return new wxMacPrintDialog( parent, data );
161#else
162 return new wxGenericPrintDialog( parent, data );
163#endif
164}
165
2997ca30 166wxPageSetupDialogBase *wxNativePrintFactory::CreatePageSetupDialog( wxWindow *parent,
891daf8c
RR
167 wxPageSetupDialogData *data )
168{
169#if defined(__WXMSW__) && !defined(__WXUNIVERSAL__)
170 return new wxWindowsPageSetupDialog( parent, data );
171#elif defined(__WXMAC__)
172 return new wxMacPageSetupDialog( parent, data );
173#else
174 return new wxGenericPageSetupDialog( parent, data );
175#endif
176}
177
6038ec8e
RR
178bool wxNativePrintFactory::HasPrintSetupDialog()
179{
180#if defined(__WXMSW__) && !defined(__WXUNIVERSAL__)
181 return false;
182#elif defined(__WXMAC__)
183 return false;
184#else
185 // Only here do we need to provide the print setup
186 // dialog ourselves, the other platforms either have
187 // none, don't make it accessible or let you configure
188 // the printer from the wxPrintDialog anyway.
189 return true;
190#endif
2997ca30 191
6038ec8e
RR
192}
193
2997ca30 194wxDialog *wxNativePrintFactory::CreatePrintSetupDialog( wxWindow *parent,
891daf8c 195 wxPrintData *data )
6038ec8e
RR
196{
197#if defined(__WXMSW__) && !defined(__WXUNIVERSAL__)
ce403057
WS
198 wxUnusedVar(parent);
199 wxUnusedVar(data);
6038ec8e
RR
200 return NULL;
201#elif defined(__WXMAC__)
ce403057
WS
202 wxUnusedVar(parent);
203 wxUnusedVar(data);
6038ec8e
RR
204 return NULL;
205#else
206 // Only here do we need to provide the print setup
207 // dialog ourselves, the other platforms either have
208 // none, don't make it accessible or let you configure
209 // the printer from the wxPrintDialog anyway.
210 return new wxGenericPrintSetupDialog( parent, data );
211#endif
212}
213
888dde65 214wxDCImpl* wxNativePrintFactory::CreatePrinterDCImpl( wxPrinterDC *owner, const wxPrintData& data )
147bf263 215{
26eef304 216#if defined(__WXGTK__) || defined(__WXMOTIF__) || ( defined(__WXUNIVERSAL__) && !defined(__WXMAC__) )
888dde65 217 return new wxPostScriptDCImpl( owner, data );
147bf263 218#else
888dde65 219 return new wxPrinterDCImpl( owner, data );
147bf263
JS
220#endif
221}
222
6038ec8e
RR
223bool wxNativePrintFactory::HasOwnPrintToFile()
224{
225 // Only relevant for PostScript and here the
2997ca30 226 // setup dialog provides no "print to file"
6038ec8e
RR
227 // option. In the GNOME setup dialog, the
228 // setup dialog has its own print to file.
229 return false;
230}
231
232bool wxNativePrintFactory::HasPrinterLine()
233{
234 // Only relevant for PostScript for now
235 return true;
236}
237
238wxString wxNativePrintFactory::CreatePrinterLine()
239{
240 // Only relevant for PostScript for now
2997ca30 241
6038ec8e
RR
242 // We should query "lpstat -d" here
243 return _("Generic PostScript");
244}
245
246bool wxNativePrintFactory::HasStatusLine()
247{
248 // Only relevant for PostScript for now
2997ca30 249 return true;
6038ec8e
RR
250}
251
252wxString wxNativePrintFactory::CreateStatusLine()
253{
254 // Only relevant for PostScript for now
2997ca30 255
6038ec8e
RR
256 // We should query "lpstat -r" or "lpstat -p" here
257 return _("Ready");
258}
259
8850cbd3
RR
260wxPrintNativeDataBase *wxNativePrintFactory::CreatePrintNativeData()
261{
262#if defined(__WXMSW__) && !defined(__WXUNIVERSAL__)
263 return new wxWindowsPrintNativeData;
264#elif defined(__WXMAC__)
c347101b 265 return wxOSXCreatePrintData();
8850cbd3
RR
266#else
267 return new wxPostScriptPrintNativeData;
268#endif
269}
270
271//----------------------------------------------------------------------------
272// wxPrintNativeDataBase
273//----------------------------------------------------------------------------
274
275IMPLEMENT_ABSTRACT_CLASS(wxPrintNativeDataBase, wxObject)
276
277wxPrintNativeDataBase::wxPrintNativeDataBase()
2997ca30
WS
278{
279 m_ref = 1;
8850cbd3
RR
280}
281
0ab2de15
RR
282//----------------------------------------------------------------------------
283// wxPrintFactoryModule
284//----------------------------------------------------------------------------
285
286class wxPrintFactoryModule: public wxModule
287{
288public:
289 wxPrintFactoryModule() {}
290 bool OnInit() { return true; }
291 void OnExit() { wxPrintFactory::SetPrintFactory( NULL ); }
2997ca30 292
0ab2de15
RR
293private:
294 DECLARE_DYNAMIC_CLASS(wxPrintFactoryModule)
295};
296
297IMPLEMENT_DYNAMIC_CLASS(wxPrintFactoryModule, wxModule)
298
e81e3883
RR
299//----------------------------------------------------------------------------
300// wxPrinterBase
301//----------------------------------------------------------------------------
302
303IMPLEMENT_CLASS(wxPrinterBase, wxObject)
7bcb11d3
JS
304
305wxPrinterBase::wxPrinterBase(wxPrintDialogData *data)
c801d85f 306{
d3b9f782
VZ
307 m_currentPrintout = NULL;
308 sm_abortWindow = NULL;
7e548f6b 309 sm_abortIt = false;
7bcb11d3
JS
310 if (data)
311 m_printDialogData = (*data);
f6bcfd97 312 sm_lastError = wxPRINTER_NO_ERROR;
c801d85f
KB
313}
314
d3b9f782 315wxWindow *wxPrinterBase::sm_abortWindow = NULL;
7e548f6b 316bool wxPrinterBase::sm_abortIt = false;
f6bcfd97 317wxPrinterError wxPrinterBase::sm_lastError = wxPRINTER_NO_ERROR;
c801d85f 318
34da0970 319wxPrinterBase::~wxPrinterBase()
c801d85f
KB
320{
321}
322
446659ca 323wxPrintAbortDialog *wxPrinterBase::CreateAbortWindow(wxWindow *parent, wxPrintout * printout)
c801d85f 324{
1a19b321 325 return new wxPrintAbortDialog(parent, printout->GetTitle());
c801d85f
KB
326}
327
161f4f73 328void wxPrinterBase::ReportError(wxWindow *parent, wxPrintout *WXUNUSED(printout), const wxString& message)
c801d85f 329{
7bcb11d3 330 wxMessageBox(message, _("Printing Error"), wxOK, parent);
c801d85f
KB
331}
332
c061373d
RR
333wxPrintDialogData& wxPrinterBase::GetPrintDialogData() const
334{
335 return (wxPrintDialogData&) m_printDialogData;
336}
337
e81e3883
RR
338//----------------------------------------------------------------------------
339// wxPrinter
340//----------------------------------------------------------------------------
341
342IMPLEMENT_CLASS(wxPrinter, wxPrinterBase)
343
344wxPrinter::wxPrinter(wxPrintDialogData *data)
345{
346 m_pimpl = wxPrintFactory::GetFactory()->CreatePrinter( data );
347}
348
349wxPrinter::~wxPrinter()
350{
351 delete m_pimpl;
352}
353
446659ca 354wxPrintAbortDialog *wxPrinter::CreateAbortWindow(wxWindow *parent, wxPrintout *printout)
e81e3883
RR
355{
356 return m_pimpl->CreateAbortWindow( parent, printout );
357}
358
359void wxPrinter::ReportError(wxWindow *parent, wxPrintout *printout, const wxString& message)
360{
361 m_pimpl->ReportError( parent, printout, message );
362}
363
364bool wxPrinter::Setup(wxWindow *parent)
365{
366 return m_pimpl->Setup( parent );
367}
368
369bool wxPrinter::Print(wxWindow *parent, wxPrintout *printout, bool prompt)
370{
32eec436
VZ
371 if ( !prompt && m_printDialogData.GetToPage() == 0 )
372 {
373 // If the dialog is not shown, set the pages range to print everything
374 // by default (as otherwise we wouldn't print anything at all which is
375 // certainly not a reasonable default behaviour).
376 int minPage, maxPage, selFrom, selTo;
377 printout->GetPageInfo(&minPage, &maxPage, &selFrom, &selTo);
378
379 wxPrintDialogData& pdd = m_pimpl->GetPrintDialogData();
380 pdd.SetFromPage(minPage);
381 pdd.SetToPage(maxPage);
382 }
383
e81e3883
RR
384 return m_pimpl->Print( parent, printout, prompt );
385}
386
387wxDC* wxPrinter::PrintDialog(wxWindow *parent)
388{
389 return m_pimpl->PrintDialog( parent );
390}
391
c061373d
RR
392wxPrintDialogData& wxPrinter::GetPrintDialogData() const
393{
394 return m_pimpl->GetPrintDialogData();
395}
396
397// ---------------------------------------------------------------------------
891daf8c 398// wxPrintDialogBase: the dialog for printing.
c061373d
RR
399// ---------------------------------------------------------------------------
400
891daf8c 401IMPLEMENT_ABSTRACT_CLASS(wxPrintDialogBase, wxDialog)
c061373d 402
6ce8c562 403wxPrintDialogBase::wxPrintDialogBase(wxWindow *parent,
2997ca30 404 wxWindowID id,
6ce8c562
VZ
405 const wxString &title,
406 const wxPoint &pos,
407 const wxSize &size,
408 long style)
891daf8c
RR
409 : wxDialog( parent, id, title.empty() ? wxString(_("Print")) : title,
410 pos, size, style )
c061373d
RR
411{
412}
413
414// ---------------------------------------------------------------------------
891daf8c 415// wxPrintDialog: the dialog for printing
c061373d
RR
416// ---------------------------------------------------------------------------
417
418IMPLEMENT_CLASS(wxPrintDialog, wxObject)
419
420wxPrintDialog::wxPrintDialog(wxWindow *parent, wxPrintDialogData* data)
421{
422 m_pimpl = wxPrintFactory::GetFactory()->CreatePrintDialog( parent, data );
423}
424
425wxPrintDialog::wxPrintDialog(wxWindow *parent, wxPrintData* data)
426{
427 m_pimpl = wxPrintFactory::GetFactory()->CreatePrintDialog( parent, data );
428}
429
430wxPrintDialog::~wxPrintDialog()
431{
432 delete m_pimpl;
433}
434
435int wxPrintDialog::ShowModal()
436{
437 return m_pimpl->ShowModal();
438}
439
440wxPrintDialogData& wxPrintDialog::GetPrintDialogData()
441{
442 return m_pimpl->GetPrintDialogData();
443}
444
445wxPrintData& wxPrintDialog::GetPrintData()
446{
447 return m_pimpl->GetPrintData();
448}
891daf8c 449
c061373d
RR
450wxDC *wxPrintDialog::GetPrintDC()
451{
452 return m_pimpl->GetPrintDC();
453}
454
891daf8c
RR
455// ---------------------------------------------------------------------------
456// wxPageSetupDialogBase: the page setup dialog
457// ---------------------------------------------------------------------------
458
459IMPLEMENT_ABSTRACT_CLASS(wxPageSetupDialogBase, wxDialog)
460
461wxPageSetupDialogBase::wxPageSetupDialogBase(wxWindow *parent,
2997ca30 462 wxWindowID id,
891daf8c
RR
463 const wxString &title,
464 const wxPoint &pos,
465 const wxSize &size,
466 long style)
467 : wxDialog( parent, id, title.empty() ? wxString(_("Page setup")) : title,
468 pos, size, style )
469{
470}
471
472// ---------------------------------------------------------------------------
473// wxPageSetupDialog: the page setup dialog
474// ---------------------------------------------------------------------------
475
476IMPLEMENT_CLASS(wxPageSetupDialog, wxObject)
477
478wxPageSetupDialog::wxPageSetupDialog(wxWindow *parent, wxPageSetupDialogData *data )
479{
480 m_pimpl = wxPrintFactory::GetFactory()->CreatePageSetupDialog( parent, data );
481}
482
483wxPageSetupDialog::~wxPageSetupDialog()
484{
485 delete m_pimpl;
486}
487
488int wxPageSetupDialog::ShowModal()
489{
490 return m_pimpl->ShowModal();
491}
492
493wxPageSetupDialogData& wxPageSetupDialog::GetPageSetupDialogData()
494{
495 return m_pimpl->GetPageSetupDialogData();
496}
497
498// old name
499wxPageSetupDialogData& wxPageSetupDialog::GetPageSetupData()
500{
501 return m_pimpl->GetPageSetupDialogData();
502}
503
e81e3883
RR
504//----------------------------------------------------------------------------
505// wxPrintAbortDialog
506//----------------------------------------------------------------------------
507
508BEGIN_EVENT_TABLE(wxPrintAbortDialog, wxDialog)
509 EVT_BUTTON(wxID_CANCEL, wxPrintAbortDialog::OnCancel)
510END_EVENT_TABLE()
511
1a19b321
VS
512wxPrintAbortDialog::wxPrintAbortDialog(wxWindow *parent,
513 const wxString& documentTitle,
514 const wxPoint& pos,
515 const wxSize& size,
516 long style,
517 const wxString& name)
518 : wxDialog(parent, wxID_ANY, _("Printing"), pos, size, style, name)
519{
e7aa9bb7
VS
520 wxBoxSizer *mainSizer = new wxBoxSizer(wxVERTICAL);
521 mainSizer->Add(new wxStaticText(this, wxID_ANY, _("Please wait while printing...")),
522 wxSizerFlags().Expand().DoubleBorder());
523
524 wxFlexGridSizer *gridSizer = new wxFlexGridSizer(2, wxSize(20, 0));
525 gridSizer->Add(new wxStaticText(this, wxID_ANY, _("Document:")));
526 gridSizer->AddGrowableCol(1);
527 gridSizer->Add(new wxStaticText(this, wxID_ANY, documentTitle));
528 gridSizer->Add(new wxStaticText(this, wxID_ANY, _("Progress:")));
529 m_progress = new wxStaticText(this, wxID_ANY, _("Preparing"));
530 m_progress->SetMinSize(wxSize(250, -1));
531 gridSizer->Add(m_progress);
532 mainSizer->Add(gridSizer, wxSizerFlags().Expand().DoubleBorder(wxLEFT | wxRIGHT));
533
534 mainSizer->Add(CreateStdDialogButtonSizer(wxCANCEL),
535 wxSizerFlags().Expand().DoubleBorder());
536
537 SetSizerAndFit(mainSizer);
538}
539
540void wxPrintAbortDialog::SetProgress(int currentPage, int totalPages,
541 int currentCopy, int totalCopies)
542{
543 wxString text;
544 text.Printf(_("Printing page %d of %d"), currentPage, totalPages);
545 if ( totalCopies > 1 )
546 text += wxString::Format(_(" (copy %d of %d)"), currentCopy, totalCopies);
547 m_progress->SetLabel(text);
1a19b321
VS
548}
549
e81e3883
RR
550void wxPrintAbortDialog::OnCancel(wxCommandEvent& WXUNUSED(event))
551{
ef344c36 552 wxCHECK_RET( wxPrinterBase::sm_abortWindow != NULL, "OnCancel called twice" );
84cabd36 553
e81e3883 554 wxPrinterBase::sm_abortIt = true;
861eef72 555 wxPrinterBase::sm_abortWindow->Destroy();
d3b9f782 556 wxPrinterBase::sm_abortWindow = NULL;
e81e3883
RR
557}
558
559//----------------------------------------------------------------------------
560// wxPrintout
561//----------------------------------------------------------------------------
562
563IMPLEMENT_ABSTRACT_CLASS(wxPrintout, wxObject)
7bcb11d3 564
34da0970 565wxPrintout::wxPrintout(const wxString& title)
c801d85f 566{
7bcb11d3 567 m_printoutTitle = title ;
d3b9f782 568 m_printoutDC = NULL;
7bcb11d3
JS
569 m_pageWidthMM = 0;
570 m_pageHeightMM = 0;
571 m_pageWidthPixels = 0;
572 m_pageHeightPixels = 0;
573 m_PPIScreenX = 0;
574 m_PPIScreenY = 0;
575 m_PPIPrinterX = 0;
576 m_PPIPrinterY = 0;
1bd122dd 577 m_preview = NULL;
c801d85f
KB
578}
579
34da0970 580wxPrintout::~wxPrintout()
c801d85f 581{
c801d85f
KB
582}
583
584bool wxPrintout::OnBeginDocument(int WXUNUSED(startPage), int WXUNUSED(endPage))
585{
fc799548 586 return GetDC()->StartDoc(_("Printing ") + m_printoutTitle);
c801d85f
KB
587}
588
34da0970 589void wxPrintout::OnEndDocument()
c801d85f 590{
7bcb11d3 591 GetDC()->EndDoc();
c801d85f
KB
592}
593
34da0970 594void wxPrintout::OnBeginPrinting()
c801d85f
KB
595{
596}
597
34da0970 598void wxPrintout::OnEndPrinting()
c801d85f
KB
599{
600}
601
602bool wxPrintout::HasPage(int page)
603{
7bcb11d3 604 return (page == 1);
c801d85f
KB
605}
606
607void wxPrintout::GetPageInfo(int *minPage, int *maxPage, int *fromPage, int *toPage)
608{
7bcb11d3
JS
609 *minPage = 1;
610 *maxPage = 32000;
611 *fromPage = 1;
612 *toPage = 1;
c801d85f
KB
613}
614
f415cab9
JS
615void wxPrintout::FitThisSizeToPaper(const wxSize& imageSize)
616{
617 // Set the DC scale and origin so that the given image size fits within the
618 // entire page and the origin is at the top left corner of the page. Note
619 // that with most printers, portions of the page will be non-printable. Use
620 // this if you're managing your own page margins.
621 if (!m_printoutDC) return;
622 wxRect paperRect = GetPaperRectPixels();
623 wxCoord pw, ph;
624 GetPageSizePixels(&pw, &ph);
625 wxCoord w, h;
626 m_printoutDC->GetSize(&w, &h);
627 float scaleX = ((float(paperRect.width) * w) / (float(pw) * imageSize.x));
628 float scaleY = ((float(paperRect.height) * h) / (float(ph) * imageSize.y));
629 float actualScale = wxMin(scaleX, scaleY);
03647350 630 m_printoutDC->SetUserScale(actualScale, actualScale);
f415cab9
JS
631 m_printoutDC->SetDeviceOrigin(0, 0);
632 wxRect logicalPaperRect = GetLogicalPaperRect();
633 SetLogicalOrigin(logicalPaperRect.x, logicalPaperRect.y);
634}
635
636void wxPrintout::FitThisSizeToPage(const wxSize& imageSize)
637{
638 // Set the DC scale and origin so that the given image size fits within the
639 // printable area of the page and the origin is at the top left corner of
640 // the printable area.
641 if (!m_printoutDC) return;
642 int w, h;
643 m_printoutDC->GetSize(&w, &h);
644 float scaleX = float(w) / imageSize.x;
645 float scaleY = float(h) / imageSize.y;
646 float actualScale = wxMin(scaleX, scaleY);
03647350 647 m_printoutDC->SetUserScale(actualScale, actualScale);
f415cab9
JS
648 m_printoutDC->SetDeviceOrigin(0, 0);
649}
650
651void wxPrintout::FitThisSizeToPageMargins(const wxSize& imageSize, const wxPageSetupDialogData& pageSetupData)
652{
653 // Set the DC scale and origin so that the given image size fits within the
654 // page margins defined in the given wxPageSetupDialogData object and the
655 // origin is at the top left corner of the page margins.
656 if (!m_printoutDC) return;
657 wxRect paperRect = GetPaperRectPixels();
658 wxCoord pw, ph;
659 GetPageSizePixels(&pw, &ph);
660 wxPoint topLeft = pageSetupData.GetMarginTopLeft();
661 wxPoint bottomRight = pageSetupData.GetMarginBottomRight();
662 wxCoord mw, mh;
663 GetPageSizeMM(&mw, &mh);
664 float mmToDeviceX = float(pw) / mw;
665 float mmToDeviceY = float(ph) / mh;
5a70d3f5
VZ
666 wxRect pageMarginsRect(paperRect.x + wxRound(mmToDeviceX * topLeft.x),
667 paperRect.y + wxRound(mmToDeviceY * topLeft.y),
668 paperRect.width - wxRound(mmToDeviceX * (topLeft.x + bottomRight.x)),
669 paperRect.height - wxRound(mmToDeviceY * (topLeft.y + bottomRight.y)));
f415cab9
JS
670 wxCoord w, h;
671 m_printoutDC->GetSize(&w, &h);
672 float scaleX = (float(pageMarginsRect.width) * w) / (float(pw) * imageSize.x);
673 float scaleY = (float(pageMarginsRect.height) * h) / (float(ph) * imageSize.y);
674 float actualScale = wxMin(scaleX, scaleY);
03647350 675 m_printoutDC->SetUserScale(actualScale, actualScale);
f415cab9
JS
676 m_printoutDC->SetDeviceOrigin(0, 0);
677 wxRect logicalPageMarginsRect = GetLogicalPageMarginsRect(pageSetupData);
678 SetLogicalOrigin(logicalPageMarginsRect.x, logicalPageMarginsRect.y);
679}
680
681void wxPrintout::MapScreenSizeToPaper()
682{
683 // Set the DC scale so that an image on the screen is the same size on the
684 // paper and the origin is at the top left of the paper. Note that with most
685 // printers, portions of the page will be cut off. Use this if you're
686 // managing your own page margins.
687 if (!m_printoutDC) return;
688 MapScreenSizeToPage();
689 wxRect logicalPaperRect = GetLogicalPaperRect();
690 SetLogicalOrigin(logicalPaperRect.x, logicalPaperRect.y);
691}
692
693void wxPrintout::MapScreenSizeToPage()
694{
695 // Set the DC scale and origin so that an image on the screen is the same
696 // size on the paper and the origin is at the top left of the printable area.
697 if (!m_printoutDC) return;
03647350
VZ
698 int ppiScreenX, ppiScreenY;
699 GetPPIScreen(&ppiScreenX, &ppiScreenY);
700 int ppiPrinterX, ppiPrinterY;
701 GetPPIPrinter(&ppiPrinterX, &ppiPrinterY);
702 int w, h;
703 m_printoutDC->GetSize(&w, &h);
704 int pageSizePixelsX, pageSizePixelsY;
705 GetPageSizePixels(&pageSizePixelsX, &pageSizePixelsY);
706 float userScaleX = (float(ppiPrinterX) * w) / (float(ppiScreenX) * pageSizePixelsX);
707 float userScaleY = (float(ppiPrinterY) * h) / (float(ppiScreenY) * pageSizePixelsY);
f415cab9
JS
708 m_printoutDC->SetUserScale(userScaleX, userScaleY);
709 m_printoutDC->SetDeviceOrigin(0, 0);
710}
711
712void wxPrintout::MapScreenSizeToPageMargins(const wxPageSetupDialogData& pageSetupData)
713{
714 // Set the DC scale so that an image on the screen is the same size on the
715 // paper and the origin is at the top left of the page margins defined by
716 // the given wxPageSetupDialogData object.
717 if (!m_printoutDC) return;
718 MapScreenSizeToPage();
719 wxRect logicalPageMarginsRect = GetLogicalPageMarginsRect(pageSetupData);
720 SetLogicalOrigin(logicalPageMarginsRect.x, logicalPageMarginsRect.y);
721}
722
723void wxPrintout::MapScreenSizeToDevice()
724{
725 // Set the DC scale so that a screen pixel is the same size as a device
726 // pixel and the origin is at the top left of the printable area.
727 if (!m_printoutDC) return;
03647350
VZ
728 int w, h;
729 m_printoutDC->GetSize(&w, &h);
730 int pageSizePixelsX, pageSizePixelsY;
731 GetPageSizePixels(&pageSizePixelsX, &pageSizePixelsY);
732 float userScaleX = float(w) / pageSizePixelsX;
733 float userScaleY = float(h) / pageSizePixelsY;
f415cab9
JS
734 m_printoutDC->SetUserScale(userScaleX, userScaleY);
735 m_printoutDC->SetDeviceOrigin(0, 0);
736}
737
738wxRect wxPrintout::GetLogicalPaperRect() const
739{
740 // Return the rectangle in logical units that corresponds to the paper
741 // rectangle.
742 wxRect paperRect = GetPaperRectPixels();
743 wxCoord pw, ph;
744 GetPageSizePixels(&pw, &ph);
745 wxCoord w, h;
746 m_printoutDC->GetSize(&w, &h);
747 if (w == pw && h == ph) {
748 // this DC matches the printed page, so no scaling
03647350
VZ
749 return wxRect(m_printoutDC->DeviceToLogicalX(paperRect.x),
750 m_printoutDC->DeviceToLogicalY(paperRect.y),
751 m_printoutDC->DeviceToLogicalXRel(paperRect.width),
f415cab9
JS
752 m_printoutDC->DeviceToLogicalYRel(paperRect.height));
753 }
754 // This DC doesn't match the printed page, so we have to scale.
755 float scaleX = float(w) / pw;
756 float scaleY = float(h) / ph;
03647350
VZ
757 return wxRect(m_printoutDC->DeviceToLogicalX(wxRound(paperRect.x * scaleX)),
758 m_printoutDC->DeviceToLogicalY(wxRound(paperRect.y * scaleY)),
759 m_printoutDC->DeviceToLogicalXRel(wxRound(paperRect.width * scaleX)),
5a70d3f5 760 m_printoutDC->DeviceToLogicalYRel(wxRound(paperRect.height * scaleY)));
f415cab9
JS
761}
762
763wxRect wxPrintout::GetLogicalPageRect() const
764{
765 // Return the rectangle in logical units that corresponds to the printable
766 // area.
767 int w, h;
768 m_printoutDC->GetSize(&w, &h);
03647350
VZ
769 return wxRect(m_printoutDC->DeviceToLogicalX(0),
770 m_printoutDC->DeviceToLogicalY(0),
771 m_printoutDC->DeviceToLogicalXRel(w),
f415cab9
JS
772 m_printoutDC->DeviceToLogicalYRel(h));
773}
774
775wxRect wxPrintout::GetLogicalPageMarginsRect(const wxPageSetupDialogData& pageSetupData) const
776{
777 // Return the rectangle in logical units that corresponds to the region
778 // within the page margins as specified by the given wxPageSetupDialogData
779 // object.
03647350 780
02255e07
RR
781 // We get the paper size in device units and the margins in mm,
782 // so we need to calculate the conversion with this trick
f415cab9
JS
783 wxCoord pw, ph;
784 GetPageSizePixels(&pw, &ph);
f415cab9
JS
785 wxCoord mw, mh;
786 GetPageSizeMM(&mw, &mh);
787 float mmToDeviceX = float(pw) / mw;
788 float mmToDeviceY = float(ph) / mh;
02255e07 789
03647350 790 // paper size in device units
02255e07 791 wxRect paperRect = GetPaperRectPixels();
03647350 792
02255e07
RR
793 // margins in mm
794 wxPoint topLeft = pageSetupData.GetMarginTopLeft();
795 wxPoint bottomRight = pageSetupData.GetMarginBottomRight();
03647350 796
02255e07
RR
797 // calculate margins in device units
798 wxRect pageMarginsRect(
799 paperRect.x + wxRound(mmToDeviceX * topLeft.x),
800 paperRect.y + wxRound(mmToDeviceY * topLeft.y),
801 paperRect.width - wxRound(mmToDeviceX * (topLeft.x + bottomRight.x)),
5a70d3f5 802 paperRect.height - wxRound(mmToDeviceY * (topLeft.y + bottomRight.y)));
03647350 803
f415cab9
JS
804 wxCoord w, h;
805 m_printoutDC->GetSize(&w, &h);
03647350 806 if (w == pw && h == ph)
02255e07 807 {
f415cab9 808 // this DC matches the printed page, so no scaling
02255e07 809 return wxRect(
03647350
VZ
810 m_printoutDC->DeviceToLogicalX(pageMarginsRect.x),
811 m_printoutDC->DeviceToLogicalY(pageMarginsRect.y),
812 m_printoutDC->DeviceToLogicalXRel(pageMarginsRect.width),
f415cab9
JS
813 m_printoutDC->DeviceToLogicalYRel(pageMarginsRect.height));
814 }
03647350 815
f415cab9
JS
816 // This DC doesn't match the printed page, so we have to scale.
817 float scaleX = float(w) / pw;
818 float scaleY = float(h) / ph;
03647350
VZ
819 return wxRect(m_printoutDC->DeviceToLogicalX(wxRound(pageMarginsRect.x * scaleX)),
820 m_printoutDC->DeviceToLogicalY(wxRound(pageMarginsRect.y * scaleY)),
821 m_printoutDC->DeviceToLogicalXRel(wxRound(pageMarginsRect.width * scaleX)),
5a70d3f5 822 m_printoutDC->DeviceToLogicalYRel(wxRound(pageMarginsRect.height * scaleY)));
f415cab9
JS
823}
824
825void wxPrintout::SetLogicalOrigin(wxCoord x, wxCoord y)
826{
827 // Set the device origin by specifying a point in logical coordinates.
02255e07 828 m_printoutDC->SetDeviceOrigin(
03647350 829 m_printoutDC->LogicalToDeviceX(x),
02255e07 830 m_printoutDC->LogicalToDeviceY(y) );
f415cab9 831}
03647350 832
f415cab9
JS
833void wxPrintout::OffsetLogicalOrigin(wxCoord xoff, wxCoord yoff)
834{
835 // Offset the device origin by a specified distance in device coordinates.
02255e07
RR
836 wxPoint dev_org = m_printoutDC->GetDeviceOrigin();
837 m_printoutDC->SetDeviceOrigin(
03647350 838 dev_org.x + m_printoutDC->LogicalToDeviceXRel(xoff),
02255e07 839 dev_org.y + m_printoutDC->LogicalToDeviceYRel(yoff) );
f415cab9 840}
03647350 841
f415cab9 842
e81e3883
RR
843//----------------------------------------------------------------------------
844// wxPreviewCanvas
845//----------------------------------------------------------------------------
846
847IMPLEMENT_CLASS(wxPreviewCanvas, wxWindow)
848
849BEGIN_EVENT_TABLE(wxPreviewCanvas, wxScrolledWindow)
850 EVT_PAINT(wxPreviewCanvas::OnPaint)
851 EVT_CHAR(wxPreviewCanvas::OnChar)
b88bf073 852 EVT_IDLE(wxPreviewCanvas::OnIdle)
e81e3883 853 EVT_SYS_COLOUR_CHANGED(wxPreviewCanvas::OnSysColourChanged)
fb6efdf2
VZ
854#if wxUSE_MOUSEWHEEL
855 EVT_MOUSEWHEEL(wxPreviewCanvas::OnMouseWheel)
856#endif
e81e3883 857END_EVENT_TABLE()
7bcb11d3 858
e9cafd42
VZ
859// VZ: the current code doesn't refresh properly without
860// wxFULL_REPAINT_ON_RESIZE, this must be fixed as otherwise we have
861// really horrible flicker when resizing the preview frame, but without
862// this style it simply doesn't work correctly at all...
c801d85f 863wxPreviewCanvas::wxPreviewCanvas(wxPrintPreviewBase *preview, wxWindow *parent,
7bcb11d3 864 const wxPoint& pos, const wxSize& size, long style, const wxString& name):
7e548f6b 865wxScrolledWindow(parent, wxID_ANY, pos, size, style | wxFULL_REPAINT_ON_RESIZE, name)
c801d85f 866{
7bcb11d3 867 m_printPreview = preview;
46b24ef9
JS
868#ifdef __WXMAC__
869 // The app workspace colour is always white, but we should have
870 // a contrast with the page.
ca5020c2 871 wxSystemColour colourIndex = wxSYS_COLOUR_3DDKSHADOW;
92b799e0
JS
872#elif defined(__WXGTK__)
873 wxSystemColour colourIndex = wxSYS_COLOUR_BTNFACE;
46b24ef9
JS
874#else
875 wxSystemColour colourIndex = wxSYS_COLOUR_APPWORKSPACE;
e71fd398 876#endif
46b24ef9 877 SetBackgroundColour(wxSystemSettings::GetColour(colourIndex));
8826f46f 878
d2b354f9 879 SetScrollbars(10, 10, 100, 100);
c801d85f
KB
880}
881
34da0970 882wxPreviewCanvas::~wxPreviewCanvas()
c801d85f
KB
883{
884}
885
886void wxPreviewCanvas::OnPaint(wxPaintEvent& WXUNUSED(event))
887{
7bcb11d3
JS
888 wxPaintDC dc(this);
889 PrepareDC( dc );
8826f46f 890
a56fcaaf 891/*
809934d2
RR
892#ifdef __WXGTK__
893 if (!GetUpdateRegion().IsEmpty())
894 dc.SetClippingRegion( GetUpdateRegion() );
895#endif
a56fcaaf 896*/
809934d2 897
7bcb11d3
JS
898 if (m_printPreview)
899 {
900 m_printPreview->PaintPage(this, dc);
901 }
c801d85f
KB
902}
903
b88bf073
VS
904void wxPreviewCanvas::OnIdle(wxIdleEvent& event)
905{
88b98787
VS
906 event.Skip();
907
908 // prevent UpdatePageRendering() from being called recursively:
909 static bool s_inIdle = false;
910 if ( s_inIdle )
911 return;
912 s_inIdle = true;
913
b88bf073
VS
914 if ( m_printPreview )
915 {
916 if ( m_printPreview->UpdatePageRendering() )
917 Refresh();
918 }
88b98787
VS
919
920 s_inIdle = false;
b88bf073
VS
921}
922
c801d85f
KB
923// Responds to colour changes, and passes event on to children.
924void wxPreviewCanvas::OnSysColourChanged(wxSysColourChangedEvent& event)
925{
46b24ef9
JS
926#ifdef __WXMAC__
927 // The app workspace colour is always white, but we should have
928 // a contrast with the page.
ca5020c2 929 wxSystemColour colourIndex = wxSYS_COLOUR_3DDKSHADOW;
92b799e0
JS
930#elif defined(__WXGTK__)
931 wxSystemColour colourIndex = wxSYS_COLOUR_BTNFACE;
46b24ef9
JS
932#else
933 wxSystemColour colourIndex = wxSYS_COLOUR_APPWORKSPACE;
e71fd398 934#endif
46b24ef9 935 SetBackgroundColour(wxSystemSettings::GetColour(colourIndex));
7bcb11d3 936 Refresh();
8826f46f 937
7bcb11d3
JS
938 // Propagate the event to the non-top-level children
939 wxWindow::OnSysColourChanged(event);
c801d85f
KB
940}
941
d2b354f9
JS
942void wxPreviewCanvas::OnChar(wxKeyEvent &event)
943{
b38b0d22 944 wxPreviewControlBar* controlBar = ((wxPreviewFrame*) GetParent())->GetControlBar();
043c6705 945 switch (event.GetKeyCode())
d2b354f9 946 {
043c6705
VZ
947 case WXK_RETURN:
948 controlBar->OnPrint();
949 return;
82579461
VZ
950 case (int)'+':
951 case WXK_NUMPAD_ADD:
952 case WXK_ADD:
953 controlBar->DoZoomIn();
954 return;
955 case (int)'-':
956 case WXK_NUMPAD_SUBTRACT:
957 case WXK_SUBTRACT:
958 controlBar->DoZoomOut();
959 return;
b38b0d22
JS
960 }
961
d2b354f9
JS
962 if (!event.ControlDown())
963 {
964 event.Skip();
965 return;
966 }
e71fd398 967
b38b0d22
JS
968 switch(event.GetKeyCode())
969 {
faa94f3e 970 case WXK_PAGEDOWN:
b38b0d22 971 controlBar->OnNext(); break;
faa94f3e 972 case WXK_PAGEUP:
b38b0d22
JS
973 controlBar->OnPrevious(); break;
974 case WXK_HOME:
975 controlBar->OnFirst(); break;
976 case WXK_END:
977 controlBar->OnLast(); break;
978 default:
979 event.Skip();
980 }
d2b354f9
JS
981}
982
fb6efdf2
VZ
983#if wxUSE_MOUSEWHEEL
984
985void wxPreviewCanvas::OnMouseWheel(wxMouseEvent& event)
986{
987 wxPreviewControlBar *
988 controlBar = wxStaticCast(GetParent(), wxPreviewFrame)->GetControlBar();
989
990 if ( controlBar )
991 {
992 if ( event.ControlDown() && event.GetWheelRotation() != 0 )
993 {
994 int currentZoom = controlBar->GetZoomControl();
995
996 int delta;
997 if ( currentZoom < 100 )
998 delta = 5;
999 else if ( currentZoom <= 120 )
1000 delta = 10;
1001 else
1002 delta = 50;
1003
1004 if ( event.GetWheelRotation() > 0 )
1005 delta = -delta;
1006
1007 int newZoom = currentZoom + delta;
1008 if ( newZoom < 10 )
1009 newZoom = 10;
1010 if ( newZoom > 200 )
1011 newZoom = 200;
1012 if ( newZoom != currentZoom )
1013 {
1014 controlBar->SetZoomControl(newZoom);
1015 m_printPreview->SetZoom(newZoom);
1016 Refresh();
1017 }
1018 return;
1019 }
1020 }
1021
1022 event.Skip();
1023}
1024
1025#endif // wxUSE_MOUSEWHEEL
1026
d7cfeeb4
VZ
1027namespace
1028{
1029
1030// This is by the controls in the print preview as the maximal (and hence
1031// longest) page number we may have to display.
1032enum { MAX_PAGE_NUMBER = 99999 };
1033
1034} // anonymous namespace
1035
1036// ----------------------------------------------------------------------------
1037// wxPrintPageMaxCtrl
1038// ----------------------------------------------------------------------------
1039
1040// A simple static control showing the maximal number of pages.
1041class wxPrintPageMaxCtrl : public wxStaticText
1042{
1043public:
1044 wxPrintPageMaxCtrl(wxWindow *parent)
1045 : wxStaticText(
1046 parent,
1047 wxID_ANY,
1048 wxString(),
1049 wxDefaultPosition,
1050 wxSize
1051 (
1052 parent->GetTextExtent(MaxAsString(MAX_PAGE_NUMBER)).x,
1053 wxDefaultCoord
1054 ),
1055 wxST_NO_AUTORESIZE | wxALIGN_CENTRE
1056 )
1057 {
1058 }
1059
1060 // Set the maximal page to display once we really know what it is.
1061 void SetMaxPage(int maxPage)
1062 {
1063 SetLabel(MaxAsString(maxPage));
1064 }
1065
1066private:
1067 static wxString MaxAsString(int maxPage)
1068 {
1069 return wxString::Format("/ %d", maxPage);
1070 }
1071
1072 wxDECLARE_NO_COPY_CLASS(wxPrintPageMaxCtrl);
1073};
1074
82579461
VZ
1075// ----------------------------------------------------------------------------
1076// wxPrintPageTextCtrl
1077// ----------------------------------------------------------------------------
1078
d7cfeeb4
VZ
1079// This text control contains the page number in the specified interval.
1080//
1081// Invalid pages are not accepted and the control contents is validated when it
1082// loses focus. Conversely, if the user changes the page to another valid one
1083// or presses Enter, OnGotoPage() method of the preview object will be called.
82579461
VZ
1084class wxPrintPageTextCtrl : public wxTextCtrl
1085{
1086public:
d7cfeeb4 1087 wxPrintPageTextCtrl(wxPreviewControlBar *preview)
82579461
VZ
1088 : wxTextCtrl(preview,
1089 wxID_PREVIEW_GOTO,
d7cfeeb4 1090 wxString(),
82579461 1091 wxDefaultPosition,
d7cfeeb4
VZ
1092 // We use hardcoded maximal page number for the width
1093 // instead of fitting it to the values we can show because
1094 // the control looks uncomfortably narrow if the real page
1095 // number is just one or two digits.
1096 wxSize
1097 (
1098 preview->GetTextExtent(PageAsString(MAX_PAGE_NUMBER)).x,
1099 wxDefaultCoord
1100 ),
82579461
VZ
1101 wxTE_PROCESS_ENTER
1102#if wxUSE_VALIDATORS
1103 , wxTextValidator(wxFILTER_DIGITS)
1104#endif // wxUSE_VALIDATORS
1105 ),
d7cfeeb4 1106 m_preview(preview)
82579461 1107 {
d7cfeeb4
VZ
1108 m_minPage =
1109 m_maxPage =
1110 m_page = 1;
82579461
VZ
1111
1112 Connect(wxEVT_KILL_FOCUS,
1113 wxFocusEventHandler(wxPrintPageTextCtrl::OnKillFocus));
ce7fe42e 1114 Connect(wxEVT_TEXT_ENTER,
82579461
VZ
1115 wxCommandEventHandler(wxPrintPageTextCtrl::OnTextEnter));
1116 }
1117
d7cfeeb4
VZ
1118 // Update the pages range, must be called after OnPreparePrinting() as
1119 // these values are not known before.
1120 void SetPageInfo(int minPage, int maxPage)
1121 {
1122 m_minPage = minPage;
1123 m_maxPage = maxPage;
1124
1125 // Show the first page by default.
1126 SetPageNumber(minPage);
1127 }
1128
82579461
VZ
1129 // Helpers to conveniently set or get the current page number. Return value
1130 // is 0 if the current controls contents is invalid.
1131 void SetPageNumber(int page)
1132 {
1133 wxASSERT( IsValidPage(page) );
1134
1135 SetValue(PageAsString(page));
1136 }
1137
1138 int GetPageNumber() const
1139 {
1140 long value;
1141 if ( !GetValue().ToLong(&value) || !IsValidPage(value) )
1142 return 0;
1143
1144 // Cast is safe because the value is less than (int) m_maxPage.
1145 return static_cast<int>(value);
1146 }
1147
1148private:
1149 static wxString PageAsString(int page)
1150 {
1151 return wxString::Format("%d", page);
1152 }
1153
1154 bool IsValidPage(int page) const
1155 {
1156 return page >= m_minPage && page <= m_maxPage;
1157 }
1158
1159 bool DoChangePage()
1160 {
1161 const int page = GetPageNumber();
1162
1163 if ( !page )
1164 return false;
1165
1166 if ( page != m_page )
1167 {
1168 // We have a valid page, remember it.
1169 m_page = page;
1170
1171 // And notify the owner about the change.
1172 m_preview->OnGotoPage();
1173 }
1174 //else: Nothing really changed.
1175
1176 return true;
1177 }
1178
1179 void OnKillFocus(wxFocusEvent& event)
1180 {
1181 if ( !DoChangePage() )
1182 {
1183 // The current contents is invalid so reset it back to the last
1184 // known good page index.
1185 SetPageNumber(m_page);
1186 }
1187
1188 event.Skip();
1189 }
1190
1191 void OnTextEnter(wxCommandEvent& WXUNUSED(event))
1192 {
1193 DoChangePage();
1194 }
1195
1196
1197 wxPreviewControlBar * const m_preview;
1198
d7cfeeb4
VZ
1199 int m_minPage,
1200 m_maxPage;
82579461
VZ
1201
1202 // This is the last valid page value that we had, we revert to it if an
1203 // invalid page is entered.
1204 int m_page;
1205
1206 wxDECLARE_NO_COPY_CLASS(wxPrintPageTextCtrl);
1207};
1208
e81e3883
RR
1209//----------------------------------------------------------------------------
1210// wxPreviewControlBar
1211//----------------------------------------------------------------------------
1212
1213IMPLEMENT_CLASS(wxPreviewControlBar, wxWindow)
c801d85f
KB
1214
1215BEGIN_EVENT_TABLE(wxPreviewControlBar, wxPanel)
8826f46f 1216 EVT_BUTTON(wxID_PREVIEW_CLOSE, wxPreviewControlBar::OnWindowClose)
90b6b974 1217 EVT_BUTTON(wxID_PREVIEW_PRINT, wxPreviewControlBar::OnPrintButton)
0f90ec93
KB
1218 EVT_BUTTON(wxID_PREVIEW_PREVIOUS, wxPreviewControlBar::OnPreviousButton)
1219 EVT_BUTTON(wxID_PREVIEW_NEXT, wxPreviewControlBar::OnNextButton)
bf89b538
JS
1220 EVT_BUTTON(wxID_PREVIEW_FIRST, wxPreviewControlBar::OnFirstButton)
1221 EVT_BUTTON(wxID_PREVIEW_LAST, wxPreviewControlBar::OnLastButton)
82579461
VZ
1222 EVT_BUTTON(wxID_PREVIEW_ZOOM_IN, wxPreviewControlBar::OnZoomInButton)
1223 EVT_BUTTON(wxID_PREVIEW_ZOOM_OUT, wxPreviewControlBar::OnZoomOutButton)
1224
1225 EVT_UPDATE_UI(wxID_PREVIEW_PREVIOUS, wxPreviewControlBar::OnUpdatePreviousButton)
1226 EVT_UPDATE_UI(wxID_PREVIEW_NEXT, wxPreviewControlBar::OnUpdateNextButton)
1227 EVT_UPDATE_UI(wxID_PREVIEW_FIRST, wxPreviewControlBar::OnUpdateFirstButton)
1228 EVT_UPDATE_UI(wxID_PREVIEW_LAST, wxPreviewControlBar::OnUpdateLastButton)
1229 EVT_UPDATE_UI(wxID_PREVIEW_ZOOM_IN, wxPreviewControlBar::OnUpdateZoomInButton)
1230 EVT_UPDATE_UI(wxID_PREVIEW_ZOOM_OUT, wxPreviewControlBar::OnUpdateZoomOutButton)
1231
1232 EVT_CHOICE(wxID_PREVIEW_ZOOM, wxPreviewControlBar::OnZoomChoice)
8826f46f 1233 EVT_PAINT(wxPreviewControlBar::OnPaint)
82579461 1234
c801d85f 1235END_EVENT_TABLE()
7bcb11d3 1236
c801d85f 1237wxPreviewControlBar::wxPreviewControlBar(wxPrintPreviewBase *preview, long buttons,
7bcb11d3
JS
1238 wxWindow *parent, const wxPoint& pos, const wxSize& size,
1239 long style, const wxString& name):
7e548f6b 1240wxPanel(parent, wxID_ANY, pos, size, style, name)
c801d85f 1241{
7bcb11d3 1242 m_printPreview = preview;
d3b9f782 1243 m_closeButton = NULL;
d3b9f782 1244 m_zoomControl = NULL;
82579461 1245 m_currentPageText = NULL;
d7cfeeb4 1246 m_maxPageText = NULL;
7bcb11d3 1247 m_buttonFlags = buttons;
c801d85f
KB
1248}
1249
34da0970 1250wxPreviewControlBar::~wxPreviewControlBar()
c801d85f
KB
1251{
1252}
1253
1254void wxPreviewControlBar::OnPaint(wxPaintEvent& WXUNUSED(event))
1255{
7bcb11d3 1256 wxPaintDC dc(this);
8826f46f 1257
7bcb11d3
JS
1258 int w, h;
1259 GetSize(&w, &h);
1260 dc.SetPen(*wxBLACK_PEN);
1261 dc.SetBrush(*wxTRANSPARENT_BRUSH);
1262 dc.DrawLine( 0, h-1, w, h-1 );
c801d85f
KB
1263}
1264
c330a2cf 1265void wxPreviewControlBar::OnWindowClose(wxCommandEvent& WXUNUSED(event))
c801d85f 1266{
7bcb11d3 1267 wxPreviewFrame *frame = (wxPreviewFrame *)GetParent();
7e548f6b 1268 frame->Close(true);
c801d85f
KB
1269}
1270
b38b0d22 1271void wxPreviewControlBar::OnPrint(void)
c801d85f 1272{
7bcb11d3 1273 wxPrintPreviewBase *preview = GetPrintPreview();
7e548f6b 1274 preview->Print(true);
c801d85f
KB
1275}
1276
82579461
VZ
1277void wxPreviewControlBar::OnNext()
1278{
1279 if ( IsNextEnabled() )
1280 DoGotoPage(GetPrintPreview()->GetCurrentPage() + 1);
1281}
1282
1283void wxPreviewControlBar::OnPrevious()
1284{
1285 if ( IsPreviousEnabled() )
1286 DoGotoPage(GetPrintPreview()->GetCurrentPage() - 1);
1287}
1288
1289void wxPreviewControlBar::OnFirst()
1290{
1291 if ( IsFirstEnabled() )
1292 DoGotoPage(GetPrintPreview()->GetMinPage());
1293}
1294
1295void wxPreviewControlBar::OnLast()
1296{
1297 if ( IsLastEnabled() )
1298 DoGotoPage(GetPrintPreview()->GetMaxPage());
1299}
1300
1301bool wxPreviewControlBar::IsNextEnabled() const
c801d85f 1302{
7bcb11d3 1303 wxPrintPreviewBase *preview = GetPrintPreview();
82579461
VZ
1304 if ( !preview )
1305 return false;
1306
1307 const int currentPage = preview->GetCurrentPage();
1308 return currentPage < preview->GetMaxPage() &&
1309 preview->GetPrintout()->HasPage(currentPage + 1);
c801d85f
KB
1310}
1311
82579461 1312bool wxPreviewControlBar::IsPreviousEnabled() const
c801d85f 1313{
7bcb11d3 1314 wxPrintPreviewBase *preview = GetPrintPreview();
82579461
VZ
1315 if ( !preview )
1316 return false;
1317
1318 const int currentPage = preview->GetCurrentPage();
1319 return currentPage > preview->GetMinPage() &&
1320 preview->GetPrintout()->HasPage(currentPage - 1);
c801d85f
KB
1321}
1322
82579461 1323bool wxPreviewControlBar::IsFirstEnabled() const
bf89b538
JS
1324{
1325 wxPrintPreviewBase *preview = GetPrintPreview();
82579461
VZ
1326 if (!preview)
1327 return false;
1328
1329 return preview->GetPrintout()->HasPage(preview->GetMinPage());
bf89b538
JS
1330}
1331
82579461 1332bool wxPreviewControlBar::IsLastEnabled() const
bf89b538
JS
1333{
1334 wxPrintPreviewBase *preview = GetPrintPreview();
82579461
VZ
1335 if (!preview)
1336 return false;
1337
1338 return preview->GetPrintout()->HasPage(preview->GetMaxPage());
bf89b538
JS
1339}
1340
82579461
VZ
1341void wxPreviewControlBar::DoGotoPage(int page)
1342{
1343 wxPrintPreviewBase *preview = GetPrintPreview();
1344 wxCHECK_RET( preview, "Shouldn't be called if there is no preview." );
1345
1346 preview->SetCurrentPage(page);
1347
1348 if ( m_currentPageText )
1349 m_currentPageText->SetPageNumber(page);
1350}
1351
1352void wxPreviewControlBar::OnGotoPage()
bf89b538
JS
1353{
1354 wxPrintPreviewBase *preview = GetPrintPreview();
1355 if (preview)
1356 {
bf89b538
JS
1357 if (preview->GetMinPage() > 0)
1358 {
82579461
VZ
1359 long currentPage = m_currentPageText->GetPageNumber();
1360 if ( currentPage )
1361 {
bf89b538
JS
1362 if (preview->GetPrintout()->HasPage(currentPage))
1363 {
1364 preview->SetCurrentPage(currentPage);
1365 }
82579461 1366 }
bf89b538
JS
1367 }
1368 }
1369}
1370
82579461 1371void wxPreviewControlBar::DoZoom()
c801d85f 1372{
7bcb11d3
JS
1373 int zoom = GetZoomControl();
1374 if (GetPrintPreview())
1375 GetPrintPreview()->SetZoom(zoom);
c801d85f
KB
1376}
1377
82579461
VZ
1378bool wxPreviewControlBar::IsZoomInEnabled() const
1379{
1380 if ( !m_zoomControl )
1381 return false;
1382
1383 const unsigned sel = m_zoomControl->GetSelection();
1384 return sel < m_zoomControl->GetCount() - 1;
1385}
1386
1387bool wxPreviewControlBar::IsZoomOutEnabled() const
1388{
1389 return m_zoomControl && m_zoomControl->GetSelection() > 0;
1390}
1391
1392void wxPreviewControlBar::DoZoomIn()
1393{
1394 if (IsZoomInEnabled())
1395 {
1396 m_zoomControl->SetSelection(m_zoomControl->GetSelection() + 1);
1397 DoZoom();
1398 }
1399}
1400
1401void wxPreviewControlBar::DoZoomOut()
1402{
1403 if (IsZoomOutEnabled())
1404 {
1405 m_zoomControl->SetSelection(m_zoomControl->GetSelection() - 1);
1406 DoZoom();
1407 }
1408}
1409
1410namespace
1411{
1412
1413// Helper class used by wxPreviewControlBar::CreateButtons() to add buttons
1414// sequentially to it in the simplest way possible.
1415class SizerWithButtons
1416{
1417public:
1418 // Constructor creates the sizer that will hold the buttons and stores the
1419 // parent that will be used for their creation.
1420 SizerWithButtons(wxWindow *parent)
1421 : m_sizer(new wxBoxSizer(wxHORIZONTAL)),
1422 m_parent(parent)
1423 {
1424 m_hasContents =
1425 m_needsSeparator = false;
1426 }
1427
1428 // Destructor associates the sizer with the parent window.
1429 ~SizerWithButtons()
1430 {
1431 m_parent->SetSizer(m_sizer);
1432 m_sizer->Fit(m_parent);
1433 }
1434
1435
1436 // Add an arbitrary window to the sizer.
1437 void Add(wxWindow *win)
1438 {
1439 if ( m_needsSeparator )
1440 {
1441 m_needsSeparator = false;
1442
1443 m_sizer->AddSpacer(2*wxSizerFlags::GetDefaultBorder());
1444 }
1445
1446 m_hasContents = true;
1447
1448 m_sizer->Add(win,
1449 wxSizerFlags().Border(wxLEFT | wxTOP | wxBOTTOM).Center());
1450 }
1451
1452 // Add a button with the specified id, bitmap and tooltip.
1453 void AddButton(wxWindowID btnId,
1454 const wxArtID& artId,
1455 const wxString& tooltip)
1456 {
1457 // We don't use (smaller) images inside a button with a text label but
1458 // rather toolbar-like bitmap buttons hence use wxART_TOOLBAR and not
1459 // wxART_BUTTON here.
1460 wxBitmap bmp = wxArtProvider::GetBitmap(artId, wxART_TOOLBAR);
1461 wxBitmapButton * const btn = new wxBitmapButton(m_parent, btnId, bmp);
1462 btn->SetToolTip(tooltip);
1463
1464 Add(btn);
1465 }
1466
1467 // Add a control at the right end of the window. This should be called last
1468 // as everything else added after it will be added on the right side too.
1469 void AddAtEnd(wxWindow *win)
1470 {
1471 m_sizer->AddStretchSpacer();
1472 m_sizer->Add(win,
1473 wxSizerFlags().Border(wxTOP | wxBOTTOM | wxRIGHT).Center());
1474 }
1475
1476 // Indicates the end of a group of buttons, a separator will be added after
1477 // it.
1478 void EndOfGroup()
1479 {
1480 if ( m_hasContents )
1481 {
1482 m_needsSeparator = true;
1483 m_hasContents = false;
1484 }
1485 }
1486
1487private:
1488 wxSizer * const m_sizer;
1489 wxWindow * const m_parent;
1490
1491 // If true, we have some controls since the last group beginning. This is
1492 // used to avoid inserting two consecutive separators if EndOfGroup() is
1493 // called twice.
1494 bool m_hasContents;
1495
1496 // If true, a separator should be inserted before adding the next button.
1497 bool m_needsSeparator;
1498
1499 wxDECLARE_NO_COPY_CLASS(SizerWithButtons);
1500};
1501
1502} // anonymous namespace
1503
34da0970 1504void wxPreviewControlBar::CreateButtons()
c801d85f 1505{
82579461 1506 SizerWithButtons sizer(this);
e71fd398 1507
82579461 1508 // Print button group (a single button).
7bcb11d3
JS
1509 if (m_buttonFlags & wxPREVIEW_PRINT)
1510 {
82579461
VZ
1511 sizer.AddButton(wxID_PREVIEW_PRINT, wxART_PRINT, _("Print"));
1512 sizer.EndOfGroup();
7bcb11d3 1513 }
e71fd398 1514
82579461 1515 // Page selection buttons group.
bf89b538
JS
1516 if (m_buttonFlags & wxPREVIEW_FIRST)
1517 {
82579461 1518 sizer.AddButton(wxID_PREVIEW_FIRST, wxART_GOTO_FIRST, _("First page"));
bf89b538 1519 }
e71fd398 1520
7bcb11d3
JS
1521 if (m_buttonFlags & wxPREVIEW_PREVIOUS)
1522 {
82579461 1523 sizer.AddButton(wxID_PREVIEW_PREVIOUS, wxART_GO_BACK, _("Previous page"));
7bcb11d3 1524 }
e71fd398 1525
82579461 1526 if (m_buttonFlags & wxPREVIEW_GOTO)
7bcb11d3 1527 {
d7cfeeb4 1528 m_currentPageText = new wxPrintPageTextCtrl(this);
82579461
VZ
1529 sizer.Add(m_currentPageText);
1530
d7cfeeb4
VZ
1531 m_maxPageText = new wxPrintPageMaxCtrl(this);
1532 sizer.Add(m_maxPageText);
bf89b538 1533 }
e71fd398 1534
82579461 1535 if (m_buttonFlags & wxPREVIEW_NEXT)
bf89b538 1536 {
82579461 1537 sizer.AddButton(wxID_PREVIEW_NEXT, wxART_GO_FORWARD, _("Next page"));
bf89b538 1538 }
e71fd398 1539
82579461 1540 if (m_buttonFlags & wxPREVIEW_LAST)
bf89b538 1541 {
82579461 1542 sizer.AddButton(wxID_PREVIEW_LAST, wxART_GOTO_LAST, _("Last page"));
7bcb11d3 1543 }
e71fd398 1544
82579461
VZ
1545 sizer.EndOfGroup();
1546
1547 // Zoom controls group.
7bcb11d3
JS
1548 if (m_buttonFlags & wxPREVIEW_ZOOM)
1549 {
82579461
VZ
1550 sizer.AddButton(wxID_PREVIEW_ZOOM_OUT, wxART_MINUS, _("Zoom Out"));
1551
e71fd398 1552 wxString choices[] =
c25ccf85 1553 {
2b5f62a0 1554 wxT("10%"), wxT("15%"), wxT("20%"), wxT("25%"), wxT("30%"), wxT("35%"), wxT("40%"), wxT("45%"), wxT("50%"), wxT("55%"),
9dff8515
JS
1555 wxT("60%"), wxT("65%"), wxT("70%"), wxT("75%"), wxT("80%"), wxT("85%"), wxT("90%"), wxT("95%"), wxT("100%"), wxT("110%"),
1556 wxT("120%"), wxT("150%"), wxT("200%")
c25ccf85 1557 };
c25ccf85 1558 int n = WXSIZEOF(choices);
e71fd398 1559
7e548f6b 1560 m_zoomControl = new wxChoice( this, wxID_PREVIEW_ZOOM, wxDefaultPosition, wxSize(70,wxDefaultCoord), n, choices, 0 );
82579461 1561 sizer.Add(m_zoomControl);
7bcb11d3 1562 SetZoomControl(m_printPreview->GetZoom());
82579461
VZ
1563
1564 sizer.AddButton(wxID_PREVIEW_ZOOM_IN, wxART_PLUS, _("Zoom In"));
1565
1566 sizer.EndOfGroup();
7bcb11d3 1567 }
8826f46f 1568
82579461
VZ
1569 // Close button group (single button again).
1570 m_closeButton = new wxButton(this, wxID_PREVIEW_CLOSE, _("&Close"));
1571 sizer.AddAtEnd(m_closeButton);
c801d85f
KB
1572}
1573
d7cfeeb4
VZ
1574void wxPreviewControlBar::SetPageInfo(int minPage, int maxPage)
1575{
1576 if ( m_currentPageText )
1577 m_currentPageText->SetPageInfo(minPage, maxPage);
1578
1579 if ( m_maxPageText )
1580 m_maxPageText->SetMaxPage(maxPage);
1581}
1582
c801d85f
KB
1583void wxPreviewControlBar::SetZoomControl(int zoom)
1584{
7bcb11d3 1585 if (m_zoomControl)
963907fa
RR
1586 {
1587 int n, count = m_zoomControl->GetCount();
1588 long val;
1589 for (n=0; n<count; n++)
1590 {
1591 if (m_zoomControl->GetString(n).BeforeFirst(wxT('%')).ToLong(&val) &&
1592 (val >= long(zoom)))
1593 {
1594 m_zoomControl->SetSelection(n);
1595 return;
1596 }
1597 }
7e548f6b 1598
963907fa
RR
1599 m_zoomControl->SetSelection(count-1);
1600 }
c801d85f
KB
1601}
1602
34da0970 1603int wxPreviewControlBar::GetZoomControl()
c801d85f 1604{
963907fa 1605 if (m_zoomControl && (m_zoomControl->GetStringSelection() != wxEmptyString))
7bcb11d3 1606 {
963907fa
RR
1607 long val;
1608 if (m_zoomControl->GetStringSelection().BeforeFirst(wxT('%')).ToLong(&val))
1609 return int(val);
7bcb11d3 1610 }
7e548f6b 1611
963907fa 1612 return 0;
c801d85f
KB
1613}
1614
1615
1616/*
7bcb11d3
JS
1617* Preview frame
1618*/
c801d85f 1619
e81e3883
RR
1620IMPLEMENT_CLASS(wxPreviewFrame, wxFrame)
1621
e3065973 1622BEGIN_EVENT_TABLE(wxPreviewFrame, wxFrame)
043c6705 1623 EVT_CHAR_HOOK(wxPreviewFrame::OnChar)
0f90ec93 1624 EVT_CLOSE(wxPreviewFrame::OnCloseWindow)
e3065973
JS
1625END_EVENT_TABLE()
1626
043c6705
VZ
1627void wxPreviewFrame::OnChar(wxKeyEvent &event)
1628{
1629 if ( event.GetKeyCode() == WXK_ESCAPE )
1630 {
1631 Close(true);
1632 }
1633 else
1634 {
1635 event.Skip();
1636 }
1637}
1638
a5ae8241 1639wxPreviewFrame::wxPreviewFrame(wxPrintPreviewBase *preview, wxWindow *parent, const wxString& title,
7bcb11d3 1640 const wxPoint& pos, const wxSize& size, long style, const wxString& name):
7e548f6b 1641wxFrame(parent, wxID_ANY, title, pos, size, style, name)
c801d85f 1642{
7bcb11d3
JS
1643 m_printPreview = preview;
1644 m_controlBar = NULL;
1645 m_previewCanvas = NULL;
7c995553 1646 m_windowDisabler = NULL;
6aacfc73 1647 m_modalityKind = wxPreviewFrame_NonModal;
46b24ef9 1648
a5ae8241 1649 // Give the application icon
46b24ef9
JS
1650#ifdef __WXMSW__
1651 wxFrame* topFrame = wxDynamicCast(wxTheApp->GetTopWindow(), wxFrame);
1652 if (topFrame)
7ccf316e 1653 SetIcons(topFrame->GetIcons());
e71fd398 1654#endif
c801d85f
KB
1655}
1656
34da0970 1657wxPreviewFrame::~wxPreviewFrame()
c801d85f 1658{
7bcb11d3
JS
1659 wxPrintout *printout = m_printPreview->GetPrintout();
1660 if (printout)
1661 {
1662 delete printout;
1663 m_printPreview->SetPrintout(NULL);
1664 m_printPreview->SetCanvas(NULL);
1665 m_printPreview->SetFrame(NULL);
1666 }
b88bf073
VS
1667
1668 m_previewCanvas->SetPreview(NULL);
6aacfc73
VZ
1669 delete m_printPreview;
1670}
1671
1672void wxPreviewFrame::OnCloseWindow(wxCloseEvent& WXUNUSED(event))
1673{
1674 // Reenable any windows we disabled by undoing whatever we did in our
1675 // Initialize().
1676 switch ( m_modalityKind )
1677 {
1678 case wxPreviewFrame_AppModal:
1679 delete m_windowDisabler;
1680 m_windowDisabler = NULL;
1681 break;
1682
1683 case wxPreviewFrame_WindowModal:
1684 if ( GetParent() )
1685 GetParent()->Enable();
1686 break;
1687
1688 case wxPreviewFrame_NonModal:
1689 break;
1690 }
8826f46f 1691
7bcb11d3 1692 Destroy();
c801d85f
KB
1693}
1694
1e5ad6e1 1695void wxPreviewFrame::InitializeWithModality(wxPreviewFrameModalityKind kind)
c801d85f 1696{
3080bf59 1697#if wxUSE_STATUSBAR
7bcb11d3 1698 CreateStatusBar();
3080bf59 1699#endif
7bcb11d3
JS
1700 CreateCanvas();
1701 CreateControlBar();
8826f46f 1702
7bcb11d3
JS
1703 m_printPreview->SetCanvas(m_previewCanvas);
1704 m_printPreview->SetFrame(this);
8826f46f 1705
9dff8515 1706 wxBoxSizer *item0 = new wxBoxSizer( wxVERTICAL );
8826f46f 1707
9dff8515
JS
1708 item0->Add( m_controlBar, 0, wxGROW|wxALIGN_CENTER_VERTICAL, 5 );
1709 item0->Add( m_previewCanvas, 1, wxGROW|wxALIGN_CENTER_VERTICAL, 5 );
8826f46f 1710
7e548f6b 1711 SetAutoLayout( true );
9dff8515 1712 SetSizer( item0 );
8826f46f 1713
6aacfc73
VZ
1714 m_modalityKind = kind;
1715 switch ( m_modalityKind )
1716 {
1717 case wxPreviewFrame_AppModal:
1718 // Disable everything.
1719 m_windowDisabler = new wxWindowDisabler( this );
1720 break;
1721
1722 case wxPreviewFrame_WindowModal:
1723 // Disable our parent if we have one.
1724 if ( GetParent() )
1725 GetParent()->Disable();
1726 break;
1727
1728 case wxPreviewFrame_NonModal:
1729 // Nothing to do, we don't need to disable any windows.
1730 break;
1731 }
1732
46aa0c9e
VS
1733 if ( m_modalityKind != wxPreviewFrame_NonModal )
1734 {
1735 // Behave like modal dialogs, don't show in taskbar. This implies
1736 // removing the minimize box, because minimizing windows without
1737 // taskbar entry is confusing.
6c0613fd 1738 SetWindowStyle((GetWindowStyle() & ~wxMINIMIZE_BOX) | wxFRAME_NO_TASKBAR);
46aa0c9e 1739 }
8826f46f 1740
7bcb11d3 1741 Layout();
e71fd398 1742
d2b354f9
JS
1743 m_printPreview->AdjustScrollbars(m_previewCanvas);
1744 m_previewCanvas->SetFocus();
1745 m_controlBar->SetFocus();
c801d85f
KB
1746}
1747
34da0970 1748void wxPreviewFrame::CreateCanvas()
c801d85f 1749{
7bcb11d3 1750 m_previewCanvas = new wxPreviewCanvas(m_printPreview, this);
c801d85f
KB
1751}
1752
34da0970 1753void wxPreviewFrame::CreateControlBar()
c801d85f 1754{
7bcb11d3
JS
1755 long buttons = wxPREVIEW_DEFAULT;
1756 if (m_printPreview->GetPrintoutForPrinting())
1757 buttons |= wxPREVIEW_PRINT;
8826f46f 1758
e562dfb1 1759 m_controlBar = new wxPreviewControlBar(m_printPreview, buttons, this);
7bcb11d3 1760 m_controlBar->CreateButtons();
c801d85f 1761}
7bcb11d3 1762
c801d85f 1763/*
7bcb11d3
JS
1764* Print preview
1765*/
c801d85f 1766
891daf8c
RR
1767IMPLEMENT_CLASS(wxPrintPreviewBase, wxObject)
1768
8826f46f
VZ
1769wxPrintPreviewBase::wxPrintPreviewBase(wxPrintout *printout,
1770 wxPrintout *printoutForPrinting,
1771 wxPrintData *data)
1772{
1773 if (data)
1774 m_printDialogData = (*data);
1775
1776 Init(printout, printoutForPrinting);
1777}
1778
1779wxPrintPreviewBase::wxPrintPreviewBase(wxPrintout *printout,
1780 wxPrintout *printoutForPrinting,
1781 wxPrintDialogData *data)
1782{
1783 if (data)
1784 m_printDialogData = (*data);
1785
1786 Init(printout, printoutForPrinting);
1787}
1788
1789void wxPrintPreviewBase::Init(wxPrintout *printout,
1790 wxPrintout *printoutForPrinting)
c801d85f 1791{
7e548f6b 1792 m_isOk = true;
7bcb11d3
JS
1793 m_previewPrintout = printout;
1794 if (m_previewPrintout)
1bd122dd 1795 m_previewPrintout->SetPreview(static_cast<wxPrintPreview *>(this));
8826f46f 1796
7bcb11d3 1797 m_printPrintout = printoutForPrinting;
8826f46f 1798
7bcb11d3
JS
1799 m_previewCanvas = NULL;
1800 m_previewFrame = NULL;
1801 m_previewBitmap = NULL;
88b98787 1802 m_previewFailed = false;
7bcb11d3 1803 m_currentPage = 1;
c25ccf85 1804 m_currentZoom = 70;
41e1c9d8
VZ
1805 m_topMargin =
1806 m_leftMargin = 2*wxSizerFlags::GetDefaultBorder();
7bcb11d3
JS
1807 m_pageWidth = 0;
1808 m_pageHeight = 0;
7e548f6b 1809 m_printingPrepared = false;
d2b354f9
JS
1810 m_minPage = 1;
1811 m_maxPage = 1;
c801d85f
KB
1812}
1813
34da0970 1814wxPrintPreviewBase::~wxPrintPreviewBase()
c801d85f 1815{
7bcb11d3
JS
1816 if (m_previewPrintout)
1817 delete m_previewPrintout;
1818 if (m_previewBitmap)
1819 delete m_previewBitmap;
1820 if (m_printPrintout)
1821 delete m_printPrintout;
c801d85f
KB
1822}
1823
1824bool wxPrintPreviewBase::SetCurrentPage(int pageNum)
1825{
7bcb11d3 1826 if (m_currentPage == pageNum)
7e548f6b 1827 return true;
8826f46f 1828
7bcb11d3 1829 m_currentPage = pageNum;
88b98787
VS
1830
1831 InvalidatePreviewBitmap();
e71fd398 1832
7bcb11d3
JS
1833 if (m_previewCanvas)
1834 {
d2b354f9 1835 AdjustScrollbars(m_previewCanvas);
e71fd398 1836
7bcb11d3 1837 m_previewCanvas->Refresh();
d2b354f9 1838 m_previewCanvas->SetFocus();
7bcb11d3 1839 }
7e548f6b 1840 return true;
c801d85f
KB
1841}
1842
383f6abd 1843int wxPrintPreviewBase::GetCurrentPage() const
259c43f6 1844 { return m_currentPage; }
383f6abd 1845void wxPrintPreviewBase::SetPrintout(wxPrintout *printout)
259c43f6 1846 { m_previewPrintout = printout; }
383f6abd 1847wxPrintout *wxPrintPreviewBase::GetPrintout() const
259c43f6 1848 { return m_previewPrintout; }
383f6abd 1849wxPrintout *wxPrintPreviewBase::GetPrintoutForPrinting() const
259c43f6 1850 { return m_printPrintout; }
383f6abd 1851void wxPrintPreviewBase::SetFrame(wxFrame *frame)
259c43f6 1852 { m_previewFrame = frame; }
383f6abd 1853void wxPrintPreviewBase::SetCanvas(wxPreviewCanvas *canvas)
259c43f6 1854 { m_previewCanvas = canvas; }
383f6abd 1855wxFrame *wxPrintPreviewBase::GetFrame() const
e81e3883 1856 { return m_previewFrame; }
383f6abd 1857wxPreviewCanvas *wxPrintPreviewBase::GetCanvas() const
e81e3883
RR
1858 { return m_previewCanvas; }
1859
f415cab9
JS
1860void wxPrintPreviewBase::CalcRects(wxPreviewCanvas *canvas, wxRect& pageRect, wxRect& paperRect)
1861{
1862 // Calculate the rectangles for the printable area of the page and the
1863 // entire paper as they appear on the canvas on-screen.
1864 int canvasWidth, canvasHeight;
1865 canvas->GetSize(&canvasWidth, &canvasHeight);
1866
1867 float zoomScale = float(m_currentZoom) / 100;
1868 float screenPrintableWidth = zoomScale * m_pageWidth * m_previewScaleX;
1869 float screenPrintableHeight = zoomScale * m_pageHeight * m_previewScaleY;
1870
1871 wxRect devicePaperRect = m_previewPrintout->GetPaperRectPixels();
1872 wxCoord devicePrintableWidth, devicePrintableHeight;
1873 m_previewPrintout->GetPageSizePixels(&devicePrintableWidth, &devicePrintableHeight);
1874 float scaleX = screenPrintableWidth / devicePrintableWidth;
1875 float scaleY = screenPrintableHeight / devicePrintableHeight;
1876 paperRect.width = wxCoord(scaleX * devicePaperRect.width);
1877 paperRect.height = wxCoord(scaleY * devicePaperRect.height);
1878
1879 paperRect.x = wxCoord((canvasWidth - paperRect.width)/ 2.0);
1880 if (paperRect.x < m_leftMargin)
1881 paperRect.x = m_leftMargin;
1882 paperRect.y = wxCoord((canvasHeight - paperRect.height)/ 2.0);
1883 if (paperRect.y < m_topMargin)
1884 paperRect.y = m_topMargin;
1885
1886 pageRect.x = paperRect.x - wxCoord(scaleX * devicePaperRect.x);
1887 pageRect.y = paperRect.y - wxCoord(scaleY * devicePaperRect.y);
1888 pageRect.width = wxCoord(screenPrintableWidth);
1889 pageRect.height = wxCoord(screenPrintableHeight);
1890}
1891
1892
88b98787
VS
1893void wxPrintPreviewBase::InvalidatePreviewBitmap()
1894{
1895 wxDELETE(m_previewBitmap);
1896 // if there was a problem with rendering the preview, try again now
1897 // that it changed in some way (less memory may be needed, for example):
1898 m_previewFailed = false;
1899}
1900
b88bf073
VS
1901bool wxPrintPreviewBase::UpdatePageRendering()
1902{
1903 if ( m_previewBitmap )
1904 return false;
1905
88b98787
VS
1906 if ( m_previewFailed )
1907 return false;
1908
b88bf073 1909 if ( !RenderPage(m_currentPage) )
88b98787
VS
1910 {
1911 m_previewFailed = true; // don't waste time failing again
b88bf073 1912 return false;
88b98787 1913 }
b88bf073
VS
1914
1915 return true;
1916}
1917
d2b354f9 1918bool wxPrintPreviewBase::PaintPage(wxPreviewCanvas *canvas, wxDC& dc)
c801d85f 1919{
7bcb11d3 1920 DrawBlankPage(canvas, dc);
8826f46f 1921
7bcb11d3 1922 if (!m_previewBitmap)
7e548f6b 1923 return false;
7bcb11d3 1924 if (!canvas)
7e548f6b 1925 return false;
8826f46f 1926
f415cab9
JS
1927 wxRect pageRect, paperRect;
1928 CalcRects(canvas, pageRect, paperRect);
7bcb11d3
JS
1929 wxMemoryDC temp_dc;
1930 temp_dc.SelectObject(*m_previewBitmap);
8826f46f 1931
03647350 1932 dc.Blit(pageRect.x, pageRect.y,
f415cab9 1933 m_previewBitmap->GetWidth(), m_previewBitmap->GetHeight(), &temp_dc, 0, 0);
8826f46f 1934
7bcb11d3 1935 temp_dc.SelectObject(wxNullBitmap);
7e548f6b 1936 return true;
c801d85f
KB
1937}
1938
d2b354f9
JS
1939// Adjusts the scrollbars for the current scale
1940void wxPrintPreviewBase::AdjustScrollbars(wxPreviewCanvas *canvas)
1941{
1942 if (!canvas)
1943 return ;
1944
f415cab9
JS
1945 wxRect pageRect, paperRect;
1946 CalcRects(canvas, pageRect, paperRect);
1947 int totalWidth = paperRect.width + 2 * m_leftMargin;
1948 int totalHeight = paperRect.height + 2 * m_topMargin;
1949 int scrollUnitsX = totalWidth / 10;
1950 int scrollUnitsY = totalHeight / 10;
d2b354f9
JS
1951 wxSize virtualSize = canvas->GetVirtualSize();
1952 if (virtualSize.GetWidth() != totalWidth || virtualSize.GetHeight() != totalHeight)
7e548f6b 1953 canvas->SetScrollbars(10, 10, scrollUnitsX, scrollUnitsY, 0, 0, true);
d2b354f9
JS
1954}
1955
25a3fca2
VS
1956bool wxPrintPreviewBase::RenderPageIntoDC(wxDC& dc, int pageNum)
1957{
1958 m_previewPrintout->SetDC(&dc);
1959 m_previewPrintout->SetPageSizePixels(m_pageWidth, m_pageHeight);
1960
1961 // Need to delay OnPreparePrinting() until here, so we have enough
d7cfeeb4 1962 // information and a wxDC.
25a3fca2
VS
1963 if (!m_printingPrepared)
1964 {
d7cfeeb4
VZ
1965 m_printingPrepared = true;
1966
25a3fca2
VS
1967 m_previewPrintout->OnPreparePrinting();
1968 int selFrom, selTo;
1969 m_previewPrintout->GetPageInfo(&m_minPage, &m_maxPage, &selFrom, &selTo);
d7cfeeb4
VZ
1970
1971 // Update the wxPreviewControlBar page range display.
1972 if ( m_previewFrame )
1973 {
1974 wxPreviewControlBar * const
1975 controlBar = ((wxPreviewFrame*)m_previewFrame)->GetControlBar();
1976 if ( controlBar )
1977 controlBar->SetPageInfo(m_minPage, m_maxPage);
1978 }
25a3fca2
VS
1979 }
1980
1981 m_previewPrintout->OnBeginPrinting();
1982
1983 if (!m_previewPrintout->OnBeginDocument(m_printDialogData.GetFromPage(), m_printDialogData.GetToPage()))
1984 {
1985 wxMessageBox(_("Could not start document preview."), _("Print Preview Failure"), wxOK);
1986 return false;
1987 }
1988
1989 m_previewPrintout->OnPrintPage(pageNum);
1990 m_previewPrintout->OnEndDocument();
1991 m_previewPrintout->OnEndPrinting();
1992
1993 m_previewPrintout->SetDC(NULL);
1994
1995 return true;
1996}
1997
1998bool wxPrintPreviewBase::RenderPageIntoBitmap(wxBitmap& bmp, int pageNum)
1999{
2000 wxMemoryDC memoryDC;
2001 memoryDC.SelectObject(bmp);
2002 memoryDC.Clear();
2003
2004 return RenderPageIntoDC(memoryDC, pageNum);
2005}
2006
c801d85f
KB
2007bool wxPrintPreviewBase::RenderPage(int pageNum)
2008{
f6bcfd97
BP
2009 wxBusyCursor busy;
2010
7bcb11d3 2011 if (!m_previewCanvas)
c801d85f 2012 {
9a83f860 2013 wxFAIL_MSG(wxT("wxPrintPreviewBase::RenderPage: must use wxPrintPreviewBase::SetCanvas to let me know about the canvas!"));
7e548f6b 2014 return false;
c801d85f 2015 }
8826f46f 2016
f415cab9
JS
2017 wxRect pageRect, paperRect;
2018 CalcRects(m_previewCanvas, pageRect, paperRect);
8826f46f 2019
7bcb11d3
JS
2020 if (!m_previewBitmap)
2021 {
f415cab9
JS
2022 m_previewBitmap = new wxBitmap(pageRect.width, pageRect.height);
2023
a1b806b9 2024 if (!m_previewBitmap || !m_previewBitmap->IsOk())
7bcb11d3 2025 {
88b98787 2026 InvalidatePreviewBitmap();
7bcb11d3 2027 wxMessageBox(_("Sorry, not enough memory to create a preview."), _("Print Preview Failure"), wxOK);
7e548f6b 2028 return false;
7bcb11d3
JS
2029 }
2030 }
8826f46f 2031
25a3fca2 2032 if ( !RenderPageIntoBitmap(*m_previewBitmap, pageNum) )
7bcb11d3 2033 {
88b98787 2034 InvalidatePreviewBitmap();
25a3fca2 2035 wxMessageBox(_("Sorry, not enough memory to create a preview."), _("Print Preview Failure"), wxOK);
7e548f6b 2036 return false;
7bcb11d3 2037 }
8826f46f 2038
3080bf59
VZ
2039#if wxUSE_STATUSBAR
2040 wxString status;
7bcb11d3 2041 if (m_maxPage != 0)
7e548f6b 2042 status = wxString::Format(_("Page %d of %d"), pageNum, m_maxPage);
7bcb11d3 2043 else
7e548f6b 2044 status = wxString::Format(_("Page %d"), pageNum);
8826f46f 2045
7bcb11d3 2046 if (m_previewFrame)
3080bf59
VZ
2047 m_previewFrame->SetStatusText(status);
2048#endif
8826f46f 2049
7e548f6b 2050 return true;
c801d85f
KB
2051}
2052
d2b354f9 2053bool wxPrintPreviewBase::DrawBlankPage(wxPreviewCanvas *canvas, wxDC& dc)
c801d85f 2054{
f415cab9 2055 wxRect pageRect, paperRect;
8826f46f 2056
f415cab9 2057 CalcRects(canvas, pageRect, paperRect);
8826f46f 2058
f415cab9
JS
2059 // Draw shadow, allowing for 1-pixel border AROUND the actual paper
2060 wxCoord shadowOffset = 4;
8826f46f 2061
7bcb11d3
JS
2062 dc.SetPen(*wxBLACK_PEN);
2063 dc.SetBrush(*wxBLACK_BRUSH);
f415cab9
JS
2064 dc.DrawRectangle(paperRect.x + shadowOffset, paperRect.y + paperRect.height + 1,
2065 paperRect.width, shadowOffset);
2066
2067 dc.DrawRectangle(paperRect.x + paperRect.width, paperRect.y + shadowOffset,
2068 shadowOffset, paperRect.height);
8826f46f 2069
f415cab9 2070 // Draw blank page allowing for 1-pixel border AROUND the actual paper
7bcb11d3
JS
2071 dc.SetPen(*wxBLACK_PEN);
2072 dc.SetBrush(*wxWHITE_BRUSH);
03647350 2073 dc.DrawRectangle(paperRect.x - 2, paperRect.y - 1,
f415cab9 2074 paperRect.width + 3, paperRect.height + 2);
8826f46f 2075
7e548f6b 2076 return true;
2a47d3c1
JS
2077}
2078
7bcb11d3 2079void wxPrintPreviewBase::SetZoom(int percent)
2a47d3c1 2080{
7bcb11d3
JS
2081 if (m_currentZoom == percent)
2082 return;
8826f46f 2083
7bcb11d3 2084 m_currentZoom = percent;
88b98787
VS
2085
2086 InvalidatePreviewBitmap();
aff37a19 2087
7bcb11d3
JS
2088 if (m_previewCanvas)
2089 {
d2b354f9 2090 AdjustScrollbars(m_previewCanvas);
95724b1a 2091 ((wxScrolledWindow *) m_previewCanvas)->Scroll(0, 0);
e71fd398 2092 m_previewCanvas->ClearBackground();
7bcb11d3 2093 m_previewCanvas->Refresh();
d2b354f9 2094 m_previewCanvas->SetFocus();
7bcb11d3 2095 }
2a47d3c1
JS
2096}
2097
383f6abd
WS
2098wxPrintDialogData& wxPrintPreviewBase::GetPrintDialogData()
2099{
e81e3883
RR
2100 return m_printDialogData;
2101}
2102
383f6abd 2103int wxPrintPreviewBase::GetZoom() const
e81e3883 2104{ return m_currentZoom; }
383f6abd 2105int wxPrintPreviewBase::GetMaxPage() const
e81e3883 2106{ return m_maxPage; }
383f6abd 2107int wxPrintPreviewBase::GetMinPage() const
e81e3883 2108{ return m_minPage; }
b7cacb43 2109bool wxPrintPreviewBase::IsOk() const
e81e3883 2110{ return m_isOk; }
383f6abd 2111void wxPrintPreviewBase::SetOk(bool ok)
e81e3883 2112{ m_isOk = ok; }
f415cab9 2113
e81e3883
RR
2114//----------------------------------------------------------------------------
2115// wxPrintPreview
2116//----------------------------------------------------------------------------
2117
2118IMPLEMENT_CLASS(wxPrintPreview, wxPrintPreviewBase)
2119
2120wxPrintPreview::wxPrintPreview(wxPrintout *printout,
2121 wxPrintout *printoutForPrinting,
2122 wxPrintDialogData *data) :
2123 wxPrintPreviewBase( printout, printoutForPrinting, data )
2124{
2125 m_pimpl = wxPrintFactory::GetFactory()->
2126 CreatePrintPreview( printout, printoutForPrinting, data );
2127}
2128
2129wxPrintPreview::wxPrintPreview(wxPrintout *printout,
2130 wxPrintout *printoutForPrinting,
2131 wxPrintData *data ) :
2132 wxPrintPreviewBase( printout, printoutForPrinting, data )
2133{
2134 m_pimpl = wxPrintFactory::GetFactory()->
2135 CreatePrintPreview( printout, printoutForPrinting, data );
2136}
2137
2138wxPrintPreview::~wxPrintPreview()
2139{
2140 delete m_pimpl;
383f6abd 2141
e81e3883
RR
2142 // don't delete twice
2143 m_printPrintout = NULL;
2144 m_previewPrintout = NULL;
2145 m_previewBitmap = NULL;
2146}
2147
2148bool wxPrintPreview::SetCurrentPage(int pageNum)
2149{
2150 return m_pimpl->SetCurrentPage( pageNum );
2151}
2152
383f6abd
WS
2153int wxPrintPreview::GetCurrentPage() const
2154{
e81e3883
RR
2155 return m_pimpl->GetCurrentPage();
2156}
2157
383f6abd
WS
2158void wxPrintPreview::SetPrintout(wxPrintout *printout)
2159{
e81e3883
RR
2160 m_pimpl->SetPrintout( printout );
2161}
2162
383f6abd
WS
2163wxPrintout *wxPrintPreview::GetPrintout() const
2164{
e81e3883
RR
2165 return m_pimpl->GetPrintout();
2166}
2167
383f6abd
WS
2168wxPrintout *wxPrintPreview::GetPrintoutForPrinting() const
2169{
e81e3883
RR
2170 return m_pimpl->GetPrintoutForPrinting();
2171}
2172
383f6abd
WS
2173void wxPrintPreview::SetFrame(wxFrame *frame)
2174{
e81e3883
RR
2175 m_pimpl->SetFrame( frame );
2176}
2177
383f6abd
WS
2178void wxPrintPreview::SetCanvas(wxPreviewCanvas *canvas)
2179{
e81e3883
RR
2180 m_pimpl->SetCanvas( canvas );
2181}
2182
383f6abd 2183wxFrame *wxPrintPreview::GetFrame() const
e81e3883
RR
2184{
2185 return m_pimpl->GetFrame();
2186}
2187
383f6abd
WS
2188wxPreviewCanvas *wxPrintPreview::GetCanvas() const
2189{
e81e3883
RR
2190 return m_pimpl->GetCanvas();
2191}
2192
2193bool wxPrintPreview::PaintPage(wxPreviewCanvas *canvas, wxDC& dc)
2194{
2195 return m_pimpl->PaintPage( canvas, dc );
2196}
2197
b88bf073
VS
2198bool wxPrintPreview::UpdatePageRendering()
2199{
2200 return m_pimpl->UpdatePageRendering();
2201}
2202
e81e3883
RR
2203bool wxPrintPreview::DrawBlankPage(wxPreviewCanvas *canvas, wxDC& dc)
2204{
2205 return m_pimpl->DrawBlankPage( canvas, dc );
2206}
2207
2208void wxPrintPreview::AdjustScrollbars(wxPreviewCanvas *canvas)
2209{
2210 m_pimpl->AdjustScrollbars( canvas );
2211}
2212
2213bool wxPrintPreview::RenderPage(int pageNum)
2214{
2215 return m_pimpl->RenderPage( pageNum );
2216}
2217
2218void wxPrintPreview::SetZoom(int percent)
2219{
2220 m_pimpl->SetZoom( percent );
2221}
2222
d8766675
JS
2223int wxPrintPreview::GetZoom() const
2224{
2225 return m_pimpl->GetZoom();
2226}
2227
e81e3883
RR
2228wxPrintDialogData& wxPrintPreview::GetPrintDialogData()
2229{
2230 return m_pimpl->GetPrintDialogData();
2231}
2232
2233int wxPrintPreview::GetMaxPage() const
2234{
2235 return m_pimpl->GetMaxPage();
2236}
2237
2238int wxPrintPreview::GetMinPage() const
2239{
2240 return m_pimpl->GetMinPage();
2241}
2242
b7cacb43 2243bool wxPrintPreview::IsOk() const
e81e3883 2244{
a1b806b9 2245 return m_pimpl->IsOk();
e81e3883
RR
2246}
2247
2248void wxPrintPreview::SetOk(bool ok)
2249{
2250 m_pimpl->SetOk( ok );
2251}
2252
2253bool wxPrintPreview::Print(bool interactive)
2254{
2255 return m_pimpl->Print( interactive );
2256}
2257
2258void wxPrintPreview::DetermineScaling()
2259{
2260 m_pimpl->DetermineScaling();
2261}
2262
d427503c 2263#endif // wxUSE_PRINTING_ARCHITECTURE