]>
Commit | Line | Data |
---|---|---|
a660d684 KB |
1 | \section{\class{wxList}}\label{wxlist} |
2 | ||
6e6110ee VZ |
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{wxHashTable}{wxhashtable}\rtfsp for a faster method of storage | |
7 | when random access is required. | |
8 | ||
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 stricter 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). | |
16 | ||
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\_IMPLEMENT\_LIST} macros like this | |
23 | (notice the similarity with WX\_DECLARE\_OBJARRAY and WX\_IMPLEMENT\_OBJARRAY | |
24 | macros): | |
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; | |
54 | list.Add(element); // ok | |
55 | list.Add(17); // error: incorrect type | |
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 | ||
67 | For compatibility with previous versions wxList and wxStringList classes are | |
68 | still defined, but their usage is deprecated and they will disappear in the | |
69 | future versions completely. | |
a660d684 | 70 | |
d8996187 VZ |
71 | In the documentation of the list classes below, you should replace wxNode with |
72 | wxListName::Node and wxObject with the list element type (i.e. the first | |
73 | parameter of WX\_DECLARE\_LIST) for the template lists. | |
74 | ||
a660d684 KB |
75 | \wxheading{Derived from} |
76 | ||
77 | \helpref{wxObject}{wxobject} | |
78 | ||
954b8ae6 JS |
79 | \wxheading{Include files} |
80 | ||
81 | <wx/list.h> | |
82 | ||
a660d684 KB |
83 | \wxheading{Example} |
84 | ||
85 | It is very common to iterate on a list as follows: | |
86 | ||
87 | \begin{verbatim} | |
88 | ... | |
9838df2c JS |
89 | wxWindow *win1 = new wxWindow(...); |
90 | wxWindow *win2 = new wxWindow(...); | |
a660d684 KB |
91 | |
92 | wxList SomeList; | |
9838df2c JS |
93 | SomeList.Append(win1); |
94 | SomeList.Append(win2); | |
a660d684 KB |
95 | |
96 | ... | |
97 | ||
f3a65071 | 98 | wxNode *node = SomeList.GetFirst(); |
a660d684 KB |
99 | while (node) |
100 | { | |
d8996187 | 101 | wxWindow *win = node->GetData(); |
a660d684 | 102 | ... |
d8996187 | 103 | node = node->GetNext(); |
a660d684 KB |
104 | } |
105 | \end{verbatim} | |
106 | ||
107 | To delete nodes in a list as the list is being traversed, replace | |
108 | ||
109 | \begin{verbatim} | |
110 | ... | |
d8996187 | 111 | node = node->GetNext(); |
a660d684 KB |
112 | ... |
113 | \end{verbatim} | |
114 | ||
115 | with | |
116 | ||
117 | \begin{verbatim} | |
118 | ... | |
9838df2c | 119 | delete win; |
a660d684 | 120 | delete node; |
f3a65071 | 121 | node = SomeList.GetFirst(); |
a660d684 KB |
122 | ... |
123 | \end{verbatim} | |
124 | ||
125 | See \helpref{wxNode}{wxnode} for members that retrieve the data associated with a node, and | |
126 | members for getting to the next or previous node. | |
127 | ||
a660d684 KB |
128 | \wxheading{See also} |
129 | ||
6e6110ee VZ |
130 | \helpref{wxNode}{wxnode}, \helpref{wxStringList}{wxstringlist}, |
131 | \helpref{wxArray}{wxarray} | |
a660d684 KB |
132 | |
133 | \latexignore{\rtfignore{\wxheading{Members}}} | |
134 | ||
a660d684 KB |
135 | \membersection{wxList::wxList} |
136 | ||
137 | \func{}{wxList}{\void} | |
138 | ||
139 | \func{}{wxList}{\param{unsigned int}{ key\_type}} | |
140 | ||
141 | \func{}{wxList}{\param{int}{ n}, \param{wxObject *}{objects[]}} | |
142 | ||
143 | \func{}{wxList}{\param{wxObject *}{object}, ...} | |
144 | ||
145 | Constructors. {\it key\_type} is one of wxKEY\_NONE, wxKEY\_INTEGER, or wxKEY\_STRING, | |
146 | and indicates what sort of keying is required (if any). | |
147 | ||
148 | {\it objects} is an array of {\it n} objects with which to initialize the list. | |
149 | ||
150 | The variable-length argument list constructor must be supplied with a | |
151 | terminating NULL. | |
152 | ||
153 | \membersection{wxList::\destruct{wxList}} | |
154 | ||
155 | \func{}{\destruct{wxList}}{\void} | |
156 | ||
157 | Destroys the list. Also destroys any remaining nodes, but does not destroy | |
158 | client data held in the nodes. | |
159 | ||
e12be2f7 | 160 | \membersection{wxList::Append}\label{wxlistappend} |
a660d684 KB |
161 | |
162 | \func{wxNode *}{Append}{\param{wxObject *}{object}} | |
163 | ||
164 | \func{wxNode *}{Append}{\param{long}{ key}, \param{wxObject *}{object}} | |
165 | ||
166 | \func{wxNode *}{Append}{\param{const wxString\& }{key}, \param{wxObject *}{object}} | |
167 | ||
168 | Appends a new {\bf wxNode} to the end of the list and puts a pointer to the | |
169 | \rtfsp{\it object} in the node. The last two forms store a key with the object for | |
170 | later retrieval using the key. The new node is returned in each case. | |
171 | ||
172 | The key string is copied and stored by the list implementation. | |
173 | ||
e12be2f7 | 174 | \membersection{wxList::Clear}\label{wxlistclear} |
a660d684 KB |
175 | |
176 | \func{void}{Clear}{\void} | |
177 | ||
178 | Clears the list (but does not delete the client data stored with each node). | |
179 | ||
6be663cf | 180 | \membersection{wxList::DeleteContents}\label{wxlistdeletecontents} |
a660d684 KB |
181 | |
182 | \func{void}{DeleteContents}{\param{bool}{ destroy}} | |
183 | ||
184 | If {\it destroy} is TRUE, instructs the list to call {\it delete} on the client contents of | |
185 | a node whenever the node is destroyed. The default is FALSE. | |
186 | ||
e12be2f7 | 187 | \membersection{wxList::DeleteNode}\label{wxlistdeletenode} |
a660d684 KB |
188 | |
189 | \func{bool}{DeleteNode}{\param{wxNode *}{node}} | |
190 | ||
191 | Deletes the given node from the list, returning TRUE if successful. | |
192 | ||
e12be2f7 | 193 | \membersection{wxList::DeleteObject}\label{wxlistdeleteobject} |
a660d684 KB |
194 | |
195 | \func{bool}{DeleteObject}{\param{wxObject *}{object}} | |
196 | ||
197 | Finds the given client {\it object} and deletes the appropriate node from the list, returning | |
198 | TRUE if successful. The application must delete the actual object separately. | |
199 | ||
e12be2f7 | 200 | \membersection{wxList::Find}\label{wxlistfind} |
a660d684 KB |
201 | |
202 | \func{wxNode *}{Find}{\param{long}{ key}} | |
203 | ||
204 | \func{wxNode *}{Find}{\param{const wxString\& }{key}} | |
205 | ||
206 | Returns the node whose stored key matches {\it key}. Use on a keyed list only. | |
207 | ||
d8996187 VZ |
208 | \membersection{wxList::GetCount}\label{wxlistgetcount} |
209 | ||
210 | \constfunc{size\_t}{GetCount}{\void} | |
211 | ||
212 | Returns the number of elements in the list. | |
213 | ||
e12be2f7 | 214 | \membersection{wxList::GetFirst}\label{wxlistgetfirst} |
a660d684 | 215 | |
f3a65071 | 216 | \func{wxNode *}{GetFirst}{\void} |
a660d684 KB |
217 | |
218 | Returns the first node in the list (NULL if the list is empty). | |
219 | ||
e12be2f7 | 220 | \membersection{wxList::GetLast}\label{wxlistgetlast} |
d8996187 VZ |
221 | |
222 | \func{wxNode *}{GetLast}{\void} | |
223 | ||
224 | Returns the last node in the list (NULL if the list is empty). | |
225 | ||
e12be2f7 | 226 | \membersection{wxList::IndexOf}\label{wxlistindexof} |
77c5eefb VZ |
227 | |
228 | \func{int}{IndexOf}{\param{wxObject*}{ obj }} | |
229 | ||
f3339684 | 230 | Returns the index of {\it obj} within the list or NOT\_FOUND if {\it obj} |
77c5eefb VZ |
231 | is not found in the list. |
232 | ||
e12be2f7 | 233 | \membersection{wxList::Insert}\label{wxlistinsert} |
a660d684 KB |
234 | |
235 | \func{wxNode *}{Insert}{\param{wxObject *}{object}} | |
236 | ||
237 | Insert object at front of list. | |
238 | ||
d8996187 | 239 | \func{wxNode *}{Insert}{\param{size\_t }{position}, \param{wxObject *}{object}} |
a660d684 | 240 | |
d8996187 VZ |
241 | Insert object before {\it position}, i.e. the index of the new item in the |
242 | list will be equal to {\it position}. {\it position} should be less than or | |
243 | equal to \helpref{GetCount}{wxlistgetcount}; if it is equal to it, this is the | |
244 | same as calling \helpref{Append}{wxlistappend}. | |
a660d684 | 245 | |
d8996187 | 246 | \func{wxNode *}{Insert}{\param{wxNode *}{node}, \param{wxObject *}{object}} |
a660d684 | 247 | |
d8996187 | 248 | Inserts the object before the given {\it node}. |
a660d684 | 249 | |
d8996187 | 250 | \membersection{wxList::Item}\label{wxlistitem} |
a660d684 | 251 | |
d8996187 VZ |
252 | \constfunc{wxNode *}{Item}{\param{size\_t }{index}} |
253 | ||
254 | Returns the node at given position in the list. | |
a660d684 | 255 | |
e12be2f7 | 256 | \membersection{wxList::Member}\label{wxlistmember} |
a660d684 KB |
257 | |
258 | \func{wxNode *}{Member}{\param{wxObject *}{object}} | |
259 | ||
d8996187 VZ |
260 | {\bf NB:} This function is deprecated, use \helpref{Find}{wxlistfind} instead. |
261 | ||
a660d684 KB |
262 | Returns the node associated with {\it object} if it is in the list, NULL otherwise. |
263 | ||
e12be2f7 | 264 | \membersection{wxList::Nth}\label{wxlistnth} |
a660d684 KB |
265 | |
266 | \func{wxNode *}{Nth}{\param{int}{ n}} | |
267 | ||
d8996187 VZ |
268 | {\bf NB:} This function is deprecated, use \helpref{Item}{wxlistitem} instead. |
269 | ||
a660d684 KB |
270 | Returns the {\it nth} node in the list, indexing from zero (NULL if the list is empty |
271 | or the nth node could not be found). | |
272 | ||
e12be2f7 | 273 | \membersection{wxList::Number}\label{wxlistnumber} |
a660d684 KB |
274 | |
275 | \func{int}{Number}{\void} | |
276 | ||
d8996187 VZ |
277 | {\bf NB:} This function is deprecated, use \helpref{GetCount}{wxlistgetcount} instead. |
278 | ||
a660d684 KB |
279 | Returns the number of elements in the list. |
280 | ||
e12be2f7 | 281 | \membersection{wxList::Sort}\label{wxlistsort} |
a660d684 KB |
282 | |
283 | \func{void}{Sort}{\param{wxSortCompareFunction}{ compfunc}} | |
284 | ||
285 | \begin{verbatim} | |
286 | // Type of compare function for list sort operation (as in 'qsort') | |
287 | typedef int (*wxSortCompareFunction)(const void *elem1, const void *elem2); | |
288 | \end{verbatim} | |
289 | ||
290 | Allows the sorting of arbitrary lists by giving | |
291 | a function to compare two list elements. We use the system {\bf qsort} function | |
292 | for the actual sorting process. The sort function receives pointers to wxObject pointers (wxObject **), | |
293 | so be careful to dereference appropriately. | |
294 | ||
295 | Example: | |
296 | ||
297 | \begin{verbatim} | |
298 | int listcompare(const void *arg1, const void *arg2) | |
299 | { | |
300 | return(compare(**(wxString **)arg1, // use the wxString 'compare' | |
301 | **(wxString **)arg2)); // function | |
302 | } | |
303 | ||
304 | void main() | |
305 | { | |
306 | wxList list; | |
307 | ||
308 | list.Append(new wxString("DEF")); | |
309 | list.Append(new wxString("GHI")); | |
310 | list.Append(new wxString("ABC")); | |
311 | list.Sort(listcompare); | |
312 | } | |
313 | \end{verbatim} | |
314 |