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"
20 //-----------------------------------------------------------------------------
22 //-----------------------------------------------------------------------------
24 extern bool g_blockEventsOnDrag
;
26 //-----------------------------------------------------------------------------
28 //-----------------------------------------------------------------------------
30 static void gtk_listitem_select_callback( GtkWidget
*WXUNUSED(widget
), wxListBox
*listbox
)
32 if (!listbox
->HasVMT()) return;
33 if (g_blockEventsOnDrag
) return;
35 wxCommandEvent
event(wxEVT_COMMAND_LISTBOX_SELECTED
, listbox
->GetId() );
37 wxArrayInt aSelections
;
38 int count
= listbox
->GetSelections(aSelections
);
41 event
.m_commandInt
= aSelections
[0] ;
42 event
.m_clientData
= listbox
->GetClientData(event
.m_commandInt
);
43 wxString
str(listbox
->GetString(event
.m_commandInt
));
45 event
.m_commandString
= copystring((char *)(const char *)str
);
49 event
.m_commandInt
= -1 ;
50 event
.m_commandString
= copystring("") ;
53 event
.SetEventObject( listbox
);
55 listbox
->GetEventHandler()->ProcessEvent( event
);
56 if (event
.m_commandString
) delete[] event
.m_commandString
;
59 //-----------------------------------------------------------------------------
61 IMPLEMENT_DYNAMIC_CLASS(wxListBox
,wxControl
)
63 wxListBox::wxListBox(void)
68 wxListBox::wxListBox( wxWindow
*parent
, wxWindowID id
,
69 const wxPoint
&pos
, const wxSize
&size
,
70 int n
, const wxString choices
[],
71 long style
, const wxString
&name
)
73 Create( parent
, id
, pos
, size
, n
, choices
, style
, name
);
76 bool wxListBox::Create( wxWindow
*parent
, wxWindowID id
,
77 const wxPoint
&pos
, const wxSize
&size
,
78 int n
, const wxString choices
[],
79 long style
, const wxString
&name
)
83 PreCreation( parent
, id
, pos
, size
, style
, name
);
85 m_widget
= gtk_scrolled_window_new( NULL
, NULL
);
86 gtk_scrolled_window_set_policy( GTK_SCROLLED_WINDOW(m_widget
),
87 GTK_POLICY_AUTOMATIC
, GTK_POLICY_AUTOMATIC
);
89 m_list
= GTK_LIST( gtk_list_new() );
91 // @@ what's the difference between BROWSE and SINGLE?
92 GtkSelectionMode mode
= GTK_SELECTION_BROWSE
;
93 if ( style
& wxLB_MULTIPLE
)
94 mode
= GTK_SELECTION_MULTIPLE
;
95 else if ( style
& wxLB_EXTENDED
)
96 mode
= GTK_SELECTION_EXTENDED
;
98 gtk_list_set_selection_mode( GTK_LIST(m_list
), mode
);
100 gtk_container_add (GTK_CONTAINER(m_widget
), GTK_WIDGET(m_list
) );
101 gtk_widget_show( GTK_WIDGET(m_list
) );
103 for (int i
= 0; i
< n
; i
++)
105 GtkWidget
*list_item
;
106 list_item
= gtk_list_item_new_with_label( choices
[i
] );
108 gtk_container_add( GTK_CONTAINER(m_list
), list_item
);
110 gtk_signal_connect( GTK_OBJECT(list_item
), "select",
111 GTK_SIGNAL_FUNC(gtk_listitem_select_callback
), (gpointer
)this );
113 if ( style
& wxLB_MULTIPLE
)
114 gtk_signal_connect( GTK_OBJECT(list_item
), "deselect",
115 GTK_SIGNAL_FUNC(gtk_listitem_select_callback
), (gpointer
)this );
117 m_clientData
.Append( (wxObject
*)NULL
);
119 gtk_widget_show( list_item
);
124 gtk_widget_realize( GTK_WIDGET(m_list
) );
131 void wxListBox::Append( const wxString
&item
)
133 Append( item
, (char*)NULL
);
136 void wxListBox::Append( const wxString
&item
, char *clientData
)
138 GtkWidget
*list_item
;
139 list_item
= gtk_list_item_new_with_label( item
);
141 gtk_signal_connect( GTK_OBJECT(list_item
), "select",
142 GTK_SIGNAL_FUNC(gtk_listitem_select_callback
), (gpointer
)this );
144 if ( GetWindowStyleFlag() & wxLB_MULTIPLE
)
145 gtk_signal_connect( GTK_OBJECT(list_item
), "deselect",
146 GTK_SIGNAL_FUNC(gtk_listitem_select_callback
), (gpointer
)this );
148 gtk_container_add( GTK_CONTAINER(m_list
), list_item
);
150 m_clientData
.Append( (wxObject
*)clientData
);
152 gtk_widget_show( list_item
);
155 void wxListBox::Clear(void)
157 gtk_list_clear_items( m_list
, 0, Number() );
159 m_clientData
.Clear();
162 void wxListBox::Delete( int n
)
164 gtk_list_clear_items( m_list
, n
, n
);
166 wxNode
*node
= m_clientData
.Nth( n
);
169 wxFAIL_MSG("wxListBox::Delete wrong index");
172 m_clientData
.DeleteNode( node
);
175 void wxListBox::Deselect( int n
)
177 gtk_list_unselect_item( m_list
, n
);
180 int wxListBox::FindString( const wxString
&item
) const
182 GList
*child
= m_list
->children
;
186 GtkBin
*bin
= GTK_BIN( child
->data
);
187 GtkLabel
*label
= GTK_LABEL( bin
->child
);
188 if (item
== label
->label
) return count
;
195 char *wxListBox::GetClientData( int n
) const
197 wxNode
*node
= m_clientData
.Nth( n
);
198 if (node
) return ((char*)node
->Data());
202 int wxListBox::GetSelection(void) const
204 GList
*selection
= m_list
->selection
;
207 GList
*child
= m_list
->children
;
211 if (child
->data
== selection
->data
) return count
;
219 int wxListBox::GetSelections(wxArrayInt
& aSelections
) const
221 // get the number of selected items first
222 GList
*child
= m_list
->children
;
224 for ( child
= m_list
->children
; child
!= NULL
; child
= child
->next
) {
225 if ( GTK_WIDGET(child
->data
)->state
== GTK_STATE_SELECTED
)
233 aSelections
.Alloc(count
); // optimization attempt
235 for ( child
= m_list
->children
; child
!= NULL
; child
= child
->next
, i
++ ) {
236 if ( GTK_WIDGET(child
->data
)->state
== GTK_STATE_SELECTED
)
244 wxString
wxListBox::GetString( int n
) const
246 GList
*child
= g_list_nth( m_list
->children
, n
);
249 GtkBin
*bin
= GTK_BIN( child
->data
);
250 GtkLabel
*label
= GTK_LABEL( bin
->child
);
256 wxString
wxListBox::GetStringSelection(void) const
258 GList
*selection
= m_list
->selection
;
261 GtkBin
*bin
= GTK_BIN( selection
->data
);
262 wxString tmp
= GTK_LABEL( bin
->child
)->label
;
268 int wxListBox::Number(void)
270 GList
*child
= m_list
->children
;
272 while (child
) { count
++; child
= child
->next
; };
276 bool wxListBox::Selected( int n
)
278 GList
*target
= g_list_nth( m_list
->children
, n
);
281 GList
*child
= m_list
->selection
;
284 if (child
->data
== target
->data
) return TRUE
;
291 void wxListBox::Set( int WXUNUSED(n
), const wxString
*WXUNUSED(choices
) )
295 void wxListBox::SetClientData( int n
, char *clientData
)
297 wxNode
*node
= m_clientData
.Nth( n
);
298 if (node
) node
->SetData( (wxObject
*)clientData
);
301 void wxListBox::SetFirstItem( int WXUNUSED(n
) )
305 void wxListBox::SetFirstItem( const wxString
&WXUNUSED(item
) )
309 void wxListBox::SetSelection( int n
, bool select
)
312 gtk_list_select_item( m_list
, n
);
314 gtk_list_unselect_item( m_list
, n
);
317 void wxListBox::SetString( int n
, const wxString
&string
)
319 GList
*child
= g_list_nth( m_list
->children
, n
);
322 GtkBin
*bin
= GTK_BIN( child
->data
);
323 GtkLabel
*label
= GTK_LABEL( bin
->child
);
324 gtk_label_set( label
, string
);
328 void wxListBox::SetStringSelection( const wxString
&string
, bool select
)
330 SetSelection( FindString(string
), select
);
333 int wxListBox::GetIndex( GtkWidget
*item
) const
337 GList
*child
= m_list
->children
;
341 if (GTK_WIDGET(child
->data
) == item
) return count
;
349 GtkWidget
*wxListBox::GetDropTargetWidget(void)
351 return GTK_WIDGET(m_list
);