]> git.saurik.com Git - wxWidgets.git/blame - src/gtk/choice.cpp
updated setup.h for OpenVMS
[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
9dc44eff 20#include <gtk/gtk.h>
9e691f46 21#include "wx/gtk/private.h"
9dc44eff 22#include "wx/gtk/private/gtk2-compat.h"
66bd6b93 23
a2c94110
VZ
24// ----------------------------------------------------------------------------
25// GTK callbacks
26// ----------------------------------------------------------------------------
c801d85f 27
865bb325 28extern "C" {
6c8a980f 29
a2c94110
VZ
30static void
31gtk_choice_changed_callback( GtkWidget *WXUNUSED(widget), wxChoice *choice )
32{
33 choice->SendSelectionChangedEvent(wxEVT_COMMAND_CHOICE_SELECTED);
6de97a3b 34}
a2c94110 35
865bb325 36}
c801d85f 37
e1e955e1
RR
38//-----------------------------------------------------------------------------
39// wxChoice
c801d85f
KB
40//-----------------------------------------------------------------------------
41
e78c1d78 42void wxChoice::Init()
c801d85f 43{
d3b9f782 44 m_strings = NULL;
e78c1d78 45 m_stringCellIndex = 0;
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{
4dcaf11a
RR
66 if (!PreCreation( parent, pos, size ) ||
67 !CreateBase( parent, id, pos, size, style, validator, name ))
68 {
223d09f6 69 wxFAIL_MSG( wxT("wxChoice creation failed") );
0a164d4c 70 return false;
4dcaf11a 71 }
6de97a3b 72
a236aa20 73 if ( IsSorted() )
e01c8145 74 {
a236aa20 75 // if our m_strings != NULL, Append() will check for it and insert
e01c8145 76 // items in the correct order
c272f12f 77 m_strings = new wxGtkCollatedArrayString;
e01c8145
VZ
78 }
79
9dc44eff
PC
80#ifdef __WXGTK3__
81 m_widget = gtk_combo_box_text_new();
82#else
a2c94110 83 m_widget = gtk_combo_box_new_text();
9dc44eff 84#endif
9ff9d30c 85 g_object_ref(m_widget);
16edee16 86
a2c94110 87 Append(n, choices);
29006414 88
f03fc89f 89 m_parent->DoAddChild( this );
29006414 90
abdeb9e7 91 PostCreation(size);
29006414 92
a2c94110
VZ
93 g_signal_connect_after (m_widget, "changed",
94 G_CALLBACK (gtk_choice_changed_callback), this);
4b8e857f 95
0a164d4c 96 return true;
6de97a3b 97}
29006414 98
fd0eed64
RR
99wxChoice::~wxChoice()
100{
e01c8145 101 delete m_strings;
fd0eed64
RR
102}
103
a2c94110
VZ
104void wxChoice::SendSelectionChangedEvent(wxEventType evt_type)
105{
106 if (!m_hasVMT)
107 return;
108
109 if (GetSelection() == -1)
110 return;
111
112 wxCommandEvent event( evt_type, GetId() );
113
114 int n = GetSelection();
115 event.SetInt( n );
116 event.SetString( GetStringSelection() );
117 event.SetEventObject( this );
118 InitCommandEventWithItems( event, n );
119
120 HandleWindowEvent( event );
121}
122
e78c1d78
RR
123void wxChoice::GTKInsertComboBoxTextItem( unsigned int n, const wxString& text )
124{
9dc44eff
PC
125#ifdef __WXGTK3__
126 gtk_combo_box_text_insert_text(GTK_COMBO_BOX_TEXT(m_widget), n, wxGTK_CONV(text));
127#else
e78c1d78 128 gtk_combo_box_insert_text( GTK_COMBO_BOX( m_widget ), n, wxGTK_CONV( text ) );
9dc44eff 129#endif
e78c1d78
RR
130}
131
a236aa20
VZ
132int wxChoice::DoInsertItems(const wxArrayStringsAdapter & items,
133 unsigned int pos,
134 void **clientData, wxClientDataType type)
fd0eed64 135{
a2c94110 136 wxCHECK_MSG( m_widget != NULL, -1, wxT("invalid control") );
29006414 137
a2c94110 138 wxASSERT_MSG( !IsSorted() || (pos == GetCount()),
9a83f860 139 wxT("In a sorted choice data could only be appended"));
243dbf1a 140
a2c94110 141 const int count = items.GetCount();
243dbf1a 142
a2c94110
VZ
143 int n = wxNOT_FOUND;
144
a2c94110 145 for ( int i = 0; i < count; ++i )
a236aa20 146 {
a2c94110
VZ
147 n = pos + i;
148 // If sorted, use this wxSortedArrayStrings to determine
149 // the right insertion point
c272f12f 150 if (m_strings)
a2c94110 151 n = m_strings->Add(items[i]);
ce00f59b 152
e78c1d78 153 GTKInsertComboBoxTextItem( n, items[i] );
243dbf1a 154
a2c94110
VZ
155 m_clientData.Insert( NULL, n );
156 AssignNewItemClientData(n, clientData, i, type);
16edee16
RR
157 }
158
a2c94110 159 InvalidateBestSize();
261a9107 160
a2c94110 161 return n;
fd0eed64 162}
f96aa4d9 163
aa61d352 164void wxChoice::DoSetItemClientData(unsigned int n, void* clientData)
fd0eed64 165{
a236aa20 166 m_clientData[n] = clientData;
fd0eed64
RR
167}
168
aa61d352 169void* wxChoice::DoGetItemClientData(unsigned int n) const
fd0eed64 170{
a236aa20 171 return m_clientData[n];
fd0eed64 172}
29006414 173
a236aa20 174void wxChoice::DoClear()
c801d85f 175{
a2c94110 176 wxCHECK_RET( m_widget != NULL, wxT("invalid control") );
f96aa4d9 177
bce926c5 178 GTKDisableEvents();
a2c94110 179
e78c1d78
RR
180 GtkComboBox* combobox = GTK_COMBO_BOX( m_widget );
181 GtkTreeModel* model = gtk_combo_box_get_model( combobox );
182 gtk_list_store_clear(GTK_LIST_STORE(model));
29006414 183
a236aa20 184 m_clientData.Clear();
2ee3ee1b 185
a2c94110 186 if (m_strings)
2ee3ee1b 187 m_strings->Clear();
16edee16 188
bce926c5 189 GTKEnableEvents();
a2c94110
VZ
190
191 InvalidateBestSize();
6de97a3b 192}
c801d85f 193
a236aa20 194void wxChoice::DoDeleteOneItem(unsigned int n)
2f6407b9 195{
a2c94110 196 wxCHECK_RET( m_widget != NULL, wxT("invalid control") );
9a83f860 197 wxCHECK_RET( IsValid(n), wxT("invalid index in wxChoice::Delete") );
645420d8 198
e78c1d78
RR
199 GtkComboBox* combobox = GTK_COMBO_BOX( m_widget );
200 GtkTreeModel* model = gtk_combo_box_get_model( combobox );
201 GtkListStore* store = GTK_LIST_STORE(model);
202 GtkTreeIter iter;
203 gtk_tree_model_iter_nth_child( model, &iter,
204 NULL, (gint) n );
205 gtk_list_store_remove( store, &iter );
206
a2c94110
VZ
207 m_clientData.RemoveAt( n );
208 if ( m_strings )
209 m_strings->RemoveAt( n );
e2380ce1 210
a2c94110 211 InvalidateBestSize();
2f6407b9
RR
212}
213
a2c94110 214int wxChoice::FindString( const wxString &item, bool bCase ) const
c801d85f 215{
a2c94110
VZ
216 wxCHECK_MSG( m_widget != NULL, wxNOT_FOUND, wxT("invalid control") );
217
218 GtkComboBox* combobox = GTK_COMBO_BOX( m_widget );
219 GtkTreeModel* model = gtk_combo_box_get_model( combobox );
220 GtkTreeIter iter;
221 gtk_tree_model_get_iter_first( model, &iter );
222 if (!gtk_list_store_iter_is_valid(GTK_LIST_STORE(model), &iter ))
223 return -1;
fd0eed64 224 int count = 0;
a2c94110 225 do
fd0eed64 226 {
a2c94110 227 GValue value = { 0, };
e78c1d78 228 gtk_tree_model_get_value( model, &iter, m_stringCellIndex, &value );
a2c94110
VZ
229 wxString str = wxGTK_CONV_BACK( g_value_get_string( &value ) );
230 g_value_unset( &value );
29006414 231
a2c94110 232 if (item.IsSameAs( str, bCase ) )
9e691f46 233 return count;
29006414 234
9e691f46 235 count++;
fd0eed64 236 }
a2c94110 237 while ( gtk_tree_model_iter_next(model, &iter) );
29006414 238
0a164d4c 239 return wxNOT_FOUND;
6de97a3b 240}
c801d85f 241
9abe166a 242int wxChoice::GetSelection() const
c801d85f 243{
a2c94110 244 return gtk_combo_box_get_active( GTK_COMBO_BOX( m_widget ) );
6de97a3b 245}
c801d85f 246
a2c94110 247void wxChoice::SetString(unsigned int n, const wxString &text)
6c8a980f 248{
a2c94110
VZ
249 wxCHECK_RET( m_widget != NULL, wxT("invalid control") );
250
251 GtkComboBox* combobox = GTK_COMBO_BOX( m_widget );
252 wxCHECK_RET( IsValid(n), wxT("invalid index") );
6c8a980f 253
a2c94110
VZ
254 GtkTreeModel *model = gtk_combo_box_get_model( combobox );
255 GtkTreeIter iter;
256 if (gtk_tree_model_iter_nth_child (model, &iter, NULL, n))
34b5e560 257 {
a2c94110
VZ
258 GValue value = { 0, };
259 g_value_init( &value, G_TYPE_STRING );
260 g_value_set_string( &value, wxGTK_CONV( text ) );
e78c1d78 261 gtk_list_store_set_value( GTK_LIST_STORE(model), &iter, m_stringCellIndex, &value );
a2c94110 262 g_value_unset( &value );
34b5e560 263 }
a2c94110
VZ
264
265 InvalidateBestSize();
6c8a980f
VZ
266}
267
aa61d352 268wxString wxChoice::GetString(unsigned int n) const
c801d85f 269{
a2c94110
VZ
270 wxCHECK_MSG( m_widget != NULL, wxEmptyString, wxT("invalid control") );
271
272 wxString str;
fd0eed64 273
a2c94110
VZ
274 GtkComboBox* combobox = GTK_COMBO_BOX( m_widget );
275 GtkTreeModel *model = gtk_combo_box_get_model( combobox );
276 GtkTreeIter iter;
277 if (gtk_tree_model_iter_nth_child (model, &iter, NULL, n))
c801d85f 278 {
a2c94110 279 GValue value = { 0, };
e78c1d78 280 gtk_tree_model_get_value( model, &iter, m_stringCellIndex, &value );
a2c94110
VZ
281 wxString tmp = wxGTK_CONV_BACK( g_value_get_string( &value ) );
282 g_value_unset( &value );
283 return tmp;
6de97a3b 284 }
29006414 285
a2c94110 286 return str;
6de97a3b 287}
c801d85f 288
aa61d352 289unsigned int wxChoice::GetCount() const
c801d85f 290{
a2c94110
VZ
291 wxCHECK_MSG( m_widget != NULL, 0, wxT("invalid control") );
292
293 GtkComboBox* combobox = GTK_COMBO_BOX( m_widget );
294 GtkTreeModel* model = gtk_combo_box_get_model( combobox );
295 GtkTreeIter iter;
296 gtk_tree_model_get_iter_first( model, &iter );
297 if (!gtk_list_store_iter_is_valid(GTK_LIST_STORE(model), &iter ))
298 return 0;
299 unsigned int ret = 1;
300 while (gtk_tree_model_iter_next( model, &iter ))
301 ret++;
302 return ret;
6de97a3b 303}
c801d85f 304
debe6624 305void wxChoice::SetSelection( int n )
c801d85f 306{
a2c94110 307 wxCHECK_RET( m_widget != NULL, wxT("invalid control") );
29006414 308
bce926c5 309 GTKDisableEvents();
29006414 310
a2c94110
VZ
311 GtkComboBox* combobox = GTK_COMBO_BOX( m_widget );
312 gtk_combo_box_set_active( combobox, n );
29006414 313
bce926c5 314 GTKEnableEvents();
f96aa4d9
RR
315}
316
3f16e52c
RR
317void wxChoice::SetColumns(int n)
318{
319 gtk_combo_box_set_wrap_width(GTK_COMBO_BOX(m_widget), n);
320}
321
322int wxChoice::GetColumns() const
323{
324 // gtk_combo_box_get_wrap_width() was added in gtk 2.6
325 gint intval;
326 g_object_get(G_OBJECT(m_widget), "wrap-width", &intval, NULL);
327 return intval;
328}
329
330
bce926c5 331void wxChoice::GTKDisableEvents()
e01c8145 332{
a2c94110
VZ
333 g_signal_handlers_block_by_func(m_widget,
334 (gpointer) gtk_choice_changed_callback, this);
e01c8145
VZ
335}
336
bce926c5 337void wxChoice::GTKEnableEvents()
f68586e5 338{
a2c94110
VZ
339 g_signal_handlers_unblock_by_func(m_widget,
340 (gpointer) gtk_choice_changed_callback, this);
f68586e5
VZ
341}
342
a2c94110 343
ef5c70f9 344GdkWindow *wxChoice::GTKGetWindow(wxArrayGdkWindows& WXUNUSED(windows)) const
2b904684 345{
385e8575 346 return gtk_widget_get_window(m_widget);
2b904684
RR
347}
348
0662f990
VZ
349// Notice that this method shouldn't be necessary, because GTK calculates
350// properly size of the combobox but for unknown reasons it doesn't work
351// correctly in wx without this.
352wxSize wxChoice::DoGetBestSize() const
353{
354 // strangely, this returns a width of 188 pixels from GTK+ (?)
355 wxSize ret( wxControl::DoGetBestSize() );
356
357 // we know better our horizontal extent: it depends on the longest string
358 // in the combobox
359 if ( m_widget )
360 {
e4e14871 361 ret.x = GetCount() > 0 ? 0 : 60; // start with something "sensible"
0662f990
VZ
362 int width;
363 unsigned int count = GetCount();
364 for ( unsigned int n = 0; n < count; n++ )
365 {
366 GetTextExtent(GetString(n), &width, NULL, NULL, NULL );
367 if ( width + 40 > ret.x ) // 40 for drop down arrow and space around text
368 ret.x = width + 40;
369 }
370 }
371
372 // empty combobox should have some reasonable default size too
373 if ((GetCount() == 0) && (ret.x < 80))
374 ret.x = 80;
375
376 CacheBestSize(ret);
377 return ret;
378}
379
c2193ac9
RR
380void wxChoice::DoApplyWidgetStyle(GtkRcStyle *style)
381{
9dc44eff
PC
382 GTKApplyStyle(m_widget, style);
383 GTKApplyStyle(gtk_bin_get_child(GTK_BIN(m_widget)), style);
c2193ac9
RR
384}
385
386
9d522606
RD
387// static
388wxVisualAttributes
389wxChoice::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant))
390{
a2c94110 391 return GetDefaultAttributesFromGTKWidget(gtk_combo_box_new);
9d522606
RD
392}
393
a73554d4 394
a2c94110 395#endif // wxUSE_CHOICE || wxUSE_COMBOBOX