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 //-----------------------------------------------------------------------------
44 m_stringCellIndex
= 0;
47 bool wxChoice::Create( wxWindow
*parent
, wxWindowID id
,
48 const wxPoint
&pos
, const wxSize
&size
,
49 const wxArrayString
& choices
,
50 long style
, const wxValidator
& validator
,
51 const wxString
&name
)
53 wxCArrayString
chs(choices
);
55 return Create( parent
, id
, pos
, size
, chs
.GetCount(), chs
.GetStrings(),
56 style
, validator
, name
);
59 bool wxChoice::Create( wxWindow
*parent
, wxWindowID id
,
60 const wxPoint
&pos
, const wxSize
&size
,
61 int n
, const wxString choices
[],
62 long style
, const wxValidator
& validator
,
63 const wxString
&name
)
65 if (!PreCreation( parent
, pos
, size
) ||
66 !CreateBase( parent
, id
, pos
, size
, style
, validator
, name
))
68 wxFAIL_MSG( wxT("wxChoice creation failed") );
74 // if our m_strings != NULL, Append() will check for it and insert
75 // items in the correct order
76 m_strings
= new wxGtkCollatedArrayString
;
79 m_widget
= gtk_combo_box_new_text();
80 g_object_ref(m_widget
);
84 m_parent
->DoAddChild( this );
88 g_signal_connect_after (m_widget
, "changed",
89 G_CALLBACK (gtk_choice_changed_callback
), this);
99 void wxChoice::SendSelectionChangedEvent(wxEventType evt_type
)
104 if (GetSelection() == -1)
107 wxCommandEvent
event( evt_type
, GetId() );
109 int n
= GetSelection();
111 event
.SetString( GetStringSelection() );
112 event
.SetEventObject( this );
113 InitCommandEventWithItems( event
, n
);
115 HandleWindowEvent( event
);
118 void wxChoice::GTKInsertComboBoxTextItem( unsigned int n
, const wxString
& text
)
120 gtk_combo_box_insert_text( GTK_COMBO_BOX( m_widget
), n
, wxGTK_CONV( text
) );
123 int wxChoice::DoInsertItems(const wxArrayStringsAdapter
& items
,
125 void **clientData
, wxClientDataType type
)
127 wxCHECK_MSG( m_widget
!= NULL
, -1, wxT("invalid control") );
129 wxASSERT_MSG( !IsSorted() || (pos
== GetCount()),
130 wxT("In a sorted choice data could only be appended"));
132 const int count
= items
.GetCount();
136 for ( int i
= 0; i
< count
; ++i
)
139 // If sorted, use this wxSortedArrayStrings to determine
140 // the right insertion point
142 n
= m_strings
->Add(items
[i
]);
144 GTKInsertComboBoxTextItem( n
, items
[i
] );
146 m_clientData
.Insert( NULL
, n
);
147 AssignNewItemClientData(n
, clientData
, i
, type
);
150 InvalidateBestSize();
155 void wxChoice::DoSetItemClientData(unsigned int n
, void* clientData
)
157 m_clientData
[n
] = clientData
;
160 void* wxChoice::DoGetItemClientData(unsigned int n
) const
162 return m_clientData
[n
];
165 void wxChoice::DoClear()
167 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid control") );
171 GtkComboBox
* combobox
= GTK_COMBO_BOX( m_widget
);
172 GtkTreeModel
* model
= gtk_combo_box_get_model( combobox
);
173 gtk_list_store_clear(GTK_LIST_STORE(model
));
175 m_clientData
.Clear();
182 InvalidateBestSize();
185 void wxChoice::DoDeleteOneItem(unsigned int n
)
187 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid control") );
188 wxCHECK_RET( IsValid(n
), wxT("invalid index in wxChoice::Delete") );
190 GtkComboBox
* combobox
= GTK_COMBO_BOX( m_widget
);
191 GtkTreeModel
* model
= gtk_combo_box_get_model( combobox
);
192 GtkListStore
* store
= GTK_LIST_STORE(model
);
194 gtk_tree_model_iter_nth_child( model
, &iter
,
196 gtk_list_store_remove( store
, &iter
);
198 m_clientData
.RemoveAt( n
);
200 m_strings
->RemoveAt( n
);
202 InvalidateBestSize();
205 int wxChoice::FindString( const wxString
&item
, bool bCase
) const
207 wxCHECK_MSG( m_widget
!= NULL
, wxNOT_FOUND
, wxT("invalid control") );
209 GtkComboBox
* combobox
= GTK_COMBO_BOX( m_widget
);
210 GtkTreeModel
* model
= gtk_combo_box_get_model( combobox
);
212 gtk_tree_model_get_iter_first( model
, &iter
);
213 if (!gtk_list_store_iter_is_valid(GTK_LIST_STORE(model
), &iter
))
218 GValue value
= { 0, };
219 gtk_tree_model_get_value( model
, &iter
, m_stringCellIndex
, &value
);
220 wxString str
= wxGTK_CONV_BACK( g_value_get_string( &value
) );
221 g_value_unset( &value
);
223 if (item
.IsSameAs( str
, bCase
) )
228 while ( gtk_tree_model_iter_next(model
, &iter
) );
233 int wxChoice::GetSelection() const
235 return gtk_combo_box_get_active( GTK_COMBO_BOX( m_widget
) );
238 void wxChoice::SetString(unsigned int n
, const wxString
&text
)
240 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid control") );
242 GtkComboBox
* combobox
= GTK_COMBO_BOX( m_widget
);
243 wxCHECK_RET( IsValid(n
), wxT("invalid index") );
245 GtkTreeModel
*model
= gtk_combo_box_get_model( combobox
);
247 if (gtk_tree_model_iter_nth_child (model
, &iter
, NULL
, n
))
249 GValue value
= { 0, };
250 g_value_init( &value
, G_TYPE_STRING
);
251 g_value_set_string( &value
, wxGTK_CONV( text
) );
252 gtk_list_store_set_value( GTK_LIST_STORE(model
), &iter
, m_stringCellIndex
, &value
);
253 g_value_unset( &value
);
256 InvalidateBestSize();
259 wxString
wxChoice::GetString(unsigned int n
) const
261 wxCHECK_MSG( m_widget
!= NULL
, wxEmptyString
, wxT("invalid control") );
265 GtkComboBox
* combobox
= GTK_COMBO_BOX( m_widget
);
266 GtkTreeModel
*model
= gtk_combo_box_get_model( combobox
);
268 if (gtk_tree_model_iter_nth_child (model
, &iter
, NULL
, n
))
270 GValue value
= { 0, };
271 gtk_tree_model_get_value( model
, &iter
, m_stringCellIndex
, &value
);
272 wxString tmp
= wxGTK_CONV_BACK( g_value_get_string( &value
) );
273 g_value_unset( &value
);
280 unsigned int wxChoice::GetCount() const
282 wxCHECK_MSG( m_widget
!= NULL
, 0, wxT("invalid control") );
284 GtkComboBox
* combobox
= GTK_COMBO_BOX( m_widget
);
285 GtkTreeModel
* model
= gtk_combo_box_get_model( combobox
);
287 gtk_tree_model_get_iter_first( model
, &iter
);
288 if (!gtk_list_store_iter_is_valid(GTK_LIST_STORE(model
), &iter
))
290 unsigned int ret
= 1;
291 while (gtk_tree_model_iter_next( model
, &iter
))
296 void wxChoice::SetSelection( int n
)
298 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid control") );
302 GtkComboBox
* combobox
= GTK_COMBO_BOX( m_widget
);
303 gtk_combo_box_set_active( combobox
, n
);
308 void wxChoice::SetColumns(int n
)
310 gtk_combo_box_set_wrap_width(GTK_COMBO_BOX(m_widget
), n
);
313 int wxChoice::GetColumns() const
315 // gtk_combo_box_get_wrap_width() was added in gtk 2.6
317 g_object_get(G_OBJECT(m_widget
), "wrap-width", &intval
, NULL
);
322 void wxChoice::GTKDisableEvents()
324 g_signal_handlers_block_by_func(m_widget
,
325 (gpointer
) gtk_choice_changed_callback
, this);
328 void wxChoice::GTKEnableEvents()
330 g_signal_handlers_unblock_by_func(m_widget
,
331 (gpointer
) gtk_choice_changed_callback
, this);
335 GdkWindow
*wxChoice::GTKGetWindow(wxArrayGdkWindows
& WXUNUSED(windows
)) const
337 return m_widget
->window
;
340 // Notice that this method shouldn't be necessary, because GTK calculates
341 // properly size of the combobox but for unknown reasons it doesn't work
342 // correctly in wx without this.
343 wxSize
wxChoice::DoGetBestSize() const
345 // strangely, this returns a width of 188 pixels from GTK+ (?)
346 wxSize
ret( wxControl::DoGetBestSize() );
348 // we know better our horizontal extent: it depends on the longest string
352 ret
.x
= GetCount() > 0 ? 0 : 60; // start with something "sensible"
354 unsigned int count
= GetCount();
355 for ( unsigned int n
= 0; n
< count
; n
++ )
357 GetTextExtent(GetString(n
), &width
, NULL
, NULL
, NULL
);
358 if ( width
+ 40 > ret
.x
) // 40 for drop down arrow and space around text
363 // empty combobox should have some reasonable default size too
364 if ((GetCount() == 0) && (ret
.x
< 80))
371 void wxChoice::DoApplyWidgetStyle(GtkRcStyle
*style
)
373 gtk_widget_modify_style(m_widget
, style
);
374 gtk_widget_modify_style(GTK_BIN(m_widget
)->child
, style
);
380 wxChoice::GetClassDefaultAttributes(wxWindowVariant
WXUNUSED(variant
))
382 return GetDefaultAttributesFromGTKWidget(gtk_combo_box_new
);
386 #endif // wxUSE_CHOICE || wxUSE_COMBOBOX