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