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