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