]> git.saurik.com Git - wxWidgets.git/blame - src/gtk1/listbox.cpp
fixed private member access problem
[wxWidgets.git] / src / gtk1 / listbox.cpp
CommitLineData
c801d85f
KB
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
6a6d4eed 16#include "wx/dynarray.h"
c801d85f
KB
17#include "wx/listbox.h"
18
19//-----------------------------------------------------------------------------
20// wxListBox
21//-----------------------------------------------------------------------------
22
23void 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
43IMPLEMENT_DYNAMIC_CLASS(wxListBox,wxControl)
44
45wxListBox::wxListBox(void)
46{
47 m_list = NULL;
48};
49
50wxListBox::wxListBox( wxWindow *parent, wxWindowID id,
51 const wxPoint &pos, const wxSize &size,
debe6624
JS
52 int n, const wxString choices[],
53 long style, const wxString &name )
c801d85f
KB
54{
55 Create( parent, id, pos, size, n, choices, style, name );
56};
57
58bool wxListBox::Create( wxWindow *parent, wxWindowID id,
59 const wxPoint &pos, const wxSize &size,
debe6624
JS
60 int n, const wxString choices[],
61 long style, const wxString &name )
c801d85f
KB
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() );
6a6d4eed
VZ
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 );
c801d85f
KB
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
e2414cbe
RR
95 m_clientData.Append( (wxObject*)NULL );
96
c801d85f
KB
97 gtk_widget_show( list_item );
98 };
99
100 PostCreation();
101
102 Show( TRUE );
103
104 return TRUE;
105};
106
107void 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
e2414cbe
RR
117 m_clientData.Append( (wxObject*)NULL );
118
c801d85f
KB
119 gtk_widget_show( list_item );
120};
121
e2414cbe 122void wxListBox::Append( const wxString &item, char *clientData )
c801d85f 123{
e2414cbe
RR
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 );
c801d85f
KB
135};
136
137void wxListBox::Clear(void)
138{
139 gtk_list_clear_items( m_list, 0, Number() );
e2414cbe
RR
140
141 m_clientData.Clear();
c801d85f
KB
142};
143
144void wxListBox::Delete( int n )
145{
146 gtk_list_clear_items( m_list, n, n );
e2414cbe
RR
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 );
c801d85f
KB
155};
156
157void wxListBox::Deselect( int n )
158{
159 gtk_list_unselect_item( m_list, n );
160};
161
162int 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
e2414cbe 177char *wxListBox::GetClientData( int n ) const
c801d85f 178{
e2414cbe
RR
179 wxNode *node = m_clientData.Nth( n );
180 if (node) return ((char*)node->Data());
c801d85f
KB
181 return NULL;
182};
183
184int 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
6a6d4eed 201int wxListBox::GetSelections(wxArrayInt& aSelections) const
c801d85f 202{
6a6d4eed
VZ
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;
c801d85f
KB
224};
225
226wxString 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
238wxString 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
250int 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
debe6624 258bool wxListBox::Selected( int n )
c801d85f
KB
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
debe6624 273void wxListBox::Set( int WXUNUSED(n), const wxString *WXUNUSED(choices) )
c801d85f
KB
274{
275};
276
e2414cbe 277void wxListBox::SetClientData( int n, char *clientData )
c801d85f 278{
e2414cbe
RR
279 wxNode *node = m_clientData.Nth( n );
280 if (node) node->SetData( (wxObject*)clientData );
c801d85f
KB
281};
282
283void wxListBox::SetFirstItem( int WXUNUSED(n) )
284{
285};
286
287void wxListBox::SetFirstItem( const wxString &WXUNUSED(item) )
288{
289};
290
debe6624 291void wxListBox::SetSelection( int n, bool select )
c801d85f
KB
292{
293 if (select)
294 gtk_list_select_item( m_list, n );
295 else
296 gtk_list_unselect_item( m_list, n );
297};
298
debe6624 299void wxListBox::SetString( int WXUNUSED(n), const wxString &WXUNUSED(string) )
c801d85f
KB
300{
301};
302
debe6624 303void wxListBox::SetStringSelection( const wxString &string, bool select )
c801d85f
KB
304{
305 SetSelection( FindString(string), select );
306};
307
308int 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