]> git.saurik.com Git - wxWidgets.git/blame - src/common/prntbase.cpp
just to resolve a dummy conflict
[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];
4de6207a 412 if (m_zoomControl && (m_zoomControl->GetStringSelection() != _T("")))
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;
8826f46f 566
7bcb11d3 567 printout->OnPreparePrinting();
8826f46f 568
7bcb11d3
JS
569 // Get some parameters from the printout, if defined
570 int selFrom, selTo;
571 printout->GetPageInfo(&m_minPage, &m_maxPage, &selFrom, &selTo);
c801d85f
KB
572}
573
34da0970 574wxPrintPreviewBase::~wxPrintPreviewBase()
c801d85f 575{
7bcb11d3
JS
576 if (m_previewPrintout)
577 delete m_previewPrintout;
578 if (m_previewBitmap)
579 delete m_previewBitmap;
580 if (m_printPrintout)
581 delete m_printPrintout;
c801d85f
KB
582}
583
584bool wxPrintPreviewBase::SetCurrentPage(int pageNum)
585{
7bcb11d3
JS
586 if (m_currentPage == pageNum)
587 return TRUE;
8826f46f 588
7bcb11d3
JS
589 m_currentPage = pageNum;
590 if (m_previewBitmap)
591 {
592 delete m_previewBitmap;
593 m_previewBitmap = NULL;
594 }
8826f46f 595
7bcb11d3
JS
596 if (m_previewCanvas)
597 {
598 RenderPage(pageNum);
599 m_previewCanvas->Refresh();
600 }
c801d85f 601 return TRUE;
c801d85f
KB
602}
603
604bool wxPrintPreviewBase::PaintPage(wxWindow *canvas, wxDC& dc)
605{
7bcb11d3 606 DrawBlankPage(canvas, dc);
8826f46f 607
7bcb11d3
JS
608 if (!m_previewBitmap)
609 RenderPage(m_currentPage);
8826f46f 610
7bcb11d3
JS
611 if (!m_previewBitmap)
612 return FALSE;
8826f46f 613
7bcb11d3
JS
614 if (!canvas)
615 return FALSE;
8826f46f 616
7bcb11d3
JS
617 int canvasWidth, canvasHeight;
618 canvas->GetSize(&canvasWidth, &canvasHeight);
8826f46f 619
7bcb11d3
JS
620 double zoomScale = ((float)m_currentZoom/(float)100);
621 double actualWidth = (zoomScale*m_pageWidth*m_previewScale);
622 // float actualHeight = (float)(zoomScale*m_pageHeight*m_previewScale);
8826f46f 623
7bcb11d3
JS
624 int x = (int) ((canvasWidth - actualWidth)/2.0);
625 if (x < m_leftMargin)
626 x = m_leftMargin;
627 int y = m_topMargin;
8826f46f 628
7bcb11d3
JS
629 wxMemoryDC temp_dc;
630 temp_dc.SelectObject(*m_previewBitmap);
8826f46f 631
7bcb11d3 632 dc.Blit(x, y, m_previewBitmap->GetWidth(), m_previewBitmap->GetHeight(), &temp_dc, 0, 0);
8826f46f 633
7bcb11d3 634 temp_dc.SelectObject(wxNullBitmap);
8826f46f 635
7bcb11d3 636 return TRUE;
c801d85f
KB
637}
638
639bool wxPrintPreviewBase::RenderPage(int pageNum)
640{
7bcb11d3 641 int canvasWidth, canvasHeight;
8826f46f 642
7bcb11d3 643 if (!m_previewCanvas)
c801d85f 644 {
7bcb11d3
JS
645 wxMessageBox(_("wxPrintPreviewBase::RenderPage: must use wxPrintPreviewBase::SetCanvas to let me know about the canvas!"),
646 _("Print Preview Failure"), wxOK);
647 return FALSE;
c801d85f 648 }
7bcb11d3 649 m_previewCanvas->GetSize(&canvasWidth, &canvasHeight);
8826f46f 650
7bcb11d3
JS
651 double zoomScale = (m_currentZoom/100.0);
652 int actualWidth = (int)(zoomScale*m_pageWidth*m_previewScale);
653 int actualHeight = (int)(zoomScale*m_pageHeight*m_previewScale);
8826f46f 654
7bcb11d3
JS
655 int x = (int)((canvasWidth - actualWidth)/2.0);
656 if (x < m_leftMargin)
657 x = m_leftMargin;
658 // int y = m_topMargin;
8826f46f
VZ
659
660
7bcb11d3
JS
661 if (!m_previewBitmap)
662 {
663 m_previewBitmap = new wxBitmap((int)actualWidth, (int)actualHeight);
664 if (!m_previewBitmap || !m_previewBitmap->Ok())
665 {
666 if (m_previewBitmap)
667 delete m_previewBitmap;
668 wxMessageBox(_("Sorry, not enough memory to create a preview."), _("Print Preview Failure"), wxOK);
669 return FALSE;
670 }
671 }
8826f46f 672
7bcb11d3
JS
673 wxMemoryDC memoryDC;
674 memoryDC.SelectObject(*m_previewBitmap);
8826f46f 675
7bcb11d3 676 memoryDC.Clear();
8826f46f 677
7bcb11d3
JS
678 m_previewPrintout->SetDC(&memoryDC);
679 m_previewPrintout->SetPageSizePixels(m_pageWidth, m_pageHeight);
8826f46f 680
7bcb11d3 681 m_previewPrintout->OnBeginPrinting();
c801d85f 682
7bcb11d3
JS
683 if (!m_previewPrintout->OnBeginDocument(m_printDialogData.GetFromPage(), m_printDialogData.GetToPage()))
684 {
685 wxMessageBox(_("Could not start document preview."), _("Print Preview Failure"), wxOK);
8826f46f 686
7bcb11d3 687 memoryDC.SelectObject(wxNullBitmap);
8826f46f 688
7bcb11d3
JS
689 delete m_previewBitmap;
690 return FALSE;
691 }
8826f46f 692
7bcb11d3
JS
693 m_previewPrintout->OnPrintPage(pageNum);
694 m_previewPrintout->OnEndDocument();
695 m_previewPrintout->OnEndPrinting();
8826f46f 696
7bcb11d3 697 m_previewPrintout->SetDC(NULL);
8826f46f 698
c801d85f 699 memoryDC.SelectObject(wxNullBitmap);
8826f46f 700
cf2f341a 701 wxChar buf[200];
7bcb11d3 702 if (m_maxPage != 0)
cf2f341a 703 wxSprintf(buf, _("Page %d of %d"), pageNum, m_maxPage);
7bcb11d3 704 else
cf2f341a 705 wxSprintf(buf, _("Page %d"), pageNum);
8826f46f 706
7bcb11d3
JS
707 if (m_previewFrame)
708 m_previewFrame->SetStatusText(buf);
8826f46f 709
7bcb11d3 710 return TRUE;
c801d85f
KB
711}
712
713
714bool wxPrintPreviewBase::DrawBlankPage(wxWindow *canvas, wxDC& dc)
715{
7bcb11d3
JS
716 int canvasWidth, canvasHeight;
717 canvas->GetSize(&canvasWidth, &canvasHeight);
8826f46f 718
7bcb11d3
JS
719 float zoomScale = (float)((float)m_currentZoom/(float)100);
720 float actualWidth = zoomScale*m_pageWidth*m_previewScale;
721 float actualHeight = zoomScale*m_pageHeight*m_previewScale;
8826f46f 722
7bcb11d3
JS
723 float x = (float)((canvasWidth - actualWidth)/2.0);
724 if (x < m_leftMargin)
725 x = (float)m_leftMargin;
726 float y = (float)m_topMargin;
8826f46f 727
7bcb11d3
JS
728 // Draw shadow, allowing for 1-pixel border AROUND the actual page
729 int shadowOffset = 4;
730 dc.SetPen(*wxBLACK_PEN);
731 dc.SetBrush(*wxBLACK_BRUSH);
732 /*
733 dc.DrawRectangle((int)(x-1 + shadowOffset), (int)(y-1 + shadowOffset), (int)(actualWidth+2), (int)(actualHeight+2));
734 */
735 dc.DrawRectangle((int)(x + shadowOffset), (int)(y + actualHeight+1), (int)(actualWidth), shadowOffset);
736 dc.DrawRectangle((int)(x + actualWidth), (int)(y + shadowOffset), shadowOffset, (int)(actualHeight));
8826f46f 737
7bcb11d3
JS
738 // Draw blank page allowing for 1-pixel border AROUND the actual page
739 dc.SetPen(*wxBLACK_PEN);
740 dc.SetBrush(*wxWHITE_BRUSH);
8826f46f 741
7bcb11d3
JS
742 /*
743 wxRegion update_region = canvas->GetUpdateRegion();
744 wxRect r = update_region.GetBox();
8826f46f 745
7bcb11d3
JS
746 printf( "x: %d y: %d w: %d h: %d.\n", (int)r.x, (int)r.y, (int)r.width, (int)r.height );
747 */
8826f46f 748
7bcb11d3 749 dc.DrawRectangle((int)(x-2), (int)(y-1), (int)(actualWidth+3), (int)(actualHeight+2));
8826f46f 750
2a47d3c1
JS
751 return TRUE;
752}
753
7bcb11d3 754void wxPrintPreviewBase::SetZoom(int percent)
2a47d3c1 755{
7bcb11d3
JS
756 if (m_currentZoom == percent)
757 return;
8826f46f 758
7bcb11d3
JS
759 m_currentZoom = percent;
760 if (m_previewBitmap)
761 {
762 delete m_previewBitmap;
763 m_previewBitmap = NULL;
764 }
aff37a19 765
8826f46f 766
7bcb11d3
JS
767 if (m_previewCanvas)
768 {
aff37a19 769 RenderPage(m_currentPage);
7bcb11d3
JS
770 m_previewCanvas->Clear();
771 m_previewCanvas->Refresh();
772 }
2a47d3c1
JS
773}
774
d427503c 775#endif // wxUSE_PRINTING_ARCHITECTURE