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