]>
Commit | Line | Data |
---|---|---|
6c8a980f | 1 | /////////////////////////////////////////////////////////////////////////////// |
aa61d352 | 2 | // Name: src/common/ctrlsub.cpp |
1e6feb95 | 3 | // Purpose: wxItemContainer implementation |
6c8a980f VZ |
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 |
6c8a980f VZ |
10 | /////////////////////////////////////////////////////////////////////////////// |
11 | ||
12 | // ============================================================================ | |
13 | // declarations | |
14 | // ============================================================================ | |
15 | ||
16 | // ---------------------------------------------------------------------------- | |
17 | // headers | |
18 | // ---------------------------------------------------------------------------- | |
19 | ||
6c8a980f 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_CONTROLS |
28 | ||
6c8a980f VZ |
29 | #ifndef WX_PRECOMP |
30 | #include "wx/ctrlsub.h" | |
a9711a4d | 31 | #include "wx/arrstr.h" |
6c8a980f VZ |
32 | #endif |
33 | ||
1440bc7a VZ |
34 | IMPLEMENT_ABSTRACT_CLASS(wxControlWithItems, wxControl) |
35 | ||
6c8a980f | 36 | // ============================================================================ |
8ba7c771 | 37 | // wxItemContainerImmutable implementation |
6c8a980f VZ |
38 | // ============================================================================ |
39 | ||
8ba7c771 | 40 | wxItemContainerImmutable::~wxItemContainerImmutable() |
799ea011 GD |
41 | { |
42 | // this destructor is required for Darwin | |
43 | } | |
44 | ||
6c8a980f VZ |
45 | // ---------------------------------------------------------------------------- |
46 | // selection | |
47 | // ---------------------------------------------------------------------------- | |
48 | ||
8ba7c771 | 49 | wxString wxItemContainerImmutable::GetStringSelection() const |
6c8a980f VZ |
50 | { |
51 | wxString s; | |
aa61d352 | 52 | |
6c8a980f | 53 | int sel = GetSelection(); |
aa61d352 VZ |
54 | if ( sel != wxNOT_FOUND ) |
55 | s = GetString((unsigned int)sel); | |
6c8a980f VZ |
56 | |
57 | return s; | |
58 | } | |
59 | ||
8ba7c771 | 60 | bool wxItemContainerImmutable::SetStringSelection(const wxString& s) |
64fa6f16 VZ |
61 | { |
62 | const int sel = FindString(s); | |
63 | if ( sel == wxNOT_FOUND ) | |
64 | return false; | |
65 | ||
c6179a84 | 66 | SetSelection(sel); |
64fa6f16 VZ |
67 | |
68 | return true; | |
69 | } | |
70 | ||
8ba7c771 | 71 | wxArrayString wxItemContainerImmutable::GetStrings() const |
b8d5be01 | 72 | { |
8ba7c771 VZ |
73 | wxArrayString result; |
74 | ||
aa61d352 | 75 | const unsigned int count = GetCount(); |
8ba7c771 | 76 | result.Alloc(count); |
aa61d352 | 77 | for ( unsigned int n = 0; n < count; n++ ) |
b8d5be01 | 78 | result.Add(GetString(n)); |
8ba7c771 VZ |
79 | |
80 | return result; | |
81 | } | |
82 | ||
83 | // ============================================================================ | |
84 | // wxItemContainer implementation | |
85 | // ============================================================================ | |
86 | ||
87 | wxItemContainer::~wxItemContainer() | |
88 | { | |
89 | // this destructor is required for Darwin | |
b8d5be01 SC |
90 | } |
91 | ||
0e0bc921 | 92 | // ---------------------------------------------------------------------------- |
a236aa20 | 93 | // deleting items |
0e0bc921 VZ |
94 | // ---------------------------------------------------------------------------- |
95 | ||
a236aa20 | 96 | void wxItemContainer::Clear() |
0e0bc921 | 97 | { |
a236aa20 | 98 | if ( HasClientObjectData() ) |
0e0bc921 | 99 | { |
a236aa20 VZ |
100 | const unsigned count = GetCount(); |
101 | for ( unsigned i = 0; i < count; ++i ) | |
102 | ResetItemClientObject(i); | |
0e0bc921 | 103 | } |
a236aa20 VZ |
104 | |
105 | m_clientDataItemsType = wxClientData_None; | |
106 | ||
107 | DoClear(); | |
0e0bc921 VZ |
108 | } |
109 | ||
a236aa20 | 110 | void wxItemContainer::Delete(unsigned int pos) |
243dbf1a | 111 | { |
a236aa20 | 112 | wxCHECK_RET( pos < GetCount(), _T("invalid index") ); |
243dbf1a | 113 | |
a236aa20 VZ |
114 | if ( HasClientObjectData() ) |
115 | ResetItemClientObject(pos); | |
116 | ||
117 | DoDeleteOneItem(pos); | |
118 | ||
119 | if ( IsEmpty() ) | |
120 | { | |
121 | m_clientDataItemsType = wxClientData_None; | |
122 | } | |
243dbf1a VZ |
123 | } |
124 | ||
a236aa20 VZ |
125 | // ---------------------------------------------------------------------------- |
126 | // | |
127 | // ---------------------------------------------------------------------------- | |
128 | ||
129 | int wxItemContainer::DoInsertItemsInLoop(const wxArrayStringsAdapter& items, | |
130 | unsigned int pos, | |
131 | void **clientData, | |
132 | wxClientDataType type) | |
243dbf1a | 133 | { |
a236aa20 VZ |
134 | int n = wxNOT_FOUND; |
135 | ||
136 | const unsigned int count = items.GetCount(); | |
137 | for ( unsigned int i = 0; i < count; ++i ) | |
138 | { | |
139 | n = DoInsertOneItem(items[i], pos++); | |
140 | if ( n == wxNOT_FOUND ) | |
141 | break; | |
142 | ||
143 | AssignNewItemClientData(n, clientData, i, type); | |
144 | } | |
243dbf1a VZ |
145 | |
146 | return n; | |
147 | } | |
148 | ||
a236aa20 VZ |
149 | int |
150 | wxItemContainer::DoInsertOneItem(const wxString& WXUNUSED(item), | |
151 | unsigned int WXUNUSED(pos)) | |
152 | { | |
153 | wxFAIL_MSG( _T("Must be overridden if DoInsertItemsInLoop() is used") ); | |
154 | ||
155 | return wxNOT_FOUND; | |
156 | } | |
157 | ||
158 | ||
6c8a980f VZ |
159 | // ---------------------------------------------------------------------------- |
160 | // client data | |
161 | // ---------------------------------------------------------------------------- | |
162 | ||
aa61d352 | 163 | void wxItemContainer::SetClientObject(unsigned int n, wxClientData *data) |
6c8a980f | 164 | { |
a236aa20 VZ |
165 | wxASSERT_MSG( m_clientDataItemsType == wxClientData_Object || |
166 | m_clientDataItemsType == wxClientData_None, | |
6c8a980f VZ |
167 | wxT("can't have both object and void client data") ); |
168 | ||
da6069e2 VZ |
169 | if ( m_clientDataItemsType == wxClientData_Object ) |
170 | { | |
a236aa20 VZ |
171 | wxClientData * clientDataOld |
172 | = wx_static_cast(wxClientData *, DoGetItemClientData(n)); | |
da6069e2 VZ |
173 | if ( clientDataOld ) |
174 | delete clientDataOld; | |
175 | } | |
176 | else // m_clientDataItemsType == wxClientData_None | |
177 | { | |
178 | // now we have object client data | |
a236aa20 VZ |
179 | DoInitItemClientData(); |
180 | ||
da6069e2 VZ |
181 | m_clientDataItemsType = wxClientData_Object; |
182 | } | |
6c8a980f | 183 | |
a236aa20 | 184 | DoSetItemClientData(n, data); |
6c8a980f VZ |
185 | } |
186 | ||
aa61d352 | 187 | wxClientData *wxItemContainer::GetClientObject(unsigned int n) const |
6c8a980f | 188 | { |
a236aa20 | 189 | wxCHECK_MSG( m_clientDataItemsType == wxClientData_Object, NULL, |
6c8a980f VZ |
190 | wxT("this window doesn't have object client data") ); |
191 | ||
a236aa20 | 192 | return wx_static_cast(wxClientData *, DoGetItemClientData(n)); |
6c8a980f VZ |
193 | } |
194 | ||
aa61d352 | 195 | void wxItemContainer::SetClientData(unsigned int n, void *data) |
6c8a980f | 196 | { |
a236aa20 VZ |
197 | if ( m_clientDataItemsType == wxClientData_None ) |
198 | { | |
199 | DoInitItemClientData(); | |
200 | m_clientDataItemsType = wxClientData_Void; | |
201 | } | |
202 | ||
203 | wxASSERT_MSG( m_clientDataItemsType == wxClientData_Void, | |
6c8a980f VZ |
204 | wxT("can't have both object and void client data") ); |
205 | ||
206 | DoSetItemClientData(n, data); | |
6c8a980f VZ |
207 | } |
208 | ||
aa61d352 | 209 | void *wxItemContainer::GetClientData(unsigned int n) const |
6c8a980f | 210 | { |
a236aa20 | 211 | wxCHECK_MSG( m_clientDataItemsType == wxClientData_Void, NULL, |
6c8a980f VZ |
212 | wxT("this window doesn't have void client data") ); |
213 | ||
214 | return DoGetItemClientData(n); | |
215 | } | |
216 | ||
a236aa20 VZ |
217 | void wxItemContainer::AssignNewItemClientData(unsigned int pos, |
218 | void **clientData, | |
219 | unsigned int n, | |
220 | wxClientDataType type) | |
221 | { | |
222 | switch ( type ) | |
223 | { | |
224 | case wxClientData_Object: | |
225 | SetClientObject | |
226 | ( | |
227 | pos, | |
228 | (wx_reinterpret_cast(wxClientData **, clientData))[n] | |
229 | ); | |
230 | break; | |
231 | ||
232 | case wxClientData_Void: | |
233 | SetClientData(pos, clientData[n]); | |
234 | break; | |
235 | ||
236 | default: | |
237 | wxFAIL_MSG( _T("unknown client data type") ); | |
238 | // fall through | |
239 | ||
240 | case wxClientData_None: | |
241 | // nothing to do | |
242 | break; | |
243 | } | |
244 | } | |
245 | ||
246 | void wxItemContainer::ResetItemClientObject(unsigned int n) | |
247 | { | |
248 | wxClientData * const data = GetClientObject(n); | |
249 | if ( data ) | |
250 | { | |
251 | delete data; | |
252 | DoSetItemClientData(n, NULL); | |
253 | } | |
254 | } | |
255 | ||
4c15a375 VZ |
256 | // ============================================================================ |
257 | // wxControlWithItems implementation | |
258 | // ============================================================================ | |
259 | ||
a236aa20 VZ |
260 | void |
261 | wxControlWithItemsBase::InitCommandEventWithItems(wxCommandEvent& event, int n) | |
4c15a375 VZ |
262 | { |
263 | InitCommandEvent(event); | |
264 | ||
265 | if ( n != wxNOT_FOUND ) | |
266 | { | |
267 | if ( HasClientObjectData() ) | |
268 | event.SetClientObject(GetClientObject(n)); | |
269 | else if ( HasClientUntypedData() ) | |
270 | event.SetClientData(GetClientData(n)); | |
271 | } | |
272 | } | |
273 | ||
1e6feb95 | 274 | #endif // wxUSE_CONTROLS |