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