use GtkComboBox instead of deprecated GtkOptionMenu for wxChoice; this also allows to derive wxComboBox from wxChoice in wxGTK as in wxMSW (ticket #9150)
git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@53641 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
This commit is contained in:
@@ -42,16 +42,7 @@ gtkcombobox_text_changed_callback( GtkWidget *WXUNUSED(widget), wxComboBox *comb
|
||||
static void
|
||||
gtkcombobox_changed_callback( GtkWidget *WXUNUSED(widget), wxComboBox *combo )
|
||||
{
|
||||
if (!combo->m_hasVMT) return;
|
||||
|
||||
if (combo->GetSelection() == -1)
|
||||
return;
|
||||
|
||||
wxCommandEvent event( wxEVT_COMMAND_COMBOBOX_SELECTED, combo->GetId() );
|
||||
event.SetInt( combo->GetSelection() );
|
||||
event.SetString( combo->GetStringSelection() );
|
||||
event.SetEventObject( combo );
|
||||
combo->HandleWindowEvent( event );
|
||||
combo->SendSelectionChangedEvent(wxEVT_COMMAND_COMBOBOX_SELECTED);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -59,9 +50,9 @@ gtkcombobox_changed_callback( GtkWidget *WXUNUSED(widget), wxComboBox *combo )
|
||||
// wxComboBox
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
IMPLEMENT_DYNAMIC_CLASS(wxComboBox,wxControl)
|
||||
IMPLEMENT_DYNAMIC_CLASS(wxComboBox, wxChoice)
|
||||
|
||||
BEGIN_EVENT_TABLE(wxComboBox, wxControl)
|
||||
BEGIN_EVENT_TABLE(wxComboBox, wxChoice)
|
||||
EVT_CHAR(wxComboBox::OnChar)
|
||||
|
||||
EVT_MENU(wxID_CUT, wxComboBox::OnCut)
|
||||
@@ -101,8 +92,6 @@ bool wxComboBox::Create( wxWindow *parent, wxWindowID id, const wxString& value,
|
||||
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 ))
|
||||
@@ -116,11 +105,11 @@ bool wxComboBox::Create( wxWindow *parent, wxWindowID id, const wxString& value,
|
||||
|
||||
m_widget = gtk_combo_box_entry_new_text();
|
||||
|
||||
// Set it up to trigger default item on enter key press
|
||||
// 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));
|
||||
|
||||
|
||||
if (HasFlag(wxBORDER_NONE))
|
||||
{
|
||||
// Doesn't seem to work
|
||||
@@ -139,8 +128,6 @@ bool wxComboBox::Create( wxWindow *parent, wxWindowID id, const wxString& value,
|
||||
|
||||
PostCreation(size);
|
||||
|
||||
ConnectWidget( m_widget );
|
||||
|
||||
gtk_entry_set_text( entry, wxGTK_CONV(value) );
|
||||
|
||||
if (style & wxCB_READONLY)
|
||||
@@ -152,7 +139,6 @@ bool wxComboBox::Create( wxWindow *parent, wxWindowID id, const wxString& value,
|
||||
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;
|
||||
@@ -168,205 +154,6 @@ GtkEditable *wxComboBox::GetEditable() const
|
||||
return GTK_EDITABLE( GTK_BIN(m_widget)->child );
|
||||
}
|
||||
|
||||
wxComboBox::~wxComboBox()
|
||||
{
|
||||
Clear();
|
||||
|
||||
delete m_strings;
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
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") );
|
||||
|
||||
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 );
|
||||
}
|
||||
|
||||
InvalidateBestSize();
|
||||
}
|
||||
|
||||
int wxComboBox::FindString( const wxString &item, bool bCase ) const
|
||||
{
|
||||
wxCHECK_MSG( m_widget != NULL, wxNOT_FOUND, wxT("invalid combobox") );
|
||||
|
||||
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
|
||||
{
|
||||
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 (item.IsSameAs( str, bCase ) )
|
||||
return count;
|
||||
|
||||
count++;
|
||||
}
|
||||
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;
|
||||
|
||||
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))
|
||||
{
|
||||
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;
|
||||
}
|
||||
|
||||
return str;
|
||||
}
|
||||
|
||||
unsigned int wxComboBox::GetCount() const
|
||||
{
|
||||
wxCHECK_MSG( m_widget != NULL, 0, wxT("invalid combobox") );
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
void wxComboBox::SetSelection( int n )
|
||||
{
|
||||
wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") );
|
||||
|
||||
DisableEvents();
|
||||
|
||||
GtkComboBox* combobox = GTK_COMBO_BOX( m_widget );
|
||||
gtk_combo_box_set_active( combobox, n );
|
||||
|
||||
EnableEvents();
|
||||
}
|
||||
|
||||
void wxComboBox::OnChar( wxKeyEvent &event )
|
||||
{
|
||||
switch ( event.GetKeyCode() )
|
||||
@@ -423,34 +210,6 @@ GdkWindow *wxComboBox::GTKGetWindow(wxArrayGdkWindows& windows) const
|
||||
return GetEntry()->text_area;
|
||||
}
|
||||
|
||||
wxSize wxComboBox::DoGetBestSize() const
|
||||
{
|
||||
// strangely, this returns a width of 188 pixels from GTK+ (?)
|
||||
wxSize ret( wxControl::DoGetBestSize() );
|
||||
|
||||
// we know better our horizontal extent: it depends on the longest string
|
||||
// in the combobox
|
||||
if ( m_widget )
|
||||
{
|
||||
ret.x = 60; // start with something "sensible"
|
||||
int width;
|
||||
unsigned int count = GetCount();
|
||||
for ( unsigned int n = 0; n < count; n++ )
|
||||
{
|
||||
GetTextExtent(GetString(n), &width, NULL, NULL, NULL );
|
||||
if ( width + 40 > ret.x ) // 40 for drop down arrow and space around text
|
||||
ret.x = width + 40;
|
||||
}
|
||||
}
|
||||
|
||||
// empty combobox should have some reasonable default size too
|
||||
if ((GetCount() == 0) && (ret.x < 80))
|
||||
ret.x = 80;
|
||||
|
||||
CacheBestSize(ret);
|
||||
return ret;
|
||||
}
|
||||
|
||||
// static
|
||||
wxVisualAttributes
|
||||
wxComboBox::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant))
|
||||
|
Reference in New Issue
Block a user