| 1 | ///////////////////////////////////////////////////////////////////////////// |
| 2 | // Name: src/html/htmprint.cpp |
| 3 | // Purpose: html printing classes |
| 4 | // Author: Vaclav Slavik |
| 5 | // Created: 25/09/99 |
| 6 | // RCS-ID: $Id$ |
| 7 | // Copyright: (c) Vaclav Slavik, 1999 |
| 8 | // Licence: wxWindows licence |
| 9 | ///////////////////////////////////////////////////////////////////////////// |
| 10 | |
| 11 | // For compilers that support precompilation, includes "wx/wx.h". |
| 12 | #include "wx/wxprec.h" |
| 13 | |
| 14 | #ifdef __BORLANDC__ |
| 15 | #pragma hdrstop |
| 16 | #endif |
| 17 | |
| 18 | #if wxUSE_HTML && wxUSE_PRINTING_ARCHITECTURE && wxUSE_STREAMS |
| 19 | |
| 20 | #ifndef WX_PRECOMP |
| 21 | #include "wx/log.h" |
| 22 | #include "wx/intl.h" |
| 23 | #include "wx/dc.h" |
| 24 | #include "wx/settings.h" |
| 25 | #include "wx/msgdlg.h" |
| 26 | #include "wx/module.h" |
| 27 | #endif |
| 28 | |
| 29 | #include "wx/print.h" |
| 30 | #include "wx/printdlg.h" |
| 31 | #include "wx/html/htmprint.h" |
| 32 | #include "wx/wxhtml.h" |
| 33 | #include "wx/wfstream.h" |
| 34 | |
| 35 | |
| 36 | //-------------------------------------------------------------------------------- |
| 37 | // wxHtmlDCRenderer |
| 38 | //-------------------------------------------------------------------------------- |
| 39 | |
| 40 | |
| 41 | wxHtmlDCRenderer::wxHtmlDCRenderer() : wxObject() |
| 42 | { |
| 43 | m_DC = NULL; |
| 44 | m_Width = m_Height = 0; |
| 45 | m_Cells = NULL; |
| 46 | m_Parser = new wxHtmlWinParser(); |
| 47 | m_FS = new wxFileSystem(); |
| 48 | m_Parser->SetFS(m_FS); |
| 49 | } |
| 50 | |
| 51 | |
| 52 | |
| 53 | wxHtmlDCRenderer::~wxHtmlDCRenderer() |
| 54 | { |
| 55 | if (m_Cells) delete m_Cells; |
| 56 | if (m_Parser) delete m_Parser; |
| 57 | if (m_FS) delete m_FS; |
| 58 | } |
| 59 | |
| 60 | |
| 61 | |
| 62 | void wxHtmlDCRenderer::SetDC(wxDC *dc, double pixel_scale) |
| 63 | { |
| 64 | m_DC = dc; |
| 65 | m_Parser->SetDC(m_DC, pixel_scale); |
| 66 | } |
| 67 | |
| 68 | |
| 69 | |
| 70 | void wxHtmlDCRenderer::SetSize(int width, int height) |
| 71 | { |
| 72 | m_Width = width; |
| 73 | m_Height = height; |
| 74 | } |
| 75 | |
| 76 | |
| 77 | void wxHtmlDCRenderer::SetHtmlText(const wxString& html, const wxString& basepath, bool isdir) |
| 78 | { |
| 79 | if (m_DC == NULL) return; |
| 80 | |
| 81 | if (m_Cells != NULL) delete m_Cells; |
| 82 | |
| 83 | m_FS->ChangePathTo(basepath, isdir); |
| 84 | m_Cells = (wxHtmlContainerCell*) m_Parser->Parse(html); |
| 85 | m_Cells->SetIndent(0, wxHTML_INDENT_ALL, wxHTML_UNITS_PIXELS); |
| 86 | m_Cells->Layout(m_Width); |
| 87 | } |
| 88 | |
| 89 | |
| 90 | void wxHtmlDCRenderer::SetFonts(const wxString& normal_face, const wxString& fixed_face, |
| 91 | const int *sizes) |
| 92 | { |
| 93 | m_Parser->SetFonts(normal_face, fixed_face, sizes); |
| 94 | if (m_DC == NULL && m_Cells != NULL) |
| 95 | m_Cells->Layout(m_Width); |
| 96 | } |
| 97 | |
| 98 | void wxHtmlDCRenderer::SetStandardFonts(int size, |
| 99 | const wxString& normal_face, |
| 100 | const wxString& fixed_face) |
| 101 | { |
| 102 | m_Parser->SetStandardFonts(size, normal_face, fixed_face); |
| 103 | if (m_DC == NULL && m_Cells != NULL) |
| 104 | m_Cells->Layout(m_Width); |
| 105 | } |
| 106 | |
| 107 | int wxHtmlDCRenderer::Render(int x, int y, |
| 108 | wxArrayInt& known_pagebreaks, |
| 109 | int from, int dont_render, int to) |
| 110 | { |
| 111 | int pbreak, hght; |
| 112 | |
| 113 | if (m_Cells == NULL || m_DC == NULL) return 0; |
| 114 | |
| 115 | pbreak = (int)(from + m_Height); |
| 116 | while (m_Cells->AdjustPagebreak(&pbreak, known_pagebreaks)) {} |
| 117 | hght = pbreak - from; |
| 118 | if(to < hght) |
| 119 | hght = to; |
| 120 | |
| 121 | if (!dont_render) |
| 122 | { |
| 123 | wxHtmlRenderingInfo rinfo; |
| 124 | wxDefaultHtmlRenderingStyle rstyle; |
| 125 | rinfo.SetStyle(&rstyle); |
| 126 | m_DC->SetBrush(*wxWHITE_BRUSH); |
| 127 | m_DC->SetClippingRegion(x, y, m_Width, hght); |
| 128 | m_Cells->Draw(*m_DC, |
| 129 | x, (y - from), |
| 130 | y, pbreak + (y /*- from*/), |
| 131 | rinfo); |
| 132 | m_DC->DestroyClippingRegion(); |
| 133 | } |
| 134 | |
| 135 | if (pbreak < m_Cells->GetHeight()) return pbreak; |
| 136 | else return GetTotalHeight(); |
| 137 | } |
| 138 | |
| 139 | |
| 140 | int wxHtmlDCRenderer::GetTotalHeight() |
| 141 | { |
| 142 | if (m_Cells) return m_Cells->GetHeight(); |
| 143 | else return 0; |
| 144 | } |
| 145 | |
| 146 | |
| 147 | //-------------------------------------------------------------------------------- |
| 148 | // wxHtmlPrintout |
| 149 | //-------------------------------------------------------------------------------- |
| 150 | |
| 151 | |
| 152 | wxList wxHtmlPrintout::m_Filters; |
| 153 | |
| 154 | wxHtmlPrintout::wxHtmlPrintout(const wxString& title) : wxPrintout(title) |
| 155 | { |
| 156 | m_Renderer = new wxHtmlDCRenderer; |
| 157 | m_RendererHdr = new wxHtmlDCRenderer; |
| 158 | m_NumPages = wxHTML_PRINT_MAX_PAGES; |
| 159 | m_Document = m_BasePath = wxEmptyString; m_BasePathIsDir = true; |
| 160 | m_Headers[0] = m_Headers[1] = wxEmptyString; |
| 161 | m_Footers[0] = m_Footers[1] = wxEmptyString; |
| 162 | m_HeaderHeight = m_FooterHeight = 0; |
| 163 | SetMargins(); // to default values |
| 164 | } |
| 165 | |
| 166 | |
| 167 | |
| 168 | wxHtmlPrintout::~wxHtmlPrintout() |
| 169 | { |
| 170 | delete m_Renderer; |
| 171 | delete m_RendererHdr; |
| 172 | } |
| 173 | |
| 174 | void wxHtmlPrintout::CleanUpStatics() |
| 175 | { |
| 176 | WX_CLEAR_LIST(wxList, m_Filters); |
| 177 | } |
| 178 | |
| 179 | // Adds input filter |
| 180 | void wxHtmlPrintout::AddFilter(wxHtmlFilter *filter) |
| 181 | { |
| 182 | m_Filters.Append(filter); |
| 183 | } |
| 184 | |
| 185 | void wxHtmlPrintout::OnPreparePrinting() |
| 186 | { |
| 187 | int pageWidth, pageHeight, mm_w, mm_h, scr_w, scr_h, dc_w, dc_h; |
| 188 | float ppmm_h, ppmm_v; |
| 189 | |
| 190 | GetPageSizePixels(&pageWidth, &pageHeight); |
| 191 | GetPageSizeMM(&mm_w, &mm_h); |
| 192 | ppmm_h = (float)pageWidth / mm_w; |
| 193 | ppmm_v = (float)pageHeight / mm_h; |
| 194 | |
| 195 | int ppiPrinterX, ppiPrinterY; |
| 196 | GetPPIPrinter(&ppiPrinterX, &ppiPrinterY); |
| 197 | int ppiScreenX, ppiScreenY; |
| 198 | GetPPIScreen(&ppiScreenX, &ppiScreenY); |
| 199 | |
| 200 | wxDisplaySize(&scr_w, &scr_h); |
| 201 | GetDC()->GetSize(&dc_w, &dc_h); |
| 202 | |
| 203 | GetDC()->SetUserScale((double)dc_w / (double)pageWidth, (double)dc_w / (double)pageWidth); |
| 204 | |
| 205 | /* prepare headers/footers renderer: */ |
| 206 | |
| 207 | m_RendererHdr->SetDC(GetDC(), (double)ppiPrinterY / (double)ppiScreenY); |
| 208 | m_RendererHdr->SetSize((int) (ppmm_h * (mm_w - m_MarginLeft - m_MarginRight)), |
| 209 | (int) (ppmm_v * (mm_h - m_MarginTop - m_MarginBottom))); |
| 210 | if (m_Headers[0] != wxEmptyString) |
| 211 | { |
| 212 | m_RendererHdr->SetHtmlText(TranslateHeader(m_Headers[0], 1)); |
| 213 | m_HeaderHeight = m_RendererHdr->GetTotalHeight(); |
| 214 | } |
| 215 | else if (m_Headers[1] != wxEmptyString) |
| 216 | { |
| 217 | m_RendererHdr->SetHtmlText(TranslateHeader(m_Headers[1], 1)); |
| 218 | m_HeaderHeight = m_RendererHdr->GetTotalHeight(); |
| 219 | } |
| 220 | if (m_Footers[0] != wxEmptyString) |
| 221 | { |
| 222 | m_RendererHdr->SetHtmlText(TranslateHeader(m_Footers[0], 1)); |
| 223 | m_FooterHeight = m_RendererHdr->GetTotalHeight(); |
| 224 | } |
| 225 | else if (m_Footers[1] != wxEmptyString) |
| 226 | { |
| 227 | m_RendererHdr->SetHtmlText(TranslateHeader(m_Footers[1], 1)); |
| 228 | m_FooterHeight = m_RendererHdr->GetTotalHeight(); |
| 229 | } |
| 230 | |
| 231 | /* prepare main renderer: */ |
| 232 | m_Renderer->SetDC(GetDC(), (double)ppiPrinterY / (double)ppiScreenY); |
| 233 | m_Renderer->SetSize((int) (ppmm_h * (mm_w - m_MarginLeft - m_MarginRight)), |
| 234 | (int) (ppmm_v * (mm_h - m_MarginTop - m_MarginBottom) - |
| 235 | m_FooterHeight - m_HeaderHeight - |
| 236 | ((m_HeaderHeight == 0) ? 0 : m_MarginSpace * ppmm_v) - |
| 237 | ((m_FooterHeight == 0) ? 0 : m_MarginSpace * ppmm_v) |
| 238 | )); |
| 239 | m_Renderer->SetHtmlText(m_Document, m_BasePath, m_BasePathIsDir); |
| 240 | CountPages(); |
| 241 | } |
| 242 | |
| 243 | bool wxHtmlPrintout::OnBeginDocument(int startPage, int endPage) |
| 244 | { |
| 245 | if (!wxPrintout::OnBeginDocument(startPage, endPage)) return false; |
| 246 | |
| 247 | return true; |
| 248 | } |
| 249 | |
| 250 | |
| 251 | bool wxHtmlPrintout::OnPrintPage(int page) |
| 252 | { |
| 253 | wxDC *dc = GetDC(); |
| 254 | if (dc && dc->IsOk()) |
| 255 | { |
| 256 | if (HasPage(page)) |
| 257 | RenderPage(dc, page); |
| 258 | return true; |
| 259 | } |
| 260 | else return false; |
| 261 | } |
| 262 | |
| 263 | |
| 264 | void wxHtmlPrintout::GetPageInfo(int *minPage, int *maxPage, int *selPageFrom, int *selPageTo) |
| 265 | { |
| 266 | *minPage = 1; |
| 267 | if ( m_NumPages >= (signed)m_PageBreaks.GetCount()-1) |
| 268 | *maxPage = m_NumPages; |
| 269 | else |
| 270 | *maxPage = (signed)m_PageBreaks.GetCount()-1; |
| 271 | *selPageFrom = 1; |
| 272 | *selPageTo = (signed)m_PageBreaks.GetCount()-1; |
| 273 | } |
| 274 | |
| 275 | |
| 276 | |
| 277 | bool wxHtmlPrintout::HasPage(int pageNum) |
| 278 | { |
| 279 | return pageNum > 0 && (unsigned)pageNum < m_PageBreaks.GetCount(); |
| 280 | } |
| 281 | |
| 282 | |
| 283 | |
| 284 | void wxHtmlPrintout::SetHtmlText(const wxString& html, const wxString &basepath, bool isdir) |
| 285 | { |
| 286 | m_Document = html; |
| 287 | m_BasePath = basepath; |
| 288 | m_BasePathIsDir = isdir; |
| 289 | } |
| 290 | |
| 291 | void wxHtmlPrintout::SetHtmlFile(const wxString& htmlfile) |
| 292 | { |
| 293 | wxFileSystem fs; |
| 294 | wxFSFile *ff; |
| 295 | |
| 296 | if (wxFileExists(htmlfile)) |
| 297 | ff = fs.OpenFile(wxFileSystem::FileNameToURL(htmlfile)); |
| 298 | else |
| 299 | ff = fs.OpenFile(htmlfile); |
| 300 | |
| 301 | if (ff == NULL) |
| 302 | { |
| 303 | wxLogError(htmlfile + _(": file does not exist!")); |
| 304 | return; |
| 305 | } |
| 306 | |
| 307 | bool done = false; |
| 308 | wxHtmlFilterHTML defaultFilter; |
| 309 | wxString doc; |
| 310 | |
| 311 | wxList::compatibility_iterator node = m_Filters.GetFirst(); |
| 312 | while (node) |
| 313 | { |
| 314 | wxHtmlFilter *h = (wxHtmlFilter*) node->GetData(); |
| 315 | if (h->CanRead(*ff)) |
| 316 | { |
| 317 | doc = h->ReadFile(*ff); |
| 318 | done = true; |
| 319 | break; |
| 320 | } |
| 321 | node = node->GetNext(); |
| 322 | } |
| 323 | |
| 324 | if (!done) |
| 325 | doc = defaultFilter.ReadFile(*ff); |
| 326 | |
| 327 | SetHtmlText(doc, htmlfile, false); |
| 328 | delete ff; |
| 329 | } |
| 330 | |
| 331 | |
| 332 | |
| 333 | void wxHtmlPrintout::SetHeader(const wxString& header, int pg) |
| 334 | { |
| 335 | if (pg == wxPAGE_ALL || pg == wxPAGE_EVEN) |
| 336 | m_Headers[0] = header; |
| 337 | if (pg == wxPAGE_ALL || pg == wxPAGE_ODD) |
| 338 | m_Headers[1] = header; |
| 339 | } |
| 340 | |
| 341 | |
| 342 | |
| 343 | void wxHtmlPrintout::SetFooter(const wxString& footer, int pg) |
| 344 | { |
| 345 | if (pg == wxPAGE_ALL || pg == wxPAGE_EVEN) |
| 346 | m_Footers[0] = footer; |
| 347 | if (pg == wxPAGE_ALL || pg == wxPAGE_ODD) |
| 348 | m_Footers[1] = footer; |
| 349 | } |
| 350 | |
| 351 | |
| 352 | |
| 353 | void wxHtmlPrintout::CountPages() |
| 354 | { |
| 355 | wxBusyCursor wait; |
| 356 | int pageWidth, pageHeight, mm_w, mm_h; |
| 357 | float ppmm_h, ppmm_v; |
| 358 | |
| 359 | GetPageSizePixels(&pageWidth, &pageHeight); |
| 360 | GetPageSizeMM(&mm_w, &mm_h); |
| 361 | ppmm_h = (float)pageWidth / mm_w; |
| 362 | ppmm_v = (float)pageHeight / mm_h; |
| 363 | |
| 364 | int pos = 0; |
| 365 | m_NumPages = 0; |
| 366 | // m_PageBreaks[0] = 0; |
| 367 | |
| 368 | m_PageBreaks.Clear(); |
| 369 | m_PageBreaks.Add( 0); |
| 370 | do |
| 371 | { |
| 372 | pos = m_Renderer->Render((int)( ppmm_h * m_MarginLeft), |
| 373 | (int) (ppmm_v * (m_MarginTop + (m_HeaderHeight == 0 ? 0 : m_MarginSpace)) + m_HeaderHeight), |
| 374 | m_PageBreaks, |
| 375 | pos, true, INT_MAX); |
| 376 | m_PageBreaks.Add( pos); |
| 377 | if( m_PageBreaks.GetCount() > wxHTML_PRINT_MAX_PAGES) |
| 378 | { |
| 379 | wxMessageBox( _("HTML pagination algorithm generated more than the allowed maximum number of pages and it can't continue any longer!"), |
| 380 | _("Warning"), wxCANCEL | wxICON_ERROR ); |
| 381 | break; |
| 382 | } |
| 383 | } while (pos < m_Renderer->GetTotalHeight()); |
| 384 | } |
| 385 | |
| 386 | |
| 387 | |
| 388 | void wxHtmlPrintout::RenderPage(wxDC *dc, int page) |
| 389 | { |
| 390 | wxBusyCursor wait; |
| 391 | |
| 392 | int pageWidth, pageHeight, mm_w, mm_h, scr_w, scr_h, dc_w, dc_h; |
| 393 | float ppmm_h, ppmm_v; |
| 394 | |
| 395 | GetPageSizePixels(&pageWidth, &pageHeight); |
| 396 | GetPageSizeMM(&mm_w, &mm_h); |
| 397 | ppmm_h = (float)pageWidth / mm_w; |
| 398 | ppmm_v = (float)pageHeight / mm_h; |
| 399 | wxDisplaySize(&scr_w, &scr_h); |
| 400 | dc->GetSize(&dc_w, &dc_h); |
| 401 | |
| 402 | int ppiPrinterX, ppiPrinterY; |
| 403 | GetPPIPrinter(&ppiPrinterX, &ppiPrinterY); |
| 404 | wxUnusedVar(ppiPrinterX); |
| 405 | int ppiScreenX, ppiScreenY; |
| 406 | GetPPIScreen(&ppiScreenX, &ppiScreenY); |
| 407 | wxUnusedVar(ppiScreenX); |
| 408 | |
| 409 | dc->SetUserScale((double)dc_w / (double)pageWidth, (double)dc_w / (double)pageWidth); |
| 410 | |
| 411 | m_Renderer->SetDC(dc, (double)ppiPrinterY / (double)ppiScreenY); |
| 412 | |
| 413 | dc->SetBackgroundMode(wxTRANSPARENT); |
| 414 | |
| 415 | m_Renderer->Render((int) (ppmm_h * m_MarginLeft), |
| 416 | (int) (ppmm_v * (m_MarginTop + (m_HeaderHeight == 0 ? 0 : m_MarginSpace)) + m_HeaderHeight), m_PageBreaks, |
| 417 | m_PageBreaks[page-1], false, m_PageBreaks[page]-m_PageBreaks[page-1]); |
| 418 | |
| 419 | |
| 420 | m_RendererHdr->SetDC(dc, (double)ppiPrinterY / (double)ppiScreenY); |
| 421 | if (m_Headers[page % 2] != wxEmptyString) |
| 422 | { |
| 423 | m_RendererHdr->SetHtmlText(TranslateHeader(m_Headers[page % 2], page)); |
| 424 | m_RendererHdr->Render((int) (ppmm_h * m_MarginLeft), (int) (ppmm_v * m_MarginTop), m_PageBreaks); |
| 425 | } |
| 426 | if (m_Footers[page % 2] != wxEmptyString) |
| 427 | { |
| 428 | m_RendererHdr->SetHtmlText(TranslateHeader(m_Footers[page % 2], page)); |
| 429 | m_RendererHdr->Render((int) (ppmm_h * m_MarginLeft), (int) (pageHeight - ppmm_v * m_MarginBottom - m_FooterHeight), m_PageBreaks); |
| 430 | } |
| 431 | } |
| 432 | |
| 433 | |
| 434 | |
| 435 | wxString wxHtmlPrintout::TranslateHeader(const wxString& instr, int page) |
| 436 | { |
| 437 | wxString r = instr; |
| 438 | wxString num; |
| 439 | |
| 440 | num.Printf(wxT("%i"), page); |
| 441 | r.Replace(wxT("@PAGENUM@"), num); |
| 442 | |
| 443 | num.Printf(wxT("%lu"), (unsigned long)(m_PageBreaks.GetCount() - 1)); |
| 444 | r.Replace(wxT("@PAGESCNT@"), num); |
| 445 | |
| 446 | const wxDateTime now = wxDateTime::Now(); |
| 447 | r.Replace(wxT("@DATE@"), now.FormatDate()); |
| 448 | r.Replace(wxT("@TIME@"), now.FormatTime()); |
| 449 | |
| 450 | r.Replace(wxT("@TITLE@"), GetTitle()); |
| 451 | |
| 452 | return r; |
| 453 | } |
| 454 | |
| 455 | |
| 456 | |
| 457 | void wxHtmlPrintout::SetMargins(float top, float bottom, float left, float right, float spaces) |
| 458 | { |
| 459 | m_MarginTop = top; |
| 460 | m_MarginBottom = bottom; |
| 461 | m_MarginLeft = left; |
| 462 | m_MarginRight = right; |
| 463 | m_MarginSpace = spaces; |
| 464 | } |
| 465 | |
| 466 | |
| 467 | |
| 468 | |
| 469 | void wxHtmlPrintout::SetFonts(const wxString& normal_face, const wxString& fixed_face, |
| 470 | const int *sizes) |
| 471 | { |
| 472 | m_Renderer->SetFonts(normal_face, fixed_face, sizes); |
| 473 | m_RendererHdr->SetFonts(normal_face, fixed_face, sizes); |
| 474 | } |
| 475 | |
| 476 | void wxHtmlPrintout::SetStandardFonts(int size, |
| 477 | const wxString& normal_face, |
| 478 | const wxString& fixed_face) |
| 479 | { |
| 480 | m_Renderer->SetStandardFonts(size, normal_face, fixed_face); |
| 481 | m_RendererHdr->SetStandardFonts(size, normal_face, fixed_face); |
| 482 | } |
| 483 | |
| 484 | |
| 485 | |
| 486 | //---------------------------------------------------------------------------- |
| 487 | // wxHtmlEasyPrinting |
| 488 | //---------------------------------------------------------------------------- |
| 489 | |
| 490 | |
| 491 | wxHtmlEasyPrinting::wxHtmlEasyPrinting(const wxString& name, wxWindow *parentWindow) |
| 492 | { |
| 493 | m_ParentWindow = parentWindow; |
| 494 | m_Name = name; |
| 495 | m_PrintData = NULL; |
| 496 | m_PageSetupData = new wxPageSetupDialogData; |
| 497 | m_Headers[0] = m_Headers[1] = m_Footers[0] = m_Footers[1] = wxEmptyString; |
| 498 | |
| 499 | m_PageSetupData->EnableMargins(true); |
| 500 | m_PageSetupData->SetMarginTopLeft(wxPoint(25, 25)); |
| 501 | m_PageSetupData->SetMarginBottomRight(wxPoint(25, 25)); |
| 502 | |
| 503 | SetFonts(wxEmptyString, wxEmptyString, NULL); |
| 504 | } |
| 505 | |
| 506 | |
| 507 | |
| 508 | wxHtmlEasyPrinting::~wxHtmlEasyPrinting() |
| 509 | { |
| 510 | delete m_PrintData; |
| 511 | delete m_PageSetupData; |
| 512 | } |
| 513 | |
| 514 | |
| 515 | wxPrintData *wxHtmlEasyPrinting::GetPrintData() |
| 516 | { |
| 517 | if (m_PrintData == NULL) |
| 518 | m_PrintData = new wxPrintData(); |
| 519 | return m_PrintData; |
| 520 | } |
| 521 | |
| 522 | |
| 523 | bool wxHtmlEasyPrinting::PreviewFile(const wxString &htmlfile) |
| 524 | { |
| 525 | wxHtmlPrintout *p1 = CreatePrintout(); |
| 526 | p1->SetHtmlFile(htmlfile); |
| 527 | wxHtmlPrintout *p2 = CreatePrintout(); |
| 528 | p2->SetHtmlFile(htmlfile); |
| 529 | return DoPreview(p1, p2); |
| 530 | } |
| 531 | |
| 532 | |
| 533 | |
| 534 | bool wxHtmlEasyPrinting::PreviewText(const wxString &htmltext, const wxString &basepath) |
| 535 | { |
| 536 | wxHtmlPrintout *p1 = CreatePrintout(); |
| 537 | p1->SetHtmlText(htmltext, basepath, true); |
| 538 | wxHtmlPrintout *p2 = CreatePrintout(); |
| 539 | p2->SetHtmlText(htmltext, basepath, true); |
| 540 | return DoPreview(p1, p2); |
| 541 | } |
| 542 | |
| 543 | |
| 544 | |
| 545 | bool wxHtmlEasyPrinting::PrintFile(const wxString &htmlfile) |
| 546 | { |
| 547 | wxHtmlPrintout *p = CreatePrintout(); |
| 548 | p->SetHtmlFile(htmlfile); |
| 549 | bool ret = DoPrint(p); |
| 550 | delete p; |
| 551 | return ret; |
| 552 | } |
| 553 | |
| 554 | |
| 555 | |
| 556 | bool wxHtmlEasyPrinting::PrintText(const wxString &htmltext, const wxString &basepath) |
| 557 | { |
| 558 | wxHtmlPrintout *p = CreatePrintout(); |
| 559 | p->SetHtmlText(htmltext, basepath, true); |
| 560 | bool ret = DoPrint(p); |
| 561 | delete p; |
| 562 | return ret; |
| 563 | } |
| 564 | |
| 565 | |
| 566 | |
| 567 | bool wxHtmlEasyPrinting::DoPreview(wxHtmlPrintout *printout1, wxHtmlPrintout *printout2) |
| 568 | { |
| 569 | // Pass two printout objects: for preview, and possible printing. |
| 570 | wxPrintDialogData printDialogData(*GetPrintData()); |
| 571 | wxPrintPreview *preview = new wxPrintPreview(printout1, printout2, &printDialogData); |
| 572 | if (!preview->Ok()) |
| 573 | { |
| 574 | delete preview; |
| 575 | return false; |
| 576 | } |
| 577 | |
| 578 | wxPreviewFrame *frame = new wxPreviewFrame(preview, m_ParentWindow, |
| 579 | m_Name + _(" Preview"), |
| 580 | wxPoint(100, 100), wxSize(650, 500)); |
| 581 | frame->Centre(wxBOTH); |
| 582 | frame->Initialize(); |
| 583 | frame->Show(true); |
| 584 | return true; |
| 585 | } |
| 586 | |
| 587 | |
| 588 | |
| 589 | bool wxHtmlEasyPrinting::DoPrint(wxHtmlPrintout *printout) |
| 590 | { |
| 591 | wxPrintDialogData printDialogData(*GetPrintData()); |
| 592 | wxPrinter printer(&printDialogData); |
| 593 | |
| 594 | if (!printer.Print(m_ParentWindow, printout, true)) |
| 595 | { |
| 596 | return false; |
| 597 | } |
| 598 | |
| 599 | (*GetPrintData()) = printer.GetPrintDialogData().GetPrintData(); |
| 600 | return true; |
| 601 | } |
| 602 | |
| 603 | |
| 604 | |
| 605 | |
| 606 | void wxHtmlEasyPrinting::PageSetup() |
| 607 | { |
| 608 | if (!GetPrintData()->Ok()) |
| 609 | { |
| 610 | wxLogError(_("There was a problem during page setup: you may need to set a default printer.")); |
| 611 | return; |
| 612 | } |
| 613 | |
| 614 | m_PageSetupData->SetPrintData(*GetPrintData()); |
| 615 | wxPageSetupDialog pageSetupDialog(m_ParentWindow, m_PageSetupData); |
| 616 | |
| 617 | if (pageSetupDialog.ShowModal() == wxID_OK) |
| 618 | { |
| 619 | (*GetPrintData()) = pageSetupDialog.GetPageSetupData().GetPrintData(); |
| 620 | (*m_PageSetupData) = pageSetupDialog.GetPageSetupData(); |
| 621 | } |
| 622 | } |
| 623 | |
| 624 | |
| 625 | |
| 626 | void wxHtmlEasyPrinting::SetHeader(const wxString& header, int pg) |
| 627 | { |
| 628 | if (pg == wxPAGE_ALL || pg == wxPAGE_EVEN) |
| 629 | m_Headers[0] = header; |
| 630 | if (pg == wxPAGE_ALL || pg == wxPAGE_ODD) |
| 631 | m_Headers[1] = header; |
| 632 | } |
| 633 | |
| 634 | |
| 635 | |
| 636 | void wxHtmlEasyPrinting::SetFooter(const wxString& footer, int pg) |
| 637 | { |
| 638 | if (pg == wxPAGE_ALL || pg == wxPAGE_EVEN) |
| 639 | m_Footers[0] = footer; |
| 640 | if (pg == wxPAGE_ALL || pg == wxPAGE_ODD) |
| 641 | m_Footers[1] = footer; |
| 642 | } |
| 643 | |
| 644 | |
| 645 | void wxHtmlEasyPrinting::SetFonts(const wxString& normal_face, const wxString& fixed_face, |
| 646 | const int *sizes) |
| 647 | { |
| 648 | m_fontMode = FontMode_Explicit; |
| 649 | m_FontFaceNormal = normal_face; |
| 650 | m_FontFaceFixed = fixed_face; |
| 651 | |
| 652 | if (sizes) |
| 653 | { |
| 654 | m_FontsSizes = m_FontsSizesArr; |
| 655 | for (int i = 0; i < 7; i++) m_FontsSizes[i] = sizes[i]; |
| 656 | } |
| 657 | else |
| 658 | m_FontsSizes = NULL; |
| 659 | } |
| 660 | |
| 661 | void wxHtmlEasyPrinting::SetStandardFonts(int size, |
| 662 | const wxString& normal_face, |
| 663 | const wxString& fixed_face) |
| 664 | { |
| 665 | m_fontMode = FontMode_Standard; |
| 666 | m_FontFaceNormal = normal_face; |
| 667 | m_FontFaceFixed = fixed_face; |
| 668 | m_FontsSizesArr[0] = size; |
| 669 | } |
| 670 | |
| 671 | |
| 672 | wxHtmlPrintout *wxHtmlEasyPrinting::CreatePrintout() |
| 673 | { |
| 674 | wxHtmlPrintout *p = new wxHtmlPrintout(m_Name); |
| 675 | |
| 676 | if (m_fontMode == FontMode_Explicit) |
| 677 | { |
| 678 | p->SetFonts(m_FontFaceNormal, m_FontFaceFixed, m_FontsSizes); |
| 679 | } |
| 680 | else // FontMode_Standard |
| 681 | { |
| 682 | p->SetStandardFonts(m_FontsSizesArr[0], |
| 683 | m_FontFaceNormal, m_FontFaceFixed); |
| 684 | } |
| 685 | |
| 686 | p->SetHeader(m_Headers[0], wxPAGE_EVEN); |
| 687 | p->SetHeader(m_Headers[1], wxPAGE_ODD); |
| 688 | p->SetFooter(m_Footers[0], wxPAGE_EVEN); |
| 689 | p->SetFooter(m_Footers[1], wxPAGE_ODD); |
| 690 | |
| 691 | p->SetMargins(m_PageSetupData->GetMarginTopLeft().y, |
| 692 | m_PageSetupData->GetMarginBottomRight().y, |
| 693 | m_PageSetupData->GetMarginTopLeft().x, |
| 694 | m_PageSetupData->GetMarginBottomRight().x); |
| 695 | |
| 696 | return p; |
| 697 | } |
| 698 | |
| 699 | // A module to allow initialization/cleanup |
| 700 | // without calling these functions from app.cpp or from |
| 701 | // the user's application. |
| 702 | |
| 703 | class wxHtmlPrintingModule: public wxModule |
| 704 | { |
| 705 | DECLARE_DYNAMIC_CLASS(wxHtmlPrintingModule) |
| 706 | public: |
| 707 | wxHtmlPrintingModule() : wxModule() {} |
| 708 | bool OnInit() { return true; } |
| 709 | void OnExit() { wxHtmlPrintout::CleanUpStatics(); } |
| 710 | }; |
| 711 | |
| 712 | IMPLEMENT_DYNAMIC_CLASS(wxHtmlPrintingModule, wxModule) |
| 713 | |
| 714 | |
| 715 | // This hack forces the linker to always link in m_* files |
| 716 | // (wxHTML doesn't work without handlers from these files) |
| 717 | #include "wx/html/forcelnk.h" |
| 718 | FORCE_WXHTML_MODULES() |
| 719 | |
| 720 | #endif // wxUSE_HTML & wxUSE_PRINTING_ARCHITECTURE |