]> git.saurik.com Git - wxWidgets.git/blame - docs/latex/wx/list.tex
fixed Update() signature
[wxWidgets.git] / docs / latex / wx / list.tex
CommitLineData
a660d684
KB
1\section{\class{wxList}}\label{wxlist}
2
6e6110ee
VZ
3wxList classes provide linked list functionality for wxWindows, and for an
4application if it wishes. Depending on the form of constructor used, a list
5can be keyed on integer or string keys to provide a primitive look-up ability.
e676441f 6See \helpref{wxHashMap}{wxhashmap}\rtfsp for a faster method of storage
6e6110ee
VZ
7when random access is required.
8
9While wxList class in the previous versions of wxWindows only could contain
10elements of type wxObject and had essentially untyped interface (thus allowing
11you to put apples in the list and read back oranges from it), the new wxList
5442f435 12classes family may contain elements of any type and has much more strict type
6e6110ee
VZ
13checking. Unfortunately, it also requires an additional line to be inserted in
14your program for each list class you use (which is the only solution short of
15using templates which is not done in wxWindows because of portability issues).
16
17The general idea is to have the base class wxListBase working with {\it void *}
18data but make all of its dangerous (because untyped) functions protected, so
19that they can only be used from derived classes which, in turn, expose a type
20safe interface. With this approach a new wxList-like class must be defined for
21each list type (i.e. list of ints, of wxStrings or of MyObjects). This is done
bb250157 22with {\it WX\_DECLARE\_LIST} and {\it WX\_DEFINE\_LIST} macros like this
6e6110ee
VZ
23(notice the similarity with WX\_DECLARE\_OBJARRAY and WX\_IMPLEMENT\_OBJARRAY
24macros):
25
26\wxheading{Example}
27
6e6110ee
VZ
28\begin{verbatim}
29 // this part might be in a header or source (.cpp) file
30 class MyListElement
31 {
32 ... // whatever
33 };
34
35 // declare our list class: this macro declares and partly implements MyList
36 // class (which derives from wxListBase)
f776e250 37 WX_DECLARE_LIST(MyListElement, MyList);
6e6110ee
VZ
38
39 ...
40
2edb0bde 41 // the only requirement for the rest is to be AFTER the full declaration of
6e6110ee
VZ
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
44
45 #include <wx/listimpl.cpp>
f776e250 46 WX_DEFINE_LIST(MyList);
6e6110ee
VZ
47
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.
51 MyList list;
52 MyListElement element;
bb250157
VZ
53 list.Append(element); // ok
54 list.Append(17); // error: incorrect type
6e6110ee
VZ
55
56 // let's iterate over the list
57 for ( MyList::Node *node = list.GetFirst(); node; node = node->GetNext() )
58 {
59 MyListElement *current = node->GetData();
60
61 ...process the current element...
62 }
63\end{verbatim}
6e6110ee
VZ
64
65For compatibility with previous versions wxList and wxStringList classes are
66still defined, but their usage is deprecated and they will disappear in the
703f03c3
VZ
67future versions completely. The use of the latter is especially discouraged as
68it is not only unsafe but is also much less efficient than
35d367d8 69\helpref{wxArrayString}{wxarraystring} class.
a660d684 70
2b5f62a0
VZ
71In the documentation of the list classes below, the template notations are
72used even though these classes are not really templates at all -- but it helps
73to think about them as if they were. You should replace wxNode<T> with
74wxListName::Node and T itself with the list element type (i.e. the first
75parameter of WX\_DECLARE\_LIST).
d8996187 76
a660d684
KB
77\wxheading{Derived from}
78
79\helpref{wxObject}{wxobject}
80
954b8ae6
JS
81\wxheading{Include files}
82
83<wx/list.h>
84
a660d684
KB
85\wxheading{Example}
86
87It is very common to iterate on a list as follows:
88
89\begin{verbatim}
90 ...
9838df2c
JS
91 wxWindow *win1 = new wxWindow(...);
92 wxWindow *win2 = new wxWindow(...);
a660d684
KB
93
94 wxList SomeList;
9838df2c
JS
95 SomeList.Append(win1);
96 SomeList.Append(win2);
a660d684
KB
97
98 ...
99
f3a65071 100 wxNode *node = SomeList.GetFirst();
a660d684
KB
101 while (node)
102 {
d8996187 103 wxWindow *win = node->GetData();
a660d684 104 ...
d8996187 105 node = node->GetNext();
a660d684
KB
106 }
107\end{verbatim}
108
109To delete nodes in a list as the list is being traversed, replace
110
111\begin{verbatim}
112 ...
d8996187 113 node = node->GetNext();
a660d684
KB
114 ...
115\end{verbatim}
116
117with
118
119\begin{verbatim}
120 ...
9838df2c 121 delete win;
a660d684 122 delete node;
f3a65071 123 node = SomeList.GetFirst();
a660d684
KB
124 ...
125\end{verbatim}
126
127See \helpref{wxNode}{wxnode} for members that retrieve the data associated with a node, and
128members for getting to the next or previous node.
129
a660d684
KB
130\wxheading{See also}
131
6e6110ee
VZ
132\helpref{wxNode}{wxnode}, \helpref{wxStringList}{wxstringlist},
133\helpref{wxArray}{wxarray}
a660d684
KB
134
135\latexignore{\rtfignore{\wxheading{Members}}}
136
a660d684
KB
137\membersection{wxList::wxList}
138
139\func{}{wxList}{\void}
140
141\func{}{wxList}{\param{unsigned int}{ key\_type}}
142
2b5f62a0 143\func{}{wxList}{\param{int}{ n}, \param{T *}{objects[]}}
a660d684 144
2b5f62a0 145\func{}{wxList}{\param{T *}{object}, ...}
a660d684
KB
146
147Constructors. {\it key\_type} is one of wxKEY\_NONE, wxKEY\_INTEGER, or wxKEY\_STRING,
148and indicates what sort of keying is required (if any).
149
150{\it objects} is an array of {\it n} objects with which to initialize the list.
151
152The variable-length argument list constructor must be supplied with a
153terminating NULL.
154
155\membersection{wxList::\destruct{wxList}}
156
157\func{}{\destruct{wxList}}{\void}
158
159Destroys the list. Also destroys any remaining nodes, but does not destroy
160client data held in the nodes.
161
e12be2f7 162\membersection{wxList::Append}\label{wxlistappend}
a660d684 163
2b5f62a0 164\func{wxNode<T> *}{Append}{\param{T *}{object}}
a660d684 165
2b5f62a0 166\func{wxNode<T> *}{Append}{\param{long}{ key}, \param{T *}{object}}
a660d684 167
2b5f62a0 168\func{wxNode<T> *}{Append}{\param{const wxString\& }{key}, \param{T *}{object}}
a660d684 169
2b5f62a0
VZ
170Appends a new \helpref{wxNode}{wxnode} to the end of the list and puts a
171pointer to the \rtfsp{\it object} in the node. The last two forms store a key
172with the object for later retrieval using the key. The new node is returned in
173each case.
a660d684
KB
174
175The key string is copied and stored by the list implementation.
176
e12be2f7 177\membersection{wxList::Clear}\label{wxlistclear}
a660d684
KB
178
179\func{void}{Clear}{\void}
180
ba9f095e 181Clears the list (but does not delete the client data stored with each node
cc81d32f 182unless you called DeleteContents({\tt true}), in which case it deletes data).
a660d684 183
6be663cf 184\membersection{wxList::DeleteContents}\label{wxlistdeletecontents}
a660d684
KB
185
186\func{void}{DeleteContents}{\param{bool}{ destroy}}
187
cc81d32f
VS
188If {\it destroy} is {\tt true}, instructs the list to call {\it delete} on the client contents of
189a node whenever the node is destroyed. The default is {\tt false}.
a660d684 190
e12be2f7 191\membersection{wxList::DeleteNode}\label{wxlistdeletenode}
a660d684 192
2b5f62a0 193\func{bool}{DeleteNode}{\param{wxNode<T> *}{node}}
a660d684 194
cc81d32f 195Deletes the given node from the list, returning {\tt true} if successful.
a660d684 196
e12be2f7 197\membersection{wxList::DeleteObject}\label{wxlistdeleteobject}
a660d684 198
2b5f62a0 199\func{bool}{DeleteObject}{\param{T *}{object}}
a660d684
KB
200
201Finds the given client {\it object} and deletes the appropriate node from the list, returning
cc81d32f 202{\tt true} if successful. The application must delete the actual object separately.
a660d684 203
e12be2f7 204\membersection{wxList::Find}\label{wxlistfind}
a660d684 205
2b5f62a0 206\func{wxNode<T> *}{Find}{\param{T *}{ object}}
a660d684 207
2b5f62a0
VZ
208Returns the node whose client date is {\it object} or NULL if none found.
209
210\func{wxNode<T> *}{Find}{\param{long}{ key}}
211
212\func{wxNode<T> *}{Find}{\param{const wxString\& }{key}}
a660d684
KB
213
214Returns the node whose stored key matches {\it key}. Use on a keyed list only.
215
d8996187
VZ
216\membersection{wxList::GetCount}\label{wxlistgetcount}
217
218\constfunc{size\_t}{GetCount}{\void}
219
220Returns the number of elements in the list.
221
e12be2f7 222\membersection{wxList::GetFirst}\label{wxlistgetfirst}
a660d684 223
2b5f62a0 224\func{wxNode<T> *}{GetFirst}{\void}
a660d684
KB
225
226Returns the first node in the list (NULL if the list is empty).
227
e12be2f7 228\membersection{wxList::GetLast}\label{wxlistgetlast}
d8996187 229
2b5f62a0 230\func{wxNode<T> *}{GetLast}{\void}
d8996187
VZ
231
232Returns the last node in the list (NULL if the list is empty).
233
e12be2f7 234\membersection{wxList::IndexOf}\label{wxlistindexof}
77c5eefb 235
2b5f62a0 236\func{int}{IndexOf}{\param{T*}{ obj }}
77c5eefb 237
cd4915e2 238Returns the index of {\it obj} within the list or wxNOT\_FOUND if {\it obj}
77c5eefb
VZ
239is not found in the list.
240
e12be2f7 241\membersection{wxList::Insert}\label{wxlistinsert}
a660d684 242
2b5f62a0 243\func{wxNode<T> *}{Insert}{\param{T *}{object}}
a660d684
KB
244
245Insert object at front of list.
246
2b5f62a0 247\func{wxNode<T> *}{Insert}{\param{size\_t }{position}, \param{T *}{object}}
a660d684 248
d8996187
VZ
249Insert object before {\it position}, i.e. the index of the new item in the
250list will be equal to {\it position}. {\it position} should be less than or
251equal to \helpref{GetCount}{wxlistgetcount}; if it is equal to it, this is the
252same as calling \helpref{Append}{wxlistappend}.
a660d684 253
2b5f62a0 254\func{wxNode<T> *}{Insert}{\param{wxNode<T> *}{node}, \param{T *}{object}}
a660d684 255
d8996187 256Inserts the object before the given {\it node}.
a660d684 257
b79a8705
VZ
258\membersection{wxList::IsEmpty}\label{wxlistisempty}
259
260\constfunc{bool}{IsEmpty}{\void}
261
cc81d32f 262Returns {\tt true} if the list is empty, {\tt false} otherwise.
b79a8705 263
0b0625e9
JS
264% Use different label name to avoid clashing with wxListItem label
265\membersection{wxList::Item}\label{wxlistitemfunc}
a660d684 266
2b5f62a0 267\constfunc{wxNode<T> *}{Item}{\param{size\_t }{index}}
d8996187
VZ
268
269Returns the node at given position in the list.
a660d684 270
e12be2f7 271\membersection{wxList::Member}\label{wxlistmember}
a660d684 272
2b5f62a0 273\func{wxNode<T> *}{Member}{\param{T *}{object}}
a660d684 274
d8996187
VZ
275{\bf NB:} This function is deprecated, use \helpref{Find}{wxlistfind} instead.
276
a660d684
KB
277Returns the node associated with {\it object} if it is in the list, NULL otherwise.
278
e12be2f7 279\membersection{wxList::Nth}\label{wxlistnth}
a660d684 280
2b5f62a0 281\func{wxNode<T> *}{Nth}{\param{int}{ n}}
a660d684 282
0b0625e9 283{\bf NB:} This function is deprecated, use \helpref{Item}{wxlistitemfunc} instead.
d8996187 284
a660d684
KB
285Returns the {\it nth} node in the list, indexing from zero (NULL if the list is empty
286or the nth node could not be found).
287
e12be2f7 288\membersection{wxList::Number}\label{wxlistnumber}
a660d684
KB
289
290\func{int}{Number}{\void}
291
d8996187
VZ
292{\bf NB:} This function is deprecated, use \helpref{GetCount}{wxlistgetcount} instead.
293
a660d684
KB
294Returns the number of elements in the list.
295
e12be2f7 296\membersection{wxList::Sort}\label{wxlistsort}
a660d684
KB
297
298\func{void}{Sort}{\param{wxSortCompareFunction}{ compfunc}}
299
300\begin{verbatim}
301 // Type of compare function for list sort operation (as in 'qsort')
302 typedef int (*wxSortCompareFunction)(const void *elem1, const void *elem2);
303\end{verbatim}
304
305Allows the sorting of arbitrary lists by giving
306a function to compare two list elements. We use the system {\bf qsort} function
064998bf
VZ
307for the actual sorting process.
308
309If you use untyped wxList the sort function receives pointers to wxObject
310pointers (wxObject **), so be careful to dereference appropriately - but,
311of course, a better solution is to use list of appropriate type defined with
312{\tt WX\_DECLARE\_LIST}.
a660d684
KB
313
314Example:
315
316\begin{verbatim}
317 int listcompare(const void *arg1, const void *arg2)
318 {
319 return(compare(**(wxString **)arg1, // use the wxString 'compare'
320 **(wxString **)arg2)); // function
321 }
322
323 void main()
324 {
325 wxList list;
326
327 list.Append(new wxString("DEF"));
328 list.Append(new wxString("GHI"));
329 list.Append(new wxString("ABC"));
330 list.Sort(listcompare);
331 }
332\end{verbatim}
333