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