]> git.saurik.com Git - wxWidgets.git/blob - src/gtk/choice.cpp
testing header replace
[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 void wxChoice::Init()
44 {
45 m_strings = (wxSortedArrayString *)NULL;
46 m_stringCellIndex = 0;
47 }
48
49 bool wxChoice::Create( wxWindow *parent, wxWindowID id,
50 const wxPoint &pos, const wxSize &size,
51 const wxArrayString& choices,
52 long style, const wxValidator& validator,
53 const wxString &name )
54 {
55 wxCArrayString chs(choices);
56
57 return Create( parent, id, pos, size, chs.GetCount(), chs.GetStrings(),
58 style, validator, name );
59 }
60
61 bool wxChoice::Create( wxWindow *parent, wxWindowID id,
62 const wxPoint &pos, const wxSize &size,
63 int n, const wxString choices[],
64 long style, const wxValidator& validator,
65 const wxString &name )
66 {
67 if (!PreCreation( parent, pos, size ) ||
68 !CreateBase( parent, id, pos, size, style, validator, name ))
69 {
70 wxFAIL_MSG( wxT("wxChoice creation failed") );
71 return false;
72 }
73
74 if ( IsSorted() )
75 {
76 // if our m_strings != NULL, Append() will check for it and insert
77 // items in the correct order
78 m_strings = new wxSortedArrayString;
79 }
80
81 m_widget = gtk_combo_box_new_text();
82 g_object_ref(m_widget);
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 void wxChoice::GTKInsertComboBoxTextItem( unsigned int n, const wxString& text )
121 {
122 gtk_combo_box_insert_text( GTK_COMBO_BOX( m_widget ), n, wxGTK_CONV( text ) );
123 }
124
125 int wxChoice::DoInsertItems(const wxArrayStringsAdapter & items,
126 unsigned int pos,
127 void **clientData, wxClientDataType type)
128 {
129 wxCHECK_MSG( m_widget != NULL, -1, wxT("invalid control") );
130
131 wxASSERT_MSG( !IsSorted() || (pos == GetCount()),
132 _T("In a sorted choice data could only be appended"));
133
134 const int count = items.GetCount();
135
136 int n = wxNOT_FOUND;
137
138 for ( int i = 0; i < count; ++i )
139 {
140 n = pos + i;
141 // If sorted, use this wxSortedArrayStrings to determine
142 // the right insertion point
143 if(m_strings)
144 n = m_strings->Add(items[i]);
145
146 GTKInsertComboBoxTextItem( n, items[i] );
147
148 m_clientData.Insert( NULL, n );
149 AssignNewItemClientData(n, clientData, i, type);
150 }
151
152 InvalidateBestSize();
153
154 return n;
155 }
156
157 void wxChoice::DoSetItemClientData(unsigned int n, void* clientData)
158 {
159 m_clientData[n] = clientData;
160 }
161
162 void* wxChoice::DoGetItemClientData(unsigned int n) const
163 {
164 return m_clientData[n];
165 }
166
167 void wxChoice::DoClear()
168 {
169 wxCHECK_RET( m_widget != NULL, wxT("invalid control") );
170
171 DisableEvents();
172
173 GtkComboBox* combobox = GTK_COMBO_BOX( m_widget );
174 GtkTreeModel* model = gtk_combo_box_get_model( combobox );
175 gtk_list_store_clear(GTK_LIST_STORE(model));
176
177 m_clientData.Clear();
178
179 if (m_strings)
180 m_strings->Clear();
181
182 EnableEvents();
183
184 InvalidateBestSize();
185 }
186
187 void wxChoice::DoDeleteOneItem(unsigned int n)
188 {
189 wxCHECK_RET( m_widget != NULL, wxT("invalid control") );
190 wxCHECK_RET( IsValid(n), _T("invalid index in wxChoice::Delete") );
191
192 GtkComboBox* combobox = GTK_COMBO_BOX( m_widget );
193 GtkTreeModel* model = gtk_combo_box_get_model( combobox );
194 GtkListStore* store = GTK_LIST_STORE(model);
195 GtkTreeIter iter;
196 gtk_tree_model_iter_nth_child( model, &iter,
197 NULL, (gint) n );
198 gtk_list_store_remove( store, &iter );
199
200 m_clientData.RemoveAt( n );
201 if ( m_strings )
202 m_strings->RemoveAt( n );
203
204 InvalidateBestSize();
205 }
206
207 int wxChoice::FindString( const wxString &item, bool bCase ) const
208 {
209 wxCHECK_MSG( m_widget != NULL, wxNOT_FOUND, wxT("invalid control") );
210
211 GtkComboBox* combobox = GTK_COMBO_BOX( m_widget );
212 GtkTreeModel* model = gtk_combo_box_get_model( combobox );
213 GtkTreeIter iter;
214 gtk_tree_model_get_iter_first( model, &iter );
215 if (!gtk_list_store_iter_is_valid(GTK_LIST_STORE(model), &iter ))
216 return -1;
217 int count = 0;
218 do
219 {
220 GValue value = { 0, };
221 gtk_tree_model_get_value( model, &iter, m_stringCellIndex, &value );
222 wxString str = wxGTK_CONV_BACK( g_value_get_string( &value ) );
223 g_value_unset( &value );
224
225 if (item.IsSameAs( str, bCase ) )
226 return count;
227
228 count++;
229 }
230 while ( gtk_tree_model_iter_next(model, &iter) );
231
232 return wxNOT_FOUND;
233 }
234
235 int wxChoice::GetSelection() const
236 {
237 return gtk_combo_box_get_active( GTK_COMBO_BOX( m_widget ) );
238 }
239
240 void wxChoice::SetString(unsigned int n, const wxString &text)
241 {
242 wxCHECK_RET( m_widget != NULL, wxT("invalid control") );
243
244 GtkComboBox* combobox = GTK_COMBO_BOX( m_widget );
245 wxCHECK_RET( IsValid(n), wxT("invalid index") );
246
247 GtkTreeModel *model = gtk_combo_box_get_model( combobox );
248 GtkTreeIter iter;
249 if (gtk_tree_model_iter_nth_child (model, &iter, NULL, n))
250 {
251 GValue value = { 0, };
252 g_value_init( &value, G_TYPE_STRING );
253 g_value_set_string( &value, wxGTK_CONV( text ) );
254 gtk_list_store_set_value( GTK_LIST_STORE(model), &iter, m_stringCellIndex, &value );
255 g_value_unset( &value );
256 }
257
258 InvalidateBestSize();
259 }
260
261 wxString wxChoice::GetString(unsigned int n) const
262 {
263 wxCHECK_MSG( m_widget != NULL, wxEmptyString, wxT("invalid control") );
264
265 wxString str;
266
267 GtkComboBox* combobox = GTK_COMBO_BOX( m_widget );
268 GtkTreeModel *model = gtk_combo_box_get_model( combobox );
269 GtkTreeIter iter;
270 if (gtk_tree_model_iter_nth_child (model, &iter, NULL, n))
271 {
272 GValue value = { 0, };
273 gtk_tree_model_get_value( model, &iter, m_stringCellIndex, &value );
274 wxString tmp = wxGTK_CONV_BACK( g_value_get_string( &value ) );
275 g_value_unset( &value );
276 return tmp;
277 }
278
279 return str;
280 }
281
282 unsigned int wxChoice::GetCount() const
283 {
284 wxCHECK_MSG( m_widget != NULL, 0, wxT("invalid control") );
285
286 GtkComboBox* combobox = GTK_COMBO_BOX( m_widget );
287 GtkTreeModel* model = gtk_combo_box_get_model( combobox );
288 GtkTreeIter iter;
289 gtk_tree_model_get_iter_first( model, &iter );
290 if (!gtk_list_store_iter_is_valid(GTK_LIST_STORE(model), &iter ))
291 return 0;
292 unsigned int ret = 1;
293 while (gtk_tree_model_iter_next( model, &iter ))
294 ret++;
295 return ret;
296 }
297
298 void wxChoice::SetSelection( int n )
299 {
300 wxCHECK_RET( m_widget != NULL, wxT("invalid control") );
301
302 DisableEvents();
303
304 GtkComboBox* combobox = GTK_COMBO_BOX( m_widget );
305 gtk_combo_box_set_active( combobox, n );
306
307 EnableEvents();
308 }
309
310 void wxChoice::SetColumns(int n)
311 {
312 gtk_combo_box_set_wrap_width(GTK_COMBO_BOX(m_widget), n);
313 }
314
315 int wxChoice::GetColumns() const
316 {
317 // gtk_combo_box_get_wrap_width() was added in gtk 2.6
318 gint intval;
319 g_object_get(G_OBJECT(m_widget), "wrap-width", &intval, NULL);
320 return intval;
321 }
322
323
324 void wxChoice::DisableEvents()
325 {
326 g_signal_handlers_block_by_func(m_widget,
327 (gpointer) gtk_choice_changed_callback, this);
328 }
329
330 void wxChoice::EnableEvents()
331 {
332 g_signal_handlers_unblock_by_func(m_widget,
333 (gpointer) gtk_choice_changed_callback, this);
334 }
335
336
337 GdkWindow *wxChoice::GTKGetWindow(wxArrayGdkWindows& WXUNUSED(windows)) const
338 {
339 return m_widget->window;
340 }
341
342 // Notice that this method shouldn't be necessary, because GTK calculates
343 // properly size of the combobox but for unknown reasons it doesn't work
344 // correctly in wx without this.
345 wxSize wxChoice::DoGetBestSize() const
346 {
347 // strangely, this returns a width of 188 pixels from GTK+ (?)
348 wxSize ret( wxControl::DoGetBestSize() );
349
350 // we know better our horizontal extent: it depends on the longest string
351 // in the combobox
352 if ( m_widget )
353 {
354 ret.x = GetCount() > 0 ? 0 : 60; // start with something "sensible"
355 int width;
356 unsigned int count = GetCount();
357 for ( unsigned int n = 0; n < count; n++ )
358 {
359 GetTextExtent(GetString(n), &width, NULL, NULL, NULL );
360 if ( width + 40 > ret.x ) // 40 for drop down arrow and space around text
361 ret.x = width + 40;
362 }
363 }
364
365 // empty combobox should have some reasonable default size too
366 if ((GetCount() == 0) && (ret.x < 80))
367 ret.x = 80;
368
369 CacheBestSize(ret);
370 return ret;
371 }
372
373 void wxChoice::DoApplyWidgetStyle(GtkRcStyle *style)
374 {
375 gtk_widget_modify_style(m_widget, style);
376 gtk_widget_modify_style(GTK_BIN(m_widget)->child, style);
377 }
378
379
380 // static
381 wxVisualAttributes
382 wxChoice::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant))
383 {
384 return GetDefaultAttributesFromGTKWidget(gtk_combo_box_new);
385 }
386
387
388 #endif // wxUSE_CHOICE || wxUSE_COMBOBOX