]> git.saurik.com Git - wxWidgets.git/blob - src/html/m_tables.cpp
Fixed broken compilation due to Ove's last nice changes :-(
[wxWidgets.git] / src / html / m_tables.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: m_tables.cpp
3 // Purpose: wxHtml module for tables
4 // Author: Vaclav Slavik
5 // RCS-ID: $Id$
6 // Copyright: (c) 1999 Vaclav Slavik
7 // Licence: wxWindows Licence
8 /////////////////////////////////////////////////////////////////////////////
9
10 #ifdef __GNUG__
11 #pragma implementation
12 #endif
13
14 #include "wx/wxprec.h"
15
16 #include "wx/defs.h"
17 #if wxUSE_HTML && wxUSE_STREAMS
18 #ifdef __BORDLANDC__
19 #pragma hdrstop
20 #endif
21
22 #ifndef WXPRECOMP
23 #include "wx/wx.h"
24 #endif
25
26
27 /*
28 REMARKS:
29 1. This version of m_tables doesn't support auto-layout algorithm.
30 This means that all columns are of same width unless explicitly specified.
31 */
32
33
34 #include "wx/html/forcelnk.h"
35 #include "wx/html/m_templ.h"
36
37 #include "wx/html/htmlcell.h"
38
39 FORCE_LINK_ME(m_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
51 typedef struct {
52 int width, units; // universal
53 int leftpos, pixwidth, maxrealwidth; // temporary (depends on width of table)
54 } colStruct;
55
56 typedef enum {
57 cellSpan,
58 cellUsed,
59 cellFree
60 } cellState;
61
62 typedef struct {
63 wxHtmlContainerCell *cont;
64 int colspan, rowspan;
65 int minheight, valign;
66 cellState flag;
67 } cellStruct;
68
69
70 class 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 double m_PixelScale;
97
98
99 public:
100 wxHtmlTableCell(wxHtmlContainerCell *parent, const wxHtmlTag& tag, double pixel_scale = 1.0);
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
116 wxHtmlTableCell::wxHtmlTableCell(wxHtmlContainerCell *parent, const wxHtmlTag& tag, double pixel_scale)
117 : wxHtmlContainerCell(parent)
118 {
119 m_PixelScale = pixel_scale;
120 m_HasBorders = (tag.HasParam(wxT("BORDER")) && tag.GetParam(wxT("BORDER")) != wxT("0"));
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;
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;
132 m_Spacing = (int)(m_PixelScale * (double)m_Spacing);
133 m_Padding = (int)(m_PixelScale * (double)m_Padding);
134
135 if (m_HasBorders)
136 SetBorder(TABLE_BORDER_CLR_1, TABLE_BORDER_CLR_2);
137 }
138
139
140
141 wxHtmlTableCell::~wxHtmlTableCell()
142 {
143 if (m_ColsInfo) free(m_ColsInfo);
144 if (m_CellInfo) {
145 for (int i = 0; i < m_NumRows; i++)
146 free(m_CellInfo[i]);
147 free(m_CellInfo);
148 }
149 }
150
151
152
153 void wxHtmlTableCell::ReallocCols(int cols)
154 {
155 int i,j;
156
157 for (i = 0; i < m_NumRows; i++) {
158 m_CellInfo[i] = (cellStruct*) realloc(m_CellInfo[i], sizeof(cellStruct) * cols);
159 for (j = m_NumCols; j < cols; j++)
160 m_CellInfo[i][j].flag = cellFree;
161 }
162
163 m_ColsInfo = (colStruct*) realloc(m_ColsInfo, sizeof(colStruct) * cols);
164 for (j = m_NumCols; j < cols; j++) {
165 m_ColsInfo[j].width = 0;
166 m_ColsInfo[j].units = wxHTML_UNITS_PERCENT;
167 }
168
169 m_NumCols = cols;
170 }
171
172
173
174 void wxHtmlTableCell::ReallocRows(int rows)
175 {
176 m_CellInfo = (cellStruct**) realloc(m_CellInfo, sizeof(cellStruct*) * rows);
177 for (int row = m_NumRows; row < rows ; row++)
178 {
179 if (m_NumCols == 0)
180 m_CellInfo[row] = NULL;
181 else
182 {
183 m_CellInfo[row] = (cellStruct*) malloc(sizeof(cellStruct) * m_NumCols);
184 for (int col = 0; col < m_NumCols; col++)
185 m_CellInfo[row][col].flag = cellFree;
186 }
187 }
188 m_NumRows = rows;
189 }
190
191
192 void wxHtmlTableCell::AddRow(const wxHtmlTag& tag)
193 {
194 if (m_ActualRow + 1 > m_NumRows - 1)
195 ReallocRows(m_ActualRow + 2);
196 m_ActualRow++;
197 m_ActualCol = -1;
198
199 /* scan params: */
200 m_rBkg = m_tBkg;
201 if (tag.HasParam(wxT("BGCOLOR"))) tag.ScanParam(wxT("BGCOLOR"), wxT("#%lX"), &m_rBkg);
202 if (tag.HasParam(wxT("VALIGN"))) m_rValign = tag.GetParam(wxT("VALIGN")); else m_rValign = m_tValign;
203 }
204
205
206
207 void wxHtmlTableCell::AddCell(wxHtmlContainerCell *cell, const wxHtmlTag& tag)
208 {
209 do {
210 m_ActualCol++;
211 } while ((m_ActualCol < m_NumCols) && (m_CellInfo[m_ActualRow][m_ActualCol].flag != cellFree));
212 if (m_ActualCol > m_NumCols - 1)
213 ReallocCols(m_ActualCol + 1);
214
215 int r = m_ActualRow, c = m_ActualCol;
216
217 m_CellInfo[r][c].cont = cell;
218 m_CellInfo[r][c].colspan = 1;
219 m_CellInfo[r][c].rowspan = 1;
220 m_CellInfo[r][c].flag = cellUsed;
221 m_CellInfo[r][c].minheight = 0;
222 m_CellInfo[r][c].valign = wxHTML_ALIGN_TOP;
223
224 /* scan for parameters: */
225
226 // width:
227 {
228 if (tag.HasParam("WIDTH")) {
229 wxString wd = tag.GetParam("WIDTH");
230
231 if (wd[wd.Length()-1] == '%') {
232 wxSscanf(wd.c_str(), wxT("%i%%"), &m_ColsInfo[c].width);
233 m_ColsInfo[c].units = wxHTML_UNITS_PERCENT;
234 }
235 else {
236 wxSscanf(wd.c_str(), wxT("%i"), &m_ColsInfo[c].width);
237 m_ColsInfo[c].width = (int)(m_PixelScale * (double)m_ColsInfo[c].width);
238 m_ColsInfo[c].units = wxHTML_UNITS_PIXELS;
239 }
240 }
241 }
242
243
244 // spanning:
245 {
246 if (tag.HasParam(wxT("COLSPAN"))) tag.ScanParam(wxT("COLSPAN"), wxT("%i"), &m_CellInfo[r][c].colspan);
247 if (tag.HasParam(wxT("ROWSPAN"))) tag.ScanParam(wxT("ROWSPAN"), wxT("%i"), &m_CellInfo[r][c].rowspan);
248 if ((m_CellInfo[r][c].colspan != 1) || (m_CellInfo[r][c].rowspan != 1)) {
249 int i, j;
250
251 if (r + m_CellInfo[r][c].rowspan > m_NumRows) ReallocRows(r + m_CellInfo[r][c].rowspan);
252 if (c + m_CellInfo[r][c].colspan > m_NumCols) ReallocCols(c + m_CellInfo[r][c].colspan);
253 for (i = r; i < r + m_CellInfo[r][c].rowspan; i++)
254 for (j = c; j < c + m_CellInfo[r][c].colspan; j++)
255 m_CellInfo[i][j].flag = cellSpan;
256 m_CellInfo[r][c].flag = cellUsed;
257 }
258 }
259
260 //background color:
261 {
262 int bk = m_rBkg;
263 if (tag.HasParam(wxT("BGCOLOR"))) tag.ScanParam(wxT("BGCOLOR"), wxT("#%lX"), &bk);
264 if (bk != -1) {
265 wxColour clr = wxColour((bk & 0xFF0000) >> 16 , (bk & 0x00FF00) >> 8, (bk & 0x0000FF));
266 cell -> SetBackgroundColour(clr);
267 }
268 }
269 if (m_HasBorders)
270 cell -> SetBorder(TABLE_BORDER_CLR_2, TABLE_BORDER_CLR_1);
271
272 // vertical alignment:
273 {
274 wxString valign;
275 if (tag.HasParam(wxT("VALIGN"))) valign = tag.GetParam(wxT("VALIGN")); else valign = m_tValign;
276 valign.MakeUpper();
277 if (valign == wxT("TOP")) m_CellInfo[r][c].valign = wxHTML_ALIGN_TOP;
278 else if (valign == wxT("BOTTOM")) m_CellInfo[r][c].valign = wxHTML_ALIGN_BOTTOM;
279 else m_CellInfo[r][c].valign = wxHTML_ALIGN_CENTER;
280 }
281
282 cell -> SetIndent(m_Padding, wxHTML_INDENT_ALL, wxHTML_UNITS_PIXELS);
283 }
284
285
286
287
288
289 void wxHtmlTableCell::Layout(int w)
290 {
291 /*
292
293 WIDTH ADJUSTING :
294
295 */
296
297 if (m_WidthFloatUnits == wxHTML_UNITS_PERCENT) {
298 if (m_WidthFloat < 0) m_Width = (100 + m_WidthFloat) * w / 100;
299 else m_Width = m_WidthFloat * w / 100;
300 }
301 else {
302 if (m_WidthFloat < 0) m_Width = w + m_WidthFloat;
303 else m_Width = m_WidthFloat;
304 }
305
306
307 /*
308
309 LAYOUTING :
310
311 */
312
313 /* 1. setup columns widths: */
314 {
315 int wpix = m_Width - (m_NumCols + 1) * m_Spacing;
316 int i, j;
317 int wtemp = 0;
318
319 // 1a. setup fixed-width columns:
320 for (i = 0; i < m_NumCols; i++)
321 if (m_ColsInfo[i].units == wxHTML_UNITS_PIXELS)
322 wpix -= (m_ColsInfo[i].pixwidth = m_ColsInfo[i].width);
323
324 // 1b. setup floating-width columns:
325 for (i = 0; i < m_NumCols; i++)
326 if ((m_ColsInfo[i].units == wxHTML_UNITS_PERCENT) && (m_ColsInfo[i].width != 0))
327 wtemp += (m_ColsInfo[i].pixwidth = m_ColsInfo[i].width * wpix / 100);
328 wpix -= wtemp;
329
330 // 1c. setup defalut columns (no width specification supplied):
331 // NOTE! This algorithm doesn't conform to HTML standard : it assigns equal widths
332 // instead of optimal
333 for (i = j = 0; i < m_NumCols; i++)
334 if (m_ColsInfo[i].width == 0) j++;
335 for (i = 0; i < m_NumCols; i++)
336 if (m_ColsInfo[i].width == 0)
337 m_ColsInfo[i].pixwidth = wpix / j;
338 }
339
340 /* 2. compute positions of columns: */
341 {
342 int wpos = m_Spacing;
343 for (int i = 0; i < m_NumCols; i++) {
344 m_ColsInfo[i].leftpos = wpos;
345 wpos += m_ColsInfo[i].pixwidth + m_Spacing;
346 }
347 }
348
349 /* 3. sub-layout all cells: */
350 {
351 int *ypos = new int[m_NumRows + 1];
352
353 int actcol, actrow;
354 int fullwid;
355 wxHtmlContainerCell *actcell;
356
357 for (actrow = 0; actrow <= m_NumRows; actrow++) ypos[actrow] = m_Spacing;
358
359 for (actrow = 0; actrow < m_NumRows; actrow++) {
360
361 // 3a. sub-layout and detect max height:
362
363 for (actcol = 0; actcol < m_NumCols; actcol++) {
364 if (m_CellInfo[actrow][actcol].flag != cellUsed) continue;
365 actcell = m_CellInfo[actrow][actcol].cont;
366 fullwid = 0;
367 for (int i = actcol; i < m_CellInfo[actrow][actcol].colspan + actcol; i++)
368 fullwid += m_ColsInfo[i].pixwidth;
369 fullwid += (m_CellInfo[actrow][actcol].colspan - 1) * m_Spacing;
370 actcell -> SetMinHeight(m_CellInfo[actrow][actcol].minheight, m_CellInfo[actrow][actcol].valign);
371 actcell -> Layout(fullwid);
372
373 if (ypos[actrow] + actcell -> GetHeight() + m_CellInfo[actrow][actcol].rowspan * m_Spacing > ypos[actrow + m_CellInfo[actrow][actcol].rowspan])
374 ypos[actrow + m_CellInfo[actrow][actcol].rowspan] =
375 ypos[actrow] + actcell -> GetHeight() + m_CellInfo[actrow][actcol].rowspan * m_Spacing;
376 }
377 }
378
379
380 for (actrow = 0; actrow < m_NumRows; actrow++) {
381
382 // 3b. place cells in row & let'em all have same height:
383
384 for (actcol = 0; actcol < m_NumCols; actcol++) {
385 if (m_CellInfo[actrow][actcol].flag != cellUsed) continue;
386 actcell = m_CellInfo[actrow][actcol].cont;
387 actcell -> SetMinHeight(
388 ypos[actrow + m_CellInfo[actrow][actcol].rowspan] - ypos[actrow] - m_Spacing,
389 m_CellInfo[actrow][actcol].valign);
390 fullwid = 0;
391 for (int i = actcol; i < m_CellInfo[actrow][actcol].colspan + actcol; i++)
392 fullwid += m_ColsInfo[i].pixwidth;
393 fullwid += (m_CellInfo[actrow][actcol].colspan - 1) * m_Spacing;
394 actcell -> Layout(fullwid);
395 actcell -> SetPos(m_ColsInfo[actcol].leftpos, ypos[actrow]);
396 }
397
398 }
399 m_Height = ypos[m_NumRows];
400 delete[] ypos;
401 }
402 }
403
404
405
406
407
408
409 //-----------------------------------------------------------------------------
410 // The tables handler:
411 //-----------------------------------------------------------------------------
412
413
414 TAG_HANDLER_BEGIN(TABLE, "TABLE,TR,TD,TH")
415
416 TAG_HANDLER_VARS
417 wxHtmlTableCell* m_Table;
418 wxString m_tAlign, m_rAlign;
419 int m_OldAlign;
420
421 TAG_HANDLER_CONSTR(TABLE)
422 {
423 m_Table = NULL;
424 m_tAlign = m_rAlign = wxEmptyString;
425 m_OldAlign = wxHTML_ALIGN_LEFT;
426 }
427
428
429 TAG_HANDLER_PROC(tag)
430 {
431 wxHtmlContainerCell *c;
432
433 // new table started, backup upper-level table (if any) and create new:
434 if (tag.GetName() == wxT("TABLE")) {
435 wxHtmlTableCell *oldt = m_Table;
436 wxHtmlContainerCell *oldcont;
437 int m_OldAlign;
438
439 oldcont = c = m_WParser -> OpenContainer();
440
441 c -> SetWidthFloat(tag, m_WParser -> GetPixelScale());
442 m_Table = new wxHtmlTableCell(c, tag, m_WParser -> GetPixelScale());
443 m_OldAlign = m_WParser -> GetAlign();
444 m_tAlign = wxEmptyString;
445 if (tag.HasParam(wxT("ALIGN"))) m_tAlign = tag.GetParam(wxT("ALIGN"));
446
447 ParseInner(tag);
448
449 m_WParser -> SetAlign(m_OldAlign);
450 m_WParser -> SetContainer(oldcont);
451 m_WParser -> CloseContainer();
452 m_Table = oldt;
453 return TRUE;
454 }
455
456
457 else if (m_Table && !tag.IsEnding()) {
458 // new row in table
459 if (tag.GetName() == wxT("TR")) {
460 m_Table -> AddRow(tag);
461 m_rAlign = m_tAlign;
462 if (tag.HasParam(wxT("ALIGN"))) m_rAlign = tag.GetParam(wxT("ALIGN"));
463 }
464
465 // new cell
466 else {
467 m_WParser -> SetAlign(m_OldAlign);
468 c = m_WParser -> SetContainer(new wxHtmlContainerCell(m_Table));
469 m_Table -> AddCell(c, tag);
470
471 m_WParser -> OpenContainer();
472
473 if (tag.GetName() == wxT("TH")) /*header style*/ {
474 m_WParser -> SetAlign(wxHTML_ALIGN_CENTER);
475 }
476
477 {
478 wxString als;
479
480 als = m_rAlign;
481 if (tag.HasParam(wxT("ALIGN"))) als = tag.GetParam(wxT("ALIGN"));
482 als.MakeUpper();
483 if (als == wxT("RIGHT")) m_WParser -> SetAlign(wxHTML_ALIGN_RIGHT);
484 else if (als == wxT("CENTER")) m_WParser -> SetAlign(wxHTML_ALIGN_CENTER);
485 }
486 m_WParser -> OpenContainer();
487 }
488 }
489 return FALSE;
490 }
491
492 TAG_HANDLER_END(TABLE)
493
494
495
496
497
498 TAGS_MODULE_BEGIN(Tables)
499
500 TAGS_MODULE_ADD(TABLE)
501
502 TAGS_MODULE_END(Tables)
503
504
505 #endif