]>
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 | |
46 | // layouting algorithm and can be determined by trying to | |
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 | ||
74 | // should we draw borders or not? | |
75 | bool m_HasBorders; | |
76 | // number of columns; rows | |
77 | int m_NumCols, m_NumRows; | |
78 | // array of column information | |
79 | colStruct *m_ColsInfo; | |
80 | // 2D array of all cells in the table : m_CellInfo[row][column] | |
81 | cellStruct **m_CellInfo; | |
82 | // spaces between cells | |
83 | int m_Spacing; | |
84 | // cells internal indentation | |
85 | int m_Padding; | |
86 | ||
87 | private: | |
88 | /* ...and these are valid only when parsing the table: */ | |
89 | ||
90 | // number of actual column (ranging from 0..m_NumCols) | |
91 | int m_ActualCol, m_ActualRow; | |
92 | ||
93 | // default values (for table and row): | |
94 | wxColour m_tBkg, m_rBkg; | |
95 | wxString m_tValign, m_rValign; | |
96 | ||
97 | double m_PixelScale; | |
98 | ||
99 | ||
100 | public: | |
101 | wxHtmlTableCell(wxHtmlContainerCell *parent, const wxHtmlTag& tag, double pixel_scale = 1.0); | |
d3c7fc99 | 102 | virtual ~wxHtmlTableCell(); |
3bc7a423 VS |
103 | |
104 | virtual void RemoveExtraSpacing(bool top, bool bottom); | |
105 | ||
79d6c018 VS |
106 | virtual void Layout(int w); |
107 | ||
108 | void AddRow(const wxHtmlTag& tag); | |
109 | void AddCell(wxHtmlContainerCell *cell, const wxHtmlTag& tag); | |
110 | ||
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 VZ |
121 | |
122 | DECLARE_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; |
25c71ccc | 131 | m_HasBorders = |
b0bdbbfe | 132 | (tag.HasParam(wxT("BORDER")) && tag.GetParam(wxT("BORDER")) != wxT("0")); |
5526e819 VS |
133 | m_ColsInfo = NULL; |
134 | m_NumCols = m_NumRows = 0; | |
135 | m_CellInfo = NULL; | |
136 | m_ActualCol = m_ActualRow = -1; | |
137 | ||
138 | /* scan params: */ | |
04dbb646 | 139 | if (tag.HasParam(wxT("BGCOLOR"))) |
884898a7 | 140 | { |
8bd72d90 | 141 | tag.GetParamAsColour(wxT("BGCOLOR"), &m_tBkg); |
884898a7 VZ |
142 | if (m_tBkg.Ok()) |
143 | SetBackgroundColour(m_tBkg); | |
144 | } | |
04dbb646 VZ |
145 | if (tag.HasParam(wxT("VALIGN"))) |
146 | m_tValign = tag.GetParam(wxT("VALIGN")); | |
147 | else | |
8bd72d90 VS |
148 | m_tValign = wxEmptyString; |
149 | if (!tag.GetParamAsInt(wxT("CELLSPACING"), &m_Spacing)) | |
150 | m_Spacing = 2; | |
151 | if (!tag.GetParamAsInt(wxT("CELLPADDING"), &m_Padding)) | |
152 | m_Padding = 3; | |
edbd0635 VS |
153 | m_Spacing = (int)(m_PixelScale * (double)m_Spacing); |
154 | m_Padding = (int)(m_PixelScale * (double)m_Padding); | |
5526e819 VS |
155 | |
156 | if (m_HasBorders) | |
157 | SetBorder(TABLE_BORDER_CLR_1, TABLE_BORDER_CLR_2); | |
158 | } | |
159 | ||
160 | ||
161 | ||
162 | wxHtmlTableCell::~wxHtmlTableCell() | |
163 | { | |
164 | if (m_ColsInfo) free(m_ColsInfo); | |
04dbb646 | 165 | if (m_CellInfo) |
4f9297b0 | 166 | { |
5526e819 VS |
167 | for (int i = 0; i < m_NumRows; i++) |
168 | free(m_CellInfo[i]); | |
169 | free(m_CellInfo); | |
170 | } | |
171 | } | |
172 | ||
173 | ||
3bc7a423 VS |
174 | void wxHtmlTableCell::RemoveExtraSpacing(bool WXUNUSED(top), |
175 | bool WXUNUSED(bottom)) | |
176 | { | |
177 | // Don't remove any spacing in the table -- it's always desirable, | |
178 | // because it's part of table's definition. | |
179 | // (If wxHtmlContainerCell::RemoveExtraSpacing() was applied to tables, | |
180 | // then upper left cell of a table would be positioned above other cells | |
181 | // if the table was the first element on the page.) | |
182 | } | |
5526e819 VS |
183 | |
184 | void wxHtmlTableCell::ReallocCols(int cols) | |
185 | { | |
186 | int i,j; | |
187 | ||
04dbb646 | 188 | for (i = 0; i < m_NumRows; i++) |
4f9297b0 | 189 | { |
5526e819 VS |
190 | m_CellInfo[i] = (cellStruct*) realloc(m_CellInfo[i], sizeof(cellStruct) * cols); |
191 | for (j = m_NumCols; j < cols; j++) | |
192 | m_CellInfo[i][j].flag = cellFree; | |
193 | } | |
194 | ||
195 | m_ColsInfo = (colStruct*) realloc(m_ColsInfo, sizeof(colStruct) * cols); | |
04dbb646 | 196 | for (j = m_NumCols; j < cols; j++) |
4f9297b0 | 197 | { |
5526e819 | 198 | m_ColsInfo[j].width = 0; |
efba2b89 | 199 | m_ColsInfo[j].units = wxHTML_UNITS_PERCENT; |
79d6c018 | 200 | m_ColsInfo[j].minWidth = m_ColsInfo[j].maxWidth = -1; |
5526e819 VS |
201 | } |
202 | ||
203 | m_NumCols = cols; | |
204 | } | |
205 | ||
206 | ||
207 | ||
208 | void wxHtmlTableCell::ReallocRows(int rows) | |
209 | { | |
210 | m_CellInfo = (cellStruct**) realloc(m_CellInfo, sizeof(cellStruct*) * rows); | |
04dbb646 | 211 | for (int row = m_NumRows; row < rows ; row++) |
80eab469 | 212 | { |
04dbb646 | 213 | if (m_NumCols == 0) |
80eab469 | 214 | m_CellInfo[row] = NULL; |
04dbb646 | 215 | else |
80eab469 VS |
216 | { |
217 | m_CellInfo[row] = (cellStruct*) malloc(sizeof(cellStruct) * m_NumCols); | |
218 | for (int col = 0; col < m_NumCols; col++) | |
219 | m_CellInfo[row][col].flag = cellFree; | |
220 | } | |
5526e819 | 221 | } |
5526e819 VS |
222 | m_NumRows = rows; |
223 | } | |
224 | ||
225 | ||
5526e819 VS |
226 | void wxHtmlTableCell::AddRow(const wxHtmlTag& tag) |
227 | { | |
5526e819 | 228 | m_ActualCol = -1; |
d361bbff VS |
229 | // VS: real allocation of row entry is done in AddCell in order |
230 | // to correctly handle empty rows (i.e. "<tr></tr>") | |
231 | // m_ActualCol == -1 indicates that AddCell has to allocate new row. | |
5526e819 | 232 | |
d361bbff | 233 | // scan params: |
5526e819 | 234 | m_rBkg = m_tBkg; |
04dbb646 | 235 | if (tag.HasParam(wxT("BGCOLOR"))) |
8bd72d90 | 236 | tag.GetParamAsColour(wxT("BGCOLOR"), &m_rBkg); |
04dbb646 VZ |
237 | if (tag.HasParam(wxT("VALIGN"))) |
238 | m_rValign = tag.GetParam(wxT("VALIGN")); | |
239 | else | |
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 | ||
277 | // width: | |
278 | { | |
8bd72d90 | 279 | if (tag.HasParam(wxT("WIDTH"))) |
2fa3b707 | 280 | { |
8bd72d90 | 281 | wxString wd = tag.GetParam(wxT("WIDTH")); |
5526e819 | 282 | |
93763ad5 | 283 | if (wd[wd.length()-1] == wxT('%')) |
2fa3b707 | 284 | { |
66a77a74 | 285 | wxSscanf(wd.c_str(), wxT("%i%%"), &m_ColsInfo[c].width); |
efba2b89 | 286 | m_ColsInfo[c].units = wxHTML_UNITS_PERCENT; |
5526e819 | 287 | } |
04dbb646 | 288 | else |
2fa3b707 | 289 | { |
66a77a74 | 290 | wxSscanf(wd.c_str(), wxT("%i"), &m_ColsInfo[c].width); |
edbd0635 | 291 | m_ColsInfo[c].width = (int)(m_PixelScale * (double)m_ColsInfo[c].width); |
efba2b89 | 292 | m_ColsInfo[c].units = wxHTML_UNITS_PIXELS; |
5526e819 VS |
293 | } |
294 | } | |
295 | } | |
296 | ||
297 | ||
298 | // spanning: | |
299 | { | |
8bd72d90 VS |
300 | tag.GetParamAsInt(wxT("COLSPAN"), &m_CellInfo[r][c].colspan); |
301 | tag.GetParamAsInt(wxT("ROWSPAN"), &m_CellInfo[r][c].rowspan); | |
fbe77cea VS |
302 | |
303 | // VS: the standard says this about col/rowspan: | |
25c71ccc VZ |
304 | // "This attribute specifies the number of rows spanned by the |
305 | // current cell. The default value of this attribute is one ("1"). | |
306 | // The value zero ("0") means that the cell spans all rows from the | |
307 | // current row to the last row of the table." All mainstream | |
fbe77cea | 308 | // browsers act as if 0==1, though, and so does wxHTML. |
25c71ccc | 309 | if (m_CellInfo[r][c].colspan < 1) |
fbe77cea | 310 | m_CellInfo[r][c].colspan = 1; |
25c71ccc | 311 | if (m_CellInfo[r][c].rowspan < 1) |
fbe77cea VS |
312 | m_CellInfo[r][c].rowspan = 1; |
313 | ||
314 | if ((m_CellInfo[r][c].colspan > 1) || (m_CellInfo[r][c].rowspan > 1)) | |
2fa3b707 | 315 | { |
5526e819 VS |
316 | int i, j; |
317 | ||
04dbb646 | 318 | if (r + m_CellInfo[r][c].rowspan > m_NumRows) |
8bd72d90 | 319 | ReallocRows(r + m_CellInfo[r][c].rowspan); |
04dbb646 | 320 | if (c + m_CellInfo[r][c].colspan > m_NumCols) |
8bd72d90 | 321 | ReallocCols(c + m_CellInfo[r][c].colspan); |
5526e819 VS |
322 | for (i = r; i < r + m_CellInfo[r][c].rowspan; i++) |
323 | for (j = c; j < c + m_CellInfo[r][c].colspan; j++) | |
324 | m_CellInfo[i][j].flag = cellSpan; | |
325 | m_CellInfo[r][c].flag = cellUsed; | |
326 | } | |
327 | } | |
328 | ||
329 | //background color: | |
330 | { | |
8bd72d90 | 331 | wxColour bk = m_rBkg; |
04dbb646 | 332 | if (tag.HasParam(wxT("BGCOLOR"))) |
8bd72d90 VS |
333 | tag.GetParamAsColour(wxT("BGCOLOR"), &bk); |
334 | if (bk.Ok()) | |
335 | cell->SetBackgroundColour(bk); | |
5526e819 VS |
336 | } |
337 | if (m_HasBorders) | |
4f9297b0 | 338 | cell->SetBorder(TABLE_BORDER_CLR_2, TABLE_BORDER_CLR_1); |
5526e819 VS |
339 | |
340 | // vertical alignment: | |
341 | { | |
342 | wxString valign; | |
04dbb646 VZ |
343 | if (tag.HasParam(wxT("VALIGN"))) |
344 | valign = tag.GetParam(wxT("VALIGN")); | |
345 | else | |
8bd72d90 | 346 | valign = m_tValign; |
5526e819 | 347 | valign.MakeUpper(); |
04dbb646 | 348 | if (valign == wxT("TOP")) |
8bd72d90 | 349 | m_CellInfo[r][c].valign = wxHTML_ALIGN_TOP; |
04dbb646 | 350 | else if (valign == wxT("BOTTOM")) |
8bd72d90 | 351 | m_CellInfo[r][c].valign = wxHTML_ALIGN_BOTTOM; |
efba2b89 | 352 | else m_CellInfo[r][c].valign = wxHTML_ALIGN_CENTER; |
5526e819 VS |
353 | } |
354 | ||
ca16b7a9 VS |
355 | // nowrap |
356 | if (tag.HasParam(wxT("NOWRAP"))) | |
357 | m_CellInfo[r][c].nowrap = true; | |
358 | else | |
359 | m_CellInfo[r][c].nowrap = false; | |
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 | ||
411 | m_MaxTotalWidth += (m_NumCols + 1) * m_Spacing; | |
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 | ||
450 | LAYOUTING : | |
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 VS |
458 | { |
459 | int wpix = m_Width - (m_NumCols + 1) * m_Spacing; | |
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 | |
ca16b7a9 VS |
498 | newWidth = wxMin(newWidth, w - (m_NumCols + 1) * m_Spacing); |
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 | |
512 | int minRequired = 0; | |
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 | } |
ca16b7a9 VS |
523 | wpix = wtemp; |
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++; | |
ca16b7a9 VS |
534 | if (wpix < 0) |
535 | wpix = 0; | |
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 | { | |
573 | int wpos = m_Spacing; | |
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 | } | |
579 | } | |
580 | ||
581 | /* 3. sub-layout all cells: */ | |
582 | { | |
2776d7c3 | 583 | int *ypos = new int[m_NumRows + 1]; |
5526e819 VS |
584 | |
585 | int actcol, actrow; | |
586 | int fullwid; | |
587 | wxHtmlContainerCell *actcell; | |
588 | ||
2fa3b707 VS |
589 | ypos[0] = m_Spacing; |
590 | for (actrow = 1; actrow <= m_NumRows; actrow++) ypos[actrow] = -1; | |
04dbb646 | 591 | for (actrow = 0; actrow < m_NumRows; actrow++) |
2fa3b707 VS |
592 | { |
593 | if (ypos[actrow] == -1) ypos[actrow] = ypos[actrow-1]; | |
5526e819 VS |
594 | // 3a. sub-layout and detect max height: |
595 | ||
596 | for (actcol = 0; actcol < m_NumCols; actcol++) { | |
597 | if (m_CellInfo[actrow][actcol].flag != cellUsed) continue; | |
598 | actcell = m_CellInfo[actrow][actcol].cont; | |
599 | fullwid = 0; | |
600 | for (int i = actcol; i < m_CellInfo[actrow][actcol].colspan + actcol; i++) | |
601 | fullwid += m_ColsInfo[i].pixwidth; | |
a97a264f | 602 | fullwid += (m_CellInfo[actrow][actcol].colspan - 1) * m_Spacing; |
4f9297b0 VS |
603 | actcell->SetMinHeight(m_CellInfo[actrow][actcol].minheight, m_CellInfo[actrow][actcol].valign); |
604 | actcell->Layout(fullwid); | |
5526e819 | 605 | |
4f9297b0 | 606 | if (ypos[actrow] + actcell->GetHeight() + m_CellInfo[actrow][actcol].rowspan * m_Spacing > ypos[actrow + m_CellInfo[actrow][actcol].rowspan]) |
5526e819 | 607 | ypos[actrow + m_CellInfo[actrow][actcol].rowspan] = |
4f9297b0 | 608 | ypos[actrow] + actcell->GetHeight() + m_CellInfo[actrow][actcol].rowspan * m_Spacing; |
5526e819 VS |
609 | } |
610 | } | |
611 | ||
04dbb646 | 612 | for (actrow = 0; actrow < m_NumRows; actrow++) |
2fa3b707 | 613 | { |
5526e819 VS |
614 | // 3b. place cells in row & let'em all have same height: |
615 | ||
04dbb646 | 616 | for (actcol = 0; actcol < m_NumCols; actcol++) |
2fa3b707 | 617 | { |
5526e819 VS |
618 | if (m_CellInfo[actrow][actcol].flag != cellUsed) continue; |
619 | actcell = m_CellInfo[actrow][actcol].cont; | |
4f9297b0 | 620 | actcell->SetMinHeight( |
a97a264f | 621 | ypos[actrow + m_CellInfo[actrow][actcol].rowspan] - ypos[actrow] - m_Spacing, |
5526e819 VS |
622 | m_CellInfo[actrow][actcol].valign); |
623 | fullwid = 0; | |
624 | for (int i = actcol; i < m_CellInfo[actrow][actcol].colspan + actcol; i++) | |
625 | fullwid += m_ColsInfo[i].pixwidth; | |
a97a264f | 626 | fullwid += (m_CellInfo[actrow][actcol].colspan - 1) * m_Spacing; |
4f9297b0 VS |
627 | actcell->Layout(fullwid); |
628 | actcell->SetPos(m_ColsInfo[actcol].leftpos, ypos[actrow]); | |
5526e819 | 629 | } |
5526e819 VS |
630 | } |
631 | m_Height = ypos[m_NumRows]; | |
2776d7c3 | 632 | delete[] ypos; |
5526e819 | 633 | } |
8c571f46 VS |
634 | |
635 | /* 4. adjust table's width if it was too small: */ | |
636 | if (m_NumCols > 0) | |
637 | { | |
25c71ccc | 638 | int twidth = m_ColsInfo[m_NumCols-1].leftpos + |
8c571f46 VS |
639 | m_ColsInfo[m_NumCols-1].pixwidth + m_Spacing; |
640 | if (twidth > m_Width) | |
641 | m_Width = twidth; | |
642 | } | |
5526e819 VS |
643 | } |
644 | ||
645 | ||
646 | ||
647 | ||
648 | ||
649 | ||
650 | //----------------------------------------------------------------------------- | |
651 | // The tables handler: | |
652 | //----------------------------------------------------------------------------- | |
653 | ||
654 | ||
655 | TAG_HANDLER_BEGIN(TABLE, "TABLE,TR,TD,TH") | |
656 | ||
657 | TAG_HANDLER_VARS | |
658 | wxHtmlTableCell* m_Table; | |
659 | wxString m_tAlign, m_rAlign; | |
5526e819 VS |
660 | |
661 | TAG_HANDLER_CONSTR(TABLE) | |
662 | { | |
663 | m_Table = NULL; | |
01325161 | 664 | m_tAlign = m_rAlign = wxEmptyString; |
5526e819 VS |
665 | } |
666 | ||
667 | ||
668 | TAG_HANDLER_PROC(tag) | |
669 | { | |
670 | wxHtmlContainerCell *c; | |
671 | ||
672 | // new table started, backup upper-level table (if any) and create new: | |
04dbb646 | 673 | if (tag.GetName() == wxT("TABLE")) |
2fa3b707 | 674 | { |
5526e819 VS |
675 | wxHtmlTableCell *oldt = m_Table; |
676 | wxHtmlContainerCell *oldcont; | |
5526e819 | 677 | |
4f9297b0 | 678 | oldcont = c = m_WParser->OpenContainer(); |
5526e819 | 679 | |
ca16b7a9 VS |
680 | m_Table = new wxHtmlTableCell(c, tag); |
681 | ||
682 | // width: | |
683 | { | |
684 | if (tag.HasParam(wxT("WIDTH"))) | |
685 | { | |
686 | wxString wd = tag.GetParam(wxT("WIDTH")); | |
687 | ||
93763ad5 | 688 | if (wd[wd.length()-1] == wxT('%')) |
ca16b7a9 VS |
689 | { |
690 | int width = 0; | |
691 | wxSscanf(wd.c_str(), wxT("%i%%"), &width); | |
692 | m_Table->SetWidthFloat(width, wxHTML_UNITS_PERCENT); | |
693 | } | |
694 | else | |
695 | { | |
696 | int width = 0; | |
697 | wxSscanf(wd.c_str(), wxT("%i"), &width); | |
25c71ccc | 698 | m_Table->SetWidthFloat((int)(m_WParser->GetPixelScale() * width), wxHTML_UNITS_PIXELS); |
ca16b7a9 VS |
699 | } |
700 | } | |
701 | else | |
702 | m_Table->SetWidthFloat(0, wxHTML_UNITS_PIXELS); | |
703 | } | |
2755d437 | 704 | int oldAlign = m_WParser->GetAlign(); |
5526e819 | 705 | m_tAlign = wxEmptyString; |
8bd72d90 VS |
706 | if (tag.HasParam(wxT("ALIGN"))) |
707 | m_tAlign = tag.GetParam(wxT("ALIGN")); | |
5526e819 VS |
708 | |
709 | ParseInner(tag); | |
710 | ||
2755d437 | 711 | m_WParser->SetAlign(oldAlign); |
4f9297b0 VS |
712 | m_WParser->SetContainer(oldcont); |
713 | m_WParser->CloseContainer(); | |
25c71ccc | 714 | |
5526e819 | 715 | m_Table = oldt; |
d1da8872 | 716 | return true; |
5526e819 VS |
717 | } |
718 | ||
719 | ||
211dfedd | 720 | else if (m_Table) |
2fa3b707 | 721 | { |
5526e819 | 722 | // new row in table |
04dbb646 | 723 | if (tag.GetName() == wxT("TR")) |
2fa3b707 | 724 | { |
4f9297b0 | 725 | m_Table->AddRow(tag); |
5526e819 | 726 | m_rAlign = m_tAlign; |
04dbb646 | 727 | if (tag.HasParam(wxT("ALIGN"))) |
8bd72d90 | 728 | m_rAlign = tag.GetParam(wxT("ALIGN")); |
5526e819 VS |
729 | } |
730 | ||
731 | // new cell | |
04dbb646 | 732 | else |
2fa3b707 | 733 | { |
4f9297b0 VS |
734 | c = m_WParser->SetContainer(new wxHtmlContainerCell(m_Table)); |
735 | m_Table->AddCell(c, tag); | |
5526e819 | 736 | |
4f9297b0 | 737 | m_WParser->OpenContainer(); |
5526e819 | 738 | |
04dbb646 | 739 | if (tag.GetName() == wxT("TH")) /*header style*/ |
4f9297b0 | 740 | m_WParser->SetAlign(wxHTML_ALIGN_CENTER); |
2755d437 VS |
741 | else |
742 | m_WParser->SetAlign(wxHTML_ALIGN_LEFT); | |
743 | ||
744 | wxString als; | |
745 | ||
746 | als = m_rAlign; | |
747 | if (tag.HasParam(wxT("ALIGN"))) | |
748 | als = tag.GetParam(wxT("ALIGN")); | |
749 | als.MakeUpper(); | |
750 | if (als == wxT("RIGHT")) | |
751 | m_WParser->SetAlign(wxHTML_ALIGN_RIGHT); | |
752 | else if (als == wxT("LEFT")) | |
753 | m_WParser->SetAlign(wxHTML_ALIGN_LEFT); | |
754 | else if (als == wxT("CENTER")) | |
755 | m_WParser->SetAlign(wxHTML_ALIGN_CENTER); | |
756 | ||
4f9297b0 | 757 | m_WParser->OpenContainer(); |
5526e819 VS |
758 | } |
759 | } | |
d1da8872 | 760 | return false; |
5526e819 VS |
761 | } |
762 | ||
763 | TAG_HANDLER_END(TABLE) | |
764 | ||
765 | ||
766 | ||
767 | ||
768 | ||
769 | TAGS_MODULE_BEGIN(Tables) | |
770 | ||
771 | TAGS_MODULE_ADD(TABLE) | |
772 | ||
773 | TAGS_MODULE_END(Tables) | |
774 | ||
775 | ||
776 | #endif |