fixed handling of HTML tables with empty row(s)
[wxWidgets.git] / src / html / m_tables.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: m_tables.cpp
3 // Purpose: wxHtml module for tables
4 // Author: Vaclav Slavik
5 // RCS-ID: $Id$
6 // Copyright: (c) 1999 Vaclav Slavik
7 // Licence: wxWindows Licence
8 /////////////////////////////////////////////////////////////////////////////
9
10 #ifdef __GNUG__
11 #pragma implementation
12 #endif
13
14 #include "wx/wxprec.h"
15
16 #include "wx/defs.h"
17 #if wxUSE_HTML && wxUSE_STREAMS
18 #ifdef __BORDLANDC__
19 #pragma hdrstop
20 #endif
21
22 #ifndef WXPRECOMP
23 #include "wx/wx.h"
24 #endif
25
26
27 /*
28 REMARKS:
29 1. This version of m_tables doesn't support auto-layout algorithm.
30 This means that all columns are of same width unless explicitly specified.
31 */
32
33
34 #include "wx/html/forcelnk.h"
35 #include "wx/html/m_templ.h"
36
37 #include "wx/html/htmlcell.h"
38
39 FORCE_LINK_ME(m_tables)
40
41
42 #define TABLE_BORDER_CLR_1 wxColour(0xC5, 0xC2, 0xC5)
43 #define TABLE_BORDER_CLR_2 wxColour(0x62, 0x61, 0x62)
44
45
46 //-----------------------------------------------------------------------------
47 // wxHtmlTableCell
48 //-----------------------------------------------------------------------------
49
50
51 typedef struct {
52 int width, units; // universal
53 int leftpos, pixwidth, maxrealwidth; // temporary (depends on width of table)
54 } colStruct;
55
56 typedef enum {
57 cellSpan,
58 cellUsed,
59 cellFree
60 } cellState;
61
62 typedef struct {
63 wxHtmlContainerCell *cont;
64 int colspan, rowspan;
65 int minheight, valign;
66 cellState flag;
67 } cellStruct;
68
69
70 class wxHtmlTableCell : public wxHtmlContainerCell
71 {
72 protected:
73 /* These are real attributes: */
74 bool m_HasBorders;
75 // should we draw borders or not?
76 int m_NumCols, m_NumRows;
77 // number of columns; rows
78 colStruct *m_ColsInfo;
79 // array of column information
80 cellStruct **m_CellInfo;
81 // 2D array of all cells in the table : m_CellInfo[row][column]
82 int m_Spacing;
83 // spaces between cells
84 int m_Padding;
85 // cells internal indentation
86
87 private:
88 /* ...and these are valid only during parsing of table: */
89 int m_ActualCol, m_ActualRow;
90 // number of actual column (ranging from 0..m_NumCols)
91
92 // default values (for table and row):
93 int m_tBkg, m_rBkg;
94 wxString m_tValign, m_rValign;
95
96 double m_PixelScale;
97
98
99 public:
100 wxHtmlTableCell(wxHtmlContainerCell *parent, const wxHtmlTag& tag, double pixel_scale = 1.0);
101 ~wxHtmlTableCell();
102 virtual void Layout(int w);
103
104 void AddRow(const wxHtmlTag& tag);
105 void AddCell(wxHtmlContainerCell *cell, const wxHtmlTag& tag);
106 private:
107 void ReallocCols(int cols);
108 void ReallocRows(int rows);
109 // reallocates memory to given number of cols/rows
110 // and changes m_NumCols/m_NumRows value to reflect this change
111 // NOTE! You CAN'T change m_NumCols/m_NumRows before calling this!!
112 };
113
114
115
116 wxHtmlTableCell::wxHtmlTableCell(wxHtmlContainerCell *parent, const wxHtmlTag& tag, double pixel_scale)
117 : wxHtmlContainerCell(parent)
118 {
119 m_PixelScale = pixel_scale;
120 m_HasBorders = (tag.HasParam(wxT("BORDER")) && tag.GetParam(wxT("BORDER")) != wxT("0"));
121 m_ColsInfo = NULL;
122 m_NumCols = m_NumRows = 0;
123 m_CellInfo = NULL;
124 m_ActualCol = m_ActualRow = -1;
125
126 /* scan params: */
127 m_tBkg = m_rBkg = -1;
128 if (tag.HasParam(wxT("BGCOLOR"))) tag.ScanParam(wxT("BGCOLOR"), wxT("#%lX"), &m_tBkg);
129 if (tag.HasParam(wxT("VALIGN"))) m_tValign = tag.GetParam(wxT("VALIGN")); else m_tValign = wxEmptyString;
130 if (tag.HasParam(wxT("CELLSPACING")) && tag.ScanParam(wxT("CELLSPACING"), wxT("%i"), &m_Spacing) == 1) {} else m_Spacing = 2;
131 if (tag.HasParam(wxT("CELLPADDING")) && tag.ScanParam(wxT("CELLPADDING"), wxT("%i"), &m_Padding) == 1) {} else m_Padding = 3;
132 m_Spacing = (int)(m_PixelScale * (double)m_Spacing);
133 m_Padding = (int)(m_PixelScale * (double)m_Padding);
134
135 if (m_HasBorders)
136 SetBorder(TABLE_BORDER_CLR_1, TABLE_BORDER_CLR_2);
137 }
138
139
140
141 wxHtmlTableCell::~wxHtmlTableCell()
142 {
143 if (m_ColsInfo) free(m_ColsInfo);
144 if (m_CellInfo)
145 {
146 for (int i = 0; i < m_NumRows; i++)
147 free(m_CellInfo[i]);
148 free(m_CellInfo);
149 }
150 }
151
152
153
154 void wxHtmlTableCell::ReallocCols(int cols)
155 {
156 int i,j;
157
158 for (i = 0; i < m_NumRows; i++)
159 {
160 m_CellInfo[i] = (cellStruct*) realloc(m_CellInfo[i], sizeof(cellStruct) * cols);
161 for (j = m_NumCols; j < cols; j++)
162 m_CellInfo[i][j].flag = cellFree;
163 }
164
165 m_ColsInfo = (colStruct*) realloc(m_ColsInfo, sizeof(colStruct) * cols);
166 for (j = m_NumCols; j < cols; j++)
167 {
168 m_ColsInfo[j].width = 0;
169 m_ColsInfo[j].units = wxHTML_UNITS_PERCENT;
170 }
171
172 m_NumCols = cols;
173 }
174
175
176
177 void wxHtmlTableCell::ReallocRows(int rows)
178 {
179 m_CellInfo = (cellStruct**) realloc(m_CellInfo, sizeof(cellStruct*) * rows);
180 for (int row = m_NumRows; row < rows ; row++)
181 {
182 if (m_NumCols == 0)
183 m_CellInfo[row] = NULL;
184 else
185 {
186 m_CellInfo[row] = (cellStruct*) malloc(sizeof(cellStruct) * m_NumCols);
187 for (int col = 0; col < m_NumCols; col++)
188 m_CellInfo[row][col].flag = cellFree;
189 }
190 }
191 m_NumRows = rows;
192 }
193
194
195 void wxHtmlTableCell::AddRow(const wxHtmlTag& tag)
196 {
197 m_ActualCol = -1;
198 // VS: real allocation of row entry is done in AddCell in order
199 // to correctly handle empty rows (i.e. "<tr></tr>")
200 // m_ActualCol == -1 indicates that AddCell has to allocate new row.
201
202 // scan params:
203 m_rBkg = m_tBkg;
204 if (tag.HasParam(wxT("BGCOLOR")))
205 tag.ScanParam(wxT("BGCOLOR"), wxT("#%lX"), &m_rBkg);
206 if (tag.HasParam(wxT("VALIGN")))
207 m_rValign = tag.GetParam(wxT("VALIGN"));
208 else
209 m_rValign = m_tValign;
210 }
211
212
213
214 void wxHtmlTableCell::AddCell(wxHtmlContainerCell *cell, const wxHtmlTag& tag)
215 {
216 // Is this cell in new row?
217 // VS: we can't do it in AddRow, see my comment there
218 if (m_ActualCol == -1)
219 {
220 if (m_ActualRow + 1 > m_NumRows - 1)
221 ReallocRows(m_ActualRow + 2);
222 m_ActualRow++;
223 }
224
225 // cells & columns:
226 do
227 {
228 m_ActualCol++;
229 } while ((m_ActualCol < m_NumCols) && (m_CellInfo[m_ActualRow][m_ActualCol].flag != cellFree));
230
231 if (m_ActualCol > m_NumCols - 1)
232 ReallocCols(m_ActualCol + 1);
233
234 int r = m_ActualRow, c = m_ActualCol;
235
236 m_CellInfo[r][c].cont = cell;
237 m_CellInfo[r][c].colspan = 1;
238 m_CellInfo[r][c].rowspan = 1;
239 m_CellInfo[r][c].flag = cellUsed;
240 m_CellInfo[r][c].minheight = 0;
241 m_CellInfo[r][c].valign = wxHTML_ALIGN_TOP;
242
243 /* scan for parameters: */
244
245 // width:
246 {
247 if (tag.HasParam("WIDTH"))
248 {
249 wxString wd = tag.GetParam("WIDTH");
250
251 if (wd[wd.Length()-1] == '%')
252 {
253 wxSscanf(wd.c_str(), wxT("%i%%"), &m_ColsInfo[c].width);
254 m_ColsInfo[c].units = wxHTML_UNITS_PERCENT;
255 }
256 else
257 {
258 wxSscanf(wd.c_str(), wxT("%i"), &m_ColsInfo[c].width);
259 m_ColsInfo[c].width = (int)(m_PixelScale * (double)m_ColsInfo[c].width);
260 m_ColsInfo[c].units = wxHTML_UNITS_PIXELS;
261 }
262 }
263 }
264
265
266 // spanning:
267 {
268 if (tag.HasParam(wxT("COLSPAN"))) tag.ScanParam(wxT("COLSPAN"), wxT("%i"), &m_CellInfo[r][c].colspan);
269 if (tag.HasParam(wxT("ROWSPAN"))) tag.ScanParam(wxT("ROWSPAN"), wxT("%i"), &m_CellInfo[r][c].rowspan);
270 if ((m_CellInfo[r][c].colspan != 1) || (m_CellInfo[r][c].rowspan != 1))
271 {
272 int i, j;
273
274 if (r + m_CellInfo[r][c].rowspan > m_NumRows) ReallocRows(r + m_CellInfo[r][c].rowspan);
275 if (c + m_CellInfo[r][c].colspan > m_NumCols) ReallocCols(c + m_CellInfo[r][c].colspan);
276 for (i = r; i < r + m_CellInfo[r][c].rowspan; i++)
277 for (j = c; j < c + m_CellInfo[r][c].colspan; j++)
278 m_CellInfo[i][j].flag = cellSpan;
279 m_CellInfo[r][c].flag = cellUsed;
280 }
281 }
282
283 //background color:
284 {
285 int bk = m_rBkg;
286 if (tag.HasParam(wxT("BGCOLOR"))) tag.ScanParam(wxT("BGCOLOR"), wxT("#%lX"), &bk);
287 if (bk != -1)
288 {
289 wxColour clr = wxColour((bk & 0xFF0000) >> 16 , (bk & 0x00FF00) >> 8, (bk & 0x0000FF));
290 cell->SetBackgroundColour(clr);
291 }
292 }
293 if (m_HasBorders)
294 cell->SetBorder(TABLE_BORDER_CLR_2, TABLE_BORDER_CLR_1);
295
296 // vertical alignment:
297 {
298 wxString valign;
299 if (tag.HasParam(wxT("VALIGN"))) valign = tag.GetParam(wxT("VALIGN")); else valign = m_tValign;
300 valign.MakeUpper();
301 if (valign == wxT("TOP")) m_CellInfo[r][c].valign = wxHTML_ALIGN_TOP;
302 else if (valign == wxT("BOTTOM")) m_CellInfo[r][c].valign = wxHTML_ALIGN_BOTTOM;
303 else m_CellInfo[r][c].valign = wxHTML_ALIGN_CENTER;
304 }
305
306 cell->SetIndent(m_Padding, wxHTML_INDENT_ALL, wxHTML_UNITS_PIXELS);
307 }
308
309
310
311
312
313 void wxHtmlTableCell::Layout(int w)
314 {
315 /*
316
317 WIDTH ADJUSTING :
318
319 */
320
321 if (m_WidthFloatUnits == wxHTML_UNITS_PERCENT)
322 {
323 if (m_WidthFloat < 0) m_Width = (100 + m_WidthFloat) * w / 100;
324 else m_Width = m_WidthFloat * w / 100;
325 }
326 else
327 {
328 if (m_WidthFloat < 0) m_Width = w + m_WidthFloat;
329 else m_Width = m_WidthFloat;
330 }
331
332
333 /*
334
335 LAYOUTING :
336
337 */
338
339 /* 1. setup columns widths: */
340 {
341 int wpix = m_Width - (m_NumCols + 1) * m_Spacing;
342 int i, j;
343 int wtemp = 0;
344
345 // 1a. setup fixed-width columns:
346 for (i = 0; i < m_NumCols; i++)
347 if (m_ColsInfo[i].units == wxHTML_UNITS_PIXELS)
348 wpix -= (m_ColsInfo[i].pixwidth = m_ColsInfo[i].width);
349
350 // 1b. setup floating-width columns:
351 for (i = 0; i < m_NumCols; i++)
352 if ((m_ColsInfo[i].units == wxHTML_UNITS_PERCENT) && (m_ColsInfo[i].width != 0))
353 wtemp += (m_ColsInfo[i].pixwidth = m_ColsInfo[i].width * wpix / 100);
354 wpix -= wtemp;
355
356 // 1c. setup defalut columns (no width specification supplied):
357 // NOTE! This algorithm doesn't conform to HTML standard : it assigns equal widths
358 // instead of optimal
359 for (i = j = 0; i < m_NumCols; i++)
360 if (m_ColsInfo[i].width == 0) j++;
361 for (i = 0; i < m_NumCols; i++)
362 if (m_ColsInfo[i].width == 0)
363 m_ColsInfo[i].pixwidth = wpix / j;
364 }
365
366 /* 2. compute positions of columns: */
367 {
368 int wpos = m_Spacing;
369 for (int i = 0; i < m_NumCols; i++)
370 {
371 m_ColsInfo[i].leftpos = wpos;
372 wpos += m_ColsInfo[i].pixwidth + m_Spacing;
373 }
374 }
375
376 /* 3. sub-layout all cells: */
377 {
378 int *ypos = new int[m_NumRows + 1];
379
380 int actcol, actrow;
381 int fullwid;
382 wxHtmlContainerCell *actcell;
383
384 ypos[0] = m_Spacing;
385 for (actrow = 1; actrow <= m_NumRows; actrow++) ypos[actrow] = -1;
386 for (actrow = 0; actrow < m_NumRows; actrow++)
387 {
388 if (ypos[actrow] == -1) ypos[actrow] = ypos[actrow-1];
389 // 3a. sub-layout and detect max height:
390
391 for (actcol = 0; actcol < m_NumCols; actcol++) {
392 if (m_CellInfo[actrow][actcol].flag != cellUsed) continue;
393 actcell = m_CellInfo[actrow][actcol].cont;
394 fullwid = 0;
395 for (int i = actcol; i < m_CellInfo[actrow][actcol].colspan + actcol; i++)
396 fullwid += m_ColsInfo[i].pixwidth;
397 fullwid += (m_CellInfo[actrow][actcol].colspan - 1) * m_Spacing;
398 actcell->SetMinHeight(m_CellInfo[actrow][actcol].minheight, m_CellInfo[actrow][actcol].valign);
399 actcell->Layout(fullwid);
400
401 if (ypos[actrow] + actcell->GetHeight() + m_CellInfo[actrow][actcol].rowspan * m_Spacing > ypos[actrow + m_CellInfo[actrow][actcol].rowspan])
402 ypos[actrow + m_CellInfo[actrow][actcol].rowspan] =
403 ypos[actrow] + actcell->GetHeight() + m_CellInfo[actrow][actcol].rowspan * m_Spacing;
404 }
405 }
406
407 for (actrow = 0; actrow < m_NumRows; actrow++)
408 {
409 // 3b. place cells in row & let'em all have same height:
410
411 for (actcol = 0; actcol < m_NumCols; actcol++)
412 {
413 if (m_CellInfo[actrow][actcol].flag != cellUsed) continue;
414 actcell = m_CellInfo[actrow][actcol].cont;
415 actcell->SetMinHeight(
416 ypos[actrow + m_CellInfo[actrow][actcol].rowspan] - ypos[actrow] - m_Spacing,
417 m_CellInfo[actrow][actcol].valign);
418 fullwid = 0;
419 for (int i = actcol; i < m_CellInfo[actrow][actcol].colspan + actcol; i++)
420 fullwid += m_ColsInfo[i].pixwidth;
421 fullwid += (m_CellInfo[actrow][actcol].colspan - 1) * m_Spacing;
422 actcell->Layout(fullwid);
423 actcell->SetPos(m_ColsInfo[actcol].leftpos, ypos[actrow]);
424 }
425 }
426 m_Height = ypos[m_NumRows];
427 delete[] ypos;
428 }
429 }
430
431
432
433
434
435
436 //-----------------------------------------------------------------------------
437 // The tables handler:
438 //-----------------------------------------------------------------------------
439
440
441 TAG_HANDLER_BEGIN(TABLE, "TABLE,TR,TD,TH")
442
443 TAG_HANDLER_VARS
444 wxHtmlTableCell* m_Table;
445 wxString m_tAlign, m_rAlign;
446 int m_OldAlign;
447
448 TAG_HANDLER_CONSTR(TABLE)
449 {
450 m_Table = NULL;
451 m_tAlign = m_rAlign = wxEmptyString;
452 m_OldAlign = wxHTML_ALIGN_LEFT;
453 }
454
455
456 TAG_HANDLER_PROC(tag)
457 {
458 wxHtmlContainerCell *c;
459
460 // new table started, backup upper-level table (if any) and create new:
461 if (tag.GetName() == wxT("TABLE"))
462 {
463 wxHtmlTableCell *oldt = m_Table;
464 wxHtmlContainerCell *oldcont;
465 int m_OldAlign;
466
467 oldcont = c = m_WParser->OpenContainer();
468
469 c->SetWidthFloat(tag, m_WParser->GetPixelScale());
470 m_Table = new wxHtmlTableCell(c, tag, m_WParser->GetPixelScale());
471 m_OldAlign = m_WParser->GetAlign();
472 m_tAlign = wxEmptyString;
473 if (tag.HasParam(wxT("ALIGN"))) m_tAlign = tag.GetParam(wxT("ALIGN"));
474
475 ParseInner(tag);
476
477 m_WParser->SetAlign(m_OldAlign);
478 m_WParser->SetContainer(oldcont);
479 m_WParser->CloseContainer();
480 m_Table = oldt;
481 return TRUE;
482 }
483
484
485 else if (m_Table && !tag.IsEnding())
486 {
487 // new row in table
488 if (tag.GetName() == wxT("TR"))
489 {
490 m_Table->AddRow(tag);
491 m_rAlign = m_tAlign;
492 if (tag.HasParam(wxT("ALIGN"))) m_rAlign = tag.GetParam(wxT("ALIGN"));
493 }
494
495 // new cell
496 else
497 {
498 m_WParser->SetAlign(m_OldAlign);
499 c = m_WParser->SetContainer(new wxHtmlContainerCell(m_Table));
500 m_Table->AddCell(c, tag);
501
502 m_WParser->OpenContainer();
503
504 if (tag.GetName() == wxT("TH")) /*header style*/
505 {
506 m_WParser->SetAlign(wxHTML_ALIGN_CENTER);
507 }
508
509 {
510 wxString als;
511
512 als = m_rAlign;
513 if (tag.HasParam(wxT("ALIGN"))) als = tag.GetParam(wxT("ALIGN"));
514 als.MakeUpper();
515 if (als == wxT("RIGHT")) m_WParser->SetAlign(wxHTML_ALIGN_RIGHT);
516 else if (als == wxT("CENTER")) m_WParser->SetAlign(wxHTML_ALIGN_CENTER);
517 }
518 m_WParser->OpenContainer();
519 }
520 }
521 return FALSE;
522 }
523
524 TAG_HANDLER_END(TABLE)
525
526
527
528
529
530 TAGS_MODULE_BEGIN(Tables)
531
532 TAGS_MODULE_ADD(TABLE)
533
534 TAGS_MODULE_END(Tables)
535
536
537 #endif