support for GTK3
[wxWidgets.git] / src / gtk / choice.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/gtk/choice.cpp
3 // Purpose:
4 // Author: Robert Roebling
5 // Id: $Id$
6 // Copyright: (c) 1998 Robert Roebling
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
9
10 #include "wx/wxprec.h"
11
12 #if wxUSE_CHOICE || wxUSE_COMBOBOX
13
14 #include "wx/choice.h"
15
16 #ifndef WX_PRECOMP
17 #include "wx/arrstr.h"
18 #endif
19
20 #include <gtk/gtk.h>
21 #include "wx/gtk/private.h"
22 #include "wx/gtk/private/gtk2-compat.h"
23
24 // ----------------------------------------------------------------------------
25 // GTK callbacks
26 // ----------------------------------------------------------------------------
27
28 extern "C" {
29
30 static void
31 gtk_choice_changed_callback( GtkWidget *WXUNUSED(widget), wxChoice *choice )
32 {
33 choice->SendSelectionChangedEvent(wxEVT_COMMAND_CHOICE_SELECTED);
34 }
35
36 }
37
38 //-----------------------------------------------------------------------------
39 // wxChoice
40 //-----------------------------------------------------------------------------
41
42 void wxChoice::Init()
43 {
44 m_strings = NULL;
45 m_stringCellIndex = 0;
46 }
47
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 )
53 {
54 wxCArrayString chs(choices);
55
56 return Create( parent, id, pos, size, chs.GetCount(), chs.GetStrings(),
57 style, validator, name );
58 }
59
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 )
65 {
66 if (!PreCreation( parent, pos, size ) ||
67 !CreateBase( parent, id, pos, size, style, validator, name ))
68 {
69 wxFAIL_MSG( wxT("wxChoice creation failed") );
70 return false;
71 }
72
73 if ( IsSorted() )
74 {
75 // if our m_strings != NULL, Append() will check for it and insert
76 // items in the correct order
77 m_strings = new wxGtkCollatedArrayString;
78 }
79
80 #ifdef __WXGTK3__
81 m_widget = gtk_combo_box_text_new();
82 #else
83 m_widget = gtk_combo_box_new_text();
84 #endif
85 g_object_ref(m_widget);
86
87 Append(n, choices);
88
89 m_parent->DoAddChild( this );
90
91 PostCreation(size);
92
93 g_signal_connect_after (m_widget, "changed",
94 G_CALLBACK (gtk_choice_changed_callback), this);
95
96 return true;
97 }
98
99 wxChoice::~wxChoice()
100 {
101 delete m_strings;
102 }
103
104 void wxChoice::SendSelectionChangedEvent(wxEventType evt_type)
105 {
106 if (!m_hasVMT)
107 return;
108
109 if (GetSelection() == -1)
110 return;
111
112 wxCommandEvent event( evt_type, GetId() );
113
114 int n = GetSelection();
115 event.SetInt( n );
116 event.SetString( GetStringSelection() );
117 event.SetEventObject( this );
118 InitCommandEventWithItems( event, n );
119
120 HandleWindowEvent( event );
121 }
122
123 void wxChoice::GTKInsertComboBoxTextItem( unsigned int n, const wxString& text )
124 {
125 #ifdef __WXGTK3__
126 gtk_combo_box_text_insert_text(GTK_COMBO_BOX_TEXT(m_widget), n, wxGTK_CONV(text));
127 #else
128 gtk_combo_box_insert_text( GTK_COMBO_BOX( m_widget ), n, wxGTK_CONV( text ) );
129 #endif
130 }
131
132 int wxChoice::DoInsertItems(const wxArrayStringsAdapter & items,
133 unsigned int pos,
134 void **clientData, wxClientDataType type)
135 {
136 wxCHECK_MSG( m_widget != NULL, -1, wxT("invalid control") );
137
138 wxASSERT_MSG( !IsSorted() || (pos == GetCount()),
139 wxT("In a sorted choice data could only be appended"));
140
141 const int count = items.GetCount();
142
143 int n = wxNOT_FOUND;
144
145 for ( int i = 0; i < count; ++i )
146 {
147 n = pos + i;
148 // If sorted, use this wxSortedArrayStrings to determine
149 // the right insertion point
150 if (m_strings)
151 n = m_strings->Add(items[i]);
152
153 GTKInsertComboBoxTextItem( n, items[i] );
154
155 m_clientData.Insert( NULL, n );
156 AssignNewItemClientData(n, clientData, i, type);
157 }
158
159 InvalidateBestSize();
160
161 return n;
162 }
163
164 void wxChoice::DoSetItemClientData(unsigned int n, void* clientData)
165 {
166 m_clientData[n] = clientData;
167 }
168
169 void* wxChoice::DoGetItemClientData(unsigned int n) const
170 {
171 return m_clientData[n];
172 }
173
174 void wxChoice::DoClear()
175 {
176 wxCHECK_RET( m_widget != NULL, wxT("invalid control") );
177
178 GTKDisableEvents();
179
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));
183
184 m_clientData.Clear();
185
186 if (m_strings)
187 m_strings->Clear();
188
189 GTKEnableEvents();
190
191 InvalidateBestSize();
192 }
193
194 void wxChoice::DoDeleteOneItem(unsigned int n)
195 {
196 wxCHECK_RET( m_widget != NULL, wxT("invalid control") );
197 wxCHECK_RET( IsValid(n), wxT("invalid index in wxChoice::Delete") );
198
199 GtkComboBox* combobox = GTK_COMBO_BOX( m_widget );
200 GtkTreeModel* model = gtk_combo_box_get_model( combobox );
201 GtkListStore* store = GTK_LIST_STORE(model);
202 GtkTreeIter iter;
203 gtk_tree_model_iter_nth_child( model, &iter,
204 NULL, (gint) n );
205 gtk_list_store_remove( store, &iter );
206
207 m_clientData.RemoveAt( n );
208 if ( m_strings )
209 m_strings->RemoveAt( n );
210
211 InvalidateBestSize();
212 }
213
214 int wxChoice::FindString( const wxString &item, bool bCase ) const
215 {
216 wxCHECK_MSG( m_widget != NULL, wxNOT_FOUND, wxT("invalid control") );
217
218 GtkComboBox* combobox = GTK_COMBO_BOX( m_widget );
219 GtkTreeModel* model = gtk_combo_box_get_model( combobox );
220 GtkTreeIter iter;
221 gtk_tree_model_get_iter_first( model, &iter );
222 if (!gtk_list_store_iter_is_valid(GTK_LIST_STORE(model), &iter ))
223 return -1;
224 int count = 0;
225 do
226 {
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 );
231
232 if (item.IsSameAs( str, bCase ) )
233 return count;
234
235 count++;
236 }
237 while ( gtk_tree_model_iter_next(model, &iter) );
238
239 return wxNOT_FOUND;
240 }
241
242 int wxChoice::GetSelection() const
243 {
244 return gtk_combo_box_get_active( GTK_COMBO_BOX( m_widget ) );
245 }
246
247 void wxChoice::SetString(unsigned int n, const wxString &text)
248 {
249 wxCHECK_RET( m_widget != NULL, wxT("invalid control") );
250
251 GtkComboBox* combobox = GTK_COMBO_BOX( m_widget );
252 wxCHECK_RET( IsValid(n), wxT("invalid index") );
253
254 GtkTreeModel *model = gtk_combo_box_get_model( combobox );
255 GtkTreeIter iter;
256 if (gtk_tree_model_iter_nth_child (model, &iter, NULL, n))
257 {
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 );
263 }
264
265 InvalidateBestSize();
266 }
267
268 wxString wxChoice::GetString(unsigned int n) const
269 {
270 wxCHECK_MSG( m_widget != NULL, wxEmptyString, wxT("invalid control") );
271
272 wxString str;
273
274 GtkComboBox* combobox = GTK_COMBO_BOX( m_widget );
275 GtkTreeModel *model = gtk_combo_box_get_model( combobox );
276 GtkTreeIter iter;
277 if (gtk_tree_model_iter_nth_child (model, &iter, NULL, n))
278 {
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 );
283 return tmp;
284 }
285
286 return str;
287 }
288
289 unsigned int wxChoice::GetCount() const
290 {
291 wxCHECK_MSG( m_widget != NULL, 0, wxT("invalid control") );
292
293 GtkComboBox* combobox = GTK_COMBO_BOX( m_widget );
294 GtkTreeModel* model = gtk_combo_box_get_model( combobox );
295 GtkTreeIter iter;
296 gtk_tree_model_get_iter_first( model, &iter );
297 if (!gtk_list_store_iter_is_valid(GTK_LIST_STORE(model), &iter ))
298 return 0;
299 unsigned int ret = 1;
300 while (gtk_tree_model_iter_next( model, &iter ))
301 ret++;
302 return ret;
303 }
304
305 void wxChoice::SetSelection( int n )
306 {
307 wxCHECK_RET( m_widget != NULL, wxT("invalid control") );
308
309 GTKDisableEvents();
310
311 GtkComboBox* combobox = GTK_COMBO_BOX( m_widget );
312 gtk_combo_box_set_active( combobox, n );
313
314 GTKEnableEvents();
315 }
316
317 void wxChoice::SetColumns(int n)
318 {
319 gtk_combo_box_set_wrap_width(GTK_COMBO_BOX(m_widget), n);
320 }
321
322 int wxChoice::GetColumns() const
323 {
324 // gtk_combo_box_get_wrap_width() was added in gtk 2.6
325 gint intval;
326 g_object_get(G_OBJECT(m_widget), "wrap-width", &intval, NULL);
327 return intval;
328 }
329
330
331 void wxChoice::GTKDisableEvents()
332 {
333 g_signal_handlers_block_by_func(m_widget,
334 (gpointer) gtk_choice_changed_callback, this);
335 }
336
337 void wxChoice::GTKEnableEvents()
338 {
339 g_signal_handlers_unblock_by_func(m_widget,
340 (gpointer) gtk_choice_changed_callback, this);
341 }
342
343
344 GdkWindow *wxChoice::GTKGetWindow(wxArrayGdkWindows& WXUNUSED(windows)) const
345 {
346 return gtk_widget_get_window(m_widget);
347 }
348
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
353 {
354 // strangely, this returns a width of 188 pixels from GTK+ (?)
355 wxSize ret( wxControl::DoGetBestSize() );
356
357 // we know better our horizontal extent: it depends on the longest string
358 // in the combobox
359 if ( m_widget )
360 {
361 ret.x = GetCount() > 0 ? 0 : 60; // start with something "sensible"
362 int width;
363 unsigned int count = GetCount();
364 for ( unsigned int n = 0; n < count; n++ )
365 {
366 GetTextExtent(GetString(n), &width, NULL, NULL, NULL );
367 if ( width + 40 > ret.x ) // 40 for drop down arrow and space around text
368 ret.x = width + 40;
369 }
370 }
371
372 // empty combobox should have some reasonable default size too
373 if ((GetCount() == 0) && (ret.x < 80))
374 ret.x = 80;
375
376 CacheBestSize(ret);
377 return ret;
378 }
379
380 void wxChoice::DoApplyWidgetStyle(GtkRcStyle *style)
381 {
382 GTKApplyStyle(m_widget, style);
383 GTKApplyStyle(gtk_bin_get_child(GTK_BIN(m_widget)), style);
384 }
385
386
387 // static
388 wxVisualAttributes
389 wxChoice::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant))
390 {
391 return GetDefaultAttributesFromGTKWidget(gtk_combo_box_new);
392 }
393
394
395 #endif // wxUSE_CHOICE || wxUSE_COMBOBOX