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