]> git.saurik.com Git - wxWidgets.git/blob - src/html/htmlcell.cpp
bf9750d649d2d6ac0de23b843acf8dcbf556fb7d
[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+m_Height && x < 0+m_Width)))
188 return wxConstCast(this, wxHtmlCell);
189 else if ((flags & wxHTML_FIND_NEAREST_BEFORE) &&
190 (y >= 0+m_Height || (y >= 0 && x >= 0)))
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 // adjust for cases when the start/end position is completely
288 // outside the cell:
289 if ( pt1.y < 0 )
290 pt1.x = 0;
291 if ( pt2.y >= m_Height )
292 pt2.x = m_Width;
293
294 // before selection:
295 while ( pt1.x > 0 && i < len )
296 {
297 dc.GetTextExtent(m_Word[i], &charW, &charH);
298 pt1.x -= charW;
299 if ( pt1.x >= 0 )
300 {
301 pos1 += charW;
302 i++;
303 }
304 }
305
306 // in selection:
307 unsigned j = i;
308 pos2 = pos1;
309 pt2.x -= pos2;
310 while ( pt2.x > 0 && j < len )
311 {
312 dc.GetTextExtent(m_Word[j], &charW, &charH);
313 pt2.x -= charW;
314 if ( pt2.x >= 0 )
315 {
316 pos2 += charW;
317 j++;
318 }
319 }
320
321 pos1 = i;
322 pos2 = j;
323 }
324
325 void wxHtmlWordCell::SetSelectionPrivPos(wxDC& dc, wxHtmlSelection *s) const
326 {
327 unsigned p1, p2;
328
329 Split(dc,
330 this == s->GetFromCell() ? s->GetFromPos() : wxDefaultPosition,
331 this == s->GetToCell() ? s->GetToPos() : wxDefaultPosition,
332 p1, p2);
333
334 wxPoint p(0, m_Word.length());
335
336 if ( this == s->GetFromCell() )
337 p.x = p1; // selection starts here
338 if ( this == s->GetToCell() )
339 p.y = p2; // selection ends here
340
341 if ( this == s->GetFromCell() )
342 s->SetFromPrivPos(p);
343 if ( this == s->GetToCell() )
344 s->SetToPrivPos(p);
345 }
346
347
348 static void SwitchSelState(wxDC& dc, wxHtmlRenderingInfo& info,
349 bool toSelection)
350 {
351 wxColour fg = info.GetState().GetFgColour();
352 wxColour bg = info.GetState().GetBgColour();
353
354 if ( toSelection )
355 {
356 dc.SetBackgroundMode(wxSOLID);
357 dc.SetTextForeground(info.GetStyle().GetSelectedTextColour(fg));
358 dc.SetTextBackground(info.GetStyle().GetSelectedTextBgColour(bg));
359 dc.SetBackground(wxBrush(info.GetStyle().GetSelectedTextBgColour(bg),
360 wxSOLID));
361 }
362 else
363 {
364 dc.SetBackgroundMode(wxTRANSPARENT);
365 dc.SetTextForeground(fg);
366 dc.SetTextBackground(bg);
367 dc.SetBackground(wxBrush(bg, wxSOLID));
368 }
369 }
370
371
372 void wxHtmlWordCell::Draw(wxDC& dc, int x, int y,
373 int WXUNUSED(view_y1), int WXUNUSED(view_y2),
374 wxHtmlRenderingInfo& info)
375 {
376 #if 0 // useful for debugging
377 dc.DrawRectangle(x+m_PosX,y+m_PosY,m_Width,m_Height);
378 #endif
379
380 bool drawSelectionAfterCell = false;
381
382 if ( info.GetState().GetSelectionState() == wxHTML_SEL_CHANGING )
383 {
384 // Selection changing, we must draw the word piecewise:
385 wxHtmlSelection *s = info.GetSelection();
386 wxString txt;
387 int w, h;
388 int ofs = 0;
389
390 wxPoint priv = (this == s->GetFromCell()) ?
391 s->GetFromPrivPos() : s->GetToPrivPos();
392
393 // NB: this is quite a hack: in order to compute selection boundaries
394 // (in word's characters) we must know current font, which is only
395 // possible inside rendering code. Therefore we update the
396 // information here and store it in wxHtmlSelection so that
397 // ConvertToText can use it later:
398 if ( priv == wxDefaultPosition )
399 {
400 SetSelectionPrivPos(dc, s);
401 priv = (this == s->GetFromCell()) ?
402 s->GetFromPrivPos() : s->GetToPrivPos();
403 }
404
405 int part1 = priv.x;
406 int part2 = priv.y;
407
408 if ( part1 > 0 )
409 {
410 txt = m_Word.Mid(0, part1);
411 dc.DrawText(txt, x + m_PosX, y + m_PosY);
412 dc.GetTextExtent(txt, &w, &h);
413 ofs += w;
414 }
415
416 SwitchSelState(dc, info, true);
417
418 txt = m_Word.Mid(part1, part2-part1);
419 dc.DrawText(txt, ofs + x + m_PosX, y + m_PosY);
420
421 if ( (size_t)part2 < m_Word.length() )
422 {
423 dc.GetTextExtent(txt, &w, &h);
424 ofs += w;
425 SwitchSelState(dc, info, false);
426 txt = m_Word.Mid(part2);
427 dc.DrawText(txt, ofs + x + m_PosX, y + m_PosY);
428 }
429 else
430 drawSelectionAfterCell = true;
431 }
432 else
433 {
434 wxHtmlSelectionState selstate = info.GetState().GetSelectionState();
435 // Not changing selection state, draw the word in single mode:
436 if ( selstate != wxHTML_SEL_OUT &&
437 dc.GetBackgroundMode() != wxSOLID )
438 {
439 SwitchSelState(dc, info, true);
440 }
441 else if ( selstate == wxHTML_SEL_OUT &&
442 dc.GetBackgroundMode() == wxSOLID )
443 {
444 SwitchSelState(dc, info, false);
445 }
446 dc.DrawText(m_Word, x + m_PosX, y + m_PosY);
447 drawSelectionAfterCell = (selstate != wxHTML_SEL_OUT);
448 }
449
450 // NB: If the text is justified then there is usually some free space
451 // between adjacent cells and drawing the selection only onto cells
452 // would result in ugly unselected spaces. The code below detects
453 // this special case and renders the selection *outside* the sell,
454 // too.
455 if ( m_Parent->GetAlignHor() == wxHTML_ALIGN_JUSTIFY &&
456 drawSelectionAfterCell )
457 {
458 wxHtmlCell *nextCell = m_Next;
459 while ( nextCell && nextCell->IsFormattingCell() )
460 nextCell = nextCell->GetNext();
461 if ( nextCell )
462 {
463 int nextX = nextCell->GetPosX();
464 if ( m_PosX + m_Width < nextX )
465 {
466 dc.SetBrush(dc.GetBackground());
467 dc.SetPen(*wxTRANSPARENT_PEN);
468 dc.DrawRectangle(x + m_PosX + m_Width, y + m_PosY,
469 nextX - m_PosX - m_Width, m_Height);
470 }
471 }
472 }
473 }
474
475
476 wxString wxHtmlWordCell::ConvertToText(wxHtmlSelection *s) const
477 {
478 if ( s && (this == s->GetFromCell() || this == s->GetToCell()) )
479 {
480 wxPoint priv = this == s->GetFromCell() ? s->GetFromPrivPos()
481 : s->GetToPrivPos();
482
483 // VZ: we may be called before we had a chance to re-render ourselves
484 // and in this case GetFrom/ToPrivPos() is not set yet -- assume
485 // that this only happens in case of a double/triple click (which
486 // seems to be the case now) and so it makes sense to select the
487 // entire contents of the cell in this case
488 //
489 // TODO: but this really needs to be fixed in some better way later...
490 if ( priv != wxDefaultPosition )
491 {
492 int part1 = priv.x;
493 int part2 = priv.y;
494 return m_Word.Mid(part1, part2-part1);
495 }
496 //else: return the whole word below
497 }
498
499 return m_Word;
500 }
501
502 wxCursor wxHtmlWordCell::GetCursor() const
503 {
504 if ( !GetLink() )
505 {
506 if ( !gs_cursorText )
507 gs_cursorText = new wxCursor(wxCURSOR_IBEAM);
508 return *gs_cursorText;
509 }
510 else
511 return wxHtmlCell::GetCursor();
512 }
513
514
515 //-----------------------------------------------------------------------------
516 // wxHtmlContainerCell
517 //-----------------------------------------------------------------------------
518
519
520 wxHtmlContainerCell::wxHtmlContainerCell(wxHtmlContainerCell *parent) : wxHtmlCell()
521 {
522 m_Cells = m_LastCell = NULL;
523 m_Parent = parent;
524 if (m_Parent) m_Parent->InsertCell(this);
525 m_AlignHor = wxHTML_ALIGN_LEFT;
526 m_AlignVer = wxHTML_ALIGN_BOTTOM;
527 m_IndentLeft = m_IndentRight = m_IndentTop = m_IndentBottom = 0;
528 m_WidthFloat = 100; m_WidthFloatUnits = wxHTML_UNITS_PERCENT;
529 m_UseBkColour = FALSE;
530 m_UseBorder = FALSE;
531 m_MinHeight = 0;
532 m_MinHeightAlign = wxHTML_ALIGN_TOP;
533 m_LastLayout = -1;
534 }
535
536 wxHtmlContainerCell::~wxHtmlContainerCell()
537 {
538 wxHtmlCell *cell = m_Cells;
539 while ( cell )
540 {
541 wxHtmlCell *cellNext = cell->GetNext();
542 delete cell;
543 cell = cellNext;
544 }
545 }
546
547
548
549 void wxHtmlContainerCell::SetIndent(int i, int what, int units)
550 {
551 int val = (units == wxHTML_UNITS_PIXELS) ? i : -i;
552 if (what & wxHTML_INDENT_LEFT) m_IndentLeft = val;
553 if (what & wxHTML_INDENT_RIGHT) m_IndentRight = val;
554 if (what & wxHTML_INDENT_TOP) m_IndentTop = val;
555 if (what & wxHTML_INDENT_BOTTOM) m_IndentBottom = val;
556 m_LastLayout = -1;
557 }
558
559
560
561 int wxHtmlContainerCell::GetIndent(int ind) const
562 {
563 if (ind & wxHTML_INDENT_LEFT) return m_IndentLeft;
564 else if (ind & wxHTML_INDENT_RIGHT) return m_IndentRight;
565 else if (ind & wxHTML_INDENT_TOP) return m_IndentTop;
566 else if (ind & wxHTML_INDENT_BOTTOM) return m_IndentBottom;
567 else return -1; /* BUG! Should not be called... */
568 }
569
570
571
572
573 int wxHtmlContainerCell::GetIndentUnits(int ind) const
574 {
575 bool p = FALSE;
576 if (ind & wxHTML_INDENT_LEFT) p = m_IndentLeft < 0;
577 else if (ind & wxHTML_INDENT_RIGHT) p = m_IndentRight < 0;
578 else if (ind & wxHTML_INDENT_TOP) p = m_IndentTop < 0;
579 else if (ind & wxHTML_INDENT_BOTTOM) p = m_IndentBottom < 0;
580 if (p) return wxHTML_UNITS_PERCENT;
581 else return wxHTML_UNITS_PIXELS;
582 }
583
584
585
586 bool wxHtmlContainerCell::AdjustPagebreak(int *pagebreak, int* known_pagebreaks, int number_of_pages) const
587 {
588 if (!m_CanLiveOnPagebreak)
589 return wxHtmlCell::AdjustPagebreak(pagebreak, known_pagebreaks, number_of_pages);
590
591 else
592 {
593 wxHtmlCell *c = GetFirstChild();
594 bool rt = FALSE;
595 int pbrk = *pagebreak - m_PosY;
596
597 while (c)
598 {
599 if (c->AdjustPagebreak(&pbrk, known_pagebreaks, number_of_pages))
600 rt = TRUE;
601 c = c->GetNext();
602 }
603 if (rt)
604 *pagebreak = pbrk + m_PosY;
605 return rt;
606 }
607 }
608
609
610
611 void wxHtmlContainerCell::Layout(int w)
612 {
613 wxHtmlCell::Layout(w);
614
615 if (m_LastLayout == w) return;
616
617 // VS: Any attempt to layout with negative or zero width leads to hell,
618 // but we can't ignore such attempts completely, since it sometimes
619 // happen (e.g. when trying how small a table can be). The best thing we
620 // can do is to set the width of child cells to zero
621 if (w < 1)
622 {
623 m_Width = 0;
624 for (wxHtmlCell *cell = m_Cells; cell; cell = cell->GetNext())
625 cell->Layout(0);
626 // this does two things: it recursively calls this code on all
627 // child contrainers and resets children's position to (0,0)
628 return;
629 }
630
631 wxHtmlCell *cell = m_Cells, *line = m_Cells;
632 long xpos = 0, ypos = m_IndentTop;
633 int xdelta = 0, ybasicpos = 0, ydiff;
634 int s_width, s_indent;
635 int ysizeup = 0, ysizedown = 0;
636 int MaxLineWidth = 0;
637 int xcnt = 0;
638
639
640 /*
641
642 WIDTH ADJUSTING :
643
644 */
645
646 if (m_WidthFloatUnits == wxHTML_UNITS_PERCENT)
647 {
648 if (m_WidthFloat < 0) m_Width = (100 + m_WidthFloat) * w / 100;
649 else m_Width = m_WidthFloat * w / 100;
650 }
651 else
652 {
653 if (m_WidthFloat < 0) m_Width = w + m_WidthFloat;
654 else m_Width = m_WidthFloat;
655 }
656
657 if (m_Cells)
658 {
659 int l = (m_IndentLeft < 0) ? (-m_IndentLeft * m_Width / 100) : m_IndentLeft;
660 int r = (m_IndentRight < 0) ? (-m_IndentRight * m_Width / 100) : m_IndentRight;
661 for (wxHtmlCell *cell = m_Cells; cell; cell = cell->GetNext())
662 cell->Layout(m_Width - (l + r));
663 }
664
665 /*
666
667 LAYOUTING :
668
669 */
670
671 // adjust indentation:
672 s_indent = (m_IndentLeft < 0) ? (-m_IndentLeft * m_Width / 100) : m_IndentLeft;
673 s_width = m_Width - s_indent - ((m_IndentRight < 0) ? (-m_IndentRight * m_Width / 100) : m_IndentRight);
674
675 // my own layouting:
676 while (cell != NULL)
677 {
678 switch (m_AlignVer)
679 {
680 case wxHTML_ALIGN_TOP : ybasicpos = 0; break;
681 case wxHTML_ALIGN_BOTTOM : ybasicpos = - cell->GetHeight(); break;
682 case wxHTML_ALIGN_CENTER : ybasicpos = - cell->GetHeight() / 2; break;
683 }
684 ydiff = cell->GetHeight() + ybasicpos;
685
686 if (cell->GetDescent() + ydiff > ysizedown) ysizedown = cell->GetDescent() + ydiff;
687 if (ybasicpos + cell->GetDescent() < -ysizeup) ysizeup = - (ybasicpos + cell->GetDescent());
688
689 cell->SetPos(xpos, ybasicpos + cell->GetDescent());
690 xpos += cell->GetWidth();
691 cell = cell->GetNext();
692 xcnt++;
693
694 // force new line if occured:
695 if ((cell == NULL) || (xpos + cell->GetWidth() > s_width))
696 {
697 if (xpos > MaxLineWidth) MaxLineWidth = xpos;
698 if (ysizeup < 0) ysizeup = 0;
699 if (ysizedown < 0) ysizedown = 0;
700 switch (m_AlignHor) {
701 case wxHTML_ALIGN_LEFT :
702 case wxHTML_ALIGN_JUSTIFY :
703 xdelta = 0;
704 break;
705 case wxHTML_ALIGN_RIGHT :
706 xdelta = 0 + (s_width - xpos);
707 break;
708 case wxHTML_ALIGN_CENTER :
709 xdelta = 0 + (s_width - xpos) / 2;
710 break;
711 }
712 if (xdelta < 0) xdelta = 0;
713 xdelta += s_indent;
714
715 ypos += ysizeup;
716
717 if (m_AlignHor != wxHTML_ALIGN_JUSTIFY || cell == NULL)
718 while (line != cell)
719 {
720 line->SetPos(line->GetPosX() + xdelta,
721 ypos + line->GetPosY());
722 line = line->GetNext();
723 }
724 else
725 {
726 int counter = 0;
727 int step = (s_width - xpos);
728 if (step < 0) step = 0;
729 xcnt--;
730 if (xcnt > 0) while (line != cell)
731 {
732 line->SetPos(line->GetPosX() + s_indent +
733 (counter++ * step / xcnt),
734 ypos + line->GetPosY());
735 line = line->GetNext();
736 }
737 xcnt++;
738 }
739
740 ypos += ysizedown;
741 xpos = xcnt = 0;
742 ysizeup = ysizedown = 0;
743 line = cell;
744 }
745 }
746
747 // setup height & width, depending on container layout:
748 m_Height = ypos + (ysizedown + ysizeup) + m_IndentBottom;
749
750 if (m_Height < m_MinHeight)
751 {
752 if (m_MinHeightAlign != wxHTML_ALIGN_TOP)
753 {
754 int diff = m_MinHeight - m_Height;
755 if (m_MinHeightAlign == wxHTML_ALIGN_CENTER) diff /= 2;
756 cell = m_Cells;
757 while (cell)
758 {
759 cell->SetPos(cell->GetPosX(), cell->GetPosY() + diff);
760 cell = cell->GetNext();
761 }
762 }
763 m_Height = m_MinHeight;
764 }
765
766 MaxLineWidth += s_indent + ((m_IndentRight < 0) ? (-m_IndentRight * m_Width / 100) : m_IndentRight);
767 if (m_Width < MaxLineWidth) m_Width = MaxLineWidth;
768
769 m_LastLayout = w;
770 }
771
772 void wxHtmlContainerCell::UpdateRenderingStatePre(wxHtmlRenderingInfo& info,
773 wxHtmlCell *cell) const
774 {
775 wxHtmlSelection *s = info.GetSelection();
776 if (!s) return;
777 if (s->GetFromCell() == cell || s->GetToCell() == cell)
778 {
779 info.GetState().SetSelectionState(wxHTML_SEL_CHANGING);
780 }
781 }
782
783 void wxHtmlContainerCell::UpdateRenderingStatePost(wxHtmlRenderingInfo& info,
784 wxHtmlCell *cell) const
785 {
786 wxHtmlSelection *s = info.GetSelection();
787 if (!s) return;
788 if (s->GetToCell() == cell)
789 info.GetState().SetSelectionState(wxHTML_SEL_OUT);
790 else if (s->GetFromCell() == cell)
791 info.GetState().SetSelectionState(wxHTML_SEL_IN);
792 }
793
794 #define mMin(a, b) (((a) < (b)) ? (a) : (b))
795 #define mMax(a, b) (((a) < (b)) ? (b) : (a))
796
797 void wxHtmlContainerCell::Draw(wxDC& dc, int x, int y, int view_y1, int view_y2,
798 wxHtmlRenderingInfo& info)
799 {
800 // container visible, draw it:
801 if ((y + m_PosY <= view_y2) && (y + m_PosY + m_Height > view_y1))
802 {
803 if (m_UseBkColour)
804 {
805 wxBrush myb = wxBrush(m_BkColour, wxSOLID);
806
807 int real_y1 = mMax(y + m_PosY, view_y1);
808 int real_y2 = mMin(y + m_PosY + m_Height - 1, view_y2);
809
810 dc.SetBrush(myb);
811 dc.SetPen(*wxTRANSPARENT_PEN);
812 dc.DrawRectangle(x + m_PosX, real_y1, m_Width, real_y2 - real_y1 + 1);
813 }
814
815 if (m_UseBorder)
816 {
817 wxPen mypen1(m_BorderColour1, 1, wxSOLID);
818 wxPen mypen2(m_BorderColour2, 1, wxSOLID);
819
820 dc.SetPen(mypen1);
821 dc.DrawLine(x + m_PosX, y + m_PosY, x + m_PosX, y + m_PosY + m_Height - 1);
822 dc.DrawLine(x + m_PosX, y + m_PosY, x + m_PosX + m_Width, y + m_PosY);
823 dc.SetPen(mypen2);
824 dc.DrawLine(x + m_PosX + m_Width - 1, y + m_PosY, x + m_PosX + m_Width - 1, y + m_PosY + m_Height - 1);
825 dc.DrawLine(x + m_PosX, y + m_PosY + m_Height - 1, x + m_PosX + m_Width, y + m_PosY + m_Height - 1);
826 }
827
828 if (m_Cells)
829 {
830 // draw container's contents:
831 for (wxHtmlCell *cell = m_Cells; cell; cell = cell->GetNext())
832 {
833 UpdateRenderingStatePre(info, cell);
834 cell->Draw(dc,
835 x + m_PosX, y + m_PosY, view_y1, view_y2,
836 info);
837 UpdateRenderingStatePost(info, cell);
838 }
839 }
840 }
841 // container invisible, just proceed font+color changing:
842 else
843 {
844 DrawInvisible(dc, x, y, info);
845 }
846 }
847
848
849
850 void wxHtmlContainerCell::DrawInvisible(wxDC& dc, int x, int y,
851 wxHtmlRenderingInfo& info)
852 {
853 if (m_Cells)
854 {
855 for (wxHtmlCell *cell = m_Cells; cell; cell = cell->GetNext())
856 {
857 UpdateRenderingStatePre(info, cell);
858 cell->DrawInvisible(dc, x + m_PosX, y + m_PosY, info);
859 UpdateRenderingStatePost(info, cell);
860 }
861 }
862 }
863
864
865 wxColour wxHtmlContainerCell::GetBackgroundColour()
866 {
867 if (m_UseBkColour)
868 return m_BkColour;
869 else
870 return wxNullColour;
871 }
872
873
874
875 wxHtmlLinkInfo *wxHtmlContainerCell::GetLink(int x, int y) const
876 {
877 wxHtmlCell *cell = FindCellByPos(x, y);
878
879 // VZ: I don't know if we should pass absolute or relative coords to
880 // wxHtmlCell::GetLink()? As the base class version just ignores them
881 // anyhow, it hardly matters right now but should still be clarified
882 return cell ? cell->GetLink(x, y) : NULL;
883 }
884
885
886
887 void wxHtmlContainerCell::InsertCell(wxHtmlCell *f)
888 {
889 if (!m_Cells) m_Cells = m_LastCell = f;
890 else
891 {
892 m_LastCell->SetNext(f);
893 m_LastCell = f;
894 if (m_LastCell) while (m_LastCell->GetNext()) m_LastCell = m_LastCell->GetNext();
895 }
896 f->SetParent(this);
897 m_LastLayout = -1;
898 }
899
900
901
902 void wxHtmlContainerCell::SetAlign(const wxHtmlTag& tag)
903 {
904 if (tag.HasParam(wxT("ALIGN")))
905 {
906 wxString alg = tag.GetParam(wxT("ALIGN"));
907 alg.MakeUpper();
908 if (alg == wxT("CENTER"))
909 SetAlignHor(wxHTML_ALIGN_CENTER);
910 else if (alg == wxT("LEFT"))
911 SetAlignHor(wxHTML_ALIGN_LEFT);
912 else if (alg == wxT("JUSTIFY"))
913 SetAlignHor(wxHTML_ALIGN_JUSTIFY);
914 else if (alg == wxT("RIGHT"))
915 SetAlignHor(wxHTML_ALIGN_RIGHT);
916 m_LastLayout = -1;
917 }
918 }
919
920
921
922 void wxHtmlContainerCell::SetWidthFloat(const wxHtmlTag& tag, double pixel_scale)
923 {
924 if (tag.HasParam(wxT("WIDTH")))
925 {
926 int wdi;
927 wxString wd = tag.GetParam(wxT("WIDTH"));
928
929 if (wd[wd.Length()-1] == wxT('%'))
930 {
931 wxSscanf(wd.c_str(), wxT("%i%%"), &wdi);
932 SetWidthFloat(wdi, wxHTML_UNITS_PERCENT);
933 }
934 else
935 {
936 wxSscanf(wd.c_str(), wxT("%i"), &wdi);
937 SetWidthFloat((int)(pixel_scale * (double)wdi), wxHTML_UNITS_PIXELS);
938 }
939 m_LastLayout = -1;
940 }
941 }
942
943
944
945 const wxHtmlCell* wxHtmlContainerCell::Find(int condition, const void* param) const
946 {
947 if (m_Cells)
948 {
949 const wxHtmlCell *r = NULL;
950
951 for (wxHtmlCell *cell = m_Cells; cell; cell = cell->GetNext())
952 {
953 r = cell->Find(condition, param);
954 if (r) return r;
955 }
956 }
957 return NULL;
958 }
959
960
961 wxHtmlCell *wxHtmlContainerCell::FindCellByPos(wxCoord x, wxCoord y,
962 unsigned flags) const
963 {
964 if ( flags & wxHTML_FIND_EXACT )
965 {
966 for ( const wxHtmlCell *cell = m_Cells; cell; cell = cell->GetNext() )
967 {
968 int cx = cell->GetPosX(),
969 cy = cell->GetPosY();
970
971 if ( (cx <= x) && (cx + cell->GetWidth() > x) &&
972 (cy <= y) && (cy + cell->GetHeight() > y) )
973 {
974 return cell->FindCellByPos(x - cx, y - cy, flags);
975 }
976 }
977 }
978 else if ( flags & wxHTML_FIND_NEAREST_AFTER )
979 {
980 wxHtmlCell *c;
981 for ( const wxHtmlCell *cell = m_Cells; cell; cell = cell->GetNext() )
982 {
983 if ( cell->IsFormattingCell() )
984 continue;
985 int cellY = cell->GetPosY();
986 if (!( y < cellY || (y < cellY + cell->GetHeight() &&
987 x < cell->GetPosX() + cell->GetWidth()) ))
988 continue;
989
990 c = cell->FindCellByPos(x - cell->GetPosX(), y - cellY, flags);
991 if (c) return c;
992 }
993 }
994 else if ( flags & wxHTML_FIND_NEAREST_BEFORE )
995 {
996 wxHtmlCell *c2, *c = NULL;
997 for ( const wxHtmlCell *cell = m_Cells; cell; cell = cell->GetNext() )
998 {
999 if ( cell->IsFormattingCell() )
1000 continue;
1001 int cellY = cell->GetPosY();
1002 if (!( cellY + cell->GetHeight() <= y ||
1003 (y >= cellY && x >= cell->GetPosX()) ))
1004 break;
1005 c2 = cell->FindCellByPos(x - cell->GetPosX(), y - cellY, flags);
1006 if (c2)
1007 c = c2;
1008 }
1009 if (c) return c;
1010 }
1011
1012 return NULL;
1013 }
1014
1015
1016 void wxHtmlContainerCell::OnMouseClick(wxWindow *parent, int x, int y, const wxMouseEvent& event)
1017 {
1018 wxHtmlCell *cell = FindCellByPos(x, y);
1019 if ( cell )
1020 cell->OnMouseClick(parent, x, y, event);
1021 }
1022
1023
1024
1025 void wxHtmlContainerCell::GetHorizontalConstraints(int *left, int *right) const
1026 {
1027 int cleft = m_PosX + m_Width, cright = m_PosX; // worst case
1028 int l, r;
1029
1030 for (wxHtmlCell *cell = m_Cells; cell; cell = cell->GetNext())
1031 {
1032 cell->GetHorizontalConstraints(&l, &r);
1033 if (l < cleft)
1034 cleft = l;
1035 if (r > cright)
1036 cright = r;
1037 }
1038
1039 cleft -= (m_IndentLeft < 0) ? (-m_IndentLeft * m_Width / 100) : m_IndentLeft;
1040 cright += (m_IndentRight < 0) ? (-m_IndentRight * m_Width / 100) : m_IndentRight;
1041
1042 if (left)
1043 *left = cleft;
1044 if (right)
1045 *right = cright;
1046 }
1047
1048
1049 wxHtmlCell *wxHtmlContainerCell::GetFirstTerminal() const
1050 {
1051 if ( m_Cells )
1052 {
1053 wxHtmlCell *c2;
1054 for (wxHtmlCell *c = m_Cells; c; c = c->GetNext())
1055 {
1056 c2 = c->GetFirstTerminal();
1057 if ( c2 )
1058 return c2;
1059 }
1060 }
1061 return NULL;
1062 }
1063
1064 wxHtmlCell *wxHtmlContainerCell::GetLastTerminal() const
1065 {
1066 if ( m_Cells )
1067 {
1068 // most common case first:
1069 wxHtmlCell *c = m_LastCell->GetLastTerminal();
1070 if ( c )
1071 return c;
1072
1073 wxHtmlCell *c2 = NULL;
1074 for (c = m_Cells; c; c = c->GetNext())
1075 c2 = c->GetLastTerminal();
1076 return c2;
1077 }
1078 else
1079 return NULL;
1080 }
1081
1082
1083
1084
1085 // --------------------------------------------------------------------------
1086 // wxHtmlColourCell
1087 // --------------------------------------------------------------------------
1088
1089 void wxHtmlColourCell::Draw(wxDC& dc,
1090 int x, int y,
1091 int WXUNUSED(view_y1), int WXUNUSED(view_y2),
1092 wxHtmlRenderingInfo& info)
1093 {
1094 DrawInvisible(dc, x, y, info);
1095 }
1096
1097 void wxHtmlColourCell::DrawInvisible(wxDC& dc,
1098 int WXUNUSED(x), int WXUNUSED(y),
1099 wxHtmlRenderingInfo& info)
1100 {
1101 wxHtmlRenderingState& state = info.GetState();
1102 if (m_Flags & wxHTML_CLR_FOREGROUND)
1103 {
1104 state.SetFgColour(m_Colour);
1105 if (state.GetSelectionState() != wxHTML_SEL_IN)
1106 dc.SetTextForeground(m_Colour);
1107 else
1108 dc.SetTextForeground(
1109 info.GetStyle().GetSelectedTextColour(m_Colour));
1110 }
1111 if (m_Flags & wxHTML_CLR_BACKGROUND)
1112 {
1113 state.SetBgColour(m_Colour);
1114 if (state.GetSelectionState() != wxHTML_SEL_IN)
1115 {
1116 dc.SetTextBackground(m_Colour);
1117 dc.SetBackground(wxBrush(m_Colour, wxSOLID));
1118 }
1119 else
1120 {
1121 wxColour c = info.GetStyle().GetSelectedTextBgColour(m_Colour);
1122 dc.SetTextBackground(c);
1123 dc.SetBackground(wxBrush(c, wxSOLID));
1124 }
1125 }
1126 }
1127
1128
1129
1130
1131 // ---------------------------------------------------------------------------
1132 // wxHtmlFontCell
1133 // ---------------------------------------------------------------------------
1134
1135 void wxHtmlFontCell::Draw(wxDC& dc,
1136 int WXUNUSED(x), int WXUNUSED(y),
1137 int WXUNUSED(view_y1), int WXUNUSED(view_y2),
1138 wxHtmlRenderingInfo& WXUNUSED(info))
1139 {
1140 dc.SetFont(m_Font);
1141 }
1142
1143 void wxHtmlFontCell::DrawInvisible(wxDC& dc, int WXUNUSED(x), int WXUNUSED(y),
1144 wxHtmlRenderingInfo& WXUNUSED(info))
1145 {
1146 dc.SetFont(m_Font);
1147 }
1148
1149
1150
1151
1152
1153
1154
1155
1156 // ---------------------------------------------------------------------------
1157 // wxHtmlWidgetCell
1158 // ---------------------------------------------------------------------------
1159
1160 wxHtmlWidgetCell::wxHtmlWidgetCell(wxWindow *wnd, int w)
1161 {
1162 int sx, sy;
1163 m_Wnd = wnd;
1164 m_Wnd->GetSize(&sx, &sy);
1165 m_Width = sx, m_Height = sy;
1166 m_WidthFloat = w;
1167 }
1168
1169
1170 void wxHtmlWidgetCell::Draw(wxDC& WXUNUSED(dc),
1171 int WXUNUSED(x), int WXUNUSED(y),
1172 int WXUNUSED(view_y1), int WXUNUSED(view_y2),
1173 wxHtmlRenderingInfo& WXUNUSED(info))
1174 {
1175 int absx = 0, absy = 0, stx, sty;
1176 wxHtmlCell *c = this;
1177
1178 while (c)
1179 {
1180 absx += c->GetPosX();
1181 absy += c->GetPosY();
1182 c = c->GetParent();
1183 }
1184
1185 ((wxScrolledWindow*)(m_Wnd->GetParent()))->GetViewStart(&stx, &sty);
1186 m_Wnd->SetSize(absx - wxHTML_SCROLL_STEP * stx, absy - wxHTML_SCROLL_STEP * sty, m_Width, m_Height);
1187 }
1188
1189
1190
1191 void wxHtmlWidgetCell::DrawInvisible(wxDC& WXUNUSED(dc),
1192 int WXUNUSED(x), int WXUNUSED(y),
1193 wxHtmlRenderingInfo& WXUNUSED(info))
1194 {
1195 int absx = 0, absy = 0, stx, sty;
1196 wxHtmlCell *c = this;
1197
1198 while (c)
1199 {
1200 absx += c->GetPosX();
1201 absy += c->GetPosY();
1202 c = c->GetParent();
1203 }
1204
1205 ((wxScrolledWindow*)(m_Wnd->GetParent()))->GetViewStart(&stx, &sty);
1206 m_Wnd->SetSize(absx - wxHTML_SCROLL_STEP * stx, absy - wxHTML_SCROLL_STEP * sty, m_Width, m_Height);
1207 }
1208
1209
1210
1211 void wxHtmlWidgetCell::Layout(int w)
1212 {
1213 if (m_WidthFloat != 0)
1214 {
1215 m_Width = (w * m_WidthFloat) / 100;
1216 m_Wnd->SetSize(m_Width, m_Height);
1217 }
1218
1219 wxHtmlCell::Layout(w);
1220 }
1221
1222
1223
1224 // ----------------------------------------------------------------------------
1225 // wxHtmlTerminalCellsInterator
1226 // ----------------------------------------------------------------------------
1227
1228 const wxHtmlCell* wxHtmlTerminalCellsInterator::operator++()
1229 {
1230 if ( !m_pos )
1231 return NULL;
1232
1233 do
1234 {
1235 if ( m_pos == m_to )
1236 {
1237 m_pos = NULL;
1238 return NULL;
1239 }
1240
1241 if ( m_pos->GetNext() )
1242 m_pos = m_pos->GetNext();
1243 else
1244 {
1245 // we must go up the hierarchy until we reach container where this
1246 // is not the last child, and then go down to first terminal cell:
1247 while ( m_pos->GetNext() == NULL )
1248 {
1249 m_pos = m_pos->GetParent();
1250 if ( !m_pos )
1251 return NULL;
1252 }
1253 m_pos = m_pos->GetNext();
1254 }
1255 while ( m_pos->GetFirstChild() != NULL )
1256 m_pos = m_pos->GetFirstChild();
1257 } while ( !m_pos->IsTerminalCell() );
1258
1259 return m_pos;
1260 }
1261
1262
1263
1264
1265
1266
1267
1268 //-----------------------------------------------------------------------------
1269 // Cleanup
1270 //-----------------------------------------------------------------------------
1271
1272 class wxHtmlCellModule: public wxModule
1273 {
1274 DECLARE_DYNAMIC_CLASS(wxHtmlCellModule)
1275 public:
1276 wxHtmlCellModule() : wxModule() {}
1277 bool OnInit() { return true; }
1278 void OnExit()
1279 {
1280 wxDELETE(gs_cursorLink);
1281 wxDELETE(gs_cursorText);
1282 }
1283 };
1284
1285 IMPLEMENT_DYNAMIC_CLASS(wxHtmlCellModule, wxModule)
1286
1287 #endif