From 78b3b018ce9edca5742d8ab263d849f77f3d19bf Mon Sep 17 00:00:00 2001 From: Robert Roebling Date: Wed, 8 Dec 2004 22:08:28 +0000 Subject: [PATCH] Corrected wxComboBox::GetValue() after a selection has changed. Added test for that to the "widgets" sample. git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@30906 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- include/wx/gtk/combobox.h | 2 +- include/wx/gtk1/combobox.h | 2 +- samples/widgets/combobox.cpp | 2 ++ src/gtk/combobox.cpp | 67 +++++++++++++++++++++++------------- src/gtk1/combobox.cpp | 67 +++++++++++++++++++++++------------- 5 files changed, 92 insertions(+), 48 deletions(-) diff --git a/include/wx/gtk/combobox.h b/include/wx/gtk/combobox.h index c4c7bf3d89..0592f6f193 100644 --- a/include/wx/gtk/combobox.h +++ b/include/wx/gtk/combobox.h @@ -121,7 +121,7 @@ public: void OnSize( wxSizeEvent &event ); void OnChar( wxKeyEvent &event ); - bool m_alreadySent; + bool m_ignoreNextUpdate:1; wxList m_clientDataList; wxList m_clientObjectList; int m_prevSelection; diff --git a/include/wx/gtk1/combobox.h b/include/wx/gtk1/combobox.h index c4c7bf3d89..0592f6f193 100644 --- a/include/wx/gtk1/combobox.h +++ b/include/wx/gtk1/combobox.h @@ -121,7 +121,7 @@ public: void OnSize( wxSizeEvent &event ); void OnChar( wxKeyEvent &event ); - bool m_alreadySent; + bool m_ignoreNextUpdate:1; wxList m_clientDataList; wxList m_clientObjectList; int m_prevSelection; diff --git a/samples/widgets/combobox.cpp b/samples/widgets/combobox.cpp index 9bfdc11157..72c4b023f1 100644 --- a/samples/widgets/combobox.cpp +++ b/samples/widgets/combobox.cpp @@ -545,6 +545,8 @@ void ComboboxWidgetsPage::OnComboBox(wxCommandEvent& event) m_textDelete->SetValue(wxString::Format(_T("%ld"), sel)); wxLogMessage(_T("Combobox item %ld selected"), sel); + + wxLogMessage(_T("Combobox GetValue(): %s"), m_combobox->GetValue().c_str() ); } void ComboboxWidgetsPage::OnCheckOrRadioBox(wxCommandEvent& WXUNUSED(event)) diff --git a/src/gtk/combobox.cpp b/src/gtk/combobox.cpp index 7d42568e62..b72887da0c 100644 --- a/src/gtk/combobox.cpp +++ b/src/gtk/combobox.cpp @@ -39,6 +39,35 @@ extern bool g_isIdle; extern bool g_blockEventsOnDrag; +//----------------------------------------------------------------------------- +// "changed" - typing and list item matches get changed, select-child +// if it doesn't match an item then just get a single changed +//----------------------------------------------------------------------------- + +static void +gtk_text_changed_callback( GtkWidget *WXUNUSED(widget), wxComboBox *combo ) +{ + if (g_isIdle) wxapp_install_idle_handler(); + + if (combo->m_ignoreNextUpdate) + { + combo->m_ignoreNextUpdate = FALSE; + return; + } + + if (!combo->m_hasVMT) return; + + wxCommandEvent event( wxEVT_COMMAND_TEXT_UPDATED, combo->GetId() ); + event.SetString( combo->GetValue() ); + event.SetEventObject( combo ); + combo->GetEventHandler()->ProcessEvent( event ); +} + +static void +gtk_dummy_callback(GtkEntry *WXUNUSED(entry), GtkCombo *WXUNUSED(combo)) +{ +} + //----------------------------------------------------------------------------- // "select-child" - click/cursor get select-child, changed, select-child //----------------------------------------------------------------------------- @@ -61,35 +90,27 @@ gtk_combo_select_child_callback( GtkList *WXUNUSED(list), GtkWidget *WXUNUSED(wi combo->m_prevSelection = curSelection; + // Quickly set the value of the combo box + // as GTK+ does that only AFTER the event + // is sent. + gtk_signal_disconnect_by_func( GTK_OBJECT(GTK_COMBO(combo->GetHandle())->entry), + GTK_SIGNAL_FUNC(gtk_text_changed_callback), (gpointer)combo ); + combo->SetValue( combo->GetStringSelection() ); + gtk_signal_connect( GTK_OBJECT(GTK_COMBO(combo->GetHandle())->entry), "changed", + GTK_SIGNAL_FUNC(gtk_text_changed_callback), (gpointer)combo ); + wxCommandEvent event( wxEVT_COMMAND_COMBOBOX_SELECTED, combo->GetId() ); event.SetInt( curSelection ); event.SetString( combo->GetStringSelection() ); event.SetEventObject( combo ); combo->GetEventHandler()->ProcessEvent( event ); -} - -//----------------------------------------------------------------------------- -// "changed" - typing and list item matches get changed, select-child -// if it doesn't match an item then just get a single changed -//----------------------------------------------------------------------------- - -static void -gtk_text_changed_callback( GtkWidget *WXUNUSED(widget), wxComboBox *combo ) -{ - if (g_isIdle) wxapp_install_idle_handler(); - - if (!combo->m_hasVMT) return; - - wxCommandEvent event( wxEVT_COMMAND_TEXT_UPDATED, combo->GetId() ); - event.SetString( combo->GetValue() ); - event.SetEventObject( combo ); - combo->GetEventHandler()->ProcessEvent( event ); -} -static void -gtk_dummy_callback(GtkEntry *WXUNUSED(entry), GtkCombo *WXUNUSED(combo)) -{ + // Now send the event ourselves + wxCommandEvent event2( wxEVT_COMMAND_TEXT_UPDATED, combo->GetId() ); + event2.SetString( combo->GetValue() ); + event2.SetEventObject( combo ); + combo->GetEventHandler()->ProcessEvent( event2 ); } //----------------------------------------------------------------------------- @@ -122,7 +143,7 @@ bool wxComboBox::Create( wxWindow *parent, wxWindowID id, const wxString& value, long style, const wxValidator& validator, const wxString& name ) { - m_alreadySent = FALSE; + m_ignoreNextUpdate = FALSE; m_needParent = TRUE; m_acceptsFocus = TRUE; m_prevSelection = 0; diff --git a/src/gtk1/combobox.cpp b/src/gtk1/combobox.cpp index 7d42568e62..b72887da0c 100644 --- a/src/gtk1/combobox.cpp +++ b/src/gtk1/combobox.cpp @@ -39,6 +39,35 @@ extern bool g_isIdle; extern bool g_blockEventsOnDrag; +//----------------------------------------------------------------------------- +// "changed" - typing and list item matches get changed, select-child +// if it doesn't match an item then just get a single changed +//----------------------------------------------------------------------------- + +static void +gtk_text_changed_callback( GtkWidget *WXUNUSED(widget), wxComboBox *combo ) +{ + if (g_isIdle) wxapp_install_idle_handler(); + + if (combo->m_ignoreNextUpdate) + { + combo->m_ignoreNextUpdate = FALSE; + return; + } + + if (!combo->m_hasVMT) return; + + wxCommandEvent event( wxEVT_COMMAND_TEXT_UPDATED, combo->GetId() ); + event.SetString( combo->GetValue() ); + event.SetEventObject( combo ); + combo->GetEventHandler()->ProcessEvent( event ); +} + +static void +gtk_dummy_callback(GtkEntry *WXUNUSED(entry), GtkCombo *WXUNUSED(combo)) +{ +} + //----------------------------------------------------------------------------- // "select-child" - click/cursor get select-child, changed, select-child //----------------------------------------------------------------------------- @@ -61,35 +90,27 @@ gtk_combo_select_child_callback( GtkList *WXUNUSED(list), GtkWidget *WXUNUSED(wi combo->m_prevSelection = curSelection; + // Quickly set the value of the combo box + // as GTK+ does that only AFTER the event + // is sent. + gtk_signal_disconnect_by_func( GTK_OBJECT(GTK_COMBO(combo->GetHandle())->entry), + GTK_SIGNAL_FUNC(gtk_text_changed_callback), (gpointer)combo ); + combo->SetValue( combo->GetStringSelection() ); + gtk_signal_connect( GTK_OBJECT(GTK_COMBO(combo->GetHandle())->entry), "changed", + GTK_SIGNAL_FUNC(gtk_text_changed_callback), (gpointer)combo ); + wxCommandEvent event( wxEVT_COMMAND_COMBOBOX_SELECTED, combo->GetId() ); event.SetInt( curSelection ); event.SetString( combo->GetStringSelection() ); event.SetEventObject( combo ); combo->GetEventHandler()->ProcessEvent( event ); -} - -//----------------------------------------------------------------------------- -// "changed" - typing and list item matches get changed, select-child -// if it doesn't match an item then just get a single changed -//----------------------------------------------------------------------------- - -static void -gtk_text_changed_callback( GtkWidget *WXUNUSED(widget), wxComboBox *combo ) -{ - if (g_isIdle) wxapp_install_idle_handler(); - - if (!combo->m_hasVMT) return; - - wxCommandEvent event( wxEVT_COMMAND_TEXT_UPDATED, combo->GetId() ); - event.SetString( combo->GetValue() ); - event.SetEventObject( combo ); - combo->GetEventHandler()->ProcessEvent( event ); -} -static void -gtk_dummy_callback(GtkEntry *WXUNUSED(entry), GtkCombo *WXUNUSED(combo)) -{ + // Now send the event ourselves + wxCommandEvent event2( wxEVT_COMMAND_TEXT_UPDATED, combo->GetId() ); + event2.SetString( combo->GetValue() ); + event2.SetEventObject( combo ); + combo->GetEventHandler()->ProcessEvent( event2 ); } //----------------------------------------------------------------------------- @@ -122,7 +143,7 @@ bool wxComboBox::Create( wxWindow *parent, wxWindowID id, const wxString& value, long style, const wxValidator& validator, const wxString& name ) { - m_alreadySent = FALSE; + m_ignoreNextUpdate = FALSE; m_needParent = TRUE; m_acceptsFocus = TRUE; m_prevSelection = 0; -- 2.45.2