#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 <gdk/gdk.h>
+#include <gtk/gtk.h>
//-----------------------------------------------------------------------------
// idle system
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);
}
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
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 );
PostCreation();
+ SetFont( parent->GetFont() );
+
+ SetBestSize(size);
+
SetBackgroundColour( parent->GetBackgroundColour() );
SetForegroundColour( parent->GetForegroundColour() );
- SetFont( parent->GetFont() );
Show( TRUE );
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;
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();
}
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<wxObject>
+ // and not wxList<wxClientData> 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 )
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") );
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();
}
}
-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() );
}
else
{
- // can't use Insert() :-(
- m_clientList.Append( (wxObject*) NULL );
+ m_clientList.Insert( (wxObject*) NULL );
}
}
else
// 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))
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( m_widget->style->font, 'H' );
+ return ret;
+}
+
+#endif // wxUSE_CHOICE
+