]> git.saurik.com Git - wxWidgets.git/blame - src/gtk1/listbox.cpp
added virtual bool wxWindow::AcceptsFocus()
[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
95 gtk_widget_show( list_item );
96 };
97
98 PostCreation();
99
100 Show( TRUE );
101
102 return TRUE;
103};
104
105void wxListBox::Append( const wxString &item )
106{
107 GtkWidget *list_item;
108 list_item = gtk_list_item_new_with_label( item );
109
110 gtk_signal_connect( GTK_OBJECT(list_item), "select",
111 GTK_SIGNAL_FUNC(gtk_listitem_select_callback), (gpointer)this );
112
113 gtk_container_add( GTK_CONTAINER(m_list), list_item );
114
115 gtk_widget_show( list_item );
116};
117
118void wxListBox::Append( const wxString &WXUNUSED(item), char *WXUNUSED(clientData) )
119{
6a6d4eed 120 wxFAIL_MSG("wxListBox::Append(clientdata) not implemented");
c801d85f
KB
121};
122
123void wxListBox::Clear(void)
124{
125 gtk_list_clear_items( m_list, 0, Number() );
126};
127
128void wxListBox::Delete( int n )
129{
130 gtk_list_clear_items( m_list, n, n );
131};
132
133void wxListBox::Deselect( int n )
134{
135 gtk_list_unselect_item( m_list, n );
136};
137
138int wxListBox::FindString( const wxString &item ) const
139{
140 GList *child = m_list->children;
141 int count = 0;
142 while (child)
143 {
144 GtkBin *bin = GTK_BIN( child->data );
145 GtkLabel *label = GTK_LABEL( bin->child );
146 if (item == label->label) return count;
147 count++;
148 child = child->next;
149 };
150 return -1;
151};
152
debe6624 153char *wxListBox::GetClientData( int WXUNUSED(n) ) const
c801d85f 154{
6a6d4eed
VZ
155 wxFAIL_MSG("wxListBox::GetClientData not implemented");
156
c801d85f
KB
157 return NULL;
158};
159
160int wxListBox::GetSelection(void) const
161{
162 GList *selection = m_list->selection;
163 if (selection)
164 {
165 GList *child = m_list->children;
166 int count = 0;
167 while (child)
168 {
169 if (child->data == selection->data) return count;
170 count++;
171 child = child->next;
172 };
173 };
174 return -1;
175};
176
6a6d4eed 177int wxListBox::GetSelections(wxArrayInt& aSelections) const
c801d85f 178{
6a6d4eed
VZ
179 // get the number of selected items first
180 GList *child = m_list->children;
181 int count = 0;
182 for ( child = m_list->children; child != NULL; child = child->next ) {
183 if ( GTK_WIDGET(child->data)->state == GTK_STATE_SELECTED )
184 count++;
185 }
186
187 aSelections.Empty();
188
189 if ( count > 0 ) {
190 // now fill the list
191 aSelections.Alloc(count); // optimization attempt
192 int i = 0;
193 for ( child = m_list->children; child != NULL; child = child->next, i++ ) {
194 if ( GTK_WIDGET(child->data)->state == GTK_STATE_SELECTED )
195 aSelections.Add(i);
196 }
197 }
198
199 return count;
c801d85f
KB
200};
201
202wxString wxListBox::GetString( int n ) const
203{
204 GList *child = g_list_nth( m_list->children, n );
205 if (child)
206 {
207 GtkBin *bin = GTK_BIN( child->data );
208 GtkLabel *label = GTK_LABEL( bin->child );
209 return label->label;
210 };
211 return "";
212};
213
214wxString wxListBox::GetStringSelection(void) const
215{
216 GList *selection = m_list->selection;
217 if (selection)
218 {
219 GtkBin *bin = GTK_BIN( selection->data );
220 wxString tmp = GTK_LABEL( bin->child )->label;
221 return tmp;
222 };
223 return "";
224};
225
226int wxListBox::Number(void)
227{
228 GList *child = m_list->children;
229 int count = 0;
230 while (child) { count++; child = child->next; };
231 return count;
232};
233
debe6624 234bool wxListBox::Selected( int n )
c801d85f
KB
235{
236 GList *target = g_list_nth( m_list->children, n );
237 if (target)
238 {
239 GList *child = m_list->selection;
240 while (child)
241 {
242 if (child->data == target->data) return TRUE;
243 child = child->next;
244 };
245 };
246 return FALSE;
247};
248
debe6624 249void wxListBox::Set( int WXUNUSED(n), const wxString *WXUNUSED(choices) )
c801d85f
KB
250{
251};
252
debe6624 253void wxListBox::SetClientData( int WXUNUSED(n), char *WXUNUSED(clientData) )
c801d85f
KB
254{
255};
256
257void wxListBox::SetFirstItem( int WXUNUSED(n) )
258{
259};
260
261void wxListBox::SetFirstItem( const wxString &WXUNUSED(item) )
262{
263};
264
debe6624 265void wxListBox::SetSelection( int n, bool select )
c801d85f
KB
266{
267 if (select)
268 gtk_list_select_item( m_list, n );
269 else
270 gtk_list_unselect_item( m_list, n );
271};
272
debe6624 273void wxListBox::SetString( int WXUNUSED(n), const wxString &WXUNUSED(string) )
c801d85f
KB
274{
275};
276
debe6624 277void wxListBox::SetStringSelection( const wxString &string, bool select )
c801d85f
KB
278{
279 SetSelection( FindString(string), select );
280};
281
282int wxListBox::GetIndex( GtkWidget *item ) const
283{
284 if (item)
285 {
286 GList *child = m_list->children;
287 int count = 0;
288 while (child)
289 {
290 if (GTK_WIDGET(child->data) == item) return count;
291 count++;
292 child = child->next;
293 };
294 };
295 return -1;
296};
297
298