]> git.saurik.com Git - wxWidgets.git/blob - src/html/htmlcell.cpp
added operator[](unsigned int) const -- testing it now on Linux/axp,
[wxWidgets.git] / src / html / htmlcell.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: htmlcell.cpp
3 // Purpose: wxHtmlCell - basic element of HTML output
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
19 #ifdef __BORDLANDC__
20 #pragma hdrstop
21 #endif
22
23 #ifndef WXPRECOMP
24 #include "wx/wx.h"
25 #endif
26
27 #include "wx/html/htmlcell.h"
28 #include "wx/html/htmlwin.h"
29 #include <stdlib.h>
30
31
32 //-----------------------------------------------------------------------------
33 // wxHtmlCell
34 //-----------------------------------------------------------------------------
35
36
37 void wxHtmlCell::OnMouseClick(wxWindow *parent, int x, int y,
38 bool WXUNUSED(left),
39 bool WXUNUSED(middle),
40 bool WXUNUSED(right))
41 {
42 wxString lnk = GetLink(x, y);
43 if (lnk != wxEmptyString)
44 ((wxHtmlWindow*)parent) -> OnLinkClicked(lnk);
45 // note : this overcasting is legal because parent is *always* wxHtmlWindow
46 }
47
48
49
50 bool wxHtmlCell::AdjustPagebreak(int *pagebreak)
51 {
52
53 if ((!m_CanLiveOnPagebreak) &&
54 m_PosY < *pagebreak && m_PosY + m_Height > *pagebreak) {
55 *pagebreak = m_PosY;
56 if (m_Next != NULL) m_Next -> AdjustPagebreak(pagebreak);
57 return TRUE;
58 }
59
60 else {
61 if (m_Next != NULL) return m_Next -> AdjustPagebreak(pagebreak);
62 else return FALSE;
63 }
64 }
65
66
67
68
69 //-----------------------------------------------------------------------------
70 // wxHtmlWordCell
71 //-----------------------------------------------------------------------------
72
73 wxHtmlWordCell::wxHtmlWordCell(const wxString& word, wxDC& dc) : wxHtmlCell()
74 {
75 m_Word = word;
76
77 m_Word.Replace(wxT("&nbsp;"), wxT(" "), TRUE);
78 m_Word.Replace(wxT("&quot;"), wxT("\""), TRUE);
79 m_Word.Replace(wxT("&lt;"), wxT("<"), TRUE);
80 m_Word.Replace(wxT("&gt;"), wxT(">"), TRUE);
81 m_Word.Replace(wxT("&amp;"), wxT("&"), TRUE);
82
83 m_Word.Replace(wxT("&nbsp "), wxT(" "), TRUE);
84 m_Word.Replace(wxT("&quot "), wxT("\""), TRUE);
85 m_Word.Replace(wxT("&lt "), wxT("<"), TRUE);
86 m_Word.Replace(wxT("&gt "), wxT(">"), TRUE);
87 m_Word.Replace(wxT("&amp "), wxT("&"), TRUE);
88
89 dc.GetTextExtent(m_Word, &m_Width, &m_Height, &m_Descent);
90 SetCanLiveOnPagebreak(FALSE);
91 }
92
93
94
95 void wxHtmlWordCell::Draw(wxDC& dc, int x, int y, int view_y1, int view_y2)
96 {
97 dc.DrawText(m_Word, x + m_PosX, y + m_PosY);
98 wxHtmlCell::Draw(dc, x, y, view_y1, view_y2);
99 }
100
101
102
103 //-----------------------------------------------------------------------------
104 // wxHtmlContainerCell
105 //-----------------------------------------------------------------------------
106
107
108 wxHtmlContainerCell::wxHtmlContainerCell(wxHtmlContainerCell *parent) : wxHtmlCell()
109 {
110 m_Cells = m_LastCell = NULL;
111 m_Parent = parent;
112 if (m_Parent) m_Parent -> InsertCell(this);
113 m_AlignHor = wxHTML_ALIGN_LEFT;
114 m_AlignVer = wxHTML_ALIGN_BOTTOM;
115 m_IndentLeft = m_IndentRight = m_IndentTop = m_IndentBottom = 0;
116 m_WidthFloat = 100; m_WidthFloatUnits = wxHTML_UNITS_PERCENT;
117 m_UseBkColour = FALSE;
118 m_UseBorder = FALSE;
119 m_MinHeight = m_MaxLineWidth = 0;
120 m_MinHeightAlign = wxHTML_ALIGN_TOP;
121 }
122
123
124
125 void wxHtmlContainerCell::SetIndent(int i, int what, int units)
126 {
127 int val = (units == wxHTML_UNITS_PIXELS) ? i : -i;
128 if (what & wxHTML_INDENT_LEFT) m_IndentLeft = val;
129 if (what & wxHTML_INDENT_RIGHT) m_IndentRight = val;
130 if (what & wxHTML_INDENT_TOP) m_IndentTop = val;
131 if (what & wxHTML_INDENT_BOTTOM) m_IndentBottom = val;
132 }
133
134
135
136 int wxHtmlContainerCell::GetIndent(int ind) const
137 {
138 if (ind & wxHTML_INDENT_LEFT) return m_IndentLeft;
139 else if (ind & wxHTML_INDENT_RIGHT) return m_IndentRight;
140 else if (ind & wxHTML_INDENT_TOP) return m_IndentTop;
141 else if (ind & wxHTML_INDENT_BOTTOM) return m_IndentBottom;
142 else return -1; /* BUG! Should not be called... */
143 }
144
145
146
147
148 int wxHtmlContainerCell::GetIndentUnits(int ind) const
149 {
150 bool p = FALSE;
151 if (ind & wxHTML_INDENT_LEFT) p = m_IndentLeft < 0;
152 else if (ind & wxHTML_INDENT_RIGHT) p = m_IndentRight < 0;
153 else if (ind & wxHTML_INDENT_TOP) p = m_IndentTop < 0;
154 else if (ind & wxHTML_INDENT_BOTTOM) p = m_IndentBottom < 0;
155 if (p) return wxHTML_UNITS_PERCENT;
156 else return wxHTML_UNITS_PIXELS;
157 }
158
159
160
161 bool wxHtmlContainerCell::AdjustPagebreak(int *pagebreak)
162 {
163 if (!m_CanLiveOnPagebreak)
164 return wxHtmlCell::AdjustPagebreak(pagebreak);
165
166 else {
167 wxHtmlCell *c = GetFirstCell();
168 bool rt = FALSE;
169 int pbrk = *pagebreak - m_PosY;
170
171 while (c) {
172 if (c -> AdjustPagebreak(&pbrk)) rt = TRUE;
173 c = c -> GetNext();
174 }
175 if (rt) *pagebreak = pbrk + m_PosY;
176 return rt;
177 }
178 }
179
180
181
182 void wxHtmlContainerCell::Layout(int w)
183 {
184 wxHtmlCell *cell = m_Cells, *line = m_Cells;
185 long xpos = 0, ypos = m_IndentTop;
186 int xdelta = 0, ybasicpos = 0, ydiff;
187 int s_width, s_indent;
188 int ysizeup = 0, ysizedown = 0;
189
190 /*
191
192 WIDTH ADJUSTING :
193
194 */
195
196 if (m_WidthFloatUnits == wxHTML_UNITS_PERCENT) {
197 if (m_WidthFloat < 0) m_Width = (100 + m_WidthFloat) * w / 100;
198 else m_Width = m_WidthFloat * w / 100;
199 }
200 else {
201 if (m_WidthFloat < 0) m_Width = w + m_WidthFloat;
202 else m_Width = m_WidthFloat;
203 }
204
205 if (m_Cells) {
206 int l = (m_IndentLeft < 0) ? (-m_IndentLeft * m_Width / 100) : m_IndentLeft;
207 int r = (m_IndentRight < 0) ? (-m_IndentRight * m_Width / 100) : m_IndentRight;
208 m_Cells -> Layout(m_Width - (l + r));
209 }
210
211 /*
212
213 LAYOUTING :
214
215 */
216
217 // adjust indentation:
218 s_indent = (m_IndentLeft < 0) ? (-m_IndentLeft * m_Width / 100) : m_IndentLeft;
219 s_width = m_Width - s_indent - ((m_IndentRight < 0) ? (-m_IndentRight * m_Width / 100) : m_IndentRight);
220
221 m_MaxLineWidth = 0;
222
223 // my own layouting:
224 while (cell != NULL) {
225 switch (m_AlignVer) {
226 case wxHTML_ALIGN_TOP : ybasicpos = 0; break;
227 case wxHTML_ALIGN_BOTTOM : ybasicpos = - cell -> GetHeight(); break;
228 case wxHTML_ALIGN_CENTER : ybasicpos = - cell -> GetHeight() / 2; break;
229 }
230 ydiff = cell -> GetHeight() + ybasicpos;
231
232 if (cell -> GetDescent() + ydiff > ysizedown) ysizedown = cell -> GetDescent() + ydiff;
233 if (ybasicpos + cell -> GetDescent() < -ysizeup) ysizeup = - (ybasicpos + cell -> GetDescent());
234
235 cell -> SetPos(xpos, ybasicpos + cell -> GetDescent());
236 xpos += cell -> GetWidth();
237 cell = cell -> GetNext();
238
239 // force new line if occured:
240 if ((cell == NULL) || (xpos + cell -> GetWidth() > s_width)) {
241 if (xpos > m_MaxLineWidth) m_MaxLineWidth = xpos;
242 if (ysizeup < 0) ysizeup = 0;
243 if (ysizedown < 0) ysizedown = 0;
244 switch (m_AlignHor) {
245 case wxHTML_ALIGN_LEFT : xdelta = 0; break;
246 case wxHTML_ALIGN_RIGHT : xdelta = 0 + (s_width - xpos); break;
247 case wxHTML_ALIGN_CENTER : xdelta = 0 + (s_width - xpos) / 2; break;
248 }
249 if (xdelta < 0) xdelta = 0;
250 xdelta += s_indent;
251
252 ypos += ysizeup;
253 while (line != cell) {
254 line -> SetPos(line -> GetPosX() + xdelta, ypos + line -> GetPosY());
255 line = line -> GetNext();
256 }
257
258 ypos += ysizedown;
259 xpos = 0;
260 ysizeup = ysizedown = 0;
261 line = cell;
262 }
263 }
264
265 // setup height & width, depending on container layout:
266 m_Height = ypos + (ysizedown + ysizeup) + m_IndentBottom;
267
268 if (m_Height < m_MinHeight) {
269 if (m_MinHeightAlign != wxHTML_ALIGN_TOP) {
270 int diff = m_MinHeight - m_Height;
271 if (m_MinHeightAlign == wxHTML_ALIGN_CENTER) diff /= 2;
272 cell = m_Cells;
273 while (cell) {
274 cell -> SetPos(cell -> GetPosX(), cell -> GetPosY() + diff);
275 cell = cell -> GetNext();
276 }
277 }
278 m_Height = m_MinHeight;
279 }
280
281 m_MaxLineWidth += s_indent + ((m_IndentRight < 0) ? (-m_IndentRight * m_Width / 100) : m_IndentRight);
282 if (m_Width < m_MaxLineWidth) m_Width = m_MaxLineWidth;
283
284 wxHtmlCell::Layout(w);
285 }
286
287
288 #define mMin(a, b) (((a) < (b)) ? (a) : (b))
289 #define mMax(a, b) (((a) < (b)) ? (b) : (a))
290
291 void wxHtmlContainerCell::Draw(wxDC& dc, int x, int y, int view_y1, int view_y2)
292 {
293 // container visible, draw it:
294 if ((y + m_PosY < view_y2) && (y + m_PosY + m_Height > view_y1)) {
295
296 if (m_UseBkColour) {
297 wxBrush myb = wxBrush(m_BkColour, wxSOLID);
298
299 int real_y1 = mMax(y + m_PosY, view_y1);
300 int real_y2 = mMin(y + m_PosY + m_Height - 1, view_y2);
301
302 dc.SetBrush(myb);
303 dc.SetPen(*wxTRANSPARENT_PEN);
304 dc.DrawRectangle(x + m_PosX, real_y1, m_Width, real_y2 - real_y1 + 1);
305 }
306
307 if (m_UseBorder) {
308 wxPen mypen1(m_BorderColour1, 1, wxSOLID);
309 wxPen mypen2(m_BorderColour2, 1, wxSOLID);
310
311 dc.SetPen(mypen1);
312 dc.DrawLine(x + m_PosX, y + m_PosY, x + m_PosX, y + m_PosY + m_Height - 1);
313 dc.DrawLine(x + m_PosX, y + m_PosY, x + m_PosX + m_Width - 1, y + m_PosY);
314 dc.SetPen(mypen2);
315 dc.DrawLine(x + m_PosX + m_Width - 1, y + m_PosY, x + m_PosX + m_Width - 1, y + m_PosY + m_Height - 1);
316 dc.DrawLine(x + m_PosX, y + m_PosY + m_Height - 1, x + m_PosX + m_Width - 1, y + m_PosY + m_Height - 1);
317 }
318
319 if (m_Cells) m_Cells -> Draw(dc, x + m_PosX, y + m_PosY, view_y1, view_y2);
320 }
321 // container invisible, just proceed font+color changing:
322 else {
323 if (m_Cells) m_Cells -> DrawInvisible(dc, x + m_PosX, y + m_PosY);
324 }
325
326 wxHtmlCell::Draw(dc, x, y, view_y1, view_y2);
327 }
328
329
330
331 void wxHtmlContainerCell::DrawInvisible(wxDC& dc, int x, int y)
332 {
333 if (m_Cells) m_Cells -> DrawInvisible(dc, x + m_PosX, y + m_PosY);
334 wxHtmlCell::DrawInvisible(dc, x, y);
335 }
336
337
338
339 wxString wxHtmlContainerCell::GetLink(int x, int y) const
340 {
341 wxHtmlCell *c = m_Cells;
342 int cx, cy, cw, ch;
343
344 while (c) {
345 cx = c -> GetPosX(), cy = c -> GetPosY();
346 cw = c -> GetWidth(), ch = c -> GetHeight();
347 if ((x >= cx) && (x < cx + cw) && (y >= cy) && (y < cy + ch))
348 return c -> GetLink(x - cx, y - cy);
349 c = c -> GetNext();
350 }
351 return wxEmptyString;
352 }
353
354
355
356 void wxHtmlContainerCell::InsertCell(wxHtmlCell *f)
357 {
358 if (!m_Cells) m_Cells = m_LastCell = f;
359 else {
360 m_LastCell -> SetNext(f);
361 m_LastCell = f;
362 if (m_LastCell) while (m_LastCell -> GetNext()) m_LastCell = m_LastCell -> GetNext();
363 }
364 f -> SetParent(this);
365 }
366
367
368
369 void wxHtmlContainerCell::SetAlign(const wxHtmlTag& tag)
370 {
371 if (tag.HasParam("ALIGN")) {
372 wxString alg = tag.GetParam("ALIGN");
373 alg.MakeUpper();
374 if (alg == "CENTER")
375 SetAlignHor(wxHTML_ALIGN_CENTER);
376 else if (alg == "LEFT")
377 SetAlignHor(wxHTML_ALIGN_LEFT);
378 else if (alg == "RIGHT")
379 SetAlignHor(wxHTML_ALIGN_RIGHT);
380 }
381 }
382
383
384
385 void wxHtmlContainerCell::SetWidthFloat(const wxHtmlTag& tag)
386 {
387 if (tag.HasParam("WIDTH")) {
388 int wdi;
389 wxString wd = tag.GetParam("WIDTH");
390
391 if (wd[wd.Length()-1] == '%') {
392 wxSscanf(wd.c_str(), wxT("%i%%"), &wdi);
393 SetWidthFloat(wdi, wxHTML_UNITS_PERCENT);
394 }
395 else {
396 wxSscanf(wd.c_str(), wxT("%i"), &wdi);
397 SetWidthFloat(wdi, wxHTML_UNITS_PIXELS);
398 }
399 }
400 }
401
402
403
404 const wxHtmlCell* wxHtmlContainerCell::Find(int condition, const void* param) const
405 {
406 const wxHtmlCell *r = NULL;
407
408 if (m_Cells) {
409 r = m_Cells -> Find(condition, param);
410 if (r) return r;
411 }
412
413 return wxHtmlCell::Find(condition, param);
414 }
415
416
417
418 void wxHtmlContainerCell::OnMouseClick(wxWindow *parent, int x, int y, bool left, bool middle, bool right)
419 {
420 if (m_Cells) {
421 wxHtmlCell *c = m_Cells;
422 while (c) {
423 if ( (c -> GetPosX() <= x) &&
424 (c -> GetPosY() <= y) &&
425 (c -> GetPosX() + c -> GetWidth() > x) &&
426 (c -> GetPosY() + c -> GetHeight() > y)) {
427 c -> OnMouseClick(parent, x - c -> GetPosX(), y - c -> GetPosY(), left, middle, right);
428 break;
429 }
430 c = c -> GetNext();
431 }
432 }
433 }
434
435
436
437
438
439 //--------------------------------------------------------------------------------
440 // wxHtmlColourCell
441 //--------------------------------------------------------------------------------
442
443 void wxHtmlColourCell::Draw(wxDC& dc, int x, int y, int view_y1, int view_y2)
444 {
445 if (m_Flags & wxHTML_CLR_FOREGROUND)
446 dc.SetTextForeground(m_Colour);
447 if (m_Flags & wxHTML_CLR_BACKGROUND) {
448 dc.SetBackground(wxBrush(m_Colour, wxSOLID));
449 dc.SetTextBackground(m_Colour);
450 }
451 wxHtmlCell::Draw(dc, x, y, view_y1, view_y2);
452 }
453
454 void wxHtmlColourCell::DrawInvisible(wxDC& dc, int x, int y)
455 {
456 if (m_Flags & wxHTML_CLR_FOREGROUND)
457 dc.SetTextForeground(m_Colour);
458 if (m_Flags & wxHTML_CLR_BACKGROUND) {
459 dc.SetBackground(wxBrush(m_Colour, wxSOLID));
460 dc.SetTextBackground(m_Colour);
461 }
462 wxHtmlCell::DrawInvisible(dc, x, y);
463 }
464
465
466
467
468 //--------------------------------------------------------------------------------
469 // wxHtmlFontCell
470 //--------------------------------------------------------------------------------
471
472 void wxHtmlFontCell::Draw(wxDC& dc, int x, int y, int view_y1, int view_y2)
473 {
474 dc.SetFont(*m_Font);
475 wxHtmlCell::Draw(dc, x, y, view_y1, view_y2);
476 }
477
478 void wxHtmlFontCell::DrawInvisible(wxDC& dc, int x, int y)
479 {
480 dc.SetFont(*m_Font);
481 wxHtmlCell::DrawInvisible(dc, x, y);
482 }
483
484
485
486
487
488
489
490
491 //--------------------------------------------------------------------------------
492 // wxHtmlWidgetCell
493 //--------------------------------------------------------------------------------
494
495 wxHtmlWidgetCell::wxHtmlWidgetCell(wxWindow *wnd, int w)
496 {
497 int sx, sy;
498 m_Wnd = wnd;
499 m_Wnd -> GetSize(&sx, &sy);
500 m_Width = sx, m_Height = sy;
501 m_WidthFloat = w;
502 }
503
504
505 void wxHtmlWidgetCell::Draw(wxDC& dc, int x, int y, int view_y1, int view_y2)
506 {
507 int absx = 0, absy = 0, stx, sty;
508 wxHtmlCell *c = this;
509
510 while (c) {
511 absx += c -> GetPosX();
512 absy += c -> GetPosY();
513 c = c -> GetParent();
514 }
515
516 ((wxScrolledWindow*)(m_Wnd -> GetParent())) -> ViewStart(&stx, &sty);
517 m_Wnd -> SetSize(absx - wxHTML_SCROLL_STEP * stx, absy - wxHTML_SCROLL_STEP * sty, m_Width, m_Height);
518
519 wxHtmlCell::Draw(dc, x, y, view_y1, view_y2);
520 }
521
522
523
524 void wxHtmlWidgetCell::DrawInvisible(wxDC& dc, int x, int y)
525 {
526 int absx = 0, absy = 0, stx, sty;
527 wxHtmlCell *c = this;
528
529 while (c) {
530 absx += c -> GetPosX();
531 absy += c -> GetPosY();
532 c = c -> GetParent();
533 }
534
535 ((wxScrolledWindow*)(m_Wnd -> GetParent())) -> ViewStart(&stx, &sty);
536 m_Wnd -> SetSize(absx - wxHTML_SCROLL_STEP * stx, absy - wxHTML_SCROLL_STEP * sty, m_Width, m_Height);
537
538 wxHtmlCell::DrawInvisible(dc, x, y);
539 }
540
541
542
543 void wxHtmlWidgetCell::Layout(int w)
544 {
545 if (m_WidthFloat != 0) {
546 m_Width = (w * m_WidthFloat) / 100;
547 m_Wnd -> SetSize(m_Width, m_Height);
548 }
549
550 wxHtmlCell::Layout(w);
551 }
552
553 #endif