]> git.saurik.com Git - wxWidgets.git/blob - src/gtk/listbox.cpp
Removed warnings mesgs
[wxWidgets.git] / src / gtk / 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
19 //-----------------------------------------------------------------------------
20 // wxListBox
21 //-----------------------------------------------------------------------------
22
23 void gtk_listitem_select_callback( GtkWidget *widget, gpointer data )
24 {
25 wxListBox *listbox = (wxListBox*)data;
26
27 wxCommandEvent event(wxEVT_COMMAND_LISTBOX_SELECTED, listbox->GetId() );
28
29 event.SetInt( listbox->GetIndex( widget ) );
30
31 GtkBin *bin = GTK_BIN( widget );
32 GtkLabel *label = GTK_LABEL( bin->child );
33 wxString tmp( label->label );
34 event.SetString( WXSTRINGCAST(tmp) );
35
36 event.SetEventObject( listbox );
37
38 listbox->ProcessEvent( event );
39 };
40
41 //-----------------------------------------------------------------------------
42
43 IMPLEMENT_DYNAMIC_CLASS(wxListBox,wxControl)
44
45 wxListBox::wxListBox(void)
46 {
47 m_list = NULL;
48 };
49
50 wxListBox::wxListBox( wxWindow *parent, wxWindowID id,
51 const wxPoint &pos, const wxSize &size,
52 int n, const wxString choices[],
53 long style, const wxString &name )
54 {
55 Create( parent, id, pos, size, n, choices, style, name );
56 };
57
58 bool wxListBox::Create( wxWindow *parent, wxWindowID id,
59 const wxPoint &pos, const wxSize &size,
60 int n, const wxString choices[],
61 long style, const wxString &name )
62 {
63 m_needParent = TRUE;
64
65 PreCreation( parent, id, pos, size, style, name );
66
67 m_widget = gtk_scrolled_window_new( NULL, NULL );
68 gtk_scrolled_window_set_policy( GTK_SCROLLED_WINDOW(m_widget),
69 GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC );
70
71 m_list = GTK_LIST( gtk_list_new() );
72
73 // @@ what's the difference between BROWSE and SINGLE?
74 GtkSelectionMode mode = GTK_SELECTION_BROWSE;
75 if ( style & wxLB_MULTIPLE )
76 mode = GTK_SELECTION_MULTIPLE;
77 else if ( style & wxLB_EXTENDED )
78 mode = GTK_SELECTION_EXTENDED;
79
80 gtk_list_set_selection_mode( GTK_LIST(m_list), mode );
81
82 gtk_container_add (GTK_CONTAINER(m_widget), GTK_WIDGET(m_list) );
83 gtk_widget_show( GTK_WIDGET(m_list) );
84
85 for (int i = 0; i < n; i++)
86 {
87 GtkWidget *list_item;
88 list_item = gtk_list_item_new_with_label( choices[i] );
89
90 gtk_signal_connect( GTK_OBJECT(list_item), "select",
91 GTK_SIGNAL_FUNC(gtk_listitem_select_callback), (gpointer)this );
92
93 gtk_container_add( GTK_CONTAINER(m_list), list_item );
94
95 m_clientData.Append( (wxObject*)NULL );
96
97 gtk_widget_show( list_item );
98 };
99
100 PostCreation();
101
102 Show( TRUE );
103
104 return TRUE;
105 };
106
107 void wxListBox::Append( const wxString &item )
108 {
109 GtkWidget *list_item;
110 list_item = gtk_list_item_new_with_label( item );
111
112 gtk_signal_connect( GTK_OBJECT(list_item), "select",
113 GTK_SIGNAL_FUNC(gtk_listitem_select_callback), (gpointer)this );
114
115 gtk_container_add( GTK_CONTAINER(m_list), list_item );
116
117 m_clientData.Append( (wxObject*)NULL );
118
119 gtk_widget_show( list_item );
120 };
121
122 void wxListBox::Append( const wxString &item, char *clientData )
123 {
124 GtkWidget *list_item;
125 list_item = gtk_list_item_new_with_label( item );
126
127 gtk_signal_connect( GTK_OBJECT(list_item), "select",
128 GTK_SIGNAL_FUNC(gtk_listitem_select_callback), (gpointer)this );
129
130 gtk_container_add( GTK_CONTAINER(m_list), list_item );
131
132 m_clientData.Append( (wxObject*)clientData );
133
134 gtk_widget_show( list_item );
135 };
136
137 void wxListBox::Clear(void)
138 {
139 gtk_list_clear_items( m_list, 0, Number() );
140
141 m_clientData.Clear();
142 };
143
144 void wxListBox::Delete( int n )
145 {
146 gtk_list_clear_items( m_list, n, n );
147
148 wxNode *node = m_clientData.Nth( n );
149 if (!node)
150 {
151 wxFAIL_MSG("wxListBox::Delete wrong index");
152 }
153 else
154 m_clientData.DeleteNode( node );
155 };
156
157 void wxListBox::Deselect( int n )
158 {
159 gtk_list_unselect_item( m_list, n );
160 };
161
162 int wxListBox::FindString( const wxString &item ) const
163 {
164 GList *child = m_list->children;
165 int count = 0;
166 while (child)
167 {
168 GtkBin *bin = GTK_BIN( child->data );
169 GtkLabel *label = GTK_LABEL( bin->child );
170 if (item == label->label) return count;
171 count++;
172 child = child->next;
173 };
174 return -1;
175 };
176
177 char *wxListBox::GetClientData( int n ) const
178 {
179 wxNode *node = m_clientData.Nth( n );
180 if (node) return ((char*)node->Data());
181 return NULL;
182 };
183
184 int wxListBox::GetSelection(void) const
185 {
186 GList *selection = m_list->selection;
187 if (selection)
188 {
189 GList *child = m_list->children;
190 int count = 0;
191 while (child)
192 {
193 if (child->data == selection->data) return count;
194 count++;
195 child = child->next;
196 };
197 };
198 return -1;
199 };
200
201 int wxListBox::GetSelections(wxArrayInt& aSelections) const
202 {
203 // get the number of selected items first
204 GList *child = m_list->children;
205 int count = 0;
206 for ( child = m_list->children; child != NULL; child = child->next ) {
207 if ( GTK_WIDGET(child->data)->state == GTK_STATE_SELECTED )
208 count++;
209 }
210
211 aSelections.Empty();
212
213 if ( count > 0 ) {
214 // now fill the list
215 aSelections.Alloc(count); // optimization attempt
216 int i = 0;
217 for ( child = m_list->children; child != NULL; child = child->next, i++ ) {
218 if ( GTK_WIDGET(child->data)->state == GTK_STATE_SELECTED )
219 aSelections.Add(i);
220 }
221 }
222
223 return count;
224 };
225
226 wxString wxListBox::GetString( int n ) const
227 {
228 GList *child = g_list_nth( m_list->children, n );
229 if (child)
230 {
231 GtkBin *bin = GTK_BIN( child->data );
232 GtkLabel *label = GTK_LABEL( bin->child );
233 return label->label;
234 };
235 return "";
236 };
237
238 wxString wxListBox::GetStringSelection(void) const
239 {
240 GList *selection = m_list->selection;
241 if (selection)
242 {
243 GtkBin *bin = GTK_BIN( selection->data );
244 wxString tmp = GTK_LABEL( bin->child )->label;
245 return tmp;
246 };
247 return "";
248 };
249
250 int wxListBox::Number(void)
251 {
252 GList *child = m_list->children;
253 int count = 0;
254 while (child) { count++; child = child->next; };
255 return count;
256 };
257
258 bool wxListBox::Selected( int n )
259 {
260 GList *target = g_list_nth( m_list->children, n );
261 if (target)
262 {
263 GList *child = m_list->selection;
264 while (child)
265 {
266 if (child->data == target->data) return TRUE;
267 child = child->next;
268 };
269 };
270 return FALSE;
271 };
272
273 void wxListBox::Set( int WXUNUSED(n), const wxString *WXUNUSED(choices) )
274 {
275 };
276
277 void wxListBox::SetClientData( int n, char *clientData )
278 {
279 wxNode *node = m_clientData.Nth( n );
280 if (node) node->SetData( (wxObject*)clientData );
281 };
282
283 void wxListBox::SetFirstItem( int WXUNUSED(n) )
284 {
285 };
286
287 void wxListBox::SetFirstItem( const wxString &WXUNUSED(item) )
288 {
289 };
290
291 void wxListBox::SetSelection( int n, bool select )
292 {
293 if (select)
294 gtk_list_select_item( m_list, n );
295 else
296 gtk_list_unselect_item( m_list, n );
297 };
298
299 void wxListBox::SetString( int WXUNUSED(n), const wxString &WXUNUSED(string) )
300 {
301 };
302
303 void wxListBox::SetStringSelection( const wxString &string, bool select )
304 {
305 SetSelection( FindString(string), select );
306 };
307
308 int wxListBox::GetIndex( GtkWidget *item ) const
309 {
310 if (item)
311 {
312 GList *child = m_list->children;
313 int count = 0;
314 while (child)
315 {
316 if (GTK_WIDGET(child->data) == item) return count;
317 count++;
318 child = child->next;
319 };
320 };
321 return -1;
322 };
323
324