]>
Commit | Line | Data |
---|---|---|
1 | ///////////////////////////////////////////////////////////////////////////// | |
2 | // Name: src/html/m_tables.cpp | |
3 | // Purpose: wxHtml module for tables | |
4 | // Author: Vaclav Slavik | |
5 | // RCS-ID: $Id$ | |
6 | // Copyright: (c) 1999 Vaclav Slavik | |
7 | // Licence: wxWindows licence | |
8 | ///////////////////////////////////////////////////////////////////////////// | |
9 | ||
10 | #include "wx/wxprec.h" | |
11 | ||
12 | #ifdef __BORLANDC__ | |
13 | #pragma hdrstop | |
14 | #endif | |
15 | ||
16 | #if wxUSE_HTML && wxUSE_STREAMS | |
17 | ||
18 | #ifndef WX_PRECOMP | |
19 | #include "wx/wxcrtvararg.h" | |
20 | #endif | |
21 | ||
22 | #include "wx/html/forcelnk.h" | |
23 | #include "wx/html/m_templ.h" | |
24 | ||
25 | #include "wx/html/htmlcell.h" | |
26 | ||
27 | FORCE_LINK_ME(m_tables) | |
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 | ||
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; | |
65 | bool nowrap; | |
66 | }; | |
67 | ||
68 | ||
69 | class wxHtmlTableCell : public wxHtmlContainerCell | |
70 | { | |
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); | |
102 | virtual ~wxHtmlTableCell(); | |
103 | ||
104 | virtual void RemoveExtraSpacing(bool top, bool bottom); | |
105 | ||
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 | |
119 | // only once, before first Layout(). | |
120 | void ComputeMinMaxWidths(); | |
121 | ||
122 | DECLARE_NO_COPY_CLASS(wxHtmlTableCell) | |
123 | }; | |
124 | ||
125 | ||
126 | ||
127 | wxHtmlTableCell::wxHtmlTableCell(wxHtmlContainerCell *parent, const wxHtmlTag& tag, double pixel_scale) | |
128 | : wxHtmlContainerCell(parent) | |
129 | { | |
130 | m_PixelScale = pixel_scale; | |
131 | m_HasBorders = | |
132 | (tag.HasParam(wxT("BORDER")) && tag.GetParam(wxT("BORDER")) != wxT("0")); | |
133 | m_ColsInfo = NULL; | |
134 | m_NumCols = m_NumRows = 0; | |
135 | m_CellInfo = NULL; | |
136 | m_ActualCol = m_ActualRow = -1; | |
137 | ||
138 | /* scan params: */ | |
139 | if (tag.HasParam(wxT("BGCOLOR"))) | |
140 | { | |
141 | tag.GetParamAsColour(wxT("BGCOLOR"), &m_tBkg); | |
142 | if (m_tBkg.Ok()) | |
143 | SetBackgroundColour(m_tBkg); | |
144 | } | |
145 | if (tag.HasParam(wxT("VALIGN"))) | |
146 | m_tValign = tag.GetParam(wxT("VALIGN")); | |
147 | else | |
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; | |
153 | m_Spacing = (int)(m_PixelScale * (double)m_Spacing); | |
154 | m_Padding = (int)(m_PixelScale * (double)m_Padding); | |
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); | |
165 | if (m_CellInfo) | |
166 | { | |
167 | for (int i = 0; i < m_NumRows; i++) | |
168 | free(m_CellInfo[i]); | |
169 | free(m_CellInfo); | |
170 | } | |
171 | } | |
172 | ||
173 | ||
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 | } | |
183 | ||
184 | void wxHtmlTableCell::ReallocCols(int cols) | |
185 | { | |
186 | int i,j; | |
187 | ||
188 | for (i = 0; i < m_NumRows; i++) | |
189 | { | |
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); | |
196 | for (j = m_NumCols; j < cols; j++) | |
197 | { | |
198 | m_ColsInfo[j].width = 0; | |
199 | m_ColsInfo[j].units = wxHTML_UNITS_PERCENT; | |
200 | m_ColsInfo[j].minWidth = m_ColsInfo[j].maxWidth = -1; | |
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); | |
211 | for (int row = m_NumRows; row < rows ; row++) | |
212 | { | |
213 | if (m_NumCols == 0) | |
214 | m_CellInfo[row] = NULL; | |
215 | else | |
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 | } | |
221 | } | |
222 | m_NumRows = rows; | |
223 | } | |
224 | ||
225 | ||
226 | void wxHtmlTableCell::AddRow(const wxHtmlTag& tag) | |
227 | { | |
228 | m_ActualCol = -1; | |
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. | |
232 | ||
233 | // scan params: | |
234 | m_rBkg = m_tBkg; | |
235 | if (tag.HasParam(wxT("BGCOLOR"))) | |
236 | tag.GetParamAsColour(wxT("BGCOLOR"), &m_rBkg); | |
237 | if (tag.HasParam(wxT("VALIGN"))) | |
238 | m_rValign = tag.GetParam(wxT("VALIGN")); | |
239 | else | |
240 | m_rValign = m_tValign; | |
241 | } | |
242 | ||
243 | ||
244 | ||
245 | void wxHtmlTableCell::AddCell(wxHtmlContainerCell *cell, const wxHtmlTag& tag) | |
246 | { | |
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: | |
257 | do | |
258 | { | |
259 | m_ActualCol++; | |
260 | } while ((m_ActualCol < m_NumCols) && | |
261 | (m_CellInfo[m_ActualRow][m_ActualCol].flag != cellFree)); | |
262 | ||
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; | |
273 | m_CellInfo[r][c].valign = wxHTML_ALIGN_TOP; | |
274 | ||
275 | /* scan for parameters: */ | |
276 | ||
277 | // width: | |
278 | { | |
279 | if (tag.HasParam(wxT("WIDTH"))) | |
280 | { | |
281 | wxString wd = tag.GetParam(wxT("WIDTH")); | |
282 | ||
283 | if (wd[wd.length()-1] == wxT('%')) | |
284 | { | |
285 | if ( wxSscanf(wd.c_str(), wxT("%i%%"), &m_ColsInfo[c].width) == 1 ) | |
286 | { | |
287 | m_ColsInfo[c].units = wxHTML_UNITS_PERCENT; | |
288 | } | |
289 | } | |
290 | else | |
291 | { | |
292 | long width; | |
293 | if ( wd.ToLong(&width) ) | |
294 | { | |
295 | m_ColsInfo[c].width = (int)(m_PixelScale * (double)width); | |
296 | m_ColsInfo[c].units = wxHTML_UNITS_PIXELS; | |
297 | } | |
298 | } | |
299 | } | |
300 | } | |
301 | ||
302 | ||
303 | // spanning: | |
304 | { | |
305 | tag.GetParamAsInt(wxT("COLSPAN"), &m_CellInfo[r][c].colspan); | |
306 | tag.GetParamAsInt(wxT("ROWSPAN"), &m_CellInfo[r][c].rowspan); | |
307 | ||
308 | // VS: the standard says this about col/rowspan: | |
309 | // "This attribute specifies the number of rows spanned by the | |
310 | // current cell. The default value of this attribute is one ("1"). | |
311 | // The value zero ("0") means that the cell spans all rows from the | |
312 | // current row to the last row of the table." All mainstream | |
313 | // browsers act as if 0==1, though, and so does wxHTML. | |
314 | if (m_CellInfo[r][c].colspan < 1) | |
315 | m_CellInfo[r][c].colspan = 1; | |
316 | if (m_CellInfo[r][c].rowspan < 1) | |
317 | m_CellInfo[r][c].rowspan = 1; | |
318 | ||
319 | if ((m_CellInfo[r][c].colspan > 1) || (m_CellInfo[r][c].rowspan > 1)) | |
320 | { | |
321 | int i, j; | |
322 | ||
323 | if (r + m_CellInfo[r][c].rowspan > m_NumRows) | |
324 | ReallocRows(r + m_CellInfo[r][c].rowspan); | |
325 | if (c + m_CellInfo[r][c].colspan > m_NumCols) | |
326 | ReallocCols(c + m_CellInfo[r][c].colspan); | |
327 | for (i = r; i < r + m_CellInfo[r][c].rowspan; i++) | |
328 | for (j = c; j < c + m_CellInfo[r][c].colspan; j++) | |
329 | m_CellInfo[i][j].flag = cellSpan; | |
330 | m_CellInfo[r][c].flag = cellUsed; | |
331 | } | |
332 | } | |
333 | ||
334 | //background color: | |
335 | { | |
336 | wxColour bk = m_rBkg; | |
337 | if (tag.HasParam(wxT("BGCOLOR"))) | |
338 | tag.GetParamAsColour(wxT("BGCOLOR"), &bk); | |
339 | if (bk.Ok()) | |
340 | cell->SetBackgroundColour(bk); | |
341 | } | |
342 | if (m_HasBorders) | |
343 | cell->SetBorder(TABLE_BORDER_CLR_2, TABLE_BORDER_CLR_1); | |
344 | ||
345 | // vertical alignment: | |
346 | { | |
347 | wxString valign; | |
348 | if (tag.HasParam(wxT("VALIGN"))) | |
349 | valign = tag.GetParam(wxT("VALIGN")); | |
350 | else | |
351 | valign = m_tValign; | |
352 | valign.MakeUpper(); | |
353 | if (valign == wxT("TOP")) | |
354 | m_CellInfo[r][c].valign = wxHTML_ALIGN_TOP; | |
355 | else if (valign == wxT("BOTTOM")) | |
356 | m_CellInfo[r][c].valign = wxHTML_ALIGN_BOTTOM; | |
357 | else m_CellInfo[r][c].valign = wxHTML_ALIGN_CENTER; | |
358 | } | |
359 | ||
360 | // nowrap | |
361 | if (tag.HasParam(wxT("NOWRAP"))) | |
362 | m_CellInfo[r][c].nowrap = true; | |
363 | else | |
364 | m_CellInfo[r][c].nowrap = false; | |
365 | ||
366 | cell->SetIndent(m_Padding, wxHTML_INDENT_ALL, wxHTML_UNITS_PIXELS); | |
367 | } | |
368 | ||
369 | void wxHtmlTableCell::ComputeMinMaxWidths() | |
370 | { | |
371 | if (m_NumCols == 0 || m_ColsInfo[0].minWidth != wxDefaultCoord) return; | |
372 | ||
373 | m_MaxTotalWidth = 0; | |
374 | int percentage = 0; | |
375 | for (int c = 0; c < m_NumCols; c++) | |
376 | { | |
377 | for (int r = 0; r < m_NumRows; r++) | |
378 | { | |
379 | cellStruct& cell = m_CellInfo[r][c]; | |
380 | if (cell.flag == cellUsed) | |
381 | { | |
382 | cell.cont->Layout(2*m_Padding + 1); | |
383 | int maxWidth = cell.cont->GetMaxTotalWidth(); | |
384 | int width = cell.nowrap?maxWidth:cell.cont->GetWidth(); | |
385 | width -= (cell.colspan-1) * m_Spacing; | |
386 | maxWidth -= (cell.colspan-1) * m_Spacing; | |
387 | // HTML 4.0 says it is acceptable to distribute min/max | |
388 | width /= cell.colspan; | |
389 | maxWidth /= cell.colspan; | |
390 | for (int j = 0; j < cell.colspan; j++) { | |
391 | if (width > m_ColsInfo[c+j].minWidth) | |
392 | m_ColsInfo[c+j].minWidth = width; | |
393 | if (maxWidth > m_ColsInfo[c+j].maxWidth) | |
394 | m_ColsInfo[c+j].maxWidth = maxWidth; | |
395 | } | |
396 | } | |
397 | } | |
398 | // Calculate maximum table width, required for nested tables | |
399 | if (m_ColsInfo[c].units == wxHTML_UNITS_PIXELS) | |
400 | m_MaxTotalWidth += wxMax(m_ColsInfo[c].width, m_ColsInfo[c].minWidth); | |
401 | else if ((m_ColsInfo[c].units == wxHTML_UNITS_PERCENT) && (m_ColsInfo[c].width != 0)) | |
402 | percentage += m_ColsInfo[c].width; | |
403 | else | |
404 | m_MaxTotalWidth += m_ColsInfo[c].maxWidth; | |
405 | } | |
406 | ||
407 | if (percentage >= 100) | |
408 | { | |
409 | // Table would have infinite length | |
410 | // Make it ridiculous large | |
411 | m_MaxTotalWidth = 0xFFFFFF; | |
412 | } | |
413 | else | |
414 | m_MaxTotalWidth = m_MaxTotalWidth * 100 / (100 - percentage); | |
415 | ||
416 | m_MaxTotalWidth += (m_NumCols + 1) * m_Spacing; | |
417 | } | |
418 | ||
419 | void wxHtmlTableCell::Layout(int w) | |
420 | { | |
421 | ComputeMinMaxWidths(); | |
422 | ||
423 | wxHtmlCell::Layout(w); | |
424 | ||
425 | /* | |
426 | ||
427 | WIDTH ADJUSTING : | |
428 | ||
429 | */ | |
430 | ||
431 | if (m_WidthFloatUnits == wxHTML_UNITS_PERCENT) | |
432 | { | |
433 | if (m_WidthFloat < 0) | |
434 | { | |
435 | if (m_WidthFloat < -100) | |
436 | m_WidthFloat = -100; | |
437 | m_Width = (100 + m_WidthFloat) * w / 100; | |
438 | } | |
439 | else | |
440 | { | |
441 | if (m_WidthFloat > 100) | |
442 | m_WidthFloat = 100; | |
443 | m_Width = m_WidthFloat * w / 100; | |
444 | } | |
445 | } | |
446 | else | |
447 | { | |
448 | if (m_WidthFloat < 0) m_Width = w + m_WidthFloat; | |
449 | else m_Width = m_WidthFloat; | |
450 | } | |
451 | ||
452 | ||
453 | /* | |
454 | ||
455 | LAYOUTING : | |
456 | ||
457 | */ | |
458 | ||
459 | /* 1. setup columns widths: | |
460 | ||
461 | The algorithm tries to keep the table size less than w if possible. | |
462 | */ | |
463 | { | |
464 | int wpix = m_Width - (m_NumCols + 1) * m_Spacing; | |
465 | int i, j; | |
466 | ||
467 | // 1a. setup fixed-width columns: | |
468 | for (i = 0; i < m_NumCols; i++) | |
469 | if (m_ColsInfo[i].units == wxHTML_UNITS_PIXELS) | |
470 | { | |
471 | m_ColsInfo[i].pixwidth = wxMax(m_ColsInfo[i].width, | |
472 | m_ColsInfo[i].minWidth); | |
473 | wpix -= m_ColsInfo[i].pixwidth; | |
474 | } | |
475 | ||
476 | // 1b. Calculate maximum possible width if line wrapping would be disabled | |
477 | // Recalculate total width if m_WidthFloat is zero to keep tables as small | |
478 | // as possible. | |
479 | int maxWidth = 0; | |
480 | for (i = 0; i < m_NumCols; i++) | |
481 | if (m_ColsInfo[i].width == 0) | |
482 | { | |
483 | maxWidth += m_ColsInfo[i].maxWidth; | |
484 | } | |
485 | ||
486 | if (!m_WidthFloat) | |
487 | { | |
488 | // Recalculate table width since no table width was initially given | |
489 | int newWidth = m_Width - wpix + maxWidth; | |
490 | ||
491 | // Make sure that floating-width columns will have the right size. | |
492 | // Calculate sum of all floating-width columns | |
493 | int percentage = 0; | |
494 | for (i = 0; i < m_NumCols; i++) | |
495 | if ((m_ColsInfo[i].units == wxHTML_UNITS_PERCENT) && (m_ColsInfo[i].width != 0)) | |
496 | percentage += m_ColsInfo[i].width; | |
497 | ||
498 | if (percentage >= 100) | |
499 | newWidth = w; | |
500 | else | |
501 | newWidth = newWidth * 100 / (100 - percentage); | |
502 | ||
503 | newWidth = wxMin(newWidth, w - (m_NumCols + 1) * m_Spacing); | |
504 | wpix -= m_Width - newWidth; | |
505 | m_Width = newWidth; | |
506 | } | |
507 | ||
508 | ||
509 | // 1c. setup floating-width columns: | |
510 | int wtemp = wpix; | |
511 | for (i = 0; i < m_NumCols; i++) | |
512 | if ((m_ColsInfo[i].units == wxHTML_UNITS_PERCENT) && (m_ColsInfo[i].width != 0)) | |
513 | { | |
514 | m_ColsInfo[i].pixwidth = wxMin(m_ColsInfo[i].width, 100) * wpix / 100; | |
515 | ||
516 | // Make sure to leave enough space for the other columns | |
517 | int minRequired = 0; | |
518 | for (j = 0; j < m_NumCols; j++) | |
519 | { | |
520 | if ((m_ColsInfo[j].units == wxHTML_UNITS_PERCENT && j > i) || | |
521 | !m_ColsInfo[j].width) | |
522 | minRequired += m_ColsInfo[j].minWidth; | |
523 | } | |
524 | m_ColsInfo[i].pixwidth = wxMax(wxMin(wtemp - minRequired, m_ColsInfo[i].pixwidth), m_ColsInfo[i].minWidth); | |
525 | ||
526 | wtemp -= m_ColsInfo[i].pixwidth; | |
527 | } | |
528 | wpix = wtemp; | |
529 | ||
530 | // 1d. setup default columns (no width specification supplied): | |
531 | // The algorithm assigns calculates the maximum possible width if line | |
532 | // wrapping would be disabled and assigns column width as a fraction | |
533 | // based upon the maximum width of a column | |
534 | // FIXME: I'm not sure if this algorithm is conform to HTML standard, | |
535 | // though it seems to be much better than the old one | |
536 | ||
537 | for (i = j = 0; i < m_NumCols; i++) | |
538 | if (m_ColsInfo[i].width == 0) j++; | |
539 | if (wpix < 0) | |
540 | wpix = 0; | |
541 | ||
542 | // Assign widths | |
543 | for (i = 0; i < m_NumCols; i++) | |
544 | if (m_ColsInfo[i].width == 0) | |
545 | { | |
546 | // Assign with, make sure not to drop below minWidth | |
547 | if (maxWidth) | |
548 | m_ColsInfo[i].pixwidth = (int)(wpix * (m_ColsInfo[i].maxWidth / (float)maxWidth) + 0.5); | |
549 | else | |
550 | m_ColsInfo[i].pixwidth = wpix / j; | |
551 | ||
552 | // Make sure to leave enough space for the other columns | |
553 | int minRequired = 0; | |
554 | int r; | |
555 | for (r = i + 1; r < m_NumCols; r++) | |
556 | { | |
557 | if (!m_ColsInfo[r].width) | |
558 | minRequired += m_ColsInfo[r].minWidth; | |
559 | } | |
560 | m_ColsInfo[i].pixwidth = wxMax(wxMin(wpix - minRequired, m_ColsInfo[i].pixwidth), m_ColsInfo[i].minWidth); | |
561 | ||
562 | if (maxWidth) | |
563 | { | |
564 | if (m_ColsInfo[i].pixwidth > (wpix * (m_ColsInfo[i].maxWidth / (float)maxWidth) + 0.5)) | |
565 | { | |
566 | int diff = (int)(m_ColsInfo[i].pixwidth - (wpix * m_ColsInfo[i].maxWidth / (float)maxWidth + 0.5)); | |
567 | maxWidth += diff - m_ColsInfo[i].maxWidth; | |
568 | } | |
569 | else | |
570 | maxWidth -= m_ColsInfo[i].maxWidth; | |
571 | } | |
572 | wpix -= m_ColsInfo[i].pixwidth; | |
573 | } | |
574 | } | |
575 | ||
576 | /* 2. compute positions of columns: */ | |
577 | { | |
578 | int wpos = m_Spacing; | |
579 | for (int i = 0; i < m_NumCols; i++) | |
580 | { | |
581 | m_ColsInfo[i].leftpos = wpos; | |
582 | wpos += m_ColsInfo[i].pixwidth + m_Spacing; | |
583 | } | |
584 | ||
585 | // add the remaining space to the last column | |
586 | if (m_NumCols > 0 && wpos < m_Width) | |
587 | m_ColsInfo[m_NumCols-1].pixwidth += m_Width - wpos; | |
588 | } | |
589 | ||
590 | /* 3. sub-layout all cells: */ | |
591 | { | |
592 | int *ypos = new int[m_NumRows + 1]; | |
593 | ||
594 | int actcol, actrow; | |
595 | int fullwid; | |
596 | wxHtmlContainerCell *actcell; | |
597 | ||
598 | ypos[0] = m_Spacing; | |
599 | for (actrow = 1; actrow <= m_NumRows; actrow++) ypos[actrow] = -1; | |
600 | for (actrow = 0; actrow < m_NumRows; actrow++) | |
601 | { | |
602 | if (ypos[actrow] == -1) ypos[actrow] = ypos[actrow-1]; | |
603 | // 3a. sub-layout and detect max height: | |
604 | ||
605 | for (actcol = 0; actcol < m_NumCols; actcol++) { | |
606 | if (m_CellInfo[actrow][actcol].flag != cellUsed) continue; | |
607 | actcell = m_CellInfo[actrow][actcol].cont; | |
608 | fullwid = 0; | |
609 | for (int i = actcol; i < m_CellInfo[actrow][actcol].colspan + actcol; i++) | |
610 | fullwid += m_ColsInfo[i].pixwidth; | |
611 | fullwid += (m_CellInfo[actrow][actcol].colspan - 1) * m_Spacing; | |
612 | actcell->SetMinHeight(m_CellInfo[actrow][actcol].minheight, m_CellInfo[actrow][actcol].valign); | |
613 | actcell->Layout(fullwid); | |
614 | ||
615 | if (ypos[actrow] + actcell->GetHeight() + m_CellInfo[actrow][actcol].rowspan * m_Spacing > ypos[actrow + m_CellInfo[actrow][actcol].rowspan]) | |
616 | ypos[actrow + m_CellInfo[actrow][actcol].rowspan] = | |
617 | ypos[actrow] + actcell->GetHeight() + m_CellInfo[actrow][actcol].rowspan * m_Spacing; | |
618 | } | |
619 | } | |
620 | ||
621 | for (actrow = 0; actrow < m_NumRows; actrow++) | |
622 | { | |
623 | // 3b. place cells in row & let'em all have same height: | |
624 | ||
625 | for (actcol = 0; actcol < m_NumCols; actcol++) | |
626 | { | |
627 | if (m_CellInfo[actrow][actcol].flag != cellUsed) continue; | |
628 | actcell = m_CellInfo[actrow][actcol].cont; | |
629 | actcell->SetMinHeight( | |
630 | ypos[actrow + m_CellInfo[actrow][actcol].rowspan] - ypos[actrow] - m_Spacing, | |
631 | m_CellInfo[actrow][actcol].valign); | |
632 | fullwid = 0; | |
633 | for (int i = actcol; i < m_CellInfo[actrow][actcol].colspan + actcol; i++) | |
634 | fullwid += m_ColsInfo[i].pixwidth; | |
635 | fullwid += (m_CellInfo[actrow][actcol].colspan - 1) * m_Spacing; | |
636 | actcell->Layout(fullwid); | |
637 | actcell->SetPos(m_ColsInfo[actcol].leftpos, ypos[actrow]); | |
638 | } | |
639 | } | |
640 | m_Height = ypos[m_NumRows]; | |
641 | delete[] ypos; | |
642 | } | |
643 | ||
644 | /* 4. adjust table's width if it was too small: */ | |
645 | if (m_NumCols > 0) | |
646 | { | |
647 | int twidth = m_ColsInfo[m_NumCols-1].leftpos + | |
648 | m_ColsInfo[m_NumCols-1].pixwidth + m_Spacing; | |
649 | if (twidth > m_Width) | |
650 | m_Width = twidth; | |
651 | } | |
652 | } | |
653 | ||
654 | ||
655 | ||
656 | ||
657 | ||
658 | ||
659 | //----------------------------------------------------------------------------- | |
660 | // The tables handler: | |
661 | //----------------------------------------------------------------------------- | |
662 | ||
663 | ||
664 | TAG_HANDLER_BEGIN(TABLE, "TABLE,TR,TD,TH") | |
665 | ||
666 | TAG_HANDLER_VARS | |
667 | wxHtmlTableCell* m_Table; | |
668 | wxString m_tAlign, m_rAlign; | |
669 | wxHtmlContainerCell *m_enclosingContainer; | |
670 | ||
671 | TAG_HANDLER_CONSTR(TABLE) | |
672 | { | |
673 | m_Table = NULL; | |
674 | m_enclosingContainer = NULL; | |
675 | m_tAlign = m_rAlign = wxEmptyString; | |
676 | } | |
677 | ||
678 | ||
679 | TAG_HANDLER_PROC(tag) | |
680 | { | |
681 | wxHtmlContainerCell *c; | |
682 | ||
683 | // new table started, backup upper-level table (if any) and create new: | |
684 | if (tag.GetName() == wxT("TABLE")) | |
685 | { | |
686 | wxHtmlTableCell *oldt = m_Table; | |
687 | ||
688 | m_enclosingContainer = c = m_WParser->OpenContainer(); | |
689 | ||
690 | m_Table = new wxHtmlTableCell(c, tag, m_WParser->GetPixelScale()); | |
691 | ||
692 | // width: | |
693 | { | |
694 | if (tag.HasParam(wxT("WIDTH"))) | |
695 | { | |
696 | wxString wd = tag.GetParam(wxT("WIDTH")); | |
697 | ||
698 | if (wd[wd.length()-1] == wxT('%')) | |
699 | { | |
700 | int width = 0; | |
701 | wxSscanf(wd.c_str(), wxT("%i%%"), &width); | |
702 | m_Table->SetWidthFloat(width, wxHTML_UNITS_PERCENT); | |
703 | } | |
704 | else | |
705 | { | |
706 | int width = 0; | |
707 | wxSscanf(wd.c_str(), wxT("%i"), &width); | |
708 | m_Table->SetWidthFloat((int)(m_WParser->GetPixelScale() * width), wxHTML_UNITS_PIXELS); | |
709 | } | |
710 | } | |
711 | else | |
712 | m_Table->SetWidthFloat(0, wxHTML_UNITS_PIXELS); | |
713 | } | |
714 | int oldAlign = m_WParser->GetAlign(); | |
715 | m_tAlign = wxEmptyString; | |
716 | if (tag.HasParam(wxT("ALIGN"))) | |
717 | m_tAlign = tag.GetParam(wxT("ALIGN")); | |
718 | ||
719 | ParseInner(tag); | |
720 | ||
721 | m_WParser->SetAlign(oldAlign); | |
722 | m_WParser->SetContainer(m_enclosingContainer); | |
723 | m_WParser->CloseContainer(); | |
724 | ||
725 | m_Table = oldt; | |
726 | ||
727 | return true; // ParseInner() called | |
728 | } | |
729 | ||
730 | ||
731 | else if (m_Table) | |
732 | { | |
733 | // new row in table | |
734 | if (tag.GetName() == wxT("TR")) | |
735 | { | |
736 | m_Table->AddRow(tag); | |
737 | m_rAlign = m_tAlign; | |
738 | if (tag.HasParam(wxT("ALIGN"))) | |
739 | m_rAlign = tag.GetParam(wxT("ALIGN")); | |
740 | } | |
741 | ||
742 | // new cell | |
743 | else | |
744 | { | |
745 | c = m_WParser->SetContainer(new wxHtmlContainerCell(m_Table)); | |
746 | m_Table->AddCell(c, tag); | |
747 | ||
748 | m_WParser->OpenContainer(); | |
749 | ||
750 | if (tag.GetName() == wxT("TH")) /*header style*/ | |
751 | m_WParser->SetAlign(wxHTML_ALIGN_CENTER); | |
752 | else | |
753 | m_WParser->SetAlign(wxHTML_ALIGN_LEFT); | |
754 | ||
755 | wxString als; | |
756 | ||
757 | als = m_rAlign; | |
758 | if (tag.HasParam(wxT("ALIGN"))) | |
759 | als = tag.GetParam(wxT("ALIGN")); | |
760 | als.MakeUpper(); | |
761 | if (als == wxT("RIGHT")) | |
762 | m_WParser->SetAlign(wxHTML_ALIGN_RIGHT); | |
763 | else if (als == wxT("LEFT")) | |
764 | m_WParser->SetAlign(wxHTML_ALIGN_LEFT); | |
765 | else if (als == wxT("CENTER")) | |
766 | m_WParser->SetAlign(wxHTML_ALIGN_CENTER); | |
767 | ||
768 | m_WParser->OpenContainer(); | |
769 | ||
770 | ParseInner(tag); | |
771 | ||
772 | // set the current container back to the enclosing one so that | |
773 | // text outside of <th> and <td> isn't included in any cell | |
774 | // (this happens often enough in practice because it's common | |
775 | // to use whitespace between </td> and the next <td>): | |
776 | m_WParser->SetContainer(m_enclosingContainer); | |
777 | ||
778 | return true; // ParseInner() called | |
779 | } | |
780 | } | |
781 | ||
782 | return false; | |
783 | } | |
784 | ||
785 | TAG_HANDLER_END(TABLE) | |
786 | ||
787 | ||
788 | ||
789 | ||
790 | ||
791 | TAGS_MODULE_BEGIN(Tables) | |
792 | ||
793 | TAGS_MODULE_ADD(TABLE) | |
794 | ||
795 | TAGS_MODULE_END(Tables) | |
796 | ||
797 | ||
798 | #endif |