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