1 /////////////////////////////////////////////////////////////////////////////
4 // Author: Robert Roebling
7 // Copyright: (c) 1998 Robert Roebling, Julian Smart and Markus Holzem
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
13 #pragma implementation "listbox.h"
16 #include "wx/dynarray.h"
17 #include "wx/listbox.h"
21 //-----------------------------------------------------------------------------
23 //-----------------------------------------------------------------------------
25 extern bool g_blockEventsOnDrag
;
27 //-----------------------------------------------------------------------------
28 // "select" and "deselect"
29 //-----------------------------------------------------------------------------
31 static void gtk_listitem_select_callback( GtkWidget
*WXUNUSED(widget
), wxListBox
*listbox
)
33 if (!listbox
->HasVMT()) return;
34 if (g_blockEventsOnDrag
) return;
36 wxCommandEvent
event(wxEVT_COMMAND_LISTBOX_SELECTED
, listbox
->GetId() );
38 wxArrayInt aSelections
;
39 int count
= listbox
->GetSelections(aSelections
);
42 event
.m_commandInt
= aSelections
[0] ;
43 event
.m_clientData
= listbox
->GetClientData(event
.m_commandInt
);
44 wxString
str(listbox
->GetString(event
.m_commandInt
));
46 event
.m_commandString
= copystring((char *)(const char *)str
);
50 event
.m_commandInt
= -1 ;
51 event
.m_commandString
= copystring("") ;
54 event
.SetEventObject( listbox
);
56 listbox
->GetEventHandler()->ProcessEvent( event
);
57 if (event
.m_commandString
) delete[] event
.m_commandString
;
60 //-----------------------------------------------------------------------------
62 //-----------------------------------------------------------------------------
64 IMPLEMENT_DYNAMIC_CLASS(wxListBox
,wxControl
)
66 wxListBox::wxListBox(void)
68 m_list
= (GtkList
*) NULL
;
71 bool wxListBox::Create( wxWindow
*parent
, wxWindowID id
,
72 const wxPoint
&pos
, const wxSize
&size
,
73 int n
, const wxString choices
[],
74 long style
, const wxValidator
& validator
, const wxString
&name
)
78 PreCreation( parent
, id
, pos
, size
, style
, name
);
80 SetValidator( validator
);
82 m_widget
= gtk_scrolled_window_new( (GtkAdjustment
*) NULL
, (GtkAdjustment
*) NULL
);
83 gtk_scrolled_window_set_policy( GTK_SCROLLED_WINDOW(m_widget
),
84 GTK_POLICY_AUTOMATIC
, GTK_POLICY_AUTOMATIC
);
86 m_list
= GTK_LIST( gtk_list_new() );
88 GtkSelectionMode mode
= GTK_SELECTION_BROWSE
;
89 if (style
& wxLB_MULTIPLE
)
90 mode
= GTK_SELECTION_MULTIPLE
;
91 else if (style
& wxLB_EXTENDED
)
92 mode
= GTK_SELECTION_EXTENDED
;
94 gtk_list_set_selection_mode( GTK_LIST(m_list
), mode
);
96 gtk_container_add (GTK_CONTAINER(m_widget
), GTK_WIDGET(m_list
) );
97 gtk_widget_show( GTK_WIDGET(m_list
) );
99 wxSize newSize
= size
;
100 if (newSize
.x
== -1) newSize
.x
= 100;
101 if (newSize
.y
== -1) newSize
.y
= 110;
102 SetSize( newSize
.x
, newSize
.y
);
104 for (int i
= 0; i
< n
; i
++)
106 GtkWidget
*list_item
;
107 list_item
= gtk_list_item_new_with_label( choices
[i
] );
109 gtk_container_add( GTK_CONTAINER(m_list
), list_item
);
111 gtk_signal_connect( GTK_OBJECT(list_item
), "select",
112 GTK_SIGNAL_FUNC(gtk_listitem_select_callback
), (gpointer
)this );
114 if (style
& wxLB_MULTIPLE
)
115 gtk_signal_connect( GTK_OBJECT(list_item
), "deselect",
116 GTK_SIGNAL_FUNC(gtk_listitem_select_callback
), (gpointer
)this );
118 m_clientData
.Append( (wxObject
*)NULL
);
120 gtk_widget_show( list_item
);
125 gtk_widget_realize( GTK_WIDGET(m_list
) );
132 void wxListBox::Append( const wxString
&item
)
134 Append( item
, (char*)NULL
);
137 void wxListBox::Append( const wxString
&item
, char *clientData
)
139 wxCHECK_RET( m_list
!= NULL
, "invalid list ctrl" );
141 GtkWidget
*list_item
= gtk_list_item_new_with_label( item
);
145 GtkBin
*bin
= GTK_BIN( list_item
);
146 gtk_widget_set_style( bin
->child
,
148 gtk_widget_get_style( GTK_WIDGET(m_list
) ) ) );
150 gtk_widget_set_style( GTK_WIDGET(bin
),
152 gtk_widget_get_style( GTK_WIDGET(m_list
) ) ) );
155 gtk_signal_connect( GTK_OBJECT(list_item
), "select",
156 GTK_SIGNAL_FUNC(gtk_listitem_select_callback
), (gpointer
)this );
158 if (GetWindowStyleFlag() & wxLB_MULTIPLE
)
159 gtk_signal_connect( GTK_OBJECT(list_item
), "deselect",
160 GTK_SIGNAL_FUNC(gtk_listitem_select_callback
), (gpointer
)this );
162 m_clientData
.Append( (wxObject
*)clientData
);
164 gtk_container_add( GTK_CONTAINER(m_list
), list_item
);
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 list ctrl" );
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 list ctrl" );
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 list ctrl" );
211 gtk_list_unselect_item( m_list
, n
);
214 int wxListBox::FindString( const wxString
&item
) const
216 wxCHECK_MSG( m_list
!= NULL
, -1, "invalid list ctrl" );
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 list ctrl" );
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 list ctrl" );
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 list ctrl" );
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 list ctrl" );
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 list ctrl" );
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 list ctrl" );
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 list ctrl" );
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 list ctrl" );
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 list ctrl" );
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 list ctrl" );
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 list ctrl" );
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 list ctrl" );
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 void wxListBox::SetFont( const wxFont
&font
)
457 wxCHECK_RET( m_list
!= NULL
, "invalid list ctrl" );
459 if (((wxFont
*)&font
)->Ok())
462 m_font
= *wxSWISS_FONT
;
464 GtkStyle
*style
= (GtkStyle
*) NULL
;
467 m_hasOwnStyle
= TRUE
;
468 style
= gtk_style_copy( gtk_widget_get_style( GTK_WIDGET(m_list
) ) );
472 style
= gtk_widget_get_style( GTK_WIDGET(m_list
) );
475 gdk_font_unref( style
->font
);
476 style
->font
= gdk_font_ref( m_font
.GetInternalFont( 1.0 ) );
478 gtk_widget_set_style( GTK_WIDGET(m_list
), style
);
481 GList
*child
= m_list
->children
;
484 GtkBin
*bin
= (GtkBin
*) child
->data
;
485 gtk_widget_set_style( bin
->child
,
487 gtk_widget_get_style( GTK_WIDGET(m_list
) ) ) );
492 bool wxListBox::IsOwnGtkWindow( GdkWindow
*window
)
494 if (wxWindow::IsOwnGtkWindow( window
)) return TRUE
;
496 GList
*child
= m_list
->children
;
499 GtkBin
*bin
= GTK_BIN( child
->data
);
500 if (bin
->child
->window
== window
) return TRUE
;
507 void wxListBox::SetBackgroundColour( const wxColour
&colour
)
509 wxCHECK_RET( m_list
!= NULL
, "invalid list ctrl" );
511 m_backgroundColour
= colour
;
512 if (!m_backgroundColour
.Ok()) return;
514 SetBackgroundColourHelper( GTK_WIDGET(m_list
)->window
);
516 GtkStyle
*style
= (GtkStyle
*) NULL
;
519 m_hasOwnStyle
= TRUE
;
520 style
= gtk_style_copy( gtk_widget_get_style( GTK_WIDGET(m_list
) ) );
524 style
= gtk_widget_get_style( GTK_WIDGET(m_list
) );
527 style
->base
[GTK_STATE_NORMAL
] = *m_backgroundColour
.GetColor();
528 style
->bg
[GTK_STATE_NORMAL
] = *m_backgroundColour
.GetColor();
530 gtk_widget_set_style( GTK_WIDGET(m_list
), style
);
532 GList
*child
= m_list
->children
;
535 GtkWidget
*item
= GTK_WIDGET(child
->data
);
536 gtk_widget_set_style( item
,
538 gtk_widget_get_style( GTK_WIDGET(m_list
) ) ) );