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 gtk_widget_set_style( list_item
, m_widgetStyle
);
147 gtk_widget_set_style( GTK_BIN( list_item
)->child
, m_widgetStyle
);
150 gtk_signal_connect( GTK_OBJECT(list_item
), "select",
151 GTK_SIGNAL_FUNC(gtk_listitem_select_callback
), (gpointer
)this );
153 if (GetWindowStyleFlag() & wxLB_MULTIPLE
)
154 gtk_signal_connect( GTK_OBJECT(list_item
), "deselect",
155 GTK_SIGNAL_FUNC(gtk_listitem_select_callback
), (gpointer
)this );
157 m_clientData
.Append( (wxObject
*)clientData
);
159 gtk_container_add( GTK_CONTAINER(m_list
), list_item
);
161 gtk_widget_show( list_item
);
163 ConnectWidget( list_item
);
165 ConnectDnDWidget( list_item
);
168 void wxListBox
::Clear(void)
170 wxCHECK_RET( m_list
!= NULL
, "invalid listbox" );
172 gtk_list_clear_items( m_list
, 0, Number() );
174 m_clientData
.Clear();
177 void wxListBox
::Delete( int n
)
179 wxCHECK_RET( m_list
!= NULL
, "invalid listbox" );
181 GList
*child
= g_list_nth( m_list
->children
, n
);
185 wxFAIL_MSG("wrong listbox index");
189 GList
*list
= g_list_append( NULL
, child
->data
);
190 gtk_list_remove_items( m_list
, list
);
193 wxNode
*node
= m_clientData
.Nth( n
);
196 wxFAIL_MSG("wrong listbox index");
199 m_clientData
.DeleteNode( node
);
202 void wxListBox
::Deselect( int n
)
204 wxCHECK_RET( m_list
!= NULL
, "invalid listbox" );
206 gtk_list_unselect_item( m_list
, n
);
209 int wxListBox
::FindString( const wxString
&item
) const
211 wxCHECK_MSG( m_list
!= NULL
, -1, "invalid listbox" );
213 GList
*child
= m_list
->children
;
217 GtkBin
*bin
= GTK_BIN( child
->data
);
218 GtkLabel
*label
= GTK_LABEL( bin
->child
);
219 if (item
== label
->label
) return count
;
224 // it's not an error if the string is not found - this function may be used to
225 // test for existence of the string in the listbox, so don't give any
226 // errors/assert failures.
231 char *wxListBox
::GetClientData( int n
) const
233 wxCHECK_MSG( m_list
!= NULL
, (char*) NULL
, "invalid listbox" );
235 wxNode
*node
= m_clientData
.Nth( n
);
236 if (node
) return ((char*)node
->Data());
238 wxFAIL_MSG("wrong listbox index");
239 return (char *) NULL
;
242 int wxListBox
::GetSelection(void) const
244 wxCHECK_MSG( m_list
!= NULL
, -1, "invalid listbox" );
246 GList
*child
= m_list
->children
;
250 if (GTK_WIDGET(child
->data
)->state
== GTK_STATE_SELECTED
) return count
;
257 int wxListBox
::GetSelections(wxArrayInt
& aSelections
) const
259 wxCHECK_MSG( m_list
!= NULL
, -1, "invalid listbox" );
261 // get the number of selected items first
262 GList
*child
= m_list
->children
;
264 for ( child
= m_list
->children
; child
!= NULL
; child
= child
->next
)
266 if ( GTK_WIDGET(child
->data
)->state
== GTK_STATE_SELECTED
)
274 aSelections
.Alloc(count
); // optimization attempt
276 for ( child
= m_list
->children
; child
!= NULL
; child
= child
->next
, i
++ )
278 if ( GTK_WIDGET(child
->data
)->state
== GTK_STATE_SELECTED
)
286 wxString wxListBox
::GetString( int n
) const
288 wxCHECK_MSG( m_list
!= NULL
, "", "invalid listbox" );
290 GList
*child
= g_list_nth( m_list
->children
, n
);
293 GtkBin
*bin
= GTK_BIN( child
->data
);
294 GtkLabel
*label
= GTK_LABEL( bin
->child
);
297 wxFAIL_MSG("wrong listbox index");
301 wxString wxListBox
::GetStringSelection(void) const
303 wxCHECK_MSG( m_list
!= NULL
, "", "invalid listbox" );
305 GList
*selection
= m_list
->selection
;
308 GtkBin
*bin
= GTK_BIN( selection
->data
);
309 wxString tmp
= GTK_LABEL( bin
->child
)->label
;
312 wxFAIL_MSG("no listbox selection available");
316 int wxListBox
::Number(void)
318 wxCHECK_MSG( m_list
!= NULL
, -1, "invalid listbox" );
320 GList
*child
= m_list
->children
;
322 while (child
) { count
++; child
= child
->next
; }
326 bool wxListBox
::Selected( int n
)
328 wxCHECK_MSG( m_list
!= NULL
, FALSE
, "invalid listbox" );
330 GList
*target
= g_list_nth( m_list
->children
, n
);
333 GList
*child
= m_list
->selection
;
336 if (child
->data
== target
->data
) return TRUE
;
340 wxFAIL_MSG("wrong listbox index");
344 void wxListBox
::Set( int WXUNUSED(n
), const wxString
*WXUNUSED(choices
) )
346 wxFAIL_MSG("wxListBox::Set not implemented");
349 void wxListBox
::SetClientData( int n
, char *clientData
)
351 wxCHECK_RET( m_list
!= NULL
, "invalid listbox" );
353 wxNode
*node
= m_clientData
.Nth( n
);
356 node
->SetData( (wxObject
*)clientData
);
360 wxFAIL_MSG("wrong listbox index");
364 void wxListBox
::SetFirstItem( int WXUNUSED(n
) )
366 wxFAIL_MSG("wxListBox::SetFirstItem not implemented");
369 void wxListBox
::SetFirstItem( const wxString
&WXUNUSED(item
) )
371 wxFAIL_MSG("wxListBox::SetFirstItem not implemented");
374 void wxListBox
::SetSelection( int n
, bool select
)
376 wxCHECK_RET( m_list
!= NULL
, "invalid listbox" );
379 gtk_list_select_item( m_list
, n
);
381 gtk_list_unselect_item( m_list
, n
);
384 void wxListBox
::SetString( int n
, const wxString
&string
)
386 wxCHECK_RET( m_list
!= NULL
, "invalid listbox" );
388 GList
*child
= g_list_nth( m_list
->children
, n
);
391 GtkBin
*bin
= GTK_BIN( child
->data
);
392 GtkLabel
*label
= GTK_LABEL( bin
->child
);
393 gtk_label_set( label
, string
);
397 wxFAIL_MSG("wrong listbox index");
401 void wxListBox
::SetStringSelection( const wxString
&string
, bool select
)
403 wxCHECK_RET( m_list
!= NULL
, "invalid listbox" );
405 SetSelection( FindString(string
), select
);
408 int wxListBox
::GetIndex( GtkWidget
*item
) const
412 GList
*child
= m_list
->children
;
416 if (GTK_WIDGET(child
->data
) == item
) return count
;
424 void wxListBox
::SetDropTarget( wxDropTarget
*dropTarget
)
426 wxCHECK_RET( m_list
!= NULL
, "invalid listbox" );
428 GList
*child
= m_list
->children
;
431 DisconnectDnDWidget( GTK_WIDGET( child
->data
) );
435 wxWindow
::SetDropTarget( dropTarget
);
437 child
= m_list
->children
;
440 ConnectDnDWidget( GTK_WIDGET( child
->data
) );
445 GtkWidget
*wxListBox
::GetConnectWidget(void)
447 return GTK_WIDGET(m_list
);
450 bool wxListBox
::IsOwnGtkWindow( GdkWindow
*window
)
452 if (wxWindow
::IsOwnGtkWindow( window
)) return TRUE
;
454 GList
*child
= m_list
->children
;
457 GtkBin
*bin
= GTK_BIN( child
->data
);
458 if (bin
->child
->window
== window
) return TRUE
;
465 void wxListBox
::SetFont( const wxFont
&font
)
467 wxCHECK_RET( m_list
!= NULL
, "invalid listbox" );
469 wxControl
::SetFont( font
);
471 GList
*child
= m_list
->children
;
474 gtk_widget_set_style( GTK_BIN(child
->data
)->child
, m_widgetStyle
);
480 void wxListBox
::SetBackgroundColour( const wxColour
&colour
)
482 wxCHECK_RET( m_list
!= NULL
, "invalid listbox" );
484 wxControl
::SetBackgroundColour( colour
);
486 if (!m_backgroundColour
.Ok()) return;
488 // gtk_widget_set_style( GTK_WIDGET(m_list), m_widgetStyle );
490 GdkWindow
*window
= GTK_WIDGET(m_list
)->window
;
491 m_backgroundColour
.CalcPixel( gdk_window_get_colormap( window
) );
492 gdk_window_set_background( window
, m_backgroundColour
.GetColor() );
493 gdk_window_clear( window
);
495 GList
*child
= m_list
->children
;
498 gtk_widget_set_style( GTK_WIDGET(child
->data
), m_widgetStyle
);
499 // gtk_widget_set_style( GTK_BIN(child->data)->child, m_widgetStyle );