]>
git.saurik.com Git - wxWidgets.git/blob - src/html/m_tables.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: mod_tables.cpp
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>
29 1. This version of mod_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(mod_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
;
98 wxHtmlTableCell(wxHtmlContainerCell
*parent
, const wxHtmlTag
& tag
);
100 virtual void Layout(int w
);
102 void AddRow(const wxHtmlTag
& tag
);
103 void AddCell(wxHtmlContainerCell
*cell
, const wxHtmlTag
& tag
);
105 void ReallocCols(int cols
);
106 void ReallocRows(int rows
);
107 // reallocates memory to given number of cols/rows
108 // and changes m_NumCols/m_NumRows value to reflect this change
109 // NOTE! You CAN'T change m_NumCols/m_NumRows before calling this!!
114 wxHtmlTableCell::wxHtmlTableCell(wxHtmlContainerCell
*parent
, const wxHtmlTag
& tag
)
115 : wxHtmlContainerCell(parent
)
117 m_HasBorders
= (tag
.HasParam("BORDER") && tag
.GetParam("BORDER") != "0");
119 m_NumCols
= m_NumRows
= 0;
121 m_ActualCol
= m_ActualRow
= -1;
124 m_tBkg
= m_rBkg
= -1;
125 if (tag
.HasParam("BGCOLOR")) tag
.ScanParam("BGCOLOR", "#%lX", &m_tBkg
);
126 if (tag
.HasParam("VALIGN")) m_tValign
= tag
.GetParam("VALIGN"); else m_tValign
= wxEmptyString
;
127 if (tag
.HasParam("CELLSPACING") && tag
.ScanParam("CELLSPACING", "%i", &m_Spacing
) == 1) {} else m_Spacing
= 2;
128 if (tag
.HasParam("CELLPADDING") && tag
.ScanParam("CELLPADDING", "%i", &m_Padding
) == 1) {} else m_Padding
= 3;
131 SetBorder(TABLE_BORDER_CLR_1
, TABLE_BORDER_CLR_2
);
136 wxHtmlTableCell::~wxHtmlTableCell()
138 if (m_ColsInfo
) free(m_ColsInfo
);
140 for (int i
= 0; i
< m_NumRows
; i
++)
148 void wxHtmlTableCell::ReallocCols(int cols
)
152 for (i
= 0; i
< m_NumRows
; i
++) {
153 m_CellInfo
[i
] = (cellStruct
*) realloc(m_CellInfo
[i
], sizeof(cellStruct
) * cols
);
154 for (j
= m_NumCols
; j
< cols
; j
++)
155 m_CellInfo
[i
][j
].flag
= cellFree
;
158 m_ColsInfo
= (colStruct
*) realloc(m_ColsInfo
, sizeof(colStruct
) * cols
);
159 for (j
= m_NumCols
; j
< cols
; j
++) {
160 m_ColsInfo
[j
].width
= 0;
161 m_ColsInfo
[j
].units
= wxHTML_UNITS_PERCENT
;
169 void wxHtmlTableCell::ReallocRows(int rows
)
171 m_CellInfo
= (cellStruct
**) realloc(m_CellInfo
, sizeof(cellStruct
*) * rows
);
172 if (m_NumCols
!= 0) {
174 m_CellInfo
[x
] = (cellStruct
*) malloc(sizeof(cellStruct
) * m_NumCols
);
175 for (int i
= 0; i
< m_NumCols
; i
++)
176 m_CellInfo
[x
][i
].flag
= cellFree
;
179 m_CellInfo
[rows
- 1] = NULL
;
185 void wxHtmlTableCell::AddRow(const wxHtmlTag
& tag
)
187 if (m_ActualRow
+ 1 > m_NumRows
- 1)
188 ReallocRows(m_ActualRow
+ 2);
194 if (tag
.HasParam("BGCOLOR")) tag
.ScanParam("BGCOLOR", "#%lX", &m_rBkg
);
195 if (tag
.HasParam("VALIGN")) m_rValign
= tag
.GetParam("VALIGN"); else m_rValign
= m_tValign
;
200 void wxHtmlTableCell::AddCell(wxHtmlContainerCell
*cell
, const wxHtmlTag
& tag
)
204 } while ((m_ActualCol
< m_NumCols
) && (m_CellInfo
[m_ActualRow
][m_ActualCol
].flag
!= cellFree
));
205 if (m_ActualCol
> m_NumCols
- 1)
206 ReallocCols(m_ActualCol
+ 1);
208 int r
= m_ActualRow
, c
= m_ActualCol
;
210 m_CellInfo
[r
][c
].cont
= cell
;
211 m_CellInfo
[r
][c
].colspan
= 1;
212 m_CellInfo
[r
][c
].rowspan
= 1;
213 m_CellInfo
[r
][c
].flag
= cellUsed
;
214 m_CellInfo
[r
][c
].minheight
= 0;
215 m_CellInfo
[r
][c
].valign
= wxHTML_ALIGN_TOP
;
217 /* scan for parameters: */
221 if (tag
.HasParam("WIDTH")) {
222 wxString wd
= tag
.GetParam("WIDTH");
224 if (wd
[wd
.Length()-1] == '%') {
225 sscanf(wd
.c_str(), "%i%%", &m_ColsInfo
[c
].width
);
226 m_ColsInfo
[c
].units
= wxHTML_UNITS_PERCENT
;
229 sscanf(wd
.c_str(), "%i", &m_ColsInfo
[c
].width
);
230 m_ColsInfo
[c
].units
= wxHTML_UNITS_PIXELS
;
238 if (tag
.HasParam("COLSPAN")) tag
.ScanParam("COLSPAN", "%i", &m_CellInfo
[r
][c
].colspan
);
239 if (tag
.HasParam("ROWSPAN")) tag
.ScanParam("ROWSPAN", "%i", &m_CellInfo
[r
][c
].rowspan
);
240 if ((m_CellInfo
[r
][c
].colspan
!= 1) || (m_CellInfo
[r
][c
].rowspan
!= 1)) {
243 if (r
+ m_CellInfo
[r
][c
].rowspan
> m_NumRows
) ReallocRows(r
+ m_CellInfo
[r
][c
].rowspan
);
244 if (c
+ m_CellInfo
[r
][c
].colspan
> m_NumCols
) ReallocCols(c
+ m_CellInfo
[r
][c
].colspan
);
245 for (i
= r
; i
< r
+ m_CellInfo
[r
][c
].rowspan
; i
++)
246 for (j
= c
; j
< c
+ m_CellInfo
[r
][c
].colspan
; j
++)
247 m_CellInfo
[i
][j
].flag
= cellSpan
;
248 m_CellInfo
[r
][c
].flag
= cellUsed
;
255 if (tag
.HasParam("BGCOLOR")) tag
.ScanParam("BGCOLOR", "#%lX", &bk
);
257 wxColour clr
= wxColour((bk
& 0xFF0000) >> 16 , (bk
& 0x00FF00) >> 8, (bk
& 0x0000FF));
258 cell
-> SetBackgroundColour(clr
);
262 cell
-> SetBorder(TABLE_BORDER_CLR_2
, TABLE_BORDER_CLR_1
);
264 // vertical alignment:
267 if (tag
.HasParam("VALIGN")) valign
= tag
.GetParam("VALIGN"); else valign
= m_tValign
;
269 if (valign
== "TOP") m_CellInfo
[r
][c
].valign
= wxHTML_ALIGN_TOP
;
270 else if (valign
== "BOTTOM") m_CellInfo
[r
][c
].valign
= wxHTML_ALIGN_BOTTOM
;
271 else m_CellInfo
[r
][c
].valign
= wxHTML_ALIGN_CENTER
;
274 cell
-> SetIndent(m_Padding
, wxHTML_INDENT_ALL
, wxHTML_UNITS_PIXELS
);
281 void wxHtmlTableCell::Layout(int w
)
289 if (m_WidthFloatUnits
== wxHTML_UNITS_PERCENT
) {
290 if (m_WidthFloat
< 0) m_Width
= (100 + m_WidthFloat
) * w
/ 100;
291 else m_Width
= m_WidthFloat
* w
/ 100;
294 if (m_WidthFloat
< 0) m_Width
= w
+ m_WidthFloat
;
295 else m_Width
= m_WidthFloat
;
305 /* 1. setup columns widths: */
307 int wpix
= m_Width
- (m_NumCols
+ 1) * m_Spacing
;
311 // 1a. setup fixed-width columns:
312 for (i
= 0; i
< m_NumCols
; i
++)
313 if (m_ColsInfo
[i
].units
== wxHTML_UNITS_PIXELS
)
314 wpix
-= (m_ColsInfo
[i
].pixwidth
= m_ColsInfo
[i
].width
);
316 // 1b. setup floating-width columns:
317 for (i
= 0; i
< m_NumCols
; i
++)
318 if ((m_ColsInfo
[i
].units
== wxHTML_UNITS_PERCENT
) && (m_ColsInfo
[i
].width
!= 0))
319 wtemp
+= (m_ColsInfo
[i
].pixwidth
= m_ColsInfo
[i
].width
* wpix
/ 100);
322 // 1c. setup defalut columns (no width specification supplied):
323 // NOTE! This algorithm doesn't conform to HTML standard : it assigns equal widths
324 // instead of optimal
325 for (i
= j
= 0; i
< m_NumCols
; i
++)
326 if (m_ColsInfo
[i
].width
== 0) j
++;
327 for (i
= 0; i
< m_NumCols
; i
++)
328 if (m_ColsInfo
[i
].width
== 0)
329 m_ColsInfo
[i
].pixwidth
= wpix
/ j
;
332 /* 2. compute positions of columns: */
334 int wpos
= m_Spacing
;
335 for (int i
= 0; i
< m_NumCols
; i
++) {
336 m_ColsInfo
[i
].leftpos
= wpos
;
337 wpos
+= m_ColsInfo
[i
].pixwidth
+ m_Spacing
;
341 /* 3. sub-layout all cells: */
343 int *ypos
= new int[m_NumRows
+ 1];
347 wxHtmlContainerCell
*actcell
;
349 for (actrow
= 0; actrow
<= m_NumRows
; actrow
++) ypos
[actrow
] = m_Spacing
;
351 for (actrow
= 0; actrow
< m_NumRows
; actrow
++) {
353 // 3a. sub-layout and detect max height:
355 for (actcol
= 0; actcol
< m_NumCols
; actcol
++) {
356 if (m_CellInfo
[actrow
][actcol
].flag
!= cellUsed
) continue;
357 actcell
= m_CellInfo
[actrow
][actcol
].cont
;
359 for (int i
= actcol
; i
< m_CellInfo
[actrow
][actcol
].colspan
+ actcol
; i
++)
360 fullwid
+= m_ColsInfo
[i
].pixwidth
;
361 actcell
-> SetMinHeight(m_CellInfo
[actrow
][actcol
].minheight
, m_CellInfo
[actrow
][actcol
].valign
);
362 actcell
-> Layout(fullwid
);
364 if (ypos
[actrow
] + actcell
-> GetHeight() + m_CellInfo
[actrow
][actcol
].rowspan
* m_Spacing
> ypos
[actrow
+ m_CellInfo
[actrow
][actcol
].rowspan
])
365 ypos
[actrow
+ m_CellInfo
[actrow
][actcol
].rowspan
] =
366 ypos
[actrow
] + actcell
-> GetHeight() + m_CellInfo
[actrow
][actcol
].rowspan
* m_Spacing
;
371 for (actrow
= 0; actrow
< m_NumRows
; actrow
++) {
373 // 3b. place cells in row & let'em all have same height:
375 for (actcol
= 0; actcol
< m_NumCols
; actcol
++) {
376 if (m_CellInfo
[actrow
][actcol
].flag
!= cellUsed
) continue;
377 actcell
= m_CellInfo
[actrow
][actcol
].cont
;
378 actcell
-> SetMinHeight(
379 ypos
[actrow
+ m_CellInfo
[actrow
][actcol
].rowspan
] - ypos
[actrow
] - m_CellInfo
[actrow
][actcol
].rowspan
* m_Spacing
,
380 m_CellInfo
[actrow
][actcol
].valign
);
382 for (int i
= actcol
; i
< m_CellInfo
[actrow
][actcol
].colspan
+ actcol
; i
++)
383 fullwid
+= m_ColsInfo
[i
].pixwidth
;
384 actcell
-> Layout(fullwid
);
385 actcell
-> SetPos(m_ColsInfo
[actcol
].leftpos
, ypos
[actrow
]);
389 m_Height
= ypos
[m_NumRows
];
399 //-----------------------------------------------------------------------------
400 // The tables handler:
401 //-----------------------------------------------------------------------------
404 TAG_HANDLER_BEGIN(TABLE
, "TABLE,TR,TD,TH")
407 wxHtmlTableCell
* m_Table
;
408 wxString m_tAlign
, m_rAlign
;
411 TAG_HANDLER_CONSTR(TABLE
)
414 m_tAlign
= m_rAlign
= wxEmptyString
;
415 m_OldAlign
= wxHTML_ALIGN_LEFT
;
419 TAG_HANDLER_PROC(tag
)
421 wxHtmlContainerCell
*c
;
423 // new table started, backup upper-level table (if any) and create new:
424 if (tag
.GetName() == "TABLE") {
425 wxHtmlTableCell
*oldt
= m_Table
;
426 wxHtmlContainerCell
*oldcont
;
429 oldcont
= c
= m_WParser
-> OpenContainer();
431 c
-> SetWidthFloat(tag
);
432 m_Table
= new wxHtmlTableCell(c
, tag
);
433 m_OldAlign
= m_WParser
-> GetAlign();
434 m_tAlign
= wxEmptyString
;
435 if (tag
.HasParam("ALIGN")) m_tAlign
= tag
.GetParam("ALIGN");
439 m_WParser
-> SetAlign(m_OldAlign
);
440 m_WParser
-> SetContainer(oldcont
);
441 m_WParser
-> CloseContainer();
447 else if (m_Table
&& !tag
.IsEnding()) {
449 if (tag
.GetName() == "TR") {
450 m_Table
-> AddRow(tag
);
452 if (tag
.HasParam("ALIGN")) m_rAlign
= tag
.GetParam("ALIGN");
457 m_WParser
-> SetAlign(m_OldAlign
);
458 c
= m_WParser
-> SetContainer(new wxHtmlContainerCell(m_Table
));
459 m_Table
-> AddCell(c
, tag
);
461 m_WParser
-> OpenContainer();
463 if (tag
.GetName() == "TH") /*header style*/ {
464 m_WParser
-> SetAlign(wxHTML_ALIGN_CENTER
);
471 if (tag
.HasParam("ALIGN")) als
= tag
.GetParam("ALIGN");
473 if (als
== "RIGHT") m_WParser
-> SetAlign(wxHTML_ALIGN_RIGHT
);
474 else if (als
== "CENTER") m_WParser
-> SetAlign(wxHTML_ALIGN_CENTER
);
476 m_WParser
-> OpenContainer();
482 TAG_HANDLER_END(TABLE
)
488 TAGS_MODULE_BEGIN(Tables
)
490 TAGS_MODULE_ADD(TABLE
)
492 TAGS_MODULE_END(Tables
)