]>
Commit | Line | Data |
---|---|---|
5526e819 | 1 | ///////////////////////////////////////////////////////////////////////////// |
c88293a4 | 2 | // Name: 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 | ||
14f355c2 | 10 | #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA) |
3364ab79 RS |
11 | #pragma implementation |
12 | #endif | |
13 | ||
3096bd2f | 14 | #include "wx/wxprec.h" |
3364ab79 | 15 | |
314260fb | 16 | #include "wx/defs.h" |
f6bcfd97 | 17 | #if wxUSE_HTML && wxUSE_STREAMS |
2b5f62a0 | 18 | #ifdef __BORLANDC__ |
3364ab79 RS |
19 | #pragma hdrstop |
20 | #endif | |
21 | ||
22 | #ifndef WXPRECOMP | |
3364ab79 RS |
23 | #endif |
24 | ||
69941f05 VS |
25 | #include "wx/html/forcelnk.h" |
26 | #include "wx/html/m_templ.h" | |
5526e819 | 27 | |
69941f05 | 28 | #include "wx/html/htmlcell.h" |
5526e819 | 29 | |
c88293a4 | 30 | FORCE_LINK_ME(m_tables) |
5526e819 VS |
31 | |
32 | ||
33 | #define TABLE_BORDER_CLR_1 wxColour(0xC5, 0xC2, 0xC5) | |
34 | #define TABLE_BORDER_CLR_2 wxColour(0x62, 0x61, 0x62) | |
35 | ||
36 | ||
37 | //----------------------------------------------------------------------------- | |
38 | // wxHtmlTableCell | |
39 | //----------------------------------------------------------------------------- | |
40 | ||
41 | ||
79d6c018 VS |
42 | struct colStruct |
43 | { | |
44 | int width, units; | |
45 | // width of the column either in pixels or percents | |
46 | // ('width' is the number, 'units' determines its meaning) | |
47 | int minWidth, maxWidth; | |
48 | // minimal/maximal column width. This is needed by HTML 4.0 | |
49 | // layouting algorithm and can be determined by trying to | |
50 | // layout table cells with width=1 and width=infinity | |
51 | int leftpos, pixwidth, maxrealwidth; | |
52 | // temporary (depends on actual width of table) | |
53 | }; | |
54 | ||
55 | enum cellState | |
56 | { | |
57 | cellSpan, | |
58 | cellUsed, | |
59 | cellFree | |
60 | }; | |
61 | ||
62 | struct cellStruct | |
63 | { | |
64 | wxHtmlContainerCell *cont; | |
65 | int colspan, rowspan; | |
66 | int minheight, valign; | |
67 | cellState flag; | |
ca16b7a9 | 68 | bool nowrap; |
79d6c018 | 69 | }; |
5526e819 VS |
70 | |
71 | ||
72 | class wxHtmlTableCell : public wxHtmlContainerCell | |
73 | { | |
79d6c018 VS |
74 | protected: |
75 | /* These are real attributes: */ | |
76 | ||
77 | // should we draw borders or not? | |
78 | bool m_HasBorders; | |
79 | // number of columns; rows | |
80 | int m_NumCols, m_NumRows; | |
81 | // array of column information | |
82 | colStruct *m_ColsInfo; | |
83 | // 2D array of all cells in the table : m_CellInfo[row][column] | |
84 | cellStruct **m_CellInfo; | |
85 | // spaces between cells | |
86 | int m_Spacing; | |
87 | // cells internal indentation | |
88 | int m_Padding; | |
89 | ||
90 | private: | |
91 | /* ...and these are valid only when parsing the table: */ | |
92 | ||
93 | // number of actual column (ranging from 0..m_NumCols) | |
94 | int m_ActualCol, m_ActualRow; | |
95 | ||
96 | // default values (for table and row): | |
97 | wxColour m_tBkg, m_rBkg; | |
98 | wxString m_tValign, m_rValign; | |
99 | ||
100 | double m_PixelScale; | |
101 | ||
102 | ||
103 | public: | |
104 | wxHtmlTableCell(wxHtmlContainerCell *parent, const wxHtmlTag& tag, double pixel_scale = 1.0); | |
105 | ~wxHtmlTableCell(); | |
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"))) |
8bd72d90 | 140 | tag.GetParamAsColour(wxT("BGCOLOR"), &m_tBkg); |
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 VS |
151 | |
152 | if (m_HasBorders) | |
153 | SetBorder(TABLE_BORDER_CLR_1, TABLE_BORDER_CLR_2); | |
154 | } | |
155 | ||
156 | ||
157 | ||
158 | wxHtmlTableCell::~wxHtmlTableCell() | |
159 | { | |
160 | if (m_ColsInfo) free(m_ColsInfo); | |
04dbb646 | 161 | if (m_CellInfo) |
4f9297b0 | 162 | { |
5526e819 VS |
163 | for (int i = 0; i < m_NumRows; i++) |
164 | free(m_CellInfo[i]); | |
165 | free(m_CellInfo); | |
166 | } | |
167 | } | |
168 | ||
169 | ||
170 | ||
171 | void wxHtmlTableCell::ReallocCols(int cols) | |
172 | { | |
173 | int i,j; | |
174 | ||
04dbb646 | 175 | for (i = 0; i < m_NumRows; i++) |
4f9297b0 | 176 | { |
5526e819 VS |
177 | m_CellInfo[i] = (cellStruct*) realloc(m_CellInfo[i], sizeof(cellStruct) * cols); |
178 | for (j = m_NumCols; j < cols; j++) | |
179 | m_CellInfo[i][j].flag = cellFree; | |
180 | } | |
181 | ||
182 | m_ColsInfo = (colStruct*) realloc(m_ColsInfo, sizeof(colStruct) * cols); | |
04dbb646 | 183 | for (j = m_NumCols; j < cols; j++) |
4f9297b0 | 184 | { |
5526e819 | 185 | m_ColsInfo[j].width = 0; |
efba2b89 | 186 | m_ColsInfo[j].units = wxHTML_UNITS_PERCENT; |
79d6c018 | 187 | m_ColsInfo[j].minWidth = m_ColsInfo[j].maxWidth = -1; |
5526e819 VS |
188 | } |
189 | ||
190 | m_NumCols = cols; | |
191 | } | |
192 | ||
193 | ||
194 | ||
195 | void wxHtmlTableCell::ReallocRows(int rows) | |
196 | { | |
197 | m_CellInfo = (cellStruct**) realloc(m_CellInfo, sizeof(cellStruct*) * rows); | |
04dbb646 | 198 | for (int row = m_NumRows; row < rows ; row++) |
80eab469 | 199 | { |
04dbb646 | 200 | if (m_NumCols == 0) |
80eab469 | 201 | m_CellInfo[row] = NULL; |
04dbb646 | 202 | else |
80eab469 VS |
203 | { |
204 | m_CellInfo[row] = (cellStruct*) malloc(sizeof(cellStruct) * m_NumCols); | |
205 | for (int col = 0; col < m_NumCols; col++) | |
206 | m_CellInfo[row][col].flag = cellFree; | |
207 | } | |
5526e819 | 208 | } |
5526e819 VS |
209 | m_NumRows = rows; |
210 | } | |
211 | ||
212 | ||
5526e819 VS |
213 | void wxHtmlTableCell::AddRow(const wxHtmlTag& tag) |
214 | { | |
5526e819 | 215 | m_ActualCol = -1; |
d361bbff VS |
216 | // VS: real allocation of row entry is done in AddCell in order |
217 | // to correctly handle empty rows (i.e. "<tr></tr>") | |
218 | // m_ActualCol == -1 indicates that AddCell has to allocate new row. | |
5526e819 | 219 | |
d361bbff | 220 | // scan params: |
5526e819 | 221 | m_rBkg = m_tBkg; |
04dbb646 | 222 | if (tag.HasParam(wxT("BGCOLOR"))) |
8bd72d90 | 223 | tag.GetParamAsColour(wxT("BGCOLOR"), &m_rBkg); |
04dbb646 VZ |
224 | if (tag.HasParam(wxT("VALIGN"))) |
225 | m_rValign = tag.GetParam(wxT("VALIGN")); | |
226 | else | |
d361bbff | 227 | m_rValign = m_tValign; |
5526e819 VS |
228 | } |
229 | ||
230 | ||
231 | ||
232 | void wxHtmlTableCell::AddCell(wxHtmlContainerCell *cell, const wxHtmlTag& tag) | |
233 | { | |
d361bbff VS |
234 | // Is this cell in new row? |
235 | // VS: we can't do it in AddRow, see my comment there | |
236 | if (m_ActualCol == -1) | |
237 | { | |
238 | if (m_ActualRow + 1 > m_NumRows - 1) | |
239 | ReallocRows(m_ActualRow + 2); | |
240 | m_ActualRow++; | |
241 | } | |
242 | ||
243 | // cells & columns: | |
04dbb646 | 244 | do |
4f9297b0 | 245 | { |
5526e819 | 246 | m_ActualCol++; |
04dbb646 | 247 | } while ((m_ActualCol < m_NumCols) && |
8bd72d90 | 248 | (m_CellInfo[m_ActualRow][m_ActualCol].flag != cellFree)); |
04dbb646 | 249 | |
5526e819 VS |
250 | if (m_ActualCol > m_NumCols - 1) |
251 | ReallocCols(m_ActualCol + 1); | |
252 | ||
253 | int r = m_ActualRow, c = m_ActualCol; | |
254 | ||
255 | m_CellInfo[r][c].cont = cell; | |
256 | m_CellInfo[r][c].colspan = 1; | |
257 | m_CellInfo[r][c].rowspan = 1; | |
258 | m_CellInfo[r][c].flag = cellUsed; | |
259 | m_CellInfo[r][c].minheight = 0; | |
efba2b89 | 260 | m_CellInfo[r][c].valign = wxHTML_ALIGN_TOP; |
5526e819 VS |
261 | |
262 | /* scan for parameters: */ | |
263 | ||
264 | // width: | |
265 | { | |
8bd72d90 | 266 | if (tag.HasParam(wxT("WIDTH"))) |
2fa3b707 | 267 | { |
8bd72d90 | 268 | wxString wd = tag.GetParam(wxT("WIDTH")); |
5526e819 | 269 | |
8bd72d90 | 270 | if (wd[wd.Length()-1] == wxT('%')) |
2fa3b707 | 271 | { |
66a77a74 | 272 | wxSscanf(wd.c_str(), wxT("%i%%"), &m_ColsInfo[c].width); |
efba2b89 | 273 | m_ColsInfo[c].units = wxHTML_UNITS_PERCENT; |
5526e819 | 274 | } |
04dbb646 | 275 | else |
2fa3b707 | 276 | { |
66a77a74 | 277 | wxSscanf(wd.c_str(), wxT("%i"), &m_ColsInfo[c].width); |
edbd0635 | 278 | m_ColsInfo[c].width = (int)(m_PixelScale * (double)m_ColsInfo[c].width); |
efba2b89 | 279 | m_ColsInfo[c].units = wxHTML_UNITS_PIXELS; |
5526e819 VS |
280 | } |
281 | } | |
282 | } | |
283 | ||
284 | ||
285 | // spanning: | |
286 | { | |
8bd72d90 VS |
287 | tag.GetParamAsInt(wxT("COLSPAN"), &m_CellInfo[r][c].colspan); |
288 | tag.GetParamAsInt(wxT("ROWSPAN"), &m_CellInfo[r][c].rowspan); | |
fbe77cea VS |
289 | |
290 | // VS: the standard says this about col/rowspan: | |
25c71ccc VZ |
291 | // "This attribute specifies the number of rows spanned by the |
292 | // current cell. The default value of this attribute is one ("1"). | |
293 | // The value zero ("0") means that the cell spans all rows from the | |
294 | // current row to the last row of the table." All mainstream | |
fbe77cea | 295 | // browsers act as if 0==1, though, and so does wxHTML. |
25c71ccc | 296 | if (m_CellInfo[r][c].colspan < 1) |
fbe77cea | 297 | m_CellInfo[r][c].colspan = 1; |
25c71ccc | 298 | if (m_CellInfo[r][c].rowspan < 1) |
fbe77cea VS |
299 | m_CellInfo[r][c].rowspan = 1; |
300 | ||
301 | if ((m_CellInfo[r][c].colspan > 1) || (m_CellInfo[r][c].rowspan > 1)) | |
2fa3b707 | 302 | { |
5526e819 VS |
303 | int i, j; |
304 | ||
04dbb646 | 305 | if (r + m_CellInfo[r][c].rowspan > m_NumRows) |
8bd72d90 | 306 | ReallocRows(r + m_CellInfo[r][c].rowspan); |
04dbb646 | 307 | if (c + m_CellInfo[r][c].colspan > m_NumCols) |
8bd72d90 | 308 | ReallocCols(c + m_CellInfo[r][c].colspan); |
5526e819 VS |
309 | for (i = r; i < r + m_CellInfo[r][c].rowspan; i++) |
310 | for (j = c; j < c + m_CellInfo[r][c].colspan; j++) | |
311 | m_CellInfo[i][j].flag = cellSpan; | |
312 | m_CellInfo[r][c].flag = cellUsed; | |
313 | } | |
314 | } | |
315 | ||
316 | //background color: | |
317 | { | |
8bd72d90 | 318 | wxColour bk = m_rBkg; |
04dbb646 | 319 | if (tag.HasParam(wxT("BGCOLOR"))) |
8bd72d90 VS |
320 | tag.GetParamAsColour(wxT("BGCOLOR"), &bk); |
321 | if (bk.Ok()) | |
322 | cell->SetBackgroundColour(bk); | |
5526e819 VS |
323 | } |
324 | if (m_HasBorders) | |
4f9297b0 | 325 | cell->SetBorder(TABLE_BORDER_CLR_2, TABLE_BORDER_CLR_1); |
5526e819 VS |
326 | |
327 | // vertical alignment: | |
328 | { | |
329 | wxString valign; | |
04dbb646 VZ |
330 | if (tag.HasParam(wxT("VALIGN"))) |
331 | valign = tag.GetParam(wxT("VALIGN")); | |
332 | else | |
8bd72d90 | 333 | valign = m_tValign; |
5526e819 | 334 | valign.MakeUpper(); |
04dbb646 | 335 | if (valign == wxT("TOP")) |
8bd72d90 | 336 | m_CellInfo[r][c].valign = wxHTML_ALIGN_TOP; |
04dbb646 | 337 | else if (valign == wxT("BOTTOM")) |
8bd72d90 | 338 | m_CellInfo[r][c].valign = wxHTML_ALIGN_BOTTOM; |
efba2b89 | 339 | else m_CellInfo[r][c].valign = wxHTML_ALIGN_CENTER; |
5526e819 VS |
340 | } |
341 | ||
ca16b7a9 VS |
342 | // nowrap |
343 | if (tag.HasParam(wxT("NOWRAP"))) | |
344 | m_CellInfo[r][c].nowrap = true; | |
345 | else | |
346 | m_CellInfo[r][c].nowrap = false; | |
347 | ||
4f9297b0 | 348 | cell->SetIndent(m_Padding, wxHTML_INDENT_ALL, wxHTML_UNITS_PIXELS); |
5526e819 VS |
349 | } |
350 | ||
79d6c018 VS |
351 | void wxHtmlTableCell::ComputeMinMaxWidths() |
352 | { | |
353 | if (m_NumCols == 0 || m_ColsInfo[0].minWidth != -1) return; | |
25c71ccc | 354 | |
ca16b7a9 VS |
355 | m_MaxTotalWidth = 0; |
356 | int percentage = 0; | |
79d6c018 VS |
357 | for (int c = 0; c < m_NumCols; c++) |
358 | { | |
359 | for (int r = 0; r < m_NumRows; r++) | |
360 | { | |
361 | cellStruct& cell = m_CellInfo[r][c]; | |
362 | if (cell.flag == cellUsed) | |
363 | { | |
026d1fac | 364 | cell.cont->Layout(2*m_Padding + 1); |
ca16b7a9 VS |
365 | int maxWidth = cell.cont->GetMaxTotalWidth(); |
366 | int width = cell.nowrap?maxWidth:cell.cont->GetWidth(); | |
026d1fac | 367 | width -= (cell.colspan-1) * m_Spacing; |
ca16b7a9 | 368 | maxWidth -= (cell.colspan-1) * m_Spacing; |
79d6c018 | 369 | // HTML 4.0 says it is acceptable to distribute min/max |
79d6c018 | 370 | width /= cell.colspan; |
ca16b7a9 VS |
371 | maxWidth /= cell.colspan; |
372 | for (int j = 0; j < cell.colspan; j++) { | |
79d6c018 VS |
373 | if (width > m_ColsInfo[c+j].minWidth) |
374 | m_ColsInfo[c+j].minWidth = width; | |
ca16b7a9 VS |
375 | if (maxWidth > m_ColsInfo[c+j].maxWidth) |
376 | m_ColsInfo[c+j].maxWidth = maxWidth; | |
377 | } | |
79d6c018 VS |
378 | } |
379 | } | |
ca16b7a9 VS |
380 | // Calculate maximum table width, required for nested tables |
381 | if (m_ColsInfo[c].units == wxHTML_UNITS_PIXELS) | |
382 | m_MaxTotalWidth += wxMax(m_ColsInfo[c].width, m_ColsInfo[c].minWidth); | |
383 | else if ((m_ColsInfo[c].units == wxHTML_UNITS_PERCENT) && (m_ColsInfo[c].width != 0)) | |
384 | percentage += m_ColsInfo[c].width; | |
385 | else | |
386 | m_MaxTotalWidth += m_ColsInfo[c].maxWidth; | |
79d6c018 | 387 | } |
5526e819 | 388 | |
ca16b7a9 VS |
389 | if (percentage >= 100) |
390 | { | |
391 | // Table would have infinite length | |
392 | // Make it ridiculous large | |
393 | m_MaxTotalWidth = 0xFFFFFF; | |
394 | } | |
395 | else | |
396 | m_MaxTotalWidth = m_MaxTotalWidth * 100 / (100 - percentage); | |
397 | ||
398 | m_MaxTotalWidth += (m_NumCols + 1) * m_Spacing; | |
399 | } | |
5526e819 VS |
400 | |
401 | void wxHtmlTableCell::Layout(int w) | |
402 | { | |
79d6c018 | 403 | ComputeMinMaxWidths(); |
25c71ccc | 404 | |
026d1fac | 405 | wxHtmlCell::Layout(w); |
79d6c018 | 406 | |
5526e819 VS |
407 | /* |
408 | ||
409 | WIDTH ADJUSTING : | |
410 | ||
411 | */ | |
412 | ||
04dbb646 | 413 | if (m_WidthFloatUnits == wxHTML_UNITS_PERCENT) |
4f9297b0 | 414 | { |
ca16b7a9 VS |
415 | if (m_WidthFloat < 0) |
416 | { | |
417 | if (m_WidthFloat < -100) | |
418 | m_WidthFloat = -100; | |
419 | m_Width = (100 + m_WidthFloat) * w / 100; | |
420 | } | |
25c71ccc | 421 | else |
ca16b7a9 VS |
422 | { |
423 | if (m_WidthFloat > 100) | |
424 | m_WidthFloat = 100; | |
425 | m_Width = m_WidthFloat * w / 100; | |
426 | } | |
5526e819 | 427 | } |
04dbb646 | 428 | else |
4f9297b0 | 429 | { |
5526e819 VS |
430 | if (m_WidthFloat < 0) m_Width = w + m_WidthFloat; |
431 | else m_Width = m_WidthFloat; | |
432 | } | |
433 | ||
434 | ||
435 | /* | |
436 | ||
437 | LAYOUTING : | |
438 | ||
439 | */ | |
440 | ||
25c71ccc VZ |
441 | /* 1. setup columns widths: |
442 | ||
ca16b7a9 VS |
443 | The algorithm tries to keep the table size less than w if possible. |
444 | */ | |
5526e819 VS |
445 | { |
446 | int wpix = m_Width - (m_NumCols + 1) * m_Spacing; | |
447 | int i, j; | |
5526e819 VS |
448 | |
449 | // 1a. setup fixed-width columns: | |
450 | for (i = 0; i < m_NumCols; i++) | |
efba2b89 | 451 | if (m_ColsInfo[i].units == wxHTML_UNITS_PIXELS) |
79d6c018 | 452 | { |
25c71ccc | 453 | m_ColsInfo[i].pixwidth = wxMax(m_ColsInfo[i].width, |
79d6c018 VS |
454 | m_ColsInfo[i].minWidth); |
455 | wpix -= m_ColsInfo[i].pixwidth; | |
456 | } | |
5526e819 | 457 | |
ca16b7a9 VS |
458 | // 1b. Calculate maximum possible width if line wrapping would be disabled |
459 | // Recalculate total width if m_WidthFloat is zero to keep tables as small | |
460 | // as possible. | |
461 | int maxWidth = 0; | |
462 | for (i = 0; i < m_NumCols; i++) | |
463 | if (m_ColsInfo[i].width == 0) | |
464 | { | |
465 | maxWidth += m_ColsInfo[i].maxWidth; | |
466 | } | |
467 | ||
468 | if (!m_WidthFloat) | |
469 | { | |
470 | // Recalculate table width since no table width was initially given | |
471 | int newWidth = m_Width - wpix + maxWidth; | |
472 | ||
473 | // Make sure that floating-width columns will have the right size. | |
474 | // Calculate sum of all floating-width columns | |
475 | int percentage = 0; | |
476 | for (i = 0; i < m_NumCols; i++) | |
477 | if ((m_ColsInfo[i].units == wxHTML_UNITS_PERCENT) && (m_ColsInfo[i].width != 0)) | |
478 | percentage += m_ColsInfo[i].width; | |
479 | ||
480 | if (percentage >= 100) | |
481 | newWidth = w; | |
482 | else | |
483 | newWidth = newWidth * 100 / (100 - percentage); | |
25c71ccc | 484 | |
ca16b7a9 VS |
485 | newWidth = wxMin(newWidth, w - (m_NumCols + 1) * m_Spacing); |
486 | wpix -= m_Width - newWidth; | |
487 | m_Width = newWidth; | |
488 | } | |
25c71ccc | 489 | |
ca16b7a9 VS |
490 | |
491 | // 1c. setup floating-width columns: | |
492 | int wtemp = wpix; | |
5526e819 | 493 | for (i = 0; i < m_NumCols; i++) |
efba2b89 | 494 | if ((m_ColsInfo[i].units == wxHTML_UNITS_PERCENT) && (m_ColsInfo[i].width != 0)) |
79d6c018 | 495 | { |
ca16b7a9 VS |
496 | m_ColsInfo[i].pixwidth = wxMin(m_ColsInfo[i].width, 100) * wpix / 100; |
497 | ||
498 | // Make sure to leave enough space for the other columns | |
499 | int minRequired = 0; | |
500 | for (j = 0; j < m_NumCols; j++) | |
501 | { | |
502 | if ((m_ColsInfo[j].units == wxHTML_UNITS_PERCENT && j > i) || | |
503 | !m_ColsInfo[j].width) | |
504 | minRequired += m_ColsInfo[j].minWidth; | |
505 | } | |
506 | m_ColsInfo[i].pixwidth = wxMax(wxMin(wtemp - minRequired, m_ColsInfo[i].pixwidth), m_ColsInfo[i].minWidth); | |
507 | ||
508 | wtemp -= m_ColsInfo[i].pixwidth; | |
79d6c018 | 509 | } |
ca16b7a9 VS |
510 | wpix = wtemp; |
511 | ||
512 | // 1d. setup default columns (no width specification supplied): | |
513 | // The algorithm assigns calculates the maximum possible width if line | |
514 | // wrapping would be disabled and assigns column width as a fraction | |
515 | // based upon the maximum width of a column | |
516 | // FIXME: I'm not sure if this algorithm is conform to HTML standard, | |
517 | // though it seems to be much better than the old one | |
5526e819 | 518 | |
5526e819 VS |
519 | for (i = j = 0; i < m_NumCols; i++) |
520 | if (m_ColsInfo[i].width == 0) j++; | |
ca16b7a9 VS |
521 | if (wpix < 0) |
522 | wpix = 0; | |
523 | ||
524 | // Assign widths | |
5526e819 VS |
525 | for (i = 0; i < m_NumCols; i++) |
526 | if (m_ColsInfo[i].width == 0) | |
8c571f46 | 527 | { |
ca16b7a9 VS |
528 | // Assign with, make sure not to drop below minWidth |
529 | if (maxWidth) | |
25c71ccc | 530 | m_ColsInfo[i].pixwidth = (int)(wpix * (m_ColsInfo[i].maxWidth / (float)maxWidth) + 0.5); |
ca16b7a9 VS |
531 | else |
532 | m_ColsInfo[i].pixwidth = wpix / j; | |
25c71ccc | 533 | |
ca16b7a9 VS |
534 | // Make sure to leave enough space for the other columns |
535 | int minRequired = 0; | |
536 | int r; | |
537 | for (r = i + 1; r < m_NumCols; r++) | |
538 | { | |
539 | if (!m_ColsInfo[r].width) | |
540 | minRequired += m_ColsInfo[r].minWidth; | |
541 | } | |
542 | m_ColsInfo[i].pixwidth = wxMax(wxMin(wpix - minRequired, m_ColsInfo[i].pixwidth), m_ColsInfo[i].minWidth); | |
543 | ||
544 | if (maxWidth) | |
545 | { | |
546 | if (m_ColsInfo[i].pixwidth > (wpix * (m_ColsInfo[i].maxWidth / (float)maxWidth) + 0.5)) | |
547 | { | |
25c71ccc | 548 | int diff = (int)(m_ColsInfo[i].pixwidth - (wpix * m_ColsInfo[i].maxWidth / (float)maxWidth + 0.5)); |
ca16b7a9 VS |
549 | maxWidth += diff - m_ColsInfo[i].maxWidth; |
550 | } | |
551 | else | |
552 | maxWidth -= m_ColsInfo[i].maxWidth; | |
553 | } | |
554 | wpix -= m_ColsInfo[i].pixwidth; | |
8c571f46 | 555 | } |
5526e819 VS |
556 | } |
557 | ||
558 | /* 2. compute positions of columns: */ | |
559 | { | |
560 | int wpos = m_Spacing; | |
04dbb646 | 561 | for (int i = 0; i < m_NumCols; i++) |
2fa3b707 | 562 | { |
5526e819 VS |
563 | m_ColsInfo[i].leftpos = wpos; |
564 | wpos += m_ColsInfo[i].pixwidth + m_Spacing; | |
565 | } | |
566 | } | |
567 | ||
568 | /* 3. sub-layout all cells: */ | |
569 | { | |
2776d7c3 | 570 | int *ypos = new int[m_NumRows + 1]; |
5526e819 VS |
571 | |
572 | int actcol, actrow; | |
573 | int fullwid; | |
574 | wxHtmlContainerCell *actcell; | |
575 | ||
2fa3b707 VS |
576 | ypos[0] = m_Spacing; |
577 | for (actrow = 1; actrow <= m_NumRows; actrow++) ypos[actrow] = -1; | |
04dbb646 | 578 | for (actrow = 0; actrow < m_NumRows; actrow++) |
2fa3b707 VS |
579 | { |
580 | if (ypos[actrow] == -1) ypos[actrow] = ypos[actrow-1]; | |
5526e819 VS |
581 | // 3a. sub-layout and detect max height: |
582 | ||
583 | for (actcol = 0; actcol < m_NumCols; actcol++) { | |
584 | if (m_CellInfo[actrow][actcol].flag != cellUsed) continue; | |
585 | actcell = m_CellInfo[actrow][actcol].cont; | |
586 | fullwid = 0; | |
587 | for (int i = actcol; i < m_CellInfo[actrow][actcol].colspan + actcol; i++) | |
588 | fullwid += m_ColsInfo[i].pixwidth; | |
a97a264f | 589 | fullwid += (m_CellInfo[actrow][actcol].colspan - 1) * m_Spacing; |
4f9297b0 VS |
590 | actcell->SetMinHeight(m_CellInfo[actrow][actcol].minheight, m_CellInfo[actrow][actcol].valign); |
591 | actcell->Layout(fullwid); | |
5526e819 | 592 | |
4f9297b0 | 593 | if (ypos[actrow] + actcell->GetHeight() + m_CellInfo[actrow][actcol].rowspan * m_Spacing > ypos[actrow + m_CellInfo[actrow][actcol].rowspan]) |
5526e819 | 594 | ypos[actrow + m_CellInfo[actrow][actcol].rowspan] = |
4f9297b0 | 595 | ypos[actrow] + actcell->GetHeight() + m_CellInfo[actrow][actcol].rowspan * m_Spacing; |
5526e819 VS |
596 | } |
597 | } | |
598 | ||
04dbb646 | 599 | for (actrow = 0; actrow < m_NumRows; actrow++) |
2fa3b707 | 600 | { |
5526e819 VS |
601 | // 3b. place cells in row & let'em all have same height: |
602 | ||
04dbb646 | 603 | for (actcol = 0; actcol < m_NumCols; actcol++) |
2fa3b707 | 604 | { |
5526e819 VS |
605 | if (m_CellInfo[actrow][actcol].flag != cellUsed) continue; |
606 | actcell = m_CellInfo[actrow][actcol].cont; | |
4f9297b0 | 607 | actcell->SetMinHeight( |
a97a264f | 608 | ypos[actrow + m_CellInfo[actrow][actcol].rowspan] - ypos[actrow] - m_Spacing, |
5526e819 VS |
609 | m_CellInfo[actrow][actcol].valign); |
610 | fullwid = 0; | |
611 | for (int i = actcol; i < m_CellInfo[actrow][actcol].colspan + actcol; i++) | |
612 | fullwid += m_ColsInfo[i].pixwidth; | |
a97a264f | 613 | fullwid += (m_CellInfo[actrow][actcol].colspan - 1) * m_Spacing; |
4f9297b0 VS |
614 | actcell->Layout(fullwid); |
615 | actcell->SetPos(m_ColsInfo[actcol].leftpos, ypos[actrow]); | |
5526e819 | 616 | } |
5526e819 VS |
617 | } |
618 | m_Height = ypos[m_NumRows]; | |
2776d7c3 | 619 | delete[] ypos; |
5526e819 | 620 | } |
8c571f46 VS |
621 | |
622 | /* 4. adjust table's width if it was too small: */ | |
623 | if (m_NumCols > 0) | |
624 | { | |
25c71ccc | 625 | int twidth = m_ColsInfo[m_NumCols-1].leftpos + |
8c571f46 VS |
626 | m_ColsInfo[m_NumCols-1].pixwidth + m_Spacing; |
627 | if (twidth > m_Width) | |
628 | m_Width = twidth; | |
629 | } | |
5526e819 VS |
630 | } |
631 | ||
632 | ||
633 | ||
634 | ||
635 | ||
636 | ||
637 | //----------------------------------------------------------------------------- | |
638 | // The tables handler: | |
639 | //----------------------------------------------------------------------------- | |
640 | ||
641 | ||
642 | TAG_HANDLER_BEGIN(TABLE, "TABLE,TR,TD,TH") | |
643 | ||
644 | TAG_HANDLER_VARS | |
645 | wxHtmlTableCell* m_Table; | |
646 | wxString m_tAlign, m_rAlign; | |
5526e819 VS |
647 | |
648 | TAG_HANDLER_CONSTR(TABLE) | |
649 | { | |
650 | m_Table = NULL; | |
01325161 | 651 | m_tAlign = m_rAlign = wxEmptyString; |
5526e819 VS |
652 | } |
653 | ||
654 | ||
655 | TAG_HANDLER_PROC(tag) | |
656 | { | |
657 | wxHtmlContainerCell *c; | |
658 | ||
659 | // new table started, backup upper-level table (if any) and create new: | |
04dbb646 | 660 | if (tag.GetName() == wxT("TABLE")) |
2fa3b707 | 661 | { |
5526e819 VS |
662 | wxHtmlTableCell *oldt = m_Table; |
663 | wxHtmlContainerCell *oldcont; | |
5526e819 | 664 | |
4f9297b0 | 665 | oldcont = c = m_WParser->OpenContainer(); |
5526e819 | 666 | |
ca16b7a9 VS |
667 | m_Table = new wxHtmlTableCell(c, tag); |
668 | ||
669 | // width: | |
670 | { | |
671 | if (tag.HasParam(wxT("WIDTH"))) | |
672 | { | |
673 | wxString wd = tag.GetParam(wxT("WIDTH")); | |
674 | ||
675 | if (wd[wd.Length()-1] == wxT('%')) | |
676 | { | |
677 | int width = 0; | |
678 | wxSscanf(wd.c_str(), wxT("%i%%"), &width); | |
679 | m_Table->SetWidthFloat(width, wxHTML_UNITS_PERCENT); | |
680 | } | |
681 | else | |
682 | { | |
683 | int width = 0; | |
684 | wxSscanf(wd.c_str(), wxT("%i"), &width); | |
25c71ccc | 685 | m_Table->SetWidthFloat((int)(m_WParser->GetPixelScale() * width), wxHTML_UNITS_PIXELS); |
ca16b7a9 VS |
686 | } |
687 | } | |
688 | else | |
689 | m_Table->SetWidthFloat(0, wxHTML_UNITS_PIXELS); | |
690 | } | |
2755d437 | 691 | int oldAlign = m_WParser->GetAlign(); |
5526e819 | 692 | m_tAlign = wxEmptyString; |
8bd72d90 VS |
693 | if (tag.HasParam(wxT("ALIGN"))) |
694 | m_tAlign = tag.GetParam(wxT("ALIGN")); | |
5526e819 VS |
695 | |
696 | ParseInner(tag); | |
697 | ||
2755d437 | 698 | m_WParser->SetAlign(oldAlign); |
4f9297b0 VS |
699 | m_WParser->SetContainer(oldcont); |
700 | m_WParser->CloseContainer(); | |
25c71ccc | 701 | |
5526e819 VS |
702 | m_Table = oldt; |
703 | return TRUE; | |
704 | } | |
705 | ||
706 | ||
211dfedd | 707 | else if (m_Table) |
2fa3b707 | 708 | { |
5526e819 | 709 | // new row in table |
04dbb646 | 710 | if (tag.GetName() == wxT("TR")) |
2fa3b707 | 711 | { |
4f9297b0 | 712 | m_Table->AddRow(tag); |
5526e819 | 713 | m_rAlign = m_tAlign; |
04dbb646 | 714 | if (tag.HasParam(wxT("ALIGN"))) |
8bd72d90 | 715 | m_rAlign = tag.GetParam(wxT("ALIGN")); |
5526e819 VS |
716 | } |
717 | ||
718 | // new cell | |
04dbb646 | 719 | else |
2fa3b707 | 720 | { |
4f9297b0 VS |
721 | c = m_WParser->SetContainer(new wxHtmlContainerCell(m_Table)); |
722 | m_Table->AddCell(c, tag); | |
5526e819 | 723 | |
4f9297b0 | 724 | m_WParser->OpenContainer(); |
5526e819 | 725 | |
04dbb646 | 726 | if (tag.GetName() == wxT("TH")) /*header style*/ |
4f9297b0 | 727 | m_WParser->SetAlign(wxHTML_ALIGN_CENTER); |
2755d437 VS |
728 | else |
729 | m_WParser->SetAlign(wxHTML_ALIGN_LEFT); | |
730 | ||
731 | wxString als; | |
732 | ||
733 | als = m_rAlign; | |
734 | if (tag.HasParam(wxT("ALIGN"))) | |
735 | als = tag.GetParam(wxT("ALIGN")); | |
736 | als.MakeUpper(); | |
737 | if (als == wxT("RIGHT")) | |
738 | m_WParser->SetAlign(wxHTML_ALIGN_RIGHT); | |
739 | else if (als == wxT("LEFT")) | |
740 | m_WParser->SetAlign(wxHTML_ALIGN_LEFT); | |
741 | else if (als == wxT("CENTER")) | |
742 | m_WParser->SetAlign(wxHTML_ALIGN_CENTER); | |
743 | ||
4f9297b0 | 744 | m_WParser->OpenContainer(); |
5526e819 VS |
745 | } |
746 | } | |
747 | return FALSE; | |
748 | } | |
749 | ||
750 | TAG_HANDLER_END(TABLE) | |
751 | ||
752 | ||
753 | ||
754 | ||
755 | ||
756 | TAGS_MODULE_BEGIN(Tables) | |
757 | ||
758 | TAGS_MODULE_ADD(TABLE) | |
759 | ||
760 | TAGS_MODULE_END(Tables) | |
761 | ||
762 | ||
763 | #endif |