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