]>
Commit | Line | Data |
---|---|---|
e0c6027b | 1 | /////////////////////////////////////////////////////////////////////////////// |
e19ac18a | 2 | // Name: src/generic/vlbox.cpp |
e0c6027b VZ |
3 | // Purpose: implementation of wxVListBox |
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> | |
0a53b9b8 | 9 | // License: wxWindows license |
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 | ||
179e085f RN |
27 | #if wxUSE_LISTBOX |
28 | ||
2a673eb1 WS |
29 | #include "wx/vlbox.h" |
30 | ||
e0c6027b VZ |
31 | #ifndef WX_PRECOMP |
32 | #include "wx/settings.h" | |
bb178b29 | 33 | #include "wx/dcclient.h" |
2a673eb1 | 34 | #include "wx/listbox.h" |
e0c6027b VZ |
35 | #endif //WX_PRECOMP |
36 | ||
0975a8a0 | 37 | #include "wx/dcbuffer.h" |
be465555 | 38 | #include "wx/selstore.h" |
04125489 | 39 | #include "wx/renderer.h" |
e0c6027b VZ |
40 | |
41 | // ---------------------------------------------------------------------------- | |
42 | // event tables | |
43 | // ---------------------------------------------------------------------------- | |
44 | ||
45 | BEGIN_EVENT_TABLE(wxVListBox, wxVScrolledWindow) | |
46 | EVT_PAINT(wxVListBox::OnPaint) | |
47 | ||
48 | EVT_KEY_DOWN(wxVListBox::OnKeyDown) | |
49 | EVT_LEFT_DOWN(wxVListBox::OnLeftDown) | |
50 | EVT_LEFT_DCLICK(wxVListBox::OnLeftDClick) | |
04125489 VZ |
51 | |
52 | EVT_SET_FOCUS(wxVListBox::OnSetOrKillFocus) | |
53 | EVT_KILL_FOCUS(wxVListBox::OnSetOrKillFocus) | |
858ad670 JS |
54 | |
55 | EVT_SIZE(wxVListBox::OnSize) | |
e0c6027b VZ |
56 | END_EVENT_TABLE() |
57 | ||
58 | // ============================================================================ | |
59 | // implementation | |
60 | // ============================================================================ | |
61 | ||
0c8392ca RD |
62 | IMPLEMENT_ABSTRACT_CLASS(wxVListBox, wxVScrolledWindow) |
63 | ||
e0c6027b VZ |
64 | // ---------------------------------------------------------------------------- |
65 | // wxVListBox creation | |
66 | // ---------------------------------------------------------------------------- | |
67 | ||
68 | void wxVListBox::Init() | |
69 | { | |
970b97a2 VZ |
70 | m_current = |
71 | m_anchor = wxNOT_FOUND; | |
be465555 | 72 | m_selStore = NULL; |
e0c6027b VZ |
73 | } |
74 | ||
75 | bool wxVListBox::Create(wxWindow *parent, | |
76 | wxWindowID id, | |
77 | const wxPoint& pos, | |
78 | const wxSize& size, | |
e0c6027b VZ |
79 | long style, |
80 | const wxString& name) | |
81 | { | |
a047aff2 | 82 | #ifdef __WXMSW__ |
9543d01c VZ |
83 | if ( (style & wxBORDER_MASK) == wxDEFAULT ) |
84 | style |= wxBORDER_THEME; | |
a047aff2 JS |
85 | #endif |
86 | ||
fef7400f | 87 | style |= wxWANTS_CHARS | wxFULL_REPAINT_ON_RESIZE; |
e0c6027b VZ |
88 | if ( !wxVScrolledWindow::Create(parent, id, pos, size, style, name) ) |
89 | return false; | |
90 | ||
be465555 VZ |
91 | if ( style & wxLB_MULTIPLE ) |
92 | m_selStore = new wxSelectionStore; | |
e0c6027b | 93 | |
dc596072 RD |
94 | // make sure the native widget has the right colour since we do |
95 | // transparent drawing by default | |
96 | SetBackgroundColour(GetBackgroundColour()); | |
04125489 VZ |
97 | |
98 | // leave m_colBgSel in an invalid state: it means for OnDrawBackground() | |
99 | // to use wxRendererNative instead of painting selection bg ourselves | |
100 | m_colBgSel = wxNullColour; | |
9a9b4940 | 101 | |
0975a8a0 JS |
102 | // flicker-free drawing requires this |
103 | SetBackgroundStyle(wxBG_STYLE_CUSTOM); | |
104 | ||
e0c6027b VZ |
105 | return true; |
106 | } | |
107 | ||
be465555 VZ |
108 | wxVListBox::~wxVListBox() |
109 | { | |
110 | delete m_selStore; | |
111 | } | |
112 | ||
113 | void wxVListBox::SetItemCount(size_t count) | |
114 | { | |
9543d01c VZ |
115 | // don't leave the current index invalid |
116 | if ( m_current != wxNOT_FOUND && (size_t)m_current >= count ) | |
117 | m_current = count - 1; // also ok when count == 0 as wxNOT_FOUND == -1 | |
118 | ||
be465555 VZ |
119 | if ( m_selStore ) |
120 | { | |
121 | // tell the selection store that our number of items has changed | |
122 | m_selStore->SetItemCount(count); | |
123 | } | |
124 | ||
f18eaf26 | 125 | SetRowCount(count); |
be465555 VZ |
126 | } |
127 | ||
e0c6027b VZ |
128 | // ---------------------------------------------------------------------------- |
129 | // selection handling | |
130 | // ---------------------------------------------------------------------------- | |
131 | ||
be465555 VZ |
132 | bool wxVListBox::IsSelected(size_t line) const |
133 | { | |
134 | return m_selStore ? m_selStore->IsSelected(line) : (int)line == m_current; | |
135 | } | |
136 | ||
137 | bool wxVListBox::Select(size_t item, bool select) | |
138 | { | |
139 | wxCHECK_MSG( m_selStore, false, | |
140 | _T("Select() may only be used with multiselection listbox") ); | |
141 | ||
142 | wxCHECK_MSG( item < GetItemCount(), false, | |
143 | _T("Select(): invalid item index") ); | |
144 | ||
145 | bool changed = m_selStore->SelectItem(item, select); | |
146 | if ( changed ) | |
147 | { | |
148 | // selection really changed | |
e02c72fa | 149 | RefreshRow(item); |
be465555 VZ |
150 | } |
151 | ||
152 | DoSetCurrent(item); | |
153 | ||
154 | return changed; | |
155 | } | |
156 | ||
157 | bool wxVListBox::SelectRange(size_t from, size_t to) | |
e0c6027b | 158 | { |
be465555 VZ |
159 | wxCHECK_MSG( m_selStore, false, |
160 | _T("SelectRange() may only be used with multiselection listbox") ); | |
161 | ||
162 | // make sure items are in correct order | |
163 | if ( from > to ) | |
164 | { | |
165 | size_t tmp = from; | |
166 | from = to; | |
167 | to = tmp; | |
168 | } | |
169 | ||
170 | wxCHECK_MSG( to < GetItemCount(), false, | |
171 | _T("SelectRange(): invalid item index") ); | |
172 | ||
173 | wxArrayInt changed; | |
174 | if ( !m_selStore->SelectRange(from, to, true, &changed) ) | |
175 | { | |
176 | // too many items have changed, we didn't record them in changed array | |
177 | // so we have no choice but to refresh everything between from and to | |
e02c72fa | 178 | RefreshRows(from, to); |
be465555 VZ |
179 | } |
180 | else // we've got the indices of the changed items | |
181 | { | |
182 | const size_t count = changed.GetCount(); | |
183 | if ( !count ) | |
184 | { | |
185 | // nothing changed | |
186 | return false; | |
187 | } | |
188 | ||
189 | // refresh just the lines which have really changed | |
190 | for ( size_t n = 0; n < count; n++ ) | |
191 | { | |
e02c72fa | 192 | RefreshRow(changed[n]); |
be465555 VZ |
193 | } |
194 | } | |
195 | ||
196 | // something changed | |
197 | return true; | |
198 | } | |
199 | ||
200 | bool wxVListBox::DoSelectAll(bool select) | |
201 | { | |
202 | wxCHECK_MSG( m_selStore, false, | |
203 | _T("SelectAll may only be used with multiselection listbox") ); | |
204 | ||
205 | size_t count = GetItemCount(); | |
206 | if ( count ) | |
207 | { | |
208 | wxArrayInt changed; | |
209 | if ( !m_selStore->SelectRange(0, count - 1, select) || | |
210 | !changed.IsEmpty() ) | |
211 | { | |
212 | Refresh(); | |
213 | ||
214 | // something changed | |
215 | return true; | |
216 | } | |
217 | } | |
218 | ||
219 | return false; | |
220 | } | |
221 | ||
222 | bool wxVListBox::DoSetCurrent(int current) | |
223 | { | |
6c9210a7 VZ |
224 | wxASSERT_MSG( current == wxNOT_FOUND || |
225 | (current >= 0 && (size_t)current < GetItemCount()), | |
226 | _T("wxVListBox::DoSetCurrent(): invalid item index") ); | |
227 | ||
be465555 | 228 | if ( current == m_current ) |
e0c6027b VZ |
229 | { |
230 | // nothing to do | |
be465555 | 231 | return false; |
e0c6027b VZ |
232 | } |
233 | ||
be465555 | 234 | if ( m_current != wxNOT_FOUND ) |
e02c72fa | 235 | RefreshRow(m_current); |
e0c6027b | 236 | |
be465555 | 237 | m_current = current; |
e0c6027b | 238 | |
be465555 | 239 | if ( m_current != wxNOT_FOUND ) |
e0c6027b VZ |
240 | { |
241 | // if the line is not visible at all, we scroll it into view but we | |
242 | // don't need to refresh it -- it will be redrawn anyhow | |
be465555 | 243 | if ( !IsVisible(m_current) ) |
e0c6027b | 244 | { |
e02c72fa | 245 | ScrollToRow(m_current); |
e0c6027b VZ |
246 | } |
247 | else // line is at least partly visible | |
248 | { | |
249 | // it is, indeed, only partly visible, so scroll it into view to | |
250 | // make it entirely visible | |
e02c72fa VZ |
251 | while ( (size_t)m_current + 1 == GetVisibleRowsEnd() && |
252 | ScrollToRow(GetVisibleBegin() + 1) ) ; | |
e0c6027b VZ |
253 | |
254 | // but in any case refresh it as even if it was only partly visible | |
255 | // before we need to redraw it entirely as its background changed | |
e02c72fa | 256 | RefreshRow(m_current); |
e0c6027b | 257 | } |
be465555 | 258 | } |
e0c6027b | 259 | |
be465555 VZ |
260 | return true; |
261 | } | |
e0c6027b | 262 | |
be465555 VZ |
263 | void wxVListBox::SendSelectedEvent() |
264 | { | |
265 | wxASSERT_MSG( m_current != wxNOT_FOUND, | |
266 | _T("SendSelectedEvent() shouldn't be called") ); | |
267 | ||
268 | wxCommandEvent event(wxEVT_COMMAND_LISTBOX_SELECTED, GetId()); | |
269 | event.SetEventObject(this); | |
687706f5 | 270 | event.SetInt(m_current); |
be465555 VZ |
271 | |
272 | (void)GetEventHandler()->ProcessEvent(event); | |
273 | } | |
274 | ||
275 | void wxVListBox::SetSelection(int selection) | |
276 | { | |
6c9210a7 VZ |
277 | wxCHECK_RET( selection == wxNOT_FOUND || |
278 | (selection >= 0 && (size_t)selection < GetItemCount()), | |
279 | _T("wxVListBox::SetSelection(): invalid item index") ); | |
280 | ||
de5d3a20 VZ |
281 | if ( HasMultipleSelection() ) |
282 | { | |
ad679137 VZ |
283 | if (selection != wxNOT_FOUND) |
284 | Select(selection); | |
285 | else | |
286 | DeselectAll(); | |
de5d3a20 VZ |
287 | m_anchor = selection; |
288 | } | |
be465555 VZ |
289 | |
290 | DoSetCurrent(selection); | |
291 | } | |
292 | ||
293 | size_t wxVListBox::GetSelectedCount() const | |
294 | { | |
295 | return m_selStore ? m_selStore->GetSelectedCount() | |
296 | : m_current == wxNOT_FOUND ? 0 : 1; | |
297 | } | |
298 | ||
299 | int wxVListBox::GetFirstSelected(unsigned long& cookie) const | |
300 | { | |
301 | cookie = 0; | |
302 | ||
303 | return GetNextSelected(cookie); | |
304 | } | |
305 | ||
306 | int wxVListBox::GetNextSelected(unsigned long& cookie) const | |
307 | { | |
308 | wxCHECK_MSG( m_selStore, wxNOT_FOUND, | |
309 | _T("GetFirst/NextSelected() may only be used with multiselection listboxes") ); | |
310 | ||
311 | while ( cookie < GetItemCount() ) | |
312 | { | |
313 | if ( IsSelected(cookie++) ) | |
314 | return cookie - 1; | |
e0c6027b | 315 | } |
be465555 VZ |
316 | |
317 | return wxNOT_FOUND; | |
e0c6027b VZ |
318 | } |
319 | ||
04125489 VZ |
320 | void wxVListBox::RefreshSelected() |
321 | { | |
322 | // only refresh those items which are currently visible and selected: | |
323 | for ( size_t n = GetVisibleBegin(), end = GetVisibleEnd(); n < end; n++ ) | |
324 | { | |
325 | if ( IsSelected(n) ) | |
f18eaf26 | 326 | RefreshRow(n); |
04125489 VZ |
327 | } |
328 | } | |
329 | ||
293b15f7 VZ |
330 | wxRect wxVListBox::GetItemRect(size_t n) const |
331 | { | |
332 | wxRect itemrect; | |
333 | ||
334 | // check that this item is visible | |
335 | const size_t lineMax = GetVisibleEnd(); | |
336 | if ( n >= lineMax ) | |
337 | return itemrect; | |
338 | size_t line = GetVisibleBegin(); | |
339 | if ( n < line ) | |
340 | return itemrect; | |
341 | ||
342 | while ( line <= n ) | |
343 | { | |
344 | itemrect.y += itemrect.height; | |
345 | itemrect.height = OnGetRowHeight(line); | |
346 | ||
347 | line++; | |
348 | } | |
349 | ||
350 | itemrect.width = GetClientSize().x; | |
351 | ||
352 | return itemrect; | |
353 | } | |
354 | ||
e0c6027b | 355 | // ---------------------------------------------------------------------------- |
9a9b4940 | 356 | // wxVListBox appearance parameters |
e0c6027b VZ |
357 | // ---------------------------------------------------------------------------- |
358 | ||
359 | void wxVListBox::SetMargins(const wxPoint& pt) | |
360 | { | |
361 | if ( pt != m_ptMargins ) | |
362 | { | |
363 | m_ptMargins = pt; | |
364 | ||
365 | Refresh(); | |
366 | } | |
367 | } | |
368 | ||
9a9b4940 VZ |
369 | void wxVListBox::SetSelectionBackground(const wxColour& col) |
370 | { | |
371 | m_colBgSel = col; | |
372 | } | |
373 | ||
374 | // ---------------------------------------------------------------------------- | |
375 | // wxVListBox painting | |
376 | // ---------------------------------------------------------------------------- | |
377 | ||
e02c72fa | 378 | wxCoord wxVListBox::OnGetRowHeight(size_t line) const |
e0c6027b VZ |
379 | { |
380 | return OnMeasureItem(line) + 2*m_ptMargins.y; | |
381 | } | |
382 | ||
383 | void wxVListBox::OnDrawSeparator(wxDC& WXUNUSED(dc), | |
384 | wxRect& WXUNUSED(rect), | |
385 | size_t WXUNUSED(n)) const | |
386 | { | |
387 | } | |
388 | ||
c848185a VZ |
389 | bool |
390 | wxVListBox::DoDrawSolidBackground(const wxColour& col, | |
391 | wxDC& dc, | |
392 | const wxRect& rect, | |
393 | size_t n) const | |
27d0dcd0 | 394 | { |
c848185a VZ |
395 | if ( !col.IsOk() ) |
396 | return false; | |
397 | ||
398 | // we need to render selected and current items differently | |
399 | const bool isSelected = IsSelected(n), | |
400 | isCurrent = IsCurrent(n); | |
401 | if ( isSelected || isCurrent ) | |
27d0dcd0 | 402 | { |
c848185a | 403 | if ( isSelected ) |
27d0dcd0 | 404 | { |
04ee05f9 | 405 | dc.SetBrush(wxBrush(col, wxBRUSHSTYLE_SOLID)); |
27d0dcd0 | 406 | } |
c848185a VZ |
407 | else // !selected |
408 | { | |
409 | dc.SetBrush(*wxTRANSPARENT_BRUSH); | |
410 | } | |
411 | dc.SetPen(*(isCurrent ? wxBLACK_PEN : wxTRANSPARENT_PEN)); | |
412 | dc.DrawRectangle(rect); | |
04125489 | 413 | } |
c848185a VZ |
414 | //else: do nothing for the normal items |
415 | ||
416 | return true; | |
417 | } | |
418 | ||
419 | void wxVListBox::OnDrawBackground(wxDC& dc, const wxRect& rect, size_t n) const | |
420 | { | |
421 | // use wxRendererNative for more native look unless we use custom bg colour | |
422 | if ( !DoDrawSolidBackground(m_colBgSel, dc, rect, n) ) | |
04125489 VZ |
423 | { |
424 | int flags = 0; | |
425 | if ( IsSelected(n) ) | |
426 | flags |= wxCONTROL_SELECTED; | |
427 | if ( IsCurrent(n) ) | |
428 | flags |= wxCONTROL_CURRENT; | |
d836b8bc | 429 | if ( wxWindow::FindFocus() == wx_const_cast(wxVListBox*, this) ) |
04125489 VZ |
430 | flags |= wxCONTROL_FOCUSED; |
431 | ||
432 | wxRendererNative::Get().DrawItemSelectionRect( | |
433 | wx_const_cast(wxVListBox *, this), dc, rect, flags); | |
27d0dcd0 | 434 | } |
27d0dcd0 VZ |
435 | } |
436 | ||
437 | void wxVListBox::OnPaint(wxPaintEvent& WXUNUSED(event)) | |
e0c6027b | 438 | { |
0975a8a0 JS |
439 | wxSize clientSize = GetClientSize(); |
440 | ||
2e992e06 | 441 | wxAutoBufferedPaintDC dc(this); |
e0c6027b VZ |
442 | |
443 | // the update rectangle | |
444 | wxRect rectUpdate = GetUpdateClientRect(); | |
445 | ||
959b1a33 VZ |
446 | // fill it with background colour |
447 | dc.SetBackground(GetBackgroundColour()); | |
0975a8a0 JS |
448 | dc.Clear(); |
449 | ||
e0c6027b | 450 | // the bounding rectangle of the current line |
e02c72fa VZ |
451 | wxRect rectRow; |
452 | rectRow.width = clientSize.x; | |
e0c6027b VZ |
453 | |
454 | // iterate over all visible lines | |
b1b408af | 455 | const size_t lineMax = GetVisibleEnd(); |
e02c72fa | 456 | for ( size_t line = GetVisibleBegin(); line < lineMax; line++ ) |
e0c6027b | 457 | { |
e02c72fa | 458 | const wxCoord hRow = OnGetRowHeight(line); |
e0c6027b | 459 | |
e02c72fa | 460 | rectRow.height = hRow; |
e0c6027b VZ |
461 | |
462 | // and draw the ones which intersect the update rect | |
e02c72fa | 463 | if ( rectRow.Intersects(rectUpdate) ) |
e0c6027b VZ |
464 | { |
465 | // don't allow drawing outside of the lines rectangle | |
e02c72fa | 466 | wxDCClipper clip(dc, rectRow); |
e0c6027b | 467 | |
e02c72fa | 468 | wxRect rect = rectRow; |
27d0dcd0 VZ |
469 | OnDrawBackground(dc, rect, line); |
470 | ||
e0c6027b VZ |
471 | OnDrawSeparator(dc, rect, line); |
472 | ||
473 | rect.Deflate(m_ptMargins.x, m_ptMargins.y); | |
474 | OnDrawItem(dc, rect, line); | |
475 | } | |
476 | else // no intersection | |
477 | { | |
e02c72fa | 478 | if ( rectRow.GetTop() > rectUpdate.GetBottom() ) |
e0c6027b VZ |
479 | { |
480 | // we are already below the update rect, no need to continue | |
481 | // further | |
482 | break; | |
483 | } | |
484 | //else: the next line may intersect the update rect | |
485 | } | |
486 | ||
e02c72fa | 487 | rectRow.y += hRow; |
e0c6027b VZ |
488 | } |
489 | } | |
490 | ||
04125489 VZ |
491 | void wxVListBox::OnSetOrKillFocus(wxFocusEvent& WXUNUSED(event)) |
492 | { | |
493 | // we need to repaint the selection when we get the focus since | |
494 | // wxRendererNative in general draws the focused selection differently | |
495 | // from the unfocused selection (see OnDrawItem): | |
496 | RefreshSelected(); | |
497 | } | |
498 | ||
858ad670 JS |
499 | void wxVListBox::OnSize(wxSizeEvent& event) |
500 | { | |
501 | UpdateScrollbar(); | |
502 | event.Skip(); | |
503 | } | |
04125489 | 504 | |
be465555 VZ |
505 | // ============================================================================ |
506 | // wxVListBox keyboard/mouse handling | |
507 | // ============================================================================ | |
508 | ||
970b97a2 | 509 | void wxVListBox::DoHandleItemClick(int item, int flags) |
be465555 VZ |
510 | { |
511 | // has anything worth telling the client code about happened? | |
512 | bool notify = false; | |
513 | ||
514 | if ( HasMultipleSelection() ) | |
515 | { | |
516 | // select the iteem clicked? | |
517 | bool select = true; | |
518 | ||
519 | // NB: the keyboard interface we implement here corresponds to | |
520 | // wxLB_EXTENDED rather than wxLB_MULTIPLE but this one makes more | |
521 | // sense IMHO | |
970b97a2 | 522 | if ( flags & ItemClick_Shift ) |
be465555 VZ |
523 | { |
524 | if ( m_current != wxNOT_FOUND ) | |
525 | { | |
970b97a2 VZ |
526 | if ( m_anchor == wxNOT_FOUND ) |
527 | m_anchor = m_current; | |
528 | ||
be465555 VZ |
529 | select = false; |
530 | ||
970b97a2 VZ |
531 | // only the range from the selection anchor to new m_current |
532 | // must be selected | |
be465555 VZ |
533 | if ( DeselectAll() ) |
534 | notify = true; | |
535 | ||
970b97a2 | 536 | if ( SelectRange(m_anchor, item) ) |
be465555 VZ |
537 | notify = true; |
538 | } | |
539 | //else: treat it as ordinary click/keypress | |
540 | } | |
970b97a2 | 541 | else // Shift not pressed |
be465555 | 542 | { |
970b97a2 | 543 | m_anchor = item; |
be465555 | 544 | |
970b97a2 VZ |
545 | if ( flags & ItemClick_Ctrl ) |
546 | { | |
547 | select = false; | |
be465555 | 548 | |
970b97a2 VZ |
549 | if ( !(flags & ItemClick_Kbd) ) |
550 | { | |
551 | Toggle(item); | |
552 | ||
553 | // the status of the item has definitely changed | |
554 | notify = true; | |
555 | } | |
556 | //else: Ctrl-arrow pressed, don't change selection | |
557 | } | |
558 | //else: behave as in single selection case | |
be465555 | 559 | } |
be465555 VZ |
560 | |
561 | if ( select ) | |
562 | { | |
563 | // make the clicked item the only selection | |
564 | if ( DeselectAll() ) | |
565 | notify = true; | |
566 | ||
567 | if ( Select(item) ) | |
568 | notify = true; | |
569 | } | |
570 | } | |
571 | ||
572 | // in any case the item should become the current one | |
573 | if ( DoSetCurrent(item) ) | |
574 | { | |
575 | if ( !HasMultipleSelection() ) | |
576 | { | |
577 | // this has also changed the selection for single selection case | |
578 | notify = true; | |
579 | } | |
580 | } | |
581 | ||
582 | if ( notify ) | |
583 | { | |
584 | // notify the user about the selection change | |
585 | SendSelectedEvent(); | |
586 | } | |
587 | //else: nothing changed at all | |
588 | } | |
589 | ||
e0c6027b | 590 | // ---------------------------------------------------------------------------- |
be465555 | 591 | // keyboard handling |
e0c6027b VZ |
592 | // ---------------------------------------------------------------------------- |
593 | ||
594 | void wxVListBox::OnKeyDown(wxKeyEvent& event) | |
595 | { | |
970b97a2 VZ |
596 | // flags for DoHandleItemClick() |
597 | int flags = ItemClick_Kbd; | |
598 | ||
999836aa | 599 | int current; |
e0c6027b VZ |
600 | switch ( event.GetKeyCode() ) |
601 | { | |
602 | case WXK_HOME: | |
be465555 | 603 | current = 0; |
e0c6027b VZ |
604 | break; |
605 | ||
606 | case WXK_END: | |
e02c72fa | 607 | current = GetRowCount() - 1; |
e0c6027b VZ |
608 | break; |
609 | ||
610 | case WXK_DOWN: | |
e02c72fa | 611 | if ( m_current == (int)GetRowCount() - 1 ) |
e0c6027b VZ |
612 | return; |
613 | ||
be465555 | 614 | current = m_current + 1; |
e0c6027b VZ |
615 | break; |
616 | ||
617 | case WXK_UP: | |
be465555 | 618 | if ( m_current == wxNOT_FOUND ) |
e02c72fa | 619 | current = GetRowCount() - 1; |
be465555 VZ |
620 | else if ( m_current != 0 ) |
621 | current = m_current - 1; | |
622 | else // m_current == 0 | |
e0c6027b VZ |
623 | return; |
624 | break; | |
625 | ||
626 | case WXK_PAGEDOWN: | |
e0c6027b | 627 | PageDown(); |
e02c72fa | 628 | current = GetVisibleBegin(); |
e0c6027b VZ |
629 | break; |
630 | ||
631 | case WXK_PAGEUP: | |
e02c72fa | 632 | if ( m_current == (int)GetVisibleBegin() ) |
e0c6027b VZ |
633 | { |
634 | PageUp(); | |
635 | } | |
636 | ||
e02c72fa | 637 | current = GetVisibleBegin(); |
e0c6027b VZ |
638 | break; |
639 | ||
970b97a2 VZ |
640 | case WXK_SPACE: |
641 | // hack: pressing space should work like a mouse click rather than | |
642 | // like a keyboard arrow press, so trick DoHandleItemClick() in | |
643 | // thinking we were clicked | |
644 | flags &= ~ItemClick_Kbd; | |
645 | current = m_current; | |
646 | break; | |
647 | ||
80c700cb RD |
648 | #ifdef __WXMSW__ |
649 | case WXK_TAB: | |
650 | // Since we are using wxWANTS_CHARS we need to send navigation | |
651 | // events for the tabs on MSW | |
f029f1d1 | 652 | HandleAsNavigationKey(event); |
80c700cb RD |
653 | // fall through to default |
654 | #endif | |
e0c6027b VZ |
655 | default: |
656 | event.Skip(); | |
999836aa | 657 | current = 0; // just to silent the stupid compiler warnings |
8703bc01 | 658 | wxUnusedVar(current); |
e0c6027b VZ |
659 | return; |
660 | } | |
661 | ||
970b97a2 VZ |
662 | if ( event.ShiftDown() ) |
663 | flags |= ItemClick_Shift; | |
664 | if ( event.ControlDown() ) | |
665 | flags |= ItemClick_Ctrl; | |
666 | ||
667 | DoHandleItemClick(current, flags); | |
e0c6027b VZ |
668 | } |
669 | ||
670 | // ---------------------------------------------------------------------------- | |
671 | // wxVListBox mouse handling | |
672 | // ---------------------------------------------------------------------------- | |
673 | ||
674 | void wxVListBox::OnLeftDown(wxMouseEvent& event) | |
675 | { | |
c7778877 | 676 | SetFocus(); |
4e115ed2 | 677 | |
10368bff | 678 | int item = VirtualHitTest(event.GetPosition().y); |
e0c6027b | 679 | |
6c9210a7 VZ |
680 | if ( item != wxNOT_FOUND ) |
681 | { | |
970b97a2 VZ |
682 | int flags = 0; |
683 | if ( event.ShiftDown() ) | |
684 | flags |= ItemClick_Shift; | |
685 | ||
6c9210a7 VZ |
686 | // under Mac Apple-click is used in the same way as Ctrl-click |
687 | // elsewhere | |
be465555 | 688 | #ifdef __WXMAC__ |
970b97a2 | 689 | if ( event.MetaDown() ) |
be465555 | 690 | #else |
970b97a2 | 691 | if ( event.ControlDown() ) |
be465555 | 692 | #endif |
970b97a2 VZ |
693 | flags |= ItemClick_Ctrl; |
694 | ||
695 | DoHandleItemClick(item, flags); | |
6c9210a7 | 696 | } |
e0c6027b VZ |
697 | } |
698 | ||
4e115ed2 | 699 | void wxVListBox::OnLeftDClick(wxMouseEvent& eventMouse) |
e0c6027b | 700 | { |
10368bff | 701 | int item = VirtualHitTest(eventMouse.GetPosition().y); |
be465555 | 702 | if ( item != wxNOT_FOUND ) |
e0c6027b | 703 | { |
e0c6027b | 704 | |
0975a8a0 JS |
705 | // if item double-clicked was not yet selected, then treat |
706 | // this event as a left-click instead | |
707 | if ( item == m_current ) | |
708 | { | |
709 | wxCommandEvent event(wxEVT_COMMAND_LISTBOX_DOUBLECLICKED, GetId()); | |
710 | event.SetEventObject(this); | |
711 | event.SetInt(item); | |
712 | ||
713 | (void)GetEventHandler()->ProcessEvent(event); | |
714 | } | |
715 | else | |
716 | { | |
717 | OnLeftDown(eventMouse); | |
718 | } | |
e19ac18a | 719 | |
e0c6027b VZ |
720 | } |
721 | } | |
722 | ||
dc596072 RD |
723 | |
724 | // ---------------------------------------------------------------------------- | |
725 | // use the same default attributes as wxListBox | |
726 | // ---------------------------------------------------------------------------- | |
727 | ||
dc596072 RD |
728 | //static |
729 | wxVisualAttributes | |
730 | wxVListBox::GetClassDefaultAttributes(wxWindowVariant variant) | |
731 | { | |
732 | return wxListBox::GetClassDefaultAttributes(variant); | |
733 | } | |
179e085f | 734 | |
8b939bc0 | 735 | #endif |