X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/a2c9411060933aa5e92edf3e6251f9fc90090d20..e83c4d401206ff9d5568f03ade6d9713c57bdd51:/src/gtk/combobox.cpp?ds=sidebyside diff --git a/src/gtk/combobox.cpp b/src/gtk/combobox.cpp index 88a9c2fd29..a67b4a852b 100644 --- a/src/gtk/combobox.cpp +++ b/src/gtk/combobox.cpp @@ -21,7 +21,9 @@ #include "wx/arrstr.h" #endif +#include #include "wx/gtk/private.h" +#include "wx/gtk/private/gtk2-compat.h" // ---------------------------------------------------------------------------- // GTK callbacks @@ -44,14 +46,26 @@ gtkcombobox_changed_callback( GtkWidget *WXUNUSED(widget), wxComboBox *combo ) { combo->SendSelectionChangedEvent(wxEVT_COMMAND_COMBOBOX_SELECTED); } + +static void +gtkcombobox_popupshown_callback(GObject *WXUNUSED(gobject), + GParamSpec *WXUNUSED(param_spec), + wxComboBox *combo) +{ + gboolean isShown; + g_object_get( combo->m_widget, "popup-shown", &isShown, NULL ); + wxCommandEvent event( isShown ? wxEVT_COMMAND_COMBOBOX_DROPDOWN + : wxEVT_COMMAND_COMBOBOX_CLOSEUP, + combo->GetId() ); + event.SetEventObject( combo ); + combo->HandleWindowEvent( event ); +} } //----------------------------------------------------------------------------- // wxComboBox //----------------------------------------------------------------------------- -IMPLEMENT_DYNAMIC_CLASS(wxComboBox, wxChoice) - BEGIN_EVENT_TABLE(wxComboBox, wxChoice) EVT_CHAR(wxComboBox::OnChar) @@ -72,6 +86,11 @@ BEGIN_EVENT_TABLE(wxComboBox, wxChoice) EVT_UPDATE_UI(wxID_SELECTALL, wxComboBox::OnUpdateSelectAll) END_EVENT_TABLE() +void wxComboBox::Init() +{ + m_entry = NULL; +} + bool wxComboBox::Create( wxWindow *parent, wxWindowID id, const wxString& value, const wxPoint& pos, const wxSize& size, @@ -91,8 +110,6 @@ bool wxComboBox::Create( wxWindow *parent, wxWindowID id, const wxString& value, long style, const wxValidator& validator, const wxString& name ) { - m_strings = NULL; - if (!PreCreation( parent, pos, size ) || !CreateBase( parent, id, pos, size, style, validator, name )) { @@ -101,14 +118,9 @@ bool wxComboBox::Create( wxWindow *parent, wxWindowID id, const wxString& value, } if (HasFlag(wxCB_SORT)) - m_strings = new wxSortedArrayString(); + m_strings = new wxGtkCollatedArrayString(); - m_widget = gtk_combo_box_entry_new_text(); - - // Set it up to trigger default item on enter key press - GtkWidget *widget = gtk_bin_get_child(GTK_BIN(m_widget)); - gtk_entry_set_activates_default(GTK_ENTRY(widget), - !HasFlag(wxTE_PROCESS_ENTER)); + GTKCreateComboBoxWidget(); if (HasFlag(wxBORDER_NONE)) { @@ -118,40 +130,76 @@ bool wxComboBox::Create( wxWindow *parent, wxWindowID id, const wxString& value, GtkEntry * const entry = GetEntry(); - gtk_entry_set_editable( entry, TRUE ); + if ( entry ) + { + // Set it up to trigger default item on enter key press + gtk_entry_set_activates_default( entry, + !HasFlag(wxTE_PROCESS_ENTER) ); + + gtk_editable_set_editable(GTK_EDITABLE(entry), true); + } Append(n, choices); m_parent->DoAddChild( this ); - m_focusWidget = GTK_WIDGET( entry ); + if ( entry ) + m_focusWidget = GTK_WIDGET( entry ); PostCreation(size); - gtk_entry_set_text( entry, wxGTK_CONV(value) ); - - if (style & wxCB_READONLY) - gtk_entry_set_editable( entry, FALSE ); - - g_signal_connect_after (entry, "changed", - G_CALLBACK (gtkcombobox_text_changed_callback), this); + if ( entry ) + { + if (style & wxCB_READONLY) + { + // this will assert and do nothing if the value is not in our list + // of strings which is the desired behaviour (for consistency with + // wxMSW and also because it doesn't make sense to have a string + // which is not a possible choice in a read-only combobox) + SetStringSelection(value); + gtk_editable_set_editable(GTK_EDITABLE(entry), false); + } + else // editable combobox + { + // any value is accepted, even if it's not in our list + gtk_entry_set_text( entry, wxGTK_CONV(value) ); + } + + g_signal_connect_after (entry, "changed", + G_CALLBACK (gtkcombobox_text_changed_callback), this); + } g_signal_connect_after (m_widget, "changed", G_CALLBACK (gtkcombobox_changed_callback), this); +#ifndef __WXGTK3__ + if ( !gtk_check_version(2,10,0) ) +#endif + { + g_signal_connect (m_widget, "notify::popup-shown", + G_CALLBACK (gtkcombobox_popupshown_callback), this); + } + SetInitialSize(size); // need this too because this is a wxControlWithItems return true; } -GtkEntry *wxComboBox::GetEntry() const +void wxComboBox::GTKCreateComboBoxWidget() { - return GTK_ENTRY(GTK_BIN(m_widget)->child); +#ifdef __WXGTK3__ + m_widget = gtk_combo_box_text_new_with_entry(); +#else + m_widget = gtk_combo_box_entry_new_text(); +#endif + g_object_ref(m_widget); + + m_entry = GTK_ENTRY(gtk_bin_get_child(GTK_BIN(m_widget))); } GtkEditable *wxComboBox::GetEditable() const { - return GTK_EDITABLE( GTK_BIN(m_widget)->child ); + return GTK_EDITABLE(gtk_bin_get_child(GTK_BIN(m_widget))); } void wxComboBox::OnChar( wxKeyEvent &event ) @@ -159,7 +207,7 @@ void wxComboBox::OnChar( wxKeyEvent &event ) switch ( event.GetKeyCode() ) { case WXK_RETURN: - if ( HasFlag(wxTE_PROCESS_ENTER) ) + if ( HasFlag(wxTE_PROCESS_ENTER) && GetEntry() ) { // GTK automatically selects an item if its in the list wxCommandEvent eventEnter(wxEVT_COMMAND_TEXT_ENTER, GetId()); @@ -180,22 +228,41 @@ void wxComboBox::OnChar( wxKeyEvent &event ) event.Skip(); } -void wxComboBox::DisableEvents() +void wxComboBox::EnableTextChangedEvents(bool enable) { - g_signal_handlers_block_by_func(GTK_BIN(m_widget)->child, - (gpointer)gtkcombobox_text_changed_callback, this); + if ( !GetEntry() ) + return; + + if ( enable ) + { + g_signal_handlers_unblock_by_func(gtk_bin_get_child(GTK_BIN(m_widget)), + (gpointer)gtkcombobox_text_changed_callback, this); + } + else // disable + { + g_signal_handlers_block_by_func(gtk_bin_get_child(GTK_BIN(m_widget)), + (gpointer)gtkcombobox_text_changed_callback, this); + } +} + +void wxComboBox::GTKDisableEvents() +{ + EnableTextChangedEvents(false); g_signal_handlers_block_by_func(m_widget, (gpointer)gtkcombobox_changed_callback, this); + g_signal_handlers_block_by_func(m_widget, + (gpointer)gtkcombobox_popupshown_callback, this); } -void wxComboBox::EnableEvents() +void wxComboBox::GTKEnableEvents() { - g_signal_handlers_unblock_by_func(GTK_BIN(m_widget)->child, - (gpointer)gtkcombobox_text_changed_callback, this); + EnableTextChangedEvents(true); g_signal_handlers_unblock_by_func(m_widget, (gpointer)gtkcombobox_changed_callback, this); + g_signal_handlers_unblock_by_func(m_widget, + (gpointer)gtkcombobox_popupshown_callback, this); } GtkWidget* wxComboBox::GetConnectWidget() @@ -203,18 +270,50 @@ GtkWidget* wxComboBox::GetConnectWidget() return GTK_WIDGET( GetEntry() ); } -GdkWindow *wxComboBox::GTKGetWindow(wxArrayGdkWindows& windows) const +GdkWindow* wxComboBox::GTKGetWindow(wxArrayGdkWindows& /* windows */) const { - wxUnusedVar(windows); - - return GetEntry()->text_area; +#ifdef __WXGTK3__ + // no access to internal GdkWindows + return NULL; +#else + return gtk_entry_get_text_window(GetEntry()); +#endif } // static wxVisualAttributes wxComboBox::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant)) { +#ifdef __WXGTK3__ + return GetDefaultAttributesFromGTKWidget(gtk_combo_box_new_with_entry, true); +#else return GetDefaultAttributesFromGTKWidget(gtk_combo_box_entry_new, true); +#endif +} + +void wxComboBox::SetValue(const wxString& value) +{ + if ( HasFlag(wxCB_READONLY) ) + SetStringSelection(value); + else + wxTextEntry::SetValue(value); +} + +void wxComboBox::SetString(unsigned int n, const wxString& text) +{ + wxChoice::SetString(n, text); + + if ( static_cast(n) == GetSelection() ) + { + // We also need to update the currently shown text, for consistency + // with wxMSW and also because it makes sense as leaving the old string + // in the text but not in the list would be confusing to the user. + SetValue(text); + + // And we need to keep the selection unchanged, modifying the item is + // not supposed to deselect it. + SetSelection(n); + } } // ---------------------------------------------------------------------------- @@ -291,4 +390,13 @@ void wxComboBox::OnUpdateSelectAll(wxUpdateUIEvent& event) event.Enable(!wxTextEntry::IsEmpty()); } +void wxComboBox::Popup() +{ + gtk_combo_box_popup( GTK_COMBO_BOX(m_widget) ); +} + +void wxComboBox::Dismiss() +{ + gtk_combo_box_popdown( GTK_COMBO_BOX(m_widget) ); +} #endif // wxUSE_COMBOBOX