]> git.saurik.com Git - wxWidgets.git/blame - src/common/prntbase.cpp
Applied patch [ 619386 ] uxtheme.dll support
[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{
7bcb11d3 550 CreateStatusBar();
7bcb11d3
JS
551 CreateCanvas();
552 CreateControlBar();
8826f46f 553
7bcb11d3
JS
554 m_printPreview->SetCanvas(m_previewCanvas);
555 m_printPreview->SetFrame(this);
8826f46f 556
9dff8515 557 wxBoxSizer *item0 = new wxBoxSizer( wxVERTICAL );
8826f46f 558
9dff8515
JS
559 item0->Add( m_controlBar, 0, wxGROW|wxALIGN_CENTER_VERTICAL, 5 );
560 item0->Add( m_previewCanvas, 1, wxGROW|wxALIGN_CENTER_VERTICAL, 5 );
8826f46f 561
9dff8515
JS
562 SetAutoLayout( TRUE );
563 SetSizer( item0 );
8826f46f 564
feea0f95
JS
565 // MakeModal doesn't work on wxMac, especially when there
566 // are multiple top-level windows.
567#ifndef __WXMAC__
7bcb11d3 568 MakeModal(TRUE);
feea0f95 569#endif
8826f46f 570
7bcb11d3 571 Layout();
c801d85f
KB
572}
573
34da0970 574void wxPreviewFrame::CreateCanvas()
c801d85f 575{
7bcb11d3 576 m_previewCanvas = new wxPreviewCanvas(m_printPreview, this);
c801d85f
KB
577}
578
34da0970 579void wxPreviewFrame::CreateControlBar()
c801d85f 580{
7bcb11d3
JS
581 long buttons = wxPREVIEW_DEFAULT;
582 if (m_printPreview->GetPrintoutForPrinting())
583 buttons |= wxPREVIEW_PRINT;
8826f46f 584
7bcb11d3
JS
585 m_controlBar = new wxPreviewControlBar(m_printPreview, buttons, this, wxPoint(0, 0), wxSize(400, 40));
586 m_controlBar->CreateButtons();
c801d85f 587}
7bcb11d3 588
c801d85f 589/*
7bcb11d3
JS
590* Print preview
591*/
c801d85f 592
8826f46f
VZ
593wxPrintPreviewBase::wxPrintPreviewBase(wxPrintout *printout,
594 wxPrintout *printoutForPrinting,
595 wxPrintData *data)
596{
597 if (data)
598 m_printDialogData = (*data);
599
600 Init(printout, printoutForPrinting);
601}
602
603wxPrintPreviewBase::wxPrintPreviewBase(wxPrintout *printout,
604 wxPrintout *printoutForPrinting,
605 wxPrintDialogData *data)
606{
607 if (data)
608 m_printDialogData = (*data);
609
610 Init(printout, printoutForPrinting);
611}
612
613void wxPrintPreviewBase::Init(wxPrintout *printout,
614 wxPrintout *printoutForPrinting)
c801d85f 615{
7bcb11d3
JS
616 m_isOk = TRUE;
617 m_previewPrintout = printout;
618 if (m_previewPrintout)
619 m_previewPrintout->SetIsPreview(TRUE);
8826f46f 620
7bcb11d3 621 m_printPrintout = printoutForPrinting;
8826f46f 622
7bcb11d3
JS
623 m_previewCanvas = NULL;
624 m_previewFrame = NULL;
625 m_previewBitmap = NULL;
626 m_currentPage = 1;
c25ccf85 627 m_currentZoom = 70;
7bcb11d3
JS
628 m_topMargin = 40;
629 m_leftMargin = 40;
630 m_pageWidth = 0;
631 m_pageHeight = 0;
1044a386 632 m_printingPrepared = FALSE;
8826f46f 633
1044a386
JS
634 // Too soon! Moved to RenderPage.
635 // printout->OnPreparePrinting();
8826f46f 636
7bcb11d3
JS
637 // Get some parameters from the printout, if defined
638 int selFrom, selTo;
639 printout->GetPageInfo(&m_minPage, &m_maxPage, &selFrom, &selTo);
c801d85f
KB
640}
641
34da0970 642wxPrintPreviewBase::~wxPrintPreviewBase()
c801d85f 643{
7bcb11d3
JS
644 if (m_previewPrintout)
645 delete m_previewPrintout;
646 if (m_previewBitmap)
647 delete m_previewBitmap;
648 if (m_printPrintout)
649 delete m_printPrintout;
c801d85f
KB
650}
651
652bool wxPrintPreviewBase::SetCurrentPage(int pageNum)
653{
7bcb11d3
JS
654 if (m_currentPage == pageNum)
655 return TRUE;
8826f46f 656
7bcb11d3
JS
657 m_currentPage = pageNum;
658 if (m_previewBitmap)
659 {
660 delete m_previewBitmap;
661 m_previewBitmap = NULL;
662 }
8826f46f 663
7bcb11d3
JS
664 if (m_previewCanvas)
665 {
9eee81a4 666 if (!RenderPage(pageNum))
bf89b538 667 return FALSE;
7bcb11d3
JS
668 m_previewCanvas->Refresh();
669 }
c801d85f 670 return TRUE;
c801d85f
KB
671}
672
673bool wxPrintPreviewBase::PaintPage(wxWindow *canvas, wxDC& dc)
674{
7bcb11d3 675 DrawBlankPage(canvas, dc);
8826f46f 676
7bcb11d3 677 if (!m_previewBitmap)
9eee81a4 678 if (!RenderPage(m_currentPage))
bf89b538 679 return FALSE;
8826f46f 680
7bcb11d3
JS
681 if (!m_previewBitmap)
682 return FALSE;
8826f46f 683
7bcb11d3
JS
684 if (!canvas)
685 return FALSE;
8826f46f 686
7bcb11d3
JS
687 int canvasWidth, canvasHeight;
688 canvas->GetSize(&canvasWidth, &canvasHeight);
8826f46f 689
7bcb11d3
JS
690 double zoomScale = ((float)m_currentZoom/(float)100);
691 double actualWidth = (zoomScale*m_pageWidth*m_previewScale);
692 // float actualHeight = (float)(zoomScale*m_pageHeight*m_previewScale);
8826f46f 693
7bcb11d3
JS
694 int x = (int) ((canvasWidth - actualWidth)/2.0);
695 if (x < m_leftMargin)
696 x = m_leftMargin;
697 int y = m_topMargin;
8826f46f 698
7bcb11d3
JS
699 wxMemoryDC temp_dc;
700 temp_dc.SelectObject(*m_previewBitmap);
8826f46f 701
7bcb11d3 702 dc.Blit(x, y, m_previewBitmap->GetWidth(), m_previewBitmap->GetHeight(), &temp_dc, 0, 0);
8826f46f 703
7bcb11d3 704 temp_dc.SelectObject(wxNullBitmap);
8826f46f 705
7bcb11d3 706 return TRUE;
c801d85f
KB
707}
708
709bool wxPrintPreviewBase::RenderPage(int pageNum)
710{
f6bcfd97
BP
711 wxBusyCursor busy;
712
7bcb11d3 713 int canvasWidth, canvasHeight;
8826f46f 714
7bcb11d3 715 if (!m_previewCanvas)
c801d85f 716 {
f6bcfd97 717 wxFAIL_MSG(_T("wxPrintPreviewBase::RenderPage: must use wxPrintPreviewBase::SetCanvas to let me know about the canvas!"));
771779ed 718
7bcb11d3 719 return FALSE;
c801d85f 720 }
7bcb11d3 721 m_previewCanvas->GetSize(&canvasWidth, &canvasHeight);
8826f46f 722
7bcb11d3
JS
723 double zoomScale = (m_currentZoom/100.0);
724 int actualWidth = (int)(zoomScale*m_pageWidth*m_previewScale);
725 int actualHeight = (int)(zoomScale*m_pageHeight*m_previewScale);
8826f46f 726
7bcb11d3
JS
727 int x = (int)((canvasWidth - actualWidth)/2.0);
728 if (x < m_leftMargin)
729 x = m_leftMargin;
730 // int y = m_topMargin;
8826f46f
VZ
731
732
7bcb11d3
JS
733 if (!m_previewBitmap)
734 {
735 m_previewBitmap = new wxBitmap((int)actualWidth, (int)actualHeight);
736 if (!m_previewBitmap || !m_previewBitmap->Ok())
737 {
9eee81a4 738 if (m_previewBitmap) {
7bcb11d3 739 delete m_previewBitmap;
9eee81a4
GD
740 m_previewBitmap = NULL;
741 }
7bcb11d3
JS
742 wxMessageBox(_("Sorry, not enough memory to create a preview."), _("Print Preview Failure"), wxOK);
743 return FALSE;
744 }
745 }
8826f46f 746
7bcb11d3
JS
747 wxMemoryDC memoryDC;
748 memoryDC.SelectObject(*m_previewBitmap);
8826f46f 749
7bcb11d3 750 memoryDC.Clear();
8826f46f 751
7bcb11d3
JS
752 m_previewPrintout->SetDC(&memoryDC);
753 m_previewPrintout->SetPageSizePixels(m_pageWidth, m_pageHeight);
8826f46f 754
1044a386
JS
755 // Need to delay OnPreparePrinting until here, so we have enough information.
756 if (!m_printingPrepared)
757 {
758 m_previewPrintout->OnPreparePrinting();
759 m_printingPrepared = TRUE;
760 }
761
7bcb11d3 762 m_previewPrintout->OnBeginPrinting();
c801d85f 763
7bcb11d3
JS
764 if (!m_previewPrintout->OnBeginDocument(m_printDialogData.GetFromPage(), m_printDialogData.GetToPage()))
765 {
766 wxMessageBox(_("Could not start document preview."), _("Print Preview Failure"), wxOK);
8826f46f 767
7bcb11d3 768 memoryDC.SelectObject(wxNullBitmap);
8826f46f 769
7bcb11d3 770 delete m_previewBitmap;
9eee81a4 771 m_previewBitmap = NULL;
7bcb11d3
JS
772 return FALSE;
773 }
8826f46f 774
7bcb11d3
JS
775 m_previewPrintout->OnPrintPage(pageNum);
776 m_previewPrintout->OnEndDocument();
777 m_previewPrintout->OnEndPrinting();
8826f46f 778
7bcb11d3 779 m_previewPrintout->SetDC(NULL);
8826f46f 780
c801d85f 781 memoryDC.SelectObject(wxNullBitmap);
8826f46f 782
cf2f341a 783 wxChar buf[200];
7bcb11d3 784 if (m_maxPage != 0)
cf2f341a 785 wxSprintf(buf, _("Page %d of %d"), pageNum, m_maxPage);
7bcb11d3 786 else
cf2f341a 787 wxSprintf(buf, _("Page %d"), pageNum);
8826f46f 788
7bcb11d3
JS
789 if (m_previewFrame)
790 m_previewFrame->SetStatusText(buf);
8826f46f 791
7bcb11d3 792 return TRUE;
c801d85f
KB
793}
794
795
796bool wxPrintPreviewBase::DrawBlankPage(wxWindow *canvas, wxDC& dc)
797{
7bcb11d3
JS
798 int canvasWidth, canvasHeight;
799 canvas->GetSize(&canvasWidth, &canvasHeight);
8826f46f 800
7bcb11d3
JS
801 float zoomScale = (float)((float)m_currentZoom/(float)100);
802 float actualWidth = zoomScale*m_pageWidth*m_previewScale;
803 float actualHeight = zoomScale*m_pageHeight*m_previewScale;
8826f46f 804
7bcb11d3
JS
805 float x = (float)((canvasWidth - actualWidth)/2.0);
806 if (x < m_leftMargin)
807 x = (float)m_leftMargin;
808 float y = (float)m_topMargin;
8826f46f 809
7bcb11d3
JS
810 // Draw shadow, allowing for 1-pixel border AROUND the actual page
811 int shadowOffset = 4;
812 dc.SetPen(*wxBLACK_PEN);
813 dc.SetBrush(*wxBLACK_BRUSH);
814 /*
815 dc.DrawRectangle((int)(x-1 + shadowOffset), (int)(y-1 + shadowOffset), (int)(actualWidth+2), (int)(actualHeight+2));
816 */
817 dc.DrawRectangle((int)(x + shadowOffset), (int)(y + actualHeight+1), (int)(actualWidth), shadowOffset);
818 dc.DrawRectangle((int)(x + actualWidth), (int)(y + shadowOffset), shadowOffset, (int)(actualHeight));
8826f46f 819
7bcb11d3
JS
820 // Draw blank page allowing for 1-pixel border AROUND the actual page
821 dc.SetPen(*wxBLACK_PEN);
822 dc.SetBrush(*wxWHITE_BRUSH);
8826f46f 823
7bcb11d3
JS
824 /*
825 wxRegion update_region = canvas->GetUpdateRegion();
826 wxRect r = update_region.GetBox();
8826f46f 827
7bcb11d3
JS
828 printf( "x: %d y: %d w: %d h: %d.\n", (int)r.x, (int)r.y, (int)r.width, (int)r.height );
829 */
8826f46f 830
7bcb11d3 831 dc.DrawRectangle((int)(x-2), (int)(y-1), (int)(actualWidth+3), (int)(actualHeight+2));
8826f46f 832
2a47d3c1
JS
833 return TRUE;
834}
835
7bcb11d3 836void wxPrintPreviewBase::SetZoom(int percent)
2a47d3c1 837{
7bcb11d3
JS
838 if (m_currentZoom == percent)
839 return;
8826f46f 840
7bcb11d3
JS
841 m_currentZoom = percent;
842 if (m_previewBitmap)
843 {
844 delete m_previewBitmap;
845 m_previewBitmap = NULL;
846 }
aff37a19 847
7bcb11d3
JS
848 if (m_previewCanvas)
849 {
aff37a19 850 RenderPage(m_currentPage);
95724b1a 851 ((wxScrolledWindow *) m_previewCanvas)->Scroll(0, 0);
7bcb11d3
JS
852 m_previewCanvas->Clear();
853 m_previewCanvas->Refresh();
854 }
2a47d3c1
JS
855}
856
d427503c 857#endif // wxUSE_PRINTING_ARCHITECTURE