]>
Commit | Line | Data |
---|---|---|
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 | 28 | extern "C" { |
6c8a980f | 29 | |
a2c94110 VZ |
30 | static void |
31 | gtk_choice_changed_callback( GtkWidget *WXUNUSED(widget), wxChoice *choice ) | |
32 | { | |
33 | choice->SendSelectionChangedEvent(wxEVT_COMMAND_CHOICE_SELECTED); | |
6de97a3b | 34 | } |
a2c94110 | 35 | |
865bb325 | 36 | } |
c801d85f | 37 | |
e1e955e1 RR |
38 | //----------------------------------------------------------------------------- |
39 | // wxChoice | |
c801d85f KB |
40 | //----------------------------------------------------------------------------- |
41 | ||
e78c1d78 | 42 | void wxChoice::Init() |
c801d85f | 43 | { |
d3b9f782 | 44 | m_strings = NULL; |
e78c1d78 | 45 | m_stringCellIndex = 0; |
6de97a3b | 46 | } |
c801d85f | 47 | |
584ad2a3 MB |
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 | ||
debe6624 | 60 | bool 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 |
99 | wxChoice::~wxChoice() |
100 | { | |
e01c8145 | 101 | delete m_strings; |
fd0eed64 RR |
102 | } |
103 | ||
a2c94110 VZ |
104 | void wxChoice::SendSelectionChangedEvent(wxEventType evt_type) |
105 | { | |
a2c94110 VZ |
106 | if (GetSelection() == -1) |
107 | return; | |
108 | ||
109 | wxCommandEvent event( evt_type, GetId() ); | |
110 | ||
111 | int n = GetSelection(); | |
112 | event.SetInt( n ); | |
113 | event.SetString( GetStringSelection() ); | |
114 | event.SetEventObject( this ); | |
115 | InitCommandEventWithItems( event, n ); | |
116 | ||
117 | HandleWindowEvent( event ); | |
118 | } | |
119 | ||
e78c1d78 RR |
120 | void wxChoice::GTKInsertComboBoxTextItem( unsigned int n, const wxString& text ) |
121 | { | |
9dc44eff PC |
122 | #ifdef __WXGTK3__ |
123 | gtk_combo_box_text_insert_text(GTK_COMBO_BOX_TEXT(m_widget), n, wxGTK_CONV(text)); | |
124 | #else | |
e78c1d78 | 125 | gtk_combo_box_insert_text( GTK_COMBO_BOX( m_widget ), n, wxGTK_CONV( text ) ); |
9dc44eff | 126 | #endif |
e78c1d78 RR |
127 | } |
128 | ||
a236aa20 VZ |
129 | int wxChoice::DoInsertItems(const wxArrayStringsAdapter & items, |
130 | unsigned int pos, | |
131 | void **clientData, wxClientDataType type) | |
fd0eed64 | 132 | { |
a2c94110 | 133 | wxCHECK_MSG( m_widget != NULL, -1, wxT("invalid control") ); |
29006414 | 134 | |
a2c94110 | 135 | wxASSERT_MSG( !IsSorted() || (pos == GetCount()), |
9a83f860 | 136 | wxT("In a sorted choice data could only be appended")); |
243dbf1a | 137 | |
a2c94110 | 138 | const int count = items.GetCount(); |
243dbf1a | 139 | |
a2c94110 VZ |
140 | int n = wxNOT_FOUND; |
141 | ||
a2c94110 | 142 | for ( int i = 0; i < count; ++i ) |
a236aa20 | 143 | { |
a2c94110 VZ |
144 | n = pos + i; |
145 | // If sorted, use this wxSortedArrayStrings to determine | |
146 | // the right insertion point | |
c272f12f | 147 | if (m_strings) |
a2c94110 | 148 | n = m_strings->Add(items[i]); |
ce00f59b | 149 | |
e78c1d78 | 150 | GTKInsertComboBoxTextItem( n, items[i] ); |
243dbf1a | 151 | |
a2c94110 VZ |
152 | m_clientData.Insert( NULL, n ); |
153 | AssignNewItemClientData(n, clientData, i, type); | |
16edee16 RR |
154 | } |
155 | ||
a2c94110 | 156 | InvalidateBestSize(); |
261a9107 | 157 | |
a2c94110 | 158 | return n; |
fd0eed64 | 159 | } |
f96aa4d9 | 160 | |
aa61d352 | 161 | void wxChoice::DoSetItemClientData(unsigned int n, void* clientData) |
fd0eed64 | 162 | { |
a236aa20 | 163 | m_clientData[n] = clientData; |
fd0eed64 RR |
164 | } |
165 | ||
aa61d352 | 166 | void* wxChoice::DoGetItemClientData(unsigned int n) const |
fd0eed64 | 167 | { |
a236aa20 | 168 | return m_clientData[n]; |
fd0eed64 | 169 | } |
29006414 | 170 | |
a236aa20 | 171 | void wxChoice::DoClear() |
c801d85f | 172 | { |
a2c94110 | 173 | wxCHECK_RET( m_widget != NULL, wxT("invalid control") ); |
f96aa4d9 | 174 | |
bce926c5 | 175 | GTKDisableEvents(); |
a2c94110 | 176 | |
e78c1d78 RR |
177 | GtkComboBox* combobox = GTK_COMBO_BOX( m_widget ); |
178 | GtkTreeModel* model = gtk_combo_box_get_model( combobox ); | |
179 | gtk_list_store_clear(GTK_LIST_STORE(model)); | |
29006414 | 180 | |
a236aa20 | 181 | m_clientData.Clear(); |
2ee3ee1b | 182 | |
a2c94110 | 183 | if (m_strings) |
2ee3ee1b | 184 | m_strings->Clear(); |
16edee16 | 185 | |
bce926c5 | 186 | GTKEnableEvents(); |
a2c94110 VZ |
187 | |
188 | InvalidateBestSize(); | |
6de97a3b | 189 | } |
c801d85f | 190 | |
a236aa20 | 191 | void wxChoice::DoDeleteOneItem(unsigned int n) |
2f6407b9 | 192 | { |
a2c94110 | 193 | wxCHECK_RET( m_widget != NULL, wxT("invalid control") ); |
9a83f860 | 194 | wxCHECK_RET( IsValid(n), wxT("invalid index in wxChoice::Delete") ); |
645420d8 | 195 | |
e78c1d78 RR |
196 | GtkComboBox* combobox = GTK_COMBO_BOX( m_widget ); |
197 | GtkTreeModel* model = gtk_combo_box_get_model( combobox ); | |
198 | GtkListStore* store = GTK_LIST_STORE(model); | |
199 | GtkTreeIter iter; | |
200 | gtk_tree_model_iter_nth_child( model, &iter, | |
201 | NULL, (gint) n ); | |
202 | gtk_list_store_remove( store, &iter ); | |
203 | ||
a2c94110 VZ |
204 | m_clientData.RemoveAt( n ); |
205 | if ( m_strings ) | |
206 | m_strings->RemoveAt( n ); | |
e2380ce1 | 207 | |
a2c94110 | 208 | InvalidateBestSize(); |
2f6407b9 RR |
209 | } |
210 | ||
a2c94110 | 211 | int wxChoice::FindString( const wxString &item, bool bCase ) const |
c801d85f | 212 | { |
a2c94110 VZ |
213 | wxCHECK_MSG( m_widget != NULL, wxNOT_FOUND, wxT("invalid control") ); |
214 | ||
215 | GtkComboBox* combobox = GTK_COMBO_BOX( m_widget ); | |
216 | GtkTreeModel* model = gtk_combo_box_get_model( combobox ); | |
217 | GtkTreeIter iter; | |
218 | gtk_tree_model_get_iter_first( model, &iter ); | |
219 | if (!gtk_list_store_iter_is_valid(GTK_LIST_STORE(model), &iter )) | |
220 | return -1; | |
fd0eed64 | 221 | int count = 0; |
a2c94110 | 222 | do |
fd0eed64 | 223 | { |
a2c94110 | 224 | GValue value = { 0, }; |
e78c1d78 | 225 | gtk_tree_model_get_value( model, &iter, m_stringCellIndex, &value ); |
a2c94110 VZ |
226 | wxString str = wxGTK_CONV_BACK( g_value_get_string( &value ) ); |
227 | g_value_unset( &value ); | |
29006414 | 228 | |
a2c94110 | 229 | if (item.IsSameAs( str, bCase ) ) |
9e691f46 | 230 | return count; |
29006414 | 231 | |
9e691f46 | 232 | count++; |
fd0eed64 | 233 | } |
a2c94110 | 234 | while ( gtk_tree_model_iter_next(model, &iter) ); |
29006414 | 235 | |
0a164d4c | 236 | return wxNOT_FOUND; |
6de97a3b | 237 | } |
c801d85f | 238 | |
9abe166a | 239 | int wxChoice::GetSelection() const |
c801d85f | 240 | { |
a2c94110 | 241 | return gtk_combo_box_get_active( GTK_COMBO_BOX( m_widget ) ); |
6de97a3b | 242 | } |
c801d85f | 243 | |
a2c94110 | 244 | void wxChoice::SetString(unsigned int n, const wxString &text) |
6c8a980f | 245 | { |
a2c94110 VZ |
246 | wxCHECK_RET( m_widget != NULL, wxT("invalid control") ); |
247 | ||
248 | GtkComboBox* combobox = GTK_COMBO_BOX( m_widget ); | |
249 | wxCHECK_RET( IsValid(n), wxT("invalid index") ); | |
6c8a980f | 250 | |
a2c94110 VZ |
251 | GtkTreeModel *model = gtk_combo_box_get_model( combobox ); |
252 | GtkTreeIter iter; | |
253 | if (gtk_tree_model_iter_nth_child (model, &iter, NULL, n)) | |
34b5e560 | 254 | { |
a2c94110 VZ |
255 | GValue value = { 0, }; |
256 | g_value_init( &value, G_TYPE_STRING ); | |
257 | g_value_set_string( &value, wxGTK_CONV( text ) ); | |
e78c1d78 | 258 | gtk_list_store_set_value( GTK_LIST_STORE(model), &iter, m_stringCellIndex, &value ); |
a2c94110 | 259 | g_value_unset( &value ); |
34b5e560 | 260 | } |
a2c94110 VZ |
261 | |
262 | InvalidateBestSize(); | |
6c8a980f VZ |
263 | } |
264 | ||
aa61d352 | 265 | wxString wxChoice::GetString(unsigned int n) const |
c801d85f | 266 | { |
a2c94110 VZ |
267 | wxCHECK_MSG( m_widget != NULL, wxEmptyString, wxT("invalid control") ); |
268 | ||
269 | wxString str; | |
fd0eed64 | 270 | |
a2c94110 VZ |
271 | GtkComboBox* combobox = GTK_COMBO_BOX( m_widget ); |
272 | GtkTreeModel *model = gtk_combo_box_get_model( combobox ); | |
273 | GtkTreeIter iter; | |
274 | if (gtk_tree_model_iter_nth_child (model, &iter, NULL, n)) | |
c801d85f | 275 | { |
a2c94110 | 276 | GValue value = { 0, }; |
e78c1d78 | 277 | gtk_tree_model_get_value( model, &iter, m_stringCellIndex, &value ); |
a2c94110 VZ |
278 | wxString tmp = wxGTK_CONV_BACK( g_value_get_string( &value ) ); |
279 | g_value_unset( &value ); | |
280 | return tmp; | |
6de97a3b | 281 | } |
29006414 | 282 | |
a2c94110 | 283 | return str; |
6de97a3b | 284 | } |
c801d85f | 285 | |
aa61d352 | 286 | unsigned int wxChoice::GetCount() const |
c801d85f | 287 | { |
a2c94110 VZ |
288 | wxCHECK_MSG( m_widget != NULL, 0, wxT("invalid control") ); |
289 | ||
290 | GtkComboBox* combobox = GTK_COMBO_BOX( m_widget ); | |
291 | GtkTreeModel* model = gtk_combo_box_get_model( combobox ); | |
292 | GtkTreeIter iter; | |
293 | gtk_tree_model_get_iter_first( model, &iter ); | |
294 | if (!gtk_list_store_iter_is_valid(GTK_LIST_STORE(model), &iter )) | |
295 | return 0; | |
296 | unsigned int ret = 1; | |
297 | while (gtk_tree_model_iter_next( model, &iter )) | |
298 | ret++; | |
299 | return ret; | |
6de97a3b | 300 | } |
c801d85f | 301 | |
debe6624 | 302 | void wxChoice::SetSelection( int n ) |
c801d85f | 303 | { |
a2c94110 | 304 | wxCHECK_RET( m_widget != NULL, wxT("invalid control") ); |
29006414 | 305 | |
bce926c5 | 306 | GTKDisableEvents(); |
29006414 | 307 | |
a2c94110 VZ |
308 | GtkComboBox* combobox = GTK_COMBO_BOX( m_widget ); |
309 | gtk_combo_box_set_active( combobox, n ); | |
29006414 | 310 | |
bce926c5 | 311 | GTKEnableEvents(); |
f96aa4d9 RR |
312 | } |
313 | ||
3f16e52c RR |
314 | void wxChoice::SetColumns(int n) |
315 | { | |
316 | gtk_combo_box_set_wrap_width(GTK_COMBO_BOX(m_widget), n); | |
317 | } | |
318 | ||
319 | int wxChoice::GetColumns() const | |
320 | { | |
321 | // gtk_combo_box_get_wrap_width() was added in gtk 2.6 | |
322 | gint intval; | |
323 | g_object_get(G_OBJECT(m_widget), "wrap-width", &intval, NULL); | |
324 | return intval; | |
325 | } | |
326 | ||
bce926c5 | 327 | void wxChoice::GTKDisableEvents() |
e01c8145 | 328 | { |
a2c94110 VZ |
329 | g_signal_handlers_block_by_func(m_widget, |
330 | (gpointer) gtk_choice_changed_callback, this); | |
e01c8145 VZ |
331 | } |
332 | ||
bce926c5 | 333 | void wxChoice::GTKEnableEvents() |
f68586e5 | 334 | { |
a2c94110 VZ |
335 | g_signal_handlers_unblock_by_func(m_widget, |
336 | (gpointer) gtk_choice_changed_callback, this); | |
f68586e5 VZ |
337 | } |
338 | ||
ef5c70f9 | 339 | GdkWindow *wxChoice::GTKGetWindow(wxArrayGdkWindows& WXUNUSED(windows)) const |
2b904684 | 340 | { |
385e8575 | 341 | return gtk_widget_get_window(m_widget); |
2b904684 RR |
342 | } |
343 | ||
0662f990 VZ |
344 | wxSize wxChoice::DoGetBestSize() const |
345 | { | |
7eb0acef VZ |
346 | // Get the height of the control from GTK+ itself, but use our own version |
347 | // to compute the width large enough to show all our strings as GTK+ | |
348 | // doesn't seem to take the control contents into account. | |
7a78a937 VZ |
349 | return GetSizeFromTextSize(wxChoiceBase::DoGetBestSize().x); |
350 | } | |
351 | ||
352 | wxSize wxChoice::DoGetSizeFromTextSize(int xlen, int ylen) const | |
353 | { | |
354 | wxASSERT_MSG( m_widget, wxS("GetSizeFromTextSize called before creation") ); | |
355 | ||
356 | // a GtkEntry for wxComboBox and a GtkCellView for wxChoice | |
357 | GtkWidget* childPart = gtk_bin_get_child(GTK_BIN(m_widget)); | |
358 | ||
359 | // Set a as small as possible size for the control, so preferred sizes | |
360 | // return "natural" sizes, not taking into account the previous ones (which | |
361 | // seems to be GTK+3 behaviour) | |
362 | gtk_widget_set_size_request(m_widget, 0, 0); | |
363 | ||
364 | // We are interested in the difference of sizes between the whole contol | |
365 | // and its child part. I.e. arrow, separators, etc. | |
366 | GtkRequisition req; | |
1897abe1 | 367 | gtk_widget_get_preferred_size(childPart, NULL, &req); |
7a78a937 VZ |
368 | wxSize totalS = GTKGetPreferredSize(m_widget); |
369 | ||
370 | wxSize tsize(xlen + totalS.x - req.width, totalS.y); | |
371 | ||
372 | // For a wxChoice, not for wxComboBox, add some margins | |
373 | if ( !GTK_IS_ENTRY(childPart) ) | |
374 | tsize.IncBy(5, 0); | |
375 | ||
376 | // Perhaps the user wants something different from CharHeight | |
377 | if ( ylen > 0 ) | |
378 | tsize.IncBy(0, ylen - GetCharHeight()); | |
379 | ||
380 | return tsize; | |
0662f990 VZ |
381 | } |
382 | ||
c2193ac9 RR |
383 | void wxChoice::DoApplyWidgetStyle(GtkRcStyle *style) |
384 | { | |
9dc44eff PC |
385 | GTKApplyStyle(m_widget, style); |
386 | GTKApplyStyle(gtk_bin_get_child(GTK_BIN(m_widget)), style); | |
c2193ac9 RR |
387 | } |
388 | ||
9d522606 RD |
389 | // static |
390 | wxVisualAttributes | |
391 | wxChoice::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant)) | |
392 | { | |
7fff16b8 | 393 | return GetDefaultAttributesFromGTKWidget(gtk_combo_box_new()); |
9d522606 RD |
394 | } |
395 | ||
a2c94110 | 396 | #endif // wxUSE_CHOICE || wxUSE_COMBOBOX |