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 ConnectWidget( list_item
);
119 m_clientData
.Append( (wxObject
*)NULL
);
121 gtk_widget_show( list_item
);
124 m_parent
->AddChild( this );
126 (m_parent
->m_insertCallback
)( m_parent
, this );
130 gtk_widget_realize( GTK_WIDGET(m_list
) );
132 SetBackgroundColour( parent
->GetBackgroundColour() );
133 SetForegroundColour( parent
->GetForegroundColour() );
140 void wxListBox::Append( const wxString
&item
)
142 Append( item
, (char*)NULL
);
145 void wxListBox::Append( const wxString
&item
, char *clientData
)
147 wxCHECK_RET( m_list
!= NULL
, "invalid listbox" );
149 GtkWidget
*list_item
= gtk_list_item_new_with_label( item
);
151 gtk_signal_connect( GTK_OBJECT(list_item
), "select",
152 GTK_SIGNAL_FUNC(gtk_listitem_select_callback
), (gpointer
)this );
154 if (GetWindowStyleFlag() & wxLB_MULTIPLE
)
155 gtk_signal_connect( GTK_OBJECT(list_item
), "deselect",
156 GTK_SIGNAL_FUNC(gtk_listitem_select_callback
), (gpointer
)this );
158 ConnectWidget( list_item
);
160 m_clientData
.Append( (wxObject
*)clientData
);
162 gtk_container_add( GTK_CONTAINER(m_list
), list_item
);
164 if (m_widgetStyle
) ApplyWidgetStyle();
166 gtk_widget_show( list_item
);
168 ConnectWidget( list_item
);
170 ConnectDnDWidget( list_item
);
173 void wxListBox::Clear(void)
175 wxCHECK_RET( m_list
!= NULL
, "invalid listbox" );
177 gtk_list_clear_items( m_list
, 0, Number() );
179 m_clientData
.Clear();
182 void wxListBox::Delete( int n
)
184 wxCHECK_RET( m_list
!= NULL
, "invalid listbox" );
186 GList
*child
= g_list_nth( m_list
->children
, n
);
190 wxFAIL_MSG("wrong listbox index");
194 GList
*list
= g_list_append( NULL
, child
->data
);
195 gtk_list_remove_items( m_list
, list
);
198 wxNode
*node
= m_clientData
.Nth( n
);
201 wxFAIL_MSG("wrong listbox index");
204 m_clientData
.DeleteNode( node
);
207 void wxListBox::Deselect( int n
)
209 wxCHECK_RET( m_list
!= NULL
, "invalid listbox" );
211 gtk_list_unselect_item( m_list
, n
);
214 int wxListBox::FindString( const wxString
&item
) const
216 wxCHECK_MSG( m_list
!= NULL
, -1, "invalid listbox" );
218 GList
*child
= m_list
->children
;
222 GtkBin
*bin
= GTK_BIN( child
->data
);
223 GtkLabel
*label
= GTK_LABEL( bin
->child
);
224 if (item
== label
->label
) return count
;
229 // it's not an error if the string is not found - this function may be used to
230 // test for existence of the string in the listbox, so don't give any
231 // errors/assert failures.
236 char *wxListBox::GetClientData( int n
) const
238 wxCHECK_MSG( m_list
!= NULL
, (char*) NULL
, "invalid listbox" );
240 wxNode
*node
= m_clientData
.Nth( n
);
241 if (node
) return ((char*)node
->Data());
243 wxFAIL_MSG("wrong listbox index");
244 return (char *) NULL
;
247 int wxListBox::GetSelection(void) const
249 wxCHECK_MSG( m_list
!= NULL
, -1, "invalid listbox" );
251 GList
*child
= m_list
->children
;
255 if (GTK_WIDGET(child
->data
)->state
== GTK_STATE_SELECTED
) return count
;
262 int wxListBox::GetSelections(wxArrayInt
& aSelections
) const
264 wxCHECK_MSG( m_list
!= NULL
, -1, "invalid listbox" );
266 // get the number of selected items first
267 GList
*child
= m_list
->children
;
269 for ( child
= m_list
->children
; child
!= NULL
; child
= child
->next
)
271 if ( GTK_WIDGET(child
->data
)->state
== GTK_STATE_SELECTED
)
279 aSelections
.Alloc(count
); // optimization attempt
281 for ( child
= m_list
->children
; child
!= NULL
; child
= child
->next
, i
++ )
283 if ( GTK_WIDGET(child
->data
)->state
== GTK_STATE_SELECTED
)
291 wxString
wxListBox::GetString( int n
) const
293 wxCHECK_MSG( m_list
!= NULL
, "", "invalid listbox" );
295 GList
*child
= g_list_nth( m_list
->children
, n
);
298 GtkBin
*bin
= GTK_BIN( child
->data
);
299 GtkLabel
*label
= GTK_LABEL( bin
->child
);
302 wxFAIL_MSG("wrong listbox index");
306 wxString
wxListBox::GetStringSelection(void) const
308 wxCHECK_MSG( m_list
!= NULL
, "", "invalid listbox" );
310 GList
*selection
= m_list
->selection
;
313 GtkBin
*bin
= GTK_BIN( selection
->data
);
314 wxString tmp
= GTK_LABEL( bin
->child
)->label
;
317 wxFAIL_MSG("no listbox selection available");
321 int wxListBox::Number(void)
323 wxCHECK_MSG( m_list
!= NULL
, -1, "invalid listbox" );
325 GList
*child
= m_list
->children
;
327 while (child
) { count
++; child
= child
->next
; }
331 bool wxListBox::Selected( int n
)
333 wxCHECK_MSG( m_list
!= NULL
, FALSE
, "invalid listbox" );
335 GList
*target
= g_list_nth( m_list
->children
, n
);
338 GList
*child
= m_list
->selection
;
341 if (child
->data
== target
->data
) return TRUE
;
345 wxFAIL_MSG("wrong listbox index");
349 void wxListBox::Set( int WXUNUSED(n
), const wxString
*WXUNUSED(choices
) )
351 wxFAIL_MSG("wxListBox::Set not implemented");
354 void wxListBox::SetClientData( int n
, char *clientData
)
356 wxCHECK_RET( m_list
!= NULL
, "invalid listbox" );
358 wxNode
*node
= m_clientData
.Nth( n
);
361 node
->SetData( (wxObject
*)clientData
);
365 wxFAIL_MSG("wrong listbox index");
369 void wxListBox::SetFirstItem( int WXUNUSED(n
) )
371 wxFAIL_MSG("wxListBox::SetFirstItem not implemented");
374 void wxListBox::SetFirstItem( const wxString
&WXUNUSED(item
) )
376 wxFAIL_MSG("wxListBox::SetFirstItem not implemented");
379 void wxListBox::SetSelection( int n
, bool select
)
381 wxCHECK_RET( m_list
!= NULL
, "invalid listbox" );
384 gtk_list_select_item( m_list
, n
);
386 gtk_list_unselect_item( m_list
, n
);
389 void wxListBox::SetString( int n
, const wxString
&string
)
391 wxCHECK_RET( m_list
!= NULL
, "invalid listbox" );
393 GList
*child
= g_list_nth( m_list
->children
, n
);
396 GtkBin
*bin
= GTK_BIN( child
->data
);
397 GtkLabel
*label
= GTK_LABEL( bin
->child
);
398 gtk_label_set( label
, string
);
402 wxFAIL_MSG("wrong listbox index");
406 void wxListBox::SetStringSelection( const wxString
&string
, bool select
)
408 wxCHECK_RET( m_list
!= NULL
, "invalid listbox" );
410 SetSelection( FindString(string
), select
);
413 int wxListBox::GetIndex( GtkWidget
*item
) const
417 GList
*child
= m_list
->children
;
421 if (GTK_WIDGET(child
->data
) == item
) return count
;
429 void wxListBox::SetDropTarget( wxDropTarget
*dropTarget
)
431 wxCHECK_RET( m_list
!= NULL
, "invalid listbox" );
433 GList
*child
= m_list
->children
;
436 DisconnectDnDWidget( GTK_WIDGET( child
->data
) );
440 wxWindow::SetDropTarget( dropTarget
);
442 child
= m_list
->children
;
445 ConnectDnDWidget( GTK_WIDGET( child
->data
) );
450 GtkWidget
*wxListBox::GetConnectWidget(void)
452 return GTK_WIDGET(m_list
);
455 bool wxListBox::IsOwnGtkWindow( GdkWindow
*window
)
457 if (wxWindow::IsOwnGtkWindow( window
)) return TRUE
;
459 GList
*child
= m_list
->children
;
462 GtkWidget
*bin
= GTK_WIDGET( child
->data
);
463 if (bin
->window
== window
) return TRUE
;
470 void wxListBox::ApplyWidgetStyle()
474 GdkWindow
*window
= GTK_WIDGET(m_list
)->window
;
475 m_backgroundColour
.CalcPixel( gdk_window_get_colormap( window
) );
476 gdk_window_set_background( window
, m_backgroundColour
.GetColor() );
477 gdk_window_clear( window
);
479 GList
*child
= m_list
->children
;
482 gtk_widget_set_style( GTK_BIN(child
->data
)->child
, m_widgetStyle
);
483 gtk_widget_set_style( GTK_WIDGET(child
->data
), m_widgetStyle
);