]>
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 // width of the column either in pixels or percents
54 // ('width' is the number, 'units' determines its meaning)
55 int minWidth
, maxWidth
;
56 // minimal/maximal column width. This is needed by HTML 4.0
57 // layouting algorithm and can be determined by trying to
58 // layout table cells with width=1 and width=infinity
59 int leftpos
, pixwidth
, maxrealwidth
;
60 // temporary (depends on actual width of table)
72 wxHtmlContainerCell
*cont
;
74 int minheight
, valign
;
79 class wxHtmlTableCell
: public wxHtmlContainerCell
82 /* These are real attributes: */
84 // should we draw borders or not?
86 // number of columns; rows
87 int m_NumCols
, m_NumRows
;
88 // array of column information
89 colStruct
*m_ColsInfo
;
90 // 2D array of all cells in the table : m_CellInfo[row][column]
91 cellStruct
**m_CellInfo
;
92 // spaces between cells
94 // cells internal indentation
98 /* ...and these are valid only when parsing the table: */
100 // number of actual column (ranging from 0..m_NumCols)
101 int m_ActualCol
, m_ActualRow
;
103 // default values (for table and row):
104 wxColour m_tBkg
, m_rBkg
;
105 wxString m_tValign
, m_rValign
;
111 wxHtmlTableCell(wxHtmlContainerCell
*parent
, const wxHtmlTag
& tag
, double pixel_scale
= 1.0);
113 virtual void Layout(int w
);
115 void AddRow(const wxHtmlTag
& tag
);
116 void AddCell(wxHtmlContainerCell
*cell
, const wxHtmlTag
& tag
);
119 // Reallocates memory to given number of cols/rows
120 // and changes m_NumCols/m_NumRows value to reflect this change
121 // NOTE! You CAN'T change m_NumCols/m_NumRows before calling this!!
122 void ReallocCols(int cols
);
123 void ReallocRows(int rows
);
125 // Computes minimal and maximal widths of columns. Needs to be called
126 // only once, before first Layout().
127 void ComputeMinMaxWidths();
132 wxHtmlTableCell::wxHtmlTableCell(wxHtmlContainerCell
*parent
, const wxHtmlTag
& tag
, double pixel_scale
)
133 : wxHtmlContainerCell(parent
)
135 m_PixelScale
= pixel_scale
;
137 (tag
.HasParam(wxT("BORDER")) && tag
.GetParam(wxT("BORDER")) != wxT("0"));
139 m_NumCols
= m_NumRows
= 0;
141 m_ActualCol
= m_ActualRow
= -1;
144 if (tag
.HasParam(wxT("BGCOLOR")))
145 tag
.GetParamAsColour(wxT("BGCOLOR"), &m_tBkg
);
146 if (tag
.HasParam(wxT("VALIGN")))
147 m_tValign
= tag
.GetParam(wxT("VALIGN"));
149 m_tValign
= wxEmptyString
;
150 if (!tag
.GetParamAsInt(wxT("CELLSPACING"), &m_Spacing
))
152 if (!tag
.GetParamAsInt(wxT("CELLPADDING"), &m_Padding
))
154 m_Spacing
= (int)(m_PixelScale
* (double)m_Spacing
);
155 m_Padding
= (int)(m_PixelScale
* (double)m_Padding
);
158 SetBorder(TABLE_BORDER_CLR_1
, TABLE_BORDER_CLR_2
);
163 wxHtmlTableCell::~wxHtmlTableCell()
165 if (m_ColsInfo
) free(m_ColsInfo
);
168 for (int i
= 0; i
< m_NumRows
; i
++)
176 void wxHtmlTableCell::ReallocCols(int cols
)
180 for (i
= 0; i
< m_NumRows
; i
++)
182 m_CellInfo
[i
] = (cellStruct
*) realloc(m_CellInfo
[i
], sizeof(cellStruct
) * cols
);
183 for (j
= m_NumCols
; j
< cols
; j
++)
184 m_CellInfo
[i
][j
].flag
= cellFree
;
187 m_ColsInfo
= (colStruct
*) realloc(m_ColsInfo
, sizeof(colStruct
) * cols
);
188 for (j
= m_NumCols
; j
< cols
; j
++)
190 m_ColsInfo
[j
].width
= 0;
191 m_ColsInfo
[j
].units
= wxHTML_UNITS_PERCENT
;
192 m_ColsInfo
[j
].minWidth
= m_ColsInfo
[j
].maxWidth
= -1;
200 void wxHtmlTableCell::ReallocRows(int rows
)
202 m_CellInfo
= (cellStruct
**) realloc(m_CellInfo
, sizeof(cellStruct
*) * rows
);
203 for (int row
= m_NumRows
; row
< rows
; row
++)
206 m_CellInfo
[row
] = NULL
;
209 m_CellInfo
[row
] = (cellStruct
*) malloc(sizeof(cellStruct
) * m_NumCols
);
210 for (int col
= 0; col
< m_NumCols
; col
++)
211 m_CellInfo
[row
][col
].flag
= cellFree
;
218 void wxHtmlTableCell::AddRow(const wxHtmlTag
& tag
)
221 // VS: real allocation of row entry is done in AddCell in order
222 // to correctly handle empty rows (i.e. "<tr></tr>")
223 // m_ActualCol == -1 indicates that AddCell has to allocate new row.
227 if (tag
.HasParam(wxT("BGCOLOR")))
228 tag
.GetParamAsColour(wxT("BGCOLOR"), &m_rBkg
);
229 if (tag
.HasParam(wxT("VALIGN")))
230 m_rValign
= tag
.GetParam(wxT("VALIGN"));
232 m_rValign
= m_tValign
;
237 void wxHtmlTableCell::AddCell(wxHtmlContainerCell
*cell
, const wxHtmlTag
& tag
)
239 // Is this cell in new row?
240 // VS: we can't do it in AddRow, see my comment there
241 if (m_ActualCol
== -1)
243 if (m_ActualRow
+ 1 > m_NumRows
- 1)
244 ReallocRows(m_ActualRow
+ 2);
252 } while ((m_ActualCol
< m_NumCols
) &&
253 (m_CellInfo
[m_ActualRow
][m_ActualCol
].flag
!= cellFree
));
255 if (m_ActualCol
> m_NumCols
- 1)
256 ReallocCols(m_ActualCol
+ 1);
258 int r
= m_ActualRow
, c
= m_ActualCol
;
260 m_CellInfo
[r
][c
].cont
= cell
;
261 m_CellInfo
[r
][c
].colspan
= 1;
262 m_CellInfo
[r
][c
].rowspan
= 1;
263 m_CellInfo
[r
][c
].flag
= cellUsed
;
264 m_CellInfo
[r
][c
].minheight
= 0;
265 m_CellInfo
[r
][c
].valign
= wxHTML_ALIGN_TOP
;
267 /* scan for parameters: */
271 if (tag
.HasParam(wxT("WIDTH")))
273 wxString wd
= tag
.GetParam(wxT("WIDTH"));
275 if (wd
[wd
.Length()-1] == wxT('%'))
277 wxSscanf(wd
.c_str(), wxT("%i%%"), &m_ColsInfo
[c
].width
);
278 m_ColsInfo
[c
].units
= wxHTML_UNITS_PERCENT
;
282 wxSscanf(wd
.c_str(), wxT("%i"), &m_ColsInfo
[c
].width
);
283 m_ColsInfo
[c
].width
= (int)(m_PixelScale
* (double)m_ColsInfo
[c
].width
);
284 m_ColsInfo
[c
].units
= wxHTML_UNITS_PIXELS
;
292 tag
.GetParamAsInt(wxT("COLSPAN"), &m_CellInfo
[r
][c
].colspan
);
293 tag
.GetParamAsInt(wxT("ROWSPAN"), &m_CellInfo
[r
][c
].rowspan
);
294 if ((m_CellInfo
[r
][c
].colspan
!= 1) || (m_CellInfo
[r
][c
].rowspan
!= 1))
298 if (r
+ m_CellInfo
[r
][c
].rowspan
> m_NumRows
)
299 ReallocRows(r
+ m_CellInfo
[r
][c
].rowspan
);
300 if (c
+ m_CellInfo
[r
][c
].colspan
> m_NumCols
)
301 ReallocCols(c
+ m_CellInfo
[r
][c
].colspan
);
302 for (i
= r
; i
< r
+ m_CellInfo
[r
][c
].rowspan
; i
++)
303 for (j
= c
; j
< c
+ m_CellInfo
[r
][c
].colspan
; j
++)
304 m_CellInfo
[i
][j
].flag
= cellSpan
;
305 m_CellInfo
[r
][c
].flag
= cellUsed
;
311 wxColour bk
= m_rBkg
;
312 if (tag
.HasParam(wxT("BGCOLOR")))
313 tag
.GetParamAsColour(wxT("BGCOLOR"), &bk
);
315 cell
->SetBackgroundColour(bk
);
318 cell
->SetBorder(TABLE_BORDER_CLR_2
, TABLE_BORDER_CLR_1
);
320 // vertical alignment:
323 if (tag
.HasParam(wxT("VALIGN")))
324 valign
= tag
.GetParam(wxT("VALIGN"));
328 if (valign
== wxT("TOP"))
329 m_CellInfo
[r
][c
].valign
= wxHTML_ALIGN_TOP
;
330 else if (valign
== wxT("BOTTOM"))
331 m_CellInfo
[r
][c
].valign
= wxHTML_ALIGN_BOTTOM
;
332 else m_CellInfo
[r
][c
].valign
= wxHTML_ALIGN_CENTER
;
335 cell
->SetIndent(m_Padding
, wxHTML_INDENT_ALL
, wxHTML_UNITS_PIXELS
);
340 void wxHtmlTableCell::ComputeMinMaxWidths()
342 if (m_NumCols
== 0 || m_ColsInfo
[0].minWidth
!= -1) return;
344 int left
, right
, width
;
346 for (int c
= 0; c
< m_NumCols
; c
++)
348 for (int r
= 0; r
< m_NumRows
; r
++)
350 cellStruct
& cell
= m_CellInfo
[r
][c
];
351 if (cell
.flag
== cellUsed
)
353 cell
.cont
->Layout(2*m_Padding
+ 1);
354 cell
.cont
->GetHorizontalConstraints(&left
, &right
);
355 width
= right
- left
;
356 width
-= (cell
.colspan
-1) * m_Spacing
;
357 // HTML 4.0 says it is acceptable to distribute min/max
358 // width of spanning cells evently
359 width
/= cell
.colspan
;
360 for (int j
= 0; j
< cell
.colspan
; j
++)
361 if (width
> m_ColsInfo
[c
+j
].minWidth
)
362 m_ColsInfo
[c
+j
].minWidth
= width
;
367 // FIXME -- compute maxWidth as well. Not needed yet, so there's no
368 // point in computing it.
372 void wxHtmlTableCell::Layout(int w
)
374 ComputeMinMaxWidths();
376 wxHtmlCell::Layout(w
);
384 if (m_WidthFloatUnits
== wxHTML_UNITS_PERCENT
)
386 if (m_WidthFloat
< 0) m_Width
= (100 + m_WidthFloat
) * w
/ 100;
387 else m_Width
= m_WidthFloat
* w
/ 100;
391 if (m_WidthFloat
< 0) m_Width
= w
+ m_WidthFloat
;
392 else m_Width
= m_WidthFloat
;
402 /* 1. setup columns widths: */
404 int wpix
= m_Width
- (m_NumCols
+ 1) * m_Spacing
;
407 // 1a. setup fixed-width columns:
408 for (i
= 0; i
< m_NumCols
; i
++)
409 if (m_ColsInfo
[i
].units
== wxHTML_UNITS_PIXELS
)
411 m_ColsInfo
[i
].pixwidth
= wxMax(m_ColsInfo
[i
].width
,
412 m_ColsInfo
[i
].minWidth
);
413 wpix
-= m_ColsInfo
[i
].pixwidth
;
416 // 1b. setup floating-width columns:
418 for (i
= 0; i
< m_NumCols
; i
++)
419 if ((m_ColsInfo
[i
].units
== wxHTML_UNITS_PERCENT
) && (m_ColsInfo
[i
].width
!= 0))
421 m_ColsInfo
[i
].pixwidth
= wxMax(m_ColsInfo
[i
].width
* wpix
/ 100,
422 m_ColsInfo
[i
].minWidth
);
423 wtemp
+= m_ColsInfo
[i
].pixwidth
;
427 // 1c. setup defalut columns (no width specification supplied):
428 // NOTE! This algorithm doesn't conform to HTML standard : it assigns equal widths
429 // instead of optimal
430 for (i
= j
= 0; i
< m_NumCols
; i
++)
431 if (m_ColsInfo
[i
].width
== 0) j
++;
432 for (i
= 0; i
< m_NumCols
; i
++)
433 if (m_ColsInfo
[i
].width
== 0)
434 m_ColsInfo
[i
].pixwidth
= wpix
/ j
;
437 /* 2. compute positions of columns: */
439 int wpos
= m_Spacing
;
440 for (int i
= 0; i
< m_NumCols
; i
++)
442 m_ColsInfo
[i
].leftpos
= wpos
;
443 wpos
+= m_ColsInfo
[i
].pixwidth
+ m_Spacing
;
447 /* 3. sub-layout all cells: */
449 int *ypos
= new int[m_NumRows
+ 1];
453 wxHtmlContainerCell
*actcell
;
456 for (actrow
= 1; actrow
<= m_NumRows
; actrow
++) ypos
[actrow
] = -1;
457 for (actrow
= 0; actrow
< m_NumRows
; actrow
++)
459 if (ypos
[actrow
] == -1) ypos
[actrow
] = ypos
[actrow
-1];
460 // 3a. sub-layout and detect max height:
462 for (actcol
= 0; actcol
< m_NumCols
; actcol
++) {
463 if (m_CellInfo
[actrow
][actcol
].flag
!= cellUsed
) continue;
464 actcell
= m_CellInfo
[actrow
][actcol
].cont
;
466 for (int i
= actcol
; i
< m_CellInfo
[actrow
][actcol
].colspan
+ actcol
; i
++)
467 fullwid
+= m_ColsInfo
[i
].pixwidth
;
468 fullwid
+= (m_CellInfo
[actrow
][actcol
].colspan
- 1) * m_Spacing
;
469 actcell
->SetMinHeight(m_CellInfo
[actrow
][actcol
].minheight
, m_CellInfo
[actrow
][actcol
].valign
);
470 actcell
->Layout(fullwid
);
472 if (ypos
[actrow
] + actcell
->GetHeight() + m_CellInfo
[actrow
][actcol
].rowspan
* m_Spacing
> ypos
[actrow
+ m_CellInfo
[actrow
][actcol
].rowspan
])
473 ypos
[actrow
+ m_CellInfo
[actrow
][actcol
].rowspan
] =
474 ypos
[actrow
] + actcell
->GetHeight() + m_CellInfo
[actrow
][actcol
].rowspan
* m_Spacing
;
478 for (actrow
= 0; actrow
< m_NumRows
; actrow
++)
480 // 3b. place cells in row & let'em all have same height:
482 for (actcol
= 0; actcol
< m_NumCols
; actcol
++)
484 if (m_CellInfo
[actrow
][actcol
].flag
!= cellUsed
) continue;
485 actcell
= m_CellInfo
[actrow
][actcol
].cont
;
486 actcell
->SetMinHeight(
487 ypos
[actrow
+ m_CellInfo
[actrow
][actcol
].rowspan
] - ypos
[actrow
] - m_Spacing
,
488 m_CellInfo
[actrow
][actcol
].valign
);
490 for (int i
= actcol
; i
< m_CellInfo
[actrow
][actcol
].colspan
+ actcol
; i
++)
491 fullwid
+= m_ColsInfo
[i
].pixwidth
;
492 fullwid
+= (m_CellInfo
[actrow
][actcol
].colspan
- 1) * m_Spacing
;
493 actcell
->Layout(fullwid
);
494 actcell
->SetPos(m_ColsInfo
[actcol
].leftpos
, ypos
[actrow
]);
497 m_Height
= ypos
[m_NumRows
];
507 //-----------------------------------------------------------------------------
508 // The tables handler:
509 //-----------------------------------------------------------------------------
512 TAG_HANDLER_BEGIN(TABLE
, "TABLE,TR,TD,TH")
515 wxHtmlTableCell
* m_Table
;
516 wxString m_tAlign
, m_rAlign
;
519 TAG_HANDLER_CONSTR(TABLE
)
522 m_tAlign
= m_rAlign
= wxEmptyString
;
523 m_OldAlign
= wxHTML_ALIGN_LEFT
;
527 TAG_HANDLER_PROC(tag
)
529 wxHtmlContainerCell
*c
;
531 // new table started, backup upper-level table (if any) and create new:
532 if (tag
.GetName() == wxT("TABLE"))
534 wxHtmlTableCell
*oldt
= m_Table
;
535 wxHtmlContainerCell
*oldcont
;
538 oldcont
= c
= m_WParser
->OpenContainer();
540 c
->SetWidthFloat(tag
, m_WParser
->GetPixelScale());
541 m_Table
= new wxHtmlTableCell(c
, tag
, m_WParser
->GetPixelScale());
542 m_OldAlign
= m_WParser
->GetAlign();
543 m_tAlign
= wxEmptyString
;
544 if (tag
.HasParam(wxT("ALIGN")))
545 m_tAlign
= tag
.GetParam(wxT("ALIGN"));
549 m_WParser
->SetAlign(m_OldAlign
);
550 m_WParser
->SetContainer(oldcont
);
551 m_WParser
->CloseContainer();
560 if (tag
.GetName() == wxT("TR"))
562 m_Table
->AddRow(tag
);
564 if (tag
.HasParam(wxT("ALIGN")))
565 m_rAlign
= tag
.GetParam(wxT("ALIGN"));
571 m_WParser
->SetAlign(m_OldAlign
);
572 c
= m_WParser
->SetContainer(new wxHtmlContainerCell(m_Table
));
573 m_Table
->AddCell(c
, tag
);
575 m_WParser
->OpenContainer();
577 if (tag
.GetName() == wxT("TH")) /*header style*/
579 m_WParser
->SetAlign(wxHTML_ALIGN_CENTER
);
586 if (tag
.HasParam(wxT("ALIGN")))
587 als
= tag
.GetParam(wxT("ALIGN"));
589 if (als
== wxT("RIGHT"))
590 m_WParser
->SetAlign(wxHTML_ALIGN_RIGHT
);
591 else if (als
== wxT("CENTER"))
592 m_WParser
->SetAlign(wxHTML_ALIGN_CENTER
);
594 m_WParser
->OpenContainer();
600 TAG_HANDLER_END(TABLE
)
606 TAGS_MODULE_BEGIN(Tables
)
608 TAGS_MODULE_ADD(TABLE
)
610 TAGS_MODULE_END(Tables
)