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