#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
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 );
int wxChoice::DoAppend( const wxString &item )
{
- wxCHECK_MSG( m_widget != NULL, -1, wxT("invalid choice") );
+ wxCHECK_MSG( m_widget != NULL, -1, wxT("invalid choice control") );
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 combobox") );
+ 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 combobox") );
+ 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 combobox") );
+ 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 combobox") );
+ 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)
+ 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)
+ while ( node )
{
- wxClientData *cd = (wxClientData*)node->Data();
- if (cd) delete cd;
+ delete (wxClientData *)node->Data();
node = node->Next();
}
}
m_clientList.Clear();
+
+ if ( m_strings )
+ m_strings->Clear();
}
-void wxChoice::Delete( int WXUNUSED(n) )
+void wxChoice::Delete( int n )
{
- wxFAIL_MSG( wxT("wxChoice:Delete not implemented") );
+ 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
+ int i,
+ count = GetCount();
+
+ wxCHECK_RET( n >= 0 && n < count, _T("invalid index in wxChoice::Delete") );
+
+ wxArrayString items;
+ items.Alloc(count);
+ for ( i = 0; i < count; i++ )
+ {
+ if ( i != n )
+ items.Add(GetString(i));
+ }
+
+ Clear();
+
+ for ( i = 0; i < count - 1; i++ )
+ {
+ Append(items[i]);
+ }
}
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;
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") );
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") );
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();
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 );
}
}
-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(GET_STYLE_FONT( m_widget->style ), 'H');
+
+ return ret;
+}
+
+#endif // wxUSE_CHOICE
+