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() );
135 void wxListBox::Append( const wxString
&item
)
137 Append( item
, (char*)NULL
);
140 void wxListBox::Append( const wxString
&item
, char *clientData
)
142 wxCHECK_RET( m_list
!= NULL
, "invalid listbox" );
144 GtkWidget
*list_item
= gtk_list_item_new_with_label( item
);
148 gtk_widget_set_style( list_item
, m_widgetStyle
);
149 gtk_widget_set_style( GTK_BIN( list_item
)->child
, m_widgetStyle
);
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 ConnectWidget( list_item
);
161 m_clientData
.Append( (wxObject
*)clientData
);
163 gtk_container_add( GTK_CONTAINER(m_list
), list_item
);
165 gtk_widget_show( list_item
);
167 ConnectWidget( list_item
);
169 ConnectDnDWidget( list_item
);
172 void wxListBox::Clear(void)
174 wxCHECK_RET( m_list
!= NULL
, "invalid listbox" );
176 gtk_list_clear_items( m_list
, 0, Number() );
178 m_clientData
.Clear();
181 void wxListBox::Delete( int n
)
183 wxCHECK_RET( m_list
!= NULL
, "invalid listbox" );
185 GList
*child
= g_list_nth( m_list
->children
, n
);
189 wxFAIL_MSG("wrong listbox index");
193 GList
*list
= g_list_append( NULL
, child
->data
);
194 gtk_list_remove_items( m_list
, list
);
197 wxNode
*node
= m_clientData
.Nth( n
);
200 wxFAIL_MSG("wrong listbox index");
203 m_clientData
.DeleteNode( node
);
206 void wxListBox::Deselect( int n
)
208 wxCHECK_RET( m_list
!= NULL
, "invalid listbox" );
210 gtk_list_unselect_item( m_list
, n
);
213 int wxListBox::FindString( const wxString
&item
) const
215 wxCHECK_MSG( m_list
!= NULL
, -1, "invalid listbox" );
217 GList
*child
= m_list
->children
;
221 GtkBin
*bin
= GTK_BIN( child
->data
);
222 GtkLabel
*label
= GTK_LABEL( bin
->child
);
223 if (item
== label
->label
) return count
;
228 // it's not an error if the string is not found - this function may be used to
229 // test for existence of the string in the listbox, so don't give any
230 // errors/assert failures.
235 char *wxListBox::GetClientData( int n
) const
237 wxCHECK_MSG( m_list
!= NULL
, (char*) NULL
, "invalid listbox" );
239 wxNode
*node
= m_clientData
.Nth( n
);
240 if (node
) return ((char*)node
->Data());
242 wxFAIL_MSG("wrong listbox index");
243 return (char *) NULL
;
246 int wxListBox::GetSelection(void) const
248 wxCHECK_MSG( m_list
!= NULL
, -1, "invalid listbox" );
250 GList
*child
= m_list
->children
;
254 if (GTK_WIDGET(child
->data
)->state
== GTK_STATE_SELECTED
) return count
;
261 int wxListBox::GetSelections(wxArrayInt
& aSelections
) const
263 wxCHECK_MSG( m_list
!= NULL
, -1, "invalid listbox" );
265 // get the number of selected items first
266 GList
*child
= m_list
->children
;
268 for ( child
= m_list
->children
; child
!= NULL
; child
= child
->next
)
270 if ( GTK_WIDGET(child
->data
)->state
== GTK_STATE_SELECTED
)
278 aSelections
.Alloc(count
); // optimization attempt
280 for ( child
= m_list
->children
; child
!= NULL
; child
= child
->next
, i
++ )
282 if ( GTK_WIDGET(child
->data
)->state
== GTK_STATE_SELECTED
)
290 wxString
wxListBox::GetString( int n
) const
292 wxCHECK_MSG( m_list
!= NULL
, "", "invalid listbox" );
294 GList
*child
= g_list_nth( m_list
->children
, n
);
297 GtkBin
*bin
= GTK_BIN( child
->data
);
298 GtkLabel
*label
= GTK_LABEL( bin
->child
);
301 wxFAIL_MSG("wrong listbox index");
305 wxString
wxListBox::GetStringSelection(void) const
307 wxCHECK_MSG( m_list
!= NULL
, "", "invalid listbox" );
309 GList
*selection
= m_list
->selection
;
312 GtkBin
*bin
= GTK_BIN( selection
->data
);
313 wxString tmp
= GTK_LABEL( bin
->child
)->label
;
316 wxFAIL_MSG("no listbox selection available");
320 int wxListBox::Number(void)
322 wxCHECK_MSG( m_list
!= NULL
, -1, "invalid listbox" );
324 GList
*child
= m_list
->children
;
326 while (child
) { count
++; child
= child
->next
; }
330 bool wxListBox::Selected( int n
)
332 wxCHECK_MSG( m_list
!= NULL
, FALSE
, "invalid listbox" );
334 GList
*target
= g_list_nth( m_list
->children
, n
);
337 GList
*child
= m_list
->selection
;
340 if (child
->data
== target
->data
) return TRUE
;
344 wxFAIL_MSG("wrong listbox index");
348 void wxListBox::Set( int WXUNUSED(n
), const wxString
*WXUNUSED(choices
) )
350 wxFAIL_MSG("wxListBox::Set not implemented");
353 void wxListBox::SetClientData( int n
, char *clientData
)
355 wxCHECK_RET( m_list
!= NULL
, "invalid listbox" );
357 wxNode
*node
= m_clientData
.Nth( n
);
360 node
->SetData( (wxObject
*)clientData
);
364 wxFAIL_MSG("wrong listbox index");
368 void wxListBox::SetFirstItem( int WXUNUSED(n
) )
370 wxFAIL_MSG("wxListBox::SetFirstItem not implemented");
373 void wxListBox::SetFirstItem( const wxString
&WXUNUSED(item
) )
375 wxFAIL_MSG("wxListBox::SetFirstItem not implemented");
378 void wxListBox::SetSelection( int n
, bool select
)
380 wxCHECK_RET( m_list
!= NULL
, "invalid listbox" );
383 gtk_list_select_item( m_list
, n
);
385 gtk_list_unselect_item( m_list
, n
);
388 void wxListBox::SetString( int n
, const wxString
&string
)
390 wxCHECK_RET( m_list
!= NULL
, "invalid listbox" );
392 GList
*child
= g_list_nth( m_list
->children
, n
);
395 GtkBin
*bin
= GTK_BIN( child
->data
);
396 GtkLabel
*label
= GTK_LABEL( bin
->child
);
397 gtk_label_set( label
, string
);
401 wxFAIL_MSG("wrong listbox index");
405 void wxListBox::SetStringSelection( const wxString
&string
, bool select
)
407 wxCHECK_RET( m_list
!= NULL
, "invalid listbox" );
409 SetSelection( FindString(string
), select
);
412 int wxListBox::GetIndex( GtkWidget
*item
) const
416 GList
*child
= m_list
->children
;
420 if (GTK_WIDGET(child
->data
) == item
) return count
;
428 void wxListBox::SetDropTarget( wxDropTarget
*dropTarget
)
430 wxCHECK_RET( m_list
!= NULL
, "invalid listbox" );
432 GList
*child
= m_list
->children
;
435 DisconnectDnDWidget( GTK_WIDGET( child
->data
) );
439 wxWindow::SetDropTarget( dropTarget
);
441 child
= m_list
->children
;
444 ConnectDnDWidget( GTK_WIDGET( child
->data
) );
449 GtkWidget
*wxListBox::GetConnectWidget(void)
451 return GTK_WIDGET(m_list
);
454 bool wxListBox::IsOwnGtkWindow( GdkWindow
*window
)
456 if (wxWindow::IsOwnGtkWindow( window
)) return TRUE
;
458 GList
*child
= m_list
->children
;
461 GtkWidget
*bin
= GTK_WIDGET( child
->data
);
462 if (bin
->window
== window
) return TRUE
;
469 void wxListBox::SetFont( const wxFont
&font
)
471 wxCHECK_RET( m_list
!= NULL
, "invalid listbox" );
473 wxControl::SetFont( font
);
475 GList
*child
= m_list
->children
;
478 gtk_widget_set_style( GTK_BIN(child
->data
)->child
, m_widgetStyle
);
483 void wxListBox::SetBackgroundColour( const wxColour
&colour
)
485 wxCHECK_RET( m_list
!= NULL
, "invalid listbox" );
487 wxControl::SetBackgroundColour( colour
);
489 if (!m_backgroundColour
.Ok()) return;
491 GdkWindow
*window
= GTK_WIDGET(m_list
)->window
;
492 m_backgroundColour
.CalcPixel( gdk_window_get_colormap( window
) );
493 gdk_window_set_background( window
, m_backgroundColour
.GetColor() );
494 gdk_window_clear( window
);
496 GList
*child
= m_list
->children
;
499 gtk_widget_set_style( GTK_WIDGET(child
->data
), m_widgetStyle
);