use 'I' cursor when over text
[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 "wx/module.h"
34
35 #include <stdlib.h>
36
37 //-----------------------------------------------------------------------------
38 // Global variables
39 //-----------------------------------------------------------------------------
40
41 static wxCursor *gs_cursorLink = NULL;
42 static wxCursor *gs_cursorText = NULL;
43
44
45 //-----------------------------------------------------------------------------
46 // Helper classes
47 //-----------------------------------------------------------------------------
48
49 void wxHtmlSelection::Set(const wxPoint& fromPos, const wxHtmlCell *fromCell,
50 const wxPoint& toPos, const wxHtmlCell *toCell)
51 {
52 m_fromCell = fromCell;
53 m_toCell = toCell;
54 m_fromPos = fromPos;
55 m_toPos = toPos;
56 }
57
58 void wxHtmlSelection::Set(const wxHtmlCell *fromCell, const wxHtmlCell *toCell)
59 {
60 wxPoint p1 = fromCell ? fromCell->GetAbsPos() : wxDefaultPosition;
61 wxPoint p2 = toCell ? toCell->GetAbsPos() : wxDefaultPosition;
62 if ( toCell )
63 {
64 p2.x += toCell->GetWidth()-1;
65 p2.y += toCell->GetHeight()-1;
66 }
67 Set(p1, fromCell, p2, toCell);
68 }
69
70 wxColour wxDefaultHtmlRenderingStyle::GetSelectedTextColour(
71 const wxColour& clr)
72 {
73 return wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT);
74 }
75
76 wxColour wxDefaultHtmlRenderingStyle::GetSelectedTextBgColour(
77 const wxColour& WXUNUSED(clr))
78 {
79 return wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHT);
80 }
81
82
83 //-----------------------------------------------------------------------------
84 // wxHtmlCell
85 //-----------------------------------------------------------------------------
86
87 wxHtmlCell::wxHtmlCell() : wxObject()
88 {
89 m_Next = NULL;
90 m_Parent = NULL;
91 m_Width = m_Height = m_Descent = 0;
92 m_CanLiveOnPagebreak = TRUE;
93 m_Link = NULL;
94 }
95
96 wxHtmlCell::~wxHtmlCell()
97 {
98 delete m_Link;
99 }
100
101
102 void wxHtmlCell::OnMouseClick(wxWindow *parent, int x, int y,
103 const wxMouseEvent& event)
104 {
105 wxHtmlLinkInfo *lnk = GetLink(x, y);
106 if (lnk != NULL)
107 {
108 wxHtmlLinkInfo lnk2(*lnk);
109 lnk2.SetEvent(&event);
110 lnk2.SetHtmlCell(this);
111
112 // note : this cast is legal because parent is *always* wxHtmlWindow
113 wxStaticCast(parent, wxHtmlWindow)->OnLinkClicked(lnk2);
114 }
115 }
116
117
118 wxCursor wxHtmlCell::GetCursor() const
119 {
120 if ( GetLink() )
121 {
122 if ( !gs_cursorLink )
123 gs_cursorLink = new wxCursor(wxCURSOR_HAND);
124 return *gs_cursorLink;
125 }
126 else
127 return *wxSTANDARD_CURSOR;
128 }
129
130
131 bool wxHtmlCell::AdjustPagebreak(int *pagebreak, int* WXUNUSED(known_pagebreaks), int WXUNUSED(number_of_pages)) const
132 {
133 if ((!m_CanLiveOnPagebreak) &&
134 m_PosY < *pagebreak && m_PosY + m_Height > *pagebreak)
135 {
136 *pagebreak = m_PosY;
137 return TRUE;
138 }
139
140 return FALSE;
141 }
142
143
144
145 void wxHtmlCell::SetLink(const wxHtmlLinkInfo& link)
146 {
147 if (m_Link) delete m_Link;
148 m_Link = NULL;
149 if (link.GetHref() != wxEmptyString)
150 m_Link = new wxHtmlLinkInfo(link);
151 }
152
153
154 void wxHtmlCell::Layout(int WXUNUSED(w))
155 {
156 SetPos(0, 0);
157 }
158
159
160
161 void wxHtmlCell::GetHorizontalConstraints(int *left, int *right) const
162 {
163 if (left)
164 *left = m_PosX;
165 if (right)
166 *right = m_PosX + m_Width;
167 }
168
169
170
171 const wxHtmlCell* wxHtmlCell::Find(int WXUNUSED(condition), const void* WXUNUSED(param)) const
172 {
173 return NULL;
174 }
175
176
177 wxHtmlCell *wxHtmlCell::FindCellByPos(wxCoord x, wxCoord y,
178 unsigned flags) const
179 {
180 if ( x >= 0 && x < m_Width && y >= 0 && y < m_Height )
181 {
182 return wxConstCast(this, wxHtmlCell);
183 }
184 else
185 {
186 if ((flags & wxHTML_FIND_NEAREST_AFTER) &&
187 (y < 0 || (y == 0 && x <= 0)))
188 return wxConstCast(this, wxHtmlCell);
189 else if ((flags & wxHTML_FIND_NEAREST_BEFORE) &&
190 (y > m_Height-1 || (y == m_Height-1 && x >= m_Width)))
191 return wxConstCast(this, wxHtmlCell);
192 else
193 return NULL;
194 }
195 }
196
197
198 wxPoint wxHtmlCell::GetAbsPos() const
199 {
200 wxPoint p(m_PosX, m_PosY);
201 for (wxHtmlCell *parent = m_Parent; parent; parent = parent->m_Parent)
202 {
203 p.x += parent->m_PosX;
204 p.y += parent->m_PosY;
205 }
206 return p;
207 }
208
209 unsigned wxHtmlCell::GetDepth() const
210 {
211 unsigned d = 0;
212 for (wxHtmlCell *p = m_Parent; p; p = p->m_Parent)
213 d++;
214 return d;
215 }
216
217 bool wxHtmlCell::IsBefore(wxHtmlCell *cell) const
218 {
219 const wxHtmlCell *c1 = this;
220 const wxHtmlCell *c2 = cell;
221 unsigned d1 = GetDepth();
222 unsigned d2 = cell->GetDepth();
223
224 if ( d1 > d2 )
225 for (; d1 != d2; d1-- )
226 c1 = c1->m_Parent;
227 else if ( d1 < d2 )
228 for (; d1 != d2; d2-- )
229 c2 = c2->m_Parent;
230
231 if ( cell == this )
232 return true;
233
234 while ( c1 && c2 )
235 {
236 if ( c1->m_Parent == c2->m_Parent )
237 {
238 while ( c1 )
239 {
240 if ( c1 == c2 )
241 return true;
242 c1 = c1->GetNext();
243 }
244 return false;
245 }
246 else
247 {
248 c1 = c1->m_Parent;
249 c2 = c2->m_Parent;
250 }
251 }
252
253 wxFAIL_MSG(_T("Cells are in different trees"));
254 return false;
255 }
256
257
258 //-----------------------------------------------------------------------------
259 // wxHtmlWordCell
260 //-----------------------------------------------------------------------------
261
262 wxHtmlWordCell::wxHtmlWordCell(const wxString& word, wxDC& dc) : wxHtmlCell()
263 {
264 m_Word = word;
265 dc.GetTextExtent(m_Word, &m_Width, &m_Height, &m_Descent);
266 SetCanLiveOnPagebreak(FALSE);
267 }
268
269
270 // Splits m_Word into up to three parts according to selection, returns
271 // substring before, in and after selection and the points (in relative coords)
272 // where s2 and s3 start:
273 void wxHtmlWordCell::Split(wxDC& dc,
274 const wxPoint& selFrom, const wxPoint& selTo,
275 unsigned& pos1, unsigned& pos2) const
276 {
277 wxPoint pt1 = (selFrom == wxDefaultPosition) ?
278 wxDefaultPosition : selFrom - GetAbsPos();
279 wxPoint pt2 = (selTo == wxDefaultPosition) ?
280 wxPoint(m_Width, -1) : selTo - GetAbsPos();
281
282 wxCoord charW, charH;
283 unsigned len = m_Word.length();
284 unsigned i = 0;
285 pos1 = 0;
286
287 // before selection:
288 while ( pt1.x > 0 && i < len )
289 {
290 dc.GetTextExtent(m_Word[i], &charW, &charH);
291 pt1.x -= charW;
292 if ( pt1.x >= 0 )
293 {
294 pos1 += charW;
295 i++;
296 }
297 }
298
299 // in selection:
300 unsigned j = i;
301 pos2 = pos1;
302 pt2.x -= pos2;
303 while ( pt2.x > 0 && j < len )
304 {
305 dc.GetTextExtent(m_Word[j], &charW, &charH);
306 pt2.x -= charW;
307 if ( pt2.x >= 0 )
308 {
309 pos2 += charW;
310 j++;
311 }
312 }
313
314 pos1 = i;
315 pos2 = j;
316 }
317
318 void wxHtmlWordCell::SetSelectionPrivPos(wxDC& dc, wxHtmlSelection *s) const
319 {
320 unsigned p1, p2;
321
322 Split(dc,
323 this == s->GetFromCell() ? s->GetFromPos() : wxDefaultPosition,
324 this == s->GetToCell() ? s->GetToPos() : wxDefaultPosition,
325 p1, p2);
326
327 wxPoint p(0, m_Word.length());
328
329 if ( this == s->GetFromCell() )
330 p.x = p1; // selection starts here
331 if ( this == s->GetToCell() )
332 p.y = p2; // selection ends here
333
334 if ( this == s->GetFromCell() )
335 s->SetFromPrivPos(p);
336 if ( this == s->GetToCell() )
337 s->SetToPrivPos(p);
338 }
339
340
341 static void SwitchSelState(wxDC& dc, wxHtmlRenderingInfo& info,
342 bool toSelection)
343 {
344 wxColour fg = info.GetState().GetFgColour();
345 wxColour bg = info.GetState().GetBgColour();
346
347 if ( toSelection )
348 {
349 dc.SetBackgroundMode(wxSOLID);
350 dc.SetTextForeground(info.GetStyle().GetSelectedTextColour(fg));
351 dc.SetTextBackground(info.GetStyle().GetSelectedTextBgColour(bg));
352 }
353 else
354 {
355 dc.SetBackgroundMode(wxTRANSPARENT);
356 dc.SetTextForeground(fg);
357 dc.SetTextBackground(bg);
358 }
359 }
360
361
362 void wxHtmlWordCell::Draw(wxDC& dc, int x, int y,
363 int WXUNUSED(view_y1), int WXUNUSED(view_y2),
364 wxHtmlRenderingInfo& info)
365 {
366 #if 0 // useful for debugging
367 dc.DrawRectangle(x+m_PosX,y+m_PosY,m_Width,m_Height);
368 #endif
369
370 if ( info.GetState().GetSelectionState() == wxHTML_SEL_CHANGING )
371 {
372 // Selection changing, we must draw the word piecewise:
373 wxHtmlSelection *s = info.GetSelection();
374 wxString txt;
375 int w, h;
376 int ofs = 0;
377
378 wxPoint priv = (this == s->GetFromCell()) ?
379 s->GetFromPrivPos() : s->GetToPrivPos();
380
381 // NB: this is quite a hack: in order to compute selection boundaries
382 // (in word's characters) we must know current font, which is only
383 // possible inside rendering code. Therefore we update the
384 // information here and store it in wxHtmlSelection so that
385 // ConvertToText can use it later:
386 if ( priv == wxDefaultPosition )
387 {
388 SetSelectionPrivPos(dc, s);
389 priv = (this == s->GetFromCell()) ?
390 s->GetFromPrivPos() : s->GetToPrivPos();
391 }
392
393 int part1 = priv.x;
394 int part2 = priv.y;
395
396 if ( part1 > 0 )
397 {
398 txt = m_Word.Mid(0, part1);
399 dc.DrawText(txt, x + m_PosX, y + m_PosY);
400 dc.GetTextExtent(txt, &w, &h);
401 ofs += w;
402 }
403
404 SwitchSelState(dc, info, true);
405
406 txt = m_Word.Mid(part1, part2-part1);
407 dc.DrawText(txt, ofs + x + m_PosX, y + m_PosY);
408
409 if ( (size_t)part2 < m_Word.length() )
410 {
411 dc.GetTextExtent(txt, &w, &h);
412 ofs += w;
413 SwitchSelState(dc, info, false);
414 txt = m_Word.Mid(part2);
415 dc.DrawText(txt, ofs + x + m_PosX, y + m_PosY);
416 }
417 }
418 else
419 {
420 // Not changing selection state, draw the word in single mode:
421
422 if ( info.GetState().GetSelectionState() != wxHTML_SEL_OUT &&
423 dc.GetBackgroundMode() != wxSOLID )
424 {
425 SwitchSelState(dc, info, true);
426 }
427 else if ( info.GetState().GetSelectionState() == wxHTML_SEL_OUT &&
428 dc.GetBackgroundMode() == wxSOLID )
429 {
430 SwitchSelState(dc, info, false);
431 }
432 dc.DrawText(m_Word, x + m_PosX, y + m_PosY);
433 }
434 }
435
436
437 wxString wxHtmlWordCell::ConvertToText(wxHtmlSelection *s) const
438 {
439 if ( s && (this == s->GetFromCell() || this == s->GetToCell()) )
440 {
441 wxPoint priv = (this == s->GetFromCell()) ?
442 s->GetFromPrivPos() : s->GetToPrivPos();
443 int part1 = priv.x;
444 int part2 = priv.y;
445 return m_Word.Mid(part1, part2-part1);
446 }
447 else
448 return m_Word;
449 }
450
451 wxCursor wxHtmlWordCell::GetCursor() const
452 {
453 if ( !GetLink() )
454 {
455 if ( !gs_cursorText )
456 gs_cursorText = new wxCursor(wxCURSOR_IBEAM);
457 return *gs_cursorText;
458 }
459 else
460 return wxHtmlCell::GetCursor();
461 }
462
463
464
465 //-----------------------------------------------------------------------------
466 // wxHtmlContainerCell
467 //-----------------------------------------------------------------------------
468
469
470 wxHtmlContainerCell::wxHtmlContainerCell(wxHtmlContainerCell *parent) : wxHtmlCell()
471 {
472 m_Cells = m_LastCell = NULL;
473 m_Parent = parent;
474 if (m_Parent) m_Parent->InsertCell(this);
475 m_AlignHor = wxHTML_ALIGN_LEFT;
476 m_AlignVer = wxHTML_ALIGN_BOTTOM;
477 m_IndentLeft = m_IndentRight = m_IndentTop = m_IndentBottom = 0;
478 m_WidthFloat = 100; m_WidthFloatUnits = wxHTML_UNITS_PERCENT;
479 m_UseBkColour = FALSE;
480 m_UseBorder = FALSE;
481 m_MinHeight = 0;
482 m_MinHeightAlign = wxHTML_ALIGN_TOP;
483 m_LastLayout = -1;
484 }
485
486 wxHtmlContainerCell::~wxHtmlContainerCell()
487 {
488 wxHtmlCell *cell = m_Cells;
489 while ( cell )
490 {
491 wxHtmlCell *cellNext = cell->GetNext();
492 delete cell;
493 cell = cellNext;
494 }
495 }
496
497
498
499 void wxHtmlContainerCell::SetIndent(int i, int what, int units)
500 {
501 int val = (units == wxHTML_UNITS_PIXELS) ? i : -i;
502 if (what & wxHTML_INDENT_LEFT) m_IndentLeft = val;
503 if (what & wxHTML_INDENT_RIGHT) m_IndentRight = val;
504 if (what & wxHTML_INDENT_TOP) m_IndentTop = val;
505 if (what & wxHTML_INDENT_BOTTOM) m_IndentBottom = val;
506 m_LastLayout = -1;
507 }
508
509
510
511 int wxHtmlContainerCell::GetIndent(int ind) const
512 {
513 if (ind & wxHTML_INDENT_LEFT) return m_IndentLeft;
514 else if (ind & wxHTML_INDENT_RIGHT) return m_IndentRight;
515 else if (ind & wxHTML_INDENT_TOP) return m_IndentTop;
516 else if (ind & wxHTML_INDENT_BOTTOM) return m_IndentBottom;
517 else return -1; /* BUG! Should not be called... */
518 }
519
520
521
522
523 int wxHtmlContainerCell::GetIndentUnits(int ind) const
524 {
525 bool p = FALSE;
526 if (ind & wxHTML_INDENT_LEFT) p = m_IndentLeft < 0;
527 else if (ind & wxHTML_INDENT_RIGHT) p = m_IndentRight < 0;
528 else if (ind & wxHTML_INDENT_TOP) p = m_IndentTop < 0;
529 else if (ind & wxHTML_INDENT_BOTTOM) p = m_IndentBottom < 0;
530 if (p) return wxHTML_UNITS_PERCENT;
531 else return wxHTML_UNITS_PIXELS;
532 }
533
534
535
536 bool wxHtmlContainerCell::AdjustPagebreak(int *pagebreak, int* known_pagebreaks, int number_of_pages) const
537 {
538 if (!m_CanLiveOnPagebreak)
539 return wxHtmlCell::AdjustPagebreak(pagebreak, known_pagebreaks, number_of_pages);
540
541 else
542 {
543 wxHtmlCell *c = GetFirstChild();
544 bool rt = FALSE;
545 int pbrk = *pagebreak - m_PosY;
546
547 while (c)
548 {
549 if (c->AdjustPagebreak(&pbrk, known_pagebreaks, number_of_pages))
550 rt = TRUE;
551 c = c->GetNext();
552 }
553 if (rt)
554 *pagebreak = pbrk + m_PosY;
555 return rt;
556 }
557 }
558
559
560
561 void wxHtmlContainerCell::Layout(int w)
562 {
563 wxHtmlCell::Layout(w);
564
565 if (m_LastLayout == w) return;
566
567 // VS: Any attempt to layout with negative or zero width leads to hell,
568 // but we can't ignore such attempts completely, since it sometimes
569 // happen (e.g. when trying how small a table can be). The best thing we
570 // can do is to set the width of child cells to zero
571 if (w < 1)
572 {
573 m_Width = 0;
574 for (wxHtmlCell *cell = m_Cells; cell; cell = cell->GetNext())
575 cell->Layout(0);
576 // this does two things: it recursively calls this code on all
577 // child contrainers and resets children's position to (0,0)
578 return;
579 }
580
581 wxHtmlCell *cell = m_Cells, *line = m_Cells;
582 long xpos = 0, ypos = m_IndentTop;
583 int xdelta = 0, ybasicpos = 0, ydiff;
584 int s_width, s_indent;
585 int ysizeup = 0, ysizedown = 0;
586 int MaxLineWidth = 0;
587 int xcnt = 0;
588
589
590 /*
591
592 WIDTH ADJUSTING :
593
594 */
595
596 if (m_WidthFloatUnits == wxHTML_UNITS_PERCENT)
597 {
598 if (m_WidthFloat < 0) m_Width = (100 + m_WidthFloat) * w / 100;
599 else m_Width = m_WidthFloat * w / 100;
600 }
601 else
602 {
603 if (m_WidthFloat < 0) m_Width = w + m_WidthFloat;
604 else m_Width = m_WidthFloat;
605 }
606
607 if (m_Cells)
608 {
609 int l = (m_IndentLeft < 0) ? (-m_IndentLeft * m_Width / 100) : m_IndentLeft;
610 int r = (m_IndentRight < 0) ? (-m_IndentRight * m_Width / 100) : m_IndentRight;
611 for (wxHtmlCell *cell = m_Cells; cell; cell = cell->GetNext())
612 cell->Layout(m_Width - (l + r));
613 }
614
615 /*
616
617 LAYOUTING :
618
619 */
620
621 // adjust indentation:
622 s_indent = (m_IndentLeft < 0) ? (-m_IndentLeft * m_Width / 100) : m_IndentLeft;
623 s_width = m_Width - s_indent - ((m_IndentRight < 0) ? (-m_IndentRight * m_Width / 100) : m_IndentRight);
624
625 // my own layouting:
626 while (cell != NULL)
627 {
628 switch (m_AlignVer)
629 {
630 case wxHTML_ALIGN_TOP : ybasicpos = 0; break;
631 case wxHTML_ALIGN_BOTTOM : ybasicpos = - cell->GetHeight(); break;
632 case wxHTML_ALIGN_CENTER : ybasicpos = - cell->GetHeight() / 2; break;
633 }
634 ydiff = cell->GetHeight() + ybasicpos;
635
636 if (cell->GetDescent() + ydiff > ysizedown) ysizedown = cell->GetDescent() + ydiff;
637 if (ybasicpos + cell->GetDescent() < -ysizeup) ysizeup = - (ybasicpos + cell->GetDescent());
638
639 cell->SetPos(xpos, ybasicpos + cell->GetDescent());
640 xpos += cell->GetWidth();
641 cell = cell->GetNext();
642 xcnt++;
643
644 // force new line if occured:
645 if ((cell == NULL) || (xpos + cell->GetWidth() > s_width))
646 {
647 if (xpos > MaxLineWidth) MaxLineWidth = xpos;
648 if (ysizeup < 0) ysizeup = 0;
649 if (ysizedown < 0) ysizedown = 0;
650 switch (m_AlignHor) {
651 case wxHTML_ALIGN_LEFT :
652 case wxHTML_ALIGN_JUSTIFY :
653 xdelta = 0;
654 break;
655 case wxHTML_ALIGN_RIGHT :
656 xdelta = 0 + (s_width - xpos);
657 break;
658 case wxHTML_ALIGN_CENTER :
659 xdelta = 0 + (s_width - xpos) / 2;
660 break;
661 }
662 if (xdelta < 0) xdelta = 0;
663 xdelta += s_indent;
664
665 ypos += ysizeup;
666
667 if (m_AlignHor != wxHTML_ALIGN_JUSTIFY || cell == NULL)
668 while (line != cell)
669 {
670 line->SetPos(line->GetPosX() + xdelta,
671 ypos + line->GetPosY());
672 line = line->GetNext();
673 }
674 else
675 {
676 int counter = 0;
677 int step = (s_width - xpos);
678 if (step < 0) step = 0;
679 xcnt--;
680 if (xcnt > 0) while (line != cell)
681 {
682 line->SetPos(line->GetPosX() + s_indent +
683 (counter++ * step / xcnt),
684 ypos + line->GetPosY());
685 line = line->GetNext();
686 }
687 xcnt++;
688 }
689
690 ypos += ysizedown;
691 xpos = xcnt = 0;
692 ysizeup = ysizedown = 0;
693 line = cell;
694 }
695 }
696
697 // setup height & width, depending on container layout:
698 m_Height = ypos + (ysizedown + ysizeup) + m_IndentBottom;
699
700 if (m_Height < m_MinHeight)
701 {
702 if (m_MinHeightAlign != wxHTML_ALIGN_TOP)
703 {
704 int diff = m_MinHeight - m_Height;
705 if (m_MinHeightAlign == wxHTML_ALIGN_CENTER) diff /= 2;
706 cell = m_Cells;
707 while (cell)
708 {
709 cell->SetPos(cell->GetPosX(), cell->GetPosY() + diff);
710 cell = cell->GetNext();
711 }
712 }
713 m_Height = m_MinHeight;
714 }
715
716 MaxLineWidth += s_indent + ((m_IndentRight < 0) ? (-m_IndentRight * m_Width / 100) : m_IndentRight);
717 if (m_Width < MaxLineWidth) m_Width = MaxLineWidth;
718
719 m_LastLayout = w;
720 }
721
722 void wxHtmlContainerCell::UpdateRenderingStatePre(wxHtmlRenderingInfo& info,
723 wxHtmlCell *cell) const
724 {
725 wxHtmlSelection *s = info.GetSelection();
726 if (!s) return;
727 if (s->GetFromCell() == cell || s->GetToCell() == cell)
728 {
729 info.GetState().SetSelectionState(wxHTML_SEL_CHANGING);
730 }
731 }
732
733 void wxHtmlContainerCell::UpdateRenderingStatePost(wxHtmlRenderingInfo& info,
734 wxHtmlCell *cell) const
735 {
736 wxHtmlSelection *s = info.GetSelection();
737 if (!s) return;
738 if (s->GetToCell() == cell)
739 info.GetState().SetSelectionState(wxHTML_SEL_OUT);
740 else if (s->GetFromCell() == cell)
741 info.GetState().SetSelectionState(wxHTML_SEL_IN);
742 }
743
744 #define mMin(a, b) (((a) < (b)) ? (a) : (b))
745 #define mMax(a, b) (((a) < (b)) ? (b) : (a))
746
747 void wxHtmlContainerCell::Draw(wxDC& dc, int x, int y, int view_y1, int view_y2,
748 wxHtmlRenderingInfo& info)
749 {
750 // container visible, draw it:
751 if ((y + m_PosY <= view_y2) && (y + m_PosY + m_Height > view_y1))
752 {
753 if (m_UseBkColour)
754 {
755 wxBrush myb = wxBrush(m_BkColour, wxSOLID);
756
757 int real_y1 = mMax(y + m_PosY, view_y1);
758 int real_y2 = mMin(y + m_PosY + m_Height - 1, view_y2);
759
760 dc.SetBrush(myb);
761 dc.SetPen(*wxTRANSPARENT_PEN);
762 dc.DrawRectangle(x + m_PosX, real_y1, m_Width, real_y2 - real_y1 + 1);
763 }
764
765 if (m_UseBorder)
766 {
767 wxPen mypen1(m_BorderColour1, 1, wxSOLID);
768 wxPen mypen2(m_BorderColour2, 1, wxSOLID);
769
770 dc.SetPen(mypen1);
771 dc.DrawLine(x + m_PosX, y + m_PosY, x + m_PosX, y + m_PosY + m_Height - 1);
772 dc.DrawLine(x + m_PosX, y + m_PosY, x + m_PosX + m_Width, y + m_PosY);
773 dc.SetPen(mypen2);
774 dc.DrawLine(x + m_PosX + m_Width - 1, y + m_PosY, x + m_PosX + m_Width - 1, y + m_PosY + m_Height - 1);
775 dc.DrawLine(x + m_PosX, y + m_PosY + m_Height - 1, x + m_PosX + m_Width, y + m_PosY + m_Height - 1);
776 }
777
778 if (m_Cells)
779 {
780 // draw container's contents:
781 for (wxHtmlCell *cell = m_Cells; cell; cell = cell->GetNext())
782 {
783 UpdateRenderingStatePre(info, cell);
784 cell->Draw(dc,
785 x + m_PosX, y + m_PosY, view_y1, view_y2,
786 info);
787 UpdateRenderingStatePost(info, cell);
788 }
789 }
790 }
791 // container invisible, just proceed font+color changing:
792 else
793 {
794 DrawInvisible(dc, x, y, info);
795 }
796 }
797
798
799
800 void wxHtmlContainerCell::DrawInvisible(wxDC& dc, int x, int y,
801 wxHtmlRenderingInfo& info)
802 {
803 if (m_Cells)
804 {
805 for (wxHtmlCell *cell = m_Cells; cell; cell = cell->GetNext())
806 {
807 UpdateRenderingStatePre(info, cell);
808 cell->DrawInvisible(dc, x + m_PosX, y + m_PosY, info);
809 UpdateRenderingStatePost(info, cell);
810 }
811 }
812 }
813
814
815 wxColour wxHtmlContainerCell::GetBackgroundColour()
816 {
817 if (m_UseBkColour)
818 return m_BkColour;
819 else
820 return wxNullColour;
821 }
822
823
824
825 wxHtmlLinkInfo *wxHtmlContainerCell::GetLink(int x, int y) const
826 {
827 wxHtmlCell *cell = FindCellByPos(x, y);
828
829 // VZ: I don't know if we should pass absolute or relative coords to
830 // wxHtmlCell::GetLink()? As the base class version just ignores them
831 // anyhow, it hardly matters right now but should still be clarified
832 return cell ? cell->GetLink(x, y) : NULL;
833 }
834
835
836
837 void wxHtmlContainerCell::InsertCell(wxHtmlCell *f)
838 {
839 if (!m_Cells) m_Cells = m_LastCell = f;
840 else
841 {
842 m_LastCell->SetNext(f);
843 m_LastCell = f;
844 if (m_LastCell) while (m_LastCell->GetNext()) m_LastCell = m_LastCell->GetNext();
845 }
846 f->SetParent(this);
847 m_LastLayout = -1;
848 }
849
850
851
852 void wxHtmlContainerCell::SetAlign(const wxHtmlTag& tag)
853 {
854 if (tag.HasParam(wxT("ALIGN")))
855 {
856 wxString alg = tag.GetParam(wxT("ALIGN"));
857 alg.MakeUpper();
858 if (alg == wxT("CENTER"))
859 SetAlignHor(wxHTML_ALIGN_CENTER);
860 else if (alg == wxT("LEFT"))
861 SetAlignHor(wxHTML_ALIGN_LEFT);
862 else if (alg == wxT("JUSTIFY"))
863 SetAlignHor(wxHTML_ALIGN_JUSTIFY);
864 else if (alg == wxT("RIGHT"))
865 SetAlignHor(wxHTML_ALIGN_RIGHT);
866 m_LastLayout = -1;
867 }
868 }
869
870
871
872 void wxHtmlContainerCell::SetWidthFloat(const wxHtmlTag& tag, double pixel_scale)
873 {
874 if (tag.HasParam(wxT("WIDTH")))
875 {
876 int wdi;
877 wxString wd = tag.GetParam(wxT("WIDTH"));
878
879 if (wd[wd.Length()-1] == wxT('%'))
880 {
881 wxSscanf(wd.c_str(), wxT("%i%%"), &wdi);
882 SetWidthFloat(wdi, wxHTML_UNITS_PERCENT);
883 }
884 else
885 {
886 wxSscanf(wd.c_str(), wxT("%i"), &wdi);
887 SetWidthFloat((int)(pixel_scale * (double)wdi), wxHTML_UNITS_PIXELS);
888 }
889 m_LastLayout = -1;
890 }
891 }
892
893
894
895 const wxHtmlCell* wxHtmlContainerCell::Find(int condition, const void* param) const
896 {
897 if (m_Cells)
898 {
899 const wxHtmlCell *r = NULL;
900
901 for (wxHtmlCell *cell = m_Cells; cell; cell = cell->GetNext())
902 {
903 r = cell->Find(condition, param);
904 if (r) return r;
905 }
906 }
907 return NULL;
908 }
909
910
911 wxHtmlCell *wxHtmlContainerCell::FindCellByPos(wxCoord x, wxCoord y,
912 unsigned flags) const
913 {
914 if ( flags & wxHTML_FIND_EXACT )
915 {
916 for ( const wxHtmlCell *cell = m_Cells; cell; cell = cell->GetNext() )
917 {
918 int cx = cell->GetPosX(),
919 cy = cell->GetPosY();
920
921 if ( (cx <= x) && (cx + cell->GetWidth() > x) &&
922 (cy <= y) && (cy + cell->GetHeight() > y) )
923 {
924 return cell->FindCellByPos(x - cx, y - cy, flags);
925 }
926 }
927 }
928 else if ( flags & wxHTML_FIND_NEAREST_AFTER )
929 {
930 wxHtmlCell *c;
931 int y2;
932 for ( const wxHtmlCell *cell = m_Cells; cell; cell = cell->GetNext() )
933 {
934 y2 = cell->GetPosY() + cell->GetHeight() - 1;
935 if (y2 < y || (y2 == y && cell->GetPosX()+cell->GetWidth()-1 < x))
936 continue;
937 c = cell->FindCellByPos(x - cell->GetPosX(), y - cell->GetPosY(),
938 flags);
939 if (c) return c;
940 }
941 }
942 else if ( flags & wxHTML_FIND_NEAREST_BEFORE )
943 {
944 wxHtmlCell *c2, *c = NULL;
945 for ( const wxHtmlCell *cell = m_Cells; cell; cell = cell->GetNext() )
946 {
947 if (cell->GetPosY() > y ||
948 (cell->GetPosY() == y && cell->GetPosX() > x))
949 break;
950 c2 = cell->FindCellByPos(x - cell->GetPosX(), y - cell->GetPosY(),
951 flags);
952 if (c2)
953 c = c2;
954 }
955 if (c) return c;
956 }
957
958 return NULL;
959 }
960
961
962 void wxHtmlContainerCell::OnMouseClick(wxWindow *parent, int x, int y, const wxMouseEvent& event)
963 {
964 wxHtmlCell *cell = FindCellByPos(x, y);
965 if ( cell )
966 cell->OnMouseClick(parent, x, y, event);
967 }
968
969
970
971 void wxHtmlContainerCell::GetHorizontalConstraints(int *left, int *right) const
972 {
973 int cleft = m_PosX + m_Width, cright = m_PosX; // worst case
974 int l, r;
975
976 for (wxHtmlCell *cell = m_Cells; cell; cell = cell->GetNext())
977 {
978 cell->GetHorizontalConstraints(&l, &r);
979 if (l < cleft)
980 cleft = l;
981 if (r > cright)
982 cright = r;
983 }
984
985 cleft -= (m_IndentLeft < 0) ? (-m_IndentLeft * m_Width / 100) : m_IndentLeft;
986 cright += (m_IndentRight < 0) ? (-m_IndentRight * m_Width / 100) : m_IndentRight;
987
988 if (left)
989 *left = cleft;
990 if (right)
991 *right = cright;
992 }
993
994
995 wxHtmlCell *wxHtmlContainerCell::GetFirstTerminal() const
996 {
997 if ( m_Cells )
998 {
999 wxHtmlCell *c2;
1000 for (wxHtmlCell *c = m_Cells; c; c = c->GetNext())
1001 {
1002 c2 = c->GetFirstTerminal();
1003 if ( c2 )
1004 return c2;
1005 }
1006 }
1007 return NULL;
1008 }
1009
1010 wxHtmlCell *wxHtmlContainerCell::GetLastTerminal() const
1011 {
1012 if ( m_Cells )
1013 {
1014 // most common case first:
1015 wxHtmlCell *c = m_LastCell->GetLastTerminal();
1016 if ( c )
1017 return c;
1018
1019 wxHtmlCell *c2 = NULL;
1020 for (c = m_Cells; c; c = c->GetNext())
1021 c2 = c->GetLastTerminal();
1022 return c2;
1023 }
1024 else
1025 return NULL;
1026 }
1027
1028
1029
1030
1031 // --------------------------------------------------------------------------
1032 // wxHtmlColourCell
1033 // --------------------------------------------------------------------------
1034
1035 void wxHtmlColourCell::Draw(wxDC& dc,
1036 int x, int y,
1037 int WXUNUSED(view_y1), int WXUNUSED(view_y2),
1038 wxHtmlRenderingInfo& info)
1039 {
1040 DrawInvisible(dc, x, y, info);
1041 }
1042
1043 void wxHtmlColourCell::DrawInvisible(wxDC& dc,
1044 int WXUNUSED(x), int WXUNUSED(y),
1045 wxHtmlRenderingInfo& info)
1046 {
1047 wxHtmlRenderingState& state = info.GetState();
1048 if (m_Flags & wxHTML_CLR_FOREGROUND)
1049 {
1050 state.SetFgColour(m_Colour);
1051 if (state.GetSelectionState() != wxHTML_SEL_IN)
1052 dc.SetTextForeground(m_Colour);
1053 else
1054 dc.SetTextForeground(
1055 info.GetStyle().GetSelectedTextColour(m_Colour));
1056 }
1057 if (m_Flags & wxHTML_CLR_BACKGROUND)
1058 {
1059 state.SetBgColour(m_Colour);
1060 if (state.GetSelectionState() != wxHTML_SEL_IN)
1061 dc.SetTextBackground(m_Colour);
1062 else
1063 dc.SetTextBackground(
1064 info.GetStyle().GetSelectedTextBgColour(m_Colour));
1065 dc.SetBackground(wxBrush(m_Colour, wxSOLID));
1066 }
1067 }
1068
1069
1070
1071
1072 // ---------------------------------------------------------------------------
1073 // wxHtmlFontCell
1074 // ---------------------------------------------------------------------------
1075
1076 void wxHtmlFontCell::Draw(wxDC& dc,
1077 int WXUNUSED(x), int WXUNUSED(y),
1078 int WXUNUSED(view_y1), int WXUNUSED(view_y2),
1079 wxHtmlRenderingInfo& WXUNUSED(info))
1080 {
1081 dc.SetFont(m_Font);
1082 }
1083
1084 void wxHtmlFontCell::DrawInvisible(wxDC& dc, int WXUNUSED(x), int WXUNUSED(y),
1085 wxHtmlRenderingInfo& WXUNUSED(info))
1086 {
1087 dc.SetFont(m_Font);
1088 }
1089
1090
1091
1092
1093
1094
1095
1096
1097 // ---------------------------------------------------------------------------
1098 // wxHtmlWidgetCell
1099 // ---------------------------------------------------------------------------
1100
1101 wxHtmlWidgetCell::wxHtmlWidgetCell(wxWindow *wnd, int w)
1102 {
1103 int sx, sy;
1104 m_Wnd = wnd;
1105 m_Wnd->GetSize(&sx, &sy);
1106 m_Width = sx, m_Height = sy;
1107 m_WidthFloat = w;
1108 }
1109
1110
1111 void wxHtmlWidgetCell::Draw(wxDC& WXUNUSED(dc),
1112 int WXUNUSED(x), int WXUNUSED(y),
1113 int WXUNUSED(view_y1), int WXUNUSED(view_y2),
1114 wxHtmlRenderingInfo& WXUNUSED(info))
1115 {
1116 int absx = 0, absy = 0, stx, sty;
1117 wxHtmlCell *c = this;
1118
1119 while (c)
1120 {
1121 absx += c->GetPosX();
1122 absy += c->GetPosY();
1123 c = c->GetParent();
1124 }
1125
1126 ((wxScrolledWindow*)(m_Wnd->GetParent()))->GetViewStart(&stx, &sty);
1127 m_Wnd->SetSize(absx - wxHTML_SCROLL_STEP * stx, absy - wxHTML_SCROLL_STEP * sty, m_Width, m_Height);
1128 }
1129
1130
1131
1132 void wxHtmlWidgetCell::DrawInvisible(wxDC& WXUNUSED(dc),
1133 int WXUNUSED(x), int WXUNUSED(y),
1134 wxHtmlRenderingInfo& WXUNUSED(info))
1135 {
1136 int absx = 0, absy = 0, stx, sty;
1137 wxHtmlCell *c = this;
1138
1139 while (c)
1140 {
1141 absx += c->GetPosX();
1142 absy += c->GetPosY();
1143 c = c->GetParent();
1144 }
1145
1146 ((wxScrolledWindow*)(m_Wnd->GetParent()))->GetViewStart(&stx, &sty);
1147 m_Wnd->SetSize(absx - wxHTML_SCROLL_STEP * stx, absy - wxHTML_SCROLL_STEP * sty, m_Width, m_Height);
1148 }
1149
1150
1151
1152 void wxHtmlWidgetCell::Layout(int w)
1153 {
1154 if (m_WidthFloat != 0)
1155 {
1156 m_Width = (w * m_WidthFloat) / 100;
1157 m_Wnd->SetSize(m_Width, m_Height);
1158 }
1159
1160 wxHtmlCell::Layout(w);
1161 }
1162
1163
1164
1165 // ----------------------------------------------------------------------------
1166 // wxHtmlTerminalCellsInterator
1167 // ----------------------------------------------------------------------------
1168
1169 const wxHtmlCell* wxHtmlTerminalCellsInterator::operator++()
1170 {
1171 if ( !m_pos )
1172 return NULL;
1173
1174 do
1175 {
1176 if ( m_pos == m_to )
1177 {
1178 m_pos = NULL;
1179 return NULL;
1180 }
1181
1182 if ( m_pos->GetNext() )
1183 m_pos = m_pos->GetNext();
1184 else
1185 {
1186 // we must go up the hierarchy until we reach container where this
1187 // is not the last child, and then go down to first terminal cell:
1188 while ( m_pos->GetNext() == NULL )
1189 {
1190 m_pos = m_pos->GetParent();
1191 if ( !m_pos )
1192 return NULL;
1193 }
1194 m_pos = m_pos->GetNext();
1195 }
1196 while ( m_pos->GetFirstChild() != NULL )
1197 m_pos = m_pos->GetFirstChild();
1198 } while ( !m_pos->IsTerminalCell() );
1199
1200 return m_pos;
1201 }
1202
1203
1204
1205
1206
1207
1208
1209 //-----------------------------------------------------------------------------
1210 // Cleanup
1211 //-----------------------------------------------------------------------------
1212
1213 class wxHtmlCellModule: public wxModule
1214 {
1215 DECLARE_DYNAMIC_CLASS(wxHtmlCellModule)
1216 public:
1217 wxHtmlCellModule() : wxModule() {}
1218 bool OnInit() { return true; }
1219 void OnExit()
1220 {
1221 wxDELETE(gs_cursorLink);
1222 wxDELETE(gs_cursorText);
1223 }
1224 };
1225
1226 IMPLEMENT_DYNAMIC_CLASS(wxHtmlCellModule, wxModule)
1227
1228 #endif