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