1 ////////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxList implementation
4 // Author: Julian Smart
5 // Modified by: VZ at 16/11/98: WX_DECLARE_LIST() and typesafe lists added
8 // Copyright: (c) Julian Smart and Markus Holzem
9 // Licence: wxWindows license
10 ////////////////////////////////////////////////////////////////////////////////
12 // =============================================================================
14 // =============================================================================
16 // -----------------------------------------------------------------------------
18 // -----------------------------------------------------------------------------
20 #pragma implementation "list.h"
23 // For compilers that support precompilation, includes "wx.h".
24 #include "wx/wxprec.h"
37 #include "wx/utils.h" // for copystring() (beurk...)
40 // -----------------------------------------------------------------------------
41 // implementation of standard lists
42 // -----------------------------------------------------------------------------
44 #include "wx/listimpl.cpp"
45 WX_DEFINE_LIST(wxWindowList
);
47 // =============================================================================
49 // =============================================================================
51 // -----------------------------------------------------------------------------
53 // -----------------------------------------------------------------------------
55 wxListKey wxDefaultListKey
;
57 bool wxListKey::operator==(wxListKeyValue value
) const
62 wxFAIL_MSG(_T("bad key type."));
63 // let compiler optimize the line above away in release build
64 // by not putting return here...
67 return wxStrcmp(m_key
.string
, value
.string
) == 0;
70 return m_key
.integer
== value
.integer
;
74 // -----------------------------------------------------------------------------
76 // -----------------------------------------------------------------------------
78 wxNodeBase::wxNodeBase(wxListBase
*list
,
79 wxNodeBase
*previous
, wxNodeBase
*next
,
80 void *data
, const wxListKey
& key
)
84 m_previous
= previous
;
87 switch ( key
.GetKeyType() )
93 m_key
.integer
= key
.GetNumber();
97 // to be free()d later
98 m_key
.string
= wxStrdup(key
.GetString());
102 wxFAIL_MSG(_T("invalid key type"));
106 previous
->m_next
= this;
109 next
->m_previous
= this;
112 wxNodeBase::~wxNodeBase()
114 // handle the case when we're being deleted from the list by the user (i.e.
115 // not by the list itself from DeleteNode) - we must do it for
116 // compatibility with old code
117 if ( m_list
!= NULL
)
119 if ( m_list
->m_keyType
== wxKEY_STRING
)
124 m_list
->DetachNode(this);
128 int wxNodeBase::IndexOf() const
130 wxCHECK_MSG( m_list
, wxNOT_FOUND
, _T("node doesn't belong to a list in IndexOf"));
132 // It would be more efficient to implement IndexOf() completely inside
133 // wxListBase (only traverse the list once), but this is probably a more
134 // reusable way of doing it. Can always be optimized at a later date (since
135 // IndexOf() resides in wxListBase as well) if efficiency is a problem.
137 wxNodeBase
*prev
= m_previous
;
139 for( i
= 0; prev
; i
++ )
141 prev
= prev
->m_previous
;
147 // -----------------------------------------------------------------------------
149 // -----------------------------------------------------------------------------
151 void wxListBase::Init(wxKeyType keyType
)
154 m_nodeLast
= (wxNodeBase
*) NULL
;
160 wxListBase::wxListBase(size_t count
, void *elements
[])
164 for ( size_t n
= 0; n
< count
; n
++ )
170 void wxListBase::DoCopy(const wxListBase
& list
)
172 wxASSERT_MSG( !list
.m_destroy
,
173 _T("copying list which owns it's elements is a bad idea") );
175 m_count
= list
.m_count
;
176 m_destroy
= list
.m_destroy
;
177 m_keyType
= list
.m_keyType
;
179 m_nodeLast
= (wxNodeBase
*) NULL
;
181 for ( wxNodeBase
*node
= list
.GetFirst(); node
; node
= node
->GetNext() )
187 wxListBase::~wxListBase()
189 wxNodeBase
*each
= m_nodeFirst
;
190 while ( each
!= NULL
)
192 wxNodeBase
*next
= each
->GetNext();
198 wxNodeBase
*wxListBase::AppendCommon(wxNodeBase
*node
)
203 m_nodeLast
= m_nodeFirst
;
207 m_nodeLast
->m_next
= node
;
216 wxNodeBase
*wxListBase::Append(void *object
)
218 // all objects in a keyed list should have a key
219 wxCHECK_MSG( m_keyType
== wxKEY_NONE
, (wxNodeBase
*)NULL
,
220 _T("need a key for the object to append") );
222 wxNodeBase
*node
= CreateNode(m_nodeLast
, (wxNodeBase
*)NULL
, object
);
224 return AppendCommon(node
);
227 wxNodeBase
*wxListBase::Append(long key
, void *object
)
229 wxCHECK_MSG( (m_keyType
== wxKEY_INTEGER
) ||
230 (m_keyType
== wxKEY_NONE
&& m_count
== 0),
232 _T("can't append object with numeric key to this list") );
234 wxNodeBase
*node
= CreateNode(m_nodeLast
, (wxNodeBase
*)NULL
, object
, key
);
235 return AppendCommon(node
);
238 wxNodeBase
*wxListBase::Append (const wxChar
*key
, void *object
)
240 wxCHECK_MSG( (m_keyType
== wxKEY_STRING
) ||
241 (m_keyType
== wxKEY_NONE
&& m_count
== 0),
243 _T("can't append object with string key to this list") );
245 wxNodeBase
*node
= CreateNode(m_nodeLast
, (wxNodeBase
*)NULL
, object
, key
);
246 return AppendCommon(node
);
249 wxNodeBase
*wxListBase::Insert(wxNodeBase
*position
, void *object
)
251 // all objects in a keyed list should have a key
252 wxCHECK_MSG( m_keyType
== wxKEY_NONE
, (wxNodeBase
*)NULL
,
253 _T("need a key for the object to insert") );
255 wxCHECK_MSG( !position
|| position
->m_list
== this, (wxNodeBase
*)NULL
,
256 _T("can't insert before a node from another list") );
258 // previous and next node for the node being inserted
259 wxNodeBase
*prev
, *next
;
262 prev
= position
->GetPrevious();
267 // inserting in the beginning of the list
268 prev
= (wxNodeBase
*)NULL
;
272 wxNodeBase
*node
= CreateNode(prev
, next
, object
);
288 wxNodeBase
*wxListBase::Item(size_t n
) const
290 for ( wxNodeBase
*current
= GetFirst(); current
; current
= current
->GetNext() )
298 wxFAIL_MSG( _T("invalid index in wxListBase::Item") );
300 return (wxNodeBase
*)NULL
;
303 wxNodeBase
*wxListBase::Find(const wxListKey
& key
) const
305 wxASSERT_MSG( m_keyType
== key
.GetKeyType(),
306 _T("this list is not keyed on the type of this key") );
308 for ( wxNodeBase
*current
= GetFirst(); current
; current
= current
->GetNext() )
310 if ( key
== current
->m_key
)
317 return (wxNodeBase
*)NULL
;
320 wxNodeBase
*wxListBase::Find(void *object
) const
322 for ( wxNodeBase
*current
= GetFirst(); current
; current
= current
->GetNext() )
324 if ( current
->GetData() == object
)
329 return (wxNodeBase
*)NULL
;
332 int wxListBase::IndexOf(void *object
) const
334 wxNodeBase
*node
= Find( object
);
336 return node
? node
->IndexOf() : wxNOT_FOUND
;
339 void wxListBase::DoDeleteNode(wxNodeBase
*node
)
342 if ( m_keyType
== wxKEY_STRING
)
344 free(node
->m_key
.string
);
352 // so that the node knows that it's being deleted by the list
357 wxNodeBase
*wxListBase::DetachNode(wxNodeBase
*node
)
359 wxCHECK_MSG( node
, NULL
, _T("detaching NULL wxNodeBase") );
360 wxCHECK_MSG( node
->m_list
== this, NULL
,
361 _T("detaching node which is not from this list") );
364 wxNodeBase
**prevNext
= node
->GetPrevious() ? &node
->GetPrevious()->m_next
366 wxNodeBase
**nextPrev
= node
->GetNext() ? &node
->GetNext()->m_previous
369 *prevNext
= node
->GetNext();
370 *nextPrev
= node
->GetPrevious();
374 // mark the node as not belonging to this list any more
380 bool wxListBase::DeleteNode(wxNodeBase
*node
)
382 if ( !DetachNode(node
) )
390 bool wxListBase::DeleteObject(void *object
)
392 for ( wxNodeBase
*current
= GetFirst(); current
; current
= current
->GetNext() )
394 if ( current
->GetData() == object
)
405 void wxListBase::Clear()
407 wxNodeBase
*current
= m_nodeFirst
;
410 wxNodeBase
*next
= current
->GetNext();
411 DoDeleteNode(current
);
416 m_nodeLast
= (wxNodeBase
*)NULL
;
421 void wxListBase::ForEach(wxListIterateFunction F
)
423 for ( wxNodeBase
*current
= GetFirst(); current
; current
= current
->GetNext() )
425 (*F
)(current
->GetData());
429 void *wxListBase::FirstThat(wxListIterateFunction F
)
431 for ( wxNodeBase
*current
= GetFirst(); current
; current
= current
->GetNext() )
433 if ( (*F
)(current
->GetData()) )
434 return current
->GetData();
437 return (wxNodeBase
*)NULL
;
440 void *wxListBase::LastThat(wxListIterateFunction F
)
442 for ( wxNodeBase
*current
= GetLast(); current
; current
= current
->GetPrevious() )
444 if ( (*F
)(current
->GetData()) )
445 return current
->GetData();
448 return (wxNodeBase
*)NULL
;
451 // (stefan.hammes@urz.uni-heidelberg.de)
453 // function for sorting lists. the concept is borrowed from 'qsort'.
454 // by giving a sort function, arbitrary lists can be sorted.
456 // - put wxObject pointers into an array
457 // - sort the array with qsort
458 // - put back the sorted wxObject pointers into the list
460 // CAVE: the sort function receives pointers to wxObject pointers (wxObject **),
461 // so dereference right!
463 // int listcompare(const void *arg1, const void *arg2)
465 // return(compare(**(wxString **)arg1,
466 // **(wxString **)arg2));
473 // list.Append(new wxString("DEF"));
474 // list.Append(new wxString("GHI"));
475 // list.Append(new wxString("ABC"));
476 // list.Sort(listcompare);
479 void wxListBase::Sort(const wxSortCompareFunction compfunc
)
481 // allocate an array for the wxObject pointers of the list
482 const size_t num
= GetCount();
483 void **objArray
= new void *[num
];
484 void **objPtr
= objArray
;
486 // go through the list and put the pointers into the array
488 for ( node
= GetFirst(); node
; node
= node
->Next() )
490 *objPtr
++ = node
->Data();
494 qsort((void *)objArray
,num
,sizeof(wxObject
*),compfunc
);
496 // put the sorted pointers back into the list
498 for ( node
= GetFirst(); node
; node
= node
->Next() )
500 node
->SetData(*objPtr
++);
507 // -----------------------------------------------------------------------------
508 // wxList (a.k.a. wxObjectList)
509 // -----------------------------------------------------------------------------
511 void wxObjectListNode::DeleteData()
513 delete (wxObject
*)GetData();
516 // -----------------------------------------------------------------------------
518 // -----------------------------------------------------------------------------
520 // instead of WX_DEFINE_LIST(wxStringListBase) we define this function
522 void wxStringListNode::DeleteData()
524 delete [] (char *)GetData();
527 bool wxStringList::Delete(const wxChar
*s
)
529 wxStringListNode
*current
;
531 for ( current
= GetFirst(); current
; current
= current
->GetNext() )
533 if ( wxStrcmp(current
->GetData(), s
) == 0 )
544 void wxStringList::DoCopy(const wxStringList
& other
)
546 wxASSERT( GetCount() == 0 ); // this list must be empty before copying!
548 size_t count
= other
.GetCount();
549 for ( size_t n
= 0; n
< count
; n
++ )
551 Add(other
.Item(n
)->GetData());
555 // Variable argument list, terminated by a zero
556 // Makes new storage for the strings
557 wxStringList::wxStringList (const wxChar
*first
, ...)
565 const wxChar
*s
= first
;
570 s
= va_arg(ap
, const wxChar
*);
583 // Only makes new strings if arg is TRUE
584 wxChar
**wxStringList::ListToArray(bool new_copies
) const
586 wxChar
**string_array
= new wxChar
*[GetCount()];
587 wxStringListNode
*node
= GetFirst();
588 for (size_t i
= 0; i
< GetCount(); i
++)
590 wxChar
*s
= node
->GetData();
592 string_array
[i
] = copystring(s
);
595 node
= node
->GetNext();
601 // Checks whether s is a member of the list
602 bool wxStringList::Member(const wxChar
*s
) const
604 for ( wxStringListNode
*node
= GetFirst(); node
; node
= node
->GetNext() )
606 const wxChar
*s1
= node
->GetData();
607 if (s
== s1
|| wxStrcmp (s
, s1
) == 0)
615 wx_comparestrings(const void *arg1
, const void *arg2
)
617 wxChar
**s1
= (wxChar
**) arg1
;
618 wxChar
**s2
= (wxChar
**) arg2
;
620 return wxStrcmp (*s1
, *s2
);
623 // Sort a list of strings - deallocates old nodes, allocates new
624 void wxStringList::Sort()
626 size_t N
= GetCount();
627 wxChar
**array
= new wxChar
*[N
];
628 wxStringListNode
*node
;
631 for ( node
= GetFirst(); node
; node
= node
->GetNext() )
633 array
[i
++] = node
->GetData();
636 qsort (array
, N
, sizeof (wxChar
*), wx_comparestrings
);
639 for ( node
= GetFirst(); node
; node
= node
->GetNext() )
640 node
->SetData( array
[i
++] );