]> git.saurik.com Git - wxWidgets.git/blob - src/gtk/choice.cpp
Fix regression in wxGTK wxFilePickerCtrl due to wxFileDialog changes.
[wxWidgets.git] / src / gtk / choice.cpp
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 <gtk/gtk.h>
21 #include "wx/gtk/private.h"
22 #include "wx/gtk/private/gtk2-compat.h"
23
24 // ----------------------------------------------------------------------------
25 // GTK callbacks
26 // ----------------------------------------------------------------------------
27
28 extern "C" {
29
30 static void
31 gtk_choice_changed_callback( GtkWidget *WXUNUSED(widget), wxChoice *choice )
32 {
33 choice->SendSelectionChangedEvent(wxEVT_CHOICE);
34 }
35
36 }
37
38 //-----------------------------------------------------------------------------
39 // wxChoice
40 //-----------------------------------------------------------------------------
41
42 void wxChoice::Init()
43 {
44 m_strings = NULL;
45 m_stringCellIndex = 0;
46 }
47
48 bool 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
60 bool wxChoice::Create( wxWindow *parent, wxWindowID id,
61 const wxPoint &pos, const wxSize &size,
62 int n, const wxString choices[],
63 long style, const wxValidator& validator,
64 const wxString &name )
65 {
66 if (!PreCreation( parent, pos, size ) ||
67 !CreateBase( parent, id, pos, size, style, validator, name ))
68 {
69 wxFAIL_MSG( wxT("wxChoice creation failed") );
70 return false;
71 }
72
73 if ( IsSorted() )
74 {
75 // if our m_strings != NULL, Append() will check for it and insert
76 // items in the correct order
77 m_strings = new wxGtkCollatedArrayString;
78 }
79
80 #ifdef __WXGTK3__
81 m_widget = gtk_combo_box_text_new();
82 #else
83 m_widget = gtk_combo_box_new_text();
84 #endif
85 g_object_ref(m_widget);
86
87 Append(n, choices);
88
89 m_parent->DoAddChild( this );
90
91 PostCreation(size);
92
93 g_signal_connect_after (m_widget, "changed",
94 G_CALLBACK (gtk_choice_changed_callback), this);
95
96 return true;
97 }
98
99 wxChoice::~wxChoice()
100 {
101 delete m_strings;
102 }
103
104 void wxChoice::GTKInsertComboBoxTextItem( unsigned int n, const wxString& text )
105 {
106 #ifdef __WXGTK3__
107 gtk_combo_box_text_insert_text(GTK_COMBO_BOX_TEXT(m_widget), n, wxGTK_CONV(text));
108 #else
109 gtk_combo_box_insert_text( GTK_COMBO_BOX( m_widget ), n, wxGTK_CONV( text ) );
110 #endif
111 }
112
113 int wxChoice::DoInsertItems(const wxArrayStringsAdapter & items,
114 unsigned int pos,
115 void **clientData, wxClientDataType type)
116 {
117 wxCHECK_MSG( m_widget != NULL, -1, wxT("invalid control") );
118
119 wxASSERT_MSG( !IsSorted() || (pos == GetCount()),
120 wxT("In a sorted choice data could only be appended"));
121
122 const int count = items.GetCount();
123
124 int n = wxNOT_FOUND;
125
126 for ( int i = 0; i < count; ++i )
127 {
128 n = pos + i;
129 // If sorted, use this wxSortedArrayStrings to determine
130 // the right insertion point
131 if (m_strings)
132 n = m_strings->Add(items[i]);
133
134 GTKInsertComboBoxTextItem( n, items[i] );
135
136 m_clientData.Insert( NULL, n );
137 AssignNewItemClientData(n, clientData, i, type);
138 }
139
140 InvalidateBestSize();
141
142 return n;
143 }
144
145 void wxChoice::DoSetItemClientData(unsigned int n, void* clientData)
146 {
147 m_clientData[n] = clientData;
148 }
149
150 void* wxChoice::DoGetItemClientData(unsigned int n) const
151 {
152 return m_clientData[n];
153 }
154
155 void wxChoice::DoClear()
156 {
157 wxCHECK_RET( m_widget != NULL, wxT("invalid control") );
158
159 GTKDisableEvents();
160
161 GtkComboBox* combobox = GTK_COMBO_BOX( m_widget );
162 GtkTreeModel* model = gtk_combo_box_get_model( combobox );
163 gtk_list_store_clear(GTK_LIST_STORE(model));
164
165 m_clientData.Clear();
166
167 if (m_strings)
168 m_strings->Clear();
169
170 GTKEnableEvents();
171
172 InvalidateBestSize();
173 }
174
175 void wxChoice::DoDeleteOneItem(unsigned int n)
176 {
177 wxCHECK_RET( m_widget != NULL, wxT("invalid control") );
178 wxCHECK_RET( IsValid(n), wxT("invalid index in wxChoice::Delete") );
179
180 GtkComboBox* combobox = GTK_COMBO_BOX( m_widget );
181 GtkTreeModel* model = gtk_combo_box_get_model( combobox );
182 GtkListStore* store = GTK_LIST_STORE(model);
183 GtkTreeIter iter;
184 if ( !gtk_tree_model_iter_nth_child(model, &iter, NULL, n) )
185 {
186 // This is really not supposed to happen for a valid index.
187 wxFAIL_MSG(wxS("Item unexpectedly not found."));
188 return;
189 }
190 gtk_list_store_remove( store, &iter );
191
192 m_clientData.RemoveAt( n );
193 if ( m_strings )
194 m_strings->RemoveAt( n );
195
196 InvalidateBestSize();
197 }
198
199 int wxChoice::FindString( const wxString &item, bool bCase ) const
200 {
201 wxCHECK_MSG( m_widget != NULL, wxNOT_FOUND, wxT("invalid control") );
202
203 GtkComboBox* combobox = GTK_COMBO_BOX( m_widget );
204 GtkTreeModel* model = gtk_combo_box_get_model( combobox );
205 GtkTreeIter iter;
206 gtk_tree_model_get_iter_first( model, &iter );
207 if (!gtk_list_store_iter_is_valid(GTK_LIST_STORE(model), &iter ))
208 return -1;
209 int count = 0;
210 do
211 {
212 GValue value = { 0, };
213 gtk_tree_model_get_value( model, &iter, m_stringCellIndex, &value );
214 wxString str = wxGTK_CONV_BACK( g_value_get_string( &value ) );
215 g_value_unset( &value );
216
217 if (item.IsSameAs( str, bCase ) )
218 return count;
219
220 count++;
221 }
222 while ( gtk_tree_model_iter_next(model, &iter) );
223
224 return wxNOT_FOUND;
225 }
226
227 int wxChoice::GetSelection() const
228 {
229 return gtk_combo_box_get_active( GTK_COMBO_BOX( m_widget ) );
230 }
231
232 void wxChoice::SetString(unsigned int n, const wxString &text)
233 {
234 wxCHECK_RET( m_widget != NULL, wxT("invalid control") );
235
236 GtkComboBox* combobox = GTK_COMBO_BOX( m_widget );
237 wxCHECK_RET( IsValid(n), wxT("invalid index") );
238
239 GtkTreeModel *model = gtk_combo_box_get_model( combobox );
240 GtkTreeIter iter;
241 if (gtk_tree_model_iter_nth_child (model, &iter, NULL, n))
242 {
243 GValue value = { 0, };
244 g_value_init( &value, G_TYPE_STRING );
245 g_value_set_string( &value, wxGTK_CONV( text ) );
246 gtk_list_store_set_value( GTK_LIST_STORE(model), &iter, m_stringCellIndex, &value );
247 g_value_unset( &value );
248 }
249
250 InvalidateBestSize();
251 }
252
253 wxString wxChoice::GetString(unsigned int n) const
254 {
255 wxCHECK_MSG( m_widget != NULL, wxEmptyString, wxT("invalid control") );
256
257 wxString str;
258
259 GtkComboBox* combobox = GTK_COMBO_BOX( m_widget );
260 GtkTreeModel *model = gtk_combo_box_get_model( combobox );
261 GtkTreeIter iter;
262 if (gtk_tree_model_iter_nth_child (model, &iter, NULL, n))
263 {
264 GValue value = { 0, };
265 gtk_tree_model_get_value( model, &iter, m_stringCellIndex, &value );
266 wxString tmp = wxGTK_CONV_BACK( g_value_get_string( &value ) );
267 g_value_unset( &value );
268 return tmp;
269 }
270
271 return str;
272 }
273
274 unsigned int wxChoice::GetCount() const
275 {
276 wxCHECK_MSG( m_widget != NULL, 0, wxT("invalid control") );
277
278 GtkComboBox* combobox = GTK_COMBO_BOX( m_widget );
279 GtkTreeModel* model = gtk_combo_box_get_model( combobox );
280 GtkTreeIter iter;
281 gtk_tree_model_get_iter_first( model, &iter );
282 if (!gtk_list_store_iter_is_valid(GTK_LIST_STORE(model), &iter ))
283 return 0;
284 unsigned int ret = 1;
285 while (gtk_tree_model_iter_next( model, &iter ))
286 ret++;
287 return ret;
288 }
289
290 void wxChoice::SetSelection( int n )
291 {
292 wxCHECK_RET( m_widget != NULL, wxT("invalid control") );
293
294 GTKDisableEvents();
295
296 GtkComboBox* combobox = GTK_COMBO_BOX( m_widget );
297 gtk_combo_box_set_active( combobox, n );
298
299 GTKEnableEvents();
300 }
301
302 void wxChoice::SetColumns(int n)
303 {
304 gtk_combo_box_set_wrap_width(GTK_COMBO_BOX(m_widget), n);
305 }
306
307 int wxChoice::GetColumns() const
308 {
309 // gtk_combo_box_get_wrap_width() was added in gtk 2.6
310 gint intval;
311 g_object_get(G_OBJECT(m_widget), "wrap-width", &intval, NULL);
312 return intval;
313 }
314
315 void wxChoice::GTKDisableEvents()
316 {
317 g_signal_handlers_block_by_func(m_widget,
318 (gpointer) gtk_choice_changed_callback, this);
319 }
320
321 void wxChoice::GTKEnableEvents()
322 {
323 g_signal_handlers_unblock_by_func(m_widget,
324 (gpointer) gtk_choice_changed_callback, this);
325 }
326
327 GdkWindow *wxChoice::GTKGetWindow(wxArrayGdkWindows& WXUNUSED(windows)) const
328 {
329 return gtk_widget_get_window(m_widget);
330 }
331
332 wxSize wxChoice::DoGetBestSize() const
333 {
334 // Get the height of the control from GTK+ itself, but use our own version
335 // to compute the width large enough to show all our strings as GTK+
336 // doesn't seem to take the control contents into account.
337 return GetSizeFromTextSize(wxChoiceBase::DoGetBestSize().x);
338 }
339
340 wxSize wxChoice::DoGetSizeFromTextSize(int xlen, int ylen) const
341 {
342 wxASSERT_MSG( m_widget, wxS("GetSizeFromTextSize called before creation") );
343
344 // a GtkEntry for wxComboBox and a GtkCellView for wxChoice
345 GtkWidget* childPart = gtk_bin_get_child(GTK_BIN(m_widget));
346
347 // Set a as small as possible size for the control, so preferred sizes
348 // return "natural" sizes, not taking into account the previous ones (which
349 // seems to be GTK+3 behaviour)
350 gtk_widget_set_size_request(m_widget, 0, 0);
351
352 // We are interested in the difference of sizes between the whole contol
353 // and its child part. I.e. arrow, separators, etc.
354 GtkRequisition req;
355 gtk_widget_get_preferred_size(childPart, NULL, &req);
356 wxSize totalS = GTKGetPreferredSize(m_widget);
357
358 wxSize tsize(xlen + totalS.x - req.width, totalS.y);
359
360 // For a wxChoice, not for wxComboBox, add some margins
361 if ( !GTK_IS_ENTRY(childPart) )
362 tsize.IncBy(5, 0);
363
364 // Perhaps the user wants something different from CharHeight
365 if ( ylen > 0 )
366 tsize.IncBy(0, ylen - GetCharHeight());
367
368 return tsize;
369 }
370
371 void wxChoice::DoApplyWidgetStyle(GtkRcStyle *style)
372 {
373 GTKApplyStyle(m_widget, style);
374 GTKApplyStyle(gtk_bin_get_child(GTK_BIN(m_widget)), style);
375 }
376
377 // static
378 wxVisualAttributes
379 wxChoice::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant))
380 {
381 return GetDefaultAttributesFromGTKWidget(gtk_combo_box_new());
382 }
383
384 #endif // wxUSE_CHOICE || wxUSE_COMBOBOX