]>
Commit | Line | Data |
---|---|---|
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 | ||
27 | extern "C" { | |
28 | ||
29 | static void | |
30 | gtk_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 | ||
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 | m_widget = gtk_combo_box_new_text(); | |
80 | g_object_ref(m_widget); | |
81 | ||
82 | Append(n, choices); | |
83 | ||
84 | m_parent->DoAddChild( this ); | |
85 | ||
86 | PostCreation(size); | |
87 | ||
88 | g_signal_connect_after (m_widget, "changed", | |
89 | G_CALLBACK (gtk_choice_changed_callback), this); | |
90 | ||
91 | return true; | |
92 | } | |
93 | ||
94 | wxChoice::~wxChoice() | |
95 | { | |
96 | delete m_strings; | |
97 | } | |
98 | ||
99 | void wxChoice::SendSelectionChangedEvent(wxEventType evt_type) | |
100 | { | |
101 | if (!m_hasVMT) | |
102 | return; | |
103 | ||
104 | if (GetSelection() == -1) | |
105 | return; | |
106 | ||
107 | wxCommandEvent event( evt_type, GetId() ); | |
108 | ||
109 | int n = GetSelection(); | |
110 | event.SetInt( n ); | |
111 | event.SetString( GetStringSelection() ); | |
112 | event.SetEventObject( this ); | |
113 | InitCommandEventWithItems( event, n ); | |
114 | ||
115 | HandleWindowEvent( event ); | |
116 | } | |
117 | ||
118 | void wxChoice::GTKInsertComboBoxTextItem( unsigned int n, const wxString& text ) | |
119 | { | |
120 | gtk_combo_box_insert_text( GTK_COMBO_BOX( m_widget ), n, wxGTK_CONV( text ) ); | |
121 | } | |
122 | ||
123 | int wxChoice::DoInsertItems(const wxArrayStringsAdapter & items, | |
124 | unsigned int pos, | |
125 | void **clientData, wxClientDataType type) | |
126 | { | |
127 | wxCHECK_MSG( m_widget != NULL, -1, wxT("invalid control") ); | |
128 | ||
129 | wxASSERT_MSG( !IsSorted() || (pos == GetCount()), | |
130 | wxT("In a sorted choice data could only be appended")); | |
131 | ||
132 | const int count = items.GetCount(); | |
133 | ||
134 | int n = wxNOT_FOUND; | |
135 | ||
136 | for ( int i = 0; i < count; ++i ) | |
137 | { | |
138 | n = pos + i; | |
139 | // If sorted, use this wxSortedArrayStrings to determine | |
140 | // the right insertion point | |
141 | if (m_strings) | |
142 | n = m_strings->Add(items[i]); | |
143 | ||
144 | GTKInsertComboBoxTextItem( n, items[i] ); | |
145 | ||
146 | m_clientData.Insert( NULL, n ); | |
147 | AssignNewItemClientData(n, clientData, i, type); | |
148 | } | |
149 | ||
150 | InvalidateBestSize(); | |
151 | ||
152 | return n; | |
153 | } | |
154 | ||
155 | void wxChoice::DoSetItemClientData(unsigned int n, void* clientData) | |
156 | { | |
157 | m_clientData[n] = clientData; | |
158 | } | |
159 | ||
160 | void* wxChoice::DoGetItemClientData(unsigned int n) const | |
161 | { | |
162 | return m_clientData[n]; | |
163 | } | |
164 | ||
165 | void wxChoice::DoClear() | |
166 | { | |
167 | wxCHECK_RET( m_widget != NULL, wxT("invalid control") ); | |
168 | ||
169 | GTKDisableEvents(); | |
170 | ||
171 | GtkComboBox* combobox = GTK_COMBO_BOX( m_widget ); | |
172 | GtkTreeModel* model = gtk_combo_box_get_model( combobox ); | |
173 | gtk_list_store_clear(GTK_LIST_STORE(model)); | |
174 | ||
175 | m_clientData.Clear(); | |
176 | ||
177 | if (m_strings) | |
178 | m_strings->Clear(); | |
179 | ||
180 | GTKEnableEvents(); | |
181 | ||
182 | InvalidateBestSize(); | |
183 | } | |
184 | ||
185 | void wxChoice::DoDeleteOneItem(unsigned int n) | |
186 | { | |
187 | wxCHECK_RET( m_widget != NULL, wxT("invalid control") ); | |
188 | wxCHECK_RET( IsValid(n), wxT("invalid index in wxChoice::Delete") ); | |
189 | ||
190 | GtkComboBox* combobox = GTK_COMBO_BOX( m_widget ); | |
191 | GtkTreeModel* model = gtk_combo_box_get_model( combobox ); | |
192 | GtkListStore* store = GTK_LIST_STORE(model); | |
193 | GtkTreeIter iter; | |
194 | gtk_tree_model_iter_nth_child( model, &iter, | |
195 | NULL, (gint) n ); | |
196 | gtk_list_store_remove( store, &iter ); | |
197 | ||
198 | m_clientData.RemoveAt( n ); | |
199 | if ( m_strings ) | |
200 | m_strings->RemoveAt( n ); | |
201 | ||
202 | InvalidateBestSize(); | |
203 | } | |
204 | ||
205 | int wxChoice::FindString( const wxString &item, bool bCase ) const | |
206 | { | |
207 | wxCHECK_MSG( m_widget != NULL, wxNOT_FOUND, wxT("invalid control") ); | |
208 | ||
209 | GtkComboBox* combobox = GTK_COMBO_BOX( m_widget ); | |
210 | GtkTreeModel* model = gtk_combo_box_get_model( combobox ); | |
211 | GtkTreeIter iter; | |
212 | gtk_tree_model_get_iter_first( model, &iter ); | |
213 | if (!gtk_list_store_iter_is_valid(GTK_LIST_STORE(model), &iter )) | |
214 | return -1; | |
215 | int count = 0; | |
216 | do | |
217 | { | |
218 | GValue value = { 0, }; | |
219 | gtk_tree_model_get_value( model, &iter, m_stringCellIndex, &value ); | |
220 | wxString str = wxGTK_CONV_BACK( g_value_get_string( &value ) ); | |
221 | g_value_unset( &value ); | |
222 | ||
223 | if (item.IsSameAs( str, bCase ) ) | |
224 | return count; | |
225 | ||
226 | count++; | |
227 | } | |
228 | while ( gtk_tree_model_iter_next(model, &iter) ); | |
229 | ||
230 | return wxNOT_FOUND; | |
231 | } | |
232 | ||
233 | int wxChoice::GetSelection() const | |
234 | { | |
235 | return gtk_combo_box_get_active( GTK_COMBO_BOX( m_widget ) ); | |
236 | } | |
237 | ||
238 | void wxChoice::SetString(unsigned int n, const wxString &text) | |
239 | { | |
240 | wxCHECK_RET( m_widget != NULL, wxT("invalid control") ); | |
241 | ||
242 | GtkComboBox* combobox = GTK_COMBO_BOX( m_widget ); | |
243 | wxCHECK_RET( IsValid(n), wxT("invalid index") ); | |
244 | ||
245 | GtkTreeModel *model = gtk_combo_box_get_model( combobox ); | |
246 | GtkTreeIter iter; | |
247 | if (gtk_tree_model_iter_nth_child (model, &iter, NULL, n)) | |
248 | { | |
249 | GValue value = { 0, }; | |
250 | g_value_init( &value, G_TYPE_STRING ); | |
251 | g_value_set_string( &value, wxGTK_CONV( text ) ); | |
252 | gtk_list_store_set_value( GTK_LIST_STORE(model), &iter, m_stringCellIndex, &value ); | |
253 | g_value_unset( &value ); | |
254 | } | |
255 | ||
256 | InvalidateBestSize(); | |
257 | } | |
258 | ||
259 | wxString wxChoice::GetString(unsigned int n) const | |
260 | { | |
261 | wxCHECK_MSG( m_widget != NULL, wxEmptyString, wxT("invalid control") ); | |
262 | ||
263 | wxString str; | |
264 | ||
265 | GtkComboBox* combobox = GTK_COMBO_BOX( m_widget ); | |
266 | GtkTreeModel *model = gtk_combo_box_get_model( combobox ); | |
267 | GtkTreeIter iter; | |
268 | if (gtk_tree_model_iter_nth_child (model, &iter, NULL, n)) | |
269 | { | |
270 | GValue value = { 0, }; | |
271 | gtk_tree_model_get_value( model, &iter, m_stringCellIndex, &value ); | |
272 | wxString tmp = wxGTK_CONV_BACK( g_value_get_string( &value ) ); | |
273 | g_value_unset( &value ); | |
274 | return tmp; | |
275 | } | |
276 | ||
277 | return str; | |
278 | } | |
279 | ||
280 | unsigned int wxChoice::GetCount() const | |
281 | { | |
282 | wxCHECK_MSG( m_widget != NULL, 0, wxT("invalid control") ); | |
283 | ||
284 | GtkComboBox* combobox = GTK_COMBO_BOX( m_widget ); | |
285 | GtkTreeModel* model = gtk_combo_box_get_model( combobox ); | |
286 | GtkTreeIter iter; | |
287 | gtk_tree_model_get_iter_first( model, &iter ); | |
288 | if (!gtk_list_store_iter_is_valid(GTK_LIST_STORE(model), &iter )) | |
289 | return 0; | |
290 | unsigned int ret = 1; | |
291 | while (gtk_tree_model_iter_next( model, &iter )) | |
292 | ret++; | |
293 | return ret; | |
294 | } | |
295 | ||
296 | void wxChoice::SetSelection( int n ) | |
297 | { | |
298 | wxCHECK_RET( m_widget != NULL, wxT("invalid control") ); | |
299 | ||
300 | GTKDisableEvents(); | |
301 | ||
302 | GtkComboBox* combobox = GTK_COMBO_BOX( m_widget ); | |
303 | gtk_combo_box_set_active( combobox, n ); | |
304 | ||
305 | GTKEnableEvents(); | |
306 | } | |
307 | ||
308 | void wxChoice::SetColumns(int n) | |
309 | { | |
310 | gtk_combo_box_set_wrap_width(GTK_COMBO_BOX(m_widget), n); | |
311 | } | |
312 | ||
313 | int wxChoice::GetColumns() const | |
314 | { | |
315 | // gtk_combo_box_get_wrap_width() was added in gtk 2.6 | |
316 | gint intval; | |
317 | g_object_get(G_OBJECT(m_widget), "wrap-width", &intval, NULL); | |
318 | return intval; | |
319 | } | |
320 | ||
321 | ||
322 | void wxChoice::GTKDisableEvents() | |
323 | { | |
324 | g_signal_handlers_block_by_func(m_widget, | |
325 | (gpointer) gtk_choice_changed_callback, this); | |
326 | } | |
327 | ||
328 | void wxChoice::GTKEnableEvents() | |
329 | { | |
330 | g_signal_handlers_unblock_by_func(m_widget, | |
331 | (gpointer) gtk_choice_changed_callback, this); | |
332 | } | |
333 | ||
334 | ||
335 | GdkWindow *wxChoice::GTKGetWindow(wxArrayGdkWindows& WXUNUSED(windows)) const | |
336 | { | |
337 | return m_widget->window; | |
338 | } | |
339 | ||
340 | // Notice that this method shouldn't be necessary, because GTK calculates | |
341 | // properly size of the combobox but for unknown reasons it doesn't work | |
342 | // correctly in wx without this. | |
343 | wxSize wxChoice::DoGetBestSize() const | |
344 | { | |
345 | // strangely, this returns a width of 188 pixels from GTK+ (?) | |
346 | wxSize ret( wxControl::DoGetBestSize() ); | |
347 | ||
348 | // we know better our horizontal extent: it depends on the longest string | |
349 | // in the combobox | |
350 | if ( m_widget ) | |
351 | { | |
352 | ret.x = GetCount() > 0 ? 0 : 60; // start with something "sensible" | |
353 | int width; | |
354 | unsigned int count = GetCount(); | |
355 | for ( unsigned int n = 0; n < count; n++ ) | |
356 | { | |
357 | GetTextExtent(GetString(n), &width, NULL, NULL, NULL ); | |
358 | if ( width + 40 > ret.x ) // 40 for drop down arrow and space around text | |
359 | ret.x = width + 40; | |
360 | } | |
361 | } | |
362 | ||
363 | // empty combobox should have some reasonable default size too | |
364 | if ((GetCount() == 0) && (ret.x < 80)) | |
365 | ret.x = 80; | |
366 | ||
367 | CacheBestSize(ret); | |
368 | return ret; | |
369 | } | |
370 | ||
371 | void wxChoice::DoApplyWidgetStyle(GtkRcStyle *style) | |
372 | { | |
373 | gtk_widget_modify_style(m_widget, style); | |
374 | gtk_widget_modify_style(GTK_BIN(m_widget)->child, style); | |
375 | } | |
376 | ||
377 | ||
378 | // static | |
379 | wxVisualAttributes | |
380 | wxChoice::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant)) | |
381 | { | |
382 | return GetDefaultAttributesFromGTKWidget(gtk_combo_box_new); | |
383 | } | |
384 | ||
385 | ||
386 | #endif // wxUSE_CHOICE || wxUSE_COMBOBOX |