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