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