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