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