]> git.saurik.com Git - wxWidgets.git/blame - src/common/list.cpp
long -> wxCoord
[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_count = list.m_count;
169 m_destroy = list.m_destroy;
170 m_keyType = list.m_keyType;
171 m_nodeFirst =
172 m_nodeLast = (wxNodeBase *) NULL;
c801d85f 173
ecf9e593
VS
174 switch (m_keyType) {
175
176 case wxKEY_INTEGER:
177 {
178 long key;
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 {
189 const wxChar *key;
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 }
c801d85f
KB
207}
208
fd3f686c 209wxListBase::~wxListBase()
c801d85f 210{
fd3f686c
VZ
211 wxNodeBase *each = m_nodeFirst;
212 while ( each != NULL )
213 {
214 wxNodeBase *next = each->GetNext();
215 DoDeleteNode(each);
216 each = next;
217 }
c801d85f
KB
218}
219
fd3f686c 220wxNodeBase *wxListBase::AppendCommon(wxNodeBase *node)
c801d85f 221{
fd3f686c
VZ
222 if ( !m_nodeFirst )
223 {
224 m_nodeFirst = node;
225 m_nodeLast = m_nodeFirst;
226 }
227 else
228 {
229 m_nodeLast->m_next = node;
230 m_nodeLast = node;
231 }
232
233 m_count++;
234
235 return node;
c801d85f
KB
236}
237
fd3f686c 238wxNodeBase *wxListBase::Append(void *object)
c801d85f 239{
fd3f686c
VZ
240 // all objects in a keyed list should have a key
241 wxCHECK_MSG( m_keyType == wxKEY_NONE, (wxNodeBase *)NULL,
223d09f6 242 wxT("need a key for the object to append") );
c801d85f 243
fd3f686c
VZ
244 wxNodeBase *node = CreateNode(m_nodeLast, (wxNodeBase *)NULL, object);
245
246 return AppendCommon(node);
c801d85f
KB
247}
248
fd3f686c 249wxNodeBase *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
50920146 260wxNodeBase *wxListBase::Append (const wxChar *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
271wxNodeBase *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
ff528365 294 wxNodeBase *node = CreateNode(prev, next, object);
fd3f686c 295 if ( !m_nodeFirst )
c801d85f 296 {
fd3f686c 297 m_nodeLast = node;
c801d85f 298 }
c801d85f 299
fd3f686c 300 if ( prev == NULL )
c801d85f 301 {
fd3f686c 302 m_nodeFirst = node;
c801d85f 303 }
c801d85f 304
fd3f686c
VZ
305 m_count++;
306
c801d85f
KB
307 return node;
308}
309
fd3f686c 310wxNodeBase *wxListBase::Item(size_t n) const
c801d85f 311{
fd3f686c 312 for ( wxNodeBase *current = GetFirst(); current; current = current->GetNext() )
c801d85f 313 {
fd3f686c
VZ
314 if ( n-- == 0 )
315 {
316 return current;
317 }
c801d85f 318 }
c801d85f 319
223d09f6 320 wxFAIL_MSG( wxT("invalid index in wxListBase::Item") );
c801d85f 321
1b4092eb 322 return (wxNodeBase *)NULL;
c801d85f
KB
323}
324
fd3f686c 325wxNodeBase *wxListBase::Find(const wxListKey& key) const
c801d85f 326{
fd3f686c 327 wxASSERT_MSG( m_keyType == key.GetKeyType(),
223d09f6 328 wxT("this list is not keyed on the type of this key") );
c801d85f 329
fd3f686c
VZ
330 for ( wxNodeBase *current = GetFirst(); current; current = current->GetNext() )
331 {
332 if ( key == current->m_key )
333 {
334 return current;
335 }
336 }
c801d85f 337
fd3f686c
VZ
338 // not found
339 return (wxNodeBase *)NULL;
c801d85f
KB
340}
341
fd3f686c 342wxNodeBase *wxListBase::Find(void *object) const
c801d85f 343{
fd3f686c 344 for ( wxNodeBase *current = GetFirst(); current; current = current->GetNext() )
c801d85f 345 {
fd3f686c
VZ
346 if ( current->GetData() == object )
347 return current;
c801d85f 348 }
fd3f686c
VZ
349
350 // not found
351 return (wxNodeBase *)NULL;
c801d85f
KB
352}
353
77c5eefb
VZ
354int wxListBase::IndexOf(void *object) const
355{
356 wxNodeBase *node = Find( object );
357
3c67202d 358 return node ? node->IndexOf() : wxNOT_FOUND;
77c5eefb
VZ
359}
360
fd3f686c 361void wxListBase::DoDeleteNode(wxNodeBase *node)
c801d85f 362{
fd3f686c
VZ
363 // free node's data
364 if ( m_keyType == wxKEY_STRING )
c801d85f 365 {
fd3f686c 366 free(node->m_key.string);
c801d85f 367 }
c801d85f 368
fd3f686c 369 if ( m_destroy )
c801d85f 370 {
fd3f686c 371 node->DeleteData();
c801d85f 372 }
c801d85f 373
09914df7
VZ
374 // so that the node knows that it's being deleted by the list
375 node->m_list = NULL;
fd3f686c 376 delete node;
c801d85f
KB
377}
378
fd3f686c 379wxNodeBase *wxListBase::DetachNode(wxNodeBase *node)
c801d85f 380{
223d09f6 381 wxCHECK_MSG( node, NULL, wxT("detaching NULL wxNodeBase") );
fd3f686c 382 wxCHECK_MSG( node->m_list == this, NULL,
223d09f6 383 wxT("detaching node which is not from this list") );
c801d85f 384
fd3f686c
VZ
385 // update the list
386 wxNodeBase **prevNext = node->GetPrevious() ? &node->GetPrevious()->m_next
387 : &m_nodeFirst;
388 wxNodeBase **nextPrev = node->GetNext() ? &node->GetNext()->m_previous
389 : &m_nodeLast;
c801d85f 390
fd3f686c
VZ
391 *prevNext = node->GetNext();
392 *nextPrev = node->GetPrevious();
c801d85f 393
fd3f686c 394 m_count--;
c801d85f 395
fd3f686c
VZ
396 // mark the node as not belonging to this list any more
397 node->m_list = NULL;
c801d85f 398
fd3f686c 399 return node;
c801d85f
KB
400}
401
fd3f686c 402bool wxListBase::DeleteNode(wxNodeBase *node)
c801d85f 403{
fd3f686c
VZ
404 if ( !DetachNode(node) )
405 return FALSE;
406
407 DoDeleteNode(node);
408
409 return TRUE;
c801d85f
KB
410}
411
fd3f686c 412bool wxListBase::DeleteObject(void *object)
c801d85f 413{
fd3f686c
VZ
414 for ( wxNodeBase *current = GetFirst(); current; current = current->GetNext() )
415 {
416 if ( current->GetData() == object )
417 {
418 DeleteNode(current);
419 return TRUE;
420 }
421 }
422
423 // not found
424 return FALSE;
c801d85f
KB
425}
426
fd3f686c 427void wxListBase::Clear()
c801d85f 428{
fd3f686c
VZ
429 wxNodeBase *current = m_nodeFirst;
430 while ( current )
c801d85f 431 {
fd3f686c
VZ
432 wxNodeBase *next = current->GetNext();
433 DoDeleteNode(current);
434 current = next;
c801d85f 435 }
fd3f686c
VZ
436
437 m_nodeFirst =
438 m_nodeLast = (wxNodeBase *)NULL;
439
440 m_count = 0;
c801d85f
KB
441}
442
fd3f686c 443void wxListBase::ForEach(wxListIterateFunction F)
c801d85f 444{
fd3f686c
VZ
445 for ( wxNodeBase *current = GetFirst(); current; current = current->GetNext() )
446 {
447 (*F)(current->GetData());
c801d85f
KB
448 }
449}
fd3f686c
VZ
450
451void *wxListBase::FirstThat(wxListIterateFunction F)
c801d85f 452{
fd3f686c
VZ
453 for ( wxNodeBase *current = GetFirst(); current; current = current->GetNext() )
454 {
455 if ( (*F)(current->GetData()) )
456 return current->GetData();
c801d85f 457 }
fd3f686c
VZ
458
459 return (wxNodeBase *)NULL;
c801d85f 460}
fd3f686c
VZ
461
462void *wxListBase::LastThat(wxListIterateFunction F)
c801d85f 463{
fd3f686c
VZ
464 for ( wxNodeBase *current = GetLast(); current; current = current->GetPrevious() )
465 {
466 if ( (*F)(current->GetData()) )
467 return current->GetData();
c801d85f 468 }
fd3f686c
VZ
469
470 return (wxNodeBase *)NULL;
c801d85f
KB
471}
472
473// (stefan.hammes@urz.uni-heidelberg.de)
474//
475// function for sorting lists. the concept is borrowed from 'qsort'.
476// by giving a sort function, arbitrary lists can be sorted.
477// method:
478// - put wxObject pointers into an array
479// - sort the array with qsort
480// - put back the sorted wxObject pointers into the list
481//
482// CAVE: the sort function receives pointers to wxObject pointers (wxObject **),
483// so dereference right!
484// EXAMPLE:
485// int listcompare(const void *arg1, const void *arg2)
486// {
487// return(compare(**(wxString **)arg1,
488// **(wxString **)arg2));
489// }
490//
491// void main()
fd3f686c
VZ
492// {
493// wxListBase list;
c801d85f
KB
494//
495// list.Append(new wxString("DEF"));
496// list.Append(new wxString("GHI"));
497// list.Append(new wxString("ABC"));
498// list.Sort(listcompare);
499// }
500
fd3f686c 501void wxListBase::Sort(const wxSortCompareFunction compfunc)
c801d85f 502{
fd3f686c
VZ
503 // allocate an array for the wxObject pointers of the list
504 const size_t num = GetCount();
505 void **objArray = new void *[num];
506 void **objPtr = objArray;
507
508 // go through the list and put the pointers into the array
509 wxNodeBase *node;
510 for ( node = GetFirst(); node; node = node->Next() )
511 {
512 *objPtr++ = node->Data();
513 }
514
515 // sort the array
516 qsort((void *)objArray,num,sizeof(wxObject *),compfunc);
517
518 // put the sorted pointers back into the list
519 objPtr = objArray;
520 for ( node = GetFirst(); node; node = node->Next() )
521 {
522 node->SetData(*objPtr++);
523 }
524
525 // free the array
526 delete[] objArray;
c801d85f
KB
527}
528
fd3f686c
VZ
529// -----------------------------------------------------------------------------
530// wxList (a.k.a. wxObjectList)
531// -----------------------------------------------------------------------------
c801d85f 532
fd3f686c 533void wxObjectListNode::DeleteData()
c801d85f 534{
fd3f686c 535 delete (wxObject *)GetData();
c801d85f
KB
536}
537
fd3f686c
VZ
538// -----------------------------------------------------------------------------
539// wxStringList
540// -----------------------------------------------------------------------------
541
542// instead of WX_DEFINE_LIST(wxStringListBase) we define this function
543// ourselves
544void wxStringListNode::DeleteData()
341287bf 545{
fd3f686c 546 delete [] (char *)GetData();
341287bf
JS
547}
548
50920146 549bool wxStringList::Delete(const wxChar *s)
f0824a5a
VZ
550{
551 wxStringListNode *current;
552
553 for ( current = GetFirst(); current; current = current->GetNext() )
554 {
50920146 555 if ( wxStrcmp(current->GetData(), s) == 0 )
f0824a5a
VZ
556 {
557 DeleteNode(current);
558 return TRUE;
559 }
560 }
561
562 // not found
563 return FALSE;
564}
565
db9504c5
VZ
566void wxStringList::DoCopy(const wxStringList& other)
567{
568 wxASSERT( GetCount() == 0 ); // this list must be empty before copying!
569
570 size_t count = other.GetCount();
571 for ( size_t n = 0; n < count; n++ )
572 {
77c5eefb 573 Add(other.Item(n)->GetData());
db9504c5
VZ
574 }
575}
576
c801d85f
KB
577// Variable argument list, terminated by a zero
578// Makes new storage for the strings
50920146 579wxStringList::wxStringList (const wxChar *first, ...)
c801d85f 580{
fd3f686c 581 if ( !first )
c801d85f
KB
582 return;
583
584 va_list ap;
fd3f686c 585 va_start(ap, first);
c801d85f 586
50920146 587 const wxChar *s = first;
c801d85f 588 for (;;)
fd3f686c
VZ
589 {
590 Add(s);
591
50920146 592 s = va_arg(ap, const wxChar *);
fd3f686c 593 // if (s == NULL)
2049ba38 594#ifdef __WXMSW__
48c12cb1 595 if ((int)(long) s == 0)
c801d85f
KB
596#else
597 if ((long) s == 0)
598#endif
fd3f686c
VZ
599 break;
600 }
c801d85f 601
fd3f686c 602 va_end(ap);
341287bf
JS
603}
604
fd3f686c 605// Only makes new strings if arg is TRUE
50920146 606wxChar **wxStringList::ListToArray(bool new_copies) const
341287bf 607{
50920146 608 wxChar **string_array = new wxChar *[GetCount()];
fd3f686c
VZ
609 wxStringListNode *node = GetFirst();
610 for (size_t i = 0; i < GetCount(); i++)
c801d85f 611 {
50920146 612 wxChar *s = node->GetData();
fd3f686c
VZ
613 if ( new_copies )
614 string_array[i] = copystring(s);
615 else
616 string_array[i] = s;
617 node = node->GetNext();
c801d85f 618 }
c801d85f 619
fd3f686c 620 return string_array;
c801d85f
KB
621}
622
fd3f686c 623// Checks whether s is a member of the list
50920146 624bool wxStringList::Member(const wxChar *s) const
c801d85f 625{
fd3f686c 626 for ( wxStringListNode *node = GetFirst(); node; node = node->GetNext() )
c801d85f 627 {
50920146
OK
628 const wxChar *s1 = node->GetData();
629 if (s == s1 || wxStrcmp (s, s1) == 0)
fd3f686c 630 return TRUE;
c801d85f 631 }
fd3f686c
VZ
632
633 return FALSE;
c801d85f
KB
634}
635
913df6f2 636static int LINKAGEMODE
fd3f686c 637wx_comparestrings(const void *arg1, const void *arg2)
c801d85f 638{
50920146
OK
639 wxChar **s1 = (wxChar **) arg1;
640 wxChar **s2 = (wxChar **) arg2;
c801d85f 641
50920146 642 return wxStrcmp (*s1, *s2);
c801d85f
KB
643}
644
645// Sort a list of strings - deallocates old nodes, allocates new
fd3f686c 646void wxStringList::Sort()
c801d85f 647{
fd3f686c 648 size_t N = GetCount();
50920146 649 wxChar **array = new wxChar *[N];
2a040d3f 650 wxStringListNode *node;
c801d85f 651
fd3f686c 652 size_t i = 0;
2a040d3f 653 for ( node = GetFirst(); node; node = node->GetNext() )
c801d85f 654 {
fd3f686c 655 array[i++] = node->GetData();
c801d85f 656 }
341287bf 657
50920146 658 qsort (array, N, sizeof (wxChar *), wx_comparestrings);
341287bf 659
9257d0b7
VZ
660 i = 0;
661 for ( node = GetFirst(); node; node = node->GetNext() )
662 node->SetData( array[i++] );
341287bf 663
9257d0b7 664 delete [] array;
341287bf 665}