]> git.saurik.com Git - wxWidgets.git/blame - src/gtk/choice.cpp
Fix regression in wxGTK wxFilePickerCtrl due to wxFileDialog changes.
[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{
ce7fe42e 33 choice->SendSelectionChangedEvent(wxEVT_CHOICE);
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
e78c1d78
RR
104void wxChoice::GTKInsertComboBoxTextItem( unsigned int n, const wxString& text )
105{
9dc44eff
PC
106#ifdef __WXGTK3__
107 gtk_combo_box_text_insert_text(GTK_COMBO_BOX_TEXT(m_widget), n, wxGTK_CONV(text));
108#else
e78c1d78 109 gtk_combo_box_insert_text( GTK_COMBO_BOX( m_widget ), n, wxGTK_CONV( text ) );
9dc44eff 110#endif
e78c1d78
RR
111}
112
a236aa20
VZ
113int wxChoice::DoInsertItems(const wxArrayStringsAdapter & items,
114 unsigned int pos,
115 void **clientData, wxClientDataType type)
fd0eed64 116{
a2c94110 117 wxCHECK_MSG( m_widget != NULL, -1, wxT("invalid control") );
29006414 118
a2c94110 119 wxASSERT_MSG( !IsSorted() || (pos == GetCount()),
9a83f860 120 wxT("In a sorted choice data could only be appended"));
243dbf1a 121
a2c94110 122 const int count = items.GetCount();
243dbf1a 123
a2c94110
VZ
124 int n = wxNOT_FOUND;
125
a2c94110 126 for ( int i = 0; i < count; ++i )
a236aa20 127 {
a2c94110
VZ
128 n = pos + i;
129 // If sorted, use this wxSortedArrayStrings to determine
130 // the right insertion point
c272f12f 131 if (m_strings)
a2c94110 132 n = m_strings->Add(items[i]);
ce00f59b 133
e78c1d78 134 GTKInsertComboBoxTextItem( n, items[i] );
243dbf1a 135
a2c94110
VZ
136 m_clientData.Insert( NULL, n );
137 AssignNewItemClientData(n, clientData, i, type);
16edee16
RR
138 }
139
a2c94110 140 InvalidateBestSize();
261a9107 141
a2c94110 142 return n;
fd0eed64 143}
f96aa4d9 144
aa61d352 145void wxChoice::DoSetItemClientData(unsigned int n, void* clientData)
fd0eed64 146{
a236aa20 147 m_clientData[n] = clientData;
fd0eed64
RR
148}
149
aa61d352 150void* wxChoice::DoGetItemClientData(unsigned int n) const
fd0eed64 151{
a236aa20 152 return m_clientData[n];
fd0eed64 153}
29006414 154
a236aa20 155void wxChoice::DoClear()
c801d85f 156{
a2c94110 157 wxCHECK_RET( m_widget != NULL, wxT("invalid control") );
f96aa4d9 158
bce926c5 159 GTKDisableEvents();
a2c94110 160
e78c1d78
RR
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));
29006414 164
a236aa20 165 m_clientData.Clear();
2ee3ee1b 166
a2c94110 167 if (m_strings)
2ee3ee1b 168 m_strings->Clear();
16edee16 169
bce926c5 170 GTKEnableEvents();
a2c94110
VZ
171
172 InvalidateBestSize();
6de97a3b 173}
c801d85f 174
a236aa20 175void wxChoice::DoDeleteOneItem(unsigned int n)
2f6407b9 176{
a2c94110 177 wxCHECK_RET( m_widget != NULL, wxT("invalid control") );
9a83f860 178 wxCHECK_RET( IsValid(n), wxT("invalid index in wxChoice::Delete") );
645420d8 179
e78c1d78
RR
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;
bcfe86f0
VZ
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 }
e78c1d78
RR
190 gtk_list_store_remove( store, &iter );
191
a2c94110
VZ
192 m_clientData.RemoveAt( n );
193 if ( m_strings )
194 m_strings->RemoveAt( n );
e2380ce1 195
a2c94110 196 InvalidateBestSize();
2f6407b9
RR
197}
198
a2c94110 199int wxChoice::FindString( const wxString &item, bool bCase ) const
c801d85f 200{
a2c94110
VZ
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;
fd0eed64 209 int count = 0;
a2c94110 210 do
fd0eed64 211 {
a2c94110 212 GValue value = { 0, };
e78c1d78 213 gtk_tree_model_get_value( model, &iter, m_stringCellIndex, &value );
a2c94110
VZ
214 wxString str = wxGTK_CONV_BACK( g_value_get_string( &value ) );
215 g_value_unset( &value );
29006414 216
a2c94110 217 if (item.IsSameAs( str, bCase ) )
9e691f46 218 return count;
29006414 219
9e691f46 220 count++;
fd0eed64 221 }
a2c94110 222 while ( gtk_tree_model_iter_next(model, &iter) );
29006414 223
0a164d4c 224 return wxNOT_FOUND;
6de97a3b 225}
c801d85f 226
9abe166a 227int wxChoice::GetSelection() const
c801d85f 228{
a2c94110 229 return gtk_combo_box_get_active( GTK_COMBO_BOX( m_widget ) );
6de97a3b 230}
c801d85f 231
a2c94110 232void wxChoice::SetString(unsigned int n, const wxString &text)
6c8a980f 233{
a2c94110
VZ
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") );
6c8a980f 238
a2c94110
VZ
239 GtkTreeModel *model = gtk_combo_box_get_model( combobox );
240 GtkTreeIter iter;
241 if (gtk_tree_model_iter_nth_child (model, &iter, NULL, n))
34b5e560 242 {
a2c94110
VZ
243 GValue value = { 0, };
244 g_value_init( &value, G_TYPE_STRING );
245 g_value_set_string( &value, wxGTK_CONV( text ) );
e78c1d78 246 gtk_list_store_set_value( GTK_LIST_STORE(model), &iter, m_stringCellIndex, &value );
a2c94110 247 g_value_unset( &value );
34b5e560 248 }
a2c94110
VZ
249
250 InvalidateBestSize();
6c8a980f
VZ
251}
252
aa61d352 253wxString wxChoice::GetString(unsigned int n) const
c801d85f 254{
a2c94110
VZ
255 wxCHECK_MSG( m_widget != NULL, wxEmptyString, wxT("invalid control") );
256
257 wxString str;
fd0eed64 258
a2c94110
VZ
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))
c801d85f 263 {
a2c94110 264 GValue value = { 0, };
e78c1d78 265 gtk_tree_model_get_value( model, &iter, m_stringCellIndex, &value );
a2c94110
VZ
266 wxString tmp = wxGTK_CONV_BACK( g_value_get_string( &value ) );
267 g_value_unset( &value );
268 return tmp;
6de97a3b 269 }
29006414 270
a2c94110 271 return str;
6de97a3b 272}
c801d85f 273
aa61d352 274unsigned int wxChoice::GetCount() const
c801d85f 275{
a2c94110
VZ
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;
6de97a3b 288}
c801d85f 289
debe6624 290void wxChoice::SetSelection( int n )
c801d85f 291{
a2c94110 292 wxCHECK_RET( m_widget != NULL, wxT("invalid control") );
29006414 293
bce926c5 294 GTKDisableEvents();
29006414 295
a2c94110
VZ
296 GtkComboBox* combobox = GTK_COMBO_BOX( m_widget );
297 gtk_combo_box_set_active( combobox, n );
29006414 298
bce926c5 299 GTKEnableEvents();
f96aa4d9
RR
300}
301
3f16e52c
RR
302void wxChoice::SetColumns(int n)
303{
304 gtk_combo_box_set_wrap_width(GTK_COMBO_BOX(m_widget), n);
305}
306
307int 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
bce926c5 315void wxChoice::GTKDisableEvents()
e01c8145 316{
a2c94110
VZ
317 g_signal_handlers_block_by_func(m_widget,
318 (gpointer) gtk_choice_changed_callback, this);
e01c8145
VZ
319}
320
bce926c5 321void wxChoice::GTKEnableEvents()
f68586e5 322{
a2c94110
VZ
323 g_signal_handlers_unblock_by_func(m_widget,
324 (gpointer) gtk_choice_changed_callback, this);
f68586e5
VZ
325}
326
ef5c70f9 327GdkWindow *wxChoice::GTKGetWindow(wxArrayGdkWindows& WXUNUSED(windows)) const
2b904684 328{
385e8575 329 return gtk_widget_get_window(m_widget);
2b904684
RR
330}
331
0662f990
VZ
332wxSize wxChoice::DoGetBestSize() const
333{
7eb0acef
VZ
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.
7a78a937
VZ
337 return GetSizeFromTextSize(wxChoiceBase::DoGetBestSize().x);
338}
339
340wxSize 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;
1897abe1 355 gtk_widget_get_preferred_size(childPart, NULL, &req);
7a78a937
VZ
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;
0662f990
VZ
369}
370
c2193ac9
RR
371void wxChoice::DoApplyWidgetStyle(GtkRcStyle *style)
372{
9dc44eff
PC
373 GTKApplyStyle(m_widget, style);
374 GTKApplyStyle(gtk_bin_get_child(GTK_BIN(m_widget)), style);
c2193ac9
RR
375}
376
9d522606
RD
377// static
378wxVisualAttributes
379wxChoice::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant))
380{
7fff16b8 381 return GetDefaultAttributesFromGTKWidget(gtk_combo_box_new());
9d522606
RD
382}
383
a2c94110 384#endif // wxUSE_CHOICE || wxUSE_COMBOBOX