]>
Commit | Line | Data |
---|---|---|
53010e52 RR |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: combobox.cpp | |
3 | // Purpose: | |
4 | // Author: Robert Roebling | |
dbf858b5 | 5 | // Id: $Id$ |
01111366 | 6 | // Copyright: (c) 1998 Robert Roebling |
805dd538 | 7 | // Licence: wxWindows licence |
53010e52 RR |
8 | ///////////////////////////////////////////////////////////////////////////// |
9 | ||
14f355c2 | 10 | #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA) |
53010e52 RR |
11 | #pragma implementation "combobox.h" |
12 | #endif | |
13 | ||
14f355c2 VS |
14 | // For compilers that support precompilation, includes "wx.h". |
15 | #include "wx/wxprec.h" | |
16 | ||
53010e52 | 17 | #include "wx/combobox.h" |
dcf924a3 RR |
18 | |
19 | #if wxUSE_COMBOBOX | |
20 | ||
72a16063 | 21 | #include "wx/settings.h" |
584ad2a3 | 22 | #include "wx/arrstr.h" |
b62c3631 | 23 | #include "wx/intl.h" |
53010e52 | 24 | |
78bcfcfc VZ |
25 | #include "wx/textctrl.h" // for wxEVT_COMMAND_TEXT_UPDATED |
26 | ||
9e691f46 | 27 | #include "wx/gtk/private.h" |
83624f79 | 28 | |
acfd422a RR |
29 | //----------------------------------------------------------------------------- |
30 | // idle system | |
31 | //----------------------------------------------------------------------------- | |
32 | ||
33 | extern void wxapp_install_idle_handler(); | |
34 | extern bool g_isIdle; | |
35 | ||
47908e25 RR |
36 | //----------------------------------------------------------------------------- |
37 | // data | |
38 | //----------------------------------------------------------------------------- | |
39 | ||
40 | extern bool g_blockEventsOnDrag; | |
41 | ||
53010e52 | 42 | //----------------------------------------------------------------------------- |
461573cc | 43 | // "select-child" - click/cursor get select-child, changed, select-child |
47908e25 | 44 | //----------------------------------------------------------------------------- |
47908e25 | 45 | |
8a85884a | 46 | static void |
461573cc | 47 | gtk_combo_select_child_callback( GtkList *WXUNUSED(list), GtkWidget *WXUNUSED(widget), wxComboBox *combo ) |
53010e52 | 48 | { |
acfd422a | 49 | if (g_isIdle) wxapp_install_idle_handler(); |
8a85884a | 50 | |
a2053b27 | 51 | if (!combo->m_hasVMT) return; |
30ed6e5c | 52 | |
acfd422a | 53 | if (g_blockEventsOnDrag) return; |
805dd538 | 54 | |
159b66c0 | 55 | int curSelection = combo->GetSelection(); |
30ed6e5c | 56 | |
159b66c0 RR |
57 | if (combo->m_prevSelection != curSelection) |
58 | { | |
59 | GtkWidget *list = GTK_COMBO(combo->m_widget)->list; | |
60 | gtk_list_unselect_item( GTK_LIST(list), combo->m_prevSelection ); | |
61 | } | |
159b66c0 RR |
62 | combo->m_prevSelection = curSelection; |
63 | ||
8a85884a | 64 | wxCommandEvent event( wxEVT_COMMAND_COMBOBOX_SELECTED, combo->GetId() ); |
159b66c0 | 65 | event.SetInt( curSelection ); |
29006414 | 66 | event.SetString( combo->GetStringSelection() ); |
8a85884a | 67 | event.SetEventObject( combo ); |
31528cd3 | 68 | |
8a85884a | 69 | combo->GetEventHandler()->ProcessEvent( event ); |
6de97a3b | 70 | } |
47908e25 | 71 | |
0c77152e | 72 | //----------------------------------------------------------------------------- |
461573cc RR |
73 | // "changed" - typing and list item matches get changed, select-child |
74 | // if it doesn't match an item then just get a single changed | |
0c77152e RR |
75 | //----------------------------------------------------------------------------- |
76 | ||
eeccd5d9 | 77 | static void |
0c77152e RR |
78 | gtk_text_changed_callback( GtkWidget *WXUNUSED(widget), wxComboBox *combo ) |
79 | { | |
acfd422a | 80 | if (g_isIdle) wxapp_install_idle_handler(); |
31528cd3 | 81 | |
a2053b27 RR |
82 | if (!combo->m_hasVMT) return; |
83 | ||
8a85884a | 84 | wxCommandEvent event( wxEVT_COMMAND_TEXT_UPDATED, combo->GetId() ); |
29006414 | 85 | event.SetString( combo->GetValue() ); |
0c77152e RR |
86 | event.SetEventObject( combo ); |
87 | combo->GetEventHandler()->ProcessEvent( event ); | |
0c77152e RR |
88 | } |
89 | ||
461573cc RR |
90 | static void |
91 | gtk_dummy_callback(GtkEntry *WXUNUSED(entry), GtkCombo *WXUNUSED(combo)) | |
92 | { | |
93 | } | |
94 | ||
e1e955e1 RR |
95 | //----------------------------------------------------------------------------- |
96 | // wxComboBox | |
53010e52 RR |
97 | //----------------------------------------------------------------------------- |
98 | ||
99 | IMPLEMENT_DYNAMIC_CLASS(wxComboBox,wxControl) | |
100 | ||
b4071e91 | 101 | BEGIN_EVENT_TABLE(wxComboBox, wxControl) |
fd0eed64 | 102 | EVT_SIZE(wxComboBox::OnSize) |
8a85884a | 103 | EVT_CHAR(wxComboBox::OnChar) |
b4071e91 RR |
104 | END_EVENT_TABLE() |
105 | ||
584ad2a3 MB |
106 | bool wxComboBox::Create( wxWindow *parent, wxWindowID id, |
107 | const wxString& value, | |
108 | const wxPoint& pos, const wxSize& size, | |
109 | const wxArrayString& choices, | |
110 | long style, const wxValidator& validator, | |
111 | const wxString& name ) | |
112 | { | |
113 | wxCArrayString chs(choices); | |
114 | ||
115 | return Create( parent, id, value, pos, size, chs.GetCount(), | |
116 | chs.GetStrings(), style, validator, name ); | |
117 | } | |
118 | ||
fd0eed64 RR |
119 | bool wxComboBox::Create( wxWindow *parent, wxWindowID id, const wxString& value, |
120 | const wxPoint& pos, const wxSize& size, | |
121 | int n, const wxString choices[], | |
805dd538 VZ |
122 | long style, const wxValidator& validator, |
123 | const wxString& name ) | |
53010e52 | 124 | { |
fd0eed64 RR |
125 | m_alreadySent = FALSE; |
126 | m_needParent = TRUE; | |
b292e2f5 | 127 | m_acceptsFocus = TRUE; |
159b66c0 | 128 | m_prevSelection = 0; |
805dd538 | 129 | |
db434467 | 130 | if (!PreCreation( parent, pos, size ) || |
4dcaf11a RR |
131 | !CreateBase( parent, id, pos, size, style, validator, name )) |
132 | { | |
223d09f6 | 133 | wxFAIL_MSG( wxT("wxComboBox creation failed") ); |
9d9b7755 | 134 | return FALSE; |
4dcaf11a | 135 | } |
6de97a3b | 136 | |
fd0eed64 | 137 | m_widget = gtk_combo_new(); |
461573cc | 138 | GtkCombo *combo = GTK_COMBO(m_widget); |
30ed6e5c | 139 | |
461573cc RR |
140 | // Disable GTK's broken events ... |
141 | gtk_signal_disconnect( GTK_OBJECT(combo->entry), combo->entry_change_id ); | |
142 | // ... and add surogate handler. | |
143 | combo->entry_change_id = gtk_signal_connect (GTK_OBJECT (combo->entry), "changed", | |
144 | (GtkSignalFunc) gtk_dummy_callback, combo); | |
805dd538 | 145 | |
8a85884a | 146 | // make it more useable |
3ca6a5f0 | 147 | gtk_combo_set_use_arrows_always( GTK_COMBO(m_widget), TRUE ); |
30ed6e5c | 148 | |
3ca6a5f0 BP |
149 | // and case-sensitive |
150 | gtk_combo_set_case_sensitive( GTK_COMBO(m_widget), TRUE ); | |
151 | ||
fd0eed64 | 152 | GtkWidget *list = GTK_COMBO(m_widget)->list; |
805dd538 | 153 | |
2e1d7104 | 154 | #ifndef __WXGTK20__ |
81a0614b | 155 | // gtk_list_set_selection_mode( GTK_LIST(list), GTK_SELECTION_MULTIPLE ); |
2e1d7104 | 156 | #endif |
159b66c0 | 157 | |
fd0eed64 RR |
158 | for (int i = 0; i < n; i++) |
159 | { | |
fab591c5 | 160 | GtkWidget *list_item = gtk_list_item_new_with_label( wxGTK_CONV( choices[i] ) ); |
805dd538 | 161 | |
fd0eed64 | 162 | m_clientDataList.Append( (wxObject*)NULL ); |
f5e27805 | 163 | m_clientObjectList.Append( (wxObject*)NULL ); |
805dd538 | 164 | |
fd0eed64 | 165 | gtk_container_add( GTK_CONTAINER(list), list_item ); |
805dd538 | 166 | |
19da4326 | 167 | gtk_widget_show( list_item ); |
fd0eed64 | 168 | } |
805dd538 | 169 | |
f03fc89f | 170 | m_parent->DoAddChild( this ); |
30ed6e5c | 171 | |
461573cc | 172 | m_focusWidget = combo->entry; |
805dd538 | 173 | |
fd0eed64 | 174 | PostCreation(); |
e8e24dfa | 175 | InheritAttributes(); |
53010e52 | 176 | |
461573cc | 177 | ConnectWidget( combo->button ); |
805dd538 | 178 | |
461573cc RR |
179 | // MSW's combo box shows the value and the selection is -1 |
180 | gtk_entry_set_text( GTK_ENTRY(combo->entry), wxGTK_CONV(value) ); | |
181 | gtk_list_unselect_all( GTK_LIST(combo->list) ); | |
805dd538 | 182 | |
a260fe6a | 183 | if (style & wxCB_READONLY) |
461573cc | 184 | gtk_entry_set_editable( GTK_ENTRY( combo->entry ), FALSE ); |
a260fe6a | 185 | |
461573cc RR |
186 | gtk_signal_connect( GTK_OBJECT(combo->entry), "changed", |
187 | GTK_SIGNAL_FUNC(gtk_text_changed_callback), (gpointer)this ); | |
188 | ||
189 | gtk_signal_connect( GTK_OBJECT(combo->list), "select-child", | |
190 | GTK_SIGNAL_FUNC(gtk_combo_select_child_callback), (gpointer)this ); | |
805dd538 | 191 | |
db434467 RR |
192 | wxSize size_best( DoGetBestSize() ); |
193 | wxSize new_size( size ); | |
194 | if (new_size.x == -1) | |
195 | new_size.x = size_best.x; | |
196 | if (new_size.y == -1) | |
197 | new_size.y = size_best.y; | |
198 | if (new_size.y > size_best.y) | |
199 | new_size.y = size_best.y; | |
200 | if ((new_size.x != size.x) || (new_size.y != size.y)) | |
4494ad58 | 201 | { |
db434467 | 202 | SetSize( new_size.x, new_size.y ); |
30ed6e5c | 203 | |
4494ad58 RR |
204 | // This is required for tool bar support |
205 | gtk_widget_set_usize( m_widget, new_size.x, new_size.y ); | |
206 | } | |
207 | ||
fd0eed64 | 208 | Show( TRUE ); |
805dd538 | 209 | |
fd0eed64 RR |
210 | return TRUE; |
211 | } | |
212 | ||
213 | wxComboBox::~wxComboBox() | |
214 | { | |
222ed1d6 | 215 | wxList::compatibility_iterator node = m_clientObjectList.GetFirst(); |
fd0eed64 RR |
216 | while (node) |
217 | { | |
b1d4dd7a | 218 | wxClientData *cd = (wxClientData*)node->GetData(); |
fd0eed64 | 219 | if (cd) delete cd; |
b1d4dd7a | 220 | node = node->GetNext(); |
fd0eed64 | 221 | } |
7d6d2cd4 RR |
222 | m_clientObjectList.Clear(); |
223 | ||
fd0eed64 | 224 | m_clientDataList.Clear(); |
6de97a3b | 225 | } |
53010e52 | 226 | |
2b5f62a0 VZ |
227 | void wxComboBox::SetFocus() |
228 | { | |
229 | if ( m_hasFocus ) | |
230 | { | |
231 | // don't do anything if we already have focus | |
232 | return; | |
233 | } | |
234 | ||
235 | gtk_widget_grab_focus( m_focusWidget ); | |
236 | } | |
237 | ||
6f6f938f | 238 | int wxComboBox::DoAppend( const wxString &item ) |
53010e52 | 239 | { |
2a68b7a0 | 240 | wxCHECK_MSG( m_widget != NULL, -1, wxT("invalid combobox") ); |
805dd538 | 241 | |
461573cc | 242 | DisableEvents(); |
30ed6e5c | 243 | |
fd0eed64 | 244 | GtkWidget *list = GTK_COMBO(m_widget)->list; |
805dd538 | 245 | |
fab591c5 | 246 | GtkWidget *list_item = gtk_list_item_new_with_label( wxGTK_CONV( item ) ); |
805dd538 | 247 | |
ec5d85fb RR |
248 | gtk_container_add( GTK_CONTAINER(list), list_item ); |
249 | ||
2b07d713 RR |
250 | if (GTK_WIDGET_REALIZED(m_widget)) |
251 | { | |
252 | gtk_widget_realize( list_item ); | |
253 | gtk_widget_realize( GTK_BIN(list_item)->child ); | |
254 | ||
255 | if (m_widgetStyle) ApplyWidgetStyle(); | |
256 | } | |
805dd538 | 257 | |
fd0eed64 | 258 | gtk_widget_show( list_item ); |
30ed6e5c | 259 | |
6f6f938f | 260 | const int count = GetCount(); |
53010e52 | 261 | |
6f6f938f | 262 | if ( (int)m_clientDataList.GetCount() < count ) |
f5e27805 | 263 | m_clientDataList.Append( (wxObject*) NULL ); |
6f6f938f | 264 | if ( (int)m_clientObjectList.GetCount() < count ) |
f5e27805 | 265 | m_clientObjectList.Append( (wxObject*) NULL ); |
805dd538 | 266 | |
6f6f938f | 267 | EnableEvents(); |
805dd538 | 268 | |
6f6f938f | 269 | return count - 1; |
fd0eed64 RR |
270 | } |
271 | ||
6f6f938f | 272 | int wxComboBox::DoInsert( const wxString &item, int pos ) |
243dbf1a | 273 | { |
708c45a6 VZ |
274 | wxCHECK_MSG( !(GetWindowStyle() & wxCB_SORT), -1, |
275 | wxT("can't insert into sorted list")); | |
276 | ||
277 | wxCHECK_MSG( m_widget != NULL, -1, wxT("invalid combobox") ); | |
243dbf1a VZ |
278 | |
279 | int count = GetCount(); | |
6f6f938f VZ |
280 | wxCHECK_MSG( (pos >= 0) && (pos <= count), -1, wxT("invalid index") ); |
281 | ||
243dbf1a | 282 | if (pos == count) |
6f6f938f | 283 | return Append(item); |
243dbf1a VZ |
284 | |
285 | DisableEvents(); | |
286 | ||
287 | GtkWidget *list = GTK_COMBO(m_widget)->list; | |
288 | ||
289 | GtkWidget *list_item = gtk_list_item_new_with_label( wxGTK_CONV( item ) ); | |
290 | ||
291 | GList *gitem_list = g_list_alloc (); | |
292 | gitem_list->data = list_item; | |
293 | gtk_list_insert_items( GTK_LIST (list), gitem_list, pos ); | |
294 | ||
295 | if (GTK_WIDGET_REALIZED(m_widget)) | |
296 | { | |
297 | gtk_widget_realize( list_item ); | |
298 | gtk_widget_realize( GTK_BIN(list_item)->child ); | |
299 | ||
708c45a6 VZ |
300 | if (m_widgetStyle) |
301 | ApplyWidgetStyle(); | |
243dbf1a VZ |
302 | } |
303 | ||
304 | gtk_widget_show( list_item ); | |
305 | ||
6f6f938f | 306 | count = GetCount(); |
243dbf1a | 307 | |
6f6f938f | 308 | if ( (int)m_clientDataList.GetCount() < count ) |
243dbf1a | 309 | m_clientDataList.Insert( pos, (wxObject*) NULL ); |
6f6f938f | 310 | if ( (int)m_clientObjectList.GetCount() < count ) |
243dbf1a VZ |
311 | m_clientObjectList.Insert( pos, (wxObject*) NULL ); |
312 | ||
6f6f938f | 313 | EnableEvents(); |
243dbf1a | 314 | |
6f6f938f | 315 | return pos; |
243dbf1a VZ |
316 | } |
317 | ||
6f6f938f | 318 | void wxComboBox::DoSetItemClientData( int n, void* clientData ) |
fd0eed64 | 319 | { |
223d09f6 | 320 | wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") ); |
805dd538 | 321 | |
222ed1d6 | 322 | wxList::compatibility_iterator node = m_clientDataList.Item( n ); |
fd0eed64 | 323 | if (!node) return; |
805dd538 | 324 | |
f5e27805 | 325 | node->SetData( (wxObject*) clientData ); |
6de97a3b | 326 | } |
53010e52 | 327 | |
6f6f938f | 328 | void* wxComboBox::DoGetItemClientData( int n ) const |
53010e52 | 329 | { |
223d09f6 | 330 | wxCHECK_MSG( m_widget != NULL, NULL, wxT("invalid combobox") ); |
805dd538 | 331 | |
222ed1d6 | 332 | wxList::compatibility_iterator node = m_clientDataList.Item( n ); |
805dd538 | 333 | |
30ed6e5c | 334 | return node ? node->GetData() : NULL; |
fd0eed64 RR |
335 | } |
336 | ||
6f6f938f | 337 | void wxComboBox::DoSetItemClientObject( int n, wxClientData* clientData ) |
fd0eed64 | 338 | { |
223d09f6 | 339 | wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") ); |
805dd538 | 340 | |
222ed1d6 | 341 | wxList::compatibility_iterator node = m_clientObjectList.Item( n ); |
fd0eed64 | 342 | if (!node) return; |
805dd538 | 343 | |
e94e2e95 | 344 | // wxItemContainer already deletes data for us |
805dd538 | 345 | |
fd0eed64 | 346 | node->SetData( (wxObject*) clientData ); |
6de97a3b | 347 | } |
53010e52 | 348 | |
6f6f938f | 349 | wxClientData* wxComboBox::DoGetItemClientObject( int n ) const |
53010e52 | 350 | { |
223d09f6 | 351 | wxCHECK_MSG( m_widget != NULL, (wxClientData*)NULL, wxT("invalid combobox") ); |
805dd538 | 352 | |
222ed1d6 | 353 | wxList::compatibility_iterator node = m_clientObjectList.Item( n ); |
805dd538 | 354 | |
30ed6e5c | 355 | return node ? (wxClientData*) node->GetData() : NULL; |
fd0eed64 RR |
356 | } |
357 | ||
358 | void wxComboBox::Clear() | |
359 | { | |
223d09f6 | 360 | wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") ); |
805dd538 | 361 | |
461573cc | 362 | DisableEvents(); |
30ed6e5c | 363 | |
fd0eed64 | 364 | GtkWidget *list = GTK_COMBO(m_widget)->list; |
6f6f938f | 365 | gtk_list_clear_items( GTK_LIST(list), 0, GetCount() ); |
805dd538 | 366 | |
222ed1d6 | 367 | wxList::compatibility_iterator node = m_clientObjectList.GetFirst(); |
fd0eed64 RR |
368 | while (node) |
369 | { | |
b1d4dd7a | 370 | wxClientData *cd = (wxClientData*)node->GetData(); |
fd0eed64 | 371 | if (cd) delete cd; |
b1d4dd7a | 372 | node = node->GetNext(); |
fd0eed64 | 373 | } |
f5e27805 | 374 | m_clientObjectList.Clear(); |
805dd538 | 375 | |
fd0eed64 | 376 | m_clientDataList.Clear(); |
30ed6e5c | 377 | |
461573cc | 378 | EnableEvents(); |
6de97a3b | 379 | } |
53010e52 | 380 | |
fd0eed64 | 381 | void wxComboBox::Delete( int n ) |
53010e52 | 382 | { |
223d09f6 | 383 | wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") ); |
805dd538 | 384 | |
fd0eed64 | 385 | GtkList *listbox = GTK_LIST( GTK_COMBO(m_widget)->list ); |
805dd538 | 386 | |
fd0eed64 | 387 | GList *child = g_list_nth( listbox->children, n ); |
805dd538 | 388 | |
fd0eed64 RR |
389 | if (!child) |
390 | { | |
223d09f6 | 391 | wxFAIL_MSG(wxT("wrong index")); |
fd0eed64 RR |
392 | return; |
393 | } | |
805dd538 | 394 | |
461573cc | 395 | DisableEvents(); |
30ed6e5c | 396 | |
bbe0af5b | 397 | GList *list = g_list_append( (GList*) NULL, child->data ); |
fd0eed64 RR |
398 | gtk_list_remove_items( listbox, list ); |
399 | g_list_free( list ); | |
805dd538 | 400 | |
222ed1d6 | 401 | wxList::compatibility_iterator node = m_clientObjectList.Item( n ); |
f5e27805 | 402 | if (node) |
fd0eed64 | 403 | { |
b1d4dd7a | 404 | wxClientData *cd = (wxClientData*)node->GetData(); |
fd0eed64 | 405 | if (cd) delete cd; |
222ed1d6 | 406 | m_clientObjectList.Erase( node ); |
f5e27805 | 407 | } |
805dd538 | 408 | |
b1d4dd7a | 409 | node = m_clientDataList.Item( n ); |
f5e27805 | 410 | if (node) |
222ed1d6 MB |
411 | m_clientDataList.Erase( node ); |
412 | ||
461573cc RR |
413 | EnableEvents(); |
414 | } | |
415 | ||
416 | void wxComboBox::SetString(int n, const wxString &text) | |
417 | { | |
418 | wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") ); | |
419 | ||
420 | GtkWidget *list = GTK_COMBO(m_widget)->list; | |
421 | ||
422 | GList *child = g_list_nth( GTK_LIST(list)->children, n ); | |
423 | if (child) | |
424 | { | |
425 | GtkBin *bin = GTK_BIN( child->data ); | |
426 | GtkLabel *label = GTK_LABEL( bin->child ); | |
427 | gtk_label_set_text(label, wxGTK_CONV(text)); | |
428 | } | |
429 | else | |
430 | { | |
431 | wxFAIL_MSG( wxT("wxComboBox: wrong index") ); | |
fd0eed64 | 432 | } |
6de97a3b | 433 | } |
53010e52 | 434 | |
6f6f938f | 435 | int wxComboBox::FindString( const wxString &item ) const |
53010e52 | 436 | { |
223d09f6 | 437 | wxCHECK_MSG( m_widget != NULL, -1, wxT("invalid combobox") ); |
805dd538 | 438 | |
fd0eed64 | 439 | GtkWidget *list = GTK_COMBO(m_widget)->list; |
805dd538 | 440 | |
53010e52 RR |
441 | GList *child = GTK_LIST(list)->children; |
442 | int count = 0; | |
443 | while (child) | |
444 | { | |
fd0eed64 RR |
445 | GtkBin *bin = GTK_BIN( child->data ); |
446 | GtkLabel *label = GTK_LABEL( bin->child ); | |
2b5f62a0 VZ |
447 | #ifdef __WXGTK20__ |
448 | wxString str( wxGTK_CONV_BACK( gtk_label_get_text(label) ) ); | |
449 | #else | |
450 | wxString str( label->label ); | |
451 | #endif | |
452 | if (item == str) | |
7cf8cb48 | 453 | return count; |
30ed6e5c | 454 | |
fd0eed64 RR |
455 | count++; |
456 | child = child->next; | |
457 | } | |
805dd538 | 458 | |
7cf8cb48 | 459 | return wxNOT_FOUND; |
fd0eed64 RR |
460 | } |
461 | ||
462 | int wxComboBox::GetSelection() const | |
463 | { | |
223d09f6 | 464 | wxCHECK_MSG( m_widget != NULL, -1, wxT("invalid combobox") ); |
805dd538 | 465 | |
fd0eed64 | 466 | GtkWidget *list = GTK_COMBO(m_widget)->list; |
805dd538 | 467 | |
fd0eed64 RR |
468 | GList *selection = GTK_LIST(list)->selection; |
469 | if (selection) | |
470 | { | |
471 | GList *child = GTK_LIST(list)->children; | |
472 | int count = 0; | |
473 | while (child) | |
474 | { | |
475 | if (child->data == selection->data) return count; | |
476 | count++; | |
477 | child = child->next; | |
478 | } | |
6de97a3b | 479 | } |
805dd538 | 480 | |
fd0eed64 | 481 | return -1; |
6de97a3b | 482 | } |
53010e52 | 483 | |
debe6624 | 484 | wxString wxComboBox::GetString( int n ) const |
53010e52 | 485 | { |
223d09f6 | 486 | wxCHECK_MSG( m_widget != NULL, wxT(""), wxT("invalid combobox") ); |
805dd538 | 487 | |
fd0eed64 | 488 | GtkWidget *list = GTK_COMBO(m_widget)->list; |
805dd538 | 489 | |
7cf8cb48 | 490 | wxString str; |
fd0eed64 RR |
491 | GList *child = g_list_nth( GTK_LIST(list)->children, n ); |
492 | if (child) | |
493 | { | |
494 | GtkBin *bin = GTK_BIN( child->data ); | |
495 | GtkLabel *label = GTK_LABEL( bin->child ); | |
2e1d7104 | 496 | #ifdef __WXGTK20__ |
2b5f62a0 | 497 | str = wxGTK_CONV_BACK( gtk_label_get_text(label) ); |
2e1d7104 RR |
498 | #else |
499 | str = wxString( label->label ); | |
500 | #endif | |
7cf8cb48 VZ |
501 | } |
502 | else | |
503 | { | |
223d09f6 | 504 | wxFAIL_MSG( wxT("wxComboBox: wrong index") ); |
fd0eed64 | 505 | } |
805dd538 | 506 | |
7cf8cb48 | 507 | return str; |
6de97a3b | 508 | } |
53010e52 | 509 | |
fd0eed64 | 510 | wxString wxComboBox::GetStringSelection() const |
53010e52 | 511 | { |
223d09f6 | 512 | wxCHECK_MSG( m_widget != NULL, wxT(""), wxT("invalid combobox") ); |
805dd538 | 513 | |
fd0eed64 | 514 | GtkWidget *list = GTK_COMBO(m_widget)->list; |
805dd538 | 515 | |
fd0eed64 RR |
516 | GList *selection = GTK_LIST(list)->selection; |
517 | if (selection) | |
518 | { | |
519 | GtkBin *bin = GTK_BIN( selection->data ); | |
2b5f62a0 VZ |
520 | GtkLabel *label = GTK_LABEL( bin->child ); |
521 | #ifdef __WXGTK20__ | |
522 | wxString tmp( wxGTK_CONV_BACK( gtk_label_get_text(label) ) ); | |
523 | #else | |
524 | wxString tmp( label->label ); | |
525 | #endif | |
fd0eed64 RR |
526 | return tmp; |
527 | } | |
805dd538 | 528 | |
223d09f6 | 529 | wxFAIL_MSG( wxT("wxComboBox: no selection") ); |
805dd538 | 530 | |
223d09f6 | 531 | return wxT(""); |
6de97a3b | 532 | } |
53010e52 | 533 | |
6f6f938f | 534 | int wxComboBox::GetCount() const |
53010e52 | 535 | { |
223d09f6 | 536 | wxCHECK_MSG( m_widget != NULL, 0, wxT("invalid combobox") ); |
805dd538 | 537 | |
fd0eed64 | 538 | GtkWidget *list = GTK_COMBO(m_widget)->list; |
805dd538 | 539 | |
fd0eed64 RR |
540 | GList *child = GTK_LIST(list)->children; |
541 | int count = 0; | |
542 | while (child) { count++; child = child->next; } | |
543 | return count; | |
6de97a3b | 544 | } |
53010e52 | 545 | |
debe6624 | 546 | void wxComboBox::SetSelection( int n ) |
53010e52 | 547 | { |
223d09f6 | 548 | wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") ); |
805dd538 | 549 | |
953704c1 RR |
550 | DisableEvents(); |
551 | ||
fd0eed64 | 552 | GtkWidget *list = GTK_COMBO(m_widget)->list; |
159b66c0 | 553 | gtk_list_unselect_item( GTK_LIST(list), m_prevSelection ); |
fd0eed64 | 554 | gtk_list_select_item( GTK_LIST(list), n ); |
159b66c0 | 555 | m_prevSelection = n; |
953704c1 RR |
556 | |
557 | EnableEvents(); | |
6de97a3b | 558 | } |
53010e52 | 559 | |
95561ddf | 560 | bool wxComboBox::SetStringSelection( const wxString &string ) |
47908e25 | 561 | { |
95561ddf | 562 | wxCHECK_MSG( m_widget != NULL, false, wxT("invalid combobox") ); |
805dd538 | 563 | |
fd0eed64 | 564 | int res = FindString( string ); |
95561ddf | 565 | if (res == -1) return false; |
fd0eed64 | 566 | SetSelection( res ); |
95561ddf | 567 | return true; |
6de97a3b | 568 | } |
47908e25 | 569 | |
fd0eed64 | 570 | wxString wxComboBox::GetValue() const |
53010e52 | 571 | { |
2e1d7104 RR |
572 | GtkEntry *entry = GTK_ENTRY( GTK_COMBO(m_widget)->entry ); |
573 | wxString tmp( wxGTK_CONV_BACK( gtk_entry_get_text( entry ) ) ); | |
574 | ||
30ed6e5c | 575 | #if 0 |
2e1d7104 RR |
576 | for (int i = 0; i < wxStrlen(tmp.c_str()) +1; i++) |
577 | { | |
578 | wxChar c = tmp[i]; | |
579 | printf( "%d ", (int) (c) ); | |
580 | } | |
581 | printf( "\n" ); | |
582 | #endif | |
30ed6e5c | 583 | |
fd0eed64 | 584 | return tmp; |
6de97a3b | 585 | } |
53010e52 RR |
586 | |
587 | void wxComboBox::SetValue( const wxString& value ) | |
588 | { | |
223d09f6 | 589 | wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") ); |
805dd538 | 590 | |
fd0eed64 | 591 | GtkWidget *entry = GTK_COMBO(m_widget)->entry; |
223d09f6 | 592 | wxString tmp = wxT(""); |
fd0eed64 | 593 | if (!value.IsNull()) tmp = value; |
fab591c5 | 594 | gtk_entry_set_text( GTK_ENTRY(entry), wxGTK_CONV( tmp ) ); |
6de97a3b | 595 | } |
53010e52 | 596 | |
fd0eed64 | 597 | void wxComboBox::Copy() |
53010e52 | 598 | { |
223d09f6 | 599 | wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") ); |
805dd538 | 600 | |
fd0eed64 | 601 | GtkWidget *entry = GTK_COMBO(m_widget)->entry; |
9e691f46 | 602 | gtk_editable_copy_clipboard( GTK_EDITABLE(entry) DUMMY_CLIPBOARD_ARG ); |
6de97a3b | 603 | } |
53010e52 | 604 | |
fd0eed64 | 605 | void wxComboBox::Cut() |
53010e52 | 606 | { |
223d09f6 | 607 | wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") ); |
805dd538 | 608 | |
fd0eed64 | 609 | GtkWidget *entry = GTK_COMBO(m_widget)->entry; |
9e691f46 | 610 | gtk_editable_cut_clipboard( GTK_EDITABLE(entry) DUMMY_CLIPBOARD_ARG ); |
6de97a3b | 611 | } |
53010e52 | 612 | |
fd0eed64 | 613 | void wxComboBox::Paste() |
53010e52 | 614 | { |
223d09f6 | 615 | wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") ); |
805dd538 | 616 | |
fd0eed64 | 617 | GtkWidget *entry = GTK_COMBO(m_widget)->entry; |
9e691f46 | 618 | gtk_editable_paste_clipboard( GTK_EDITABLE(entry) DUMMY_CLIPBOARD_ARG); |
6de97a3b | 619 | } |
53010e52 | 620 | |
debe6624 | 621 | void wxComboBox::SetInsertionPoint( long pos ) |
53010e52 | 622 | { |
223d09f6 | 623 | wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") ); |
805dd538 | 624 | |
6f6f938f VZ |
625 | if ( pos == GetLastPosition() ) |
626 | pos = -1; | |
627 | ||
fd0eed64 | 628 | GtkWidget *entry = GTK_COMBO(m_widget)->entry; |
073c8fe9 | 629 | gtk_entry_set_position( GTK_ENTRY(entry), (int)pos ); |
6de97a3b | 630 | } |
53010e52 | 631 | |
fd0eed64 | 632 | long wxComboBox::GetInsertionPoint() const |
53010e52 | 633 | { |
9e691f46 | 634 | return (long) GET_EDITABLE_POS( GTK_COMBO(m_widget)->entry ); |
6de97a3b | 635 | } |
53010e52 | 636 | |
fd0eed64 | 637 | long wxComboBox::GetLastPosition() const |
53010e52 | 638 | { |
fd0eed64 RR |
639 | GtkWidget *entry = GTK_COMBO(m_widget)->entry; |
640 | int pos = GTK_ENTRY(entry)->text_length; | |
641 | return (long) pos-1; | |
6de97a3b | 642 | } |
53010e52 | 643 | |
debe6624 | 644 | void wxComboBox::Replace( long from, long to, const wxString& value ) |
53010e52 | 645 | { |
223d09f6 | 646 | wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") ); |
805dd538 | 647 | |
fd0eed64 RR |
648 | GtkWidget *entry = GTK_COMBO(m_widget)->entry; |
649 | gtk_editable_delete_text( GTK_EDITABLE(entry), (gint)from, (gint)to ); | |
650 | if (value.IsNull()) return; | |
651 | gint pos = (gint)to; | |
30ed6e5c | 652 | |
2e1d7104 RR |
653 | #if wxUSE_UNICODE |
654 | wxCharBuffer buffer = wxConvUTF8.cWX2MB( value ); | |
655 | gtk_editable_insert_text( GTK_EDITABLE(entry), (const char*) buffer, strlen( (const char*) buffer ), &pos ); | |
656 | #else | |
657 | gtk_editable_insert_text( GTK_EDITABLE(entry), value.c_str(), value.Length(), &pos ); | |
658 | #endif | |
6de97a3b | 659 | } |
53010e52 | 660 | |
20d10ee1 | 661 | void wxComboBox::SetSelection( long from, long to ) |
53010e52 | 662 | { |
20d10ee1 VZ |
663 | GtkWidget *entry = GTK_COMBO(m_widget)->entry; |
664 | gtk_editable_select_region( GTK_EDITABLE(entry), (gint)from, (gint)to ); | |
6de97a3b | 665 | } |
53010e52 | 666 | |
20d10ee1 | 667 | void wxComboBox::SetEditable( bool editable ) |
53010e52 | 668 | { |
20d10ee1 VZ |
669 | GtkWidget *entry = GTK_COMBO(m_widget)->entry; |
670 | gtk_entry_set_editable( GTK_ENTRY(entry), editable ); | |
b4071e91 RR |
671 | } |
672 | ||
8a85884a VZ |
673 | void wxComboBox::OnChar( wxKeyEvent &event ) |
674 | { | |
12a3f227 | 675 | if ( event.GetKeyCode() == WXK_RETURN ) |
8a85884a | 676 | { |
461573cc RR |
677 | // GTK automatically selects an item if its in the list |
678 | wxCommandEvent event(wxEVT_COMMAND_TEXT_ENTER, GetId()); | |
679 | event.SetString( GetValue() ); | |
680 | event.SetInt( GetSelection() ); | |
681 | event.SetEventObject( this ); | |
3352cfff RR |
682 | |
683 | if (!GetEventHandler()->ProcessEvent( event )) | |
684 | { | |
685 | // This will invoke the dialog default action, such | |
686 | // as the clicking the default button. | |
687 | ||
688 | wxWindow *top_frame = m_parent; | |
689 | while (top_frame->GetParent() && !(top_frame->IsTopLevel())) | |
690 | top_frame = top_frame->GetParent(); | |
691 | ||
692 | if (top_frame && GTK_IS_WINDOW(top_frame->m_widget)) | |
693 | { | |
694 | GtkWindow *window = GTK_WINDOW(top_frame->m_widget); | |
695 | ||
696 | if (window->default_widget) | |
697 | gtk_widget_activate (window->default_widget); | |
698 | } | |
699 | } | |
30ed6e5c | 700 | |
461573cc RR |
701 | // Catch GTK event so that GTK doesn't open the drop |
702 | // down list upon RETURN. | |
0878fb4c | 703 | return; |
8a85884a | 704 | } |
30ed6e5c | 705 | |
7cf8cb48 | 706 | event.Skip(); |
8a85884a VZ |
707 | } |
708 | ||
953704c1 RR |
709 | void wxComboBox::DisableEvents() |
710 | { | |
461573cc RR |
711 | gtk_signal_disconnect_by_func( GTK_OBJECT(GTK_COMBO(m_widget)->list), |
712 | GTK_SIGNAL_FUNC(gtk_combo_select_child_callback), (gpointer)this ); | |
713 | gtk_signal_disconnect_by_func( GTK_OBJECT(GTK_COMBO(m_widget)->entry), | |
714 | GTK_SIGNAL_FUNC(gtk_text_changed_callback), (gpointer)this ); | |
953704c1 RR |
715 | } |
716 | ||
717 | void wxComboBox::EnableEvents() | |
718 | { | |
461573cc RR |
719 | gtk_signal_connect( GTK_OBJECT(GTK_COMBO(m_widget)->list), "select-child", |
720 | GTK_SIGNAL_FUNC(gtk_combo_select_child_callback), (gpointer)this ); | |
721 | gtk_signal_connect( GTK_OBJECT(GTK_COMBO(m_widget)->entry), "changed", | |
722 | GTK_SIGNAL_FUNC(gtk_text_changed_callback), (gpointer)this ); | |
953704c1 RR |
723 | } |
724 | ||
b4071e91 RR |
725 | void wxComboBox::OnSize( wxSizeEvent &event ) |
726 | { | |
f03fc89f | 727 | event.Skip(); |
31528cd3 | 728 | |
b02da6b1 | 729 | #if 0 |
fd0eed64 RR |
730 | int w = 21; |
731 | gtk_widget_set_usize( GTK_COMBO(m_widget)->entry, m_width-w-1, m_height ); | |
805dd538 | 732 | |
fd0eed64 RR |
733 | gtk_widget_set_uposition( GTK_COMBO(m_widget)->button, m_x+m_width-w, m_y ); |
734 | gtk_widget_set_usize( GTK_COMBO(m_widget)->button, w, m_height ); | |
b02da6b1 | 735 | #endif // 0 |
6de97a3b | 736 | } |
53010e52 | 737 | |
58614078 | 738 | void wxComboBox::ApplyWidgetStyle() |
868a2826 | 739 | { |
fd0eed64 | 740 | SetWidgetStyle(); |
805dd538 | 741 | |
72a16063 | 742 | // gtk_widget_set_style( GTK_COMBO(m_widget)->button, m_widgetStyle ); |
fd0eed64 RR |
743 | gtk_widget_set_style( GTK_COMBO(m_widget)->entry, m_widgetStyle ); |
744 | gtk_widget_set_style( GTK_COMBO(m_widget)->list, m_widgetStyle ); | |
805dd538 | 745 | |
fd0eed64 RR |
746 | GtkList *list = GTK_LIST( GTK_COMBO(m_widget)->list ); |
747 | GList *child = list->children; | |
748 | while (child) | |
749 | { | |
750 | gtk_widget_set_style( GTK_WIDGET(child->data), m_widgetStyle ); | |
805dd538 | 751 | |
fd0eed64 RR |
752 | GtkBin *bin = GTK_BIN(child->data); |
753 | gtk_widget_set_style( bin->child, m_widgetStyle ); | |
805dd538 | 754 | |
fd0eed64 RR |
755 | child = child->next; |
756 | } | |
868a2826 | 757 | } |
b4071e91 | 758 | |
fd0eed64 | 759 | GtkWidget* wxComboBox::GetConnectWidget() |
97b3455a | 760 | { |
fd0eed64 | 761 | return GTK_COMBO(m_widget)->entry; |
97b3455a RR |
762 | } |
763 | ||
b4071e91 RR |
764 | bool wxComboBox::IsOwnGtkWindow( GdkWindow *window ) |
765 | { | |
fd0eed64 RR |
766 | return ( (window == GTK_ENTRY( GTK_COMBO(m_widget)->entry )->text_area) || |
767 | (window == GTK_COMBO(m_widget)->button->window ) ); | |
b4071e91 | 768 | } |
ac57418f | 769 | |
f68586e5 VZ |
770 | wxSize wxComboBox::DoGetBestSize() const |
771 | { | |
db434467 | 772 | wxSize ret( wxControl::DoGetBestSize() ); |
a6fc8ae3 VZ |
773 | |
774 | // we know better our horizontal extent: it depends on the longest string | |
775 | // in the combobox | |
776 | ret.x = 0; | |
777 | if ( m_widget ) | |
778 | { | |
60d85ccb RR |
779 | int width; |
780 | size_t count = GetCount(); | |
a6fc8ae3 VZ |
781 | for ( size_t n = 0; n < count; n++ ) |
782 | { | |
2b1ff57f | 783 | GetTextExtent( GetString(n), &width, NULL, NULL, NULL ); |
a6fc8ae3 VZ |
784 | if ( width > ret.x ) |
785 | ret.x = width; | |
786 | } | |
787 | } | |
788 | ||
789 | // empty combobox should have some reasonable default size too | |
790 | if ( ret.x < 100 ) | |
791 | ret.x = 100; | |
db434467 | 792 | return ret; |
f68586e5 VZ |
793 | } |
794 | ||
dcf924a3 | 795 | #endif |