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
);
54 listbox
->GetEventHandler()->ProcessEvent( event
);
55 if (event
.m_commandString
) delete[] event
.m_commandString
;
58 //-----------------------------------------------------------------------------
60 IMPLEMENT_DYNAMIC_CLASS(wxListBox
,wxControl
)
62 wxListBox::wxListBox(void)
67 wxListBox::wxListBox( wxWindow
*parent
, wxWindowID id
,
68 const wxPoint
&pos
, const wxSize
&size
,
69 int n
, const wxString choices
[],
70 long style
, const wxString
&name
)
72 Create( parent
, id
, pos
, size
, n
, choices
, style
, name
);
75 bool wxListBox::Create( wxWindow
*parent
, wxWindowID id
,
76 const wxPoint
&pos
, const wxSize
&size
,
77 int n
, const wxString choices
[],
78 long style
, const wxString
&name
)
82 PreCreation( parent
, id
, pos
, size
, style
, name
);
84 m_widget
= gtk_scrolled_window_new( NULL
, NULL
);
85 gtk_scrolled_window_set_policy( GTK_SCROLLED_WINDOW(m_widget
),
86 GTK_POLICY_AUTOMATIC
, GTK_POLICY_AUTOMATIC
);
88 m_list
= GTK_LIST( gtk_list_new() );
90 // @@ what's the difference between BROWSE and SINGLE?
91 GtkSelectionMode mode
= GTK_SELECTION_BROWSE
;
92 if ( style
& wxLB_MULTIPLE
)
93 mode
= GTK_SELECTION_MULTIPLE
;
94 else if ( style
& wxLB_EXTENDED
)
95 mode
= GTK_SELECTION_EXTENDED
;
97 gtk_list_set_selection_mode( GTK_LIST(m_list
), mode
);
99 gtk_container_add (GTK_CONTAINER(m_widget
), GTK_WIDGET(m_list
) );
100 gtk_widget_show( GTK_WIDGET(m_list
) );
102 for (int i
= 0; i
< n
; i
++)
104 GtkWidget
*list_item
;
105 list_item
= gtk_list_item_new_with_label( choices
[i
] );
107 gtk_container_add( GTK_CONTAINER(m_list
), list_item
);
109 gtk_signal_connect( GTK_OBJECT(list_item
), "select",
110 GTK_SIGNAL_FUNC(gtk_listitem_select_callback
), (gpointer
)this );
112 if ( style
& wxLB_MULTIPLE
)
113 gtk_signal_connect( GTK_OBJECT(list_item
), "deselect",
114 GTK_SIGNAL_FUNC(gtk_listitem_select_callback
), (gpointer
)this );
116 m_clientData
.Append( (wxObject
*)NULL
);
118 gtk_widget_show( list_item
);
123 gtk_widget_realize( GTK_WIDGET(m_list
) );
130 void wxListBox::Append( const wxString
&item
)
132 Append( item
, (char*)NULL
);
135 void wxListBox::Append( const wxString
&item
, char *clientData
)
137 GtkWidget
*list_item
;
138 list_item
= gtk_list_item_new_with_label( item
);
140 gtk_signal_connect( GTK_OBJECT(list_item
), "select",
141 GTK_SIGNAL_FUNC(gtk_listitem_select_callback
), (gpointer
)this );
143 if ( GetWindowStyleFlag() & wxLB_MULTIPLE
)
144 gtk_signal_connect( GTK_OBJECT(list_item
), "deselect",
145 GTK_SIGNAL_FUNC(gtk_listitem_select_callback
), (gpointer
)this );
147 gtk_container_add( GTK_CONTAINER(m_list
), list_item
);
149 m_clientData
.Append( (wxObject
*)clientData
);
151 gtk_widget_show( list_item
);
154 void wxListBox::Clear(void)
156 gtk_list_clear_items( m_list
, 0, Number() );
158 m_clientData
.Clear();
161 void wxListBox::Delete( int n
)
163 gtk_list_clear_items( m_list
, n
, n
);
165 wxNode
*node
= m_clientData
.Nth( n
);
168 wxFAIL_MSG("wxListBox::Delete wrong index");
171 m_clientData
.DeleteNode( node
);
174 void wxListBox::Deselect( int n
)
176 gtk_list_unselect_item( m_list
, n
);
179 int wxListBox::FindString( const wxString
&item
) const
181 GList
*child
= m_list
->children
;
185 GtkBin
*bin
= GTK_BIN( child
->data
);
186 GtkLabel
*label
= GTK_LABEL( bin
->child
);
187 if (item
== label
->label
) return count
;
194 char *wxListBox::GetClientData( int n
) const
196 wxNode
*node
= m_clientData
.Nth( n
);
197 if (node
) return ((char*)node
->Data());
201 int wxListBox::GetSelection(void) const
203 GList
*selection
= m_list
->selection
;
206 GList
*child
= m_list
->children
;
210 if (child
->data
== selection
->data
) return count
;
218 int wxListBox::GetSelections(wxArrayInt
& aSelections
) const
220 // get the number of selected items first
221 GList
*child
= m_list
->children
;
223 for ( child
= m_list
->children
; child
!= NULL
; child
= child
->next
) {
224 if ( GTK_WIDGET(child
->data
)->state
== GTK_STATE_SELECTED
)
232 aSelections
.Alloc(count
); // optimization attempt
234 for ( child
= m_list
->children
; child
!= NULL
; child
= child
->next
, i
++ ) {
235 if ( GTK_WIDGET(child
->data
)->state
== GTK_STATE_SELECTED
)
243 wxString
wxListBox::GetString( int n
) const
245 GList
*child
= g_list_nth( m_list
->children
, n
);
248 GtkBin
*bin
= GTK_BIN( child
->data
);
249 GtkLabel
*label
= GTK_LABEL( bin
->child
);
255 wxString
wxListBox::GetStringSelection(void) const
257 GList
*selection
= m_list
->selection
;
260 GtkBin
*bin
= GTK_BIN( selection
->data
);
261 wxString tmp
= GTK_LABEL( bin
->child
)->label
;
267 int wxListBox::Number(void)
269 GList
*child
= m_list
->children
;
271 while (child
) { count
++; child
= child
->next
; };
275 bool wxListBox::Selected( int n
)
277 GList
*target
= g_list_nth( m_list
->children
, n
);
280 GList
*child
= m_list
->selection
;
283 if (child
->data
== target
->data
) return TRUE
;
290 void wxListBox::Set( int WXUNUSED(n
), const wxString
*WXUNUSED(choices
) )
294 void wxListBox::SetClientData( int n
, char *clientData
)
296 wxNode
*node
= m_clientData
.Nth( n
);
297 if (node
) node
->SetData( (wxObject
*)clientData
);
300 void wxListBox::SetFirstItem( int WXUNUSED(n
) )
304 void wxListBox::SetFirstItem( const wxString
&WXUNUSED(item
) )
308 void wxListBox::SetSelection( int n
, bool select
)
311 gtk_list_select_item( m_list
, n
);
313 gtk_list_unselect_item( m_list
, n
);
316 void wxListBox::SetString( int n
, const wxString
&string
)
318 GList
*child
= g_list_nth( m_list
->children
, n
);
321 GtkBin
*bin
= GTK_BIN( child
->data
);
322 GtkLabel
*label
= GTK_LABEL( bin
->child
);
323 gtk_label_set( label
, string
);
327 void wxListBox::SetStringSelection( const wxString
&string
, bool select
)
329 SetSelection( FindString(string
), select
);
332 int wxListBox::GetIndex( GtkWidget
*item
) const
336 GList
*child
= m_list
->children
;
340 if (GTK_WIDGET(child
->data
) == item
) return count
;
348 GtkWidget
*wxListBox::GetDropTargetWidget(void)
350 return GTK_WIDGET(m_list
);