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