| 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 |
| 9 | // Licence: wxWindows licence |
| 10 | ///////////////////////////////////////////////////////////////////////////// |
| 11 | |
| 12 | #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA) |
| 13 | #pragma implementation "prntbase.h" |
| 14 | #pragma implementation "printdlg.h" |
| 15 | #endif |
| 16 | |
| 17 | // For compilers that support precompilation, includes "wx.h". |
| 18 | #include "wx/wxprec.h" |
| 19 | |
| 20 | #ifdef __BORLANDC__ |
| 21 | #pragma hdrstop |
| 22 | #endif |
| 23 | |
| 24 | #include "wx/defs.h" |
| 25 | |
| 26 | #if wxUSE_PRINTING_ARCHITECTURE |
| 27 | |
| 28 | #ifndef WX_PRECOMP |
| 29 | #include "wx/utils.h" |
| 30 | #include "wx/dc.h" |
| 31 | #include "wx/app.h" |
| 32 | #include "wx/msgdlg.h" |
| 33 | #include "wx/layout.h" |
| 34 | #include "wx/choice.h" |
| 35 | #include "wx/button.h" |
| 36 | #include "wx/settings.h" |
| 37 | #include "wx/dcmemory.h" |
| 38 | #include "wx/stattext.h" |
| 39 | #include "wx/intl.h" |
| 40 | #include "wx/textdlg.h" |
| 41 | #include "wx/sizer.h" |
| 42 | #endif // !WX_PRECOMP |
| 43 | |
| 44 | #include "wx/prntbase.h" |
| 45 | #include "wx/dcprint.h" |
| 46 | #include "wx/printdlg.h" |
| 47 | #include "wx/print.h" |
| 48 | #include "wx/module.h" |
| 49 | |
| 50 | #include <stdlib.h> |
| 51 | #include <string.h> |
| 52 | |
| 53 | #ifdef __WXMSW__ |
| 54 | #include "wx/msw/private.h" |
| 55 | #include <commdlg.h> |
| 56 | |
| 57 | #ifndef __WIN32__ |
| 58 | #include <print.h> |
| 59 | #endif |
| 60 | #endif // __WXMSW__ |
| 61 | |
| 62 | IMPLEMENT_CLASS(wxPrintPreviewBase, wxObject) |
| 63 | |
| 64 | //---------------------------------------------------------------------------- |
| 65 | // wxPrintFactory |
| 66 | //---------------------------------------------------------------------------- |
| 67 | |
| 68 | wxPrintFactory *wxPrintFactory::m_factory = NULL; |
| 69 | |
| 70 | void wxPrintFactory::SetPrintFactory( wxPrintFactory *factory ) |
| 71 | { |
| 72 | if (wxPrintFactory::m_factory) |
| 73 | delete wxPrintFactory::m_factory; |
| 74 | |
| 75 | wxPrintFactory::m_factory = factory; |
| 76 | } |
| 77 | |
| 78 | wxPrintFactory *wxPrintFactory::GetFactory() |
| 79 | { |
| 80 | if (!wxPrintFactory::m_factory) |
| 81 | wxPrintFactory::m_factory = new wxNativePrintFactory; |
| 82 | |
| 83 | return wxPrintFactory::m_factory; |
| 84 | } |
| 85 | |
| 86 | //---------------------------------------------------------------------------- |
| 87 | // wxNativePrintFactory |
| 88 | //---------------------------------------------------------------------------- |
| 89 | |
| 90 | wxPrinterBase *wxNativePrintFactory::CreatePrinter( wxPrintDialogData *data ) |
| 91 | { |
| 92 | #if defined(__WXMSW__) && !defined(__WXUNIVERSAL__) |
| 93 | return new wxWindowsPrinter( data ); |
| 94 | #elif defined(__WXMAC__) |
| 95 | return new wxMacPrinter( data ); |
| 96 | #elif defined(__WXPM__) |
| 97 | return new wxOS2Printer( data ); |
| 98 | #else |
| 99 | return new wxPostScriptPrinter( data ); |
| 100 | #endif |
| 101 | }; |
| 102 | |
| 103 | wxPrintPreviewBase *wxNativePrintFactory::CreatePrintPreview( wxPrintout *preview, |
| 104 | wxPrintout *printout, wxPrintDialogData *data ) |
| 105 | { |
| 106 | #if defined(__WXMSW__) && !defined(__WXUNIVERSAL__) |
| 107 | return new wxWindowsPrintPreview( preview, printout, data ); |
| 108 | #elif defined(__WXMAC__) |
| 109 | return new wxMacPrintPreview( preview, printout, data ); |
| 110 | #elif defined(__WXPM__) |
| 111 | return new wxOS2PrintPreview( preview, printout, data ); |
| 112 | #else |
| 113 | return new wxPostScriptPrintPreview( preview, printout, data ); |
| 114 | #endif |
| 115 | } |
| 116 | |
| 117 | wxPrintPreviewBase *wxNativePrintFactory::CreatePrintPreview( wxPrintout *preview, |
| 118 | wxPrintout *printout, wxPrintData *data ) |
| 119 | { |
| 120 | #if defined(__WXMSW__) && !defined(__WXUNIVERSAL__) |
| 121 | return new wxWindowsPrintPreview( preview, printout, data ); |
| 122 | #elif defined(__WXMAC__) |
| 123 | return new wxMacPrintPreview( preview, printout, data ); |
| 124 | #elif defined(__WXPM__) |
| 125 | return new wxOS2PrintPreview( preview, printout, data ); |
| 126 | #else |
| 127 | return new wxPostScriptPrintPreview( preview, printout, data ); |
| 128 | #endif |
| 129 | } |
| 130 | |
| 131 | wxPrintDialogBase *wxNativePrintFactory::CreatePrintDialog( wxWindow *parent, |
| 132 | wxPrintDialogData *data ) |
| 133 | { |
| 134 | #if defined(__WXMSW__) && !defined(__WXUNIVERSAL__) |
| 135 | return new wxWindowsPrintDialog( parent, data ); |
| 136 | #elif defined(__WXMAC__) |
| 137 | return new wxMacPrintDialog( parent, data ); |
| 138 | #else |
| 139 | return new wxGenericPrintDialog( parent, data ); |
| 140 | #endif |
| 141 | } |
| 142 | |
| 143 | wxPrintDialogBase *wxNativePrintFactory::CreatePrintDialog( wxWindow *parent, |
| 144 | wxPrintData *data ) |
| 145 | { |
| 146 | #if defined(__WXMSW__) && !defined(__WXUNIVERSAL__) |
| 147 | return new wxWindowsPrintDialog( parent, data ); |
| 148 | #elif defined(__WXMAC__) |
| 149 | return new wxMacPrintDialog( parent, data ); |
| 150 | #else |
| 151 | return new wxGenericPrintDialog( parent, data ); |
| 152 | #endif |
| 153 | } |
| 154 | |
| 155 | bool wxNativePrintFactory::HasPrintSetupDialog() |
| 156 | { |
| 157 | #if defined(__WXMSW__) && !defined(__WXUNIVERSAL__) |
| 158 | return false; |
| 159 | #elif defined(__WXMAC__) |
| 160 | return false; |
| 161 | #else |
| 162 | // Only here do we need to provide the print setup |
| 163 | // dialog ourselves, the other platforms either have |
| 164 | // none, don't make it accessible or let you configure |
| 165 | // the printer from the wxPrintDialog anyway. |
| 166 | return true; |
| 167 | #endif |
| 168 | |
| 169 | } |
| 170 | |
| 171 | wxDialog *wxNativePrintFactory::CreatePrintSetupDialog( wxWindow *parent, wxPrintData *data ) |
| 172 | { |
| 173 | #if defined(__WXMSW__) && !defined(__WXUNIVERSAL__) |
| 174 | wxUnusedVar(parent); |
| 175 | wxUnusedVar(data); |
| 176 | return NULL; |
| 177 | #elif defined(__WXMAC__) |
| 178 | wxUnusedVar(parent); |
| 179 | wxUnusedVar(data); |
| 180 | return NULL; |
| 181 | #else |
| 182 | // Only here do we need to provide the print setup |
| 183 | // dialog ourselves, the other platforms either have |
| 184 | // none, don't make it accessible or let you configure |
| 185 | // the printer from the wxPrintDialog anyway. |
| 186 | return new wxGenericPrintSetupDialog( parent, data ); |
| 187 | #endif |
| 188 | } |
| 189 | |
| 190 | bool wxNativePrintFactory::HasOwnPrintToFile() |
| 191 | { |
| 192 | // Only relevant for PostScript and here the |
| 193 | // setup dialog provides no "print to file" |
| 194 | // option. In the GNOME setup dialog, the |
| 195 | // setup dialog has its own print to file. |
| 196 | return false; |
| 197 | } |
| 198 | |
| 199 | bool wxNativePrintFactory::HasPrinterLine() |
| 200 | { |
| 201 | // Only relevant for PostScript for now |
| 202 | return true; |
| 203 | } |
| 204 | |
| 205 | wxString wxNativePrintFactory::CreatePrinterLine() |
| 206 | { |
| 207 | // Only relevant for PostScript for now |
| 208 | |
| 209 | // We should query "lpstat -d" here |
| 210 | return _("Generic PostScript"); |
| 211 | } |
| 212 | |
| 213 | bool wxNativePrintFactory::HasStatusLine() |
| 214 | { |
| 215 | // Only relevant for PostScript for now |
| 216 | return true; |
| 217 | } |
| 218 | |
| 219 | wxString wxNativePrintFactory::CreateStatusLine() |
| 220 | { |
| 221 | // Only relevant for PostScript for now |
| 222 | |
| 223 | // We should query "lpstat -r" or "lpstat -p" here |
| 224 | return _("Ready"); |
| 225 | } |
| 226 | |
| 227 | wxPrintNativeDataBase *wxNativePrintFactory::CreatePrintNativeData() |
| 228 | { |
| 229 | #if defined(__WXMSW__) && !defined(__WXUNIVERSAL__) |
| 230 | return new wxWindowsPrintNativeData; |
| 231 | #elif defined(__WXMAC__) |
| 232 | return new wxMacPrintNativeData; |
| 233 | #else |
| 234 | return new wxPostScriptPrintNativeData; |
| 235 | #endif |
| 236 | } |
| 237 | |
| 238 | //---------------------------------------------------------------------------- |
| 239 | // wxPrintNativeDataBase |
| 240 | //---------------------------------------------------------------------------- |
| 241 | |
| 242 | IMPLEMENT_ABSTRACT_CLASS(wxPrintNativeDataBase, wxObject) |
| 243 | |
| 244 | wxPrintNativeDataBase::wxPrintNativeDataBase() |
| 245 | { |
| 246 | m_ref = 1; |
| 247 | } |
| 248 | |
| 249 | //---------------------------------------------------------------------------- |
| 250 | // wxPrintFactoryModule |
| 251 | //---------------------------------------------------------------------------- |
| 252 | |
| 253 | class wxPrintFactoryModule: public wxModule |
| 254 | { |
| 255 | public: |
| 256 | wxPrintFactoryModule() {} |
| 257 | bool OnInit() { return true; } |
| 258 | void OnExit() { wxPrintFactory::SetPrintFactory( NULL ); } |
| 259 | |
| 260 | private: |
| 261 | DECLARE_DYNAMIC_CLASS(wxPrintFactoryModule) |
| 262 | }; |
| 263 | |
| 264 | IMPLEMENT_DYNAMIC_CLASS(wxPrintFactoryModule, wxModule) |
| 265 | |
| 266 | //---------------------------------------------------------------------------- |
| 267 | // wxPrinterBase |
| 268 | //---------------------------------------------------------------------------- |
| 269 | |
| 270 | IMPLEMENT_CLASS(wxPrinterBase, wxObject) |
| 271 | |
| 272 | wxPrinterBase::wxPrinterBase(wxPrintDialogData *data) |
| 273 | { |
| 274 | m_currentPrintout = (wxPrintout *) NULL; |
| 275 | sm_abortWindow = (wxWindow *) NULL; |
| 276 | sm_abortIt = false; |
| 277 | if (data) |
| 278 | m_printDialogData = (*data); |
| 279 | sm_lastError = wxPRINTER_NO_ERROR; |
| 280 | } |
| 281 | |
| 282 | wxWindow *wxPrinterBase::sm_abortWindow = (wxWindow *) NULL; |
| 283 | bool wxPrinterBase::sm_abortIt = false; |
| 284 | wxPrinterError wxPrinterBase::sm_lastError = wxPRINTER_NO_ERROR; |
| 285 | |
| 286 | wxPrinterBase::~wxPrinterBase() |
| 287 | { |
| 288 | } |
| 289 | |
| 290 | wxWindow *wxPrinterBase::CreateAbortWindow(wxWindow *parent, wxPrintout * printout) |
| 291 | { |
| 292 | wxPrintAbortDialog *dialog = new wxPrintAbortDialog(parent, _("Printing ") , wxDefaultPosition, wxDefaultSize, wxDEFAULT_DIALOG_STYLE); |
| 293 | |
| 294 | wxBoxSizer *button_sizer = new wxBoxSizer( wxVERTICAL ); |
| 295 | button_sizer->Add( new wxStaticText(dialog, wxID_ANY, _("Please wait while printing\n") + printout->GetTitle() ), 0, wxALL, 10 ); |
| 296 | button_sizer->Add( new wxButton( dialog, wxID_CANCEL, wxT("Cancel") ), 0, wxALL | wxALIGN_CENTER, 10 ); |
| 297 | |
| 298 | dialog->SetAutoLayout( true ); |
| 299 | dialog->SetSizer( button_sizer ); |
| 300 | |
| 301 | button_sizer->Fit(dialog); |
| 302 | button_sizer->SetSizeHints (dialog) ; |
| 303 | |
| 304 | return dialog; |
| 305 | } |
| 306 | |
| 307 | void wxPrinterBase::ReportError(wxWindow *parent, wxPrintout *WXUNUSED(printout), const wxString& message) |
| 308 | { |
| 309 | wxMessageBox(message, _("Printing Error"), wxOK, parent); |
| 310 | } |
| 311 | |
| 312 | wxPrintDialogData& wxPrinterBase::GetPrintDialogData() const |
| 313 | { |
| 314 | return (wxPrintDialogData&) m_printDialogData; |
| 315 | } |
| 316 | |
| 317 | //---------------------------------------------------------------------------- |
| 318 | // wxPrinter |
| 319 | //---------------------------------------------------------------------------- |
| 320 | |
| 321 | IMPLEMENT_CLASS(wxPrinter, wxPrinterBase) |
| 322 | |
| 323 | wxPrinter::wxPrinter(wxPrintDialogData *data) |
| 324 | { |
| 325 | m_pimpl = wxPrintFactory::GetFactory()->CreatePrinter( data ); |
| 326 | } |
| 327 | |
| 328 | wxPrinter::~wxPrinter() |
| 329 | { |
| 330 | delete m_pimpl; |
| 331 | } |
| 332 | |
| 333 | wxWindow *wxPrinter::CreateAbortWindow(wxWindow *parent, wxPrintout *printout) |
| 334 | { |
| 335 | return m_pimpl->CreateAbortWindow( parent, printout ); |
| 336 | } |
| 337 | |
| 338 | void wxPrinter::ReportError(wxWindow *parent, wxPrintout *printout, const wxString& message) |
| 339 | { |
| 340 | m_pimpl->ReportError( parent, printout, message ); |
| 341 | } |
| 342 | |
| 343 | bool wxPrinter::Setup(wxWindow *parent) |
| 344 | { |
| 345 | return m_pimpl->Setup( parent ); |
| 346 | } |
| 347 | |
| 348 | bool wxPrinter::Print(wxWindow *parent, wxPrintout *printout, bool prompt) |
| 349 | { |
| 350 | return m_pimpl->Print( parent, printout, prompt ); |
| 351 | } |
| 352 | |
| 353 | wxDC* wxPrinter::PrintDialog(wxWindow *parent) |
| 354 | { |
| 355 | return m_pimpl->PrintDialog( parent ); |
| 356 | } |
| 357 | |
| 358 | wxPrintDialogData& wxPrinter::GetPrintDialogData() const |
| 359 | { |
| 360 | return m_pimpl->GetPrintDialogData(); |
| 361 | } |
| 362 | |
| 363 | // --------------------------------------------------------------------------- |
| 364 | // wxPrintDialogBase: the common dialog for printing. |
| 365 | // --------------------------------------------------------------------------- |
| 366 | |
| 367 | IMPLEMENT_ABSTRACT_CLASS(wxPrintDialogBase, wxObject) |
| 368 | |
| 369 | wxPrintDialogBase::wxPrintDialogBase(wxWindow *parent, |
| 370 | wxWindowID id, |
| 371 | const wxString &title, |
| 372 | const wxPoint &pos, |
| 373 | const wxSize &size, |
| 374 | long style) |
| 375 | : wxDialog |
| 376 | ( |
| 377 | parent, |
| 378 | id, |
| 379 | title.empty() ? wxString(_("Print")) : title, |
| 380 | pos, |
| 381 | size, |
| 382 | style |
| 383 | ) |
| 384 | { |
| 385 | } |
| 386 | |
| 387 | // --------------------------------------------------------------------------- |
| 388 | // wxPrintDialog: the common dialog for printing. |
| 389 | // --------------------------------------------------------------------------- |
| 390 | |
| 391 | IMPLEMENT_CLASS(wxPrintDialog, wxObject) |
| 392 | |
| 393 | wxPrintDialog::wxPrintDialog(wxWindow *parent, wxPrintDialogData* data) |
| 394 | { |
| 395 | m_pimpl = wxPrintFactory::GetFactory()->CreatePrintDialog( parent, data ); |
| 396 | } |
| 397 | |
| 398 | wxPrintDialog::wxPrintDialog(wxWindow *parent, wxPrintData* data) |
| 399 | { |
| 400 | m_pimpl = wxPrintFactory::GetFactory()->CreatePrintDialog( parent, data ); |
| 401 | } |
| 402 | |
| 403 | wxPrintDialog::~wxPrintDialog() |
| 404 | { |
| 405 | delete m_pimpl; |
| 406 | } |
| 407 | |
| 408 | int wxPrintDialog::ShowModal() |
| 409 | { |
| 410 | return m_pimpl->ShowModal(); |
| 411 | } |
| 412 | |
| 413 | wxPrintDialogData& wxPrintDialog::GetPrintDialogData() |
| 414 | { |
| 415 | return m_pimpl->GetPrintDialogData(); |
| 416 | } |
| 417 | |
| 418 | wxPrintData& wxPrintDialog::GetPrintData() |
| 419 | { |
| 420 | return m_pimpl->GetPrintData(); |
| 421 | } |
| 422 | wxDC *wxPrintDialog::GetPrintDC() |
| 423 | { |
| 424 | return m_pimpl->GetPrintDC(); |
| 425 | } |
| 426 | |
| 427 | //---------------------------------------------------------------------------- |
| 428 | // wxPrintAbortDialog |
| 429 | //---------------------------------------------------------------------------- |
| 430 | |
| 431 | BEGIN_EVENT_TABLE(wxPrintAbortDialog, wxDialog) |
| 432 | EVT_BUTTON(wxID_CANCEL, wxPrintAbortDialog::OnCancel) |
| 433 | END_EVENT_TABLE() |
| 434 | |
| 435 | void wxPrintAbortDialog::OnCancel(wxCommandEvent& WXUNUSED(event)) |
| 436 | { |
| 437 | wxPrinterBase::sm_abortIt = true; |
| 438 | wxPrinterBase::sm_abortWindow->Show(false); |
| 439 | wxPrinterBase::sm_abortWindow->Close(true); |
| 440 | wxPrinterBase::sm_abortWindow = (wxWindow *) NULL; |
| 441 | } |
| 442 | |
| 443 | //---------------------------------------------------------------------------- |
| 444 | // wxPrintout |
| 445 | //---------------------------------------------------------------------------- |
| 446 | |
| 447 | IMPLEMENT_ABSTRACT_CLASS(wxPrintout, wxObject) |
| 448 | |
| 449 | wxPrintout::wxPrintout(const wxString& title) |
| 450 | { |
| 451 | m_printoutTitle = title ; |
| 452 | m_printoutDC = (wxDC *) NULL; |
| 453 | m_pageWidthMM = 0; |
| 454 | m_pageHeightMM = 0; |
| 455 | m_pageWidthPixels = 0; |
| 456 | m_pageHeightPixels = 0; |
| 457 | m_PPIScreenX = 0; |
| 458 | m_PPIScreenY = 0; |
| 459 | m_PPIPrinterX = 0; |
| 460 | m_PPIPrinterY = 0; |
| 461 | m_isPreview = false; |
| 462 | } |
| 463 | |
| 464 | wxPrintout::~wxPrintout() |
| 465 | { |
| 466 | } |
| 467 | |
| 468 | bool wxPrintout::OnBeginDocument(int WXUNUSED(startPage), int WXUNUSED(endPage)) |
| 469 | { |
| 470 | return GetDC()->StartDoc(_("Printing ") + m_printoutTitle); |
| 471 | } |
| 472 | |
| 473 | void wxPrintout::OnEndDocument() |
| 474 | { |
| 475 | GetDC()->EndDoc(); |
| 476 | } |
| 477 | |
| 478 | void wxPrintout::OnBeginPrinting() |
| 479 | { |
| 480 | } |
| 481 | |
| 482 | void wxPrintout::OnEndPrinting() |
| 483 | { |
| 484 | } |
| 485 | |
| 486 | bool wxPrintout::HasPage(int page) |
| 487 | { |
| 488 | return (page == 1); |
| 489 | } |
| 490 | |
| 491 | void wxPrintout::GetPageInfo(int *minPage, int *maxPage, int *fromPage, int *toPage) |
| 492 | { |
| 493 | *minPage = 1; |
| 494 | *maxPage = 32000; |
| 495 | *fromPage = 1; |
| 496 | *toPage = 1; |
| 497 | } |
| 498 | |
| 499 | //---------------------------------------------------------------------------- |
| 500 | // wxPreviewCanvas |
| 501 | //---------------------------------------------------------------------------- |
| 502 | |
| 503 | IMPLEMENT_CLASS(wxPreviewCanvas, wxWindow) |
| 504 | |
| 505 | BEGIN_EVENT_TABLE(wxPreviewCanvas, wxScrolledWindow) |
| 506 | EVT_PAINT(wxPreviewCanvas::OnPaint) |
| 507 | EVT_CHAR(wxPreviewCanvas::OnChar) |
| 508 | EVT_SYS_COLOUR_CHANGED(wxPreviewCanvas::OnSysColourChanged) |
| 509 | END_EVENT_TABLE() |
| 510 | |
| 511 | // VZ: the current code doesn't refresh properly without |
| 512 | // wxFULL_REPAINT_ON_RESIZE, this must be fixed as otherwise we have |
| 513 | // really horrible flicker when resizing the preview frame, but without |
| 514 | // this style it simply doesn't work correctly at all... |
| 515 | wxPreviewCanvas::wxPreviewCanvas(wxPrintPreviewBase *preview, wxWindow *parent, |
| 516 | const wxPoint& pos, const wxSize& size, long style, const wxString& name): |
| 517 | wxScrolledWindow(parent, wxID_ANY, pos, size, style | wxFULL_REPAINT_ON_RESIZE, name) |
| 518 | { |
| 519 | m_printPreview = preview; |
| 520 | #ifdef __WXMAC__ |
| 521 | // The app workspace colour is always white, but we should have |
| 522 | // a contrast with the page. |
| 523 | wxSystemColour colourIndex = wxSYS_COLOUR_3DDKSHADOW; |
| 524 | #else |
| 525 | wxSystemColour colourIndex = wxSYS_COLOUR_APPWORKSPACE; |
| 526 | #endif |
| 527 | SetBackgroundColour(wxSystemSettings::GetColour(colourIndex)); |
| 528 | |
| 529 | SetScrollbars(10, 10, 100, 100); |
| 530 | } |
| 531 | |
| 532 | wxPreviewCanvas::~wxPreviewCanvas() |
| 533 | { |
| 534 | } |
| 535 | |
| 536 | void wxPreviewCanvas::OnPaint(wxPaintEvent& WXUNUSED(event)) |
| 537 | { |
| 538 | wxPaintDC dc(this); |
| 539 | PrepareDC( dc ); |
| 540 | |
| 541 | /* |
| 542 | #ifdef __WXGTK__ |
| 543 | if (!GetUpdateRegion().IsEmpty()) |
| 544 | dc.SetClippingRegion( GetUpdateRegion() ); |
| 545 | #endif |
| 546 | */ |
| 547 | |
| 548 | if (m_printPreview) |
| 549 | { |
| 550 | m_printPreview->PaintPage(this, dc); |
| 551 | } |
| 552 | } |
| 553 | |
| 554 | // Responds to colour changes, and passes event on to children. |
| 555 | void wxPreviewCanvas::OnSysColourChanged(wxSysColourChangedEvent& event) |
| 556 | { |
| 557 | #ifdef __WXMAC__ |
| 558 | // The app workspace colour is always white, but we should have |
| 559 | // a contrast with the page. |
| 560 | wxSystemColour colourIndex = wxSYS_COLOUR_3DDKSHADOW; |
| 561 | #else |
| 562 | wxSystemColour colourIndex = wxSYS_COLOUR_APPWORKSPACE; |
| 563 | #endif |
| 564 | SetBackgroundColour(wxSystemSettings::GetColour(colourIndex)); |
| 565 | Refresh(); |
| 566 | |
| 567 | // Propagate the event to the non-top-level children |
| 568 | wxWindow::OnSysColourChanged(event); |
| 569 | } |
| 570 | |
| 571 | void wxPreviewCanvas::OnChar(wxKeyEvent &event) |
| 572 | { |
| 573 | wxPreviewControlBar* controlBar = ((wxPreviewFrame*) GetParent())->GetControlBar(); |
| 574 | if (event.GetKeyCode() == WXK_ESCAPE) |
| 575 | { |
| 576 | ((wxPreviewFrame*) GetParent())->Close(true); |
| 577 | return; |
| 578 | } |
| 579 | else if (event.GetKeyCode() == WXK_TAB) |
| 580 | { |
| 581 | controlBar->OnGoto(); |
| 582 | return; |
| 583 | } |
| 584 | else if (event.GetKeyCode() == WXK_RETURN) |
| 585 | { |
| 586 | controlBar->OnPrint(); |
| 587 | return; |
| 588 | } |
| 589 | |
| 590 | if (!event.ControlDown()) |
| 591 | { |
| 592 | event.Skip(); |
| 593 | return; |
| 594 | } |
| 595 | |
| 596 | switch(event.GetKeyCode()) |
| 597 | { |
| 598 | case WXK_NEXT: |
| 599 | controlBar->OnNext(); break; |
| 600 | case WXK_PRIOR: |
| 601 | controlBar->OnPrevious(); break; |
| 602 | case WXK_HOME: |
| 603 | controlBar->OnFirst(); break; |
| 604 | case WXK_END: |
| 605 | controlBar->OnLast(); break; |
| 606 | default: |
| 607 | event.Skip(); |
| 608 | } |
| 609 | } |
| 610 | |
| 611 | //---------------------------------------------------------------------------- |
| 612 | // wxPreviewControlBar |
| 613 | //---------------------------------------------------------------------------- |
| 614 | |
| 615 | IMPLEMENT_CLASS(wxPreviewControlBar, wxWindow) |
| 616 | |
| 617 | BEGIN_EVENT_TABLE(wxPreviewControlBar, wxPanel) |
| 618 | EVT_BUTTON(wxID_PREVIEW_CLOSE, wxPreviewControlBar::OnWindowClose) |
| 619 | EVT_BUTTON(wxID_PREVIEW_PRINT, wxPreviewControlBar::OnPrintButton) |
| 620 | EVT_BUTTON(wxID_PREVIEW_PREVIOUS, wxPreviewControlBar::OnPreviousButton) |
| 621 | EVT_BUTTON(wxID_PREVIEW_NEXT, wxPreviewControlBar::OnNextButton) |
| 622 | EVT_BUTTON(wxID_PREVIEW_FIRST, wxPreviewControlBar::OnFirstButton) |
| 623 | EVT_BUTTON(wxID_PREVIEW_LAST, wxPreviewControlBar::OnLastButton) |
| 624 | EVT_BUTTON(wxID_PREVIEW_GOTO, wxPreviewControlBar::OnGotoButton) |
| 625 | EVT_CHOICE(wxID_PREVIEW_ZOOM, wxPreviewControlBar::OnZoom) |
| 626 | EVT_PAINT(wxPreviewControlBar::OnPaint) |
| 627 | END_EVENT_TABLE() |
| 628 | |
| 629 | wxPreviewControlBar::wxPreviewControlBar(wxPrintPreviewBase *preview, long buttons, |
| 630 | wxWindow *parent, const wxPoint& pos, const wxSize& size, |
| 631 | long style, const wxString& name): |
| 632 | wxPanel(parent, wxID_ANY, pos, size, style, name) |
| 633 | { |
| 634 | m_printPreview = preview; |
| 635 | m_closeButton = (wxButton *) NULL; |
| 636 | m_nextPageButton = (wxButton *) NULL; |
| 637 | m_previousPageButton = (wxButton *) NULL; |
| 638 | m_printButton = (wxButton *) NULL; |
| 639 | m_zoomControl = (wxChoice *) NULL; |
| 640 | m_buttonFlags = buttons; |
| 641 | } |
| 642 | |
| 643 | wxPreviewControlBar::~wxPreviewControlBar() |
| 644 | { |
| 645 | } |
| 646 | |
| 647 | void wxPreviewControlBar::OnPaint(wxPaintEvent& WXUNUSED(event)) |
| 648 | { |
| 649 | wxPaintDC dc(this); |
| 650 | |
| 651 | int w, h; |
| 652 | GetSize(&w, &h); |
| 653 | dc.SetPen(*wxBLACK_PEN); |
| 654 | dc.SetBrush(*wxTRANSPARENT_BRUSH); |
| 655 | dc.DrawLine( 0, h-1, w, h-1 ); |
| 656 | } |
| 657 | |
| 658 | void wxPreviewControlBar::OnWindowClose(wxCommandEvent& WXUNUSED(event)) |
| 659 | { |
| 660 | wxPreviewFrame *frame = (wxPreviewFrame *)GetParent(); |
| 661 | frame->Close(true); |
| 662 | } |
| 663 | |
| 664 | void wxPreviewControlBar::OnPrint(void) |
| 665 | { |
| 666 | wxPrintPreviewBase *preview = GetPrintPreview(); |
| 667 | preview->Print(true); |
| 668 | } |
| 669 | |
| 670 | void wxPreviewControlBar::OnNext(void) |
| 671 | { |
| 672 | wxPrintPreviewBase *preview = GetPrintPreview(); |
| 673 | if (preview) |
| 674 | { |
| 675 | int currentPage = preview->GetCurrentPage(); |
| 676 | if ((preview->GetMaxPage() > 0) && |
| 677 | (currentPage < preview->GetMaxPage()) && |
| 678 | preview->GetPrintout()->HasPage(currentPage + 1)) |
| 679 | { |
| 680 | preview->SetCurrentPage(currentPage + 1); |
| 681 | } |
| 682 | } |
| 683 | } |
| 684 | |
| 685 | void wxPreviewControlBar::OnPrevious(void) |
| 686 | { |
| 687 | wxPrintPreviewBase *preview = GetPrintPreview(); |
| 688 | if (preview) |
| 689 | { |
| 690 | int currentPage = preview->GetCurrentPage(); |
| 691 | if ((preview->GetMinPage() > 0) && |
| 692 | (currentPage > preview->GetMinPage()) && |
| 693 | preview->GetPrintout()->HasPage(currentPage - 1)) |
| 694 | { |
| 695 | preview->SetCurrentPage(currentPage - 1); |
| 696 | } |
| 697 | } |
| 698 | } |
| 699 | |
| 700 | void wxPreviewControlBar::OnFirst(void) |
| 701 | { |
| 702 | wxPrintPreviewBase *preview = GetPrintPreview(); |
| 703 | if (preview) |
| 704 | { |
| 705 | int currentPage = preview->GetMinPage(); |
| 706 | if (preview->GetPrintout()->HasPage(currentPage)) |
| 707 | { |
| 708 | preview->SetCurrentPage(currentPage); |
| 709 | } |
| 710 | } |
| 711 | } |
| 712 | |
| 713 | void wxPreviewControlBar::OnLast(void) |
| 714 | { |
| 715 | wxPrintPreviewBase *preview = GetPrintPreview(); |
| 716 | if (preview) |
| 717 | { |
| 718 | int currentPage = preview->GetMaxPage(); |
| 719 | if (preview->GetPrintout()->HasPage(currentPage)) |
| 720 | { |
| 721 | preview->SetCurrentPage(currentPage); |
| 722 | } |
| 723 | } |
| 724 | } |
| 725 | |
| 726 | void wxPreviewControlBar::OnGoto(void) |
| 727 | { |
| 728 | wxPrintPreviewBase *preview = GetPrintPreview(); |
| 729 | if (preview) |
| 730 | { |
| 731 | long currentPage; |
| 732 | |
| 733 | if (preview->GetMinPage() > 0) |
| 734 | { |
| 735 | wxString strPrompt; |
| 736 | wxString strPage; |
| 737 | |
| 738 | strPrompt.Printf( _("Enter a page number between %d and %d:"), |
| 739 | preview->GetMinPage(), preview->GetMaxPage()); |
| 740 | strPage.Printf( wxT("%d"), preview->GetCurrentPage() ); |
| 741 | |
| 742 | strPage = |
| 743 | wxGetTextFromUser( strPrompt, _("Goto Page"), strPage, GetParent()); |
| 744 | |
| 745 | if ( strPage.ToLong( ¤tPage ) ) |
| 746 | if (preview->GetPrintout()->HasPage(currentPage)) |
| 747 | { |
| 748 | preview->SetCurrentPage(currentPage); |
| 749 | } |
| 750 | } |
| 751 | } |
| 752 | } |
| 753 | |
| 754 | void wxPreviewControlBar::OnZoom(wxCommandEvent& WXUNUSED(event)) |
| 755 | { |
| 756 | int zoom = GetZoomControl(); |
| 757 | if (GetPrintPreview()) |
| 758 | GetPrintPreview()->SetZoom(zoom); |
| 759 | } |
| 760 | |
| 761 | void wxPreviewControlBar::CreateButtons() |
| 762 | { |
| 763 | SetSize(0, 0, 400, 40); |
| 764 | |
| 765 | wxBoxSizer *item0 = new wxBoxSizer( wxHORIZONTAL ); |
| 766 | |
| 767 | m_closeButton = new wxButton( this, wxID_PREVIEW_CLOSE, _("&Close"), wxDefaultPosition, wxDefaultSize, 0 ); |
| 768 | item0->Add( m_closeButton, 0, wxALIGN_CENTRE|wxALL, 5 ); |
| 769 | |
| 770 | if (m_buttonFlags & wxPREVIEW_PRINT) |
| 771 | { |
| 772 | m_printButton = new wxButton( this, wxID_PREVIEW_PRINT, _("&Print..."), wxDefaultPosition, wxDefaultSize, 0 ); |
| 773 | item0->Add( m_printButton, 0, wxALIGN_CENTRE|wxALL, 5 ); |
| 774 | } |
| 775 | |
| 776 | if (m_buttonFlags & wxPREVIEW_FIRST) |
| 777 | { |
| 778 | m_firstPageButton = new wxButton( this, wxID_PREVIEW_FIRST, _("|<<"), wxDefaultPosition, wxDefaultSize, wxBU_EXACTFIT ); |
| 779 | item0->Add( m_firstPageButton, 0, wxALIGN_CENTRE|wxALL, 5 ); |
| 780 | } |
| 781 | |
| 782 | if (m_buttonFlags & wxPREVIEW_PREVIOUS) |
| 783 | { |
| 784 | m_previousPageButton = new wxButton( this, wxID_PREVIEW_PREVIOUS, _("<<"), wxDefaultPosition, wxDefaultSize, wxBU_EXACTFIT ); |
| 785 | item0->Add( m_previousPageButton, 0, wxALIGN_CENTRE|wxRIGHT|wxTOP|wxBOTTOM, 5 ); |
| 786 | } |
| 787 | |
| 788 | if (m_buttonFlags & wxPREVIEW_NEXT) |
| 789 | { |
| 790 | m_nextPageButton = new wxButton( this, wxID_PREVIEW_NEXT, _(">>"), wxDefaultPosition, wxDefaultSize, wxBU_EXACTFIT ); |
| 791 | item0->Add( m_nextPageButton, 0, wxALIGN_CENTRE|wxRIGHT|wxTOP|wxBOTTOM, 5 ); |
| 792 | } |
| 793 | |
| 794 | if (m_buttonFlags & wxPREVIEW_LAST) |
| 795 | { |
| 796 | m_lastPageButton = new wxButton( this, wxID_PREVIEW_LAST, _(">>|"), wxDefaultPosition, wxDefaultSize, wxBU_EXACTFIT ); |
| 797 | item0->Add( m_lastPageButton, 0, wxALIGN_CENTRE|wxRIGHT|wxTOP|wxBOTTOM, 5 ); |
| 798 | } |
| 799 | |
| 800 | if (m_buttonFlags & wxPREVIEW_GOTO) |
| 801 | { |
| 802 | m_gotoPageButton = new wxButton( this, wxID_PREVIEW_GOTO, _("&Goto..."), wxDefaultPosition, wxDefaultSize, 0 ); |
| 803 | item0->Add( m_gotoPageButton, 0, wxALIGN_CENTRE|wxALL, 5 ); |
| 804 | } |
| 805 | |
| 806 | if (m_buttonFlags & wxPREVIEW_ZOOM) |
| 807 | { |
| 808 | wxString choices[] = |
| 809 | { |
| 810 | wxT("10%"), wxT("15%"), wxT("20%"), wxT("25%"), wxT("30%"), wxT("35%"), wxT("40%"), wxT("45%"), wxT("50%"), wxT("55%"), |
| 811 | wxT("60%"), wxT("65%"), wxT("70%"), wxT("75%"), wxT("80%"), wxT("85%"), wxT("90%"), wxT("95%"), wxT("100%"), wxT("110%"), |
| 812 | wxT("120%"), wxT("150%"), wxT("200%") |
| 813 | }; |
| 814 | int n = WXSIZEOF(choices); |
| 815 | |
| 816 | m_zoomControl = new wxChoice( this, wxID_PREVIEW_ZOOM, wxDefaultPosition, wxSize(70,wxDefaultCoord), n, choices, 0 ); |
| 817 | item0->Add( m_zoomControl, 0, wxALIGN_CENTRE|wxALL, 5 ); |
| 818 | SetZoomControl(m_printPreview->GetZoom()); |
| 819 | } |
| 820 | |
| 821 | SetSizer(item0); |
| 822 | item0->Fit(this); |
| 823 | } |
| 824 | |
| 825 | void wxPreviewControlBar::SetZoomControl(int zoom) |
| 826 | { |
| 827 | if (m_zoomControl) |
| 828 | { |
| 829 | int n, count = m_zoomControl->GetCount(); |
| 830 | long val; |
| 831 | for (n=0; n<count; n++) |
| 832 | { |
| 833 | if (m_zoomControl->GetString(n).BeforeFirst(wxT('%')).ToLong(&val) && |
| 834 | (val >= long(zoom))) |
| 835 | { |
| 836 | m_zoomControl->SetSelection(n); |
| 837 | return; |
| 838 | } |
| 839 | } |
| 840 | |
| 841 | m_zoomControl->SetSelection(count-1); |
| 842 | } |
| 843 | } |
| 844 | |
| 845 | int wxPreviewControlBar::GetZoomControl() |
| 846 | { |
| 847 | if (m_zoomControl && (m_zoomControl->GetStringSelection() != wxEmptyString)) |
| 848 | { |
| 849 | long val; |
| 850 | if (m_zoomControl->GetStringSelection().BeforeFirst(wxT('%')).ToLong(&val)) |
| 851 | return int(val); |
| 852 | } |
| 853 | |
| 854 | return 0; |
| 855 | } |
| 856 | |
| 857 | |
| 858 | /* |
| 859 | * Preview frame |
| 860 | */ |
| 861 | |
| 862 | IMPLEMENT_CLASS(wxPreviewFrame, wxFrame) |
| 863 | |
| 864 | BEGIN_EVENT_TABLE(wxPreviewFrame, wxFrame) |
| 865 | EVT_CLOSE(wxPreviewFrame::OnCloseWindow) |
| 866 | END_EVENT_TABLE() |
| 867 | |
| 868 | wxPreviewFrame::wxPreviewFrame(wxPrintPreviewBase *preview, wxWindow *parent, const wxString& title, |
| 869 | const wxPoint& pos, const wxSize& size, long style, const wxString& name): |
| 870 | wxFrame(parent, wxID_ANY, title, pos, size, style, name) |
| 871 | { |
| 872 | m_printPreview = preview; |
| 873 | m_controlBar = NULL; |
| 874 | m_previewCanvas = NULL; |
| 875 | m_windowDisabler = NULL; |
| 876 | |
| 877 | // Give the application icon |
| 878 | #ifdef __WXMSW__ |
| 879 | wxFrame* topFrame = wxDynamicCast(wxTheApp->GetTopWindow(), wxFrame); |
| 880 | if (topFrame) |
| 881 | SetIcon(topFrame->GetIcon()); |
| 882 | #endif |
| 883 | } |
| 884 | |
| 885 | wxPreviewFrame::~wxPreviewFrame() |
| 886 | { |
| 887 | } |
| 888 | |
| 889 | void wxPreviewFrame::OnCloseWindow(wxCloseEvent& WXUNUSED(event)) |
| 890 | { |
| 891 | if (m_windowDisabler) |
| 892 | delete m_windowDisabler; |
| 893 | |
| 894 | // Need to delete the printout and the print preview |
| 895 | wxPrintout *printout = m_printPreview->GetPrintout(); |
| 896 | if (printout) |
| 897 | { |
| 898 | delete printout; |
| 899 | m_printPreview->SetPrintout(NULL); |
| 900 | m_printPreview->SetCanvas(NULL); |
| 901 | m_printPreview->SetFrame(NULL); |
| 902 | } |
| 903 | delete m_printPreview; |
| 904 | |
| 905 | Destroy(); |
| 906 | } |
| 907 | |
| 908 | void wxPreviewFrame::Initialize() |
| 909 | { |
| 910 | #if wxUSE_STATUSBAR |
| 911 | CreateStatusBar(); |
| 912 | #endif |
| 913 | CreateCanvas(); |
| 914 | CreateControlBar(); |
| 915 | |
| 916 | m_printPreview->SetCanvas(m_previewCanvas); |
| 917 | m_printPreview->SetFrame(this); |
| 918 | |
| 919 | wxBoxSizer *item0 = new wxBoxSizer( wxVERTICAL ); |
| 920 | |
| 921 | item0->Add( m_controlBar, 0, wxGROW|wxALIGN_CENTER_VERTICAL, 5 ); |
| 922 | item0->Add( m_previewCanvas, 1, wxGROW|wxALIGN_CENTER_VERTICAL, 5 ); |
| 923 | |
| 924 | SetAutoLayout( true ); |
| 925 | SetSizer( item0 ); |
| 926 | |
| 927 | m_windowDisabler = new wxWindowDisabler(this); |
| 928 | |
| 929 | Layout(); |
| 930 | |
| 931 | m_printPreview->AdjustScrollbars(m_previewCanvas); |
| 932 | m_previewCanvas->SetFocus(); |
| 933 | m_controlBar->SetFocus(); |
| 934 | } |
| 935 | |
| 936 | void wxPreviewFrame::CreateCanvas() |
| 937 | { |
| 938 | m_previewCanvas = new wxPreviewCanvas(m_printPreview, this); |
| 939 | } |
| 940 | |
| 941 | void wxPreviewFrame::CreateControlBar() |
| 942 | { |
| 943 | long buttons = wxPREVIEW_DEFAULT; |
| 944 | if (m_printPreview->GetPrintoutForPrinting()) |
| 945 | buttons |= wxPREVIEW_PRINT; |
| 946 | |
| 947 | m_controlBar = new wxPreviewControlBar(m_printPreview, buttons, this, wxPoint(0, 0), wxSize(400, 40)); |
| 948 | m_controlBar->CreateButtons(); |
| 949 | } |
| 950 | |
| 951 | /* |
| 952 | * Print preview |
| 953 | */ |
| 954 | |
| 955 | wxPrintPreviewBase::wxPrintPreviewBase(wxPrintout *printout, |
| 956 | wxPrintout *printoutForPrinting, |
| 957 | wxPrintData *data) |
| 958 | { |
| 959 | if (data) |
| 960 | m_printDialogData = (*data); |
| 961 | |
| 962 | Init(printout, printoutForPrinting); |
| 963 | } |
| 964 | |
| 965 | wxPrintPreviewBase::wxPrintPreviewBase(wxPrintout *printout, |
| 966 | wxPrintout *printoutForPrinting, |
| 967 | wxPrintDialogData *data) |
| 968 | { |
| 969 | if (data) |
| 970 | m_printDialogData = (*data); |
| 971 | |
| 972 | Init(printout, printoutForPrinting); |
| 973 | } |
| 974 | |
| 975 | void wxPrintPreviewBase::Init(wxPrintout *printout, |
| 976 | wxPrintout *printoutForPrinting) |
| 977 | { |
| 978 | m_isOk = true; |
| 979 | m_previewPrintout = printout; |
| 980 | if (m_previewPrintout) |
| 981 | m_previewPrintout->SetIsPreview(true); |
| 982 | |
| 983 | m_printPrintout = printoutForPrinting; |
| 984 | |
| 985 | m_previewCanvas = NULL; |
| 986 | m_previewFrame = NULL; |
| 987 | m_previewBitmap = NULL; |
| 988 | m_currentPage = 1; |
| 989 | m_currentZoom = 70; |
| 990 | m_topMargin = 40; |
| 991 | m_leftMargin = 40; |
| 992 | m_pageWidth = 0; |
| 993 | m_pageHeight = 0; |
| 994 | m_printingPrepared = false; |
| 995 | m_minPage = 1; |
| 996 | m_maxPage = 1; |
| 997 | } |
| 998 | |
| 999 | wxPrintPreviewBase::~wxPrintPreviewBase() |
| 1000 | { |
| 1001 | if (m_previewPrintout) |
| 1002 | delete m_previewPrintout; |
| 1003 | if (m_previewBitmap) |
| 1004 | delete m_previewBitmap; |
| 1005 | if (m_printPrintout) |
| 1006 | delete m_printPrintout; |
| 1007 | } |
| 1008 | |
| 1009 | bool wxPrintPreviewBase::SetCurrentPage(int pageNum) |
| 1010 | { |
| 1011 | if (m_currentPage == pageNum) |
| 1012 | return true; |
| 1013 | |
| 1014 | m_currentPage = pageNum; |
| 1015 | if (m_previewBitmap) |
| 1016 | { |
| 1017 | delete m_previewBitmap; |
| 1018 | m_previewBitmap = NULL; |
| 1019 | } |
| 1020 | |
| 1021 | if (m_previewCanvas) |
| 1022 | { |
| 1023 | AdjustScrollbars(m_previewCanvas); |
| 1024 | |
| 1025 | if (!RenderPage(pageNum)) |
| 1026 | return false; |
| 1027 | m_previewCanvas->Refresh(); |
| 1028 | m_previewCanvas->SetFocus(); |
| 1029 | } |
| 1030 | return true; |
| 1031 | } |
| 1032 | |
| 1033 | int wxPrintPreviewBase::GetCurrentPage() const |
| 1034 | { return m_currentPage; }; |
| 1035 | void wxPrintPreviewBase::SetPrintout(wxPrintout *printout) |
| 1036 | { m_previewPrintout = printout; }; |
| 1037 | wxPrintout *wxPrintPreviewBase::GetPrintout() const |
| 1038 | { return m_previewPrintout; }; |
| 1039 | wxPrintout *wxPrintPreviewBase::GetPrintoutForPrinting() const |
| 1040 | { return m_printPrintout; }; |
| 1041 | void wxPrintPreviewBase::SetFrame(wxFrame *frame) |
| 1042 | { m_previewFrame = frame; }; |
| 1043 | void wxPrintPreviewBase::SetCanvas(wxPreviewCanvas *canvas) |
| 1044 | { m_previewCanvas = canvas; }; |
| 1045 | wxFrame *wxPrintPreviewBase::GetFrame() const |
| 1046 | { return m_previewFrame; } |
| 1047 | wxPreviewCanvas *wxPrintPreviewBase::GetCanvas() const |
| 1048 | { return m_previewCanvas; } |
| 1049 | |
| 1050 | bool wxPrintPreviewBase::PaintPage(wxPreviewCanvas *canvas, wxDC& dc) |
| 1051 | { |
| 1052 | DrawBlankPage(canvas, dc); |
| 1053 | |
| 1054 | if (!m_previewBitmap) |
| 1055 | if (!RenderPage(m_currentPage)) |
| 1056 | return false; |
| 1057 | |
| 1058 | if (!m_previewBitmap) |
| 1059 | return false; |
| 1060 | |
| 1061 | if (!canvas) |
| 1062 | return false; |
| 1063 | |
| 1064 | int canvasWidth, canvasHeight; |
| 1065 | canvas->GetSize(&canvasWidth, &canvasHeight); |
| 1066 | |
| 1067 | double zoomScale = ((float)m_currentZoom/(float)100); |
| 1068 | double actualWidth = (zoomScale*m_pageWidth*m_previewScale); |
| 1069 | // float actualHeight = (float)(zoomScale*m_pageHeight*m_previewScale); |
| 1070 | |
| 1071 | int x = (int) ((canvasWidth - actualWidth)/2.0); |
| 1072 | if (x < m_leftMargin) |
| 1073 | x = m_leftMargin; |
| 1074 | int y = m_topMargin; |
| 1075 | |
| 1076 | wxMemoryDC temp_dc; |
| 1077 | temp_dc.SelectObject(*m_previewBitmap); |
| 1078 | |
| 1079 | dc.Blit(x, y, m_previewBitmap->GetWidth(), m_previewBitmap->GetHeight(), &temp_dc, 0, 0); |
| 1080 | |
| 1081 | temp_dc.SelectObject(wxNullBitmap); |
| 1082 | |
| 1083 | return true; |
| 1084 | } |
| 1085 | |
| 1086 | // Adjusts the scrollbars for the current scale |
| 1087 | void wxPrintPreviewBase::AdjustScrollbars(wxPreviewCanvas *canvas) |
| 1088 | { |
| 1089 | if (!canvas) |
| 1090 | return ; |
| 1091 | |
| 1092 | int canvasWidth, canvasHeight; |
| 1093 | canvas->GetSize(&canvasWidth, &canvasHeight); |
| 1094 | |
| 1095 | double zoomScale = ((float)m_currentZoom/(float)100); |
| 1096 | double actualWidth = (zoomScale*m_pageWidth*m_previewScale); |
| 1097 | double actualHeight = (zoomScale*m_pageHeight*m_previewScale); |
| 1098 | |
| 1099 | // Set the scrollbars appropriately |
| 1100 | int totalWidth = (int)(actualWidth + 2*m_leftMargin); |
| 1101 | int totalHeight = (int)(actualHeight + 2*m_topMargin); |
| 1102 | int scrollUnitsX = totalWidth/10; |
| 1103 | int scrollUnitsY = totalHeight/10; |
| 1104 | wxSize virtualSize = canvas->GetVirtualSize(); |
| 1105 | if (virtualSize.GetWidth() != totalWidth || virtualSize.GetHeight() != totalHeight) |
| 1106 | canvas->SetScrollbars(10, 10, scrollUnitsX, scrollUnitsY, 0, 0, true); |
| 1107 | } |
| 1108 | |
| 1109 | bool wxPrintPreviewBase::RenderPage(int pageNum) |
| 1110 | { |
| 1111 | wxBusyCursor busy; |
| 1112 | |
| 1113 | int canvasWidth, canvasHeight; |
| 1114 | |
| 1115 | if (!m_previewCanvas) |
| 1116 | { |
| 1117 | wxFAIL_MSG(_T("wxPrintPreviewBase::RenderPage: must use wxPrintPreviewBase::SetCanvas to let me know about the canvas!")); |
| 1118 | |
| 1119 | return false; |
| 1120 | } |
| 1121 | m_previewCanvas->GetSize(&canvasWidth, &canvasHeight); |
| 1122 | |
| 1123 | double zoomScale = (m_currentZoom/100.0); |
| 1124 | int actualWidth = (int)(zoomScale*m_pageWidth*m_previewScale); |
| 1125 | int actualHeight = (int)(zoomScale*m_pageHeight*m_previewScale); |
| 1126 | |
| 1127 | if (!m_previewBitmap) |
| 1128 | { |
| 1129 | m_previewBitmap = new wxBitmap((int)actualWidth, (int)actualHeight); |
| 1130 | if (!m_previewBitmap || !m_previewBitmap->Ok()) |
| 1131 | { |
| 1132 | if (m_previewBitmap) { |
| 1133 | delete m_previewBitmap; |
| 1134 | m_previewBitmap = NULL; |
| 1135 | } |
| 1136 | wxMessageBox(_("Sorry, not enough memory to create a preview."), _("Print Preview Failure"), wxOK); |
| 1137 | return false; |
| 1138 | } |
| 1139 | } |
| 1140 | |
| 1141 | wxMemoryDC memoryDC; |
| 1142 | memoryDC.SelectObject(*m_previewBitmap); |
| 1143 | |
| 1144 | memoryDC.Clear(); |
| 1145 | |
| 1146 | m_previewPrintout->SetDC(&memoryDC); |
| 1147 | m_previewPrintout->SetPageSizePixels(m_pageWidth, m_pageHeight); |
| 1148 | |
| 1149 | // Need to delay OnPreparePrinting until here, so we have enough information. |
| 1150 | if (!m_printingPrepared) |
| 1151 | { |
| 1152 | m_previewPrintout->OnPreparePrinting(); |
| 1153 | int selFrom, selTo; |
| 1154 | m_previewPrintout->GetPageInfo(&m_minPage, &m_maxPage, &selFrom, &selTo); |
| 1155 | m_printingPrepared = true; |
| 1156 | } |
| 1157 | |
| 1158 | m_previewPrintout->OnBeginPrinting(); |
| 1159 | |
| 1160 | if (!m_previewPrintout->OnBeginDocument(m_printDialogData.GetFromPage(), m_printDialogData.GetToPage())) |
| 1161 | { |
| 1162 | wxMessageBox(_("Could not start document preview."), _("Print Preview Failure"), wxOK); |
| 1163 | |
| 1164 | memoryDC.SelectObject(wxNullBitmap); |
| 1165 | |
| 1166 | delete m_previewBitmap; |
| 1167 | m_previewBitmap = NULL; |
| 1168 | return false; |
| 1169 | } |
| 1170 | |
| 1171 | m_previewPrintout->OnPrintPage(pageNum); |
| 1172 | m_previewPrintout->OnEndDocument(); |
| 1173 | m_previewPrintout->OnEndPrinting(); |
| 1174 | |
| 1175 | m_previewPrintout->SetDC(NULL); |
| 1176 | |
| 1177 | memoryDC.SelectObject(wxNullBitmap); |
| 1178 | |
| 1179 | #if wxUSE_STATUSBAR |
| 1180 | wxString status; |
| 1181 | if (m_maxPage != 0) |
| 1182 | status = wxString::Format(_("Page %d of %d"), pageNum, m_maxPage); |
| 1183 | else |
| 1184 | status = wxString::Format(_("Page %d"), pageNum); |
| 1185 | |
| 1186 | if (m_previewFrame) |
| 1187 | m_previewFrame->SetStatusText(status); |
| 1188 | #endif |
| 1189 | |
| 1190 | return true; |
| 1191 | } |
| 1192 | |
| 1193 | |
| 1194 | bool wxPrintPreviewBase::DrawBlankPage(wxPreviewCanvas *canvas, wxDC& dc) |
| 1195 | { |
| 1196 | int canvasWidth, canvasHeight; |
| 1197 | canvas->GetSize(&canvasWidth, &canvasHeight); |
| 1198 | |
| 1199 | float zoomScale = (float)((float)m_currentZoom/(float)100); |
| 1200 | float actualWidth = zoomScale*m_pageWidth*m_previewScale; |
| 1201 | float actualHeight = zoomScale*m_pageHeight*m_previewScale; |
| 1202 | |
| 1203 | float x = (float)((canvasWidth - actualWidth)/2.0); |
| 1204 | if (x < m_leftMargin) |
| 1205 | x = (float)m_leftMargin; |
| 1206 | float y = (float)m_topMargin; |
| 1207 | |
| 1208 | // Draw shadow, allowing for 1-pixel border AROUND the actual page |
| 1209 | int shadowOffset = 4; |
| 1210 | dc.SetPen(*wxBLACK_PEN); |
| 1211 | dc.SetBrush(*wxBLACK_BRUSH); |
| 1212 | /* |
| 1213 | dc.DrawRectangle((int)(x-1 + shadowOffset), (int)(y-1 + shadowOffset), (int)(actualWidth+2), (int)(actualHeight+2)); |
| 1214 | */ |
| 1215 | dc.DrawRectangle((int)(x + shadowOffset), (int)(y + actualHeight+1), (int)(actualWidth), shadowOffset); |
| 1216 | dc.DrawRectangle((int)(x + actualWidth), (int)(y + shadowOffset), shadowOffset, (int)(actualHeight)); |
| 1217 | |
| 1218 | // Draw blank page allowing for 1-pixel border AROUND the actual page |
| 1219 | dc.SetPen(*wxBLACK_PEN); |
| 1220 | dc.SetBrush(*wxWHITE_BRUSH); |
| 1221 | |
| 1222 | /* |
| 1223 | wxRegion update_region = canvas->GetUpdateRegion(); |
| 1224 | wxRect r = update_region.GetBox(); |
| 1225 | |
| 1226 | printf( "x: %d y: %d w: %d h: %d.\n", (int)r.x, (int)r.y, (int)r.width, (int)r.height ); |
| 1227 | */ |
| 1228 | |
| 1229 | dc.DrawRectangle((int)(x-2), (int)(y-1), (int)(actualWidth+3), (int)(actualHeight+2)); |
| 1230 | |
| 1231 | return true; |
| 1232 | } |
| 1233 | |
| 1234 | void wxPrintPreviewBase::SetZoom(int percent) |
| 1235 | { |
| 1236 | if (m_currentZoom == percent) |
| 1237 | return; |
| 1238 | |
| 1239 | m_currentZoom = percent; |
| 1240 | if (m_previewBitmap) |
| 1241 | { |
| 1242 | delete m_previewBitmap; |
| 1243 | m_previewBitmap = NULL; |
| 1244 | } |
| 1245 | |
| 1246 | if (m_previewCanvas) |
| 1247 | { |
| 1248 | AdjustScrollbars(m_previewCanvas); |
| 1249 | RenderPage(m_currentPage); |
| 1250 | ((wxScrolledWindow *) m_previewCanvas)->Scroll(0, 0); |
| 1251 | m_previewCanvas->ClearBackground(); |
| 1252 | m_previewCanvas->Refresh(); |
| 1253 | m_previewCanvas->SetFocus(); |
| 1254 | } |
| 1255 | } |
| 1256 | |
| 1257 | wxPrintDialogData& wxPrintPreviewBase::GetPrintDialogData() |
| 1258 | { |
| 1259 | return m_printDialogData; |
| 1260 | } |
| 1261 | |
| 1262 | int wxPrintPreviewBase::GetZoom() const |
| 1263 | { return m_currentZoom; } |
| 1264 | int wxPrintPreviewBase::GetMaxPage() const |
| 1265 | { return m_maxPage; } |
| 1266 | int wxPrintPreviewBase::GetMinPage() const |
| 1267 | { return m_minPage; } |
| 1268 | bool wxPrintPreviewBase::Ok() const |
| 1269 | { return m_isOk; } |
| 1270 | void wxPrintPreviewBase::SetOk(bool ok) |
| 1271 | { m_isOk = ok; } |
| 1272 | //---------------------------------------------------------------------------- |
| 1273 | // wxPrintPreview |
| 1274 | //---------------------------------------------------------------------------- |
| 1275 | |
| 1276 | IMPLEMENT_CLASS(wxPrintPreview, wxPrintPreviewBase) |
| 1277 | |
| 1278 | wxPrintPreview::wxPrintPreview(wxPrintout *printout, |
| 1279 | wxPrintout *printoutForPrinting, |
| 1280 | wxPrintDialogData *data) : |
| 1281 | wxPrintPreviewBase( printout, printoutForPrinting, data ) |
| 1282 | { |
| 1283 | m_pimpl = wxPrintFactory::GetFactory()-> |
| 1284 | CreatePrintPreview( printout, printoutForPrinting, data ); |
| 1285 | } |
| 1286 | |
| 1287 | wxPrintPreview::wxPrintPreview(wxPrintout *printout, |
| 1288 | wxPrintout *printoutForPrinting, |
| 1289 | wxPrintData *data ) : |
| 1290 | wxPrintPreviewBase( printout, printoutForPrinting, data ) |
| 1291 | { |
| 1292 | m_pimpl = wxPrintFactory::GetFactory()-> |
| 1293 | CreatePrintPreview( printout, printoutForPrinting, data ); |
| 1294 | } |
| 1295 | |
| 1296 | wxPrintPreview::~wxPrintPreview() |
| 1297 | { |
| 1298 | delete m_pimpl; |
| 1299 | |
| 1300 | // don't delete twice |
| 1301 | m_printPrintout = NULL; |
| 1302 | m_previewPrintout = NULL; |
| 1303 | m_previewBitmap = NULL; |
| 1304 | } |
| 1305 | |
| 1306 | bool wxPrintPreview::SetCurrentPage(int pageNum) |
| 1307 | { |
| 1308 | return m_pimpl->SetCurrentPage( pageNum ); |
| 1309 | } |
| 1310 | |
| 1311 | int wxPrintPreview::GetCurrentPage() const |
| 1312 | { |
| 1313 | return m_pimpl->GetCurrentPage(); |
| 1314 | } |
| 1315 | |
| 1316 | void wxPrintPreview::SetPrintout(wxPrintout *printout) |
| 1317 | { |
| 1318 | m_pimpl->SetPrintout( printout ); |
| 1319 | } |
| 1320 | |
| 1321 | wxPrintout *wxPrintPreview::GetPrintout() const |
| 1322 | { |
| 1323 | return m_pimpl->GetPrintout(); |
| 1324 | } |
| 1325 | |
| 1326 | wxPrintout *wxPrintPreview::GetPrintoutForPrinting() const |
| 1327 | { |
| 1328 | return m_pimpl->GetPrintoutForPrinting(); |
| 1329 | } |
| 1330 | |
| 1331 | void wxPrintPreview::SetFrame(wxFrame *frame) |
| 1332 | { |
| 1333 | m_pimpl->SetFrame( frame ); |
| 1334 | } |
| 1335 | |
| 1336 | void wxPrintPreview::SetCanvas(wxPreviewCanvas *canvas) |
| 1337 | { |
| 1338 | m_pimpl->SetCanvas( canvas ); |
| 1339 | } |
| 1340 | |
| 1341 | wxFrame *wxPrintPreview::GetFrame() const |
| 1342 | { |
| 1343 | return m_pimpl->GetFrame(); |
| 1344 | } |
| 1345 | |
| 1346 | wxPreviewCanvas *wxPrintPreview::GetCanvas() const |
| 1347 | { |
| 1348 | return m_pimpl->GetCanvas(); |
| 1349 | } |
| 1350 | |
| 1351 | bool wxPrintPreview::PaintPage(wxPreviewCanvas *canvas, wxDC& dc) |
| 1352 | { |
| 1353 | return m_pimpl->PaintPage( canvas, dc ); |
| 1354 | } |
| 1355 | |
| 1356 | bool wxPrintPreview::DrawBlankPage(wxPreviewCanvas *canvas, wxDC& dc) |
| 1357 | { |
| 1358 | return m_pimpl->DrawBlankPage( canvas, dc ); |
| 1359 | } |
| 1360 | |
| 1361 | void wxPrintPreview::AdjustScrollbars(wxPreviewCanvas *canvas) |
| 1362 | { |
| 1363 | m_pimpl->AdjustScrollbars( canvas ); |
| 1364 | } |
| 1365 | |
| 1366 | bool wxPrintPreview::RenderPage(int pageNum) |
| 1367 | { |
| 1368 | return m_pimpl->RenderPage( pageNum ); |
| 1369 | } |
| 1370 | |
| 1371 | void wxPrintPreview::SetZoom(int percent) |
| 1372 | { |
| 1373 | m_pimpl->SetZoom( percent ); |
| 1374 | } |
| 1375 | |
| 1376 | wxPrintDialogData& wxPrintPreview::GetPrintDialogData() |
| 1377 | { |
| 1378 | return m_pimpl->GetPrintDialogData(); |
| 1379 | } |
| 1380 | |
| 1381 | int wxPrintPreview::GetMaxPage() const |
| 1382 | { |
| 1383 | return m_pimpl->GetMaxPage(); |
| 1384 | } |
| 1385 | |
| 1386 | int wxPrintPreview::GetMinPage() const |
| 1387 | { |
| 1388 | return m_pimpl->GetMinPage(); |
| 1389 | } |
| 1390 | |
| 1391 | bool wxPrintPreview::Ok() const |
| 1392 | { |
| 1393 | return m_pimpl->Ok(); |
| 1394 | } |
| 1395 | |
| 1396 | void wxPrintPreview::SetOk(bool ok) |
| 1397 | { |
| 1398 | m_pimpl->SetOk( ok ); |
| 1399 | } |
| 1400 | |
| 1401 | bool wxPrintPreview::Print(bool interactive) |
| 1402 | { |
| 1403 | return m_pimpl->Print( interactive ); |
| 1404 | } |
| 1405 | |
| 1406 | void wxPrintPreview::DetermineScaling() |
| 1407 | { |
| 1408 | m_pimpl->DetermineScaling(); |
| 1409 | } |
| 1410 | |
| 1411 | |
| 1412 | #endif // wxUSE_PRINTING_ARCHITECTURE |