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