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 m_clientData
.Append( (wxObject
*)NULL
);
97 gtk_widget_show( list_item
);
102 gtk_widget_realize( GTK_WIDGET(m_list
) );
109 void wxListBox::Append( const wxString
&item
)
111 GtkWidget
*list_item
;
112 list_item
= gtk_list_item_new_with_label( item
);
114 gtk_signal_connect( GTK_OBJECT(list_item
), "select",
115 GTK_SIGNAL_FUNC(gtk_listitem_select_callback
), (gpointer
)this );
117 gtk_container_add( GTK_CONTAINER(m_list
), list_item
);
119 m_clientData
.Append( (wxObject
*)NULL
);
121 gtk_widget_show( list_item
);
124 void wxListBox::Append( const wxString
&item
, char *clientData
)
126 GtkWidget
*list_item
;
127 list_item
= gtk_list_item_new_with_label( item
);
129 gtk_signal_connect( GTK_OBJECT(list_item
), "select",
130 GTK_SIGNAL_FUNC(gtk_listitem_select_callback
), (gpointer
)this );
132 gtk_container_add( GTK_CONTAINER(m_list
), list_item
);
134 m_clientData
.Append( (wxObject
*)clientData
);
136 gtk_widget_show( list_item
);
139 void wxListBox::Clear(void)
141 gtk_list_clear_items( m_list
, 0, Number() );
143 m_clientData
.Clear();
146 void wxListBox::Delete( int n
)
148 gtk_list_clear_items( m_list
, n
, n
);
150 wxNode
*node
= m_clientData
.Nth( n
);
153 wxFAIL_MSG("wxListBox::Delete wrong index");
156 m_clientData
.DeleteNode( node
);
159 void wxListBox::Deselect( int n
)
161 gtk_list_unselect_item( m_list
, n
);
164 int wxListBox::FindString( const wxString
&item
) const
166 GList
*child
= m_list
->children
;
170 GtkBin
*bin
= GTK_BIN( child
->data
);
171 GtkLabel
*label
= GTK_LABEL( bin
->child
);
172 if (item
== label
->label
) return count
;
179 char *wxListBox::GetClientData( int n
) const
181 wxNode
*node
= m_clientData
.Nth( n
);
182 if (node
) return ((char*)node
->Data());
186 int wxListBox::GetSelection(void) const
188 GList
*selection
= m_list
->selection
;
191 GList
*child
= m_list
->children
;
195 if (child
->data
== selection
->data
) return count
;
203 int wxListBox::GetSelections(wxArrayInt
& aSelections
) const
205 // get the number of selected items first
206 GList
*child
= m_list
->children
;
208 for ( child
= m_list
->children
; child
!= NULL
; child
= child
->next
) {
209 if ( GTK_WIDGET(child
->data
)->state
== GTK_STATE_SELECTED
)
217 aSelections
.Alloc(count
); // optimization attempt
219 for ( child
= m_list
->children
; child
!= NULL
; child
= child
->next
, i
++ ) {
220 if ( GTK_WIDGET(child
->data
)->state
== GTK_STATE_SELECTED
)
228 wxString
wxListBox::GetString( int n
) const
230 GList
*child
= g_list_nth( m_list
->children
, n
);
233 GtkBin
*bin
= GTK_BIN( child
->data
);
234 GtkLabel
*label
= GTK_LABEL( bin
->child
);
240 wxString
wxListBox::GetStringSelection(void) const
242 GList
*selection
= m_list
->selection
;
245 GtkBin
*bin
= GTK_BIN( selection
->data
);
246 wxString tmp
= GTK_LABEL( bin
->child
)->label
;
252 int wxListBox::Number(void)
254 GList
*child
= m_list
->children
;
256 while (child
) { count
++; child
= child
->next
; };
260 bool wxListBox::Selected( int n
)
262 GList
*target
= g_list_nth( m_list
->children
, n
);
265 GList
*child
= m_list
->selection
;
268 if (child
->data
== target
->data
) return TRUE
;
275 void wxListBox::Set( int WXUNUSED(n
), const wxString
*WXUNUSED(choices
) )
279 void wxListBox::SetClientData( int n
, char *clientData
)
281 wxNode
*node
= m_clientData
.Nth( n
);
282 if (node
) node
->SetData( (wxObject
*)clientData
);
285 void wxListBox::SetFirstItem( int WXUNUSED(n
) )
289 void wxListBox::SetFirstItem( const wxString
&WXUNUSED(item
) )
293 void wxListBox::SetSelection( int n
, bool select
)
296 gtk_list_select_item( m_list
, n
);
298 gtk_list_unselect_item( m_list
, n
);
301 void wxListBox::SetString( int WXUNUSED(n
), const wxString
&WXUNUSED(string
) )
305 void wxListBox::SetStringSelection( const wxString
&string
, bool select
)
307 SetSelection( FindString(string
), select
);
310 int wxListBox::GetIndex( GtkWidget
*item
) const
314 GList
*child
= m_list
->children
;
318 if (GTK_WIDGET(child
->data
) == item
) return count
;
326 GtkWidget
*wxListBox::GetDropTargetWidget(void)
328 return GTK_WIDGET(m_list
);