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