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 GList
*child
= g_list_nth( m_list
->children
, n
);
163 wxFAIL_MSG("wrong listbox index");
167 GList
*list
= g_list_append( NULL
, child
->data
);
168 gtk_list_remove_items( m_list
, list
);
171 wxNode
*node
= m_clientData
.Nth( n
);
174 wxFAIL_MSG("wrong listbox index");
177 m_clientData
.DeleteNode( node
);
180 void wxListBox::Deselect( int n
)
182 gtk_list_unselect_item( m_list
, n
);
185 int wxListBox::FindString( const wxString
&item
) const
187 GList
*child
= m_list
->children
;
191 GtkBin
*bin
= GTK_BIN( child
->data
);
192 GtkLabel
*label
= GTK_LABEL( bin
->child
);
193 if (item
== label
->label
) return count
;
197 wxFAIL_MSG("wrong listbox index");
201 char *wxListBox::GetClientData( int n
) const
203 wxNode
*node
= m_clientData
.Nth( n
);
204 if (node
) return ((char*)node
->Data());
206 wxFAIL_MSG("wrong listbox index");
207 return (char *) NULL
;
210 int wxListBox::GetSelection(void) const
212 GList
*selection
= m_list
->selection
;
215 GList
*child
= m_list
->children
;
219 if (child
->data
== selection
->data
) return count
;
224 wxFAIL_MSG("wrong listbox index");
228 int wxListBox::GetSelections(wxArrayInt
& aSelections
) const
230 // get the number of selected items first
231 GList
*child
= m_list
->children
;
233 for ( child
= m_list
->children
; child
!= NULL
; child
= child
->next
) {
234 if ( GTK_WIDGET(child
->data
)->state
== GTK_STATE_SELECTED
)
242 aSelections
.Alloc(count
); // optimization attempt
244 for ( child
= m_list
->children
; child
!= NULL
; child
= child
->next
, i
++ ) {
245 if ( GTK_WIDGET(child
->data
)->state
== GTK_STATE_SELECTED
)
253 wxString
wxListBox::GetString( int n
) const
255 GList
*child
= g_list_nth( m_list
->children
, n
);
258 GtkBin
*bin
= GTK_BIN( child
->data
);
259 GtkLabel
*label
= GTK_LABEL( bin
->child
);
262 wxFAIL_MSG("wrong listbox index");
266 wxString
wxListBox::GetStringSelection(void) const
268 GList
*selection
= m_list
->selection
;
271 GtkBin
*bin
= GTK_BIN( selection
->data
);
272 wxString tmp
= GTK_LABEL( bin
->child
)->label
;
275 wxFAIL_MSG("no listbox selection available");
279 int wxListBox::Number(void)
281 GList
*child
= m_list
->children
;
283 while (child
) { count
++; child
= child
->next
; }
287 bool wxListBox::Selected( int n
)
289 GList
*target
= g_list_nth( m_list
->children
, n
);
292 GList
*child
= m_list
->selection
;
295 if (child
->data
== target
->data
) return TRUE
;
299 wxFAIL_MSG("wrong listbox index");
303 void wxListBox::Set( int WXUNUSED(n
), const wxString
*WXUNUSED(choices
) )
305 wxFAIL_MSG("wxListBox::Set not implemented");
308 void wxListBox::SetClientData( int n
, char *clientData
)
310 wxNode
*node
= m_clientData
.Nth( n
);
313 node
->SetData( (wxObject
*)clientData
);
317 wxFAIL_MSG("wrong listbox index");
321 void wxListBox::SetFirstItem( int WXUNUSED(n
) )
323 wxFAIL_MSG("wxListBox::SetFirstItem not implemented");
326 void wxListBox::SetFirstItem( const wxString
&WXUNUSED(item
) )
328 wxFAIL_MSG("wxListBox::SetFirstItem not implemented");
331 void wxListBox::SetSelection( int n
, bool select
)
334 gtk_list_select_item( m_list
, n
);
336 gtk_list_unselect_item( m_list
, n
);
339 void wxListBox::SetString( int n
, const wxString
&string
)
341 GList
*child
= g_list_nth( m_list
->children
, n
);
344 GtkBin
*bin
= GTK_BIN( child
->data
);
345 GtkLabel
*label
= GTK_LABEL( bin
->child
);
346 gtk_label_set( label
, string
);
350 wxFAIL_MSG("wrong listbox index");
354 void wxListBox::SetStringSelection( const wxString
&string
, bool select
)
356 SetSelection( FindString(string
), select
);
359 int wxListBox::GetIndex( GtkWidget
*item
) const
363 GList
*child
= m_list
->children
;
367 if (GTK_WIDGET(child
->data
) == item
) return count
;
375 GtkWidget
*wxListBox::GetConnectWidget(void)
377 return GTK_WIDGET(m_list
);