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