]> git.saurik.com Git - wxWidgets.git/blame - src/html/m_tables.cpp
Compile fix in dialup,
[wxWidgets.git] / src / html / m_tables.cpp
CommitLineData
5526e819
VS
1/////////////////////////////////////////////////////////////////////////////
2// Name: mod_tables.cpp
3// Purpose: wxHtml module for tables
4// Author: Vaclav Slavik
69941f05 5// RCS-ID: $Id$
5526e819
VS
6// Copyright: (c) 1999 Vaclav Slavik
7// Licence: wxWindows Licence
8/////////////////////////////////////////////////////////////////////////////
9
3364ab79
RS
10#ifdef __GNUG__
11#pragma implementation
12#endif
13
3096bd2f 14#include "wx/wxprec.h"
3364ab79 15
314260fb 16#include "wx/defs.h"
5526e819 17#if wxUSE_HTML
3364ab79
RS
18#ifdef __BORDLANDC__
19#pragma hdrstop
20#endif
21
22#ifndef WXPRECOMP
3096bd2f 23#include "wx/wx.h"
3364ab79
RS
24#endif
25
5526e819
VS
26
27/*
28REMARKS:
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.
31*/
32
33
69941f05
VS
34#include "wx/html/forcelnk.h"
35#include "wx/html/m_templ.h"
5526e819 36
69941f05 37#include "wx/html/htmlcell.h"
5526e819
VS
38
39FORCE_LINK_ME(mod_tables)
40
41
42#define TABLE_BORDER_CLR_1 wxColour(0xC5, 0xC2, 0xC5)
43#define TABLE_BORDER_CLR_2 wxColour(0x62, 0x61, 0x62)
44
45
46//-----------------------------------------------------------------------------
47// wxHtmlTableCell
48//-----------------------------------------------------------------------------
49
50
51typedef struct {
52 int width, units; // universal
53 int leftpos, pixwidth, maxrealwidth; // temporary (depends on width of table)
54 } colStruct;
55
56typedef enum {
57 cellSpan,
58 cellUsed,
59 cellFree
60 } cellState;
61
62typedef struct {
63 wxHtmlContainerCell *cont;
64 int colspan, rowspan;
65 int minheight, valign;
66 cellState flag;
67 } cellStruct;
68
69
70class wxHtmlTableCell : public wxHtmlContainerCell
71{
72 protected:
73 /* These are real attributes: */
74 bool m_HasBorders;
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]
82 int m_Spacing;
83 // spaces between cells
84 int m_Padding;
85 // cells internal indentation
86
87 private:
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)
91
92 // default values (for table and row):
93 int m_tBkg, m_rBkg;
94 wxString m_tValign, m_rValign;
95
96
97 public:
98 wxHtmlTableCell(wxHtmlContainerCell *parent, const wxHtmlTag& tag);
99 ~wxHtmlTableCell();
100 virtual void Layout(int w);
101
102 void AddRow(const wxHtmlTag& tag);
103 void AddCell(wxHtmlContainerCell *cell, const wxHtmlTag& tag);
104 private:
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!!
110};
111
112
113
114wxHtmlTableCell::wxHtmlTableCell(wxHtmlContainerCell *parent, const wxHtmlTag& tag)
115 : wxHtmlContainerCell(parent)
116{
2136a764 117 m_HasBorders = (tag.HasParam("BORDER") && tag.GetParam("BORDER") != "0");
5526e819
VS
118 m_ColsInfo = NULL;
119 m_NumCols = m_NumRows = 0;
120 m_CellInfo = NULL;
121 m_ActualCol = m_ActualRow = -1;
122
123 /* scan params: */
124 m_tBkg = m_rBkg = -1;
66a77a74
OK
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;
5526e819
VS
129
130 if (m_HasBorders)
131 SetBorder(TABLE_BORDER_CLR_1, TABLE_BORDER_CLR_2);
132}
133
134
135
136wxHtmlTableCell::~wxHtmlTableCell()
137{
138 if (m_ColsInfo) free(m_ColsInfo);
139 if (m_CellInfo) {
140 for (int i = 0; i < m_NumRows; i++)
141 free(m_CellInfo[i]);
142 free(m_CellInfo);
143 }
144}
145
146
147
148void wxHtmlTableCell::ReallocCols(int cols)
149{
150 int i,j;
151
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;
156 }
157
158 m_ColsInfo = (colStruct*) realloc(m_ColsInfo, sizeof(colStruct) * cols);
159 for (j = m_NumCols; j < cols; j++) {
160 m_ColsInfo[j].width = 0;
efba2b89 161 m_ColsInfo[j].units = wxHTML_UNITS_PERCENT;
5526e819
VS
162 }
163
164 m_NumCols = cols;
165}
166
167
168
169void wxHtmlTableCell::ReallocRows(int rows)
170{
171 m_CellInfo = (cellStruct**) realloc(m_CellInfo, sizeof(cellStruct*) * rows);
172 if (m_NumCols != 0) {
173 int x = rows - 1;
af23d90a 174 m_CellInfo[x] = (cellStruct*) malloc(sizeof(cellStruct) * m_NumCols);
5526e819
VS
175 for (int i = 0; i < m_NumCols; i++)
176 m_CellInfo[x][i].flag = cellFree;
177 }
178 else
179 m_CellInfo[rows - 1] = NULL;
180 m_NumRows = rows;
181}
182
183
184
185void wxHtmlTableCell::AddRow(const wxHtmlTag& tag)
186{
187 if (m_ActualRow + 1 > m_NumRows - 1)
188 ReallocRows(m_ActualRow + 2);
189 m_ActualRow++;
190 m_ActualCol = -1;
191
192 /* scan params: */
193 m_rBkg = m_tBkg;
66a77a74
OK
194 if (tag.HasParam(wxT("BGCOLOR"))) tag.ScanParam(wxT("BGCOLOR"), wxT("#%lX"), &m_rBkg);
195 if (tag.HasParam(wxT("VALIGN"))) m_rValign = tag.GetParam(wxT("VALIGN")); else m_rValign = m_tValign;
5526e819
VS
196}
197
198
199
200void wxHtmlTableCell::AddCell(wxHtmlContainerCell *cell, const wxHtmlTag& tag)
201{
202 do {
203 m_ActualCol++;
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);
207
208 int r = m_ActualRow, c = m_ActualCol;
209
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;
efba2b89 215 m_CellInfo[r][c].valign = wxHTML_ALIGN_TOP;
5526e819
VS
216
217 /* scan for parameters: */
218
219 // width:
220 {
221 if (tag.HasParam("WIDTH")) {
222 wxString wd = tag.GetParam("WIDTH");
223
224 if (wd[wd.Length()-1] == '%') {
66a77a74 225 wxSscanf(wd.c_str(), wxT("%i%%"), &m_ColsInfo[c].width);
efba2b89 226 m_ColsInfo[c].units = wxHTML_UNITS_PERCENT;
5526e819
VS
227 }
228 else {
66a77a74 229 wxSscanf(wd.c_str(), wxT("%i"), &m_ColsInfo[c].width);
efba2b89 230 m_ColsInfo[c].units = wxHTML_UNITS_PIXELS;
5526e819
VS
231 }
232 }
233 }
234
235
236 // spanning:
237 {
66a77a74
OK
238 if (tag.HasParam(wxT("COLSPAN"))) tag.ScanParam(wxT("COLSPAN"), wxT("%i"), &m_CellInfo[r][c].colspan);
239 if (tag.HasParam(wxT("ROWSPAN"))) tag.ScanParam(wxT("ROWSPAN"), wxT("%i"), &m_CellInfo[r][c].rowspan);
5526e819
VS
240 if ((m_CellInfo[r][c].colspan != 1) || (m_CellInfo[r][c].rowspan != 1)) {
241 int i, j;
242
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;
249 }
250 }
251
252 //background color:
253 {
254 int bk = m_rBkg;
66a77a74 255 if (tag.HasParam(wxT("BGCOLOR"))) tag.ScanParam(wxT("BGCOLOR"), wxT("#%lX"), &bk);
5526e819
VS
256 if (bk != -1) {
257 wxColour clr = wxColour((bk & 0xFF0000) >> 16 , (bk & 0x00FF00) >> 8, (bk & 0x0000FF));
258 cell -> SetBackgroundColour(clr);
259 }
260 }
261 if (m_HasBorders)
262 cell -> SetBorder(TABLE_BORDER_CLR_2, TABLE_BORDER_CLR_1);
263
264 // vertical alignment:
265 {
266 wxString valign;
267 if (tag.HasParam("VALIGN")) valign = tag.GetParam("VALIGN"); else valign = m_tValign;
268 valign.MakeUpper();
efba2b89
VS
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;
5526e819
VS
272 }
273
efba2b89 274 cell -> SetIndent(m_Padding, wxHTML_INDENT_ALL, wxHTML_UNITS_PIXELS);
5526e819
VS
275}
276
277
278
279
280
281void wxHtmlTableCell::Layout(int w)
282{
283 /*
284
285 WIDTH ADJUSTING :
286
287 */
288
efba2b89 289 if (m_WidthFloatUnits == wxHTML_UNITS_PERCENT) {
5526e819
VS
290 if (m_WidthFloat < 0) m_Width = (100 + m_WidthFloat) * w / 100;
291 else m_Width = m_WidthFloat * w / 100;
292 }
293 else {
294 if (m_WidthFloat < 0) m_Width = w + m_WidthFloat;
295 else m_Width = m_WidthFloat;
296 }
297
298
299 /*
300
301 LAYOUTING :
302
303 */
304
305 /* 1. setup columns widths: */
306 {
307 int wpix = m_Width - (m_NumCols + 1) * m_Spacing;
308 int i, j;
309 int wtemp = 0;
310
311 // 1a. setup fixed-width columns:
312 for (i = 0; i < m_NumCols; i++)
efba2b89 313 if (m_ColsInfo[i].units == wxHTML_UNITS_PIXELS)
5526e819
VS
314 wpix -= (m_ColsInfo[i].pixwidth = m_ColsInfo[i].width);
315
316 // 1b. setup floating-width columns:
317 for (i = 0; i < m_NumCols; i++)
efba2b89 318 if ((m_ColsInfo[i].units == wxHTML_UNITS_PERCENT) && (m_ColsInfo[i].width != 0))
5526e819
VS
319 wtemp += (m_ColsInfo[i].pixwidth = m_ColsInfo[i].width * wpix / 100);
320 wpix -= wtemp;
321
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;
330 }
331
332 /* 2. compute positions of columns: */
333 {
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;
338 }
339 }
340
341 /* 3. sub-layout all cells: */
342 {
2776d7c3 343 int *ypos = new int[m_NumRows + 1];
5526e819
VS
344
345 int actcol, actrow;
346 int fullwid;
347 wxHtmlContainerCell *actcell;
348
349 for (actrow = 0; actrow <= m_NumRows; actrow++) ypos[actrow] = m_Spacing;
350
351 for (actrow = 0; actrow < m_NumRows; actrow++) {
352
353 // 3a. sub-layout and detect max height:
354
355 for (actcol = 0; actcol < m_NumCols; actcol++) {
356 if (m_CellInfo[actrow][actcol].flag != cellUsed) continue;
357 actcell = m_CellInfo[actrow][actcol].cont;
358 fullwid = 0;
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);
363
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;
367 }
368 }
369
370
371 for (actrow = 0; actrow < m_NumRows; actrow++) {
372
373 // 3b. place cells in row & let'em all have same height:
374
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);
381 fullwid = 0;
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]);
386 }
387
388 }
389 m_Height = ypos[m_NumRows];
2776d7c3 390 delete[] ypos;
5526e819
VS
391 }
392}
393
394
395
396
397
398
399//-----------------------------------------------------------------------------
400// The tables handler:
401//-----------------------------------------------------------------------------
402
403
404TAG_HANDLER_BEGIN(TABLE, "TABLE,TR,TD,TH")
405
406 TAG_HANDLER_VARS
407 wxHtmlTableCell* m_Table;
408 wxString m_tAlign, m_rAlign;
409 int m_OldAlign;
410
411 TAG_HANDLER_CONSTR(TABLE)
412 {
413 m_Table = NULL;
01325161
VS
414 m_tAlign = m_rAlign = wxEmptyString;
415 m_OldAlign = wxHTML_ALIGN_LEFT;
5526e819
VS
416 }
417
418
419 TAG_HANDLER_PROC(tag)
420 {
421 wxHtmlContainerCell *c;
422
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;
427 int m_OldAlign;
428
429 oldcont = c = m_WParser -> OpenContainer();
430
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");
436
437 ParseInner(tag);
438
439 m_WParser -> SetAlign(m_OldAlign);
440 m_WParser -> SetContainer(oldcont);
441 m_WParser -> CloseContainer();
442 m_Table = oldt;
443 return TRUE;
444 }
445
446
447 else if (m_Table && !tag.IsEnding()) {
448 // new row in table
449 if (tag.GetName() == "TR") {
450 m_Table -> AddRow(tag);
451 m_rAlign = m_tAlign;
452 if (tag.HasParam("ALIGN")) m_rAlign = tag.GetParam("ALIGN");
453 }
454
455 // new cell
456 else {
457 m_WParser -> SetAlign(m_OldAlign);
458 c = m_WParser -> SetContainer(new wxHtmlContainerCell(m_Table));
459 m_Table -> AddCell(c, tag);
460
461 m_WParser -> OpenContainer();
462
463 if (tag.GetName() == "TH") /*header style*/ {
efba2b89 464 m_WParser -> SetAlign(wxHTML_ALIGN_CENTER);
5526e819
VS
465 }
466
467 {
468 wxString als;
469
470 als = m_rAlign;
471 if (tag.HasParam("ALIGN")) als = tag.GetParam("ALIGN");
472 als.MakeUpper();
efba2b89
VS
473 if (als == "RIGHT") m_WParser -> SetAlign(wxHTML_ALIGN_RIGHT);
474 else if (als == "CENTER") m_WParser -> SetAlign(wxHTML_ALIGN_CENTER);
5526e819
VS
475 }
476 m_WParser -> OpenContainer();
477 }
478 }
479 return FALSE;
480 }
481
482TAG_HANDLER_END(TABLE)
483
484
485
486
487
488TAGS_MODULE_BEGIN(Tables)
489
490 TAGS_MODULE_ADD(TABLE)
491
492TAGS_MODULE_END(Tables)
493
494
495#endif