]>
Commit | Line | Data |
---|---|---|
1 | ///////////////////////////////////////////////////////////////////////////// | |
2 | // Name: src/html/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 | #include "wx/wxprec.h" | |
11 | ||
12 | #ifdef __BORLANDC__ | |
13 | #pragma hdrstop | |
14 | #endif | |
15 | ||
16 | #if wxUSE_HTML && wxUSE_STREAMS | |
17 | ||
18 | #ifndef WX_PRECOMP | |
19 | #include "wx/dynarray.h" | |
20 | #include "wx/brush.h" | |
21 | #include "wx/colour.h" | |
22 | #include "wx/dc.h" | |
23 | #include "wx/settings.h" | |
24 | #include "wx/module.h" | |
25 | #include "wx/wxcrtvararg.h" | |
26 | #endif | |
27 | ||
28 | #include "wx/html/htmlcell.h" | |
29 | #include "wx/html/htmlwin.h" | |
30 | ||
31 | #include <stdlib.h> | |
32 | ||
33 | //----------------------------------------------------------------------------- | |
34 | // Helper classes | |
35 | //----------------------------------------------------------------------------- | |
36 | ||
37 | void wxHtmlSelection::Set(const wxPoint& fromPos, const wxHtmlCell *fromCell, | |
38 | const wxPoint& toPos, const wxHtmlCell *toCell) | |
39 | { | |
40 | m_fromCell = fromCell; | |
41 | m_toCell = toCell; | |
42 | m_fromPos = fromPos; | |
43 | m_toPos = toPos; | |
44 | } | |
45 | ||
46 | void wxHtmlSelection::Set(const wxHtmlCell *fromCell, const wxHtmlCell *toCell) | |
47 | { | |
48 | wxPoint p1 = fromCell ? fromCell->GetAbsPos() : wxDefaultPosition; | |
49 | wxPoint p2 = toCell ? toCell->GetAbsPos() : wxDefaultPosition; | |
50 | if ( toCell ) | |
51 | { | |
52 | p2.x += toCell->GetWidth(); | |
53 | p2.y += toCell->GetHeight(); | |
54 | } | |
55 | Set(p1, fromCell, p2, toCell); | |
56 | } | |
57 | ||
58 | wxColour | |
59 | wxDefaultHtmlRenderingStyle:: | |
60 | GetSelectedTextColour(const wxColour& WXUNUSED(clr)) | |
61 | { | |
62 | return wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT); | |
63 | } | |
64 | ||
65 | wxColour | |
66 | wxDefaultHtmlRenderingStyle:: | |
67 | GetSelectedTextBgColour(const wxColour& WXUNUSED(clr)) | |
68 | { | |
69 | return wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHT); | |
70 | } | |
71 | ||
72 | ||
73 | //----------------------------------------------------------------------------- | |
74 | // wxHtmlCell | |
75 | //----------------------------------------------------------------------------- | |
76 | ||
77 | IMPLEMENT_ABSTRACT_CLASS(wxHtmlCell, wxObject) | |
78 | ||
79 | wxHtmlCell::wxHtmlCell() : wxObject() | |
80 | { | |
81 | m_Next = NULL; | |
82 | m_Parent = NULL; | |
83 | m_Width = m_Height = m_Descent = 0; | |
84 | m_ScriptMode = wxHTML_SCRIPT_NORMAL; // <sub> or <sup> mode | |
85 | m_ScriptBaseline = 0; // <sub> or <sup> baseline | |
86 | m_CanLiveOnPagebreak = true; | |
87 | m_Link = NULL; | |
88 | } | |
89 | ||
90 | wxHtmlCell::~wxHtmlCell() | |
91 | { | |
92 | delete m_Link; | |
93 | } | |
94 | ||
95 | // Update the descent value when whe are in a <sub> or <sup>. | |
96 | // prevbase is the parent base | |
97 | void wxHtmlCell::SetScriptMode(wxHtmlScriptMode mode, long previousBase) | |
98 | { | |
99 | m_ScriptMode = mode; | |
100 | ||
101 | if (mode == wxHTML_SCRIPT_SUP) | |
102 | m_ScriptBaseline = previousBase - (m_Height + 1) / 2; | |
103 | else if (mode == wxHTML_SCRIPT_SUB) | |
104 | m_ScriptBaseline = previousBase + (m_Height + 1) / 6; | |
105 | else | |
106 | m_ScriptBaseline = 0; | |
107 | ||
108 | m_Descent += m_ScriptBaseline; | |
109 | } | |
110 | ||
111 | #if WXWIN_COMPATIBILITY_2_6 | |
112 | ||
113 | struct wxHtmlCellOnMouseClickCompatHelper; | |
114 | ||
115 | static wxHtmlCellOnMouseClickCompatHelper *gs_helperOnMouseClick = NULL; | |
116 | ||
117 | // helper for routing calls to new ProcessMouseClick() method to deprecated | |
118 | // OnMouseClick() method | |
119 | struct wxHtmlCellOnMouseClickCompatHelper | |
120 | { | |
121 | wxHtmlCellOnMouseClickCompatHelper(wxHtmlWindowInterface *window_, | |
122 | const wxPoint& pos_, | |
123 | const wxMouseEvent& event_) | |
124 | : window(window_), pos(pos_), event(event_), retval(false) | |
125 | { | |
126 | } | |
127 | ||
128 | bool CallOnMouseClick(wxHtmlCell *cell) | |
129 | { | |
130 | wxHtmlCellOnMouseClickCompatHelper *oldHelper = gs_helperOnMouseClick; | |
131 | gs_helperOnMouseClick = this; | |
132 | cell->OnMouseClick | |
133 | ( | |
134 | window ? window->GetHTMLWindow() : NULL, | |
135 | pos.x, pos.y, | |
136 | event | |
137 | ); | |
138 | gs_helperOnMouseClick = oldHelper; | |
139 | return retval; | |
140 | } | |
141 | ||
142 | wxHtmlWindowInterface *window; | |
143 | const wxPoint& pos; | |
144 | const wxMouseEvent& event; | |
145 | bool retval; | |
146 | }; | |
147 | #endif // WXWIN_COMPATIBILITY_2_6 | |
148 | ||
149 | bool wxHtmlCell::ProcessMouseClick(wxHtmlWindowInterface *window, | |
150 | const wxPoint& pos, | |
151 | const wxMouseEvent& event) | |
152 | { | |
153 | wxCHECK_MSG( window, false, _T("window interface must be provided") ); | |
154 | ||
155 | #if WXWIN_COMPATIBILITY_2_6 | |
156 | // NB: this hack puts the body of ProcessMouseClick() into OnMouseClick() | |
157 | // (for which it has to pass the arguments and return value via a | |
158 | // helper variable because these two methods have different | |
159 | // signatures), so that old code overriding OnMouseClick will continue | |
160 | // to work | |
161 | wxHtmlCellOnMouseClickCompatHelper compat(window, pos, event); | |
162 | return compat.CallOnMouseClick(this); | |
163 | } | |
164 | ||
165 | void wxHtmlCell::OnMouseClick(wxWindow *, int, int, const wxMouseEvent& event) | |
166 | { | |
167 | wxCHECK_RET( gs_helperOnMouseClick, _T("unexpected call to OnMouseClick") ); | |
168 | wxHtmlWindowInterface *window = gs_helperOnMouseClick->window; | |
169 | const wxPoint& pos = gs_helperOnMouseClick->pos; | |
170 | #endif // WXWIN_COMPATIBILITY_2_6 | |
171 | ||
172 | wxHtmlLinkInfo *lnk = GetLink(pos.x, pos.y); | |
173 | bool retval = false; | |
174 | ||
175 | if (lnk) | |
176 | { | |
177 | wxHtmlLinkInfo lnk2(*lnk); | |
178 | lnk2.SetEvent(&event); | |
179 | lnk2.SetHtmlCell(this); | |
180 | ||
181 | window->OnHTMLLinkClicked(lnk2); | |
182 | retval = true; | |
183 | } | |
184 | ||
185 | #if WXWIN_COMPATIBILITY_2_6 | |
186 | gs_helperOnMouseClick->retval = retval; | |
187 | #else | |
188 | return retval; | |
189 | #endif // WXWIN_COMPATIBILITY_2_6 | |
190 | } | |
191 | ||
192 | #if WXWIN_COMPATIBILITY_2_6 | |
193 | wxCursor wxHtmlCell::GetCursor() const | |
194 | { | |
195 | return wxNullCursor; | |
196 | } | |
197 | #endif // WXWIN_COMPATIBILITY_2_6 | |
198 | ||
199 | wxCursor wxHtmlCell::GetMouseCursor(wxHtmlWindowInterface *window) const | |
200 | { | |
201 | #if WXWIN_COMPATIBILITY_2_6 | |
202 | // NB: Older versions of wx used GetCursor() virtual method in place of | |
203 | // GetMouseCursor(interface). This code ensures that user code that | |
204 | // overriden GetCursor() continues to work. The trick is that the base | |
205 | // wxHtmlCell::GetCursor() method simply returns wxNullCursor, so we | |
206 | // know that GetCursor() was overriden iff it returns valid cursor. | |
207 | wxCursor cur = GetCursor(); | |
208 | if (cur.Ok()) | |
209 | return cur; | |
210 | #endif // WXWIN_COMPATIBILITY_2_6 | |
211 | ||
212 | if ( GetLink() ) | |
213 | { | |
214 | return window->GetHTMLCursor(wxHtmlWindowInterface::HTMLCursor_Link); | |
215 | } | |
216 | else | |
217 | { | |
218 | return window->GetHTMLCursor(wxHtmlWindowInterface::HTMLCursor_Default); | |
219 | } | |
220 | } | |
221 | ||
222 | ||
223 | bool wxHtmlCell::AdjustPagebreak(int *pagebreak, | |
224 | wxArrayInt& WXUNUSED(known_pagebreaks)) const | |
225 | { | |
226 | if ((!m_CanLiveOnPagebreak) && | |
227 | m_PosY < *pagebreak && m_PosY + m_Height > *pagebreak) | |
228 | { | |
229 | *pagebreak = m_PosY; | |
230 | return true; | |
231 | } | |
232 | ||
233 | return false; | |
234 | } | |
235 | ||
236 | ||
237 | ||
238 | void wxHtmlCell::SetLink(const wxHtmlLinkInfo& link) | |
239 | { | |
240 | if (m_Link) delete m_Link; | |
241 | m_Link = NULL; | |
242 | if (link.GetHref() != wxEmptyString) | |
243 | m_Link = new wxHtmlLinkInfo(link); | |
244 | } | |
245 | ||
246 | ||
247 | void wxHtmlCell::Layout(int WXUNUSED(w)) | |
248 | { | |
249 | SetPos(0, 0); | |
250 | } | |
251 | ||
252 | ||
253 | ||
254 | const wxHtmlCell* wxHtmlCell::Find(int WXUNUSED(condition), const void* WXUNUSED(param)) const | |
255 | { | |
256 | return NULL; | |
257 | } | |
258 | ||
259 | ||
260 | wxHtmlCell *wxHtmlCell::FindCellByPos(wxCoord x, wxCoord y, | |
261 | unsigned flags) const | |
262 | { | |
263 | if ( x >= 0 && x < m_Width && y >= 0 && y < m_Height ) | |
264 | { | |
265 | return wxConstCast(this, wxHtmlCell); | |
266 | } | |
267 | else | |
268 | { | |
269 | if ((flags & wxHTML_FIND_NEAREST_AFTER) && | |
270 | (y < 0 || (y < 0+m_Height && x < 0+m_Width))) | |
271 | return wxConstCast(this, wxHtmlCell); | |
272 | else if ((flags & wxHTML_FIND_NEAREST_BEFORE) && | |
273 | (y >= 0+m_Height || (y >= 0 && x >= 0))) | |
274 | return wxConstCast(this, wxHtmlCell); | |
275 | else | |
276 | return NULL; | |
277 | } | |
278 | } | |
279 | ||
280 | ||
281 | wxPoint wxHtmlCell::GetAbsPos(wxHtmlCell *rootCell) const | |
282 | { | |
283 | wxPoint p(m_PosX, m_PosY); | |
284 | for (wxHtmlCell *parent = m_Parent; parent && parent != rootCell; | |
285 | parent = parent->m_Parent) | |
286 | { | |
287 | p.x += parent->m_PosX; | |
288 | p.y += parent->m_PosY; | |
289 | } | |
290 | return p; | |
291 | } | |
292 | ||
293 | wxHtmlCell *wxHtmlCell::GetRootCell() const | |
294 | { | |
295 | wxHtmlCell *c = wxConstCast(this, wxHtmlCell); | |
296 | while ( c->m_Parent ) | |
297 | c = c->m_Parent; | |
298 | return c; | |
299 | } | |
300 | ||
301 | unsigned wxHtmlCell::GetDepth() const | |
302 | { | |
303 | unsigned d = 0; | |
304 | for (wxHtmlCell *p = m_Parent; p; p = p->m_Parent) | |
305 | d++; | |
306 | return d; | |
307 | } | |
308 | ||
309 | bool wxHtmlCell::IsBefore(wxHtmlCell *cell) const | |
310 | { | |
311 | const wxHtmlCell *c1 = this; | |
312 | const wxHtmlCell *c2 = cell; | |
313 | unsigned d1 = GetDepth(); | |
314 | unsigned d2 = cell->GetDepth(); | |
315 | ||
316 | if ( d1 > d2 ) | |
317 | for (; d1 != d2; d1-- ) | |
318 | c1 = c1->m_Parent; | |
319 | else if ( d1 < d2 ) | |
320 | for (; d1 != d2; d2-- ) | |
321 | c2 = c2->m_Parent; | |
322 | ||
323 | if ( cell == this ) | |
324 | return true; | |
325 | ||
326 | while ( c1 && c2 ) | |
327 | { | |
328 | if ( c1->m_Parent == c2->m_Parent ) | |
329 | { | |
330 | while ( c1 ) | |
331 | { | |
332 | if ( c1 == c2 ) | |
333 | return true; | |
334 | c1 = c1->GetNext(); | |
335 | } | |
336 | return false; | |
337 | } | |
338 | else | |
339 | { | |
340 | c1 = c1->m_Parent; | |
341 | c2 = c2->m_Parent; | |
342 | } | |
343 | } | |
344 | ||
345 | wxFAIL_MSG(_T("Cells are in different trees")); | |
346 | return false; | |
347 | } | |
348 | ||
349 | ||
350 | //----------------------------------------------------------------------------- | |
351 | // wxHtmlWordCell | |
352 | //----------------------------------------------------------------------------- | |
353 | ||
354 | IMPLEMENT_ABSTRACT_CLASS(wxHtmlWordCell, wxHtmlCell) | |
355 | ||
356 | wxHtmlWordCell::wxHtmlWordCell(const wxString& word, const wxDC& dc) : wxHtmlCell() | |
357 | { | |
358 | m_Word = word; | |
359 | wxCoord w, h, d; | |
360 | dc.GetTextExtent(m_Word, &w, &h, &d); | |
361 | m_Width = w; | |
362 | m_Height = h; | |
363 | m_Descent = d; | |
364 | SetCanLiveOnPagebreak(false); | |
365 | m_allowLinebreak = true; | |
366 | } | |
367 | ||
368 | void wxHtmlWordCell::SetPreviousWord(wxHtmlWordCell *cell) | |
369 | { | |
370 | if ( cell && m_Parent == cell->m_Parent && | |
371 | !wxIsspace(cell->m_Word.Last()) && !wxIsspace(m_Word[0u]) ) | |
372 | { | |
373 | m_allowLinebreak = false; | |
374 | } | |
375 | } | |
376 | ||
377 | // Splits m_Word into up to three parts according to selection, returns | |
378 | // substring before, in and after selection and the points (in relative coords) | |
379 | // where s2 and s3 start: | |
380 | void wxHtmlWordCell::Split(const wxDC& dc, | |
381 | const wxPoint& selFrom, const wxPoint& selTo, | |
382 | unsigned& pos1, unsigned& pos2) const | |
383 | { | |
384 | wxPoint pt1 = (selFrom == wxDefaultPosition) ? | |
385 | wxDefaultPosition : selFrom - GetAbsPos(); | |
386 | wxPoint pt2 = (selTo == wxDefaultPosition) ? | |
387 | wxPoint(m_Width, wxDefaultCoord) : selTo - GetAbsPos(); | |
388 | ||
389 | unsigned len = m_Word.length(); | |
390 | unsigned i = 0; | |
391 | pos1 = 0; | |
392 | ||
393 | // adjust for cases when the start/end position is completely | |
394 | // outside the cell: | |
395 | if ( pt1.y < 0 ) | |
396 | pt1.x = 0; | |
397 | if ( pt2.y >= m_Height ) | |
398 | pt2.x = m_Width; | |
399 | ||
400 | // before selection: | |
401 | #ifdef __WXMAC__ | |
402 | // implementation using PartialExtents to support fractional widths | |
403 | wxArrayInt widths ; | |
404 | dc.GetPartialTextExtents(m_Word,widths) ; | |
405 | while( i < len && pt1.x >= widths[i] ) | |
406 | i++ ; | |
407 | #else // __WXMAC__ | |
408 | wxCoord charW, charH; | |
409 | while ( pt1.x > 0 && i < len ) | |
410 | { | |
411 | dc.GetTextExtent(m_Word[i], &charW, &charH); | |
412 | pt1.x -= charW; | |
413 | if ( pt1.x >= 0 ) | |
414 | { | |
415 | pos1 += charW; | |
416 | i++; | |
417 | } | |
418 | } | |
419 | #endif // __WXMAC__/!__WXMAC__ | |
420 | ||
421 | // in selection: | |
422 | unsigned j = i; | |
423 | #ifdef __WXMAC__ | |
424 | while( j < len && pt2.x >= widths[j] ) | |
425 | j++ ; | |
426 | #else // __WXMAC__ | |
427 | pos2 = pos1; | |
428 | pt2.x -= pos2; | |
429 | while ( pt2.x > 0 && j < len ) | |
430 | { | |
431 | dc.GetTextExtent(m_Word[j], &charW, &charH); | |
432 | pt2.x -= charW; | |
433 | if ( pt2.x >= 0 ) | |
434 | { | |
435 | pos2 += charW; | |
436 | j++; | |
437 | } | |
438 | } | |
439 | #endif // __WXMAC__/!__WXMAC__ | |
440 | ||
441 | pos1 = i; | |
442 | pos2 = j; | |
443 | } | |
444 | ||
445 | void wxHtmlWordCell::SetSelectionPrivPos(const wxDC& dc, wxHtmlSelection *s) const | |
446 | { | |
447 | unsigned p1, p2; | |
448 | ||
449 | Split(dc, | |
450 | this == s->GetFromCell() ? s->GetFromPos() : wxDefaultPosition, | |
451 | this == s->GetToCell() ? s->GetToPos() : wxDefaultPosition, | |
452 | p1, p2); | |
453 | ||
454 | wxPoint p(0, m_Word.length()); | |
455 | ||
456 | if ( this == s->GetFromCell() ) | |
457 | p.x = p1; // selection starts here | |
458 | if ( this == s->GetToCell() ) | |
459 | p.y = p2; // selection ends here | |
460 | ||
461 | if ( this == s->GetFromCell() ) | |
462 | s->SetFromPrivPos(p); | |
463 | if ( this == s->GetToCell() ) | |
464 | s->SetToPrivPos(p); | |
465 | } | |
466 | ||
467 | ||
468 | static void SwitchSelState(wxDC& dc, wxHtmlRenderingInfo& info, | |
469 | bool toSelection) | |
470 | { | |
471 | wxColour fg = info.GetState().GetFgColour(); | |
472 | wxColour bg = info.GetState().GetBgColour(); | |
473 | ||
474 | if ( toSelection ) | |
475 | { | |
476 | dc.SetBackgroundMode(wxSOLID); | |
477 | dc.SetTextForeground(info.GetStyle().GetSelectedTextColour(fg)); | |
478 | dc.SetTextBackground(info.GetStyle().GetSelectedTextBgColour(bg)); | |
479 | dc.SetBackground(wxBrush(info.GetStyle().GetSelectedTextBgColour(bg), | |
480 | wxSOLID)); | |
481 | } | |
482 | else | |
483 | { | |
484 | dc.SetBackgroundMode(wxTRANSPARENT); | |
485 | dc.SetTextForeground(fg); | |
486 | dc.SetTextBackground(bg); | |
487 | dc.SetBackground(wxBrush(bg, wxSOLID)); | |
488 | } | |
489 | } | |
490 | ||
491 | ||
492 | void wxHtmlWordCell::Draw(wxDC& dc, int x, int y, | |
493 | int WXUNUSED(view_y1), int WXUNUSED(view_y2), | |
494 | wxHtmlRenderingInfo& info) | |
495 | { | |
496 | #if 0 // useful for debugging | |
497 | dc.SetPen(*wxBLACK_PEN); | |
498 | dc.DrawRectangle(x+m_PosX,y+m_PosY,m_Width /* VZ: +1? */ ,m_Height); | |
499 | #endif | |
500 | ||
501 | bool drawSelectionAfterCell = false; | |
502 | ||
503 | if ( info.GetState().GetSelectionState() == wxHTML_SEL_CHANGING ) | |
504 | { | |
505 | // Selection changing, we must draw the word piecewise: | |
506 | wxHtmlSelection *s = info.GetSelection(); | |
507 | wxString txt; | |
508 | int w, h; | |
509 | int ofs = 0; | |
510 | ||
511 | wxPoint priv = (this == s->GetFromCell()) ? | |
512 | s->GetFromPrivPos() : s->GetToPrivPos(); | |
513 | ||
514 | // NB: this is quite a hack: in order to compute selection boundaries | |
515 | // (in word's characters) we must know current font, which is only | |
516 | // possible inside rendering code. Therefore we update the | |
517 | // information here and store it in wxHtmlSelection so that | |
518 | // ConvertToText can use it later: | |
519 | if ( priv == wxDefaultPosition ) | |
520 | { | |
521 | SetSelectionPrivPos(dc, s); | |
522 | priv = (this == s->GetFromCell()) ? | |
523 | s->GetFromPrivPos() : s->GetToPrivPos(); | |
524 | } | |
525 | ||
526 | int part1 = priv.x; | |
527 | int part2 = priv.y; | |
528 | ||
529 | if ( part1 > 0 ) | |
530 | { | |
531 | txt = m_Word.Mid(0, part1); | |
532 | dc.DrawText(txt, x + m_PosX, y + m_PosY); | |
533 | dc.GetTextExtent(txt, &w, &h); | |
534 | ofs += w; | |
535 | } | |
536 | ||
537 | SwitchSelState(dc, info, true); | |
538 | ||
539 | txt = m_Word.Mid(part1, part2-part1); | |
540 | dc.DrawText(txt, ofs + x + m_PosX, y + m_PosY); | |
541 | ||
542 | if ( (size_t)part2 < m_Word.length() ) | |
543 | { | |
544 | dc.GetTextExtent(txt, &w, &h); | |
545 | ofs += w; | |
546 | SwitchSelState(dc, info, false); | |
547 | txt = m_Word.Mid(part2); | |
548 | dc.DrawText(txt, ofs + x + m_PosX, y + m_PosY); | |
549 | } | |
550 | else | |
551 | drawSelectionAfterCell = true; | |
552 | } | |
553 | else | |
554 | { | |
555 | wxHtmlSelectionState selstate = info.GetState().GetSelectionState(); | |
556 | // Not changing selection state, draw the word in single mode: | |
557 | if ( selstate != wxHTML_SEL_OUT && | |
558 | dc.GetBackgroundMode() != wxSOLID ) | |
559 | { | |
560 | SwitchSelState(dc, info, true); | |
561 | } | |
562 | else if ( selstate == wxHTML_SEL_OUT && | |
563 | dc.GetBackgroundMode() == wxSOLID ) | |
564 | { | |
565 | SwitchSelState(dc, info, false); | |
566 | } | |
567 | dc.DrawText(m_Word, x + m_PosX, y + m_PosY); | |
568 | drawSelectionAfterCell = (selstate != wxHTML_SEL_OUT); | |
569 | } | |
570 | ||
571 | // NB: If the text is justified then there is usually some free space | |
572 | // between adjacent cells and drawing the selection only onto cells | |
573 | // would result in ugly unselected spaces. The code below detects | |
574 | // this special case and renders the selection *outside* the sell, | |
575 | // too. | |
576 | if ( m_Parent->GetAlignHor() == wxHTML_ALIGN_JUSTIFY && | |
577 | drawSelectionAfterCell ) | |
578 | { | |
579 | wxHtmlCell *nextCell = m_Next; | |
580 | while ( nextCell && nextCell->IsFormattingCell() ) | |
581 | nextCell = nextCell->GetNext(); | |
582 | if ( nextCell ) | |
583 | { | |
584 | int nextX = nextCell->GetPosX(); | |
585 | if ( m_PosX + m_Width < nextX ) | |
586 | { | |
587 | dc.SetBrush(dc.GetBackground()); | |
588 | dc.SetPen(*wxTRANSPARENT_PEN); | |
589 | dc.DrawRectangle(x + m_PosX + m_Width, y + m_PosY, | |
590 | nextX - m_PosX - m_Width, m_Height); | |
591 | } | |
592 | } | |
593 | } | |
594 | } | |
595 | ||
596 | ||
597 | wxString wxHtmlWordCell::ConvertToText(wxHtmlSelection *s) const | |
598 | { | |
599 | if ( s && (this == s->GetFromCell() || this == s->GetToCell()) ) | |
600 | { | |
601 | wxPoint priv = this == s->GetFromCell() ? s->GetFromPrivPos() | |
602 | : s->GetToPrivPos(); | |
603 | ||
604 | // VZ: we may be called before we had a chance to re-render ourselves | |
605 | // and in this case GetFrom/ToPrivPos() is not set yet -- assume | |
606 | // that this only happens in case of a double/triple click (which | |
607 | // seems to be the case now) and so it makes sense to select the | |
608 | // entire contents of the cell in this case | |
609 | // | |
610 | // TODO: but this really needs to be fixed in some better way later... | |
611 | if ( priv != wxDefaultPosition ) | |
612 | { | |
613 | int part1 = priv.x; | |
614 | int part2 = priv.y; | |
615 | return m_Word.Mid(part1, part2-part1); | |
616 | } | |
617 | //else: return the whole word below | |
618 | } | |
619 | ||
620 | return m_Word; | |
621 | } | |
622 | ||
623 | wxCursor wxHtmlWordCell::GetMouseCursor(wxHtmlWindowInterface *window) const | |
624 | { | |
625 | if ( !GetLink() ) | |
626 | { | |
627 | return window->GetHTMLCursor(wxHtmlWindowInterface::HTMLCursor_Text); | |
628 | } | |
629 | else | |
630 | { | |
631 | return wxHtmlCell::GetMouseCursor(window); | |
632 | } | |
633 | } | |
634 | ||
635 | ||
636 | //----------------------------------------------------------------------------- | |
637 | // wxHtmlContainerCell | |
638 | //----------------------------------------------------------------------------- | |
639 | ||
640 | IMPLEMENT_ABSTRACT_CLASS(wxHtmlContainerCell, wxHtmlCell) | |
641 | ||
642 | wxHtmlContainerCell::wxHtmlContainerCell(wxHtmlContainerCell *parent) : wxHtmlCell() | |
643 | { | |
644 | m_Cells = m_LastCell = NULL; | |
645 | m_Parent = parent; | |
646 | m_MaxTotalWidth = 0; | |
647 | if (m_Parent) m_Parent->InsertCell(this); | |
648 | m_AlignHor = wxHTML_ALIGN_LEFT; | |
649 | m_AlignVer = wxHTML_ALIGN_BOTTOM; | |
650 | m_IndentLeft = m_IndentRight = m_IndentTop = m_IndentBottom = 0; | |
651 | m_WidthFloat = 100; m_WidthFloatUnits = wxHTML_UNITS_PERCENT; | |
652 | m_UseBkColour = false; | |
653 | m_UseBorder = false; | |
654 | m_MinHeight = 0; | |
655 | m_MinHeightAlign = wxHTML_ALIGN_TOP; | |
656 | m_LastLayout = -1; | |
657 | } | |
658 | ||
659 | wxHtmlContainerCell::~wxHtmlContainerCell() | |
660 | { | |
661 | wxHtmlCell *cell = m_Cells; | |
662 | while ( cell ) | |
663 | { | |
664 | wxHtmlCell *cellNext = cell->GetNext(); | |
665 | delete cell; | |
666 | cell = cellNext; | |
667 | } | |
668 | } | |
669 | ||
670 | ||
671 | ||
672 | void wxHtmlContainerCell::SetIndent(int i, int what, int units) | |
673 | { | |
674 | int val = (units == wxHTML_UNITS_PIXELS) ? i : -i; | |
675 | if (what & wxHTML_INDENT_LEFT) m_IndentLeft = val; | |
676 | if (what & wxHTML_INDENT_RIGHT) m_IndentRight = val; | |
677 | if (what & wxHTML_INDENT_TOP) m_IndentTop = val; | |
678 | if (what & wxHTML_INDENT_BOTTOM) m_IndentBottom = val; | |
679 | m_LastLayout = -1; | |
680 | } | |
681 | ||
682 | ||
683 | ||
684 | int wxHtmlContainerCell::GetIndent(int ind) const | |
685 | { | |
686 | if (ind & wxHTML_INDENT_LEFT) return m_IndentLeft; | |
687 | else if (ind & wxHTML_INDENT_RIGHT) return m_IndentRight; | |
688 | else if (ind & wxHTML_INDENT_TOP) return m_IndentTop; | |
689 | else if (ind & wxHTML_INDENT_BOTTOM) return m_IndentBottom; | |
690 | else return -1; /* BUG! Should not be called... */ | |
691 | } | |
692 | ||
693 | ||
694 | ||
695 | ||
696 | int wxHtmlContainerCell::GetIndentUnits(int ind) const | |
697 | { | |
698 | bool p = false; | |
699 | if (ind & wxHTML_INDENT_LEFT) p = m_IndentLeft < 0; | |
700 | else if (ind & wxHTML_INDENT_RIGHT) p = m_IndentRight < 0; | |
701 | else if (ind & wxHTML_INDENT_TOP) p = m_IndentTop < 0; | |
702 | else if (ind & wxHTML_INDENT_BOTTOM) p = m_IndentBottom < 0; | |
703 | if (p) return wxHTML_UNITS_PERCENT; | |
704 | else return wxHTML_UNITS_PIXELS; | |
705 | } | |
706 | ||
707 | ||
708 | bool wxHtmlContainerCell::AdjustPagebreak(int *pagebreak, | |
709 | wxArrayInt& known_pagebreaks) const | |
710 | { | |
711 | if (!m_CanLiveOnPagebreak) | |
712 | return wxHtmlCell::AdjustPagebreak(pagebreak, known_pagebreaks); | |
713 | ||
714 | wxHtmlCell *c = GetFirstChild(); | |
715 | bool rt = false; | |
716 | int pbrk = *pagebreak - m_PosY; | |
717 | ||
718 | while (c) | |
719 | { | |
720 | if (c->AdjustPagebreak(&pbrk, known_pagebreaks)) | |
721 | rt = true; | |
722 | c = c->GetNext(); | |
723 | } | |
724 | if (rt) | |
725 | *pagebreak = pbrk + m_PosY; | |
726 | return rt; | |
727 | } | |
728 | ||
729 | ||
730 | void wxHtmlContainerCell::Layout(int w) | |
731 | { | |
732 | wxHtmlCell::Layout(w); | |
733 | ||
734 | if (m_LastLayout == w) return; | |
735 | ||
736 | // VS: Any attempt to layout with negative or zero width leads to hell, | |
737 | // but we can't ignore such attempts completely, since it sometimes | |
738 | // happen (e.g. when trying how small a table can be). The best thing we | |
739 | // can do is to set the width of child cells to zero | |
740 | if (w < 1) | |
741 | { | |
742 | m_Width = 0; | |
743 | for (wxHtmlCell *cell = m_Cells; cell; cell = cell->GetNext()) | |
744 | cell->Layout(0); | |
745 | // this does two things: it recursively calls this code on all | |
746 | // child contrainers and resets children's position to (0,0) | |
747 | return; | |
748 | } | |
749 | ||
750 | wxHtmlCell *nextCell; | |
751 | long xpos = 0, ypos = m_IndentTop; | |
752 | int xdelta = 0, ybasicpos = 0, ydiff; | |
753 | int s_width, nextWordWidth, s_indent; | |
754 | int ysizeup = 0, ysizedown = 0; | |
755 | int MaxLineWidth = 0; | |
756 | int curLineWidth = 0; | |
757 | m_MaxTotalWidth = 0; | |
758 | ||
759 | ||
760 | /* | |
761 | ||
762 | WIDTH ADJUSTING : | |
763 | ||
764 | */ | |
765 | ||
766 | if (m_WidthFloatUnits == wxHTML_UNITS_PERCENT) | |
767 | { | |
768 | if (m_WidthFloat < 0) m_Width = (100 + m_WidthFloat) * w / 100; | |
769 | else m_Width = m_WidthFloat * w / 100; | |
770 | } | |
771 | else | |
772 | { | |
773 | if (m_WidthFloat < 0) m_Width = w + m_WidthFloat; | |
774 | else m_Width = m_WidthFloat; | |
775 | } | |
776 | ||
777 | if (m_Cells) | |
778 | { | |
779 | int l = (m_IndentLeft < 0) ? (-m_IndentLeft * m_Width / 100) : m_IndentLeft; | |
780 | int r = (m_IndentRight < 0) ? (-m_IndentRight * m_Width / 100) : m_IndentRight; | |
781 | for (wxHtmlCell *cell = m_Cells; cell; cell = cell->GetNext()) | |
782 | cell->Layout(m_Width - (l + r)); | |
783 | } | |
784 | ||
785 | /* | |
786 | ||
787 | LAYOUTING : | |
788 | ||
789 | */ | |
790 | ||
791 | // adjust indentation: | |
792 | s_indent = (m_IndentLeft < 0) ? (-m_IndentLeft * m_Width / 100) : m_IndentLeft; | |
793 | s_width = m_Width - s_indent - ((m_IndentRight < 0) ? (-m_IndentRight * m_Width / 100) : m_IndentRight); | |
794 | ||
795 | // my own layouting: | |
796 | wxHtmlCell *cell = m_Cells, | |
797 | *line = m_Cells; | |
798 | while (cell != NULL) | |
799 | { | |
800 | switch (m_AlignVer) | |
801 | { | |
802 | case wxHTML_ALIGN_TOP : ybasicpos = 0; break; | |
803 | case wxHTML_ALIGN_BOTTOM : ybasicpos = - cell->GetHeight(); break; | |
804 | case wxHTML_ALIGN_CENTER : ybasicpos = - cell->GetHeight() / 2; break; | |
805 | } | |
806 | ydiff = cell->GetHeight() + ybasicpos; | |
807 | ||
808 | if (cell->GetDescent() + ydiff > ysizedown) ysizedown = cell->GetDescent() + ydiff; | |
809 | if (ybasicpos + cell->GetDescent() < -ysizeup) ysizeup = - (ybasicpos + cell->GetDescent()); | |
810 | ||
811 | // layout nonbreakable run of cells: | |
812 | cell->SetPos(xpos, ybasicpos + cell->GetDescent()); | |
813 | xpos += cell->GetWidth(); | |
814 | if (!cell->IsTerminalCell()) | |
815 | { | |
816 | // Container cell indicates new line | |
817 | if (curLineWidth > m_MaxTotalWidth) | |
818 | m_MaxTotalWidth = curLineWidth; | |
819 | ||
820 | if (wxMax(cell->GetWidth(), cell->GetMaxTotalWidth()) > m_MaxTotalWidth) | |
821 | m_MaxTotalWidth = cell->GetMaxTotalWidth(); | |
822 | curLineWidth = 0; | |
823 | } | |
824 | else | |
825 | // Normal cell, add maximum cell width to line width | |
826 | curLineWidth += cell->GetMaxTotalWidth(); | |
827 | ||
828 | cell = cell->GetNext(); | |
829 | ||
830 | // compute length of the next word that would be added: | |
831 | nextWordWidth = 0; | |
832 | if (cell) | |
833 | { | |
834 | nextCell = cell; | |
835 | do | |
836 | { | |
837 | nextWordWidth += nextCell->GetWidth(); | |
838 | nextCell = nextCell->GetNext(); | |
839 | } while (nextCell && !nextCell->IsLinebreakAllowed()); | |
840 | } | |
841 | ||
842 | // force new line if occurred: | |
843 | if ((cell == NULL) || | |
844 | (xpos + nextWordWidth > s_width && cell->IsLinebreakAllowed())) | |
845 | { | |
846 | if (xpos > MaxLineWidth) MaxLineWidth = xpos; | |
847 | if (ysizeup < 0) ysizeup = 0; | |
848 | if (ysizedown < 0) ysizedown = 0; | |
849 | switch (m_AlignHor) { | |
850 | case wxHTML_ALIGN_LEFT : | |
851 | case wxHTML_ALIGN_JUSTIFY : | |
852 | xdelta = 0; | |
853 | break; | |
854 | case wxHTML_ALIGN_RIGHT : | |
855 | xdelta = 0 + (s_width - xpos); | |
856 | break; | |
857 | case wxHTML_ALIGN_CENTER : | |
858 | xdelta = 0 + (s_width - xpos) / 2; | |
859 | break; | |
860 | } | |
861 | if (xdelta < 0) xdelta = 0; | |
862 | xdelta += s_indent; | |
863 | ||
864 | ypos += ysizeup; | |
865 | ||
866 | if (m_AlignHor != wxHTML_ALIGN_JUSTIFY || cell == NULL) | |
867 | { | |
868 | while (line != cell) | |
869 | { | |
870 | line->SetPos(line->GetPosX() + xdelta, | |
871 | ypos + line->GetPosY()); | |
872 | line = line->GetNext(); | |
873 | } | |
874 | } | |
875 | else // align == justify | |
876 | { | |
877 | // we have to distribute the extra horz space between the cells | |
878 | // on this line | |
879 | ||
880 | // an added complication is that some cells have fixed size and | |
881 | // shouldn't get any increment (it so happens that these cells | |
882 | // also don't allow line break on them which provides with an | |
883 | // easy way to test for this) -- and neither should the cells | |
884 | // adjacent to them as this could result in a visible space | |
885 | // between two cells separated by, e.g. font change, cell which | |
886 | // is wrong | |
887 | ||
888 | int step = s_width - xpos; | |
889 | if ( step > 0 ) | |
890 | { | |
891 | // first count the cells which will get extra space | |
892 | int total = -1; | |
893 | ||
894 | const wxHtmlCell *c; | |
895 | if ( line != cell ) | |
896 | { | |
897 | for ( c = line; c != cell; c = c->GetNext() ) | |
898 | { | |
899 | if ( c->IsLinebreakAllowed() ) | |
900 | { | |
901 | total++; | |
902 | } | |
903 | } | |
904 | } | |
905 | ||
906 | // and now extra space to those cells which merit it | |
907 | if ( total ) | |
908 | { | |
909 | // first visible cell on line is not moved: | |
910 | while (line !=cell && !line->IsLinebreakAllowed()) | |
911 | { | |
912 | line->SetPos(line->GetPosX() + s_indent, | |
913 | line->GetPosY() + ypos); | |
914 | line = line->GetNext(); | |
915 | } | |
916 | ||
917 | if (line != cell) | |
918 | { | |
919 | line->SetPos(line->GetPosX() + s_indent, | |
920 | line->GetPosY() + ypos); | |
921 | ||
922 | line = line->GetNext(); | |
923 | } | |
924 | ||
925 | for ( int n = 0; line != cell; line = line->GetNext() ) | |
926 | { | |
927 | if ( line->IsLinebreakAllowed() ) | |
928 | { | |
929 | // offset the next cell relative to this one | |
930 | // thus increasing our size | |
931 | n++; | |
932 | } | |
933 | ||
934 | line->SetPos(line->GetPosX() + s_indent + | |
935 | ((n * step) / total), | |
936 | line->GetPosY() + ypos); | |
937 | } | |
938 | } | |
939 | else | |
940 | { | |
941 | // this will cause the code to enter "else branch" below: | |
942 | step = 0; | |
943 | } | |
944 | } | |
945 | // else branch: | |
946 | if ( step <= 0 ) // no extra space to distribute | |
947 | { | |
948 | // just set the indent properly | |
949 | while (line != cell) | |
950 | { | |
951 | line->SetPos(line->GetPosX() + s_indent, | |
952 | line->GetPosY() + ypos); | |
953 | line = line->GetNext(); | |
954 | } | |
955 | } | |
956 | } | |
957 | ||
958 | ypos += ysizedown; | |
959 | xpos = 0; | |
960 | ysizeup = ysizedown = 0; | |
961 | line = cell; | |
962 | } | |
963 | } | |
964 | ||
965 | // setup height & width, depending on container layout: | |
966 | m_Height = ypos + (ysizedown + ysizeup) + m_IndentBottom; | |
967 | ||
968 | if (m_Height < m_MinHeight) | |
969 | { | |
970 | if (m_MinHeightAlign != wxHTML_ALIGN_TOP) | |
971 | { | |
972 | int diff = m_MinHeight - m_Height; | |
973 | if (m_MinHeightAlign == wxHTML_ALIGN_CENTER) diff /= 2; | |
974 | cell = m_Cells; | |
975 | while (cell) | |
976 | { | |
977 | cell->SetPos(cell->GetPosX(), cell->GetPosY() + diff); | |
978 | cell = cell->GetNext(); | |
979 | } | |
980 | } | |
981 | m_Height = m_MinHeight; | |
982 | } | |
983 | ||
984 | if (curLineWidth > m_MaxTotalWidth) | |
985 | m_MaxTotalWidth = curLineWidth; | |
986 | ||
987 | m_MaxTotalWidth += s_indent + ((m_IndentRight < 0) ? (-m_IndentRight * m_Width / 100) : m_IndentRight); | |
988 | MaxLineWidth += s_indent + ((m_IndentRight < 0) ? (-m_IndentRight * m_Width / 100) : m_IndentRight); | |
989 | if (m_Width < MaxLineWidth) m_Width = MaxLineWidth; | |
990 | ||
991 | m_LastLayout = w; | |
992 | } | |
993 | ||
994 | void wxHtmlContainerCell::UpdateRenderingStatePre(wxHtmlRenderingInfo& info, | |
995 | wxHtmlCell *cell) const | |
996 | { | |
997 | wxHtmlSelection *s = info.GetSelection(); | |
998 | if (!s) return; | |
999 | if (s->GetFromCell() == cell || s->GetToCell() == cell) | |
1000 | { | |
1001 | info.GetState().SetSelectionState(wxHTML_SEL_CHANGING); | |
1002 | } | |
1003 | } | |
1004 | ||
1005 | void wxHtmlContainerCell::UpdateRenderingStatePost(wxHtmlRenderingInfo& info, | |
1006 | wxHtmlCell *cell) const | |
1007 | { | |
1008 | wxHtmlSelection *s = info.GetSelection(); | |
1009 | if (!s) return; | |
1010 | if (s->GetToCell() == cell) | |
1011 | info.GetState().SetSelectionState(wxHTML_SEL_OUT); | |
1012 | else if (s->GetFromCell() == cell) | |
1013 | info.GetState().SetSelectionState(wxHTML_SEL_IN); | |
1014 | } | |
1015 | ||
1016 | #define mMin(a, b) (((a) < (b)) ? (a) : (b)) | |
1017 | #define mMax(a, b) (((a) < (b)) ? (b) : (a)) | |
1018 | ||
1019 | void wxHtmlContainerCell::Draw(wxDC& dc, int x, int y, int view_y1, int view_y2, | |
1020 | wxHtmlRenderingInfo& info) | |
1021 | { | |
1022 | #if 0 // useful for debugging | |
1023 | dc.SetPen(*wxRED_PEN); | |
1024 | dc.DrawRectangle(x+m_PosX,y+m_PosY,m_Width,m_Height); | |
1025 | #endif | |
1026 | ||
1027 | int xlocal = x + m_PosX; | |
1028 | int ylocal = y + m_PosY; | |
1029 | ||
1030 | if (m_UseBkColour) | |
1031 | { | |
1032 | wxBrush myb = wxBrush(m_BkColour, wxSOLID); | |
1033 | ||
1034 | int real_y1 = mMax(ylocal, view_y1); | |
1035 | int real_y2 = mMin(ylocal + m_Height - 1, view_y2); | |
1036 | ||
1037 | dc.SetBrush(myb); | |
1038 | dc.SetPen(*wxTRANSPARENT_PEN); | |
1039 | dc.DrawRectangle(xlocal, real_y1, m_Width, real_y2 - real_y1 + 1); | |
1040 | } | |
1041 | ||
1042 | if (m_UseBorder) | |
1043 | { | |
1044 | wxPen mypen1(m_BorderColour1, 1, wxSOLID); | |
1045 | wxPen mypen2(m_BorderColour2, 1, wxSOLID); | |
1046 | ||
1047 | dc.SetPen(mypen1); | |
1048 | dc.DrawLine(xlocal, ylocal, xlocal, ylocal + m_Height - 1); | |
1049 | dc.DrawLine(xlocal, ylocal, xlocal + m_Width, ylocal); | |
1050 | dc.SetPen(mypen2); | |
1051 | dc.DrawLine(xlocal + m_Width - 1, ylocal, xlocal + m_Width - 1, ylocal + m_Height - 1); | |
1052 | dc.DrawLine(xlocal, ylocal + m_Height - 1, xlocal + m_Width, ylocal + m_Height - 1); | |
1053 | } | |
1054 | ||
1055 | if (m_Cells) | |
1056 | { | |
1057 | // draw container's contents: | |
1058 | for (wxHtmlCell *cell = m_Cells; cell; cell = cell->GetNext()) | |
1059 | { | |
1060 | ||
1061 | // optimize drawing: don't render off-screen content: | |
1062 | if ((ylocal + cell->GetPosY() <= view_y2) && | |
1063 | (ylocal + cell->GetPosY() + cell->GetHeight() > view_y1)) | |
1064 | { | |
1065 | // the cell is visible, draw it: | |
1066 | UpdateRenderingStatePre(info, cell); | |
1067 | cell->Draw(dc, | |
1068 | xlocal, ylocal, view_y1, view_y2, | |
1069 | info); | |
1070 | UpdateRenderingStatePost(info, cell); | |
1071 | } | |
1072 | else | |
1073 | { | |
1074 | // the cell is off-screen, proceed with font+color+etc. | |
1075 | // changes only: | |
1076 | cell->DrawInvisible(dc, xlocal, ylocal, info); | |
1077 | } | |
1078 | } | |
1079 | } | |
1080 | } | |
1081 | ||
1082 | ||
1083 | ||
1084 | void wxHtmlContainerCell::DrawInvisible(wxDC& dc, int x, int y, | |
1085 | wxHtmlRenderingInfo& info) | |
1086 | { | |
1087 | if (m_Cells) | |
1088 | { | |
1089 | for (wxHtmlCell *cell = m_Cells; cell; cell = cell->GetNext()) | |
1090 | { | |
1091 | UpdateRenderingStatePre(info, cell); | |
1092 | cell->DrawInvisible(dc, x + m_PosX, y + m_PosY, info); | |
1093 | UpdateRenderingStatePost(info, cell); | |
1094 | } | |
1095 | } | |
1096 | } | |
1097 | ||
1098 | ||
1099 | wxColour wxHtmlContainerCell::GetBackgroundColour() | |
1100 | { | |
1101 | if (m_UseBkColour) | |
1102 | return m_BkColour; | |
1103 | else | |
1104 | return wxNullColour; | |
1105 | } | |
1106 | ||
1107 | ||
1108 | ||
1109 | wxHtmlLinkInfo *wxHtmlContainerCell::GetLink(int x, int y) const | |
1110 | { | |
1111 | wxHtmlCell *cell = FindCellByPos(x, y); | |
1112 | ||
1113 | // VZ: I don't know if we should pass absolute or relative coords to | |
1114 | // wxHtmlCell::GetLink()? As the base class version just ignores them | |
1115 | // anyhow, it hardly matters right now but should still be clarified | |
1116 | return cell ? cell->GetLink(x, y) : NULL; | |
1117 | } | |
1118 | ||
1119 | ||
1120 | ||
1121 | void wxHtmlContainerCell::InsertCell(wxHtmlCell *f) | |
1122 | { | |
1123 | if (!m_Cells) m_Cells = m_LastCell = f; | |
1124 | else | |
1125 | { | |
1126 | m_LastCell->SetNext(f); | |
1127 | m_LastCell = f; | |
1128 | if (m_LastCell) while (m_LastCell->GetNext()) m_LastCell = m_LastCell->GetNext(); | |
1129 | } | |
1130 | f->SetParent(this); | |
1131 | m_LastLayout = -1; | |
1132 | } | |
1133 | ||
1134 | ||
1135 | ||
1136 | void wxHtmlContainerCell::SetAlign(const wxHtmlTag& tag) | |
1137 | { | |
1138 | if (tag.HasParam(wxT("ALIGN"))) | |
1139 | { | |
1140 | wxString alg = tag.GetParam(wxT("ALIGN")); | |
1141 | alg.MakeUpper(); | |
1142 | if (alg == wxT("CENTER")) | |
1143 | SetAlignHor(wxHTML_ALIGN_CENTER); | |
1144 | else if (alg == wxT("LEFT")) | |
1145 | SetAlignHor(wxHTML_ALIGN_LEFT); | |
1146 | else if (alg == wxT("JUSTIFY")) | |
1147 | SetAlignHor(wxHTML_ALIGN_JUSTIFY); | |
1148 | else if (alg == wxT("RIGHT")) | |
1149 | SetAlignHor(wxHTML_ALIGN_RIGHT); | |
1150 | m_LastLayout = -1; | |
1151 | } | |
1152 | } | |
1153 | ||
1154 | ||
1155 | ||
1156 | void wxHtmlContainerCell::SetWidthFloat(const wxHtmlTag& tag, double pixel_scale) | |
1157 | { | |
1158 | if (tag.HasParam(wxT("WIDTH"))) | |
1159 | { | |
1160 | int wdi; | |
1161 | wxString wd = tag.GetParam(wxT("WIDTH")); | |
1162 | ||
1163 | if (wd[wd.length()-1] == wxT('%')) | |
1164 | { | |
1165 | wxSscanf(wd.c_str(), wxT("%i%%"), &wdi); | |
1166 | SetWidthFloat(wdi, wxHTML_UNITS_PERCENT); | |
1167 | } | |
1168 | else | |
1169 | { | |
1170 | wxSscanf(wd.c_str(), wxT("%i"), &wdi); | |
1171 | SetWidthFloat((int)(pixel_scale * (double)wdi), wxHTML_UNITS_PIXELS); | |
1172 | } | |
1173 | m_LastLayout = -1; | |
1174 | } | |
1175 | } | |
1176 | ||
1177 | ||
1178 | ||
1179 | const wxHtmlCell* wxHtmlContainerCell::Find(int condition, const void* param) const | |
1180 | { | |
1181 | if (m_Cells) | |
1182 | { | |
1183 | for (wxHtmlCell *cell = m_Cells; cell; cell = cell->GetNext()) | |
1184 | { | |
1185 | const wxHtmlCell *r = cell->Find(condition, param); | |
1186 | if (r) return r; | |
1187 | } | |
1188 | } | |
1189 | return NULL; | |
1190 | } | |
1191 | ||
1192 | ||
1193 | wxHtmlCell *wxHtmlContainerCell::FindCellByPos(wxCoord x, wxCoord y, | |
1194 | unsigned flags) const | |
1195 | { | |
1196 | if ( flags & wxHTML_FIND_EXACT ) | |
1197 | { | |
1198 | for ( const wxHtmlCell *cell = m_Cells; cell; cell = cell->GetNext() ) | |
1199 | { | |
1200 | int cx = cell->GetPosX(), | |
1201 | cy = cell->GetPosY(); | |
1202 | ||
1203 | if ( (cx <= x) && (cx + cell->GetWidth() > x) && | |
1204 | (cy <= y) && (cy + cell->GetHeight() > y) ) | |
1205 | { | |
1206 | return cell->FindCellByPos(x - cx, y - cy, flags); | |
1207 | } | |
1208 | } | |
1209 | } | |
1210 | else if ( flags & wxHTML_FIND_NEAREST_AFTER ) | |
1211 | { | |
1212 | wxHtmlCell *c; | |
1213 | for ( const wxHtmlCell *cell = m_Cells; cell; cell = cell->GetNext() ) | |
1214 | { | |
1215 | if ( cell->IsFormattingCell() ) | |
1216 | continue; | |
1217 | int cellY = cell->GetPosY(); | |
1218 | if (!( y < cellY || (y < cellY + cell->GetHeight() && | |
1219 | x < cell->GetPosX() + cell->GetWidth()) )) | |
1220 | continue; | |
1221 | ||
1222 | c = cell->FindCellByPos(x - cell->GetPosX(), y - cellY, flags); | |
1223 | if (c) return c; | |
1224 | } | |
1225 | } | |
1226 | else if ( flags & wxHTML_FIND_NEAREST_BEFORE ) | |
1227 | { | |
1228 | wxHtmlCell *c2, *c = NULL; | |
1229 | for ( const wxHtmlCell *cell = m_Cells; cell; cell = cell->GetNext() ) | |
1230 | { | |
1231 | if ( cell->IsFormattingCell() ) | |
1232 | continue; | |
1233 | int cellY = cell->GetPosY(); | |
1234 | if (!( cellY + cell->GetHeight() <= y || | |
1235 | (y >= cellY && x >= cell->GetPosX()) )) | |
1236 | break; | |
1237 | c2 = cell->FindCellByPos(x - cell->GetPosX(), y - cellY, flags); | |
1238 | if (c2) | |
1239 | c = c2; | |
1240 | } | |
1241 | if (c) return c; | |
1242 | } | |
1243 | ||
1244 | return NULL; | |
1245 | } | |
1246 | ||
1247 | ||
1248 | bool wxHtmlContainerCell::ProcessMouseClick(wxHtmlWindowInterface *window, | |
1249 | const wxPoint& pos, | |
1250 | const wxMouseEvent& event) | |
1251 | { | |
1252 | #if WXWIN_COMPATIBILITY_2_6 | |
1253 | wxHtmlCellOnMouseClickCompatHelper compat(window, pos, event); | |
1254 | return compat.CallOnMouseClick(this); | |
1255 | } | |
1256 | ||
1257 | void wxHtmlContainerCell::OnMouseClick(wxWindow*, | |
1258 | int, int, const wxMouseEvent& event) | |
1259 | { | |
1260 | wxCHECK_RET( gs_helperOnMouseClick, _T("unexpected call to OnMouseClick") ); | |
1261 | wxHtmlWindowInterface *window = gs_helperOnMouseClick->window; | |
1262 | const wxPoint& pos = gs_helperOnMouseClick->pos; | |
1263 | #endif // WXWIN_COMPATIBILITY_2_6 | |
1264 | ||
1265 | bool retval = false; | |
1266 | wxHtmlCell *cell = FindCellByPos(pos.x, pos.y); | |
1267 | if ( cell ) | |
1268 | retval = cell->ProcessMouseClick(window, pos, event); | |
1269 | ||
1270 | #if WXWIN_COMPATIBILITY_2_6 | |
1271 | gs_helperOnMouseClick->retval = retval; | |
1272 | #else | |
1273 | return retval; | |
1274 | #endif // WXWIN_COMPATIBILITY_2_6 | |
1275 | } | |
1276 | ||
1277 | ||
1278 | wxHtmlCell *wxHtmlContainerCell::GetFirstTerminal() const | |
1279 | { | |
1280 | if ( m_Cells ) | |
1281 | { | |
1282 | wxHtmlCell *c2; | |
1283 | for (wxHtmlCell *c = m_Cells; c; c = c->GetNext()) | |
1284 | { | |
1285 | c2 = c->GetFirstTerminal(); | |
1286 | if ( c2 ) | |
1287 | return c2; | |
1288 | } | |
1289 | } | |
1290 | return NULL; | |
1291 | } | |
1292 | ||
1293 | wxHtmlCell *wxHtmlContainerCell::GetLastTerminal() const | |
1294 | { | |
1295 | if ( m_Cells ) | |
1296 | { | |
1297 | // most common case first: | |
1298 | wxHtmlCell *c = m_LastCell->GetLastTerminal(); | |
1299 | if ( c ) | |
1300 | return c; | |
1301 | ||
1302 | wxHtmlCell *ctmp; | |
1303 | wxHtmlCell *c2 = NULL; | |
1304 | for (c = m_Cells; c; c = c->GetNext()) | |
1305 | { | |
1306 | ctmp = c->GetLastTerminal(); | |
1307 | if ( ctmp ) | |
1308 | c2 = ctmp; | |
1309 | } | |
1310 | return c2; | |
1311 | } | |
1312 | else | |
1313 | return NULL; | |
1314 | } | |
1315 | ||
1316 | ||
1317 | static bool IsEmptyContainer(wxHtmlContainerCell *cell) | |
1318 | { | |
1319 | for ( wxHtmlCell *c = cell->GetFirstChild(); c; c = c->GetNext() ) | |
1320 | { | |
1321 | if ( !c->IsTerminalCell() || !c->IsFormattingCell() ) | |
1322 | return false; | |
1323 | } | |
1324 | return true; | |
1325 | } | |
1326 | ||
1327 | void wxHtmlContainerCell::RemoveExtraSpacing(bool top, bool bottom) | |
1328 | { | |
1329 | if ( top ) | |
1330 | SetIndent(0, wxHTML_INDENT_TOP); | |
1331 | if ( bottom ) | |
1332 | SetIndent(0, wxHTML_INDENT_BOTTOM); | |
1333 | ||
1334 | if ( m_Cells ) | |
1335 | { | |
1336 | wxHtmlCell *c; | |
1337 | wxHtmlContainerCell *cont; | |
1338 | if ( top ) | |
1339 | { | |
1340 | for ( c = m_Cells; c; c = c->GetNext() ) | |
1341 | { | |
1342 | if ( c->IsTerminalCell() ) | |
1343 | { | |
1344 | if ( !c->IsFormattingCell() ) | |
1345 | break; | |
1346 | } | |
1347 | else | |
1348 | { | |
1349 | cont = (wxHtmlContainerCell*)c; | |
1350 | if ( IsEmptyContainer(cont) ) | |
1351 | { | |
1352 | cont->SetIndent(0, wxHTML_INDENT_VERTICAL); | |
1353 | } | |
1354 | else | |
1355 | { | |
1356 | cont->RemoveExtraSpacing(true, false); | |
1357 | break; | |
1358 | } | |
1359 | } | |
1360 | } | |
1361 | } | |
1362 | ||
1363 | if ( bottom ) | |
1364 | { | |
1365 | wxArrayPtrVoid arr; | |
1366 | for ( c = m_Cells; c; c = c->GetNext() ) | |
1367 | arr.Add((void*)c); | |
1368 | ||
1369 | for ( int i = arr.GetCount() - 1; i >= 0; i--) | |
1370 | { | |
1371 | c = (wxHtmlCell*)arr[i]; | |
1372 | if ( c->IsTerminalCell() ) | |
1373 | { | |
1374 | if ( !c->IsFormattingCell() ) | |
1375 | break; | |
1376 | } | |
1377 | else | |
1378 | { | |
1379 | cont = (wxHtmlContainerCell*)c; | |
1380 | if ( IsEmptyContainer(cont) ) | |
1381 | { | |
1382 | cont->SetIndent(0, wxHTML_INDENT_VERTICAL); | |
1383 | } | |
1384 | else | |
1385 | { | |
1386 | cont->RemoveExtraSpacing(false, true); | |
1387 | break; | |
1388 | } | |
1389 | } | |
1390 | } | |
1391 | } | |
1392 | } | |
1393 | } | |
1394 | ||
1395 | ||
1396 | ||
1397 | ||
1398 | // -------------------------------------------------------------------------- | |
1399 | // wxHtmlColourCell | |
1400 | // -------------------------------------------------------------------------- | |
1401 | ||
1402 | IMPLEMENT_ABSTRACT_CLASS(wxHtmlColourCell, wxHtmlCell) | |
1403 | ||
1404 | void wxHtmlColourCell::Draw(wxDC& dc, | |
1405 | int x, int y, | |
1406 | int WXUNUSED(view_y1), int WXUNUSED(view_y2), | |
1407 | wxHtmlRenderingInfo& info) | |
1408 | { | |
1409 | DrawInvisible(dc, x, y, info); | |
1410 | } | |
1411 | ||
1412 | void wxHtmlColourCell::DrawInvisible(wxDC& dc, | |
1413 | int WXUNUSED(x), int WXUNUSED(y), | |
1414 | wxHtmlRenderingInfo& info) | |
1415 | { | |
1416 | wxHtmlRenderingState& state = info.GetState(); | |
1417 | if (m_Flags & wxHTML_CLR_FOREGROUND) | |
1418 | { | |
1419 | state.SetFgColour(m_Colour); | |
1420 | if (state.GetSelectionState() != wxHTML_SEL_IN) | |
1421 | dc.SetTextForeground(m_Colour); | |
1422 | else | |
1423 | dc.SetTextForeground( | |
1424 | info.GetStyle().GetSelectedTextColour(m_Colour)); | |
1425 | } | |
1426 | if (m_Flags & wxHTML_CLR_BACKGROUND) | |
1427 | { | |
1428 | state.SetBgColour(m_Colour); | |
1429 | if (state.GetSelectionState() != wxHTML_SEL_IN) | |
1430 | { | |
1431 | dc.SetTextBackground(m_Colour); | |
1432 | dc.SetBackground(wxBrush(m_Colour, wxSOLID)); | |
1433 | } | |
1434 | else | |
1435 | { | |
1436 | wxColour c = info.GetStyle().GetSelectedTextBgColour(m_Colour); | |
1437 | dc.SetTextBackground(c); | |
1438 | dc.SetBackground(wxBrush(c, wxSOLID)); | |
1439 | } | |
1440 | } | |
1441 | } | |
1442 | ||
1443 | ||
1444 | ||
1445 | ||
1446 | // --------------------------------------------------------------------------- | |
1447 | // wxHtmlFontCell | |
1448 | // --------------------------------------------------------------------------- | |
1449 | ||
1450 | IMPLEMENT_ABSTRACT_CLASS(wxHtmlFontCell, wxHtmlCell) | |
1451 | ||
1452 | void wxHtmlFontCell::Draw(wxDC& dc, | |
1453 | int WXUNUSED(x), int WXUNUSED(y), | |
1454 | int WXUNUSED(view_y1), int WXUNUSED(view_y2), | |
1455 | wxHtmlRenderingInfo& WXUNUSED(info)) | |
1456 | { | |
1457 | dc.SetFont(m_Font); | |
1458 | } | |
1459 | ||
1460 | void wxHtmlFontCell::DrawInvisible(wxDC& dc, int WXUNUSED(x), int WXUNUSED(y), | |
1461 | wxHtmlRenderingInfo& WXUNUSED(info)) | |
1462 | { | |
1463 | dc.SetFont(m_Font); | |
1464 | } | |
1465 | ||
1466 | ||
1467 | ||
1468 | ||
1469 | ||
1470 | ||
1471 | ||
1472 | ||
1473 | // --------------------------------------------------------------------------- | |
1474 | // wxHtmlWidgetCell | |
1475 | // --------------------------------------------------------------------------- | |
1476 | ||
1477 | IMPLEMENT_ABSTRACT_CLASS(wxHtmlWidgetCell, wxHtmlCell) | |
1478 | ||
1479 | wxHtmlWidgetCell::wxHtmlWidgetCell(wxWindow *wnd, int w) | |
1480 | { | |
1481 | int sx, sy; | |
1482 | m_Wnd = wnd; | |
1483 | m_Wnd->GetSize(&sx, &sy); | |
1484 | m_Width = sx, m_Height = sy; | |
1485 | m_WidthFloat = w; | |
1486 | } | |
1487 | ||
1488 | ||
1489 | void wxHtmlWidgetCell::Draw(wxDC& WXUNUSED(dc), | |
1490 | int WXUNUSED(x), int WXUNUSED(y), | |
1491 | int WXUNUSED(view_y1), int WXUNUSED(view_y2), | |
1492 | wxHtmlRenderingInfo& WXUNUSED(info)) | |
1493 | { | |
1494 | int absx = 0, absy = 0, stx, sty; | |
1495 | wxHtmlCell *c = this; | |
1496 | ||
1497 | while (c) | |
1498 | { | |
1499 | absx += c->GetPosX(); | |
1500 | absy += c->GetPosY(); | |
1501 | c = c->GetParent(); | |
1502 | } | |
1503 | ||
1504 | ((wxScrolledWindow*)(m_Wnd->GetParent()))->GetViewStart(&stx, &sty); | |
1505 | m_Wnd->SetSize(absx - wxHTML_SCROLL_STEP * stx, absy - wxHTML_SCROLL_STEP * sty, m_Width, m_Height); | |
1506 | } | |
1507 | ||
1508 | ||
1509 | ||
1510 | void wxHtmlWidgetCell::DrawInvisible(wxDC& WXUNUSED(dc), | |
1511 | int WXUNUSED(x), int WXUNUSED(y), | |
1512 | wxHtmlRenderingInfo& WXUNUSED(info)) | |
1513 | { | |
1514 | int absx = 0, absy = 0, stx, sty; | |
1515 | wxHtmlCell *c = this; | |
1516 | ||
1517 | while (c) | |
1518 | { | |
1519 | absx += c->GetPosX(); | |
1520 | absy += c->GetPosY(); | |
1521 | c = c->GetParent(); | |
1522 | } | |
1523 | ||
1524 | ((wxScrolledWindow*)(m_Wnd->GetParent()))->GetViewStart(&stx, &sty); | |
1525 | m_Wnd->SetSize(absx - wxHTML_SCROLL_STEP * stx, absy - wxHTML_SCROLL_STEP * sty, m_Width, m_Height); | |
1526 | } | |
1527 | ||
1528 | ||
1529 | ||
1530 | void wxHtmlWidgetCell::Layout(int w) | |
1531 | { | |
1532 | if (m_WidthFloat != 0) | |
1533 | { | |
1534 | m_Width = (w * m_WidthFloat) / 100; | |
1535 | m_Wnd->SetSize(m_Width, m_Height); | |
1536 | } | |
1537 | ||
1538 | wxHtmlCell::Layout(w); | |
1539 | } | |
1540 | ||
1541 | ||
1542 | ||
1543 | // ---------------------------------------------------------------------------- | |
1544 | // wxHtmlTerminalCellsInterator | |
1545 | // ---------------------------------------------------------------------------- | |
1546 | ||
1547 | const wxHtmlCell* wxHtmlTerminalCellsInterator::operator++() | |
1548 | { | |
1549 | if ( !m_pos ) | |
1550 | return NULL; | |
1551 | ||
1552 | do | |
1553 | { | |
1554 | if ( m_pos == m_to ) | |
1555 | { | |
1556 | m_pos = NULL; | |
1557 | return NULL; | |
1558 | } | |
1559 | ||
1560 | if ( m_pos->GetNext() ) | |
1561 | m_pos = m_pos->GetNext(); | |
1562 | else | |
1563 | { | |
1564 | // we must go up the hierarchy until we reach container where this | |
1565 | // is not the last child, and then go down to first terminal cell: | |
1566 | while ( m_pos->GetNext() == NULL ) | |
1567 | { | |
1568 | m_pos = m_pos->GetParent(); | |
1569 | if ( !m_pos ) | |
1570 | return NULL; | |
1571 | } | |
1572 | m_pos = m_pos->GetNext(); | |
1573 | } | |
1574 | while ( m_pos->GetFirstChild() != NULL ) | |
1575 | m_pos = m_pos->GetFirstChild(); | |
1576 | } while ( !m_pos->IsTerminalCell() ); | |
1577 | ||
1578 | return m_pos; | |
1579 | } | |
1580 | ||
1581 | #endif |