X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/2ee3ee1bc8ac2f95029835ebc115fac0a1fbd02a..7b1279006496ba3251a755bcef91301ad107bb78:/src/gtk/choice.cpp diff --git a/src/gtk/choice.cpp b/src/gtk/choice.cpp index 9e209bf95f..103ebf3901 100644 --- a/src/gtk/choice.cpp +++ b/src/gtk/choice.cpp @@ -12,12 +12,13 @@ #pragma implementation "choice.h" #endif -#include "wx/choice.h" +#include "wx/defs.h" #if wxUSE_CHOICE -#include "gdk/gdk.h" -#include "gtk/gtk.h" +#include "wx/choice.h" + +#include "wx/gtk/private.h" //----------------------------------------------------------------------------- // idle system @@ -46,9 +47,17 @@ static void gtk_choice_clicked_callback( GtkWidget *WXUNUSED(widget), wxChoice * if (g_blockEventsOnDrag) return; wxCommandEvent event(wxEVT_COMMAND_CHOICE_SELECTED, choice->GetId() ); - event.SetInt( choice->GetSelection() ); + int n = choice->GetSelection(); + + event.SetInt( n ); event.SetString( choice->GetStringSelection() ); event.SetEventObject(choice); + + if ( choice->HasClientObjectData() ) + event.SetClientObject( choice->GetClientObject(n) ); + else if ( choice->HasClientUntypedData() ) + event.SetClientData( choice->GetClientData(n) ); + choice->GetEventHandler()->ProcessEvent(event); } @@ -82,13 +91,6 @@ bool wxChoice::Create( wxWindow *parent, wxWindowID id, m_widget = gtk_option_menu_new(); - wxSize newSize(size); - if (newSize.x == -1) - newSize.x = 80; - if (newSize.y == -1) - newSize.y = 26; - SetSize( newSize.x, newSize.y ); - if ( style & wxCB_SORT ) { // if our m_strings != NULL, DoAppend() will check for it and insert @@ -100,7 +102,7 @@ bool wxChoice::Create( wxWindow *parent, wxWindowID id, for (int i = 0; i < n; i++) { - AppendHelper(menu, choices[i]); + GtkAppendHelper(menu, choices[i]); } gtk_option_menu_set_menu( GTK_OPTION_MENU(m_widget), menu ); @@ -109,9 +111,12 @@ bool wxChoice::Create( wxWindow *parent, wxWindowID id, PostCreation(); + SetFont( parent->GetFont() ); + + SetBestSize(size); + SetBackgroundColour( parent->GetBackgroundColour() ); SetForegroundColour( parent->GetForegroundColour() ); - SetFont( parent->GetFont() ); Show( TRUE ); @@ -131,35 +136,35 @@ int wxChoice::DoAppend( const wxString &item ) GtkWidget *menu = gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) ); - return AppendHelper(menu, item); + return GtkAppendHelper(menu, item); } -void wxChoice::DoSetClientData( int n, void* clientData ) +void wxChoice::DoSetItemClientData( int n, void* clientData ) { wxCHECK_RET( m_widget != NULL, wxT("invalid choice control") ); wxNode *node = m_clientList.Nth( n ); - wxCHECK_RET( node, wxT("invalid index in wxChoice::DoSetClientData") ); + wxCHECK_RET( node, wxT("invalid index in wxChoice::DoSetItemClientData") ); node->SetData( (wxObject*) clientData ); } -void* wxChoice::DoGetClientData( int n ) const +void* wxChoice::DoGetItemClientData( int n ) const { wxCHECK_MSG( m_widget != NULL, NULL, wxT("invalid choice control") ); wxNode *node = m_clientList.Nth( n ); - wxCHECK_MSG( node, NULL, wxT("invalid index in wxChoice::DoGetClientData") ); + wxCHECK_MSG( node, NULL, wxT("invalid index in wxChoice::DoGetItemClientData") ); return node->Data(); } -void wxChoice::DoSetClientObject( int n, wxClientData* clientData ) +void wxChoice::DoSetItemClientObject( int n, wxClientData* clientData ) { wxCHECK_RET( m_widget != NULL, wxT("invalid choice control") ); wxNode *node = m_clientList.Nth( n ); - wxCHECK_RET( node, wxT("invalid index in wxChoice::DoSetClientObject") ); + wxCHECK_RET( node, wxT("invalid index in wxChoice::DoSetItemClientObject") ); wxClientData *cd = (wxClientData*) node->Data(); delete cd; @@ -167,13 +172,13 @@ void wxChoice::DoSetClientObject( int n, wxClientData* clientData ) node->SetData( (wxObject*) clientData ); } -wxClientData* wxChoice::DoGetClientObject( int n ) const +wxClientData* wxChoice::DoGetItemClientObject( int n ) const { wxCHECK_MSG( m_widget != NULL, (wxClientData*) NULL, wxT("invalid choice control") ); wxNode *node = m_clientList.Nth( n ); wxCHECK_MSG( node, (wxClientData *)NULL, - wxT("invalid index in wxChoice::DoGetClientObject") ); + wxT("invalid index in wxChoice::DoGetItemClientObject") ); return (wxClientData*) node->Data(); } @@ -186,8 +191,18 @@ void wxChoice::Clear() GtkWidget *menu = gtk_menu_new(); gtk_option_menu_set_menu( GTK_OPTION_MENU(m_widget), menu ); - if ( m_clientDataItemsType == ClientData_Object ) - m_clientList.DeleteContents(TRUE); + if ( HasClientObjectData() ) + { + // destroy the data (due to Robert's idea of using wxList + // and not wxList we can't just say + // m_clientList.DeleteContents(TRUE) - this would crash! + wxNode *node = m_clientList.First(); + while ( node ) + { + delete (wxClientData *)node->Data(); + node = node->Next(); + } + } m_clientList.Clear(); if ( m_strings ) @@ -213,16 +228,18 @@ int wxChoice::FindString( const wxString &string ) const { GtkBin *bin = GTK_BIN( child->data ); GtkLabel *label = (GtkLabel *) NULL; - if (bin->child) label = GTK_LABEL(bin->child); - if (!label) label = GTK_LABEL( GTK_BUTTON(m_widget)->child ); + if (bin->child) + label = GTK_LABEL(bin->child); + if (!label) + label = GTK_LABEL( BUTTON_CHILD(m_widget) ); wxASSERT_MSG( label != NULL , wxT("wxChoice: invalid label") ); - if (string == wxString(label->label,*wxConvCurrent)) - return count; + if (string == wxString(label->label,*wxConvCurrent)) + return count; - child = child->next; - count++; + child = child->next; + count++; } return -1; @@ -246,6 +263,13 @@ int wxChoice::GetSelection() const return -1; } +void wxChoice::SetString( int WXUNUSED(n), const wxString& WXUNUSED(string) ) +{ + wxCHECK_RET( m_widget != NULL, wxT("invalid choice") ); + + wxFAIL_MSG(wxT("not implemented")); +} + wxString wxChoice::GetString( int n ) const { wxCHECK_MSG( m_widget != NULL, wxT(""), wxT("invalid choice") ); @@ -259,8 +283,10 @@ wxString wxChoice::GetString( int n ) const if (count == n) { GtkLabel *label = (GtkLabel *) NULL; - if (bin->child) label = GTK_LABEL(bin->child); - if (!label) label = GTK_LABEL( GTK_BUTTON(m_widget)->child ); + if (bin->child) + label = GTK_LABEL(bin->child); + if (!label) + label = GTK_LABEL( BUTTON_CHILD(m_widget) ); wxASSERT_MSG( label != NULL , wxT("wxChoice: invalid label") ); @@ -298,36 +324,6 @@ void wxChoice::SetSelection( int n ) gtk_option_menu_set_history( GTK_OPTION_MENU(m_widget), (gint)tmp ); } -void wxChoice::DisableEvents() -{ -/* - GtkMenuShell *menu_shell = GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) ) ); - GList *child = menu_shell->children; - while (child) - { - gtk_signal_disconnect_by_func( GTK_OBJECT( child->data ), - GTK_SIGNAL_FUNC(gtk_choice_clicked_callback), (gpointer*)this ); - - child = child->next; - } -*/ -} - -void wxChoice::EnableEvents() -{ -/* - GtkMenuShell *menu_shell = GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) ) ); - GList *child = menu_shell->children; - while (child) - { - gtk_signal_connect( GTK_OBJECT( child->data ), "activate", - GTK_SIGNAL_FUNC(gtk_choice_clicked_callback), (gpointer*)this ); - - child = child->next; - } -*/ -} - void wxChoice::ApplyWidgetStyle() { SetWidgetStyle(); @@ -344,8 +340,10 @@ void wxChoice::ApplyWidgetStyle() GtkBin *bin = GTK_BIN( child->data ); GtkWidget *label = (GtkWidget *) NULL; - if (bin->child) label = bin->child; - if (!label) label = GTK_BUTTON(m_widget)->child; + if (bin->child) + label = bin->child; + if (!label) + label = BUTTON_CHILD(m_widget); gtk_widget_set_style( label, m_widgetStyle ); @@ -353,7 +351,7 @@ void wxChoice::ApplyWidgetStyle() } } -size_t wxChoice::AppendHelper(GtkWidget *menu, const wxString& item) +size_t wxChoice::GtkAppendHelper(GtkWidget *menu, const wxString& item) { GtkWidget *menu_item = gtk_menu_item_new_with_label( item.mbc_str() ); @@ -372,8 +370,7 @@ size_t wxChoice::AppendHelper(GtkWidget *menu, const wxString& item) } else { - // can't use Insert() :-( - m_clientList.Append( (wxObject*) NULL ); + m_clientList.Insert( (wxObject*) NULL ); } } else @@ -385,7 +382,7 @@ size_t wxChoice::AppendHelper(GtkWidget *menu, const wxString& item) // don't call wxChoice::GetCount() from here because it doesn't work // if we're called from ctor (and GtkMenuShell is still NULL) - index = m_clientList.GetCount(); + index = m_clientList.GetCount() - 1; } if (GTK_WIDGET_REALIZED(m_widget)) @@ -405,4 +402,47 @@ size_t wxChoice::AppendHelper(GtkWidget *menu, const wxString& item) return index; } -#endif +wxSize wxChoice::DoGetBestSize() const +{ + wxSize ret( wxControl::DoGetBestSize() ); + + // we know better our horizontal extent: it depends on the longest string + // we have + ret.x = 0; + if ( m_widget ) + { + GdkFont *font = m_font.GetInternalFont(); + + wxCoord width; + size_t count = GetCount(); + 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; + } + + // add extra for the choice "=" button + + // VZ: I don't know how to get the right value, it seems to be in + // GtkOptionMenuProps struct from gtkoptionmenu.c but we can't get + // to it - maybe we can use gtk_option_menu_size_request() for this + // somehow? + // + // This default value works only for the default GTK+ theme (i.e. + // no theme at all) (FIXME) + static const int widthChoiceIndicator = 35; + ret.x += widthChoiceIndicator; + } + + // but not less than the minimal width + if ( ret.x < 80 ) + ret.x = 80; + + ret.y = 16 + gdk_char_height(GET_STYLE_FONT( m_widget->style ), 'H'); + + return ret; +} + +#endif // wxUSE_CHOICE +