]> git.saurik.com Git - wxWidgets.git/blame - src/gtk/choice.cpp
Use Unix EOL format for the new files.
[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
e78c1d78 43void wxChoice::Init()
c801d85f 44{
d3b9f782 45 m_strings = NULL;
e78c1d78 46 m_stringCellIndex = 0;
6de97a3b 47}
c801d85f 48
584ad2a3
MB
49bool 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
debe6624 61bool wxChoice::Create( wxWindow *parent, wxWindowID id,
fd0eed64
RR
62 const wxPoint &pos, const wxSize &size,
63 int n, const wxString choices[],
a2c94110
VZ
64 long style, const wxValidator& validator,
65 const wxString &name )
c801d85f 66{
4dcaf11a
RR
67 if (!PreCreation( parent, pos, size ) ||
68 !CreateBase( parent, id, pos, size, style, validator, name ))
69 {
223d09f6 70 wxFAIL_MSG( wxT("wxChoice creation failed") );
0a164d4c 71 return false;
4dcaf11a 72 }
6de97a3b 73
a236aa20 74 if ( IsSorted() )
e01c8145 75 {
a236aa20 76 // if our m_strings != NULL, Append() will check for it and insert
e01c8145 77 // items in the correct order
c272f12f 78 m_strings = new wxGtkCollatedArrayString;
e01c8145
VZ
79 }
80
a2c94110 81 m_widget = gtk_combo_box_new_text();
9ff9d30c 82 g_object_ref(m_widget);
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
e78c1d78
RR
120void 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
a236aa20
VZ
125int wxChoice::DoInsertItems(const wxArrayStringsAdapter & items,
126 unsigned int pos,
127 void **clientData, wxClientDataType type)
fd0eed64 128{
a2c94110 129 wxCHECK_MSG( m_widget != NULL, -1, wxT("invalid control") );
29006414 130
a2c94110 131 wxASSERT_MSG( !IsSorted() || (pos == GetCount()),
9a83f860 132 wxT("In a sorted choice data could only be appended"));
243dbf1a 133
a2c94110 134 const int count = items.GetCount();
243dbf1a 135
a2c94110
VZ
136 int n = wxNOT_FOUND;
137
a2c94110 138 for ( int i = 0; i < count; ++i )
a236aa20 139 {
a2c94110
VZ
140 n = pos + i;
141 // If sorted, use this wxSortedArrayStrings to determine
142 // the right insertion point
c272f12f 143 if (m_strings)
a2c94110 144 n = m_strings->Add(items[i]);
c272f12f 145
e78c1d78 146 GTKInsertComboBoxTextItem( n, items[i] );
243dbf1a 147
a2c94110
VZ
148 m_clientData.Insert( NULL, n );
149 AssignNewItemClientData(n, clientData, i, type);
16edee16
RR
150 }
151
a2c94110 152 InvalidateBestSize();
261a9107 153
a2c94110 154 return n;
fd0eed64 155}
f96aa4d9 156
aa61d352 157void wxChoice::DoSetItemClientData(unsigned int n, void* clientData)
fd0eed64 158{
a236aa20 159 m_clientData[n] = clientData;
fd0eed64
RR
160}
161
aa61d352 162void* wxChoice::DoGetItemClientData(unsigned int n) const
fd0eed64 163{
a236aa20 164 return m_clientData[n];
fd0eed64 165}
29006414 166
a236aa20 167void wxChoice::DoClear()
c801d85f 168{
a2c94110 169 wxCHECK_RET( m_widget != NULL, wxT("invalid control") );
f96aa4d9 170
a2c94110
VZ
171 DisableEvents();
172
e78c1d78
RR
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));
29006414 176
a236aa20 177 m_clientData.Clear();
2ee3ee1b 178
a2c94110 179 if (m_strings)
2ee3ee1b 180 m_strings->Clear();
16edee16 181
a2c94110
VZ
182 EnableEvents();
183
184 InvalidateBestSize();
6de97a3b 185}
c801d85f 186
a236aa20 187void wxChoice::DoDeleteOneItem(unsigned int n)
2f6407b9 188{
a2c94110 189 wxCHECK_RET( m_widget != NULL, wxT("invalid control") );
9a83f860 190 wxCHECK_RET( IsValid(n), wxT("invalid index in wxChoice::Delete") );
645420d8 191
e78c1d78
RR
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
a2c94110
VZ
200 m_clientData.RemoveAt( n );
201 if ( m_strings )
202 m_strings->RemoveAt( n );
e2380ce1 203
a2c94110 204 InvalidateBestSize();
2f6407b9
RR
205}
206
a2c94110 207int wxChoice::FindString( const wxString &item, bool bCase ) const
c801d85f 208{
a2c94110
VZ
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;
fd0eed64 217 int count = 0;
a2c94110 218 do
fd0eed64 219 {
a2c94110 220 GValue value = { 0, };
e78c1d78 221 gtk_tree_model_get_value( model, &iter, m_stringCellIndex, &value );
a2c94110
VZ
222 wxString str = wxGTK_CONV_BACK( g_value_get_string( &value ) );
223 g_value_unset( &value );
29006414 224
a2c94110 225 if (item.IsSameAs( str, bCase ) )
9e691f46 226 return count;
29006414 227
9e691f46 228 count++;
fd0eed64 229 }
a2c94110 230 while ( gtk_tree_model_iter_next(model, &iter) );
29006414 231
0a164d4c 232 return wxNOT_FOUND;
6de97a3b 233}
c801d85f 234
9abe166a 235int wxChoice::GetSelection() const
c801d85f 236{
a2c94110 237 return gtk_combo_box_get_active( GTK_COMBO_BOX( m_widget ) );
6de97a3b 238}
c801d85f 239
a2c94110 240void wxChoice::SetString(unsigned int n, const wxString &text)
6c8a980f 241{
a2c94110
VZ
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") );
6c8a980f 246
a2c94110
VZ
247 GtkTreeModel *model = gtk_combo_box_get_model( combobox );
248 GtkTreeIter iter;
249 if (gtk_tree_model_iter_nth_child (model, &iter, NULL, n))
34b5e560 250 {
a2c94110
VZ
251 GValue value = { 0, };
252 g_value_init( &value, G_TYPE_STRING );
253 g_value_set_string( &value, wxGTK_CONV( text ) );
e78c1d78 254 gtk_list_store_set_value( GTK_LIST_STORE(model), &iter, m_stringCellIndex, &value );
a2c94110 255 g_value_unset( &value );
34b5e560 256 }
a2c94110
VZ
257
258 InvalidateBestSize();
6c8a980f
VZ
259}
260
aa61d352 261wxString wxChoice::GetString(unsigned int n) const
c801d85f 262{
a2c94110
VZ
263 wxCHECK_MSG( m_widget != NULL, wxEmptyString, wxT("invalid control") );
264
265 wxString str;
fd0eed64 266
a2c94110
VZ
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))
c801d85f 271 {
a2c94110 272 GValue value = { 0, };
e78c1d78 273 gtk_tree_model_get_value( model, &iter, m_stringCellIndex, &value );
a2c94110
VZ
274 wxString tmp = wxGTK_CONV_BACK( g_value_get_string( &value ) );
275 g_value_unset( &value );
276 return tmp;
6de97a3b 277 }
29006414 278
a2c94110 279 return str;
6de97a3b 280}
c801d85f 281
aa61d352 282unsigned int wxChoice::GetCount() const
c801d85f 283{
a2c94110
VZ
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;
6de97a3b 296}
c801d85f 297
debe6624 298void wxChoice::SetSelection( int n )
c801d85f 299{
a2c94110 300 wxCHECK_RET( m_widget != NULL, wxT("invalid control") );
29006414 301
a2c94110 302 DisableEvents();
29006414 303
a2c94110
VZ
304 GtkComboBox* combobox = GTK_COMBO_BOX( m_widget );
305 gtk_combo_box_set_active( combobox, n );
29006414 306
a2c94110 307 EnableEvents();
f96aa4d9
RR
308}
309
3f16e52c
RR
310void wxChoice::SetColumns(int n)
311{
312 gtk_combo_box_set_wrap_width(GTK_COMBO_BOX(m_widget), n);
313}
314
315int 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
a2c94110 324void wxChoice::DisableEvents()
e01c8145 325{
a2c94110
VZ
326 g_signal_handlers_block_by_func(m_widget,
327 (gpointer) gtk_choice_changed_callback, this);
e01c8145
VZ
328}
329
a2c94110 330void wxChoice::EnableEvents()
f68586e5 331{
a2c94110
VZ
332 g_signal_handlers_unblock_by_func(m_widget,
333 (gpointer) gtk_choice_changed_callback, this);
f68586e5
VZ
334}
335
a2c94110 336
ef5c70f9 337GdkWindow *wxChoice::GTKGetWindow(wxArrayGdkWindows& WXUNUSED(windows)) const
2b904684 338{
a2c94110 339 return m_widget->window;
2b904684
RR
340}
341
0662f990
VZ
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.
345wxSize 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 {
e4e14871 354 ret.x = GetCount() > 0 ? 0 : 60; // start with something "sensible"
0662f990
VZ
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
c2193ac9
RR
373void 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
9d522606
RD
380// static
381wxVisualAttributes
382wxChoice::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant))
383{
a2c94110 384 return GetDefaultAttributesFromGTKWidget(gtk_combo_box_new);
9d522606
RD
385}
386
a73554d4 387
a2c94110 388#endif // wxUSE_CHOICE || wxUSE_COMBOBOX