]> git.saurik.com Git - wxWidgets.git/blame - src/common/list.cpp
Upported wxListBox key handling and SetFirstItem()
[wxWidgets.git] / src / common / list.cpp
CommitLineData
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$
8// Copyright: (c) Julian Smart and Markus Holzem
fd3f686c
VZ
9// Licence: wxWindows license
10////////////////////////////////////////////////////////////////////////////////
c801d85f 11
fd3f686c
VZ
12// =============================================================================
13// declarations
14// =============================================================================
15
16// -----------------------------------------------------------------------------
17// headers
18// -----------------------------------------------------------------------------
c801d85f
KB
19#ifdef __GNUG__
20#pragma implementation "list.h"
21#endif
22
23// For compilers that support precompilation, includes "wx.h".
24#include "wx/wxprec.h"
25
26#ifdef __BORLANDC__
fd3f686c 27 #pragma hdrstop
c801d85f
KB
28#endif
29
fd3f686c
VZ
30#include <stdarg.h>
31#include <stdlib.h>
32#include <string.h>
33
c801d85f 34#ifndef WX_PRECOMP
fd3f686c
VZ
35 #include "wx/defs.h"
36 #include "wx/list.h"
37 #include "wx/utils.h" // for copystring() (beurk...)
c801d85f
KB
38#endif
39
fd3f686c
VZ
40// =============================================================================
41// implementation
42// =============================================================================
c801d85f 43
ff528365
VZ
44// -----------------------------------------------------------------------------
45// wxListKey
46// -----------------------------------------------------------------------------
47
8a2c6ef8
JS
48wxListKey wxDefaultListKey;
49
ff528365
VZ
50bool 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
71wxNodeBase::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
105wxNodeBase::~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
121int 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 144void 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
153wxListBase::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 163void 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
ecf9e593 173 switch (m_keyType) {
bcaa23de 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 210wxListBase::~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 221wxNodeBase *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 239wxNodeBase *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
fd3f686c
VZ
245 wxNodeBase *node = CreateNode(m_nodeLast, (wxNodeBase *)NULL, object);
246
247 return AppendCommon(node);
c801d85f
KB
248}
249
fd3f686c 250wxNodeBase *wxListBase::Append(long key, void *object)
c801d85f 251{
fd3f686c
VZ
252 wxCHECK_MSG( (m_keyType == wxKEY_INTEGER) ||
253 (m_keyType == wxKEY_NONE && m_count == 0),
254 (wxNodeBase *)NULL,
223d09f6 255 wxT("can't append object with numeric key to this list") );
fd3f686c
VZ
256
257 wxNodeBase *node = CreateNode(m_nodeLast, (wxNodeBase *)NULL, object, key);
258 return AppendCommon(node);
c801d85f
KB
259}
260
50920146 261wxNodeBase *wxListBase::Append (const wxChar *key, void *object)
c801d85f 262{
fd3f686c
VZ
263 wxCHECK_MSG( (m_keyType == wxKEY_STRING) ||
264 (m_keyType == wxKEY_NONE && m_count == 0),
265 (wxNodeBase *)NULL,
223d09f6 266 wxT("can't append object with string key to this list") );
fd3f686c
VZ
267
268 wxNodeBase *node = CreateNode(m_nodeLast, (wxNodeBase *)NULL, object, key);
269 return AppendCommon(node);
270}
c801d85f 271
fd3f686c
VZ
272wxNodeBase *wxListBase::Insert(wxNodeBase *position, void *object)
273{
274 // all objects in a keyed list should have a key
275 wxCHECK_MSG( m_keyType == wxKEY_NONE, (wxNodeBase *)NULL,
223d09f6 276 wxT("need a key for the object to insert") );
c801d85f 277
ff528365 278 wxCHECK_MSG( !position || position->m_list == this, (wxNodeBase *)NULL,
223d09f6 279 wxT("can't insert before a node from another list") );
ff528365
VZ
280
281 // previous and next node for the node being inserted
282 wxNodeBase *prev, *next;
fd3f686c 283 if ( position )
ff528365 284 {
fd3f686c 285 prev = position->GetPrevious();
ff528365
VZ
286 next = position;
287 }
288 else
289 {
290 // inserting in the beginning of the list
291 prev = (wxNodeBase *)NULL;
292 next = m_nodeFirst;
293 }
c801d85f 294
ff528365 295 wxNodeBase *node = CreateNode(prev, next, object);
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 311wxNodeBase *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 326wxNodeBase *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
fd3f686c 343wxNodeBase *wxListBase::Find(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
355int 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 362void 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 380wxNodeBase *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 403bool wxListBase::DeleteNode(wxNodeBase *node)
c801d85f 404{
fd3f686c
VZ
405 if ( !DetachNode(node) )
406 return FALSE;
407
408 DoDeleteNode(node);
409
410 return TRUE;
c801d85f
KB
411}
412
fd3f686c 413bool 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);
420 return TRUE;
421 }
422 }
423
424 // not found
425 return FALSE;
c801d85f
KB
426}
427
fd3f686c 428void 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 444void 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
452void *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
463void *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 502void 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;
511 for ( node = GetFirst(); node; node = node->Next() )
512 {
513 *objPtr++ = node->Data();
514 }
515
516 // sort the array
517 qsort((void *)objArray,num,sizeof(wxObject *),compfunc);
518
519 // put the sorted pointers back into the list
520 objPtr = objArray;
521 for ( node = GetFirst(); node; node = node->Next() )
522 {
523 node->SetData(*objPtr++);
524 }
525
526 // free the array
527 delete[] objArray;
c801d85f
KB
528}
529
fd3f686c
VZ
530// -----------------------------------------------------------------------------
531// wxList (a.k.a. wxObjectList)
532// -----------------------------------------------------------------------------
c801d85f 533
fd3f686c 534void wxObjectListNode::DeleteData()
c801d85f 535{
fd3f686c 536 delete (wxObject *)GetData();
c801d85f
KB
537}
538
fd3f686c
VZ
539// -----------------------------------------------------------------------------
540// wxStringList
541// -----------------------------------------------------------------------------
542
543// instead of WX_DEFINE_LIST(wxStringListBase) we define this function
544// ourselves
545void wxStringListNode::DeleteData()
341287bf 546{
fd3f686c 547 delete [] (char *)GetData();
341287bf
JS
548}
549
50920146 550bool wxStringList::Delete(const wxChar *s)
f0824a5a
VZ
551{
552 wxStringListNode *current;
553
554 for ( current = GetFirst(); current; current = current->GetNext() )
555 {
50920146 556 if ( wxStrcmp(current->GetData(), s) == 0 )
f0824a5a
VZ
557 {
558 DeleteNode(current);
559 return TRUE;
560 }
561 }
562
563 // not found
564 return FALSE;
565}
566
db9504c5
VZ
567void wxStringList::DoCopy(const wxStringList& other)
568{
569 wxASSERT( GetCount() == 0 ); // this list must be empty before copying!
570
571 size_t count = other.GetCount();
572 for ( size_t n = 0; n < count; n++ )
573 {
77c5eefb 574 Add(other.Item(n)->GetData());
db9504c5
VZ
575 }
576}
577
c801d85f
KB
578// Variable argument list, terminated by a zero
579// Makes new storage for the strings
50920146 580wxStringList::wxStringList (const wxChar *first, ...)
c801d85f 581{
0d3a69ec 582 DeleteContents(TRUE);
fd3f686c 583 if ( !first )
c801d85f
KB
584 return;
585
586 va_list ap;
fd3f686c 587 va_start(ap, first);
c801d85f 588
50920146 589 const wxChar *s = first;
c801d85f 590 for (;;)
fd3f686c
VZ
591 {
592 Add(s);
593
50920146 594 s = va_arg(ap, const wxChar *);
fd3f686c 595 // if (s == NULL)
2049ba38 596#ifdef __WXMSW__
48c12cb1 597 if ((int)(long) s == 0)
c801d85f
KB
598#else
599 if ((long) s == 0)
600#endif
fd3f686c
VZ
601 break;
602 }
c801d85f 603
fd3f686c 604 va_end(ap);
341287bf
JS
605}
606
fd3f686c 607// Only makes new strings if arg is TRUE
50920146 608wxChar **wxStringList::ListToArray(bool new_copies) const
341287bf 609{
50920146 610 wxChar **string_array = new wxChar *[GetCount()];
fd3f686c
VZ
611 wxStringListNode *node = GetFirst();
612 for (size_t i = 0; i < GetCount(); i++)
c801d85f 613 {
50920146 614 wxChar *s = node->GetData();
fd3f686c
VZ
615 if ( new_copies )
616 string_array[i] = copystring(s);
617 else
618 string_array[i] = s;
619 node = node->GetNext();
c801d85f 620 }
c801d85f 621
fd3f686c 622 return string_array;
c801d85f
KB
623}
624
fd3f686c 625// Checks whether s is a member of the list
50920146 626bool wxStringList::Member(const wxChar *s) const
c801d85f 627{
fd3f686c 628 for ( wxStringListNode *node = GetFirst(); node; node = node->GetNext() )
c801d85f 629 {
50920146
OK
630 const wxChar *s1 = node->GetData();
631 if (s == s1 || wxStrcmp (s, s1) == 0)
fd3f686c 632 return TRUE;
c801d85f 633 }
fd3f686c
VZ
634
635 return FALSE;
c801d85f
KB
636}
637
913df6f2 638static int LINKAGEMODE
fd3f686c 639wx_comparestrings(const void *arg1, const void *arg2)
c801d85f 640{
50920146
OK
641 wxChar **s1 = (wxChar **) arg1;
642 wxChar **s2 = (wxChar **) arg2;
c801d85f 643
50920146 644 return wxStrcmp (*s1, *s2);
c801d85f
KB
645}
646
647// Sort a list of strings - deallocates old nodes, allocates new
fd3f686c 648void wxStringList::Sort()
c801d85f 649{
fd3f686c 650 size_t N = GetCount();
50920146 651 wxChar **array = new wxChar *[N];
2a040d3f 652 wxStringListNode *node;
c801d85f 653
fd3f686c 654 size_t i = 0;
2a040d3f 655 for ( node = GetFirst(); node; node = node->GetNext() )
c801d85f 656 {
fd3f686c 657 array[i++] = node->GetData();
c801d85f 658 }
341287bf 659
50920146 660 qsort (array, N, sizeof (wxChar *), wx_comparestrings);
341287bf 661
9257d0b7
VZ
662 i = 0;
663 for ( node = GetFirst(); node; node = node->GetNext() )
664 node->SetData( array[i++] );
341287bf 665
9257d0b7 666 delete [] array;
341287bf 667}