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