1 /////////////////////////////////////////////////////////////////////////////
4 // Author: Robert Roebling
7 // Copyright: (c) 1998 Robert Roebling, Julian Smart and Markus Holzem
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
13 #pragma implementation "listbox.h"
16 #include "wx/dynarray.h"
17 #include "wx/listbox.h"
19 //-----------------------------------------------------------------------------
21 //-----------------------------------------------------------------------------
23 void gtk_listitem_select_callback( GtkWidget
*widget
, gpointer data
)
25 wxListBox
*listbox
= (wxListBox
*)data
;
27 wxCommandEvent
event(wxEVT_COMMAND_LISTBOX_SELECTED
, listbox
->GetId() );
29 event
.SetInt( listbox
->GetIndex( widget
) );
31 GtkBin
*bin
= GTK_BIN( widget
);
32 GtkLabel
*label
= GTK_LABEL( bin
->child
);
33 wxString
tmp( label
->label
);
34 event
.SetString( WXSTRINGCAST(tmp
) );
36 event
.SetEventObject( listbox
);
38 listbox
->ProcessEvent( event
);
41 //-----------------------------------------------------------------------------
43 IMPLEMENT_DYNAMIC_CLASS(wxListBox
,wxControl
)
45 wxListBox::wxListBox(void)
50 wxListBox::wxListBox( wxWindow
*parent
, wxWindowID id
,
51 const wxPoint
&pos
, const wxSize
&size
,
52 int n
, const wxString choices
[],
53 long style
, const wxString
&name
)
55 Create( parent
, id
, pos
, size
, n
, choices
, style
, name
);
58 bool wxListBox::Create( wxWindow
*parent
, wxWindowID id
,
59 const wxPoint
&pos
, const wxSize
&size
,
60 int n
, const wxString choices
[],
61 long style
, const wxString
&name
)
65 PreCreation( parent
, id
, pos
, size
, style
, name
);
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
);
71 m_list
= GTK_LIST( gtk_list_new() );
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
;
80 gtk_list_set_selection_mode( GTK_LIST(m_list
), mode
);
82 gtk_container_add (GTK_CONTAINER(m_widget
), GTK_WIDGET(m_list
) );
83 gtk_widget_show( GTK_WIDGET(m_list
) );
85 for (int i
= 0; i
< n
; i
++)
88 list_item
= gtk_list_item_new_with_label( choices
[i
] );
90 gtk_signal_connect( GTK_OBJECT(list_item
), "select",
91 GTK_SIGNAL_FUNC(gtk_listitem_select_callback
), (gpointer
)this );
93 gtk_container_add( GTK_CONTAINER(m_list
), list_item
);
95 gtk_widget_show( list_item
);
105 void wxListBox::Append( const wxString
&item
)
107 GtkWidget
*list_item
;
108 list_item
= gtk_list_item_new_with_label( item
);
110 gtk_signal_connect( GTK_OBJECT(list_item
), "select",
111 GTK_SIGNAL_FUNC(gtk_listitem_select_callback
), (gpointer
)this );
113 gtk_container_add( GTK_CONTAINER(m_list
), list_item
);
115 gtk_widget_show( list_item
);
118 void wxListBox::Append( const wxString
&WXUNUSED(item
), char *WXUNUSED(clientData
) )
120 wxFAIL_MSG("wxListBox::Append(clientdata) not implemented");
123 void wxListBox::Clear(void)
125 gtk_list_clear_items( m_list
, 0, Number() );
128 void wxListBox::Delete( int n
)
130 gtk_list_clear_items( m_list
, n
, n
);
133 void wxListBox::Deselect( int n
)
135 gtk_list_unselect_item( m_list
, n
);
138 int wxListBox::FindString( const wxString
&item
) const
140 GList
*child
= m_list
->children
;
144 GtkBin
*bin
= GTK_BIN( child
->data
);
145 GtkLabel
*label
= GTK_LABEL( bin
->child
);
146 if (item
== label
->label
) return count
;
153 char *wxListBox::GetClientData( int WXUNUSED(n
) ) const
155 wxFAIL_MSG("wxListBox::GetClientData not implemented");
160 int wxListBox::GetSelection(void) const
162 GList
*selection
= m_list
->selection
;
165 GList
*child
= m_list
->children
;
169 if (child
->data
== selection
->data
) return count
;
177 int wxListBox::GetSelections(wxArrayInt
& aSelections
) const
179 // get the number of selected items first
180 GList
*child
= m_list
->children
;
182 for ( child
= m_list
->children
; child
!= NULL
; child
= child
->next
) {
183 if ( GTK_WIDGET(child
->data
)->state
== GTK_STATE_SELECTED
)
191 aSelections
.Alloc(count
); // optimization attempt
193 for ( child
= m_list
->children
; child
!= NULL
; child
= child
->next
, i
++ ) {
194 if ( GTK_WIDGET(child
->data
)->state
== GTK_STATE_SELECTED
)
202 wxString
wxListBox::GetString( int n
) const
204 GList
*child
= g_list_nth( m_list
->children
, n
);
207 GtkBin
*bin
= GTK_BIN( child
->data
);
208 GtkLabel
*label
= GTK_LABEL( bin
->child
);
214 wxString
wxListBox::GetStringSelection(void) const
216 GList
*selection
= m_list
->selection
;
219 GtkBin
*bin
= GTK_BIN( selection
->data
);
220 wxString tmp
= GTK_LABEL( bin
->child
)->label
;
226 int wxListBox::Number(void)
228 GList
*child
= m_list
->children
;
230 while (child
) { count
++; child
= child
->next
; };
234 bool wxListBox::Selected( int n
)
236 GList
*target
= g_list_nth( m_list
->children
, n
);
239 GList
*child
= m_list
->selection
;
242 if (child
->data
== target
->data
) return TRUE
;
249 void wxListBox::Set( int WXUNUSED(n
), const wxString
*WXUNUSED(choices
) )
253 void wxListBox::SetClientData( int WXUNUSED(n
), char *WXUNUSED(clientData
) )
257 void wxListBox::SetFirstItem( int WXUNUSED(n
) )
261 void wxListBox::SetFirstItem( const wxString
&WXUNUSED(item
) )
265 void wxListBox::SetSelection( int n
, bool select
)
268 gtk_list_select_item( m_list
, n
);
270 gtk_list_unselect_item( m_list
, n
);
273 void wxListBox::SetString( int WXUNUSED(n
), const wxString
&WXUNUSED(string
) )
277 void wxListBox::SetStringSelection( const wxString
&string
, bool select
)
279 SetSelection( FindString(string
), select
);
282 int wxListBox::GetIndex( GtkWidget
*item
) const
286 GList
*child
= m_list
->children
;
290 if (GTK_WIDGET(child
->data
) == item
) return count
;