// Name: src/gtk/combobox.cpp
// Purpose:
// Author: Robert Roebling
-// Id: $Id$
// Copyright: (c) 1998 Robert Roebling
// Licence: wxWindows licence
/////////////////////////////////////////////////////////////////////////////
#ifndef WX_PRECOMP
#include "wx/intl.h"
#include "wx/settings.h"
- #include "wx/textctrl.h" // for wxEVT_COMMAND_TEXT_UPDATED
+ #include "wx/textctrl.h" // for wxEVT_TEXT
#include "wx/arrstr.h"
#endif
+#include <gtk/gtk.h>
#include "wx/gtk/private.h"
+#include "wx/gtk/private/gtk2-compat.h"
// ----------------------------------------------------------------------------
// GTK callbacks
static void
gtkcombobox_text_changed_callback( GtkWidget *WXUNUSED(widget), wxComboBox *combo )
{
- if (!combo->m_hasVMT) return;
-
- wxCommandEvent event( wxEVT_COMMAND_TEXT_UPDATED, combo->GetId() );
+ wxCommandEvent event( wxEVT_TEXT, combo->GetId() );
event.SetString( combo->GetValue() );
event.SetEventObject( combo );
- combo->GetEventHandler()->ProcessEvent( event );
+ combo->HandleWindowEvent( event );
}
static void
gtkcombobox_changed_callback( GtkWidget *WXUNUSED(widget), wxComboBox *combo )
{
- if (!combo->m_hasVMT) return;
-
- if (combo->GetSelection() == -1)
- return;
+ combo->SendSelectionChangedEvent(wxEVT_COMBOBOX);
+}
- wxCommandEvent event( wxEVT_COMMAND_COMBOBOX_SELECTED, combo->GetId() );
- event.SetInt( combo->GetSelection() );
- event.SetString( combo->GetStringSelection() );
+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_COMBOBOX_DROPDOWN
+ : wxEVT_COMBOBOX_CLOSEUP,
+ combo->GetId() );
event.SetEventObject( combo );
- combo->GetEventHandler()->ProcessEvent( event );
+ combo->HandleWindowEvent( event );
}
+
}
//-----------------------------------------------------------------------------
// wxComboBox
//-----------------------------------------------------------------------------
-IMPLEMENT_DYNAMIC_CLASS(wxComboBox,wxControl)
-
-BEGIN_EVENT_TABLE(wxComboBox, wxControl)
+BEGIN_EVENT_TABLE(wxComboBox, wxChoice)
EVT_CHAR(wxComboBox::OnChar)
EVT_MENU(wxID_CUT, wxComboBox::OnCut)
EVT_UPDATE_UI(wxID_SELECTALL, wxComboBox::OnUpdateSelectAll)
END_EVENT_TABLE()
+wxComboBox::~wxComboBox()
+{
+ if (m_entry)
+ GTKDisconnect(m_entry);
+}
+
+void wxComboBox::Init()
+{
+ m_entry = NULL;
+}
+
bool wxComboBox::Create( wxWindow *parent, wxWindowID id,
const wxString& value,
const wxPoint& pos, const wxSize& size,
long style, const wxValidator& validator,
const wxString& name )
{
- m_strings = NULL;
- m_ignoreNextUpdate = false;
- m_prevSelection = 0;
-
if (!PreCreation( parent, pos, size ) ||
!CreateBase( parent, id, pos, size, style, validator, name ))
{
}
if (HasFlag(wxCB_SORT))
- m_strings = new wxSortedArrayString();
+ m_strings = new wxGtkCollatedArrayString();
- m_widget = gtk_combo_box_entry_new_text();
+ GTKCreateComboBoxWidget();
if (HasFlag(wxBORDER_NONE))
{
GtkEntry * const entry = GetEntry();
- gtk_entry_set_editable( entry, TRUE );
-
- Append(n, choices);
-
- m_parent->DoAddChild( this );
-
- m_focusWidget = GTK_WIDGET( entry );
-
- PostCreation(size);
-
- ConnectWidget( m_widget );
-
- 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);
-
- g_signal_connect_after (m_widget, "changed",
- G_CALLBACK (gtkcombobox_changed_callback), this);
-
-
- SetInitialSize(size); // need this too because this is a wxControlWithItems
-
- return true;
-}
-
-GtkEntry *wxComboBox::GetEntry() const
-{
- return GTK_ENTRY(GTK_BIN(m_widget)->child);
-}
-
-GtkEditable *wxComboBox::GetEditable() const
-{
- return GTK_EDITABLE( GTK_BIN(m_widget)->child );
-}
-
-wxComboBox::~wxComboBox()
-{
- Clear();
-
- delete m_strings;
-}
-
-void wxComboBox::SetFocus()
-{
- if ( m_hasFocus )
+ if ( entry )
{
- // don't do anything if we already have focus
- return;
- }
+ // Set it up to trigger default item on enter key press
+ gtk_entry_set_activates_default( entry,
+ !HasFlag(wxTE_PROCESS_ENTER) );
- gtk_widget_grab_focus( m_focusWidget );
-}
-
-int wxComboBox::DoInsertItems(const wxArrayStringsAdapter & items,
- unsigned int pos,
- void **clientData, wxClientDataType type)
-{
- wxCHECK_MSG( m_widget != NULL, -1, wxT("invalid combobox") );
-
- wxASSERT_MSG( !IsSorted() || (pos == GetCount()),
- _T("In a sorted combobox data could only be appended"));
-
- const int count = items.GetCount();
-
- int n = wxNOT_FOUND;
-
- GtkComboBox* combobox = GTK_COMBO_BOX( m_widget );
- for( int i = 0; i < count; ++i )
- {
- n = pos + i;
- // If sorted, use this wxSortedArrayStrings to determine
- // the right insertion point
- if(m_strings)
- n = m_strings->Add(items[i]);
-
- gtk_combo_box_insert_text( combobox, n, wxGTK_CONV( items[i] ) );
-
- m_clientData.Insert( NULL, n );
- AssignNewItemClientData(n, clientData, i, type);
+ gtk_editable_set_editable(GTK_EDITABLE(entry), true);
}
- InvalidateBestSize();
-
- return n;
-}
-
-void wxComboBox::DoSetItemClientData(unsigned int n, void* clientData)
-{
- m_clientData[n] = clientData;
-}
-
-void* wxComboBox::DoGetItemClientData(unsigned int n) const
-{
- return m_clientData[n];
-}
-
-void wxComboBox::DoClear()
-{
- wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") );
-
- DisableEvents();
-
- GtkComboBox* combobox = GTK_COMBO_BOX( m_widget );
- const unsigned int count = GetCount();
- for (unsigned int i = 0; i < count; i++)
- gtk_combo_box_remove_text( combobox, 0 );
-
- m_clientData.Clear();
-
- if(m_strings)
- m_strings->Clear();
-
- EnableEvents();
-
- InvalidateBestSize();
-}
-
-void wxComboBox::DoDeleteOneItem(unsigned int n)
-{
- wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") );
-
- wxCHECK_RET( IsValid(n), wxT("invalid index") );
-
- GtkComboBox* combobox = GTK_COMBO_BOX( m_widget );
- gtk_combo_box_remove_text( combobox, n );
-
- m_clientData.RemoveAt( n );
- if(m_strings)
- m_strings->RemoveAt( n );
-
- InvalidateBestSize();
-}
-
-void wxComboBox::SetString(unsigned int n, const wxString &text)
-{
- wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") );
-
- GtkComboBox* combobox = GTK_COMBO_BOX( m_widget );
- wxCHECK_RET( IsValid(n), wxT("invalid index") );
+ Append(n, choices);
- GtkTreeModel *model = gtk_combo_box_get_model( combobox );
- GtkTreeIter iter;
- if (gtk_tree_model_iter_nth_child (model, &iter, NULL, n))
- {
- GValue value = { 0, };
- g_value_init( &value, G_TYPE_STRING );
- g_value_set_string( &value, wxGTK_CONV( text ) );
- gtk_list_store_set_value( GTK_LIST_STORE(model), &iter, 0, &value );
- g_value_unset( &value );
- }
+ m_parent->DoAddChild( this );
- InvalidateBestSize();
-}
+ if ( entry )
+ m_focusWidget = GTK_WIDGET( entry );
-int wxComboBox::FindString( const wxString &item, bool bCase ) const
-{
- wxCHECK_MSG( m_widget != NULL, wxNOT_FOUND, wxT("invalid combobox") );
+ PostCreation(size);
- GtkComboBox* combobox = GTK_COMBO_BOX( m_widget );
- GtkTreeModel* model = gtk_combo_box_get_model( combobox );
- GtkTreeIter iter;
- gtk_tree_model_get_iter_first( model, &iter );
- if (!gtk_list_store_iter_is_valid(GTK_LIST_STORE(model), &iter ))
- return -1;
- int count = 0;
- do
+ if ( entry )
{
- GValue value = { 0, };
- gtk_tree_model_get_value( model, &iter, 0, &value );
- wxString str = wxGTK_CONV_BACK( g_value_get_string( &value ) );
- g_value_unset( &value );
+ 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) );
+ }
- if (item.IsSameAs( str, bCase ) )
- return count;
+ g_signal_connect_after (entry, "changed",
+ G_CALLBACK (gtkcombobox_text_changed_callback), this);
- count++;
+ GTKConnectInsertTextSignal(entry);
+ GTKConnectClipboardSignals(GTK_WIDGET(entry));
}
- while ( gtk_tree_model_iter_next(model, &iter) );
-
- return wxNOT_FOUND;
-}
-int wxComboBox::GetSelection() const
-{
- GtkComboBox* combobox = GTK_COMBO_BOX( m_widget );
- return gtk_combo_box_get_active( combobox );
-}
-
-int wxComboBox::GetCurrentSelection() const
-{
- wxCHECK_MSG( m_widget != NULL, -1, wxT("invalid combobox") );
-
- GtkComboBox* combobox = GTK_COMBO_BOX( m_widget );
- return gtk_combo_box_get_active( combobox );
-}
-
-wxString wxComboBox::GetString(unsigned int n) const
-{
- wxCHECK_MSG( m_widget != NULL, wxEmptyString, wxT("invalid combobox") );
-
- wxString str;
+ g_signal_connect_after (m_widget, "changed",
+ G_CALLBACK (gtkcombobox_changed_callback), this);
- GtkComboBox* combobox = GTK_COMBO_BOX( m_widget );
- GtkTreeModel *model = gtk_combo_box_get_model( combobox );
- GtkTreeIter iter;
- if (gtk_tree_model_iter_nth_child (model, &iter, NULL, n))
+#ifndef __WXGTK3__
+ if ( !gtk_check_version(2,10,0) )
+#endif
{
- GValue value = { 0, };
- gtk_tree_model_get_value( model, &iter, 0, &value );
- wxString tmp = wxGTK_CONV_BACK( g_value_get_string( &value ) );
- g_value_unset( &value );
- return tmp;
+ g_signal_connect (m_widget, "notify::popup-shown",
+ G_CALLBACK (gtkcombobox_popupshown_callback), this);
}
- return str;
+ return true;
}
-unsigned int wxComboBox::GetCount() const
+void wxComboBox::GTKCreateComboBoxWidget()
{
- wxCHECK_MSG( m_widget != NULL, 0, wxT("invalid combobox") );
+#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);
- GtkComboBox* combobox = GTK_COMBO_BOX( m_widget );
- GtkTreeModel* model = gtk_combo_box_get_model( combobox );
- GtkTreeIter iter;
- gtk_tree_model_get_iter_first( model, &iter );
- if (!gtk_list_store_iter_is_valid(GTK_LIST_STORE(model), &iter ))
- return 0;
- unsigned int ret = 1;
- while (gtk_tree_model_iter_next( model, &iter ))
- ret++;
- return ret;
+ m_entry = GTK_ENTRY(gtk_bin_get_child(GTK_BIN(m_widget)));
}
-void wxComboBox::SetSelection( int n )
+GtkEditable *wxComboBox::GetEditable() const
{
- wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") );
-
- DisableEvents();
-
- GtkComboBox* combobox = GTK_COMBO_BOX( m_widget );
- gtk_combo_box_set_active( combobox, n );
-
- EnableEvents();
+ return GTK_EDITABLE(gtk_bin_get_child(GTK_BIN(m_widget)));
}
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());
+ wxCommandEvent eventEnter(wxEVT_TEXT_ENTER, GetId());
eventEnter.SetString( GetValue() );
eventEnter.SetInt( GetSelection() );
eventEnter.SetEventObject( this );
- if ( GetEventHandler()->ProcessEvent(eventEnter) )
+ if ( HandleWindowEvent(eventEnter) )
{
// Catch GTK event so that GTK doesn't open the drop
// down list upon RETURN.
event.Skip();
}
-void wxComboBox::DisableEvents()
+void wxComboBox::EnableTextChangedEvents(bool enable)
+{
+ 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()
{
- g_signal_handlers_block_by_func(GTK_BIN(m_widget)->child,
- (gpointer)gtkcombobox_text_changed_callback, this);
+ 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()
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
}
-wxSize wxComboBox::DoGetBestSize() const
+// static
+wxVisualAttributes
+wxComboBox::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant))
{
- wxSize ret( wxControl::DoGetBestSize() );
-
- // we know better our horizontal extent: it depends on the longest string
- // in the combobox
- if ( m_widget )
- {
- int width;
- unsigned int count = GetCount();
- for ( unsigned int n = 0; n < count; n++ )
- {
- GetTextExtent(GetString(n), &width, NULL, NULL, NULL );
- if ( width > ret.x )
- ret.x = width;
- }
- }
-
- // empty combobox should have some reasonable default size too
- if ( ret.x < 100 )
- ret.x = 100;
+#ifdef __WXGTK3__
+ return GetDefaultAttributesFromGTKWidget(gtk_combo_box_new_with_entry(), true);
+#else
+ return GetDefaultAttributesFromGTKWidget(gtk_combo_box_entry_new(), true);
+#endif
+}
- CacheBestSize(ret);
- return ret;
+void wxComboBox::SetValue(const wxString& value)
+{
+ if ( HasFlag(wxCB_READONLY) )
+ SetStringSelection(value);
+ else
+ wxTextEntry::SetValue(value);
}
-// static
-wxVisualAttributes
-wxComboBox::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant))
+void wxComboBox::SetString(unsigned int n, const wxString& text)
{
- return GetDefaultAttributesFromGTKWidget(gtk_combo_box_entry_new, true);
+ wxChoice::SetString(n, text);
+
+ if ( static_cast<int>(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);
+ }
}
// ----------------------------------------------------------------------------
void wxComboBox::OnDelete(wxCommandEvent& WXUNUSED(event))
{
- long from, to;
- GetSelection(& from, & to);
- if (from != -1 && to != -1)
- Remove(from, to);
+ RemoveSelection();
}
void wxComboBox::OnSelectAll(wxCommandEvent& WXUNUSED(event))
{
- SetSelection(-1, -1);
+ SelectAll();
}
void wxComboBox::OnUpdateCut(wxUpdateUIEvent& event)
void wxComboBox::OnUpdateSelectAll(wxUpdateUIEvent& event)
{
- event.Enable(GetLastPosition() > 0);
+ 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) );
+}
+
+wxSize wxComboBox::DoGetSizeFromTextSize(int xlen, int ylen) const
+{
+ wxSize tsize( wxChoice::DoGetSizeFromTextSize(xlen, ylen) );
+
+ GtkEntry* entry = GetEntry();
+ if (entry)
+ {
+ // Add the margins we have previously set, but only the horizontal border
+ // as vertical one has been taken account in the previous call.
+ // Also get other GTK+ margins.
+ tsize.IncBy(GTKGetEntryMargins(entry).x, 0);
+ }
+
+ return tsize;
}
#endif // wxUSE_COMBOBOX