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"
21 //-----------------------------------------------------------------------------
23 //-----------------------------------------------------------------------------
25 extern bool g_blockEventsOnDrag
;
27 //-----------------------------------------------------------------------------
29 //-----------------------------------------------------------------------------
31 static void gtk_listitem_select_callback( GtkWidget
*WXUNUSED(widget
), wxListBox
*listbox
)
33 if (!listbox
->HasVMT()) return;
34 if (g_blockEventsOnDrag
) return;
36 wxCommandEvent
event(wxEVT_COMMAND_LISTBOX_SELECTED
, listbox
->GetId() );
38 wxArrayInt aSelections
;
39 int count
= listbox
->GetSelections(aSelections
);
42 event
.m_commandInt
= aSelections
[0] ;
43 event
.m_clientData
= listbox
->GetClientData(event
.m_commandInt
);
44 wxString
str(listbox
->GetString(event
.m_commandInt
));
46 event
.m_commandString
= copystring((char *)(const char *)str
);
50 event
.m_commandInt
= -1 ;
51 event
.m_commandString
= copystring("") ;
54 event
.SetEventObject( listbox
);
56 listbox
->GetEventHandler()->ProcessEvent( event
);
57 if (event
.m_commandString
) delete[] event
.m_commandString
;
60 //-----------------------------------------------------------------------------
62 IMPLEMENT_DYNAMIC_CLASS(wxListBox
,wxControl
)
64 wxListBox::wxListBox(void)
66 m_list
= (GtkList
*) NULL
;
69 bool wxListBox::Create( wxWindow
*parent
, wxWindowID id
,
70 const wxPoint
&pos
, const wxSize
&size
,
71 int n
, const wxString choices
[],
72 long style
, const wxValidator
& validator
, const wxString
&name
)
76 PreCreation( parent
, id
, pos
, size
, style
, name
);
78 SetValidator( validator
);
80 m_widget
= gtk_scrolled_window_new( (GtkAdjustment
*) NULL
, (GtkAdjustment
*) NULL
);
81 gtk_scrolled_window_set_policy( GTK_SCROLLED_WINDOW(m_widget
),
82 GTK_POLICY_AUTOMATIC
, GTK_POLICY_AUTOMATIC
);
84 m_list
= GTK_LIST( gtk_list_new() );
86 // @@ what's the difference between BROWSE and SINGLE?
87 GtkSelectionMode mode
= GTK_SELECTION_BROWSE
;
88 if ( style
& wxLB_MULTIPLE
)
89 mode
= GTK_SELECTION_MULTIPLE
;
90 else if ( style
& wxLB_EXTENDED
)
91 mode
= GTK_SELECTION_EXTENDED
;
93 gtk_list_set_selection_mode( GTK_LIST(m_list
), mode
);
95 gtk_container_add (GTK_CONTAINER(m_widget
), GTK_WIDGET(m_list
) );
96 gtk_widget_show( GTK_WIDGET(m_list
) );
98 for (int i
= 0; i
< n
; i
++)
100 GtkWidget
*list_item
;
101 list_item
= gtk_list_item_new_with_label( choices
[i
] );
103 gtk_container_add( GTK_CONTAINER(m_list
), list_item
);
105 gtk_signal_connect( GTK_OBJECT(list_item
), "select",
106 GTK_SIGNAL_FUNC(gtk_listitem_select_callback
), (gpointer
)this );
108 if ( style
& wxLB_MULTIPLE
)
109 gtk_signal_connect( GTK_OBJECT(list_item
), "deselect",
110 GTK_SIGNAL_FUNC(gtk_listitem_select_callback
), (gpointer
)this );
112 m_clientData
.Append( (wxObject
*)NULL
);
114 gtk_widget_show( list_item
);
119 gtk_widget_realize( GTK_WIDGET(m_list
) );
126 void wxListBox::Append( const wxString
&item
)
128 Append( item
, (char*)NULL
);
131 void wxListBox::Append( const wxString
&item
, char *clientData
)
133 GtkWidget
*list_item
;
134 list_item
= gtk_list_item_new_with_label( item
);
136 gtk_signal_connect( GTK_OBJECT(list_item
), "select",
137 GTK_SIGNAL_FUNC(gtk_listitem_select_callback
), (gpointer
)this );
139 if ( GetWindowStyleFlag() & wxLB_MULTIPLE
)
140 gtk_signal_connect( GTK_OBJECT(list_item
), "deselect",
141 GTK_SIGNAL_FUNC(gtk_listitem_select_callback
), (gpointer
)this );
143 gtk_container_add( GTK_CONTAINER(m_list
), list_item
);
145 m_clientData
.Append( (wxObject
*)clientData
);
147 gtk_widget_show( list_item
);
150 void wxListBox::Clear(void)
152 gtk_list_clear_items( m_list
, 0, Number() );
154 m_clientData
.Clear();
157 void wxListBox::Delete( int n
)
159 gtk_list_clear_items( m_list
, n
, n
);
161 wxNode
*node
= m_clientData
.Nth( n
);
164 wxFAIL_MSG("wxListBox::Delete wrong index");
167 m_clientData
.DeleteNode( node
);
170 void wxListBox::Deselect( int n
)
172 gtk_list_unselect_item( m_list
, n
);
175 int wxListBox::FindString( const wxString
&item
) const
177 GList
*child
= m_list
->children
;
181 GtkBin
*bin
= GTK_BIN( child
->data
);
182 GtkLabel
*label
= GTK_LABEL( bin
->child
);
183 if (item
== label
->label
) return count
;
190 char *wxListBox::GetClientData( int n
) const
192 wxNode
*node
= m_clientData
.Nth( n
);
193 if (node
) return ((char*)node
->Data());
194 return (char *) NULL
;
197 int wxListBox::GetSelection(void) const
199 GList
*selection
= m_list
->selection
;
202 GList
*child
= m_list
->children
;
206 if (child
->data
== selection
->data
) return count
;
214 int wxListBox::GetSelections(wxArrayInt
& aSelections
) const
216 // get the number of selected items first
217 GList
*child
= m_list
->children
;
219 for ( child
= m_list
->children
; child
!= NULL
; child
= child
->next
) {
220 if ( GTK_WIDGET(child
->data
)->state
== GTK_STATE_SELECTED
)
228 aSelections
.Alloc(count
); // optimization attempt
230 for ( child
= m_list
->children
; child
!= NULL
; child
= child
->next
, i
++ ) {
231 if ( GTK_WIDGET(child
->data
)->state
== GTK_STATE_SELECTED
)
239 wxString
wxListBox::GetString( int n
) const
241 GList
*child
= g_list_nth( m_list
->children
, n
);
244 GtkBin
*bin
= GTK_BIN( child
->data
);
245 GtkLabel
*label
= GTK_LABEL( bin
->child
);
251 wxString
wxListBox::GetStringSelection(void) const
253 GList
*selection
= m_list
->selection
;
256 GtkBin
*bin
= GTK_BIN( selection
->data
);
257 wxString tmp
= GTK_LABEL( bin
->child
)->label
;
263 int wxListBox::Number(void)
265 GList
*child
= m_list
->children
;
267 while (child
) { count
++; child
= child
->next
; }
271 bool wxListBox::Selected( int n
)
273 GList
*target
= g_list_nth( m_list
->children
, n
);
276 GList
*child
= m_list
->selection
;
279 if (child
->data
== target
->data
) return TRUE
;
286 void wxListBox::Set( int WXUNUSED(n
), const wxString
*WXUNUSED(choices
) )
290 void wxListBox::SetClientData( int n
, char *clientData
)
292 wxNode
*node
= m_clientData
.Nth( n
);
293 if (node
) node
->SetData( (wxObject
*)clientData
);
296 void wxListBox::SetFirstItem( int WXUNUSED(n
) )
300 void wxListBox::SetFirstItem( const wxString
&WXUNUSED(item
) )
304 void wxListBox::SetSelection( int n
, bool select
)
307 gtk_list_select_item( m_list
, n
);
309 gtk_list_unselect_item( m_list
, n
);
312 void wxListBox::SetString( int n
, const wxString
&string
)
314 GList
*child
= g_list_nth( m_list
->children
, n
);
317 GtkBin
*bin
= GTK_BIN( child
->data
);
318 GtkLabel
*label
= GTK_LABEL( bin
->child
);
319 gtk_label_set( label
, string
);
323 void wxListBox::SetStringSelection( const wxString
&string
, bool select
)
325 SetSelection( FindString(string
), select
);
328 int wxListBox::GetIndex( GtkWidget
*item
) const
332 GList
*child
= m_list
->children
;
336 if (GTK_WIDGET(child
->data
) == item
) return count
;
344 GtkWidget
*wxListBox::GetConnectWidget(void)
346 return GTK_WIDGET(m_list
);