]> git.saurik.com Git - wxWidgets.git/blob - src/gtk1/listbox.cpp
Added Set/GetItemState, GetNextItem.. to wxTreeCtrl
[wxWidgets.git] / src / gtk1 / listbox.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: listbox.cpp
3 // Purpose:
4 // Author: Robert Roebling
5 // Created: 01/02/97
6 // Id:
7 // Copyright: (c) 1998 Robert Roebling, Julian Smart and Markus Holzem
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
10
11
12 #ifdef __GNUG__
13 #pragma implementation "listbox.h"
14 #endif
15
16 #include "wx/dynarray.h"
17 #include "wx/listbox.h"
18 #include "wx/utils.h"
19 #include <wx/intl.h>
20
21 //-----------------------------------------------------------------------------
22 // data
23 //-----------------------------------------------------------------------------
24
25 extern bool g_blockEventsOnDrag;
26
27 //-----------------------------------------------------------------------------
28 // wxListBox
29 //-----------------------------------------------------------------------------
30
31 static void gtk_listitem_select_callback( GtkWidget *WXUNUSED(widget), wxListBox *listbox )
32 {
33 if (!listbox->HasVMT()) return;
34 if (g_blockEventsOnDrag) return;
35
36 wxCommandEvent event(wxEVT_COMMAND_LISTBOX_SELECTED, listbox->GetId() );
37
38 wxArrayInt aSelections;
39 int count = listbox->GetSelections(aSelections);
40 if ( count > 0 )
41 {
42 event.m_commandInt = aSelections[0] ;
43 event.m_clientData = listbox->GetClientData(event.m_commandInt);
44 wxString str(listbox->GetString(event.m_commandInt));
45 if (str != "")
46 event.m_commandString = copystring((char *)(const char *)str);
47 }
48 else
49 {
50 event.m_commandInt = -1 ;
51 event.m_commandString = copystring("") ;
52 }
53
54 event.SetEventObject( listbox );
55
56 listbox->GetEventHandler()->ProcessEvent( event );
57 if (event.m_commandString) delete[] event.m_commandString ;
58 }
59
60 //-----------------------------------------------------------------------------
61
62 IMPLEMENT_DYNAMIC_CLASS(wxListBox,wxControl)
63
64 wxListBox::wxListBox(void)
65 {
66 m_list = (GtkList *) NULL;
67 }
68
69 bool wxListBox::Create( wxWindow *parent, wxWindowID id,
70 const wxPoint &pos, const wxSize &size,
71 int n, const wxString choices[],
72 long style, const wxValidator& validator, const wxString &name )
73 {
74 m_needParent = TRUE;
75
76 PreCreation( parent, id, pos, size, style, name );
77
78 SetValidator( validator );
79
80 m_widget = gtk_scrolled_window_new( (GtkAdjustment *) NULL, (GtkAdjustment *) NULL );
81 gtk_scrolled_window_set_policy( GTK_SCROLLED_WINDOW(m_widget),
82 GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC );
83
84 m_list = GTK_LIST( gtk_list_new() );
85
86 // @@ what's the difference between BROWSE and SINGLE?
87 GtkSelectionMode mode = GTK_SELECTION_BROWSE;
88 if ( style & wxLB_MULTIPLE )
89 mode = GTK_SELECTION_MULTIPLE;
90 else if ( style & wxLB_EXTENDED )
91 mode = GTK_SELECTION_EXTENDED;
92
93 gtk_list_set_selection_mode( GTK_LIST(m_list), mode );
94
95 gtk_container_add (GTK_CONTAINER(m_widget), GTK_WIDGET(m_list) );
96 gtk_widget_show( GTK_WIDGET(m_list) );
97
98 for (int i = 0; i < n; i++)
99 {
100 GtkWidget *list_item;
101 list_item = gtk_list_item_new_with_label( choices[i] );
102
103 gtk_container_add( GTK_CONTAINER(m_list), list_item );
104
105 gtk_signal_connect( GTK_OBJECT(list_item), "select",
106 GTK_SIGNAL_FUNC(gtk_listitem_select_callback), (gpointer)this );
107
108 if ( style & wxLB_MULTIPLE )
109 gtk_signal_connect( GTK_OBJECT(list_item), "deselect",
110 GTK_SIGNAL_FUNC(gtk_listitem_select_callback), (gpointer)this );
111
112 m_clientData.Append( (wxObject*)NULL );
113
114 gtk_widget_show( list_item );
115 }
116
117 PostCreation();
118
119 gtk_widget_realize( GTK_WIDGET(m_list) );
120
121 Show( TRUE );
122
123 return TRUE;
124 }
125
126 void wxListBox::Append( const wxString &item )
127 {
128 Append( item, (char*)NULL );
129 }
130
131 void wxListBox::Append( const wxString &item, char *clientData )
132 {
133 GtkWidget *list_item;
134 list_item = gtk_list_item_new_with_label( item );
135
136 gtk_signal_connect( GTK_OBJECT(list_item), "select",
137 GTK_SIGNAL_FUNC(gtk_listitem_select_callback), (gpointer)this );
138
139 if ( GetWindowStyleFlag() & wxLB_MULTIPLE )
140 gtk_signal_connect( GTK_OBJECT(list_item), "deselect",
141 GTK_SIGNAL_FUNC(gtk_listitem_select_callback), (gpointer)this );
142
143 gtk_container_add( GTK_CONTAINER(m_list), list_item );
144
145 m_clientData.Append( (wxObject*)clientData );
146
147 gtk_widget_show( list_item );
148 }
149
150 void wxListBox::Clear(void)
151 {
152 gtk_list_clear_items( m_list, 0, Number() );
153
154 m_clientData.Clear();
155 }
156
157 void wxListBox::Delete( int n )
158 {
159 GList *child = g_list_nth( m_list->children, n );
160
161 if (!child)
162 {
163 wxFAIL_MSG("wrong listbox index");
164 return;
165 }
166
167 GList *list = g_list_append( NULL, child->data );
168 gtk_list_remove_items( m_list, list );
169 g_list_free( list );
170
171 wxNode *node = m_clientData.Nth( n );
172 if (!node)
173 {
174 wxFAIL_MSG("wrong listbox index");
175 }
176 else
177 m_clientData.DeleteNode( node );
178 }
179
180 void wxListBox::Deselect( int n )
181 {
182 gtk_list_unselect_item( m_list, n );
183 }
184
185 int wxListBox::FindString( const wxString &item ) const
186 {
187 GList *child = m_list->children;
188 int count = 0;
189 while (child)
190 {
191 GtkBin *bin = GTK_BIN( child->data );
192 GtkLabel *label = GTK_LABEL( bin->child );
193 if (item == label->label) return count;
194 count++;
195 child = child->next;
196 }
197 wxFAIL_MSG("wrong listbox index");
198 return -1;
199 }
200
201 char *wxListBox::GetClientData( int n ) const
202 {
203 wxNode *node = m_clientData.Nth( n );
204 if (node) return ((char*)node->Data());
205
206 wxFAIL_MSG("wrong listbox index");
207 return (char *) NULL;
208 }
209
210 int wxListBox::GetSelection(void) const
211 {
212 GList *selection = m_list->selection;
213 if (selection)
214 {
215 GList *child = m_list->children;
216 int count = 0;
217 while (child)
218 {
219 if (child->data == selection->data) return count;
220 count++;
221 child = child->next;
222 }
223 }
224 wxFAIL_MSG("wrong listbox index");
225 return -1;
226 }
227
228 int wxListBox::GetSelections(wxArrayInt& aSelections) const
229 {
230 // get the number of selected items first
231 GList *child = m_list->children;
232 int count = 0;
233 for ( child = m_list->children; child != NULL; child = child->next ) {
234 if ( GTK_WIDGET(child->data)->state == GTK_STATE_SELECTED )
235 count++;
236 }
237
238 aSelections.Empty();
239
240 if ( count > 0 ) {
241 // now fill the list
242 aSelections.Alloc(count); // optimization attempt
243 int i = 0;
244 for ( child = m_list->children; child != NULL; child = child->next, i++ ) {
245 if ( GTK_WIDGET(child->data)->state == GTK_STATE_SELECTED )
246 aSelections.Add(i);
247 }
248 }
249
250 return count;
251 }
252
253 wxString wxListBox::GetString( int n ) const
254 {
255 GList *child = g_list_nth( m_list->children, n );
256 if (child)
257 {
258 GtkBin *bin = GTK_BIN( child->data );
259 GtkLabel *label = GTK_LABEL( bin->child );
260 return label->label;
261 }
262 wxFAIL_MSG("wrong listbox index");
263 return "";
264 }
265
266 wxString wxListBox::GetStringSelection(void) const
267 {
268 GList *selection = m_list->selection;
269 if (selection)
270 {
271 GtkBin *bin = GTK_BIN( selection->data );
272 wxString tmp = GTK_LABEL( bin->child )->label;
273 return tmp;
274 }
275 wxFAIL_MSG("no listbox selection available");
276 return "";
277 }
278
279 int wxListBox::Number(void)
280 {
281 GList *child = m_list->children;
282 int count = 0;
283 while (child) { count++; child = child->next; }
284 return count;
285 }
286
287 bool wxListBox::Selected( int n )
288 {
289 GList *target = g_list_nth( m_list->children, n );
290 if (target)
291 {
292 GList *child = m_list->selection;
293 while (child)
294 {
295 if (child->data == target->data) return TRUE;
296 child = child->next;
297 }
298 }
299 wxFAIL_MSG("wrong listbox index");
300 return FALSE;
301 }
302
303 void wxListBox::Set( int WXUNUSED(n), const wxString *WXUNUSED(choices) )
304 {
305 wxFAIL_MSG("wxListBox::Set not implemented");
306 }
307
308 void wxListBox::SetClientData( int n, char *clientData )
309 {
310 wxNode *node = m_clientData.Nth( n );
311 if (node)
312 {
313 node->SetData( (wxObject*)clientData );
314 }
315 else
316 {
317 wxFAIL_MSG("wrong listbox index");
318 }
319 }
320
321 void wxListBox::SetFirstItem( int WXUNUSED(n) )
322 {
323 wxFAIL_MSG("wxListBox::SetFirstItem not implemented");
324 }
325
326 void wxListBox::SetFirstItem( const wxString &WXUNUSED(item) )
327 {
328 wxFAIL_MSG("wxListBox::SetFirstItem not implemented");
329 }
330
331 void wxListBox::SetSelection( int n, bool select )
332 {
333 if (select)
334 gtk_list_select_item( m_list, n );
335 else
336 gtk_list_unselect_item( m_list, n );
337 }
338
339 void wxListBox::SetString( int n, const wxString &string )
340 {
341 GList *child = g_list_nth( m_list->children, n );
342 if (child)
343 {
344 GtkBin *bin = GTK_BIN( child->data );
345 GtkLabel *label = GTK_LABEL( bin->child );
346 gtk_label_set( label, string );
347 }
348 else
349 {
350 wxFAIL_MSG("wrong listbox index");
351 }
352 }
353
354 void wxListBox::SetStringSelection( const wxString &string, bool select )
355 {
356 SetSelection( FindString(string), select );
357 }
358
359 int wxListBox::GetIndex( GtkWidget *item ) const
360 {
361 if (item)
362 {
363 GList *child = m_list->children;
364 int count = 0;
365 while (child)
366 {
367 if (GTK_WIDGET(child->data) == item) return count;
368 count++;
369 child = child->next;
370 }
371 }
372 return -1;
373 }
374
375 GtkWidget *wxListBox::GetConnectWidget(void)
376 {
377 return GTK_WIDGET(m_list);
378 }
379
380
381