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