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