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