compilation fix
[wxWidgets.git] / src / html / htmlcell.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: htmlcell.cpp
3 // Purpose: wxHtmlCell - basic element of HTML output
4 // Author: Vaclav Slavik
5 // RCS-ID: $Id$
6 // Copyright: (c) 1999 Vaclav Slavik
7 // Licence: wxWindows Licence
8 /////////////////////////////////////////////////////////////////////////////
9
10 #ifdef __GNUG__
11 #pragma implementation "htmlcell.h"
12 #endif
13
14 #include "wx/wxprec.h"
15
16 #include "wx/defs.h"
17
18 #if wxUSE_HTML && wxUSE_STREAMS
19
20 #ifdef __BORLANDC__
21 #pragma hdrstop
22 #endif
23
24 #ifndef WXPRECOMP
25 #include "wx/brush.h"
26 #include "wx/colour.h"
27 #include "wx/dc.h"
28 #endif
29
30 #include "wx/html/htmlcell.h"
31 #include "wx/html/htmlwin.h"
32 #include "wx/settings.h"
33 #include <stdlib.h>
34
35 //-----------------------------------------------------------------------------
36 // Helper classes
37 //-----------------------------------------------------------------------------
38
39 void wxHtmlSelection::Set(const wxPoint& fromPos, wxHtmlCell *fromCell,
40 const wxPoint& toPos, wxHtmlCell *toCell)
41 {
42 m_fromCell = fromCell;
43 m_toCell = toCell;
44 m_fromPos = fromPos;
45 m_toPos = toPos;
46 }
47
48 void wxHtmlSelection::Set(wxHtmlCell *fromCell, wxHtmlCell *toCell)
49 {
50 wxPoint p1 = fromCell ? fromCell->GetAbsPos() : wxDefaultPosition;
51 wxPoint p2 = toCell ? toCell->GetAbsPos() : wxDefaultPosition;
52 if ( toCell )
53 {
54 p2.x += toCell->GetWidth()-1;
55 p2.y += toCell->GetHeight()-1;
56 }
57 Set(p1, fromCell, p2, toCell);
58 }
59
60 //-----------------------------------------------------------------------------
61 // wxHtmlCell
62 //-----------------------------------------------------------------------------
63
64 wxHtmlCell::wxHtmlCell() : wxObject()
65 {
66 m_Next = NULL;
67 m_Parent = NULL;
68 m_Width = m_Height = m_Descent = 0;
69 m_CanLiveOnPagebreak = TRUE;
70 m_Link = NULL;
71 }
72
73 wxHtmlCell::~wxHtmlCell()
74 {
75 delete m_Link;
76 }
77
78
79 void wxHtmlCell::OnMouseClick(wxWindow *parent, int x, int y,
80 const wxMouseEvent& event)
81 {
82 wxHtmlLinkInfo *lnk = GetLink(x, y);
83 if (lnk != NULL)
84 {
85 wxHtmlLinkInfo lnk2(*lnk);
86 lnk2.SetEvent(&event);
87 lnk2.SetHtmlCell(this);
88
89 // note : this cast is legal because parent is *always* wxHtmlWindow
90 wxStaticCast(parent, wxHtmlWindow)->OnLinkClicked(lnk2);
91 }
92 }
93
94
95
96 bool wxHtmlCell::AdjustPagebreak(int *pagebreak, int* WXUNUSED(known_pagebreaks), int WXUNUSED(number_of_pages)) const
97 {
98 if ((!m_CanLiveOnPagebreak) &&
99 m_PosY < *pagebreak && m_PosY + m_Height > *pagebreak)
100 {
101 *pagebreak = m_PosY;
102 return TRUE;
103 }
104
105 return FALSE;
106 }
107
108
109
110 void wxHtmlCell::SetLink(const wxHtmlLinkInfo& link)
111 {
112 if (m_Link) delete m_Link;
113 m_Link = NULL;
114 if (link.GetHref() != wxEmptyString)
115 m_Link = new wxHtmlLinkInfo(link);
116 }
117
118
119
120 void wxHtmlCell::Layout(int WXUNUSED(w))
121 {
122 SetPos(0, 0);
123 }
124
125
126
127 void wxHtmlCell::GetHorizontalConstraints(int *left, int *right) const
128 {
129 if (left)
130 *left = m_PosX;
131 if (right)
132 *right = m_PosX + m_Width;
133 }
134
135
136
137 const wxHtmlCell* wxHtmlCell::Find(int WXUNUSED(condition), const void* WXUNUSED(param)) const
138 {
139 return NULL;
140 }
141
142
143 wxHtmlCell *wxHtmlCell::FindCellByPos(wxCoord x, wxCoord y,
144 unsigned flags) const
145 {
146 if ( x >= 0 && x < m_Width && y >= 0 && y < m_Height )
147 {
148 return wxConstCast(this, wxHtmlCell);
149 }
150 else
151 {
152 if ((flags & wxHTML_FIND_NEAREST_AFTER) &&
153 (y < 0 || (y == 0 && x <= 0)))
154 return wxConstCast(this, wxHtmlCell);
155 else if ((flags & wxHTML_FIND_NEAREST_BEFORE) &&
156 (y > m_Height-1 || (y == m_Height-1 && x >= m_Width)))
157 return wxConstCast(this, wxHtmlCell);
158 else
159 return NULL;
160 }
161 }
162
163
164 wxPoint wxHtmlCell::GetAbsPos() const
165 {
166 wxPoint p(m_PosX, m_PosY);
167 for (wxHtmlCell *parent = m_Parent; parent; parent = parent->m_Parent)
168 {
169 p.x += parent->m_PosX;
170 p.y += parent->m_PosY;
171 }
172 return p;
173 }
174
175 unsigned wxHtmlCell::GetDepth() const
176 {
177 unsigned d = 0;
178 for (wxHtmlCell *p = m_Parent; p; p = p->m_Parent)
179 d++;
180 return d;
181 }
182
183 bool wxHtmlCell::IsBefore(wxHtmlCell *cell) const
184 {
185 const wxHtmlCell *c1 = this;
186 const wxHtmlCell *c2 = cell;
187 unsigned d1 = GetDepth();
188 unsigned d2 = cell->GetDepth();
189
190 if ( d1 > d2 )
191 for (; d1 != d2; d1-- )
192 c1 = c1->m_Parent;
193 else if ( d1 < d2 )
194 for (; d1 != d2; d2-- )
195 c2 = c2->m_Parent;
196
197 if ( cell == this )
198 return true;
199
200 while ( c1 && c2 )
201 {
202 if ( c1->m_Parent == c2->m_Parent )
203 {
204 while ( c1 )
205 {
206 if ( c1 == c2 )
207 return true;
208 c1 = c1->GetNext();
209 }
210 return false;
211 }
212 else
213 {
214 c1 = c1->m_Parent;
215 c2 = c2->m_Parent;
216 }
217 }
218
219 wxFAIL_MSG(_T("Cells are in different trees"));
220 return false;
221 }
222
223
224 //-----------------------------------------------------------------------------
225 // wxHtmlWordCell
226 //-----------------------------------------------------------------------------
227
228 wxHtmlWordCell::wxHtmlWordCell(const wxString& word, wxDC& dc) : wxHtmlCell()
229 {
230 m_Word = word;
231 dc.GetTextExtent(m_Word, &m_Width, &m_Height, &m_Descent);
232 SetCanLiveOnPagebreak(FALSE);
233 }
234
235
236
237 void wxHtmlWordCell::Draw(wxDC& dc, int x, int y,
238 int WXUNUSED(view_y1), int WXUNUSED(view_y2),
239 wxHtmlRenderingState& state)
240 {
241 if (state.GetSelectionState() != wxHTML_SEL_OUT &&
242 dc.GetBackgroundMode() != wxSOLID)
243 {
244 dc.SetBackgroundMode(wxSOLID);
245 dc.SetTextBackground(
246 wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHT));
247 dc.SetTextForeground(
248 wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT));
249 }
250 else if (state.GetSelectionState() == wxHTML_SEL_OUT &&
251 dc.GetBackgroundMode() == wxSOLID)
252 {
253 dc.SetBackgroundMode(wxTRANSPARENT);
254 dc.SetTextForeground(state.GetFgColour());
255 dc.SetTextBackground(state.GetBgColour());
256 }
257
258 // FIXME - use selection
259 dc.DrawText(m_Word, x + m_PosX, y + m_PosY);
260 }
261
262
263 wxString wxHtmlWordCell::ConvertToText(wxHtmlSelection *sel) const
264 {
265 // FIXME - use selection
266 return m_Word;
267 }
268
269
270
271 //-----------------------------------------------------------------------------
272 // wxHtmlContainerCell
273 //-----------------------------------------------------------------------------
274
275
276 wxHtmlContainerCell::wxHtmlContainerCell(wxHtmlContainerCell *parent) : wxHtmlCell()
277 {
278 m_Cells = m_LastCell = NULL;
279 m_Parent = parent;
280 if (m_Parent) m_Parent->InsertCell(this);
281 m_AlignHor = wxHTML_ALIGN_LEFT;
282 m_AlignVer = wxHTML_ALIGN_BOTTOM;
283 m_IndentLeft = m_IndentRight = m_IndentTop = m_IndentBottom = 0;
284 m_WidthFloat = 100; m_WidthFloatUnits = wxHTML_UNITS_PERCENT;
285 m_UseBkColour = FALSE;
286 m_UseBorder = FALSE;
287 m_MinHeight = 0;
288 m_MinHeightAlign = wxHTML_ALIGN_TOP;
289 m_LastLayout = -1;
290 }
291
292 wxHtmlContainerCell::~wxHtmlContainerCell()
293 {
294 wxHtmlCell *cell = m_Cells;
295 while ( cell )
296 {
297 wxHtmlCell *cellNext = cell->GetNext();
298 delete cell;
299 cell = cellNext;
300 }
301 }
302
303
304
305 void wxHtmlContainerCell::SetIndent(int i, int what, int units)
306 {
307 int val = (units == wxHTML_UNITS_PIXELS) ? i : -i;
308 if (what & wxHTML_INDENT_LEFT) m_IndentLeft = val;
309 if (what & wxHTML_INDENT_RIGHT) m_IndentRight = val;
310 if (what & wxHTML_INDENT_TOP) m_IndentTop = val;
311 if (what & wxHTML_INDENT_BOTTOM) m_IndentBottom = val;
312 m_LastLayout = -1;
313 }
314
315
316
317 int wxHtmlContainerCell::GetIndent(int ind) const
318 {
319 if (ind & wxHTML_INDENT_LEFT) return m_IndentLeft;
320 else if (ind & wxHTML_INDENT_RIGHT) return m_IndentRight;
321 else if (ind & wxHTML_INDENT_TOP) return m_IndentTop;
322 else if (ind & wxHTML_INDENT_BOTTOM) return m_IndentBottom;
323 else return -1; /* BUG! Should not be called... */
324 }
325
326
327
328
329 int wxHtmlContainerCell::GetIndentUnits(int ind) const
330 {
331 bool p = FALSE;
332 if (ind & wxHTML_INDENT_LEFT) p = m_IndentLeft < 0;
333 else if (ind & wxHTML_INDENT_RIGHT) p = m_IndentRight < 0;
334 else if (ind & wxHTML_INDENT_TOP) p = m_IndentTop < 0;
335 else if (ind & wxHTML_INDENT_BOTTOM) p = m_IndentBottom < 0;
336 if (p) return wxHTML_UNITS_PERCENT;
337 else return wxHTML_UNITS_PIXELS;
338 }
339
340
341
342 bool wxHtmlContainerCell::AdjustPagebreak(int *pagebreak, int* known_pagebreaks, int number_of_pages) const
343 {
344 if (!m_CanLiveOnPagebreak)
345 return wxHtmlCell::AdjustPagebreak(pagebreak, known_pagebreaks, number_of_pages);
346
347 else
348 {
349 wxHtmlCell *c = GetFirstChild();
350 bool rt = FALSE;
351 int pbrk = *pagebreak - m_PosY;
352
353 while (c)
354 {
355 if (c->AdjustPagebreak(&pbrk, known_pagebreaks, number_of_pages))
356 rt = TRUE;
357 c = c->GetNext();
358 }
359 if (rt)
360 *pagebreak = pbrk + m_PosY;
361 return rt;
362 }
363 }
364
365
366
367 void wxHtmlContainerCell::Layout(int w)
368 {
369 wxHtmlCell::Layout(w);
370
371 if (m_LastLayout == w) return;
372
373 // VS: Any attempt to layout with negative or zero width leads to hell,
374 // but we can't ignore such attempts completely, since it sometimes
375 // happen (e.g. when trying how small a table can be). The best thing we
376 // can do is to set the width of child cells to zero
377 if (w < 1)
378 {
379 m_Width = 0;
380 for (wxHtmlCell *cell = m_Cells; cell; cell = cell->GetNext())
381 cell->Layout(0);
382 // this does two things: it recursively calls this code on all
383 // child contrainers and resets children's position to (0,0)
384 return;
385 }
386
387 wxHtmlCell *cell = m_Cells, *line = m_Cells;
388 long xpos = 0, ypos = m_IndentTop;
389 int xdelta = 0, ybasicpos = 0, ydiff;
390 int s_width, s_indent;
391 int ysizeup = 0, ysizedown = 0;
392 int MaxLineWidth = 0;
393 int xcnt = 0;
394
395
396 /*
397
398 WIDTH ADJUSTING :
399
400 */
401
402 if (m_WidthFloatUnits == wxHTML_UNITS_PERCENT)
403 {
404 if (m_WidthFloat < 0) m_Width = (100 + m_WidthFloat) * w / 100;
405 else m_Width = m_WidthFloat * w / 100;
406 }
407 else
408 {
409 if (m_WidthFloat < 0) m_Width = w + m_WidthFloat;
410 else m_Width = m_WidthFloat;
411 }
412
413 if (m_Cells)
414 {
415 int l = (m_IndentLeft < 0) ? (-m_IndentLeft * m_Width / 100) : m_IndentLeft;
416 int r = (m_IndentRight < 0) ? (-m_IndentRight * m_Width / 100) : m_IndentRight;
417 for (wxHtmlCell *cell = m_Cells; cell; cell = cell->GetNext())
418 cell->Layout(m_Width - (l + r));
419 }
420
421 /*
422
423 LAYOUTING :
424
425 */
426
427 // adjust indentation:
428 s_indent = (m_IndentLeft < 0) ? (-m_IndentLeft * m_Width / 100) : m_IndentLeft;
429 s_width = m_Width - s_indent - ((m_IndentRight < 0) ? (-m_IndentRight * m_Width / 100) : m_IndentRight);
430
431 // my own layouting:
432 while (cell != NULL)
433 {
434 switch (m_AlignVer)
435 {
436 case wxHTML_ALIGN_TOP : ybasicpos = 0; break;
437 case wxHTML_ALIGN_BOTTOM : ybasicpos = - cell->GetHeight(); break;
438 case wxHTML_ALIGN_CENTER : ybasicpos = - cell->GetHeight() / 2; break;
439 }
440 ydiff = cell->GetHeight() + ybasicpos;
441
442 if (cell->GetDescent() + ydiff > ysizedown) ysizedown = cell->GetDescent() + ydiff;
443 if (ybasicpos + cell->GetDescent() < -ysizeup) ysizeup = - (ybasicpos + cell->GetDescent());
444
445 cell->SetPos(xpos, ybasicpos + cell->GetDescent());
446 xpos += cell->GetWidth();
447 cell = cell->GetNext();
448 xcnt++;
449
450 // force new line if occured:
451 if ((cell == NULL) || (xpos + cell->GetWidth() > s_width))
452 {
453 if (xpos > MaxLineWidth) MaxLineWidth = xpos;
454 if (ysizeup < 0) ysizeup = 0;
455 if (ysizedown < 0) ysizedown = 0;
456 switch (m_AlignHor) {
457 case wxHTML_ALIGN_LEFT :
458 case wxHTML_ALIGN_JUSTIFY :
459 xdelta = 0;
460 break;
461 case wxHTML_ALIGN_RIGHT :
462 xdelta = 0 + (s_width - xpos);
463 break;
464 case wxHTML_ALIGN_CENTER :
465 xdelta = 0 + (s_width - xpos) / 2;
466 break;
467 }
468 if (xdelta < 0) xdelta = 0;
469 xdelta += s_indent;
470
471 ypos += ysizeup;
472
473 if (m_AlignHor != wxHTML_ALIGN_JUSTIFY || cell == NULL)
474 while (line != cell)
475 {
476 line->SetPos(line->GetPosX() + xdelta,
477 ypos + line->GetPosY());
478 line = line->GetNext();
479 }
480 else
481 {
482 int counter = 0;
483 int step = (s_width - xpos);
484 if (step < 0) step = 0;
485 xcnt--;
486 if (xcnt > 0) while (line != cell)
487 {
488 line->SetPos(line->GetPosX() + s_indent +
489 (counter++ * step / xcnt),
490 ypos + line->GetPosY());
491 line = line->GetNext();
492 }
493 xcnt++;
494 }
495
496 ypos += ysizedown;
497 xpos = xcnt = 0;
498 ysizeup = ysizedown = 0;
499 line = cell;
500 }
501 }
502
503 // setup height & width, depending on container layout:
504 m_Height = ypos + (ysizedown + ysizeup) + m_IndentBottom;
505
506 if (m_Height < m_MinHeight)
507 {
508 if (m_MinHeightAlign != wxHTML_ALIGN_TOP)
509 {
510 int diff = m_MinHeight - m_Height;
511 if (m_MinHeightAlign == wxHTML_ALIGN_CENTER) diff /= 2;
512 cell = m_Cells;
513 while (cell)
514 {
515 cell->SetPos(cell->GetPosX(), cell->GetPosY() + diff);
516 cell = cell->GetNext();
517 }
518 }
519 m_Height = m_MinHeight;
520 }
521
522 MaxLineWidth += s_indent + ((m_IndentRight < 0) ? (-m_IndentRight * m_Width / 100) : m_IndentRight);
523 if (m_Width < MaxLineWidth) m_Width = MaxLineWidth;
524
525 m_LastLayout = w;
526 }
527
528 void wxHtmlContainerCell::UpdateRenderingStatePre(wxHtmlRenderingState& state,
529 wxHtmlCell *cell) const
530 {
531 wxHtmlSelection *s = state.GetSelection();
532 if (!s) return;
533 if (s->GetFromCell() == cell || s->GetToCell() == cell)
534 {
535 state.SetSelectionState(wxHTML_SEL_CHANGING);
536 }
537 }
538
539 void wxHtmlContainerCell::UpdateRenderingStatePost(wxHtmlRenderingState& state,
540 wxHtmlCell *cell) const
541 {
542 wxHtmlSelection *s = state.GetSelection();
543 if (!s) return;
544 if (s->GetToCell() == cell)
545 state.SetSelectionState(wxHTML_SEL_OUT);
546 else if (s->GetFromCell() == cell)
547 state.SetSelectionState(wxHTML_SEL_IN);
548 }
549
550 #define mMin(a, b) (((a) < (b)) ? (a) : (b))
551 #define mMax(a, b) (((a) < (b)) ? (b) : (a))
552
553 void wxHtmlContainerCell::Draw(wxDC& dc, int x, int y, int view_y1, int view_y2,
554 wxHtmlRenderingState& state)
555 {
556 // container visible, draw it:
557 if ((y + m_PosY <= view_y2) && (y + m_PosY + m_Height > view_y1))
558 {
559 if (m_UseBkColour)
560 {
561 wxBrush myb = wxBrush(m_BkColour, wxSOLID);
562
563 int real_y1 = mMax(y + m_PosY, view_y1);
564 int real_y2 = mMin(y + m_PosY + m_Height - 1, view_y2);
565
566 dc.SetBrush(myb);
567 dc.SetPen(*wxTRANSPARENT_PEN);
568 dc.DrawRectangle(x + m_PosX, real_y1, m_Width, real_y2 - real_y1 + 1);
569 }
570
571 if (m_UseBorder)
572 {
573 wxPen mypen1(m_BorderColour1, 1, wxSOLID);
574 wxPen mypen2(m_BorderColour2, 1, wxSOLID);
575
576 dc.SetPen(mypen1);
577 dc.DrawLine(x + m_PosX, y + m_PosY, x + m_PosX, y + m_PosY + m_Height - 1);
578 dc.DrawLine(x + m_PosX, y + m_PosY, x + m_PosX + m_Width, y + m_PosY);
579 dc.SetPen(mypen2);
580 dc.DrawLine(x + m_PosX + m_Width - 1, y + m_PosY, x + m_PosX + m_Width - 1, y + m_PosY + m_Height - 1);
581 dc.DrawLine(x + m_PosX, y + m_PosY + m_Height - 1, x + m_PosX + m_Width, y + m_PosY + m_Height - 1);
582 }
583
584 if (m_Cells)
585 {
586 // draw container's contents:
587 for (wxHtmlCell *cell = m_Cells; cell; cell = cell->GetNext())
588 {
589 UpdateRenderingStatePre(state, cell);
590 cell->Draw(dc,
591 x + m_PosX, y + m_PosY, view_y1, view_y2,
592 state);
593 UpdateRenderingStatePost(state, cell);
594 }
595 }
596 }
597 // container invisible, just proceed font+color changing:
598 else
599 {
600 DrawInvisible(dc, x, y, state);
601 }
602 }
603
604
605
606 void wxHtmlContainerCell::DrawInvisible(wxDC& dc, int x, int y,
607 wxHtmlRenderingState& state)
608 {
609 if (m_Cells)
610 {
611 for (wxHtmlCell *cell = m_Cells; cell; cell = cell->GetNext())
612 {
613 UpdateRenderingStatePre(state, cell);
614 cell->DrawInvisible(dc, x + m_PosX, y + m_PosY, state);
615 UpdateRenderingStatePost(state, cell);
616 }
617 }
618 }
619
620
621 wxColour wxHtmlContainerCell::GetBackgroundColour()
622 {
623 if (m_UseBkColour)
624 return m_BkColour;
625 else
626 return wxNullColour;
627 }
628
629
630
631 wxHtmlLinkInfo *wxHtmlContainerCell::GetLink(int x, int y) const
632 {
633 wxHtmlCell *cell = FindCellByPos(x, y);
634
635 // VZ: I don't know if we should pass absolute or relative coords to
636 // wxHtmlCell::GetLink()? As the base class version just ignores them
637 // anyhow, it hardly matters right now but should still be clarified
638 return cell ? cell->GetLink(x, y) : NULL;
639 }
640
641
642
643 void wxHtmlContainerCell::InsertCell(wxHtmlCell *f)
644 {
645 if (!m_Cells) m_Cells = m_LastCell = f;
646 else
647 {
648 m_LastCell->SetNext(f);
649 m_LastCell = f;
650 if (m_LastCell) while (m_LastCell->GetNext()) m_LastCell = m_LastCell->GetNext();
651 }
652 f->SetParent(this);
653 m_LastLayout = -1;
654 }
655
656
657
658 void wxHtmlContainerCell::SetAlign(const wxHtmlTag& tag)
659 {
660 if (tag.HasParam(wxT("ALIGN")))
661 {
662 wxString alg = tag.GetParam(wxT("ALIGN"));
663 alg.MakeUpper();
664 if (alg == wxT("CENTER"))
665 SetAlignHor(wxHTML_ALIGN_CENTER);
666 else if (alg == wxT("LEFT"))
667 SetAlignHor(wxHTML_ALIGN_LEFT);
668 else if (alg == wxT("JUSTIFY"))
669 SetAlignHor(wxHTML_ALIGN_JUSTIFY);
670 else if (alg == wxT("RIGHT"))
671 SetAlignHor(wxHTML_ALIGN_RIGHT);
672 m_LastLayout = -1;
673 }
674 }
675
676
677
678 void wxHtmlContainerCell::SetWidthFloat(const wxHtmlTag& tag, double pixel_scale)
679 {
680 if (tag.HasParam(wxT("WIDTH")))
681 {
682 int wdi;
683 wxString wd = tag.GetParam(wxT("WIDTH"));
684
685 if (wd[wd.Length()-1] == wxT('%'))
686 {
687 wxSscanf(wd.c_str(), wxT("%i%%"), &wdi);
688 SetWidthFloat(wdi, wxHTML_UNITS_PERCENT);
689 }
690 else
691 {
692 wxSscanf(wd.c_str(), wxT("%i"), &wdi);
693 SetWidthFloat((int)(pixel_scale * (double)wdi), wxHTML_UNITS_PIXELS);
694 }
695 m_LastLayout = -1;
696 }
697 }
698
699
700
701 const wxHtmlCell* wxHtmlContainerCell::Find(int condition, const void* param) const
702 {
703 if (m_Cells)
704 {
705 const wxHtmlCell *r = NULL;
706
707 for (wxHtmlCell *cell = m_Cells; cell; cell = cell->GetNext())
708 {
709 r = cell->Find(condition, param);
710 if (r) return r;
711 }
712 }
713 return NULL;
714 }
715
716
717 wxHtmlCell *wxHtmlContainerCell::FindCellByPos(wxCoord x, wxCoord y,
718 unsigned flags) const
719 {
720 if ( flags & wxHTML_FIND_EXACT )
721 {
722 for ( const wxHtmlCell *cell = m_Cells; cell; cell = cell->GetNext() )
723 {
724 int cx = cell->GetPosX(),
725 cy = cell->GetPosY();
726
727 if ( (cx <= x) && (cx + cell->GetWidth() > x) &&
728 (cy <= y) && (cy + cell->GetHeight() > y) )
729 {
730 return cell->FindCellByPos(x - cx, y - cy, flags);
731 }
732 }
733 }
734 else if ( flags & wxHTML_FIND_NEAREST_AFTER )
735 {
736 wxHtmlCell *c;
737 int y2;
738 for ( const wxHtmlCell *cell = m_Cells; cell; cell = cell->GetNext() )
739 {
740 y2 = cell->GetPosY() + cell->GetHeight() - 1;
741 if (y2 < y || (y2 == y && cell->GetPosX()+cell->GetWidth()-1 < x))
742 continue;
743 c = cell->FindCellByPos(x - cell->GetPosX(), y - cell->GetPosY(),
744 flags);
745 if (c) return c;
746 }
747 }
748 else if ( flags & wxHTML_FIND_NEAREST_BEFORE )
749 {
750 wxHtmlCell *c;
751 for ( const wxHtmlCell *cell = m_Cells; cell; cell = cell->GetNext() )
752 {
753 if (cell->GetPosY() > y ||
754 (cell->GetPosY() == y && cell->GetPosX() > x))
755 break;
756 c = cell->FindCellByPos(x - cell->GetPosX(), y - cell->GetPosY(),
757 flags);
758 if (c) return c;
759 }
760 }
761
762 return NULL;
763 }
764
765
766 void wxHtmlContainerCell::OnMouseClick(wxWindow *parent, int x, int y, const wxMouseEvent& event)
767 {
768 wxHtmlCell *cell = FindCellByPos(x, y);
769 if ( cell )
770 cell->OnMouseClick(parent, x, y, event);
771 }
772
773
774
775 void wxHtmlContainerCell::GetHorizontalConstraints(int *left, int *right) const
776 {
777 int cleft = m_PosX + m_Width, cright = m_PosX; // worst case
778 int l, r;
779
780 for (wxHtmlCell *cell = m_Cells; cell; cell = cell->GetNext())
781 {
782 cell->GetHorizontalConstraints(&l, &r);
783 if (l < cleft)
784 cleft = l;
785 if (r > cright)
786 cright = r;
787 }
788
789 cleft -= (m_IndentLeft < 0) ? (-m_IndentLeft * m_Width / 100) : m_IndentLeft;
790 cright += (m_IndentRight < 0) ? (-m_IndentRight * m_Width / 100) : m_IndentRight;
791
792 if (left)
793 *left = cleft;
794 if (right)
795 *right = cright;
796 }
797
798
799 wxHtmlCell *wxHtmlContainerCell::GetFirstTerminal() const
800 {
801 if ( m_Cells )
802 {
803 wxHtmlCell *c2;
804 for (wxHtmlCell *c = m_Cells; c; c = c->GetNext())
805 {
806 c2 = c->GetFirstTerminal();
807 if ( c2 )
808 return c2;
809 }
810 }
811 return NULL;
812 }
813
814 wxHtmlCell *wxHtmlContainerCell::GetLastTerminal() const
815 {
816 if ( m_Cells )
817 {
818 // most common case first:
819 wxHtmlCell *c = m_LastCell->GetLastTerminal();
820 if ( c )
821 return c;
822
823 wxHtmlCell *c2 = NULL;
824 for (c = m_Cells; c; c = c->GetNext())
825 c2 = c->GetLastTerminal();
826 return c2;
827 }
828 else
829 return NULL;
830 }
831
832
833
834
835 // --------------------------------------------------------------------------
836 // wxHtmlColourCell
837 // --------------------------------------------------------------------------
838
839 void wxHtmlColourCell::Draw(wxDC& dc,
840 int x, int y,
841 int WXUNUSED(view_y1), int WXUNUSED(view_y2),
842 wxHtmlRenderingState& state)
843 {
844 DrawInvisible(dc, x, y, state);
845 }
846
847 void wxHtmlColourCell::DrawInvisible(wxDC& dc,
848 int WXUNUSED(x), int WXUNUSED(y),
849 wxHtmlRenderingState& state)
850 {
851 if (m_Flags & wxHTML_CLR_FOREGROUND)
852 {
853 state.SetFgColour(m_Colour);
854 if (state.GetSelectionState() != wxHTML_SEL_IN)
855 dc.SetTextForeground(m_Colour);
856 }
857 if (m_Flags & wxHTML_CLR_BACKGROUND)
858 {
859 state.SetBgColour(m_Colour);
860 if (state.GetSelectionState() != wxHTML_SEL_IN)
861 dc.SetTextBackground(m_Colour);
862 dc.SetBackground(wxBrush(m_Colour, wxSOLID));
863 }
864 }
865
866
867
868
869 // ---------------------------------------------------------------------------
870 // wxHtmlFontCell
871 // ---------------------------------------------------------------------------
872
873 void wxHtmlFontCell::Draw(wxDC& dc,
874 int WXUNUSED(x), int WXUNUSED(y),
875 int WXUNUSED(view_y1), int WXUNUSED(view_y2),
876 wxHtmlRenderingState& WXUNUSED(state))
877 {
878 dc.SetFont(m_Font);
879 }
880
881 void wxHtmlFontCell::DrawInvisible(wxDC& dc, int WXUNUSED(x), int WXUNUSED(y),
882 wxHtmlRenderingState& WXUNUSED(state))
883 {
884 dc.SetFont(m_Font);
885 }
886
887
888
889
890
891
892
893
894 // ---------------------------------------------------------------------------
895 // wxHtmlWidgetCell
896 // ---------------------------------------------------------------------------
897
898 wxHtmlWidgetCell::wxHtmlWidgetCell(wxWindow *wnd, int w)
899 {
900 int sx, sy;
901 m_Wnd = wnd;
902 m_Wnd->GetSize(&sx, &sy);
903 m_Width = sx, m_Height = sy;
904 m_WidthFloat = w;
905 }
906
907
908 void wxHtmlWidgetCell::Draw(wxDC& WXUNUSED(dc),
909 int WXUNUSED(x), int WXUNUSED(y),
910 int WXUNUSED(view_y1), int WXUNUSED(view_y2),
911 wxHtmlRenderingState& WXUNUSED(state))
912 {
913 int absx = 0, absy = 0, stx, sty;
914 wxHtmlCell *c = this;
915
916 while (c)
917 {
918 absx += c->GetPosX();
919 absy += c->GetPosY();
920 c = c->GetParent();
921 }
922
923 ((wxScrolledWindow*)(m_Wnd->GetParent()))->GetViewStart(&stx, &sty);
924 m_Wnd->SetSize(absx - wxHTML_SCROLL_STEP * stx, absy - wxHTML_SCROLL_STEP * sty, m_Width, m_Height);
925 }
926
927
928
929 void wxHtmlWidgetCell::DrawInvisible(wxDC& WXUNUSED(dc),
930 int WXUNUSED(x), int WXUNUSED(y),
931 wxHtmlRenderingState& WXUNUSED(state))
932 {
933 int absx = 0, absy = 0, stx, sty;
934 wxHtmlCell *c = this;
935
936 while (c)
937 {
938 absx += c->GetPosX();
939 absy += c->GetPosY();
940 c = c->GetParent();
941 }
942
943 ((wxScrolledWindow*)(m_Wnd->GetParent()))->GetViewStart(&stx, &sty);
944 m_Wnd->SetSize(absx - wxHTML_SCROLL_STEP * stx, absy - wxHTML_SCROLL_STEP * sty, m_Width, m_Height);
945 }
946
947
948
949 void wxHtmlWidgetCell::Layout(int w)
950 {
951 if (m_WidthFloat != 0)
952 {
953 m_Width = (w * m_WidthFloat) / 100;
954 m_Wnd->SetSize(m_Width, m_Height);
955 }
956
957 wxHtmlCell::Layout(w);
958 }
959
960
961
962 // ----------------------------------------------------------------------------
963 // wxHtmlTerminalCellsInterator
964 // ----------------------------------------------------------------------------
965
966 const wxHtmlCell* wxHtmlTerminalCellsInterator::operator++()
967 {
968 if ( !m_pos )
969 return NULL;
970
971 do
972 {
973 if ( m_pos == m_to )
974 {
975 m_pos = NULL;
976 return NULL;
977 }
978
979 if ( m_pos->GetNext() )
980 m_pos = m_pos->GetNext();
981 else
982 {
983 // we must go up the hierarchy until we reach container where this
984 // is not the last child, and then go down to first terminal cell:
985 while ( m_pos->GetNext() == NULL )
986 {
987 m_pos = m_pos->GetParent();
988 if ( !m_pos )
989 return NULL;
990 }
991 m_pos = m_pos->GetNext();
992 }
993 while ( m_pos->GetFirstChild() != NULL )
994 m_pos = m_pos->GetFirstChild();
995 } while ( !m_pos->IsTerminalCell() );
996
997 return m_pos;
998 }
999
1000 #endif