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