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