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