tables code fixes
[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
12 #endif
13
14 #include "wx/wxprec.h"
15
16 #include "wx/defs.h"
17
18 #if wxUSE_HTML && wxUSE_STREAMS
19
20 #ifdef __BORDLANDC__
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 <stdlib.h>
33
34
35 //-----------------------------------------------------------------------------
36 // wxHtmlCell
37 //-----------------------------------------------------------------------------
38
39 wxHtmlCell::wxHtmlCell() : wxObject()
40 {
41 m_Next = NULL;
42 m_Parent = NULL;
43 m_Width = m_Height = m_Descent = 0;
44 m_CanLiveOnPagebreak = TRUE;
45 m_Link = NULL;
46 }
47
48 wxHtmlCell::~wxHtmlCell()
49 {
50 if (m_Link) delete m_Link;
51 }
52
53
54 void wxHtmlCell::OnMouseClick(wxWindow *parent, int x, int y,
55 const wxMouseEvent& event)
56 {
57 wxHtmlLinkInfo *lnk = GetLink(x, y);
58 if (lnk != NULL)
59 {
60 wxHtmlLinkInfo lnk2(*lnk);
61 lnk2.SetEvent(&event);
62 lnk2.SetHtmlCell(this);
63 ((wxHtmlWindow*)parent)->OnLinkClicked(lnk2);
64 // note : this overcasting is legal because parent is *always* wxHtmlWindow
65 }
66 }
67
68
69
70 bool wxHtmlCell::AdjustPagebreak(int *pagebreak) const
71 {
72 if ((!m_CanLiveOnPagebreak) &&
73 m_PosY < *pagebreak && m_PosY + m_Height > *pagebreak)
74 {
75 *pagebreak = m_PosY;
76 return TRUE;
77 }
78 else
79 return FALSE;
80 }
81
82
83
84 void wxHtmlCell::SetLink(const wxHtmlLinkInfo& link)
85 {
86 if (m_Link) delete m_Link;
87 m_Link = NULL;
88 if (link.GetHref() != wxEmptyString)
89 m_Link = new wxHtmlLinkInfo(link);
90 }
91
92
93
94 void wxHtmlCell::Layout(int WXUNUSED(w))
95 {
96 SetPos(0, 0);
97 }
98
99
100
101 void wxHtmlCell::GetHorizontalConstraints(int *left, int *right) const
102 {
103 if (left)
104 *left = m_PosX;
105 if (right)
106 *right = m_PosX + m_Width;
107 }
108
109
110
111 const wxHtmlCell* wxHtmlCell::Find(int WXUNUSED(condition), const void* WXUNUSED(param)) const
112 {
113 return NULL;
114 }
115
116
117
118 //-----------------------------------------------------------------------------
119 // wxHtmlWordCell
120 //-----------------------------------------------------------------------------
121
122 wxHtmlWordCell::wxHtmlWordCell(const wxString& word, wxDC& dc) : wxHtmlCell()
123 {
124 m_Word = word;
125 dc.GetTextExtent(m_Word, &m_Width, &m_Height, &m_Descent);
126 SetCanLiveOnPagebreak(FALSE);
127 }
128
129
130
131 void wxHtmlWordCell::Draw(wxDC& dc, int x, int y, int WXUNUSED(view_y1), int WXUNUSED(view_y2))
132 {
133 dc.DrawText(m_Word, x + m_PosX, y + m_PosY);
134 }
135
136
137
138 //-----------------------------------------------------------------------------
139 // wxHtmlContainerCell
140 //-----------------------------------------------------------------------------
141
142
143 wxHtmlContainerCell::wxHtmlContainerCell(wxHtmlContainerCell *parent) : wxHtmlCell()
144 {
145 m_Cells = m_LastCell = NULL;
146 m_Parent = parent;
147 if (m_Parent) m_Parent->InsertCell(this);
148 m_AlignHor = wxHTML_ALIGN_LEFT;
149 m_AlignVer = wxHTML_ALIGN_BOTTOM;
150 m_IndentLeft = m_IndentRight = m_IndentTop = m_IndentBottom = 0;
151 m_WidthFloat = 100; m_WidthFloatUnits = wxHTML_UNITS_PERCENT;
152 m_UseBkColour = FALSE;
153 m_UseBorder = FALSE;
154 m_MinHeight = 0;
155 m_MinHeightAlign = wxHTML_ALIGN_TOP;
156 m_LastLayout = -1;
157 }
158
159 wxHtmlContainerCell::~wxHtmlContainerCell()
160 {
161 wxHtmlCell *cell = m_Cells;
162 while ( cell )
163 {
164 wxHtmlCell *cellNext = cell->GetNext();
165 delete cell;
166 cell = cellNext;
167 }
168 }
169
170
171
172 void wxHtmlContainerCell::SetIndent(int i, int what, int units)
173 {
174 int val = (units == wxHTML_UNITS_PIXELS) ? i : -i;
175 if (what & wxHTML_INDENT_LEFT) m_IndentLeft = val;
176 if (what & wxHTML_INDENT_RIGHT) m_IndentRight = val;
177 if (what & wxHTML_INDENT_TOP) m_IndentTop = val;
178 if (what & wxHTML_INDENT_BOTTOM) m_IndentBottom = val;
179 m_LastLayout = -1;
180 }
181
182
183
184 int wxHtmlContainerCell::GetIndent(int ind) const
185 {
186 if (ind & wxHTML_INDENT_LEFT) return m_IndentLeft;
187 else if (ind & wxHTML_INDENT_RIGHT) return m_IndentRight;
188 else if (ind & wxHTML_INDENT_TOP) return m_IndentTop;
189 else if (ind & wxHTML_INDENT_BOTTOM) return m_IndentBottom;
190 else return -1; /* BUG! Should not be called... */
191 }
192
193
194
195
196 int wxHtmlContainerCell::GetIndentUnits(int ind) const
197 {
198 bool p = FALSE;
199 if (ind & wxHTML_INDENT_LEFT) p = m_IndentLeft < 0;
200 else if (ind & wxHTML_INDENT_RIGHT) p = m_IndentRight < 0;
201 else if (ind & wxHTML_INDENT_TOP) p = m_IndentTop < 0;
202 else if (ind & wxHTML_INDENT_BOTTOM) p = m_IndentBottom < 0;
203 if (p) return wxHTML_UNITS_PERCENT;
204 else return wxHTML_UNITS_PIXELS;
205 }
206
207
208
209 bool wxHtmlContainerCell::AdjustPagebreak(int *pagebreak) const
210 {
211 if (!m_CanLiveOnPagebreak)
212 return wxHtmlCell::AdjustPagebreak(pagebreak);
213
214 else
215 {
216 wxHtmlCell *c = GetFirstCell();
217 bool rt = FALSE;
218 int pbrk = *pagebreak - m_PosY;
219
220 while (c)
221 {
222 if (c->AdjustPagebreak(&pbrk))
223 rt = TRUE;
224 c = c->GetNext();
225 }
226 if (rt)
227 *pagebreak = pbrk + m_PosY;
228 return rt;
229 }
230 }
231
232
233
234 void wxHtmlContainerCell::Layout(int w)
235 {
236 wxHtmlCell::Layout(w);
237
238 if (m_LastLayout == w) return;
239
240 // VS: Any attempt to layout with negative or zero width leads to hell,
241 // but we can't ignore such attempts completely, since it sometimes
242 // happen (e.g. when trying how small a table can be). The best thing we
243 // can do is to set the width of child cells to zero
244 if (w < 1)
245 {
246 m_Width = 0;
247 for (wxHtmlCell *cell = m_Cells; cell; cell = cell->GetNext())
248 cell->Layout(0);
249 // this does two things: it recursively calls this code on all child
250 // contrainers and resets children's position to (0,0)
251 return;
252 }
253
254 wxHtmlCell *cell = m_Cells, *line = m_Cells;
255 long xpos = 0, ypos = m_IndentTop;
256 int xdelta = 0, ybasicpos = 0, ydiff;
257 int s_width, s_indent;
258 int ysizeup = 0, ysizedown = 0;
259 int MaxLineWidth = 0;
260 int xcnt = 0;
261
262
263 /*
264
265 WIDTH ADJUSTING :
266
267 */
268
269 if (m_WidthFloatUnits == wxHTML_UNITS_PERCENT)
270 {
271 if (m_WidthFloat < 0) m_Width = (100 + m_WidthFloat) * w / 100;
272 else m_Width = m_WidthFloat * w / 100;
273 }
274 else
275 {
276 if (m_WidthFloat < 0) m_Width = w + m_WidthFloat;
277 else m_Width = m_WidthFloat;
278 }
279
280 if (m_Cells)
281 {
282 int l = (m_IndentLeft < 0) ? (-m_IndentLeft * m_Width / 100) : m_IndentLeft;
283 int r = (m_IndentRight < 0) ? (-m_IndentRight * m_Width / 100) : m_IndentRight;
284 for (wxHtmlCell *cell = m_Cells; cell; cell = cell->GetNext())
285 cell->Layout(m_Width - (l + r));
286 }
287
288 /*
289
290 LAYOUTING :
291
292 */
293
294 // adjust indentation:
295 s_indent = (m_IndentLeft < 0) ? (-m_IndentLeft * m_Width / 100) : m_IndentLeft;
296 s_width = m_Width - s_indent - ((m_IndentRight < 0) ? (-m_IndentRight * m_Width / 100) : m_IndentRight);
297
298 // my own layouting:
299 while (cell != NULL)
300 {
301 switch (m_AlignVer)
302 {
303 case wxHTML_ALIGN_TOP : ybasicpos = 0; break;
304 case wxHTML_ALIGN_BOTTOM : ybasicpos = - cell->GetHeight(); break;
305 case wxHTML_ALIGN_CENTER : ybasicpos = - cell->GetHeight() / 2; break;
306 }
307 ydiff = cell->GetHeight() + ybasicpos;
308
309 if (cell->GetDescent() + ydiff > ysizedown) ysizedown = cell->GetDescent() + ydiff;
310 if (ybasicpos + cell->GetDescent() < -ysizeup) ysizeup = - (ybasicpos + cell->GetDescent());
311
312 cell->SetPos(xpos, ybasicpos + cell->GetDescent());
313 xpos += cell->GetWidth();
314 cell = cell->GetNext();
315 xcnt++;
316
317 // force new line if occured:
318 if ((cell == NULL) || (xpos + cell->GetWidth() > s_width))
319 {
320 if (xpos > MaxLineWidth) MaxLineWidth = xpos;
321 if (ysizeup < 0) ysizeup = 0;
322 if (ysizedown < 0) ysizedown = 0;
323 switch (m_AlignHor) {
324 case wxHTML_ALIGN_LEFT :
325 case wxHTML_ALIGN_JUSTIFY :
326 xdelta = 0;
327 break;
328 case wxHTML_ALIGN_RIGHT :
329 xdelta = 0 + (s_width - xpos);
330 break;
331 case wxHTML_ALIGN_CENTER :
332 xdelta = 0 + (s_width - xpos) / 2;
333 break;
334 }
335 if (xdelta < 0) xdelta = 0;
336 xdelta += s_indent;
337
338 ypos += ysizeup;
339
340 if (m_AlignHor != wxHTML_ALIGN_JUSTIFY || cell == NULL)
341 while (line != cell)
342 {
343 line->SetPos(line->GetPosX() + xdelta,
344 ypos + line->GetPosY());
345 line = line->GetNext();
346 }
347 else
348 {
349 int counter = 0;
350 int step = (s_width - xpos);
351 if (step < 0) step = 0;
352 xcnt--;
353 if (xcnt > 0) while (line != cell)
354 {
355 line->SetPos(line->GetPosX() + s_indent +
356 (counter++ * step / xcnt),
357 ypos + line->GetPosY());
358 line = line->GetNext();
359 }
360 xcnt++;
361 }
362
363 ypos += ysizedown;
364 xpos = xcnt = 0;
365 ysizeup = ysizedown = 0;
366 line = cell;
367 }
368 }
369
370 // setup height & width, depending on container layout:
371 m_Height = ypos + (ysizedown + ysizeup) + m_IndentBottom;
372
373 if (m_Height < m_MinHeight)
374 {
375 if (m_MinHeightAlign != wxHTML_ALIGN_TOP)
376 {
377 int diff = m_MinHeight - m_Height;
378 if (m_MinHeightAlign == wxHTML_ALIGN_CENTER) diff /= 2;
379 cell = m_Cells;
380 while (cell)
381 {
382 cell->SetPos(cell->GetPosX(), cell->GetPosY() + diff);
383 cell = cell->GetNext();
384 }
385 }
386 m_Height = m_MinHeight;
387 }
388
389 MaxLineWidth += s_indent + ((m_IndentRight < 0) ? (-m_IndentRight * m_Width / 100) : m_IndentRight);
390 if (m_Width < MaxLineWidth) m_Width = MaxLineWidth;
391
392 m_LastLayout = w;
393 }
394
395
396 #define mMin(a, b) (((a) < (b)) ? (a) : (b))
397 #define mMax(a, b) (((a) < (b)) ? (b) : (a))
398
399 void wxHtmlContainerCell::Draw(wxDC& dc, int x, int y, int view_y1, int view_y2)
400 {
401 // container visible, draw it:
402 if ((y + m_PosY < view_y2) && (y + m_PosY + m_Height > view_y1))
403 {
404
405 if (m_UseBkColour)
406 {
407 wxBrush myb = wxBrush(m_BkColour, wxSOLID);
408
409 int real_y1 = mMax(y + m_PosY, view_y1);
410 int real_y2 = mMin(y + m_PosY + m_Height - 1, view_y2);
411
412 dc.SetBrush(myb);
413 dc.SetPen(*wxTRANSPARENT_PEN);
414 dc.DrawRectangle(x + m_PosX, real_y1, m_Width, real_y2 - real_y1 + 1);
415 }
416
417 if (m_UseBorder)
418 {
419 wxPen mypen1(m_BorderColour1, 1, wxSOLID);
420 wxPen mypen2(m_BorderColour2, 1, wxSOLID);
421
422 dc.SetPen(mypen1);
423 dc.DrawLine(x + m_PosX, y + m_PosY, x + m_PosX, y + m_PosY + m_Height - 1);
424 dc.DrawLine(x + m_PosX, y + m_PosY, x + m_PosX + m_Width - 1, y + m_PosY);
425 dc.SetPen(mypen2);
426 dc.DrawLine(x + m_PosX + m_Width - 1, y + m_PosY, x + m_PosX + m_Width - 1, y + m_PosY + m_Height - 1);
427 dc.DrawLine(x + m_PosX, y + m_PosY + m_Height - 1, x + m_PosX + m_Width - 1, y + m_PosY + m_Height - 1);
428 }
429
430 if (m_Cells)
431 {
432 for (wxHtmlCell *cell = m_Cells; cell; cell = cell->GetNext())
433 cell->Draw(dc, x + m_PosX, y + m_PosY, view_y1, view_y2);
434 }
435 }
436 // container invisible, just proceed font+color changing:
437 else
438 {
439 if (m_Cells)
440 {
441 for (wxHtmlCell *cell = m_Cells; cell; cell = cell->GetNext())
442 cell->DrawInvisible(dc, x + m_PosX, y + m_PosY);
443 }
444 }
445 }
446
447
448
449 void wxHtmlContainerCell::DrawInvisible(wxDC& dc, int x, int y)
450 {
451 if (m_Cells)
452 {
453 for (wxHtmlCell *cell = m_Cells; cell; cell = cell->GetNext())
454 cell->DrawInvisible(dc, x + m_PosX, y + m_PosY);
455 }
456 }
457
458
459
460 wxHtmlLinkInfo *wxHtmlContainerCell::GetLink(int x, int y) const
461 {
462 wxHtmlCell *c = m_Cells;
463 int cx, cy, cw, ch;
464
465 while (c)
466 {
467 cx = c->GetPosX(), cy = c->GetPosY();
468 cw = c->GetWidth(), ch = c->GetHeight();
469 if ((x >= cx) && (x < cx + cw) && (y >= cy) && (y < cy + ch))
470 return c->GetLink(x - cx, y - cy);
471 c = c->GetNext();
472 }
473 return NULL;
474 }
475
476
477
478 void wxHtmlContainerCell::InsertCell(wxHtmlCell *f)
479 {
480 if (!m_Cells) m_Cells = m_LastCell = f;
481 else
482 {
483 m_LastCell->SetNext(f);
484 m_LastCell = f;
485 if (m_LastCell) while (m_LastCell->GetNext()) m_LastCell = m_LastCell->GetNext();
486 }
487 f->SetParent(this);
488 m_LastLayout = -1;
489 }
490
491
492
493 void wxHtmlContainerCell::SetAlign(const wxHtmlTag& tag)
494 {
495 if (tag.HasParam(wxT("ALIGN")))
496 {
497 wxString alg = tag.GetParam(wxT("ALIGN"));
498 alg.MakeUpper();
499 if (alg == wxT("CENTER"))
500 SetAlignHor(wxHTML_ALIGN_CENTER);
501 else if (alg == wxT("LEFT"))
502 SetAlignHor(wxHTML_ALIGN_LEFT);
503 else if (alg == wxT("JUSTIFY"))
504 SetAlignHor(wxHTML_ALIGN_JUSTIFY);
505 else if (alg == wxT("RIGHT"))
506 SetAlignHor(wxHTML_ALIGN_RIGHT);
507 m_LastLayout = -1;
508 }
509 }
510
511
512
513 void wxHtmlContainerCell::SetWidthFloat(const wxHtmlTag& tag, double pixel_scale)
514 {
515 if (tag.HasParam(wxT("WIDTH")))
516 {
517 int wdi;
518 wxString wd = tag.GetParam(wxT("WIDTH"));
519
520 if (wd[wd.Length()-1] == wxT('%'))
521 {
522 wxSscanf(wd.c_str(), wxT("%i%%"), &wdi);
523 SetWidthFloat(wdi, wxHTML_UNITS_PERCENT);
524 }
525 else
526 {
527 wxSscanf(wd.c_str(), wxT("%i"), &wdi);
528 SetWidthFloat((int)(pixel_scale * (double)wdi), wxHTML_UNITS_PIXELS);
529 }
530 m_LastLayout = -1;
531 }
532 }
533
534
535
536 const wxHtmlCell* wxHtmlContainerCell::Find(int condition, const void* param) const
537 {
538 if (m_Cells)
539 {
540 const wxHtmlCell *r = NULL;
541
542 for (wxHtmlCell *cell = m_Cells; cell; cell = cell->GetNext())
543 {
544 r = cell->Find(condition, param);
545 if (r) return r;
546 }
547 }
548 return NULL;
549 }
550
551
552
553 void wxHtmlContainerCell::OnMouseClick(wxWindow *parent, int x, int y, const wxMouseEvent& event)
554 {
555 if (m_Cells)
556 {
557 wxHtmlCell *c = m_Cells;
558 while (c)
559 {
560 if ( (c->GetPosX() <= x) &&
561 (c->GetPosY() <= y) &&
562 (c->GetPosX() + c->GetWidth() > x) &&
563 (c->GetPosY() + c->GetHeight() > y))
564 {
565 c->OnMouseClick(parent, x - c->GetPosX(), y - c->GetPosY(), event);
566 break;
567 }
568 c = c->GetNext();
569 }
570 }
571 }
572
573
574
575 void wxHtmlContainerCell::GetHorizontalConstraints(int *left, int *right) const
576 {
577 int cleft = m_PosX + m_Width, cright = m_PosX; // worst case
578 int l, r;
579
580 for (wxHtmlCell *cell = m_Cells; cell; cell = cell->GetNext())
581 {
582 cell->GetHorizontalConstraints(&l, &r);
583 if (l < cleft)
584 cleft = l;
585 if (r > cright)
586 cright = r;
587 }
588
589 cleft -= (m_IndentLeft < 0) ? (-m_IndentLeft * m_Width / 100) : m_IndentLeft;
590 cright += (m_IndentRight < 0) ? (-m_IndentRight * m_Width / 100) : m_IndentRight;
591
592 if (left)
593 *left = cleft;
594 if (right)
595 *right = cright;
596 }
597
598
599
600
601
602 //--------------------------------------------------------------------------------
603 // wxHtmlColourCell
604 //--------------------------------------------------------------------------------
605
606 void wxHtmlColourCell::Draw(wxDC& dc, int WXUNUSED(x), int WXUNUSED(y), int WXUNUSED(view_y1), int WXUNUSED(view_y2))
607 {
608 if (m_Flags & wxHTML_CLR_FOREGROUND)
609 dc.SetTextForeground(m_Colour);
610 if (m_Flags & wxHTML_CLR_BACKGROUND)
611 {
612 dc.SetBackground(wxBrush(m_Colour, wxSOLID));
613 dc.SetTextBackground(m_Colour);
614 }
615 }
616
617 void wxHtmlColourCell::DrawInvisible(wxDC& dc, int WXUNUSED(x), int WXUNUSED(y))
618 {
619 if (m_Flags & wxHTML_CLR_FOREGROUND)
620 dc.SetTextForeground(m_Colour);
621 if (m_Flags & wxHTML_CLR_BACKGROUND)
622 {
623 dc.SetBackground(wxBrush(m_Colour, wxSOLID));
624 dc.SetTextBackground(m_Colour);
625 }
626 }
627
628
629
630
631 //--------------------------------------------------------------------------------
632 // wxHtmlFontCell
633 //--------------------------------------------------------------------------------
634
635 void wxHtmlFontCell::Draw(wxDC& dc, int WXUNUSED(x), int WXUNUSED(y), int WXUNUSED(view_y1), int WXUNUSED(view_y2))
636 {
637 dc.SetFont(m_Font);
638 }
639
640 void wxHtmlFontCell::DrawInvisible(wxDC& dc, int WXUNUSED(x), int WXUNUSED(y))
641 {
642 dc.SetFont(m_Font);
643 }
644
645
646
647
648
649
650
651
652 //--------------------------------------------------------------------------------
653 // wxHtmlWidgetCell
654 //--------------------------------------------------------------------------------
655
656 wxHtmlWidgetCell::wxHtmlWidgetCell(wxWindow *wnd, int w)
657 {
658 int sx, sy;
659 m_Wnd = wnd;
660 m_Wnd->GetSize(&sx, &sy);
661 m_Width = sx, m_Height = sy;
662 m_WidthFloat = w;
663 }
664
665
666 void wxHtmlWidgetCell::Draw(wxDC& WXUNUSED(dc), int WXUNUSED(x), int WXUNUSED(y), int WXUNUSED(view_y1), int WXUNUSED(view_y2))
667 {
668 int absx = 0, absy = 0, stx, sty;
669 wxHtmlCell *c = this;
670
671 while (c)
672 {
673 absx += c->GetPosX();
674 absy += c->GetPosY();
675 c = c->GetParent();
676 }
677
678 ((wxScrolledWindow*)(m_Wnd->GetParent()))->GetViewStart(&stx, &sty);
679 m_Wnd->SetSize(absx - wxHTML_SCROLL_STEP * stx, absy - wxHTML_SCROLL_STEP * sty, m_Width, m_Height);
680 }
681
682
683
684 void wxHtmlWidgetCell::DrawInvisible(wxDC& WXUNUSED(dc), int WXUNUSED(x), int WXUNUSED(y))
685 {
686 int absx = 0, absy = 0, stx, sty;
687 wxHtmlCell *c = this;
688
689 while (c)
690 {
691 absx += c->GetPosX();
692 absy += c->GetPosY();
693 c = c->GetParent();
694 }
695
696 ((wxScrolledWindow*)(m_Wnd->GetParent()))->GetViewStart(&stx, &sty);
697 m_Wnd->SetSize(absx - wxHTML_SCROLL_STEP * stx, absy - wxHTML_SCROLL_STEP * sty, m_Width, m_Height);
698 }
699
700
701
702 void wxHtmlWidgetCell::Layout(int w)
703 {
704 if (m_WidthFloat != 0)
705 {
706 m_Width = (w * m_WidthFloat) / 100;
707 m_Wnd->SetSize(m_Width, m_Height);
708 }
709
710 wxHtmlCell::Layout(w);
711 }
712
713 #endif