create tests/window directory for wxWindow tests
[wxWidgets.git] / src / gtk / choice.cpp
0 / 356 (  0%)
CommitLineData
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
27extern "C" {
28
29static void
30gtk_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
41IMPLEMENT_DYNAMIC_CLASS(wxChoice, wxControlWithItems)
42
43wxChoice::wxChoice()
44 : m_strings(NULL)
45{
46}
47
48bool 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
60bool 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
96wxChoice::~wxChoice()
97{
98 delete m_strings;
99}
100
101void 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
120int 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
153void wxChoice::DoSetItemClientData(unsigned int n, void* clientData)
154{
155 m_clientData[n] = clientData;
156}
157
158void* wxChoice::DoGetItemClientData(unsigned int n) const
159{
160 return m_clientData[n];
161}
162
163void 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
183void 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
196int 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
224int wxChoice::GetSelection() const
225{
226 return gtk_combo_box_get_active( GTK_COMBO_BOX( m_widget ) );
227}
228
229void 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
250wxString 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
271unsigned 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
287void 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
299void wxChoice::DisableEvents()
300{
301 g_signal_handlers_block_by_func(m_widget,
302 (gpointer) gtk_choice_changed_callback, this);
303}
304
305void wxChoice::EnableEvents()
306{
307 g_signal_handlers_unblock_by_func(m_widget,
308 (gpointer) gtk_choice_changed_callback, this);
309}
310
311
312GdkWindow *wxChoice::GTKGetWindow(wxArrayGdkWindows& WXUNUSED(windows)) const
313{
314 return m_widget->window;
315}
316
317// Notice that this method shouldn't be necessary, because GTK calculates
318// properly size of the combobox but for unknown reasons it doesn't work
319// correctly in wx without this.
320wxSize wxChoice::DoGetBestSize() const
321{
322 // strangely, this returns a width of 188 pixels from GTK+ (?)
323 wxSize ret( wxControl::DoGetBestSize() );
324
325 // we know better our horizontal extent: it depends on the longest string
326 // in the combobox
327 if ( m_widget )
328 {
329 ret.x = 60; // start with something "sensible"
330 int width;
331 unsigned int count = GetCount();
332 for ( unsigned int n = 0; n < count; n++ )
333 {
334 GetTextExtent(GetString(n), &width, NULL, NULL, NULL );
335 if ( width + 40 > ret.x ) // 40 for drop down arrow and space around text
336 ret.x = width + 40;
337 }
338 }
339
340 // empty combobox should have some reasonable default size too
341 if ((GetCount() == 0) && (ret.x < 80))
342 ret.x = 80;
343
344 CacheBestSize(ret);
345 return ret;
346}
347
348// static
349wxVisualAttributes
350wxChoice::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant))
351{
352 return GetDefaultAttributesFromGTKWidget(gtk_combo_box_new);
353}
354
355
356#endif // wxUSE_CHOICE || wxUSE_COMBOBOX