X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/b1d4dd7add411ebff1ce4a8d1d992e0e43ee26b1..4b1e72e266ec6993d665c65d5f5e59e7981afc17:/src/gtk/choice.cpp diff --git a/src/gtk/choice.cpp b/src/gtk/choice.cpp index c9251f3982..651bf9d161 100644 --- a/src/gtk/choice.cpp +++ b/src/gtk/choice.cpp @@ -4,11 +4,11 @@ // Author: Robert Roebling // Id: $Id$ // Copyright: (c) 1998 Robert Roebling -// Licence: wxWindows licence +// Licence: wxWidgets licence ///////////////////////////////////////////////////////////////////////////// -#ifdef __GNUG__ +#if defined(__GNUG__) && !defined(NO_GCC_PRAGMA) #pragma implementation "choice.h" #endif @@ -17,6 +17,7 @@ #if wxUSE_CHOICE #include "wx/choice.h" +#include "wx/arrstr.h" #include "wx/gtk/private.h" @@ -72,6 +73,18 @@ wxChoice::wxChoice() m_strings = (wxSortedArrayString *)NULL; } +bool wxChoice::Create( wxWindow *parent, wxWindowID id, + const wxPoint &pos, const wxSize &size, + const wxArrayString& choices, + long style, const wxValidator& validator, + const wxString &name ) +{ + wxCArrayString chs(choices); + + return Create( parent, id, pos, size, chs.GetCount(), chs.GetStrings(), + style, validator, name ); +} + bool wxChoice::Create( wxWindow *parent, wxWindowID id, const wxPoint &pos, const wxSize &size, int n, const wxString choices[], @@ -102,23 +115,15 @@ bool wxChoice::Create( wxWindow *parent, wxWindowID id, for (int i = 0; i < n; i++) { - GtkAppendHelper(menu, choices[i]); + GtkAddHelper(menu, i, choices[i]); } gtk_option_menu_set_menu( GTK_OPTION_MENU(m_widget), menu ); m_parent->DoAddChild( this ); - PostCreation(); - - SetFont( parent->GetFont() ); - - SetBestSize(size); - - SetBackgroundColour( parent->GetBackgroundColour() ); - SetForegroundColour( parent->GetForegroundColour() ); - - Show( TRUE ); + PostCreation(size); + SetBestSize(size); // need this too because this is a wxControlWithItems return TRUE; } @@ -136,14 +141,27 @@ int wxChoice::DoAppend( const wxString &item ) GtkWidget *menu = gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) ); - return GtkAppendHelper(menu, item); + return GtkAddHelper(menu, GetCount(), item); +} + +int wxChoice::DoInsert( const wxString &item, int pos ) +{ + wxCHECK_MSG( m_widget != NULL, -1, wxT("invalid choice control") ); + wxCHECK_MSG( (pos>=0) && (pos<=GetCount()), -1, wxT("invalid index")); + + if (pos == GetCount()) + return DoAppend(item); + + GtkWidget *menu = gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) ); + + return GtkAddHelper(menu, pos, item); } void wxChoice::DoSetItemClientData( int n, void* clientData ) { wxCHECK_RET( m_widget != NULL, wxT("invalid choice control") ); - wxNode *node = m_clientList.Item( n ); + wxList::compatibility_iterator node = m_clientList.Item( n ); wxCHECK_RET( node, wxT("invalid index in wxChoice::DoSetItemClientData") ); node->SetData( (wxObject*) clientData ); @@ -153,7 +171,7 @@ void* wxChoice::DoGetItemClientData( int n ) const { wxCHECK_MSG( m_widget != NULL, NULL, wxT("invalid choice control") ); - wxNode *node = m_clientList.Item( n ); + wxList::compatibility_iterator node = m_clientList.Item( n ); wxCHECK_MSG( node, NULL, wxT("invalid index in wxChoice::DoGetItemClientData") ); return node->GetData(); @@ -163,7 +181,7 @@ void wxChoice::DoSetItemClientObject( int n, wxClientData* clientData ) { wxCHECK_RET( m_widget != NULL, wxT("invalid choice control") ); - wxNode *node = m_clientList.Item( n ); + wxList::compatibility_iterator node = m_clientList.Item( n ); wxCHECK_RET( node, wxT("invalid index in wxChoice::DoSetItemClientObject") ); // wxItemContainer already deletes data for us @@ -175,7 +193,7 @@ wxClientData* wxChoice::DoGetItemClientObject( int n ) const { wxCHECK_MSG( m_widget != NULL, (wxClientData*) NULL, wxT("invalid choice control") ); - wxNode *node = m_clientList.Item( n ); + wxList::compatibility_iterator node = m_clientList.Item( n ); wxCHECK_MSG( node, (wxClientData *)NULL, wxT("invalid index in wxChoice::DoGetItemClientObject") ); @@ -195,7 +213,7 @@ void wxChoice::Clear() // 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.GetFirst(); + wxList::compatibility_iterator node = m_clientList.GetFirst(); while ( node ) { delete (wxClientData *)node->GetData(); @@ -213,18 +231,49 @@ void wxChoice::Delete( int n ) wxCHECK_RET( m_widget != NULL, wxT("invalid choice") ); // VZ: apparently GTK+ doesn't have a built-in function to do it (not even - // in 2.0), hence this dump implementation - still better than nothing + // in 2.0), hence this dumb implementation -- still better than nothing int i, count = GetCount(); wxCHECK_RET( n >= 0 && n < count, _T("invalid index in wxChoice::Delete") ); + const bool hasClientData = m_clientDataItemsType != wxClientData_None; + const bool hasObjectData = m_clientDataItemsType == wxClientData_Object; + + wxList::compatibility_iterator node = m_clientList.GetFirst(); + wxArrayString items; + wxArrayPtrVoid itemsData; items.Alloc(count); for ( i = 0; i < count; i++ ) { if ( i != n ) + { items.Add(GetString(i)); + if ( hasClientData ) + { + // also save the client data + itemsData.Add(node->GetData()); + } + } + else // need to delete the client object too + { + if ( hasObjectData ) + { + delete (wxClientData *)node->GetData(); + } + } + + if ( hasClientData ) + { + node = node->GetNext(); + } + } + + if ( hasObjectData ) + { + // prevent Clear() from destroying all client data + m_clientDataItemsType = wxClientData_None; } Clear(); @@ -232,6 +281,11 @@ void wxChoice::Delete( int n ) for ( i = 0; i < count - 1; i++ ) { Append(items[i]); + + if ( hasObjectData ) + SetClientObject(i, (wxClientData *)itemsData[i]); + else if ( hasClientData ) + SetClientData(i, itemsData[i]); } } @@ -255,7 +309,7 @@ int wxChoice::FindString( const wxString &string ) const label = GTK_LABEL( BUTTON_CHILD(m_widget) ); wxASSERT_MSG( label != NULL , wxT("wxChoice: invalid label") ); - + #ifdef __WXGTK20__ wxString tmp( wxGTK_CONV_BACK( gtk_label_get_text( label) ) ); #else @@ -274,15 +328,15 @@ int wxChoice::FindString( const wxString &string ) const int wxChoice::GetSelection() const { wxCHECK_MSG( m_widget != NULL, -1, wxT("invalid choice") ); - + #ifdef __WXGTK20__ return gtk_option_menu_get_history( GTK_OPTION_MENU(m_widget) ); - + #else GtkMenuShell *menu_shell = GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) ) ); int count = 0; - + GList *child = menu_shell->children; while (child) { @@ -296,11 +350,33 @@ int wxChoice::GetSelection() const #endif } -void wxChoice::SetString( int WXUNUSED(n), const wxString& WXUNUSED(string) ) +void wxChoice::SetString( int n, const wxString& str ) { wxCHECK_RET( m_widget != NULL, wxT("invalid choice") ); - wxFAIL_MSG(wxT("not implemented")); + GtkMenuShell *menu_shell = GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) ) ); + int count = 0; + GList *child = menu_shell->children; + while (child) + { + GtkBin *bin = GTK_BIN( child->data ); + if (count == n) + { + GtkLabel *label = (GtkLabel *) NULL; + 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") ); + + gtk_label_set_text( label, wxGTK_CONV( str ) ); + + return; + } + child = child->next; + count++; + } } wxString wxChoice::GetString( int n ) const @@ -388,8 +464,10 @@ void wxChoice::ApplyWidgetStyle() } } -size_t wxChoice::GtkAppendHelper(GtkWidget *menu, const wxString& item) +int wxChoice::GtkAddHelper(GtkWidget *menu, int pos, const wxString& item) { + wxCHECK_MSG((pos>=0) && (pos<=(int)m_clientList.GetCount()), -1, wxT("invalid index")); + GtkWidget *menu_item = gtk_menu_item_new_with_label( wxGTK_CONV( item ) ); size_t index; @@ -412,14 +490,22 @@ size_t wxChoice::GtkAppendHelper(GtkWidget *menu, const wxString& item) } else { + // don't call wxChoice::GetCount() from here because it doesn't work + // if we're called from ctor (and GtkMenuShell is still NULL) + // normal control, just append + if (pos == (int)m_clientList.GetCount()) + { gtk_menu_append( GTK_MENU(menu), menu_item ); - m_clientList.Append( (wxObject*) NULL ); - - // 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() - 1; + } + else + { + gtk_menu_insert( GTK_MENU(menu), menu_item, pos ); + m_clientList.Insert( pos, (wxObject*) NULL ); + index = pos; + } } if (GTK_WIDGET_REALIZED(m_widget)) @@ -452,7 +538,7 @@ wxSize wxChoice::DoGetBestSize() const size_t count = GetCount(); for ( size_t n = 0; n < count; n++ ) { - GetTextExtent( GetString(n), &width, NULL, NULL, NULL, &m_font ); + GetTextExtent( GetString(n), &width, NULL, NULL, NULL ); if ( width > ret.x ) ret.x = width; } @@ -474,10 +560,31 @@ wxSize wxChoice::DoGetBestSize() const if ( ret.x < 80 ) ret.x = 80; - ret.y = 16 + gdk_char_height(GET_STYLE_FONT( m_widget->style ), 'H' ); + // If this request_size is called with no entries then + // the returned height is wrong. Give it a reasonable + // default value. + if (ret.y <= 18) + ret.y = 8 + GetCharHeight(); return ret; } +bool wxChoice::IsOwnGtkWindow( GdkWindow *window ) +{ +#ifdef __WXGTK20__ + return GTK_BUTTON(m_widget)->event_window; +#else + return (window == m_widget->window); +#endif +} + +// static +wxVisualAttributes +wxChoice::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant)) +{ + return GetDefaultAttributesFromGTKWidget(gtk_option_menu_new); +} + + #endif // wxUSE_CHOICE