]> git.saurik.com Git - wxWidgets.git/blame - src/common/prntbase.cpp
typo(s) fixed
[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
8826f46f 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"
39#endif
40
41#include "wx/prntbase.h"
42#include "wx/dcprint.h"
43#include "wx/printdlg.h"
2a47d3c1 44#include "wx/module.h"
c801d85f
KB
45
46#include <stdlib.h>
47#include <string.h>
48
2049ba38 49#ifdef __WXMSW__
d427503c
VZ
50 #include "wx/msw/private.h"
51 #include <commdlg.h>
c801d85f 52
d427503c
VZ
53 #ifndef __WIN32__
54 #include <print.h>
55 #endif
56#endif // __WXMSW__
c801d85f 57
c801d85f
KB
58IMPLEMENT_CLASS(wxPrinterBase, wxObject)
59IMPLEMENT_ABSTRACT_CLASS(wxPrintout, wxObject)
60IMPLEMENT_CLASS(wxPreviewCanvas, wxWindow)
61IMPLEMENT_CLASS(wxPreviewControlBar, wxWindow)
62IMPLEMENT_CLASS(wxPreviewFrame, wxFrame)
63IMPLEMENT_CLASS(wxPrintPreviewBase, wxObject)
64
65BEGIN_EVENT_TABLE(wxPrintAbortDialog, wxDialog)
d427503c 66 EVT_BUTTON(wxID_CANCEL, wxPrintAbortDialog::OnCancel)
c801d85f
KB
67END_EVENT_TABLE()
68
69BEGIN_EVENT_TABLE(wxPreviewCanvas, wxScrolledWindow)
d427503c
VZ
70 EVT_PAINT(wxPreviewCanvas::OnPaint)
71 EVT_SYS_COLOUR_CHANGED(wxPreviewCanvas::OnSysColourChanged)
c801d85f 72END_EVENT_TABLE()
c801d85f
KB
73
74/*
7bcb11d3
JS
75* Printer
76*/
77
78wxPrinterBase::wxPrinterBase(wxPrintDialogData *data)
c801d85f 79{
7bcb11d3
JS
80 m_currentPrintout = (wxPrintout *) NULL;
81 sm_abortWindow = (wxWindow *) NULL;
82 sm_abortIt = FALSE;
83 if (data)
84 m_printDialogData = (*data);
c801d85f
KB
85}
86
34da0970
JS
87wxWindow *wxPrinterBase::sm_abortWindow = (wxWindow *) NULL;
88bool wxPrinterBase::sm_abortIt = FALSE;
c801d85f 89
34da0970 90wxPrinterBase::~wxPrinterBase()
c801d85f
KB
91{
92}
93
94void wxPrintAbortDialog::OnCancel(wxCommandEvent& WXUNUSED(event))
95{
7bcb11d3
JS
96 wxPrinterBase::sm_abortIt = TRUE;
97 wxPrinterBase::sm_abortWindow->Show(FALSE);
98 wxPrinterBase::sm_abortWindow->Close(TRUE);
99 wxPrinterBase::sm_abortWindow = (wxWindow *) NULL;
c801d85f
KB
100}
101
102wxWindow *wxPrinterBase::CreateAbortWindow(wxWindow *parent, wxPrintout *WXUNUSED(printout))
103{
7bcb11d3
JS
104 wxPrintAbortDialog *dialog = new wxPrintAbortDialog(parent, _("Printing"), wxPoint(0, 0), wxSize(400, 400), wxDEFAULT_DIALOG_STYLE);
105 (void) new wxStaticText(dialog, -1, _("Please wait..."), wxPoint(5, 5));
8826f46f 106
7bcb11d3 107 wxButton *button = new wxButton(dialog, wxID_CANCEL, _("Cancel"), wxPoint(5, 30));
8826f46f 108
7bcb11d3
JS
109 dialog->Fit();
110 button->Centre(wxHORIZONTAL);
8826f46f 111
7bcb11d3
JS
112 dialog->Centre();
113 return dialog;
c801d85f
KB
114}
115
116void wxPrinterBase::ReportError(wxWindow *parent, wxPrintout *WXUNUSED(printout), char *message)
117{
7bcb11d3 118 wxMessageBox(message, _("Printing Error"), wxOK, parent);
c801d85f
KB
119}
120
121/*
7bcb11d3
JS
122* Printout class
123*/
124
34da0970 125wxPrintout::wxPrintout(const wxString& title)
c801d85f 126{
7bcb11d3
JS
127 m_printoutTitle = title ;
128 m_printoutDC = (wxDC *) NULL;
129 m_pageWidthMM = 0;
130 m_pageHeightMM = 0;
131 m_pageWidthPixels = 0;
132 m_pageHeightPixels = 0;
133 m_PPIScreenX = 0;
134 m_PPIScreenY = 0;
135 m_PPIPrinterX = 0;
136 m_PPIPrinterY = 0;
137 m_isPreview = FALSE;
c801d85f
KB
138}
139
34da0970 140wxPrintout::~wxPrintout()
c801d85f 141{
c801d85f
KB
142}
143
144bool wxPrintout::OnBeginDocument(int WXUNUSED(startPage), int WXUNUSED(endPage))
145{
7bcb11d3 146 return GetDC()->StartDoc(_("Printing"));
c801d85f
KB
147}
148
34da0970 149void wxPrintout::OnEndDocument()
c801d85f 150{
7bcb11d3 151 GetDC()->EndDoc();
c801d85f
KB
152}
153
34da0970 154void wxPrintout::OnBeginPrinting()
c801d85f
KB
155{
156}
157
34da0970 158void wxPrintout::OnEndPrinting()
c801d85f
KB
159{
160}
161
162bool wxPrintout::HasPage(int page)
163{
7bcb11d3 164 return (page == 1);
c801d85f
KB
165}
166
167void wxPrintout::GetPageInfo(int *minPage, int *maxPage, int *fromPage, int *toPage)
168{
7bcb11d3
JS
169 *minPage = 1;
170 *maxPage = 32000;
171 *fromPage = 1;
172 *toPage = 1;
c801d85f
KB
173}
174
175/*
7bcb11d3
JS
176* Preview canvas
177*/
178
c801d85f 179wxPreviewCanvas::wxPreviewCanvas(wxPrintPreviewBase *preview, wxWindow *parent,
7bcb11d3
JS
180 const wxPoint& pos, const wxSize& size, long style, const wxString& name):
181wxScrolledWindow(parent, -1, pos, size, style, name)
c801d85f 182{
7bcb11d3
JS
183 m_printPreview = preview;
184 SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_APPWORKSPACE));
8826f46f 185
7bcb11d3 186 SetScrollbars(15, 18, 100, 100);
c801d85f
KB
187}
188
34da0970 189wxPreviewCanvas::~wxPreviewCanvas()
c801d85f
KB
190{
191}
192
193void wxPreviewCanvas::OnPaint(wxPaintEvent& WXUNUSED(event))
194{
7bcb11d3
JS
195 wxPaintDC dc(this);
196 PrepareDC( dc );
8826f46f 197
809934d2
RR
198#ifdef __WXGTK__
199 if (!GetUpdateRegion().IsEmpty())
200 dc.SetClippingRegion( GetUpdateRegion() );
201#endif
202
7bcb11d3
JS
203 if (m_printPreview)
204 {
205 m_printPreview->PaintPage(this, dc);
206 }
c801d85f
KB
207}
208
209// Responds to colour changes, and passes event on to children.
210void wxPreviewCanvas::OnSysColourChanged(wxSysColourChangedEvent& event)
211{
7bcb11d3
JS
212 SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_APPWORKSPACE));
213 Refresh();
8826f46f 214
7bcb11d3
JS
215 // Propagate the event to the non-top-level children
216 wxWindow::OnSysColourChanged(event);
c801d85f
KB
217}
218
219/*
7bcb11d3
JS
220* Preview control bar
221*/
c801d85f
KB
222
223BEGIN_EVENT_TABLE(wxPreviewControlBar, wxPanel)
8826f46f
VZ
224 EVT_BUTTON(wxID_PREVIEW_CLOSE, wxPreviewControlBar::OnWindowClose)
225 EVT_BUTTON(wxID_PREVIEW_PRINT, wxPreviewControlBar::OnPrint)
0f90ec93
KB
226 EVT_BUTTON(wxID_PREVIEW_PREVIOUS, wxPreviewControlBar::OnPreviousButton)
227 EVT_BUTTON(wxID_PREVIEW_NEXT, wxPreviewControlBar::OnNextButton)
228 EVT_CHAR(wxPreviewControlBar::OnChar)
8826f46f
VZ
229 EVT_CHOICE(wxID_PREVIEW_ZOOM, wxPreviewControlBar::OnZoom)
230 EVT_PAINT(wxPreviewControlBar::OnPaint)
c801d85f 231END_EVENT_TABLE()
7bcb11d3 232
c801d85f 233wxPreviewControlBar::wxPreviewControlBar(wxPrintPreviewBase *preview, long buttons,
7bcb11d3
JS
234 wxWindow *parent, const wxPoint& pos, const wxSize& size,
235 long style, const wxString& name):
236wxPanel(parent, -1, pos, size, style, name)
c801d85f 237{
7bcb11d3
JS
238 m_printPreview = preview;
239 m_closeButton = (wxButton *) NULL;
240 m_nextPageButton = (wxButton *) NULL;
241 m_previousPageButton = (wxButton *) NULL;
242 m_printButton = (wxButton *) NULL;
243 m_zoomControl = (wxChoice *) NULL;
244 m_buttonFlags = buttons;
c801d85f
KB
245}
246
34da0970 247wxPreviewControlBar::~wxPreviewControlBar()
c801d85f
KB
248{
249}
250
251void wxPreviewControlBar::OnPaint(wxPaintEvent& WXUNUSED(event))
252{
7bcb11d3 253 wxPaintDC dc(this);
8826f46f 254
7bcb11d3
JS
255 int w, h;
256 GetSize(&w, &h);
257 dc.SetPen(*wxBLACK_PEN);
258 dc.SetBrush(*wxTRANSPARENT_BRUSH);
259 dc.DrawLine( 0, h-1, w, h-1 );
c801d85f
KB
260}
261
c330a2cf 262void wxPreviewControlBar::OnWindowClose(wxCommandEvent& WXUNUSED(event))
c801d85f 263{
7bcb11d3
JS
264 wxPreviewFrame *frame = (wxPreviewFrame *)GetParent();
265 frame->Close(TRUE);
c801d85f
KB
266}
267
268void wxPreviewControlBar::OnPrint(wxCommandEvent& WXUNUSED(event))
269{
7bcb11d3
JS
270 wxPrintPreviewBase *preview = GetPrintPreview();
271 preview->Print(TRUE);
c801d85f
KB
272}
273
0f90ec93
KB
274void wxPreviewControlBar::OnChar(wxKeyEvent &event)
275{
276 switch(event.KeyCode())
277 {
278 case WXK_NEXT:
279 OnNext(); break;
280 case WXK_PRIOR:
281 OnPrevious(); break;
282 default:
283 event.Skip();
284 }
285}
286
287void wxPreviewControlBar::OnNext(void)
c801d85f 288{
7bcb11d3
JS
289 wxPrintPreviewBase *preview = GetPrintPreview();
290 if (preview)
c801d85f 291 {
7bcb11d3
JS
292 int currentPage = preview->GetCurrentPage();
293 if ((preview->GetMaxPage() > 0) &&
294 (currentPage < preview->GetMaxPage()) &&
295 preview->GetPrintout()->HasPage(currentPage + 1))
296 {
297 preview->SetCurrentPage(currentPage + 1);
298 }
c801d85f 299 }
c801d85f
KB
300}
301
0f90ec93 302void wxPreviewControlBar::OnPrevious(void)
c801d85f 303{
7bcb11d3
JS
304 wxPrintPreviewBase *preview = GetPrintPreview();
305 if (preview)
c801d85f 306 {
7bcb11d3
JS
307 int currentPage = preview->GetCurrentPage();
308 if ((preview->GetMinPage() > 0) &&
309 (currentPage > preview->GetMinPage()) &&
310 preview->GetPrintout()->HasPage(currentPage - 1))
311 {
312 preview->SetCurrentPage(currentPage - 1);
313 }
c801d85f 314 }
c801d85f
KB
315}
316
317void wxPreviewControlBar::OnZoom(wxCommandEvent& WXUNUSED(event))
318{
7bcb11d3
JS
319 int zoom = GetZoomControl();
320 if (GetPrintPreview())
321 GetPrintPreview()->SetZoom(zoom);
c801d85f
KB
322}
323
34da0970 324void wxPreviewControlBar::CreateButtons()
c801d85f 325{
7bcb11d3 326 SetSize(0, 0, 400, 40);
8826f46f 327
7bcb11d3
JS
328 /*
329 #ifdef __WXMSW__
330 int fontSize = 9;
331 #else
332 int fontSize = 10;
333 #endif
8826f46f 334
7bcb11d3
JS
335 wxFont buttonFont(fontSize, wxSWISS, wxNORMAL, wxBOLD);
336 SetFont(buttonFont);
337 */
8826f46f 338
7bcb11d3 339 int buttonWidth = 65;
031b2a7b
RR
340#ifdef __WXGTK__
341 int buttonHeight = -1;
342#else
343 int buttonHeight = 24;
344#endif
8826f46f 345
7bcb11d3
JS
346 int x = 5;
347 int y = 5;
8826f46f 348
9838df2c 349#ifdef __WXMOTIF__
7bcb11d3 350 int gap = 15;
9838df2c 351#else
7bcb11d3 352 int gap = 5;
9838df2c 353#endif
8826f46f 354
7bcb11d3
JS
355 m_closeButton = new wxButton(this, wxID_PREVIEW_CLOSE, _("Close"),
356 wxPoint(x, y), wxSize(buttonWidth, buttonHeight));
8826f46f 357
7bcb11d3 358 x += gap + buttonWidth;
8826f46f 359
7bcb11d3
JS
360 if (m_buttonFlags & wxPREVIEW_PRINT)
361 {
362 m_printButton = new wxButton(this, wxID_PREVIEW_PRINT, _("Print..."), wxPoint(x, y),
363 wxSize(buttonWidth, buttonHeight));
364 x += gap + buttonWidth;
365 }
8826f46f 366
7bcb11d3
JS
367 if (m_buttonFlags & wxPREVIEW_PREVIOUS)
368 {
58c837a4 369 m_previousPageButton = new wxButton(this, wxID_PREVIEW_PREVIOUS, wxT("<<"), wxPoint(x, y),
7bcb11d3
JS
370 wxSize(buttonWidth, buttonHeight));
371 x += gap + buttonWidth;
372 }
8826f46f 373
7bcb11d3
JS
374 if (m_buttonFlags & wxPREVIEW_NEXT)
375 {
58c837a4 376 m_nextPageButton = new wxButton(this, wxID_PREVIEW_NEXT, wxT(">>"),
7bcb11d3
JS
377 wxPoint(x, y), wxSize(buttonWidth, buttonHeight));
378 x += gap + buttonWidth;
379 }
8826f46f 380
7bcb11d3
JS
381 if (m_buttonFlags & wxPREVIEW_ZOOM)
382 {
c25ccf85
RR
383 static const char *choices[] =
384 {
385 "10%", "15%", "20%", "25%", "30%", "35%", "40%", "45%", "50%", "55%",
386 "60%", "65%", "70%", "75%", "80%", "85%", "90%", "95%", "100%", "110%",
387 "120%", "150%", "200%"
388 };
389
c25ccf85 390 int n = WXSIZEOF(choices);
6adaedf0
JS
391
392 wxString* strings = new wxString[n];
393 int i;
394 for (i = 0; i < n; i++ )
395 strings[i] = choices[i];
396
397 m_zoomControl = new wxChoice(this, wxID_PREVIEW_ZOOM,
398 wxPoint(x, y),
399 wxSize(100, -1),
400 n,
401 strings
402 );
403 delete[] strings;
404
7bcb11d3
JS
405 SetZoomControl(m_printPreview->GetZoom());
406 }
8826f46f 407
7bcb11d3 408 // m_closeButton->SetDefault();
c801d85f
KB
409}
410
411void wxPreviewControlBar::SetZoomControl(int zoom)
412{
7bcb11d3
JS
413 char buf[20];
414 sprintf(buf, "%d%%", zoom);
16a12a3d 415// Someone is calling methods that do no exist in wxChoice!! So I'll just comment out for VA for now
7bcb11d3
JS
416 if (m_zoomControl)
417 m_zoomControl->SetStringSelection(buf);
c801d85f
KB
418}
419
34da0970 420int wxPreviewControlBar::GetZoomControl()
c801d85f 421{
cf2f341a 422 wxChar buf[20];
223d09f6 423 if (m_zoomControl && (m_zoomControl->GetStringSelection() != wxT("")))
7bcb11d3 424 {
cf2f341a
OK
425 wxStrcpy(buf, m_zoomControl->GetStringSelection());
426 buf[wxStrlen(buf) - 1] = 0;
427 return (int)wxAtoi(buf);
7bcb11d3
JS
428 }
429 else return 0;
c801d85f
KB
430}
431
432
433/*
7bcb11d3
JS
434* Preview frame
435*/
c801d85f 436
e3065973 437BEGIN_EVENT_TABLE(wxPreviewFrame, wxFrame)
0f90ec93 438 EVT_CLOSE(wxPreviewFrame::OnCloseWindow)
e3065973
JS
439END_EVENT_TABLE()
440
c801d85f 441wxPreviewFrame::wxPreviewFrame(wxPrintPreviewBase *preview, wxFrame *parent, const wxString& title,
7bcb11d3
JS
442 const wxPoint& pos, const wxSize& size, long style, const wxString& name):
443wxFrame(parent, -1, title, pos, size, style, name)
c801d85f 444{
7bcb11d3
JS
445 m_printPreview = preview;
446 m_controlBar = NULL;
447 m_previewCanvas = NULL;
c801d85f
KB
448}
449
34da0970 450wxPreviewFrame::~wxPreviewFrame()
c801d85f
KB
451{
452}
453
74e3313b 454void wxPreviewFrame::OnCloseWindow(wxCloseEvent& WXUNUSED(event))
c801d85f 455{
7bcb11d3 456 MakeModal(FALSE);
8826f46f 457
7bcb11d3
JS
458 // Need to delete the printout and the print preview
459 wxPrintout *printout = m_printPreview->GetPrintout();
460 if (printout)
461 {
462 delete printout;
463 m_printPreview->SetPrintout(NULL);
464 m_printPreview->SetCanvas(NULL);
465 m_printPreview->SetFrame(NULL);
466 }
467 delete m_printPreview;
8826f46f 468
7bcb11d3 469 Destroy();
c801d85f
KB
470}
471
34da0970 472void wxPreviewFrame::Initialize()
c801d85f 473{
7bcb11d3 474 CreateStatusBar();
8826f46f 475
7bcb11d3
JS
476 CreateCanvas();
477 CreateControlBar();
8826f46f 478
7bcb11d3
JS
479 m_printPreview->SetCanvas(m_previewCanvas);
480 m_printPreview->SetFrame(this);
8826f46f 481
7bcb11d3 482 // Set layout constraints here
8826f46f 483
7bcb11d3
JS
484 // Control bar constraints
485 wxLayoutConstraints *c1 = new wxLayoutConstraints;
486 // int w, h;
487 // m_controlBar->GetSize(&w, &h);
488 int h;
031b2a7b 489#if (defined(__WXMSW__) || defined(__WXGTK__))
7bcb11d3 490 h = 40;
c801d85f 491#else
7bcb11d3 492 h = 60;
c801d85f 493#endif
8826f46f 494
7bcb11d3
JS
495 c1->left.SameAs (this, wxLeft);
496 c1->top.SameAs (this, wxTop);
497 c1->right.SameAs (this, wxRight);
498 c1->height.Absolute (h);
8826f46f 499
7bcb11d3 500 m_controlBar->SetConstraints(c1);
8826f46f 501
7bcb11d3
JS
502 // Canvas constraints
503 wxLayoutConstraints *c2 = new wxLayoutConstraints;
8826f46f 504
7bcb11d3
JS
505 c2->left.SameAs (this, wxLeft);
506 c2->top.Below (m_controlBar);
507 c2->right.SameAs (this, wxRight);
508 c2->bottom.SameAs (this, wxBottom);
8826f46f 509
7bcb11d3 510 m_previewCanvas->SetConstraints(c2);
8826f46f 511
7bcb11d3 512 SetAutoLayout(TRUE);
8826f46f 513
7bcb11d3 514 MakeModal(TRUE);
8826f46f 515
7bcb11d3 516 Layout();
c801d85f
KB
517}
518
34da0970 519void wxPreviewFrame::CreateCanvas()
c801d85f 520{
7bcb11d3 521 m_previewCanvas = new wxPreviewCanvas(m_printPreview, this);
c801d85f
KB
522}
523
34da0970 524void wxPreviewFrame::CreateControlBar()
c801d85f 525{
7bcb11d3
JS
526 long buttons = wxPREVIEW_DEFAULT;
527 if (m_printPreview->GetPrintoutForPrinting())
528 buttons |= wxPREVIEW_PRINT;
8826f46f 529
7bcb11d3
JS
530 m_controlBar = new wxPreviewControlBar(m_printPreview, buttons, this, wxPoint(0, 0), wxSize(400, 40));
531 m_controlBar->CreateButtons();
c801d85f 532}
7bcb11d3 533
c801d85f 534/*
7bcb11d3
JS
535* Print preview
536*/
c801d85f 537
8826f46f
VZ
538wxPrintPreviewBase::wxPrintPreviewBase(wxPrintout *printout,
539 wxPrintout *printoutForPrinting,
540 wxPrintData *data)
541{
542 if (data)
543 m_printDialogData = (*data);
544
545 Init(printout, printoutForPrinting);
546}
547
548wxPrintPreviewBase::wxPrintPreviewBase(wxPrintout *printout,
549 wxPrintout *printoutForPrinting,
550 wxPrintDialogData *data)
551{
552 if (data)
553 m_printDialogData = (*data);
554
555 Init(printout, printoutForPrinting);
556}
557
558void wxPrintPreviewBase::Init(wxPrintout *printout,
559 wxPrintout *printoutForPrinting)
c801d85f 560{
7bcb11d3
JS
561 m_isOk = TRUE;
562 m_previewPrintout = printout;
563 if (m_previewPrintout)
564 m_previewPrintout->SetIsPreview(TRUE);
8826f46f 565
7bcb11d3 566 m_printPrintout = printoutForPrinting;
8826f46f 567
7bcb11d3
JS
568 m_previewCanvas = NULL;
569 m_previewFrame = NULL;
570 m_previewBitmap = NULL;
571 m_currentPage = 1;
c25ccf85 572 m_currentZoom = 70;
7bcb11d3
JS
573 m_topMargin = 40;
574 m_leftMargin = 40;
575 m_pageWidth = 0;
576 m_pageHeight = 0;
1044a386 577 m_printingPrepared = FALSE;
8826f46f 578
1044a386
JS
579 // Too soon! Moved to RenderPage.
580 // printout->OnPreparePrinting();
8826f46f 581
7bcb11d3
JS
582 // Get some parameters from the printout, if defined
583 int selFrom, selTo;
584 printout->GetPageInfo(&m_minPage, &m_maxPage, &selFrom, &selTo);
c801d85f
KB
585}
586
34da0970 587wxPrintPreviewBase::~wxPrintPreviewBase()
c801d85f 588{
7bcb11d3
JS
589 if (m_previewPrintout)
590 delete m_previewPrintout;
591 if (m_previewBitmap)
592 delete m_previewBitmap;
593 if (m_printPrintout)
594 delete m_printPrintout;
c801d85f
KB
595}
596
597bool wxPrintPreviewBase::SetCurrentPage(int pageNum)
598{
7bcb11d3
JS
599 if (m_currentPage == pageNum)
600 return TRUE;
8826f46f 601
7bcb11d3
JS
602 m_currentPage = pageNum;
603 if (m_previewBitmap)
604 {
605 delete m_previewBitmap;
606 m_previewBitmap = NULL;
607 }
8826f46f 608
7bcb11d3
JS
609 if (m_previewCanvas)
610 {
611 RenderPage(pageNum);
612 m_previewCanvas->Refresh();
613 }
c801d85f 614 return TRUE;
c801d85f
KB
615}
616
617bool wxPrintPreviewBase::PaintPage(wxWindow *canvas, wxDC& dc)
618{
7bcb11d3 619 DrawBlankPage(canvas, dc);
8826f46f 620
7bcb11d3
JS
621 if (!m_previewBitmap)
622 RenderPage(m_currentPage);
8826f46f 623
7bcb11d3
JS
624 if (!m_previewBitmap)
625 return FALSE;
8826f46f 626
7bcb11d3
JS
627 if (!canvas)
628 return FALSE;
8826f46f 629
7bcb11d3
JS
630 int canvasWidth, canvasHeight;
631 canvas->GetSize(&canvasWidth, &canvasHeight);
8826f46f 632
7bcb11d3
JS
633 double zoomScale = ((float)m_currentZoom/(float)100);
634 double actualWidth = (zoomScale*m_pageWidth*m_previewScale);
635 // float actualHeight = (float)(zoomScale*m_pageHeight*m_previewScale);
8826f46f 636
7bcb11d3
JS
637 int x = (int) ((canvasWidth - actualWidth)/2.0);
638 if (x < m_leftMargin)
639 x = m_leftMargin;
640 int y = m_topMargin;
8826f46f 641
7bcb11d3
JS
642 wxMemoryDC temp_dc;
643 temp_dc.SelectObject(*m_previewBitmap);
8826f46f 644
7bcb11d3 645 dc.Blit(x, y, m_previewBitmap->GetWidth(), m_previewBitmap->GetHeight(), &temp_dc, 0, 0);
8826f46f 646
7bcb11d3 647 temp_dc.SelectObject(wxNullBitmap);
8826f46f 648
7bcb11d3 649 return TRUE;
c801d85f
KB
650}
651
652bool wxPrintPreviewBase::RenderPage(int pageNum)
653{
7bcb11d3 654 int canvasWidth, canvasHeight;
8826f46f 655
7bcb11d3 656 if (!m_previewCanvas)
c801d85f 657 {
7bcb11d3
JS
658 wxMessageBox(_("wxPrintPreviewBase::RenderPage: must use wxPrintPreviewBase::SetCanvas to let me know about the canvas!"),
659 _("Print Preview Failure"), wxOK);
660 return FALSE;
c801d85f 661 }
7bcb11d3 662 m_previewCanvas->GetSize(&canvasWidth, &canvasHeight);
8826f46f 663
7bcb11d3
JS
664 double zoomScale = (m_currentZoom/100.0);
665 int actualWidth = (int)(zoomScale*m_pageWidth*m_previewScale);
666 int actualHeight = (int)(zoomScale*m_pageHeight*m_previewScale);
8826f46f 667
7bcb11d3
JS
668 int x = (int)((canvasWidth - actualWidth)/2.0);
669 if (x < m_leftMargin)
670 x = m_leftMargin;
671 // int y = m_topMargin;
8826f46f
VZ
672
673
7bcb11d3
JS
674 if (!m_previewBitmap)
675 {
676 m_previewBitmap = new wxBitmap((int)actualWidth, (int)actualHeight);
677 if (!m_previewBitmap || !m_previewBitmap->Ok())
678 {
679 if (m_previewBitmap)
680 delete m_previewBitmap;
681 wxMessageBox(_("Sorry, not enough memory to create a preview."), _("Print Preview Failure"), wxOK);
682 return FALSE;
683 }
684 }
8826f46f 685
7bcb11d3
JS
686 wxMemoryDC memoryDC;
687 memoryDC.SelectObject(*m_previewBitmap);
8826f46f 688
7bcb11d3 689 memoryDC.Clear();
8826f46f 690
7bcb11d3
JS
691 m_previewPrintout->SetDC(&memoryDC);
692 m_previewPrintout->SetPageSizePixels(m_pageWidth, m_pageHeight);
8826f46f 693
1044a386
JS
694 // Need to delay OnPreparePrinting until here, so we have enough information.
695 if (!m_printingPrepared)
696 {
697 m_previewPrintout->OnPreparePrinting();
698 m_printingPrepared = TRUE;
699 }
700
7bcb11d3 701 m_previewPrintout->OnBeginPrinting();
c801d85f 702
7bcb11d3
JS
703 if (!m_previewPrintout->OnBeginDocument(m_printDialogData.GetFromPage(), m_printDialogData.GetToPage()))
704 {
705 wxMessageBox(_("Could not start document preview."), _("Print Preview Failure"), wxOK);
8826f46f 706
7bcb11d3 707 memoryDC.SelectObject(wxNullBitmap);
8826f46f 708
7bcb11d3
JS
709 delete m_previewBitmap;
710 return FALSE;
711 }
8826f46f 712
7bcb11d3
JS
713 m_previewPrintout->OnPrintPage(pageNum);
714 m_previewPrintout->OnEndDocument();
715 m_previewPrintout->OnEndPrinting();
8826f46f 716
7bcb11d3 717 m_previewPrintout->SetDC(NULL);
8826f46f 718
c801d85f 719 memoryDC.SelectObject(wxNullBitmap);
8826f46f 720
cf2f341a 721 wxChar buf[200];
7bcb11d3 722 if (m_maxPage != 0)
cf2f341a 723 wxSprintf(buf, _("Page %d of %d"), pageNum, m_maxPage);
7bcb11d3 724 else
cf2f341a 725 wxSprintf(buf, _("Page %d"), pageNum);
8826f46f 726
7bcb11d3
JS
727 if (m_previewFrame)
728 m_previewFrame->SetStatusText(buf);
8826f46f 729
7bcb11d3 730 return TRUE;
c801d85f
KB
731}
732
733
734bool wxPrintPreviewBase::DrawBlankPage(wxWindow *canvas, wxDC& dc)
735{
7bcb11d3
JS
736 int canvasWidth, canvasHeight;
737 canvas->GetSize(&canvasWidth, &canvasHeight);
8826f46f 738
7bcb11d3
JS
739 float zoomScale = (float)((float)m_currentZoom/(float)100);
740 float actualWidth = zoomScale*m_pageWidth*m_previewScale;
741 float actualHeight = zoomScale*m_pageHeight*m_previewScale;
8826f46f 742
7bcb11d3
JS
743 float x = (float)((canvasWidth - actualWidth)/2.0);
744 if (x < m_leftMargin)
745 x = (float)m_leftMargin;
746 float y = (float)m_topMargin;
8826f46f 747
7bcb11d3
JS
748 // Draw shadow, allowing for 1-pixel border AROUND the actual page
749 int shadowOffset = 4;
750 dc.SetPen(*wxBLACK_PEN);
751 dc.SetBrush(*wxBLACK_BRUSH);
752 /*
753 dc.DrawRectangle((int)(x-1 + shadowOffset), (int)(y-1 + shadowOffset), (int)(actualWidth+2), (int)(actualHeight+2));
754 */
755 dc.DrawRectangle((int)(x + shadowOffset), (int)(y + actualHeight+1), (int)(actualWidth), shadowOffset);
756 dc.DrawRectangle((int)(x + actualWidth), (int)(y + shadowOffset), shadowOffset, (int)(actualHeight));
8826f46f 757
7bcb11d3
JS
758 // Draw blank page allowing for 1-pixel border AROUND the actual page
759 dc.SetPen(*wxBLACK_PEN);
760 dc.SetBrush(*wxWHITE_BRUSH);
8826f46f 761
7bcb11d3
JS
762 /*
763 wxRegion update_region = canvas->GetUpdateRegion();
764 wxRect r = update_region.GetBox();
8826f46f 765
7bcb11d3
JS
766 printf( "x: %d y: %d w: %d h: %d.\n", (int)r.x, (int)r.y, (int)r.width, (int)r.height );
767 */
8826f46f 768
7bcb11d3 769 dc.DrawRectangle((int)(x-2), (int)(y-1), (int)(actualWidth+3), (int)(actualHeight+2));
8826f46f 770
2a47d3c1
JS
771 return TRUE;
772}
773
7bcb11d3 774void wxPrintPreviewBase::SetZoom(int percent)
2a47d3c1 775{
7bcb11d3
JS
776 if (m_currentZoom == percent)
777 return;
8826f46f 778
7bcb11d3
JS
779 m_currentZoom = percent;
780 if (m_previewBitmap)
781 {
782 delete m_previewBitmap;
783 m_previewBitmap = NULL;
784 }
aff37a19 785
7bcb11d3
JS
786 if (m_previewCanvas)
787 {
aff37a19 788 RenderPage(m_currentPage);
95724b1a 789 ((wxScrolledWindow *) m_previewCanvas)->Scroll(0, 0);
7bcb11d3
JS
790 m_previewCanvas->Clear();
791 m_previewCanvas->Refresh();
792 }
2a47d3c1
JS
793}
794
d427503c 795#endif // wxUSE_PRINTING_ARCHITECTURE