]> git.saurik.com Git - wxWidgets.git/blame - docs/latex/wx/list.tex
wxPerl documentation updates
[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.
6See \helpref{wxHashTable}{wxhashtable}\rtfsp for a faster method of storage
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
28{\small%
29\begin{verbatim}
30 // this part might be in a header or source (.cpp) file
31 class MyListElement
32 {
33 ... // whatever
34 };
35
36 // declare our list class: this macro declares and partly implements MyList
37 // class (which derives from wxListBase)
f776e250 38 WX_DECLARE_LIST(MyListElement, MyList);
6e6110ee
VZ
39
40 ...
41
42 // the only requirment for the rest is to be AFTER the full declaration of
43 // MyListElement (for WX_DECLARE_LIST forward declaration is enough), but
44 // usually it will be found in the source file and not in the header
45
46 #include <wx/listimpl.cpp>
f776e250 47 WX_DEFINE_LIST(MyList);
6e6110ee
VZ
48
49 // now MyList class may be used as a usual wxList, but all of its methods
50 // will take/return the objects of the right (i.e. MyListElement) type. You
51 // also have MyList::Node type which is the type-safe version of wxNode.
52 MyList list;
53 MyListElement element;
bb250157
VZ
54 list.Append(element); // ok
55 list.Append(17); // error: incorrect type
6e6110ee
VZ
56
57 // let's iterate over the list
58 for ( MyList::Node *node = list.GetFirst(); node; node = node->GetNext() )
59 {
60 MyListElement *current = node->GetData();
61
62 ...process the current element...
63 }
64\end{verbatim}
65}
66
67For compatibility with previous versions wxList and wxStringList classes are
68still defined, but their usage is deprecated and they will disappear in the
703f03c3
VZ
69future versions completely. The use of the latter is especially discouraged as
70it is not only unsafe but is also much less efficient than
35d367d8 71\helpref{wxArrayString}{wxarraystring} class.
a660d684 72
d8996187
VZ
73In the documentation of the list classes below, you should replace wxNode with
74wxListName::Node and wxObject with the list element type (i.e. the first
75parameter of WX\_DECLARE\_LIST) for the template lists.
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
143\func{}{wxList}{\param{int}{ n}, \param{wxObject *}{objects[]}}
144
145\func{}{wxList}{\param{wxObject *}{object}, ...}
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
KB
163
164\func{wxNode *}{Append}{\param{wxObject *}{object}}
165
166\func{wxNode *}{Append}{\param{long}{ key}, \param{wxObject *}{object}}
167
168\func{wxNode *}{Append}{\param{const wxString\& }{key}, \param{wxObject *}{object}}
169
170Appends a new {\bf wxNode} to the end of the list and puts a pointer to the
171\rtfsp{\it object} in the node. The last two forms store a key with the object for
172later retrieval using the key. The new node is returned in each case.
173
174The key string is copied and stored by the list implementation.
175
e12be2f7 176\membersection{wxList::Clear}\label{wxlistclear}
a660d684
KB
177
178\func{void}{Clear}{\void}
179
ba9f095e
VS
180Clears the list (but does not delete the client data stored with each node
181unless you called DeleteContents(TRUE), in which case it deletes data).
a660d684 182
6be663cf 183\membersection{wxList::DeleteContents}\label{wxlistdeletecontents}
a660d684
KB
184
185\func{void}{DeleteContents}{\param{bool}{ destroy}}
186
187If {\it destroy} is TRUE, instructs the list to call {\it delete} on the client contents of
188a node whenever the node is destroyed. The default is FALSE.
189
e12be2f7 190\membersection{wxList::DeleteNode}\label{wxlistdeletenode}
a660d684
KB
191
192\func{bool}{DeleteNode}{\param{wxNode *}{node}}
193
194Deletes the given node from the list, returning TRUE if successful.
195
e12be2f7 196\membersection{wxList::DeleteObject}\label{wxlistdeleteobject}
a660d684
KB
197
198\func{bool}{DeleteObject}{\param{wxObject *}{object}}
199
200Finds the given client {\it object} and deletes the appropriate node from the list, returning
201TRUE if successful. The application must delete the actual object separately.
202
e12be2f7 203\membersection{wxList::Find}\label{wxlistfind}
a660d684
KB
204
205\func{wxNode *}{Find}{\param{long}{ key}}
206
207\func{wxNode *}{Find}{\param{const wxString\& }{key}}
208
209Returns the node whose stored key matches {\it key}. Use on a keyed list only.
210
d8996187
VZ
211\membersection{wxList::GetCount}\label{wxlistgetcount}
212
213\constfunc{size\_t}{GetCount}{\void}
214
215Returns the number of elements in the list.
216
e12be2f7 217\membersection{wxList::GetFirst}\label{wxlistgetfirst}
a660d684 218
f3a65071 219\func{wxNode *}{GetFirst}{\void}
a660d684
KB
220
221Returns the first node in the list (NULL if the list is empty).
222
e12be2f7 223\membersection{wxList::GetLast}\label{wxlistgetlast}
d8996187
VZ
224
225\func{wxNode *}{GetLast}{\void}
226
227Returns the last node in the list (NULL if the list is empty).
228
e12be2f7 229\membersection{wxList::IndexOf}\label{wxlistindexof}
77c5eefb
VZ
230
231\func{int}{IndexOf}{\param{wxObject*}{ obj }}
232
cd4915e2 233Returns the index of {\it obj} within the list or wxNOT\_FOUND if {\it obj}
77c5eefb
VZ
234is not found in the list.
235
e12be2f7 236\membersection{wxList::Insert}\label{wxlistinsert}
a660d684
KB
237
238\func{wxNode *}{Insert}{\param{wxObject *}{object}}
239
240Insert object at front of list.
241
d8996187 242\func{wxNode *}{Insert}{\param{size\_t }{position}, \param{wxObject *}{object}}
a660d684 243
d8996187
VZ
244Insert object before {\it position}, i.e. the index of the new item in the
245list will be equal to {\it position}. {\it position} should be less than or
246equal to \helpref{GetCount}{wxlistgetcount}; if it is equal to it, this is the
247same as calling \helpref{Append}{wxlistappend}.
a660d684 248
d8996187 249\func{wxNode *}{Insert}{\param{wxNode *}{node}, \param{wxObject *}{object}}
a660d684 250
d8996187 251Inserts the object before the given {\it node}.
a660d684 252
d8996187 253\membersection{wxList::Item}\label{wxlistitem}
a660d684 254
d8996187
VZ
255\constfunc{wxNode *}{Item}{\param{size\_t }{index}}
256
257Returns the node at given position in the list.
a660d684 258
e12be2f7 259\membersection{wxList::Member}\label{wxlistmember}
a660d684
KB
260
261\func{wxNode *}{Member}{\param{wxObject *}{object}}
262
d8996187
VZ
263{\bf NB:} This function is deprecated, use \helpref{Find}{wxlistfind} instead.
264
a660d684
KB
265Returns the node associated with {\it object} if it is in the list, NULL otherwise.
266
e12be2f7 267\membersection{wxList::Nth}\label{wxlistnth}
a660d684
KB
268
269\func{wxNode *}{Nth}{\param{int}{ n}}
270
d8996187
VZ
271{\bf NB:} This function is deprecated, use \helpref{Item}{wxlistitem} instead.
272
a660d684
KB
273Returns the {\it nth} node in the list, indexing from zero (NULL if the list is empty
274or the nth node could not be found).
275
e12be2f7 276\membersection{wxList::Number}\label{wxlistnumber}
a660d684
KB
277
278\func{int}{Number}{\void}
279
d8996187
VZ
280{\bf NB:} This function is deprecated, use \helpref{GetCount}{wxlistgetcount} instead.
281
a660d684
KB
282Returns the number of elements in the list.
283
e12be2f7 284\membersection{wxList::Sort}\label{wxlistsort}
a660d684
KB
285
286\func{void}{Sort}{\param{wxSortCompareFunction}{ compfunc}}
287
288\begin{verbatim}
289 // Type of compare function for list sort operation (as in 'qsort')
290 typedef int (*wxSortCompareFunction)(const void *elem1, const void *elem2);
291\end{verbatim}
292
293Allows the sorting of arbitrary lists by giving
294a function to compare two list elements. We use the system {\bf qsort} function
064998bf
VZ
295for the actual sorting process.
296
297If you use untyped wxList the sort function receives pointers to wxObject
298pointers (wxObject **), so be careful to dereference appropriately - but,
299of course, a better solution is to use list of appropriate type defined with
300{\tt WX\_DECLARE\_LIST}.
a660d684
KB
301
302Example:
303
304\begin{verbatim}
305 int listcompare(const void *arg1, const void *arg2)
306 {
307 return(compare(**(wxString **)arg1, // use the wxString 'compare'
308 **(wxString **)arg2)); // function
309 }
310
311 void main()
312 {
313 wxList list;
314
315 list.Append(new wxString("DEF"));
316 list.Append(new wxString("GHI"));
317 list.Append(new wxString("ABC"));
318 list.Sort(listcompare);
319 }
320\end{verbatim}
321