]> git.saurik.com Git - wxWidgets.git/blob - src/html/htmlcell.cpp
added wxEncodingConverter - scripts for creating convertion tables
[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 wxHtmlCell::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
45 wxHtmlCell::~wxHtmlCell()
46 {
47 if (m_Link) delete m_Link;
48 if (m_Next) delete m_Next;
49 }
50
51
52 void wxHtmlCell::OnMouseClick(wxWindow *parent, int x, int y,
53 bool WXUNUSED(left),
54 bool WXUNUSED(middle),
55 bool WXUNUSED(right))
56 {
57 wxHtmlLinkInfo *lnk = GetLink(x, y);
58 if (lnk != NULL)
59 ((wxHtmlWindow*)parent) -> OnLinkClicked(lnk);
60 // note : this overcasting is legal because parent is *always* wxHtmlWindow
61 }
62
63
64
65 bool wxHtmlCell::AdjustPagebreak(int *pagebreak)
66 {
67 if ((!m_CanLiveOnPagebreak) &&
68 m_PosY < *pagebreak && m_PosY + m_Height > *pagebreak) {
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
82 void wxHtmlCell::SetLink(const wxHtmlLinkInfo& link)
83 {
84 if (m_Link) delete m_Link;
85 m_Link = new wxHtmlLinkInfo(link);
86 }
87
88
89
90 //-----------------------------------------------------------------------------
91 // wxHtmlWordCell
92 //-----------------------------------------------------------------------------
93
94 wxHtmlWordCell::wxHtmlWordCell(const wxString& word, wxDC& dc) : wxHtmlCell()
95 {
96 m_Word = word;
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 }
117
118 dc.GetTextExtent(m_Word, &m_Width, &m_Height, &m_Descent);
119 SetCanLiveOnPagebreak(FALSE);
120 }
121
122
123
124 void 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
132 //-----------------------------------------------------------------------------
133 // wxHtmlContainerCell
134 //-----------------------------------------------------------------------------
135
136
137 wxHtmlContainerCell::wxHtmlContainerCell(wxHtmlContainerCell *parent) : wxHtmlCell()
138 {
139 m_Cells = m_LastCell = NULL;
140 m_Parent = parent;
141 if (m_Parent) m_Parent -> InsertCell(this);
142 m_AlignHor = wxHTML_ALIGN_LEFT;
143 m_AlignVer = wxHTML_ALIGN_BOTTOM;
144 m_IndentLeft = m_IndentRight = m_IndentTop = m_IndentBottom = 0;
145 m_WidthFloat = 100; m_WidthFloatUnits = wxHTML_UNITS_PERCENT;
146 m_UseBkColour = FALSE;
147 m_UseBorder = FALSE;
148 m_MinHeight = m_MaxLineWidth = 0;
149 m_MinHeightAlign = wxHTML_ALIGN_TOP;
150 }
151
152
153
154 void wxHtmlContainerCell::SetIndent(int i, int what, int units)
155 {
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;
161 }
162
163
164
165 int wxHtmlContainerCell::GetIndent(int ind) const
166 {
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;
171 else return -1; /* BUG! Should not be called... */
172 }
173
174
175
176
177 int wxHtmlContainerCell::GetIndentUnits(int ind) const
178 {
179 bool p = FALSE;
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;
186 }
187
188
189
190 bool wxHtmlContainerCell::AdjustPagebreak(int *pagebreak)
191 {
192 if (!m_CanLiveOnPagebreak)
193 return wxHtmlCell::AdjustPagebreak(pagebreak);
194
195 else {
196 wxHtmlCell *c = GetFirstCell();
197 bool rt = FALSE;
198 int pbrk = *pagebreak - m_PosY;
199
200 while (c) {
201 if (c -> AdjustPagebreak(&pbrk)) rt = TRUE;
202 c = c -> GetNext();
203 }
204 if (rt) *pagebreak = pbrk + m_PosY;
205 return rt;
206 }
207 }
208
209
210
211 void 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 /*
220
221 WIDTH ADJUSTING :
222
223 */
224
225 if (m_WidthFloatUnits == wxHTML_UNITS_PERCENT) {
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 :
243
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;
251
252 // my own layouting:
253 while (cell != NULL) {
254 switch (m_AlignVer) {
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;
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) {
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;
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) {
298 if (m_MinHeightAlign != wxHTML_ALIGN_TOP) {
299 int diff = m_MinHeight - m_Height;
300 if (m_MinHeightAlign == wxHTML_ALIGN_CENTER) diff /= 2;
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
320 void 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
360 void 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
368 wxHtmlLinkInfo *wxHtmlContainerCell::GetLink(int x, int y) const
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 }
380 return NULL;
381 }
382
383
384
385 void 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
398 void wxHtmlContainerCell::SetAlign(const wxHtmlTag& tag)
399 {
400 if (tag.HasParam("ALIGN")) {
401 wxString alg = tag.GetParam("ALIGN");
402 alg.MakeUpper();
403 if (alg == "CENTER")
404 SetAlignHor(wxHTML_ALIGN_CENTER);
405 else if (alg == "LEFT")
406 SetAlignHor(wxHTML_ALIGN_LEFT);
407 else if (alg == "RIGHT")
408 SetAlignHor(wxHTML_ALIGN_RIGHT);
409 }
410 }
411
412
413
414 void wxHtmlContainerCell::SetWidthFloat(const wxHtmlTag& tag, double pixel_scale)
415 {
416 if (tag.HasParam("WIDTH")) {
417 int wdi;
418 wxString wd = tag.GetParam("WIDTH");
419
420 if (wd[wd.Length()-1] == '%') {
421 wxSscanf(wd.c_str(), wxT("%i%%"), &wdi);
422 SetWidthFloat(wdi, wxHTML_UNITS_PERCENT);
423 }
424 else {
425 wxSscanf(wd.c_str(), wxT("%i"), &wdi);
426 SetWidthFloat((int)(pixel_scale * (double)wdi), wxHTML_UNITS_PIXELS);
427 }
428 }
429 }
430
431
432
433 const wxHtmlCell* wxHtmlContainerCell::Find(int condition, const void* param) const
434 {
435 const wxHtmlCell *r = NULL;
436
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
447 void 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
472 void wxHtmlColourCell::Draw(wxDC& dc, int x, int y, int view_y1, int view_y2)
473 {
474 if (m_Flags & wxHTML_CLR_FOREGROUND)
475 dc.SetTextForeground(m_Colour);
476 if (m_Flags & wxHTML_CLR_BACKGROUND) {
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
483 void wxHtmlColourCell::DrawInvisible(wxDC& dc, int x, int y)
484 {
485 if (m_Flags & wxHTML_CLR_FOREGROUND)
486 dc.SetTextForeground(m_Colour);
487 if (m_Flags & wxHTML_CLR_BACKGROUND) {
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
501 void wxHtmlFontCell::Draw(wxDC& dc, int x, int y, int view_y1, int view_y2)
502 {
503 dc.SetFont(m_Font);
504 wxHtmlCell::Draw(dc, x, y, view_y1, view_y2);
505 }
506
507 void wxHtmlFontCell::DrawInvisible(wxDC& dc, int x, int y)
508 {
509 dc.SetFont(m_Font);
510 wxHtmlCell::DrawInvisible(dc, x, y);
511 }
512
513
514
515
516
517
518
519
520 //--------------------------------------------------------------------------------
521 // wxHtmlWidgetCell
522 //--------------------------------------------------------------------------------
523
524 wxHtmlWidgetCell::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
534 void 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);
546 m_Wnd -> SetSize(absx - wxHTML_SCROLL_STEP * stx, absy - wxHTML_SCROLL_STEP * sty, m_Width, m_Height);
547
548 wxHtmlCell::Draw(dc, x, y, view_y1, view_y2);
549 }
550
551
552
553 void 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 }
563
564 ((wxScrolledWindow*)(m_Wnd -> GetParent())) -> ViewStart(&stx, &sty);
565 m_Wnd -> SetSize(absx - wxHTML_SCROLL_STEP * stx, absy - wxHTML_SCROLL_STEP * sty, m_Width, m_Height);
566
567 wxHtmlCell::DrawInvisible(dc, x, y);
568 }
569
570
571
572 void 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