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