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