X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/e90c1d2a19361551eb07778280f22be3e759cf64..b81abd0d1d0fcde8db54f8c5e736b2b9daaf2ea0:/src/gtk/combobox.cpp diff --git a/src/gtk/combobox.cpp b/src/gtk/combobox.cpp index 74ca53348b..023caa971e 100644 --- a/src/gtk/combobox.cpp +++ b/src/gtk/combobox.cpp @@ -18,8 +18,8 @@ #include "wx/settings.h" #include "wx/intl.h" -#include "gdk/gdk.h" -#include "gtk/gtk.h" +#include +#include //----------------------------------------------------------------------------- // idle system @@ -104,28 +104,25 @@ bool wxComboBox::Create( wxWindow *parent, wxWindowID id, const wxString& value, if (!PreCreation( parent, pos, size ) || !CreateBase( parent, id, pos, size, style, validator, name )) { - wxFAIL_MSG( T("wxComboBox creation failed") ); - return FALSE; + wxFAIL_MSG( wxT("wxComboBox creation failed") ); + return FALSE; } m_widget = gtk_combo_new(); // make it more useable - gtk_combo_set_use_arrows_always(GTK_COMBO(m_widget), TRUE); + gtk_combo_set_use_arrows_always( GTK_COMBO(m_widget), TRUE ); + + // and case-sensitive + gtk_combo_set_case_sensitive( GTK_COMBO(m_widget), TRUE ); - wxSize newSize = size; - if (newSize.x == -1) - newSize.x = 100; - if (newSize.y == -1) - newSize.y = 26; - SetSize( newSize.x, newSize.y ); GtkWidget *list = GTK_COMBO(m_widget)->list; for (int i = 0; i < n; i++) { /* don't send first event, which GTK sends aways when - inserting the first item */ + inserting the first item */ m_alreadySent = TRUE; GtkWidget *list_item = gtk_list_item_new_with_label( choices[i].mbc_str() ); @@ -155,9 +152,19 @@ bool wxComboBox::Create( wxWindow *parent, wxWindowID id, const wxString& value, gtk_signal_connect( GTK_OBJECT(GTK_COMBO(m_widget)->entry), "changed", GTK_SIGNAL_FUNC(gtk_text_changed_callback), (gpointer)this); + wxSize size_best( DoGetBestSize() ); + wxSize new_size( size ); + if (new_size.x == -1) + new_size.x = size_best.x; + if (new_size.y == -1) + new_size.y = size_best.y; + if (new_size.y > size_best.y) + new_size.y = size_best.y; + if ((new_size.x != size.x) || (new_size.y != size.y)) + SetSize( new_size.x, new_size.y ); + SetBackgroundColour( wxSystemSettings::GetSystemColour( wxSYS_COLOUR_WINDOW ) ); SetForegroundColour( parent->GetForegroundColour() ); - SetFont( parent->GetFont() ); Show( TRUE ); @@ -180,7 +187,7 @@ wxComboBox::~wxComboBox() void wxComboBox::AppendCommon( const wxString &item ) { - wxCHECK_RET( m_widget != NULL, T("invalid combobox") ); + wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") ); GtkWidget *list = GTK_COMBO(m_widget)->list; @@ -228,7 +235,7 @@ void wxComboBox::Append( const wxString &item, wxClientData *clientData ) void wxComboBox::SetClientData( int n, void* clientData ) { - wxCHECK_RET( m_widget != NULL, T("invalid combobox") ); + wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") ); wxNode *node = m_clientDataList.Nth( n ); if (!node) return; @@ -238,7 +245,7 @@ void wxComboBox::SetClientData( int n, void* clientData ) void* wxComboBox::GetClientData( int n ) { - wxCHECK_MSG( m_widget != NULL, NULL, T("invalid combobox") ); + wxCHECK_MSG( m_widget != NULL, NULL, wxT("invalid combobox") ); wxNode *node = m_clientDataList.Nth( n ); if (!node) return NULL; @@ -248,7 +255,7 @@ void* wxComboBox::GetClientData( int n ) void wxComboBox::SetClientObject( int n, wxClientData* clientData ) { - wxCHECK_RET( m_widget != NULL, T("invalid combobox") ); + wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") ); wxNode *node = m_clientObjectList.Nth( n ); if (!node) return; @@ -261,7 +268,7 @@ void wxComboBox::SetClientObject( int n, wxClientData* clientData ) wxClientData* wxComboBox::GetClientObject( int n ) { - wxCHECK_MSG( m_widget != NULL, (wxClientData*)NULL, T("invalid combobox") ); + wxCHECK_MSG( m_widget != NULL, (wxClientData*)NULL, wxT("invalid combobox") ); wxNode *node = m_clientDataList.Nth( n ); if (!node) return (wxClientData*) NULL; @@ -271,7 +278,7 @@ wxClientData* wxComboBox::GetClientObject( int n ) void wxComboBox::Clear() { - wxCHECK_RET( m_widget != NULL, T("invalid combobox") ); + wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") ); GtkWidget *list = GTK_COMBO(m_widget)->list; gtk_list_clear_items( GTK_LIST(list), 0, Number() ); @@ -290,7 +297,7 @@ void wxComboBox::Clear() void wxComboBox::Delete( int n ) { - wxCHECK_RET( m_widget != NULL, T("invalid combobox") ); + wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") ); GtkList *listbox = GTK_LIST( GTK_COMBO(m_widget)->list ); @@ -298,7 +305,7 @@ void wxComboBox::Delete( int n ) if (!child) { - wxFAIL_MSG(T("wrong index")); + wxFAIL_MSG(wxT("wrong index")); return; } @@ -323,7 +330,7 @@ void wxComboBox::Delete( int n ) int wxComboBox::FindString( const wxString &item ) { - wxCHECK_MSG( m_widget != NULL, -1, T("invalid combobox") ); + wxCHECK_MSG( m_widget != NULL, -1, wxT("invalid combobox") ); GtkWidget *list = GTK_COMBO(m_widget)->list; @@ -344,7 +351,7 @@ int wxComboBox::FindString( const wxString &item ) int wxComboBox::GetSelection() const { - wxCHECK_MSG( m_widget != NULL, -1, T("invalid combobox") ); + wxCHECK_MSG( m_widget != NULL, -1, wxT("invalid combobox") ); GtkWidget *list = GTK_COMBO(m_widget)->list; @@ -366,7 +373,7 @@ int wxComboBox::GetSelection() const wxString wxComboBox::GetString( int n ) const { - wxCHECK_MSG( m_widget != NULL, T(""), T("invalid combobox") ); + wxCHECK_MSG( m_widget != NULL, wxT(""), wxT("invalid combobox") ); GtkWidget *list = GTK_COMBO(m_widget)->list; @@ -380,7 +387,7 @@ wxString wxComboBox::GetString( int n ) const } else { - wxFAIL_MSG( T("wxComboBox: wrong index") ); + wxFAIL_MSG( wxT("wxComboBox: wrong index") ); } return str; @@ -388,7 +395,7 @@ wxString wxComboBox::GetString( int n ) const wxString wxComboBox::GetStringSelection() const { - wxCHECK_MSG( m_widget != NULL, T(""), T("invalid combobox") ); + wxCHECK_MSG( m_widget != NULL, wxT(""), wxT("invalid combobox") ); GtkWidget *list = GTK_COMBO(m_widget)->list; @@ -400,14 +407,14 @@ wxString wxComboBox::GetStringSelection() const return tmp; } - wxFAIL_MSG( T("wxComboBox: no selection") ); + wxFAIL_MSG( wxT("wxComboBox: no selection") ); - return T(""); + return wxT(""); } int wxComboBox::Number() const { - wxCHECK_MSG( m_widget != NULL, 0, T("invalid combobox") ); + wxCHECK_MSG( m_widget != NULL, 0, wxT("invalid combobox") ); GtkWidget *list = GTK_COMBO(m_widget)->list; @@ -419,7 +426,7 @@ int wxComboBox::Number() const void wxComboBox::SetSelection( int n ) { - wxCHECK_RET( m_widget != NULL, T("invalid combobox") ); + wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") ); DisableEvents(); @@ -431,7 +438,7 @@ void wxComboBox::SetSelection( int n ) void wxComboBox::SetStringSelection( const wxString &string ) { - wxCHECK_RET( m_widget != NULL, T("invalid combobox") ); + wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") ); int res = FindString( string ); if (res == -1) return; @@ -447,20 +454,20 @@ wxString wxComboBox::GetValue() const void wxComboBox::SetValue( const wxString& value ) { - wxCHECK_RET( m_widget != NULL, T("invalid combobox") ); + wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") ); GtkWidget *entry = GTK_COMBO(m_widget)->entry; - wxString tmp = T(""); + wxString tmp = wxT(""); if (!value.IsNull()) tmp = value; gtk_entry_set_text( GTK_ENTRY(entry), tmp.mbc_str() ); } void wxComboBox::Copy() { - wxCHECK_RET( m_widget != NULL, T("invalid combobox") ); + wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") ); GtkWidget *entry = GTK_COMBO(m_widget)->entry; -#if (GTK_MINOR_VERSION > 0) +#if defined(__WXGTK13__) || (GTK_MINOR_VERSION > 0) gtk_editable_copy_clipboard( GTK_EDITABLE(entry) ); #else gtk_editable_copy_clipboard( GTK_EDITABLE(entry), 0 ); @@ -469,10 +476,10 @@ void wxComboBox::Copy() void wxComboBox::Cut() { - wxCHECK_RET( m_widget != NULL, T("invalid combobox") ); + wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") ); GtkWidget *entry = GTK_COMBO(m_widget)->entry; -#if (GTK_MINOR_VERSION > 0) +#if defined(__WXGTK13__) || (GTK_MINOR_VERSION > 0) gtk_editable_cut_clipboard( GTK_EDITABLE(entry) ); #else gtk_editable_cut_clipboard( GTK_EDITABLE(entry), 0 ); @@ -481,10 +488,10 @@ void wxComboBox::Cut() void wxComboBox::Paste() { - wxCHECK_RET( m_widget != NULL, T("invalid combobox") ); + wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") ); GtkWidget *entry = GTK_COMBO(m_widget)->entry; -#if (GTK_MINOR_VERSION > 0) +#if defined(__WXGTK13__) || (GTK_MINOR_VERSION > 0) gtk_editable_paste_clipboard( GTK_EDITABLE(entry) ); #else gtk_editable_paste_clipboard( GTK_EDITABLE(entry), 0 ); @@ -493,7 +500,7 @@ void wxComboBox::Paste() void wxComboBox::SetInsertionPoint( long pos ) { - wxCHECK_RET( m_widget != NULL, T("invalid combobox") ); + wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") ); GtkWidget *entry = GTK_COMBO(m_widget)->entry; gtk_entry_set_position( GTK_ENTRY(entry), (int)pos ); @@ -501,7 +508,7 @@ void wxComboBox::SetInsertionPoint( long pos ) void wxComboBox::SetInsertionPointEnd() { - wxCHECK_RET( m_widget != NULL, T("invalid combobox") ); + wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") ); SetInsertionPoint( -1 ); } @@ -521,7 +528,7 @@ long wxComboBox::GetLastPosition() const void wxComboBox::Replace( long from, long to, const wxString& value ) { - wxCHECK_RET( m_widget != NULL, T("invalid combobox") ); + wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") ); // FIXME: not quite sure how to do this method right in multibyte mode GtkWidget *entry = GTK_COMBO(m_widget)->entry; @@ -533,7 +540,7 @@ void wxComboBox::Replace( long from, long to, const wxString& value ) void wxComboBox::Remove(long from, long to) { - wxCHECK_RET( m_widget != NULL, T("invalid combobox") ); + wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") ); GtkWidget *entry = GTK_COMBO(m_widget)->entry; gtk_editable_delete_text( GTK_EDITABLE(entry), (gint)from, (gint)to ); @@ -574,6 +581,7 @@ void wxComboBox::OnChar( wxKeyEvent &event ) if ( FindString(value) == wxNOT_FOUND ) { Append(value); + SetStringSelection(value); // and generate the selected event for it wxCommandEvent event( wxEVT_COMMAND_COMBOBOX_SELECTED, GetId() ); @@ -619,13 +627,13 @@ void wxComboBox::OnSize( wxSizeEvent &event ) { event.Skip(); - return; - +#if 0 int w = 21; gtk_widget_set_usize( GTK_COMBO(m_widget)->entry, m_width-w-1, m_height ); gtk_widget_set_uposition( GTK_COMBO(m_widget)->button, m_x+m_width-w, m_y ); gtk_widget_set_usize( GTK_COMBO(m_widget)->button, w, m_height ); +#endif // 0 } void wxComboBox::ApplyWidgetStyle() @@ -660,4 +668,31 @@ bool wxComboBox::IsOwnGtkWindow( GdkWindow *window ) (window == GTK_COMBO(m_widget)->button->window ) ); } +wxSize wxComboBox::DoGetBestSize() const +{ + wxSize ret( wxControl::DoGetBestSize() ); + + // we know better our horizontal extent: it depends on the longest string + // in the combobox + ret.x = 0; + if ( m_widget ) + { + GdkFont *font = m_font.GetInternalFont(); + + wxCoord width; + size_t count = Number(); + for ( size_t n = 0; n < count; n++ ) + { + width = (wxCoord)gdk_string_width(font, GetString(n).mbc_str()); + if ( width > ret.x ) + ret.x = width; + } + } + + // empty combobox should have some reasonable default size too + if ( ret.x < 100 ) + ret.x = 100; + return ret; +} + #endif