]> git.saurik.com Git - wxWidgets.git/blame_incremental - 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
1/////////////////////////////////////////////////////////////////////////////
2// Name: src/gtk/choice.cpp
3// Purpose:
4// Author: Robert Roebling
5// Id: $Id$
6// Copyright: (c) 1998 Robert Roebling
7// Licence: wxWindows licence
8/////////////////////////////////////////////////////////////////////////////
9
10#include "wx/wxprec.h"
11
12#if wxUSE_CHOICE || wxUSE_COMBOBOX
13
14#include "wx/choice.h"
15
16#ifndef WX_PRECOMP
17 #include "wx/arrstr.h"
18#endif
19
20#include "wx/gtk/private.h"
21
22
23// ----------------------------------------------------------------------------
24// GTK callbacks
25// ----------------------------------------------------------------------------
26
27extern "C" {
28
29static void
30gtk_choice_changed_callback( GtkWidget *WXUNUSED(widget), wxChoice *choice )
31{
32 choice->SendSelectionChangedEvent(wxEVT_COMMAND_CHOICE_SELECTED);
33}
34
35}
36
37//-----------------------------------------------------------------------------
38// wxChoice
39//-----------------------------------------------------------------------------
40
41IMPLEMENT_DYNAMIC_CLASS(wxChoice, wxControlWithItems)
42
43void wxChoice::Init()
44{
45 m_strings = (wxSortedArrayString *)NULL;
46 m_stringCellIndex = 0;
47}
48
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
61bool wxChoice::Create( wxWindow *parent, wxWindowID id,
62 const wxPoint &pos, const wxSize &size,
63 int n, const wxString choices[],
64 long style, const wxValidator& validator,
65 const wxString &name )
66{
67 if (!PreCreation( parent, pos, size ) ||
68 !CreateBase( parent, id, pos, size, style, validator, name ))
69 {
70 wxFAIL_MSG( wxT("wxChoice creation failed") );
71 return false;
72 }
73
74 if ( IsSorted() )
75 {
76 // if our m_strings != NULL, Append() will check for it and insert
77 // items in the correct order
78 m_strings = new wxSortedArrayString;
79 }
80
81 m_widget = gtk_combo_box_new_text();
82
83 Append(n, choices);
84
85 m_parent->DoAddChild( this );
86
87 PostCreation(size);
88
89 g_signal_connect_after (m_widget, "changed",
90 G_CALLBACK (gtk_choice_changed_callback), this);
91
92 return true;
93}
94
95wxChoice::~wxChoice()
96{
97 delete m_strings;
98}
99
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
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
124int wxChoice::DoInsertItems(const wxArrayStringsAdapter & items,
125 unsigned int pos,
126 void **clientData, wxClientDataType type)
127{
128 wxCHECK_MSG( m_widget != NULL, -1, wxT("invalid control") );
129
130 wxASSERT_MSG( !IsSorted() || (pos == GetCount()),
131 _T("In a sorted choice data could only be appended"));
132
133 const int count = items.GetCount();
134
135 int n = wxNOT_FOUND;
136
137 for ( int i = 0; i < count; ++i )
138 {
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]);
144
145 GTKInsertComboBoxTextItem( n, items[i] );
146
147 m_clientData.Insert( NULL, n );
148 AssignNewItemClientData(n, clientData, i, type);
149 }
150
151 InvalidateBestSize();
152
153 return n;
154}
155
156void wxChoice::DoSetItemClientData(unsigned int n, void* clientData)
157{
158 m_clientData[n] = clientData;
159}
160
161void* wxChoice::DoGetItemClientData(unsigned int n) const
162{
163 return m_clientData[n];
164}
165
166void wxChoice::DoClear()
167{
168 wxCHECK_RET( m_widget != NULL, wxT("invalid control") );
169
170 DisableEvents();
171
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));
175
176 m_clientData.Clear();
177
178 if (m_strings)
179 m_strings->Clear();
180
181 EnableEvents();
182
183 InvalidateBestSize();
184}
185
186void wxChoice::DoDeleteOneItem(unsigned int n)
187{
188 wxCHECK_RET( m_widget != NULL, wxT("invalid control") );
189 wxCHECK_RET( IsValid(n), _T("invalid index in wxChoice::Delete") );
190
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
199 m_clientData.RemoveAt( n );
200 if ( m_strings )
201 m_strings->RemoveAt( n );
202
203 InvalidateBestSize();
204}
205
206int wxChoice::FindString( const wxString &item, bool bCase ) const
207{
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;
216 int count = 0;
217 do
218 {
219 GValue value = { 0, };
220 gtk_tree_model_get_value( model, &iter, m_stringCellIndex, &value );
221 wxString str = wxGTK_CONV_BACK( g_value_get_string( &value ) );
222 g_value_unset( &value );
223
224 if (item.IsSameAs( str, bCase ) )
225 return count;
226
227 count++;
228 }
229 while ( gtk_tree_model_iter_next(model, &iter) );
230
231 return wxNOT_FOUND;
232}
233
234int wxChoice::GetSelection() const
235{
236 return gtk_combo_box_get_active( GTK_COMBO_BOX( m_widget ) );
237}
238
239void wxChoice::SetString(unsigned int n, const wxString &text)
240{
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") );
245
246 GtkTreeModel *model = gtk_combo_box_get_model( combobox );
247 GtkTreeIter iter;
248 if (gtk_tree_model_iter_nth_child (model, &iter, NULL, n))
249 {
250 GValue value = { 0, };
251 g_value_init( &value, G_TYPE_STRING );
252 g_value_set_string( &value, wxGTK_CONV( text ) );
253 gtk_list_store_set_value( GTK_LIST_STORE(model), &iter, m_stringCellIndex, &value );
254 g_value_unset( &value );
255 }
256
257 InvalidateBestSize();
258}
259
260wxString wxChoice::GetString(unsigned int n) const
261{
262 wxCHECK_MSG( m_widget != NULL, wxEmptyString, wxT("invalid control") );
263
264 wxString str;
265
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))
270 {
271 GValue value = { 0, };
272 gtk_tree_model_get_value( model, &iter, m_stringCellIndex, &value );
273 wxString tmp = wxGTK_CONV_BACK( g_value_get_string( &value ) );
274 g_value_unset( &value );
275 return tmp;
276 }
277
278 return str;
279}
280
281unsigned int wxChoice::GetCount() const
282{
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;
295}
296
297void wxChoice::SetSelection( int n )
298{
299 wxCHECK_RET( m_widget != NULL, wxT("invalid control") );
300
301 DisableEvents();
302
303 GtkComboBox* combobox = GTK_COMBO_BOX( m_widget );
304 gtk_combo_box_set_active( combobox, n );
305
306 EnableEvents();
307}
308
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
323void wxChoice::DisableEvents()
324{
325 g_signal_handlers_block_by_func(m_widget,
326 (gpointer) gtk_choice_changed_callback, this);
327}
328
329void wxChoice::EnableEvents()
330{
331 g_signal_handlers_unblock_by_func(m_widget,
332 (gpointer) gtk_choice_changed_callback, this);
333}
334
335
336GdkWindow *wxChoice::GTKGetWindow(wxArrayGdkWindows& WXUNUSED(windows)) const
337{
338 return m_widget->window;
339}
340
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
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
379// static
380wxVisualAttributes
381wxChoice::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant))
382{
383 return GetDefaultAttributesFromGTKWidget(gtk_combo_box_new);
384}
385
386
387#endif // wxUSE_CHOICE || wxUSE_COMBOBOX