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