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::GTKInsertComboBoxTextItem( unsigned int n
, const wxString
& text
)
107 gtk_combo_box_text_insert_text(GTK_COMBO_BOX_TEXT(m_widget
), n
, wxGTK_CONV(text
));
109 gtk_combo_box_insert_text( GTK_COMBO_BOX( m_widget
), n
, wxGTK_CONV( text
) );
113 int wxChoice::DoInsertItems(const wxArrayStringsAdapter
& items
,
115 void **clientData
, wxClientDataType type
)
117 wxCHECK_MSG( m_widget
!= NULL
, -1, wxT("invalid control") );
119 wxASSERT_MSG( !IsSorted() || (pos
== GetCount()),
120 wxT("In a sorted choice data could only be appended"));
122 const int count
= items
.GetCount();
126 for ( int i
= 0; i
< count
; ++i
)
129 // If sorted, use this wxSortedArrayStrings to determine
130 // the right insertion point
132 n
= m_strings
->Add(items
[i
]);
134 GTKInsertComboBoxTextItem( n
, items
[i
] );
136 m_clientData
.Insert( NULL
, n
);
137 AssignNewItemClientData(n
, clientData
, i
, type
);
140 InvalidateBestSize();
145 void wxChoice::DoSetItemClientData(unsigned int n
, void* clientData
)
147 m_clientData
[n
] = clientData
;
150 void* wxChoice::DoGetItemClientData(unsigned int n
) const
152 return m_clientData
[n
];
155 void wxChoice::DoClear()
157 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid control") );
161 GtkComboBox
* combobox
= GTK_COMBO_BOX( m_widget
);
162 GtkTreeModel
* model
= gtk_combo_box_get_model( combobox
);
163 gtk_list_store_clear(GTK_LIST_STORE(model
));
165 m_clientData
.Clear();
172 InvalidateBestSize();
175 void wxChoice::DoDeleteOneItem(unsigned int n
)
177 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid control") );
178 wxCHECK_RET( IsValid(n
), wxT("invalid index in wxChoice::Delete") );
180 GtkComboBox
* combobox
= GTK_COMBO_BOX( m_widget
);
181 GtkTreeModel
* model
= gtk_combo_box_get_model( combobox
);
182 GtkListStore
* store
= GTK_LIST_STORE(model
);
184 gtk_tree_model_iter_nth_child( model
, &iter
,
186 gtk_list_store_remove( store
, &iter
);
188 m_clientData
.RemoveAt( n
);
190 m_strings
->RemoveAt( n
);
192 InvalidateBestSize();
195 int wxChoice::FindString( const wxString
&item
, bool bCase
) const
197 wxCHECK_MSG( m_widget
!= NULL
, wxNOT_FOUND
, wxT("invalid control") );
199 GtkComboBox
* combobox
= GTK_COMBO_BOX( m_widget
);
200 GtkTreeModel
* model
= gtk_combo_box_get_model( combobox
);
202 gtk_tree_model_get_iter_first( model
, &iter
);
203 if (!gtk_list_store_iter_is_valid(GTK_LIST_STORE(model
), &iter
))
208 GValue value
= { 0, };
209 gtk_tree_model_get_value( model
, &iter
, m_stringCellIndex
, &value
);
210 wxString str
= wxGTK_CONV_BACK( g_value_get_string( &value
) );
211 g_value_unset( &value
);
213 if (item
.IsSameAs( str
, bCase
) )
218 while ( gtk_tree_model_iter_next(model
, &iter
) );
223 int wxChoice::GetSelection() const
225 return gtk_combo_box_get_active( GTK_COMBO_BOX( m_widget
) );
228 void wxChoice::SetString(unsigned int n
, const wxString
&text
)
230 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid control") );
232 GtkComboBox
* combobox
= GTK_COMBO_BOX( m_widget
);
233 wxCHECK_RET( IsValid(n
), wxT("invalid index") );
235 GtkTreeModel
*model
= gtk_combo_box_get_model( combobox
);
237 if (gtk_tree_model_iter_nth_child (model
, &iter
, NULL
, n
))
239 GValue value
= { 0, };
240 g_value_init( &value
, G_TYPE_STRING
);
241 g_value_set_string( &value
, wxGTK_CONV( text
) );
242 gtk_list_store_set_value( GTK_LIST_STORE(model
), &iter
, m_stringCellIndex
, &value
);
243 g_value_unset( &value
);
246 InvalidateBestSize();
249 wxString
wxChoice::GetString(unsigned int n
) const
251 wxCHECK_MSG( m_widget
!= NULL
, wxEmptyString
, wxT("invalid control") );
255 GtkComboBox
* combobox
= GTK_COMBO_BOX( m_widget
);
256 GtkTreeModel
*model
= gtk_combo_box_get_model( combobox
);
258 if (gtk_tree_model_iter_nth_child (model
, &iter
, NULL
, n
))
260 GValue value
= { 0, };
261 gtk_tree_model_get_value( model
, &iter
, m_stringCellIndex
, &value
);
262 wxString tmp
= wxGTK_CONV_BACK( g_value_get_string( &value
) );
263 g_value_unset( &value
);
270 unsigned int wxChoice::GetCount() const
272 wxCHECK_MSG( m_widget
!= NULL
, 0, wxT("invalid control") );
274 GtkComboBox
* combobox
= GTK_COMBO_BOX( m_widget
);
275 GtkTreeModel
* model
= gtk_combo_box_get_model( combobox
);
277 gtk_tree_model_get_iter_first( model
, &iter
);
278 if (!gtk_list_store_iter_is_valid(GTK_LIST_STORE(model
), &iter
))
280 unsigned int ret
= 1;
281 while (gtk_tree_model_iter_next( model
, &iter
))
286 void wxChoice::SetSelection( int n
)
288 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid control") );
292 GtkComboBox
* combobox
= GTK_COMBO_BOX( m_widget
);
293 gtk_combo_box_set_active( combobox
, n
);
298 void wxChoice::SetColumns(int n
)
300 gtk_combo_box_set_wrap_width(GTK_COMBO_BOX(m_widget
), n
);
303 int wxChoice::GetColumns() const
305 // gtk_combo_box_get_wrap_width() was added in gtk 2.6
307 g_object_get(G_OBJECT(m_widget
), "wrap-width", &intval
, NULL
);
311 void wxChoice::GTKDisableEvents()
313 g_signal_handlers_block_by_func(m_widget
,
314 (gpointer
) gtk_choice_changed_callback
, this);
317 void wxChoice::GTKEnableEvents()
319 g_signal_handlers_unblock_by_func(m_widget
,
320 (gpointer
) gtk_choice_changed_callback
, this);
323 GdkWindow
*wxChoice::GTKGetWindow(wxArrayGdkWindows
& WXUNUSED(windows
)) const
325 return gtk_widget_get_window(m_widget
);
328 wxSize
wxChoice::DoGetBestSize() const
330 // Get the height of the control from GTK+ itself, but use our own version
331 // to compute the width large enough to show all our strings as GTK+
332 // doesn't seem to take the control contents into account.
333 return GetSizeFromTextSize(wxChoiceBase::DoGetBestSize().x
);
336 wxSize
wxChoice::DoGetSizeFromTextSize(int xlen
, int ylen
) const
338 wxASSERT_MSG( m_widget
, wxS("GetSizeFromTextSize called before creation") );
340 // a GtkEntry for wxComboBox and a GtkCellView for wxChoice
341 GtkWidget
* childPart
= gtk_bin_get_child(GTK_BIN(m_widget
));
343 // Set a as small as possible size for the control, so preferred sizes
344 // return "natural" sizes, not taking into account the previous ones (which
345 // seems to be GTK+3 behaviour)
346 gtk_widget_set_size_request(m_widget
, 0, 0);
348 // We are interested in the difference of sizes between the whole contol
349 // and its child part. I.e. arrow, separators, etc.
351 gtk_widget_get_preferred_size(childPart
, NULL
, &req
);
352 wxSize totalS
= GTKGetPreferredSize(m_widget
);
354 wxSize
tsize(xlen
+ totalS
.x
- req
.width
, totalS
.y
);
356 // For a wxChoice, not for wxComboBox, add some margins
357 if ( !GTK_IS_ENTRY(childPart
) )
360 // Perhaps the user wants something different from CharHeight
362 tsize
.IncBy(0, ylen
- GetCharHeight());
367 void wxChoice::DoApplyWidgetStyle(GtkRcStyle
*style
)
369 GTKApplyStyle(m_widget
, style
);
370 GTKApplyStyle(gtk_bin_get_child(GTK_BIN(m_widget
)), style
);
375 wxChoice::GetClassDefaultAttributes(wxWindowVariant
WXUNUSED(variant
))
377 return GetDefaultAttributesFromGTKWidget(gtk_combo_box_new());
380 #endif // wxUSE_CHOICE || wxUSE_COMBOBOX