1 \section{\class{wxList
}}\label{wxlist
}
3 wxList classes provide linked list functionality for wxWindows, and for an
4 application if it wishes. Depending on the form of constructor used, a list
5 can be keyed on integer or string keys to provide a primitive look-up ability.
6 See
\helpref{wxHashMap
}{wxhashmap
}\rtfsp for a faster method of storage
7 when random access is required.
9 While wxList class in the previous versions of wxWindows only could contain
10 elements of type wxObject and had essentially untyped interface (thus allowing
11 you to put apples in the list and read back oranges from it), the new wxList
12 classes family may contain elements of any type and has much more strict type
13 checking. Unfortunately, it also requires an additional line to be inserted in
14 your program for each list class you use (which is the only solution short of
15 using templates which is not done in wxWindows because of portability issues).
17 The general idea is to have the base class wxListBase working with
{\it void *
}
18 data but make all of its dangerous (because untyped) functions protected, so
19 that they can only be used from derived classes which, in turn, expose a type
20 safe interface. With this approach a new wxList-like class must be defined for
21 each list type (i.e. list of ints, of wxStrings or of MyObjects). This is done
22 with
{\it WX
\_DECLARE\_LIST} and
{\it WX
\_DEFINE\_LIST} macros like this
23 (notice the similarity with WX
\_DECLARE\_OBJARRAY and WX
\_IMPLEMENT\_OBJARRAY
29 // this part might be in a header or source (.cpp) file
35 // declare our list class: this macro declares and partly implements MyList
36 // class (which derives from wxListBase)
37 WX_DECLARE_LIST(MyListElement, MyList);
41 // the only requirement for the rest is to be AFTER the full declaration of
42 // MyListElement (for WX_DECLARE_LIST forward declaration is enough), but
43 // usually it will be found in the source file and not in the header
45 #include <wx/listimpl.cpp>
46 WX_DEFINE_LIST(MyList);
48 // now MyList class may be used as a usual wxList, but all of its methods
49 // will take/return the objects of the right (i.e. MyListElement) type. You
50 // also have MyList::Node type which is the type-safe version of wxNode.
52 MyListElement element;
53 list.Append(element); // ok
54 list.Append(
17); // error: incorrect type
56 // let's iterate over the list
57 for ( MyList::Node *node = list.GetFirst(); node; node = node->GetNext() )
59 MyListElement *current = node->GetData();
61 ...process the current element...
65 For compatibility with previous versions wxList and wxStringList classes are
66 still defined, but their usage is deprecated and they will disappear in the
67 future versions completely. The use of the latter is especially discouraged as
68 it is not only unsafe but is also much less efficient than
69 \helpref{wxArrayString
}{wxarraystring
} class.
71 In the documentation of the list classes below, the template notations are
72 used even though these classes are not really templates at all -- but it helps
73 to think about them as if they were. You should replace wxNode<T> with
74 wxListName::Node and T itself with the list element type (i.e. the first
75 parameter of WX
\_DECLARE\_LIST).
77 \wxheading{Derived from
}
79 \helpref{wxObject
}{wxobject
}
81 \wxheading{Include files
}
87 It is very common to iterate on a list as follows:
91 wxWindow *win1 = new wxWindow(...);
92 wxWindow *win2 = new wxWindow(...);
95 SomeList.Append(win1);
96 SomeList.Append(win2);
100 wxNode *node = SomeList.GetFirst();
103 wxWindow *win = node->GetData();
105 node = node->GetNext();
109 To delete nodes in a list as the list is being traversed, replace
113 node = node->GetNext();
123 node = SomeList.GetFirst();
127 See
\helpref{wxNode
}{wxnode
} for members that retrieve the data associated with a node, and
128 members for getting to the next or previous node.
132 \helpref{wxNode
}{wxnode
},
\helpref{wxStringList
}{wxstringlist
},
133 \helpref{wxArray
}{wxarray
}
135 \latexignore{\rtfignore{\wxheading{Members
}}}
137 \membersection{wxList::wxList
}
139 \func{}{wxList
}{\void}
141 \func{}{wxList
}{\param{unsigned int
}{ key
\_type}}
143 \func{}{wxList
}{\param{int
}{ n
},
\param{T *
}{objects
[]}}
145 \func{}{wxList
}{\param{T *
}{object
}, ...
}
147 Constructors.
{\it key
\_type} is one of wxKEY
\_NONE, wxKEY
\_INTEGER, or wxKEY
\_STRING,
148 and indicates what sort of keying is required (if any).
150 {\it objects
} is an array of
{\it n
} objects with which to initialize the list.
152 The variable-length argument list constructor must be supplied with a
155 \membersection{wxList::
\destruct{wxList
}}
157 \func{}{\destruct{wxList
}}{\void}
159 Destroys the list. Also destroys any remaining nodes, but does not destroy
160 client data held in the nodes.
162 \membersection{wxList::Append
}\label{wxlistappend
}
164 \func{wxNode<T> *
}{Append
}{\param{T *
}{object
}}
166 \func{wxNode<T> *
}{Append
}{\param{long
}{ key
},
\param{T *
}{object
}}
168 \func{wxNode<T> *
}{Append
}{\param{const wxString\&
}{key
},
\param{T *
}{object
}}
170 Appends a new
\helpref{wxNode
}{wxnode
} to the end of the list and puts a
171 pointer to the
\rtfsp{\it object
} in the node. The last two forms store a key
172 with the object for later retrieval using the key. The new node is returned in
175 The key string is copied and stored by the list implementation.
177 \membersection{wxList::Clear
}\label{wxlistclear
}
179 \func{void
}{Clear
}{\void}
181 Clears the list (but does not delete the client data stored with each node
182 unless you called DeleteContents(
{\tt true
}), in which case it deletes data).
184 \membersection{wxList::DeleteContents
}\label{wxlistdeletecontents
}
186 \func{void
}{DeleteContents
}{\param{bool
}{ destroy
}}
188 If
{\it destroy
} is
{\tt true
}, instructs the list to call
{\it delete
} on the client contents of
189 a node whenever the node is destroyed. The default is
{\tt false
}.
191 \membersection{wxList::DeleteNode
}\label{wxlistdeletenode
}
193 \func{bool
}{DeleteNode
}{\param{wxNode<T> *
}{node
}}
195 Deletes the given node from the list, returning
{\tt true
} if successful.
197 \membersection{wxList::DeleteObject
}\label{wxlistdeleteobject
}
199 \func{bool
}{DeleteObject
}{\param{T *
}{object
}}
201 Finds the given client
{\it object
} and deletes the appropriate node from the list, returning
202 {\tt true
} if successful. The application must delete the actual object separately.
204 \membersection{wxList::Find
}\label{wxlistfind
}
206 \func{wxNode<T> *
}{Find
}{\param{T *
}{ object
}}
208 Returns the node whose client date is
{\it object
} or NULL if none found.
210 \func{wxNode<T> *
}{Find
}{\param{long
}{ key
}}
212 \func{wxNode<T> *
}{Find
}{\param{const wxString\&
}{key
}}
214 Returns the node whose stored key matches
{\it key
}. Use on a keyed list only.
216 \membersection{wxList::GetCount
}\label{wxlistgetcount
}
218 \constfunc{size
\_t}{GetCount
}{\void}
220 Returns the number of elements in the list.
222 \membersection{wxList::GetFirst
}\label{wxlistgetfirst
}
224 \func{wxNode<T> *
}{GetFirst
}{\void}
226 Returns the first node in the list (NULL if the list is empty).
228 \membersection{wxList::GetLast
}\label{wxlistgetlast
}
230 \func{wxNode<T> *
}{GetLast
}{\void}
232 Returns the last node in the list (NULL if the list is empty).
234 \membersection{wxList::IndexOf
}\label{wxlistindexof
}
236 \func{int
}{IndexOf
}{\param{T*
}{ obj
}}
238 Returns the index of
{\it obj
} within the list or wxNOT
\_FOUND if
{\it obj
}
239 is not found in the list.
241 \membersection{wxList::Insert
}\label{wxlistinsert
}
243 \func{wxNode<T> *
}{Insert
}{\param{T *
}{object
}}
245 Insert object at front of list.
247 \func{wxNode<T> *
}{Insert
}{\param{size
\_t }{position
},
\param{T *
}{object
}}
249 Insert object before
{\it position
}, i.e. the index of the new item in the
250 list will be equal to
{\it position
}.
{\it position
} should be less than or
251 equal to
\helpref{GetCount
}{wxlistgetcount
}; if it is equal to it, this is the
252 same as calling
\helpref{Append
}{wxlistappend
}.
254 \func{wxNode<T> *
}{Insert
}{\param{wxNode<T> *
}{node
},
\param{T *
}{object
}}
256 Inserts the object before the given
{\it node
}.
258 \membersection{wxList::IsEmpty
}\label{wxlistisempty
}
260 \constfunc{bool
}{IsEmpty
}{\void}
262 Returns
{\tt true
} if the list is empty,
{\tt false
} otherwise.
264 % Use different label name to avoid clashing with wxListItem label
265 \membersection{wxList::Item
}\label{wxlistitemfunc
}
267 \constfunc{wxNode<T> *
}{Item
}{\param{size
\_t }{index
}}
269 Returns the node at given position in the list.
271 \membersection{wxList::Member
}\label{wxlistmember
}
273 \func{wxNode<T> *
}{Member
}{\param{T *
}{object
}}
275 {\bf NB:
} This function is deprecated, use
\helpref{Find
}{wxlistfind
} instead.
277 Returns the node associated with
{\it object
} if it is in the list, NULL otherwise.
279 \membersection{wxList::Nth
}\label{wxlistnth
}
281 \func{wxNode<T> *
}{Nth
}{\param{int
}{ n
}}
283 {\bf NB:
} This function is deprecated, use
\helpref{Item
}{wxlistitemfunc
} instead.
285 Returns the
{\it nth
} node in the list, indexing from zero (NULL if the list is empty
286 or the nth node could not be found).
288 \membersection{wxList::Number
}\label{wxlistnumber
}
290 \func{int
}{Number
}{\void}
292 {\bf NB:
} This function is deprecated, use
\helpref{GetCount
}{wxlistgetcount
} instead.
294 Returns the number of elements in the list.
296 \membersection{wxList::Sort
}\label{wxlistsort
}
298 \func{void
}{Sort
}{\param{wxSortCompareFunction
}{ compfunc
}}
301 // Type of compare function for list sort operation (as in 'qsort')
302 typedef int
(*wxSortCompareFunction)(const void *elem1, const void *elem2);
305 Allows the sorting of arbitrary lists by giving
306 a function to compare two list elements. We use the system {\bf qsort} function
307 for the actual sorting process.
309 If you use untyped wxList the sort function receives pointers to wxObject
310 pointers (wxObject **), so be careful to dereference appropriately - but,
311 of course, a better solution is to use list of appropriate type defined with
312 {\tt WX
\_DECLARE\_LIST}.
317 int listcompare(const void *arg1, const void *arg2)
319 return(compare
(**(wxString **)arg1, // use the wxString 'compare'
320 **(wxString **)arg2)); // function
327 list.Append(new wxString("DEF"));
328 list.Append(new wxString("GHI"));
329 list.Append(new wxString("ABC"));
330 list.Sort(listcompare);