1 /////////////////////////////////////////////////////////////////////////////
4 // Author: Robert Roebling
6 // Copyright: (c) 1998 Robert Roebling
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
12 #pragma implementation "listbox.h"
15 #include "wx/dynarray.h"
16 #include "wx/listbox.h"
20 //-----------------------------------------------------------------------------
22 //-----------------------------------------------------------------------------
24 extern bool g_blockEventsOnDrag
;
26 //-----------------------------------------------------------------------------
27 // "select" and "deselect"
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 //-----------------------------------------------------------------------------
63 IMPLEMENT_DYNAMIC_CLASS(wxListBox
,wxControl
)
65 wxListBox::wxListBox(void)
67 m_list
= (GtkList
*) NULL
;
70 bool wxListBox::Create( wxWindow
*parent
, wxWindowID id
,
71 const wxPoint
&pos
, const wxSize
&size
,
72 int n
, const wxString choices
[],
73 long style
, const wxValidator
& validator
, const wxString
&name
)
77 PreCreation( parent
, id
, pos
, size
, style
, name
);
79 SetValidator( validator
);
81 m_widget
= gtk_scrolled_window_new( (GtkAdjustment
*) NULL
, (GtkAdjustment
*) NULL
);
82 gtk_scrolled_window_set_policy( GTK_SCROLLED_WINDOW(m_widget
),
83 GTK_POLICY_AUTOMATIC
, GTK_POLICY_AUTOMATIC
);
85 m_list
= GTK_LIST( gtk_list_new() );
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 wxSize newSize
= size
;
99 if (newSize
.x
== -1) newSize
.x
= 100;
100 if (newSize
.y
== -1) newSize
.y
= 110;
101 SetSize( newSize
.x
, newSize
.y
);
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
) );
126 SetBackgroundColour( parent
->GetBackgroundColour() );
133 void wxListBox::Append( const wxString
&item
)
135 Append( item
, (char*)NULL
);
138 void wxListBox::Append( const wxString
&item
, char *clientData
)
140 wxCHECK_RET( m_list
!= NULL
, "invalid listbox" );
142 GtkWidget
*list_item
= gtk_list_item_new_with_label( item
);
146 GtkBin
*bin
= GTK_BIN( list_item
);
147 gtk_widget_set_style( bin
->child
,
149 gtk_widget_get_style( m_widget
) ) );
152 gtk_signal_connect( GTK_OBJECT(list_item
), "select",
153 GTK_SIGNAL_FUNC(gtk_listitem_select_callback
), (gpointer
)this );
155 if (GetWindowStyleFlag() & wxLB_MULTIPLE
)
156 gtk_signal_connect( GTK_OBJECT(list_item
), "deselect",
157 GTK_SIGNAL_FUNC(gtk_listitem_select_callback
), (gpointer
)this );
159 m_clientData
.Append( (wxObject
*)clientData
);
161 gtk_container_add( GTK_CONTAINER(m_list
), list_item
);
163 gtk_widget_show( list_item
);
165 ConnectWidget( list_item
);
167 ConnectDnDWidget( list_item
);
170 void wxListBox::Clear(void)
172 wxCHECK_RET( m_list
!= NULL
, "invalid listbox" );
174 gtk_list_clear_items( m_list
, 0, Number() );
176 m_clientData
.Clear();
179 void wxListBox::Delete( int n
)
181 wxCHECK_RET( m_list
!= NULL
, "invalid listbox" );
183 GList
*child
= g_list_nth( m_list
->children
, n
);
187 wxFAIL_MSG("wrong listbox index");
191 GList
*list
= g_list_append( NULL
, child
->data
);
192 gtk_list_remove_items( m_list
, list
);
195 wxNode
*node
= m_clientData
.Nth( n
);
198 wxFAIL_MSG("wrong listbox index");
201 m_clientData
.DeleteNode( node
);
204 void wxListBox::Deselect( int n
)
206 wxCHECK_RET( m_list
!= NULL
, "invalid listbox" );
208 gtk_list_unselect_item( m_list
, n
);
211 int wxListBox::FindString( const wxString
&item
) const
213 wxCHECK_MSG( m_list
!= NULL
, -1, "invalid listbox" );
215 GList
*child
= m_list
->children
;
219 GtkBin
*bin
= GTK_BIN( child
->data
);
220 GtkLabel
*label
= GTK_LABEL( bin
->child
);
221 if (item
== label
->label
) return count
;
226 // it's not an error if the string is not found - this function may be used to
227 // test for existence of the string in the listbox, so don't give any
228 // errors/assert failures.
233 char *wxListBox::GetClientData( int n
) const
235 wxCHECK_MSG( m_list
!= NULL
, (char*) NULL
, "invalid listbox" );
237 wxNode
*node
= m_clientData
.Nth( n
);
238 if (node
) return ((char*)node
->Data());
240 wxFAIL_MSG("wrong listbox index");
241 return (char *) NULL
;
244 int wxListBox::GetSelection(void) const
246 wxCHECK_MSG( m_list
!= NULL
, -1, "invalid listbox" );
248 GList
*child
= m_list
->children
;
252 if (GTK_WIDGET(child
->data
)->state
== GTK_STATE_SELECTED
) return count
;
259 int wxListBox::GetSelections(wxArrayInt
& aSelections
) const
261 wxCHECK_MSG( m_list
!= NULL
, -1, "invalid listbox" );
263 // get the number of selected items first
264 GList
*child
= m_list
->children
;
266 for ( child
= m_list
->children
; child
!= NULL
; child
= child
->next
)
268 if ( GTK_WIDGET(child
->data
)->state
== GTK_STATE_SELECTED
)
276 aSelections
.Alloc(count
); // optimization attempt
278 for ( child
= m_list
->children
; child
!= NULL
; child
= child
->next
, i
++ )
280 if ( GTK_WIDGET(child
->data
)->state
== GTK_STATE_SELECTED
)
288 wxString
wxListBox::GetString( int n
) const
290 wxCHECK_MSG( m_list
!= NULL
, "", "invalid listbox" );
292 GList
*child
= g_list_nth( m_list
->children
, n
);
295 GtkBin
*bin
= GTK_BIN( child
->data
);
296 GtkLabel
*label
= GTK_LABEL( bin
->child
);
299 wxFAIL_MSG("wrong listbox index");
303 wxString
wxListBox::GetStringSelection(void) const
305 wxCHECK_MSG( m_list
!= NULL
, "", "invalid listbox" );
307 GList
*selection
= m_list
->selection
;
310 GtkBin
*bin
= GTK_BIN( selection
->data
);
311 wxString tmp
= GTK_LABEL( bin
->child
)->label
;
314 wxFAIL_MSG("no listbox selection available");
318 int wxListBox::Number(void)
320 wxCHECK_MSG( m_list
!= NULL
, -1, "invalid listbox" );
322 GList
*child
= m_list
->children
;
324 while (child
) { count
++; child
= child
->next
; }
328 bool wxListBox::Selected( int n
)
330 wxCHECK_MSG( m_list
!= NULL
, FALSE
, "invalid listbox" );
332 GList
*target
= g_list_nth( m_list
->children
, n
);
335 GList
*child
= m_list
->selection
;
338 if (child
->data
== target
->data
) return TRUE
;
342 wxFAIL_MSG("wrong listbox index");
346 void wxListBox::Set( int WXUNUSED(n
), const wxString
*WXUNUSED(choices
) )
348 wxFAIL_MSG("wxListBox::Set not implemented");
351 void wxListBox::SetClientData( int n
, char *clientData
)
353 wxCHECK_RET( m_list
!= NULL
, "invalid listbox" );
355 wxNode
*node
= m_clientData
.Nth( n
);
358 node
->SetData( (wxObject
*)clientData
);
362 wxFAIL_MSG("wrong listbox index");
366 void wxListBox::SetFirstItem( int WXUNUSED(n
) )
368 wxFAIL_MSG("wxListBox::SetFirstItem not implemented");
371 void wxListBox::SetFirstItem( const wxString
&WXUNUSED(item
) )
373 wxFAIL_MSG("wxListBox::SetFirstItem not implemented");
376 void wxListBox::SetSelection( int n
, bool select
)
378 wxCHECK_RET( m_list
!= NULL
, "invalid listbox" );
381 gtk_list_select_item( m_list
, n
);
383 gtk_list_unselect_item( m_list
, n
);
386 void wxListBox::SetString( int n
, const wxString
&string
)
388 wxCHECK_RET( m_list
!= NULL
, "invalid listbox" );
390 GList
*child
= g_list_nth( m_list
->children
, n
);
393 GtkBin
*bin
= GTK_BIN( child
->data
);
394 GtkLabel
*label
= GTK_LABEL( bin
->child
);
395 gtk_label_set( label
, string
);
399 wxFAIL_MSG("wrong listbox index");
403 void wxListBox::SetStringSelection( const wxString
&string
, bool select
)
405 wxCHECK_RET( m_list
!= NULL
, "invalid listbox" );
407 SetSelection( FindString(string
), select
);
410 int wxListBox::GetIndex( GtkWidget
*item
) const
414 GList
*child
= m_list
->children
;
418 if (GTK_WIDGET(child
->data
) == item
) return count
;
426 void wxListBox::SetDropTarget( wxDropTarget
*dropTarget
)
428 wxCHECK_RET( m_list
!= NULL
, "invalid listbox" );
430 GList
*child
= m_list
->children
;
433 DisconnectDnDWidget( GTK_WIDGET( child
->data
) );
437 wxWindow::SetDropTarget( dropTarget
);
439 child
= m_list
->children
;
442 ConnectDnDWidget( GTK_WIDGET( child
->data
) );
447 GtkWidget
*wxListBox::GetConnectWidget(void)
449 return GTK_WIDGET(m_list
);
452 bool wxListBox::IsOwnGtkWindow( GdkWindow
*window
)
454 if (wxWindow::IsOwnGtkWindow( window
)) return TRUE
;
456 GList
*child
= m_list
->children
;
459 GtkBin
*bin
= GTK_BIN( child
->data
);
460 if (bin
->child
->window
== window
) return TRUE
;
467 void wxListBox::SetFont( const wxFont
&font
)
469 wxCHECK_RET( m_list
!= NULL
, "invalid listbox" );
471 wxControl::SetFont( font
);
473 GList
*child
= m_list
->children
;
476 gtk_widget_set_style( GTK_BIN(child
->data
)->child
,
478 gtk_widget_get_style( m_widget
) ) );
484 void wxListBox::SetBackgroundColour( const wxColour
&colour
)
488 wxCHECK_RET( m_list
!= NULL
, "invalid listbox" );
490 wxControl::SetBackgroundColour( colour
);
494 if (!m_backgroundColour
.Ok()) return;
496 gtk_widget_set_style( GTK_WIDGET(m_list
),
498 gtk_widget_get_style( m_widget
) ) );
500 GList
*child
= m_list
->children
;
503 gtk_widget_set_style( GTK_WIDGET(child
->data
),
505 gtk_widget_get_style( m_widget
) ) );
507 gtk_widget_set_style( GTK_BIN(child
->data
)->child
,
509 gtk_widget_get_style( m_widget
) ) );