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