]> git.saurik.com Git - wxWidgets.git/blame - src/common/list.cpp
Update for bitmap, image on scaling, transparancy,
[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
40// Sun CC compatibility (interference with xview/pkg.h, apparently...)
41#if defined(SUN_CC) && defined(__XVIEW__)
fd3f686c
VZ
42 #undef va_start
43 #undef va_end
44 #undef va_arg
45 #undef va_list
c801d85f
KB
46#endif
47
fd3f686c
VZ
48// =============================================================================
49// implementation
50// =============================================================================
c801d85f 51
ff528365
VZ
52// -----------------------------------------------------------------------------
53// wxListKey
54// -----------------------------------------------------------------------------
55
56bool wxListKey::operator==(wxListKeyValue value) const
57{
58 switch ( m_keyType )
59 {
60 default:
61 wxFAIL_MSG("bad key type.");
62 // let compiler optimize the line above away in release build
63 // by not putting return here...
64
65 case wxKEY_STRING:
66 return strcmp(m_key.string, value.string) == 0;
67
68 case wxKEY_INTEGER:
69 return m_key.integer == value.integer;
70 }
77c5eefb 71}
ff528365 72
fd3f686c
VZ
73// -----------------------------------------------------------------------------
74// wxNodeBase
75// -----------------------------------------------------------------------------
c801d85f 76
fd3f686c
VZ
77wxNodeBase::wxNodeBase(wxListBase *list,
78 wxNodeBase *previous, wxNodeBase *next,
79 void *data, const wxListKey& key)
c801d85f 80{
fd3f686c
VZ
81 m_list = list;
82 m_data = data;
83 m_previous = previous;
84 m_next = next;
77c5eefb 85
fd3f686c
VZ
86 switch ( key.GetKeyType() )
87 {
88 case wxKEY_NONE:
89 break;
77c5eefb 90
fd3f686c
VZ
91 case wxKEY_INTEGER:
92 m_key.integer = key.GetNumber();
93 break;
77c5eefb 94
fd3f686c
VZ
95 case wxKEY_STRING:
96 // to be free()d later
97 m_key.string = strdup(key.GetString());
98 break;
77c5eefb 99
fd3f686c
VZ
100 default:
101 wxFAIL_MSG("invalid key type");
102 }
77c5eefb 103
fd3f686c
VZ
104 if ( previous )
105 previous->m_next = this;
77c5eefb 106
fd3f686c
VZ
107 if ( next )
108 next->m_previous = this;
109}
c801d85f 110
fd3f686c
VZ
111wxNodeBase::~wxNodeBase()
112{
113 // handle the case when we're being deleted from the list by the user (i.e.
114 // not by the list itself from DeleteNode) - we must do it for
115 // compatibility with old code
116 if ( m_list != NULL )
117 {
118 m_list->DetachNode(this);
119 }
c801d85f
KB
120}
121
77c5eefb
VZ
122int wxNodeBase::IndexOf() const
123{
124 wxCHECK_MSG( m_list, NOT_FOUND, "node doesn't belong to a list in IndexOf");
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
145void wxListBase::Init(wxKeyType keyType = wxKEY_NONE)
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
154wxListBase::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 164void wxListBase::DoCopy(const wxListBase& list)
c801d85f 165{
fd3f686c
VZ
166 wxASSERT_MSG( !list.m_destroy,
167 "copying list which owns it's elements is a bad idea" );
c801d85f 168
fd3f686c
VZ
169 m_count = list.m_count;
170 m_destroy = list.m_destroy;
171 m_keyType = list.m_keyType;
172 m_nodeFirst =
173 m_nodeLast = (wxNodeBase *) NULL;
c801d85f 174
fd3f686c
VZ
175 for ( wxNodeBase *node = list.GetFirst(); node; node = node->GetNext() )
176 {
177 Append(node);
178 }
c801d85f
KB
179}
180
fd3f686c 181wxListBase::~wxListBase()
c801d85f 182{
fd3f686c
VZ
183 wxNodeBase *each = m_nodeFirst;
184 while ( each != NULL )
185 {
186 wxNodeBase *next = each->GetNext();
187 DoDeleteNode(each);
188 each = next;
189 }
c801d85f
KB
190}
191
fd3f686c 192wxNodeBase *wxListBase::AppendCommon(wxNodeBase *node)
c801d85f 193{
fd3f686c
VZ
194 if ( !m_nodeFirst )
195 {
196 m_nodeFirst = node;
197 m_nodeLast = m_nodeFirst;
198 }
199 else
200 {
201 m_nodeLast->m_next = node;
202 m_nodeLast = node;
203 }
204
205 m_count++;
206
207 return node;
c801d85f
KB
208}
209
fd3f686c 210wxNodeBase *wxListBase::Append(void *object)
c801d85f 211{
fd3f686c
VZ
212 // all objects in a keyed list should have a key
213 wxCHECK_MSG( m_keyType == wxKEY_NONE, (wxNodeBase *)NULL,
214 "need a key for the object to append" );
c801d85f 215
fd3f686c
VZ
216 wxNodeBase *node = CreateNode(m_nodeLast, (wxNodeBase *)NULL, object);
217
218 return AppendCommon(node);
c801d85f
KB
219}
220
fd3f686c 221wxNodeBase *wxListBase::Append(long key, void *object)
c801d85f 222{
fd3f686c
VZ
223 wxCHECK_MSG( (m_keyType == wxKEY_INTEGER) ||
224 (m_keyType == wxKEY_NONE && m_count == 0),
225 (wxNodeBase *)NULL,
226 "can't append object with numeric key to this list" );
227
228 wxNodeBase *node = CreateNode(m_nodeLast, (wxNodeBase *)NULL, object, key);
229 return AppendCommon(node);
c801d85f
KB
230}
231
fd3f686c 232wxNodeBase *wxListBase::Append (const char *key, void *object)
c801d85f 233{
fd3f686c
VZ
234 wxCHECK_MSG( (m_keyType == wxKEY_STRING) ||
235 (m_keyType == wxKEY_NONE && m_count == 0),
236 (wxNodeBase *)NULL,
237 "can't append object with string key to this list" );
238
239 wxNodeBase *node = CreateNode(m_nodeLast, (wxNodeBase *)NULL, object, key);
240 return AppendCommon(node);
241}
c801d85f 242
fd3f686c
VZ
243wxNodeBase *wxListBase::Insert(wxNodeBase *position, void *object)
244{
245 // all objects in a keyed list should have a key
246 wxCHECK_MSG( m_keyType == wxKEY_NONE, (wxNodeBase *)NULL,
247 "need a key for the object to insert" );
c801d85f 248
ff528365
VZ
249 wxCHECK_MSG( !position || position->m_list == this, (wxNodeBase *)NULL,
250 "can't insert before a node from another list" );
251
252 // previous and next node for the node being inserted
253 wxNodeBase *prev, *next;
fd3f686c 254 if ( position )
ff528365 255 {
fd3f686c 256 prev = position->GetPrevious();
ff528365
VZ
257 next = position;
258 }
259 else
260 {
261 // inserting in the beginning of the list
262 prev = (wxNodeBase *)NULL;
263 next = m_nodeFirst;
264 }
c801d85f 265
ff528365 266 wxNodeBase *node = CreateNode(prev, next, object);
fd3f686c 267 if ( !m_nodeFirst )
c801d85f 268 {
fd3f686c 269 m_nodeLast = node;
c801d85f 270 }
c801d85f 271
fd3f686c 272 if ( prev == NULL )
c801d85f 273 {
fd3f686c 274 m_nodeFirst = node;
c801d85f 275 }
c801d85f 276
fd3f686c
VZ
277 m_count++;
278
c801d85f
KB
279 return node;
280}
281
fd3f686c 282wxNodeBase *wxListBase::Item(size_t n) const
c801d85f 283{
fd3f686c 284 for ( wxNodeBase *current = GetFirst(); current; current = current->GetNext() )
c801d85f 285 {
fd3f686c
VZ
286 if ( n-- == 0 )
287 {
288 return current;
289 }
c801d85f 290 }
c801d85f 291
fd3f686c 292 wxFAIL_MSG( "invalid index in wxListBase::Item" );
c801d85f 293
fd3f686c 294 return NULL;
c801d85f
KB
295}
296
fd3f686c 297wxNodeBase *wxListBase::Find(const wxListKey& key) const
c801d85f 298{
fd3f686c
VZ
299 wxASSERT_MSG( m_keyType == key.GetKeyType(),
300 "this list is not keyed on the type of this key" );
c801d85f 301
fd3f686c
VZ
302 for ( wxNodeBase *current = GetFirst(); current; current = current->GetNext() )
303 {
304 if ( key == current->m_key )
305 {
306 return current;
307 }
308 }
c801d85f 309
fd3f686c
VZ
310 // not found
311 return (wxNodeBase *)NULL;
c801d85f
KB
312}
313
fd3f686c 314wxNodeBase *wxListBase::Find(void *object) const
c801d85f 315{
fd3f686c 316 for ( wxNodeBase *current = GetFirst(); current; current = current->GetNext() )
c801d85f 317 {
fd3f686c
VZ
318 if ( current->GetData() == object )
319 return current;
c801d85f 320 }
fd3f686c
VZ
321
322 // not found
323 return (wxNodeBase *)NULL;
c801d85f
KB
324}
325
77c5eefb
VZ
326int wxListBase::IndexOf(void *object) const
327{
328 wxNodeBase *node = Find( object );
329
330 return node ? node->IndexOf() : NOT_FOUND;
331}
332
fd3f686c 333void wxListBase::DoDeleteNode(wxNodeBase *node)
c801d85f 334{
fd3f686c
VZ
335 // free node's data
336 if ( m_keyType == wxKEY_STRING )
c801d85f 337 {
fd3f686c 338 free(node->m_key.string);
c801d85f 339 }
c801d85f 340
fd3f686c 341 if ( m_destroy )
c801d85f 342 {
fd3f686c 343 node->DeleteData();
c801d85f 344 }
c801d85f 345
fd3f686c 346 delete node;
c801d85f
KB
347}
348
fd3f686c 349wxNodeBase *wxListBase::DetachNode(wxNodeBase *node)
c801d85f 350{
fd3f686c
VZ
351 wxCHECK_MSG( node, NULL, "detaching NULL wxNodeBase" );
352 wxCHECK_MSG( node->m_list == this, NULL,
353 "detaching node which is not from this list" );
c801d85f 354
fd3f686c
VZ
355 // update the list
356 wxNodeBase **prevNext = node->GetPrevious() ? &node->GetPrevious()->m_next
357 : &m_nodeFirst;
358 wxNodeBase **nextPrev = node->GetNext() ? &node->GetNext()->m_previous
359 : &m_nodeLast;
c801d85f 360
fd3f686c
VZ
361 *prevNext = node->GetNext();
362 *nextPrev = node->GetPrevious();
c801d85f 363
fd3f686c 364 m_count--;
c801d85f 365
fd3f686c
VZ
366 // mark the node as not belonging to this list any more
367 node->m_list = NULL;
c801d85f 368
fd3f686c 369 return node;
c801d85f
KB
370}
371
fd3f686c 372bool wxListBase::DeleteNode(wxNodeBase *node)
c801d85f 373{
fd3f686c
VZ
374 if ( !DetachNode(node) )
375 return FALSE;
376
377 DoDeleteNode(node);
378
379 return TRUE;
c801d85f
KB
380}
381
fd3f686c 382bool wxListBase::DeleteObject(void *object)
c801d85f 383{
fd3f686c
VZ
384 for ( wxNodeBase *current = GetFirst(); current; current = current->GetNext() )
385 {
386 if ( current->GetData() == object )
387 {
388 DeleteNode(current);
389 return TRUE;
390 }
391 }
392
393 // not found
394 return FALSE;
c801d85f
KB
395}
396
fd3f686c
VZ
397
398void wxListBase::Clear()
c801d85f 399{
fd3f686c
VZ
400 wxNodeBase *current = m_nodeFirst;
401 while ( current )
c801d85f 402 {
fd3f686c
VZ
403 wxNodeBase *next = current->GetNext();
404 DoDeleteNode(current);
405 current = next;
c801d85f 406 }
fd3f686c
VZ
407
408 m_nodeFirst =
409 m_nodeLast = (wxNodeBase *)NULL;
410
411 m_count = 0;
c801d85f
KB
412}
413
fd3f686c 414void wxListBase::ForEach(wxListIterateFunction F)
c801d85f 415{
fd3f686c
VZ
416 for ( wxNodeBase *current = GetFirst(); current; current = current->GetNext() )
417 {
418 (*F)(current->GetData());
c801d85f
KB
419 }
420}
fd3f686c
VZ
421
422void *wxListBase::FirstThat(wxListIterateFunction F)
c801d85f 423{
fd3f686c
VZ
424 for ( wxNodeBase *current = GetFirst(); current; current = current->GetNext() )
425 {
426 if ( (*F)(current->GetData()) )
427 return current->GetData();
c801d85f 428 }
fd3f686c
VZ
429
430 return (wxNodeBase *)NULL;
c801d85f 431}
fd3f686c
VZ
432
433void *wxListBase::LastThat(wxListIterateFunction F)
c801d85f 434{
fd3f686c
VZ
435 for ( wxNodeBase *current = GetLast(); current; current = current->GetPrevious() )
436 {
437 if ( (*F)(current->GetData()) )
438 return current->GetData();
c801d85f 439 }
fd3f686c
VZ
440
441 return (wxNodeBase *)NULL;
c801d85f
KB
442}
443
444// (stefan.hammes@urz.uni-heidelberg.de)
445//
446// function for sorting lists. the concept is borrowed from 'qsort'.
447// by giving a sort function, arbitrary lists can be sorted.
448// method:
449// - put wxObject pointers into an array
450// - sort the array with qsort
451// - put back the sorted wxObject pointers into the list
452//
453// CAVE: the sort function receives pointers to wxObject pointers (wxObject **),
454// so dereference right!
455// EXAMPLE:
456// int listcompare(const void *arg1, const void *arg2)
457// {
458// return(compare(**(wxString **)arg1,
459// **(wxString **)arg2));
460// }
461//
462// void main()
fd3f686c
VZ
463// {
464// wxListBase list;
c801d85f
KB
465//
466// list.Append(new wxString("DEF"));
467// list.Append(new wxString("GHI"));
468// list.Append(new wxString("ABC"));
469// list.Sort(listcompare);
470// }
471
fd3f686c 472void wxListBase::Sort(const wxSortCompareFunction compfunc)
c801d85f 473{
fd3f686c
VZ
474 // allocate an array for the wxObject pointers of the list
475 const size_t num = GetCount();
476 void **objArray = new void *[num];
477 void **objPtr = objArray;
478
479 // go through the list and put the pointers into the array
480 wxNodeBase *node;
481 for ( node = GetFirst(); node; node = node->Next() )
482 {
483 *objPtr++ = node->Data();
484 }
485
486 // sort the array
487 qsort((void *)objArray,num,sizeof(wxObject *),compfunc);
488
489 // put the sorted pointers back into the list
490 objPtr = objArray;
491 for ( node = GetFirst(); node; node = node->Next() )
492 {
493 node->SetData(*objPtr++);
494 }
495
496 // free the array
497 delete[] objArray;
c801d85f
KB
498}
499
fd3f686c
VZ
500// -----------------------------------------------------------------------------
501// wxList (a.k.a. wxObjectList)
502// -----------------------------------------------------------------------------
c801d85f 503
fd3f686c 504void wxObjectListNode::DeleteData()
c801d85f 505{
fd3f686c 506 delete (wxObject *)GetData();
c801d85f
KB
507}
508
fd3f686c
VZ
509// -----------------------------------------------------------------------------
510// wxStringList
511// -----------------------------------------------------------------------------
512
513// instead of WX_DEFINE_LIST(wxStringListBase) we define this function
514// ourselves
515void wxStringListNode::DeleteData()
341287bf 516{
fd3f686c 517 delete [] (char *)GetData();
341287bf
JS
518}
519
db9504c5
VZ
520void wxStringList::DoCopy(const wxStringList& other)
521{
522 wxASSERT( GetCount() == 0 ); // this list must be empty before copying!
523
524 size_t count = other.GetCount();
525 for ( size_t n = 0; n < count; n++ )
526 {
77c5eefb 527 Add(other.Item(n)->GetData());
db9504c5
VZ
528 }
529}
530
c801d85f
KB
531// Variable argument list, terminated by a zero
532// Makes new storage for the strings
fd3f686c 533wxStringList::wxStringList (const char *first, ...)
c801d85f 534{
fd3f686c 535 if ( !first )
c801d85f
KB
536 return;
537
538 va_list ap;
fd3f686c 539 va_start(ap, first);
c801d85f 540
fd3f686c 541 const char *s = first;
c801d85f 542 for (;;)
fd3f686c
VZ
543 {
544 Add(s);
545
546 s = va_arg(ap, const char *);
547 // if (s == NULL)
2049ba38 548#ifdef __WXMSW__
c801d85f
KB
549 if ((int) s == 0)
550#else
551 if ((long) s == 0)
552#endif
fd3f686c
VZ
553 break;
554 }
c801d85f 555
fd3f686c 556 va_end(ap);
341287bf
JS
557}
558
fd3f686c
VZ
559// Only makes new strings if arg is TRUE
560char **wxStringList::ListToArray(bool new_copies) const
341287bf 561{
fd3f686c
VZ
562 char **string_array = new char *[GetCount()];
563 wxStringListNode *node = GetFirst();
564 for (size_t i = 0; i < GetCount(); i++)
c801d85f 565 {
fd3f686c
VZ
566 char *s = node->GetData();
567 if ( new_copies )
568 string_array[i] = copystring(s);
569 else
570 string_array[i] = s;
571 node = node->GetNext();
c801d85f 572 }
c801d85f 573
fd3f686c 574 return string_array;
c801d85f
KB
575}
576
fd3f686c
VZ
577// Checks whether s is a member of the list
578bool wxStringList::Member(const char *s) const
c801d85f 579{
fd3f686c 580 for ( wxStringListNode *node = GetFirst(); node; node = node->GetNext() )
c801d85f 581 {
fd3f686c
VZ
582 const char *s1 = node->GetData();
583 if (s == s1 || strcmp (s, s1) == 0)
584 return TRUE;
c801d85f 585 }
fd3f686c
VZ
586
587 return FALSE;
c801d85f
KB
588}
589
fd3f686c
VZ
590static int
591wx_comparestrings(const void *arg1, const void *arg2)
c801d85f
KB
592{
593 char **s1 = (char **) arg1;
594 char **s2 = (char **) arg2;
595
596 return strcmp (*s1, *s2);
597}
598
599// Sort a list of strings - deallocates old nodes, allocates new
fd3f686c 600void wxStringList::Sort()
c801d85f 601{
fd3f686c
VZ
602 size_t N = GetCount();
603 char **array = new char *[N];
c801d85f 604
fd3f686c
VZ
605 size_t i = 0;
606 for ( wxStringListNode *node = GetFirst(); node; node = node->GetNext() )
c801d85f 607 {
fd3f686c 608 array[i++] = node->GetData();
c801d85f 609 }
341287bf 610
fd3f686c 611 qsort (array, N, sizeof (char *), wx_comparestrings);
341287bf
JS
612 Clear();
613
fd3f686c
VZ
614 for (i = 0; i < N; i++)
615 Append (array[i]);
341287bf 616
fd3f686c 617 delete[]array;
341287bf
JS
618}
619