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