]> git.saurik.com Git - wxWidgets.git/blame - src/common/prntbase.cpp
Update version to 2.9.4 in version.bkl too and rebake everything.
[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
57bd4c60
WS
24 #if defined(__WXMSW__)
25 #include "wx/msw/wrapcdlg.h"
26 #endif // MSW
8898456d
WS
27 #include "wx/utils.h"
28 #include "wx/dc.h"
29 #include "wx/app.h"
5a70d3f5 30 #include "wx/math.h"
8898456d
WS
31 #include "wx/msgdlg.h"
32 #include "wx/layout.h"
33 #include "wx/choice.h"
34 #include "wx/button.h"
424da8bd 35 #include "wx/bmpbuttn.h"
8898456d
WS
36 #include "wx/settings.h"
37 #include "wx/dcmemory.h"
23205be8 38 #include "wx/dcclient.h"
8898456d
WS
39 #include "wx/stattext.h"
40 #include "wx/intl.h"
41 #include "wx/textdlg.h"
42 #include "wx/sizer.h"
02761f6c 43 #include "wx/module.h"
2b5f62a0 44#endif // !WX_PRECOMP
c801d85f
KB
45
46#include "wx/prntbase.h"
c801d85f 47#include "wx/printdlg.h"
e81e3883 48#include "wx/print.h"
147bf263 49#include "wx/dcprint.h"
82579461 50#include "wx/artprov.h"
c801d85f
KB
51
52#include <stdlib.h>
53#include <string.h>
54
891daf8c
RR
55#if defined(__WXMSW__) && !defined(__WXUNIVERSAL__)
56#include "wx/msw/printdlg.h"
888dde65 57#include "wx/msw/dcprint.h"
891daf8c 58#elif defined(__WXMAC__)
c933e267
SC
59#include "wx/osx/printdlg.h"
60#include "wx/osx/private/print.h"
61#include "wx/osx/dcprint.h"
7c71eb6a
SN
62#elif defined(__WXPM__)
63#include "wx/os2/dcprint.h"
64#include "wx/generic/prntdlgg.h"
891daf8c
RR
65#else
66#include "wx/generic/prntdlgg.h"
147bf263 67#include "wx/dcps.h"
891daf8c
RR
68#endif
69
2049ba38 70#ifdef __WXMSW__
d427503c
VZ
71 #ifndef __WIN32__
72 #include <print.h>
73 #endif
74#endif // __WXMSW__
c801d85f 75
e81e3883
RR
76//----------------------------------------------------------------------------
77// wxPrintFactory
78//----------------------------------------------------------------------------
79
80wxPrintFactory *wxPrintFactory::m_factory = NULL;
383f6abd 81
e81e3883 82void wxPrintFactory::SetPrintFactory( wxPrintFactory *factory )
383f6abd
WS
83{
84 if (wxPrintFactory::m_factory)
e81e3883 85 delete wxPrintFactory::m_factory;
383f6abd
WS
86
87 wxPrintFactory::m_factory = factory;
e81e3883 88}
c801d85f 89
e81e3883
RR
90wxPrintFactory *wxPrintFactory::GetFactory()
91{
92 if (!wxPrintFactory::m_factory)
383f6abd 93 wxPrintFactory::m_factory = new wxNativePrintFactory;
c801d85f 94
e81e3883
RR
95 return wxPrintFactory::m_factory;
96}
97
98//----------------------------------------------------------------------------
99// wxNativePrintFactory
100//----------------------------------------------------------------------------
101
102wxPrinterBase *wxNativePrintFactory::CreatePrinter( wxPrintDialogData *data )
383f6abd
WS
103{
104#if defined(__WXMSW__) && !defined(__WXUNIVERSAL__)
e81e3883
RR
105 return new wxWindowsPrinter( data );
106#elif defined(__WXMAC__)
107 return new wxMacPrinter( data );
21708c73
SN
108#elif defined(__WXPM__)
109 return new wxOS2Printer( data );
e81e3883
RR
110#else
111 return new wxPostScriptPrinter( data );
112#endif
259c43f6 113}
e81e3883 114
383f6abd 115wxPrintPreviewBase *wxNativePrintFactory::CreatePrintPreview( wxPrintout *preview,
e81e3883
RR
116 wxPrintout *printout, wxPrintDialogData *data )
117{
383f6abd 118#if defined(__WXMSW__) && !defined(__WXUNIVERSAL__)
e81e3883
RR
119 return new wxWindowsPrintPreview( preview, printout, data );
120#elif defined(__WXMAC__)
121 return new wxMacPrintPreview( preview, printout, data );
21708c73
SN
122#elif defined(__WXPM__)
123 return new wxOS2PrintPreview( preview, printout, data );
e81e3883
RR
124#else
125 return new wxPostScriptPrintPreview( preview, printout, data );
126#endif
127}
128
383f6abd 129wxPrintPreviewBase *wxNativePrintFactory::CreatePrintPreview( wxPrintout *preview,
e81e3883
RR
130 wxPrintout *printout, wxPrintData *data )
131{
383f6abd 132#if defined(__WXMSW__) && !defined(__WXUNIVERSAL__)
e81e3883
RR
133 return new wxWindowsPrintPreview( preview, printout, data );
134#elif defined(__WXMAC__)
135 return new wxMacPrintPreview( preview, printout, data );
21708c73
SN
136#elif defined(__WXPM__)
137 return new wxOS2PrintPreview( preview, printout, data );
e81e3883
RR
138#else
139 return new wxPostScriptPrintPreview( preview, printout, data );
140#endif
141}
142
2997ca30 143wxPrintDialogBase *wxNativePrintFactory::CreatePrintDialog( wxWindow *parent,
c061373d
RR
144 wxPrintDialogData *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
2997ca30 155wxPrintDialogBase *wxNativePrintFactory::CreatePrintDialog( wxWindow *parent,
c061373d
RR
156 wxPrintData *data )
157{
158#if defined(__WXMSW__) && !defined(__WXUNIVERSAL__)
159 return new wxWindowsPrintDialog( parent, data );
160#elif defined(__WXMAC__)
161 return new wxMacPrintDialog( parent, data );
162#else
163 return new wxGenericPrintDialog( parent, data );
164#endif
165}
166
2997ca30 167wxPageSetupDialogBase *wxNativePrintFactory::CreatePageSetupDialog( wxWindow *parent,
891daf8c
RR
168 wxPageSetupDialogData *data )
169{
170#if defined(__WXMSW__) && !defined(__WXUNIVERSAL__)
171 return new wxWindowsPageSetupDialog( parent, data );
172#elif defined(__WXMAC__)
173 return new wxMacPageSetupDialog( parent, data );
174#else
175 return new wxGenericPageSetupDialog( parent, data );
176#endif
177}
178
6038ec8e
RR
179bool wxNativePrintFactory::HasPrintSetupDialog()
180{
181#if defined(__WXMSW__) && !defined(__WXUNIVERSAL__)
182 return false;
183#elif defined(__WXMAC__)
184 return false;
185#else
186 // Only here do we need to provide the print setup
187 // dialog ourselves, the other platforms either have
188 // none, don't make it accessible or let you configure
189 // the printer from the wxPrintDialog anyway.
190 return true;
191#endif
2997ca30 192
6038ec8e
RR
193}
194
2997ca30 195wxDialog *wxNativePrintFactory::CreatePrintSetupDialog( wxWindow *parent,
891daf8c 196 wxPrintData *data )
6038ec8e
RR
197{
198#if defined(__WXMSW__) && !defined(__WXUNIVERSAL__)
ce403057
WS
199 wxUnusedVar(parent);
200 wxUnusedVar(data);
6038ec8e
RR
201 return NULL;
202#elif defined(__WXMAC__)
ce403057
WS
203 wxUnusedVar(parent);
204 wxUnusedVar(data);
6038ec8e
RR
205 return NULL;
206#else
207 // Only here do we need to provide the print setup
208 // dialog ourselves, the other platforms either have
209 // none, don't make it accessible or let you configure
210 // the printer from the wxPrintDialog anyway.
211 return new wxGenericPrintSetupDialog( parent, data );
212#endif
213}
214
888dde65 215wxDCImpl* wxNativePrintFactory::CreatePrinterDCImpl( wxPrinterDC *owner, const wxPrintData& data )
147bf263 216{
26eef304 217#if defined(__WXGTK__) || defined(__WXMOTIF__) || ( defined(__WXUNIVERSAL__) && !defined(__WXMAC__) )
888dde65 218 return new wxPostScriptDCImpl( owner, data );
147bf263 219#else
888dde65 220 return new wxPrinterDCImpl( owner, data );
147bf263
JS
221#endif
222}
223
6038ec8e
RR
224bool wxNativePrintFactory::HasOwnPrintToFile()
225{
226 // Only relevant for PostScript and here the
2997ca30 227 // setup dialog provides no "print to file"
6038ec8e
RR
228 // option. In the GNOME setup dialog, the
229 // setup dialog has its own print to file.
230 return false;
231}
232
233bool wxNativePrintFactory::HasPrinterLine()
234{
235 // Only relevant for PostScript for now
236 return true;
237}
238
239wxString wxNativePrintFactory::CreatePrinterLine()
240{
241 // Only relevant for PostScript for now
2997ca30 242
6038ec8e
RR
243 // We should query "lpstat -d" here
244 return _("Generic PostScript");
245}
246
247bool wxNativePrintFactory::HasStatusLine()
248{
249 // Only relevant for PostScript for now
2997ca30 250 return true;
6038ec8e
RR
251}
252
253wxString wxNativePrintFactory::CreateStatusLine()
254{
255 // Only relevant for PostScript for now
2997ca30 256
6038ec8e
RR
257 // We should query "lpstat -r" or "lpstat -p" here
258 return _("Ready");
259}
260
8850cbd3
RR
261wxPrintNativeDataBase *wxNativePrintFactory::CreatePrintNativeData()
262{
263#if defined(__WXMSW__) && !defined(__WXUNIVERSAL__)
264 return new wxWindowsPrintNativeData;
265#elif defined(__WXMAC__)
c347101b 266 return wxOSXCreatePrintData();
8850cbd3
RR
267#else
268 return new wxPostScriptPrintNativeData;
269#endif
270}
271
272//----------------------------------------------------------------------------
273// wxPrintNativeDataBase
274//----------------------------------------------------------------------------
275
276IMPLEMENT_ABSTRACT_CLASS(wxPrintNativeDataBase, wxObject)
277
278wxPrintNativeDataBase::wxPrintNativeDataBase()
2997ca30
WS
279{
280 m_ref = 1;
8850cbd3
RR
281}
282
0ab2de15
RR
283//----------------------------------------------------------------------------
284// wxPrintFactoryModule
285//----------------------------------------------------------------------------
286
287class wxPrintFactoryModule: public wxModule
288{
289public:
290 wxPrintFactoryModule() {}
291 bool OnInit() { return true; }
292 void OnExit() { wxPrintFactory::SetPrintFactory( NULL ); }
2997ca30 293
0ab2de15
RR
294private:
295 DECLARE_DYNAMIC_CLASS(wxPrintFactoryModule)
296};
297
298IMPLEMENT_DYNAMIC_CLASS(wxPrintFactoryModule, wxModule)
299
e81e3883
RR
300//----------------------------------------------------------------------------
301// wxPrinterBase
302//----------------------------------------------------------------------------
303
304IMPLEMENT_CLASS(wxPrinterBase, wxObject)
7bcb11d3
JS
305
306wxPrinterBase::wxPrinterBase(wxPrintDialogData *data)
c801d85f 307{
d3b9f782
VZ
308 m_currentPrintout = NULL;
309 sm_abortWindow = NULL;
7e548f6b 310 sm_abortIt = false;
7bcb11d3
JS
311 if (data)
312 m_printDialogData = (*data);
f6bcfd97 313 sm_lastError = wxPRINTER_NO_ERROR;
c801d85f
KB
314}
315
d3b9f782 316wxWindow *wxPrinterBase::sm_abortWindow = NULL;
7e548f6b 317bool wxPrinterBase::sm_abortIt = false;
f6bcfd97 318wxPrinterError wxPrinterBase::sm_lastError = wxPRINTER_NO_ERROR;
c801d85f 319
34da0970 320wxPrinterBase::~wxPrinterBase()
c801d85f
KB
321{
322}
323
fc799548 324wxWindow *wxPrinterBase::CreateAbortWindow(wxWindow *parent, wxPrintout * printout)
c801d85f 325{
fc799548 326 wxPrintAbortDialog *dialog = new wxPrintAbortDialog(parent, _("Printing ") , wxDefaultPosition, wxDefaultSize, wxDEFAULT_DIALOG_STYLE);
8826f46f 327
fc799548 328 wxBoxSizer *button_sizer = new wxBoxSizer( wxVERTICAL );
7e548f6b 329 button_sizer->Add( new wxStaticText(dialog, wxID_ANY, _("Please wait while printing\n") + printout->GetTitle() ), 0, wxALL, 10 );
fc799548 330 button_sizer->Add( new wxButton( dialog, wxID_CANCEL, wxT("Cancel") ), 0, wxALL | wxALIGN_CENTER, 10 );
8826f46f 331
7e548f6b 332 dialog->SetAutoLayout( true );
fc799548
JS
333 dialog->SetSizer( button_sizer );
334
335 button_sizer->Fit(dialog);
336 button_sizer->SetSizeHints (dialog) ;
8826f46f 337
7bcb11d3 338 return dialog;
c801d85f
KB
339}
340
161f4f73 341void wxPrinterBase::ReportError(wxWindow *parent, wxPrintout *WXUNUSED(printout), const wxString& message)
c801d85f 342{
7bcb11d3 343 wxMessageBox(message, _("Printing Error"), wxOK, parent);
c801d85f
KB
344}
345
c061373d
RR
346wxPrintDialogData& wxPrinterBase::GetPrintDialogData() const
347{
348 return (wxPrintDialogData&) m_printDialogData;
349}
350
e81e3883
RR
351//----------------------------------------------------------------------------
352// wxPrinter
353//----------------------------------------------------------------------------
354
355IMPLEMENT_CLASS(wxPrinter, wxPrinterBase)
356
357wxPrinter::wxPrinter(wxPrintDialogData *data)
358{
359 m_pimpl = wxPrintFactory::GetFactory()->CreatePrinter( data );
360}
361
362wxPrinter::~wxPrinter()
363{
364 delete m_pimpl;
365}
366
367wxWindow *wxPrinter::CreateAbortWindow(wxWindow *parent, wxPrintout *printout)
368{
369 return m_pimpl->CreateAbortWindow( parent, printout );
370}
371
372void wxPrinter::ReportError(wxWindow *parent, wxPrintout *printout, const wxString& message)
373{
374 m_pimpl->ReportError( parent, printout, message );
375}
376
377bool wxPrinter::Setup(wxWindow *parent)
378{
379 return m_pimpl->Setup( parent );
380}
381
382bool wxPrinter::Print(wxWindow *parent, wxPrintout *printout, bool prompt)
383{
32eec436
VZ
384 if ( !prompt && m_printDialogData.GetToPage() == 0 )
385 {
386 // If the dialog is not shown, set the pages range to print everything
387 // by default (as otherwise we wouldn't print anything at all which is
388 // certainly not a reasonable default behaviour).
389 int minPage, maxPage, selFrom, selTo;
390 printout->GetPageInfo(&minPage, &maxPage, &selFrom, &selTo);
391
392 wxPrintDialogData& pdd = m_pimpl->GetPrintDialogData();
393 pdd.SetFromPage(minPage);
394 pdd.SetToPage(maxPage);
395 }
396
e81e3883
RR
397 return m_pimpl->Print( parent, printout, prompt );
398}
399
400wxDC* wxPrinter::PrintDialog(wxWindow *parent)
401{
402 return m_pimpl->PrintDialog( parent );
403}
404
c061373d
RR
405wxPrintDialogData& wxPrinter::GetPrintDialogData() const
406{
407 return m_pimpl->GetPrintDialogData();
408}
409
410// ---------------------------------------------------------------------------
891daf8c 411// wxPrintDialogBase: the dialog for printing.
c061373d
RR
412// ---------------------------------------------------------------------------
413
891daf8c 414IMPLEMENT_ABSTRACT_CLASS(wxPrintDialogBase, wxDialog)
c061373d 415
6ce8c562 416wxPrintDialogBase::wxPrintDialogBase(wxWindow *parent,
2997ca30 417 wxWindowID id,
6ce8c562
VZ
418 const wxString &title,
419 const wxPoint &pos,
420 const wxSize &size,
421 long style)
891daf8c
RR
422 : wxDialog( parent, id, title.empty() ? wxString(_("Print")) : title,
423 pos, size, style )
c061373d
RR
424{
425}
426
427// ---------------------------------------------------------------------------
891daf8c 428// wxPrintDialog: the dialog for printing
c061373d
RR
429// ---------------------------------------------------------------------------
430
431IMPLEMENT_CLASS(wxPrintDialog, wxObject)
432
433wxPrintDialog::wxPrintDialog(wxWindow *parent, wxPrintDialogData* data)
434{
435 m_pimpl = wxPrintFactory::GetFactory()->CreatePrintDialog( parent, data );
436}
437
438wxPrintDialog::wxPrintDialog(wxWindow *parent, wxPrintData* data)
439{
440 m_pimpl = wxPrintFactory::GetFactory()->CreatePrintDialog( parent, data );
441}
442
443wxPrintDialog::~wxPrintDialog()
444{
445 delete m_pimpl;
446}
447
448int wxPrintDialog::ShowModal()
449{
450 return m_pimpl->ShowModal();
451}
452
453wxPrintDialogData& wxPrintDialog::GetPrintDialogData()
454{
455 return m_pimpl->GetPrintDialogData();
456}
457
458wxPrintData& wxPrintDialog::GetPrintData()
459{
460 return m_pimpl->GetPrintData();
461}
891daf8c 462
c061373d
RR
463wxDC *wxPrintDialog::GetPrintDC()
464{
465 return m_pimpl->GetPrintDC();
466}
467
891daf8c
RR
468// ---------------------------------------------------------------------------
469// wxPageSetupDialogBase: the page setup dialog
470// ---------------------------------------------------------------------------
471
472IMPLEMENT_ABSTRACT_CLASS(wxPageSetupDialogBase, wxDialog)
473
474wxPageSetupDialogBase::wxPageSetupDialogBase(wxWindow *parent,
2997ca30 475 wxWindowID id,
891daf8c
RR
476 const wxString &title,
477 const wxPoint &pos,
478 const wxSize &size,
479 long style)
480 : wxDialog( parent, id, title.empty() ? wxString(_("Page setup")) : title,
481 pos, size, style )
482{
483}
484
485// ---------------------------------------------------------------------------
486// wxPageSetupDialog: the page setup dialog
487// ---------------------------------------------------------------------------
488
489IMPLEMENT_CLASS(wxPageSetupDialog, wxObject)
490
491wxPageSetupDialog::wxPageSetupDialog(wxWindow *parent, wxPageSetupDialogData *data )
492{
493 m_pimpl = wxPrintFactory::GetFactory()->CreatePageSetupDialog( parent, data );
494}
495
496wxPageSetupDialog::~wxPageSetupDialog()
497{
498 delete m_pimpl;
499}
500
501int wxPageSetupDialog::ShowModal()
502{
503 return m_pimpl->ShowModal();
504}
505
506wxPageSetupDialogData& wxPageSetupDialog::GetPageSetupDialogData()
507{
508 return m_pimpl->GetPageSetupDialogData();
509}
510
511// old name
512wxPageSetupDialogData& wxPageSetupDialog::GetPageSetupData()
513{
514 return m_pimpl->GetPageSetupDialogData();
515}
516
e81e3883
RR
517//----------------------------------------------------------------------------
518// wxPrintAbortDialog
519//----------------------------------------------------------------------------
520
521BEGIN_EVENT_TABLE(wxPrintAbortDialog, wxDialog)
522 EVT_BUTTON(wxID_CANCEL, wxPrintAbortDialog::OnCancel)
523END_EVENT_TABLE()
524
525void wxPrintAbortDialog::OnCancel(wxCommandEvent& WXUNUSED(event))
526{
527 wxPrinterBase::sm_abortIt = true;
528 wxPrinterBase::sm_abortWindow->Show(false);
529 wxPrinterBase::sm_abortWindow->Close(true);
861eef72 530 wxPrinterBase::sm_abortWindow->Destroy();
d3b9f782 531 wxPrinterBase::sm_abortWindow = NULL;
e81e3883
RR
532}
533
534//----------------------------------------------------------------------------
535// wxPrintout
536//----------------------------------------------------------------------------
537
538IMPLEMENT_ABSTRACT_CLASS(wxPrintout, wxObject)
7bcb11d3 539
34da0970 540wxPrintout::wxPrintout(const wxString& title)
c801d85f 541{
7bcb11d3 542 m_printoutTitle = title ;
d3b9f782 543 m_printoutDC = NULL;
7bcb11d3
JS
544 m_pageWidthMM = 0;
545 m_pageHeightMM = 0;
546 m_pageWidthPixels = 0;
547 m_pageHeightPixels = 0;
548 m_PPIScreenX = 0;
549 m_PPIScreenY = 0;
550 m_PPIPrinterX = 0;
551 m_PPIPrinterY = 0;
1bd122dd 552 m_preview = NULL;
c801d85f
KB
553}
554
34da0970 555wxPrintout::~wxPrintout()
c801d85f 556{
c801d85f
KB
557}
558
559bool wxPrintout::OnBeginDocument(int WXUNUSED(startPage), int WXUNUSED(endPage))
560{
fc799548 561 return GetDC()->StartDoc(_("Printing ") + m_printoutTitle);
c801d85f
KB
562}
563
34da0970 564void wxPrintout::OnEndDocument()
c801d85f 565{
7bcb11d3 566 GetDC()->EndDoc();
c801d85f
KB
567}
568
34da0970 569void wxPrintout::OnBeginPrinting()
c801d85f
KB
570{
571}
572
34da0970 573void wxPrintout::OnEndPrinting()
c801d85f
KB
574{
575}
576
577bool wxPrintout::HasPage(int page)
578{
7bcb11d3 579 return (page == 1);
c801d85f
KB
580}
581
582void wxPrintout::GetPageInfo(int *minPage, int *maxPage, int *fromPage, int *toPage)
583{
7bcb11d3
JS
584 *minPage = 1;
585 *maxPage = 32000;
586 *fromPage = 1;
587 *toPage = 1;
c801d85f
KB
588}
589
f415cab9
JS
590void wxPrintout::FitThisSizeToPaper(const wxSize& imageSize)
591{
592 // Set the DC scale and origin so that the given image size fits within the
593 // entire page and the origin is at the top left corner of the page. Note
594 // that with most printers, portions of the page will be non-printable. Use
595 // this if you're managing your own page margins.
596 if (!m_printoutDC) return;
597 wxRect paperRect = GetPaperRectPixels();
598 wxCoord pw, ph;
599 GetPageSizePixels(&pw, &ph);
600 wxCoord w, h;
601 m_printoutDC->GetSize(&w, &h);
602 float scaleX = ((float(paperRect.width) * w) / (float(pw) * imageSize.x));
603 float scaleY = ((float(paperRect.height) * h) / (float(ph) * imageSize.y));
604 float actualScale = wxMin(scaleX, scaleY);
03647350 605 m_printoutDC->SetUserScale(actualScale, actualScale);
f415cab9
JS
606 m_printoutDC->SetDeviceOrigin(0, 0);
607 wxRect logicalPaperRect = GetLogicalPaperRect();
608 SetLogicalOrigin(logicalPaperRect.x, logicalPaperRect.y);
609}
610
611void wxPrintout::FitThisSizeToPage(const wxSize& imageSize)
612{
613 // Set the DC scale and origin so that the given image size fits within the
614 // printable area of the page and the origin is at the top left corner of
615 // the printable area.
616 if (!m_printoutDC) return;
617 int w, h;
618 m_printoutDC->GetSize(&w, &h);
619 float scaleX = float(w) / imageSize.x;
620 float scaleY = float(h) / imageSize.y;
621 float actualScale = wxMin(scaleX, scaleY);
03647350 622 m_printoutDC->SetUserScale(actualScale, actualScale);
f415cab9
JS
623 m_printoutDC->SetDeviceOrigin(0, 0);
624}
625
626void wxPrintout::FitThisSizeToPageMargins(const wxSize& imageSize, const wxPageSetupDialogData& pageSetupData)
627{
628 // Set the DC scale and origin so that the given image size fits within the
629 // page margins defined in the given wxPageSetupDialogData object and the
630 // origin is at the top left corner of the page margins.
631 if (!m_printoutDC) return;
632 wxRect paperRect = GetPaperRectPixels();
633 wxCoord pw, ph;
634 GetPageSizePixels(&pw, &ph);
635 wxPoint topLeft = pageSetupData.GetMarginTopLeft();
636 wxPoint bottomRight = pageSetupData.GetMarginBottomRight();
637 wxCoord mw, mh;
638 GetPageSizeMM(&mw, &mh);
639 float mmToDeviceX = float(pw) / mw;
640 float mmToDeviceY = float(ph) / mh;
5a70d3f5
VZ
641 wxRect pageMarginsRect(paperRect.x + wxRound(mmToDeviceX * topLeft.x),
642 paperRect.y + wxRound(mmToDeviceY * topLeft.y),
643 paperRect.width - wxRound(mmToDeviceX * (topLeft.x + bottomRight.x)),
644 paperRect.height - wxRound(mmToDeviceY * (topLeft.y + bottomRight.y)));
f415cab9
JS
645 wxCoord w, h;
646 m_printoutDC->GetSize(&w, &h);
647 float scaleX = (float(pageMarginsRect.width) * w) / (float(pw) * imageSize.x);
648 float scaleY = (float(pageMarginsRect.height) * h) / (float(ph) * imageSize.y);
649 float actualScale = wxMin(scaleX, scaleY);
03647350 650 m_printoutDC->SetUserScale(actualScale, actualScale);
f415cab9
JS
651 m_printoutDC->SetDeviceOrigin(0, 0);
652 wxRect logicalPageMarginsRect = GetLogicalPageMarginsRect(pageSetupData);
653 SetLogicalOrigin(logicalPageMarginsRect.x, logicalPageMarginsRect.y);
654}
655
656void wxPrintout::MapScreenSizeToPaper()
657{
658 // Set the DC scale so that an image on the screen is the same size on the
659 // paper and the origin is at the top left of the paper. Note that with most
660 // printers, portions of the page will be cut off. Use this if you're
661 // managing your own page margins.
662 if (!m_printoutDC) return;
663 MapScreenSizeToPage();
664 wxRect logicalPaperRect = GetLogicalPaperRect();
665 SetLogicalOrigin(logicalPaperRect.x, logicalPaperRect.y);
666}
667
668void wxPrintout::MapScreenSizeToPage()
669{
670 // Set the DC scale and origin so that an image on the screen is the same
671 // size on the paper and the origin is at the top left of the printable area.
672 if (!m_printoutDC) return;
03647350
VZ
673 int ppiScreenX, ppiScreenY;
674 GetPPIScreen(&ppiScreenX, &ppiScreenY);
675 int ppiPrinterX, ppiPrinterY;
676 GetPPIPrinter(&ppiPrinterX, &ppiPrinterY);
677 int w, h;
678 m_printoutDC->GetSize(&w, &h);
679 int pageSizePixelsX, pageSizePixelsY;
680 GetPageSizePixels(&pageSizePixelsX, &pageSizePixelsY);
681 float userScaleX = (float(ppiPrinterX) * w) / (float(ppiScreenX) * pageSizePixelsX);
682 float userScaleY = (float(ppiPrinterY) * h) / (float(ppiScreenY) * pageSizePixelsY);
f415cab9
JS
683 m_printoutDC->SetUserScale(userScaleX, userScaleY);
684 m_printoutDC->SetDeviceOrigin(0, 0);
685}
686
687void wxPrintout::MapScreenSizeToPageMargins(const wxPageSetupDialogData& pageSetupData)
688{
689 // Set the DC scale so that an image on the screen is the same size on the
690 // paper and the origin is at the top left of the page margins defined by
691 // the given wxPageSetupDialogData object.
692 if (!m_printoutDC) return;
693 MapScreenSizeToPage();
694 wxRect logicalPageMarginsRect = GetLogicalPageMarginsRect(pageSetupData);
695 SetLogicalOrigin(logicalPageMarginsRect.x, logicalPageMarginsRect.y);
696}
697
698void wxPrintout::MapScreenSizeToDevice()
699{
700 // Set the DC scale so that a screen pixel is the same size as a device
701 // pixel and the origin is at the top left of the printable area.
702 if (!m_printoutDC) return;
03647350
VZ
703 int w, h;
704 m_printoutDC->GetSize(&w, &h);
705 int pageSizePixelsX, pageSizePixelsY;
706 GetPageSizePixels(&pageSizePixelsX, &pageSizePixelsY);
707 float userScaleX = float(w) / pageSizePixelsX;
708 float userScaleY = float(h) / pageSizePixelsY;
f415cab9
JS
709 m_printoutDC->SetUserScale(userScaleX, userScaleY);
710 m_printoutDC->SetDeviceOrigin(0, 0);
711}
712
713wxRect wxPrintout::GetLogicalPaperRect() const
714{
715 // Return the rectangle in logical units that corresponds to the paper
716 // rectangle.
717 wxRect paperRect = GetPaperRectPixels();
718 wxCoord pw, ph;
719 GetPageSizePixels(&pw, &ph);
720 wxCoord w, h;
721 m_printoutDC->GetSize(&w, &h);
722 if (w == pw && h == ph) {
723 // this DC matches the printed page, so no scaling
03647350
VZ
724 return wxRect(m_printoutDC->DeviceToLogicalX(paperRect.x),
725 m_printoutDC->DeviceToLogicalY(paperRect.y),
726 m_printoutDC->DeviceToLogicalXRel(paperRect.width),
f415cab9
JS
727 m_printoutDC->DeviceToLogicalYRel(paperRect.height));
728 }
729 // This DC doesn't match the printed page, so we have to scale.
730 float scaleX = float(w) / pw;
731 float scaleY = float(h) / ph;
03647350
VZ
732 return wxRect(m_printoutDC->DeviceToLogicalX(wxRound(paperRect.x * scaleX)),
733 m_printoutDC->DeviceToLogicalY(wxRound(paperRect.y * scaleY)),
734 m_printoutDC->DeviceToLogicalXRel(wxRound(paperRect.width * scaleX)),
5a70d3f5 735 m_printoutDC->DeviceToLogicalYRel(wxRound(paperRect.height * scaleY)));
f415cab9
JS
736}
737
738wxRect wxPrintout::GetLogicalPageRect() const
739{
740 // Return the rectangle in logical units that corresponds to the printable
741 // area.
742 int w, h;
743 m_printoutDC->GetSize(&w, &h);
03647350
VZ
744 return wxRect(m_printoutDC->DeviceToLogicalX(0),
745 m_printoutDC->DeviceToLogicalY(0),
746 m_printoutDC->DeviceToLogicalXRel(w),
f415cab9
JS
747 m_printoutDC->DeviceToLogicalYRel(h));
748}
749
750wxRect wxPrintout::GetLogicalPageMarginsRect(const wxPageSetupDialogData& pageSetupData) const
751{
752 // Return the rectangle in logical units that corresponds to the region
753 // within the page margins as specified by the given wxPageSetupDialogData
754 // object.
03647350 755
02255e07
RR
756 // We get the paper size in device units and the margins in mm,
757 // so we need to calculate the conversion with this trick
f415cab9
JS
758 wxCoord pw, ph;
759 GetPageSizePixels(&pw, &ph);
f415cab9
JS
760 wxCoord mw, mh;
761 GetPageSizeMM(&mw, &mh);
762 float mmToDeviceX = float(pw) / mw;
763 float mmToDeviceY = float(ph) / mh;
02255e07 764
03647350 765 // paper size in device units
02255e07 766 wxRect paperRect = GetPaperRectPixels();
03647350 767
02255e07
RR
768 // margins in mm
769 wxPoint topLeft = pageSetupData.GetMarginTopLeft();
770 wxPoint bottomRight = pageSetupData.GetMarginBottomRight();
03647350 771
02255e07
RR
772 // calculate margins in device units
773 wxRect pageMarginsRect(
774 paperRect.x + wxRound(mmToDeviceX * topLeft.x),
775 paperRect.y + wxRound(mmToDeviceY * topLeft.y),
776 paperRect.width - wxRound(mmToDeviceX * (topLeft.x + bottomRight.x)),
5a70d3f5 777 paperRect.height - wxRound(mmToDeviceY * (topLeft.y + bottomRight.y)));
03647350 778
f415cab9
JS
779 wxCoord w, h;
780 m_printoutDC->GetSize(&w, &h);
03647350 781 if (w == pw && h == ph)
02255e07 782 {
f415cab9 783 // this DC matches the printed page, so no scaling
02255e07 784 return wxRect(
03647350
VZ
785 m_printoutDC->DeviceToLogicalX(pageMarginsRect.x),
786 m_printoutDC->DeviceToLogicalY(pageMarginsRect.y),
787 m_printoutDC->DeviceToLogicalXRel(pageMarginsRect.width),
f415cab9
JS
788 m_printoutDC->DeviceToLogicalYRel(pageMarginsRect.height));
789 }
03647350 790
f415cab9
JS
791 // This DC doesn't match the printed page, so we have to scale.
792 float scaleX = float(w) / pw;
793 float scaleY = float(h) / ph;
03647350
VZ
794 return wxRect(m_printoutDC->DeviceToLogicalX(wxRound(pageMarginsRect.x * scaleX)),
795 m_printoutDC->DeviceToLogicalY(wxRound(pageMarginsRect.y * scaleY)),
796 m_printoutDC->DeviceToLogicalXRel(wxRound(pageMarginsRect.width * scaleX)),
5a70d3f5 797 m_printoutDC->DeviceToLogicalYRel(wxRound(pageMarginsRect.height * scaleY)));
f415cab9
JS
798}
799
800void wxPrintout::SetLogicalOrigin(wxCoord x, wxCoord y)
801{
802 // Set the device origin by specifying a point in logical coordinates.
02255e07 803 m_printoutDC->SetDeviceOrigin(
03647350 804 m_printoutDC->LogicalToDeviceX(x),
02255e07 805 m_printoutDC->LogicalToDeviceY(y) );
f415cab9 806}
03647350 807
f415cab9
JS
808void wxPrintout::OffsetLogicalOrigin(wxCoord xoff, wxCoord yoff)
809{
810 // Offset the device origin by a specified distance in device coordinates.
02255e07
RR
811 wxPoint dev_org = m_printoutDC->GetDeviceOrigin();
812 m_printoutDC->SetDeviceOrigin(
03647350 813 dev_org.x + m_printoutDC->LogicalToDeviceXRel(xoff),
02255e07 814 dev_org.y + m_printoutDC->LogicalToDeviceYRel(yoff) );
f415cab9 815}
03647350 816
f415cab9 817
e81e3883
RR
818//----------------------------------------------------------------------------
819// wxPreviewCanvas
820//----------------------------------------------------------------------------
821
822IMPLEMENT_CLASS(wxPreviewCanvas, wxWindow)
823
824BEGIN_EVENT_TABLE(wxPreviewCanvas, wxScrolledWindow)
825 EVT_PAINT(wxPreviewCanvas::OnPaint)
826 EVT_CHAR(wxPreviewCanvas::OnChar)
b88bf073 827 EVT_IDLE(wxPreviewCanvas::OnIdle)
e81e3883 828 EVT_SYS_COLOUR_CHANGED(wxPreviewCanvas::OnSysColourChanged)
fb6efdf2
VZ
829#if wxUSE_MOUSEWHEEL
830 EVT_MOUSEWHEEL(wxPreviewCanvas::OnMouseWheel)
831#endif
e81e3883 832END_EVENT_TABLE()
7bcb11d3 833
e9cafd42
VZ
834// VZ: the current code doesn't refresh properly without
835// wxFULL_REPAINT_ON_RESIZE, this must be fixed as otherwise we have
836// really horrible flicker when resizing the preview frame, but without
837// this style it simply doesn't work correctly at all...
c801d85f 838wxPreviewCanvas::wxPreviewCanvas(wxPrintPreviewBase *preview, wxWindow *parent,
7bcb11d3 839 const wxPoint& pos, const wxSize& size, long style, const wxString& name):
7e548f6b 840wxScrolledWindow(parent, wxID_ANY, pos, size, style | wxFULL_REPAINT_ON_RESIZE, name)
c801d85f 841{
7bcb11d3 842 m_printPreview = preview;
46b24ef9
JS
843#ifdef __WXMAC__
844 // The app workspace colour is always white, but we should have
845 // a contrast with the page.
ca5020c2 846 wxSystemColour colourIndex = wxSYS_COLOUR_3DDKSHADOW;
92b799e0
JS
847#elif defined(__WXGTK__)
848 wxSystemColour colourIndex = wxSYS_COLOUR_BTNFACE;
46b24ef9
JS
849#else
850 wxSystemColour colourIndex = wxSYS_COLOUR_APPWORKSPACE;
e71fd398 851#endif
46b24ef9 852 SetBackgroundColour(wxSystemSettings::GetColour(colourIndex));
8826f46f 853
d2b354f9 854 SetScrollbars(10, 10, 100, 100);
c801d85f
KB
855}
856
34da0970 857wxPreviewCanvas::~wxPreviewCanvas()
c801d85f
KB
858{
859}
860
861void wxPreviewCanvas::OnPaint(wxPaintEvent& WXUNUSED(event))
862{
7bcb11d3
JS
863 wxPaintDC dc(this);
864 PrepareDC( dc );
8826f46f 865
a56fcaaf 866/*
809934d2
RR
867#ifdef __WXGTK__
868 if (!GetUpdateRegion().IsEmpty())
869 dc.SetClippingRegion( GetUpdateRegion() );
870#endif
a56fcaaf 871*/
809934d2 872
7bcb11d3
JS
873 if (m_printPreview)
874 {
875 m_printPreview->PaintPage(this, dc);
876 }
c801d85f
KB
877}
878
b88bf073
VS
879void wxPreviewCanvas::OnIdle(wxIdleEvent& event)
880{
88b98787
VS
881 event.Skip();
882
883 // prevent UpdatePageRendering() from being called recursively:
884 static bool s_inIdle = false;
885 if ( s_inIdle )
886 return;
887 s_inIdle = true;
888
b88bf073
VS
889 if ( m_printPreview )
890 {
891 if ( m_printPreview->UpdatePageRendering() )
892 Refresh();
893 }
88b98787
VS
894
895 s_inIdle = false;
b88bf073
VS
896}
897
c801d85f
KB
898// Responds to colour changes, and passes event on to children.
899void wxPreviewCanvas::OnSysColourChanged(wxSysColourChangedEvent& event)
900{
46b24ef9
JS
901#ifdef __WXMAC__
902 // The app workspace colour is always white, but we should have
903 // a contrast with the page.
ca5020c2 904 wxSystemColour colourIndex = wxSYS_COLOUR_3DDKSHADOW;
92b799e0
JS
905#elif defined(__WXGTK__)
906 wxSystemColour colourIndex = wxSYS_COLOUR_BTNFACE;
46b24ef9
JS
907#else
908 wxSystemColour colourIndex = wxSYS_COLOUR_APPWORKSPACE;
e71fd398 909#endif
46b24ef9 910 SetBackgroundColour(wxSystemSettings::GetColour(colourIndex));
7bcb11d3 911 Refresh();
8826f46f 912
7bcb11d3
JS
913 // Propagate the event to the non-top-level children
914 wxWindow::OnSysColourChanged(event);
c801d85f
KB
915}
916
d2b354f9
JS
917void wxPreviewCanvas::OnChar(wxKeyEvent &event)
918{
b38b0d22 919 wxPreviewControlBar* controlBar = ((wxPreviewFrame*) GetParent())->GetControlBar();
043c6705 920 switch (event.GetKeyCode())
d2b354f9 921 {
043c6705
VZ
922 case WXK_RETURN:
923 controlBar->OnPrint();
924 return;
82579461
VZ
925 case (int)'+':
926 case WXK_NUMPAD_ADD:
927 case WXK_ADD:
928 controlBar->DoZoomIn();
929 return;
930 case (int)'-':
931 case WXK_NUMPAD_SUBTRACT:
932 case WXK_SUBTRACT:
933 controlBar->DoZoomOut();
934 return;
b38b0d22
JS
935 }
936
d2b354f9
JS
937 if (!event.ControlDown())
938 {
939 event.Skip();
940 return;
941 }
e71fd398 942
b38b0d22
JS
943 switch(event.GetKeyCode())
944 {
faa94f3e 945 case WXK_PAGEDOWN:
b38b0d22 946 controlBar->OnNext(); break;
faa94f3e 947 case WXK_PAGEUP:
b38b0d22
JS
948 controlBar->OnPrevious(); break;
949 case WXK_HOME:
950 controlBar->OnFirst(); break;
951 case WXK_END:
952 controlBar->OnLast(); break;
953 default:
954 event.Skip();
955 }
d2b354f9
JS
956}
957
fb6efdf2
VZ
958#if wxUSE_MOUSEWHEEL
959
960void wxPreviewCanvas::OnMouseWheel(wxMouseEvent& event)
961{
962 wxPreviewControlBar *
963 controlBar = wxStaticCast(GetParent(), wxPreviewFrame)->GetControlBar();
964
965 if ( controlBar )
966 {
967 if ( event.ControlDown() && event.GetWheelRotation() != 0 )
968 {
969 int currentZoom = controlBar->GetZoomControl();
970
971 int delta;
972 if ( currentZoom < 100 )
973 delta = 5;
974 else if ( currentZoom <= 120 )
975 delta = 10;
976 else
977 delta = 50;
978
979 if ( event.GetWheelRotation() > 0 )
980 delta = -delta;
981
982 int newZoom = currentZoom + delta;
983 if ( newZoom < 10 )
984 newZoom = 10;
985 if ( newZoom > 200 )
986 newZoom = 200;
987 if ( newZoom != currentZoom )
988 {
989 controlBar->SetZoomControl(newZoom);
990 m_printPreview->SetZoom(newZoom);
991 Refresh();
992 }
993 return;
994 }
995 }
996
997 event.Skip();
998}
999
1000#endif // wxUSE_MOUSEWHEEL
1001
d7cfeeb4
VZ
1002namespace
1003{
1004
1005// This is by the controls in the print preview as the maximal (and hence
1006// longest) page number we may have to display.
1007enum { MAX_PAGE_NUMBER = 99999 };
1008
1009} // anonymous namespace
1010
1011// ----------------------------------------------------------------------------
1012// wxPrintPageMaxCtrl
1013// ----------------------------------------------------------------------------
1014
1015// A simple static control showing the maximal number of pages.
1016class wxPrintPageMaxCtrl : public wxStaticText
1017{
1018public:
1019 wxPrintPageMaxCtrl(wxWindow *parent)
1020 : wxStaticText(
1021 parent,
1022 wxID_ANY,
1023 wxString(),
1024 wxDefaultPosition,
1025 wxSize
1026 (
1027 parent->GetTextExtent(MaxAsString(MAX_PAGE_NUMBER)).x,
1028 wxDefaultCoord
1029 ),
1030 wxST_NO_AUTORESIZE | wxALIGN_CENTRE
1031 )
1032 {
1033 }
1034
1035 // Set the maximal page to display once we really know what it is.
1036 void SetMaxPage(int maxPage)
1037 {
1038 SetLabel(MaxAsString(maxPage));
1039 }
1040
1041private:
1042 static wxString MaxAsString(int maxPage)
1043 {
1044 return wxString::Format("/ %d", maxPage);
1045 }
1046
1047 wxDECLARE_NO_COPY_CLASS(wxPrintPageMaxCtrl);
1048};
1049
82579461
VZ
1050// ----------------------------------------------------------------------------
1051// wxPrintPageTextCtrl
1052// ----------------------------------------------------------------------------
1053
d7cfeeb4
VZ
1054// This text control contains the page number in the specified interval.
1055//
1056// Invalid pages are not accepted and the control contents is validated when it
1057// loses focus. Conversely, if the user changes the page to another valid one
1058// or presses Enter, OnGotoPage() method of the preview object will be called.
82579461
VZ
1059class wxPrintPageTextCtrl : public wxTextCtrl
1060{
1061public:
d7cfeeb4 1062 wxPrintPageTextCtrl(wxPreviewControlBar *preview)
82579461
VZ
1063 : wxTextCtrl(preview,
1064 wxID_PREVIEW_GOTO,
d7cfeeb4 1065 wxString(),
82579461 1066 wxDefaultPosition,
d7cfeeb4
VZ
1067 // We use hardcoded maximal page number for the width
1068 // instead of fitting it to the values we can show because
1069 // the control looks uncomfortably narrow if the real page
1070 // number is just one or two digits.
1071 wxSize
1072 (
1073 preview->GetTextExtent(PageAsString(MAX_PAGE_NUMBER)).x,
1074 wxDefaultCoord
1075 ),
82579461
VZ
1076 wxTE_PROCESS_ENTER
1077#if wxUSE_VALIDATORS
1078 , wxTextValidator(wxFILTER_DIGITS)
1079#endif // wxUSE_VALIDATORS
1080 ),
d7cfeeb4 1081 m_preview(preview)
82579461 1082 {
d7cfeeb4
VZ
1083 m_minPage =
1084 m_maxPage =
1085 m_page = 1;
82579461
VZ
1086
1087 Connect(wxEVT_KILL_FOCUS,
1088 wxFocusEventHandler(wxPrintPageTextCtrl::OnKillFocus));
1089 Connect(wxEVT_COMMAND_TEXT_ENTER,
1090 wxCommandEventHandler(wxPrintPageTextCtrl::OnTextEnter));
1091 }
1092
d7cfeeb4
VZ
1093 // Update the pages range, must be called after OnPreparePrinting() as
1094 // these values are not known before.
1095 void SetPageInfo(int minPage, int maxPage)
1096 {
1097 m_minPage = minPage;
1098 m_maxPage = maxPage;
1099
1100 // Show the first page by default.
1101 SetPageNumber(minPage);
1102 }
1103
82579461
VZ
1104 // Helpers to conveniently set or get the current page number. Return value
1105 // is 0 if the current controls contents is invalid.
1106 void SetPageNumber(int page)
1107 {
1108 wxASSERT( IsValidPage(page) );
1109
1110 SetValue(PageAsString(page));
1111 }
1112
1113 int GetPageNumber() const
1114 {
1115 long value;
1116 if ( !GetValue().ToLong(&value) || !IsValidPage(value) )
1117 return 0;
1118
1119 // Cast is safe because the value is less than (int) m_maxPage.
1120 return static_cast<int>(value);
1121 }
1122
1123private:
1124 static wxString PageAsString(int page)
1125 {
1126 return wxString::Format("%d", page);
1127 }
1128
1129 bool IsValidPage(int page) const
1130 {
1131 return page >= m_minPage && page <= m_maxPage;
1132 }
1133
1134 bool DoChangePage()
1135 {
1136 const int page = GetPageNumber();
1137
1138 if ( !page )
1139 return false;
1140
1141 if ( page != m_page )
1142 {
1143 // We have a valid page, remember it.
1144 m_page = page;
1145
1146 // And notify the owner about the change.
1147 m_preview->OnGotoPage();
1148 }
1149 //else: Nothing really changed.
1150
1151 return true;
1152 }
1153
1154 void OnKillFocus(wxFocusEvent& event)
1155 {
1156 if ( !DoChangePage() )
1157 {
1158 // The current contents is invalid so reset it back to the last
1159 // known good page index.
1160 SetPageNumber(m_page);
1161 }
1162
1163 event.Skip();
1164 }
1165
1166 void OnTextEnter(wxCommandEvent& WXUNUSED(event))
1167 {
1168 DoChangePage();
1169 }
1170
1171
1172 wxPreviewControlBar * const m_preview;
1173
d7cfeeb4
VZ
1174 int m_minPage,
1175 m_maxPage;
82579461
VZ
1176
1177 // This is the last valid page value that we had, we revert to it if an
1178 // invalid page is entered.
1179 int m_page;
1180
1181 wxDECLARE_NO_COPY_CLASS(wxPrintPageTextCtrl);
1182};
1183
e81e3883
RR
1184//----------------------------------------------------------------------------
1185// wxPreviewControlBar
1186//----------------------------------------------------------------------------
1187
1188IMPLEMENT_CLASS(wxPreviewControlBar, wxWindow)
c801d85f
KB
1189
1190BEGIN_EVENT_TABLE(wxPreviewControlBar, wxPanel)
8826f46f 1191 EVT_BUTTON(wxID_PREVIEW_CLOSE, wxPreviewControlBar::OnWindowClose)
90b6b974 1192 EVT_BUTTON(wxID_PREVIEW_PRINT, wxPreviewControlBar::OnPrintButton)
0f90ec93
KB
1193 EVT_BUTTON(wxID_PREVIEW_PREVIOUS, wxPreviewControlBar::OnPreviousButton)
1194 EVT_BUTTON(wxID_PREVIEW_NEXT, wxPreviewControlBar::OnNextButton)
bf89b538
JS
1195 EVT_BUTTON(wxID_PREVIEW_FIRST, wxPreviewControlBar::OnFirstButton)
1196 EVT_BUTTON(wxID_PREVIEW_LAST, wxPreviewControlBar::OnLastButton)
82579461
VZ
1197 EVT_BUTTON(wxID_PREVIEW_ZOOM_IN, wxPreviewControlBar::OnZoomInButton)
1198 EVT_BUTTON(wxID_PREVIEW_ZOOM_OUT, wxPreviewControlBar::OnZoomOutButton)
1199
1200 EVT_UPDATE_UI(wxID_PREVIEW_PREVIOUS, wxPreviewControlBar::OnUpdatePreviousButton)
1201 EVT_UPDATE_UI(wxID_PREVIEW_NEXT, wxPreviewControlBar::OnUpdateNextButton)
1202 EVT_UPDATE_UI(wxID_PREVIEW_FIRST, wxPreviewControlBar::OnUpdateFirstButton)
1203 EVT_UPDATE_UI(wxID_PREVIEW_LAST, wxPreviewControlBar::OnUpdateLastButton)
1204 EVT_UPDATE_UI(wxID_PREVIEW_ZOOM_IN, wxPreviewControlBar::OnUpdateZoomInButton)
1205 EVT_UPDATE_UI(wxID_PREVIEW_ZOOM_OUT, wxPreviewControlBar::OnUpdateZoomOutButton)
1206
1207 EVT_CHOICE(wxID_PREVIEW_ZOOM, wxPreviewControlBar::OnZoomChoice)
8826f46f 1208 EVT_PAINT(wxPreviewControlBar::OnPaint)
82579461 1209
c801d85f 1210END_EVENT_TABLE()
7bcb11d3 1211
c801d85f 1212wxPreviewControlBar::wxPreviewControlBar(wxPrintPreviewBase *preview, long buttons,
7bcb11d3
JS
1213 wxWindow *parent, const wxPoint& pos, const wxSize& size,
1214 long style, const wxString& name):
7e548f6b 1215wxPanel(parent, wxID_ANY, pos, size, style, name)
c801d85f 1216{
7bcb11d3 1217 m_printPreview = preview;
d3b9f782 1218 m_closeButton = NULL;
d3b9f782 1219 m_zoomControl = NULL;
82579461 1220 m_currentPageText = NULL;
d7cfeeb4 1221 m_maxPageText = NULL;
7bcb11d3 1222 m_buttonFlags = buttons;
c801d85f
KB
1223}
1224
34da0970 1225wxPreviewControlBar::~wxPreviewControlBar()
c801d85f
KB
1226{
1227}
1228
1229void wxPreviewControlBar::OnPaint(wxPaintEvent& WXUNUSED(event))
1230{
7bcb11d3 1231 wxPaintDC dc(this);
8826f46f 1232
7bcb11d3
JS
1233 int w, h;
1234 GetSize(&w, &h);
1235 dc.SetPen(*wxBLACK_PEN);
1236 dc.SetBrush(*wxTRANSPARENT_BRUSH);
1237 dc.DrawLine( 0, h-1, w, h-1 );
c801d85f
KB
1238}
1239
c330a2cf 1240void wxPreviewControlBar::OnWindowClose(wxCommandEvent& WXUNUSED(event))
c801d85f 1241{
7bcb11d3 1242 wxPreviewFrame *frame = (wxPreviewFrame *)GetParent();
7e548f6b 1243 frame->Close(true);
c801d85f
KB
1244}
1245
b38b0d22 1246void wxPreviewControlBar::OnPrint(void)
c801d85f 1247{
7bcb11d3 1248 wxPrintPreviewBase *preview = GetPrintPreview();
7e548f6b 1249 preview->Print(true);
c801d85f
KB
1250}
1251
82579461
VZ
1252void wxPreviewControlBar::OnNext()
1253{
1254 if ( IsNextEnabled() )
1255 DoGotoPage(GetPrintPreview()->GetCurrentPage() + 1);
1256}
1257
1258void wxPreviewControlBar::OnPrevious()
1259{
1260 if ( IsPreviousEnabled() )
1261 DoGotoPage(GetPrintPreview()->GetCurrentPage() - 1);
1262}
1263
1264void wxPreviewControlBar::OnFirst()
1265{
1266 if ( IsFirstEnabled() )
1267 DoGotoPage(GetPrintPreview()->GetMinPage());
1268}
1269
1270void wxPreviewControlBar::OnLast()
1271{
1272 if ( IsLastEnabled() )
1273 DoGotoPage(GetPrintPreview()->GetMaxPage());
1274}
1275
1276bool wxPreviewControlBar::IsNextEnabled() const
c801d85f 1277{
7bcb11d3 1278 wxPrintPreviewBase *preview = GetPrintPreview();
82579461
VZ
1279 if ( !preview )
1280 return false;
1281
1282 const int currentPage = preview->GetCurrentPage();
1283 return currentPage < preview->GetMaxPage() &&
1284 preview->GetPrintout()->HasPage(currentPage + 1);
c801d85f
KB
1285}
1286
82579461 1287bool wxPreviewControlBar::IsPreviousEnabled() const
c801d85f 1288{
7bcb11d3 1289 wxPrintPreviewBase *preview = GetPrintPreview();
82579461
VZ
1290 if ( !preview )
1291 return false;
1292
1293 const int currentPage = preview->GetCurrentPage();
1294 return currentPage > preview->GetMinPage() &&
1295 preview->GetPrintout()->HasPage(currentPage - 1);
c801d85f
KB
1296}
1297
82579461 1298bool wxPreviewControlBar::IsFirstEnabled() const
bf89b538
JS
1299{
1300 wxPrintPreviewBase *preview = GetPrintPreview();
82579461
VZ
1301 if (!preview)
1302 return false;
1303
1304 return preview->GetPrintout()->HasPage(preview->GetMinPage());
bf89b538
JS
1305}
1306
82579461 1307bool wxPreviewControlBar::IsLastEnabled() const
bf89b538
JS
1308{
1309 wxPrintPreviewBase *preview = GetPrintPreview();
82579461
VZ
1310 if (!preview)
1311 return false;
1312
1313 return preview->GetPrintout()->HasPage(preview->GetMaxPage());
bf89b538
JS
1314}
1315
82579461
VZ
1316void wxPreviewControlBar::DoGotoPage(int page)
1317{
1318 wxPrintPreviewBase *preview = GetPrintPreview();
1319 wxCHECK_RET( preview, "Shouldn't be called if there is no preview." );
1320
1321 preview->SetCurrentPage(page);
1322
1323 if ( m_currentPageText )
1324 m_currentPageText->SetPageNumber(page);
1325}
1326
1327void wxPreviewControlBar::OnGotoPage()
bf89b538
JS
1328{
1329 wxPrintPreviewBase *preview = GetPrintPreview();
1330 if (preview)
1331 {
bf89b538
JS
1332 if (preview->GetMinPage() > 0)
1333 {
82579461
VZ
1334 long currentPage = m_currentPageText->GetPageNumber();
1335 if ( currentPage )
1336 {
bf89b538
JS
1337 if (preview->GetPrintout()->HasPage(currentPage))
1338 {
1339 preview->SetCurrentPage(currentPage);
1340 }
82579461 1341 }
bf89b538
JS
1342 }
1343 }
1344}
1345
82579461 1346void wxPreviewControlBar::DoZoom()
c801d85f 1347{
7bcb11d3
JS
1348 int zoom = GetZoomControl();
1349 if (GetPrintPreview())
1350 GetPrintPreview()->SetZoom(zoom);
c801d85f
KB
1351}
1352
82579461
VZ
1353bool wxPreviewControlBar::IsZoomInEnabled() const
1354{
1355 if ( !m_zoomControl )
1356 return false;
1357
1358 const unsigned sel = m_zoomControl->GetSelection();
1359 return sel < m_zoomControl->GetCount() - 1;
1360}
1361
1362bool wxPreviewControlBar::IsZoomOutEnabled() const
1363{
1364 return m_zoomControl && m_zoomControl->GetSelection() > 0;
1365}
1366
1367void wxPreviewControlBar::DoZoomIn()
1368{
1369 if (IsZoomInEnabled())
1370 {
1371 m_zoomControl->SetSelection(m_zoomControl->GetSelection() + 1);
1372 DoZoom();
1373 }
1374}
1375
1376void wxPreviewControlBar::DoZoomOut()
1377{
1378 if (IsZoomOutEnabled())
1379 {
1380 m_zoomControl->SetSelection(m_zoomControl->GetSelection() - 1);
1381 DoZoom();
1382 }
1383}
1384
1385namespace
1386{
1387
1388// Helper class used by wxPreviewControlBar::CreateButtons() to add buttons
1389// sequentially to it in the simplest way possible.
1390class SizerWithButtons
1391{
1392public:
1393 // Constructor creates the sizer that will hold the buttons and stores the
1394 // parent that will be used for their creation.
1395 SizerWithButtons(wxWindow *parent)
1396 : m_sizer(new wxBoxSizer(wxHORIZONTAL)),
1397 m_parent(parent)
1398 {
1399 m_hasContents =
1400 m_needsSeparator = false;
1401 }
1402
1403 // Destructor associates the sizer with the parent window.
1404 ~SizerWithButtons()
1405 {
1406 m_parent->SetSizer(m_sizer);
1407 m_sizer->Fit(m_parent);
1408 }
1409
1410
1411 // Add an arbitrary window to the sizer.
1412 void Add(wxWindow *win)
1413 {
1414 if ( m_needsSeparator )
1415 {
1416 m_needsSeparator = false;
1417
1418 m_sizer->AddSpacer(2*wxSizerFlags::GetDefaultBorder());
1419 }
1420
1421 m_hasContents = true;
1422
1423 m_sizer->Add(win,
1424 wxSizerFlags().Border(wxLEFT | wxTOP | wxBOTTOM).Center());
1425 }
1426
1427 // Add a button with the specified id, bitmap and tooltip.
1428 void AddButton(wxWindowID btnId,
1429 const wxArtID& artId,
1430 const wxString& tooltip)
1431 {
1432 // We don't use (smaller) images inside a button with a text label but
1433 // rather toolbar-like bitmap buttons hence use wxART_TOOLBAR and not
1434 // wxART_BUTTON here.
1435 wxBitmap bmp = wxArtProvider::GetBitmap(artId, wxART_TOOLBAR);
1436 wxBitmapButton * const btn = new wxBitmapButton(m_parent, btnId, bmp);
1437 btn->SetToolTip(tooltip);
1438
1439 Add(btn);
1440 }
1441
1442 // Add a control at the right end of the window. This should be called last
1443 // as everything else added after it will be added on the right side too.
1444 void AddAtEnd(wxWindow *win)
1445 {
1446 m_sizer->AddStretchSpacer();
1447 m_sizer->Add(win,
1448 wxSizerFlags().Border(wxTOP | wxBOTTOM | wxRIGHT).Center());
1449 }
1450
1451 // Indicates the end of a group of buttons, a separator will be added after
1452 // it.
1453 void EndOfGroup()
1454 {
1455 if ( m_hasContents )
1456 {
1457 m_needsSeparator = true;
1458 m_hasContents = false;
1459 }
1460 }
1461
1462private:
1463 wxSizer * const m_sizer;
1464 wxWindow * const m_parent;
1465
1466 // If true, we have some controls since the last group beginning. This is
1467 // used to avoid inserting two consecutive separators if EndOfGroup() is
1468 // called twice.
1469 bool m_hasContents;
1470
1471 // If true, a separator should be inserted before adding the next button.
1472 bool m_needsSeparator;
1473
1474 wxDECLARE_NO_COPY_CLASS(SizerWithButtons);
1475};
1476
1477} // anonymous namespace
1478
34da0970 1479void wxPreviewControlBar::CreateButtons()
c801d85f 1480{
82579461 1481 SizerWithButtons sizer(this);
e71fd398 1482
82579461 1483 // Print button group (a single button).
7bcb11d3
JS
1484 if (m_buttonFlags & wxPREVIEW_PRINT)
1485 {
82579461
VZ
1486 sizer.AddButton(wxID_PREVIEW_PRINT, wxART_PRINT, _("Print"));
1487 sizer.EndOfGroup();
7bcb11d3 1488 }
e71fd398 1489
82579461 1490 // Page selection buttons group.
bf89b538
JS
1491 if (m_buttonFlags & wxPREVIEW_FIRST)
1492 {
82579461 1493 sizer.AddButton(wxID_PREVIEW_FIRST, wxART_GOTO_FIRST, _("First page"));
bf89b538 1494 }
e71fd398 1495
7bcb11d3
JS
1496 if (m_buttonFlags & wxPREVIEW_PREVIOUS)
1497 {
82579461 1498 sizer.AddButton(wxID_PREVIEW_PREVIOUS, wxART_GO_BACK, _("Previous page"));
7bcb11d3 1499 }
e71fd398 1500
82579461 1501 if (m_buttonFlags & wxPREVIEW_GOTO)
7bcb11d3 1502 {
d7cfeeb4 1503 m_currentPageText = new wxPrintPageTextCtrl(this);
82579461
VZ
1504 sizer.Add(m_currentPageText);
1505
d7cfeeb4
VZ
1506 m_maxPageText = new wxPrintPageMaxCtrl(this);
1507 sizer.Add(m_maxPageText);
bf89b538 1508 }
e71fd398 1509
82579461 1510 if (m_buttonFlags & wxPREVIEW_NEXT)
bf89b538 1511 {
82579461 1512 sizer.AddButton(wxID_PREVIEW_NEXT, wxART_GO_FORWARD, _("Next page"));
bf89b538 1513 }
e71fd398 1514
82579461 1515 if (m_buttonFlags & wxPREVIEW_LAST)
bf89b538 1516 {
82579461 1517 sizer.AddButton(wxID_PREVIEW_LAST, wxART_GOTO_LAST, _("Last page"));
7bcb11d3 1518 }
e71fd398 1519
82579461
VZ
1520 sizer.EndOfGroup();
1521
1522 // Zoom controls group.
7bcb11d3
JS
1523 if (m_buttonFlags & wxPREVIEW_ZOOM)
1524 {
82579461
VZ
1525 sizer.AddButton(wxID_PREVIEW_ZOOM_OUT, wxART_MINUS, _("Zoom Out"));
1526
e71fd398 1527 wxString choices[] =
c25ccf85 1528 {
2b5f62a0 1529 wxT("10%"), wxT("15%"), wxT("20%"), wxT("25%"), wxT("30%"), wxT("35%"), wxT("40%"), wxT("45%"), wxT("50%"), wxT("55%"),
9dff8515
JS
1530 wxT("60%"), wxT("65%"), wxT("70%"), wxT("75%"), wxT("80%"), wxT("85%"), wxT("90%"), wxT("95%"), wxT("100%"), wxT("110%"),
1531 wxT("120%"), wxT("150%"), wxT("200%")
c25ccf85 1532 };
c25ccf85 1533 int n = WXSIZEOF(choices);
e71fd398 1534
7e548f6b 1535 m_zoomControl = new wxChoice( this, wxID_PREVIEW_ZOOM, wxDefaultPosition, wxSize(70,wxDefaultCoord), n, choices, 0 );
82579461 1536 sizer.Add(m_zoomControl);
7bcb11d3 1537 SetZoomControl(m_printPreview->GetZoom());
82579461
VZ
1538
1539 sizer.AddButton(wxID_PREVIEW_ZOOM_IN, wxART_PLUS, _("Zoom In"));
1540
1541 sizer.EndOfGroup();
7bcb11d3 1542 }
8826f46f 1543
82579461
VZ
1544 // Close button group (single button again).
1545 m_closeButton = new wxButton(this, wxID_PREVIEW_CLOSE, _("&Close"));
1546 sizer.AddAtEnd(m_closeButton);
c801d85f
KB
1547}
1548
d7cfeeb4
VZ
1549void wxPreviewControlBar::SetPageInfo(int minPage, int maxPage)
1550{
1551 if ( m_currentPageText )
1552 m_currentPageText->SetPageInfo(minPage, maxPage);
1553
1554 if ( m_maxPageText )
1555 m_maxPageText->SetMaxPage(maxPage);
1556}
1557
c801d85f
KB
1558void wxPreviewControlBar::SetZoomControl(int zoom)
1559{
7bcb11d3 1560 if (m_zoomControl)
963907fa
RR
1561 {
1562 int n, count = m_zoomControl->GetCount();
1563 long val;
1564 for (n=0; n<count; n++)
1565 {
1566 if (m_zoomControl->GetString(n).BeforeFirst(wxT('%')).ToLong(&val) &&
1567 (val >= long(zoom)))
1568 {
1569 m_zoomControl->SetSelection(n);
1570 return;
1571 }
1572 }
7e548f6b 1573
963907fa
RR
1574 m_zoomControl->SetSelection(count-1);
1575 }
c801d85f
KB
1576}
1577
34da0970 1578int wxPreviewControlBar::GetZoomControl()
c801d85f 1579{
963907fa 1580 if (m_zoomControl && (m_zoomControl->GetStringSelection() != wxEmptyString))
7bcb11d3 1581 {
963907fa
RR
1582 long val;
1583 if (m_zoomControl->GetStringSelection().BeforeFirst(wxT('%')).ToLong(&val))
1584 return int(val);
7bcb11d3 1585 }
7e548f6b 1586
963907fa 1587 return 0;
c801d85f
KB
1588}
1589
1590
1591/*
7bcb11d3
JS
1592* Preview frame
1593*/
c801d85f 1594
e81e3883
RR
1595IMPLEMENT_CLASS(wxPreviewFrame, wxFrame)
1596
e3065973 1597BEGIN_EVENT_TABLE(wxPreviewFrame, wxFrame)
043c6705 1598 EVT_CHAR_HOOK(wxPreviewFrame::OnChar)
0f90ec93 1599 EVT_CLOSE(wxPreviewFrame::OnCloseWindow)
e3065973
JS
1600END_EVENT_TABLE()
1601
043c6705
VZ
1602void wxPreviewFrame::OnChar(wxKeyEvent &event)
1603{
1604 if ( event.GetKeyCode() == WXK_ESCAPE )
1605 {
1606 Close(true);
1607 }
1608 else
1609 {
1610 event.Skip();
1611 }
1612}
1613
a5ae8241 1614wxPreviewFrame::wxPreviewFrame(wxPrintPreviewBase *preview, wxWindow *parent, const wxString& title,
7bcb11d3 1615 const wxPoint& pos, const wxSize& size, long style, const wxString& name):
7e548f6b 1616wxFrame(parent, wxID_ANY, title, pos, size, style, name)
c801d85f 1617{
7bcb11d3
JS
1618 m_printPreview = preview;
1619 m_controlBar = NULL;
1620 m_previewCanvas = NULL;
7c995553 1621 m_windowDisabler = NULL;
6aacfc73 1622 m_modalityKind = wxPreviewFrame_NonModal;
46b24ef9 1623
a5ae8241 1624 // Give the application icon
46b24ef9
JS
1625#ifdef __WXMSW__
1626 wxFrame* topFrame = wxDynamicCast(wxTheApp->GetTopWindow(), wxFrame);
1627 if (topFrame)
7ccf316e 1628 SetIcons(topFrame->GetIcons());
e71fd398 1629#endif
c801d85f
KB
1630}
1631
34da0970 1632wxPreviewFrame::~wxPreviewFrame()
c801d85f 1633{
7bcb11d3
JS
1634 wxPrintout *printout = m_printPreview->GetPrintout();
1635 if (printout)
1636 {
1637 delete printout;
1638 m_printPreview->SetPrintout(NULL);
1639 m_printPreview->SetCanvas(NULL);
1640 m_printPreview->SetFrame(NULL);
1641 }
b88bf073
VS
1642
1643 m_previewCanvas->SetPreview(NULL);
6aacfc73
VZ
1644 delete m_printPreview;
1645}
1646
1647void wxPreviewFrame::OnCloseWindow(wxCloseEvent& WXUNUSED(event))
1648{
1649 // Reenable any windows we disabled by undoing whatever we did in our
1650 // Initialize().
1651 switch ( m_modalityKind )
1652 {
1653 case wxPreviewFrame_AppModal:
1654 delete m_windowDisabler;
1655 m_windowDisabler = NULL;
1656 break;
1657
1658 case wxPreviewFrame_WindowModal:
1659 if ( GetParent() )
1660 GetParent()->Enable();
1661 break;
1662
1663 case wxPreviewFrame_NonModal:
1664 break;
1665 }
8826f46f 1666
7bcb11d3 1667 Destroy();
c801d85f
KB
1668}
1669
1e5ad6e1 1670void wxPreviewFrame::InitializeWithModality(wxPreviewFrameModalityKind kind)
c801d85f 1671{
3080bf59 1672#if wxUSE_STATUSBAR
7bcb11d3 1673 CreateStatusBar();
3080bf59 1674#endif
7bcb11d3
JS
1675 CreateCanvas();
1676 CreateControlBar();
8826f46f 1677
7bcb11d3
JS
1678 m_printPreview->SetCanvas(m_previewCanvas);
1679 m_printPreview->SetFrame(this);
8826f46f 1680
9dff8515 1681 wxBoxSizer *item0 = new wxBoxSizer( wxVERTICAL );
8826f46f 1682
9dff8515
JS
1683 item0->Add( m_controlBar, 0, wxGROW|wxALIGN_CENTER_VERTICAL, 5 );
1684 item0->Add( m_previewCanvas, 1, wxGROW|wxALIGN_CENTER_VERTICAL, 5 );
8826f46f 1685
7e548f6b 1686 SetAutoLayout( true );
9dff8515 1687 SetSizer( item0 );
8826f46f 1688
6aacfc73
VZ
1689 m_modalityKind = kind;
1690 switch ( m_modalityKind )
1691 {
1692 case wxPreviewFrame_AppModal:
1693 // Disable everything.
1694 m_windowDisabler = new wxWindowDisabler( this );
1695 break;
1696
1697 case wxPreviewFrame_WindowModal:
1698 // Disable our parent if we have one.
1699 if ( GetParent() )
1700 GetParent()->Disable();
1701 break;
1702
1703 case wxPreviewFrame_NonModal:
1704 // Nothing to do, we don't need to disable any windows.
1705 break;
1706 }
1707
8826f46f 1708
7bcb11d3 1709 Layout();
e71fd398 1710
d2b354f9
JS
1711 m_printPreview->AdjustScrollbars(m_previewCanvas);
1712 m_previewCanvas->SetFocus();
1713 m_controlBar->SetFocus();
c801d85f
KB
1714}
1715
34da0970 1716void wxPreviewFrame::CreateCanvas()
c801d85f 1717{
7bcb11d3 1718 m_previewCanvas = new wxPreviewCanvas(m_printPreview, this);
c801d85f
KB
1719}
1720
34da0970 1721void wxPreviewFrame::CreateControlBar()
c801d85f 1722{
7bcb11d3
JS
1723 long buttons = wxPREVIEW_DEFAULT;
1724 if (m_printPreview->GetPrintoutForPrinting())
1725 buttons |= wxPREVIEW_PRINT;
8826f46f 1726
e562dfb1 1727 m_controlBar = new wxPreviewControlBar(m_printPreview, buttons, this);
7bcb11d3 1728 m_controlBar->CreateButtons();
c801d85f 1729}
7bcb11d3 1730
c801d85f 1731/*
7bcb11d3
JS
1732* Print preview
1733*/
c801d85f 1734
891daf8c
RR
1735IMPLEMENT_CLASS(wxPrintPreviewBase, wxObject)
1736
8826f46f
VZ
1737wxPrintPreviewBase::wxPrintPreviewBase(wxPrintout *printout,
1738 wxPrintout *printoutForPrinting,
1739 wxPrintData *data)
1740{
1741 if (data)
1742 m_printDialogData = (*data);
1743
1744 Init(printout, printoutForPrinting);
1745}
1746
1747wxPrintPreviewBase::wxPrintPreviewBase(wxPrintout *printout,
1748 wxPrintout *printoutForPrinting,
1749 wxPrintDialogData *data)
1750{
1751 if (data)
1752 m_printDialogData = (*data);
1753
1754 Init(printout, printoutForPrinting);
1755}
1756
1757void wxPrintPreviewBase::Init(wxPrintout *printout,
1758 wxPrintout *printoutForPrinting)
c801d85f 1759{
7e548f6b 1760 m_isOk = true;
7bcb11d3
JS
1761 m_previewPrintout = printout;
1762 if (m_previewPrintout)
1bd122dd 1763 m_previewPrintout->SetPreview(static_cast<wxPrintPreview *>(this));
8826f46f 1764
7bcb11d3 1765 m_printPrintout = printoutForPrinting;
8826f46f 1766
7bcb11d3
JS
1767 m_previewCanvas = NULL;
1768 m_previewFrame = NULL;
1769 m_previewBitmap = NULL;
88b98787 1770 m_previewFailed = false;
7bcb11d3 1771 m_currentPage = 1;
c25ccf85 1772 m_currentZoom = 70;
41e1c9d8
VZ
1773 m_topMargin =
1774 m_leftMargin = 2*wxSizerFlags::GetDefaultBorder();
7bcb11d3
JS
1775 m_pageWidth = 0;
1776 m_pageHeight = 0;
7e548f6b 1777 m_printingPrepared = false;
d2b354f9
JS
1778 m_minPage = 1;
1779 m_maxPage = 1;
c801d85f
KB
1780}
1781
34da0970 1782wxPrintPreviewBase::~wxPrintPreviewBase()
c801d85f 1783{
7bcb11d3
JS
1784 if (m_previewPrintout)
1785 delete m_previewPrintout;
1786 if (m_previewBitmap)
1787 delete m_previewBitmap;
1788 if (m_printPrintout)
1789 delete m_printPrintout;
c801d85f
KB
1790}
1791
1792bool wxPrintPreviewBase::SetCurrentPage(int pageNum)
1793{
7bcb11d3 1794 if (m_currentPage == pageNum)
7e548f6b 1795 return true;
8826f46f 1796
7bcb11d3 1797 m_currentPage = pageNum;
88b98787
VS
1798
1799 InvalidatePreviewBitmap();
e71fd398 1800
7bcb11d3
JS
1801 if (m_previewCanvas)
1802 {
d2b354f9 1803 AdjustScrollbars(m_previewCanvas);
e71fd398 1804
7bcb11d3 1805 m_previewCanvas->Refresh();
d2b354f9 1806 m_previewCanvas->SetFocus();
7bcb11d3 1807 }
7e548f6b 1808 return true;
c801d85f
KB
1809}
1810
383f6abd 1811int wxPrintPreviewBase::GetCurrentPage() const
259c43f6 1812 { return m_currentPage; }
383f6abd 1813void wxPrintPreviewBase::SetPrintout(wxPrintout *printout)
259c43f6 1814 { m_previewPrintout = printout; }
383f6abd 1815wxPrintout *wxPrintPreviewBase::GetPrintout() const
259c43f6 1816 { return m_previewPrintout; }
383f6abd 1817wxPrintout *wxPrintPreviewBase::GetPrintoutForPrinting() const
259c43f6 1818 { return m_printPrintout; }
383f6abd 1819void wxPrintPreviewBase::SetFrame(wxFrame *frame)
259c43f6 1820 { m_previewFrame = frame; }
383f6abd 1821void wxPrintPreviewBase::SetCanvas(wxPreviewCanvas *canvas)
259c43f6 1822 { m_previewCanvas = canvas; }
383f6abd 1823wxFrame *wxPrintPreviewBase::GetFrame() const
e81e3883 1824 { return m_previewFrame; }
383f6abd 1825wxPreviewCanvas *wxPrintPreviewBase::GetCanvas() const
e81e3883
RR
1826 { return m_previewCanvas; }
1827
f415cab9
JS
1828void wxPrintPreviewBase::CalcRects(wxPreviewCanvas *canvas, wxRect& pageRect, wxRect& paperRect)
1829{
1830 // Calculate the rectangles for the printable area of the page and the
1831 // entire paper as they appear on the canvas on-screen.
1832 int canvasWidth, canvasHeight;
1833 canvas->GetSize(&canvasWidth, &canvasHeight);
1834
1835 float zoomScale = float(m_currentZoom) / 100;
1836 float screenPrintableWidth = zoomScale * m_pageWidth * m_previewScaleX;
1837 float screenPrintableHeight = zoomScale * m_pageHeight * m_previewScaleY;
1838
1839 wxRect devicePaperRect = m_previewPrintout->GetPaperRectPixels();
1840 wxCoord devicePrintableWidth, devicePrintableHeight;
1841 m_previewPrintout->GetPageSizePixels(&devicePrintableWidth, &devicePrintableHeight);
1842 float scaleX = screenPrintableWidth / devicePrintableWidth;
1843 float scaleY = screenPrintableHeight / devicePrintableHeight;
1844 paperRect.width = wxCoord(scaleX * devicePaperRect.width);
1845 paperRect.height = wxCoord(scaleY * devicePaperRect.height);
1846
1847 paperRect.x = wxCoord((canvasWidth - paperRect.width)/ 2.0);
1848 if (paperRect.x < m_leftMargin)
1849 paperRect.x = m_leftMargin;
1850 paperRect.y = wxCoord((canvasHeight - paperRect.height)/ 2.0);
1851 if (paperRect.y < m_topMargin)
1852 paperRect.y = m_topMargin;
1853
1854 pageRect.x = paperRect.x - wxCoord(scaleX * devicePaperRect.x);
1855 pageRect.y = paperRect.y - wxCoord(scaleY * devicePaperRect.y);
1856 pageRect.width = wxCoord(screenPrintableWidth);
1857 pageRect.height = wxCoord(screenPrintableHeight);
1858}
1859
1860
88b98787
VS
1861void wxPrintPreviewBase::InvalidatePreviewBitmap()
1862{
1863 wxDELETE(m_previewBitmap);
1864 // if there was a problem with rendering the preview, try again now
1865 // that it changed in some way (less memory may be needed, for example):
1866 m_previewFailed = false;
1867}
1868
b88bf073
VS
1869bool wxPrintPreviewBase::UpdatePageRendering()
1870{
1871 if ( m_previewBitmap )
1872 return false;
1873
88b98787
VS
1874 if ( m_previewFailed )
1875 return false;
1876
b88bf073 1877 if ( !RenderPage(m_currentPage) )
88b98787
VS
1878 {
1879 m_previewFailed = true; // don't waste time failing again
b88bf073 1880 return false;
88b98787 1881 }
b88bf073
VS
1882
1883 return true;
1884}
1885
d2b354f9 1886bool wxPrintPreviewBase::PaintPage(wxPreviewCanvas *canvas, wxDC& dc)
c801d85f 1887{
7bcb11d3 1888 DrawBlankPage(canvas, dc);
8826f46f 1889
7bcb11d3 1890 if (!m_previewBitmap)
7e548f6b 1891 return false;
7bcb11d3 1892 if (!canvas)
7e548f6b 1893 return false;
8826f46f 1894
f415cab9
JS
1895 wxRect pageRect, paperRect;
1896 CalcRects(canvas, pageRect, paperRect);
7bcb11d3
JS
1897 wxMemoryDC temp_dc;
1898 temp_dc.SelectObject(*m_previewBitmap);
8826f46f 1899
03647350 1900 dc.Blit(pageRect.x, pageRect.y,
f415cab9 1901 m_previewBitmap->GetWidth(), m_previewBitmap->GetHeight(), &temp_dc, 0, 0);
8826f46f 1902
7bcb11d3 1903 temp_dc.SelectObject(wxNullBitmap);
7e548f6b 1904 return true;
c801d85f
KB
1905}
1906
d2b354f9
JS
1907// Adjusts the scrollbars for the current scale
1908void wxPrintPreviewBase::AdjustScrollbars(wxPreviewCanvas *canvas)
1909{
1910 if (!canvas)
1911 return ;
1912
f415cab9
JS
1913 wxRect pageRect, paperRect;
1914 CalcRects(canvas, pageRect, paperRect);
1915 int totalWidth = paperRect.width + 2 * m_leftMargin;
1916 int totalHeight = paperRect.height + 2 * m_topMargin;
1917 int scrollUnitsX = totalWidth / 10;
1918 int scrollUnitsY = totalHeight / 10;
d2b354f9
JS
1919 wxSize virtualSize = canvas->GetVirtualSize();
1920 if (virtualSize.GetWidth() != totalWidth || virtualSize.GetHeight() != totalHeight)
7e548f6b 1921 canvas->SetScrollbars(10, 10, scrollUnitsX, scrollUnitsY, 0, 0, true);
d2b354f9
JS
1922}
1923
25a3fca2
VS
1924bool wxPrintPreviewBase::RenderPageIntoDC(wxDC& dc, int pageNum)
1925{
1926 m_previewPrintout->SetDC(&dc);
1927 m_previewPrintout->SetPageSizePixels(m_pageWidth, m_pageHeight);
1928
1929 // Need to delay OnPreparePrinting() until here, so we have enough
d7cfeeb4 1930 // information and a wxDC.
25a3fca2
VS
1931 if (!m_printingPrepared)
1932 {
d7cfeeb4
VZ
1933 m_printingPrepared = true;
1934
25a3fca2
VS
1935 m_previewPrintout->OnPreparePrinting();
1936 int selFrom, selTo;
1937 m_previewPrintout->GetPageInfo(&m_minPage, &m_maxPage, &selFrom, &selTo);
d7cfeeb4
VZ
1938
1939 // Update the wxPreviewControlBar page range display.
1940 if ( m_previewFrame )
1941 {
1942 wxPreviewControlBar * const
1943 controlBar = ((wxPreviewFrame*)m_previewFrame)->GetControlBar();
1944 if ( controlBar )
1945 controlBar->SetPageInfo(m_minPage, m_maxPage);
1946 }
25a3fca2
VS
1947 }
1948
1949 m_previewPrintout->OnBeginPrinting();
1950
1951 if (!m_previewPrintout->OnBeginDocument(m_printDialogData.GetFromPage(), m_printDialogData.GetToPage()))
1952 {
1953 wxMessageBox(_("Could not start document preview."), _("Print Preview Failure"), wxOK);
1954 return false;
1955 }
1956
1957 m_previewPrintout->OnPrintPage(pageNum);
1958 m_previewPrintout->OnEndDocument();
1959 m_previewPrintout->OnEndPrinting();
1960
1961 m_previewPrintout->SetDC(NULL);
1962
1963 return true;
1964}
1965
1966bool wxPrintPreviewBase::RenderPageIntoBitmap(wxBitmap& bmp, int pageNum)
1967{
1968 wxMemoryDC memoryDC;
1969 memoryDC.SelectObject(bmp);
1970 memoryDC.Clear();
1971
1972 return RenderPageIntoDC(memoryDC, pageNum);
1973}
1974
c801d85f
KB
1975bool wxPrintPreviewBase::RenderPage(int pageNum)
1976{
f6bcfd97
BP
1977 wxBusyCursor busy;
1978
7bcb11d3 1979 if (!m_previewCanvas)
c801d85f 1980 {
9a83f860 1981 wxFAIL_MSG(wxT("wxPrintPreviewBase::RenderPage: must use wxPrintPreviewBase::SetCanvas to let me know about the canvas!"));
7e548f6b 1982 return false;
c801d85f 1983 }
8826f46f 1984
f415cab9
JS
1985 wxRect pageRect, paperRect;
1986 CalcRects(m_previewCanvas, pageRect, paperRect);
8826f46f 1987
7bcb11d3
JS
1988 if (!m_previewBitmap)
1989 {
f415cab9
JS
1990 m_previewBitmap = new wxBitmap(pageRect.width, pageRect.height);
1991
a1b806b9 1992 if (!m_previewBitmap || !m_previewBitmap->IsOk())
7bcb11d3 1993 {
88b98787 1994 InvalidatePreviewBitmap();
7bcb11d3 1995 wxMessageBox(_("Sorry, not enough memory to create a preview."), _("Print Preview Failure"), wxOK);
7e548f6b 1996 return false;
7bcb11d3
JS
1997 }
1998 }
8826f46f 1999
25a3fca2 2000 if ( !RenderPageIntoBitmap(*m_previewBitmap, pageNum) )
7bcb11d3 2001 {
88b98787 2002 InvalidatePreviewBitmap();
25a3fca2 2003 wxMessageBox(_("Sorry, not enough memory to create a preview."), _("Print Preview Failure"), wxOK);
7e548f6b 2004 return false;
7bcb11d3 2005 }
8826f46f 2006
3080bf59
VZ
2007#if wxUSE_STATUSBAR
2008 wxString status;
7bcb11d3 2009 if (m_maxPage != 0)
7e548f6b 2010 status = wxString::Format(_("Page %d of %d"), pageNum, m_maxPage);
7bcb11d3 2011 else
7e548f6b 2012 status = wxString::Format(_("Page %d"), pageNum);
8826f46f 2013
7bcb11d3 2014 if (m_previewFrame)
3080bf59
VZ
2015 m_previewFrame->SetStatusText(status);
2016#endif
8826f46f 2017
7e548f6b 2018 return true;
c801d85f
KB
2019}
2020
d2b354f9 2021bool wxPrintPreviewBase::DrawBlankPage(wxPreviewCanvas *canvas, wxDC& dc)
c801d85f 2022{
f415cab9 2023 wxRect pageRect, paperRect;
8826f46f 2024
f415cab9 2025 CalcRects(canvas, pageRect, paperRect);
8826f46f 2026
f415cab9
JS
2027 // Draw shadow, allowing for 1-pixel border AROUND the actual paper
2028 wxCoord shadowOffset = 4;
8826f46f 2029
7bcb11d3
JS
2030 dc.SetPen(*wxBLACK_PEN);
2031 dc.SetBrush(*wxBLACK_BRUSH);
f415cab9
JS
2032 dc.DrawRectangle(paperRect.x + shadowOffset, paperRect.y + paperRect.height + 1,
2033 paperRect.width, shadowOffset);
2034
2035 dc.DrawRectangle(paperRect.x + paperRect.width, paperRect.y + shadowOffset,
2036 shadowOffset, paperRect.height);
8826f46f 2037
f415cab9 2038 // Draw blank page allowing for 1-pixel border AROUND the actual paper
7bcb11d3
JS
2039 dc.SetPen(*wxBLACK_PEN);
2040 dc.SetBrush(*wxWHITE_BRUSH);
03647350 2041 dc.DrawRectangle(paperRect.x - 2, paperRect.y - 1,
f415cab9 2042 paperRect.width + 3, paperRect.height + 2);
8826f46f 2043
7e548f6b 2044 return true;
2a47d3c1
JS
2045}
2046
7bcb11d3 2047void wxPrintPreviewBase::SetZoom(int percent)
2a47d3c1 2048{
7bcb11d3
JS
2049 if (m_currentZoom == percent)
2050 return;
8826f46f 2051
7bcb11d3 2052 m_currentZoom = percent;
88b98787
VS
2053
2054 InvalidatePreviewBitmap();
aff37a19 2055
7bcb11d3
JS
2056 if (m_previewCanvas)
2057 {
d2b354f9 2058 AdjustScrollbars(m_previewCanvas);
95724b1a 2059 ((wxScrolledWindow *) m_previewCanvas)->Scroll(0, 0);
e71fd398 2060 m_previewCanvas->ClearBackground();
7bcb11d3 2061 m_previewCanvas->Refresh();
d2b354f9 2062 m_previewCanvas->SetFocus();
7bcb11d3 2063 }
2a47d3c1
JS
2064}
2065
383f6abd
WS
2066wxPrintDialogData& wxPrintPreviewBase::GetPrintDialogData()
2067{
e81e3883
RR
2068 return m_printDialogData;
2069}
2070
383f6abd 2071int wxPrintPreviewBase::GetZoom() const
e81e3883 2072{ return m_currentZoom; }
383f6abd 2073int wxPrintPreviewBase::GetMaxPage() const
e81e3883 2074{ return m_maxPage; }
383f6abd 2075int wxPrintPreviewBase::GetMinPage() const
e81e3883 2076{ return m_minPage; }
b7cacb43 2077bool wxPrintPreviewBase::IsOk() const
e81e3883 2078{ return m_isOk; }
383f6abd 2079void wxPrintPreviewBase::SetOk(bool ok)
e81e3883 2080{ m_isOk = ok; }
f415cab9 2081
e81e3883
RR
2082//----------------------------------------------------------------------------
2083// wxPrintPreview
2084//----------------------------------------------------------------------------
2085
2086IMPLEMENT_CLASS(wxPrintPreview, wxPrintPreviewBase)
2087
2088wxPrintPreview::wxPrintPreview(wxPrintout *printout,
2089 wxPrintout *printoutForPrinting,
2090 wxPrintDialogData *data) :
2091 wxPrintPreviewBase( printout, printoutForPrinting, data )
2092{
2093 m_pimpl = wxPrintFactory::GetFactory()->
2094 CreatePrintPreview( printout, printoutForPrinting, data );
2095}
2096
2097wxPrintPreview::wxPrintPreview(wxPrintout *printout,
2098 wxPrintout *printoutForPrinting,
2099 wxPrintData *data ) :
2100 wxPrintPreviewBase( printout, printoutForPrinting, data )
2101{
2102 m_pimpl = wxPrintFactory::GetFactory()->
2103 CreatePrintPreview( printout, printoutForPrinting, data );
2104}
2105
2106wxPrintPreview::~wxPrintPreview()
2107{
2108 delete m_pimpl;
383f6abd 2109
e81e3883
RR
2110 // don't delete twice
2111 m_printPrintout = NULL;
2112 m_previewPrintout = NULL;
2113 m_previewBitmap = NULL;
2114}
2115
2116bool wxPrintPreview::SetCurrentPage(int pageNum)
2117{
2118 return m_pimpl->SetCurrentPage( pageNum );
2119}
2120
383f6abd
WS
2121int wxPrintPreview::GetCurrentPage() const
2122{
e81e3883
RR
2123 return m_pimpl->GetCurrentPage();
2124}
2125
383f6abd
WS
2126void wxPrintPreview::SetPrintout(wxPrintout *printout)
2127{
e81e3883
RR
2128 m_pimpl->SetPrintout( printout );
2129}
2130
383f6abd
WS
2131wxPrintout *wxPrintPreview::GetPrintout() const
2132{
e81e3883
RR
2133 return m_pimpl->GetPrintout();
2134}
2135
383f6abd
WS
2136wxPrintout *wxPrintPreview::GetPrintoutForPrinting() const
2137{
e81e3883
RR
2138 return m_pimpl->GetPrintoutForPrinting();
2139}
2140
383f6abd
WS
2141void wxPrintPreview::SetFrame(wxFrame *frame)
2142{
e81e3883
RR
2143 m_pimpl->SetFrame( frame );
2144}
2145
383f6abd
WS
2146void wxPrintPreview::SetCanvas(wxPreviewCanvas *canvas)
2147{
e81e3883
RR
2148 m_pimpl->SetCanvas( canvas );
2149}
2150
383f6abd 2151wxFrame *wxPrintPreview::GetFrame() const
e81e3883
RR
2152{
2153 return m_pimpl->GetFrame();
2154}
2155
383f6abd
WS
2156wxPreviewCanvas *wxPrintPreview::GetCanvas() const
2157{
e81e3883
RR
2158 return m_pimpl->GetCanvas();
2159}
2160
2161bool wxPrintPreview::PaintPage(wxPreviewCanvas *canvas, wxDC& dc)
2162{
2163 return m_pimpl->PaintPage( canvas, dc );
2164}
2165
b88bf073
VS
2166bool wxPrintPreview::UpdatePageRendering()
2167{
2168 return m_pimpl->UpdatePageRendering();
2169}
2170
e81e3883
RR
2171bool wxPrintPreview::DrawBlankPage(wxPreviewCanvas *canvas, wxDC& dc)
2172{
2173 return m_pimpl->DrawBlankPage( canvas, dc );
2174}
2175
2176void wxPrintPreview::AdjustScrollbars(wxPreviewCanvas *canvas)
2177{
2178 m_pimpl->AdjustScrollbars( canvas );
2179}
2180
2181bool wxPrintPreview::RenderPage(int pageNum)
2182{
2183 return m_pimpl->RenderPage( pageNum );
2184}
2185
2186void wxPrintPreview::SetZoom(int percent)
2187{
2188 m_pimpl->SetZoom( percent );
2189}
2190
d8766675
JS
2191int wxPrintPreview::GetZoom() const
2192{
2193 return m_pimpl->GetZoom();
2194}
2195
e81e3883
RR
2196wxPrintDialogData& wxPrintPreview::GetPrintDialogData()
2197{
2198 return m_pimpl->GetPrintDialogData();
2199}
2200
2201int wxPrintPreview::GetMaxPage() const
2202{
2203 return m_pimpl->GetMaxPage();
2204}
2205
2206int wxPrintPreview::GetMinPage() const
2207{
2208 return m_pimpl->GetMinPage();
2209}
2210
b7cacb43 2211bool wxPrintPreview::IsOk() const
e81e3883 2212{
a1b806b9 2213 return m_pimpl->IsOk();
e81e3883
RR
2214}
2215
2216void wxPrintPreview::SetOk(bool ok)
2217{
2218 m_pimpl->SetOk( ok );
2219}
2220
2221bool wxPrintPreview::Print(bool interactive)
2222{
2223 return m_pimpl->Print( interactive );
2224}
2225
2226void wxPrintPreview::DetermineScaling()
2227{
2228 m_pimpl->DetermineScaling();
2229}
2230
d427503c 2231#endif // wxUSE_PRINTING_ARCHITECTURE