use GtkComboBox instead of deprecated GtkOptionMenu for wxChoice; this also allows...
[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 "wx/gtk/private.h"
21
22
23 // ----------------------------------------------------------------------------
24 // GTK callbacks
25 // ----------------------------------------------------------------------------
26
27 extern "C" {
28
29 static void
30 gtk_choice_changed_callback( GtkWidget *WXUNUSED(widget), wxChoice *choice )
31 {
32 choice->SendSelectionChangedEvent(wxEVT_COMMAND_CHOICE_SELECTED);
33 }
34
35 }
36
37 //-----------------------------------------------------------------------------
38 // wxChoice
39 //-----------------------------------------------------------------------------
40
41 IMPLEMENT_DYNAMIC_CLASS(wxChoice, wxControlWithItems)
42
43 wxChoice::wxChoice()
44 : m_strings(NULL)
45 {
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 m_strings = NULL;
67
68 if (!PreCreation( parent, pos, size ) ||
69 !CreateBase( parent, id, pos, size, style, validator, name ))
70 {
71 wxFAIL_MSG( wxT("wxChoice creation failed") );
72 return false;
73 }
74
75 if ( IsSorted() )
76 {
77 // if our m_strings != NULL, Append() will check for it and insert
78 // items in the correct order
79 m_strings = new wxSortedArrayString;
80 }
81
82 m_widget = gtk_combo_box_new_text();
83
84 Append(n, choices);
85
86 m_parent->DoAddChild( this );
87
88 PostCreation(size);
89
90 g_signal_connect_after (m_widget, "changed",
91 G_CALLBACK (gtk_choice_changed_callback), this);
92
93 return true;
94 }
95
96 wxChoice::~wxChoice()
97 {
98 delete m_strings;
99 }
100
101 void wxChoice::SendSelectionChangedEvent(wxEventType evt_type)
102 {
103 if (!m_hasVMT)
104 return;
105
106 if (GetSelection() == -1)
107 return;
108
109 wxCommandEvent event( evt_type, GetId() );
110
111 int n = GetSelection();
112 event.SetInt( n );
113 event.SetString( GetStringSelection() );
114 event.SetEventObject( this );
115 InitCommandEventWithItems( event, n );
116
117 HandleWindowEvent( event );
118 }
119
120 int wxChoice::DoInsertItems(const wxArrayStringsAdapter & items,
121 unsigned int pos,
122 void **clientData, wxClientDataType type)
123 {
124 wxCHECK_MSG( m_widget != NULL, -1, wxT("invalid control") );
125
126 wxASSERT_MSG( !IsSorted() || (pos == GetCount()),
127 _T("In a sorted choice data could only be appended"));
128
129 const int count = items.GetCount();
130
131 int n = wxNOT_FOUND;
132
133 GtkComboBox* combobox = GTK_COMBO_BOX( m_widget );
134 for ( int i = 0; i < count; ++i )
135 {
136 n = pos + i;
137 // If sorted, use this wxSortedArrayStrings to determine
138 // the right insertion point
139 if(m_strings)
140 n = m_strings->Add(items[i]);
141
142 gtk_combo_box_insert_text( combobox, n, wxGTK_CONV( items[i] ) );
143
144 m_clientData.Insert( NULL, n );
145 AssignNewItemClientData(n, clientData, i, type);
146 }
147
148 InvalidateBestSize();
149
150 return n;
151 }
152
153 void wxChoice::DoSetItemClientData(unsigned int n, void* clientData)
154 {
155 m_clientData[n] = clientData;
156 }
157
158 void* wxChoice::DoGetItemClientData(unsigned int n) const
159 {
160 return m_clientData[n];
161 }
162
163 void wxChoice::DoClear()
164 {
165 wxCHECK_RET( m_widget != NULL, wxT("invalid control") );
166
167 DisableEvents();
168
169 const unsigned int count = GetCount();
170 for (unsigned int i = 0; i < count; i++)
171 gtk_combo_box_remove_text( GTK_COMBO_BOX(m_widget), 0 );
172
173 m_clientData.Clear();
174
175 if (m_strings)
176 m_strings->Clear();
177
178 EnableEvents();
179
180 InvalidateBestSize();
181 }
182
183 void wxChoice::DoDeleteOneItem(unsigned int n)
184 {
185 wxCHECK_RET( m_widget != NULL, wxT("invalid control") );
186 wxCHECK_RET( IsValid(n), _T("invalid index in wxChoice::Delete") );
187
188 gtk_combo_box_remove_text( GTK_COMBO_BOX( m_widget ), n );
189 m_clientData.RemoveAt( n );
190 if ( m_strings )
191 m_strings->RemoveAt( n );
192
193 InvalidateBestSize();
194 }
195
196 int wxChoice::FindString( const wxString &item, bool bCase ) const
197 {
198 wxCHECK_MSG( m_widget != NULL, wxNOT_FOUND, wxT("invalid control") );
199
200 GtkComboBox* combobox = GTK_COMBO_BOX( m_widget );
201 GtkTreeModel* model = gtk_combo_box_get_model( combobox );
202 GtkTreeIter iter;
203 gtk_tree_model_get_iter_first( model, &iter );
204 if (!gtk_list_store_iter_is_valid(GTK_LIST_STORE(model), &iter ))
205 return -1;
206 int count = 0;
207 do
208 {
209 GValue value = { 0, };
210 gtk_tree_model_get_value( model, &iter, 0, &value );
211 wxString str = wxGTK_CONV_BACK( g_value_get_string( &value ) );
212 g_value_unset( &value );
213
214 if (item.IsSameAs( str, bCase ) )
215 return count;
216
217 count++;
218 }
219 while ( gtk_tree_model_iter_next(model, &iter) );
220
221 return wxNOT_FOUND;
222 }
223
224 int wxChoice::GetSelection() const
225 {
226 return gtk_combo_box_get_active( GTK_COMBO_BOX( m_widget ) );
227 }
228
229 void wxChoice::SetString(unsigned int n, const wxString &text)
230 {
231 wxCHECK_RET( m_widget != NULL, wxT("invalid control") );
232
233 GtkComboBox* combobox = GTK_COMBO_BOX( m_widget );
234 wxCHECK_RET( IsValid(n), wxT("invalid index") );
235
236 GtkTreeModel *model = gtk_combo_box_get_model( combobox );
237 GtkTreeIter iter;
238 if (gtk_tree_model_iter_nth_child (model, &iter, NULL, n))
239 {
240 GValue value = { 0, };
241 g_value_init( &value, G_TYPE_STRING );
242 g_value_set_string( &value, wxGTK_CONV( text ) );
243 gtk_list_store_set_value( GTK_LIST_STORE(model), &iter, 0, &value );
244 g_value_unset( &value );
245 }
246
247 InvalidateBestSize();
248 }
249
250 wxString wxChoice::GetString(unsigned int n) const
251 {
252 wxCHECK_MSG( m_widget != NULL, wxEmptyString, wxT("invalid control") );
253
254 wxString str;
255
256 GtkComboBox* combobox = GTK_COMBO_BOX( m_widget );
257 GtkTreeModel *model = gtk_combo_box_get_model( combobox );
258 GtkTreeIter iter;
259 if (gtk_tree_model_iter_nth_child (model, &iter, NULL, n))
260 {
261 GValue value = { 0, };
262 gtk_tree_model_get_value( model, &iter, 0, &value );
263 wxString tmp = wxGTK_CONV_BACK( g_value_get_string( &value ) );
264 g_value_unset( &value );
265 return tmp;
266 }
267
268 return str;
269 }
270
271 unsigned int wxChoice::GetCount() const
272 {
273 wxCHECK_MSG( m_widget != NULL, 0, wxT("invalid control") );
274
275 GtkComboBox* combobox = GTK_COMBO_BOX( m_widget );
276 GtkTreeModel* model = gtk_combo_box_get_model( combobox );
277 GtkTreeIter iter;
278 gtk_tree_model_get_iter_first( model, &iter );
279 if (!gtk_list_store_iter_is_valid(GTK_LIST_STORE(model), &iter ))
280 return 0;
281 unsigned int ret = 1;
282 while (gtk_tree_model_iter_next( model, &iter ))
283 ret++;
284 return ret;
285 }
286
287 void wxChoice::SetSelection( int n )
288 {
289 wxCHECK_RET( m_widget != NULL, wxT("invalid control") );
290
291 DisableEvents();
292
293 GtkComboBox* combobox = GTK_COMBO_BOX( m_widget );
294 gtk_combo_box_set_active( combobox, n );
295
296 EnableEvents();
297 }
298
299 void wxChoice::DisableEvents()
300 {
301 g_signal_handlers_block_by_func(m_widget,
302 (gpointer) gtk_choice_changed_callback, this);
303 }
304
305 void wxChoice::EnableEvents()
306 {
307 g_signal_handlers_unblock_by_func(m_widget,
308 (gpointer) gtk_choice_changed_callback, this);
309 }
310
311
312 GdkWindow *wxChoice::GTKGetWindow(wxArrayGdkWindows& WXUNUSED(windows)) const
313 {
314 return m_widget->window;
315 }
316
317 // static
318 wxVisualAttributes
319 wxChoice::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant))
320 {
321 return GetDefaultAttributesFromGTKWidget(gtk_combo_box_new);
322 }
323
324
325 #endif // wxUSE_CHOICE || wxUSE_COMBOBOX