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