]>
Commit | Line | Data |
---|---|---|
fd3f686c | 1 | //////////////////////////////////////////////////////////////////////////////// |
7ec69821 | 2 | // Name: src/common/list.cpp |
c801d85f KB |
3 | // Purpose: wxList implementation |
4 | // Author: Julian Smart | |
fd3f686c | 5 | // Modified by: VZ at 16/11/98: WX_DECLARE_LIST() and typesafe lists added |
c801d85f KB |
6 | // Created: 04/01/98 |
7 | // RCS-ID: $Id$ | |
55d99c7a | 8 | // Copyright: (c) Julian Smart |
65571936 | 9 | // Licence: wxWindows licence |
fd3f686c | 10 | //////////////////////////////////////////////////////////////////////////////// |
c801d85f | 11 | |
fd3f686c VZ |
12 | // ============================================================================= |
13 | // declarations | |
14 | // ============================================================================= | |
15 | ||
16 | // ----------------------------------------------------------------------------- | |
17 | // headers | |
18 | // ----------------------------------------------------------------------------- | |
7daab453 | 19 | |
c801d85f KB |
20 | // For compilers that support precompilation, includes "wx.h". |
21 | #include "wx/wxprec.h" | |
22 | ||
23 | #ifdef __BORLANDC__ | |
fd3f686c | 24 | #pragma hdrstop |
c801d85f KB |
25 | #endif |
26 | ||
fd3f686c VZ |
27 | #include <stdarg.h> |
28 | #include <stdlib.h> | |
29 | #include <string.h> | |
30 | ||
c801d85f | 31 | #ifndef WX_PRECOMP |
fd3f686c | 32 | #include "wx/list.h" |
c801d85f KB |
33 | #endif |
34 | ||
df5168c4 MB |
35 | #if !wxUSE_STL |
36 | ||
fd3f686c VZ |
37 | // ============================================================================= |
38 | // implementation | |
39 | // ============================================================================= | |
c801d85f | 40 | |
ff528365 VZ |
41 | // ----------------------------------------------------------------------------- |
42 | // wxListKey | |
43 | // ----------------------------------------------------------------------------- | |
8a2c6ef8 JS |
44 | wxListKey wxDefaultListKey; |
45 | ||
ff528365 VZ |
46 | bool wxListKey::operator==(wxListKeyValue value) const |
47 | { | |
48 | switch ( m_keyType ) | |
49 | { | |
50 | default: | |
223d09f6 | 51 | wxFAIL_MSG(wxT("bad key type.")); |
ff528365 VZ |
52 | // let compiler optimize the line above away in release build |
53 | // by not putting return here... | |
54 | ||
55 | case wxKEY_STRING: | |
81727065 | 56 | return *m_key.string == *value.string; |
ff528365 VZ |
57 | |
58 | case wxKEY_INTEGER: | |
59 | return m_key.integer == value.integer; | |
60 | } | |
77c5eefb | 61 | } |
ff528365 | 62 | |
fd3f686c VZ |
63 | // ----------------------------------------------------------------------------- |
64 | // wxNodeBase | |
65 | // ----------------------------------------------------------------------------- | |
c801d85f | 66 | |
fd3f686c VZ |
67 | wxNodeBase::wxNodeBase(wxListBase *list, |
68 | wxNodeBase *previous, wxNodeBase *next, | |
69 | void *data, const wxListKey& key) | |
c801d85f | 70 | { |
fd3f686c VZ |
71 | m_list = list; |
72 | m_data = data; | |
73 | m_previous = previous; | |
74 | m_next = next; | |
77c5eefb | 75 | |
fd3f686c VZ |
76 | switch ( key.GetKeyType() ) |
77 | { | |
78 | case wxKEY_NONE: | |
79 | break; | |
77c5eefb | 80 | |
fd3f686c VZ |
81 | case wxKEY_INTEGER: |
82 | m_key.integer = key.GetNumber(); | |
83 | break; | |
77c5eefb | 84 | |
fd3f686c VZ |
85 | case wxKEY_STRING: |
86 | // to be free()d later | |
81727065 | 87 | m_key.string = new wxString(key.GetString()); |
fd3f686c | 88 | break; |
77c5eefb | 89 | |
fd3f686c | 90 | default: |
223d09f6 | 91 | wxFAIL_MSG(wxT("invalid key type")); |
fd3f686c | 92 | } |
77c5eefb | 93 | |
fd3f686c VZ |
94 | if ( previous ) |
95 | previous->m_next = this; | |
77c5eefb | 96 | |
fd3f686c VZ |
97 | if ( next ) |
98 | next->m_previous = this; | |
99 | } | |
c801d85f | 100 | |
fd3f686c VZ |
101 | wxNodeBase::~wxNodeBase() |
102 | { | |
103 | // handle the case when we're being deleted from the list by the user (i.e. | |
104 | // not by the list itself from DeleteNode) - we must do it for | |
105 | // compatibility with old code | |
106 | if ( m_list != NULL ) | |
107 | { | |
09914df7 VZ |
108 | if ( m_list->m_keyType == wxKEY_STRING ) |
109 | { | |
81727065 | 110 | delete m_key.string; |
09914df7 VZ |
111 | } |
112 | ||
fd3f686c VZ |
113 | m_list->DetachNode(this); |
114 | } | |
c801d85f KB |
115 | } |
116 | ||
77c5eefb VZ |
117 | int wxNodeBase::IndexOf() const |
118 | { | |
223d09f6 | 119 | wxCHECK_MSG( m_list, wxNOT_FOUND, wxT("node doesn't belong to a list in IndexOf")); |
77c5eefb VZ |
120 | |
121 | // It would be more efficient to implement IndexOf() completely inside | |
122 | // wxListBase (only traverse the list once), but this is probably a more | |
123 | // reusable way of doing it. Can always be optimized at a later date (since | |
124 | // IndexOf() resides in wxListBase as well) if efficiency is a problem. | |
125 | int i; | |
126 | wxNodeBase *prev = m_previous; | |
127 | ||
128 | for( i = 0; prev; i++ ) | |
129 | { | |
130 | prev = prev->m_previous; | |
131 | } | |
132 | ||
133 | return i; | |
134 | } | |
135 | ||
fd3f686c VZ |
136 | // ----------------------------------------------------------------------------- |
137 | // wxListBase | |
138 | // ----------------------------------------------------------------------------- | |
139 | ||
62448488 | 140 | void wxListBase::Init(wxKeyType keyType) |
c801d85f | 141 | { |
fd3f686c VZ |
142 | m_nodeFirst = |
143 | m_nodeLast = (wxNodeBase *) NULL; | |
144 | m_count = 0; | |
f644b28c | 145 | m_destroy = false; |
fd3f686c VZ |
146 | m_keyType = keyType; |
147 | } | |
c801d85f | 148 | |
fd3f686c VZ |
149 | wxListBase::wxListBase(size_t count, void *elements[]) |
150 | { | |
151 | Init(); | |
c801d85f | 152 | |
fd3f686c VZ |
153 | for ( size_t n = 0; n < count; n++ ) |
154 | { | |
155 | Append(elements[n]); | |
156 | } | |
c801d85f KB |
157 | } |
158 | ||
fd3f686c | 159 | void wxListBase::DoCopy(const wxListBase& list) |
c801d85f | 160 | { |
fd3f686c | 161 | wxASSERT_MSG( !list.m_destroy, |
223d09f6 | 162 | wxT("copying list which owns it's elements is a bad idea") ); |
c801d85f | 163 | |
fd3f686c VZ |
164 | m_destroy = list.m_destroy; |
165 | m_keyType = list.m_keyType; | |
166 | m_nodeFirst = | |
167 | m_nodeLast = (wxNodeBase *) NULL; | |
c801d85f | 168 | |
3d0145a5 VZ |
169 | switch (m_keyType) |
170 | { | |
ecf9e593 VS |
171 | case wxKEY_INTEGER: |
172 | { | |
bcaa23de | 173 | long key; |
ecf9e593 VS |
174 | for ( wxNodeBase *node = list.GetFirst(); node; node = node->GetNext() ) |
175 | { | |
176 | key = node->GetKeyInteger(); | |
177 | Append(key, node->GetData()); | |
178 | } | |
179 | break; | |
180 | } | |
181 | ||
182 | case wxKEY_STRING: | |
183 | { | |
bcaa23de | 184 | const wxChar *key; |
ecf9e593 VS |
185 | for ( wxNodeBase *node = list.GetFirst(); node; node = node->GetNext() ) |
186 | { | |
187 | key = node->GetKeyString(); | |
188 | Append(key, node->GetData()); | |
189 | } | |
190 | break; | |
191 | } | |
192 | ||
193 | default: | |
194 | { | |
195 | for ( wxNodeBase *node = list.GetFirst(); node; node = node->GetNext() ) | |
196 | { | |
197 | Append(node->GetData()); | |
198 | } | |
199 | break; | |
200 | } | |
fd3f686c | 201 | } |
f6bcfd97 BP |
202 | |
203 | wxASSERT_MSG( m_count == list.m_count, _T("logic error in wxList::DoCopy") ); | |
c801d85f KB |
204 | } |
205 | ||
fd3f686c | 206 | wxListBase::~wxListBase() |
c801d85f | 207 | { |
fd3f686c VZ |
208 | wxNodeBase *each = m_nodeFirst; |
209 | while ( each != NULL ) | |
210 | { | |
211 | wxNodeBase *next = each->GetNext(); | |
212 | DoDeleteNode(each); | |
213 | each = next; | |
214 | } | |
c801d85f KB |
215 | } |
216 | ||
fd3f686c | 217 | wxNodeBase *wxListBase::AppendCommon(wxNodeBase *node) |
c801d85f | 218 | { |
fd3f686c VZ |
219 | if ( !m_nodeFirst ) |
220 | { | |
221 | m_nodeFirst = node; | |
222 | m_nodeLast = m_nodeFirst; | |
223 | } | |
224 | else | |
225 | { | |
226 | m_nodeLast->m_next = node; | |
227 | m_nodeLast = node; | |
228 | } | |
229 | ||
230 | m_count++; | |
231 | ||
232 | return node; | |
c801d85f KB |
233 | } |
234 | ||
fd3f686c | 235 | wxNodeBase *wxListBase::Append(void *object) |
c801d85f | 236 | { |
fd3f686c VZ |
237 | // all objects in a keyed list should have a key |
238 | wxCHECK_MSG( m_keyType == wxKEY_NONE, (wxNodeBase *)NULL, | |
223d09f6 | 239 | wxT("need a key for the object to append") ); |
c801d85f | 240 | |
7daab453 VZ |
241 | // we use wxDefaultListKey even though it is the default parameter value |
242 | // because gcc under Mac OS X seems to miscompile this call otherwise | |
243 | wxNodeBase *node = CreateNode(m_nodeLast, (wxNodeBase *)NULL, object, | |
244 | wxDefaultListKey); | |
fd3f686c VZ |
245 | |
246 | return AppendCommon(node); | |
c801d85f KB |
247 | } |
248 | ||
fd3f686c | 249 | wxNodeBase *wxListBase::Append(long key, void *object) |
c801d85f | 250 | { |
fd3f686c VZ |
251 | wxCHECK_MSG( (m_keyType == wxKEY_INTEGER) || |
252 | (m_keyType == wxKEY_NONE && m_count == 0), | |
253 | (wxNodeBase *)NULL, | |
223d09f6 | 254 | wxT("can't append object with numeric key to this list") ); |
fd3f686c VZ |
255 | |
256 | wxNodeBase *node = CreateNode(m_nodeLast, (wxNodeBase *)NULL, object, key); | |
257 | return AppendCommon(node); | |
c801d85f KB |
258 | } |
259 | ||
81727065 | 260 | wxNodeBase *wxListBase::Append (const wxString& key, void *object) |
c801d85f | 261 | { |
fd3f686c VZ |
262 | wxCHECK_MSG( (m_keyType == wxKEY_STRING) || |
263 | (m_keyType == wxKEY_NONE && m_count == 0), | |
264 | (wxNodeBase *)NULL, | |
223d09f6 | 265 | wxT("can't append object with string key to this list") ); |
fd3f686c VZ |
266 | |
267 | wxNodeBase *node = CreateNode(m_nodeLast, (wxNodeBase *)NULL, object, key); | |
268 | return AppendCommon(node); | |
269 | } | |
c801d85f | 270 | |
fd3f686c VZ |
271 | wxNodeBase *wxListBase::Insert(wxNodeBase *position, void *object) |
272 | { | |
273 | // all objects in a keyed list should have a key | |
274 | wxCHECK_MSG( m_keyType == wxKEY_NONE, (wxNodeBase *)NULL, | |
223d09f6 | 275 | wxT("need a key for the object to insert") ); |
c801d85f | 276 | |
ff528365 | 277 | wxCHECK_MSG( !position || position->m_list == this, (wxNodeBase *)NULL, |
223d09f6 | 278 | wxT("can't insert before a node from another list") ); |
ff528365 VZ |
279 | |
280 | // previous and next node for the node being inserted | |
281 | wxNodeBase *prev, *next; | |
fd3f686c | 282 | if ( position ) |
ff528365 | 283 | { |
fd3f686c | 284 | prev = position->GetPrevious(); |
ff528365 VZ |
285 | next = position; |
286 | } | |
287 | else | |
288 | { | |
289 | // inserting in the beginning of the list | |
290 | prev = (wxNodeBase *)NULL; | |
291 | next = m_nodeFirst; | |
292 | } | |
c801d85f | 293 | |
7daab453 VZ |
294 | // wxDefaultListKey: see comment in Append() above |
295 | wxNodeBase *node = CreateNode(prev, next, object, wxDefaultListKey); | |
fd3f686c | 296 | if ( !m_nodeFirst ) |
c801d85f | 297 | { |
fd3f686c | 298 | m_nodeLast = node; |
c801d85f | 299 | } |
c801d85f | 300 | |
fd3f686c | 301 | if ( prev == NULL ) |
c801d85f | 302 | { |
fd3f686c | 303 | m_nodeFirst = node; |
c801d85f | 304 | } |
c801d85f | 305 | |
fd3f686c VZ |
306 | m_count++; |
307 | ||
c801d85f KB |
308 | return node; |
309 | } | |
310 | ||
fd3f686c | 311 | wxNodeBase *wxListBase::Item(size_t n) const |
c801d85f | 312 | { |
fd3f686c | 313 | for ( wxNodeBase *current = GetFirst(); current; current = current->GetNext() ) |
c801d85f | 314 | { |
fd3f686c VZ |
315 | if ( n-- == 0 ) |
316 | { | |
317 | return current; | |
318 | } | |
c801d85f | 319 | } |
c801d85f | 320 | |
223d09f6 | 321 | wxFAIL_MSG( wxT("invalid index in wxListBase::Item") ); |
c801d85f | 322 | |
1b4092eb | 323 | return (wxNodeBase *)NULL; |
c801d85f KB |
324 | } |
325 | ||
fd3f686c | 326 | wxNodeBase *wxListBase::Find(const wxListKey& key) const |
c801d85f | 327 | { |
fd3f686c | 328 | wxASSERT_MSG( m_keyType == key.GetKeyType(), |
223d09f6 | 329 | wxT("this list is not keyed on the type of this key") ); |
c801d85f | 330 | |
fd3f686c VZ |
331 | for ( wxNodeBase *current = GetFirst(); current; current = current->GetNext() ) |
332 | { | |
333 | if ( key == current->m_key ) | |
334 | { | |
335 | return current; | |
336 | } | |
337 | } | |
c801d85f | 338 | |
fd3f686c VZ |
339 | // not found |
340 | return (wxNodeBase *)NULL; | |
c801d85f KB |
341 | } |
342 | ||
22d080f3 | 343 | wxNodeBase *wxListBase::Find(const void *object) const |
c801d85f | 344 | { |
fd3f686c | 345 | for ( wxNodeBase *current = GetFirst(); current; current = current->GetNext() ) |
c801d85f | 346 | { |
fd3f686c VZ |
347 | if ( current->GetData() == object ) |
348 | return current; | |
c801d85f | 349 | } |
fd3f686c VZ |
350 | |
351 | // not found | |
352 | return (wxNodeBase *)NULL; | |
c801d85f KB |
353 | } |
354 | ||
77c5eefb VZ |
355 | int wxListBase::IndexOf(void *object) const |
356 | { | |
357 | wxNodeBase *node = Find( object ); | |
358 | ||
3c67202d | 359 | return node ? node->IndexOf() : wxNOT_FOUND; |
77c5eefb VZ |
360 | } |
361 | ||
fd3f686c | 362 | void wxListBase::DoDeleteNode(wxNodeBase *node) |
c801d85f | 363 | { |
fd3f686c VZ |
364 | // free node's data |
365 | if ( m_keyType == wxKEY_STRING ) | |
c801d85f | 366 | { |
fd3f686c | 367 | free(node->m_key.string); |
c801d85f | 368 | } |
c801d85f | 369 | |
fd3f686c | 370 | if ( m_destroy ) |
c801d85f | 371 | { |
fd3f686c | 372 | node->DeleteData(); |
c801d85f | 373 | } |
c801d85f | 374 | |
09914df7 VZ |
375 | // so that the node knows that it's being deleted by the list |
376 | node->m_list = NULL; | |
fd3f686c | 377 | delete node; |
c801d85f KB |
378 | } |
379 | ||
fd3f686c | 380 | wxNodeBase *wxListBase::DetachNode(wxNodeBase *node) |
c801d85f | 381 | { |
223d09f6 | 382 | wxCHECK_MSG( node, NULL, wxT("detaching NULL wxNodeBase") ); |
fd3f686c | 383 | wxCHECK_MSG( node->m_list == this, NULL, |
223d09f6 | 384 | wxT("detaching node which is not from this list") ); |
c801d85f | 385 | |
fd3f686c VZ |
386 | // update the list |
387 | wxNodeBase **prevNext = node->GetPrevious() ? &node->GetPrevious()->m_next | |
388 | : &m_nodeFirst; | |
389 | wxNodeBase **nextPrev = node->GetNext() ? &node->GetNext()->m_previous | |
390 | : &m_nodeLast; | |
c801d85f | 391 | |
fd3f686c VZ |
392 | *prevNext = node->GetNext(); |
393 | *nextPrev = node->GetPrevious(); | |
c801d85f | 394 | |
fd3f686c | 395 | m_count--; |
c801d85f | 396 | |
fd3f686c VZ |
397 | // mark the node as not belonging to this list any more |
398 | node->m_list = NULL; | |
c801d85f | 399 | |
fd3f686c | 400 | return node; |
c801d85f KB |
401 | } |
402 | ||
fd3f686c | 403 | bool wxListBase::DeleteNode(wxNodeBase *node) |
c801d85f | 404 | { |
fd3f686c | 405 | if ( !DetachNode(node) ) |
f644b28c | 406 | return false; |
fd3f686c VZ |
407 | |
408 | DoDeleteNode(node); | |
409 | ||
f644b28c | 410 | return true; |
c801d85f KB |
411 | } |
412 | ||
fd3f686c | 413 | bool wxListBase::DeleteObject(void *object) |
c801d85f | 414 | { |
fd3f686c VZ |
415 | for ( wxNodeBase *current = GetFirst(); current; current = current->GetNext() ) |
416 | { | |
417 | if ( current->GetData() == object ) | |
418 | { | |
419 | DeleteNode(current); | |
f644b28c | 420 | return true; |
fd3f686c VZ |
421 | } |
422 | } | |
423 | ||
424 | // not found | |
f644b28c | 425 | return false; |
c801d85f KB |
426 | } |
427 | ||
fd3f686c | 428 | void wxListBase::Clear() |
c801d85f | 429 | { |
fd3f686c VZ |
430 | wxNodeBase *current = m_nodeFirst; |
431 | while ( current ) | |
c801d85f | 432 | { |
fd3f686c VZ |
433 | wxNodeBase *next = current->GetNext(); |
434 | DoDeleteNode(current); | |
435 | current = next; | |
c801d85f | 436 | } |
fd3f686c VZ |
437 | |
438 | m_nodeFirst = | |
439 | m_nodeLast = (wxNodeBase *)NULL; | |
440 | ||
441 | m_count = 0; | |
c801d85f KB |
442 | } |
443 | ||
fd3f686c | 444 | void wxListBase::ForEach(wxListIterateFunction F) |
c801d85f | 445 | { |
fd3f686c VZ |
446 | for ( wxNodeBase *current = GetFirst(); current; current = current->GetNext() ) |
447 | { | |
448 | (*F)(current->GetData()); | |
c801d85f KB |
449 | } |
450 | } | |
fd3f686c VZ |
451 | |
452 | void *wxListBase::FirstThat(wxListIterateFunction F) | |
c801d85f | 453 | { |
fd3f686c VZ |
454 | for ( wxNodeBase *current = GetFirst(); current; current = current->GetNext() ) |
455 | { | |
456 | if ( (*F)(current->GetData()) ) | |
457 | return current->GetData(); | |
c801d85f | 458 | } |
fd3f686c VZ |
459 | |
460 | return (wxNodeBase *)NULL; | |
c801d85f | 461 | } |
fd3f686c VZ |
462 | |
463 | void *wxListBase::LastThat(wxListIterateFunction F) | |
c801d85f | 464 | { |
fd3f686c VZ |
465 | for ( wxNodeBase *current = GetLast(); current; current = current->GetPrevious() ) |
466 | { | |
467 | if ( (*F)(current->GetData()) ) | |
468 | return current->GetData(); | |
c801d85f | 469 | } |
fd3f686c VZ |
470 | |
471 | return (wxNodeBase *)NULL; | |
c801d85f KB |
472 | } |
473 | ||
474 | // (stefan.hammes@urz.uni-heidelberg.de) | |
475 | // | |
476 | // function for sorting lists. the concept is borrowed from 'qsort'. | |
477 | // by giving a sort function, arbitrary lists can be sorted. | |
478 | // method: | |
479 | // - put wxObject pointers into an array | |
480 | // - sort the array with qsort | |
481 | // - put back the sorted wxObject pointers into the list | |
482 | // | |
483 | // CAVE: the sort function receives pointers to wxObject pointers (wxObject **), | |
484 | // so dereference right! | |
485 | // EXAMPLE: | |
486 | // int listcompare(const void *arg1, const void *arg2) | |
487 | // { | |
488 | // return(compare(**(wxString **)arg1, | |
489 | // **(wxString **)arg2)); | |
490 | // } | |
491 | // | |
492 | // void main() | |
fd3f686c VZ |
493 | // { |
494 | // wxListBase list; | |
c801d85f KB |
495 | // |
496 | // list.Append(new wxString("DEF")); | |
497 | // list.Append(new wxString("GHI")); | |
498 | // list.Append(new wxString("ABC")); | |
499 | // list.Sort(listcompare); | |
500 | // } | |
501 | ||
fd3f686c | 502 | void wxListBase::Sort(const wxSortCompareFunction compfunc) |
c801d85f | 503 | { |
fd3f686c VZ |
504 | // allocate an array for the wxObject pointers of the list |
505 | const size_t num = GetCount(); | |
506 | void **objArray = new void *[num]; | |
507 | void **objPtr = objArray; | |
508 | ||
509 | // go through the list and put the pointers into the array | |
510 | wxNodeBase *node; | |
b1d4dd7a | 511 | for ( node = GetFirst(); node; node = node->GetNext() ) |
fd3f686c | 512 | { |
b1d4dd7a | 513 | *objPtr++ = node->GetData(); |
fd3f686c VZ |
514 | } |
515 | ||
516 | // sort the array | |
1c193821 JS |
517 | qsort((void *)objArray,num,sizeof(wxObject *), |
518 | #ifdef __WXWINCE__ | |
519 | (int (__cdecl *)(const void *,const void *)) | |
520 | #endif | |
521 | compfunc); | |
fd3f686c VZ |
522 | |
523 | // put the sorted pointers back into the list | |
524 | objPtr = objArray; | |
b1d4dd7a | 525 | for ( node = GetFirst(); node; node = node->GetNext() ) |
fd3f686c VZ |
526 | { |
527 | node->SetData(*objPtr++); | |
528 | } | |
529 | ||
530 | // free the array | |
531 | delete[] objArray; | |
c801d85f KB |
532 | } |
533 | ||
df5168c4 MB |
534 | void wxListBase::Reverse() |
535 | { | |
536 | wxNodeBase* node = m_nodeFirst; | |
537 | wxNodeBase* tmp; | |
538 | ||
539 | while (node) | |
540 | { | |
541 | // swap prev and next pointers | |
542 | tmp = node->m_next; | |
543 | node->m_next = node->m_previous; | |
544 | node->m_previous = tmp; | |
545 | ||
546 | // this is the node that was next before swapping | |
547 | node = tmp; | |
548 | } | |
549 | ||
550 | // swap first and last node | |
551 | tmp = m_nodeFirst; m_nodeFirst = m_nodeLast; m_nodeLast = tmp; | |
552 | } | |
553 | ||
554 | void wxListBase::DeleteNodes(wxNodeBase* first, wxNodeBase* last) | |
555 | { | |
556 | wxNodeBase* node = first; | |
557 | ||
558 | while (node != last) | |
559 | { | |
560 | wxNodeBase* next = node->GetNext(); | |
561 | DeleteNode(node); | |
562 | node = next; | |
563 | } | |
564 | } | |
565 | ||
f98bd52a VZ |
566 | // ============================================================================ |
567 | // compatibility section from now on | |
568 | // ============================================================================ | |
569 | ||
570 | #ifdef wxLIST_COMPATIBILITY | |
571 | ||
fd3f686c VZ |
572 | // ----------------------------------------------------------------------------- |
573 | // wxList (a.k.a. wxObjectList) | |
574 | // ----------------------------------------------------------------------------- | |
c801d85f | 575 | |
f98bd52a VZ |
576 | IMPLEMENT_DYNAMIC_CLASS(wxList, wxObject) |
577 | ||
b1d4dd7a RL |
578 | wxList::wxList( int key_type ) |
579 | : wxObjectList( (wxKeyType)key_type ) | |
580 | { | |
581 | } | |
582 | ||
fd3f686c | 583 | void wxObjectListNode::DeleteData() |
c801d85f | 584 | { |
fd3f686c | 585 | delete (wxObject *)GetData(); |
c801d85f KB |
586 | } |
587 | ||
f526f752 | 588 | // ---------------------------------------------------------------------------- |
fd3f686c | 589 | // wxStringList |
f526f752 MB |
590 | // ---------------------------------------------------------------------------- |
591 | ||
f526f752 MB |
592 | static inline wxChar* MYcopystring(const wxChar* s) |
593 | { | |
594 | wxChar* copy = new wxChar[wxStrlen(s) + 1]; | |
595 | return wxStrcpy(copy, s); | |
596 | } | |
fd3f686c | 597 | |
f98bd52a VZ |
598 | IMPLEMENT_DYNAMIC_CLASS(wxStringList, wxObject) |
599 | ||
fd3f686c VZ |
600 | // instead of WX_DEFINE_LIST(wxStringListBase) we define this function |
601 | // ourselves | |
602 | void wxStringListNode::DeleteData() | |
341287bf | 603 | { |
fd3f686c | 604 | delete [] (char *)GetData(); |
341287bf JS |
605 | } |
606 | ||
50920146 | 607 | bool wxStringList::Delete(const wxChar *s) |
f0824a5a VZ |
608 | { |
609 | wxStringListNode *current; | |
610 | ||
611 | for ( current = GetFirst(); current; current = current->GetNext() ) | |
612 | { | |
50920146 | 613 | if ( wxStrcmp(current->GetData(), s) == 0 ) |
f0824a5a VZ |
614 | { |
615 | DeleteNode(current); | |
f644b28c | 616 | return true; |
f0824a5a VZ |
617 | } |
618 | } | |
619 | ||
620 | // not found | |
f644b28c | 621 | return false; |
f0824a5a VZ |
622 | } |
623 | ||
db9504c5 VZ |
624 | void wxStringList::DoCopy(const wxStringList& other) |
625 | { | |
626 | wxASSERT( GetCount() == 0 ); // this list must be empty before copying! | |
627 | ||
628 | size_t count = other.GetCount(); | |
629 | for ( size_t n = 0; n < count; n++ ) | |
630 | { | |
77c5eefb | 631 | Add(other.Item(n)->GetData()); |
db9504c5 VZ |
632 | } |
633 | } | |
634 | ||
b1d4dd7a RL |
635 | wxStringList::wxStringList() |
636 | { | |
f644b28c | 637 | DeleteContents(true); |
b1d4dd7a RL |
638 | } |
639 | ||
c801d85f KB |
640 | // Variable argument list, terminated by a zero |
641 | // Makes new storage for the strings | |
50920146 | 642 | wxStringList::wxStringList (const wxChar *first, ...) |
c801d85f | 643 | { |
f644b28c | 644 | DeleteContents(true); |
fd3f686c | 645 | if ( !first ) |
c801d85f KB |
646 | return; |
647 | ||
648 | va_list ap; | |
fd3f686c | 649 | va_start(ap, first); |
c801d85f | 650 | |
50920146 | 651 | const wxChar *s = first; |
c801d85f | 652 | for (;;) |
fd3f686c VZ |
653 | { |
654 | Add(s); | |
655 | ||
2cfcf22d VZ |
656 | // icc gives this warning in its own va_arg() macro, argh |
657 | #ifdef __INTELC__ | |
658 | #pragma warning(push) | |
659 | #pragma warning(disable: 1684) | |
660 | #endif | |
661 | ||
50920146 | 662 | s = va_arg(ap, const wxChar *); |
2cfcf22d VZ |
663 | |
664 | #ifdef __INTELC__ | |
665 | #pragma warning(pop) | |
c801d85f | 666 | #endif |
2cfcf22d VZ |
667 | |
668 | if ( !s ) | |
fd3f686c VZ |
669 | break; |
670 | } | |
c801d85f | 671 | |
fd3f686c | 672 | va_end(ap); |
341287bf JS |
673 | } |
674 | ||
f644b28c | 675 | // Only makes new strings if arg is true |
50920146 | 676 | wxChar **wxStringList::ListToArray(bool new_copies) const |
341287bf | 677 | { |
50920146 | 678 | wxChar **string_array = new wxChar *[GetCount()]; |
fd3f686c VZ |
679 | wxStringListNode *node = GetFirst(); |
680 | for (size_t i = 0; i < GetCount(); i++) | |
c801d85f | 681 | { |
50920146 | 682 | wxChar *s = node->GetData(); |
fd3f686c | 683 | if ( new_copies ) |
f526f752 | 684 | string_array[i] = MYcopystring(s); |
fd3f686c VZ |
685 | else |
686 | string_array[i] = s; | |
687 | node = node->GetNext(); | |
c801d85f | 688 | } |
c801d85f | 689 | |
fd3f686c | 690 | return string_array; |
c801d85f KB |
691 | } |
692 | ||
fd3f686c | 693 | // Checks whether s is a member of the list |
50920146 | 694 | bool wxStringList::Member(const wxChar *s) const |
c801d85f | 695 | { |
fd3f686c | 696 | for ( wxStringListNode *node = GetFirst(); node; node = node->GetNext() ) |
c801d85f | 697 | { |
50920146 OK |
698 | const wxChar *s1 = node->GetData(); |
699 | if (s == s1 || wxStrcmp (s, s1) == 0) | |
f644b28c | 700 | return true; |
c801d85f | 701 | } |
fd3f686c | 702 | |
f644b28c | 703 | return false; |
c801d85f KB |
704 | } |
705 | ||
1c193821 JS |
706 | #ifdef __WXWINCE__ |
707 | extern "C" int __cdecl | |
708 | #else | |
90350682 | 709 | extern "C" int LINKAGEMODE |
1c193821 JS |
710 | #endif |
711 | ||
fd3f686c | 712 | wx_comparestrings(const void *arg1, const void *arg2) |
c801d85f | 713 | { |
50920146 OK |
714 | wxChar **s1 = (wxChar **) arg1; |
715 | wxChar **s2 = (wxChar **) arg2; | |
c801d85f | 716 | |
50920146 | 717 | return wxStrcmp (*s1, *s2); |
c801d85f KB |
718 | } |
719 | ||
720 | // Sort a list of strings - deallocates old nodes, allocates new | |
fd3f686c | 721 | void wxStringList::Sort() |
c801d85f | 722 | { |
fd3f686c | 723 | size_t N = GetCount(); |
50920146 | 724 | wxChar **array = new wxChar *[N]; |
2a040d3f | 725 | wxStringListNode *node; |
c801d85f | 726 | |
fd3f686c | 727 | size_t i = 0; |
2a040d3f | 728 | for ( node = GetFirst(); node; node = node->GetNext() ) |
c801d85f | 729 | { |
fd3f686c | 730 | array[i++] = node->GetData(); |
c801d85f | 731 | } |
341287bf | 732 | |
50920146 | 733 | qsort (array, N, sizeof (wxChar *), wx_comparestrings); |
341287bf | 734 | |
9257d0b7 VZ |
735 | i = 0; |
736 | for ( node = GetFirst(); node; node = node->GetNext() ) | |
737 | node->SetData( array[i++] ); | |
341287bf | 738 | |
9257d0b7 | 739 | delete [] array; |
341287bf | 740 | } |
f98bd52a | 741 | |
f526f752 MB |
742 | wxNode *wxStringList::Add(const wxChar *s) |
743 | { | |
d4d8988c VZ |
744 | return (wxNode *)(wxStringListBase::Node *) |
745 | wxStringListBase::Append(MYcopystring(s)); | |
f526f752 | 746 | } |
f644b28c | 747 | |
f526f752 MB |
748 | wxNode *wxStringList::Prepend(const wxChar *s) |
749 | { | |
d4d8988c VZ |
750 | return (wxNode *)(wxStringListBase::Node *) |
751 | wxStringListBase::Insert(MYcopystring(s)); | |
f526f752 MB |
752 | } |
753 | ||
f98bd52a VZ |
754 | #endif // wxLIST_COMPATIBILITY |
755 | ||
ed1288c1 VZ |
756 | #else // wxUSE_STL = 1 |
757 | ||
7ec69821 | 758 | #include "wx/listimpl.cpp" |
412e0d47 | 759 | WX_DEFINE_LIST(wxObjectList) |
3543c79e MB |
760 | |
761 | // with wxUSE_STL wxStringList contains wxString objects, not pointers | |
30a29593 | 762 | void _WX_LIST_HELPER_wxStringListBase::DeleteFunction( wxString WXUNUSED(X) ) |
3543c79e MB |
763 | { |
764 | } | |
ed1288c1 | 765 | |
1d300f25 VZ |
766 | wxStringListBase::BaseListType wxStringListBase::EmptyList; |
767 | ||
df5168c4 | 768 | #endif // !wxUSE_STL |