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