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/listbox.h"
18 //-----------------------------------------------------------------------------
20 //-----------------------------------------------------------------------------
22 void gtk_listitem_select_callback( GtkWidget
*widget
, gpointer data
)
24 wxListBox
*listbox
= (wxListBox
*)data
;
26 wxCommandEvent
event(wxEVT_COMMAND_LISTBOX_SELECTED
, listbox
->GetId() );
28 event
.SetInt( listbox
->GetIndex( widget
) );
30 GtkBin
*bin
= GTK_BIN( widget
);
31 GtkLabel
*label
= GTK_LABEL( bin
->child
);
32 wxString
tmp( label
->label
);
33 event
.SetString( WXSTRINGCAST(tmp
) );
35 event
.SetEventObject( listbox
);
37 listbox
->ProcessEvent( event
);
40 //-----------------------------------------------------------------------------
42 IMPLEMENT_DYNAMIC_CLASS(wxListBox
,wxControl
)
44 wxListBox::wxListBox(void)
49 wxListBox::wxListBox( wxWindow
*parent
, wxWindowID id
,
50 const wxPoint
&pos
, const wxSize
&size
,
51 const int n
, const wxString choices
[],
52 const long style
, const wxString
&name
)
54 Create( parent
, id
, pos
, size
, n
, choices
, style
, name
);
57 bool wxListBox::Create( wxWindow
*parent
, wxWindowID id
,
58 const wxPoint
&pos
, const wxSize
&size
,
59 const int n
, const wxString choices
[],
60 const long style
, const wxString
&name
)
64 PreCreation( parent
, id
, pos
, size
, style
, name
);
66 m_widget
= gtk_scrolled_window_new( NULL
, NULL
);
67 gtk_scrolled_window_set_policy( GTK_SCROLLED_WINDOW(m_widget
),
68 GTK_POLICY_AUTOMATIC
, GTK_POLICY_AUTOMATIC
);
70 m_list
= GTK_LIST( gtk_list_new() );
71 gtk_list_set_selection_mode( GTK_LIST(m_list
), GTK_SELECTION_BROWSE
);
73 gtk_container_add (GTK_CONTAINER(m_widget
), GTK_WIDGET(m_list
) );
74 gtk_widget_show( GTK_WIDGET(m_list
) );
76 for (int i
= 0; i
< n
; i
++)
79 list_item
= gtk_list_item_new_with_label( choices
[i
] );
81 gtk_signal_connect( GTK_OBJECT(list_item
), "select",
82 GTK_SIGNAL_FUNC(gtk_listitem_select_callback
), (gpointer
)this );
84 gtk_container_add( GTK_CONTAINER(m_list
), list_item
);
86 gtk_widget_show( list_item
);
96 void wxListBox::Append( const wxString
&item
)
99 list_item
= gtk_list_item_new_with_label( item
);
101 gtk_signal_connect( GTK_OBJECT(list_item
), "select",
102 GTK_SIGNAL_FUNC(gtk_listitem_select_callback
), (gpointer
)this );
104 gtk_container_add( GTK_CONTAINER(m_list
), list_item
);
106 gtk_widget_show( list_item
);
109 void wxListBox::Append( const wxString
&WXUNUSED(item
), char *WXUNUSED(clientData
) )
113 void wxListBox::Clear(void)
115 gtk_list_clear_items( m_list
, 0, Number() );
118 void wxListBox::Delete( int n
)
120 gtk_list_clear_items( m_list
, n
, n
);
123 void wxListBox::Deselect( int n
)
125 gtk_list_unselect_item( m_list
, n
);
128 int wxListBox::FindString( const wxString
&item
) const
130 GList
*child
= m_list
->children
;
134 GtkBin
*bin
= GTK_BIN( child
->data
);
135 GtkLabel
*label
= GTK_LABEL( bin
->child
);
136 if (item
== label
->label
) return count
;
143 char *wxListBox::GetClientData( const int WXUNUSED(n
) ) const
148 int wxListBox::GetSelection(void) const
150 GList
*selection
= m_list
->selection
;
153 GList
*child
= m_list
->children
;
157 if (child
->data
== selection
->data
) return count
;
165 int wxListBox::GetSelections( int **WXUNUSED(selections
) ) const
170 wxString
wxListBox::GetString( int n
) const
172 GList
*child
= g_list_nth( m_list
->children
, n
);
175 GtkBin
*bin
= GTK_BIN( child
->data
);
176 GtkLabel
*label
= GTK_LABEL( bin
->child
);
182 wxString
wxListBox::GetStringSelection(void) const
184 GList
*selection
= m_list
->selection
;
187 GtkBin
*bin
= GTK_BIN( selection
->data
);
188 wxString tmp
= GTK_LABEL( bin
->child
)->label
;
194 int wxListBox::Number(void)
196 GList
*child
= m_list
->children
;
198 while (child
) { count
++; child
= child
->next
; };
202 bool wxListBox::Selected( const int n
)
204 GList
*target
= g_list_nth( m_list
->children
, n
);
207 GList
*child
= m_list
->selection
;
210 if (child
->data
== target
->data
) return TRUE
;
217 void wxListBox::Set( const int WXUNUSED(n
), const wxString
*WXUNUSED(choices
) )
221 void wxListBox::SetClientData( const int WXUNUSED(n
), char *WXUNUSED(clientData
) )
225 void wxListBox::SetFirstItem( int WXUNUSED(n
) )
229 void wxListBox::SetFirstItem( const wxString
&WXUNUSED(item
) )
233 void wxListBox::SetSelection( const int n
, const bool select
)
236 gtk_list_select_item( m_list
, n
);
238 gtk_list_unselect_item( m_list
, n
);
241 void wxListBox::SetString( const int WXUNUSED(n
), const wxString
&WXUNUSED(string
) )
245 void wxListBox::SetStringSelection( const wxString
&string
, const bool select
)
247 SetSelection( FindString(string
), select
);
250 int wxListBox::GetIndex( GtkWidget
*item
) const
254 GList
*child
= m_list
->children
;
258 if (GTK_WIDGET(child
->data
) == item
) return count
;