]>
git.saurik.com Git - wxWidgets.git/blob - src/html/m_tables.cpp
1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxHtml module for tables
4 // Author: Vaclav Slavik
6 // Copyright: (c) 1999 Vaclav Slavik
7 // Licence: wxWindows Licence
8 /////////////////////////////////////////////////////////////////////////////
11 #pragma implementation
14 #include "wx/wxprec.h"
17 #if wxUSE_HTML && wxUSE_STREAMS
28 1. This version of m_tables doesn't support auto-layout algorithm.
29 This means that all columns are of same width unless explicitly specified.
33 #include "wx/html/forcelnk.h"
34 #include "wx/html/m_templ.h"
36 #include "wx/html/htmlcell.h"
38 FORCE_LINK_ME(m_tables
)
41 #define TABLE_BORDER_CLR_1 wxColour(0xC5, 0xC2, 0xC5)
42 #define TABLE_BORDER_CLR_2 wxColour(0x62, 0x61, 0x62)
45 //-----------------------------------------------------------------------------
47 //-----------------------------------------------------------------------------
53 int leftpos
, pixwidth
, maxrealwidth
;
54 // temporary (depends on width of table)
64 wxHtmlContainerCell
*cont
;
66 int minheight
, valign
;
71 class wxHtmlTableCell
: public wxHtmlContainerCell
74 /* These are real attributes: */
76 // should we draw borders or not?
77 int m_NumCols
, m_NumRows
;
78 // number of columns; rows
79 colStruct
*m_ColsInfo
;
80 // array of column information
81 cellStruct
**m_CellInfo
;
82 // 2D array of all cells in the table : m_CellInfo[row][column]
84 // spaces between cells
86 // cells internal indentation
89 /* ...and these are valid only during parsing of table: */
90 int m_ActualCol
, m_ActualRow
;
91 // number of actual column (ranging from 0..m_NumCols)
93 // default values (for table and row):
94 wxColour m_tBkg
, m_rBkg
;
95 wxString m_tValign
, m_rValign
;
101 wxHtmlTableCell(wxHtmlContainerCell
*parent
, const wxHtmlTag
& tag
, double pixel_scale
= 1.0);
103 virtual void Layout(int w
);
105 void AddRow(const wxHtmlTag
& tag
);
106 void AddCell(wxHtmlContainerCell
*cell
, const wxHtmlTag
& tag
);
108 void ReallocCols(int cols
);
109 void ReallocRows(int rows
);
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!!
117 wxHtmlTableCell::wxHtmlTableCell(wxHtmlContainerCell
*parent
, const wxHtmlTag
& tag
, double pixel_scale
)
118 : wxHtmlContainerCell(parent
)
120 m_PixelScale
= pixel_scale
;
121 m_HasBorders
= (tag
.GetParam(wxT("BORDER")) != wxT("0"));
123 m_NumCols
= m_NumRows
= 0;
125 m_ActualCol
= m_ActualRow
= -1;
128 if (tag
.HasParam(wxT("BGCOLOR")))
129 tag
.GetParamAsColour(wxT("BGCOLOR"), &m_tBkg
);
130 if (tag
.HasParam(wxT("VALIGN")))
131 m_tValign
= tag
.GetParam(wxT("VALIGN"));
133 m_tValign
= wxEmptyString
;
134 if (!tag
.GetParamAsInt(wxT("CELLSPACING"), &m_Spacing
))
136 if (!tag
.GetParamAsInt(wxT("CELLPADDING"), &m_Padding
))
138 m_Spacing
= (int)(m_PixelScale
* (double)m_Spacing
);
139 m_Padding
= (int)(m_PixelScale
* (double)m_Padding
);
142 SetBorder(TABLE_BORDER_CLR_1
, TABLE_BORDER_CLR_2
);
147 wxHtmlTableCell::~wxHtmlTableCell()
149 if (m_ColsInfo
) free(m_ColsInfo
);
152 for (int i
= 0; i
< m_NumRows
; i
++)
160 void wxHtmlTableCell::ReallocCols(int cols
)
164 for (i
= 0; i
< m_NumRows
; i
++)
166 m_CellInfo
[i
] = (cellStruct
*) realloc(m_CellInfo
[i
], sizeof(cellStruct
) * cols
);
167 for (j
= m_NumCols
; j
< cols
; j
++)
168 m_CellInfo
[i
][j
].flag
= cellFree
;
171 m_ColsInfo
= (colStruct
*) realloc(m_ColsInfo
, sizeof(colStruct
) * cols
);
172 for (j
= m_NumCols
; j
< cols
; j
++)
174 m_ColsInfo
[j
].width
= 0;
175 m_ColsInfo
[j
].units
= wxHTML_UNITS_PERCENT
;
183 void wxHtmlTableCell::ReallocRows(int rows
)
185 m_CellInfo
= (cellStruct
**) realloc(m_CellInfo
, sizeof(cellStruct
*) * rows
);
186 for (int row
= m_NumRows
; row
< rows
; row
++)
189 m_CellInfo
[row
] = NULL
;
192 m_CellInfo
[row
] = (cellStruct
*) malloc(sizeof(cellStruct
) * m_NumCols
);
193 for (int col
= 0; col
< m_NumCols
; col
++)
194 m_CellInfo
[row
][col
].flag
= cellFree
;
201 void wxHtmlTableCell::AddRow(const wxHtmlTag
& tag
)
204 // VS: real allocation of row entry is done in AddCell in order
205 // to correctly handle empty rows (i.e. "<tr></tr>")
206 // m_ActualCol == -1 indicates that AddCell has to allocate new row.
210 if (tag
.HasParam(wxT("BGCOLOR")))
211 tag
.GetParamAsColour(wxT("BGCOLOR"), &m_rBkg
);
212 if (tag
.HasParam(wxT("VALIGN")))
213 m_rValign
= tag
.GetParam(wxT("VALIGN"));
215 m_rValign
= m_tValign
;
220 void wxHtmlTableCell::AddCell(wxHtmlContainerCell
*cell
, const wxHtmlTag
& tag
)
222 // Is this cell in new row?
223 // VS: we can't do it in AddRow, see my comment there
224 if (m_ActualCol
== -1)
226 if (m_ActualRow
+ 1 > m_NumRows
- 1)
227 ReallocRows(m_ActualRow
+ 2);
235 } while ((m_ActualCol
< m_NumCols
) &&
236 (m_CellInfo
[m_ActualRow
][m_ActualCol
].flag
!= cellFree
));
238 if (m_ActualCol
> m_NumCols
- 1)
239 ReallocCols(m_ActualCol
+ 1);
241 int r
= m_ActualRow
, c
= m_ActualCol
;
243 m_CellInfo
[r
][c
].cont
= cell
;
244 m_CellInfo
[r
][c
].colspan
= 1;
245 m_CellInfo
[r
][c
].rowspan
= 1;
246 m_CellInfo
[r
][c
].flag
= cellUsed
;
247 m_CellInfo
[r
][c
].minheight
= 0;
248 m_CellInfo
[r
][c
].valign
= wxHTML_ALIGN_TOP
;
250 /* scan for parameters: */
254 if (tag
.HasParam(wxT("WIDTH")))
256 wxString wd
= tag
.GetParam(wxT("WIDTH"));
258 if (wd
[wd
.Length()-1] == wxT('%'))
260 wxSscanf(wd
.c_str(), wxT("%i%%"), &m_ColsInfo
[c
].width
);
261 m_ColsInfo
[c
].units
= wxHTML_UNITS_PERCENT
;
265 wxSscanf(wd
.c_str(), wxT("%i"), &m_ColsInfo
[c
].width
);
266 m_ColsInfo
[c
].width
= (int)(m_PixelScale
* (double)m_ColsInfo
[c
].width
);
267 m_ColsInfo
[c
].units
= wxHTML_UNITS_PIXELS
;
275 tag
.GetParamAsInt(wxT("COLSPAN"), &m_CellInfo
[r
][c
].colspan
);
276 tag
.GetParamAsInt(wxT("ROWSPAN"), &m_CellInfo
[r
][c
].rowspan
);
277 if ((m_CellInfo
[r
][c
].colspan
!= 1) || (m_CellInfo
[r
][c
].rowspan
!= 1))
281 if (r
+ m_CellInfo
[r
][c
].rowspan
> m_NumRows
)
282 ReallocRows(r
+ m_CellInfo
[r
][c
].rowspan
);
283 if (c
+ m_CellInfo
[r
][c
].colspan
> m_NumCols
)
284 ReallocCols(c
+ m_CellInfo
[r
][c
].colspan
);
285 for (i
= r
; i
< r
+ m_CellInfo
[r
][c
].rowspan
; i
++)
286 for (j
= c
; j
< c
+ m_CellInfo
[r
][c
].colspan
; j
++)
287 m_CellInfo
[i
][j
].flag
= cellSpan
;
288 m_CellInfo
[r
][c
].flag
= cellUsed
;
294 wxColour bk
= m_rBkg
;
295 if (tag
.HasParam(wxT("BGCOLOR")))
296 tag
.GetParamAsColour(wxT("BGCOLOR"), &bk
);
298 cell
->SetBackgroundColour(bk
);
301 cell
->SetBorder(TABLE_BORDER_CLR_2
, TABLE_BORDER_CLR_1
);
303 // vertical alignment:
306 if (tag
.HasParam(wxT("VALIGN")))
307 valign
= tag
.GetParam(wxT("VALIGN"));
311 if (valign
== wxT("TOP"))
312 m_CellInfo
[r
][c
].valign
= wxHTML_ALIGN_TOP
;
313 else if (valign
== wxT("BOTTOM"))
314 m_CellInfo
[r
][c
].valign
= wxHTML_ALIGN_BOTTOM
;
315 else m_CellInfo
[r
][c
].valign
= wxHTML_ALIGN_CENTER
;
318 cell
->SetIndent(m_Padding
, wxHTML_INDENT_ALL
, wxHTML_UNITS_PIXELS
);
325 void wxHtmlTableCell::Layout(int w
)
333 if (m_WidthFloatUnits
== wxHTML_UNITS_PERCENT
)
335 if (m_WidthFloat
< 0) m_Width
= (100 + m_WidthFloat
) * w
/ 100;
336 else m_Width
= m_WidthFloat
* w
/ 100;
340 if (m_WidthFloat
< 0) m_Width
= w
+ m_WidthFloat
;
341 else m_Width
= m_WidthFloat
;
351 /* 1. setup columns widths: */
353 int wpix
= m_Width
- (m_NumCols
+ 1) * m_Spacing
;
357 // 1a. setup fixed-width columns:
358 for (i
= 0; i
< m_NumCols
; i
++)
359 if (m_ColsInfo
[i
].units
== wxHTML_UNITS_PIXELS
)
360 wpix
-= (m_ColsInfo
[i
].pixwidth
= m_ColsInfo
[i
].width
);
362 // 1b. setup floating-width columns:
363 for (i
= 0; i
< m_NumCols
; i
++)
364 if ((m_ColsInfo
[i
].units
== wxHTML_UNITS_PERCENT
) && (m_ColsInfo
[i
].width
!= 0))
365 wtemp
+= (m_ColsInfo
[i
].pixwidth
= m_ColsInfo
[i
].width
* wpix
/ 100);
368 // 1c. setup defalut columns (no width specification supplied):
369 // NOTE! This algorithm doesn't conform to HTML standard : it assigns equal widths
370 // instead of optimal
371 for (i
= j
= 0; i
< m_NumCols
; i
++)
372 if (m_ColsInfo
[i
].width
== 0) j
++;
373 for (i
= 0; i
< m_NumCols
; i
++)
374 if (m_ColsInfo
[i
].width
== 0)
375 m_ColsInfo
[i
].pixwidth
= wpix
/ j
;
378 /* 2. compute positions of columns: */
380 int wpos
= m_Spacing
;
381 for (int i
= 0; i
< m_NumCols
; i
++)
383 m_ColsInfo
[i
].leftpos
= wpos
;
384 wpos
+= m_ColsInfo
[i
].pixwidth
+ m_Spacing
;
388 /* 3. sub-layout all cells: */
390 int *ypos
= new int[m_NumRows
+ 1];
394 wxHtmlContainerCell
*actcell
;
397 for (actrow
= 1; actrow
<= m_NumRows
; actrow
++) ypos
[actrow
] = -1;
398 for (actrow
= 0; actrow
< m_NumRows
; actrow
++)
400 if (ypos
[actrow
] == -1) ypos
[actrow
] = ypos
[actrow
-1];
401 // 3a. sub-layout and detect max height:
403 for (actcol
= 0; actcol
< m_NumCols
; actcol
++) {
404 if (m_CellInfo
[actrow
][actcol
].flag
!= cellUsed
) continue;
405 actcell
= m_CellInfo
[actrow
][actcol
].cont
;
407 for (int i
= actcol
; i
< m_CellInfo
[actrow
][actcol
].colspan
+ actcol
; i
++)
408 fullwid
+= m_ColsInfo
[i
].pixwidth
;
409 fullwid
+= (m_CellInfo
[actrow
][actcol
].colspan
- 1) * m_Spacing
;
410 actcell
->SetMinHeight(m_CellInfo
[actrow
][actcol
].minheight
, m_CellInfo
[actrow
][actcol
].valign
);
411 actcell
->Layout(fullwid
);
413 if (ypos
[actrow
] + actcell
->GetHeight() + m_CellInfo
[actrow
][actcol
].rowspan
* m_Spacing
> ypos
[actrow
+ m_CellInfo
[actrow
][actcol
].rowspan
])
414 ypos
[actrow
+ m_CellInfo
[actrow
][actcol
].rowspan
] =
415 ypos
[actrow
] + actcell
->GetHeight() + m_CellInfo
[actrow
][actcol
].rowspan
* m_Spacing
;
419 for (actrow
= 0; actrow
< m_NumRows
; actrow
++)
421 // 3b. place cells in row & let'em all have same height:
423 for (actcol
= 0; actcol
< m_NumCols
; actcol
++)
425 if (m_CellInfo
[actrow
][actcol
].flag
!= cellUsed
) continue;
426 actcell
= m_CellInfo
[actrow
][actcol
].cont
;
427 actcell
->SetMinHeight(
428 ypos
[actrow
+ m_CellInfo
[actrow
][actcol
].rowspan
] - ypos
[actrow
] - m_Spacing
,
429 m_CellInfo
[actrow
][actcol
].valign
);
431 for (int i
= actcol
; i
< m_CellInfo
[actrow
][actcol
].colspan
+ actcol
; i
++)
432 fullwid
+= m_ColsInfo
[i
].pixwidth
;
433 fullwid
+= (m_CellInfo
[actrow
][actcol
].colspan
- 1) * m_Spacing
;
434 actcell
->Layout(fullwid
);
435 actcell
->SetPos(m_ColsInfo
[actcol
].leftpos
, ypos
[actrow
]);
438 m_Height
= ypos
[m_NumRows
];
448 //-----------------------------------------------------------------------------
449 // The tables handler:
450 //-----------------------------------------------------------------------------
453 TAG_HANDLER_BEGIN(TABLE
, "TABLE,TR,TD,TH")
456 wxHtmlTableCell
* m_Table
;
457 wxString m_tAlign
, m_rAlign
;
460 TAG_HANDLER_CONSTR(TABLE
)
463 m_tAlign
= m_rAlign
= wxEmptyString
;
464 m_OldAlign
= wxHTML_ALIGN_LEFT
;
468 TAG_HANDLER_PROC(tag
)
470 wxHtmlContainerCell
*c
;
472 // new table started, backup upper-level table (if any) and create new:
473 if (tag
.GetName() == wxT("TABLE"))
475 wxHtmlTableCell
*oldt
= m_Table
;
476 wxHtmlContainerCell
*oldcont
;
479 oldcont
= c
= m_WParser
->OpenContainer();
481 c
->SetWidthFloat(tag
, m_WParser
->GetPixelScale());
482 m_Table
= new wxHtmlTableCell(c
, tag
, m_WParser
->GetPixelScale());
483 m_OldAlign
= m_WParser
->GetAlign();
484 m_tAlign
= wxEmptyString
;
485 if (tag
.HasParam(wxT("ALIGN")))
486 m_tAlign
= tag
.GetParam(wxT("ALIGN"));
490 m_WParser
->SetAlign(m_OldAlign
);
491 m_WParser
->SetContainer(oldcont
);
492 m_WParser
->CloseContainer();
498 else if (m_Table
&& !tag
.IsEnding())
501 if (tag
.GetName() == wxT("TR"))
503 m_Table
->AddRow(tag
);
505 if (tag
.HasParam(wxT("ALIGN")))
506 m_rAlign
= tag
.GetParam(wxT("ALIGN"));
512 m_WParser
->SetAlign(m_OldAlign
);
513 c
= m_WParser
->SetContainer(new wxHtmlContainerCell(m_Table
));
514 m_Table
->AddCell(c
, tag
);
516 m_WParser
->OpenContainer();
518 if (tag
.GetName() == wxT("TH")) /*header style*/
520 m_WParser
->SetAlign(wxHTML_ALIGN_CENTER
);
527 if (tag
.HasParam(wxT("ALIGN")))
528 als
= tag
.GetParam(wxT("ALIGN"));
530 if (als
== wxT("RIGHT"))
531 m_WParser
->SetAlign(wxHTML_ALIGN_RIGHT
);
532 else if (als
== wxT("CENTER"))
533 m_WParser
->SetAlign(wxHTML_ALIGN_CENTER
);
535 m_WParser
->OpenContainer();
541 TAG_HANDLER_END(TABLE
)
547 TAGS_MODULE_BEGIN(Tables
)
549 TAGS_MODULE_ADD(TABLE
)
551 TAGS_MODULE_END(Tables
)