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