]> git.saurik.com Git - wxWidgets.git/blob - src/html/m_tables.cpp
SetTitle method (internal use only) changed to virtual OnSetTitle, added GetOpenedPag...
[wxWidgets.git] / src / html / m_tables.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: mod_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
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 mod_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(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
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
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
114 wxHtmlTableCell::wxHtmlTableCell(wxHtmlContainerCell *parent, const wxHtmlTag& tag)
115 : wxHtmlContainerCell(parent)
116 {
117 m_HasBorders = (tag.HasParam("BORDER") && tag.GetParam("BORDER") != "0");
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;
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;
129
130 if (m_HasBorders)
131 SetBorder(TABLE_BORDER_CLR_1, TABLE_BORDER_CLR_2);
132 }
133
134
135
136 wxHtmlTableCell::~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
148 void 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;
161 m_ColsInfo[j].units = wxHTML_UNITS_PERCENT;
162 }
163
164 m_NumCols = cols;
165 }
166
167
168
169 void wxHtmlTableCell::ReallocRows(int rows)
170 {
171 m_CellInfo = (cellStruct**) realloc(m_CellInfo, sizeof(cellStruct*) * rows);
172 for (int row = m_NumRows; row < rows ; row++)
173 {
174 if (m_NumCols == 0)
175 m_CellInfo[row] = NULL;
176 else
177 {
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;
181 }
182 }
183 m_NumRows = rows;
184 }
185
186
187 void wxHtmlTableCell::AddRow(const wxHtmlTag& tag)
188 {
189 if (m_ActualRow + 1 > m_NumRows - 1)
190 ReallocRows(m_ActualRow + 2);
191 m_ActualRow++;
192 m_ActualCol = -1;
193
194 /* scan params: */
195 m_rBkg = m_tBkg;
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;
198 }
199
200
201
202 void wxHtmlTableCell::AddCell(wxHtmlContainerCell *cell, const wxHtmlTag& tag)
203 {
204 do {
205 m_ActualCol++;
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);
209
210 int r = m_ActualRow, c = m_ActualCol;
211
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;
218
219 /* scan for parameters: */
220
221 // width:
222 {
223 if (tag.HasParam("WIDTH")) {
224 wxString wd = tag.GetParam("WIDTH");
225
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;
229 }
230 else {
231 wxSscanf(wd.c_str(), wxT("%i"), &m_ColsInfo[c].width);
232 m_ColsInfo[c].units = wxHTML_UNITS_PIXELS;
233 }
234 }
235 }
236
237
238 // spanning:
239 {
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)) {
243 int i, j;
244
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;
251 }
252 }
253
254 //background color:
255 {
256 int bk = m_rBkg;
257 if (tag.HasParam(wxT("BGCOLOR"))) tag.ScanParam(wxT("BGCOLOR"), wxT("#%lX"), &bk);
258 if (bk != -1) {
259 wxColour clr = wxColour((bk & 0xFF0000) >> 16 , (bk & 0x00FF00) >> 8, (bk & 0x0000FF));
260 cell -> SetBackgroundColour(clr);
261 }
262 }
263 if (m_HasBorders)
264 cell -> SetBorder(TABLE_BORDER_CLR_2, TABLE_BORDER_CLR_1);
265
266 // vertical alignment:
267 {
268 wxString valign;
269 if (tag.HasParam("VALIGN")) valign = tag.GetParam("VALIGN"); else valign = m_tValign;
270 valign.MakeUpper();
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;
274 }
275
276 cell -> SetIndent(m_Padding, wxHTML_INDENT_ALL, wxHTML_UNITS_PIXELS);
277 }
278
279
280
281
282
283 void wxHtmlTableCell::Layout(int w)
284 {
285 /*
286
287 WIDTH ADJUSTING :
288
289 */
290
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;
294 }
295 else {
296 if (m_WidthFloat < 0) m_Width = w + m_WidthFloat;
297 else m_Width = m_WidthFloat;
298 }
299
300
301 /*
302
303 LAYOUTING :
304
305 */
306
307 /* 1. setup columns widths: */
308 {
309 int wpix = m_Width - (m_NumCols + 1) * m_Spacing;
310 int i, j;
311 int wtemp = 0;
312
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);
317
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);
322 wpix -= wtemp;
323
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;
332 }
333
334 /* 2. compute positions of columns: */
335 {
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;
340 }
341 }
342
343 /* 3. sub-layout all cells: */
344 {
345 int *ypos = new int[m_NumRows + 1];
346
347 int actcol, actrow;
348 int fullwid;
349 wxHtmlContainerCell *actcell;
350
351 for (actrow = 0; actrow <= m_NumRows; actrow++) ypos[actrow] = m_Spacing;
352
353 for (actrow = 0; actrow < m_NumRows; actrow++) {
354
355 // 3a. sub-layout and detect max height:
356
357 for (actcol = 0; actcol < m_NumCols; actcol++) {
358 if (m_CellInfo[actrow][actcol].flag != cellUsed) continue;
359 actcell = m_CellInfo[actrow][actcol].cont;
360 fullwid = 0;
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);
365
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;
369 }
370 }
371
372
373 for (actrow = 0; actrow < m_NumRows; actrow++) {
374
375 // 3b. place cells in row & let'em all have same 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 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);
383 fullwid = 0;
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]);
388 }
389
390 }
391 m_Height = ypos[m_NumRows];
392 delete[] ypos;
393 }
394 }
395
396
397
398
399
400
401 //-----------------------------------------------------------------------------
402 // The tables handler:
403 //-----------------------------------------------------------------------------
404
405
406 TAG_HANDLER_BEGIN(TABLE, "TABLE,TR,TD,TH")
407
408 TAG_HANDLER_VARS
409 wxHtmlTableCell* m_Table;
410 wxString m_tAlign, m_rAlign;
411 int m_OldAlign;
412
413 TAG_HANDLER_CONSTR(TABLE)
414 {
415 m_Table = NULL;
416 m_tAlign = m_rAlign = wxEmptyString;
417 m_OldAlign = wxHTML_ALIGN_LEFT;
418 }
419
420
421 TAG_HANDLER_PROC(tag)
422 {
423 wxHtmlContainerCell *c;
424
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;
429 int m_OldAlign;
430
431 oldcont = c = m_WParser -> OpenContainer();
432
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");
438
439 ParseInner(tag);
440
441 m_WParser -> SetAlign(m_OldAlign);
442 m_WParser -> SetContainer(oldcont);
443 m_WParser -> CloseContainer();
444 m_Table = oldt;
445 return TRUE;
446 }
447
448
449 else if (m_Table && !tag.IsEnding()) {
450 // new row in table
451 if (tag.GetName() == "TR") {
452 m_Table -> AddRow(tag);
453 m_rAlign = m_tAlign;
454 if (tag.HasParam("ALIGN")) m_rAlign = tag.GetParam("ALIGN");
455 }
456
457 // new cell
458 else {
459 m_WParser -> SetAlign(m_OldAlign);
460 c = m_WParser -> SetContainer(new wxHtmlContainerCell(m_Table));
461 m_Table -> AddCell(c, tag);
462
463 m_WParser -> OpenContainer();
464
465 if (tag.GetName() == "TH") /*header style*/ {
466 m_WParser -> SetAlign(wxHTML_ALIGN_CENTER);
467 }
468
469 {
470 wxString als;
471
472 als = m_rAlign;
473 if (tag.HasParam("ALIGN")) als = tag.GetParam("ALIGN");
474 als.MakeUpper();
475 if (als == "RIGHT") m_WParser -> SetAlign(wxHTML_ALIGN_RIGHT);
476 else if (als == "CENTER") m_WParser -> SetAlign(wxHTML_ALIGN_CENTER);
477 }
478 m_WParser -> OpenContainer();
479 }
480 }
481 return FALSE;
482 }
483
484 TAG_HANDLER_END(TABLE)
485
486
487
488
489
490 TAGS_MODULE_BEGIN(Tables)
491
492 TAGS_MODULE_ADD(TABLE)
493
494 TAGS_MODULE_END(Tables)
495
496
497 #endif