]> git.saurik.com Git - wxWidgets.git/blame - src/html/htmlcell.cpp
Removed some debug code
[wxWidgets.git] / src / html / htmlcell.cpp
CommitLineData
5526e819
VS
1/////////////////////////////////////////////////////////////////////////////
2// Name: htmlcell.cpp
3// Purpose: wxHtmlCell - basic element of HTML output
4// Author: Vaclav Slavik
69941f05 5// RCS-ID: $Id$
5526e819
VS
6// Copyright: (c) 1999 Vaclav Slavik
7// Licence: wxWindows Licence
8/////////////////////////////////////////////////////////////////////////////
9
5526e819 10#ifdef __GNUG__
69941f05 11#pragma implementation
5526e819
VS
12#endif
13
4dcaf11a 14#include "wx/wxprec.h"
5526e819 15
314260fb 16#include "wx/defs.h"
5526e819
VS
17#if wxUSE_HTML
18
19#ifdef __BORDLANDC__
20#pragma hdrstop
21#endif
22
23#ifndef WXPRECOMP
4dcaf11a 24#include "wx/wx.h"
5526e819
VS
25#endif
26
4dcaf11a
RR
27#include "wx/html/htmlcell.h"
28#include "wx/html/htmlwin.h"
5526e819
VS
29#include <stdlib.h>
30
31
5526e819
VS
32//-----------------------------------------------------------------------------
33// wxHtmlCell
34//-----------------------------------------------------------------------------
35
36
964688d8
VZ
37void wxHtmlCell::OnMouseClick(wxWindow *parent, int x, int y,
38 bool WXUNUSED(left),
39 bool WXUNUSED(middle),
40 bool WXUNUSED(right))
5526e819 41{
25082126
VS
42 wxString lnk = GetLink(x, y);
43 if (lnk != wxEmptyString)
44 ((wxHtmlWindow*)parent) -> OnLinkClicked(lnk);
5526e819
VS
45 // note : this overcasting is legal because parent is *always* wxHtmlWindow
46}
47
48
49
db98870d
VS
50bool wxHtmlCell::AdjustPagebreak(int *pagebreak)
51{
52
53 if ((!m_CanLiveOnPagebreak) &&
e52d6dbc 54 m_PosY < *pagebreak && m_PosY + m_Height > *pagebreak) {
db98870d
VS
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
5526e819
VS
69//-----------------------------------------------------------------------------
70// wxHtmlWordCell
71//-----------------------------------------------------------------------------
72
73wxHtmlWordCell::wxHtmlWordCell(const wxString& word, wxDC& dc) : wxHtmlCell()
74{
75 m_Word = word;
12909796 76
66a77a74
OK
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);
12909796
VS
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
5526e819 89 dc.GetTextExtent(m_Word, &m_Width, &m_Height, &m_Descent);
db98870d 90 SetCanLiveOnPagebreak(FALSE);
5526e819
VS
91}
92
93
94
95void 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
5526e819
VS
103//-----------------------------------------------------------------------------
104// wxHtmlContainerCell
105//-----------------------------------------------------------------------------
106
107
108wxHtmlContainerCell::wxHtmlContainerCell(wxHtmlContainerCell *parent) : wxHtmlCell()
109{
110 m_Cells = m_LastCell = NULL;
111 m_Parent = parent;
112 if (m_Parent) m_Parent -> InsertCell(this);
efba2b89
VS
113 m_AlignHor = wxHTML_ALIGN_LEFT;
114 m_AlignVer = wxHTML_ALIGN_BOTTOM;
5526e819 115 m_IndentLeft = m_IndentRight = m_IndentTop = m_IndentBottom = 0;
efba2b89 116 m_WidthFloat = 100; m_WidthFloatUnits = wxHTML_UNITS_PERCENT;
5526e819
VS
117 m_UseBkColour = FALSE;
118 m_UseBorder = FALSE;
119 m_MinHeight = m_MaxLineWidth = 0;
efba2b89 120 m_MinHeightAlign = wxHTML_ALIGN_TOP;
5526e819
VS
121}
122
123
124
125void wxHtmlContainerCell::SetIndent(int i, int what, int units)
126{
efba2b89
VS
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;
5526e819
VS
132}
133
134
135
136int wxHtmlContainerCell::GetIndent(int ind) const
137{
efba2b89
VS
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;
5526e819
VS
142 else return -1; /* BUG! Should not be called... */
143}
144
145
146
147
148int wxHtmlContainerCell::GetIndentUnits(int ind) const
149{
150 bool p = FALSE;
efba2b89
VS
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;
5526e819
VS
157}
158
159
160
db98870d
VS
161bool wxHtmlContainerCell::AdjustPagebreak(int *pagebreak)
162{
163 if (!m_CanLiveOnPagebreak)
164 return wxHtmlCell::AdjustPagebreak(pagebreak);
e52d6dbc 165
db98870d
VS
166 else {
167 wxHtmlCell *c = GetFirstCell();
168 bool rt = FALSE;
e52d6dbc 169 int pbrk = *pagebreak - m_PosY;
db98870d
VS
170
171 while (c) {
e52d6dbc 172 if (c -> AdjustPagebreak(&pbrk)) rt = TRUE;
db98870d
VS
173 c = c -> GetNext();
174 }
e52d6dbc 175 if (rt) *pagebreak = pbrk + m_PosY;
db98870d
VS
176 return rt;
177 }
178}
179
180
181
5526e819
VS
182void 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 /*
7e941458 191
5526e819 192 WIDTH ADJUSTING :
7e941458 193
5526e819
VS
194 */
195
efba2b89 196 if (m_WidthFloatUnits == wxHTML_UNITS_PERCENT) {
5526e819
VS
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 :
7e941458 214
5526e819
VS
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;
7e941458 222
5526e819
VS
223 // my own layouting:
224 while (cell != NULL) {
225 switch (m_AlignVer) {
efba2b89
VS
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;
5526e819
VS
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) {
efba2b89
VS
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;
5526e819
VS
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) {
efba2b89 269 if (m_MinHeightAlign != wxHTML_ALIGN_TOP) {
5526e819 270 int diff = m_MinHeight - m_Height;
efba2b89 271 if (m_MinHeightAlign == wxHTML_ALIGN_CENTER) diff /= 2;
5526e819
VS
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
291void 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
331void 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
339wxString 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
356void 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
369void wxHtmlContainerCell::SetAlign(const wxHtmlTag& tag)
370{
371 if (tag.HasParam("ALIGN")) {
372 wxString alg = tag.GetParam("ALIGN");
373 alg.MakeUpper();
374 if (alg == "CENTER")
efba2b89 375 SetAlignHor(wxHTML_ALIGN_CENTER);
9b64e798 376 else if (alg == "LEFT")
efba2b89 377 SetAlignHor(wxHTML_ALIGN_LEFT);
9b64e798 378 else if (alg == "RIGHT")
efba2b89 379 SetAlignHor(wxHTML_ALIGN_RIGHT);
5526e819
VS
380 }
381}
382
383
384
385void 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] == '%') {
66a77a74 392 wxSscanf(wd.c_str(), wxT("%i%%"), &wdi);
efba2b89 393 SetWidthFloat(wdi, wxHTML_UNITS_PERCENT);
5526e819
VS
394 }
395 else {
66a77a74 396 wxSscanf(wd.c_str(), wxT("%i"), &wdi);
efba2b89 397 SetWidthFloat(wdi, wxHTML_UNITS_PIXELS);
5526e819
VS
398 }
399 }
400}
401
402
403
404const wxHtmlCell* wxHtmlContainerCell::Find(int condition, const void* param) const
405{
406 const wxHtmlCell *r = NULL;
7e941458 407
5526e819
VS
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
418void 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
443void wxHtmlColourCell::Draw(wxDC& dc, int x, int y, int view_y1, int view_y2)
444{
efba2b89 445 if (m_Flags & wxHTML_CLR_FOREGROUND)
5526e819 446 dc.SetTextForeground(m_Colour);
efba2b89 447 if (m_Flags & wxHTML_CLR_BACKGROUND) {
5526e819
VS
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
454void wxHtmlColourCell::DrawInvisible(wxDC& dc, int x, int y)
455{
efba2b89 456 if (m_Flags & wxHTML_CLR_FOREGROUND)
5526e819 457 dc.SetTextForeground(m_Colour);
efba2b89 458 if (m_Flags & wxHTML_CLR_BACKGROUND) {
5526e819
VS
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
472void 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
478void 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
495wxHtmlWidgetCell::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
505void 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);
efba2b89 517 m_Wnd -> SetSize(absx - wxHTML_SCROLL_STEP * stx, absy - wxHTML_SCROLL_STEP * sty, m_Width, m_Height);
5526e819
VS
518
519 wxHtmlCell::Draw(dc, x, y, view_y1, view_y2);
520}
521
522
523
524void 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 }
7e941458 534
5526e819 535 ((wxScrolledWindow*)(m_Wnd -> GetParent())) -> ViewStart(&stx, &sty);
efba2b89 536 m_Wnd -> SetSize(absx - wxHTML_SCROLL_STEP * stx, absy - wxHTML_SCROLL_STEP * sty, m_Width, m_Height);
ed673c6a 537
5526e819
VS
538 wxHtmlCell::DrawInvisible(dc, x, y);
539}
540
541
542
543void 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