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