]> git.saurik.com Git - wxWidgets.git/blame - src/common/prntbase.cpp
speed up for debug mode startup (doesn't matter much but as it doesn't cost anything...
[wxWidgets.git] / src / common / prntbase.cpp
CommitLineData
c801d85f
KB
1/////////////////////////////////////////////////////////////////////////////
2// Name: prntbase.cpp
3// Purpose: Printing framework base class implementation
4// Author: Julian Smart
5// Modified by:
6// Created: 04/01/98
7// RCS-ID: $Id$
55d99c7a
JS
8// Copyright: (c) Julian Smart
9// Licence: wxWindows licence
c801d85f
KB
10/////////////////////////////////////////////////////////////////////////////
11
12#ifdef __GNUG__
13#pragma implementation "prntbase.h"
14#endif
15
16// For compilers that support precompilation, includes "wx.h".
17#include "wx/wxprec.h"
18
19#ifdef __BORLANDC__
20#pragma hdrstop
21#endif
22
c801d85f
KB
23#include "wx/defs.h"
24
d427503c
VZ
25#if wxUSE_PRINTING_ARCHITECTURE
26
c801d85f
KB
27#ifndef WX_PRECOMP
28#include "wx/utils.h"
29#include "wx/dc.h"
30#include "wx/app.h"
31#include "wx/msgdlg.h"
32#include "wx/layout.h"
33#include "wx/choice.h"
34#include "wx/button.h"
35#include "wx/settings.h"
36#include "wx/dcmemory.h"
37#include "wx/stattext.h"
38#include "wx/intl.h"
384a1303 39#include "wx/textdlg.h"
2b5f62a0
VZ
40#include "wx/sizer.h"
41#endif // !WX_PRECOMP
c801d85f
KB
42
43#include "wx/prntbase.h"
44#include "wx/dcprint.h"
45#include "wx/printdlg.h"
2a47d3c1 46#include "wx/module.h"
c801d85f
KB
47
48#include <stdlib.h>
49#include <string.h>
50
2049ba38 51#ifdef __WXMSW__
d427503c
VZ
52 #include "wx/msw/private.h"
53 #include <commdlg.h>
c801d85f 54
d427503c
VZ
55 #ifndef __WIN32__
56 #include <print.h>
57 #endif
58#endif // __WXMSW__
c801d85f 59
c801d85f
KB
60IMPLEMENT_CLASS(wxPrinterBase, wxObject)
61IMPLEMENT_ABSTRACT_CLASS(wxPrintout, wxObject)
62IMPLEMENT_CLASS(wxPreviewCanvas, wxWindow)
63IMPLEMENT_CLASS(wxPreviewControlBar, wxWindow)
64IMPLEMENT_CLASS(wxPreviewFrame, wxFrame)
65IMPLEMENT_CLASS(wxPrintPreviewBase, wxObject)
66
67BEGIN_EVENT_TABLE(wxPrintAbortDialog, wxDialog)
d427503c 68 EVT_BUTTON(wxID_CANCEL, wxPrintAbortDialog::OnCancel)
c801d85f
KB
69END_EVENT_TABLE()
70
71BEGIN_EVENT_TABLE(wxPreviewCanvas, wxScrolledWindow)
d427503c
VZ
72 EVT_PAINT(wxPreviewCanvas::OnPaint)
73 EVT_SYS_COLOUR_CHANGED(wxPreviewCanvas::OnSysColourChanged)
c801d85f 74END_EVENT_TABLE()
c801d85f
KB
75
76/*
7bcb11d3
JS
77* Printer
78*/
79
80wxPrinterBase::wxPrinterBase(wxPrintDialogData *data)
c801d85f 81{
7bcb11d3
JS
82 m_currentPrintout = (wxPrintout *) NULL;
83 sm_abortWindow = (wxWindow *) NULL;
84 sm_abortIt = FALSE;
85 if (data)
86 m_printDialogData = (*data);
f6bcfd97 87 sm_lastError = wxPRINTER_NO_ERROR;
c801d85f
KB
88}
89
34da0970
JS
90wxWindow *wxPrinterBase::sm_abortWindow = (wxWindow *) NULL;
91bool wxPrinterBase::sm_abortIt = FALSE;
f6bcfd97 92wxPrinterError wxPrinterBase::sm_lastError = wxPRINTER_NO_ERROR;
c801d85f 93
34da0970 94wxPrinterBase::~wxPrinterBase()
c801d85f
KB
95{
96}
97
98void wxPrintAbortDialog::OnCancel(wxCommandEvent& WXUNUSED(event))
99{
7bcb11d3
JS
100 wxPrinterBase::sm_abortIt = TRUE;
101 wxPrinterBase::sm_abortWindow->Show(FALSE);
102 wxPrinterBase::sm_abortWindow->Close(TRUE);
103 wxPrinterBase::sm_abortWindow = (wxWindow *) NULL;
c801d85f
KB
104}
105
fc799548 106wxWindow *wxPrinterBase::CreateAbortWindow(wxWindow *parent, wxPrintout * printout)
c801d85f 107{
fc799548 108 wxPrintAbortDialog *dialog = new wxPrintAbortDialog(parent, _("Printing ") , wxDefaultPosition, wxDefaultSize, wxDEFAULT_DIALOG_STYLE);
8826f46f 109
fc799548
JS
110 wxBoxSizer *button_sizer = new wxBoxSizer( wxVERTICAL );
111 button_sizer->Add( new wxStaticText(dialog, -1, _("Please wait while printing\n") + printout->GetTitle() ), 0, wxALL, 10 );
112 button_sizer->Add( new wxButton( dialog, wxID_CANCEL, wxT("Cancel") ), 0, wxALL | wxALIGN_CENTER, 10 );
8826f46f 113
fc799548
JS
114 dialog->SetAutoLayout( TRUE );
115 dialog->SetSizer( button_sizer );
116
117 button_sizer->Fit(dialog);
118 button_sizer->SetSizeHints (dialog) ;
8826f46f 119
7bcb11d3 120 return dialog;
c801d85f
KB
121}
122
161f4f73 123void wxPrinterBase::ReportError(wxWindow *parent, wxPrintout *WXUNUSED(printout), const wxString& message)
c801d85f 124{
7bcb11d3 125 wxMessageBox(message, _("Printing Error"), wxOK, parent);
c801d85f
KB
126}
127
128/*
7bcb11d3
JS
129* Printout class
130*/
131
34da0970 132wxPrintout::wxPrintout(const wxString& title)
c801d85f 133{
7bcb11d3
JS
134 m_printoutTitle = title ;
135 m_printoutDC = (wxDC *) NULL;
136 m_pageWidthMM = 0;
137 m_pageHeightMM = 0;
138 m_pageWidthPixels = 0;
139 m_pageHeightPixels = 0;
140 m_PPIScreenX = 0;
141 m_PPIScreenY = 0;
142 m_PPIPrinterX = 0;
143 m_PPIPrinterY = 0;
144 m_isPreview = FALSE;
c801d85f
KB
145}
146
34da0970 147wxPrintout::~wxPrintout()
c801d85f 148{
c801d85f
KB
149}
150
151bool wxPrintout::OnBeginDocument(int WXUNUSED(startPage), int WXUNUSED(endPage))
152{
fc799548 153 return GetDC()->StartDoc(_("Printing ") + m_printoutTitle);
c801d85f
KB
154}
155
34da0970 156void wxPrintout::OnEndDocument()
c801d85f 157{
7bcb11d3 158 GetDC()->EndDoc();
c801d85f
KB
159}
160
34da0970 161void wxPrintout::OnBeginPrinting()
c801d85f
KB
162{
163}
164
34da0970 165void wxPrintout::OnEndPrinting()
c801d85f
KB
166{
167}
168
169bool wxPrintout::HasPage(int page)
170{
7bcb11d3 171 return (page == 1);
c801d85f
KB
172}
173
174void wxPrintout::GetPageInfo(int *minPage, int *maxPage, int *fromPage, int *toPage)
175{
7bcb11d3
JS
176 *minPage = 1;
177 *maxPage = 32000;
178 *fromPage = 1;
179 *toPage = 1;
c801d85f
KB
180}
181
182/*
7bcb11d3
JS
183* Preview canvas
184*/
185
c801d85f 186wxPreviewCanvas::wxPreviewCanvas(wxPrintPreviewBase *preview, wxWindow *parent,
7bcb11d3
JS
187 const wxPoint& pos, const wxSize& size, long style, const wxString& name):
188wxScrolledWindow(parent, -1, pos, size, style, name)
c801d85f 189{
7bcb11d3 190 m_printPreview = preview;
46b24ef9
JS
191#ifdef __WXMAC__
192 // The app workspace colour is always white, but we should have
193 // a contrast with the page.
ca5020c2 194 wxSystemColour colourIndex = wxSYS_COLOUR_3DDKSHADOW;
46b24ef9
JS
195#else
196 wxSystemColour colourIndex = wxSYS_COLOUR_APPWORKSPACE;
197#endif
198 SetBackgroundColour(wxSystemSettings::GetColour(colourIndex));
8826f46f 199
7bcb11d3 200 SetScrollbars(15, 18, 100, 100);
c801d85f
KB
201}
202
34da0970 203wxPreviewCanvas::~wxPreviewCanvas()
c801d85f
KB
204{
205}
206
207void wxPreviewCanvas::OnPaint(wxPaintEvent& WXUNUSED(event))
208{
7bcb11d3
JS
209 wxPaintDC dc(this);
210 PrepareDC( dc );
8826f46f 211
a56fcaaf 212/*
809934d2
RR
213#ifdef __WXGTK__
214 if (!GetUpdateRegion().IsEmpty())
215 dc.SetClippingRegion( GetUpdateRegion() );
216#endif
a56fcaaf 217*/
809934d2 218
7bcb11d3
JS
219 if (m_printPreview)
220 {
221 m_printPreview->PaintPage(this, dc);
222 }
c801d85f
KB
223}
224
225// Responds to colour changes, and passes event on to children.
226void wxPreviewCanvas::OnSysColourChanged(wxSysColourChangedEvent& event)
227{
46b24ef9
JS
228#ifdef __WXMAC__
229 // The app workspace colour is always white, but we should have
230 // a contrast with the page.
ca5020c2 231 wxSystemColour colourIndex = wxSYS_COLOUR_3DDKSHADOW;
46b24ef9
JS
232#else
233 wxSystemColour colourIndex = wxSYS_COLOUR_APPWORKSPACE;
234#endif
235 SetBackgroundColour(wxSystemSettings::GetColour(colourIndex));
7bcb11d3 236 Refresh();
8826f46f 237
7bcb11d3
JS
238 // Propagate the event to the non-top-level children
239 wxWindow::OnSysColourChanged(event);
c801d85f
KB
240}
241
242/*
7bcb11d3
JS
243* Preview control bar
244*/
c801d85f
KB
245
246BEGIN_EVENT_TABLE(wxPreviewControlBar, wxPanel)
8826f46f
VZ
247 EVT_BUTTON(wxID_PREVIEW_CLOSE, wxPreviewControlBar::OnWindowClose)
248 EVT_BUTTON(wxID_PREVIEW_PRINT, wxPreviewControlBar::OnPrint)
0f90ec93
KB
249 EVT_BUTTON(wxID_PREVIEW_PREVIOUS, wxPreviewControlBar::OnPreviousButton)
250 EVT_BUTTON(wxID_PREVIEW_NEXT, wxPreviewControlBar::OnNextButton)
bf89b538
JS
251 EVT_BUTTON(wxID_PREVIEW_FIRST, wxPreviewControlBar::OnFirstButton)
252 EVT_BUTTON(wxID_PREVIEW_LAST, wxPreviewControlBar::OnLastButton)
253 EVT_BUTTON(wxID_PREVIEW_GOTO, wxPreviewControlBar::OnGotoButton)
0f90ec93 254 EVT_CHAR(wxPreviewControlBar::OnChar)
8826f46f
VZ
255 EVT_CHOICE(wxID_PREVIEW_ZOOM, wxPreviewControlBar::OnZoom)
256 EVT_PAINT(wxPreviewControlBar::OnPaint)
c801d85f 257END_EVENT_TABLE()
7bcb11d3 258
c801d85f 259wxPreviewControlBar::wxPreviewControlBar(wxPrintPreviewBase *preview, long buttons,
7bcb11d3
JS
260 wxWindow *parent, const wxPoint& pos, const wxSize& size,
261 long style, const wxString& name):
262wxPanel(parent, -1, pos, size, style, name)
c801d85f 263{
7bcb11d3
JS
264 m_printPreview = preview;
265 m_closeButton = (wxButton *) NULL;
266 m_nextPageButton = (wxButton *) NULL;
267 m_previousPageButton = (wxButton *) NULL;
268 m_printButton = (wxButton *) NULL;
269 m_zoomControl = (wxChoice *) NULL;
270 m_buttonFlags = buttons;
c801d85f
KB
271}
272
34da0970 273wxPreviewControlBar::~wxPreviewControlBar()
c801d85f
KB
274{
275}
276
277void wxPreviewControlBar::OnPaint(wxPaintEvent& WXUNUSED(event))
278{
7bcb11d3 279 wxPaintDC dc(this);
8826f46f 280
7bcb11d3
JS
281 int w, h;
282 GetSize(&w, &h);
283 dc.SetPen(*wxBLACK_PEN);
284 dc.SetBrush(*wxTRANSPARENT_BRUSH);
285 dc.DrawLine( 0, h-1, w, h-1 );
c801d85f
KB
286}
287
c330a2cf 288void wxPreviewControlBar::OnWindowClose(wxCommandEvent& WXUNUSED(event))
c801d85f 289{
7bcb11d3
JS
290 wxPreviewFrame *frame = (wxPreviewFrame *)GetParent();
291 frame->Close(TRUE);
c801d85f
KB
292}
293
294void wxPreviewControlBar::OnPrint(wxCommandEvent& WXUNUSED(event))
295{
7bcb11d3
JS
296 wxPrintPreviewBase *preview = GetPrintPreview();
297 preview->Print(TRUE);
c801d85f
KB
298}
299
0f90ec93
KB
300void wxPreviewControlBar::OnChar(wxKeyEvent &event)
301{
12a3f227 302 switch(event.GetKeyCode())
0f90ec93
KB
303 {
304 case WXK_NEXT:
305 OnNext(); break;
306 case WXK_PRIOR:
307 OnPrevious(); break;
bf89b538
JS
308 case WXK_HOME:
309 OnFirst(); break;
310 case WXK_END:
311 OnLast(); break;
312 case WXK_TAB:
313 OnGoto(); break;
0f90ec93
KB
314 default:
315 event.Skip();
316 }
317}
318
319void wxPreviewControlBar::OnNext(void)
c801d85f 320{
7bcb11d3
JS
321 wxPrintPreviewBase *preview = GetPrintPreview();
322 if (preview)
c801d85f 323 {
7bcb11d3
JS
324 int currentPage = preview->GetCurrentPage();
325 if ((preview->GetMaxPage() > 0) &&
326 (currentPage < preview->GetMaxPage()) &&
327 preview->GetPrintout()->HasPage(currentPage + 1))
328 {
329 preview->SetCurrentPage(currentPage + 1);
330 }
c801d85f 331 }
c801d85f
KB
332}
333
0f90ec93 334void wxPreviewControlBar::OnPrevious(void)
c801d85f 335{
7bcb11d3
JS
336 wxPrintPreviewBase *preview = GetPrintPreview();
337 if (preview)
c801d85f 338 {
7bcb11d3
JS
339 int currentPage = preview->GetCurrentPage();
340 if ((preview->GetMinPage() > 0) &&
341 (currentPage > preview->GetMinPage()) &&
342 preview->GetPrintout()->HasPage(currentPage - 1))
343 {
344 preview->SetCurrentPage(currentPage - 1);
345 }
c801d85f 346 }
c801d85f
KB
347}
348
bf89b538
JS
349void wxPreviewControlBar::OnFirst(void)
350{
351 wxPrintPreviewBase *preview = GetPrintPreview();
352 if (preview)
353 {
354 int currentPage = preview->GetMinPage();
355 if (preview->GetPrintout()->HasPage(currentPage))
356 {
357 preview->SetCurrentPage(currentPage);
358 }
359 }
360}
361
362void wxPreviewControlBar::OnLast(void)
363{
364 wxPrintPreviewBase *preview = GetPrintPreview();
365 if (preview)
366 {
367 int currentPage = preview->GetMaxPage();
368 if (preview->GetPrintout()->HasPage(currentPage))
369 {
370 preview->SetCurrentPage(currentPage);
371 }
372 }
373}
374
375void wxPreviewControlBar::OnGoto(void)
376{
377 wxPrintPreviewBase *preview = GetPrintPreview();
378 if (preview)
379 {
380 long currentPage;
381
382 if (preview->GetMinPage() > 0)
383 {
384 wxString strPrompt;
385 wxString strPage;
386
7e99eddf 387 strPrompt.Printf( wxT("%d...%d"),
bf89b538 388 preview->GetMinPage(), preview->GetMaxPage());
7e99eddf 389 strPage.Printf( wxT("%d"), preview->GetCurrentPage() );
bf89b538
JS
390
391 strPage =
392 wxGetTextFromUser( strPrompt, _("Goto Page"), strPage);
393
394 if ( strPage.ToLong( &currentPage ) )
395 if (preview->GetPrintout()->HasPage(currentPage))
396 {
397 preview->SetCurrentPage(currentPage);
398 }
399 }
400 }
401}
402
c801d85f
KB
403void wxPreviewControlBar::OnZoom(wxCommandEvent& WXUNUSED(event))
404{
7bcb11d3
JS
405 int zoom = GetZoomControl();
406 if (GetPrintPreview())
407 GetPrintPreview()->SetZoom(zoom);
c801d85f
KB
408}
409
34da0970 410void wxPreviewControlBar::CreateButtons()
c801d85f 411{
7bcb11d3 412 SetSize(0, 0, 400, 40);
8826f46f 413
9dff8515
JS
414 wxBoxSizer *item0 = new wxBoxSizer( wxHORIZONTAL );
415
416 int smallButtonWidth = 45;
417
418 m_closeButton = new wxButton( this, wxID_PREVIEW_CLOSE, _("&Close"), wxDefaultPosition, wxDefaultSize, 0 );
419 item0->Add( m_closeButton, 0, wxALIGN_CENTRE|wxALL, 5 );
420
7bcb11d3
JS
421 if (m_buttonFlags & wxPREVIEW_PRINT)
422 {
9dff8515
JS
423 m_printButton = new wxButton( this, wxID_PREVIEW_PRINT, _("&Print..."), wxDefaultPosition, wxDefaultSize, 0 );
424 item0->Add( m_printButton, 0, wxALIGN_CENTRE|wxALL, 5 );
7bcb11d3 425 }
9dff8515 426
bf89b538
JS
427 if (m_buttonFlags & wxPREVIEW_FIRST)
428 {
9dff8515
JS
429 m_firstPageButton = new wxButton( this, wxID_PREVIEW_FIRST, _("|<<"), wxDefaultPosition, wxSize(smallButtonWidth,-1), 0 );
430 item0->Add( m_firstPageButton, 0, wxALIGN_CENTRE|wxALL, 5 );
bf89b538 431 }
9dff8515 432
7bcb11d3
JS
433 if (m_buttonFlags & wxPREVIEW_PREVIOUS)
434 {
9dff8515
JS
435 m_previousPageButton = new wxButton( this, wxID_PREVIEW_PREVIOUS, _("<<"), wxDefaultPosition, wxSize(smallButtonWidth,-1), 0 );
436 item0->Add( m_previousPageButton, 0, wxALIGN_CENTRE|wxRIGHT|wxTOP|wxBOTTOM, 5 );
7bcb11d3 437 }
9dff8515 438
7bcb11d3
JS
439 if (m_buttonFlags & wxPREVIEW_NEXT)
440 {
9dff8515
JS
441 m_nextPageButton = new wxButton( this, wxID_PREVIEW_NEXT, _(">>"), wxDefaultPosition, wxSize(smallButtonWidth,-1), 0 );
442 item0->Add( m_nextPageButton, 0, wxALIGN_CENTRE|wxRIGHT|wxTOP|wxBOTTOM, 5 );
bf89b538 443 }
9dff8515 444
bf89b538
JS
445 if (m_buttonFlags & wxPREVIEW_LAST)
446 {
9dff8515
JS
447 m_lastPageButton = new wxButton( this, wxID_PREVIEW_LAST, _(">>|"), wxDefaultPosition, wxSize(smallButtonWidth,-1), 0 );
448 item0->Add( m_lastPageButton, 0, wxALIGN_CENTRE|wxRIGHT|wxTOP|wxBOTTOM, 5 );
bf89b538 449 }
9dff8515 450
bf89b538
JS
451 if (m_buttonFlags & wxPREVIEW_GOTO)
452 {
9dff8515
JS
453 m_gotoPageButton = new wxButton( this, wxID_PREVIEW_GOTO, _("&Goto..."), wxDefaultPosition, wxDefaultSize, 0 );
454 item0->Add( m_gotoPageButton, 0, wxALIGN_CENTRE|wxALL, 5 );
7bcb11d3 455 }
9dff8515 456
7bcb11d3
JS
457 if (m_buttonFlags & wxPREVIEW_ZOOM)
458 {
9dff8515 459 wxString choices[] =
c25ccf85 460 {
2b5f62a0 461 wxT("10%"), wxT("15%"), wxT("20%"), wxT("25%"), wxT("30%"), wxT("35%"), wxT("40%"), wxT("45%"), wxT("50%"), wxT("55%"),
9dff8515
JS
462 wxT("60%"), wxT("65%"), wxT("70%"), wxT("75%"), wxT("80%"), wxT("85%"), wxT("90%"), wxT("95%"), wxT("100%"), wxT("110%"),
463 wxT("120%"), wxT("150%"), wxT("200%")
c25ccf85 464 };
c25ccf85 465 int n = WXSIZEOF(choices);
9dff8515
JS
466
467 m_zoomControl = new wxChoice( this, wxID_PREVIEW_ZOOM, wxDefaultPosition, wxSize(70,-1), n, choices, 0 );
468 item0->Add( m_zoomControl, 0, wxALIGN_CENTRE|wxALL, 5 );
7bcb11d3
JS
469 SetZoomControl(m_printPreview->GetZoom());
470 }
8826f46f 471
9dff8515
JS
472 SetSizer(item0);
473 item0->Fit(this);
c801d85f
KB
474}
475
476void wxPreviewControlBar::SetZoomControl(int zoom)
477{
2b5f62a0
VZ
478 wxChar buf[20];
479 wxSprintf( buf, wxT("%d%%"), zoom );
480
7bcb11d3
JS
481 if (m_zoomControl)
482 m_zoomControl->SetStringSelection(buf);
c801d85f
KB
483}
484
34da0970 485int wxPreviewControlBar::GetZoomControl()
c801d85f 486{
cf2f341a 487 wxChar buf[20];
223d09f6 488 if (m_zoomControl && (m_zoomControl->GetStringSelection() != wxT("")))
7bcb11d3 489 {
cf2f341a
OK
490 wxStrcpy(buf, m_zoomControl->GetStringSelection());
491 buf[wxStrlen(buf) - 1] = 0;
492 return (int)wxAtoi(buf);
7bcb11d3
JS
493 }
494 else return 0;
c801d85f
KB
495}
496
497
498/*
7bcb11d3
JS
499* Preview frame
500*/
c801d85f 501
e3065973 502BEGIN_EVENT_TABLE(wxPreviewFrame, wxFrame)
0f90ec93 503 EVT_CLOSE(wxPreviewFrame::OnCloseWindow)
e3065973
JS
504END_EVENT_TABLE()
505
a5ae8241 506wxPreviewFrame::wxPreviewFrame(wxPrintPreviewBase *preview, wxWindow *parent, const wxString& title,
7bcb11d3
JS
507 const wxPoint& pos, const wxSize& size, long style, const wxString& name):
508wxFrame(parent, -1, title, pos, size, style, name)
c801d85f 509{
7bcb11d3
JS
510 m_printPreview = preview;
511 m_controlBar = NULL;
512 m_previewCanvas = NULL;
46b24ef9 513
a5ae8241 514 // Give the application icon
46b24ef9
JS
515#ifdef __WXMSW__
516 wxFrame* topFrame = wxDynamicCast(wxTheApp->GetTopWindow(), wxFrame);
517 if (topFrame)
518 SetIcon(topFrame->GetIcon());
519#endif
c801d85f
KB
520}
521
34da0970 522wxPreviewFrame::~wxPreviewFrame()
c801d85f
KB
523{
524}
525
74e3313b 526void wxPreviewFrame::OnCloseWindow(wxCloseEvent& WXUNUSED(event))
c801d85f 527{
feea0f95
JS
528 // MakeModal doesn't work on wxMac, especially when there
529 // are multiple top-level windows.
530#ifndef __WXMAC__
7bcb11d3 531 MakeModal(FALSE);
feea0f95 532#endif
8826f46f 533
7bcb11d3
JS
534 // Need to delete the printout and the print preview
535 wxPrintout *printout = m_printPreview->GetPrintout();
536 if (printout)
537 {
538 delete printout;
539 m_printPreview->SetPrintout(NULL);
540 m_printPreview->SetCanvas(NULL);
541 m_printPreview->SetFrame(NULL);
542 }
543 delete m_printPreview;
8826f46f 544
7bcb11d3 545 Destroy();
c801d85f
KB
546}
547
34da0970 548void wxPreviewFrame::Initialize()
c801d85f 549{
3080bf59 550#if wxUSE_STATUSBAR
7bcb11d3 551 CreateStatusBar();
3080bf59 552#endif
7bcb11d3
JS
553 CreateCanvas();
554 CreateControlBar();
8826f46f 555
7bcb11d3
JS
556 m_printPreview->SetCanvas(m_previewCanvas);
557 m_printPreview->SetFrame(this);
8826f46f 558
9dff8515 559 wxBoxSizer *item0 = new wxBoxSizer( wxVERTICAL );
8826f46f 560
9dff8515
JS
561 item0->Add( m_controlBar, 0, wxGROW|wxALIGN_CENTER_VERTICAL, 5 );
562 item0->Add( m_previewCanvas, 1, wxGROW|wxALIGN_CENTER_VERTICAL, 5 );
8826f46f 563
9dff8515
JS
564 SetAutoLayout( TRUE );
565 SetSizer( item0 );
8826f46f 566
feea0f95
JS
567 // MakeModal doesn't work on wxMac, especially when there
568 // are multiple top-level windows.
569#ifndef __WXMAC__
7bcb11d3 570 MakeModal(TRUE);
feea0f95 571#endif
8826f46f 572
7bcb11d3 573 Layout();
c801d85f
KB
574}
575
34da0970 576void wxPreviewFrame::CreateCanvas()
c801d85f 577{
7bcb11d3 578 m_previewCanvas = new wxPreviewCanvas(m_printPreview, this);
c801d85f
KB
579}
580
34da0970 581void wxPreviewFrame::CreateControlBar()
c801d85f 582{
7bcb11d3
JS
583 long buttons = wxPREVIEW_DEFAULT;
584 if (m_printPreview->GetPrintoutForPrinting())
585 buttons |= wxPREVIEW_PRINT;
8826f46f 586
7bcb11d3
JS
587 m_controlBar = new wxPreviewControlBar(m_printPreview, buttons, this, wxPoint(0, 0), wxSize(400, 40));
588 m_controlBar->CreateButtons();
c801d85f 589}
7bcb11d3 590
c801d85f 591/*
7bcb11d3
JS
592* Print preview
593*/
c801d85f 594
8826f46f
VZ
595wxPrintPreviewBase::wxPrintPreviewBase(wxPrintout *printout,
596 wxPrintout *printoutForPrinting,
597 wxPrintData *data)
598{
599 if (data)
600 m_printDialogData = (*data);
601
602 Init(printout, printoutForPrinting);
603}
604
605wxPrintPreviewBase::wxPrintPreviewBase(wxPrintout *printout,
606 wxPrintout *printoutForPrinting,
607 wxPrintDialogData *data)
608{
609 if (data)
610 m_printDialogData = (*data);
611
612 Init(printout, printoutForPrinting);
613}
614
615void wxPrintPreviewBase::Init(wxPrintout *printout,
616 wxPrintout *printoutForPrinting)
c801d85f 617{
7bcb11d3
JS
618 m_isOk = TRUE;
619 m_previewPrintout = printout;
620 if (m_previewPrintout)
621 m_previewPrintout->SetIsPreview(TRUE);
8826f46f 622
7bcb11d3 623 m_printPrintout = printoutForPrinting;
8826f46f 624
7bcb11d3
JS
625 m_previewCanvas = NULL;
626 m_previewFrame = NULL;
627 m_previewBitmap = NULL;
628 m_currentPage = 1;
c25ccf85 629 m_currentZoom = 70;
7bcb11d3
JS
630 m_topMargin = 40;
631 m_leftMargin = 40;
632 m_pageWidth = 0;
633 m_pageHeight = 0;
1044a386 634 m_printingPrepared = FALSE;
8826f46f 635
1044a386
JS
636 // Too soon! Moved to RenderPage.
637 // printout->OnPreparePrinting();
8826f46f 638
7bcb11d3
JS
639 // Get some parameters from the printout, if defined
640 int selFrom, selTo;
641 printout->GetPageInfo(&m_minPage, &m_maxPage, &selFrom, &selTo);
c801d85f
KB
642}
643
34da0970 644wxPrintPreviewBase::~wxPrintPreviewBase()
c801d85f 645{
7bcb11d3
JS
646 if (m_previewPrintout)
647 delete m_previewPrintout;
648 if (m_previewBitmap)
649 delete m_previewBitmap;
650 if (m_printPrintout)
651 delete m_printPrintout;
c801d85f
KB
652}
653
654bool wxPrintPreviewBase::SetCurrentPage(int pageNum)
655{
7bcb11d3
JS
656 if (m_currentPage == pageNum)
657 return TRUE;
8826f46f 658
7bcb11d3
JS
659 m_currentPage = pageNum;
660 if (m_previewBitmap)
661 {
662 delete m_previewBitmap;
663 m_previewBitmap = NULL;
664 }
8826f46f 665
7bcb11d3
JS
666 if (m_previewCanvas)
667 {
9eee81a4 668 if (!RenderPage(pageNum))
bf89b538 669 return FALSE;
7bcb11d3
JS
670 m_previewCanvas->Refresh();
671 }
c801d85f 672 return TRUE;
c801d85f
KB
673}
674
675bool wxPrintPreviewBase::PaintPage(wxWindow *canvas, wxDC& dc)
676{
7bcb11d3 677 DrawBlankPage(canvas, dc);
8826f46f 678
7bcb11d3 679 if (!m_previewBitmap)
9eee81a4 680 if (!RenderPage(m_currentPage))
bf89b538 681 return FALSE;
8826f46f 682
7bcb11d3
JS
683 if (!m_previewBitmap)
684 return FALSE;
8826f46f 685
7bcb11d3
JS
686 if (!canvas)
687 return FALSE;
8826f46f 688
7bcb11d3
JS
689 int canvasWidth, canvasHeight;
690 canvas->GetSize(&canvasWidth, &canvasHeight);
8826f46f 691
7bcb11d3
JS
692 double zoomScale = ((float)m_currentZoom/(float)100);
693 double actualWidth = (zoomScale*m_pageWidth*m_previewScale);
694 // float actualHeight = (float)(zoomScale*m_pageHeight*m_previewScale);
8826f46f 695
7bcb11d3
JS
696 int x = (int) ((canvasWidth - actualWidth)/2.0);
697 if (x < m_leftMargin)
698 x = m_leftMargin;
699 int y = m_topMargin;
8826f46f 700
7bcb11d3
JS
701 wxMemoryDC temp_dc;
702 temp_dc.SelectObject(*m_previewBitmap);
8826f46f 703
7bcb11d3 704 dc.Blit(x, y, m_previewBitmap->GetWidth(), m_previewBitmap->GetHeight(), &temp_dc, 0, 0);
8826f46f 705
7bcb11d3 706 temp_dc.SelectObject(wxNullBitmap);
8826f46f 707
7bcb11d3 708 return TRUE;
c801d85f
KB
709}
710
711bool wxPrintPreviewBase::RenderPage(int pageNum)
712{
f6bcfd97
BP
713 wxBusyCursor busy;
714
7bcb11d3 715 int canvasWidth, canvasHeight;
8826f46f 716
7bcb11d3 717 if (!m_previewCanvas)
c801d85f 718 {
f6bcfd97 719 wxFAIL_MSG(_T("wxPrintPreviewBase::RenderPage: must use wxPrintPreviewBase::SetCanvas to let me know about the canvas!"));
771779ed 720
7bcb11d3 721 return FALSE;
c801d85f 722 }
7bcb11d3 723 m_previewCanvas->GetSize(&canvasWidth, &canvasHeight);
8826f46f 724
7bcb11d3
JS
725 double zoomScale = (m_currentZoom/100.0);
726 int actualWidth = (int)(zoomScale*m_pageWidth*m_previewScale);
727 int actualHeight = (int)(zoomScale*m_pageHeight*m_previewScale);
8826f46f 728
7bcb11d3
JS
729 int x = (int)((canvasWidth - actualWidth)/2.0);
730 if (x < m_leftMargin)
731 x = m_leftMargin;
732 // int y = m_topMargin;
8826f46f
VZ
733
734
7bcb11d3
JS
735 if (!m_previewBitmap)
736 {
737 m_previewBitmap = new wxBitmap((int)actualWidth, (int)actualHeight);
738 if (!m_previewBitmap || !m_previewBitmap->Ok())
739 {
9eee81a4 740 if (m_previewBitmap) {
7bcb11d3 741 delete m_previewBitmap;
9eee81a4
GD
742 m_previewBitmap = NULL;
743 }
7bcb11d3
JS
744 wxMessageBox(_("Sorry, not enough memory to create a preview."), _("Print Preview Failure"), wxOK);
745 return FALSE;
746 }
747 }
8826f46f 748
7bcb11d3
JS
749 wxMemoryDC memoryDC;
750 memoryDC.SelectObject(*m_previewBitmap);
8826f46f 751
7bcb11d3 752 memoryDC.Clear();
8826f46f 753
7bcb11d3
JS
754 m_previewPrintout->SetDC(&memoryDC);
755 m_previewPrintout->SetPageSizePixels(m_pageWidth, m_pageHeight);
8826f46f 756
1044a386
JS
757 // Need to delay OnPreparePrinting until here, so we have enough information.
758 if (!m_printingPrepared)
759 {
760 m_previewPrintout->OnPreparePrinting();
761 m_printingPrepared = TRUE;
762 }
763
7bcb11d3 764 m_previewPrintout->OnBeginPrinting();
c801d85f 765
7bcb11d3
JS
766 if (!m_previewPrintout->OnBeginDocument(m_printDialogData.GetFromPage(), m_printDialogData.GetToPage()))
767 {
768 wxMessageBox(_("Could not start document preview."), _("Print Preview Failure"), wxOK);
8826f46f 769
7bcb11d3 770 memoryDC.SelectObject(wxNullBitmap);
8826f46f 771
7bcb11d3 772 delete m_previewBitmap;
9eee81a4 773 m_previewBitmap = NULL;
7bcb11d3
JS
774 return FALSE;
775 }
8826f46f 776
7bcb11d3
JS
777 m_previewPrintout->OnPrintPage(pageNum);
778 m_previewPrintout->OnEndDocument();
779 m_previewPrintout->OnEndPrinting();
8826f46f 780
7bcb11d3 781 m_previewPrintout->SetDC(NULL);
8826f46f 782
c801d85f 783 memoryDC.SelectObject(wxNullBitmap);
8826f46f 784
3080bf59
VZ
785#if wxUSE_STATUSBAR
786 wxString status;
7bcb11d3 787 if (m_maxPage != 0)
3080bf59 788 status = wxString::Format(_("Page %d of %d"), pageNum, m_maxPage);
7bcb11d3 789 else
3080bf59 790 status = wxString::Format(_("Page %d"), pageNum);
8826f46f 791
7bcb11d3 792 if (m_previewFrame)
3080bf59
VZ
793 m_previewFrame->SetStatusText(status);
794#endif
8826f46f 795
7bcb11d3 796 return TRUE;
c801d85f
KB
797}
798
799
800bool wxPrintPreviewBase::DrawBlankPage(wxWindow *canvas, wxDC& dc)
801{
7bcb11d3
JS
802 int canvasWidth, canvasHeight;
803 canvas->GetSize(&canvasWidth, &canvasHeight);
8826f46f 804
7bcb11d3
JS
805 float zoomScale = (float)((float)m_currentZoom/(float)100);
806 float actualWidth = zoomScale*m_pageWidth*m_previewScale;
807 float actualHeight = zoomScale*m_pageHeight*m_previewScale;
8826f46f 808
7bcb11d3
JS
809 float x = (float)((canvasWidth - actualWidth)/2.0);
810 if (x < m_leftMargin)
811 x = (float)m_leftMargin;
812 float y = (float)m_topMargin;
8826f46f 813
7bcb11d3
JS
814 // Draw shadow, allowing for 1-pixel border AROUND the actual page
815 int shadowOffset = 4;
816 dc.SetPen(*wxBLACK_PEN);
817 dc.SetBrush(*wxBLACK_BRUSH);
818 /*
819 dc.DrawRectangle((int)(x-1 + shadowOffset), (int)(y-1 + shadowOffset), (int)(actualWidth+2), (int)(actualHeight+2));
820 */
821 dc.DrawRectangle((int)(x + shadowOffset), (int)(y + actualHeight+1), (int)(actualWidth), shadowOffset);
822 dc.DrawRectangle((int)(x + actualWidth), (int)(y + shadowOffset), shadowOffset, (int)(actualHeight));
8826f46f 823
7bcb11d3
JS
824 // Draw blank page allowing for 1-pixel border AROUND the actual page
825 dc.SetPen(*wxBLACK_PEN);
826 dc.SetBrush(*wxWHITE_BRUSH);
8826f46f 827
7bcb11d3
JS
828 /*
829 wxRegion update_region = canvas->GetUpdateRegion();
830 wxRect r = update_region.GetBox();
8826f46f 831
7bcb11d3
JS
832 printf( "x: %d y: %d w: %d h: %d.\n", (int)r.x, (int)r.y, (int)r.width, (int)r.height );
833 */
8826f46f 834
7bcb11d3 835 dc.DrawRectangle((int)(x-2), (int)(y-1), (int)(actualWidth+3), (int)(actualHeight+2));
8826f46f 836
2a47d3c1
JS
837 return TRUE;
838}
839
7bcb11d3 840void wxPrintPreviewBase::SetZoom(int percent)
2a47d3c1 841{
7bcb11d3
JS
842 if (m_currentZoom == percent)
843 return;
8826f46f 844
7bcb11d3
JS
845 m_currentZoom = percent;
846 if (m_previewBitmap)
847 {
848 delete m_previewBitmap;
849 m_previewBitmap = NULL;
850 }
aff37a19 851
7bcb11d3
JS
852 if (m_previewCanvas)
853 {
aff37a19 854 RenderPage(m_currentPage);
95724b1a 855 ((wxScrolledWindow *) m_previewCanvas)->Scroll(0, 0);
7bcb11d3
JS
856 m_previewCanvas->Clear();
857 m_previewCanvas->Refresh();
858 }
2a47d3c1
JS
859}
860
d427503c 861#endif // wxUSE_PRINTING_ARCHITECTURE