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"
36 // ============================================================================
38 // ============================================================================
40 wxListBoxBase
::~wxListBoxBase()
42 // this destructor is required for Darwin
45 // ----------------------------------------------------------------------------
47 // ----------------------------------------------------------------------------
49 bool wxListBoxBase
::SetStringSelection(const wxString
& s
, bool select
)
51 const int sel
= FindString(s
);
52 if ( sel
== wxNOT_FOUND
)
55 SetSelection(sel
, select
);
60 void wxListBoxBase
::DeselectAll(int itemToLeaveSelected
)
62 if ( HasMultipleSelection() )
64 wxArrayInt selections
;
65 GetSelections(selections
);
67 size_t count
= selections
.GetCount();
68 for ( size_t n
= 0; n
< count
; n
++ )
70 int item
= selections
[n
];
71 if ( item
!= itemToLeaveSelected
)
75 else // single selection
77 int sel
= GetSelection();
78 if ( sel
!= wxNOT_FOUND
&& sel
!= itemToLeaveSelected
)
85 void wxListBoxBase
::UpdateOldSelections()
87 if (HasFlag(wxLB_MULTIPLE
) || HasFlag(wxLB_EXTENDED
))
88 GetSelections( m_oldSelections
);
91 static void LBSendEvent( wxCommandEvent
&event
, wxListBoxBase
*listbox
, int item
)
94 event
.SetString( listbox
->GetString( item
) );
95 if ( listbox
->HasClientObjectData() )
96 event
.SetClientObject( listbox
->GetClientObject(item
) );
97 else if ( listbox
->HasClientUntypedData() )
98 event
.SetClientData( listbox
->GetClientData(item
) );
99 listbox
->HandleWindowEvent( event
);
102 void wxListBoxBase
::CalcAndSendEvent()
104 wxCommandEvent
event(wxEVT_COMMAND_LISTBOX_SELECTED
, GetId() );
105 event
.SetEventObject( this );
107 wxArrayInt selections
;
108 GetSelections( selections
);
110 if ((selections
.GetCount() == 0) && (m_oldSelections
.GetCount() == 0))
112 // nothing changed, just leave
116 if (selections
.GetCount() == m_oldSelections
.GetCount())
118 bool changed
= false;
120 for (idx
= 0; idx
< selections
.GetCount(); idx
++)
122 if (selections
[idx
] != m_oldSelections
[idx
])
129 // nothing changed, just leave
134 if (selections
.GetCount() == 0)
136 // indicate that this is a deselection
137 event
.SetExtraLong( 0 );
138 int item
= m_oldSelections
[0];
139 m_oldSelections
= selections
;
140 LBSendEvent( event
, this, item
);
145 // Now test if any new item is selected
146 bool any_new_selected
= false;
148 for (idx
= 0; idx
< selections
.GetCount(); idx
++)
150 item
= selections
[idx
];
151 if (m_oldSelections
.Index(item
) == wxNOT_FOUND
)
153 any_new_selected
= true;
158 if (any_new_selected
)
160 // indicate that this is a selection
161 event
.SetExtraLong( 1 );
162 m_oldSelections
= selections
;
163 LBSendEvent( event
, this, item
);
167 // Now test if any new item is deselected
168 bool any_new_deselected
= false;
169 for (idx
= 0; idx
< m_oldSelections
.GetCount(); idx
++)
171 item
= m_oldSelections
[idx
];
172 if (selections
.Index(item
) == wxNOT_FOUND
)
174 any_new_deselected
= true;
179 if (any_new_deselected
)
181 // indicate that this is a selection
182 event
.SetExtraLong( 0 );
183 m_oldSelections
= selections
;
184 LBSendEvent( event
, this, item
);
188 wxLogError( wxT("Wrong wxListBox selection") );
191 // ----------------------------------------------------------------------------
193 // ----------------------------------------------------------------------------
195 void wxListBoxBase
::Command(wxCommandEvent
& event
)
197 SetSelection(event
.GetInt(), event
.GetExtraLong() != 0);
198 (void)ProcessEvent(event
);
201 // ----------------------------------------------------------------------------
202 // SetFirstItem() and such
203 // ----------------------------------------------------------------------------
205 void wxListBoxBase
::SetFirstItem(const wxString
& s
)
207 int n
= FindString(s
);
209 wxCHECK_RET( n
!= wxNOT_FOUND
, wxT("invalid string in wxListBox::SetFirstItem") );
214 void wxListBoxBase
::AppendAndEnsureVisible(const wxString
& s
)
217 EnsureVisible(GetCount() - 1);
220 void wxListBoxBase
::EnsureVisible(int WXUNUSED(n
))
222 // the base class version does nothing (the only alternative would be to
223 // call SetFirstItem() but this is probably even more stupid)
226 #endif // wxUSE_LISTBOX