]> git.saurik.com Git - wxWidgets.git/blame - src/common/prntbase.cpp
wxDataObejct and related changes (won't compile right now)
[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
966a3b2c
VZ
387 m_zoomControl = new wxChoice(this, wxID_PREVIEW_ZOOM,
388 wxPoint(x, y), wxSize(100, -1));
c25ccf85
RR
389
390 // Yes, this look stupid, but this is because gcc gives up otherwise.
391 int n = WXSIZEOF(choices);
392 for ( int i = 0; i < n; i++ )
393 m_zoomControl->Append(choices[i]);
394
7bcb11d3
JS
395 SetZoomControl(m_printPreview->GetZoom());
396 }
8826f46f 397
7bcb11d3 398 // m_closeButton->SetDefault();
c801d85f
KB
399}
400
401void wxPreviewControlBar::SetZoomControl(int zoom)
402{
7bcb11d3
JS
403 char buf[20];
404 sprintf(buf, "%d%%", zoom);
405 if (m_zoomControl)
406 m_zoomControl->SetStringSelection(buf);
c801d85f
KB
407}
408
34da0970 409int wxPreviewControlBar::GetZoomControl()
c801d85f 410{
cf2f341a 411 wxChar buf[20];
223d09f6 412 if (m_zoomControl && (m_zoomControl->GetStringSelection() != wxT("")))
7bcb11d3 413 {
cf2f341a
OK
414 wxStrcpy(buf, m_zoomControl->GetStringSelection());
415 buf[wxStrlen(buf) - 1] = 0;
416 return (int)wxAtoi(buf);
7bcb11d3
JS
417 }
418 else return 0;
c801d85f
KB
419}
420
421
422/*
7bcb11d3
JS
423* Preview frame
424*/
c801d85f 425
e3065973 426BEGIN_EVENT_TABLE(wxPreviewFrame, wxFrame)
0f90ec93 427 EVT_CLOSE(wxPreviewFrame::OnCloseWindow)
e3065973
JS
428END_EVENT_TABLE()
429
c801d85f 430wxPreviewFrame::wxPreviewFrame(wxPrintPreviewBase *preview, wxFrame *parent, const wxString& title,
7bcb11d3
JS
431 const wxPoint& pos, const wxSize& size, long style, const wxString& name):
432wxFrame(parent, -1, title, pos, size, style, name)
c801d85f 433{
7bcb11d3
JS
434 m_printPreview = preview;
435 m_controlBar = NULL;
436 m_previewCanvas = NULL;
c801d85f
KB
437}
438
34da0970 439wxPreviewFrame::~wxPreviewFrame()
c801d85f
KB
440{
441}
442
74e3313b 443void wxPreviewFrame::OnCloseWindow(wxCloseEvent& WXUNUSED(event))
c801d85f 444{
7bcb11d3 445 MakeModal(FALSE);
8826f46f 446
7bcb11d3
JS
447 // Need to delete the printout and the print preview
448 wxPrintout *printout = m_printPreview->GetPrintout();
449 if (printout)
450 {
451 delete printout;
452 m_printPreview->SetPrintout(NULL);
453 m_printPreview->SetCanvas(NULL);
454 m_printPreview->SetFrame(NULL);
455 }
456 delete m_printPreview;
8826f46f 457
7bcb11d3 458 Destroy();
c801d85f
KB
459}
460
34da0970 461void wxPreviewFrame::Initialize()
c801d85f 462{
7bcb11d3 463 CreateStatusBar();
8826f46f 464
7bcb11d3
JS
465 CreateCanvas();
466 CreateControlBar();
8826f46f 467
7bcb11d3
JS
468 m_printPreview->SetCanvas(m_previewCanvas);
469 m_printPreview->SetFrame(this);
8826f46f 470
7bcb11d3 471 // Set layout constraints here
8826f46f 472
7bcb11d3
JS
473 // Control bar constraints
474 wxLayoutConstraints *c1 = new wxLayoutConstraints;
475 // int w, h;
476 // m_controlBar->GetSize(&w, &h);
477 int h;
031b2a7b 478#if (defined(__WXMSW__) || defined(__WXGTK__))
7bcb11d3 479 h = 40;
c801d85f 480#else
7bcb11d3 481 h = 60;
c801d85f 482#endif
8826f46f 483
7bcb11d3
JS
484 c1->left.SameAs (this, wxLeft);
485 c1->top.SameAs (this, wxTop);
486 c1->right.SameAs (this, wxRight);
487 c1->height.Absolute (h);
8826f46f 488
7bcb11d3 489 m_controlBar->SetConstraints(c1);
8826f46f 490
7bcb11d3
JS
491 // Canvas constraints
492 wxLayoutConstraints *c2 = new wxLayoutConstraints;
8826f46f 493
7bcb11d3
JS
494 c2->left.SameAs (this, wxLeft);
495 c2->top.Below (m_controlBar);
496 c2->right.SameAs (this, wxRight);
497 c2->bottom.SameAs (this, wxBottom);
8826f46f 498
7bcb11d3 499 m_previewCanvas->SetConstraints(c2);
8826f46f 500
7bcb11d3 501 SetAutoLayout(TRUE);
8826f46f 502
7bcb11d3 503 MakeModal(TRUE);
8826f46f 504
7bcb11d3 505 Layout();
c801d85f
KB
506}
507
34da0970 508void wxPreviewFrame::CreateCanvas()
c801d85f 509{
7bcb11d3 510 m_previewCanvas = new wxPreviewCanvas(m_printPreview, this);
c801d85f
KB
511}
512
34da0970 513void wxPreviewFrame::CreateControlBar()
c801d85f 514{
7bcb11d3
JS
515 long buttons = wxPREVIEW_DEFAULT;
516 if (m_printPreview->GetPrintoutForPrinting())
517 buttons |= wxPREVIEW_PRINT;
8826f46f 518
7bcb11d3
JS
519 m_controlBar = new wxPreviewControlBar(m_printPreview, buttons, this, wxPoint(0, 0), wxSize(400, 40));
520 m_controlBar->CreateButtons();
c801d85f 521}
7bcb11d3 522
c801d85f 523/*
7bcb11d3
JS
524* Print preview
525*/
c801d85f 526
8826f46f
VZ
527wxPrintPreviewBase::wxPrintPreviewBase(wxPrintout *printout,
528 wxPrintout *printoutForPrinting,
529 wxPrintData *data)
530{
531 if (data)
532 m_printDialogData = (*data);
533
534 Init(printout, printoutForPrinting);
535}
536
537wxPrintPreviewBase::wxPrintPreviewBase(wxPrintout *printout,
538 wxPrintout *printoutForPrinting,
539 wxPrintDialogData *data)
540{
541 if (data)
542 m_printDialogData = (*data);
543
544 Init(printout, printoutForPrinting);
545}
546
547void wxPrintPreviewBase::Init(wxPrintout *printout,
548 wxPrintout *printoutForPrinting)
c801d85f 549{
7bcb11d3
JS
550 m_isOk = TRUE;
551 m_previewPrintout = printout;
552 if (m_previewPrintout)
553 m_previewPrintout->SetIsPreview(TRUE);
8826f46f 554
7bcb11d3 555 m_printPrintout = printoutForPrinting;
8826f46f 556
7bcb11d3
JS
557 m_previewCanvas = NULL;
558 m_previewFrame = NULL;
559 m_previewBitmap = NULL;
560 m_currentPage = 1;
c25ccf85 561 m_currentZoom = 70;
7bcb11d3
JS
562 m_topMargin = 40;
563 m_leftMargin = 40;
564 m_pageWidth = 0;
565 m_pageHeight = 0;
1044a386 566 m_printingPrepared = FALSE;
8826f46f 567
1044a386
JS
568 // Too soon! Moved to RenderPage.
569 // printout->OnPreparePrinting();
8826f46f 570
7bcb11d3
JS
571 // Get some parameters from the printout, if defined
572 int selFrom, selTo;
573 printout->GetPageInfo(&m_minPage, &m_maxPage, &selFrom, &selTo);
c801d85f
KB
574}
575
34da0970 576wxPrintPreviewBase::~wxPrintPreviewBase()
c801d85f 577{
7bcb11d3
JS
578 if (m_previewPrintout)
579 delete m_previewPrintout;
580 if (m_previewBitmap)
581 delete m_previewBitmap;
582 if (m_printPrintout)
583 delete m_printPrintout;
c801d85f
KB
584}
585
586bool wxPrintPreviewBase::SetCurrentPage(int pageNum)
587{
7bcb11d3
JS
588 if (m_currentPage == pageNum)
589 return TRUE;
8826f46f 590
7bcb11d3
JS
591 m_currentPage = pageNum;
592 if (m_previewBitmap)
593 {
594 delete m_previewBitmap;
595 m_previewBitmap = NULL;
596 }
8826f46f 597
7bcb11d3
JS
598 if (m_previewCanvas)
599 {
600 RenderPage(pageNum);
601 m_previewCanvas->Refresh();
602 }
c801d85f 603 return TRUE;
c801d85f
KB
604}
605
606bool wxPrintPreviewBase::PaintPage(wxWindow *canvas, wxDC& dc)
607{
7bcb11d3 608 DrawBlankPage(canvas, dc);
8826f46f 609
7bcb11d3
JS
610 if (!m_previewBitmap)
611 RenderPage(m_currentPage);
8826f46f 612
7bcb11d3
JS
613 if (!m_previewBitmap)
614 return FALSE;
8826f46f 615
7bcb11d3
JS
616 if (!canvas)
617 return FALSE;
8826f46f 618
7bcb11d3
JS
619 int canvasWidth, canvasHeight;
620 canvas->GetSize(&canvasWidth, &canvasHeight);
8826f46f 621
7bcb11d3
JS
622 double zoomScale = ((float)m_currentZoom/(float)100);
623 double actualWidth = (zoomScale*m_pageWidth*m_previewScale);
624 // float actualHeight = (float)(zoomScale*m_pageHeight*m_previewScale);
8826f46f 625
7bcb11d3
JS
626 int x = (int) ((canvasWidth - actualWidth)/2.0);
627 if (x < m_leftMargin)
628 x = m_leftMargin;
629 int y = m_topMargin;
8826f46f 630
7bcb11d3
JS
631 wxMemoryDC temp_dc;
632 temp_dc.SelectObject(*m_previewBitmap);
8826f46f 633
7bcb11d3 634 dc.Blit(x, y, m_previewBitmap->GetWidth(), m_previewBitmap->GetHeight(), &temp_dc, 0, 0);
8826f46f 635
7bcb11d3 636 temp_dc.SelectObject(wxNullBitmap);
8826f46f 637
7bcb11d3 638 return TRUE;
c801d85f
KB
639}
640
641bool wxPrintPreviewBase::RenderPage(int pageNum)
642{
7bcb11d3 643 int canvasWidth, canvasHeight;
8826f46f 644
7bcb11d3 645 if (!m_previewCanvas)
c801d85f 646 {
7bcb11d3
JS
647 wxMessageBox(_("wxPrintPreviewBase::RenderPage: must use wxPrintPreviewBase::SetCanvas to let me know about the canvas!"),
648 _("Print Preview Failure"), wxOK);
649 return FALSE;
c801d85f 650 }
7bcb11d3 651 m_previewCanvas->GetSize(&canvasWidth, &canvasHeight);
8826f46f 652
7bcb11d3
JS
653 double zoomScale = (m_currentZoom/100.0);
654 int actualWidth = (int)(zoomScale*m_pageWidth*m_previewScale);
655 int actualHeight = (int)(zoomScale*m_pageHeight*m_previewScale);
8826f46f 656
7bcb11d3
JS
657 int x = (int)((canvasWidth - actualWidth)/2.0);
658 if (x < m_leftMargin)
659 x = m_leftMargin;
660 // int y = m_topMargin;
8826f46f
VZ
661
662
7bcb11d3
JS
663 if (!m_previewBitmap)
664 {
665 m_previewBitmap = new wxBitmap((int)actualWidth, (int)actualHeight);
666 if (!m_previewBitmap || !m_previewBitmap->Ok())
667 {
668 if (m_previewBitmap)
669 delete m_previewBitmap;
670 wxMessageBox(_("Sorry, not enough memory to create a preview."), _("Print Preview Failure"), wxOK);
671 return FALSE;
672 }
673 }
8826f46f 674
7bcb11d3
JS
675 wxMemoryDC memoryDC;
676 memoryDC.SelectObject(*m_previewBitmap);
8826f46f 677
7bcb11d3 678 memoryDC.Clear();
8826f46f 679
7bcb11d3
JS
680 m_previewPrintout->SetDC(&memoryDC);
681 m_previewPrintout->SetPageSizePixels(m_pageWidth, m_pageHeight);
8826f46f 682
1044a386
JS
683 // Need to delay OnPreparePrinting until here, so we have enough information.
684 if (!m_printingPrepared)
685 {
686 m_previewPrintout->OnPreparePrinting();
687 m_printingPrepared = TRUE;
688 }
689
7bcb11d3 690 m_previewPrintout->OnBeginPrinting();
c801d85f 691
7bcb11d3
JS
692 if (!m_previewPrintout->OnBeginDocument(m_printDialogData.GetFromPage(), m_printDialogData.GetToPage()))
693 {
694 wxMessageBox(_("Could not start document preview."), _("Print Preview Failure"), wxOK);
8826f46f 695
7bcb11d3 696 memoryDC.SelectObject(wxNullBitmap);
8826f46f 697
7bcb11d3
JS
698 delete m_previewBitmap;
699 return FALSE;
700 }
8826f46f 701
7bcb11d3
JS
702 m_previewPrintout->OnPrintPage(pageNum);
703 m_previewPrintout->OnEndDocument();
704 m_previewPrintout->OnEndPrinting();
8826f46f 705
7bcb11d3 706 m_previewPrintout->SetDC(NULL);
8826f46f 707
c801d85f 708 memoryDC.SelectObject(wxNullBitmap);
8826f46f 709
cf2f341a 710 wxChar buf[200];
7bcb11d3 711 if (m_maxPage != 0)
cf2f341a 712 wxSprintf(buf, _("Page %d of %d"), pageNum, m_maxPage);
7bcb11d3 713 else
cf2f341a 714 wxSprintf(buf, _("Page %d"), pageNum);
8826f46f 715
7bcb11d3
JS
716 if (m_previewFrame)
717 m_previewFrame->SetStatusText(buf);
8826f46f 718
7bcb11d3 719 return TRUE;
c801d85f
KB
720}
721
722
723bool wxPrintPreviewBase::DrawBlankPage(wxWindow *canvas, wxDC& dc)
724{
7bcb11d3
JS
725 int canvasWidth, canvasHeight;
726 canvas->GetSize(&canvasWidth, &canvasHeight);
8826f46f 727
7bcb11d3
JS
728 float zoomScale = (float)((float)m_currentZoom/(float)100);
729 float actualWidth = zoomScale*m_pageWidth*m_previewScale;
730 float actualHeight = zoomScale*m_pageHeight*m_previewScale;
8826f46f 731
7bcb11d3
JS
732 float x = (float)((canvasWidth - actualWidth)/2.0);
733 if (x < m_leftMargin)
734 x = (float)m_leftMargin;
735 float y = (float)m_topMargin;
8826f46f 736
7bcb11d3
JS
737 // Draw shadow, allowing for 1-pixel border AROUND the actual page
738 int shadowOffset = 4;
739 dc.SetPen(*wxBLACK_PEN);
740 dc.SetBrush(*wxBLACK_BRUSH);
741 /*
742 dc.DrawRectangle((int)(x-1 + shadowOffset), (int)(y-1 + shadowOffset), (int)(actualWidth+2), (int)(actualHeight+2));
743 */
744 dc.DrawRectangle((int)(x + shadowOffset), (int)(y + actualHeight+1), (int)(actualWidth), shadowOffset);
745 dc.DrawRectangle((int)(x + actualWidth), (int)(y + shadowOffset), shadowOffset, (int)(actualHeight));
8826f46f 746
7bcb11d3
JS
747 // Draw blank page allowing for 1-pixel border AROUND the actual page
748 dc.SetPen(*wxBLACK_PEN);
749 dc.SetBrush(*wxWHITE_BRUSH);
8826f46f 750
7bcb11d3
JS
751 /*
752 wxRegion update_region = canvas->GetUpdateRegion();
753 wxRect r = update_region.GetBox();
8826f46f 754
7bcb11d3
JS
755 printf( "x: %d y: %d w: %d h: %d.\n", (int)r.x, (int)r.y, (int)r.width, (int)r.height );
756 */
8826f46f 757
7bcb11d3 758 dc.DrawRectangle((int)(x-2), (int)(y-1), (int)(actualWidth+3), (int)(actualHeight+2));
8826f46f 759
2a47d3c1
JS
760 return TRUE;
761}
762
7bcb11d3 763void wxPrintPreviewBase::SetZoom(int percent)
2a47d3c1 764{
7bcb11d3
JS
765 if (m_currentZoom == percent)
766 return;
8826f46f 767
7bcb11d3
JS
768 m_currentZoom = percent;
769 if (m_previewBitmap)
770 {
771 delete m_previewBitmap;
772 m_previewBitmap = NULL;
773 }
aff37a19 774
8826f46f 775
7bcb11d3
JS
776 if (m_previewCanvas)
777 {
aff37a19 778 RenderPage(m_currentPage);
7bcb11d3
JS
779 m_previewCanvas->Clear();
780 m_previewCanvas->Refresh();
781 }
2a47d3c1
JS
782}
783
d427503c 784#endif // wxUSE_PRINTING_ARCHITECTURE