only generate wxEVT_COMMAND_TEXT_ENTER if the combobox has wxTE_PROCESS_ENTER style...
[wxWidgets.git] / src / gtk / combobox.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/gtk/combobox.cpp
3 // Purpose:
4 // Author: Robert Roebling
5 // Id: $Id$
6 // Copyright: (c) 1998 Robert Roebling
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
9
10 // For compilers that support precompilation, includes "wx.h".
11 #include "wx/wxprec.h"
12
13 #if wxUSE_COMBOBOX
14
15 #include "wx/combobox.h"
16
17 #ifndef WX_PRECOMP
18 #include "wx/intl.h"
19 #include "wx/settings.h"
20 #include "wx/textctrl.h" // for wxEVT_COMMAND_TEXT_UPDATED
21 #include "wx/arrstr.h"
22 #endif
23
24 // We use GtkCombo which has been deprecated since GTK+ 2.3.0
25 // in favour of GtkComboBox for <GTK2.4 runtime
26 // We also use GtkList
27 #ifdef GTK_DISABLE_DEPRECATED
28 #undef GTK_DISABLE_DEPRECATED
29 #endif
30 #include "wx/gtk/private.h"
31
32 //-----------------------------------------------------------------------------
33 // data
34 //-----------------------------------------------------------------------------
35
36 extern bool g_blockEventsOnDrag;
37 static int g_SelectionBeforePopup = wxID_NONE; // this means the popup is hidden
38
39 //-----------------------------------------------------------------------------
40 // "changed" - typing and list item matches get changed, select-child
41 // if it doesn't match an item then just get a single changed
42 //-----------------------------------------------------------------------------
43
44 extern "C" {
45 static void
46 gtkcombo_text_changed_callback( GtkWidget *WXUNUSED(widget), wxComboBox *combo )
47 {
48 if (combo->m_ignoreNextUpdate)
49 {
50 combo->m_ignoreNextUpdate = false;
51 return;
52 }
53
54 if (!combo->m_hasVMT) return;
55
56 wxCommandEvent event( wxEVT_COMMAND_TEXT_UPDATED, combo->GetId() );
57 event.SetString( combo->GetValue() );
58 event.SetEventObject( combo );
59 combo->GetEventHandler()->ProcessEvent( event );
60 }
61 }
62
63 extern "C" {
64 static void
65 gtkcombo_dummy_callback(GtkEntry *WXUNUSED(entry), GtkCombo *WXUNUSED(combo))
66 {
67 }
68 }
69
70 extern "C" {
71 static void
72 gtkcombo_popup_hide_callback(GtkCombo *WXUNUSED(gtk_combo), wxComboBox *combo)
73 {
74 // when the popup is hidden, throw a SELECTED event only if the combobox
75 // selection changed.
76 const int curSelection = combo->GetCurrentSelection();
77
78 const bool hasChanged = curSelection != g_SelectionBeforePopup;
79
80 // reset the selection flag to value meaning that it is hidden and do it
81 // now, before generating the events, so that GetSelection() returns the
82 // new value from the event handler
83 g_SelectionBeforePopup = wxID_NONE;
84
85 if ( hasChanged )
86 {
87 wxCommandEvent event( wxEVT_COMMAND_COMBOBOX_SELECTED, combo->GetId() );
88 event.SetInt( curSelection );
89 event.SetString( combo->GetStringSelection() );
90 event.SetEventObject( combo );
91 combo->GetEventHandler()->ProcessEvent( event );
92
93 // for consistency with the other ports, send TEXT event
94 wxCommandEvent event2( wxEVT_COMMAND_TEXT_UPDATED, combo->GetId() );
95 event2.SetString( combo->GetStringSelection() );
96 event2.SetEventObject( combo );
97 combo->GetEventHandler()->ProcessEvent( event2 );
98 }
99 }
100 }
101
102 extern "C" {
103 static void
104 gtkcombo_popup_show_callback(GtkCombo *WXUNUSED(gtk_combo), wxComboBox *combo)
105 {
106 // store the combobox selection value before the popup is shown
107 g_SelectionBeforePopup = combo->GetCurrentSelection();
108 }
109 }
110
111 //-----------------------------------------------------------------------------
112 // "select-child" - click/cursor get select-child, changed, select-child
113 //-----------------------------------------------------------------------------
114
115 extern "C" {
116 static void
117 gtkcombo_combo_select_child_callback( GtkList *WXUNUSED(list), GtkWidget *WXUNUSED(widget), wxComboBox *combo )
118 {
119 if (!combo->m_hasVMT) return;
120
121 if (g_blockEventsOnDrag) return;
122
123 int curSelection = combo->GetCurrentSelection();
124
125 if (combo->m_prevSelection == curSelection) return;
126
127 GtkWidget *list = GTK_COMBO(combo->m_widget)->list;
128 gtk_list_unselect_item( GTK_LIST(list), combo->m_prevSelection );
129
130 combo->m_prevSelection = curSelection;
131
132 // Quickly set the value of the combo box
133 // as GTK+ does that only AFTER the event
134 // is sent.
135 GtkWidget* entry = GTK_COMBO(combo->GetHandle())->entry;
136 g_signal_handlers_block_by_func(
137 entry, (gpointer)gtkcombo_text_changed_callback, combo);
138 combo->SetValue( combo->GetStringSelection() );
139 g_signal_handlers_unblock_by_func(
140 entry, (gpointer)gtkcombo_text_changed_callback, combo);
141
142 // throw a SELECTED event only if the combobox popup is hidden (wxID_NONE)
143 // because when combobox popup is shown, gtkcombo_combo_select_child_callback is
144 // called each times the mouse is over an item with a pressed button so a lot
145 // of SELECTED event could be generated if the user keep the mouse button down
146 // and select other items ...
147 if (g_SelectionBeforePopup == wxID_NONE)
148 {
149 wxCommandEvent event( wxEVT_COMMAND_COMBOBOX_SELECTED, combo->GetId() );
150 event.SetInt( curSelection );
151 event.SetString( combo->GetStringSelection() );
152 event.SetEventObject( combo );
153 combo->GetEventHandler()->ProcessEvent( event );
154
155 // for consistency with the other ports, don't generate text update
156 // events while the user is browsing the combobox neither
157 wxCommandEvent event2( wxEVT_COMMAND_TEXT_UPDATED, combo->GetId() );
158 event2.SetString( combo->GetValue() );
159 event2.SetEventObject( combo );
160 combo->GetEventHandler()->ProcessEvent( event2 );
161 }
162 }
163 }
164
165 #ifdef __WXGTK24__
166 extern "C" {
167 static void
168 gtkcombobox_text_changed_callback( GtkWidget *WXUNUSED(widget), wxComboBox *combo )
169 {
170 if (!combo->m_hasVMT) return;
171
172 wxCommandEvent event( wxEVT_COMMAND_TEXT_UPDATED, combo->GetId() );
173 event.SetString( combo->GetValue() );
174 event.SetEventObject( combo );
175 combo->GetEventHandler()->ProcessEvent( event );
176 }
177 }
178
179 extern "C" {
180 static void
181 gtkcombobox_changed_callback( GtkWidget *WXUNUSED(widget), wxComboBox *combo )
182 {
183 if (!combo->m_hasVMT) return;
184
185 if (combo->GetSelection() == -1)
186 return;
187
188 wxCommandEvent event( wxEVT_COMMAND_COMBOBOX_SELECTED, combo->GetId() );
189 event.SetInt( combo->GetSelection() );
190 event.SetString( combo->GetStringSelection() );
191 event.SetEventObject( combo );
192 combo->GetEventHandler()->ProcessEvent( event );
193 }
194 }
195
196 #endif
197
198 //-----------------------------------------------------------------------------
199 // wxComboBox
200 //-----------------------------------------------------------------------------
201
202 IMPLEMENT_DYNAMIC_CLASS(wxComboBox,wxControl)
203
204 BEGIN_EVENT_TABLE(wxComboBox, wxControl)
205 EVT_SIZE(wxComboBox::OnSize)
206 EVT_CHAR(wxComboBox::OnChar)
207
208 EVT_MENU(wxID_CUT, wxComboBox::OnCut)
209 EVT_MENU(wxID_COPY, wxComboBox::OnCopy)
210 EVT_MENU(wxID_PASTE, wxComboBox::OnPaste)
211 EVT_MENU(wxID_UNDO, wxComboBox::OnUndo)
212 EVT_MENU(wxID_REDO, wxComboBox::OnRedo)
213 EVT_MENU(wxID_CLEAR, wxComboBox::OnDelete)
214 EVT_MENU(wxID_SELECTALL, wxComboBox::OnSelectAll)
215
216 EVT_UPDATE_UI(wxID_CUT, wxComboBox::OnUpdateCut)
217 EVT_UPDATE_UI(wxID_COPY, wxComboBox::OnUpdateCopy)
218 EVT_UPDATE_UI(wxID_PASTE, wxComboBox::OnUpdatePaste)
219 EVT_UPDATE_UI(wxID_UNDO, wxComboBox::OnUpdateUndo)
220 EVT_UPDATE_UI(wxID_REDO, wxComboBox::OnUpdateRedo)
221 EVT_UPDATE_UI(wxID_CLEAR, wxComboBox::OnUpdateDelete)
222 EVT_UPDATE_UI(wxID_SELECTALL, wxComboBox::OnUpdateSelectAll)
223 END_EVENT_TABLE()
224
225 bool wxComboBox::Create( wxWindow *parent, wxWindowID id,
226 const wxString& value,
227 const wxPoint& pos, const wxSize& size,
228 const wxArrayString& choices,
229 long style, const wxValidator& validator,
230 const wxString& name )
231 {
232 wxCArrayString chs(choices);
233
234 return Create( parent, id, value, pos, size, chs.GetCount(),
235 chs.GetStrings(), style, validator, name );
236 }
237
238 bool wxComboBox::Create( wxWindow *parent, wxWindowID id, const wxString& value,
239 const wxPoint& pos, const wxSize& size,
240 int n, const wxString choices[],
241 long style, const wxValidator& validator,
242 const wxString& name )
243 {
244 m_strings = NULL;
245 m_ignoreNextUpdate = false;
246 m_prevSelection = 0;
247
248 if (!PreCreation( parent, pos, size ) ||
249 !CreateBase( parent, id, pos, size, style, validator, name ))
250 {
251 wxFAIL_MSG( wxT("wxComboBox creation failed") );
252 return false;
253 }
254
255 if(HasFlag(wxCB_SORT))
256 m_strings = new wxSortedArrayString();
257
258 #ifdef __WXGTK24__
259 if (!gtk_check_version(2,4,0))
260 {
261 m_widget = gtk_combo_box_entry_new_text();
262
263 gtk_entry_set_editable( GTK_ENTRY( GTK_BIN(m_widget)->child ), TRUE );
264 }
265 else
266 #endif
267 {
268 m_widget = gtk_combo_new();
269 GtkCombo* combo = GTK_COMBO(m_widget);
270
271 // Disable GTK's broken events ...
272 g_signal_handler_disconnect (combo->entry, combo->entry_change_id);
273 // ... and add surrogate handler.
274 combo->entry_change_id = g_signal_connect (combo->entry, "changed",
275 G_CALLBACK (gtkcombo_dummy_callback),
276 combo);
277
278 // make it more useable
279 gtk_combo_set_use_arrows_always( GTK_COMBO(m_widget), TRUE );
280
281 // and case-sensitive
282 gtk_combo_set_case_sensitive( GTK_COMBO(m_widget), TRUE );
283
284 if (style & wxNO_BORDER)
285 g_object_set (combo->entry, "has-frame", FALSE, NULL );
286 }
287
288 Append(n, choices);
289
290 m_parent->DoAddChild( this );
291
292 GtkEntry *entry = NULL;
293 #ifdef __WXGTK24__
294 if (!gtk_check_version(2,4,0))
295 entry = GTK_ENTRY( GTK_BIN(m_widget)->child );
296 else
297 #endif
298 entry = GTK_ENTRY( GTK_COMBO(m_widget)->entry );
299
300 m_focusWidget = GTK_WIDGET( entry );
301
302 PostCreation(size);
303
304 #ifdef __WXGTK24__
305 if (!gtk_check_version(2,4,0))
306 ConnectWidget( m_widget );
307 else
308 #endif
309 ConnectWidget( GTK_COMBO(m_widget)->button );
310
311 #ifdef __WXGTK24__
312 if (!gtk_check_version(2,4,0))
313 {
314 gtk_entry_set_text( entry, wxGTK_CONV(value) );
315
316 if (style & wxCB_READONLY)
317 gtk_entry_set_editable( entry, FALSE );
318
319 g_signal_connect_after (entry, "changed",
320 G_CALLBACK (gtkcombobox_text_changed_callback), this);
321
322 g_signal_connect_after (m_widget, "changed",
323 G_CALLBACK (gtkcombobox_changed_callback), this);
324
325 }
326 else
327 #endif
328 {
329 GtkCombo *combo = GTK_COMBO(m_widget);
330 // MSW's combo box shows the value and the selection is -1
331 gtk_entry_set_text( entry, wxGTK_CONV(value) );
332 gtk_list_unselect_all( GTK_LIST(combo->list) );
333
334 if (style & wxCB_READONLY)
335 gtk_entry_set_editable( entry, FALSE );
336
337 // "show" and "hide" events are generated when user click on the combobox button which popups a list
338 // this list is the "popwin" gtk widget
339 g_signal_connect (GTK_COMBO(combo)->popwin, "hide",
340 G_CALLBACK (gtkcombo_popup_hide_callback), this);
341 g_signal_connect (GTK_COMBO(combo)->popwin, "show",
342 G_CALLBACK (gtkcombo_popup_show_callback), this);
343 g_signal_connect_after (combo->list, "select-child",
344 G_CALLBACK (gtkcombo_combo_select_child_callback),
345 this);
346 g_signal_connect_after (entry, "changed",
347 G_CALLBACK (gtkcombo_text_changed_callback), this);
348 }
349
350 SetInitialSize(size); // need this too because this is a wxControlWithItems
351
352 return true;
353 }
354
355 GtkEditable *wxComboBox::GetEditable() const
356 {
357 #ifdef __WXGTK24__
358 if ( !gtk_check_version(2,4,0) )
359 return GTK_EDITABLE( GTK_BIN(m_widget)->child );
360 else
361 #endif
362 return GTK_EDITABLE( GTK_COMBO(m_widget)->entry );
363 }
364
365 wxComboBox::~wxComboBox()
366 {
367 Clear();
368
369 delete m_strings;
370 }
371
372 void wxComboBox::SetFocus()
373 {
374 if ( m_hasFocus )
375 {
376 // don't do anything if we already have focus
377 return;
378 }
379
380 gtk_widget_grab_focus( m_focusWidget );
381 }
382
383 int wxComboBox::DoInsertItems(const wxArrayStringsAdapter & items,
384 unsigned int pos,
385 void **clientData, wxClientDataType type)
386 {
387 wxCHECK_MSG( m_widget != NULL, -1, wxT("invalid combobox") );
388
389 wxASSERT_MSG( !IsSorted() || (pos == GetCount()),
390 _T("In a sorted combobox data could only be appended"));
391
392 const int count = items.GetCount();
393
394 int n = wxNOT_FOUND;
395
396 #ifdef __WXGTK24__
397 if (!gtk_check_version(2,4,0))
398 {
399 GtkComboBox* combobox = GTK_COMBO_BOX( m_widget );
400 for( int i = 0; i < count; ++i )
401 {
402 n = pos + i;
403 // If sorted, use this wxSortedArrayStrings to determine
404 // the right insertion point
405 if(m_strings)
406 n = m_strings->Add(items[i]);
407
408 gtk_combo_box_insert_text( combobox, n, wxGTK_CONV( items[i] ) );
409
410 m_clientData.Insert( NULL, n );
411 AssignNewItemClientData(n, clientData, i, type);
412 }
413 }
414 else
415 #endif
416 {
417 DisableEvents();
418
419 GtkWidget *list = GTK_COMBO(m_widget)->list;
420 for( int i = 0; i < count; ++i )
421 {
422 n = pos + i;
423 // If sorted, use this wxSortedArrayStrings to determine
424 // the right insertion point
425 if(m_strings)
426 n = m_strings->Add(items[i]);
427
428 GtkWidget *list_item = gtk_list_item_new_with_label( wxGTK_CONV( items[i] ) );
429
430 // TODO construct a list with all items and call gtk_list_insert_items once?
431 GList *gitem_list = g_list_alloc ();
432 gitem_list->data = list_item;
433 gtk_list_insert_items( GTK_LIST (list), gitem_list, n );
434
435 m_clientData.Insert( NULL, n );
436 AssignNewItemClientData(n, clientData, i, type);
437
438 if (GTK_WIDGET_REALIZED(m_widget))
439 {
440 gtk_widget_realize( list_item );
441 gtk_widget_realize( GTK_BIN(list_item)->child );
442
443 ApplyWidgetStyle();
444 }
445
446 gtk_widget_show( list_item );
447 }
448
449 EnableEvents();
450 }
451
452 InvalidateBestSize();
453
454 return n;
455 }
456
457 void wxComboBox::DoSetItemClientData(unsigned int n, void* clientData)
458 {
459 m_clientData[n] = clientData;
460 }
461
462 void* wxComboBox::DoGetItemClientData(unsigned int n) const
463 {
464 return m_clientData[n];
465 }
466
467 void wxComboBox::DoClear()
468 {
469 wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") );
470
471 DisableEvents();
472
473 #ifdef __WXGTK24__
474 if (!gtk_check_version(2,4,0))
475 {
476 GtkComboBox* combobox = GTK_COMBO_BOX( m_widget );
477 const unsigned int count = GetCount();
478 for (unsigned int i = 0; i < count; i++)
479 gtk_combo_box_remove_text( combobox, 0 );
480 }
481 else // GTK+ < 2.4.0
482 #endif // __WXGTK24__
483 {
484 GtkWidget *list = GTK_COMBO(m_widget)->list;
485 gtk_list_clear_items( GTK_LIST(list), 0, GetCount() );
486 }
487
488 m_clientData.Clear();
489
490 if(m_strings)
491 m_strings->Clear();
492
493 EnableEvents();
494
495 InvalidateBestSize();
496 }
497
498 void wxComboBox::DoDeleteOneItem(unsigned int n)
499 {
500 wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") );
501
502 #ifdef __WXGTK24__
503 if (!gtk_check_version(2,4,0))
504 {
505 wxCHECK_RET( IsValid(n), wxT("invalid index") );
506
507 GtkComboBox* combobox = GTK_COMBO_BOX( m_widget );
508 gtk_combo_box_remove_text( combobox, n );
509 }
510 else
511 #endif
512 {
513 GtkList *listbox = GTK_LIST( GTK_COMBO(m_widget)->list );
514
515 GList *child = g_list_nth( listbox->children, n );
516
517 if (!child)
518 {
519 wxFAIL_MSG(wxT("wrong index"));
520 return;
521 }
522
523 DisableEvents();
524
525 GList *list = g_list_append( (GList*) NULL, child->data );
526 gtk_list_remove_items( listbox, list );
527 g_list_free( list );
528
529 EnableEvents();
530 }
531
532 m_clientData.RemoveAt( n );
533 if(m_strings)
534 m_strings->RemoveAt( n );
535
536 InvalidateBestSize();
537 }
538
539 void wxComboBox::SetString(unsigned int n, const wxString &text)
540 {
541 wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") );
542
543 #ifdef __WXGTK24__
544 if (!gtk_check_version(2,4,0))
545 {
546 GtkComboBox* combobox = GTK_COMBO_BOX( m_widget );
547 wxCHECK_RET( IsValid(n), wxT("invalid index") );
548
549 GtkTreeModel *model = gtk_combo_box_get_model( combobox );
550 GtkTreeIter iter;
551 if (gtk_tree_model_iter_nth_child (model, &iter, NULL, n))
552 {
553 GValue value = { 0, };
554 g_value_init( &value, G_TYPE_STRING );
555 g_value_set_string( &value, wxGTK_CONV( text ) );
556 gtk_list_store_set_value( GTK_LIST_STORE(model), &iter, 0, &value );
557 g_value_unset( &value );
558 }
559 }
560 else
561 #endif
562 {
563 GtkWidget *list = GTK_COMBO(m_widget)->list;
564
565 GList *child = g_list_nth( GTK_LIST(list)->children, n );
566 if (child)
567 {
568 GtkBin *bin = GTK_BIN( child->data );
569 GtkLabel *label = GTK_LABEL( bin->child );
570 gtk_label_set_text(label, wxGTK_CONV(text));
571 }
572 else
573 {
574 wxFAIL_MSG( wxT("wxComboBox: wrong index") );
575 }
576 }
577
578 InvalidateBestSize();
579 }
580
581 int wxComboBox::FindString( const wxString &item, bool bCase ) const
582 {
583 wxCHECK_MSG( m_widget != NULL, wxNOT_FOUND, wxT("invalid combobox") );
584
585 #ifdef __WXGTK24__
586 if (!gtk_check_version(2,4,0))
587 {
588 GtkComboBox* combobox = GTK_COMBO_BOX( m_widget );
589 GtkTreeModel* model = gtk_combo_box_get_model( combobox );
590 GtkTreeIter iter;
591 gtk_tree_model_get_iter_first( model, &iter );
592 if (!gtk_list_store_iter_is_valid(GTK_LIST_STORE(model), &iter ))
593 return -1;
594 int count = 0;
595 do
596 {
597 GValue value = { 0, };
598 gtk_tree_model_get_value( model, &iter, 0, &value );
599 wxString str = wxGTK_CONV_BACK( g_value_get_string( &value ) );
600 g_value_unset( &value );
601
602 if (item.IsSameAs( str, bCase ) )
603 return count;
604
605 count++;
606
607 } while (gtk_tree_model_iter_next( model, &iter ));
608 }
609 else
610 #endif
611 {
612 GtkWidget *list = GTK_COMBO(m_widget)->list;
613
614 GList *child = GTK_LIST(list)->children;
615 int count = 0;
616 while (child)
617 {
618 GtkBin *bin = GTK_BIN( child->data );
619 GtkLabel *label = GTK_LABEL( bin->child );
620 wxString str( wxGTK_CONV_BACK( gtk_label_get_text(label) ) );
621
622 if (item.IsSameAs( str , bCase ) )
623 return count;
624
625 count++;
626 child = child->next;
627 }
628 }
629
630 return wxNOT_FOUND;
631 }
632
633 int wxComboBox::GetSelection() const
634 {
635 #ifdef __WXGTK24__
636 if (!gtk_check_version(2,4,0))
637 {
638 GtkComboBox* combobox = GTK_COMBO_BOX( m_widget );
639 return gtk_combo_box_get_active( combobox );
640 }
641 else
642 #endif
643 // if the popup is currently opened, use the selection as it had been
644 // before it dropped down
645 return g_SelectionBeforePopup == wxID_NONE ? GetCurrentSelection()
646 : g_SelectionBeforePopup;
647 }
648
649 int wxComboBox::GetCurrentSelection() const
650 {
651 wxCHECK_MSG( m_widget != NULL, -1, wxT("invalid combobox") );
652
653 #ifdef __WXGTK24__
654 if (!gtk_check_version(2,4,0))
655 {
656 GtkComboBox* combobox = GTK_COMBO_BOX( m_widget );
657 return gtk_combo_box_get_active( combobox );
658 }
659 else
660 #endif
661 {
662 GtkWidget *list = GTK_COMBO(m_widget)->list;
663
664 GList *selection = GTK_LIST(list)->selection;
665 if (selection)
666 {
667 GList *child = GTK_LIST(list)->children;
668 int count = 0;
669 while (child)
670 {
671 if (child->data == selection->data) return count;
672 count++;
673 child = child->next;
674 }
675 }
676 }
677
678 return -1;
679 }
680
681 wxString wxComboBox::GetString(unsigned int n) const
682 {
683 wxCHECK_MSG( m_widget != NULL, wxEmptyString, wxT("invalid combobox") );
684
685 wxString str;
686
687 #ifdef __WXGTK24__
688 if (!gtk_check_version(2,4,0))
689 {
690 GtkComboBox* combobox = GTK_COMBO_BOX( m_widget );
691 GtkTreeModel *model = gtk_combo_box_get_model( combobox );
692 GtkTreeIter iter;
693 if (gtk_tree_model_iter_nth_child (model, &iter, NULL, n))
694 {
695 GValue value = { 0, };
696 gtk_tree_model_get_value( model, &iter, 0, &value );
697 wxString tmp = wxGTK_CONV_BACK( g_value_get_string( &value ) );
698 g_value_unset( &value );
699 return tmp;
700 }
701 }
702 else
703 #endif
704 {
705 GtkWidget *list = GTK_COMBO(m_widget)->list;
706
707 GList *child = g_list_nth( GTK_LIST(list)->children, n );
708 if (child)
709 {
710 GtkBin *bin = GTK_BIN( child->data );
711 GtkLabel *label = GTK_LABEL( bin->child );
712 str = wxGTK_CONV_BACK( gtk_label_get_text(label) );
713 }
714 else
715 {
716 wxFAIL_MSG( wxT("wxComboBox: wrong index") );
717 }
718 }
719
720 return str;
721 }
722
723 unsigned int wxComboBox::GetCount() const
724 {
725 wxCHECK_MSG( m_widget != NULL, 0, wxT("invalid combobox") );
726
727 #ifdef __WXGTK24__
728 if (!gtk_check_version(2,4,0))
729 {
730 GtkComboBox* combobox = GTK_COMBO_BOX( m_widget );
731 GtkTreeModel* model = gtk_combo_box_get_model( combobox );
732 GtkTreeIter iter;
733 gtk_tree_model_get_iter_first( model, &iter );
734 if (!gtk_list_store_iter_is_valid(GTK_LIST_STORE(model), &iter ))
735 return 0;
736 unsigned int ret = 1;
737 while (gtk_tree_model_iter_next( model, &iter ))
738 ret++;
739 return ret;
740 }
741 else
742 #endif
743 {
744 GtkWidget *list = GTK_COMBO(m_widget)->list;
745
746 GList *child = GTK_LIST(list)->children;
747 unsigned int count = 0;
748 while (child)
749 {
750 count++;
751 child = child->next;
752 }
753 return count;
754 }
755 }
756
757 void wxComboBox::SetSelection( int n )
758 {
759 wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") );
760
761 DisableEvents();
762
763 #ifdef __WXGTK24__
764 if (!gtk_check_version(2,4,0))
765 {
766 GtkComboBox* combobox = GTK_COMBO_BOX( m_widget );
767 gtk_combo_box_set_active( combobox, n );
768 }
769 else
770 #endif
771 {
772 GtkWidget *list = GTK_COMBO(m_widget)->list;
773 gtk_list_unselect_item( GTK_LIST(list), m_prevSelection );
774 gtk_list_select_item( GTK_LIST(list), n );
775 m_prevSelection = n;
776 }
777
778 EnableEvents();
779 }
780
781 void wxComboBox::OnChar( wxKeyEvent &event )
782 {
783 switch ( event.GetKeyCode() )
784 {
785 case WXK_RETURN:
786 if ( HasFlag(wxTE_PROCESS_ENTER) )
787 {
788 // GTK automatically selects an item if its in the list
789 wxCommandEvent eventEnter(wxEVT_COMMAND_TEXT_ENTER, GetId());
790 eventEnter.SetString( GetValue() );
791 eventEnter.SetInt( GetSelection() );
792 eventEnter.SetEventObject( this );
793
794 if ( GetEventHandler()->ProcessEvent(eventEnter) )
795 {
796 // Catch GTK event so that GTK doesn't open the drop
797 // down list upon RETURN.
798 return;
799 }
800 }
801 break;
802 }
803
804 event.Skip();
805 }
806
807 void wxComboBox::DisableEvents()
808 {
809 #ifdef __WXGTK24__
810 if (!gtk_check_version(2,4,0))
811 {
812 g_signal_handlers_block_by_func(GTK_BIN(m_widget)->child,
813 (gpointer)gtkcombobox_text_changed_callback, this);
814
815 g_signal_handlers_block_by_func(m_widget,
816 (gpointer)gtkcombobox_changed_callback, this);
817 }
818 else
819 #endif
820 {
821 g_signal_handlers_block_by_func(GTK_COMBO(m_widget)->list,
822 (gpointer) gtkcombo_combo_select_child_callback, this);
823
824 g_signal_handlers_block_by_func(GTK_COMBO(m_widget)->entry,
825 (gpointer) gtkcombo_text_changed_callback, this);
826 }
827 }
828
829 void wxComboBox::EnableEvents()
830 {
831 #ifdef __WXGTK24__
832 if (!gtk_check_version(2,4,0))
833 {
834 g_signal_handlers_unblock_by_func(GTK_BIN(m_widget)->child,
835 (gpointer)gtkcombobox_text_changed_callback, this);
836
837 g_signal_handlers_unblock_by_func(m_widget,
838 (gpointer)gtkcombobox_changed_callback, this);
839 }
840 else
841 #endif
842 {
843 g_signal_handlers_unblock_by_func(GTK_COMBO(m_widget)->list,
844 (gpointer) gtkcombo_combo_select_child_callback, this);
845
846 g_signal_handlers_unblock_by_func(GTK_COMBO(m_widget)->entry,
847 (gpointer) gtkcombo_text_changed_callback, this);
848 }
849 }
850
851 void wxComboBox::OnSize( wxSizeEvent &event )
852 {
853 #ifdef __WXGTK24__
854 if (!gtk_check_version(2,4,0))
855 {
856 // Do nothing
857 }
858 else
859 #endif
860 {
861 // NB: In some situations (e.g. on non-first page of a wizard, if the
862 // size used is default size), GtkCombo widget is resized correctly,
863 // but it's look is not updated, it's rendered as if it was much wider.
864 // No other widgets are affected, so it looks like a bug in GTK+.
865 // Manually requesting resize calculation (as gtk_pizza_set_size does)
866 // fixes it.
867 if (GTK_WIDGET_VISIBLE(m_widget))
868 gtk_widget_queue_resize(m_widget);
869 }
870
871 event.Skip();
872 }
873
874 void wxComboBox::DoApplyWidgetStyle(GtkRcStyle *style)
875 {
876 #ifdef __WXGTK24__
877 if (!gtk_check_version(2,4,0))
878 {
879 // Do nothing
880 }
881 else
882 #endif
883 {
884 // gtk_widget_modify_style( GTK_COMBO(m_widget)->button, syle );
885
886 gtk_widget_modify_style( GTK_COMBO(m_widget)->entry, style );
887 gtk_widget_modify_style( GTK_COMBO(m_widget)->list, style );
888
889 GtkList *list = GTK_LIST( GTK_COMBO(m_widget)->list );
890 GList *child = list->children;
891 while (child)
892 {
893 gtk_widget_modify_style( GTK_WIDGET(child->data), style );
894
895 GtkBin *bin = GTK_BIN(child->data);
896 gtk_widget_modify_style( bin->child, style );
897
898 child = child->next;
899 }
900 }
901 }
902
903 GtkWidget* wxComboBox::GetConnectWidget()
904 {
905 GtkEntry *entry = NULL;
906 #ifdef __WXGTK24__
907 if (!gtk_check_version(2,4,0))
908 entry = GTK_ENTRY( GTK_BIN(m_widget)->child );
909 else
910 #endif
911 entry = GTK_ENTRY( GTK_COMBO(m_widget)->entry );
912
913 return GTK_WIDGET( entry );
914 }
915
916 GdkWindow *wxComboBox::GTKGetWindow(wxArrayGdkWindows& windows) const
917 {
918 #ifdef __WXGTK24__
919 if (!gtk_check_version(2,4,0))
920 {
921 wxUnusedVar(windows);
922
923 return GTK_ENTRY(GTK_BIN(m_widget)->child)->text_area;
924 }
925 else
926 #endif // GTK+ 2.4
927 {
928 windows.push_back(GTK_ENTRY(GTK_COMBO(m_widget)->entry)->text_area);
929 windows.push_back(GTK_COMBO(m_widget)->button->window);
930
931 // indicate that we return multiple windows in the windows array
932 return NULL;
933 }
934 }
935
936 wxSize wxComboBox::DoGetBestSize() const
937 {
938 wxSize ret( wxControl::DoGetBestSize() );
939
940 // we know better our horizontal extent: it depends on the longest string
941 // in the combobox
942 if ( m_widget )
943 {
944 int width;
945 unsigned int count = GetCount();
946 for ( unsigned int n = 0; n < count; n++ )
947 {
948 GetTextExtent(GetString(n), &width, NULL, NULL, NULL );
949 if ( width > ret.x )
950 ret.x = width;
951 }
952 }
953
954 // empty combobox should have some reasonable default size too
955 if ( ret.x < 100 )
956 ret.x = 100;
957
958 CacheBestSize(ret);
959 return ret;
960 }
961
962 // static
963 wxVisualAttributes
964 wxComboBox::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant))
965 {
966 #ifdef __WXGTK24__
967 if (!gtk_check_version(2,4,0))
968 return GetDefaultAttributesFromGTKWidget(gtk_combo_box_entry_new, true);
969 else
970 #endif
971 return GetDefaultAttributesFromGTKWidget(gtk_combo_new, true);
972 }
973
974 // ----------------------------------------------------------------------------
975 // standard event handling
976 // ----------------------------------------------------------------------------
977
978 void wxComboBox::OnCut(wxCommandEvent& WXUNUSED(event))
979 {
980 Cut();
981 }
982
983 void wxComboBox::OnCopy(wxCommandEvent& WXUNUSED(event))
984 {
985 Copy();
986 }
987
988 void wxComboBox::OnPaste(wxCommandEvent& WXUNUSED(event))
989 {
990 Paste();
991 }
992
993 void wxComboBox::OnUndo(wxCommandEvent& WXUNUSED(event))
994 {
995 Undo();
996 }
997
998 void wxComboBox::OnRedo(wxCommandEvent& WXUNUSED(event))
999 {
1000 Redo();
1001 }
1002
1003 void wxComboBox::OnDelete(wxCommandEvent& WXUNUSED(event))
1004 {
1005 long from, to;
1006 GetSelection(& from, & to);
1007 if (from != -1 && to != -1)
1008 Remove(from, to);
1009 }
1010
1011 void wxComboBox::OnSelectAll(wxCommandEvent& WXUNUSED(event))
1012 {
1013 SetSelection(-1, -1);
1014 }
1015
1016 void wxComboBox::OnUpdateCut(wxUpdateUIEvent& event)
1017 {
1018 event.Enable( CanCut() );
1019 }
1020
1021 void wxComboBox::OnUpdateCopy(wxUpdateUIEvent& event)
1022 {
1023 event.Enable( CanCopy() );
1024 }
1025
1026 void wxComboBox::OnUpdatePaste(wxUpdateUIEvent& event)
1027 {
1028 event.Enable( CanPaste() );
1029 }
1030
1031 void wxComboBox::OnUpdateUndo(wxUpdateUIEvent& event)
1032 {
1033 event.Enable( CanUndo() );
1034 }
1035
1036 void wxComboBox::OnUpdateRedo(wxUpdateUIEvent& event)
1037 {
1038 event.Enable( CanRedo() );
1039 }
1040
1041 void wxComboBox::OnUpdateDelete(wxUpdateUIEvent& event)
1042 {
1043 event.Enable(HasSelection() && IsEditable()) ;
1044 }
1045
1046 void wxComboBox::OnUpdateSelectAll(wxUpdateUIEvent& event)
1047 {
1048 event.Enable(GetLastPosition() > 0);
1049 }
1050
1051 #endif // wxUSE_COMBOBOX