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