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