]>
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(wxT("BGCOLOR"))) tag
.ScanParam(wxT("BGCOLOR"), wxT("#%lX"), &m_tBkg
);
126 if (tag
.HasParam(wxT("VALIGN"))) m_tValign
= tag
.GetParam(wxT("VALIGN")); else m_tValign
= wxEmptyString
;
127 if (tag
.HasParam(wxT("CELLSPACING")) && tag
.ScanParam(wxT("CELLSPACING"), wxT("%i"), &m_Spacing
) == 1) {} else m_Spacing
= 2;
128 if (tag
.HasParam(wxT("CELLPADDING")) && tag
.ScanParam(wxT("CELLPADDING"), wxT("%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 for (int row
= m_NumRows
; row
< rows
; row
++)
175 m_CellInfo
[row
] = NULL
;
178 m_CellInfo
[row
] = (cellStruct
*) malloc(sizeof(cellStruct
) * m_NumCols
);
179 for (int col
= 0; col
< m_NumCols
; col
++)
180 m_CellInfo
[row
][col
].flag
= cellFree
;
187 void wxHtmlTableCell::AddRow(const wxHtmlTag
& tag
)
189 if (m_ActualRow
+ 1 > m_NumRows
- 1)
190 ReallocRows(m_ActualRow
+ 2);
196 if (tag
.HasParam(wxT("BGCOLOR"))) tag
.ScanParam(wxT("BGCOLOR"), wxT("#%lX"), &m_rBkg
);
197 if (tag
.HasParam(wxT("VALIGN"))) m_rValign
= tag
.GetParam(wxT("VALIGN")); else m_rValign
= m_tValign
;
202 void wxHtmlTableCell::AddCell(wxHtmlContainerCell
*cell
, const wxHtmlTag
& tag
)
206 } while ((m_ActualCol
< m_NumCols
) && (m_CellInfo
[m_ActualRow
][m_ActualCol
].flag
!= cellFree
));
207 if (m_ActualCol
> m_NumCols
- 1)
208 ReallocCols(m_ActualCol
+ 1);
210 int r
= m_ActualRow
, c
= m_ActualCol
;
212 m_CellInfo
[r
][c
].cont
= cell
;
213 m_CellInfo
[r
][c
].colspan
= 1;
214 m_CellInfo
[r
][c
].rowspan
= 1;
215 m_CellInfo
[r
][c
].flag
= cellUsed
;
216 m_CellInfo
[r
][c
].minheight
= 0;
217 m_CellInfo
[r
][c
].valign
= wxHTML_ALIGN_TOP
;
219 /* scan for parameters: */
223 if (tag
.HasParam("WIDTH")) {
224 wxString wd
= tag
.GetParam("WIDTH");
226 if (wd
[wd
.Length()-1] == '%') {
227 wxSscanf(wd
.c_str(), wxT("%i%%"), &m_ColsInfo
[c
].width
);
228 m_ColsInfo
[c
].units
= wxHTML_UNITS_PERCENT
;
231 wxSscanf(wd
.c_str(), wxT("%i"), &m_ColsInfo
[c
].width
);
232 m_ColsInfo
[c
].units
= wxHTML_UNITS_PIXELS
;
240 if (tag
.HasParam(wxT("COLSPAN"))) tag
.ScanParam(wxT("COLSPAN"), wxT("%i"), &m_CellInfo
[r
][c
].colspan
);
241 if (tag
.HasParam(wxT("ROWSPAN"))) tag
.ScanParam(wxT("ROWSPAN"), wxT("%i"), &m_CellInfo
[r
][c
].rowspan
);
242 if ((m_CellInfo
[r
][c
].colspan
!= 1) || (m_CellInfo
[r
][c
].rowspan
!= 1)) {
245 if (r
+ m_CellInfo
[r
][c
].rowspan
> m_NumRows
) ReallocRows(r
+ m_CellInfo
[r
][c
].rowspan
);
246 if (c
+ m_CellInfo
[r
][c
].colspan
> m_NumCols
) ReallocCols(c
+ m_CellInfo
[r
][c
].colspan
);
247 for (i
= r
; i
< r
+ m_CellInfo
[r
][c
].rowspan
; i
++)
248 for (j
= c
; j
< c
+ m_CellInfo
[r
][c
].colspan
; j
++)
249 m_CellInfo
[i
][j
].flag
= cellSpan
;
250 m_CellInfo
[r
][c
].flag
= cellUsed
;
257 if (tag
.HasParam(wxT("BGCOLOR"))) tag
.ScanParam(wxT("BGCOLOR"), wxT("#%lX"), &bk
);
259 wxColour clr
= wxColour((bk
& 0xFF0000) >> 16 , (bk
& 0x00FF00) >> 8, (bk
& 0x0000FF));
260 cell
-> SetBackgroundColour(clr
);
264 cell
-> SetBorder(TABLE_BORDER_CLR_2
, TABLE_BORDER_CLR_1
);
266 // vertical alignment:
269 if (tag
.HasParam("VALIGN")) valign
= tag
.GetParam("VALIGN"); else valign
= m_tValign
;
271 if (valign
== "TOP") m_CellInfo
[r
][c
].valign
= wxHTML_ALIGN_TOP
;
272 else if (valign
== "BOTTOM") m_CellInfo
[r
][c
].valign
= wxHTML_ALIGN_BOTTOM
;
273 else m_CellInfo
[r
][c
].valign
= wxHTML_ALIGN_CENTER
;
276 cell
-> SetIndent(m_Padding
, wxHTML_INDENT_ALL
, wxHTML_UNITS_PIXELS
);
283 void wxHtmlTableCell::Layout(int w
)
291 if (m_WidthFloatUnits
== wxHTML_UNITS_PERCENT
) {
292 if (m_WidthFloat
< 0) m_Width
= (100 + m_WidthFloat
) * w
/ 100;
293 else m_Width
= m_WidthFloat
* w
/ 100;
296 if (m_WidthFloat
< 0) m_Width
= w
+ m_WidthFloat
;
297 else m_Width
= m_WidthFloat
;
307 /* 1. setup columns widths: */
309 int wpix
= m_Width
- (m_NumCols
+ 1) * m_Spacing
;
313 // 1a. setup fixed-width columns:
314 for (i
= 0; i
< m_NumCols
; i
++)
315 if (m_ColsInfo
[i
].units
== wxHTML_UNITS_PIXELS
)
316 wpix
-= (m_ColsInfo
[i
].pixwidth
= m_ColsInfo
[i
].width
);
318 // 1b. setup floating-width columns:
319 for (i
= 0; i
< m_NumCols
; i
++)
320 if ((m_ColsInfo
[i
].units
== wxHTML_UNITS_PERCENT
) && (m_ColsInfo
[i
].width
!= 0))
321 wtemp
+= (m_ColsInfo
[i
].pixwidth
= m_ColsInfo
[i
].width
* wpix
/ 100);
324 // 1c. setup defalut columns (no width specification supplied):
325 // NOTE! This algorithm doesn't conform to HTML standard : it assigns equal widths
326 // instead of optimal
327 for (i
= j
= 0; i
< m_NumCols
; i
++)
328 if (m_ColsInfo
[i
].width
== 0) j
++;
329 for (i
= 0; i
< m_NumCols
; i
++)
330 if (m_ColsInfo
[i
].width
== 0)
331 m_ColsInfo
[i
].pixwidth
= wpix
/ j
;
334 /* 2. compute positions of columns: */
336 int wpos
= m_Spacing
;
337 for (int i
= 0; i
< m_NumCols
; i
++) {
338 m_ColsInfo
[i
].leftpos
= wpos
;
339 wpos
+= m_ColsInfo
[i
].pixwidth
+ m_Spacing
;
343 /* 3. sub-layout all cells: */
345 int *ypos
= new int[m_NumRows
+ 1];
349 wxHtmlContainerCell
*actcell
;
351 for (actrow
= 0; actrow
<= m_NumRows
; actrow
++) ypos
[actrow
] = m_Spacing
;
353 for (actrow
= 0; actrow
< m_NumRows
; actrow
++) {
355 // 3a. sub-layout and detect max height:
357 for (actcol
= 0; actcol
< m_NumCols
; actcol
++) {
358 if (m_CellInfo
[actrow
][actcol
].flag
!= cellUsed
) continue;
359 actcell
= m_CellInfo
[actrow
][actcol
].cont
;
361 for (int i
= actcol
; i
< m_CellInfo
[actrow
][actcol
].colspan
+ actcol
; i
++)
362 fullwid
+= m_ColsInfo
[i
].pixwidth
;
363 actcell
-> SetMinHeight(m_CellInfo
[actrow
][actcol
].minheight
, m_CellInfo
[actrow
][actcol
].valign
);
364 actcell
-> Layout(fullwid
);
366 if (ypos
[actrow
] + actcell
-> GetHeight() + m_CellInfo
[actrow
][actcol
].rowspan
* m_Spacing
> ypos
[actrow
+ m_CellInfo
[actrow
][actcol
].rowspan
])
367 ypos
[actrow
+ m_CellInfo
[actrow
][actcol
].rowspan
] =
368 ypos
[actrow
] + actcell
-> GetHeight() + m_CellInfo
[actrow
][actcol
].rowspan
* m_Spacing
;
373 for (actrow
= 0; actrow
< m_NumRows
; actrow
++) {
375 // 3b. place cells in row & let'em all have same 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
;
380 actcell
-> SetMinHeight(
381 ypos
[actrow
+ m_CellInfo
[actrow
][actcol
].rowspan
] - ypos
[actrow
] - m_CellInfo
[actrow
][actcol
].rowspan
* m_Spacing
,
382 m_CellInfo
[actrow
][actcol
].valign
);
384 for (int i
= actcol
; i
< m_CellInfo
[actrow
][actcol
].colspan
+ actcol
; i
++)
385 fullwid
+= m_ColsInfo
[i
].pixwidth
;
386 actcell
-> Layout(fullwid
);
387 actcell
-> SetPos(m_ColsInfo
[actcol
].leftpos
, ypos
[actrow
]);
391 m_Height
= ypos
[m_NumRows
];
401 //-----------------------------------------------------------------------------
402 // The tables handler:
403 //-----------------------------------------------------------------------------
406 TAG_HANDLER_BEGIN(TABLE
, "TABLE,TR,TD,TH")
409 wxHtmlTableCell
* m_Table
;
410 wxString m_tAlign
, m_rAlign
;
413 TAG_HANDLER_CONSTR(TABLE
)
416 m_tAlign
= m_rAlign
= wxEmptyString
;
417 m_OldAlign
= wxHTML_ALIGN_LEFT
;
421 TAG_HANDLER_PROC(tag
)
423 wxHtmlContainerCell
*c
;
425 // new table started, backup upper-level table (if any) and create new:
426 if (tag
.GetName() == "TABLE") {
427 wxHtmlTableCell
*oldt
= m_Table
;
428 wxHtmlContainerCell
*oldcont
;
431 oldcont
= c
= m_WParser
-> OpenContainer();
433 c
-> SetWidthFloat(tag
);
434 m_Table
= new wxHtmlTableCell(c
, tag
);
435 m_OldAlign
= m_WParser
-> GetAlign();
436 m_tAlign
= wxEmptyString
;
437 if (tag
.HasParam("ALIGN")) m_tAlign
= tag
.GetParam("ALIGN");
441 m_WParser
-> SetAlign(m_OldAlign
);
442 m_WParser
-> SetContainer(oldcont
);
443 m_WParser
-> CloseContainer();
449 else if (m_Table
&& !tag
.IsEnding()) {
451 if (tag
.GetName() == "TR") {
452 m_Table
-> AddRow(tag
);
454 if (tag
.HasParam("ALIGN")) m_rAlign
= tag
.GetParam("ALIGN");
459 m_WParser
-> SetAlign(m_OldAlign
);
460 c
= m_WParser
-> SetContainer(new wxHtmlContainerCell(m_Table
));
461 m_Table
-> AddCell(c
, tag
);
463 m_WParser
-> OpenContainer();
465 if (tag
.GetName() == "TH") /*header style*/ {
466 m_WParser
-> SetAlign(wxHTML_ALIGN_CENTER
);
473 if (tag
.HasParam("ALIGN")) als
= tag
.GetParam("ALIGN");
475 if (als
== "RIGHT") m_WParser
-> SetAlign(wxHTML_ALIGN_RIGHT
);
476 else if (als
== "CENTER") m_WParser
-> SetAlign(wxHTML_ALIGN_CENTER
);
478 m_WParser
-> OpenContainer();
484 TAG_HANDLER_END(TABLE
)
490 TAGS_MODULE_BEGIN(Tables
)
492 TAGS_MODULE_ADD(TABLE
)
494 TAGS_MODULE_END(Tables
)