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
)
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 void wxChoice::GTKInsertComboBoxTextItem( unsigned int n
, const wxString
& text
)
123 gtk_combo_box_text_insert_text(GTK_COMBO_BOX_TEXT(m_widget
), n
, wxGTK_CONV(text
));
125 gtk_combo_box_insert_text( GTK_COMBO_BOX( m_widget
), n
, wxGTK_CONV( text
) );
129 int wxChoice::DoInsertItems(const wxArrayStringsAdapter
& items
,
131 void **clientData
, wxClientDataType type
)
133 wxCHECK_MSG( m_widget
!= NULL
, -1, wxT("invalid control") );
135 wxASSERT_MSG( !IsSorted() || (pos
== GetCount()),
136 wxT("In a sorted choice data could only be appended"));
138 const int count
= items
.GetCount();
142 for ( int i
= 0; i
< count
; ++i
)
145 // If sorted, use this wxSortedArrayStrings to determine
146 // the right insertion point
148 n
= m_strings
->Add(items
[i
]);
150 GTKInsertComboBoxTextItem( n
, items
[i
] );
152 m_clientData
.Insert( NULL
, n
);
153 AssignNewItemClientData(n
, clientData
, i
, type
);
156 InvalidateBestSize();
161 void wxChoice::DoSetItemClientData(unsigned int n
, void* clientData
)
163 m_clientData
[n
] = clientData
;
166 void* wxChoice::DoGetItemClientData(unsigned int n
) const
168 return m_clientData
[n
];
171 void wxChoice::DoClear()
173 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid control") );
177 GtkComboBox
* combobox
= GTK_COMBO_BOX( m_widget
);
178 GtkTreeModel
* model
= gtk_combo_box_get_model( combobox
);
179 gtk_list_store_clear(GTK_LIST_STORE(model
));
181 m_clientData
.Clear();
188 InvalidateBestSize();
191 void wxChoice::DoDeleteOneItem(unsigned int n
)
193 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid control") );
194 wxCHECK_RET( IsValid(n
), wxT("invalid index in wxChoice::Delete") );
196 GtkComboBox
* combobox
= GTK_COMBO_BOX( m_widget
);
197 GtkTreeModel
* model
= gtk_combo_box_get_model( combobox
);
198 GtkListStore
* store
= GTK_LIST_STORE(model
);
200 gtk_tree_model_iter_nth_child( model
, &iter
,
202 gtk_list_store_remove( store
, &iter
);
204 m_clientData
.RemoveAt( n
);
206 m_strings
->RemoveAt( n
);
208 InvalidateBestSize();
211 int wxChoice::FindString( const wxString
&item
, bool bCase
) const
213 wxCHECK_MSG( m_widget
!= NULL
, wxNOT_FOUND
, wxT("invalid control") );
215 GtkComboBox
* combobox
= GTK_COMBO_BOX( m_widget
);
216 GtkTreeModel
* model
= gtk_combo_box_get_model( combobox
);
218 gtk_tree_model_get_iter_first( model
, &iter
);
219 if (!gtk_list_store_iter_is_valid(GTK_LIST_STORE(model
), &iter
))
224 GValue value
= { 0, };
225 gtk_tree_model_get_value( model
, &iter
, m_stringCellIndex
, &value
);
226 wxString str
= wxGTK_CONV_BACK( g_value_get_string( &value
) );
227 g_value_unset( &value
);
229 if (item
.IsSameAs( str
, bCase
) )
234 while ( gtk_tree_model_iter_next(model
, &iter
) );
239 int wxChoice::GetSelection() const
241 return gtk_combo_box_get_active( GTK_COMBO_BOX( m_widget
) );
244 void wxChoice::SetString(unsigned int n
, const wxString
&text
)
246 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid control") );
248 GtkComboBox
* combobox
= GTK_COMBO_BOX( m_widget
);
249 wxCHECK_RET( IsValid(n
), wxT("invalid index") );
251 GtkTreeModel
*model
= gtk_combo_box_get_model( combobox
);
253 if (gtk_tree_model_iter_nth_child (model
, &iter
, NULL
, n
))
255 GValue value
= { 0, };
256 g_value_init( &value
, G_TYPE_STRING
);
257 g_value_set_string( &value
, wxGTK_CONV( text
) );
258 gtk_list_store_set_value( GTK_LIST_STORE(model
), &iter
, m_stringCellIndex
, &value
);
259 g_value_unset( &value
);
262 InvalidateBestSize();
265 wxString
wxChoice::GetString(unsigned int n
) const
267 wxCHECK_MSG( m_widget
!= NULL
, wxEmptyString
, wxT("invalid control") );
271 GtkComboBox
* combobox
= GTK_COMBO_BOX( m_widget
);
272 GtkTreeModel
*model
= gtk_combo_box_get_model( combobox
);
274 if (gtk_tree_model_iter_nth_child (model
, &iter
, NULL
, n
))
276 GValue value
= { 0, };
277 gtk_tree_model_get_value( model
, &iter
, m_stringCellIndex
, &value
);
278 wxString tmp
= wxGTK_CONV_BACK( g_value_get_string( &value
) );
279 g_value_unset( &value
);
286 unsigned int wxChoice::GetCount() const
288 wxCHECK_MSG( m_widget
!= NULL
, 0, wxT("invalid control") );
290 GtkComboBox
* combobox
= GTK_COMBO_BOX( m_widget
);
291 GtkTreeModel
* model
= gtk_combo_box_get_model( combobox
);
293 gtk_tree_model_get_iter_first( model
, &iter
);
294 if (!gtk_list_store_iter_is_valid(GTK_LIST_STORE(model
), &iter
))
296 unsigned int ret
= 1;
297 while (gtk_tree_model_iter_next( model
, &iter
))
302 void wxChoice::SetSelection( int n
)
304 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid control") );
308 GtkComboBox
* combobox
= GTK_COMBO_BOX( m_widget
);
309 gtk_combo_box_set_active( combobox
, n
);
314 void wxChoice::SetColumns(int n
)
316 gtk_combo_box_set_wrap_width(GTK_COMBO_BOX(m_widget
), n
);
319 int wxChoice::GetColumns() const
321 // gtk_combo_box_get_wrap_width() was added in gtk 2.6
323 g_object_get(G_OBJECT(m_widget
), "wrap-width", &intval
, NULL
);
328 void wxChoice::GTKDisableEvents()
330 g_signal_handlers_block_by_func(m_widget
,
331 (gpointer
) gtk_choice_changed_callback
, this);
334 void wxChoice::GTKEnableEvents()
336 g_signal_handlers_unblock_by_func(m_widget
,
337 (gpointer
) gtk_choice_changed_callback
, this);
341 GdkWindow
*wxChoice::GTKGetWindow(wxArrayGdkWindows
& WXUNUSED(windows
)) const
343 return gtk_widget_get_window(m_widget
);
346 wxSize
wxChoice::DoGetBestSize() const
348 // Get the height of the control from GTK+ itself, but use our own version
349 // to compute the width large enough to show all our strings as GTK+
350 // doesn't seem to take the control contents into account.
351 return GetSizeFromTextSize(wxChoiceBase::DoGetBestSize().x
);
354 wxSize
wxChoice::DoGetSizeFromTextSize(int xlen
, int ylen
) const
356 wxASSERT_MSG( m_widget
, wxS("GetSizeFromTextSize called before creation") );
358 // a GtkEntry for wxComboBox and a GtkCellView for wxChoice
359 GtkWidget
* childPart
= gtk_bin_get_child(GTK_BIN(m_widget
));
361 // Set a as small as possible size for the control, so preferred sizes
362 // return "natural" sizes, not taking into account the previous ones (which
363 // seems to be GTK+3 behaviour)
364 gtk_widget_set_size_request(m_widget
, 0, 0);
366 // We are interested in the difference of sizes between the whole contol
367 // and its child part. I.e. arrow, separators, etc.
369 gtk_widget_size_request(childPart
, &req
);
370 wxSize totalS
= GTKGetPreferredSize(m_widget
);
372 wxSize
tsize(xlen
+ totalS
.x
- req
.width
, totalS
.y
);
374 // For a wxChoice, not for wxComboBox, add some margins
375 if ( !GTK_IS_ENTRY(childPart
) )
378 // Perhaps the user wants something different from CharHeight
380 tsize
.IncBy(0, ylen
- GetCharHeight());
385 void wxChoice::DoApplyWidgetStyle(GtkRcStyle
*style
)
387 GTKApplyStyle(m_widget
, style
);
388 GTKApplyStyle(gtk_bin_get_child(GTK_BIN(m_widget
)), style
);
394 wxChoice::GetClassDefaultAttributes(wxWindowVariant
WXUNUSED(variant
))
396 return GetDefaultAttributesFromGTKWidget(gtk_combo_box_new
);
400 #endif // wxUSE_CHOICE || wxUSE_COMBOBOX