| 1 | ///////////////////////////////////////////////////////////////////////////// |
| 2 | // Name: src/html/htmlcell.cpp |
| 3 | // Purpose: wxHtmlCell - basic element of HTML output |
| 4 | // Author: Vaclav Slavik |
| 5 | // RCS-ID: $Id$ |
| 6 | // Copyright: (c) 1999 Vaclav Slavik |
| 7 | // Licence: wxWindows licence |
| 8 | ///////////////////////////////////////////////////////////////////////////// |
| 9 | |
| 10 | #include "wx/wxprec.h" |
| 11 | |
| 12 | #ifdef __BORLANDC__ |
| 13 | #pragma hdrstop |
| 14 | #endif |
| 15 | |
| 16 | #if wxUSE_HTML && wxUSE_STREAMS |
| 17 | |
| 18 | #ifndef WX_PRECOMP |
| 19 | #include "wx/dynarray.h" |
| 20 | #include "wx/brush.h" |
| 21 | #include "wx/colour.h" |
| 22 | #include "wx/dc.h" |
| 23 | #include "wx/settings.h" |
| 24 | #include "wx/module.h" |
| 25 | #include "wx/wxcrtvararg.h" |
| 26 | #endif |
| 27 | |
| 28 | #include "wx/html/htmlcell.h" |
| 29 | #include "wx/html/htmlwin.h" |
| 30 | |
| 31 | #include <stdlib.h> |
| 32 | |
| 33 | //----------------------------------------------------------------------------- |
| 34 | // Helper classes |
| 35 | //----------------------------------------------------------------------------- |
| 36 | |
| 37 | void wxHtmlSelection::Set(const wxPoint& fromPos, const wxHtmlCell *fromCell, |
| 38 | const wxPoint& toPos, const wxHtmlCell *toCell) |
| 39 | { |
| 40 | m_fromCell = fromCell; |
| 41 | m_toCell = toCell; |
| 42 | m_fromPos = fromPos; |
| 43 | m_toPos = toPos; |
| 44 | } |
| 45 | |
| 46 | void wxHtmlSelection::Set(const wxHtmlCell *fromCell, const wxHtmlCell *toCell) |
| 47 | { |
| 48 | wxPoint p1 = fromCell ? fromCell->GetAbsPos() : wxDefaultPosition; |
| 49 | wxPoint p2 = toCell ? toCell->GetAbsPos() : wxDefaultPosition; |
| 50 | if ( toCell ) |
| 51 | { |
| 52 | p2.x += toCell->GetWidth(); |
| 53 | p2.y += toCell->GetHeight(); |
| 54 | } |
| 55 | Set(p1, fromCell, p2, toCell); |
| 56 | } |
| 57 | |
| 58 | wxColour |
| 59 | wxDefaultHtmlRenderingStyle:: |
| 60 | GetSelectedTextColour(const wxColour& WXUNUSED(clr)) |
| 61 | { |
| 62 | return wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT); |
| 63 | } |
| 64 | |
| 65 | wxColour |
| 66 | wxDefaultHtmlRenderingStyle:: |
| 67 | GetSelectedTextBgColour(const wxColour& WXUNUSED(clr)) |
| 68 | { |
| 69 | return wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHT); |
| 70 | } |
| 71 | |
| 72 | |
| 73 | //----------------------------------------------------------------------------- |
| 74 | // wxHtmlCell |
| 75 | //----------------------------------------------------------------------------- |
| 76 | |
| 77 | IMPLEMENT_ABSTRACT_CLASS(wxHtmlCell, wxObject) |
| 78 | |
| 79 | wxHtmlCell::wxHtmlCell() : wxObject() |
| 80 | { |
| 81 | m_Next = NULL; |
| 82 | m_Parent = NULL; |
| 83 | m_Width = m_Height = m_Descent = 0; |
| 84 | m_ScriptMode = wxHTML_SCRIPT_NORMAL; // <sub> or <sup> mode |
| 85 | m_ScriptBaseline = 0; // <sub> or <sup> baseline |
| 86 | m_CanLiveOnPagebreak = true; |
| 87 | m_Link = NULL; |
| 88 | } |
| 89 | |
| 90 | wxHtmlCell::~wxHtmlCell() |
| 91 | { |
| 92 | delete m_Link; |
| 93 | } |
| 94 | |
| 95 | // Update the descent value when whe are in a <sub> or <sup>. |
| 96 | // prevbase is the parent base |
| 97 | void wxHtmlCell::SetScriptMode(wxHtmlScriptMode mode, long previousBase) |
| 98 | { |
| 99 | m_ScriptMode = mode; |
| 100 | |
| 101 | if (mode == wxHTML_SCRIPT_SUP) |
| 102 | m_ScriptBaseline = previousBase - (m_Height + 1) / 2; |
| 103 | else if (mode == wxHTML_SCRIPT_SUB) |
| 104 | m_ScriptBaseline = previousBase + (m_Height + 1) / 6; |
| 105 | else |
| 106 | m_ScriptBaseline = 0; |
| 107 | |
| 108 | m_Descent += m_ScriptBaseline; |
| 109 | } |
| 110 | |
| 111 | #if WXWIN_COMPATIBILITY_2_6 |
| 112 | |
| 113 | struct wxHtmlCellOnMouseClickCompatHelper; |
| 114 | |
| 115 | static wxHtmlCellOnMouseClickCompatHelper *gs_helperOnMouseClick = NULL; |
| 116 | |
| 117 | // helper for routing calls to new ProcessMouseClick() method to deprecated |
| 118 | // OnMouseClick() method |
| 119 | struct wxHtmlCellOnMouseClickCompatHelper |
| 120 | { |
| 121 | wxHtmlCellOnMouseClickCompatHelper(wxHtmlWindowInterface *window_, |
| 122 | const wxPoint& pos_, |
| 123 | const wxMouseEvent& event_) |
| 124 | : window(window_), pos(pos_), event(event_), retval(false) |
| 125 | { |
| 126 | } |
| 127 | |
| 128 | bool CallOnMouseClick(wxHtmlCell *cell) |
| 129 | { |
| 130 | wxHtmlCellOnMouseClickCompatHelper *oldHelper = gs_helperOnMouseClick; |
| 131 | gs_helperOnMouseClick = this; |
| 132 | cell->OnMouseClick |
| 133 | ( |
| 134 | window ? window->GetHTMLWindow() : NULL, |
| 135 | pos.x, pos.y, |
| 136 | event |
| 137 | ); |
| 138 | gs_helperOnMouseClick = oldHelper; |
| 139 | return retval; |
| 140 | } |
| 141 | |
| 142 | wxHtmlWindowInterface *window; |
| 143 | const wxPoint& pos; |
| 144 | const wxMouseEvent& event; |
| 145 | bool retval; |
| 146 | }; |
| 147 | #endif // WXWIN_COMPATIBILITY_2_6 |
| 148 | |
| 149 | bool wxHtmlCell::ProcessMouseClick(wxHtmlWindowInterface *window, |
| 150 | const wxPoint& pos, |
| 151 | const wxMouseEvent& event) |
| 152 | { |
| 153 | wxCHECK_MSG( window, false, _T("window interface must be provided") ); |
| 154 | |
| 155 | #if WXWIN_COMPATIBILITY_2_6 |
| 156 | // NB: this hack puts the body of ProcessMouseClick() into OnMouseClick() |
| 157 | // (for which it has to pass the arguments and return value via a |
| 158 | // helper variable because these two methods have different |
| 159 | // signatures), so that old code overriding OnMouseClick will continue |
| 160 | // to work |
| 161 | wxHtmlCellOnMouseClickCompatHelper compat(window, pos, event); |
| 162 | return compat.CallOnMouseClick(this); |
| 163 | } |
| 164 | |
| 165 | void wxHtmlCell::OnMouseClick(wxWindow *, int, int, const wxMouseEvent& event) |
| 166 | { |
| 167 | wxCHECK_RET( gs_helperOnMouseClick, _T("unexpected call to OnMouseClick") ); |
| 168 | wxHtmlWindowInterface *window = gs_helperOnMouseClick->window; |
| 169 | const wxPoint& pos = gs_helperOnMouseClick->pos; |
| 170 | #endif // WXWIN_COMPATIBILITY_2_6 |
| 171 | |
| 172 | wxHtmlLinkInfo *lnk = GetLink(pos.x, pos.y); |
| 173 | bool retval = false; |
| 174 | |
| 175 | if (lnk) |
| 176 | { |
| 177 | wxHtmlLinkInfo lnk2(*lnk); |
| 178 | lnk2.SetEvent(&event); |
| 179 | lnk2.SetHtmlCell(this); |
| 180 | |
| 181 | window->OnHTMLLinkClicked(lnk2); |
| 182 | retval = true; |
| 183 | } |
| 184 | |
| 185 | #if WXWIN_COMPATIBILITY_2_6 |
| 186 | gs_helperOnMouseClick->retval = retval; |
| 187 | #else |
| 188 | return retval; |
| 189 | #endif // WXWIN_COMPATIBILITY_2_6 |
| 190 | } |
| 191 | |
| 192 | #if WXWIN_COMPATIBILITY_2_6 |
| 193 | wxCursor wxHtmlCell::GetCursor() const |
| 194 | { |
| 195 | return wxNullCursor; |
| 196 | } |
| 197 | #endif // WXWIN_COMPATIBILITY_2_6 |
| 198 | |
| 199 | wxCursor wxHtmlCell::GetMouseCursor(wxHtmlWindowInterface *window) const |
| 200 | { |
| 201 | #if WXWIN_COMPATIBILITY_2_6 |
| 202 | // NB: Older versions of wx used GetCursor() virtual method in place of |
| 203 | // GetMouseCursor(interface). This code ensures that user code that |
| 204 | // overriden GetCursor() continues to work. The trick is that the base |
| 205 | // wxHtmlCell::GetCursor() method simply returns wxNullCursor, so we |
| 206 | // know that GetCursor() was overriden iff it returns valid cursor. |
| 207 | wxCursor cur = GetCursor(); |
| 208 | if (cur.Ok()) |
| 209 | return cur; |
| 210 | #endif // WXWIN_COMPATIBILITY_2_6 |
| 211 | |
| 212 | if ( GetLink() ) |
| 213 | { |
| 214 | return window->GetHTMLCursor(wxHtmlWindowInterface::HTMLCursor_Link); |
| 215 | } |
| 216 | else |
| 217 | { |
| 218 | return window->GetHTMLCursor(wxHtmlWindowInterface::HTMLCursor_Default); |
| 219 | } |
| 220 | } |
| 221 | |
| 222 | |
| 223 | bool wxHtmlCell::AdjustPagebreak(int *pagebreak, |
| 224 | wxArrayInt& WXUNUSED(known_pagebreaks)) const |
| 225 | { |
| 226 | if ((!m_CanLiveOnPagebreak) && |
| 227 | m_PosY < *pagebreak && m_PosY + m_Height > *pagebreak) |
| 228 | { |
| 229 | *pagebreak = m_PosY; |
| 230 | return true; |
| 231 | } |
| 232 | |
| 233 | return false; |
| 234 | } |
| 235 | |
| 236 | |
| 237 | |
| 238 | void wxHtmlCell::SetLink(const wxHtmlLinkInfo& link) |
| 239 | { |
| 240 | if (m_Link) delete m_Link; |
| 241 | m_Link = NULL; |
| 242 | if (link.GetHref() != wxEmptyString) |
| 243 | m_Link = new wxHtmlLinkInfo(link); |
| 244 | } |
| 245 | |
| 246 | |
| 247 | void wxHtmlCell::Layout(int WXUNUSED(w)) |
| 248 | { |
| 249 | SetPos(0, 0); |
| 250 | } |
| 251 | |
| 252 | |
| 253 | |
| 254 | const wxHtmlCell* wxHtmlCell::Find(int WXUNUSED(condition), const void* WXUNUSED(param)) const |
| 255 | { |
| 256 | return NULL; |
| 257 | } |
| 258 | |
| 259 | |
| 260 | wxHtmlCell *wxHtmlCell::FindCellByPos(wxCoord x, wxCoord y, |
| 261 | unsigned flags) const |
| 262 | { |
| 263 | if ( x >= 0 && x < m_Width && y >= 0 && y < m_Height ) |
| 264 | { |
| 265 | return wxConstCast(this, wxHtmlCell); |
| 266 | } |
| 267 | else |
| 268 | { |
| 269 | if ((flags & wxHTML_FIND_NEAREST_AFTER) && |
| 270 | (y < 0 || (y < 0+m_Height && x < 0+m_Width))) |
| 271 | return wxConstCast(this, wxHtmlCell); |
| 272 | else if ((flags & wxHTML_FIND_NEAREST_BEFORE) && |
| 273 | (y >= 0+m_Height || (y >= 0 && x >= 0))) |
| 274 | return wxConstCast(this, wxHtmlCell); |
| 275 | else |
| 276 | return NULL; |
| 277 | } |
| 278 | } |
| 279 | |
| 280 | |
| 281 | wxPoint wxHtmlCell::GetAbsPos(wxHtmlCell *rootCell) const |
| 282 | { |
| 283 | wxPoint p(m_PosX, m_PosY); |
| 284 | for (wxHtmlCell *parent = m_Parent; parent && parent != rootCell; |
| 285 | parent = parent->m_Parent) |
| 286 | { |
| 287 | p.x += parent->m_PosX; |
| 288 | p.y += parent->m_PosY; |
| 289 | } |
| 290 | return p; |
| 291 | } |
| 292 | |
| 293 | wxHtmlCell *wxHtmlCell::GetRootCell() const |
| 294 | { |
| 295 | wxHtmlCell *c = wxConstCast(this, wxHtmlCell); |
| 296 | while ( c->m_Parent ) |
| 297 | c = c->m_Parent; |
| 298 | return c; |
| 299 | } |
| 300 | |
| 301 | unsigned wxHtmlCell::GetDepth() const |
| 302 | { |
| 303 | unsigned d = 0; |
| 304 | for (wxHtmlCell *p = m_Parent; p; p = p->m_Parent) |
| 305 | d++; |
| 306 | return d; |
| 307 | } |
| 308 | |
| 309 | bool wxHtmlCell::IsBefore(wxHtmlCell *cell) const |
| 310 | { |
| 311 | const wxHtmlCell *c1 = this; |
| 312 | const wxHtmlCell *c2 = cell; |
| 313 | unsigned d1 = GetDepth(); |
| 314 | unsigned d2 = cell->GetDepth(); |
| 315 | |
| 316 | if ( d1 > d2 ) |
| 317 | for (; d1 != d2; d1-- ) |
| 318 | c1 = c1->m_Parent; |
| 319 | else if ( d1 < d2 ) |
| 320 | for (; d1 != d2; d2-- ) |
| 321 | c2 = c2->m_Parent; |
| 322 | |
| 323 | if ( cell == this ) |
| 324 | return true; |
| 325 | |
| 326 | while ( c1 && c2 ) |
| 327 | { |
| 328 | if ( c1->m_Parent == c2->m_Parent ) |
| 329 | { |
| 330 | while ( c1 ) |
| 331 | { |
| 332 | if ( c1 == c2 ) |
| 333 | return true; |
| 334 | c1 = c1->GetNext(); |
| 335 | } |
| 336 | return false; |
| 337 | } |
| 338 | else |
| 339 | { |
| 340 | c1 = c1->m_Parent; |
| 341 | c2 = c2->m_Parent; |
| 342 | } |
| 343 | } |
| 344 | |
| 345 | wxFAIL_MSG(_T("Cells are in different trees")); |
| 346 | return false; |
| 347 | } |
| 348 | |
| 349 | |
| 350 | //----------------------------------------------------------------------------- |
| 351 | // wxHtmlWordCell |
| 352 | //----------------------------------------------------------------------------- |
| 353 | |
| 354 | IMPLEMENT_ABSTRACT_CLASS(wxHtmlWordCell, wxHtmlCell) |
| 355 | |
| 356 | wxHtmlWordCell::wxHtmlWordCell(const wxString& word, const wxDC& dc) : wxHtmlCell() |
| 357 | { |
| 358 | m_Word = word; |
| 359 | wxCoord w, h, d; |
| 360 | dc.GetTextExtent(m_Word, &w, &h, &d); |
| 361 | m_Width = w; |
| 362 | m_Height = h; |
| 363 | m_Descent = d; |
| 364 | SetCanLiveOnPagebreak(false); |
| 365 | m_allowLinebreak = true; |
| 366 | } |
| 367 | |
| 368 | void wxHtmlWordCell::SetPreviousWord(wxHtmlWordCell *cell) |
| 369 | { |
| 370 | if ( cell && m_Parent == cell->m_Parent && |
| 371 | !wxIsspace(cell->m_Word.Last()) && !wxIsspace(m_Word[0u]) ) |
| 372 | { |
| 373 | m_allowLinebreak = false; |
| 374 | } |
| 375 | } |
| 376 | |
| 377 | // Splits m_Word into up to three parts according to selection, returns |
| 378 | // substring before, in and after selection and the points (in relative coords) |
| 379 | // where s2 and s3 start: |
| 380 | void wxHtmlWordCell::Split(const wxDC& dc, |
| 381 | const wxPoint& selFrom, const wxPoint& selTo, |
| 382 | unsigned& pos1, unsigned& pos2) const |
| 383 | { |
| 384 | wxPoint pt1 = (selFrom == wxDefaultPosition) ? |
| 385 | wxDefaultPosition : selFrom - GetAbsPos(); |
| 386 | wxPoint pt2 = (selTo == wxDefaultPosition) ? |
| 387 | wxPoint(m_Width, wxDefaultCoord) : selTo - GetAbsPos(); |
| 388 | |
| 389 | // if the selection is entirely within this cell, make sure pt1 < pt2 in |
| 390 | // order to make the rest of this function simpler: |
| 391 | if ( selFrom != wxDefaultPosition && selTo != wxDefaultPosition && |
| 392 | selFrom.x > selTo.x ) |
| 393 | { |
| 394 | wxPoint tmp = pt1; |
| 395 | pt1 = pt2; |
| 396 | pt2 = tmp; |
| 397 | } |
| 398 | |
| 399 | unsigned len = m_Word.length(); |
| 400 | unsigned i = 0; |
| 401 | pos1 = 0; |
| 402 | |
| 403 | // adjust for cases when the start/end position is completely |
| 404 | // outside the cell: |
| 405 | if ( pt1.y < 0 ) |
| 406 | pt1.x = 0; |
| 407 | if ( pt2.y >= m_Height ) |
| 408 | pt2.x = m_Width; |
| 409 | |
| 410 | // before selection: |
| 411 | // (include character under caret only if in first half of width) |
| 412 | #ifdef __WXMAC__ |
| 413 | // implementation using PartialExtents to support fractional widths |
| 414 | wxArrayInt widths ; |
| 415 | dc.GetPartialTextExtents(m_Word,widths) ; |
| 416 | while( i < len && pt1.x >= widths[i] ) |
| 417 | i++ ; |
| 418 | if ( i < len ) |
| 419 | { |
| 420 | int charW = (i > 0) ? widths[i] - widths[i-1] : widths[i]; |
| 421 | if ( widths[i] - pt1.x < charW/2 ) |
| 422 | i++; |
| 423 | } |
| 424 | #else // !__WXMAC__ |
| 425 | wxCoord charW, charH; |
| 426 | while ( pt1.x > 0 && i < len ) |
| 427 | { |
| 428 | dc.GetTextExtent(m_Word[i], &charW, &charH); |
| 429 | pt1.x -= charW; |
| 430 | if ( pt1.x >= -charW/2 ) |
| 431 | { |
| 432 | pos1 += charW; |
| 433 | i++; |
| 434 | } |
| 435 | } |
| 436 | #endif // __WXMAC__/!__WXMAC__ |
| 437 | |
| 438 | // in selection: |
| 439 | // (include character under caret only if in first half of width) |
| 440 | unsigned j = i; |
| 441 | #ifdef __WXMAC__ |
| 442 | while( j < len && pt2.x >= widths[j] ) |
| 443 | j++ ; |
| 444 | if ( j < len ) |
| 445 | { |
| 446 | int charW = (j > 0) ? widths[j] - widths[j-1] : widths[j]; |
| 447 | if ( widths[j] - pt2.x < charW/2 ) |
| 448 | j++; |
| 449 | } |
| 450 | #else // !__WXMAC__ |
| 451 | pos2 = pos1; |
| 452 | pt2.x -= pos2; |
| 453 | while ( pt2.x > 0 && j < len ) |
| 454 | { |
| 455 | dc.GetTextExtent(m_Word[j], &charW, &charH); |
| 456 | pt2.x -= charW; |
| 457 | if ( pt2.x >= -charW/2 ) |
| 458 | { |
| 459 | pos2 += charW; |
| 460 | j++; |
| 461 | } |
| 462 | } |
| 463 | #endif // __WXMAC__/!__WXMAC__ |
| 464 | |
| 465 | pos1 = i; |
| 466 | pos2 = j; |
| 467 | } |
| 468 | |
| 469 | void wxHtmlWordCell::SetSelectionPrivPos(const wxDC& dc, wxHtmlSelection *s) const |
| 470 | { |
| 471 | unsigned p1, p2; |
| 472 | |
| 473 | Split(dc, |
| 474 | this == s->GetFromCell() ? s->GetFromPos() : wxDefaultPosition, |
| 475 | this == s->GetToCell() ? s->GetToPos() : wxDefaultPosition, |
| 476 | p1, p2); |
| 477 | |
| 478 | wxPoint p(0, m_Word.length()); |
| 479 | |
| 480 | if ( this == s->GetFromCell() ) |
| 481 | p.x = p1; // selection starts here |
| 482 | if ( this == s->GetToCell() ) |
| 483 | p.y = p2; // selection ends here |
| 484 | |
| 485 | if ( this == s->GetFromCell() ) |
| 486 | s->SetFromPrivPos(p); |
| 487 | if ( this == s->GetToCell() ) |
| 488 | s->SetToPrivPos(p); |
| 489 | } |
| 490 | |
| 491 | |
| 492 | static void SwitchSelState(wxDC& dc, wxHtmlRenderingInfo& info, |
| 493 | bool toSelection) |
| 494 | { |
| 495 | wxColour fg = info.GetState().GetFgColour(); |
| 496 | wxColour bg = info.GetState().GetBgColour(); |
| 497 | |
| 498 | if ( toSelection ) |
| 499 | { |
| 500 | dc.SetBackgroundMode(wxSOLID); |
| 501 | dc.SetTextForeground(info.GetStyle().GetSelectedTextColour(fg)); |
| 502 | dc.SetTextBackground(info.GetStyle().GetSelectedTextBgColour(bg)); |
| 503 | dc.SetBackground(wxBrush(info.GetStyle().GetSelectedTextBgColour(bg), |
| 504 | wxSOLID)); |
| 505 | } |
| 506 | else |
| 507 | { |
| 508 | dc.SetBackgroundMode(wxTRANSPARENT); |
| 509 | dc.SetTextForeground(fg); |
| 510 | dc.SetTextBackground(bg); |
| 511 | dc.SetBackground(wxBrush(bg, wxSOLID)); |
| 512 | } |
| 513 | } |
| 514 | |
| 515 | |
| 516 | void wxHtmlWordCell::Draw(wxDC& dc, int x, int y, |
| 517 | int WXUNUSED(view_y1), int WXUNUSED(view_y2), |
| 518 | wxHtmlRenderingInfo& info) |
| 519 | { |
| 520 | #if 0 // useful for debugging |
| 521 | dc.SetPen(*wxBLACK_PEN); |
| 522 | dc.DrawRectangle(x+m_PosX,y+m_PosY,m_Width /* VZ: +1? */ ,m_Height); |
| 523 | #endif |
| 524 | |
| 525 | bool drawSelectionAfterCell = false; |
| 526 | |
| 527 | if ( info.GetState().GetSelectionState() == wxHTML_SEL_CHANGING ) |
| 528 | { |
| 529 | // Selection changing, we must draw the word piecewise: |
| 530 | wxHtmlSelection *s = info.GetSelection(); |
| 531 | wxString txt; |
| 532 | int w, h; |
| 533 | int ofs = 0; |
| 534 | |
| 535 | wxPoint priv = (this == s->GetFromCell()) ? |
| 536 | s->GetFromPrivPos() : s->GetToPrivPos(); |
| 537 | |
| 538 | // NB: this is quite a hack: in order to compute selection boundaries |
| 539 | // (in word's characters) we must know current font, which is only |
| 540 | // possible inside rendering code. Therefore we update the |
| 541 | // information here and store it in wxHtmlSelection so that |
| 542 | // ConvertToText can use it later: |
| 543 | if ( priv == wxDefaultPosition ) |
| 544 | { |
| 545 | SetSelectionPrivPos(dc, s); |
| 546 | priv = (this == s->GetFromCell()) ? |
| 547 | s->GetFromPrivPos() : s->GetToPrivPos(); |
| 548 | } |
| 549 | |
| 550 | int part1 = priv.x; |
| 551 | int part2 = priv.y; |
| 552 | |
| 553 | if ( part1 > 0 ) |
| 554 | { |
| 555 | txt = m_Word.Mid(0, part1); |
| 556 | dc.DrawText(txt, x + m_PosX, y + m_PosY); |
| 557 | dc.GetTextExtent(txt, &w, &h); |
| 558 | ofs += w; |
| 559 | } |
| 560 | |
| 561 | SwitchSelState(dc, info, true); |
| 562 | |
| 563 | txt = m_Word.Mid(part1, part2-part1); |
| 564 | dc.DrawText(txt, ofs + x + m_PosX, y + m_PosY); |
| 565 | |
| 566 | if ( (size_t)part2 < m_Word.length() ) |
| 567 | { |
| 568 | dc.GetTextExtent(txt, &w, &h); |
| 569 | ofs += w; |
| 570 | SwitchSelState(dc, info, false); |
| 571 | txt = m_Word.Mid(part2); |
| 572 | dc.DrawText(txt, ofs + x + m_PosX, y + m_PosY); |
| 573 | } |
| 574 | else |
| 575 | drawSelectionAfterCell = true; |
| 576 | } |
| 577 | else |
| 578 | { |
| 579 | wxHtmlSelectionState selstate = info.GetState().GetSelectionState(); |
| 580 | // Not changing selection state, draw the word in single mode: |
| 581 | if ( selstate != wxHTML_SEL_OUT && |
| 582 | dc.GetBackgroundMode() != wxSOLID ) |
| 583 | { |
| 584 | SwitchSelState(dc, info, true); |
| 585 | } |
| 586 | else if ( selstate == wxHTML_SEL_OUT && |
| 587 | dc.GetBackgroundMode() == wxSOLID ) |
| 588 | { |
| 589 | SwitchSelState(dc, info, false); |
| 590 | } |
| 591 | dc.DrawText(m_Word, x + m_PosX, y + m_PosY); |
| 592 | drawSelectionAfterCell = (selstate != wxHTML_SEL_OUT); |
| 593 | } |
| 594 | |
| 595 | // NB: If the text is justified then there is usually some free space |
| 596 | // between adjacent cells and drawing the selection only onto cells |
| 597 | // would result in ugly unselected spaces. The code below detects |
| 598 | // this special case and renders the selection *outside* the sell, |
| 599 | // too. |
| 600 | if ( m_Parent->GetAlignHor() == wxHTML_ALIGN_JUSTIFY && |
| 601 | drawSelectionAfterCell ) |
| 602 | { |
| 603 | wxHtmlCell *nextCell = m_Next; |
| 604 | while ( nextCell && nextCell->IsFormattingCell() ) |
| 605 | nextCell = nextCell->GetNext(); |
| 606 | if ( nextCell ) |
| 607 | { |
| 608 | int nextX = nextCell->GetPosX(); |
| 609 | if ( m_PosX + m_Width < nextX ) |
| 610 | { |
| 611 | dc.SetBrush(dc.GetBackground()); |
| 612 | dc.SetPen(*wxTRANSPARENT_PEN); |
| 613 | dc.DrawRectangle(x + m_PosX + m_Width, y + m_PosY, |
| 614 | nextX - m_PosX - m_Width, m_Height); |
| 615 | } |
| 616 | } |
| 617 | } |
| 618 | } |
| 619 | |
| 620 | |
| 621 | wxString wxHtmlWordCell::ConvertToText(wxHtmlSelection *s) const |
| 622 | { |
| 623 | if ( s && (this == s->GetFromCell() || this == s->GetToCell()) ) |
| 624 | { |
| 625 | wxPoint priv = this == s->GetFromCell() ? s->GetFromPrivPos() |
| 626 | : s->GetToPrivPos(); |
| 627 | |
| 628 | // VZ: we may be called before we had a chance to re-render ourselves |
| 629 | // and in this case GetFrom/ToPrivPos() is not set yet -- assume |
| 630 | // that this only happens in case of a double/triple click (which |
| 631 | // seems to be the case now) and so it makes sense to select the |
| 632 | // entire contents of the cell in this case |
| 633 | // |
| 634 | // TODO: but this really needs to be fixed in some better way later... |
| 635 | if ( priv != wxDefaultPosition ) |
| 636 | { |
| 637 | int part1 = priv.x; |
| 638 | int part2 = priv.y; |
| 639 | return m_Word.Mid(part1, part2-part1); |
| 640 | } |
| 641 | //else: return the whole word below |
| 642 | } |
| 643 | |
| 644 | return m_Word; |
| 645 | } |
| 646 | |
| 647 | wxCursor wxHtmlWordCell::GetMouseCursor(wxHtmlWindowInterface *window) const |
| 648 | { |
| 649 | if ( !GetLink() ) |
| 650 | { |
| 651 | return window->GetHTMLCursor(wxHtmlWindowInterface::HTMLCursor_Text); |
| 652 | } |
| 653 | else |
| 654 | { |
| 655 | return wxHtmlCell::GetMouseCursor(window); |
| 656 | } |
| 657 | } |
| 658 | |
| 659 | |
| 660 | //----------------------------------------------------------------------------- |
| 661 | // wxHtmlContainerCell |
| 662 | //----------------------------------------------------------------------------- |
| 663 | |
| 664 | IMPLEMENT_ABSTRACT_CLASS(wxHtmlContainerCell, wxHtmlCell) |
| 665 | |
| 666 | wxHtmlContainerCell::wxHtmlContainerCell(wxHtmlContainerCell *parent) : wxHtmlCell() |
| 667 | { |
| 668 | m_Cells = m_LastCell = NULL; |
| 669 | m_Parent = parent; |
| 670 | m_MaxTotalWidth = 0; |
| 671 | if (m_Parent) m_Parent->InsertCell(this); |
| 672 | m_AlignHor = wxHTML_ALIGN_LEFT; |
| 673 | m_AlignVer = wxHTML_ALIGN_BOTTOM; |
| 674 | m_IndentLeft = m_IndentRight = m_IndentTop = m_IndentBottom = 0; |
| 675 | m_WidthFloat = 100; m_WidthFloatUnits = wxHTML_UNITS_PERCENT; |
| 676 | m_UseBkColour = false; |
| 677 | m_UseBorder = false; |
| 678 | m_MinHeight = 0; |
| 679 | m_MinHeightAlign = wxHTML_ALIGN_TOP; |
| 680 | m_LastLayout = -1; |
| 681 | } |
| 682 | |
| 683 | wxHtmlContainerCell::~wxHtmlContainerCell() |
| 684 | { |
| 685 | wxHtmlCell *cell = m_Cells; |
| 686 | while ( cell ) |
| 687 | { |
| 688 | wxHtmlCell *cellNext = cell->GetNext(); |
| 689 | delete cell; |
| 690 | cell = cellNext; |
| 691 | } |
| 692 | } |
| 693 | |
| 694 | |
| 695 | |
| 696 | void wxHtmlContainerCell::SetIndent(int i, int what, int units) |
| 697 | { |
| 698 | int val = (units == wxHTML_UNITS_PIXELS) ? i : -i; |
| 699 | if (what & wxHTML_INDENT_LEFT) m_IndentLeft = val; |
| 700 | if (what & wxHTML_INDENT_RIGHT) m_IndentRight = val; |
| 701 | if (what & wxHTML_INDENT_TOP) m_IndentTop = val; |
| 702 | if (what & wxHTML_INDENT_BOTTOM) m_IndentBottom = val; |
| 703 | m_LastLayout = -1; |
| 704 | } |
| 705 | |
| 706 | |
| 707 | |
| 708 | int wxHtmlContainerCell::GetIndent(int ind) const |
| 709 | { |
| 710 | if (ind & wxHTML_INDENT_LEFT) return m_IndentLeft; |
| 711 | else if (ind & wxHTML_INDENT_RIGHT) return m_IndentRight; |
| 712 | else if (ind & wxHTML_INDENT_TOP) return m_IndentTop; |
| 713 | else if (ind & wxHTML_INDENT_BOTTOM) return m_IndentBottom; |
| 714 | else return -1; /* BUG! Should not be called... */ |
| 715 | } |
| 716 | |
| 717 | |
| 718 | |
| 719 | |
| 720 | int wxHtmlContainerCell::GetIndentUnits(int ind) const |
| 721 | { |
| 722 | bool p = false; |
| 723 | if (ind & wxHTML_INDENT_LEFT) p = m_IndentLeft < 0; |
| 724 | else if (ind & wxHTML_INDENT_RIGHT) p = m_IndentRight < 0; |
| 725 | else if (ind & wxHTML_INDENT_TOP) p = m_IndentTop < 0; |
| 726 | else if (ind & wxHTML_INDENT_BOTTOM) p = m_IndentBottom < 0; |
| 727 | if (p) return wxHTML_UNITS_PERCENT; |
| 728 | else return wxHTML_UNITS_PIXELS; |
| 729 | } |
| 730 | |
| 731 | |
| 732 | bool wxHtmlContainerCell::AdjustPagebreak(int *pagebreak, |
| 733 | wxArrayInt& known_pagebreaks) const |
| 734 | { |
| 735 | if (!m_CanLiveOnPagebreak) |
| 736 | return wxHtmlCell::AdjustPagebreak(pagebreak, known_pagebreaks); |
| 737 | |
| 738 | wxHtmlCell *c = GetFirstChild(); |
| 739 | bool rt = false; |
| 740 | int pbrk = *pagebreak - m_PosY; |
| 741 | |
| 742 | while (c) |
| 743 | { |
| 744 | if (c->AdjustPagebreak(&pbrk, known_pagebreaks)) |
| 745 | rt = true; |
| 746 | c = c->GetNext(); |
| 747 | } |
| 748 | if (rt) |
| 749 | *pagebreak = pbrk + m_PosY; |
| 750 | return rt; |
| 751 | } |
| 752 | |
| 753 | |
| 754 | void wxHtmlContainerCell::Layout(int w) |
| 755 | { |
| 756 | wxHtmlCell::Layout(w); |
| 757 | |
| 758 | if (m_LastLayout == w) return; |
| 759 | |
| 760 | // VS: Any attempt to layout with negative or zero width leads to hell, |
| 761 | // but we can't ignore such attempts completely, since it sometimes |
| 762 | // happen (e.g. when trying how small a table can be). The best thing we |
| 763 | // can do is to set the width of child cells to zero |
| 764 | if (w < 1) |
| 765 | { |
| 766 | m_Width = 0; |
| 767 | for (wxHtmlCell *cell = m_Cells; cell; cell = cell->GetNext()) |
| 768 | cell->Layout(0); |
| 769 | // this does two things: it recursively calls this code on all |
| 770 | // child contrainers and resets children's position to (0,0) |
| 771 | return; |
| 772 | } |
| 773 | |
| 774 | wxHtmlCell *nextCell; |
| 775 | long xpos = 0, ypos = m_IndentTop; |
| 776 | int xdelta = 0, ybasicpos = 0, ydiff; |
| 777 | int s_width, nextWordWidth, s_indent; |
| 778 | int ysizeup = 0, ysizedown = 0; |
| 779 | int MaxLineWidth = 0; |
| 780 | int curLineWidth = 0; |
| 781 | m_MaxTotalWidth = 0; |
| 782 | |
| 783 | |
| 784 | /* |
| 785 | |
| 786 | WIDTH ADJUSTING : |
| 787 | |
| 788 | */ |
| 789 | |
| 790 | if (m_WidthFloatUnits == wxHTML_UNITS_PERCENT) |
| 791 | { |
| 792 | if (m_WidthFloat < 0) m_Width = (100 + m_WidthFloat) * w / 100; |
| 793 | else m_Width = m_WidthFloat * w / 100; |
| 794 | } |
| 795 | else |
| 796 | { |
| 797 | if (m_WidthFloat < 0) m_Width = w + m_WidthFloat; |
| 798 | else m_Width = m_WidthFloat; |
| 799 | } |
| 800 | |
| 801 | if (m_Cells) |
| 802 | { |
| 803 | int l = (m_IndentLeft < 0) ? (-m_IndentLeft * m_Width / 100) : m_IndentLeft; |
| 804 | int r = (m_IndentRight < 0) ? (-m_IndentRight * m_Width / 100) : m_IndentRight; |
| 805 | for (wxHtmlCell *cell = m_Cells; cell; cell = cell->GetNext()) |
| 806 | cell->Layout(m_Width - (l + r)); |
| 807 | } |
| 808 | |
| 809 | /* |
| 810 | |
| 811 | LAYOUTING : |
| 812 | |
| 813 | */ |
| 814 | |
| 815 | // adjust indentation: |
| 816 | s_indent = (m_IndentLeft < 0) ? (-m_IndentLeft * m_Width / 100) : m_IndentLeft; |
| 817 | s_width = m_Width - s_indent - ((m_IndentRight < 0) ? (-m_IndentRight * m_Width / 100) : m_IndentRight); |
| 818 | |
| 819 | // my own layouting: |
| 820 | wxHtmlCell *cell = m_Cells, |
| 821 | *line = m_Cells; |
| 822 | while (cell != NULL) |
| 823 | { |
| 824 | switch (m_AlignVer) |
| 825 | { |
| 826 | case wxHTML_ALIGN_TOP : ybasicpos = 0; break; |
| 827 | case wxHTML_ALIGN_BOTTOM : ybasicpos = - cell->GetHeight(); break; |
| 828 | case wxHTML_ALIGN_CENTER : ybasicpos = - cell->GetHeight() / 2; break; |
| 829 | } |
| 830 | ydiff = cell->GetHeight() + ybasicpos; |
| 831 | |
| 832 | if (cell->GetDescent() + ydiff > ysizedown) ysizedown = cell->GetDescent() + ydiff; |
| 833 | if (ybasicpos + cell->GetDescent() < -ysizeup) ysizeup = - (ybasicpos + cell->GetDescent()); |
| 834 | |
| 835 | // layout nonbreakable run of cells: |
| 836 | cell->SetPos(xpos, ybasicpos + cell->GetDescent()); |
| 837 | xpos += cell->GetWidth(); |
| 838 | if (!cell->IsTerminalCell()) |
| 839 | { |
| 840 | // Container cell indicates new line |
| 841 | if (curLineWidth > m_MaxTotalWidth) |
| 842 | m_MaxTotalWidth = curLineWidth; |
| 843 | |
| 844 | if (wxMax(cell->GetWidth(), cell->GetMaxTotalWidth()) > m_MaxTotalWidth) |
| 845 | m_MaxTotalWidth = cell->GetMaxTotalWidth(); |
| 846 | curLineWidth = 0; |
| 847 | } |
| 848 | else |
| 849 | // Normal cell, add maximum cell width to line width |
| 850 | curLineWidth += cell->GetMaxTotalWidth(); |
| 851 | |
| 852 | cell = cell->GetNext(); |
| 853 | |
| 854 | // compute length of the next word that would be added: |
| 855 | nextWordWidth = 0; |
| 856 | if (cell) |
| 857 | { |
| 858 | nextCell = cell; |
| 859 | do |
| 860 | { |
| 861 | nextWordWidth += nextCell->GetWidth(); |
| 862 | nextCell = nextCell->GetNext(); |
| 863 | } while (nextCell && !nextCell->IsLinebreakAllowed()); |
| 864 | } |
| 865 | |
| 866 | // force new line if occurred: |
| 867 | if ((cell == NULL) || |
| 868 | (xpos + nextWordWidth > s_width && cell->IsLinebreakAllowed())) |
| 869 | { |
| 870 | if (xpos > MaxLineWidth) MaxLineWidth = xpos; |
| 871 | if (ysizeup < 0) ysizeup = 0; |
| 872 | if (ysizedown < 0) ysizedown = 0; |
| 873 | switch (m_AlignHor) { |
| 874 | case wxHTML_ALIGN_LEFT : |
| 875 | case wxHTML_ALIGN_JUSTIFY : |
| 876 | xdelta = 0; |
| 877 | break; |
| 878 | case wxHTML_ALIGN_RIGHT : |
| 879 | xdelta = 0 + (s_width - xpos); |
| 880 | break; |
| 881 | case wxHTML_ALIGN_CENTER : |
| 882 | xdelta = 0 + (s_width - xpos) / 2; |
| 883 | break; |
| 884 | } |
| 885 | if (xdelta < 0) xdelta = 0; |
| 886 | xdelta += s_indent; |
| 887 | |
| 888 | ypos += ysizeup; |
| 889 | |
| 890 | if (m_AlignHor != wxHTML_ALIGN_JUSTIFY || cell == NULL) |
| 891 | { |
| 892 | while (line != cell) |
| 893 | { |
| 894 | line->SetPos(line->GetPosX() + xdelta, |
| 895 | ypos + line->GetPosY()); |
| 896 | line = line->GetNext(); |
| 897 | } |
| 898 | } |
| 899 | else // align == justify |
| 900 | { |
| 901 | // we have to distribute the extra horz space between the cells |
| 902 | // on this line |
| 903 | |
| 904 | // an added complication is that some cells have fixed size and |
| 905 | // shouldn't get any increment (it so happens that these cells |
| 906 | // also don't allow line break on them which provides with an |
| 907 | // easy way to test for this) -- and neither should the cells |
| 908 | // adjacent to them as this could result in a visible space |
| 909 | // between two cells separated by, e.g. font change, cell which |
| 910 | // is wrong |
| 911 | |
| 912 | int step = s_width - xpos; |
| 913 | if ( step > 0 ) |
| 914 | { |
| 915 | // first count the cells which will get extra space |
| 916 | int total = -1; |
| 917 | |
| 918 | const wxHtmlCell *c; |
| 919 | if ( line != cell ) |
| 920 | { |
| 921 | for ( c = line; c != cell; c = c->GetNext() ) |
| 922 | { |
| 923 | if ( c->IsLinebreakAllowed() ) |
| 924 | { |
| 925 | total++; |
| 926 | } |
| 927 | } |
| 928 | } |
| 929 | |
| 930 | // and now extra space to those cells which merit it |
| 931 | if ( total ) |
| 932 | { |
| 933 | // first visible cell on line is not moved: |
| 934 | while (line !=cell && !line->IsLinebreakAllowed()) |
| 935 | { |
| 936 | line->SetPos(line->GetPosX() + s_indent, |
| 937 | line->GetPosY() + ypos); |
| 938 | line = line->GetNext(); |
| 939 | } |
| 940 | |
| 941 | if (line != cell) |
| 942 | { |
| 943 | line->SetPos(line->GetPosX() + s_indent, |
| 944 | line->GetPosY() + ypos); |
| 945 | |
| 946 | line = line->GetNext(); |
| 947 | } |
| 948 | |
| 949 | for ( int n = 0; line != cell; line = line->GetNext() ) |
| 950 | { |
| 951 | if ( line->IsLinebreakAllowed() ) |
| 952 | { |
| 953 | // offset the next cell relative to this one |
| 954 | // thus increasing our size |
| 955 | n++; |
| 956 | } |
| 957 | |
| 958 | line->SetPos(line->GetPosX() + s_indent + |
| 959 | ((n * step) / total), |
| 960 | line->GetPosY() + ypos); |
| 961 | } |
| 962 | } |
| 963 | else |
| 964 | { |
| 965 | // this will cause the code to enter "else branch" below: |
| 966 | step = 0; |
| 967 | } |
| 968 | } |
| 969 | // else branch: |
| 970 | if ( step <= 0 ) // no extra space to distribute |
| 971 | { |
| 972 | // just set the indent properly |
| 973 | while (line != cell) |
| 974 | { |
| 975 | line->SetPos(line->GetPosX() + s_indent, |
| 976 | line->GetPosY() + ypos); |
| 977 | line = line->GetNext(); |
| 978 | } |
| 979 | } |
| 980 | } |
| 981 | |
| 982 | ypos += ysizedown; |
| 983 | xpos = 0; |
| 984 | ysizeup = ysizedown = 0; |
| 985 | line = cell; |
| 986 | } |
| 987 | } |
| 988 | |
| 989 | // setup height & width, depending on container layout: |
| 990 | m_Height = ypos + (ysizedown + ysizeup) + m_IndentBottom; |
| 991 | |
| 992 | if (m_Height < m_MinHeight) |
| 993 | { |
| 994 | if (m_MinHeightAlign != wxHTML_ALIGN_TOP) |
| 995 | { |
| 996 | int diff = m_MinHeight - m_Height; |
| 997 | if (m_MinHeightAlign == wxHTML_ALIGN_CENTER) diff /= 2; |
| 998 | cell = m_Cells; |
| 999 | while (cell) |
| 1000 | { |
| 1001 | cell->SetPos(cell->GetPosX(), cell->GetPosY() + diff); |
| 1002 | cell = cell->GetNext(); |
| 1003 | } |
| 1004 | } |
| 1005 | m_Height = m_MinHeight; |
| 1006 | } |
| 1007 | |
| 1008 | if (curLineWidth > m_MaxTotalWidth) |
| 1009 | m_MaxTotalWidth = curLineWidth; |
| 1010 | |
| 1011 | m_MaxTotalWidth += s_indent + ((m_IndentRight < 0) ? (-m_IndentRight * m_Width / 100) : m_IndentRight); |
| 1012 | MaxLineWidth += s_indent + ((m_IndentRight < 0) ? (-m_IndentRight * m_Width / 100) : m_IndentRight); |
| 1013 | if (m_Width < MaxLineWidth) m_Width = MaxLineWidth; |
| 1014 | |
| 1015 | m_LastLayout = w; |
| 1016 | } |
| 1017 | |
| 1018 | void wxHtmlContainerCell::UpdateRenderingStatePre(wxHtmlRenderingInfo& info, |
| 1019 | wxHtmlCell *cell) const |
| 1020 | { |
| 1021 | wxHtmlSelection *s = info.GetSelection(); |
| 1022 | if (!s) return; |
| 1023 | if (s->GetFromCell() == cell || s->GetToCell() == cell) |
| 1024 | { |
| 1025 | info.GetState().SetSelectionState(wxHTML_SEL_CHANGING); |
| 1026 | } |
| 1027 | } |
| 1028 | |
| 1029 | void wxHtmlContainerCell::UpdateRenderingStatePost(wxHtmlRenderingInfo& info, |
| 1030 | wxHtmlCell *cell) const |
| 1031 | { |
| 1032 | wxHtmlSelection *s = info.GetSelection(); |
| 1033 | if (!s) return; |
| 1034 | if (s->GetToCell() == cell) |
| 1035 | info.GetState().SetSelectionState(wxHTML_SEL_OUT); |
| 1036 | else if (s->GetFromCell() == cell) |
| 1037 | info.GetState().SetSelectionState(wxHTML_SEL_IN); |
| 1038 | } |
| 1039 | |
| 1040 | #define mMin(a, b) (((a) < (b)) ? (a) : (b)) |
| 1041 | #define mMax(a, b) (((a) < (b)) ? (b) : (a)) |
| 1042 | |
| 1043 | void wxHtmlContainerCell::Draw(wxDC& dc, int x, int y, int view_y1, int view_y2, |
| 1044 | wxHtmlRenderingInfo& info) |
| 1045 | { |
| 1046 | #if 0 // useful for debugging |
| 1047 | dc.SetPen(*wxRED_PEN); |
| 1048 | dc.DrawRectangle(x+m_PosX,y+m_PosY,m_Width,m_Height); |
| 1049 | #endif |
| 1050 | |
| 1051 | int xlocal = x + m_PosX; |
| 1052 | int ylocal = y + m_PosY; |
| 1053 | |
| 1054 | if (m_UseBkColour) |
| 1055 | { |
| 1056 | wxBrush myb = wxBrush(m_BkColour, wxSOLID); |
| 1057 | |
| 1058 | int real_y1 = mMax(ylocal, view_y1); |
| 1059 | int real_y2 = mMin(ylocal + m_Height - 1, view_y2); |
| 1060 | |
| 1061 | dc.SetBrush(myb); |
| 1062 | dc.SetPen(*wxTRANSPARENT_PEN); |
| 1063 | dc.DrawRectangle(xlocal, real_y1, m_Width, real_y2 - real_y1 + 1); |
| 1064 | } |
| 1065 | |
| 1066 | if (m_UseBorder) |
| 1067 | { |
| 1068 | wxPen mypen1(m_BorderColour1, 1, wxSOLID); |
| 1069 | wxPen mypen2(m_BorderColour2, 1, wxSOLID); |
| 1070 | |
| 1071 | dc.SetPen(mypen1); |
| 1072 | dc.DrawLine(xlocal, ylocal, xlocal, ylocal + m_Height - 1); |
| 1073 | dc.DrawLine(xlocal, ylocal, xlocal + m_Width, ylocal); |
| 1074 | dc.SetPen(mypen2); |
| 1075 | dc.DrawLine(xlocal + m_Width - 1, ylocal, xlocal + m_Width - 1, ylocal + m_Height - 1); |
| 1076 | dc.DrawLine(xlocal, ylocal + m_Height - 1, xlocal + m_Width, ylocal + m_Height - 1); |
| 1077 | } |
| 1078 | |
| 1079 | if (m_Cells) |
| 1080 | { |
| 1081 | // draw container's contents: |
| 1082 | for (wxHtmlCell *cell = m_Cells; cell; cell = cell->GetNext()) |
| 1083 | { |
| 1084 | |
| 1085 | // optimize drawing: don't render off-screen content: |
| 1086 | if ((ylocal + cell->GetPosY() <= view_y2) && |
| 1087 | (ylocal + cell->GetPosY() + cell->GetHeight() > view_y1)) |
| 1088 | { |
| 1089 | // the cell is visible, draw it: |
| 1090 | UpdateRenderingStatePre(info, cell); |
| 1091 | cell->Draw(dc, |
| 1092 | xlocal, ylocal, view_y1, view_y2, |
| 1093 | info); |
| 1094 | UpdateRenderingStatePost(info, cell); |
| 1095 | } |
| 1096 | else |
| 1097 | { |
| 1098 | // the cell is off-screen, proceed with font+color+etc. |
| 1099 | // changes only: |
| 1100 | cell->DrawInvisible(dc, xlocal, ylocal, info); |
| 1101 | } |
| 1102 | } |
| 1103 | } |
| 1104 | } |
| 1105 | |
| 1106 | |
| 1107 | |
| 1108 | void wxHtmlContainerCell::DrawInvisible(wxDC& dc, int x, int y, |
| 1109 | wxHtmlRenderingInfo& info) |
| 1110 | { |
| 1111 | if (m_Cells) |
| 1112 | { |
| 1113 | for (wxHtmlCell *cell = m_Cells; cell; cell = cell->GetNext()) |
| 1114 | { |
| 1115 | UpdateRenderingStatePre(info, cell); |
| 1116 | cell->DrawInvisible(dc, x + m_PosX, y + m_PosY, info); |
| 1117 | UpdateRenderingStatePost(info, cell); |
| 1118 | } |
| 1119 | } |
| 1120 | } |
| 1121 | |
| 1122 | |
| 1123 | wxColour wxHtmlContainerCell::GetBackgroundColour() |
| 1124 | { |
| 1125 | if (m_UseBkColour) |
| 1126 | return m_BkColour; |
| 1127 | else |
| 1128 | return wxNullColour; |
| 1129 | } |
| 1130 | |
| 1131 | |
| 1132 | |
| 1133 | wxHtmlLinkInfo *wxHtmlContainerCell::GetLink(int x, int y) const |
| 1134 | { |
| 1135 | wxHtmlCell *cell = FindCellByPos(x, y); |
| 1136 | |
| 1137 | // VZ: I don't know if we should pass absolute or relative coords to |
| 1138 | // wxHtmlCell::GetLink()? As the base class version just ignores them |
| 1139 | // anyhow, it hardly matters right now but should still be clarified |
| 1140 | return cell ? cell->GetLink(x, y) : NULL; |
| 1141 | } |
| 1142 | |
| 1143 | |
| 1144 | |
| 1145 | void wxHtmlContainerCell::InsertCell(wxHtmlCell *f) |
| 1146 | { |
| 1147 | if (!m_Cells) m_Cells = m_LastCell = f; |
| 1148 | else |
| 1149 | { |
| 1150 | m_LastCell->SetNext(f); |
| 1151 | m_LastCell = f; |
| 1152 | if (m_LastCell) while (m_LastCell->GetNext()) m_LastCell = m_LastCell->GetNext(); |
| 1153 | } |
| 1154 | f->SetParent(this); |
| 1155 | m_LastLayout = -1; |
| 1156 | } |
| 1157 | |
| 1158 | |
| 1159 | |
| 1160 | void wxHtmlContainerCell::SetAlign(const wxHtmlTag& tag) |
| 1161 | { |
| 1162 | if (tag.HasParam(wxT("ALIGN"))) |
| 1163 | { |
| 1164 | wxString alg = tag.GetParam(wxT("ALIGN")); |
| 1165 | alg.MakeUpper(); |
| 1166 | if (alg == wxT("CENTER")) |
| 1167 | SetAlignHor(wxHTML_ALIGN_CENTER); |
| 1168 | else if (alg == wxT("LEFT")) |
| 1169 | SetAlignHor(wxHTML_ALIGN_LEFT); |
| 1170 | else if (alg == wxT("JUSTIFY")) |
| 1171 | SetAlignHor(wxHTML_ALIGN_JUSTIFY); |
| 1172 | else if (alg == wxT("RIGHT")) |
| 1173 | SetAlignHor(wxHTML_ALIGN_RIGHT); |
| 1174 | m_LastLayout = -1; |
| 1175 | } |
| 1176 | } |
| 1177 | |
| 1178 | |
| 1179 | |
| 1180 | void wxHtmlContainerCell::SetWidthFloat(const wxHtmlTag& tag, double pixel_scale) |
| 1181 | { |
| 1182 | if (tag.HasParam(wxT("WIDTH"))) |
| 1183 | { |
| 1184 | int wdi; |
| 1185 | wxString wd = tag.GetParam(wxT("WIDTH")); |
| 1186 | |
| 1187 | if (wd[wd.length()-1] == wxT('%')) |
| 1188 | { |
| 1189 | wxSscanf(wd.c_str(), wxT("%i%%"), &wdi); |
| 1190 | SetWidthFloat(wdi, wxHTML_UNITS_PERCENT); |
| 1191 | } |
| 1192 | else |
| 1193 | { |
| 1194 | wxSscanf(wd.c_str(), wxT("%i"), &wdi); |
| 1195 | SetWidthFloat((int)(pixel_scale * (double)wdi), wxHTML_UNITS_PIXELS); |
| 1196 | } |
| 1197 | m_LastLayout = -1; |
| 1198 | } |
| 1199 | } |
| 1200 | |
| 1201 | |
| 1202 | |
| 1203 | const wxHtmlCell* wxHtmlContainerCell::Find(int condition, const void* param) const |
| 1204 | { |
| 1205 | if (m_Cells) |
| 1206 | { |
| 1207 | for (wxHtmlCell *cell = m_Cells; cell; cell = cell->GetNext()) |
| 1208 | { |
| 1209 | const wxHtmlCell *r = cell->Find(condition, param); |
| 1210 | if (r) return r; |
| 1211 | } |
| 1212 | } |
| 1213 | return NULL; |
| 1214 | } |
| 1215 | |
| 1216 | |
| 1217 | wxHtmlCell *wxHtmlContainerCell::FindCellByPos(wxCoord x, wxCoord y, |
| 1218 | unsigned flags) const |
| 1219 | { |
| 1220 | if ( flags & wxHTML_FIND_EXACT ) |
| 1221 | { |
| 1222 | for ( const wxHtmlCell *cell = m_Cells; cell; cell = cell->GetNext() ) |
| 1223 | { |
| 1224 | int cx = cell->GetPosX(), |
| 1225 | cy = cell->GetPosY(); |
| 1226 | |
| 1227 | if ( (cx <= x) && (cx + cell->GetWidth() > x) && |
| 1228 | (cy <= y) && (cy + cell->GetHeight() > y) ) |
| 1229 | { |
| 1230 | return cell->FindCellByPos(x - cx, y - cy, flags); |
| 1231 | } |
| 1232 | } |
| 1233 | } |
| 1234 | else if ( flags & wxHTML_FIND_NEAREST_AFTER ) |
| 1235 | { |
| 1236 | wxHtmlCell *c; |
| 1237 | for ( const wxHtmlCell *cell = m_Cells; cell; cell = cell->GetNext() ) |
| 1238 | { |
| 1239 | if ( cell->IsFormattingCell() ) |
| 1240 | continue; |
| 1241 | int cellY = cell->GetPosY(); |
| 1242 | if (!( y < cellY || (y < cellY + cell->GetHeight() && |
| 1243 | x < cell->GetPosX() + cell->GetWidth()) )) |
| 1244 | continue; |
| 1245 | |
| 1246 | c = cell->FindCellByPos(x - cell->GetPosX(), y - cellY, flags); |
| 1247 | if (c) return c; |
| 1248 | } |
| 1249 | } |
| 1250 | else if ( flags & wxHTML_FIND_NEAREST_BEFORE ) |
| 1251 | { |
| 1252 | wxHtmlCell *c2, *c = NULL; |
| 1253 | for ( const wxHtmlCell *cell = m_Cells; cell; cell = cell->GetNext() ) |
| 1254 | { |
| 1255 | if ( cell->IsFormattingCell() ) |
| 1256 | continue; |
| 1257 | int cellY = cell->GetPosY(); |
| 1258 | if (!( cellY + cell->GetHeight() <= y || |
| 1259 | (y >= cellY && x >= cell->GetPosX()) )) |
| 1260 | break; |
| 1261 | c2 = cell->FindCellByPos(x - cell->GetPosX(), y - cellY, flags); |
| 1262 | if (c2) |
| 1263 | c = c2; |
| 1264 | } |
| 1265 | if (c) return c; |
| 1266 | } |
| 1267 | |
| 1268 | return NULL; |
| 1269 | } |
| 1270 | |
| 1271 | |
| 1272 | bool wxHtmlContainerCell::ProcessMouseClick(wxHtmlWindowInterface *window, |
| 1273 | const wxPoint& pos, |
| 1274 | const wxMouseEvent& event) |
| 1275 | { |
| 1276 | #if WXWIN_COMPATIBILITY_2_6 |
| 1277 | wxHtmlCellOnMouseClickCompatHelper compat(window, pos, event); |
| 1278 | return compat.CallOnMouseClick(this); |
| 1279 | } |
| 1280 | |
| 1281 | void wxHtmlContainerCell::OnMouseClick(wxWindow*, |
| 1282 | int, int, const wxMouseEvent& event) |
| 1283 | { |
| 1284 | wxCHECK_RET( gs_helperOnMouseClick, _T("unexpected call to OnMouseClick") ); |
| 1285 | wxHtmlWindowInterface *window = gs_helperOnMouseClick->window; |
| 1286 | const wxPoint& pos = gs_helperOnMouseClick->pos; |
| 1287 | #endif // WXWIN_COMPATIBILITY_2_6 |
| 1288 | |
| 1289 | bool retval = false; |
| 1290 | wxHtmlCell *cell = FindCellByPos(pos.x, pos.y); |
| 1291 | if ( cell ) |
| 1292 | retval = cell->ProcessMouseClick(window, pos, event); |
| 1293 | |
| 1294 | #if WXWIN_COMPATIBILITY_2_6 |
| 1295 | gs_helperOnMouseClick->retval = retval; |
| 1296 | #else |
| 1297 | return retval; |
| 1298 | #endif // WXWIN_COMPATIBILITY_2_6 |
| 1299 | } |
| 1300 | |
| 1301 | |
| 1302 | wxHtmlCell *wxHtmlContainerCell::GetFirstTerminal() const |
| 1303 | { |
| 1304 | if ( m_Cells ) |
| 1305 | { |
| 1306 | wxHtmlCell *c2; |
| 1307 | for (wxHtmlCell *c = m_Cells; c; c = c->GetNext()) |
| 1308 | { |
| 1309 | c2 = c->GetFirstTerminal(); |
| 1310 | if ( c2 ) |
| 1311 | return c2; |
| 1312 | } |
| 1313 | } |
| 1314 | return NULL; |
| 1315 | } |
| 1316 | |
| 1317 | wxHtmlCell *wxHtmlContainerCell::GetLastTerminal() const |
| 1318 | { |
| 1319 | if ( m_Cells ) |
| 1320 | { |
| 1321 | // most common case first: |
| 1322 | wxHtmlCell *c = m_LastCell->GetLastTerminal(); |
| 1323 | if ( c ) |
| 1324 | return c; |
| 1325 | |
| 1326 | wxHtmlCell *ctmp; |
| 1327 | wxHtmlCell *c2 = NULL; |
| 1328 | for (c = m_Cells; c; c = c->GetNext()) |
| 1329 | { |
| 1330 | ctmp = c->GetLastTerminal(); |
| 1331 | if ( ctmp ) |
| 1332 | c2 = ctmp; |
| 1333 | } |
| 1334 | return c2; |
| 1335 | } |
| 1336 | else |
| 1337 | return NULL; |
| 1338 | } |
| 1339 | |
| 1340 | |
| 1341 | static bool IsEmptyContainer(wxHtmlContainerCell *cell) |
| 1342 | { |
| 1343 | for ( wxHtmlCell *c = cell->GetFirstChild(); c; c = c->GetNext() ) |
| 1344 | { |
| 1345 | if ( !c->IsTerminalCell() || !c->IsFormattingCell() ) |
| 1346 | return false; |
| 1347 | } |
| 1348 | return true; |
| 1349 | } |
| 1350 | |
| 1351 | void wxHtmlContainerCell::RemoveExtraSpacing(bool top, bool bottom) |
| 1352 | { |
| 1353 | if ( top ) |
| 1354 | SetIndent(0, wxHTML_INDENT_TOP); |
| 1355 | if ( bottom ) |
| 1356 | SetIndent(0, wxHTML_INDENT_BOTTOM); |
| 1357 | |
| 1358 | if ( m_Cells ) |
| 1359 | { |
| 1360 | wxHtmlCell *c; |
| 1361 | wxHtmlContainerCell *cont; |
| 1362 | if ( top ) |
| 1363 | { |
| 1364 | for ( c = m_Cells; c; c = c->GetNext() ) |
| 1365 | { |
| 1366 | if ( c->IsTerminalCell() ) |
| 1367 | { |
| 1368 | if ( !c->IsFormattingCell() ) |
| 1369 | break; |
| 1370 | } |
| 1371 | else |
| 1372 | { |
| 1373 | cont = (wxHtmlContainerCell*)c; |
| 1374 | if ( IsEmptyContainer(cont) ) |
| 1375 | { |
| 1376 | cont->SetIndent(0, wxHTML_INDENT_VERTICAL); |
| 1377 | } |
| 1378 | else |
| 1379 | { |
| 1380 | cont->RemoveExtraSpacing(true, false); |
| 1381 | break; |
| 1382 | } |
| 1383 | } |
| 1384 | } |
| 1385 | } |
| 1386 | |
| 1387 | if ( bottom ) |
| 1388 | { |
| 1389 | wxArrayPtrVoid arr; |
| 1390 | for ( c = m_Cells; c; c = c->GetNext() ) |
| 1391 | arr.Add((void*)c); |
| 1392 | |
| 1393 | for ( int i = arr.GetCount() - 1; i >= 0; i--) |
| 1394 | { |
| 1395 | c = (wxHtmlCell*)arr[i]; |
| 1396 | if ( c->IsTerminalCell() ) |
| 1397 | { |
| 1398 | if ( !c->IsFormattingCell() ) |
| 1399 | break; |
| 1400 | } |
| 1401 | else |
| 1402 | { |
| 1403 | cont = (wxHtmlContainerCell*)c; |
| 1404 | if ( IsEmptyContainer(cont) ) |
| 1405 | { |
| 1406 | cont->SetIndent(0, wxHTML_INDENT_VERTICAL); |
| 1407 | } |
| 1408 | else |
| 1409 | { |
| 1410 | cont->RemoveExtraSpacing(false, true); |
| 1411 | break; |
| 1412 | } |
| 1413 | } |
| 1414 | } |
| 1415 | } |
| 1416 | } |
| 1417 | } |
| 1418 | |
| 1419 | |
| 1420 | |
| 1421 | |
| 1422 | // -------------------------------------------------------------------------- |
| 1423 | // wxHtmlColourCell |
| 1424 | // -------------------------------------------------------------------------- |
| 1425 | |
| 1426 | IMPLEMENT_ABSTRACT_CLASS(wxHtmlColourCell, wxHtmlCell) |
| 1427 | |
| 1428 | void wxHtmlColourCell::Draw(wxDC& dc, |
| 1429 | int x, int y, |
| 1430 | int WXUNUSED(view_y1), int WXUNUSED(view_y2), |
| 1431 | wxHtmlRenderingInfo& info) |
| 1432 | { |
| 1433 | DrawInvisible(dc, x, y, info); |
| 1434 | } |
| 1435 | |
| 1436 | void wxHtmlColourCell::DrawInvisible(wxDC& dc, |
| 1437 | int WXUNUSED(x), int WXUNUSED(y), |
| 1438 | wxHtmlRenderingInfo& info) |
| 1439 | { |
| 1440 | wxHtmlRenderingState& state = info.GetState(); |
| 1441 | if (m_Flags & wxHTML_CLR_FOREGROUND) |
| 1442 | { |
| 1443 | state.SetFgColour(m_Colour); |
| 1444 | if (state.GetSelectionState() != wxHTML_SEL_IN) |
| 1445 | dc.SetTextForeground(m_Colour); |
| 1446 | else |
| 1447 | dc.SetTextForeground( |
| 1448 | info.GetStyle().GetSelectedTextColour(m_Colour)); |
| 1449 | } |
| 1450 | if (m_Flags & wxHTML_CLR_BACKGROUND) |
| 1451 | { |
| 1452 | state.SetBgColour(m_Colour); |
| 1453 | if (state.GetSelectionState() != wxHTML_SEL_IN) |
| 1454 | { |
| 1455 | dc.SetTextBackground(m_Colour); |
| 1456 | dc.SetBackground(wxBrush(m_Colour, wxSOLID)); |
| 1457 | } |
| 1458 | else |
| 1459 | { |
| 1460 | wxColour c = info.GetStyle().GetSelectedTextBgColour(m_Colour); |
| 1461 | dc.SetTextBackground(c); |
| 1462 | dc.SetBackground(wxBrush(c, wxSOLID)); |
| 1463 | } |
| 1464 | } |
| 1465 | } |
| 1466 | |
| 1467 | |
| 1468 | |
| 1469 | |
| 1470 | // --------------------------------------------------------------------------- |
| 1471 | // wxHtmlFontCell |
| 1472 | // --------------------------------------------------------------------------- |
| 1473 | |
| 1474 | IMPLEMENT_ABSTRACT_CLASS(wxHtmlFontCell, wxHtmlCell) |
| 1475 | |
| 1476 | void wxHtmlFontCell::Draw(wxDC& dc, |
| 1477 | int WXUNUSED(x), int WXUNUSED(y), |
| 1478 | int WXUNUSED(view_y1), int WXUNUSED(view_y2), |
| 1479 | wxHtmlRenderingInfo& WXUNUSED(info)) |
| 1480 | { |
| 1481 | dc.SetFont(m_Font); |
| 1482 | } |
| 1483 | |
| 1484 | void wxHtmlFontCell::DrawInvisible(wxDC& dc, int WXUNUSED(x), int WXUNUSED(y), |
| 1485 | wxHtmlRenderingInfo& WXUNUSED(info)) |
| 1486 | { |
| 1487 | dc.SetFont(m_Font); |
| 1488 | } |
| 1489 | |
| 1490 | |
| 1491 | |
| 1492 | |
| 1493 | |
| 1494 | |
| 1495 | |
| 1496 | |
| 1497 | // --------------------------------------------------------------------------- |
| 1498 | // wxHtmlWidgetCell |
| 1499 | // --------------------------------------------------------------------------- |
| 1500 | |
| 1501 | IMPLEMENT_ABSTRACT_CLASS(wxHtmlWidgetCell, wxHtmlCell) |
| 1502 | |
| 1503 | wxHtmlWidgetCell::wxHtmlWidgetCell(wxWindow *wnd, int w) |
| 1504 | { |
| 1505 | int sx, sy; |
| 1506 | m_Wnd = wnd; |
| 1507 | m_Wnd->GetSize(&sx, &sy); |
| 1508 | m_Width = sx, m_Height = sy; |
| 1509 | m_WidthFloat = w; |
| 1510 | } |
| 1511 | |
| 1512 | |
| 1513 | void wxHtmlWidgetCell::Draw(wxDC& WXUNUSED(dc), |
| 1514 | int WXUNUSED(x), int WXUNUSED(y), |
| 1515 | int WXUNUSED(view_y1), int WXUNUSED(view_y2), |
| 1516 | wxHtmlRenderingInfo& WXUNUSED(info)) |
| 1517 | { |
| 1518 | int absx = 0, absy = 0, stx, sty; |
| 1519 | wxHtmlCell *c = this; |
| 1520 | |
| 1521 | while (c) |
| 1522 | { |
| 1523 | absx += c->GetPosX(); |
| 1524 | absy += c->GetPosY(); |
| 1525 | c = c->GetParent(); |
| 1526 | } |
| 1527 | |
| 1528 | wxScrolledWindow *scrolwin = |
| 1529 | wxDynamicCast(m_Wnd->GetParent(), wxScrolledWindow); |
| 1530 | wxCHECK_RET( scrolwin, |
| 1531 | _T("widget cells can only be placed in wxHtmlWindow") ); |
| 1532 | |
| 1533 | scrolwin->GetViewStart(&stx, &sty); |
| 1534 | m_Wnd->SetSize(absx - wxHTML_SCROLL_STEP * stx, |
| 1535 | absy - wxHTML_SCROLL_STEP * sty, |
| 1536 | m_Width, m_Height); |
| 1537 | } |
| 1538 | |
| 1539 | |
| 1540 | |
| 1541 | void wxHtmlWidgetCell::DrawInvisible(wxDC& WXUNUSED(dc), |
| 1542 | int WXUNUSED(x), int WXUNUSED(y), |
| 1543 | wxHtmlRenderingInfo& WXUNUSED(info)) |
| 1544 | { |
| 1545 | int absx = 0, absy = 0, stx, sty; |
| 1546 | wxHtmlCell *c = this; |
| 1547 | |
| 1548 | while (c) |
| 1549 | { |
| 1550 | absx += c->GetPosX(); |
| 1551 | absy += c->GetPosY(); |
| 1552 | c = c->GetParent(); |
| 1553 | } |
| 1554 | |
| 1555 | ((wxScrolledWindow*)(m_Wnd->GetParent()))->GetViewStart(&stx, &sty); |
| 1556 | m_Wnd->SetSize(absx - wxHTML_SCROLL_STEP * stx, absy - wxHTML_SCROLL_STEP * sty, m_Width, m_Height); |
| 1557 | } |
| 1558 | |
| 1559 | |
| 1560 | |
| 1561 | void wxHtmlWidgetCell::Layout(int w) |
| 1562 | { |
| 1563 | if (m_WidthFloat != 0) |
| 1564 | { |
| 1565 | m_Width = (w * m_WidthFloat) / 100; |
| 1566 | m_Wnd->SetSize(m_Width, m_Height); |
| 1567 | } |
| 1568 | |
| 1569 | wxHtmlCell::Layout(w); |
| 1570 | } |
| 1571 | |
| 1572 | |
| 1573 | |
| 1574 | // ---------------------------------------------------------------------------- |
| 1575 | // wxHtmlTerminalCellsInterator |
| 1576 | // ---------------------------------------------------------------------------- |
| 1577 | |
| 1578 | const wxHtmlCell* wxHtmlTerminalCellsInterator::operator++() |
| 1579 | { |
| 1580 | if ( !m_pos ) |
| 1581 | return NULL; |
| 1582 | |
| 1583 | do |
| 1584 | { |
| 1585 | if ( m_pos == m_to ) |
| 1586 | { |
| 1587 | m_pos = NULL; |
| 1588 | return NULL; |
| 1589 | } |
| 1590 | |
| 1591 | if ( m_pos->GetNext() ) |
| 1592 | m_pos = m_pos->GetNext(); |
| 1593 | else |
| 1594 | { |
| 1595 | // we must go up the hierarchy until we reach container where this |
| 1596 | // is not the last child, and then go down to first terminal cell: |
| 1597 | while ( m_pos->GetNext() == NULL ) |
| 1598 | { |
| 1599 | m_pos = m_pos->GetParent(); |
| 1600 | if ( !m_pos ) |
| 1601 | return NULL; |
| 1602 | } |
| 1603 | m_pos = m_pos->GetNext(); |
| 1604 | } |
| 1605 | while ( m_pos->GetFirstChild() != NULL ) |
| 1606 | m_pos = m_pos->GetFirstChild(); |
| 1607 | } while ( !m_pos->IsTerminalCell() ); |
| 1608 | |
| 1609 | return m_pos; |
| 1610 | } |
| 1611 | |
| 1612 | #endif |