1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/gtk/choice.cpp
4 // Author: Robert Roebling
6 // Copyright: (c) 1998 Robert Roebling
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
10 #include "wx/wxprec.h"
12 #if wxUSE_CHOICE || wxUSE_COMBOBOX
14 #include "wx/choice.h"
17 #include "wx/arrstr.h"
20 #include "wx/gtk/private.h"
23 // ----------------------------------------------------------------------------
25 // ----------------------------------------------------------------------------
30 gtk_choice_changed_callback( GtkWidget
*WXUNUSED(widget
), wxChoice
*choice
)
32 choice
->SendSelectionChangedEvent(wxEVT_COMMAND_CHOICE_SELECTED
);
37 //-----------------------------------------------------------------------------
39 //-----------------------------------------------------------------------------
41 IMPLEMENT_DYNAMIC_CLASS(wxChoice
, wxControlWithItems
)
48 bool wxChoice::Create( wxWindow
*parent
, wxWindowID id
,
49 const wxPoint
&pos
, const wxSize
&size
,
50 const wxArrayString
& choices
,
51 long style
, const wxValidator
& validator
,
52 const wxString
&name
)
54 wxCArrayString
chs(choices
);
56 return Create( parent
, id
, pos
, size
, chs
.GetCount(), chs
.GetStrings(),
57 style
, validator
, name
);
60 bool wxChoice::Create( wxWindow
*parent
, wxWindowID id
,
61 const wxPoint
&pos
, const wxSize
&size
,
62 int n
, const wxString choices
[],
63 long style
, const wxValidator
& validator
,
64 const wxString
&name
)
68 if (!PreCreation( parent
, pos
, size
) ||
69 !CreateBase( parent
, id
, pos
, size
, style
, validator
, name
))
71 wxFAIL_MSG( wxT("wxChoice creation failed") );
77 // if our m_strings != NULL, Append() will check for it and insert
78 // items in the correct order
79 m_strings
= new wxSortedArrayString
;
82 m_widget
= gtk_combo_box_new_text();
86 m_parent
->DoAddChild( this );
90 g_signal_connect_after (m_widget
, "changed",
91 G_CALLBACK (gtk_choice_changed_callback
), this);
101 void wxChoice::SendSelectionChangedEvent(wxEventType evt_type
)
106 if (GetSelection() == -1)
109 wxCommandEvent
event( evt_type
, GetId() );
111 int n
= GetSelection();
113 event
.SetString( GetStringSelection() );
114 event
.SetEventObject( this );
115 InitCommandEventWithItems( event
, n
);
117 HandleWindowEvent( event
);
120 int wxChoice::DoInsertItems(const wxArrayStringsAdapter
& items
,
122 void **clientData
, wxClientDataType type
)
124 wxCHECK_MSG( m_widget
!= NULL
, -1, wxT("invalid control") );
126 wxASSERT_MSG( !IsSorted() || (pos
== GetCount()),
127 _T("In a sorted choice data could only be appended"));
129 const int count
= items
.GetCount();
133 GtkComboBox
* combobox
= GTK_COMBO_BOX( m_widget
);
134 for ( int i
= 0; i
< count
; ++i
)
137 // If sorted, use this wxSortedArrayStrings to determine
138 // the right insertion point
140 n
= m_strings
->Add(items
[i
]);
142 gtk_combo_box_insert_text( combobox
, n
, wxGTK_CONV( items
[i
] ) );
144 m_clientData
.Insert( NULL
, n
);
145 AssignNewItemClientData(n
, clientData
, i
, type
);
148 InvalidateBestSize();
153 void wxChoice::DoSetItemClientData(unsigned int n
, void* clientData
)
155 m_clientData
[n
] = clientData
;
158 void* wxChoice::DoGetItemClientData(unsigned int n
) const
160 return m_clientData
[n
];
163 void wxChoice::DoClear()
165 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid control") );
169 const unsigned int count
= GetCount();
170 for (unsigned int i
= 0; i
< count
; i
++)
171 gtk_combo_box_remove_text( GTK_COMBO_BOX(m_widget
), 0 );
173 m_clientData
.Clear();
180 InvalidateBestSize();
183 void wxChoice::DoDeleteOneItem(unsigned int n
)
185 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid control") );
186 wxCHECK_RET( IsValid(n
), _T("invalid index in wxChoice::Delete") );
188 gtk_combo_box_remove_text( GTK_COMBO_BOX( m_widget
), n
);
189 m_clientData
.RemoveAt( n
);
191 m_strings
->RemoveAt( n
);
193 InvalidateBestSize();
196 int wxChoice::FindString( const wxString
&item
, bool bCase
) const
198 wxCHECK_MSG( m_widget
!= NULL
, wxNOT_FOUND
, wxT("invalid control") );
200 GtkComboBox
* combobox
= GTK_COMBO_BOX( m_widget
);
201 GtkTreeModel
* model
= gtk_combo_box_get_model( combobox
);
203 gtk_tree_model_get_iter_first( model
, &iter
);
204 if (!gtk_list_store_iter_is_valid(GTK_LIST_STORE(model
), &iter
))
209 GValue value
= { 0, };
210 gtk_tree_model_get_value( model
, &iter
, 0, &value
);
211 wxString str
= wxGTK_CONV_BACK( g_value_get_string( &value
) );
212 g_value_unset( &value
);
214 if (item
.IsSameAs( str
, bCase
) )
219 while ( gtk_tree_model_iter_next(model
, &iter
) );
224 int wxChoice::GetSelection() const
226 return gtk_combo_box_get_active( GTK_COMBO_BOX( m_widget
) );
229 void wxChoice::SetString(unsigned int n
, const wxString
&text
)
231 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid control") );
233 GtkComboBox
* combobox
= GTK_COMBO_BOX( m_widget
);
234 wxCHECK_RET( IsValid(n
), wxT("invalid index") );
236 GtkTreeModel
*model
= gtk_combo_box_get_model( combobox
);
238 if (gtk_tree_model_iter_nth_child (model
, &iter
, NULL
, n
))
240 GValue value
= { 0, };
241 g_value_init( &value
, G_TYPE_STRING
);
242 g_value_set_string( &value
, wxGTK_CONV( text
) );
243 gtk_list_store_set_value( GTK_LIST_STORE(model
), &iter
, 0, &value
);
244 g_value_unset( &value
);
247 InvalidateBestSize();
250 wxString
wxChoice::GetString(unsigned int n
) const
252 wxCHECK_MSG( m_widget
!= NULL
, wxEmptyString
, wxT("invalid control") );
256 GtkComboBox
* combobox
= GTK_COMBO_BOX( m_widget
);
257 GtkTreeModel
*model
= gtk_combo_box_get_model( combobox
);
259 if (gtk_tree_model_iter_nth_child (model
, &iter
, NULL
, n
))
261 GValue value
= { 0, };
262 gtk_tree_model_get_value( model
, &iter
, 0, &value
);
263 wxString tmp
= wxGTK_CONV_BACK( g_value_get_string( &value
) );
264 g_value_unset( &value
);
271 unsigned int wxChoice::GetCount() const
273 wxCHECK_MSG( m_widget
!= NULL
, 0, wxT("invalid control") );
275 GtkComboBox
* combobox
= GTK_COMBO_BOX( m_widget
);
276 GtkTreeModel
* model
= gtk_combo_box_get_model( combobox
);
278 gtk_tree_model_get_iter_first( model
, &iter
);
279 if (!gtk_list_store_iter_is_valid(GTK_LIST_STORE(model
), &iter
))
281 unsigned int ret
= 1;
282 while (gtk_tree_model_iter_next( model
, &iter
))
287 void wxChoice::SetSelection( int n
)
289 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid control") );
293 GtkComboBox
* combobox
= GTK_COMBO_BOX( m_widget
);
294 gtk_combo_box_set_active( combobox
, n
);
299 void wxChoice::DisableEvents()
301 g_signal_handlers_block_by_func(m_widget
,
302 (gpointer
) gtk_choice_changed_callback
, this);
305 void wxChoice::EnableEvents()
307 g_signal_handlers_unblock_by_func(m_widget
,
308 (gpointer
) gtk_choice_changed_callback
, this);
312 GdkWindow
*wxChoice::GTKGetWindow(wxArrayGdkWindows
& WXUNUSED(windows
)) const
314 return m_widget
->window
;
319 wxChoice::GetClassDefaultAttributes(wxWindowVariant
WXUNUSED(variant
))
321 return GetDefaultAttributesFromGTKWidget(gtk_combo_box_new
);
325 #endif // wxUSE_CHOICE || wxUSE_COMBOBOX