]> git.saurik.com Git - wxWidgets.git/blame - src/html/m_tables.cpp
Final scrolling updates
[wxWidgets.git] / src / html / m_tables.cpp
CommitLineData
5526e819 1/////////////////////////////////////////////////////////////////////////////
c88293a4 2// Name: m_tables.cpp
5526e819
VS
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"
f6bcfd97 17#if wxUSE_HTML && wxUSE_STREAMS
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:
c88293a4 29 1. This version of m_tables doesn't support auto-layout algorithm.
5526e819
VS
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 38
c88293a4 39FORCE_LINK_ME(m_tables)
5526e819
VS
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
edbd0635
VS
96 double m_PixelScale;
97
5526e819
VS
98
99 public:
edbd0635 100 wxHtmlTableCell(wxHtmlContainerCell *parent, const wxHtmlTag& tag, double pixel_scale = 1.0);
5526e819
VS
101 ~wxHtmlTableCell();
102 virtual void Layout(int w);
103
104 void AddRow(const wxHtmlTag& tag);
105 void AddCell(wxHtmlContainerCell *cell, const wxHtmlTag& tag);
106 private:
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!!
112};
113
114
115
edbd0635 116wxHtmlTableCell::wxHtmlTableCell(wxHtmlContainerCell *parent, const wxHtmlTag& tag, double pixel_scale)
5526e819
VS
117 : wxHtmlContainerCell(parent)
118{
edbd0635 119 m_PixelScale = pixel_scale;
0413cec5 120 m_HasBorders = (tag.HasParam(wxT("BORDER")) && tag.GetParam(wxT("BORDER")) != wxT("0"));
5526e819
VS
121 m_ColsInfo = NULL;
122 m_NumCols = m_NumRows = 0;
123 m_CellInfo = NULL;
124 m_ActualCol = m_ActualRow = -1;
125
126 /* scan params: */
127 m_tBkg = m_rBkg = -1;
66a77a74
OK
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;
edbd0635
VS
132 m_Spacing = (int)(m_PixelScale * (double)m_Spacing);
133 m_Padding = (int)(m_PixelScale * (double)m_Padding);
5526e819
VS
134
135 if (m_HasBorders)
136 SetBorder(TABLE_BORDER_CLR_1, TABLE_BORDER_CLR_2);
137}
138
139
140
141wxHtmlTableCell::~wxHtmlTableCell()
142{
143 if (m_ColsInfo) free(m_ColsInfo);
4f9297b0
VS
144 if (m_CellInfo)
145 {
5526e819
VS
146 for (int i = 0; i < m_NumRows; i++)
147 free(m_CellInfo[i]);
148 free(m_CellInfo);
149 }
150}
151
152
153
154void wxHtmlTableCell::ReallocCols(int cols)
155{
156 int i,j;
157
4f9297b0
VS
158 for (i = 0; i < m_NumRows; i++)
159 {
5526e819
VS
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;
163 }
164
165 m_ColsInfo = (colStruct*) realloc(m_ColsInfo, sizeof(colStruct) * cols);
4f9297b0
VS
166 for (j = m_NumCols; j < cols; j++)
167 {
5526e819 168 m_ColsInfo[j].width = 0;
efba2b89 169 m_ColsInfo[j].units = wxHTML_UNITS_PERCENT;
5526e819
VS
170 }
171
172 m_NumCols = cols;
173}
174
175
176
177void wxHtmlTableCell::ReallocRows(int rows)
178{
179 m_CellInfo = (cellStruct**) realloc(m_CellInfo, sizeof(cellStruct*) * rows);
80eab469
VS
180 for (int row = m_NumRows; row < rows ; row++)
181 {
182 if (m_NumCols == 0)
183 m_CellInfo[row] = NULL;
184 else
185 {
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;
189 }
5526e819 190 }
5526e819
VS
191 m_NumRows = rows;
192}
193
194
5526e819
VS
195void wxHtmlTableCell::AddRow(const wxHtmlTag& tag)
196{
197 if (m_ActualRow + 1 > m_NumRows - 1)
198 ReallocRows(m_ActualRow + 2);
199 m_ActualRow++;
200 m_ActualCol = -1;
201
202 /* scan params: */
203 m_rBkg = m_tBkg;
66a77a74
OK
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;
5526e819
VS
206}
207
208
209
210void wxHtmlTableCell::AddCell(wxHtmlContainerCell *cell, const wxHtmlTag& tag)
211{
4f9297b0
VS
212 do
213 {
5526e819
VS
214 m_ActualCol++;
215 } while ((m_ActualCol < m_NumCols) && (m_CellInfo[m_ActualRow][m_ActualCol].flag != cellFree));
4f9297b0 216
5526e819
VS
217 if (m_ActualCol > m_NumCols - 1)
218 ReallocCols(m_ActualCol + 1);
219
220 int r = m_ActualRow, c = m_ActualCol;
221
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;
efba2b89 227 m_CellInfo[r][c].valign = wxHTML_ALIGN_TOP;
5526e819
VS
228
229 /* scan for parameters: */
230
231 // width:
232 {
4f9297b0 233 if (tag.HasParam("WIDTH"))
2fa3b707 234 {
5526e819
VS
235 wxString wd = tag.GetParam("WIDTH");
236
4f9297b0 237 if (wd[wd.Length()-1] == '%')
2fa3b707 238 {
66a77a74 239 wxSscanf(wd.c_str(), wxT("%i%%"), &m_ColsInfo[c].width);
efba2b89 240 m_ColsInfo[c].units = wxHTML_UNITS_PERCENT;
5526e819 241 }
4f9297b0 242 else
2fa3b707 243 {
66a77a74 244 wxSscanf(wd.c_str(), wxT("%i"), &m_ColsInfo[c].width);
edbd0635 245 m_ColsInfo[c].width = (int)(m_PixelScale * (double)m_ColsInfo[c].width);
efba2b89 246 m_ColsInfo[c].units = wxHTML_UNITS_PIXELS;
5526e819
VS
247 }
248 }
249 }
250
251
252 // spanning:
253 {
66a77a74
OK
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);
4f9297b0 256 if ((m_CellInfo[r][c].colspan != 1) || (m_CellInfo[r][c].rowspan != 1))
2fa3b707 257 {
5526e819
VS
258 int i, j;
259
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;
266 }
267 }
268
269 //background color:
270 {
271 int bk = m_rBkg;
66a77a74 272 if (tag.HasParam(wxT("BGCOLOR"))) tag.ScanParam(wxT("BGCOLOR"), wxT("#%lX"), &bk);
4f9297b0 273 if (bk != -1)
2fa3b707 274 {
5526e819 275 wxColour clr = wxColour((bk & 0xFF0000) >> 16 , (bk & 0x00FF00) >> 8, (bk & 0x0000FF));
4f9297b0 276 cell->SetBackgroundColour(clr);
5526e819
VS
277 }
278 }
279 if (m_HasBorders)
4f9297b0 280 cell->SetBorder(TABLE_BORDER_CLR_2, TABLE_BORDER_CLR_1);
5526e819
VS
281
282 // vertical alignment:
283 {
284 wxString valign;
0413cec5 285 if (tag.HasParam(wxT("VALIGN"))) valign = tag.GetParam(wxT("VALIGN")); else valign = m_tValign;
5526e819 286 valign.MakeUpper();
0413cec5
OK
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;
efba2b89 289 else m_CellInfo[r][c].valign = wxHTML_ALIGN_CENTER;
5526e819
VS
290 }
291
4f9297b0 292 cell->SetIndent(m_Padding, wxHTML_INDENT_ALL, wxHTML_UNITS_PIXELS);
5526e819
VS
293}
294
295
296
297
298
299void wxHtmlTableCell::Layout(int w)
300{
301 /*
302
303 WIDTH ADJUSTING :
304
305 */
306
4f9297b0
VS
307 if (m_WidthFloatUnits == wxHTML_UNITS_PERCENT)
308 {
5526e819
VS
309 if (m_WidthFloat < 0) m_Width = (100 + m_WidthFloat) * w / 100;
310 else m_Width = m_WidthFloat * w / 100;
311 }
4f9297b0
VS
312 else
313 {
5526e819
VS
314 if (m_WidthFloat < 0) m_Width = w + m_WidthFloat;
315 else m_Width = m_WidthFloat;
316 }
317
318
319 /*
320
321 LAYOUTING :
322
323 */
324
325 /* 1. setup columns widths: */
326 {
327 int wpix = m_Width - (m_NumCols + 1) * m_Spacing;
328 int i, j;
329 int wtemp = 0;
330
331 // 1a. setup fixed-width columns:
332 for (i = 0; i < m_NumCols; i++)
efba2b89 333 if (m_ColsInfo[i].units == wxHTML_UNITS_PIXELS)
5526e819
VS
334 wpix -= (m_ColsInfo[i].pixwidth = m_ColsInfo[i].width);
335
336 // 1b. setup floating-width columns:
337 for (i = 0; i < m_NumCols; i++)
efba2b89 338 if ((m_ColsInfo[i].units == wxHTML_UNITS_PERCENT) && (m_ColsInfo[i].width != 0))
5526e819
VS
339 wtemp += (m_ColsInfo[i].pixwidth = m_ColsInfo[i].width * wpix / 100);
340 wpix -= wtemp;
341
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;
350 }
351
352 /* 2. compute positions of columns: */
353 {
354 int wpos = m_Spacing;
4f9297b0 355 for (int i = 0; i < m_NumCols; i++)
2fa3b707 356 {
5526e819
VS
357 m_ColsInfo[i].leftpos = wpos;
358 wpos += m_ColsInfo[i].pixwidth + m_Spacing;
359 }
360 }
361
362 /* 3. sub-layout all cells: */
363 {
2776d7c3 364 int *ypos = new int[m_NumRows + 1];
5526e819
VS
365
366 int actcol, actrow;
367 int fullwid;
368 wxHtmlContainerCell *actcell;
369
2fa3b707
VS
370 ypos[0] = m_Spacing;
371 for (actrow = 1; actrow <= m_NumRows; actrow++) ypos[actrow] = -1;
4f9297b0 372 for (actrow = 0; actrow < m_NumRows; actrow++)
2fa3b707
VS
373 {
374 if (ypos[actrow] == -1) ypos[actrow] = ypos[actrow-1];
5526e819
VS
375 // 3a. sub-layout and detect max height:
376
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 fullwid = 0;
381 for (int i = actcol; i < m_CellInfo[actrow][actcol].colspan + actcol; i++)
382 fullwid += m_ColsInfo[i].pixwidth;
a97a264f 383 fullwid += (m_CellInfo[actrow][actcol].colspan - 1) * m_Spacing;
4f9297b0
VS
384 actcell->SetMinHeight(m_CellInfo[actrow][actcol].minheight, m_CellInfo[actrow][actcol].valign);
385 actcell->Layout(fullwid);
5526e819 386
4f9297b0 387 if (ypos[actrow] + actcell->GetHeight() + m_CellInfo[actrow][actcol].rowspan * m_Spacing > ypos[actrow + m_CellInfo[actrow][actcol].rowspan])
5526e819 388 ypos[actrow + m_CellInfo[actrow][actcol].rowspan] =
4f9297b0 389 ypos[actrow] + actcell->GetHeight() + m_CellInfo[actrow][actcol].rowspan * m_Spacing;
5526e819
VS
390 }
391 }
392
4f9297b0 393 for (actrow = 0; actrow < m_NumRows; actrow++)
2fa3b707 394 {
5526e819
VS
395 // 3b. place cells in row & let'em all have same height:
396
4f9297b0 397 for (actcol = 0; actcol < m_NumCols; actcol++)
2fa3b707 398 {
5526e819
VS
399 if (m_CellInfo[actrow][actcol].flag != cellUsed) continue;
400 actcell = m_CellInfo[actrow][actcol].cont;
4f9297b0 401 actcell->SetMinHeight(
a97a264f 402 ypos[actrow + m_CellInfo[actrow][actcol].rowspan] - ypos[actrow] - m_Spacing,
5526e819
VS
403 m_CellInfo[actrow][actcol].valign);
404 fullwid = 0;
405 for (int i = actcol; i < m_CellInfo[actrow][actcol].colspan + actcol; i++)
406 fullwid += m_ColsInfo[i].pixwidth;
a97a264f 407 fullwid += (m_CellInfo[actrow][actcol].colspan - 1) * m_Spacing;
4f9297b0
VS
408 actcell->Layout(fullwid);
409 actcell->SetPos(m_ColsInfo[actcol].leftpos, ypos[actrow]);
5526e819 410 }
5526e819
VS
411 }
412 m_Height = ypos[m_NumRows];
2776d7c3 413 delete[] ypos;
5526e819
VS
414 }
415}
416
417
418
419
420
421
422//-----------------------------------------------------------------------------
423// The tables handler:
424//-----------------------------------------------------------------------------
425
426
427TAG_HANDLER_BEGIN(TABLE, "TABLE,TR,TD,TH")
428
429 TAG_HANDLER_VARS
430 wxHtmlTableCell* m_Table;
431 wxString m_tAlign, m_rAlign;
432 int m_OldAlign;
433
434 TAG_HANDLER_CONSTR(TABLE)
435 {
436 m_Table = NULL;
01325161
VS
437 m_tAlign = m_rAlign = wxEmptyString;
438 m_OldAlign = wxHTML_ALIGN_LEFT;
5526e819
VS
439 }
440
441
442 TAG_HANDLER_PROC(tag)
443 {
444 wxHtmlContainerCell *c;
445
446 // new table started, backup upper-level table (if any) and create new:
4f9297b0 447 if (tag.GetName() == wxT("TABLE"))
2fa3b707 448 {
5526e819
VS
449 wxHtmlTableCell *oldt = m_Table;
450 wxHtmlContainerCell *oldcont;
451 int m_OldAlign;
452
4f9297b0 453 oldcont = c = m_WParser->OpenContainer();
5526e819 454
4f9297b0
VS
455 c->SetWidthFloat(tag, m_WParser->GetPixelScale());
456 m_Table = new wxHtmlTableCell(c, tag, m_WParser->GetPixelScale());
457 m_OldAlign = m_WParser->GetAlign();
5526e819 458 m_tAlign = wxEmptyString;
0413cec5 459 if (tag.HasParam(wxT("ALIGN"))) m_tAlign = tag.GetParam(wxT("ALIGN"));
5526e819
VS
460
461 ParseInner(tag);
462
4f9297b0
VS
463 m_WParser->SetAlign(m_OldAlign);
464 m_WParser->SetContainer(oldcont);
465 m_WParser->CloseContainer();
5526e819
VS
466 m_Table = oldt;
467 return TRUE;
468 }
469
470
4f9297b0 471 else if (m_Table && !tag.IsEnding())
2fa3b707 472 {
5526e819 473 // new row in table
4f9297b0 474 if (tag.GetName() == wxT("TR"))
2fa3b707 475 {
4f9297b0 476 m_Table->AddRow(tag);
5526e819 477 m_rAlign = m_tAlign;
0413cec5 478 if (tag.HasParam(wxT("ALIGN"))) m_rAlign = tag.GetParam(wxT("ALIGN"));
5526e819
VS
479 }
480
481 // new cell
4f9297b0 482 else
2fa3b707 483 {
4f9297b0
VS
484 m_WParser->SetAlign(m_OldAlign);
485 c = m_WParser->SetContainer(new wxHtmlContainerCell(m_Table));
486 m_Table->AddCell(c, tag);
5526e819 487
4f9297b0 488 m_WParser->OpenContainer();
5526e819 489
4f9297b0 490 if (tag.GetName() == wxT("TH")) /*header style*/
2fa3b707 491 {
4f9297b0 492 m_WParser->SetAlign(wxHTML_ALIGN_CENTER);
5526e819
VS
493 }
494
495 {
496 wxString als;
497
498 als = m_rAlign;
0413cec5 499 if (tag.HasParam(wxT("ALIGN"))) als = tag.GetParam(wxT("ALIGN"));
5526e819 500 als.MakeUpper();
4f9297b0
VS
501 if (als == wxT("RIGHT")) m_WParser->SetAlign(wxHTML_ALIGN_RIGHT);
502 else if (als == wxT("CENTER")) m_WParser->SetAlign(wxHTML_ALIGN_CENTER);
5526e819 503 }
4f9297b0 504 m_WParser->OpenContainer();
5526e819
VS
505 }
506 }
507 return FALSE;
508 }
509
510TAG_HANDLER_END(TABLE)
511
512
513
514
515
516TAGS_MODULE_BEGIN(Tables)
517
518 TAGS_MODULE_ADD(TABLE)
519
520TAGS_MODULE_END(Tables)
521
522
523#endif