]> git.saurik.com Git - wxWidgets.git/blob - src/gtk/choice.cpp
mac paths updated
[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
83 Append(n, choices);
84
85 m_parent->DoAddChild( this );
86
87 PostCreation(size);
88
89 g_signal_connect_after (m_widget, "changed",
90 G_CALLBACK (gtk_choice_changed_callback), this);
91
92 return true;
93 }
94
95 wxChoice::~wxChoice()
96 {
97 delete m_strings;
98 }
99
100 void wxChoice::SendSelectionChangedEvent(wxEventType evt_type)
101 {
102 if (!m_hasVMT)
103 return;
104
105 if (GetSelection() == -1)
106 return;
107
108 wxCommandEvent event( evt_type, GetId() );
109
110 int n = GetSelection();
111 event.SetInt( n );
112 event.SetString( GetStringSelection() );
113 event.SetEventObject( this );
114 InitCommandEventWithItems( event, n );
115
116 HandleWindowEvent( event );
117 }
118
119 void wxChoice::GTKInsertComboBoxTextItem( unsigned int n, const wxString& text )
120 {
121 gtk_combo_box_insert_text( GTK_COMBO_BOX( m_widget ), n, wxGTK_CONV( text ) );
122 }
123
124 int wxChoice::DoInsertItems(const wxArrayStringsAdapter & items,
125 unsigned int pos,
126 void **clientData, wxClientDataType type)
127 {
128 wxCHECK_MSG( m_widget != NULL, -1, wxT("invalid control") );
129
130 wxASSERT_MSG( !IsSorted() || (pos == GetCount()),
131 _T("In a sorted choice data could only be appended"));
132
133 const int count = items.GetCount();
134
135 int n = wxNOT_FOUND;
136
137 for ( int i = 0; i < count; ++i )
138 {
139 n = pos + i;
140 // If sorted, use this wxSortedArrayStrings to determine
141 // the right insertion point
142 if(m_strings)
143 n = m_strings->Add(items[i]);
144
145 GTKInsertComboBoxTextItem( n, items[i] );
146
147 m_clientData.Insert( NULL, n );
148 AssignNewItemClientData(n, clientData, i, type);
149 }
150
151 InvalidateBestSize();
152
153 return n;
154 }
155
156 void wxChoice::DoSetItemClientData(unsigned int n, void* clientData)
157 {
158 m_clientData[n] = clientData;
159 }
160
161 void* wxChoice::DoGetItemClientData(unsigned int n) const
162 {
163 return m_clientData[n];
164 }
165
166 void wxChoice::DoClear()
167 {
168 wxCHECK_RET( m_widget != NULL, wxT("invalid control") );
169
170 DisableEvents();
171
172 GtkComboBox* combobox = GTK_COMBO_BOX( m_widget );
173 GtkTreeModel* model = gtk_combo_box_get_model( combobox );
174 gtk_list_store_clear(GTK_LIST_STORE(model));
175
176 m_clientData.Clear();
177
178 if (m_strings)
179 m_strings->Clear();
180
181 EnableEvents();
182
183 InvalidateBestSize();
184 }
185
186 void wxChoice::DoDeleteOneItem(unsigned int n)
187 {
188 wxCHECK_RET( m_widget != NULL, wxT("invalid control") );
189 wxCHECK_RET( IsValid(n), _T("invalid index in wxChoice::Delete") );
190
191 GtkComboBox* combobox = GTK_COMBO_BOX( m_widget );
192 GtkTreeModel* model = gtk_combo_box_get_model( combobox );
193 GtkListStore* store = GTK_LIST_STORE(model);
194 GtkTreeIter iter;
195 gtk_tree_model_iter_nth_child( model, &iter,
196 NULL, (gint) n );
197 gtk_list_store_remove( store, &iter );
198
199 m_clientData.RemoveAt( n );
200 if ( m_strings )
201 m_strings->RemoveAt( n );
202
203 InvalidateBestSize();
204 }
205
206 int wxChoice::FindString( const wxString &item, bool bCase ) const
207 {
208 wxCHECK_MSG( m_widget != NULL, wxNOT_FOUND, wxT("invalid control") );
209
210 GtkComboBox* combobox = GTK_COMBO_BOX( m_widget );
211 GtkTreeModel* model = gtk_combo_box_get_model( combobox );
212 GtkTreeIter iter;
213 gtk_tree_model_get_iter_first( model, &iter );
214 if (!gtk_list_store_iter_is_valid(GTK_LIST_STORE(model), &iter ))
215 return -1;
216 int count = 0;
217 do
218 {
219 GValue value = { 0, };
220 gtk_tree_model_get_value( model, &iter, m_stringCellIndex, &value );
221 wxString str = wxGTK_CONV_BACK( g_value_get_string( &value ) );
222 g_value_unset( &value );
223
224 if (item.IsSameAs( str, bCase ) )
225 return count;
226
227 count++;
228 }
229 while ( gtk_tree_model_iter_next(model, &iter) );
230
231 return wxNOT_FOUND;
232 }
233
234 int wxChoice::GetSelection() const
235 {
236 return gtk_combo_box_get_active( GTK_COMBO_BOX( m_widget ) );
237 }
238
239 void wxChoice::SetString(unsigned int n, const wxString &text)
240 {
241 wxCHECK_RET( m_widget != NULL, wxT("invalid control") );
242
243 GtkComboBox* combobox = GTK_COMBO_BOX( m_widget );
244 wxCHECK_RET( IsValid(n), wxT("invalid index") );
245
246 GtkTreeModel *model = gtk_combo_box_get_model( combobox );
247 GtkTreeIter iter;
248 if (gtk_tree_model_iter_nth_child (model, &iter, NULL, n))
249 {
250 GValue value = { 0, };
251 g_value_init( &value, G_TYPE_STRING );
252 g_value_set_string( &value, wxGTK_CONV( text ) );
253 gtk_list_store_set_value( GTK_LIST_STORE(model), &iter, m_stringCellIndex, &value );
254 g_value_unset( &value );
255 }
256
257 InvalidateBestSize();
258 }
259
260 wxString wxChoice::GetString(unsigned int n) const
261 {
262 wxCHECK_MSG( m_widget != NULL, wxEmptyString, wxT("invalid control") );
263
264 wxString str;
265
266 GtkComboBox* combobox = GTK_COMBO_BOX( m_widget );
267 GtkTreeModel *model = gtk_combo_box_get_model( combobox );
268 GtkTreeIter iter;
269 if (gtk_tree_model_iter_nth_child (model, &iter, NULL, n))
270 {
271 GValue value = { 0, };
272 gtk_tree_model_get_value( model, &iter, m_stringCellIndex, &value );
273 wxString tmp = wxGTK_CONV_BACK( g_value_get_string( &value ) );
274 g_value_unset( &value );
275 return tmp;
276 }
277
278 return str;
279 }
280
281 unsigned int wxChoice::GetCount() const
282 {
283 wxCHECK_MSG( m_widget != NULL, 0, wxT("invalid control") );
284
285 GtkComboBox* combobox = GTK_COMBO_BOX( m_widget );
286 GtkTreeModel* model = gtk_combo_box_get_model( combobox );
287 GtkTreeIter iter;
288 gtk_tree_model_get_iter_first( model, &iter );
289 if (!gtk_list_store_iter_is_valid(GTK_LIST_STORE(model), &iter ))
290 return 0;
291 unsigned int ret = 1;
292 while (gtk_tree_model_iter_next( model, &iter ))
293 ret++;
294 return ret;
295 }
296
297 void wxChoice::SetSelection( int n )
298 {
299 wxCHECK_RET( m_widget != NULL, wxT("invalid control") );
300
301 DisableEvents();
302
303 GtkComboBox* combobox = GTK_COMBO_BOX( m_widget );
304 gtk_combo_box_set_active( combobox, n );
305
306 EnableEvents();
307 }
308
309 void wxChoice::DisableEvents()
310 {
311 g_signal_handlers_block_by_func(m_widget,
312 (gpointer) gtk_choice_changed_callback, this);
313 }
314
315 void wxChoice::EnableEvents()
316 {
317 g_signal_handlers_unblock_by_func(m_widget,
318 (gpointer) gtk_choice_changed_callback, this);
319 }
320
321
322 GdkWindow *wxChoice::GTKGetWindow(wxArrayGdkWindows& WXUNUSED(windows)) const
323 {
324 return m_widget->window;
325 }
326
327 // Notice that this method shouldn't be necessary, because GTK calculates
328 // properly size of the combobox but for unknown reasons it doesn't work
329 // correctly in wx without this.
330 wxSize wxChoice::DoGetBestSize() const
331 {
332 // strangely, this returns a width of 188 pixels from GTK+ (?)
333 wxSize ret( wxControl::DoGetBestSize() );
334
335 // we know better our horizontal extent: it depends on the longest string
336 // in the combobox
337 if ( m_widget )
338 {
339 ret.x = 60; // start with something "sensible"
340 int width;
341 unsigned int count = GetCount();
342 for ( unsigned int n = 0; n < count; n++ )
343 {
344 GetTextExtent(GetString(n), &width, NULL, NULL, NULL );
345 if ( width + 40 > ret.x ) // 40 for drop down arrow and space around text
346 ret.x = width + 40;
347 }
348 }
349
350 // empty combobox should have some reasonable default size too
351 if ((GetCount() == 0) && (ret.x < 80))
352 ret.x = 80;
353
354 CacheBestSize(ret);
355 return ret;
356 }
357
358 void wxChoice::DoApplyWidgetStyle(GtkRcStyle *style)
359 {
360 gtk_widget_modify_style(m_widget, style);
361 gtk_widget_modify_style(GTK_BIN(m_widget)->child, style);
362 }
363
364
365 // static
366 wxVisualAttributes
367 wxChoice::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant))
368 {
369 return GetDefaultAttributesFromGTKWidget(gtk_combo_box_new);
370 }
371
372
373 #endif // wxUSE_CHOICE || wxUSE_COMBOBOX