]> git.saurik.com Git - wxWidgets.git/blame_incremental - src/gtk1/listbox.cpp
Changes mostly as a result of __WXSTUBS__ compilation. The stubs code now
[wxWidgets.git] / src / gtk1 / listbox.cpp
... / ...
CommitLineData
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
25extern bool g_blockEventsOnDrag;
26
27//-----------------------------------------------------------------------------
28// wxListBox
29//-----------------------------------------------------------------------------
30
31static 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
62IMPLEMENT_DYNAMIC_CLASS(wxListBox,wxControl)
63
64wxListBox::wxListBox(void)
65{
66 m_list = NULL;
67}
68
69bool 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( NULL, 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
126void wxListBox::Append( const wxString &item )
127{
128 Append( item, (char*)NULL );
129}
130
131void 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
150void wxListBox::Clear(void)
151{
152 gtk_list_clear_items( m_list, 0, Number() );
153
154 m_clientData.Clear();
155}
156
157void wxListBox::Delete( int n )
158{
159 gtk_list_clear_items( m_list, n, n );
160
161 wxNode *node = m_clientData.Nth( n );
162 if (!node)
163 {
164 wxFAIL_MSG("wxListBox::Delete wrong index");
165 }
166 else
167 m_clientData.DeleteNode( node );
168}
169
170void wxListBox::Deselect( int n )
171{
172 gtk_list_unselect_item( m_list, n );
173}
174
175int wxListBox::FindString( const wxString &item ) const
176{
177 GList *child = m_list->children;
178 int count = 0;
179 while (child)
180 {
181 GtkBin *bin = GTK_BIN( child->data );
182 GtkLabel *label = GTK_LABEL( bin->child );
183 if (item == label->label) return count;
184 count++;
185 child = child->next;
186 }
187 return -1;
188}
189
190char *wxListBox::GetClientData( int n ) const
191{
192 wxNode *node = m_clientData.Nth( n );
193 if (node) return ((char*)node->Data());
194 return NULL;
195}
196
197int wxListBox::GetSelection(void) const
198{
199 GList *selection = m_list->selection;
200 if (selection)
201 {
202 GList *child = m_list->children;
203 int count = 0;
204 while (child)
205 {
206 if (child->data == selection->data) return count;
207 count++;
208 child = child->next;
209 }
210 }
211 return -1;
212}
213
214int wxListBox::GetSelections(wxArrayInt& aSelections) const
215{
216 // get the number of selected items first
217 GList *child = m_list->children;
218 int count = 0;
219 for ( child = m_list->children; child != NULL; child = child->next ) {
220 if ( GTK_WIDGET(child->data)->state == GTK_STATE_SELECTED )
221 count++;
222 }
223
224 aSelections.Empty();
225
226 if ( count > 0 ) {
227 // now fill the list
228 aSelections.Alloc(count); // optimization attempt
229 int i = 0;
230 for ( child = m_list->children; child != NULL; child = child->next, i++ ) {
231 if ( GTK_WIDGET(child->data)->state == GTK_STATE_SELECTED )
232 aSelections.Add(i);
233 }
234 }
235
236 return count;
237}
238
239wxString wxListBox::GetString( int n ) const
240{
241 GList *child = g_list_nth( m_list->children, n );
242 if (child)
243 {
244 GtkBin *bin = GTK_BIN( child->data );
245 GtkLabel *label = GTK_LABEL( bin->child );
246 return label->label;
247 }
248 return "";
249}
250
251wxString wxListBox::GetStringSelection(void) const
252{
253 GList *selection = m_list->selection;
254 if (selection)
255 {
256 GtkBin *bin = GTK_BIN( selection->data );
257 wxString tmp = GTK_LABEL( bin->child )->label;
258 return tmp;
259 }
260 return "";
261}
262
263int wxListBox::Number(void)
264{
265 GList *child = m_list->children;
266 int count = 0;
267 while (child) { count++; child = child->next; }
268 return count;
269}
270
271bool wxListBox::Selected( int n )
272{
273 GList *target = g_list_nth( m_list->children, n );
274 if (target)
275 {
276 GList *child = m_list->selection;
277 while (child)
278 {
279 if (child->data == target->data) return TRUE;
280 child = child->next;
281 }
282 }
283 return FALSE;
284}
285
286void wxListBox::Set( int WXUNUSED(n), const wxString *WXUNUSED(choices) )
287{
288}
289
290void wxListBox::SetClientData( int n, char *clientData )
291{
292 wxNode *node = m_clientData.Nth( n );
293 if (node) node->SetData( (wxObject*)clientData );
294}
295
296void wxListBox::SetFirstItem( int WXUNUSED(n) )
297{
298}
299
300void wxListBox::SetFirstItem( const wxString &WXUNUSED(item) )
301{
302}
303
304void wxListBox::SetSelection( int n, bool select )
305{
306 if (select)
307 gtk_list_select_item( m_list, n );
308 else
309 gtk_list_unselect_item( m_list, n );
310}
311
312void wxListBox::SetString( int n, const wxString &string )
313{
314 GList *child = g_list_nth( m_list->children, n );
315 if (child)
316 {
317 GtkBin *bin = GTK_BIN( child->data );
318 GtkLabel *label = GTK_LABEL( bin->child );
319 gtk_label_set( label, string );
320 }
321}
322
323void wxListBox::SetStringSelection( const wxString &string, bool select )
324{
325 SetSelection( FindString(string), select );
326}
327
328int wxListBox::GetIndex( GtkWidget *item ) const
329{
330 if (item)
331 {
332 GList *child = m_list->children;
333 int count = 0;
334 while (child)
335 {
336 if (GTK_WIDGET(child->data) == item) return count;
337 count++;
338 child = child->next;
339 }
340 }
341 return -1;
342}
343
344GtkWidget *wxListBox::GetConnectWidget(void)
345{
346 return GTK_WIDGET(m_list);
347}
348
349
350