]>
Commit | Line | Data |
---|---|---|
e0c6027b | 1 | /////////////////////////////////////////////////////////////////////////////// |
80fdcdb9 | 2 | // Name: src/generic/htmllbox.cpp |
e0c6027b VZ |
3 | // Purpose: implementation of wxHtmlListBox |
4 | // Author: Vadim Zeitlin | |
5 | // Modified by: | |
6 | // Created: 31.05.03 | |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) 2003 Vadim Zeitlin <vadim@wxwindows.org> | |
526954c5 | 9 | // Licence: wxWindows licence |
e0c6027b VZ |
10 | /////////////////////////////////////////////////////////////////////////////// |
11 | ||
12 | // ============================================================================ | |
13 | // declarations | |
14 | // ============================================================================ | |
15 | ||
16 | // ---------------------------------------------------------------------------- | |
17 | // headers | |
18 | // ---------------------------------------------------------------------------- | |
19 | ||
20 | // For compilers that support precompilation, includes "wx.h". | |
21 | #include "wx/wxprec.h" | |
22 | ||
23 | #ifdef __BORLANDC__ | |
24 | #pragma hdrstop | |
25 | #endif | |
26 | ||
27 | #ifndef WX_PRECOMP | |
bb178b29 | 28 | #include "wx/dcclient.h" |
e0c6027b VZ |
29 | #endif //WX_PRECOMP |
30 | ||
461dae94 VZ |
31 | #if wxUSE_HTML |
32 | ||
e0c6027b VZ |
33 | #include "wx/htmllbox.h" |
34 | ||
35 | #include "wx/html/htmlcell.h" | |
36 | #include "wx/html/winpars.h" | |
37 | ||
5ecdc7ab VZ |
38 | // this hack forces the linker to always link in m_* files |
39 | #include "wx/html/forcelnk.h" | |
40 | FORCE_WXHTML_MODULES() | |
41 | ||
bc55e31b VS |
42 | // ---------------------------------------------------------------------------- |
43 | // constants | |
44 | // ---------------------------------------------------------------------------- | |
45 | ||
46 | // small border always added to the cells: | |
47 | static const wxCoord CELL_BORDER = 2; | |
48 | ||
23318a53 FM |
49 | const char wxHtmlListBoxNameStr[] = "htmlListBox"; |
50 | const char wxSimpleHtmlListBoxNameStr[] = "simpleHtmlListBox"; | |
9ebb7cad | 51 | |
9a9b4940 | 52 | // ============================================================================ |
e0c6027b | 53 | // private classes |
9a9b4940 VZ |
54 | // ============================================================================ |
55 | ||
56 | // ---------------------------------------------------------------------------- | |
57 | // wxHtmlListBoxCache | |
e0c6027b VZ |
58 | // ---------------------------------------------------------------------------- |
59 | ||
60 | // this class is used by wxHtmlListBox to cache the parsed representation of | |
61 | // the items to avoid doing it anew each time an item must be drawn | |
e0c6027b VZ |
62 | class wxHtmlListBoxCache |
63 | { | |
aead8a4e VZ |
64 | private: |
65 | // invalidate a single item, used by Clear() and InvalidateRange() | |
66 | void InvalidateItem(size_t n) | |
67 | { | |
68 | m_items[n] = (size_t)-1; | |
5276b0a5 | 69 | wxDELETE(m_cells[n]); |
aead8a4e VZ |
70 | } |
71 | ||
e0c6027b | 72 | public: |
5ecdc7ab VZ |
73 | wxHtmlListBoxCache() |
74 | { | |
75 | for ( size_t n = 0; n < SIZE; n++ ) | |
76 | { | |
77 | m_items[n] = (size_t)-1; | |
78 | m_cells[n] = NULL; | |
79 | } | |
e0c6027b | 80 | |
5ecdc7ab VZ |
81 | m_next = 0; |
82 | } | |
e0c6027b | 83 | |
5ecdc7ab | 84 | ~wxHtmlListBoxCache() |
e0c6027b | 85 | { |
5ecdc7ab VZ |
86 | for ( size_t n = 0; n < SIZE; n++ ) |
87 | { | |
88 | delete m_cells[n]; | |
89 | } | |
90 | } | |
e0c6027b | 91 | |
5ecdc7ab VZ |
92 | // completely invalidate the cache |
93 | void Clear() | |
94 | { | |
95 | for ( size_t n = 0; n < SIZE; n++ ) | |
96 | { | |
aead8a4e | 97 | InvalidateItem(n); |
5ecdc7ab | 98 | } |
e0c6027b VZ |
99 | } |
100 | ||
101 | // return the cached cell for this index or NULL if none | |
5ecdc7ab VZ |
102 | wxHtmlCell *Get(size_t item) const |
103 | { | |
104 | for ( size_t n = 0; n < SIZE; n++ ) | |
105 | { | |
106 | if ( m_items[n] == item ) | |
107 | return m_cells[n]; | |
108 | } | |
109 | ||
110 | return NULL; | |
111 | } | |
112 | ||
113 | // returns true if we already have this item cached | |
114 | bool Has(size_t item) const { return Get(item) != NULL; } | |
115 | ||
116 | // ensure that the item is cached | |
117 | void Store(size_t item, wxHtmlCell *cell) | |
e0c6027b | 118 | { |
5ecdc7ab VZ |
119 | delete m_cells[m_next]; |
120 | m_cells[m_next] = cell; | |
121 | m_items[m_next] = item; | |
122 | ||
123 | // advance to the next item wrapping around if there are no more | |
124 | if ( ++m_next == SIZE ) | |
125 | m_next = 0; | |
e0c6027b VZ |
126 | } |
127 | ||
aead8a4e VZ |
128 | // forget the cached value of the item(s) between the given ones (inclusive) |
129 | void InvalidateRange(size_t from, size_t to) | |
130 | { | |
131 | for ( size_t n = 0; n < SIZE; n++ ) | |
132 | { | |
133 | if ( m_items[n] >= from && m_items[n] <= to ) | |
134 | { | |
135 | InvalidateItem(n); | |
136 | } | |
137 | } | |
138 | } | |
139 | ||
e0c6027b | 140 | private: |
5ecdc7ab VZ |
141 | // the max number of the items we cache |
142 | enum { SIZE = 50 }; | |
143 | ||
144 | // the index of the LRU (oldest) cell | |
145 | size_t m_next; | |
146 | ||
e0c6027b | 147 | // the parsed representation of the cached item or NULL |
5ecdc7ab | 148 | wxHtmlCell *m_cells[SIZE]; |
e0c6027b | 149 | |
5ecdc7ab VZ |
150 | // the index of the currently cached item (only valid if m_cells != NULL) |
151 | size_t m_items[SIZE]; | |
e0c6027b VZ |
152 | }; |
153 | ||
9a9b4940 VZ |
154 | // ---------------------------------------------------------------------------- |
155 | // wxHtmlListBoxStyle | |
156 | // ---------------------------------------------------------------------------- | |
157 | ||
158 | // just forward wxDefaultHtmlRenderingStyle callbacks to the main class so that | |
159 | // they could be overridden by the user code | |
160 | class wxHtmlListBoxStyle : public wxDefaultHtmlRenderingStyle | |
161 | { | |
162 | public: | |
fbfb8bcc | 163 | wxHtmlListBoxStyle(const wxHtmlListBox& hlbox) : m_hlbox(hlbox) { } |
9a9b4940 VZ |
164 | |
165 | virtual wxColour GetSelectedTextColour(const wxColour& colFg) | |
166 | { | |
c848185a VZ |
167 | // by default wxHtmlListBox doesn't implement GetSelectedTextColour() |
168 | // and returns wxNullColour from it, so use the default HTML colour for | |
169 | // selection | |
170 | wxColour col = m_hlbox.GetSelectedTextColour(colFg); | |
171 | if ( !col.IsOk() ) | |
172 | { | |
173 | col = wxDefaultHtmlRenderingStyle::GetSelectedTextColour(colFg); | |
174 | } | |
175 | ||
176 | return col; | |
9a9b4940 VZ |
177 | } |
178 | ||
179 | virtual wxColour GetSelectedTextBgColour(const wxColour& colBg) | |
180 | { | |
c848185a VZ |
181 | wxColour col = m_hlbox.GetSelectedTextBgColour(colBg); |
182 | if ( !col.IsOk() ) | |
183 | { | |
184 | col = wxDefaultHtmlRenderingStyle::GetSelectedTextBgColour(colBg); | |
185 | } | |
186 | ||
187 | return col; | |
9a9b4940 VZ |
188 | } |
189 | ||
190 | private: | |
191 | const wxHtmlListBox& m_hlbox; | |
27d0dcd0 | 192 | |
c0c133e1 | 193 | wxDECLARE_NO_COPY_CLASS(wxHtmlListBoxStyle); |
9a9b4940 VZ |
194 | }; |
195 | ||
5ecdc7ab VZ |
196 | // ---------------------------------------------------------------------------- |
197 | // event tables | |
198 | // ---------------------------------------------------------------------------- | |
199 | ||
200 | BEGIN_EVENT_TABLE(wxHtmlListBox, wxVListBox) | |
201 | EVT_SIZE(wxHtmlListBox::OnSize) | |
bc55e31b VS |
202 | EVT_MOTION(wxHtmlListBox::OnMouseMove) |
203 | EVT_LEFT_DOWN(wxHtmlListBox::OnLeftDown) | |
5ecdc7ab VZ |
204 | END_EVENT_TABLE() |
205 | ||
e0c6027b VZ |
206 | // ============================================================================ |
207 | // implementation | |
208 | // ============================================================================ | |
209 | ||
0c8392ca RD |
210 | IMPLEMENT_ABSTRACT_CLASS(wxHtmlListBox, wxVListBox) |
211 | ||
212 | ||
e0c6027b VZ |
213 | // ---------------------------------------------------------------------------- |
214 | // wxHtmlListBox creation | |
215 | // ---------------------------------------------------------------------------- | |
216 | ||
bc55e31b VS |
217 | wxHtmlListBox::wxHtmlListBox() |
218 | : wxHtmlWindowMouseHelper(this) | |
219 | { | |
220 | Init(); | |
221 | } | |
222 | ||
223 | // normal constructor which calls Create() internally | |
224 | wxHtmlListBox::wxHtmlListBox(wxWindow *parent, | |
225 | wxWindowID id, | |
226 | const wxPoint& pos, | |
227 | const wxSize& size, | |
228 | long style, | |
229 | const wxString& name) | |
230 | : wxHtmlWindowMouseHelper(this) | |
231 | { | |
232 | Init(); | |
233 | ||
234 | (void)Create(parent, id, pos, size, style, name); | |
235 | } | |
236 | ||
e0c6027b VZ |
237 | void wxHtmlListBox::Init() |
238 | { | |
239 | m_htmlParser = NULL; | |
9a9b4940 | 240 | m_htmlRendStyle = new wxHtmlListBoxStyle(*this); |
e0c6027b VZ |
241 | m_cache = new wxHtmlListBoxCache; |
242 | } | |
243 | ||
244 | bool wxHtmlListBox::Create(wxWindow *parent, | |
245 | wxWindowID id, | |
246 | const wxPoint& pos, | |
247 | const wxSize& size, | |
e0c6027b VZ |
248 | long style, |
249 | const wxString& name) | |
250 | { | |
43e319a3 | 251 | return wxVListBox::Create(parent, id, pos, size, style, name); |
e0c6027b VZ |
252 | } |
253 | ||
254 | wxHtmlListBox::~wxHtmlListBox() | |
255 | { | |
256 | delete m_cache; | |
9a9b4940 | 257 | |
e0c6027b VZ |
258 | if ( m_htmlParser ) |
259 | { | |
260 | delete m_htmlParser->GetDC(); | |
261 | delete m_htmlParser; | |
262 | } | |
9a9b4940 VZ |
263 | |
264 | delete m_htmlRendStyle; | |
265 | } | |
266 | ||
267 | // ---------------------------------------------------------------------------- | |
268 | // wxHtmlListBox appearance | |
269 | // ---------------------------------------------------------------------------- | |
270 | ||
c848185a VZ |
271 | wxColour |
272 | wxHtmlListBox::GetSelectedTextColour(const wxColour& WXUNUSED(colFg)) const | |
9a9b4940 | 273 | { |
c848185a | 274 | return wxNullColour; |
9a9b4940 VZ |
275 | } |
276 | ||
277 | wxColour | |
278 | wxHtmlListBox::GetSelectedTextBgColour(const wxColour& WXUNUSED(colBg)) const | |
279 | { | |
5ae69f62 | 280 | return GetSelectionBackground(); |
e0c6027b VZ |
281 | } |
282 | ||
283 | // ---------------------------------------------------------------------------- | |
284 | // wxHtmlListBox items markup | |
285 | // ---------------------------------------------------------------------------- | |
286 | ||
287 | wxString wxHtmlListBox::OnGetItemMarkup(size_t n) const | |
288 | { | |
289 | // we don't even need to wrap the value returned by OnGetItem() inside | |
290 | // "<html><body>" and "</body></html>" because wxHTML can parse it even | |
291 | // without these tags | |
292 | return OnGetItem(n); | |
293 | } | |
294 | ||
5ecdc7ab VZ |
295 | // ---------------------------------------------------------------------------- |
296 | // wxHtmlListBox cache handling | |
297 | // ---------------------------------------------------------------------------- | |
298 | ||
e0c6027b VZ |
299 | void wxHtmlListBox::CacheItem(size_t n) const |
300 | { | |
301 | if ( !m_cache->Has(n) ) | |
302 | { | |
303 | if ( !m_htmlParser ) | |
304 | { | |
305 | wxHtmlListBox *self = wxConstCast(this, wxHtmlListBox); | |
306 | ||
bc55e31b | 307 | self->m_htmlParser = new wxHtmlWinParser(self); |
e0c6027b | 308 | m_htmlParser->SetDC(new wxClientDC(self)); |
2d814c19 | 309 | m_htmlParser->SetFS(&self->m_filesystem); |
67339f7d | 310 | #if !wxUSE_UNICODE |
a1b806b9 | 311 | if (GetFont().IsOk()) |
85d3d198 | 312 | m_htmlParser->SetInputEncoding(GetFont().GetEncoding()); |
67339f7d | 313 | #endif |
f987cbb1 VS |
314 | // use system's default GUI font by default: |
315 | m_htmlParser->SetStandardFonts(); | |
e0c6027b VZ |
316 | } |
317 | ||
318 | wxHtmlContainerCell *cell = (wxHtmlContainerCell *)m_htmlParser-> | |
319 | Parse(OnGetItemMarkup(n)); | |
9a83f860 | 320 | wxCHECK_RET( cell, wxT("wxHtmlParser::Parse() returned NULL?") ); |
e0c6027b | 321 | |
bc55e31b VS |
322 | // set the cell's ID to item's index so that CellCoordsToPhysical() |
323 | // can quickly find the item: | |
9a83f860 | 324 | cell->SetId(wxString::Format(wxT("%lu"), (unsigned long)n)); |
bc55e31b | 325 | |
5ecdc7ab | 326 | cell->Layout(GetClientSize().x - 2*GetMargins().x); |
e0c6027b VZ |
327 | |
328 | m_cache->Store(n, cell); | |
329 | } | |
330 | } | |
331 | ||
5ecdc7ab VZ |
332 | void wxHtmlListBox::OnSize(wxSizeEvent& event) |
333 | { | |
334 | // we need to relayout all the cached cells | |
335 | m_cache->Clear(); | |
336 | ||
337 | event.Skip(); | |
338 | } | |
339 | ||
e02c72fa | 340 | void wxHtmlListBox::RefreshRow(size_t line) |
aead8a4e VZ |
341 | { |
342 | m_cache->InvalidateRange(line, line); | |
343 | ||
f18eaf26 | 344 | wxVListBox::RefreshRow(line); |
aead8a4e VZ |
345 | } |
346 | ||
e02c72fa | 347 | void wxHtmlListBox::RefreshRows(size_t from, size_t to) |
aead8a4e VZ |
348 | { |
349 | m_cache->InvalidateRange(from, to); | |
350 | ||
f18eaf26 | 351 | wxVListBox::RefreshRows(from, to); |
aead8a4e VZ |
352 | } |
353 | ||
5ecdc7ab VZ |
354 | void wxHtmlListBox::RefreshAll() |
355 | { | |
356 | m_cache->Clear(); | |
357 | ||
358 | wxVListBox::RefreshAll(); | |
359 | } | |
360 | ||
03495767 VZ |
361 | void wxHtmlListBox::SetItemCount(size_t count) |
362 | { | |
363 | // the items are going to change, forget the old ones | |
364 | m_cache->Clear(); | |
365 | ||
366 | wxVListBox::SetItemCount(count); | |
367 | } | |
368 | ||
e0c6027b VZ |
369 | // ---------------------------------------------------------------------------- |
370 | // wxHtmlListBox implementation of wxVListBox pure virtuals | |
371 | // ---------------------------------------------------------------------------- | |
372 | ||
c848185a VZ |
373 | void |
374 | wxHtmlListBox::OnDrawBackground(wxDC& dc, const wxRect& rect, size_t n) const | |
375 | { | |
376 | if ( IsSelected(n) ) | |
377 | { | |
378 | if ( DoDrawSolidBackground | |
379 | ( | |
380 | GetSelectedTextBgColour(GetBackgroundColour()), | |
381 | dc, | |
382 | rect, | |
383 | n | |
384 | ) ) | |
385 | { | |
386 | return; | |
387 | } | |
388 | //else: no custom selection background colour, use base class version | |
389 | } | |
390 | ||
391 | wxVListBox::OnDrawBackground(dc, rect, n); | |
392 | } | |
393 | ||
e0c6027b VZ |
394 | void wxHtmlListBox::OnDrawItem(wxDC& dc, const wxRect& rect, size_t n) const |
395 | { | |
396 | CacheItem(n); | |
397 | ||
398 | wxHtmlCell *cell = m_cache->Get(n); | |
9a83f860 | 399 | wxCHECK_RET( cell, wxT("this cell should be cached!") ); |
e0c6027b | 400 | |
901b583c | 401 | wxHtmlRenderingInfo htmlRendInfo; |
5ecdc7ab | 402 | |
c848185a VZ |
403 | // draw the selected cell in selected state ourselves if we're using custom |
404 | // colours (to test for this, check the callbacks by passing them any dummy | |
405 | // (but valid, to avoid asserts) colour): | |
406 | if ( IsSelected(n) && | |
407 | (GetSelectedTextColour(*wxBLACK).IsOk() || | |
408 | GetSelectedTextBgColour(*wxWHITE).IsOk()) ) | |
409 | { | |
410 | wxHtmlSelection htmlSel; | |
411 | htmlSel.Set(wxPoint(0,0), cell, wxPoint(INT_MAX, INT_MAX), cell); | |
412 | htmlRendInfo.SetSelection(&htmlSel); | |
413 | htmlRendInfo.SetStyle(m_htmlRendStyle); | |
414 | htmlRendInfo.GetState().SetSelectionState(wxHTML_SEL_IN); | |
415 | } | |
416 | //else: normal item or selected item with default colours, its background | |
417 | // was already taken care of in the base class | |
418 | ||
5ecdc7ab VZ |
419 | // note that we can't stop drawing exactly at the window boundary as then |
420 | // even the visible cells part could be not drawn, so always draw the | |
421 | // entire cell | |
bc55e31b VS |
422 | cell->Draw(dc, |
423 | rect.x + CELL_BORDER, rect.y + CELL_BORDER, | |
424 | 0, INT_MAX, htmlRendInfo); | |
e0c6027b VZ |
425 | } |
426 | ||
427 | wxCoord wxHtmlListBox::OnMeasureItem(size_t n) const | |
428 | { | |
429 | CacheItem(n); | |
430 | ||
431 | wxHtmlCell *cell = m_cache->Get(n); | |
9a83f860 | 432 | wxCHECK_MSG( cell, 0, wxT("this cell should be cached!") ); |
e0c6027b | 433 | |
d3017584 | 434 | return cell->GetHeight() + cell->GetDescent() + 4; |
e0c6027b VZ |
435 | } |
436 | ||
bc55e31b VS |
437 | // ---------------------------------------------------------------------------- |
438 | // wxHtmlListBox implementation of wxHtmlListBoxWinInterface | |
439 | // ---------------------------------------------------------------------------- | |
440 | ||
441 | void wxHtmlListBox::SetHTMLWindowTitle(const wxString& WXUNUSED(title)) | |
442 | { | |
443 | // nothing to do | |
444 | } | |
445 | ||
446 | void wxHtmlListBox::OnHTMLLinkClicked(const wxHtmlLinkInfo& link) | |
447 | { | |
448 | OnLinkClicked(GetItemForCell(link.GetHtmlCell()), link); | |
449 | } | |
450 | ||
a3b5cead VS |
451 | void wxHtmlListBox::OnLinkClicked(size_t WXUNUSED(n), |
452 | const wxHtmlLinkInfo& link) | |
453 | { | |
454 | wxHtmlLinkEvent event(GetId(), link); | |
455 | GetEventHandler()->ProcessEvent(event); | |
456 | } | |
457 | ||
bc55e31b VS |
458 | wxHtmlOpeningStatus |
459 | wxHtmlListBox::OnHTMLOpeningURL(wxHtmlURLType WXUNUSED(type), | |
460 | const wxString& WXUNUSED(url), | |
461 | wxString *WXUNUSED(redirect)) const | |
462 | { | |
463 | return wxHTML_OPEN; | |
464 | } | |
465 | ||
466 | wxPoint wxHtmlListBox::HTMLCoordsToWindow(wxHtmlCell *cell, | |
467 | const wxPoint& pos) const | |
468 | { | |
469 | return CellCoordsToPhysical(pos, cell); | |
470 | } | |
471 | ||
472 | wxWindow* wxHtmlListBox::GetHTMLWindow() { return this; } | |
473 | ||
474 | wxColour wxHtmlListBox::GetHTMLBackgroundColour() const | |
475 | { | |
476 | return GetBackgroundColour(); | |
477 | } | |
478 | ||
479 | void wxHtmlListBox::SetHTMLBackgroundColour(const wxColour& WXUNUSED(clr)) | |
480 | { | |
481 | // nothing to do | |
482 | } | |
483 | ||
484 | void wxHtmlListBox::SetHTMLBackgroundImage(const wxBitmap& WXUNUSED(bmpBg)) | |
485 | { | |
486 | // nothing to do | |
487 | } | |
488 | ||
489 | void wxHtmlListBox::SetHTMLStatusText(const wxString& WXUNUSED(text)) | |
490 | { | |
491 | // nothing to do | |
492 | } | |
493 | ||
88a1b648 VS |
494 | wxCursor wxHtmlListBox::GetHTMLCursor(HTMLCursor type) const |
495 | { | |
496 | // we don't want to show text selection cursor in listboxes | |
497 | if (type == HTMLCursor_Text) | |
498 | return wxHtmlWindow::GetDefaultHTMLCursor(HTMLCursor_Default); | |
499 | ||
500 | // in all other cases, use the same cursor as wxHtmlWindow: | |
501 | return wxHtmlWindow::GetDefaultHTMLCursor(type); | |
502 | } | |
503 | ||
bc55e31b VS |
504 | // ---------------------------------------------------------------------------- |
505 | // wxHtmlListBox handling of HTML links | |
506 | // ---------------------------------------------------------------------------- | |
aead8a4e | 507 | |
bc55e31b VS |
508 | wxPoint wxHtmlListBox::GetRootCellCoords(size_t n) const |
509 | { | |
510 | wxPoint pos(CELL_BORDER, CELL_BORDER); | |
511 | pos += GetMargins(); | |
e02c72fa | 512 | pos.y += GetRowsHeight(GetVisibleBegin(), n); |
bc55e31b VS |
513 | return pos; |
514 | } | |
515 | ||
516 | bool wxHtmlListBox::PhysicalCoordsToCell(wxPoint& pos, wxHtmlCell*& cell) const | |
517 | { | |
10368bff | 518 | int n = VirtualHitTest(pos.y); |
bc55e31b VS |
519 | if ( n == wxNOT_FOUND ) |
520 | return false; | |
521 | ||
522 | // convert mouse coordinates to coords relative to item's wxHtmlCell: | |
523 | pos -= GetRootCellCoords(n); | |
524 | ||
525 | CacheItem(n); | |
526 | cell = m_cache->Get(n); | |
527 | ||
528 | return true; | |
529 | } | |
530 | ||
531 | size_t wxHtmlListBox::GetItemForCell(const wxHtmlCell *cell) const | |
532 | { | |
9a83f860 | 533 | wxCHECK_MSG( cell, 0, wxT("no cell") ); |
bc55e31b VS |
534 | |
535 | cell = cell->GetRootCell(); | |
536 | ||
9a83f860 | 537 | wxCHECK_MSG( cell, 0, wxT("no root cell") ); |
bc55e31b VS |
538 | |
539 | // the cell's ID contains item index, see CacheItem(): | |
540 | unsigned long n; | |
541 | if ( !cell->GetId().ToULong(&n) ) | |
542 | { | |
9a83f860 | 543 | wxFAIL_MSG( wxT("unexpected root cell's ID") ); |
bc55e31b VS |
544 | return 0; |
545 | } | |
546 | ||
547 | return n; | |
548 | } | |
549 | ||
550 | wxPoint | |
551 | wxHtmlListBox::CellCoordsToPhysical(const wxPoint& pos, wxHtmlCell *cell) const | |
552 | { | |
553 | return pos + GetRootCellCoords(GetItemForCell(cell)); | |
554 | } | |
555 | ||
556 | void wxHtmlListBox::OnInternalIdle() | |
557 | { | |
558 | wxVListBox::OnInternalIdle(); | |
559 | ||
560 | if ( wxHtmlWindowMouseHelper::DidMouseMove() ) | |
561 | { | |
562 | wxPoint pos = ScreenToClient(wxGetMousePosition()); | |
563 | wxHtmlCell *cell; | |
564 | ||
565 | if ( !PhysicalCoordsToCell(pos, cell) ) | |
566 | return; | |
567 | ||
568 | wxHtmlWindowMouseHelper::HandleIdle(cell, pos); | |
569 | } | |
570 | } | |
571 | ||
572 | void wxHtmlListBox::OnMouseMove(wxMouseEvent& event) | |
573 | { | |
574 | wxHtmlWindowMouseHelper::HandleMouseMoved(); | |
575 | event.Skip(); | |
576 | } | |
577 | ||
578 | void wxHtmlListBox::OnLeftDown(wxMouseEvent& event) | |
579 | { | |
580 | wxPoint pos = event.GetPosition(); | |
581 | wxHtmlCell *cell; | |
582 | ||
583 | if ( !PhysicalCoordsToCell(pos, cell) ) | |
584 | { | |
585 | event.Skip(); | |
586 | return; | |
587 | } | |
588 | ||
589 | if ( !wxHtmlWindowMouseHelper::HandleMouseClick(cell, pos, event) ) | |
590 | { | |
591 | // no link was clicked, so let the listbox code handle the click (e.g. | |
592 | // by selecting another item in the list): | |
593 | event.Skip(); | |
594 | } | |
595 | } | |
596 | ||
9ebb7cad VZ |
597 | |
598 | // ---------------------------------------------------------------------------- | |
599 | // wxSimpleHtmlListBox | |
600 | // ---------------------------------------------------------------------------- | |
601 | ||
5cef2f65 RD |
602 | IMPLEMENT_ABSTRACT_CLASS(wxSimpleHtmlListBox, wxHtmlListBox) |
603 | ||
604 | ||
9ebb7cad VZ |
605 | bool wxSimpleHtmlListBox::Create(wxWindow *parent, wxWindowID id, |
606 | const wxPoint& pos, | |
607 | const wxSize& size, | |
608 | int n, const wxString choices[], | |
609 | long style, | |
610 | const wxValidator& validator, | |
611 | const wxString& name) | |
612 | { | |
613 | if (!wxHtmlListBox::Create(parent, id, pos, size, style, name)) | |
614 | return false; | |
615 | ||
9a9b5822 | 616 | #if wxUSE_VALIDATORS |
9ebb7cad | 617 | SetValidator(validator); |
9a9b5822 | 618 | #endif |
a236aa20 VZ |
619 | |
620 | Append(n, choices); | |
9ebb7cad VZ |
621 | |
622 | return true; | |
623 | } | |
624 | ||
625 | bool wxSimpleHtmlListBox::Create(wxWindow *parent, wxWindowID id, | |
a236aa20 VZ |
626 | const wxPoint& pos, |
627 | const wxSize& size, | |
628 | const wxArrayString& choices, | |
629 | long style, | |
630 | const wxValidator& validator, | |
631 | const wxString& name) | |
9ebb7cad VZ |
632 | { |
633 | if (!wxHtmlListBox::Create(parent, id, pos, size, style, name)) | |
634 | return false; | |
635 | ||
9a9b5822 | 636 | #if wxUSE_VALIDATORS |
9ebb7cad | 637 | SetValidator(validator); |
9a9b5822 | 638 | #endif |
a236aa20 | 639 | |
9ebb7cad VZ |
640 | Append(choices); |
641 | ||
642 | return true; | |
643 | } | |
644 | ||
645 | wxSimpleHtmlListBox::~wxSimpleHtmlListBox() | |
646 | { | |
a236aa20 | 647 | wxItemContainer::Clear(); |
9ebb7cad VZ |
648 | } |
649 | ||
a236aa20 | 650 | void wxSimpleHtmlListBox::DoClear() |
9ebb7cad | 651 | { |
a236aa20 VZ |
652 | wxASSERT(m_items.GetCount() == m_HTMLclientData.GetCount()); |
653 | ||
9ebb7cad | 654 | m_items.Clear(); |
9d8f8138 | 655 | m_HTMLclientData.Clear(); |
a236aa20 | 656 | |
9ebb7cad VZ |
657 | UpdateCount(); |
658 | } | |
659 | ||
e52cd41e VZ |
660 | void wxSimpleHtmlListBox::Clear() |
661 | { | |
662 | DoClear(); | |
663 | } | |
664 | ||
a236aa20 | 665 | void wxSimpleHtmlListBox::DoDeleteOneItem(unsigned int n) |
9ebb7cad VZ |
666 | { |
667 | m_items.RemoveAt(n); | |
a236aa20 | 668 | |
9d8f8138 | 669 | m_HTMLclientData.RemoveAt(n); |
9ebb7cad | 670 | |
9ebb7cad VZ |
671 | UpdateCount(); |
672 | } | |
673 | ||
a236aa20 VZ |
674 | int wxSimpleHtmlListBox::DoInsertItems(const wxArrayStringsAdapter& items, |
675 | unsigned int pos, | |
676 | void **clientData, | |
677 | wxClientDataType type) | |
9ebb7cad | 678 | { |
a236aa20 VZ |
679 | const unsigned int count = items.GetCount(); |
680 | ||
681 | m_items.Insert(wxEmptyString, pos, count); | |
682 | m_HTMLclientData.Insert(NULL, pos, count); | |
683 | ||
684 | for ( unsigned int i = 0; i < count; ++i, ++pos ) | |
685 | { | |
686 | m_items[pos] = items[i]; | |
687 | AssignNewItemClientData(pos, clientData, i, type); | |
688 | } | |
9ebb7cad | 689 | |
9ebb7cad | 690 | UpdateCount(); |
a236aa20 | 691 | |
7612febb | 692 | return pos - 1; |
9ebb7cad VZ |
693 | } |
694 | ||
695 | void wxSimpleHtmlListBox::SetString(unsigned int n, const wxString& s) | |
696 | { | |
697 | wxCHECK_RET( IsValid(n), | |
698 | wxT("invalid index in wxSimpleHtmlListBox::SetString") ); | |
699 | ||
a236aa20 | 700 | m_items[n]=s; |
e02c72fa | 701 | RefreshRow(n); |
9ebb7cad VZ |
702 | } |
703 | ||
704 | wxString wxSimpleHtmlListBox::GetString(unsigned int n) const | |
705 | { | |
706 | wxCHECK_MSG( IsValid(n), wxEmptyString, | |
707 | wxT("invalid index in wxSimpleHtmlListBox::GetString") ); | |
708 | ||
709 | return m_items[n]; | |
710 | } | |
711 | ||
712 | void wxSimpleHtmlListBox::UpdateCount() | |
713 | { | |
9d8f8138 | 714 | wxASSERT(m_items.GetCount() == m_HTMLclientData.GetCount()); |
9ebb7cad VZ |
715 | wxHtmlListBox::SetItemCount(m_items.GetCount()); |
716 | ||
717 | // very small optimization: if you need to add lot of items to | |
718 | // a wxSimpleHtmlListBox be sure to use the | |
719 | // wxSimpleHtmlListBox::Append(const wxArrayString&) method instead! | |
720 | if (!this->IsFrozen()) | |
721 | RefreshAll(); | |
722 | } | |
723 | ||
bc55e31b | 724 | #endif // wxUSE_HTML |