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