]> git.saurik.com Git - wxWidgets.git/blame - src/html/m_tables.cpp
No real changes, just make wxWindow::CanScroll() virtual.
[wxWidgets.git] / src / html / m_tables.cpp
CommitLineData
5526e819 1/////////////////////////////////////////////////////////////////////////////
93763ad5 2// Name: src/html/m_tables.cpp
5526e819
VS
3// Purpose: wxHtml module for tables
4// Author: Vaclav Slavik
5// Copyright: (c) 1999 Vaclav Slavik
65571936 6// Licence: wxWindows licence
5526e819
VS
7/////////////////////////////////////////////////////////////////////////////
8
3096bd2f 9#include "wx/wxprec.h"
3364ab79 10
2b5f62a0 11#ifdef __BORLANDC__
93763ad5 12 #pragma hdrstop
3364ab79
RS
13#endif
14
93763ad5
WS
15#if wxUSE_HTML && wxUSE_STREAMS
16
b4f4d3dd 17#ifndef WX_PRECOMP
193d0c93 18 #include "wx/wxcrtvararg.h"
929bb9d2 19 #include "wx/brush.h"
3364ab79
RS
20#endif
21
69941f05
VS
22#include "wx/html/forcelnk.h"
23#include "wx/html/m_templ.h"
5526e819 24
69941f05 25#include "wx/html/htmlcell.h"
5526e819 26
c88293a4 27FORCE_LINK_ME(m_tables)
5526e819
VS
28
29
30#define TABLE_BORDER_CLR_1 wxColour(0xC5, 0xC2, 0xC5)
31#define TABLE_BORDER_CLR_2 wxColour(0x62, 0x61, 0x62)
32
33
34//-----------------------------------------------------------------------------
35// wxHtmlTableCell
36//-----------------------------------------------------------------------------
37
38
79d6c018
VS
39struct colStruct
40{
41 int width, units;
42 // width of the column either in pixels or percents
43 // ('width' is the number, 'units' determines its meaning)
44 int minWidth, maxWidth;
45 // minimal/maximal column width. This is needed by HTML 4.0
d13b34d3 46 // layout algorithm and can be determined by trying to
79d6c018
VS
47 // layout table cells with width=1 and width=infinity
48 int leftpos, pixwidth, maxrealwidth;
49 // temporary (depends on actual width of table)
50};
51
52enum cellState
53{
54 cellSpan,
55 cellUsed,
56 cellFree
57};
58
59struct cellStruct
60{
61 wxHtmlContainerCell *cont;
62 int colspan, rowspan;
63 int minheight, valign;
64 cellState flag;
ca16b7a9 65 bool nowrap;
79d6c018 66};
5526e819
VS
67
68
69class wxHtmlTableCell : public wxHtmlContainerCell
70{
79d6c018
VS
71protected:
72 /* These are real attributes: */
73
79d6c018
VS
74 // number of columns; rows
75 int m_NumCols, m_NumRows;
76 // array of column information
77 colStruct *m_ColsInfo;
78 // 2D array of all cells in the table : m_CellInfo[row][column]
79 cellStruct **m_CellInfo;
80 // spaces between cells
81 int m_Spacing;
82 // cells internal indentation
83 int m_Padding;
84
85private:
86 /* ...and these are valid only when parsing the table: */
87
88 // number of actual column (ranging from 0..m_NumCols)
89 int m_ActualCol, m_ActualRow;
90
91 // default values (for table and row):
92 wxColour m_tBkg, m_rBkg;
93 wxString m_tValign, m_rValign;
94
95 double m_PixelScale;
96
97
98public:
99 wxHtmlTableCell(wxHtmlContainerCell *parent, const wxHtmlTag& tag, double pixel_scale = 1.0);
d3c7fc99 100 virtual ~wxHtmlTableCell();
3bc7a423
VS
101
102 virtual void RemoveExtraSpacing(bool top, bool bottom);
103
79d6c018
VS
104 virtual void Layout(int w);
105
106 void AddRow(const wxHtmlTag& tag);
107 void AddCell(wxHtmlContainerCell *cell, const wxHtmlTag& tag);
108
ff8af61c
VZ
109 const wxColour& GetRowDefaultBackgroundColour() const { return m_rBkg; }
110
79d6c018
VS
111private:
112 // Reallocates memory to given number of cols/rows
113 // and changes m_NumCols/m_NumRows value to reflect this change
114 // NOTE! You CAN'T change m_NumCols/m_NumRows before calling this!!
115 void ReallocCols(int cols);
116 void ReallocRows(int rows);
117
118 // Computes minimal and maximal widths of columns. Needs to be called
25c71ccc 119 // only once, before first Layout().
79d6c018 120 void ComputeMinMaxWidths();
22f3361e 121
c0c133e1 122 wxDECLARE_NO_COPY_CLASS(wxHtmlTableCell);
5526e819
VS
123};
124
125
126
edbd0635 127wxHtmlTableCell::wxHtmlTableCell(wxHtmlContainerCell *parent, const wxHtmlTag& tag, double pixel_scale)
5526e819
VS
128 : wxHtmlContainerCell(parent)
129{
edbd0635 130 m_PixelScale = pixel_scale;
5526e819
VS
131 m_ColsInfo = NULL;
132 m_NumCols = m_NumRows = 0;
133 m_CellInfo = NULL;
134 m_ActualCol = m_ActualRow = -1;
135
136 /* scan params: */
04dbb646 137 if (tag.HasParam(wxT("BGCOLOR")))
884898a7 138 {
8bd72d90 139 tag.GetParamAsColour(wxT("BGCOLOR"), &m_tBkg);
a1b806b9 140 if (m_tBkg.IsOk())
884898a7
VZ
141 SetBackgroundColour(m_tBkg);
142 }
04dbb646
VZ
143 if (tag.HasParam(wxT("VALIGN")))
144 m_tValign = tag.GetParam(wxT("VALIGN"));
145 else
8bd72d90
VS
146 m_tValign = wxEmptyString;
147 if (!tag.GetParamAsInt(wxT("CELLSPACING"), &m_Spacing))
148 m_Spacing = 2;
149 if (!tag.GetParamAsInt(wxT("CELLPADDING"), &m_Padding))
150 m_Padding = 3;
edbd0635
VS
151 m_Spacing = (int)(m_PixelScale * (double)m_Spacing);
152 m_Padding = (int)(m_PixelScale * (double)m_Padding);
5526e819 153
3aaaf1aa
VZ
154 if(tag.HasParam(wxT("BORDER")))
155 {
156 if(tag.GetParam("BORDER").IsEmpty())
157 m_Border = 1;
158 else
159 tag.GetParamAsInt(wxT("BORDER"), &m_Border);
160 }
161 if (m_Border == 1)
162 SetBorder(TABLE_BORDER_CLR_1, TABLE_BORDER_CLR_2, m_Border); // special case see wxHtmlContainerCell::Draw
163 else if (m_Border> 0)
164 SetBorder(TABLE_BORDER_CLR_1, TABLE_BORDER_CLR_2, (int)(m_PixelScale * (double)m_Border));
165 else
166 m_Border = 0;
167
5526e819
VS
168}
169
170
171
172wxHtmlTableCell::~wxHtmlTableCell()
173{
174 if (m_ColsInfo) free(m_ColsInfo);
04dbb646 175 if (m_CellInfo)
4f9297b0 176 {
5526e819
VS
177 for (int i = 0; i < m_NumRows; i++)
178 free(m_CellInfo[i]);
179 free(m_CellInfo);
180 }
181}
182
183
3bc7a423
VS
184void wxHtmlTableCell::RemoveExtraSpacing(bool WXUNUSED(top),
185 bool WXUNUSED(bottom))
186{
187 // Don't remove any spacing in the table -- it's always desirable,
188 // because it's part of table's definition.
189 // (If wxHtmlContainerCell::RemoveExtraSpacing() was applied to tables,
190 // then upper left cell of a table would be positioned above other cells
191 // if the table was the first element on the page.)
192}
5526e819
VS
193
194void wxHtmlTableCell::ReallocCols(int cols)
195{
196 int i,j;
197
04dbb646 198 for (i = 0; i < m_NumRows; i++)
4f9297b0 199 {
5526e819
VS
200 m_CellInfo[i] = (cellStruct*) realloc(m_CellInfo[i], sizeof(cellStruct) * cols);
201 for (j = m_NumCols; j < cols; j++)
202 m_CellInfo[i][j].flag = cellFree;
203 }
204
205 m_ColsInfo = (colStruct*) realloc(m_ColsInfo, sizeof(colStruct) * cols);
04dbb646 206 for (j = m_NumCols; j < cols; j++)
4f9297b0 207 {
5526e819 208 m_ColsInfo[j].width = 0;
efba2b89 209 m_ColsInfo[j].units = wxHTML_UNITS_PERCENT;
79d6c018 210 m_ColsInfo[j].minWidth = m_ColsInfo[j].maxWidth = -1;
5526e819
VS
211 }
212
213 m_NumCols = cols;
214}
215
216
217
218void wxHtmlTableCell::ReallocRows(int rows)
219{
220 m_CellInfo = (cellStruct**) realloc(m_CellInfo, sizeof(cellStruct*) * rows);
04dbb646 221 for (int row = m_NumRows; row < rows ; row++)
80eab469 222 {
04dbb646 223 if (m_NumCols == 0)
80eab469 224 m_CellInfo[row] = NULL;
04dbb646 225 else
80eab469
VS
226 {
227 m_CellInfo[row] = (cellStruct*) malloc(sizeof(cellStruct) * m_NumCols);
228 for (int col = 0; col < m_NumCols; col++)
229 m_CellInfo[row][col].flag = cellFree;
230 }
5526e819 231 }
5526e819
VS
232 m_NumRows = rows;
233}
234
235
5526e819
VS
236void wxHtmlTableCell::AddRow(const wxHtmlTag& tag)
237{
5526e819 238 m_ActualCol = -1;
d361bbff
VS
239 // VS: real allocation of row entry is done in AddCell in order
240 // to correctly handle empty rows (i.e. "<tr></tr>")
241 // m_ActualCol == -1 indicates that AddCell has to allocate new row.
5526e819 242
d361bbff 243 // scan params:
5526e819 244 m_rBkg = m_tBkg;
04dbb646 245 if (tag.HasParam(wxT("BGCOLOR")))
8bd72d90 246 tag.GetParamAsColour(wxT("BGCOLOR"), &m_rBkg);
04dbb646
VZ
247 if (tag.HasParam(wxT("VALIGN")))
248 m_rValign = tag.GetParam(wxT("VALIGN"));
249 else
d361bbff 250 m_rValign = m_tValign;
5526e819
VS
251}
252
253
254
255void wxHtmlTableCell::AddCell(wxHtmlContainerCell *cell, const wxHtmlTag& tag)
256{
d361bbff
VS
257 // Is this cell in new row?
258 // VS: we can't do it in AddRow, see my comment there
259 if (m_ActualCol == -1)
260 {
261 if (m_ActualRow + 1 > m_NumRows - 1)
262 ReallocRows(m_ActualRow + 2);
263 m_ActualRow++;
264 }
265
266 // cells & columns:
04dbb646 267 do
4f9297b0 268 {
5526e819 269 m_ActualCol++;
04dbb646 270 } while ((m_ActualCol < m_NumCols) &&
8bd72d90 271 (m_CellInfo[m_ActualRow][m_ActualCol].flag != cellFree));
04dbb646 272
5526e819
VS
273 if (m_ActualCol > m_NumCols - 1)
274 ReallocCols(m_ActualCol + 1);
275
276 int r = m_ActualRow, c = m_ActualCol;
277
278 m_CellInfo[r][c].cont = cell;
279 m_CellInfo[r][c].colspan = 1;
280 m_CellInfo[r][c].rowspan = 1;
281 m_CellInfo[r][c].flag = cellUsed;
282 m_CellInfo[r][c].minheight = 0;
efba2b89 283 m_CellInfo[r][c].valign = wxHTML_ALIGN_TOP;
5526e819
VS
284
285 /* scan for parameters: */
286
287 // width:
288 {
8bd72d90 289 if (tag.HasParam(wxT("WIDTH")))
2fa3b707 290 {
8bd72d90 291 wxString wd = tag.GetParam(wxT("WIDTH"));
5526e819 292
18764039 293 if (!wd.empty() && wd[wd.length()-1] == wxT('%'))
2fa3b707 294 {
e388dc21
VS
295 if ( wxSscanf(wd.c_str(), wxT("%i%%"), &m_ColsInfo[c].width) == 1 )
296 {
297 m_ColsInfo[c].units = wxHTML_UNITS_PERCENT;
298 }
5526e819 299 }
04dbb646 300 else
2fa3b707 301 {
e388dc21
VS
302 long width;
303 if ( wd.ToLong(&width) )
304 {
305 m_ColsInfo[c].width = (int)(m_PixelScale * (double)width);
306 m_ColsInfo[c].units = wxHTML_UNITS_PIXELS;
307 }
5526e819
VS
308 }
309 }
310 }
311
312
313 // spanning:
314 {
8bd72d90
VS
315 tag.GetParamAsInt(wxT("COLSPAN"), &m_CellInfo[r][c].colspan);
316 tag.GetParamAsInt(wxT("ROWSPAN"), &m_CellInfo[r][c].rowspan);
fbe77cea
VS
317
318 // VS: the standard says this about col/rowspan:
25c71ccc
VZ
319 // "This attribute specifies the number of rows spanned by the
320 // current cell. The default value of this attribute is one ("1").
321 // The value zero ("0") means that the cell spans all rows from the
322 // current row to the last row of the table." All mainstream
fbe77cea 323 // browsers act as if 0==1, though, and so does wxHTML.
25c71ccc 324 if (m_CellInfo[r][c].colspan < 1)
fbe77cea 325 m_CellInfo[r][c].colspan = 1;
25c71ccc 326 if (m_CellInfo[r][c].rowspan < 1)
fbe77cea
VS
327 m_CellInfo[r][c].rowspan = 1;
328
329 if ((m_CellInfo[r][c].colspan > 1) || (m_CellInfo[r][c].rowspan > 1))
2fa3b707 330 {
5526e819
VS
331 int i, j;
332
04dbb646 333 if (r + m_CellInfo[r][c].rowspan > m_NumRows)
8bd72d90 334 ReallocRows(r + m_CellInfo[r][c].rowspan);
04dbb646 335 if (c + m_CellInfo[r][c].colspan > m_NumCols)
8bd72d90 336 ReallocCols(c + m_CellInfo[r][c].colspan);
5526e819
VS
337 for (i = r; i < r + m_CellInfo[r][c].rowspan; i++)
338 for (j = c; j < c + m_CellInfo[r][c].colspan; j++)
339 m_CellInfo[i][j].flag = cellSpan;
340 m_CellInfo[r][c].flag = cellUsed;
341 }
342 }
343
344 //background color:
345 {
8bd72d90 346 wxColour bk = m_rBkg;
04dbb646 347 if (tag.HasParam(wxT("BGCOLOR")))
8bd72d90 348 tag.GetParamAsColour(wxT("BGCOLOR"), &bk);
a1b806b9 349 if (bk.IsOk())
8bd72d90 350 cell->SetBackgroundColour(bk);
5526e819 351 }
3aaaf1aa 352 if (m_Border > 0)
4f9297b0 353 cell->SetBorder(TABLE_BORDER_CLR_2, TABLE_BORDER_CLR_1);
5526e819
VS
354
355 // vertical alignment:
356 {
357 wxString valign;
04dbb646
VZ
358 if (tag.HasParam(wxT("VALIGN")))
359 valign = tag.GetParam(wxT("VALIGN"));
360 else
8bd72d90 361 valign = m_tValign;
5526e819 362 valign.MakeUpper();
04dbb646 363 if (valign == wxT("TOP"))
8bd72d90 364 m_CellInfo[r][c].valign = wxHTML_ALIGN_TOP;
04dbb646 365 else if (valign == wxT("BOTTOM"))
8bd72d90 366 m_CellInfo[r][c].valign = wxHTML_ALIGN_BOTTOM;
efba2b89 367 else m_CellInfo[r][c].valign = wxHTML_ALIGN_CENTER;
5526e819
VS
368 }
369
ca16b7a9
VS
370 // nowrap
371 if (tag.HasParam(wxT("NOWRAP")))
372 m_CellInfo[r][c].nowrap = true;
373 else
374 m_CellInfo[r][c].nowrap = false;
375
4f9297b0 376 cell->SetIndent(m_Padding, wxHTML_INDENT_ALL, wxHTML_UNITS_PIXELS);
5526e819
VS
377}
378
79d6c018
VS
379void wxHtmlTableCell::ComputeMinMaxWidths()
380{
d1da8872 381 if (m_NumCols == 0 || m_ColsInfo[0].minWidth != wxDefaultCoord) return;
25c71ccc 382
ca16b7a9
VS
383 m_MaxTotalWidth = 0;
384 int percentage = 0;
79d6c018
VS
385 for (int c = 0; c < m_NumCols; c++)
386 {
387 for (int r = 0; r < m_NumRows; r++)
388 {
389 cellStruct& cell = m_CellInfo[r][c];
390 if (cell.flag == cellUsed)
391 {
026d1fac 392 cell.cont->Layout(2*m_Padding + 1);
ca16b7a9
VS
393 int maxWidth = cell.cont->GetMaxTotalWidth();
394 int width = cell.nowrap?maxWidth:cell.cont->GetWidth();
026d1fac 395 width -= (cell.colspan-1) * m_Spacing;
ca16b7a9 396 maxWidth -= (cell.colspan-1) * m_Spacing;
79d6c018 397 // HTML 4.0 says it is acceptable to distribute min/max
79d6c018 398 width /= cell.colspan;
ca16b7a9
VS
399 maxWidth /= cell.colspan;
400 for (int j = 0; j < cell.colspan; j++) {
79d6c018
VS
401 if (width > m_ColsInfo[c+j].minWidth)
402 m_ColsInfo[c+j].minWidth = width;
ca16b7a9
VS
403 if (maxWidth > m_ColsInfo[c+j].maxWidth)
404 m_ColsInfo[c+j].maxWidth = maxWidth;
405 }
79d6c018
VS
406 }
407 }
ca16b7a9
VS
408 // Calculate maximum table width, required for nested tables
409 if (m_ColsInfo[c].units == wxHTML_UNITS_PIXELS)
410 m_MaxTotalWidth += wxMax(m_ColsInfo[c].width, m_ColsInfo[c].minWidth);
411 else if ((m_ColsInfo[c].units == wxHTML_UNITS_PERCENT) && (m_ColsInfo[c].width != 0))
412 percentage += m_ColsInfo[c].width;
413 else
414 m_MaxTotalWidth += m_ColsInfo[c].maxWidth;
79d6c018 415 }
5526e819 416
ca16b7a9
VS
417 if (percentage >= 100)
418 {
419 // Table would have infinite length
420 // Make it ridiculous large
421 m_MaxTotalWidth = 0xFFFFFF;
422 }
423 else
424 m_MaxTotalWidth = m_MaxTotalWidth * 100 / (100 - percentage);
425
3aaaf1aa 426 m_MaxTotalWidth += (m_NumCols + 1) * m_Spacing + 2 * m_Border;
ca16b7a9 427}
5526e819
VS
428
429void wxHtmlTableCell::Layout(int w)
430{
79d6c018 431 ComputeMinMaxWidths();
25c71ccc 432
026d1fac 433 wxHtmlCell::Layout(w);
79d6c018 434
5526e819
VS
435 /*
436
437 WIDTH ADJUSTING :
438
439 */
440
04dbb646 441 if (m_WidthFloatUnits == wxHTML_UNITS_PERCENT)
4f9297b0 442 {
ca16b7a9
VS
443 if (m_WidthFloat < 0)
444 {
445 if (m_WidthFloat < -100)
446 m_WidthFloat = -100;
447 m_Width = (100 + m_WidthFloat) * w / 100;
448 }
25c71ccc 449 else
ca16b7a9
VS
450 {
451 if (m_WidthFloat > 100)
452 m_WidthFloat = 100;
453 m_Width = m_WidthFloat * w / 100;
454 }
5526e819 455 }
04dbb646 456 else
4f9297b0 457 {
5526e819
VS
458 if (m_WidthFloat < 0) m_Width = w + m_WidthFloat;
459 else m_Width = m_WidthFloat;
460 }
461
462
463 /*
464
d13b34d3 465 LAYOUT :
5526e819
VS
466
467 */
468
25c71ccc
VZ
469 /* 1. setup columns widths:
470
ca16b7a9
VS
471 The algorithm tries to keep the table size less than w if possible.
472 */
5526e819 473 {
3aaaf1aa 474 int wpix = m_Width - (m_NumCols + 1) * m_Spacing - 2 * m_Border; // Available space for cell content
5526e819 475 int i, j;
5526e819
VS
476
477 // 1a. setup fixed-width columns:
478 for (i = 0; i < m_NumCols; i++)
efba2b89 479 if (m_ColsInfo[i].units == wxHTML_UNITS_PIXELS)
79d6c018 480 {
25c71ccc 481 m_ColsInfo[i].pixwidth = wxMax(m_ColsInfo[i].width,
79d6c018
VS
482 m_ColsInfo[i].minWidth);
483 wpix -= m_ColsInfo[i].pixwidth;
484 }
5526e819 485
ca16b7a9
VS
486 // 1b. Calculate maximum possible width if line wrapping would be disabled
487 // Recalculate total width if m_WidthFloat is zero to keep tables as small
488 // as possible.
489 int maxWidth = 0;
490 for (i = 0; i < m_NumCols; i++)
491 if (m_ColsInfo[i].width == 0)
492 {
493 maxWidth += m_ColsInfo[i].maxWidth;
494 }
495
496 if (!m_WidthFloat)
497 {
498 // Recalculate table width since no table width was initially given
499 int newWidth = m_Width - wpix + maxWidth;
500
501 // Make sure that floating-width columns will have the right size.
502 // Calculate sum of all floating-width columns
503 int percentage = 0;
504 for (i = 0; i < m_NumCols; i++)
505 if ((m_ColsInfo[i].units == wxHTML_UNITS_PERCENT) && (m_ColsInfo[i].width != 0))
506 percentage += m_ColsInfo[i].width;
507
508 if (percentage >= 100)
509 newWidth = w;
510 else
511 newWidth = newWidth * 100 / (100 - percentage);
25c71ccc 512
3aaaf1aa 513 newWidth = wxMin(newWidth, w - (m_NumCols + 1) * m_Spacing - 2 * m_Border);
ca16b7a9
VS
514 wpix -= m_Width - newWidth;
515 m_Width = newWidth;
516 }
25c71ccc 517
ca16b7a9
VS
518
519 // 1c. setup floating-width columns:
520 int wtemp = wpix;
5526e819 521 for (i = 0; i < m_NumCols; i++)
efba2b89 522 if ((m_ColsInfo[i].units == wxHTML_UNITS_PERCENT) && (m_ColsInfo[i].width != 0))
79d6c018 523 {
ca16b7a9
VS
524 m_ColsInfo[i].pixwidth = wxMin(m_ColsInfo[i].width, 100) * wpix / 100;
525
526 // Make sure to leave enough space for the other columns
3aaaf1aa 527 int minRequired = m_Border;
ca16b7a9
VS
528 for (j = 0; j < m_NumCols; j++)
529 {
530 if ((m_ColsInfo[j].units == wxHTML_UNITS_PERCENT && j > i) ||
531 !m_ColsInfo[j].width)
532 minRequired += m_ColsInfo[j].minWidth;
533 }
534 m_ColsInfo[i].pixwidth = wxMax(wxMin(wtemp - minRequired, m_ColsInfo[i].pixwidth), m_ColsInfo[i].minWidth);
535
536 wtemp -= m_ColsInfo[i].pixwidth;
79d6c018 537 }
3aaaf1aa 538 wpix = wtemp; // minimum cells width
ca16b7a9
VS
539
540 // 1d. setup default columns (no width specification supplied):
541 // The algorithm assigns calculates the maximum possible width if line
542 // wrapping would be disabled and assigns column width as a fraction
543 // based upon the maximum width of a column
544 // FIXME: I'm not sure if this algorithm is conform to HTML standard,
545 // though it seems to be much better than the old one
5526e819 546
5526e819
VS
547 for (i = j = 0; i < m_NumCols; i++)
548 if (m_ColsInfo[i].width == 0) j++;
3aaaf1aa
VZ
549 if (wpix < m_Border)
550 wpix = m_Border;
ca16b7a9
VS
551
552 // Assign widths
5526e819
VS
553 for (i = 0; i < m_NumCols; i++)
554 if (m_ColsInfo[i].width == 0)
8c571f46 555 {
ca16b7a9
VS
556 // Assign with, make sure not to drop below minWidth
557 if (maxWidth)
25c71ccc 558 m_ColsInfo[i].pixwidth = (int)(wpix * (m_ColsInfo[i].maxWidth / (float)maxWidth) + 0.5);
ca16b7a9
VS
559 else
560 m_ColsInfo[i].pixwidth = wpix / j;
25c71ccc 561
ca16b7a9
VS
562 // Make sure to leave enough space for the other columns
563 int minRequired = 0;
564 int r;
565 for (r = i + 1; r < m_NumCols; r++)
566 {
567 if (!m_ColsInfo[r].width)
568 minRequired += m_ColsInfo[r].minWidth;
569 }
570 m_ColsInfo[i].pixwidth = wxMax(wxMin(wpix - minRequired, m_ColsInfo[i].pixwidth), m_ColsInfo[i].minWidth);
571
572 if (maxWidth)
573 {
574 if (m_ColsInfo[i].pixwidth > (wpix * (m_ColsInfo[i].maxWidth / (float)maxWidth) + 0.5))
575 {
25c71ccc 576 int diff = (int)(m_ColsInfo[i].pixwidth - (wpix * m_ColsInfo[i].maxWidth / (float)maxWidth + 0.5));
ca16b7a9
VS
577 maxWidth += diff - m_ColsInfo[i].maxWidth;
578 }
579 else
580 maxWidth -= m_ColsInfo[i].maxWidth;
581 }
582 wpix -= m_ColsInfo[i].pixwidth;
8c571f46 583 }
5526e819
VS
584 }
585
586 /* 2. compute positions of columns: */
587 {
3aaaf1aa 588 int wpos = m_Spacing + m_Border;
04dbb646 589 for (int i = 0; i < m_NumCols; i++)
2fa3b707 590 {
5526e819
VS
591 m_ColsInfo[i].leftpos = wpos;
592 wpos += m_ColsInfo[i].pixwidth + m_Spacing;
593 }
7ed0328e
VZ
594
595 // add the remaining space to the last column
3aaaf1aa
VZ
596 if (m_NumCols > 0 && wpos < m_Width - m_Border)
597 m_ColsInfo[m_NumCols-1].pixwidth += m_Width - wpos - m_Border;
5526e819
VS
598 }
599
600 /* 3. sub-layout all cells: */
601 {
2776d7c3 602 int *ypos = new int[m_NumRows + 1];
5526e819
VS
603
604 int actcol, actrow;
605 int fullwid;
606 wxHtmlContainerCell *actcell;
607
3aaaf1aa 608 ypos[0] = m_Spacing + m_Border;
2fa3b707 609 for (actrow = 1; actrow <= m_NumRows; actrow++) ypos[actrow] = -1;
04dbb646 610 for (actrow = 0; actrow < m_NumRows; actrow++)
2fa3b707
VS
611 {
612 if (ypos[actrow] == -1) ypos[actrow] = ypos[actrow-1];
5526e819
VS
613 // 3a. sub-layout and detect max height:
614
615 for (actcol = 0; actcol < m_NumCols; actcol++) {
616 if (m_CellInfo[actrow][actcol].flag != cellUsed) continue;
617 actcell = m_CellInfo[actrow][actcol].cont;
618 fullwid = 0;
619 for (int i = actcol; i < m_CellInfo[actrow][actcol].colspan + actcol; i++)
620 fullwid += m_ColsInfo[i].pixwidth;
a97a264f 621 fullwid += (m_CellInfo[actrow][actcol].colspan - 1) * m_Spacing;
4f9297b0
VS
622 actcell->SetMinHeight(m_CellInfo[actrow][actcol].minheight, m_CellInfo[actrow][actcol].valign);
623 actcell->Layout(fullwid);
5526e819 624
4f9297b0 625 if (ypos[actrow] + actcell->GetHeight() + m_CellInfo[actrow][actcol].rowspan * m_Spacing > ypos[actrow + m_CellInfo[actrow][actcol].rowspan])
5526e819 626 ypos[actrow + m_CellInfo[actrow][actcol].rowspan] =
4f9297b0 627 ypos[actrow] + actcell->GetHeight() + m_CellInfo[actrow][actcol].rowspan * m_Spacing;
5526e819
VS
628 }
629 }
630
04dbb646 631 for (actrow = 0; actrow < m_NumRows; actrow++)
2fa3b707 632 {
5526e819
VS
633 // 3b. place cells in row & let'em all have same height:
634
04dbb646 635 for (actcol = 0; actcol < m_NumCols; actcol++)
2fa3b707 636 {
5526e819
VS
637 if (m_CellInfo[actrow][actcol].flag != cellUsed) continue;
638 actcell = m_CellInfo[actrow][actcol].cont;
4f9297b0 639 actcell->SetMinHeight(
a97a264f 640 ypos[actrow + m_CellInfo[actrow][actcol].rowspan] - ypos[actrow] - m_Spacing,
5526e819
VS
641 m_CellInfo[actrow][actcol].valign);
642 fullwid = 0;
643 for (int i = actcol; i < m_CellInfo[actrow][actcol].colspan + actcol; i++)
644 fullwid += m_ColsInfo[i].pixwidth;
a97a264f 645 fullwid += (m_CellInfo[actrow][actcol].colspan - 1) * m_Spacing;
4f9297b0
VS
646 actcell->Layout(fullwid);
647 actcell->SetPos(m_ColsInfo[actcol].leftpos, ypos[actrow]);
5526e819 648 }
5526e819 649 }
3aaaf1aa 650 m_Height = ypos[m_NumRows] + m_Border;
2776d7c3 651 delete[] ypos;
5526e819 652 }
8c571f46
VS
653
654 /* 4. adjust table's width if it was too small: */
655 if (m_NumCols > 0)
656 {
25c71ccc 657 int twidth = m_ColsInfo[m_NumCols-1].leftpos +
3aaaf1aa 658 m_ColsInfo[m_NumCols-1].pixwidth + m_Spacing + m_Border;
8c571f46
VS
659 if (twidth > m_Width)
660 m_Width = twidth;
661 }
5526e819
VS
662}
663
664
665
666
667
668
669//-----------------------------------------------------------------------------
670// The tables handler:
671//-----------------------------------------------------------------------------
672
673
674TAG_HANDLER_BEGIN(TABLE, "TABLE,TR,TD,TH")
675
676 TAG_HANDLER_VARS
677 wxHtmlTableCell* m_Table;
678 wxString m_tAlign, m_rAlign;
55ad60c9 679 wxHtmlContainerCell *m_enclosingContainer;
5526e819 680
ff8af61c
VZ
681 // Call ParseInner() preserving background colour and mode after any
682 // changes done by it.
683 void CallParseInnerWithBg(const wxHtmlTag& tag, const wxColour& colBg)
684 {
685 const wxColour oldbackclr = m_WParser->GetActualBackgroundColor();
686 const int oldbackmode = m_WParser->GetActualBackgroundMode();
687 if ( colBg.IsOk() )
688 {
689 m_WParser->SetActualBackgroundColor(colBg);
690 m_WParser->SetActualBackgroundMode(wxBRUSHSTYLE_SOLID);
691 m_WParser->GetContainer()->InsertCell(
692 new wxHtmlColourCell(colBg, wxHTML_CLR_BACKGROUND)
693 );
694 }
695
696 ParseInner(tag);
697
698 if ( oldbackmode != m_WParser->GetActualBackgroundMode() ||
699 oldbackclr != m_WParser->GetActualBackgroundColor() )
700 {
701 m_WParser->SetActualBackgroundMode(oldbackmode);
702 m_WParser->SetActualBackgroundColor(oldbackclr);
703 m_WParser->GetContainer()->InsertCell(
704 new wxHtmlColourCell(oldbackclr,
705 oldbackmode == wxBRUSHSTYLE_TRANSPARENT
706 ? wxHTML_CLR_TRANSPARENT_BACKGROUND
707 : wxHTML_CLR_BACKGROUND)
708 );
709 }
710 }
711
5526e819
VS
712 TAG_HANDLER_CONSTR(TABLE)
713 {
714 m_Table = NULL;
55ad60c9 715 m_enclosingContainer = NULL;
01325161 716 m_tAlign = m_rAlign = wxEmptyString;
5526e819
VS
717 }
718
719
720 TAG_HANDLER_PROC(tag)
721 {
722 wxHtmlContainerCell *c;
723
724 // new table started, backup upper-level table (if any) and create new:
04dbb646 725 if (tag.GetName() == wxT("TABLE"))
2fa3b707 726 {
5526e819 727 wxHtmlTableCell *oldt = m_Table;
5526e819 728
a90db8ee 729 wxHtmlContainerCell *oldEnclosing = m_enclosingContainer;
55ad60c9 730 m_enclosingContainer = c = m_WParser->OpenContainer();
5526e819 731
6987a0c4 732 m_Table = new wxHtmlTableCell(c, tag, m_WParser->GetPixelScale());
ca16b7a9
VS
733
734 // width:
735 {
736 if (tag.HasParam(wxT("WIDTH")))
737 {
e0e4b2b0
VZ
738 int width = 0;
739 bool wpercent = false;
740 if (tag.GetParamAsIntOrPercent(wxT("WIDTH"), &width, wpercent))
ca16b7a9 741 {
e0e4b2b0
VZ
742 if (wpercent)
743 {
744 m_Table->SetWidthFloat(width, wxHTML_UNITS_PERCENT);
745 }
746 else
747 {
748 m_Table->SetWidthFloat((int)(m_WParser->GetPixelScale() * width), wxHTML_UNITS_PIXELS);
749 }
ca16b7a9
VS
750 }
751 }
752 else
753 m_Table->SetWidthFloat(0, wxHTML_UNITS_PIXELS);
754 }
2755d437 755 int oldAlign = m_WParser->GetAlign();
5526e819 756 m_tAlign = wxEmptyString;
8bd72d90
VS
757 if (tag.HasParam(wxT("ALIGN")))
758 m_tAlign = tag.GetParam(wxT("ALIGN"));
5526e819 759
ff8af61c 760 CallParseInnerWithBg(tag, m_Table->GetBackgroundColour());
5526e819 761
2755d437 762 m_WParser->SetAlign(oldAlign);
55ad60c9 763 m_WParser->SetContainer(m_enclosingContainer);
4f9297b0 764 m_WParser->CloseContainer();
25c71ccc 765
5526e819 766 m_Table = oldt;
a90db8ee 767 m_enclosingContainer = oldEnclosing;
55ad60c9
VS
768
769 return true; // ParseInner() called
5526e819
VS
770 }
771
772
211dfedd 773 else if (m_Table)
2fa3b707 774 {
5526e819 775 // new row in table
04dbb646 776 if (tag.GetName() == wxT("TR"))
2fa3b707 777 {
4f9297b0 778 m_Table->AddRow(tag);
5526e819 779 m_rAlign = m_tAlign;
04dbb646 780 if (tag.HasParam(wxT("ALIGN")))
8bd72d90 781 m_rAlign = tag.GetParam(wxT("ALIGN"));
5526e819
VS
782 }
783
784 // new cell
04dbb646 785 else
2fa3b707 786 {
4f9297b0
VS
787 c = m_WParser->SetContainer(new wxHtmlContainerCell(m_Table));
788 m_Table->AddCell(c, tag);
5526e819 789
4f9297b0 790 m_WParser->OpenContainer();
5526e819 791
f14217ab 792 const bool isHeader = tag.GetName() == wxT("TH");
2755d437
VS
793
794 wxString als;
2755d437
VS
795 if (tag.HasParam(wxT("ALIGN")))
796 als = tag.GetParam(wxT("ALIGN"));
f14217ab
VZ
797 else
798 als = m_rAlign;
2755d437 799 als.MakeUpper();
f14217ab 800
2755d437
VS
801 if (als == wxT("RIGHT"))
802 m_WParser->SetAlign(wxHTML_ALIGN_RIGHT);
803 else if (als == wxT("LEFT"))
804 m_WParser->SetAlign(wxHTML_ALIGN_LEFT);
805 else if (als == wxT("CENTER"))
806 m_WParser->SetAlign(wxHTML_ALIGN_CENTER);
f14217ab
VZ
807 else // use default alignment
808 m_WParser->SetAlign(isHeader ? wxHTML_ALIGN_CENTER
809 : wxHTML_ALIGN_LEFT);
2755d437 810
4f9297b0 811 m_WParser->OpenContainer();
55ad60c9 812
f14217ab 813 // the header should be rendered in bold by default
8793514e 814 int boldOld = 0;
f14217ab
VZ
815 if ( isHeader )
816 {
817 boldOld = m_WParser->GetFontBold();
818 m_WParser->SetFontBold(true);
819 m_WParser->GetContainer()->InsertCell(
820 new wxHtmlFontCell(m_WParser->CreateCurrentFont()));
821 }
822
4bfa6c94
VZ
823 wxColour bgCol;
824 if ( !tag.GetParamAsColour(wxT("BGCOLOR"), &bgCol) )
825 bgCol = m_Table->GetRowDefaultBackgroundColour();
826
827 CallParseInnerWithBg(tag, bgCol);
55ad60c9 828
f14217ab
VZ
829 if ( isHeader )
830 {
831 m_WParser->SetFontBold(boldOld);
832 m_WParser->GetContainer()->InsertCell(
833 new wxHtmlFontCell(m_WParser->CreateCurrentFont()));
834 }
835
55ad60c9
VS
836 // set the current container back to the enclosing one so that
837 // text outside of <th> and <td> isn't included in any cell
838 // (this happens often enough in practice because it's common
839 // to use whitespace between </td> and the next <td>):
840 m_WParser->SetContainer(m_enclosingContainer);
841
842 return true; // ParseInner() called
5526e819
VS
843 }
844 }
55ad60c9 845
d1da8872 846 return false;
5526e819
VS
847 }
848
849TAG_HANDLER_END(TABLE)
850
851
852
853
854
855TAGS_MODULE_BEGIN(Tables)
856
857 TAGS_MODULE_ADD(TABLE)
858
859TAGS_MODULE_END(Tables)
860
861
862#endif