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