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
, wxListBox
*listbox
)
25 wxCommandEvent
event(wxEVT_COMMAND_LISTBOX_SELECTED
, listbox
->GetId() );
27 event
.SetInt( listbox
->GetIndex( widget
) );
29 GtkBin
*bin
= GTK_BIN( widget
);
30 GtkLabel
*label
= GTK_LABEL( bin
->child
);
31 wxString
tmp( label
->label
);
32 event
.SetString( WXSTRINGCAST(tmp
) );
33 event
.SetEventObject( listbox
);
35 listbox
->GetEventHandler()->ProcessEvent( event
);
38 //-----------------------------------------------------------------------------
40 IMPLEMENT_DYNAMIC_CLASS(wxListBox
,wxControl
)
42 wxListBox::wxListBox(void)
47 wxListBox::wxListBox( wxWindow
*parent
, wxWindowID id
,
48 const wxPoint
&pos
, const wxSize
&size
,
49 int n
, const wxString choices
[],
50 long style
, const wxString
&name
)
52 Create( parent
, id
, pos
, size
, n
, choices
, style
, name
);
55 bool wxListBox::Create( wxWindow
*parent
, wxWindowID id
,
56 const wxPoint
&pos
, const wxSize
&size
,
57 int n
, const wxString choices
[],
58 long style
, const wxString
&name
)
62 PreCreation( parent
, id
, pos
, size
, style
, name
);
64 m_widget
= gtk_scrolled_window_new( NULL
, NULL
);
65 gtk_scrolled_window_set_policy( GTK_SCROLLED_WINDOW(m_widget
),
66 GTK_POLICY_AUTOMATIC
, GTK_POLICY_AUTOMATIC
);
68 m_list
= GTK_LIST( gtk_list_new() );
70 // @@ what's the difference between BROWSE and SINGLE?
71 GtkSelectionMode mode
= GTK_SELECTION_BROWSE
;
72 if ( style
& wxLB_MULTIPLE
)
73 mode
= GTK_SELECTION_MULTIPLE
;
74 else if ( style
& wxLB_EXTENDED
)
75 mode
= GTK_SELECTION_EXTENDED
;
77 gtk_list_set_selection_mode( GTK_LIST(m_list
), mode
);
79 gtk_container_add (GTK_CONTAINER(m_widget
), GTK_WIDGET(m_list
) );
80 gtk_widget_show( GTK_WIDGET(m_list
) );
82 for (int i
= 0; i
< n
; i
++)
85 list_item
= gtk_list_item_new_with_label( choices
[i
] );
87 gtk_signal_connect( GTK_OBJECT(list_item
), "select",
88 GTK_SIGNAL_FUNC(gtk_listitem_select_callback
), (gpointer
)this );
90 gtk_container_add( GTK_CONTAINER(m_list
), list_item
);
92 m_clientData
.Append( (wxObject
*)NULL
);
94 gtk_widget_show( list_item
);
99 gtk_widget_realize( GTK_WIDGET(m_list
) );
106 void wxListBox::Append( const wxString
&item
)
108 GtkWidget
*list_item
;
109 list_item
= gtk_list_item_new_with_label( item
);
111 gtk_signal_connect( GTK_OBJECT(list_item
), "select",
112 GTK_SIGNAL_FUNC(gtk_listitem_select_callback
), (gpointer
)this );
114 gtk_container_add( GTK_CONTAINER(m_list
), list_item
);
116 m_clientData
.Append( (wxObject
*)NULL
);
118 gtk_widget_show( list_item
);
121 void wxListBox::Append( const wxString
&item
, char *clientData
)
123 GtkWidget
*list_item
;
124 list_item
= gtk_list_item_new_with_label( item
);
126 gtk_signal_connect( GTK_OBJECT(list_item
), "select",
127 GTK_SIGNAL_FUNC(gtk_listitem_select_callback
), (gpointer
)this );
129 gtk_container_add( GTK_CONTAINER(m_list
), list_item
);
131 m_clientData
.Append( (wxObject
*)clientData
);
133 gtk_widget_show( list_item
);
136 void wxListBox::Clear(void)
138 gtk_list_clear_items( m_list
, 0, Number() );
140 m_clientData
.Clear();
143 void wxListBox::Delete( int n
)
145 gtk_list_clear_items( m_list
, n
, n
);
147 wxNode
*node
= m_clientData
.Nth( n
);
150 wxFAIL_MSG("wxListBox::Delete wrong index");
153 m_clientData
.DeleteNode( node
);
156 void wxListBox::Deselect( int n
)
158 gtk_list_unselect_item( m_list
, n
);
161 int wxListBox::FindString( const wxString
&item
) const
163 GList
*child
= m_list
->children
;
167 GtkBin
*bin
= GTK_BIN( child
->data
);
168 GtkLabel
*label
= GTK_LABEL( bin
->child
);
169 if (item
== label
->label
) return count
;
176 char *wxListBox::GetClientData( int n
) const
178 wxNode
*node
= m_clientData
.Nth( n
);
179 if (node
) return ((char*)node
->Data());
183 int wxListBox::GetSelection(void) const
185 GList
*selection
= m_list
->selection
;
188 GList
*child
= m_list
->children
;
192 if (child
->data
== selection
->data
) return count
;
200 int wxListBox::GetSelections(wxArrayInt
& aSelections
) const
202 // get the number of selected items first
203 GList
*child
= m_list
->children
;
205 for ( child
= m_list
->children
; child
!= NULL
; child
= child
->next
) {
206 if ( GTK_WIDGET(child
->data
)->state
== GTK_STATE_SELECTED
)
214 aSelections
.Alloc(count
); // optimization attempt
216 for ( child
= m_list
->children
; child
!= NULL
; child
= child
->next
, i
++ ) {
217 if ( GTK_WIDGET(child
->data
)->state
== GTK_STATE_SELECTED
)
225 wxString
wxListBox::GetString( int n
) const
227 GList
*child
= g_list_nth( m_list
->children
, n
);
230 GtkBin
*bin
= GTK_BIN( child
->data
);
231 GtkLabel
*label
= GTK_LABEL( bin
->child
);
237 wxString
wxListBox::GetStringSelection(void) const
239 GList
*selection
= m_list
->selection
;
242 GtkBin
*bin
= GTK_BIN( selection
->data
);
243 wxString tmp
= GTK_LABEL( bin
->child
)->label
;
249 int wxListBox::Number(void)
251 GList
*child
= m_list
->children
;
253 while (child
) { count
++; child
= child
->next
; };
257 bool wxListBox::Selected( int n
)
259 GList
*target
= g_list_nth( m_list
->children
, n
);
262 GList
*child
= m_list
->selection
;
265 if (child
->data
== target
->data
) return TRUE
;
272 void wxListBox::Set( int WXUNUSED(n
), const wxString
*WXUNUSED(choices
) )
276 void wxListBox::SetClientData( int n
, char *clientData
)
278 wxNode
*node
= m_clientData
.Nth( n
);
279 if (node
) node
->SetData( (wxObject
*)clientData
);
282 void wxListBox::SetFirstItem( int WXUNUSED(n
) )
286 void wxListBox::SetFirstItem( const wxString
&WXUNUSED(item
) )
290 void wxListBox::SetSelection( int n
, bool select
)
293 gtk_list_select_item( m_list
, n
);
295 gtk_list_unselect_item( m_list
, n
);
298 void wxListBox::SetString( int WXUNUSED(n
), const wxString
&WXUNUSED(string
) )
302 void wxListBox::SetStringSelection( const wxString
&string
, bool select
)
304 SetSelection( FindString(string
), select
);
307 int wxListBox::GetIndex( GtkWidget
*item
) const
311 GList
*child
= m_list
->children
;
315 if (GTK_WIDGET(child
->data
) == item
) return count
;
323 GtkWidget
*wxListBox::GetDropTargetWidget(void)
325 return GTK_WIDGET(m_list
);