]>
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
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.
34 #include "wx/html/forcelnk.h"
35 #include "wx/html/m_templ.h"
37 #include "wx/html/htmlcell.h"
39 FORCE_LINK_ME(m_tables
)
42 #define TABLE_BORDER_CLR_1 wxColour(0xC5, 0xC2, 0xC5)
43 #define TABLE_BORDER_CLR_2 wxColour(0x62, 0x61, 0x62)
46 //-----------------------------------------------------------------------------
48 //-----------------------------------------------------------------------------
52 int width
, units
; // universal
53 int leftpos
, pixwidth
, maxrealwidth
; // temporary (depends on width of table)
63 wxHtmlContainerCell
*cont
;
65 int minheight
, valign
;
70 class wxHtmlTableCell
: public wxHtmlContainerCell
73 /* These are real attributes: */
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]
83 // spaces between cells
85 // cells internal indentation
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)
92 // default values (for table and row):
94 wxString m_tValign
, m_rValign
;
100 wxHtmlTableCell(wxHtmlContainerCell
*parent
, const wxHtmlTag
& tag
, double pixel_scale
= 1.0);
102 virtual void Layout(int w
);
104 void AddRow(const wxHtmlTag
& tag
);
105 void AddCell(wxHtmlContainerCell
*cell
, const wxHtmlTag
& tag
);
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!!
116 wxHtmlTableCell::wxHtmlTableCell(wxHtmlContainerCell
*parent
, const wxHtmlTag
& tag
, double pixel_scale
)
117 : wxHtmlContainerCell(parent
)
119 m_PixelScale
= pixel_scale
;
120 m_HasBorders
= (tag
.HasParam(wxT("BORDER")) && tag
.GetParam(wxT("BORDER")) != wxT("0"));
122 m_NumCols
= m_NumRows
= 0;
124 m_ActualCol
= m_ActualRow
= -1;
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
);
136 SetBorder(TABLE_BORDER_CLR_1
, TABLE_BORDER_CLR_2
);
141 wxHtmlTableCell::~wxHtmlTableCell()
143 if (m_ColsInfo
) free(m_ColsInfo
);
146 for (int i
= 0; i
< m_NumRows
; i
++)
154 void wxHtmlTableCell::ReallocCols(int cols
)
158 for (i
= 0; i
< m_NumRows
; i
++)
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
;
165 m_ColsInfo
= (colStruct
*) realloc(m_ColsInfo
, sizeof(colStruct
) * cols
);
166 for (j
= m_NumCols
; j
< cols
; j
++)
168 m_ColsInfo
[j
].width
= 0;
169 m_ColsInfo
[j
].units
= wxHTML_UNITS_PERCENT
;
177 void wxHtmlTableCell::ReallocRows(int rows
)
179 m_CellInfo
= (cellStruct
**) realloc(m_CellInfo
, sizeof(cellStruct
*) * rows
);
180 for (int row
= m_NumRows
; row
< rows
; row
++)
183 m_CellInfo
[row
] = NULL
;
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
;
195 void wxHtmlTableCell::AddRow(const wxHtmlTag
& tag
)
197 if (m_ActualRow
+ 1 > m_NumRows
- 1)
198 ReallocRows(m_ActualRow
+ 2);
204 if (tag
.HasParam(wxT("BGCOLOR"))) tag
.ScanParam(wxT("BGCOLOR"), wxT("#%lX"), &m_rBkg
);
205 if (tag
.HasParam(wxT("VALIGN"))) m_rValign
= tag
.GetParam(wxT("VALIGN")); else m_rValign
= m_tValign
;
210 void wxHtmlTableCell::AddCell(wxHtmlContainerCell
*cell
, const wxHtmlTag
& tag
)
215 } while ((m_ActualCol
< m_NumCols
) && (m_CellInfo
[m_ActualRow
][m_ActualCol
].flag
!= cellFree
));
217 if (m_ActualCol
> m_NumCols
- 1)
218 ReallocCols(m_ActualCol
+ 1);
220 int r
= m_ActualRow
, c
= m_ActualCol
;
222 m_CellInfo
[r
][c
].cont
= cell
;
223 m_CellInfo
[r
][c
].colspan
= 1;
224 m_CellInfo
[r
][c
].rowspan
= 1;
225 m_CellInfo
[r
][c
].flag
= cellUsed
;
226 m_CellInfo
[r
][c
].minheight
= 0;
227 m_CellInfo
[r
][c
].valign
= wxHTML_ALIGN_TOP
;
229 /* scan for parameters: */
233 if (tag
.HasParam("WIDTH"))
235 wxString wd
= tag
.GetParam("WIDTH");
237 if (wd
[wd
.Length()-1] == '%')
239 wxSscanf(wd
.c_str(), wxT("%i%%"), &m_ColsInfo
[c
].width
);
240 m_ColsInfo
[c
].units
= wxHTML_UNITS_PERCENT
;
244 wxSscanf(wd
.c_str(), wxT("%i"), &m_ColsInfo
[c
].width
);
245 m_ColsInfo
[c
].width
= (int)(m_PixelScale
* (double)m_ColsInfo
[c
].width
);
246 m_ColsInfo
[c
].units
= wxHTML_UNITS_PIXELS
;
254 if (tag
.HasParam(wxT("COLSPAN"))) tag
.ScanParam(wxT("COLSPAN"), wxT("%i"), &m_CellInfo
[r
][c
].colspan
);
255 if (tag
.HasParam(wxT("ROWSPAN"))) tag
.ScanParam(wxT("ROWSPAN"), wxT("%i"), &m_CellInfo
[r
][c
].rowspan
);
256 if ((m_CellInfo
[r
][c
].colspan
!= 1) || (m_CellInfo
[r
][c
].rowspan
!= 1))
260 if (r
+ m_CellInfo
[r
][c
].rowspan
> m_NumRows
) ReallocRows(r
+ m_CellInfo
[r
][c
].rowspan
);
261 if (c
+ m_CellInfo
[r
][c
].colspan
> m_NumCols
) ReallocCols(c
+ m_CellInfo
[r
][c
].colspan
);
262 for (i
= r
; i
< r
+ m_CellInfo
[r
][c
].rowspan
; i
++)
263 for (j
= c
; j
< c
+ m_CellInfo
[r
][c
].colspan
; j
++)
264 m_CellInfo
[i
][j
].flag
= cellSpan
;
265 m_CellInfo
[r
][c
].flag
= cellUsed
;
272 if (tag
.HasParam(wxT("BGCOLOR"))) tag
.ScanParam(wxT("BGCOLOR"), wxT("#%lX"), &bk
);
275 wxColour clr
= wxColour((bk
& 0xFF0000) >> 16 , (bk
& 0x00FF00) >> 8, (bk
& 0x0000FF));
276 cell
->SetBackgroundColour(clr
);
280 cell
->SetBorder(TABLE_BORDER_CLR_2
, TABLE_BORDER_CLR_1
);
282 // vertical alignment:
285 if (tag
.HasParam(wxT("VALIGN"))) valign
= tag
.GetParam(wxT("VALIGN")); else valign
= m_tValign
;
287 if (valign
== wxT("TOP")) m_CellInfo
[r
][c
].valign
= wxHTML_ALIGN_TOP
;
288 else if (valign
== wxT("BOTTOM")) m_CellInfo
[r
][c
].valign
= wxHTML_ALIGN_BOTTOM
;
289 else m_CellInfo
[r
][c
].valign
= wxHTML_ALIGN_CENTER
;
292 cell
->SetIndent(m_Padding
, wxHTML_INDENT_ALL
, wxHTML_UNITS_PIXELS
);
299 void wxHtmlTableCell::Layout(int w
)
307 if (m_WidthFloatUnits
== wxHTML_UNITS_PERCENT
)
309 if (m_WidthFloat
< 0) m_Width
= (100 + m_WidthFloat
) * w
/ 100;
310 else m_Width
= m_WidthFloat
* w
/ 100;
314 if (m_WidthFloat
< 0) m_Width
= w
+ m_WidthFloat
;
315 else m_Width
= m_WidthFloat
;
325 /* 1. setup columns widths: */
327 int wpix
= m_Width
- (m_NumCols
+ 1) * m_Spacing
;
331 // 1a. setup fixed-width columns:
332 for (i
= 0; i
< m_NumCols
; i
++)
333 if (m_ColsInfo
[i
].units
== wxHTML_UNITS_PIXELS
)
334 wpix
-= (m_ColsInfo
[i
].pixwidth
= m_ColsInfo
[i
].width
);
336 // 1b. setup floating-width columns:
337 for (i
= 0; i
< m_NumCols
; i
++)
338 if ((m_ColsInfo
[i
].units
== wxHTML_UNITS_PERCENT
) && (m_ColsInfo
[i
].width
!= 0))
339 wtemp
+= (m_ColsInfo
[i
].pixwidth
= m_ColsInfo
[i
].width
* wpix
/ 100);
342 // 1c. setup defalut columns (no width specification supplied):
343 // NOTE! This algorithm doesn't conform to HTML standard : it assigns equal widths
344 // instead of optimal
345 for (i
= j
= 0; i
< m_NumCols
; i
++)
346 if (m_ColsInfo
[i
].width
== 0) j
++;
347 for (i
= 0; i
< m_NumCols
; i
++)
348 if (m_ColsInfo
[i
].width
== 0)
349 m_ColsInfo
[i
].pixwidth
= wpix
/ j
;
352 /* 2. compute positions of columns: */
354 int wpos
= m_Spacing
;
355 for (int i
= 0; i
< m_NumCols
; i
++)
357 m_ColsInfo
[i
].leftpos
= wpos
;
358 wpos
+= m_ColsInfo
[i
].pixwidth
+ m_Spacing
;
362 /* 3. sub-layout all cells: */
364 int *ypos
= new int[m_NumRows
+ 1];
368 wxHtmlContainerCell
*actcell
;
371 for (actrow
= 1; actrow
<= m_NumRows
; actrow
++) ypos
[actrow
] = -1;
372 for (actrow
= 0; actrow
< m_NumRows
; actrow
++)
374 if (ypos
[actrow
] == -1) ypos
[actrow
] = ypos
[actrow
-1];
375 // 3a. sub-layout and detect max height:
377 for (actcol
= 0; actcol
< m_NumCols
; actcol
++) {
378 if (m_CellInfo
[actrow
][actcol
].flag
!= cellUsed
) continue;
379 actcell
= m_CellInfo
[actrow
][actcol
].cont
;
381 for (int i
= actcol
; i
< m_CellInfo
[actrow
][actcol
].colspan
+ actcol
; i
++)
382 fullwid
+= m_ColsInfo
[i
].pixwidth
;
383 fullwid
+= (m_CellInfo
[actrow
][actcol
].colspan
- 1) * m_Spacing
;
384 actcell
->SetMinHeight(m_CellInfo
[actrow
][actcol
].minheight
, m_CellInfo
[actrow
][actcol
].valign
);
385 actcell
->Layout(fullwid
);
387 if (ypos
[actrow
] + actcell
->GetHeight() + m_CellInfo
[actrow
][actcol
].rowspan
* m_Spacing
> ypos
[actrow
+ m_CellInfo
[actrow
][actcol
].rowspan
])
388 ypos
[actrow
+ m_CellInfo
[actrow
][actcol
].rowspan
] =
389 ypos
[actrow
] + actcell
->GetHeight() + m_CellInfo
[actrow
][actcol
].rowspan
* m_Spacing
;
393 for (actrow
= 0; actrow
< m_NumRows
; actrow
++)
395 // 3b. place cells in row & let'em all have same height:
397 for (actcol
= 0; actcol
< m_NumCols
; actcol
++)
399 if (m_CellInfo
[actrow
][actcol
].flag
!= cellUsed
) continue;
400 actcell
= m_CellInfo
[actrow
][actcol
].cont
;
401 actcell
->SetMinHeight(
402 ypos
[actrow
+ m_CellInfo
[actrow
][actcol
].rowspan
] - ypos
[actrow
] - m_Spacing
,
403 m_CellInfo
[actrow
][actcol
].valign
);
405 for (int i
= actcol
; i
< m_CellInfo
[actrow
][actcol
].colspan
+ actcol
; i
++)
406 fullwid
+= m_ColsInfo
[i
].pixwidth
;
407 fullwid
+= (m_CellInfo
[actrow
][actcol
].colspan
- 1) * m_Spacing
;
408 actcell
->Layout(fullwid
);
409 actcell
->SetPos(m_ColsInfo
[actcol
].leftpos
, ypos
[actrow
]);
412 m_Height
= ypos
[m_NumRows
];
422 //-----------------------------------------------------------------------------
423 // The tables handler:
424 //-----------------------------------------------------------------------------
427 TAG_HANDLER_BEGIN(TABLE
, "TABLE,TR,TD,TH")
430 wxHtmlTableCell
* m_Table
;
431 wxString m_tAlign
, m_rAlign
;
434 TAG_HANDLER_CONSTR(TABLE
)
437 m_tAlign
= m_rAlign
= wxEmptyString
;
438 m_OldAlign
= wxHTML_ALIGN_LEFT
;
442 TAG_HANDLER_PROC(tag
)
444 wxHtmlContainerCell
*c
;
446 // new table started, backup upper-level table (if any) and create new:
447 if (tag
.GetName() == wxT("TABLE"))
449 wxHtmlTableCell
*oldt
= m_Table
;
450 wxHtmlContainerCell
*oldcont
;
453 oldcont
= c
= m_WParser
->OpenContainer();
455 c
->SetWidthFloat(tag
, m_WParser
->GetPixelScale());
456 m_Table
= new wxHtmlTableCell(c
, tag
, m_WParser
->GetPixelScale());
457 m_OldAlign
= m_WParser
->GetAlign();
458 m_tAlign
= wxEmptyString
;
459 if (tag
.HasParam(wxT("ALIGN"))) m_tAlign
= tag
.GetParam(wxT("ALIGN"));
463 m_WParser
->SetAlign(m_OldAlign
);
464 m_WParser
->SetContainer(oldcont
);
465 m_WParser
->CloseContainer();
471 else if (m_Table
&& !tag
.IsEnding())
474 if (tag
.GetName() == wxT("TR"))
476 m_Table
->AddRow(tag
);
478 if (tag
.HasParam(wxT("ALIGN"))) m_rAlign
= tag
.GetParam(wxT("ALIGN"));
484 m_WParser
->SetAlign(m_OldAlign
);
485 c
= m_WParser
->SetContainer(new wxHtmlContainerCell(m_Table
));
486 m_Table
->AddCell(c
, tag
);
488 m_WParser
->OpenContainer();
490 if (tag
.GetName() == wxT("TH")) /*header style*/
492 m_WParser
->SetAlign(wxHTML_ALIGN_CENTER
);
499 if (tag
.HasParam(wxT("ALIGN"))) als
= tag
.GetParam(wxT("ALIGN"));
501 if (als
== wxT("RIGHT")) m_WParser
->SetAlign(wxHTML_ALIGN_RIGHT
);
502 else if (als
== wxT("CENTER")) m_WParser
->SetAlign(wxHTML_ALIGN_CENTER
);
504 m_WParser
->OpenContainer();
510 TAG_HANDLER_END(TABLE
)
516 TAGS_MODULE_BEGIN(Tables
)
518 TAGS_MODULE_ADD(TABLE
)
520 TAGS_MODULE_END(Tables
)