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