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)
69 wxListBox::wxListBox( wxWindow
*parent
, wxWindowID id
,
70 const wxPoint
&pos
, const wxSize
&size
,
71 int n
, const wxString choices
[],
72 long style
, const wxString
&name
)
74 Create( parent
, id
, pos
, size
, n
, choices
, style
, name
);
77 bool wxListBox::Create( wxWindow
*parent
, wxWindowID id
,
78 const wxPoint
&pos
, const wxSize
&size
,
79 int n
, const wxString choices
[],
80 long style
, const wxString
&name
)
84 PreCreation( parent
, id
, pos
, size
, style
, name
);
86 m_widget
= gtk_scrolled_window_new( NULL
, NULL
);
87 gtk_scrolled_window_set_policy( GTK_SCROLLED_WINDOW(m_widget
),
88 GTK_POLICY_AUTOMATIC
, GTK_POLICY_AUTOMATIC
);
90 m_list
= GTK_LIST( gtk_list_new() );
92 // @@ what's the difference between BROWSE and SINGLE?
93 GtkSelectionMode mode
= GTK_SELECTION_BROWSE
;
94 if ( style
& wxLB_MULTIPLE
)
95 mode
= GTK_SELECTION_MULTIPLE
;
96 else if ( style
& wxLB_EXTENDED
)
97 mode
= GTK_SELECTION_EXTENDED
;
99 gtk_list_set_selection_mode( GTK_LIST(m_list
), mode
);
101 gtk_container_add (GTK_CONTAINER(m_widget
), GTK_WIDGET(m_list
) );
102 gtk_widget_show( GTK_WIDGET(m_list
) );
104 for (int i
= 0; i
< n
; i
++)
106 GtkWidget
*list_item
;
107 list_item
= gtk_list_item_new_with_label( choices
[i
] );
109 gtk_container_add( GTK_CONTAINER(m_list
), list_item
);
111 gtk_signal_connect( GTK_OBJECT(list_item
), "select",
112 GTK_SIGNAL_FUNC(gtk_listitem_select_callback
), (gpointer
)this );
114 if ( style
& wxLB_MULTIPLE
)
115 gtk_signal_connect( GTK_OBJECT(list_item
), "deselect",
116 GTK_SIGNAL_FUNC(gtk_listitem_select_callback
), (gpointer
)this );
118 m_clientData
.Append( (wxObject
*)NULL
);
120 gtk_widget_show( list_item
);
125 gtk_widget_realize( GTK_WIDGET(m_list
) );
132 void wxListBox::Append( const wxString
&item
)
134 Append( item
, (char*)NULL
);
137 void wxListBox::Append( const wxString
&item
, char *clientData
)
139 GtkWidget
*list_item
;
140 list_item
= gtk_list_item_new_with_label( item
);
142 gtk_signal_connect( GTK_OBJECT(list_item
), "select",
143 GTK_SIGNAL_FUNC(gtk_listitem_select_callback
), (gpointer
)this );
145 if ( GetWindowStyleFlag() & wxLB_MULTIPLE
)
146 gtk_signal_connect( GTK_OBJECT(list_item
), "deselect",
147 GTK_SIGNAL_FUNC(gtk_listitem_select_callback
), (gpointer
)this );
149 gtk_container_add( GTK_CONTAINER(m_list
), list_item
);
151 m_clientData
.Append( (wxObject
*)clientData
);
153 gtk_widget_show( list_item
);
156 void wxListBox::Clear(void)
158 gtk_list_clear_items( m_list
, 0, Number() );
160 m_clientData
.Clear();
163 void wxListBox::Delete( int n
)
165 gtk_list_clear_items( m_list
, n
, n
);
167 wxNode
*node
= m_clientData
.Nth( n
);
170 wxFAIL_MSG("wxListBox::Delete wrong index");
173 m_clientData
.DeleteNode( node
);
176 void wxListBox::Deselect( int n
)
178 gtk_list_unselect_item( m_list
, n
);
181 int wxListBox::FindString( const wxString
&item
) const
183 GList
*child
= m_list
->children
;
187 GtkBin
*bin
= GTK_BIN( child
->data
);
188 GtkLabel
*label
= GTK_LABEL( bin
->child
);
189 if (item
== label
->label
) return count
;
196 char *wxListBox::GetClientData( int n
) const
198 wxNode
*node
= m_clientData
.Nth( n
);
199 if (node
) return ((char*)node
->Data());
203 int wxListBox::GetSelection(void) const
205 GList
*selection
= m_list
->selection
;
208 GList
*child
= m_list
->children
;
212 if (child
->data
== selection
->data
) return count
;
220 int wxListBox::GetSelections(wxArrayInt
& aSelections
) const
222 // get the number of selected items first
223 GList
*child
= m_list
->children
;
225 for ( child
= m_list
->children
; child
!= NULL
; child
= child
->next
) {
226 if ( GTK_WIDGET(child
->data
)->state
== GTK_STATE_SELECTED
)
234 aSelections
.Alloc(count
); // optimization attempt
236 for ( child
= m_list
->children
; child
!= NULL
; child
= child
->next
, i
++ ) {
237 if ( GTK_WIDGET(child
->data
)->state
== GTK_STATE_SELECTED
)
245 wxString
wxListBox::GetString( int n
) const
247 GList
*child
= g_list_nth( m_list
->children
, n
);
250 GtkBin
*bin
= GTK_BIN( child
->data
);
251 GtkLabel
*label
= GTK_LABEL( bin
->child
);
257 wxString
wxListBox::GetStringSelection(void) const
259 GList
*selection
= m_list
->selection
;
262 GtkBin
*bin
= GTK_BIN( selection
->data
);
263 wxString tmp
= GTK_LABEL( bin
->child
)->label
;
269 int wxListBox::Number(void)
271 GList
*child
= m_list
->children
;
273 while (child
) { count
++; child
= child
->next
; };
277 bool wxListBox::Selected( int n
)
279 GList
*target
= g_list_nth( m_list
->children
, n
);
282 GList
*child
= m_list
->selection
;
285 if (child
->data
== target
->data
) return TRUE
;
292 void wxListBox::Set( int WXUNUSED(n
), const wxString
*WXUNUSED(choices
) )
296 void wxListBox::SetClientData( int n
, char *clientData
)
298 wxNode
*node
= m_clientData
.Nth( n
);
299 if (node
) node
->SetData( (wxObject
*)clientData
);
302 void wxListBox::SetFirstItem( int WXUNUSED(n
) )
306 void wxListBox::SetFirstItem( const wxString
&WXUNUSED(item
) )
310 void wxListBox::SetSelection( int n
, bool select
)
313 gtk_list_select_item( m_list
, n
);
315 gtk_list_unselect_item( m_list
, n
);
318 void wxListBox::SetString( int n
, const wxString
&string
)
320 GList
*child
= g_list_nth( m_list
->children
, n
);
323 GtkBin
*bin
= GTK_BIN( child
->data
);
324 GtkLabel
*label
= GTK_LABEL( bin
->child
);
325 gtk_label_set( label
, string
);
329 void wxListBox::SetStringSelection( const wxString
&string
, bool select
)
331 SetSelection( FindString(string
), select
);
334 int wxListBox::GetIndex( GtkWidget
*item
) const
338 GList
*child
= m_list
->children
;
342 if (GTK_WIDGET(child
->data
) == item
) return count
;
350 GtkWidget
*wxListBox::GetConnectWidget(void)
352 return GTK_WIDGET(m_list
);