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