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