]>
Commit | Line | Data |
---|---|---|
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 "htmlcell.h" | |
12 | #endif | |
13 | ||
14 | #include "wx/wxprec.h" | |
15 | ||
16 | #include "wx/defs.h" | |
17 | ||
18 | #if wxUSE_HTML && wxUSE_STREAMS | |
19 | ||
20 | #ifdef __BORLANDC__ | |
21 | #pragma hdrstop | |
22 | #endif | |
23 | ||
24 | #ifndef WXPRECOMP | |
25 | #include "wx/brush.h" | |
26 | #include "wx/colour.h" | |
27 | #include "wx/dc.h" | |
28 | #endif | |
29 | ||
30 | #include "wx/html/htmlcell.h" | |
31 | #include "wx/html/htmlwin.h" | |
32 | #include "wx/settings.h" | |
33 | #include <stdlib.h> | |
34 | ||
35 | //----------------------------------------------------------------------------- | |
36 | // Helper classes | |
37 | //----------------------------------------------------------------------------- | |
38 | ||
39 | void wxHtmlSelection::Set(const wxPoint& fromPos, wxHtmlCell *fromCell, | |
40 | const wxPoint& toPos, wxHtmlCell *toCell) | |
41 | { | |
42 | m_fromCell = fromCell; | |
43 | m_toCell = toCell; | |
44 | m_fromPos = fromPos; | |
45 | m_toPos = toPos; | |
46 | } | |
47 | ||
48 | void wxHtmlSelection::Set(wxHtmlCell *fromCell, wxHtmlCell *toCell) | |
49 | { | |
50 | wxPoint p1 = fromCell ? fromCell->GetAbsPos() : wxDefaultPosition; | |
51 | wxPoint p2 = toCell ? toCell->GetAbsPos() : wxDefaultPosition; | |
52 | if ( toCell ) | |
53 | { | |
54 | p2.x += toCell->GetWidth()-1; | |
55 | p2.y += toCell->GetHeight()-1; | |
56 | } | |
57 | Set(p1, fromCell, p2, toCell); | |
58 | } | |
59 | ||
60 | //----------------------------------------------------------------------------- | |
61 | // wxHtmlCell | |
62 | //----------------------------------------------------------------------------- | |
63 | ||
64 | wxHtmlCell::wxHtmlCell() : wxObject() | |
65 | { | |
66 | m_Next = NULL; | |
67 | m_Parent = NULL; | |
68 | m_Width = m_Height = m_Descent = 0; | |
69 | m_CanLiveOnPagebreak = TRUE; | |
70 | m_Link = NULL; | |
71 | } | |
72 | ||
73 | wxHtmlCell::~wxHtmlCell() | |
74 | { | |
75 | delete m_Link; | |
76 | } | |
77 | ||
78 | ||
79 | void wxHtmlCell::OnMouseClick(wxWindow *parent, int x, int y, | |
80 | const wxMouseEvent& event) | |
81 | { | |
82 | wxHtmlLinkInfo *lnk = GetLink(x, y); | |
83 | if (lnk != NULL) | |
84 | { | |
85 | wxHtmlLinkInfo lnk2(*lnk); | |
86 | lnk2.SetEvent(&event); | |
87 | lnk2.SetHtmlCell(this); | |
88 | ||
89 | // note : this cast is legal because parent is *always* wxHtmlWindow | |
90 | wxStaticCast(parent, wxHtmlWindow)->OnLinkClicked(lnk2); | |
91 | } | |
92 | } | |
93 | ||
94 | ||
95 | ||
96 | bool wxHtmlCell::AdjustPagebreak(int *pagebreak, int* WXUNUSED(known_pagebreaks), int WXUNUSED(number_of_pages)) const | |
97 | { | |
98 | if ((!m_CanLiveOnPagebreak) && | |
99 | m_PosY < *pagebreak && m_PosY + m_Height > *pagebreak) | |
100 | { | |
101 | *pagebreak = m_PosY; | |
102 | return TRUE; | |
103 | } | |
104 | ||
105 | return FALSE; | |
106 | } | |
107 | ||
108 | ||
109 | ||
110 | void wxHtmlCell::SetLink(const wxHtmlLinkInfo& link) | |
111 | { | |
112 | if (m_Link) delete m_Link; | |
113 | m_Link = NULL; | |
114 | if (link.GetHref() != wxEmptyString) | |
115 | m_Link = new wxHtmlLinkInfo(link); | |
116 | } | |
117 | ||
118 | ||
119 | ||
120 | void wxHtmlCell::Layout(int WXUNUSED(w)) | |
121 | { | |
122 | SetPos(0, 0); | |
123 | } | |
124 | ||
125 | ||
126 | ||
127 | void wxHtmlCell::GetHorizontalConstraints(int *left, int *right) const | |
128 | { | |
129 | if (left) | |
130 | *left = m_PosX; | |
131 | if (right) | |
132 | *right = m_PosX + m_Width; | |
133 | } | |
134 | ||
135 | ||
136 | ||
137 | const wxHtmlCell* wxHtmlCell::Find(int WXUNUSED(condition), const void* WXUNUSED(param)) const | |
138 | { | |
139 | return NULL; | |
140 | } | |
141 | ||
142 | ||
143 | wxHtmlCell *wxHtmlCell::FindCellByPos(wxCoord x, wxCoord y, | |
144 | unsigned flags) const | |
145 | { | |
146 | if ( x >= 0 && x < m_Width && y >= 0 && y < m_Height ) | |
147 | { | |
148 | return wxConstCast(this, wxHtmlCell); | |
149 | } | |
150 | else | |
151 | { | |
152 | if ((flags & wxHTML_FIND_NEAREST_AFTER) && | |
153 | (y < 0 || (y == 0 && x <= 0))) | |
154 | return wxConstCast(this, wxHtmlCell); | |
155 | else if ((flags & wxHTML_FIND_NEAREST_BEFORE) && | |
156 | (y > m_Height-1 || (y == m_Height-1 && x >= m_Width))) | |
157 | return wxConstCast(this, wxHtmlCell); | |
158 | else | |
159 | return NULL; | |
160 | } | |
161 | } | |
162 | ||
163 | ||
164 | wxPoint wxHtmlCell::GetAbsPos() const | |
165 | { | |
166 | wxPoint p(m_PosX, m_PosY); | |
167 | for (wxHtmlCell *parent = m_Parent; parent; parent = parent->m_Parent) | |
168 | { | |
169 | p.x += parent->m_PosX; | |
170 | p.y += parent->m_PosY; | |
171 | } | |
172 | return p; | |
173 | } | |
174 | ||
175 | unsigned wxHtmlCell::GetDepth() const | |
176 | { | |
177 | unsigned d = 0; | |
178 | for (wxHtmlCell *p = m_Parent; p; p = p->m_Parent) | |
179 | d++; | |
180 | return d; | |
181 | } | |
182 | ||
183 | bool wxHtmlCell::IsBefore(wxHtmlCell *cell) const | |
184 | { | |
185 | const wxHtmlCell *c1 = this; | |
186 | const wxHtmlCell *c2 = cell; | |
187 | unsigned d1 = GetDepth(); | |
188 | unsigned d2 = cell->GetDepth(); | |
189 | ||
190 | if ( d1 > d2 ) | |
191 | for (; d1 != d2; d1-- ) | |
192 | c1 = c1->m_Parent; | |
193 | else if ( d1 < d2 ) | |
194 | for (; d1 != d2; d2-- ) | |
195 | c2 = c2->m_Parent; | |
196 | ||
197 | if ( cell == this ) | |
198 | return true; | |
199 | ||
200 | while ( c1 && c2 ) | |
201 | { | |
202 | if ( c1->m_Parent == c2->m_Parent ) | |
203 | { | |
204 | while ( c1 ) | |
205 | { | |
206 | if ( c1 == c2 ) | |
207 | return true; | |
208 | c1 = c1->GetNext(); | |
209 | } | |
210 | return false; | |
211 | } | |
212 | else | |
213 | { | |
214 | c1 = c1->m_Parent; | |
215 | c2 = c2->m_Parent; | |
216 | } | |
217 | } | |
218 | ||
219 | wxFAIL_MSG(_T("Cells are in different trees")); | |
220 | return false; | |
221 | } | |
222 | ||
223 | ||
224 | //----------------------------------------------------------------------------- | |
225 | // wxHtmlWordCell | |
226 | //----------------------------------------------------------------------------- | |
227 | ||
228 | wxHtmlWordCell::wxHtmlWordCell(const wxString& word, wxDC& dc) : wxHtmlCell() | |
229 | { | |
230 | m_Word = word; | |
231 | dc.GetTextExtent(m_Word, &m_Width, &m_Height, &m_Descent); | |
232 | SetCanLiveOnPagebreak(FALSE); | |
233 | } | |
234 | ||
235 | ||
236 | ||
237 | void wxHtmlWordCell::Draw(wxDC& dc, int x, int y, | |
238 | int WXUNUSED(view_y1), int WXUNUSED(view_y2), | |
239 | wxHtmlRenderingState& state) | |
240 | { | |
241 | if (state.GetSelectionState() != wxHTML_SEL_OUT && | |
242 | dc.GetBackgroundMode() != wxSOLID) | |
243 | { | |
244 | dc.SetBackgroundMode(wxSOLID); | |
245 | dc.SetTextBackground( | |
246 | wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHT)); | |
247 | dc.SetTextForeground( | |
248 | wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT)); | |
249 | } | |
250 | else if (state.GetSelectionState() == wxHTML_SEL_OUT && | |
251 | dc.GetBackgroundMode() == wxSOLID) | |
252 | { | |
253 | dc.SetBackgroundMode(wxTRANSPARENT); | |
254 | dc.SetTextForeground(state.GetFgColour()); | |
255 | dc.SetTextBackground(state.GetBgColour()); | |
256 | } | |
257 | ||
258 | // FIXME - use selection | |
259 | dc.DrawText(m_Word, x + m_PosX, y + m_PosY); | |
260 | } | |
261 | ||
262 | ||
263 | wxString wxHtmlWordCell::ConvertToText(wxHtmlSelection *sel) const | |
264 | { | |
265 | // FIXME - use selection | |
266 | return m_Word; | |
267 | } | |
268 | ||
269 | ||
270 | ||
271 | //----------------------------------------------------------------------------- | |
272 | // wxHtmlContainerCell | |
273 | //----------------------------------------------------------------------------- | |
274 | ||
275 | ||
276 | wxHtmlContainerCell::wxHtmlContainerCell(wxHtmlContainerCell *parent) : wxHtmlCell() | |
277 | { | |
278 | m_Cells = m_LastCell = NULL; | |
279 | m_Parent = parent; | |
280 | if (m_Parent) m_Parent->InsertCell(this); | |
281 | m_AlignHor = wxHTML_ALIGN_LEFT; | |
282 | m_AlignVer = wxHTML_ALIGN_BOTTOM; | |
283 | m_IndentLeft = m_IndentRight = m_IndentTop = m_IndentBottom = 0; | |
284 | m_WidthFloat = 100; m_WidthFloatUnits = wxHTML_UNITS_PERCENT; | |
285 | m_UseBkColour = FALSE; | |
286 | m_UseBorder = FALSE; | |
287 | m_MinHeight = 0; | |
288 | m_MinHeightAlign = wxHTML_ALIGN_TOP; | |
289 | m_LastLayout = -1; | |
290 | } | |
291 | ||
292 | wxHtmlContainerCell::~wxHtmlContainerCell() | |
293 | { | |
294 | wxHtmlCell *cell = m_Cells; | |
295 | while ( cell ) | |
296 | { | |
297 | wxHtmlCell *cellNext = cell->GetNext(); | |
298 | delete cell; | |
299 | cell = cellNext; | |
300 | } | |
301 | } | |
302 | ||
303 | ||
304 | ||
305 | void wxHtmlContainerCell::SetIndent(int i, int what, int units) | |
306 | { | |
307 | int val = (units == wxHTML_UNITS_PIXELS) ? i : -i; | |
308 | if (what & wxHTML_INDENT_LEFT) m_IndentLeft = val; | |
309 | if (what & wxHTML_INDENT_RIGHT) m_IndentRight = val; | |
310 | if (what & wxHTML_INDENT_TOP) m_IndentTop = val; | |
311 | if (what & wxHTML_INDENT_BOTTOM) m_IndentBottom = val; | |
312 | m_LastLayout = -1; | |
313 | } | |
314 | ||
315 | ||
316 | ||
317 | int wxHtmlContainerCell::GetIndent(int ind) const | |
318 | { | |
319 | if (ind & wxHTML_INDENT_LEFT) return m_IndentLeft; | |
320 | else if (ind & wxHTML_INDENT_RIGHT) return m_IndentRight; | |
321 | else if (ind & wxHTML_INDENT_TOP) return m_IndentTop; | |
322 | else if (ind & wxHTML_INDENT_BOTTOM) return m_IndentBottom; | |
323 | else return -1; /* BUG! Should not be called... */ | |
324 | } | |
325 | ||
326 | ||
327 | ||
328 | ||
329 | int wxHtmlContainerCell::GetIndentUnits(int ind) const | |
330 | { | |
331 | bool p = FALSE; | |
332 | if (ind & wxHTML_INDENT_LEFT) p = m_IndentLeft < 0; | |
333 | else if (ind & wxHTML_INDENT_RIGHT) p = m_IndentRight < 0; | |
334 | else if (ind & wxHTML_INDENT_TOP) p = m_IndentTop < 0; | |
335 | else if (ind & wxHTML_INDENT_BOTTOM) p = m_IndentBottom < 0; | |
336 | if (p) return wxHTML_UNITS_PERCENT; | |
337 | else return wxHTML_UNITS_PIXELS; | |
338 | } | |
339 | ||
340 | ||
341 | ||
342 | bool wxHtmlContainerCell::AdjustPagebreak(int *pagebreak, int* known_pagebreaks, int number_of_pages) const | |
343 | { | |
344 | if (!m_CanLiveOnPagebreak) | |
345 | return wxHtmlCell::AdjustPagebreak(pagebreak, known_pagebreaks, number_of_pages); | |
346 | ||
347 | else | |
348 | { | |
349 | wxHtmlCell *c = GetFirstChild(); | |
350 | bool rt = FALSE; | |
351 | int pbrk = *pagebreak - m_PosY; | |
352 | ||
353 | while (c) | |
354 | { | |
355 | if (c->AdjustPagebreak(&pbrk, known_pagebreaks, number_of_pages)) | |
356 | rt = TRUE; | |
357 | c = c->GetNext(); | |
358 | } | |
359 | if (rt) | |
360 | *pagebreak = pbrk + m_PosY; | |
361 | return rt; | |
362 | } | |
363 | } | |
364 | ||
365 | ||
366 | ||
367 | void wxHtmlContainerCell::Layout(int w) | |
368 | { | |
369 | wxHtmlCell::Layout(w); | |
370 | ||
371 | if (m_LastLayout == w) return; | |
372 | ||
373 | // VS: Any attempt to layout with negative or zero width leads to hell, | |
374 | // but we can't ignore such attempts completely, since it sometimes | |
375 | // happen (e.g. when trying how small a table can be). The best thing we | |
376 | // can do is to set the width of child cells to zero | |
377 | if (w < 1) | |
378 | { | |
379 | m_Width = 0; | |
380 | for (wxHtmlCell *cell = m_Cells; cell; cell = cell->GetNext()) | |
381 | cell->Layout(0); | |
382 | // this does two things: it recursively calls this code on all | |
383 | // child contrainers and resets children's position to (0,0) | |
384 | return; | |
385 | } | |
386 | ||
387 | wxHtmlCell *cell = m_Cells, *line = m_Cells; | |
388 | long xpos = 0, ypos = m_IndentTop; | |
389 | int xdelta = 0, ybasicpos = 0, ydiff; | |
390 | int s_width, s_indent; | |
391 | int ysizeup = 0, ysizedown = 0; | |
392 | int MaxLineWidth = 0; | |
393 | int xcnt = 0; | |
394 | ||
395 | ||
396 | /* | |
397 | ||
398 | WIDTH ADJUSTING : | |
399 | ||
400 | */ | |
401 | ||
402 | if (m_WidthFloatUnits == wxHTML_UNITS_PERCENT) | |
403 | { | |
404 | if (m_WidthFloat < 0) m_Width = (100 + m_WidthFloat) * w / 100; | |
405 | else m_Width = m_WidthFloat * w / 100; | |
406 | } | |
407 | else | |
408 | { | |
409 | if (m_WidthFloat < 0) m_Width = w + m_WidthFloat; | |
410 | else m_Width = m_WidthFloat; | |
411 | } | |
412 | ||
413 | if (m_Cells) | |
414 | { | |
415 | int l = (m_IndentLeft < 0) ? (-m_IndentLeft * m_Width / 100) : m_IndentLeft; | |
416 | int r = (m_IndentRight < 0) ? (-m_IndentRight * m_Width / 100) : m_IndentRight; | |
417 | for (wxHtmlCell *cell = m_Cells; cell; cell = cell->GetNext()) | |
418 | cell->Layout(m_Width - (l + r)); | |
419 | } | |
420 | ||
421 | /* | |
422 | ||
423 | LAYOUTING : | |
424 | ||
425 | */ | |
426 | ||
427 | // adjust indentation: | |
428 | s_indent = (m_IndentLeft < 0) ? (-m_IndentLeft * m_Width / 100) : m_IndentLeft; | |
429 | s_width = m_Width - s_indent - ((m_IndentRight < 0) ? (-m_IndentRight * m_Width / 100) : m_IndentRight); | |
430 | ||
431 | // my own layouting: | |
432 | while (cell != NULL) | |
433 | { | |
434 | switch (m_AlignVer) | |
435 | { | |
436 | case wxHTML_ALIGN_TOP : ybasicpos = 0; break; | |
437 | case wxHTML_ALIGN_BOTTOM : ybasicpos = - cell->GetHeight(); break; | |
438 | case wxHTML_ALIGN_CENTER : ybasicpos = - cell->GetHeight() / 2; break; | |
439 | } | |
440 | ydiff = cell->GetHeight() + ybasicpos; | |
441 | ||
442 | if (cell->GetDescent() + ydiff > ysizedown) ysizedown = cell->GetDescent() + ydiff; | |
443 | if (ybasicpos + cell->GetDescent() < -ysizeup) ysizeup = - (ybasicpos + cell->GetDescent()); | |
444 | ||
445 | cell->SetPos(xpos, ybasicpos + cell->GetDescent()); | |
446 | xpos += cell->GetWidth(); | |
447 | cell = cell->GetNext(); | |
448 | xcnt++; | |
449 | ||
450 | // force new line if occured: | |
451 | if ((cell == NULL) || (xpos + cell->GetWidth() > s_width)) | |
452 | { | |
453 | if (xpos > MaxLineWidth) MaxLineWidth = xpos; | |
454 | if (ysizeup < 0) ysizeup = 0; | |
455 | if (ysizedown < 0) ysizedown = 0; | |
456 | switch (m_AlignHor) { | |
457 | case wxHTML_ALIGN_LEFT : | |
458 | case wxHTML_ALIGN_JUSTIFY : | |
459 | xdelta = 0; | |
460 | break; | |
461 | case wxHTML_ALIGN_RIGHT : | |
462 | xdelta = 0 + (s_width - xpos); | |
463 | break; | |
464 | case wxHTML_ALIGN_CENTER : | |
465 | xdelta = 0 + (s_width - xpos) / 2; | |
466 | break; | |
467 | } | |
468 | if (xdelta < 0) xdelta = 0; | |
469 | xdelta += s_indent; | |
470 | ||
471 | ypos += ysizeup; | |
472 | ||
473 | if (m_AlignHor != wxHTML_ALIGN_JUSTIFY || cell == NULL) | |
474 | while (line != cell) | |
475 | { | |
476 | line->SetPos(line->GetPosX() + xdelta, | |
477 | ypos + line->GetPosY()); | |
478 | line = line->GetNext(); | |
479 | } | |
480 | else | |
481 | { | |
482 | int counter = 0; | |
483 | int step = (s_width - xpos); | |
484 | if (step < 0) step = 0; | |
485 | xcnt--; | |
486 | if (xcnt > 0) while (line != cell) | |
487 | { | |
488 | line->SetPos(line->GetPosX() + s_indent + | |
489 | (counter++ * step / xcnt), | |
490 | ypos + line->GetPosY()); | |
491 | line = line->GetNext(); | |
492 | } | |
493 | xcnt++; | |
494 | } | |
495 | ||
496 | ypos += ysizedown; | |
497 | xpos = xcnt = 0; | |
498 | ysizeup = ysizedown = 0; | |
499 | line = cell; | |
500 | } | |
501 | } | |
502 | ||
503 | // setup height & width, depending on container layout: | |
504 | m_Height = ypos + (ysizedown + ysizeup) + m_IndentBottom; | |
505 | ||
506 | if (m_Height < m_MinHeight) | |
507 | { | |
508 | if (m_MinHeightAlign != wxHTML_ALIGN_TOP) | |
509 | { | |
510 | int diff = m_MinHeight - m_Height; | |
511 | if (m_MinHeightAlign == wxHTML_ALIGN_CENTER) diff /= 2; | |
512 | cell = m_Cells; | |
513 | while (cell) | |
514 | { | |
515 | cell->SetPos(cell->GetPosX(), cell->GetPosY() + diff); | |
516 | cell = cell->GetNext(); | |
517 | } | |
518 | } | |
519 | m_Height = m_MinHeight; | |
520 | } | |
521 | ||
522 | MaxLineWidth += s_indent + ((m_IndentRight < 0) ? (-m_IndentRight * m_Width / 100) : m_IndentRight); | |
523 | if (m_Width < MaxLineWidth) m_Width = MaxLineWidth; | |
524 | ||
525 | m_LastLayout = w; | |
526 | } | |
527 | ||
528 | void wxHtmlContainerCell::UpdateRenderingStatePre(wxHtmlRenderingState& state, | |
529 | wxHtmlCell *cell) const | |
530 | { | |
531 | wxHtmlSelection *s = state.GetSelection(); | |
532 | if (!s) return; | |
533 | if (s->GetFromCell() == cell || s->GetToCell() == cell) | |
534 | { | |
535 | state.SetSelectionState(wxHTML_SEL_CHANGING); | |
536 | } | |
537 | } | |
538 | ||
539 | void wxHtmlContainerCell::UpdateRenderingStatePost(wxHtmlRenderingState& state, | |
540 | wxHtmlCell *cell) const | |
541 | { | |
542 | wxHtmlSelection *s = state.GetSelection(); | |
543 | if (!s) return; | |
544 | if (s->GetToCell() == cell) | |
545 | state.SetSelectionState(wxHTML_SEL_OUT); | |
546 | else if (s->GetFromCell() == cell) | |
547 | state.SetSelectionState(wxHTML_SEL_IN); | |
548 | } | |
549 | ||
550 | #define mMin(a, b) (((a) < (b)) ? (a) : (b)) | |
551 | #define mMax(a, b) (((a) < (b)) ? (b) : (a)) | |
552 | ||
553 | void wxHtmlContainerCell::Draw(wxDC& dc, int x, int y, int view_y1, int view_y2, | |
554 | wxHtmlRenderingState& state) | |
555 | { | |
556 | // container visible, draw it: | |
557 | if ((y + m_PosY <= view_y2) && (y + m_PosY + m_Height > view_y1)) | |
558 | { | |
559 | if (m_UseBkColour) | |
560 | { | |
561 | wxBrush myb = wxBrush(m_BkColour, wxSOLID); | |
562 | ||
563 | int real_y1 = mMax(y + m_PosY, view_y1); | |
564 | int real_y2 = mMin(y + m_PosY + m_Height - 1, view_y2); | |
565 | ||
566 | dc.SetBrush(myb); | |
567 | dc.SetPen(*wxTRANSPARENT_PEN); | |
568 | dc.DrawRectangle(x + m_PosX, real_y1, m_Width, real_y2 - real_y1 + 1); | |
569 | } | |
570 | ||
571 | if (m_UseBorder) | |
572 | { | |
573 | wxPen mypen1(m_BorderColour1, 1, wxSOLID); | |
574 | wxPen mypen2(m_BorderColour2, 1, wxSOLID); | |
575 | ||
576 | dc.SetPen(mypen1); | |
577 | dc.DrawLine(x + m_PosX, y + m_PosY, x + m_PosX, y + m_PosY + m_Height - 1); | |
578 | dc.DrawLine(x + m_PosX, y + m_PosY, x + m_PosX + m_Width, y + m_PosY); | |
579 | dc.SetPen(mypen2); | |
580 | dc.DrawLine(x + m_PosX + m_Width - 1, y + m_PosY, x + m_PosX + m_Width - 1, y + m_PosY + m_Height - 1); | |
581 | dc.DrawLine(x + m_PosX, y + m_PosY + m_Height - 1, x + m_PosX + m_Width, y + m_PosY + m_Height - 1); | |
582 | } | |
583 | ||
584 | if (m_Cells) | |
585 | { | |
586 | // draw container's contents: | |
587 | for (wxHtmlCell *cell = m_Cells; cell; cell = cell->GetNext()) | |
588 | { | |
589 | UpdateRenderingStatePre(state, cell); | |
590 | cell->Draw(dc, | |
591 | x + m_PosX, y + m_PosY, view_y1, view_y2, | |
592 | state); | |
593 | UpdateRenderingStatePost(state, cell); | |
594 | } | |
595 | } | |
596 | } | |
597 | // container invisible, just proceed font+color changing: | |
598 | else | |
599 | { | |
600 | DrawInvisible(dc, x, y, state); | |
601 | } | |
602 | } | |
603 | ||
604 | ||
605 | ||
606 | void wxHtmlContainerCell::DrawInvisible(wxDC& dc, int x, int y, | |
607 | wxHtmlRenderingState& state) | |
608 | { | |
609 | if (m_Cells) | |
610 | { | |
611 | for (wxHtmlCell *cell = m_Cells; cell; cell = cell->GetNext()) | |
612 | { | |
613 | UpdateRenderingStatePre(state, cell); | |
614 | cell->DrawInvisible(dc, x + m_PosX, y + m_PosY, state); | |
615 | UpdateRenderingStatePost(state, cell); | |
616 | } | |
617 | } | |
618 | } | |
619 | ||
620 | ||
621 | wxColour wxHtmlContainerCell::GetBackgroundColour() | |
622 | { | |
623 | if (m_UseBkColour) | |
624 | return m_BkColour; | |
625 | else | |
626 | return wxNullColour; | |
627 | } | |
628 | ||
629 | ||
630 | ||
631 | wxHtmlLinkInfo *wxHtmlContainerCell::GetLink(int x, int y) const | |
632 | { | |
633 | wxHtmlCell *cell = FindCellByPos(x, y); | |
634 | ||
635 | // VZ: I don't know if we should pass absolute or relative coords to | |
636 | // wxHtmlCell::GetLink()? As the base class version just ignores them | |
637 | // anyhow, it hardly matters right now but should still be clarified | |
638 | return cell ? cell->GetLink(x, y) : NULL; | |
639 | } | |
640 | ||
641 | ||
642 | ||
643 | void wxHtmlContainerCell::InsertCell(wxHtmlCell *f) | |
644 | { | |
645 | if (!m_Cells) m_Cells = m_LastCell = f; | |
646 | else | |
647 | { | |
648 | m_LastCell->SetNext(f); | |
649 | m_LastCell = f; | |
650 | if (m_LastCell) while (m_LastCell->GetNext()) m_LastCell = m_LastCell->GetNext(); | |
651 | } | |
652 | f->SetParent(this); | |
653 | m_LastLayout = -1; | |
654 | } | |
655 | ||
656 | ||
657 | ||
658 | void wxHtmlContainerCell::SetAlign(const wxHtmlTag& tag) | |
659 | { | |
660 | if (tag.HasParam(wxT("ALIGN"))) | |
661 | { | |
662 | wxString alg = tag.GetParam(wxT("ALIGN")); | |
663 | alg.MakeUpper(); | |
664 | if (alg == wxT("CENTER")) | |
665 | SetAlignHor(wxHTML_ALIGN_CENTER); | |
666 | else if (alg == wxT("LEFT")) | |
667 | SetAlignHor(wxHTML_ALIGN_LEFT); | |
668 | else if (alg == wxT("JUSTIFY")) | |
669 | SetAlignHor(wxHTML_ALIGN_JUSTIFY); | |
670 | else if (alg == wxT("RIGHT")) | |
671 | SetAlignHor(wxHTML_ALIGN_RIGHT); | |
672 | m_LastLayout = -1; | |
673 | } | |
674 | } | |
675 | ||
676 | ||
677 | ||
678 | void wxHtmlContainerCell::SetWidthFloat(const wxHtmlTag& tag, double pixel_scale) | |
679 | { | |
680 | if (tag.HasParam(wxT("WIDTH"))) | |
681 | { | |
682 | int wdi; | |
683 | wxString wd = tag.GetParam(wxT("WIDTH")); | |
684 | ||
685 | if (wd[wd.Length()-1] == wxT('%')) | |
686 | { | |
687 | wxSscanf(wd.c_str(), wxT("%i%%"), &wdi); | |
688 | SetWidthFloat(wdi, wxHTML_UNITS_PERCENT); | |
689 | } | |
690 | else | |
691 | { | |
692 | wxSscanf(wd.c_str(), wxT("%i"), &wdi); | |
693 | SetWidthFloat((int)(pixel_scale * (double)wdi), wxHTML_UNITS_PIXELS); | |
694 | } | |
695 | m_LastLayout = -1; | |
696 | } | |
697 | } | |
698 | ||
699 | ||
700 | ||
701 | const wxHtmlCell* wxHtmlContainerCell::Find(int condition, const void* param) const | |
702 | { | |
703 | if (m_Cells) | |
704 | { | |
705 | const wxHtmlCell *r = NULL; | |
706 | ||
707 | for (wxHtmlCell *cell = m_Cells; cell; cell = cell->GetNext()) | |
708 | { | |
709 | r = cell->Find(condition, param); | |
710 | if (r) return r; | |
711 | } | |
712 | } | |
713 | return NULL; | |
714 | } | |
715 | ||
716 | ||
717 | wxHtmlCell *wxHtmlContainerCell::FindCellByPos(wxCoord x, wxCoord y, | |
718 | unsigned flags) const | |
719 | { | |
720 | if ( flags & wxHTML_FIND_EXACT ) | |
721 | { | |
722 | for ( const wxHtmlCell *cell = m_Cells; cell; cell = cell->GetNext() ) | |
723 | { | |
724 | int cx = cell->GetPosX(), | |
725 | cy = cell->GetPosY(); | |
726 | ||
727 | if ( (cx <= x) && (cx + cell->GetWidth() > x) && | |
728 | (cy <= y) && (cy + cell->GetHeight() > y) ) | |
729 | { | |
730 | return cell->FindCellByPos(x - cx, y - cy, flags); | |
731 | } | |
732 | } | |
733 | } | |
734 | else if ( flags & wxHTML_FIND_NEAREST_AFTER ) | |
735 | { | |
736 | wxHtmlCell *c; | |
737 | int y2; | |
738 | for ( const wxHtmlCell *cell = m_Cells; cell; cell = cell->GetNext() ) | |
739 | { | |
740 | y2 = cell->GetPosY() + cell->GetHeight() - 1; | |
741 | if (y2 < y || (y2 == y && cell->GetPosX()+cell->GetWidth()-1 < x)) | |
742 | continue; | |
743 | c = cell->FindCellByPos(x - cell->GetPosX(), y - cell->GetPosY(), | |
744 | flags); | |
745 | if (c) return c; | |
746 | } | |
747 | } | |
748 | else if ( flags & wxHTML_FIND_NEAREST_BEFORE ) | |
749 | { | |
750 | wxHtmlCell *c2, *c = NULL; | |
751 | for ( const wxHtmlCell *cell = m_Cells; cell; cell = cell->GetNext() ) | |
752 | { | |
753 | if (cell->GetPosY() > y || | |
754 | (cell->GetPosY() == y && cell->GetPosX() > x)) | |
755 | break; | |
756 | c2 = cell->FindCellByPos(x - cell->GetPosX(), y - cell->GetPosY(), | |
757 | flags); | |
758 | if (c2) | |
759 | c = c2; | |
760 | } | |
761 | if (c) return c; | |
762 | } | |
763 | ||
764 | return NULL; | |
765 | } | |
766 | ||
767 | ||
768 | void wxHtmlContainerCell::OnMouseClick(wxWindow *parent, int x, int y, const wxMouseEvent& event) | |
769 | { | |
770 | wxHtmlCell *cell = FindCellByPos(x, y); | |
771 | if ( cell ) | |
772 | cell->OnMouseClick(parent, x, y, event); | |
773 | } | |
774 | ||
775 | ||
776 | ||
777 | void wxHtmlContainerCell::GetHorizontalConstraints(int *left, int *right) const | |
778 | { | |
779 | int cleft = m_PosX + m_Width, cright = m_PosX; // worst case | |
780 | int l, r; | |
781 | ||
782 | for (wxHtmlCell *cell = m_Cells; cell; cell = cell->GetNext()) | |
783 | { | |
784 | cell->GetHorizontalConstraints(&l, &r); | |
785 | if (l < cleft) | |
786 | cleft = l; | |
787 | if (r > cright) | |
788 | cright = r; | |
789 | } | |
790 | ||
791 | cleft -= (m_IndentLeft < 0) ? (-m_IndentLeft * m_Width / 100) : m_IndentLeft; | |
792 | cright += (m_IndentRight < 0) ? (-m_IndentRight * m_Width / 100) : m_IndentRight; | |
793 | ||
794 | if (left) | |
795 | *left = cleft; | |
796 | if (right) | |
797 | *right = cright; | |
798 | } | |
799 | ||
800 | ||
801 | wxHtmlCell *wxHtmlContainerCell::GetFirstTerminal() const | |
802 | { | |
803 | if ( m_Cells ) | |
804 | { | |
805 | wxHtmlCell *c2; | |
806 | for (wxHtmlCell *c = m_Cells; c; c = c->GetNext()) | |
807 | { | |
808 | c2 = c->GetFirstTerminal(); | |
809 | if ( c2 ) | |
810 | return c2; | |
811 | } | |
812 | } | |
813 | return NULL; | |
814 | } | |
815 | ||
816 | wxHtmlCell *wxHtmlContainerCell::GetLastTerminal() const | |
817 | { | |
818 | if ( m_Cells ) | |
819 | { | |
820 | // most common case first: | |
821 | wxHtmlCell *c = m_LastCell->GetLastTerminal(); | |
822 | if ( c ) | |
823 | return c; | |
824 | ||
825 | wxHtmlCell *c2 = NULL; | |
826 | for (c = m_Cells; c; c = c->GetNext()) | |
827 | c2 = c->GetLastTerminal(); | |
828 | return c2; | |
829 | } | |
830 | else | |
831 | return NULL; | |
832 | } | |
833 | ||
834 | ||
835 | ||
836 | ||
837 | // -------------------------------------------------------------------------- | |
838 | // wxHtmlColourCell | |
839 | // -------------------------------------------------------------------------- | |
840 | ||
841 | void wxHtmlColourCell::Draw(wxDC& dc, | |
842 | int x, int y, | |
843 | int WXUNUSED(view_y1), int WXUNUSED(view_y2), | |
844 | wxHtmlRenderingState& state) | |
845 | { | |
846 | DrawInvisible(dc, x, y, state); | |
847 | } | |
848 | ||
849 | void wxHtmlColourCell::DrawInvisible(wxDC& dc, | |
850 | int WXUNUSED(x), int WXUNUSED(y), | |
851 | wxHtmlRenderingState& state) | |
852 | { | |
853 | if (m_Flags & wxHTML_CLR_FOREGROUND) | |
854 | { | |
855 | state.SetFgColour(m_Colour); | |
856 | if (state.GetSelectionState() != wxHTML_SEL_IN) | |
857 | dc.SetTextForeground(m_Colour); | |
858 | } | |
859 | if (m_Flags & wxHTML_CLR_BACKGROUND) | |
860 | { | |
861 | state.SetBgColour(m_Colour); | |
862 | if (state.GetSelectionState() != wxHTML_SEL_IN) | |
863 | dc.SetTextBackground(m_Colour); | |
864 | dc.SetBackground(wxBrush(m_Colour, wxSOLID)); | |
865 | } | |
866 | } | |
867 | ||
868 | ||
869 | ||
870 | ||
871 | // --------------------------------------------------------------------------- | |
872 | // wxHtmlFontCell | |
873 | // --------------------------------------------------------------------------- | |
874 | ||
875 | void wxHtmlFontCell::Draw(wxDC& dc, | |
876 | int WXUNUSED(x), int WXUNUSED(y), | |
877 | int WXUNUSED(view_y1), int WXUNUSED(view_y2), | |
878 | wxHtmlRenderingState& WXUNUSED(state)) | |
879 | { | |
880 | dc.SetFont(m_Font); | |
881 | } | |
882 | ||
883 | void wxHtmlFontCell::DrawInvisible(wxDC& dc, int WXUNUSED(x), int WXUNUSED(y), | |
884 | wxHtmlRenderingState& WXUNUSED(state)) | |
885 | { | |
886 | dc.SetFont(m_Font); | |
887 | } | |
888 | ||
889 | ||
890 | ||
891 | ||
892 | ||
893 | ||
894 | ||
895 | ||
896 | // --------------------------------------------------------------------------- | |
897 | // wxHtmlWidgetCell | |
898 | // --------------------------------------------------------------------------- | |
899 | ||
900 | wxHtmlWidgetCell::wxHtmlWidgetCell(wxWindow *wnd, int w) | |
901 | { | |
902 | int sx, sy; | |
903 | m_Wnd = wnd; | |
904 | m_Wnd->GetSize(&sx, &sy); | |
905 | m_Width = sx, m_Height = sy; | |
906 | m_WidthFloat = w; | |
907 | } | |
908 | ||
909 | ||
910 | void wxHtmlWidgetCell::Draw(wxDC& WXUNUSED(dc), | |
911 | int WXUNUSED(x), int WXUNUSED(y), | |
912 | int WXUNUSED(view_y1), int WXUNUSED(view_y2), | |
913 | wxHtmlRenderingState& WXUNUSED(state)) | |
914 | { | |
915 | int absx = 0, absy = 0, stx, sty; | |
916 | wxHtmlCell *c = this; | |
917 | ||
918 | while (c) | |
919 | { | |
920 | absx += c->GetPosX(); | |
921 | absy += c->GetPosY(); | |
922 | c = c->GetParent(); | |
923 | } | |
924 | ||
925 | ((wxScrolledWindow*)(m_Wnd->GetParent()))->GetViewStart(&stx, &sty); | |
926 | m_Wnd->SetSize(absx - wxHTML_SCROLL_STEP * stx, absy - wxHTML_SCROLL_STEP * sty, m_Width, m_Height); | |
927 | } | |
928 | ||
929 | ||
930 | ||
931 | void wxHtmlWidgetCell::DrawInvisible(wxDC& WXUNUSED(dc), | |
932 | int WXUNUSED(x), int WXUNUSED(y), | |
933 | wxHtmlRenderingState& WXUNUSED(state)) | |
934 | { | |
935 | int absx = 0, absy = 0, stx, sty; | |
936 | wxHtmlCell *c = this; | |
937 | ||
938 | while (c) | |
939 | { | |
940 | absx += c->GetPosX(); | |
941 | absy += c->GetPosY(); | |
942 | c = c->GetParent(); | |
943 | } | |
944 | ||
945 | ((wxScrolledWindow*)(m_Wnd->GetParent()))->GetViewStart(&stx, &sty); | |
946 | m_Wnd->SetSize(absx - wxHTML_SCROLL_STEP * stx, absy - wxHTML_SCROLL_STEP * sty, m_Width, m_Height); | |
947 | } | |
948 | ||
949 | ||
950 | ||
951 | void wxHtmlWidgetCell::Layout(int w) | |
952 | { | |
953 | if (m_WidthFloat != 0) | |
954 | { | |
955 | m_Width = (w * m_WidthFloat) / 100; | |
956 | m_Wnd->SetSize(m_Width, m_Height); | |
957 | } | |
958 | ||
959 | wxHtmlCell::Layout(w); | |
960 | } | |
961 | ||
962 | ||
963 | ||
964 | // ---------------------------------------------------------------------------- | |
965 | // wxHtmlTerminalCellsInterator | |
966 | // ---------------------------------------------------------------------------- | |
967 | ||
968 | const wxHtmlCell* wxHtmlTerminalCellsInterator::operator++() | |
969 | { | |
970 | if ( !m_pos ) | |
971 | return NULL; | |
972 | ||
973 | do | |
974 | { | |
975 | if ( m_pos == m_to ) | |
976 | { | |
977 | m_pos = NULL; | |
978 | return NULL; | |
979 | } | |
980 | ||
981 | if ( m_pos->GetNext() ) | |
982 | m_pos = m_pos->GetNext(); | |
983 | else | |
984 | { | |
985 | // we must go up the hierarchy until we reach container where this | |
986 | // is not the last child, and then go down to first terminal cell: | |
987 | while ( m_pos->GetNext() == NULL ) | |
988 | { | |
989 | m_pos = m_pos->GetParent(); | |
990 | if ( !m_pos ) | |
991 | return NULL; | |
992 | } | |
993 | m_pos = m_pos->GetNext(); | |
994 | } | |
995 | while ( m_pos->GetFirstChild() != NULL ) | |
996 | m_pos = m_pos->GetFirstChild(); | |
997 | } while ( !m_pos->IsTerminalCell() ); | |
998 | ||
999 | return m_pos; | |
1000 | } | |
1001 | ||
1002 | #endif |