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"
21 #include "wx/gtk/private.h"
22 #include "wx/gtk/private/gtk2-compat.h"
24 // ----------------------------------------------------------------------------
26 // ----------------------------------------------------------------------------
31 gtk_choice_changed_callback( GtkWidget
*WXUNUSED(widget
), wxChoice
*choice
)
33 choice
->SendSelectionChangedEvent(wxEVT_COMMAND_CHOICE_SELECTED
);
38 //-----------------------------------------------------------------------------
40 //-----------------------------------------------------------------------------
45 m_stringCellIndex
= 0;
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
)
66 if (!PreCreation( parent
, pos
, size
) ||
67 !CreateBase( parent
, id
, pos
, size
, style
, validator
, name
))
69 wxFAIL_MSG( wxT("wxChoice creation failed") );
75 // if our m_strings != NULL, Append() will check for it and insert
76 // items in the correct order
77 m_strings
= new wxGtkCollatedArrayString
;
81 m_widget
= gtk_combo_box_text_new();
83 m_widget
= gtk_combo_box_new_text();
85 g_object_ref(m_widget
);
89 m_parent
->DoAddChild( this );
93 g_signal_connect_after (m_widget
, "changed",
94 G_CALLBACK (gtk_choice_changed_callback
), this);
104 void wxChoice::SendSelectionChangedEvent(wxEventType evt_type
)
109 if (GetSelection() == -1)
112 wxCommandEvent
event( evt_type
, GetId() );
114 int n
= GetSelection();
116 event
.SetString( GetStringSelection() );
117 event
.SetEventObject( this );
118 InitCommandEventWithItems( event
, n
);
120 HandleWindowEvent( event
);
123 void wxChoice::GTKInsertComboBoxTextItem( unsigned int n
, const wxString
& text
)
126 gtk_combo_box_text_insert_text(GTK_COMBO_BOX_TEXT(m_widget
), n
, wxGTK_CONV(text
));
128 gtk_combo_box_insert_text( GTK_COMBO_BOX( m_widget
), n
, wxGTK_CONV( text
) );
132 int wxChoice::DoInsertItems(const wxArrayStringsAdapter
& items
,
134 void **clientData
, wxClientDataType type
)
136 wxCHECK_MSG( m_widget
!= NULL
, -1, wxT("invalid control") );
138 wxASSERT_MSG( !IsSorted() || (pos
== GetCount()),
139 wxT("In a sorted choice data could only be appended"));
141 const int count
= items
.GetCount();
145 for ( int i
= 0; i
< count
; ++i
)
148 // If sorted, use this wxSortedArrayStrings to determine
149 // the right insertion point
151 n
= m_strings
->Add(items
[i
]);
153 GTKInsertComboBoxTextItem( n
, items
[i
] );
155 m_clientData
.Insert( NULL
, n
);
156 AssignNewItemClientData(n
, clientData
, i
, type
);
159 InvalidateBestSize();
164 void wxChoice::DoSetItemClientData(unsigned int n
, void* clientData
)
166 m_clientData
[n
] = clientData
;
169 void* wxChoice::DoGetItemClientData(unsigned int n
) const
171 return m_clientData
[n
];
174 void wxChoice::DoClear()
176 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid control") );
180 GtkComboBox
* combobox
= GTK_COMBO_BOX( m_widget
);
181 GtkTreeModel
* model
= gtk_combo_box_get_model( combobox
);
182 gtk_list_store_clear(GTK_LIST_STORE(model
));
184 m_clientData
.Clear();
191 InvalidateBestSize();
194 void wxChoice::DoDeleteOneItem(unsigned int n
)
196 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid control") );
197 wxCHECK_RET( IsValid(n
), wxT("invalid index in wxChoice::Delete") );
199 GtkComboBox
* combobox
= GTK_COMBO_BOX( m_widget
);
200 GtkTreeModel
* model
= gtk_combo_box_get_model( combobox
);
201 GtkListStore
* store
= GTK_LIST_STORE(model
);
203 gtk_tree_model_iter_nth_child( model
, &iter
,
205 gtk_list_store_remove( store
, &iter
);
207 m_clientData
.RemoveAt( n
);
209 m_strings
->RemoveAt( n
);
211 InvalidateBestSize();
214 int wxChoice::FindString( const wxString
&item
, bool bCase
) const
216 wxCHECK_MSG( m_widget
!= NULL
, wxNOT_FOUND
, wxT("invalid control") );
218 GtkComboBox
* combobox
= GTK_COMBO_BOX( m_widget
);
219 GtkTreeModel
* model
= gtk_combo_box_get_model( combobox
);
221 gtk_tree_model_get_iter_first( model
, &iter
);
222 if (!gtk_list_store_iter_is_valid(GTK_LIST_STORE(model
), &iter
))
227 GValue value
= { 0, };
228 gtk_tree_model_get_value( model
, &iter
, m_stringCellIndex
, &value
);
229 wxString str
= wxGTK_CONV_BACK( g_value_get_string( &value
) );
230 g_value_unset( &value
);
232 if (item
.IsSameAs( str
, bCase
) )
237 while ( gtk_tree_model_iter_next(model
, &iter
) );
242 int wxChoice::GetSelection() const
244 return gtk_combo_box_get_active( GTK_COMBO_BOX( m_widget
) );
247 void wxChoice::SetString(unsigned int n
, const wxString
&text
)
249 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid control") );
251 GtkComboBox
* combobox
= GTK_COMBO_BOX( m_widget
);
252 wxCHECK_RET( IsValid(n
), wxT("invalid index") );
254 GtkTreeModel
*model
= gtk_combo_box_get_model( combobox
);
256 if (gtk_tree_model_iter_nth_child (model
, &iter
, NULL
, n
))
258 GValue value
= { 0, };
259 g_value_init( &value
, G_TYPE_STRING
);
260 g_value_set_string( &value
, wxGTK_CONV( text
) );
261 gtk_list_store_set_value( GTK_LIST_STORE(model
), &iter
, m_stringCellIndex
, &value
);
262 g_value_unset( &value
);
265 InvalidateBestSize();
268 wxString
wxChoice::GetString(unsigned int n
) const
270 wxCHECK_MSG( m_widget
!= NULL
, wxEmptyString
, wxT("invalid control") );
274 GtkComboBox
* combobox
= GTK_COMBO_BOX( m_widget
);
275 GtkTreeModel
*model
= gtk_combo_box_get_model( combobox
);
277 if (gtk_tree_model_iter_nth_child (model
, &iter
, NULL
, n
))
279 GValue value
= { 0, };
280 gtk_tree_model_get_value( model
, &iter
, m_stringCellIndex
, &value
);
281 wxString tmp
= wxGTK_CONV_BACK( g_value_get_string( &value
) );
282 g_value_unset( &value
);
289 unsigned int wxChoice::GetCount() const
291 wxCHECK_MSG( m_widget
!= NULL
, 0, wxT("invalid control") );
293 GtkComboBox
* combobox
= GTK_COMBO_BOX( m_widget
);
294 GtkTreeModel
* model
= gtk_combo_box_get_model( combobox
);
296 gtk_tree_model_get_iter_first( model
, &iter
);
297 if (!gtk_list_store_iter_is_valid(GTK_LIST_STORE(model
), &iter
))
299 unsigned int ret
= 1;
300 while (gtk_tree_model_iter_next( model
, &iter
))
305 void wxChoice::SetSelection( int n
)
307 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid control") );
311 GtkComboBox
* combobox
= GTK_COMBO_BOX( m_widget
);
312 gtk_combo_box_set_active( combobox
, n
);
317 void wxChoice::SetColumns(int n
)
319 gtk_combo_box_set_wrap_width(GTK_COMBO_BOX(m_widget
), n
);
322 int wxChoice::GetColumns() const
324 // gtk_combo_box_get_wrap_width() was added in gtk 2.6
326 g_object_get(G_OBJECT(m_widget
), "wrap-width", &intval
, NULL
);
331 void wxChoice::GTKDisableEvents()
333 g_signal_handlers_block_by_func(m_widget
,
334 (gpointer
) gtk_choice_changed_callback
, this);
337 void wxChoice::GTKEnableEvents()
339 g_signal_handlers_unblock_by_func(m_widget
,
340 (gpointer
) gtk_choice_changed_callback
, this);
344 GdkWindow
*wxChoice::GTKGetWindow(wxArrayGdkWindows
& WXUNUSED(windows
)) const
346 return gtk_widget_get_window(m_widget
);
349 // Notice that this method shouldn't be necessary, because GTK calculates
350 // properly size of the combobox but for unknown reasons it doesn't work
351 // correctly in wx without this.
352 wxSize
wxChoice::DoGetBestSize() const
354 // strangely, this returns a width of 188 pixels from GTK+ (?)
355 wxSize
ret( wxControl::DoGetBestSize() );
357 // we know better our horizontal extent: it depends on the longest string
361 ret
.x
= GetCount() > 0 ? 0 : 60; // start with something "sensible"
363 unsigned int count
= GetCount();
364 for ( unsigned int n
= 0; n
< count
; n
++ )
366 GetTextExtent(GetString(n
), &width
, NULL
, NULL
, NULL
);
367 if ( width
+ 40 > ret
.x
) // 40 for drop down arrow and space around text
372 // empty combobox should have some reasonable default size too
373 if ((GetCount() == 0) && (ret
.x
< 80))
380 void wxChoice::DoApplyWidgetStyle(GtkRcStyle
*style
)
382 GTKApplyStyle(m_widget
, style
);
383 GTKApplyStyle(gtk_bin_get_child(GTK_BIN(m_widget
)), style
);
389 wxChoice::GetClassDefaultAttributes(wxWindowVariant
WXUNUSED(variant
))
391 return GetDefaultAttributesFromGTKWidget(gtk_combo_box_new
);
395 #endif // wxUSE_CHOICE || wxUSE_COMBOBOX