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