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
)
45 m_strings
= (wxSortedArrayString
*)NULL
;
46 m_stringCellIndex
= 0;
49 bool wxChoice::Create( wxWindow
*parent
, wxWindowID id
,
50 const wxPoint
&pos
, const wxSize
&size
,
51 const wxArrayString
& choices
,
52 long style
, const wxValidator
& validator
,
53 const wxString
&name
)
55 wxCArrayString
chs(choices
);
57 return Create( parent
, id
, pos
, size
, chs
.GetCount(), chs
.GetStrings(),
58 style
, validator
, name
);
61 bool wxChoice::Create( wxWindow
*parent
, wxWindowID id
,
62 const wxPoint
&pos
, const wxSize
&size
,
63 int n
, const wxString choices
[],
64 long style
, const wxValidator
& validator
,
65 const wxString
&name
)
67 if (!PreCreation( parent
, pos
, size
) ||
68 !CreateBase( parent
, id
, pos
, size
, style
, validator
, name
))
70 wxFAIL_MSG( wxT("wxChoice creation failed") );
76 // if our m_strings != NULL, Append() will check for it and insert
77 // items in the correct order
78 m_strings
= new wxSortedArrayString
;
81 m_widget
= gtk_combo_box_new_text();
85 m_parent
->DoAddChild( this );
89 g_signal_connect_after (m_widget
, "changed",
90 G_CALLBACK (gtk_choice_changed_callback
), this);
100 void wxChoice::SendSelectionChangedEvent(wxEventType evt_type
)
105 if (GetSelection() == -1)
108 wxCommandEvent
event( evt_type
, GetId() );
110 int n
= GetSelection();
112 event
.SetString( GetStringSelection() );
113 event
.SetEventObject( this );
114 InitCommandEventWithItems( event
, n
);
116 HandleWindowEvent( event
);
119 void wxChoice::GTKInsertComboBoxTextItem( unsigned int n
, const wxString
& text
)
121 gtk_combo_box_insert_text( GTK_COMBO_BOX( m_widget
), n
, wxGTK_CONV( text
) );
124 int wxChoice::DoInsertItems(const wxArrayStringsAdapter
& items
,
126 void **clientData
, wxClientDataType type
)
128 wxCHECK_MSG( m_widget
!= NULL
, -1, wxT("invalid control") );
130 wxASSERT_MSG( !IsSorted() || (pos
== GetCount()),
131 _T("In a sorted choice data could only be appended"));
133 const int count
= items
.GetCount();
137 for ( int i
= 0; i
< count
; ++i
)
140 // If sorted, use this wxSortedArrayStrings to determine
141 // the right insertion point
143 n
= m_strings
->Add(items
[i
]);
145 GTKInsertComboBoxTextItem( n
, items
[i
] );
147 m_clientData
.Insert( NULL
, n
);
148 AssignNewItemClientData(n
, clientData
, i
, type
);
151 InvalidateBestSize();
156 void wxChoice::DoSetItemClientData(unsigned int n
, void* clientData
)
158 m_clientData
[n
] = clientData
;
161 void* wxChoice::DoGetItemClientData(unsigned int n
) const
163 return m_clientData
[n
];
166 void wxChoice::DoClear()
168 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid control") );
172 GtkComboBox
* combobox
= GTK_COMBO_BOX( m_widget
);
173 GtkTreeModel
* model
= gtk_combo_box_get_model( combobox
);
174 gtk_list_store_clear(GTK_LIST_STORE(model
));
176 m_clientData
.Clear();
183 InvalidateBestSize();
186 void wxChoice::DoDeleteOneItem(unsigned int n
)
188 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid control") );
189 wxCHECK_RET( IsValid(n
), _T("invalid index in wxChoice::Delete") );
191 GtkComboBox
* combobox
= GTK_COMBO_BOX( m_widget
);
192 GtkTreeModel
* model
= gtk_combo_box_get_model( combobox
);
193 GtkListStore
* store
= GTK_LIST_STORE(model
);
195 gtk_tree_model_iter_nth_child( model
, &iter
,
197 gtk_list_store_remove( store
, &iter
);
199 m_clientData
.RemoveAt( n
);
201 m_strings
->RemoveAt( n
);
203 InvalidateBestSize();
206 int wxChoice::FindString( const wxString
&item
, bool bCase
) const
208 wxCHECK_MSG( m_widget
!= NULL
, wxNOT_FOUND
, wxT("invalid control") );
210 GtkComboBox
* combobox
= GTK_COMBO_BOX( m_widget
);
211 GtkTreeModel
* model
= gtk_combo_box_get_model( combobox
);
213 gtk_tree_model_get_iter_first( model
, &iter
);
214 if (!gtk_list_store_iter_is_valid(GTK_LIST_STORE(model
), &iter
))
219 GValue value
= { 0, };
220 gtk_tree_model_get_value( model
, &iter
, m_stringCellIndex
, &value
);
221 wxString str
= wxGTK_CONV_BACK( g_value_get_string( &value
) );
222 g_value_unset( &value
);
224 if (item
.IsSameAs( str
, bCase
) )
229 while ( gtk_tree_model_iter_next(model
, &iter
) );
234 int wxChoice::GetSelection() const
236 return gtk_combo_box_get_active( GTK_COMBO_BOX( m_widget
) );
239 void wxChoice::SetString(unsigned int n
, const wxString
&text
)
241 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid control") );
243 GtkComboBox
* combobox
= GTK_COMBO_BOX( m_widget
);
244 wxCHECK_RET( IsValid(n
), wxT("invalid index") );
246 GtkTreeModel
*model
= gtk_combo_box_get_model( combobox
);
248 if (gtk_tree_model_iter_nth_child (model
, &iter
, NULL
, n
))
250 GValue value
= { 0, };
251 g_value_init( &value
, G_TYPE_STRING
);
252 g_value_set_string( &value
, wxGTK_CONV( text
) );
253 gtk_list_store_set_value( GTK_LIST_STORE(model
), &iter
, m_stringCellIndex
, &value
);
254 g_value_unset( &value
);
257 InvalidateBestSize();
260 wxString
wxChoice::GetString(unsigned int n
) const
262 wxCHECK_MSG( m_widget
!= NULL
, wxEmptyString
, wxT("invalid control") );
266 GtkComboBox
* combobox
= GTK_COMBO_BOX( m_widget
);
267 GtkTreeModel
*model
= gtk_combo_box_get_model( combobox
);
269 if (gtk_tree_model_iter_nth_child (model
, &iter
, NULL
, n
))
271 GValue value
= { 0, };
272 gtk_tree_model_get_value( model
, &iter
, m_stringCellIndex
, &value
);
273 wxString tmp
= wxGTK_CONV_BACK( g_value_get_string( &value
) );
274 g_value_unset( &value
);
281 unsigned int wxChoice::GetCount() const
283 wxCHECK_MSG( m_widget
!= NULL
, 0, wxT("invalid control") );
285 GtkComboBox
* combobox
= GTK_COMBO_BOX( m_widget
);
286 GtkTreeModel
* model
= gtk_combo_box_get_model( combobox
);
288 gtk_tree_model_get_iter_first( model
, &iter
);
289 if (!gtk_list_store_iter_is_valid(GTK_LIST_STORE(model
), &iter
))
291 unsigned int ret
= 1;
292 while (gtk_tree_model_iter_next( model
, &iter
))
297 void wxChoice::SetSelection( int n
)
299 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid control") );
303 GtkComboBox
* combobox
= GTK_COMBO_BOX( m_widget
);
304 gtk_combo_box_set_active( combobox
, n
);
309 void wxChoice::DisableEvents()
311 g_signal_handlers_block_by_func(m_widget
,
312 (gpointer
) gtk_choice_changed_callback
, this);
315 void wxChoice::EnableEvents()
317 g_signal_handlers_unblock_by_func(m_widget
,
318 (gpointer
) gtk_choice_changed_callback
, this);
322 GdkWindow
*wxChoice::GTKGetWindow(wxArrayGdkWindows
& WXUNUSED(windows
)) const
324 return m_widget
->window
;
327 // Notice that this method shouldn't be necessary, because GTK calculates
328 // properly size of the combobox but for unknown reasons it doesn't work
329 // correctly in wx without this.
330 wxSize
wxChoice::DoGetBestSize() const
332 // strangely, this returns a width of 188 pixels from GTK+ (?)
333 wxSize
ret( wxControl::DoGetBestSize() );
335 // we know better our horizontal extent: it depends on the longest string
339 ret
.x
= 60; // start with something "sensible"
341 unsigned int count
= GetCount();
342 for ( unsigned int n
= 0; n
< count
; n
++ )
344 GetTextExtent(GetString(n
), &width
, NULL
, NULL
, NULL
);
345 if ( width
+ 40 > ret
.x
) // 40 for drop down arrow and space around text
350 // empty combobox should have some reasonable default size too
351 if ((GetCount() == 0) && (ret
.x
< 80))
360 wxChoice::GetClassDefaultAttributes(wxWindowVariant
WXUNUSED(variant
))
362 return GetDefaultAttributesFromGTKWidget(gtk_combo_box_new
);
366 #endif // wxUSE_CHOICE || wxUSE_COMBOBOX