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