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
);
126 gtk_widget_realize( GTK_WIDGET(m_list
) );
128 SetBackgroundColour( parent
->GetBackgroundColour() );
129 SetForegroundColour( parent
->GetForegroundColour() );
136 void wxListBox::Append( const wxString
&item
)
138 Append( item
, (char*)NULL
);
141 void wxListBox::Append( const wxString
&item
, char *clientData
)
143 wxCHECK_RET( m_list
!= NULL
, "invalid listbox" );
145 GtkWidget
*list_item
= gtk_list_item_new_with_label( item
);
147 gtk_signal_connect( GTK_OBJECT(list_item
), "select",
148 GTK_SIGNAL_FUNC(gtk_listitem_select_callback
), (gpointer
)this );
150 if (GetWindowStyleFlag() & wxLB_MULTIPLE
)
151 gtk_signal_connect( GTK_OBJECT(list_item
), "deselect",
152 GTK_SIGNAL_FUNC(gtk_listitem_select_callback
), (gpointer
)this );
154 ConnectWidget( list_item
);
156 m_clientData
.Append( (wxObject
*)clientData
);
158 gtk_container_add( GTK_CONTAINER(m_list
), list_item
);
160 if (m_widgetStyle
) ApplyWidgetStyle();
162 gtk_widget_show( list_item
);
164 ConnectWidget( list_item
);
166 ConnectDnDWidget( list_item
);
169 void wxListBox::Clear(void)
171 wxCHECK_RET( m_list
!= NULL
, "invalid listbox" );
173 gtk_list_clear_items( m_list
, 0, Number() );
175 m_clientData
.Clear();
178 void wxListBox::Delete( int n
)
180 wxCHECK_RET( m_list
!= NULL
, "invalid listbox" );
182 GList
*child
= g_list_nth( m_list
->children
, n
);
186 wxFAIL_MSG("wrong listbox index");
190 GList
*list
= g_list_append( NULL
, child
->data
);
191 gtk_list_remove_items( m_list
, list
);
194 wxNode
*node
= m_clientData
.Nth( n
);
197 wxFAIL_MSG("wrong listbox index");
200 m_clientData
.DeleteNode( node
);
203 void wxListBox::Deselect( int n
)
205 wxCHECK_RET( m_list
!= NULL
, "invalid listbox" );
207 gtk_list_unselect_item( m_list
, n
);
210 int wxListBox::FindString( const wxString
&item
) const
212 wxCHECK_MSG( m_list
!= NULL
, -1, "invalid listbox" );
214 GList
*child
= m_list
->children
;
218 GtkBin
*bin
= GTK_BIN( child
->data
);
219 GtkLabel
*label
= GTK_LABEL( bin
->child
);
220 if (item
== label
->label
) return count
;
225 // it's not an error if the string is not found - this function may be used to
226 // test for existence of the string in the listbox, so don't give any
227 // errors/assert failures.
232 char *wxListBox::GetClientData( int n
) const
234 wxCHECK_MSG( m_list
!= NULL
, (char*) NULL
, "invalid listbox" );
236 wxNode
*node
= m_clientData
.Nth( n
);
237 if (node
) return ((char*)node
->Data());
239 wxFAIL_MSG("wrong listbox index");
240 return (char *) NULL
;
243 int wxListBox::GetSelection(void) const
245 wxCHECK_MSG( m_list
!= NULL
, -1, "invalid listbox" );
247 GList
*child
= m_list
->children
;
251 if (GTK_WIDGET(child
->data
)->state
== GTK_STATE_SELECTED
) return count
;
258 int wxListBox::GetSelections(wxArrayInt
& aSelections
) const
260 wxCHECK_MSG( m_list
!= NULL
, -1, "invalid listbox" );
262 // get the number of selected items first
263 GList
*child
= m_list
->children
;
265 for ( child
= m_list
->children
; child
!= NULL
; child
= child
->next
)
267 if ( GTK_WIDGET(child
->data
)->state
== GTK_STATE_SELECTED
)
275 aSelections
.Alloc(count
); // optimization attempt
277 for ( child
= m_list
->children
; child
!= NULL
; child
= child
->next
, i
++ )
279 if ( GTK_WIDGET(child
->data
)->state
== GTK_STATE_SELECTED
)
287 wxString
wxListBox::GetString( int n
) const
289 wxCHECK_MSG( m_list
!= NULL
, "", "invalid listbox" );
291 GList
*child
= g_list_nth( m_list
->children
, n
);
294 GtkBin
*bin
= GTK_BIN( child
->data
);
295 GtkLabel
*label
= GTK_LABEL( bin
->child
);
298 wxFAIL_MSG("wrong listbox index");
302 wxString
wxListBox::GetStringSelection(void) const
304 wxCHECK_MSG( m_list
!= NULL
, "", "invalid listbox" );
306 GList
*selection
= m_list
->selection
;
309 GtkBin
*bin
= GTK_BIN( selection
->data
);
310 wxString tmp
= GTK_LABEL( bin
->child
)->label
;
313 wxFAIL_MSG("no listbox selection available");
317 int wxListBox::Number(void)
319 wxCHECK_MSG( m_list
!= NULL
, -1, "invalid listbox" );
321 GList
*child
= m_list
->children
;
323 while (child
) { count
++; child
= child
->next
; }
327 bool wxListBox::Selected( int n
)
329 wxCHECK_MSG( m_list
!= NULL
, FALSE
, "invalid listbox" );
331 GList
*target
= g_list_nth( m_list
->children
, n
);
334 GList
*child
= m_list
->selection
;
337 if (child
->data
== target
->data
) return TRUE
;
341 wxFAIL_MSG("wrong listbox index");
345 void wxListBox::Set( int WXUNUSED(n
), const wxString
*WXUNUSED(choices
) )
347 wxFAIL_MSG("wxListBox::Set not implemented");
350 void wxListBox::SetClientData( int n
, char *clientData
)
352 wxCHECK_RET( m_list
!= NULL
, "invalid listbox" );
354 wxNode
*node
= m_clientData
.Nth( n
);
357 node
->SetData( (wxObject
*)clientData
);
361 wxFAIL_MSG("wrong listbox index");
365 void wxListBox::SetFirstItem( int WXUNUSED(n
) )
367 wxFAIL_MSG("wxListBox::SetFirstItem not implemented");
370 void wxListBox::SetFirstItem( const wxString
&WXUNUSED(item
) )
372 wxFAIL_MSG("wxListBox::SetFirstItem not implemented");
375 void wxListBox::SetSelection( int n
, bool select
)
377 wxCHECK_RET( m_list
!= NULL
, "invalid listbox" );
380 gtk_list_select_item( m_list
, n
);
382 gtk_list_unselect_item( m_list
, n
);
385 void wxListBox::SetString( int n
, const wxString
&string
)
387 wxCHECK_RET( m_list
!= NULL
, "invalid listbox" );
389 GList
*child
= g_list_nth( m_list
->children
, n
);
392 GtkBin
*bin
= GTK_BIN( child
->data
);
393 GtkLabel
*label
= GTK_LABEL( bin
->child
);
394 gtk_label_set( label
, string
);
398 wxFAIL_MSG("wrong listbox index");
402 void wxListBox::SetStringSelection( const wxString
&string
, bool select
)
404 wxCHECK_RET( m_list
!= NULL
, "invalid listbox" );
406 SetSelection( FindString(string
), select
);
409 int wxListBox::GetIndex( GtkWidget
*item
) const
413 GList
*child
= m_list
->children
;
417 if (GTK_WIDGET(child
->data
) == item
) return count
;
425 void wxListBox::SetDropTarget( wxDropTarget
*dropTarget
)
427 wxCHECK_RET( m_list
!= NULL
, "invalid listbox" );
429 GList
*child
= m_list
->children
;
432 DisconnectDnDWidget( GTK_WIDGET( child
->data
) );
436 wxWindow::SetDropTarget( dropTarget
);
438 child
= m_list
->children
;
441 ConnectDnDWidget( GTK_WIDGET( child
->data
) );
446 GtkWidget
*wxListBox::GetConnectWidget(void)
448 return GTK_WIDGET(m_list
);
451 bool wxListBox::IsOwnGtkWindow( GdkWindow
*window
)
453 if (wxWindow::IsOwnGtkWindow( window
)) return TRUE
;
455 GList
*child
= m_list
->children
;
458 GtkWidget
*bin
= GTK_WIDGET( child
->data
);
459 if (bin
->window
== window
) return TRUE
;
466 void wxListBox::ApplyWidgetStyle()
470 GdkWindow
*window
= GTK_WIDGET(m_list
)->window
;
471 m_backgroundColour
.CalcPixel( gdk_window_get_colormap( window
) );
472 gdk_window_set_background( window
, m_backgroundColour
.GetColor() );
473 gdk_window_clear( window
);
475 GList
*child
= m_list
->children
;
478 gtk_widget_set_style( GTK_BIN(child
->data
)->child
, m_widgetStyle
);
479 gtk_widget_set_style( GTK_WIDGET(child
->data
), m_widgetStyle
);