]>
Commit | Line | Data |
---|---|---|
2ee3ee1b | 1 | /////////////////////////////////////////////////////////////////////////////// |
aa61d352 | 2 | // Name: src/common/lboxcmn.cpp |
2ee3ee1b VZ |
3 | // Purpose: wxListBox class methods common to all platforms |
4 | // Author: Vadim Zeitlin | |
5 | // Modified by: | |
6 | // Created: 22.10.99 | |
7 | // RCS-ID: $Id$ | |
77ffb593 | 8 | // Copyright: (c) wxWidgets team |
65571936 | 9 | // Licence: wxWindows licence |
2ee3ee1b VZ |
10 | /////////////////////////////////////////////////////////////////////////////// |
11 | ||
12 | // ============================================================================ | |
13 | // declarations | |
14 | // ============================================================================ | |
15 | ||
16 | // ---------------------------------------------------------------------------- | |
17 | // headers | |
18 | // ---------------------------------------------------------------------------- | |
19 | ||
2ee3ee1b VZ |
20 | // For compilers that support precompilation, includes "wx.h". |
21 | #include "wx/wxprec.h" | |
22 | ||
23 | #ifdef __BORLANDC__ | |
24 | #pragma hdrstop | |
25 | #endif | |
26 | ||
1e6feb95 VZ |
27 | #if wxUSE_LISTBOX |
28 | ||
2a673eb1 WS |
29 | #include "wx/listbox.h" |
30 | ||
2ee3ee1b | 31 | #ifndef WX_PRECOMP |
ed39ff57 | 32 | #include "wx/dynarray.h" |
a9711a4d | 33 | #include "wx/arrstr.h" |
2ee3ee1b VZ |
34 | #endif |
35 | ||
22892dea RR |
36 | #include "wx/log.h" |
37 | ||
2ee3ee1b VZ |
38 | // ============================================================================ |
39 | // implementation | |
40 | // ============================================================================ | |
41 | ||
799ea011 GD |
42 | wxListBoxBase::~wxListBoxBase() |
43 | { | |
44 | // this destructor is required for Darwin | |
45 | } | |
46 | ||
2ee3ee1b VZ |
47 | // ---------------------------------------------------------------------------- |
48 | // selection | |
49 | // ---------------------------------------------------------------------------- | |
50 | ||
2ee3ee1b VZ |
51 | bool wxListBoxBase::SetStringSelection(const wxString& s, bool select) |
52 | { | |
95668975 VZ |
53 | const int sel = FindString(s); |
54 | if ( sel == wxNOT_FOUND ) | |
55 | return false; | |
2ee3ee1b VZ |
56 | |
57 | SetSelection(sel, select); | |
58 | ||
f644b28c | 59 | return true; |
2ee3ee1b VZ |
60 | } |
61 | ||
1e6feb95 VZ |
62 | void wxListBoxBase::DeselectAll(int itemToLeaveSelected) |
63 | { | |
64 | if ( HasMultipleSelection() ) | |
65 | { | |
66 | wxArrayInt selections; | |
67 | GetSelections(selections); | |
68 | ||
69 | size_t count = selections.GetCount(); | |
70 | for ( size_t n = 0; n < count; n++ ) | |
71 | { | |
72 | int item = selections[n]; | |
73 | if ( item != itemToLeaveSelected ) | |
74 | Deselect(item); | |
75 | } | |
76 | } | |
77 | else // single selection | |
78 | { | |
79 | int sel = GetSelection(); | |
f644b28c | 80 | if ( sel != wxNOT_FOUND && sel != itemToLeaveSelected ) |
1e6feb95 VZ |
81 | { |
82 | Deselect(sel); | |
83 | } | |
84 | } | |
85 | } | |
86 | ||
05d790f8 RR |
87 | void wxListBoxBase::UpdateOldSelections() |
88 | { | |
89 | if (HasFlag(wxLB_MULTIPLE) || HasFlag(wxLB_EXTENDED)) | |
90 | GetSelections( m_oldSelections ); | |
91 | } | |
92 | ||
93 | static void LBSendEvent( wxCommandEvent &event, wxListBoxBase *listbox, int item ) | |
94 | { | |
95 | event.SetInt( item ); | |
96 | event.SetString( listbox->GetString( item ) ); | |
97 | if ( listbox->HasClientObjectData() ) | |
98 | event.SetClientObject( listbox->GetClientObject(item) ); | |
99 | else if ( listbox->HasClientUntypedData() ) | |
100 | event.SetClientData( listbox->GetClientData(item) ); | |
101 | listbox->HandleWindowEvent( event ); | |
102 | } | |
103 | ||
104 | void wxListBoxBase::CalcAndSendEvent() | |
105 | { | |
106 | wxCommandEvent event(wxEVT_COMMAND_LISTBOX_SELECTED, GetId() ); | |
107 | event.SetEventObject( this ); | |
108 | ||
109 | wxArrayInt selections; | |
110 | GetSelections( selections ); | |
111 | ||
112 | if ((selections.GetCount() == 0) && (m_oldSelections.GetCount() == 0)) | |
113 | { | |
114 | // nothing changed, just leave | |
115 | return; | |
116 | } | |
117 | ||
118 | if (selections.GetCount() == m_oldSelections.GetCount()) | |
119 | { | |
120 | bool changed = false; | |
121 | size_t idx; | |
122 | for (idx = 0; idx < selections.GetCount(); idx++) | |
123 | { | |
124 | if (selections[idx] != m_oldSelections[idx]) | |
125 | { | |
126 | changed = true; | |
127 | break; | |
128 | } | |
129 | } | |
130 | ||
131 | // nothing changed, just leave | |
132 | if (!changed) | |
133 | return; | |
134 | } | |
135 | ||
136 | if (selections.GetCount() == 0) | |
137 | { | |
138 | // indicate that this is a deselection | |
139 | event.SetExtraLong( 0 ); | |
140 | int item = m_oldSelections[0]; | |
141 | m_oldSelections = selections; | |
142 | LBSendEvent( event, this, item ); | |
143 | return; | |
144 | } | |
145 | ||
146 | int item; | |
147 | // Now test if any new item is selected | |
148 | bool any_new_selected = false; | |
149 | size_t idx; | |
150 | for (idx = 0; idx < selections.GetCount(); idx++) | |
151 | { | |
152 | item = selections[idx]; | |
153 | if (m_oldSelections.Index(item) == wxNOT_FOUND) | |
154 | { | |
155 | any_new_selected = true; | |
156 | break; | |
157 | } | |
158 | } | |
159 | ||
160 | if (any_new_selected) | |
161 | { | |
162 | // indicate that this is a selection | |
163 | event.SetExtraLong( 1 ); | |
164 | m_oldSelections = selections; | |
165 | LBSendEvent( event, this, item ); | |
166 | return; | |
167 | } | |
168 | ||
169 | // Now test if any new item is deselected | |
170 | bool any_new_deselected = false; | |
171 | for (idx = 0; idx < m_oldSelections.GetCount(); idx++) | |
172 | { | |
173 | item = m_oldSelections[idx]; | |
174 | if (selections.Index(item) == wxNOT_FOUND) | |
175 | { | |
176 | any_new_deselected = true; | |
177 | break; | |
178 | } | |
179 | } | |
180 | ||
181 | if (any_new_deselected) | |
182 | { | |
183 | // indicate that this is a selection | |
184 | event.SetExtraLong( 0 ); | |
185 | m_oldSelections = selections; | |
186 | LBSendEvent( event, this, item ); | |
187 | return; | |
188 | } | |
189 | ||
190 | wxLogError( wxT("Wrong wxListBox selection") ); | |
191 | } | |
192 | ||
2ee3ee1b | 193 | // ---------------------------------------------------------------------------- |
6c8a980f | 194 | // misc |
2ee3ee1b VZ |
195 | // ---------------------------------------------------------------------------- |
196 | ||
6c8a980f | 197 | void wxListBoxBase::Command(wxCommandEvent& event) |
2ee3ee1b | 198 | { |
687706f5 | 199 | SetSelection(event.GetInt(), event.GetExtraLong() != 0); |
6c8a980f | 200 | (void)ProcessEvent(event); |
2ee3ee1b VZ |
201 | } |
202 | ||
1e6feb95 VZ |
203 | // ---------------------------------------------------------------------------- |
204 | // SetFirstItem() and such | |
205 | // ---------------------------------------------------------------------------- | |
206 | ||
2ee3ee1b VZ |
207 | void wxListBoxBase::SetFirstItem(const wxString& s) |
208 | { | |
209 | int n = FindString(s); | |
210 | ||
f644b28c | 211 | wxCHECK_RET( n != wxNOT_FOUND, wxT("invalid string in wxListBox::SetFirstItem") ); |
2ee3ee1b VZ |
212 | |
213 | DoSetFirstItem(n); | |
214 | } | |
1e6feb95 VZ |
215 | |
216 | void wxListBoxBase::AppendAndEnsureVisible(const wxString& s) | |
217 | { | |
218 | Append(s); | |
219 | EnsureVisible(GetCount() - 1); | |
220 | } | |
221 | ||
222 | void wxListBoxBase::EnsureVisible(int WXUNUSED(n)) | |
223 | { | |
224 | // the base class version does nothing (the only alternative would be to | |
225 | // call SetFirstItem() but this is probably even more stupid) | |
226 | } | |
227 | ||
228 | #endif // wxUSE_LISTBOX |