| 1 | ///////////////////////////////////////////////////////////////////////////// |
| 2 | // Name: 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 | #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA) |
| 11 | #pragma implementation |
| 12 | #endif |
| 13 | |
| 14 | #include "wx/wxprec.h" |
| 15 | |
| 16 | #include "wx/defs.h" |
| 17 | #if wxUSE_HTML && wxUSE_STREAMS |
| 18 | #ifdef __BORLANDC__ |
| 19 | #pragma hdrstop |
| 20 | #endif |
| 21 | |
| 22 | #ifndef WXPRECOMP |
| 23 | #endif |
| 24 | |
| 25 | #include "wx/html/forcelnk.h" |
| 26 | #include "wx/html/m_templ.h" |
| 27 | |
| 28 | #include "wx/html/htmlcell.h" |
| 29 | |
| 30 | FORCE_LINK_ME(m_tables) |
| 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 | |
| 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; |
| 68 | bool nowrap; |
| 69 | }; |
| 70 | |
| 71 | |
| 72 | class wxHtmlTableCell : public wxHtmlContainerCell |
| 73 | { |
| 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 |
| 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 | tag.GetParamAsColour(wxT("BGCOLOR"), &m_tBkg); |
| 141 | if (tag.HasParam(wxT("VALIGN"))) |
| 142 | m_tValign = tag.GetParam(wxT("VALIGN")); |
| 143 | else |
| 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; |
| 149 | m_Spacing = (int)(m_PixelScale * (double)m_Spacing); |
| 150 | m_Padding = (int)(m_PixelScale * (double)m_Padding); |
| 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); |
| 161 | if (m_CellInfo) |
| 162 | { |
| 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 | |
| 175 | for (i = 0; i < m_NumRows; i++) |
| 176 | { |
| 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); |
| 183 | for (j = m_NumCols; j < cols; j++) |
| 184 | { |
| 185 | m_ColsInfo[j].width = 0; |
| 186 | m_ColsInfo[j].units = wxHTML_UNITS_PERCENT; |
| 187 | m_ColsInfo[j].minWidth = m_ColsInfo[j].maxWidth = -1; |
| 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); |
| 198 | for (int row = m_NumRows; row < rows ; row++) |
| 199 | { |
| 200 | if (m_NumCols == 0) |
| 201 | m_CellInfo[row] = NULL; |
| 202 | else |
| 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 | } |
| 208 | } |
| 209 | m_NumRows = rows; |
| 210 | } |
| 211 | |
| 212 | |
| 213 | void wxHtmlTableCell::AddRow(const wxHtmlTag& tag) |
| 214 | { |
| 215 | m_ActualCol = -1; |
| 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. |
| 219 | |
| 220 | // scan params: |
| 221 | m_rBkg = m_tBkg; |
| 222 | if (tag.HasParam(wxT("BGCOLOR"))) |
| 223 | tag.GetParamAsColour(wxT("BGCOLOR"), &m_rBkg); |
| 224 | if (tag.HasParam(wxT("VALIGN"))) |
| 225 | m_rValign = tag.GetParam(wxT("VALIGN")); |
| 226 | else |
| 227 | m_rValign = m_tValign; |
| 228 | } |
| 229 | |
| 230 | |
| 231 | |
| 232 | void wxHtmlTableCell::AddCell(wxHtmlContainerCell *cell, const wxHtmlTag& tag) |
| 233 | { |
| 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: |
| 244 | do |
| 245 | { |
| 246 | m_ActualCol++; |
| 247 | } while ((m_ActualCol < m_NumCols) && |
| 248 | (m_CellInfo[m_ActualRow][m_ActualCol].flag != cellFree)); |
| 249 | |
| 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; |
| 260 | m_CellInfo[r][c].valign = wxHTML_ALIGN_TOP; |
| 261 | |
| 262 | /* scan for parameters: */ |
| 263 | |
| 264 | // width: |
| 265 | { |
| 266 | if (tag.HasParam(wxT("WIDTH"))) |
| 267 | { |
| 268 | wxString wd = tag.GetParam(wxT("WIDTH")); |
| 269 | |
| 270 | if (wd[wd.Length()-1] == wxT('%')) |
| 271 | { |
| 272 | wxSscanf(wd.c_str(), wxT("%i%%"), &m_ColsInfo[c].width); |
| 273 | m_ColsInfo[c].units = wxHTML_UNITS_PERCENT; |
| 274 | } |
| 275 | else |
| 276 | { |
| 277 | wxSscanf(wd.c_str(), wxT("%i"), &m_ColsInfo[c].width); |
| 278 | m_ColsInfo[c].width = (int)(m_PixelScale * (double)m_ColsInfo[c].width); |
| 279 | m_ColsInfo[c].units = wxHTML_UNITS_PIXELS; |
| 280 | } |
| 281 | } |
| 282 | } |
| 283 | |
| 284 | |
| 285 | // spanning: |
| 286 | { |
| 287 | tag.GetParamAsInt(wxT("COLSPAN"), &m_CellInfo[r][c].colspan); |
| 288 | tag.GetParamAsInt(wxT("ROWSPAN"), &m_CellInfo[r][c].rowspan); |
| 289 | |
| 290 | // VS: the standard says this about col/rowspan: |
| 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 |
| 295 | // browsers act as if 0==1, though, and so does wxHTML. |
| 296 | if (m_CellInfo[r][c].colspan < 1) |
| 297 | m_CellInfo[r][c].colspan = 1; |
| 298 | if (m_CellInfo[r][c].rowspan < 1) |
| 299 | m_CellInfo[r][c].rowspan = 1; |
| 300 | |
| 301 | if ((m_CellInfo[r][c].colspan > 1) || (m_CellInfo[r][c].rowspan > 1)) |
| 302 | { |
| 303 | int i, j; |
| 304 | |
| 305 | if (r + m_CellInfo[r][c].rowspan > m_NumRows) |
| 306 | ReallocRows(r + m_CellInfo[r][c].rowspan); |
| 307 | if (c + m_CellInfo[r][c].colspan > m_NumCols) |
| 308 | ReallocCols(c + m_CellInfo[r][c].colspan); |
| 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 | { |
| 318 | wxColour bk = m_rBkg; |
| 319 | if (tag.HasParam(wxT("BGCOLOR"))) |
| 320 | tag.GetParamAsColour(wxT("BGCOLOR"), &bk); |
| 321 | if (bk.Ok()) |
| 322 | cell->SetBackgroundColour(bk); |
| 323 | } |
| 324 | if (m_HasBorders) |
| 325 | cell->SetBorder(TABLE_BORDER_CLR_2, TABLE_BORDER_CLR_1); |
| 326 | |
| 327 | // vertical alignment: |
| 328 | { |
| 329 | wxString valign; |
| 330 | if (tag.HasParam(wxT("VALIGN"))) |
| 331 | valign = tag.GetParam(wxT("VALIGN")); |
| 332 | else |
| 333 | valign = m_tValign; |
| 334 | valign.MakeUpper(); |
| 335 | if (valign == wxT("TOP")) |
| 336 | m_CellInfo[r][c].valign = wxHTML_ALIGN_TOP; |
| 337 | else if (valign == wxT("BOTTOM")) |
| 338 | m_CellInfo[r][c].valign = wxHTML_ALIGN_BOTTOM; |
| 339 | else m_CellInfo[r][c].valign = wxHTML_ALIGN_CENTER; |
| 340 | } |
| 341 | |
| 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 | |
| 348 | cell->SetIndent(m_Padding, wxHTML_INDENT_ALL, wxHTML_UNITS_PIXELS); |
| 349 | } |
| 350 | |
| 351 | void wxHtmlTableCell::ComputeMinMaxWidths() |
| 352 | { |
| 353 | if (m_NumCols == 0 || m_ColsInfo[0].minWidth != wxDefaultCoord) return; |
| 354 | |
| 355 | m_MaxTotalWidth = 0; |
| 356 | int percentage = 0; |
| 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 | { |
| 364 | cell.cont->Layout(2*m_Padding + 1); |
| 365 | int maxWidth = cell.cont->GetMaxTotalWidth(); |
| 366 | int width = cell.nowrap?maxWidth:cell.cont->GetWidth(); |
| 367 | width -= (cell.colspan-1) * m_Spacing; |
| 368 | maxWidth -= (cell.colspan-1) * m_Spacing; |
| 369 | // HTML 4.0 says it is acceptable to distribute min/max |
| 370 | width /= cell.colspan; |
| 371 | maxWidth /= cell.colspan; |
| 372 | for (int j = 0; j < cell.colspan; j++) { |
| 373 | if (width > m_ColsInfo[c+j].minWidth) |
| 374 | m_ColsInfo[c+j].minWidth = width; |
| 375 | if (maxWidth > m_ColsInfo[c+j].maxWidth) |
| 376 | m_ColsInfo[c+j].maxWidth = maxWidth; |
| 377 | } |
| 378 | } |
| 379 | } |
| 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; |
| 387 | } |
| 388 | |
| 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 | } |
| 400 | |
| 401 | void wxHtmlTableCell::Layout(int w) |
| 402 | { |
| 403 | ComputeMinMaxWidths(); |
| 404 | |
| 405 | wxHtmlCell::Layout(w); |
| 406 | |
| 407 | /* |
| 408 | |
| 409 | WIDTH ADJUSTING : |
| 410 | |
| 411 | */ |
| 412 | |
| 413 | if (m_WidthFloatUnits == wxHTML_UNITS_PERCENT) |
| 414 | { |
| 415 | if (m_WidthFloat < 0) |
| 416 | { |
| 417 | if (m_WidthFloat < -100) |
| 418 | m_WidthFloat = -100; |
| 419 | m_Width = (100 + m_WidthFloat) * w / 100; |
| 420 | } |
| 421 | else |
| 422 | { |
| 423 | if (m_WidthFloat > 100) |
| 424 | m_WidthFloat = 100; |
| 425 | m_Width = m_WidthFloat * w / 100; |
| 426 | } |
| 427 | } |
| 428 | else |
| 429 | { |
| 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 | |
| 441 | /* 1. setup columns widths: |
| 442 | |
| 443 | The algorithm tries to keep the table size less than w if possible. |
| 444 | */ |
| 445 | { |
| 446 | int wpix = m_Width - (m_NumCols + 1) * m_Spacing; |
| 447 | int i, j; |
| 448 | |
| 449 | // 1a. setup fixed-width columns: |
| 450 | for (i = 0; i < m_NumCols; i++) |
| 451 | if (m_ColsInfo[i].units == wxHTML_UNITS_PIXELS) |
| 452 | { |
| 453 | m_ColsInfo[i].pixwidth = wxMax(m_ColsInfo[i].width, |
| 454 | m_ColsInfo[i].minWidth); |
| 455 | wpix -= m_ColsInfo[i].pixwidth; |
| 456 | } |
| 457 | |
| 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); |
| 484 | |
| 485 | newWidth = wxMin(newWidth, w - (m_NumCols + 1) * m_Spacing); |
| 486 | wpix -= m_Width - newWidth; |
| 487 | m_Width = newWidth; |
| 488 | } |
| 489 | |
| 490 | |
| 491 | // 1c. setup floating-width columns: |
| 492 | int wtemp = wpix; |
| 493 | for (i = 0; i < m_NumCols; i++) |
| 494 | if ((m_ColsInfo[i].units == wxHTML_UNITS_PERCENT) && (m_ColsInfo[i].width != 0)) |
| 495 | { |
| 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; |
| 509 | } |
| 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 |
| 518 | |
| 519 | for (i = j = 0; i < m_NumCols; i++) |
| 520 | if (m_ColsInfo[i].width == 0) j++; |
| 521 | if (wpix < 0) |
| 522 | wpix = 0; |
| 523 | |
| 524 | // Assign widths |
| 525 | for (i = 0; i < m_NumCols; i++) |
| 526 | if (m_ColsInfo[i].width == 0) |
| 527 | { |
| 528 | // Assign with, make sure not to drop below minWidth |
| 529 | if (maxWidth) |
| 530 | m_ColsInfo[i].pixwidth = (int)(wpix * (m_ColsInfo[i].maxWidth / (float)maxWidth) + 0.5); |
| 531 | else |
| 532 | m_ColsInfo[i].pixwidth = wpix / j; |
| 533 | |
| 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 | { |
| 548 | int diff = (int)(m_ColsInfo[i].pixwidth - (wpix * m_ColsInfo[i].maxWidth / (float)maxWidth + 0.5)); |
| 549 | maxWidth += diff - m_ColsInfo[i].maxWidth; |
| 550 | } |
| 551 | else |
| 552 | maxWidth -= m_ColsInfo[i].maxWidth; |
| 553 | } |
| 554 | wpix -= m_ColsInfo[i].pixwidth; |
| 555 | } |
| 556 | } |
| 557 | |
| 558 | /* 2. compute positions of columns: */ |
| 559 | { |
| 560 | int wpos = m_Spacing; |
| 561 | for (int i = 0; i < m_NumCols; i++) |
| 562 | { |
| 563 | m_ColsInfo[i].leftpos = wpos; |
| 564 | wpos += m_ColsInfo[i].pixwidth + m_Spacing; |
| 565 | } |
| 566 | } |
| 567 | |
| 568 | /* 3. sub-layout all cells: */ |
| 569 | { |
| 570 | int *ypos = new int[m_NumRows + 1]; |
| 571 | |
| 572 | int actcol, actrow; |
| 573 | int fullwid; |
| 574 | wxHtmlContainerCell *actcell; |
| 575 | |
| 576 | ypos[0] = m_Spacing; |
| 577 | for (actrow = 1; actrow <= m_NumRows; actrow++) ypos[actrow] = -1; |
| 578 | for (actrow = 0; actrow < m_NumRows; actrow++) |
| 579 | { |
| 580 | if (ypos[actrow] == -1) ypos[actrow] = ypos[actrow-1]; |
| 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; |
| 589 | fullwid += (m_CellInfo[actrow][actcol].colspan - 1) * m_Spacing; |
| 590 | actcell->SetMinHeight(m_CellInfo[actrow][actcol].minheight, m_CellInfo[actrow][actcol].valign); |
| 591 | actcell->Layout(fullwid); |
| 592 | |
| 593 | if (ypos[actrow] + actcell->GetHeight() + m_CellInfo[actrow][actcol].rowspan * m_Spacing > ypos[actrow + m_CellInfo[actrow][actcol].rowspan]) |
| 594 | ypos[actrow + m_CellInfo[actrow][actcol].rowspan] = |
| 595 | ypos[actrow] + actcell->GetHeight() + m_CellInfo[actrow][actcol].rowspan * m_Spacing; |
| 596 | } |
| 597 | } |
| 598 | |
| 599 | for (actrow = 0; actrow < m_NumRows; actrow++) |
| 600 | { |
| 601 | // 3b. place cells in row & let'em all have same height: |
| 602 | |
| 603 | for (actcol = 0; actcol < m_NumCols; actcol++) |
| 604 | { |
| 605 | if (m_CellInfo[actrow][actcol].flag != cellUsed) continue; |
| 606 | actcell = m_CellInfo[actrow][actcol].cont; |
| 607 | actcell->SetMinHeight( |
| 608 | ypos[actrow + m_CellInfo[actrow][actcol].rowspan] - ypos[actrow] - m_Spacing, |
| 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; |
| 613 | fullwid += (m_CellInfo[actrow][actcol].colspan - 1) * m_Spacing; |
| 614 | actcell->Layout(fullwid); |
| 615 | actcell->SetPos(m_ColsInfo[actcol].leftpos, ypos[actrow]); |
| 616 | } |
| 617 | } |
| 618 | m_Height = ypos[m_NumRows]; |
| 619 | delete[] ypos; |
| 620 | } |
| 621 | |
| 622 | /* 4. adjust table's width if it was too small: */ |
| 623 | if (m_NumCols > 0) |
| 624 | { |
| 625 | int twidth = m_ColsInfo[m_NumCols-1].leftpos + |
| 626 | m_ColsInfo[m_NumCols-1].pixwidth + m_Spacing; |
| 627 | if (twidth > m_Width) |
| 628 | m_Width = twidth; |
| 629 | } |
| 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; |
| 647 | |
| 648 | TAG_HANDLER_CONSTR(TABLE) |
| 649 | { |
| 650 | m_Table = NULL; |
| 651 | m_tAlign = m_rAlign = wxEmptyString; |
| 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: |
| 660 | if (tag.GetName() == wxT("TABLE")) |
| 661 | { |
| 662 | wxHtmlTableCell *oldt = m_Table; |
| 663 | wxHtmlContainerCell *oldcont; |
| 664 | |
| 665 | oldcont = c = m_WParser->OpenContainer(); |
| 666 | |
| 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); |
| 685 | m_Table->SetWidthFloat((int)(m_WParser->GetPixelScale() * width), wxHTML_UNITS_PIXELS); |
| 686 | } |
| 687 | } |
| 688 | else |
| 689 | m_Table->SetWidthFloat(0, wxHTML_UNITS_PIXELS); |
| 690 | } |
| 691 | int oldAlign = m_WParser->GetAlign(); |
| 692 | m_tAlign = wxEmptyString; |
| 693 | if (tag.HasParam(wxT("ALIGN"))) |
| 694 | m_tAlign = tag.GetParam(wxT("ALIGN")); |
| 695 | |
| 696 | ParseInner(tag); |
| 697 | |
| 698 | m_WParser->SetAlign(oldAlign); |
| 699 | m_WParser->SetContainer(oldcont); |
| 700 | m_WParser->CloseContainer(); |
| 701 | |
| 702 | m_Table = oldt; |
| 703 | return true; |
| 704 | } |
| 705 | |
| 706 | |
| 707 | else if (m_Table) |
| 708 | { |
| 709 | // new row in table |
| 710 | if (tag.GetName() == wxT("TR")) |
| 711 | { |
| 712 | m_Table->AddRow(tag); |
| 713 | m_rAlign = m_tAlign; |
| 714 | if (tag.HasParam(wxT("ALIGN"))) |
| 715 | m_rAlign = tag.GetParam(wxT("ALIGN")); |
| 716 | } |
| 717 | |
| 718 | // new cell |
| 719 | else |
| 720 | { |
| 721 | c = m_WParser->SetContainer(new wxHtmlContainerCell(m_Table)); |
| 722 | m_Table->AddCell(c, tag); |
| 723 | |
| 724 | m_WParser->OpenContainer(); |
| 725 | |
| 726 | if (tag.GetName() == wxT("TH")) /*header style*/ |
| 727 | m_WParser->SetAlign(wxHTML_ALIGN_CENTER); |
| 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 | |
| 744 | m_WParser->OpenContainer(); |
| 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 |