1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/lboxcmn.cpp
3 // Purpose: wxListBox class methods common to all platforms
4 // Author: Vadim Zeitlin
8 // Copyright: (c) wxWidgets team
9 // Licence: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
29 #include "wx/listbox.h"
32 #include "wx/dynarray.h"
33 #include "wx/arrstr.h"
38 // ============================================================================
40 // ============================================================================
42 wxListBoxBase::~wxListBoxBase()
44 // this destructor is required for Darwin
47 // ----------------------------------------------------------------------------
49 // ----------------------------------------------------------------------------
51 bool wxListBoxBase::SetStringSelection(const wxString
& s
, bool select
)
53 const int sel
= FindString(s
);
54 if ( sel
== wxNOT_FOUND
)
57 SetSelection(sel
, select
);
62 void wxListBoxBase::DeselectAll(int itemToLeaveSelected
)
64 if ( HasMultipleSelection() )
66 wxArrayInt selections
;
67 GetSelections(selections
);
69 size_t count
= selections
.GetCount();
70 for ( size_t n
= 0; n
< count
; n
++ )
72 int item
= selections
[n
];
73 if ( item
!= itemToLeaveSelected
)
77 else // single selection
79 int sel
= GetSelection();
80 if ( sel
!= wxNOT_FOUND
&& sel
!= itemToLeaveSelected
)
87 void wxListBoxBase::UpdateOldSelections()
89 if (HasFlag(wxLB_MULTIPLE
) || HasFlag(wxLB_EXTENDED
))
90 GetSelections( m_oldSelections
);
93 bool wxListBoxBase::SendEvent(wxEventType evtType
, int item
, bool selected
)
95 wxCommandEvent
event(evtType
, GetId());
96 event
.SetEventObject(this);
99 event
.SetString(GetString(item
));
100 event
.SetExtraLong(selected
);
102 if ( HasClientObjectData() )
103 event
.SetClientObject(GetClientObject(item
));
104 else if ( HasClientUntypedData() )
105 event
.SetClientData(GetClientData(item
));
107 return HandleWindowEvent(event
);
110 bool wxListBoxBase::CalcAndSendEvent()
112 wxArrayInt selections
;
113 GetSelections(selections
);
114 bool selected
= true;
116 if ( selections
.empty() && m_oldSelections
.empty() )
118 // nothing changed, just leave
122 const size_t countSel
= selections
.size(),
123 countSelOld
= m_oldSelections
.size();
124 if ( countSel
== countSelOld
)
126 bool changed
= false;
127 for ( size_t idx
= 0; idx
< countSel
; idx
++ )
129 if (selections
[idx
] != m_oldSelections
[idx
])
136 // nothing changed, just leave
141 int item
= wxNOT_FOUND
;
142 if ( selections
.empty() )
145 item
= m_oldSelections
[0];
147 else // we [still] have some selections
149 // Now test if any new item is selected
150 bool any_new_selected
= false;
151 for ( size_t idx
= 0; idx
< countSel
; idx
++ )
153 item
= selections
[idx
];
154 if ( m_oldSelections
.Index(item
) == wxNOT_FOUND
)
156 any_new_selected
= true;
161 if ( !any_new_selected
)
163 // No new items selected, now test if any new item is deselected
164 bool any_new_deselected
= false;
165 for ( size_t idx
= 0; idx
< countSelOld
; idx
++ )
167 item
= m_oldSelections
[idx
];
168 if ( selections
.Index(item
) == wxNOT_FOUND
)
170 any_new_deselected
= true;
175 if ( any_new_deselected
)
177 // indicate that this is a selection
182 item
= wxNOT_FOUND
; // this should be impossible
187 wxASSERT_MSG( item
!= wxNOT_FOUND
,
188 "Logic error in wxListBox selection event generation code" );
190 m_oldSelections
= selections
;
192 return SendEvent(wxEVT_COMMAND_LISTBOX_SELECTED
, item
, selected
);
195 // ----------------------------------------------------------------------------
197 // ----------------------------------------------------------------------------
199 void wxListBoxBase::Command(wxCommandEvent
& event
)
201 SetSelection(event
.GetInt(), event
.GetExtraLong() != 0);
202 (void)GetEventHandler()->ProcessEvent(event
);
205 // ----------------------------------------------------------------------------
206 // SetFirstItem() and such
207 // ----------------------------------------------------------------------------
209 void wxListBoxBase::SetFirstItem(const wxString
& s
)
211 int n
= FindString(s
);
213 wxCHECK_RET( n
!= wxNOT_FOUND
, wxT("invalid string in wxListBox::SetFirstItem") );
218 void wxListBoxBase::AppendAndEnsureVisible(const wxString
& s
)
221 EnsureVisible(GetCount() - 1);
224 void wxListBoxBase::EnsureVisible(int WXUNUSED(n
))
226 // the base class version does nothing (the only alternative would be to
227 // call SetFirstItem() but this is probably even more stupid)
230 #endif // wxUSE_LISTBOX