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