1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/gtk/choice.cpp
3 // Purpose:
4 // Author: Robert Roebling
5 // Copyright: (c) 1998 Robert Roebling
6 // Licence: wxWindows licence
7 /////////////////////////////////////////////////////////////////////////////
8
9 #include "wx/wxprec.h"
10
11 #if wxUSE_CHOICE || wxUSE_COMBOBOX
12
13 #include "wx/choice.h"
14
15 #ifndef WX_PRECOMP
16 #include "wx/arrstr.h"
17 #endif
18
19 #include <gtk/gtk.h>
20 #include "wx/gtk/private.h"
21 #include "wx/gtk/private/gtk2-compat.h"
22
23 // ----------------------------------------------------------------------------
24 // GTK callbacks
25 // ----------------------------------------------------------------------------
26
27 extern "C" {
28
29 static void
gtk_choice_changed_callback(GtkWidget * WXUNUSED (widget),wxChoice * choice)30 gtk_choice_changed_callback( GtkWidget *WXUNUSED(widget), wxChoice *choice )
31 {
32 choice->SendSelectionChangedEvent(wxEVT_CHOICE);
33 }
34
35 }
36
37 //-----------------------------------------------------------------------------
38 // wxChoice
39 //-----------------------------------------------------------------------------
40
Init()41 void wxChoice::Init()
42 {
43 m_strings = NULL;
44 m_stringCellIndex = 0;
45 }
46
Create(wxWindow * parent,wxWindowID id,const wxPoint & pos,const wxSize & size,const wxArrayString & choices,long style,const wxValidator & validator,const wxString & name)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 )
52 {
53 wxCArrayString chs(choices);
54
55 return Create( parent, id, pos, size, chs.GetCount(), chs.GetStrings(),
56 style, validator, name );
57 }
58
Create(wxWindow * parent,wxWindowID id,const wxPoint & pos,const wxSize & size,int n,const wxString choices[],long style,const wxValidator & validator,const wxString & 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 )
64 {
65 if (!PreCreation( parent, pos, size ) ||
66 !CreateBase( parent, id, pos, size, style, validator, name ))
67 {
68 wxFAIL_MSG( wxT("wxChoice creation failed") );
69 return false;
70 }
71
72 if ( IsSorted() )
73 {
74 // if our m_strings != NULL, Append() will check for it and insert
75 // items in the correct order
76 m_strings = new wxGtkCollatedArrayString;
77 }
78
79 #ifdef __WXGTK3__
80 m_widget = gtk_combo_box_text_new();
81 #else
82 m_widget = gtk_combo_box_new_text();
83 #endif
84 g_object_ref(m_widget);
85
86 Append(n, choices);
87
88 m_parent->DoAddChild( this );
89
90 PostCreation(size);
91
92 g_signal_connect_after (m_widget, "changed",
93 G_CALLBACK (gtk_choice_changed_callback), this);
94
95 return true;
96 }
97
~wxChoice()98 wxChoice::~wxChoice()
99 {
100 Clear();
101 delete m_strings;
102
103 #ifdef __WXGTK3__
104 // At least with GTK+ 3.22.9, destroying a shown combobox widget results in
105 // a Gtk-CRITICAL debug message when the assertion fails inside a signal
106 // handler called from gtk_widget_unrealize(), which is annoying, so avoid
107 // it by hiding the widget before destroying it -- this doesn't look right,
108 // but shouldn't do any harm neither.
109 Hide();
110 #endif // __WXGTK3__
111 }
112
GTKInsertComboBoxTextItem(unsigned int n,const wxString & text)113 void wxChoice::GTKInsertComboBoxTextItem( unsigned int n, const wxString& text )
114 {
115 #ifdef __WXGTK3__
116 gtk_combo_box_text_insert_text(GTK_COMBO_BOX_TEXT(m_widget), n, wxGTK_CONV(text));
117 #else
118 gtk_combo_box_insert_text( GTK_COMBO_BOX( m_widget ), n, wxGTK_CONV( text ) );
119 #endif
120 }
121
DoInsertItems(const wxArrayStringsAdapter & items,unsigned int pos,void ** clientData,wxClientDataType type)122 int wxChoice::DoInsertItems(const wxArrayStringsAdapter & items,
123 unsigned int pos,
124 void **clientData, wxClientDataType type)
125 {
126 wxCHECK_MSG( m_widget != NULL, -1, wxT("invalid control") );
127
128 wxASSERT_MSG( !IsSorted() || (pos == GetCount()),
129 wxT("In a sorted choice data could only be appended"));
130
131 const int count = items.GetCount();
132
133 int n = wxNOT_FOUND;
134
135 for ( int i = 0; i < count; ++i )
136 {
137 n = pos + i;
138 // If sorted, use this wxSortedArrayStrings to determine
139 // the right insertion point
140 if (m_strings)
141 n = m_strings->Add(items[i]);
142
143 GTKInsertComboBoxTextItem( n, items[i] );
144
145 m_clientData.Insert( NULL, n );
146 AssignNewItemClientData(n, clientData, i, type);
147 }
148
149 InvalidateBestSize();
150
151 return n;
152 }
153
DoSetItemClientData(unsigned int n,void * clientData)154 void wxChoice::DoSetItemClientData(unsigned int n, void* clientData)
155 {
156 m_clientData[n] = clientData;
157 }
158
DoGetItemClientData(unsigned int n) const159 void* wxChoice::DoGetItemClientData(unsigned int n) const
160 {
161 return m_clientData[n];
162 }
163
DoClear()164 void wxChoice::DoClear()
165 {
166 wxCHECK_RET( m_widget != NULL, wxT("invalid control") );
167
168 GTKDisableEvents();
169
170 GtkComboBox* combobox = GTK_COMBO_BOX( m_widget );
171 GtkTreeModel* model = gtk_combo_box_get_model( combobox );
172 if (model)
173 gtk_list_store_clear(GTK_LIST_STORE(model));
174
175 m_clientData.Clear();
176
177 if (m_strings)
178 m_strings->Clear();
179
180 GTKEnableEvents();
181
182 InvalidateBestSize();
183 }
184
DoDeleteOneItem(unsigned int n)185 void wxChoice::DoDeleteOneItem(unsigned int n)
186 {
187 wxCHECK_RET( m_widget != NULL, wxT("invalid control") );
188 wxCHECK_RET( IsValid(n), wxT("invalid index in wxChoice::Delete") );
189
190 GtkComboBox* combobox = GTK_COMBO_BOX( m_widget );
191 GtkTreeModel* model = gtk_combo_box_get_model( combobox );
192 GtkListStore* store = GTK_LIST_STORE(model);
193 GtkTreeIter iter;
194 if ( !gtk_tree_model_iter_nth_child(model, &iter, NULL, n) )
195 {
196 // This is really not supposed to happen for a valid index.
197 wxFAIL_MSG(wxS("Item unexpectedly not found."));
198 return;
199 }
200 gtk_list_store_remove( store, &iter );
201
202 m_clientData.RemoveAt( n );
203 if ( m_strings )
204 m_strings->RemoveAt( n );
205
206 InvalidateBestSize();
207 }
208
FindString(const wxString & item,bool bCase) const209 int wxChoice::FindString( const wxString &item, bool bCase ) const
210 {
211 wxCHECK_MSG( m_widget != NULL, wxNOT_FOUND, wxT("invalid control") );
212
213 GtkComboBox* combobox = GTK_COMBO_BOX( m_widget );
214 GtkTreeModel* model = gtk_combo_box_get_model( combobox );
215 GtkTreeIter iter;
216 gtk_tree_model_get_iter_first( model, &iter );
217 if (!gtk_list_store_iter_is_valid(GTK_LIST_STORE(model), &iter ))
218 return -1;
219 int count = 0;
220 do
221 {
222 GValue value = G_VALUE_INIT;
223 gtk_tree_model_get_value( model, &iter, m_stringCellIndex, &value );
224 wxString str = wxGTK_CONV_BACK( g_value_get_string( &value ) );
225 g_value_unset( &value );
226
227 if (item.IsSameAs( str, bCase ) )
228 return count;
229
230 count++;
231 }
232 while ( gtk_tree_model_iter_next(model, &iter) );
233
234 return wxNOT_FOUND;
235 }
236
GetSelection() const237 int wxChoice::GetSelection() const
238 {
239 return gtk_combo_box_get_active( GTK_COMBO_BOX( m_widget ) );
240 }
241
SetString(unsigned int n,const wxString & text)242 void wxChoice::SetString(unsigned int n, const wxString &text)
243 {
244 wxCHECK_RET( m_widget != NULL, wxT("invalid control") );
245
246 GtkComboBox* combobox = GTK_COMBO_BOX( m_widget );
247 wxCHECK_RET( IsValid(n), wxT("invalid index") );
248
249 GtkTreeModel *model = gtk_combo_box_get_model( combobox );
250 GtkTreeIter iter;
251 if (gtk_tree_model_iter_nth_child (model, &iter, NULL, n))
252 {
253 GValue value = G_VALUE_INIT;
254 g_value_init( &value, G_TYPE_STRING );
255 g_value_set_string( &value, wxGTK_CONV( text ) );
256 gtk_list_store_set_value( GTK_LIST_STORE(model), &iter, m_stringCellIndex, &value );
257 g_value_unset( &value );
258 }
259
260 InvalidateBestSize();
261 }
262
GetString(unsigned int n) const263 wxString wxChoice::GetString(unsigned int n) const
264 {
265 wxCHECK_MSG( m_widget != NULL, wxEmptyString, wxT("invalid control") );
266
267 wxString str;
268
269 GtkComboBox* combobox = GTK_COMBO_BOX( m_widget );
270 GtkTreeModel *model = gtk_combo_box_get_model( combobox );
271 GtkTreeIter iter;
272 if (gtk_tree_model_iter_nth_child (model, &iter, NULL, n))
273 {
274 GValue value = G_VALUE_INIT;
275 gtk_tree_model_get_value( model, &iter, m_stringCellIndex, &value );
276 wxString tmp = wxGTK_CONV_BACK( g_value_get_string( &value ) );
277 g_value_unset( &value );
278 return tmp;
279 }
280
281 return str;
282 }
283
GetCount() const284 unsigned int wxChoice::GetCount() const
285 {
286 wxCHECK_MSG( m_widget != NULL, 0, wxT("invalid control") );
287
288 GtkComboBox* combobox = GTK_COMBO_BOX( m_widget );
289 GtkTreeModel* model = gtk_combo_box_get_model( combobox );
290 GtkTreeIter iter;
291 gtk_tree_model_get_iter_first( model, &iter );
292 if (!gtk_list_store_iter_is_valid(GTK_LIST_STORE(model), &iter ))
293 return 0;
294 unsigned int ret = 1;
295 while (gtk_tree_model_iter_next( model, &iter ))
296 ret++;
297 return ret;
298 }
299
SetSelection(int n)300 void wxChoice::SetSelection( int n )
301 {
302 wxCHECK_RET( m_widget != NULL, wxT("invalid control") );
303
304 GTKDisableEvents();
305
306 GtkComboBox* combobox = GTK_COMBO_BOX( m_widget );
307 gtk_combo_box_set_active( combobox, n );
308
309 GTKEnableEvents();
310 }
311
SetColumns(int n)312 void wxChoice::SetColumns(int n)
313 {
314 gtk_combo_box_set_wrap_width(GTK_COMBO_BOX(m_widget), n);
315 }
316
GetColumns() const317 int wxChoice::GetColumns() const
318 {
319 return gtk_combo_box_get_wrap_width(GTK_COMBO_BOX(m_widget));
320 }
321
GTKDisableEvents()322 void wxChoice::GTKDisableEvents()
323 {
324 g_signal_handlers_block_by_func(m_widget,
325 (gpointer) gtk_choice_changed_callback, this);
326 }
327
GTKEnableEvents()328 void wxChoice::GTKEnableEvents()
329 {
330 g_signal_handlers_unblock_by_func(m_widget,
331 (gpointer) gtk_choice_changed_callback, this);
332 }
333
GTKGetWindow(wxArrayGdkWindows & WXUNUSED (windows)) const334 GdkWindow *wxChoice::GTKGetWindow(wxArrayGdkWindows& WXUNUSED(windows)) const
335 {
336 return gtk_widget_get_window(m_widget);
337 }
338
DoGetBestSize() const339 wxSize wxChoice::DoGetBestSize() const
340 {
341 // Get the height of the control from GTK+ itself, but use our own version
342 // to compute the width large enough to show all our strings as GTK+
343 // doesn't seem to take the control contents into account.
344 return GetSizeFromTextSize(wxChoiceBase::DoGetBestSize().x);
345 }
346
DoGetSizeFromTextSize(int xlen,int ylen) const347 wxSize wxChoice::DoGetSizeFromTextSize(int xlen, int ylen) const
348 {
349 wxASSERT_MSG( m_widget, wxS("GetSizeFromTextSize called before creation") );
350
351 // a GtkEntry for wxComboBox and a GtkCellView for wxChoice
352 GtkWidget* childPart = gtk_bin_get_child(GTK_BIN(m_widget));
353
354 // Set a as small as possible size for the control, so preferred sizes
355 // return "natural" sizes, not taking into account the previous ones (which
356 // seems to be GTK+3 behaviour)
357 gtk_widget_set_size_request(m_widget, 0, 0);
358
359 // We are interested in the difference of sizes between the whole contol
360 // and its child part. I.e. arrow, separators, etc.
361 GtkRequisition req;
362 gtk_widget_get_preferred_size(childPart, NULL, &req);
363 wxSize totalS = GTKGetPreferredSize(m_widget);
364
365 wxSize tsize(xlen + totalS.x - req.width, totalS.y);
366
367 // For a wxChoice, not for wxComboBox, add some margins
368 if ( !GTK_IS_ENTRY(childPart) )
369 tsize.IncBy(5, 0);
370
371 // Perhaps the user wants something different from CharHeight
372 if ( ylen > 0 )
373 tsize.IncBy(0, ylen - GetCharHeight());
374
375 return tsize;
376 }
377
DoApplyWidgetStyle(GtkRcStyle * style)378 void wxChoice::DoApplyWidgetStyle(GtkRcStyle *style)
379 {
380 GTKApplyStyle(m_widget, style);
381 GTKApplyStyle(gtk_bin_get_child(GTK_BIN(m_widget)), style);
382 }
383
384 // static
385 wxVisualAttributes
GetClassDefaultAttributes(wxWindowVariant WXUNUSED (variant))386 wxChoice::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant))
387 {
388 return GetDefaultAttributesFromGTKWidget(gtk_combo_box_new());
389 }
390
391 #endif // wxUSE_CHOICE || wxUSE_COMBOBOX
392