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