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