]> git.saurik.com Git - wxWidgets.git/blame - src/gtk/choice.cpp
Remove m_blockEvents and use Disable/Enable instead, some more rearraging
[wxWidgets.git] / src / gtk / choice.cpp
CommitLineData
c801d85f 1/////////////////////////////////////////////////////////////////////////////
11e62fe6 2// Name: src/gtk/choice.cpp
c801d85f
KB
3// Purpose:
4// Author: Robert Roebling
dbf858b5 5// Id: $Id$
01111366 6// Copyright: (c) 1998 Robert Roebling
65571936 7// Licence: wxWindows licence
c801d85f
KB
8/////////////////////////////////////////////////////////////////////////////
9
8228b893 10#include "wx/wxprec.h"
c801d85f 11
a2c94110 12#if wxUSE_CHOICE || wxUSE_COMBOBOX
ce4169a4 13
1e6feb95 14#include "wx/choice.h"
aaa6d89a
WS
15
16#ifndef WX_PRECOMP
17 #include "wx/arrstr.h"
18#endif
1e6feb95 19
9e691f46 20#include "wx/gtk/private.h"
83624f79 21
66bd6b93 22
a2c94110
VZ
23// ----------------------------------------------------------------------------
24// GTK callbacks
25// ----------------------------------------------------------------------------
c801d85f 26
865bb325 27extern "C" {
6c8a980f 28
a2c94110
VZ
29static void
30gtk_choice_changed_callback( GtkWidget *WXUNUSED(widget), wxChoice *choice )
31{
32 choice->SendSelectionChangedEvent(wxEVT_COMMAND_CHOICE_SELECTED);
6de97a3b 33}
a2c94110 34
865bb325 35}
c801d85f 36
e1e955e1
RR
37//-----------------------------------------------------------------------------
38// wxChoice
c801d85f
KB
39//-----------------------------------------------------------------------------
40
b1294ada 41IMPLEMENT_DYNAMIC_CLASS(wxChoice, wxControlWithItems)
c801d85f 42
fd0eed64 43wxChoice::wxChoice()
a2c94110 44 : m_strings(NULL)
c801d85f 45{
6de97a3b 46}
c801d85f 47
584ad2a3
MB
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
debe6624 60bool wxChoice::Create( wxWindow *parent, wxWindowID id,
fd0eed64
RR
61 const wxPoint &pos, const wxSize &size,
62 int n, const wxString choices[],
a2c94110
VZ
63 long style, const wxValidator& validator,
64 const wxString &name )
c801d85f 65{
a2c94110
VZ
66 m_strings = NULL;
67
4dcaf11a
RR
68 if (!PreCreation( parent, pos, size ) ||
69 !CreateBase( parent, id, pos, size, style, validator, name ))
70 {
223d09f6 71 wxFAIL_MSG( wxT("wxChoice creation failed") );
0a164d4c 72 return false;
4dcaf11a 73 }
6de97a3b 74
a236aa20 75 if ( IsSorted() )
e01c8145 76 {
a236aa20 77 // if our m_strings != NULL, Append() will check for it and insert
e01c8145
VZ
78 // items in the correct order
79 m_strings = new wxSortedArrayString;
80 }
81
a2c94110 82 m_widget = gtk_combo_box_new_text();
16edee16 83
a2c94110 84 Append(n, choices);
29006414 85
f03fc89f 86 m_parent->DoAddChild( this );
29006414 87
abdeb9e7 88 PostCreation(size);
29006414 89
a2c94110
VZ
90 g_signal_connect_after (m_widget, "changed",
91 G_CALLBACK (gtk_choice_changed_callback), this);
4b8e857f 92
0a164d4c 93 return true;
6de97a3b 94}
29006414 95
fd0eed64
RR
96wxChoice::~wxChoice()
97{
e01c8145 98 delete m_strings;
fd0eed64
RR
99}
100
a2c94110
VZ
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
a236aa20
VZ
120int wxChoice::DoInsertItems(const wxArrayStringsAdapter & items,
121 unsigned int pos,
122 void **clientData, wxClientDataType type)
fd0eed64 123{
a2c94110 124 wxCHECK_MSG( m_widget != NULL, -1, wxT("invalid control") );
29006414 125
a2c94110
VZ
126 wxASSERT_MSG( !IsSorted() || (pos == GetCount()),
127 _T("In a sorted choice data could only be appended"));
243dbf1a 128
a2c94110 129 const int count = items.GetCount();
243dbf1a 130
a2c94110
VZ
131 int n = wxNOT_FOUND;
132
133 GtkComboBox* combobox = GTK_COMBO_BOX( m_widget );
134 for ( int i = 0; i < count; ++i )
a236aa20 135 {
a2c94110
VZ
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]);
243dbf1a 141
a2c94110 142 gtk_combo_box_insert_text( combobox, n, wxGTK_CONV( items[i] ) );
243dbf1a 143
a2c94110
VZ
144 m_clientData.Insert( NULL, n );
145 AssignNewItemClientData(n, clientData, i, type);
16edee16
RR
146 }
147
a2c94110 148 InvalidateBestSize();
261a9107 149
a2c94110 150 return n;
fd0eed64 151}
f96aa4d9 152
aa61d352 153void wxChoice::DoSetItemClientData(unsigned int n, void* clientData)
fd0eed64 154{
a236aa20 155 m_clientData[n] = clientData;
fd0eed64
RR
156}
157
aa61d352 158void* wxChoice::DoGetItemClientData(unsigned int n) const
fd0eed64 159{
a236aa20 160 return m_clientData[n];
fd0eed64 161}
29006414 162
a236aa20 163void wxChoice::DoClear()
c801d85f 164{
a2c94110 165 wxCHECK_RET( m_widget != NULL, wxT("invalid control") );
f96aa4d9 166
a2c94110
VZ
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 );
29006414 172
a236aa20 173 m_clientData.Clear();
2ee3ee1b 174
a2c94110 175 if (m_strings)
2ee3ee1b 176 m_strings->Clear();
16edee16 177
a2c94110
VZ
178 EnableEvents();
179
180 InvalidateBestSize();
6de97a3b 181}
c801d85f 182
a236aa20 183void wxChoice::DoDeleteOneItem(unsigned int n)
2f6407b9 184{
a2c94110 185 wxCHECK_RET( m_widget != NULL, wxT("invalid control") );
8228b893 186 wxCHECK_RET( IsValid(n), _T("invalid index in wxChoice::Delete") );
645420d8 187
a2c94110
VZ
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 );
e2380ce1 192
a2c94110 193 InvalidateBestSize();
2f6407b9
RR
194}
195
a2c94110 196int wxChoice::FindString( const wxString &item, bool bCase ) const
c801d85f 197{
a2c94110
VZ
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;
fd0eed64 206 int count = 0;
a2c94110 207 do
fd0eed64 208 {
a2c94110
VZ
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 );
29006414 213
a2c94110 214 if (item.IsSameAs( str, bCase ) )
9e691f46 215 return count;
29006414 216
9e691f46 217 count++;
fd0eed64 218 }
a2c94110 219 while ( gtk_tree_model_iter_next(model, &iter) );
29006414 220
0a164d4c 221 return wxNOT_FOUND;
6de97a3b 222}
c801d85f 223
9abe166a 224int wxChoice::GetSelection() const
c801d85f 225{
a2c94110 226 return gtk_combo_box_get_active( GTK_COMBO_BOX( m_widget ) );
6de97a3b 227}
c801d85f 228
a2c94110 229void wxChoice::SetString(unsigned int n, const wxString &text)
6c8a980f 230{
a2c94110
VZ
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") );
6c8a980f 235
a2c94110
VZ
236 GtkTreeModel *model = gtk_combo_box_get_model( combobox );
237 GtkTreeIter iter;
238 if (gtk_tree_model_iter_nth_child (model, &iter, NULL, n))
34b5e560 239 {
a2c94110
VZ
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 );
34b5e560 245 }
a2c94110
VZ
246
247 InvalidateBestSize();
6c8a980f
VZ
248}
249
aa61d352 250wxString wxChoice::GetString(unsigned int n) const
c801d85f 251{
a2c94110
VZ
252 wxCHECK_MSG( m_widget != NULL, wxEmptyString, wxT("invalid control") );
253
254 wxString str;
fd0eed64 255
a2c94110
VZ
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))
c801d85f 260 {
a2c94110
VZ
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;
6de97a3b 266 }
29006414 267
a2c94110 268 return str;
6de97a3b 269}
c801d85f 270
aa61d352 271unsigned int wxChoice::GetCount() const
c801d85f 272{
a2c94110
VZ
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;
6de97a3b 285}
c801d85f 286
debe6624 287void wxChoice::SetSelection( int n )
c801d85f 288{
a2c94110 289 wxCHECK_RET( m_widget != NULL, wxT("invalid control") );
29006414 290
a2c94110 291 DisableEvents();
29006414 292
a2c94110
VZ
293 GtkComboBox* combobox = GTK_COMBO_BOX( m_widget );
294 gtk_combo_box_set_active( combobox, n );
29006414 295
a2c94110 296 EnableEvents();
f96aa4d9
RR
297}
298
a2c94110 299void wxChoice::DisableEvents()
e01c8145 300{
a2c94110
VZ
301 g_signal_handlers_block_by_func(m_widget,
302 (gpointer) gtk_choice_changed_callback, this);
e01c8145
VZ
303}
304
a2c94110 305void wxChoice::EnableEvents()
f68586e5 306{
a2c94110
VZ
307 g_signal_handlers_unblock_by_func(m_widget,
308 (gpointer) gtk_choice_changed_callback, this);
f68586e5
VZ
309}
310
a2c94110 311
ef5c70f9 312GdkWindow *wxChoice::GTKGetWindow(wxArrayGdkWindows& WXUNUSED(windows)) const
2b904684 313{
a2c94110 314 return m_widget->window;
2b904684
RR
315}
316
0662f990
VZ
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
9d522606
RD
348// static
349wxVisualAttributes
350wxChoice::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant))
351{
a2c94110 352 return GetDefaultAttributesFromGTKWidget(gtk_combo_box_new);
9d522606
RD
353}
354
a73554d4 355
a2c94110 356#endif // wxUSE_CHOICE || wxUSE_COMBOBOX