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