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