1998-05-20 14:01:55 +00:00
|
|
|
/////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Name: choice.cpp
|
|
|
|
// Purpose:
|
|
|
|
// Author: Robert Roebling
|
|
|
|
// Created: 01/02/97
|
|
|
|
// Id:
|
|
|
|
// Copyright: (c) 1998 Robert Roebling, Julian Smart and Markus Holzem
|
|
|
|
// Licence: wxWindows licence
|
|
|
|
/////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
|
|
|
|
#ifdef __GNUG__
|
|
|
|
#pragma implementation "choice.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "wx/choice.h"
|
|
|
|
|
1998-08-02 20:38:05 +00:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// data
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
extern bool g_blockEventsOnDrag;
|
|
|
|
|
1998-05-20 14:01:55 +00:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// wxChoice
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
1998-08-02 20:38:05 +00:00
|
|
|
static void gtk_choice_clicked_callback( GtkWidget *WXUNUSED(widget), wxChoice *choice )
|
1998-05-20 14:01:55 +00:00
|
|
|
{
|
1998-08-02 20:38:05 +00:00
|
|
|
if (!choice->HasVMT()) return;
|
|
|
|
if (g_blockEventsOnDrag) return;
|
|
|
|
|
1998-07-31 20:04:04 +00:00
|
|
|
wxCommandEvent event(wxEVT_COMMAND_CHOICE_SELECTED, choice->GetId() );
|
1998-05-20 14:01:55 +00:00
|
|
|
event.SetInt( choice->GetSelection() );
|
|
|
|
wxString tmp( choice->GetStringSelection() );
|
|
|
|
event.SetString( WXSTRINGCAST(tmp) );
|
|
|
|
event.SetEventObject(choice);
|
1998-07-31 20:04:04 +00:00
|
|
|
choice->GetEventHandler()->ProcessEvent(event);
|
1998-08-14 10:07:38 +00:00
|
|
|
}
|
1998-05-20 14:01:55 +00:00
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
1998-05-30 17:11:51 +00:00
|
|
|
IMPLEMENT_DYNAMIC_CLASS(wxChoice,wxControl)
|
1998-05-20 14:01:55 +00:00
|
|
|
|
|
|
|
wxChoice::wxChoice(void)
|
|
|
|
{
|
1998-08-14 10:07:38 +00:00
|
|
|
}
|
1998-05-20 14:01:55 +00:00
|
|
|
|
1998-07-04 15:17:59 +00:00
|
|
|
bool wxChoice::Create( wxWindow *parent, wxWindowID id,
|
|
|
|
const wxPoint &pos, const wxSize &size,
|
|
|
|
int n, const wxString choices[],
|
1998-08-14 10:07:38 +00:00
|
|
|
long style, const wxValidator& validator, const wxString &name )
|
1998-05-20 14:01:55 +00:00
|
|
|
{
|
|
|
|
m_needParent = TRUE;
|
|
|
|
|
|
|
|
PreCreation( parent, id, pos, size, style, name );
|
|
|
|
|
1998-08-14 10:07:38 +00:00
|
|
|
SetValidator( validator );
|
|
|
|
|
1998-05-20 14:01:55 +00:00
|
|
|
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 );
|
|
|
|
|
|
|
|
GtkWidget *menu;
|
|
|
|
menu = gtk_menu_new();
|
|
|
|
|
|
|
|
for (int i = 0; i < n; i++)
|
|
|
|
{
|
|
|
|
GtkWidget *item;
|
|
|
|
item = gtk_menu_item_new_with_label( choices[i] );
|
|
|
|
gtk_signal_connect( GTK_OBJECT( item ), "activate",
|
|
|
|
GTK_SIGNAL_FUNC(gtk_choice_clicked_callback), (gpointer*)this );
|
|
|
|
gtk_menu_append( GTK_MENU(menu), item );
|
|
|
|
gtk_widget_show( item );
|
1998-08-14 10:07:38 +00:00
|
|
|
}
|
1998-05-20 14:01:55 +00:00
|
|
|
gtk_option_menu_set_menu( GTK_OPTION_MENU(m_widget), menu );
|
|
|
|
|
|
|
|
PostCreation();
|
|
|
|
|
|
|
|
Show( TRUE );
|
|
|
|
|
|
|
|
return TRUE;
|
1998-08-14 10:07:38 +00:00
|
|
|
}
|
1998-05-20 14:01:55 +00:00
|
|
|
|
|
|
|
void wxChoice::Append( const wxString &item )
|
|
|
|
{
|
|
|
|
GtkWidget *menu = gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) );
|
|
|
|
GtkWidget *menu_item;
|
|
|
|
menu_item = gtk_menu_item_new_with_label( item );
|
|
|
|
gtk_signal_connect( GTK_OBJECT( menu_item ), "activate",
|
|
|
|
GTK_SIGNAL_FUNC(gtk_choice_clicked_callback), (gpointer*)this );
|
|
|
|
gtk_menu_append( GTK_MENU(menu), menu_item );
|
|
|
|
gtk_widget_show( menu_item );
|
1998-08-14 10:07:38 +00:00
|
|
|
}
|
1998-05-20 14:01:55 +00:00
|
|
|
|
|
|
|
void wxChoice::Clear(void)
|
|
|
|
{
|
|
|
|
gtk_option_menu_remove_menu( GTK_OPTION_MENU(m_widget) );
|
|
|
|
GtkWidget *menu = gtk_menu_new();
|
|
|
|
gtk_option_menu_set_menu( GTK_OPTION_MENU(m_widget), menu );
|
1998-08-14 10:07:38 +00:00
|
|
|
}
|
1998-05-20 14:01:55 +00:00
|
|
|
|
1998-09-01 15:41:45 +00:00
|
|
|
void wxChoice::Delete( int WXUNUSED(n) )
|
|
|
|
{
|
|
|
|
wxFAIL_MSG( "wxChoice:Delete not implemented" );
|
|
|
|
}
|
|
|
|
|
1998-05-20 14:01:55 +00:00
|
|
|
int wxChoice::FindString( const wxString &string ) const
|
|
|
|
{
|
1998-07-31 20:04:04 +00:00
|
|
|
// If you read this code once and you think you understand
|
|
|
|
// it, then you are very wrong. Robert Roebling.
|
1998-05-20 14:01:55 +00:00
|
|
|
|
|
|
|
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 );
|
1998-08-23 03:22:56 +00:00
|
|
|
GtkLabel *label = (GtkLabel *) NULL;
|
1998-07-31 20:04:04 +00:00
|
|
|
if (bin->child) label = GTK_LABEL(bin->child);
|
1998-08-08 13:11:54 +00:00
|
|
|
|
|
|
|
wxASSERT_MSG( label != NULL , "wxChoice: invalid label" );
|
|
|
|
|
1998-05-20 14:01:55 +00:00
|
|
|
if (!label) label = GTK_LABEL( GTK_BUTTON(m_widget)->child );
|
|
|
|
if (string == label->label) return count;
|
|
|
|
child = child->next;
|
|
|
|
count++;
|
1998-08-14 10:07:38 +00:00
|
|
|
}
|
1998-08-08 13:11:54 +00:00
|
|
|
|
|
|
|
wxFAIL_MSG( "wxChoice: string not found" );
|
|
|
|
|
1998-05-20 14:01:55 +00:00
|
|
|
return -1;
|
1998-08-14 10:07:38 +00:00
|
|
|
}
|
1998-05-20 14:01:55 +00:00
|
|
|
|
|
|
|
int wxChoice::GetColumns(void) const
|
|
|
|
{
|
|
|
|
return 1;
|
1998-08-14 10:07:38 +00:00
|
|
|
}
|
1998-05-20 14:01:55 +00:00
|
|
|
|
|
|
|
int wxChoice::GetSelection(void)
|
|
|
|
{
|
|
|
|
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 (!bin->child) return count;
|
|
|
|
child = child->next;
|
|
|
|
count++;
|
1998-08-14 10:07:38 +00:00
|
|
|
}
|
1998-08-08 13:11:54 +00:00
|
|
|
|
|
|
|
wxFAIL_MSG( "wxChoice: no selection" );
|
|
|
|
|
1998-05-20 14:01:55 +00:00
|
|
|
return -1;
|
1998-08-14 10:07:38 +00:00
|
|
|
}
|
1998-05-20 14:01:55 +00:00
|
|
|
|
1998-07-04 15:17:59 +00:00
|
|
|
wxString wxChoice::GetString( int n ) const
|
1998-05-20 14:01:55 +00:00
|
|
|
{
|
|
|
|
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)
|
|
|
|
{
|
1998-08-23 03:22:56 +00:00
|
|
|
GtkLabel *label = (GtkLabel *) NULL;
|
1998-07-31 20:04:04 +00:00
|
|
|
if (bin->child) label = GTK_LABEL(bin->child);
|
1998-08-08 13:11:54 +00:00
|
|
|
|
|
|
|
wxASSERT_MSG( label != NULL , "wxChoice: invalid label" );
|
|
|
|
|
1998-05-20 14:01:55 +00:00
|
|
|
if (!label) label = GTK_LABEL( GTK_BUTTON(m_widget)->child );
|
|
|
|
return label->label;
|
1998-08-14 10:07:38 +00:00
|
|
|
}
|
1998-05-20 14:01:55 +00:00
|
|
|
child = child->next;
|
|
|
|
count++;
|
1998-08-14 10:07:38 +00:00
|
|
|
}
|
1998-08-08 13:11:54 +00:00
|
|
|
|
|
|
|
wxFAIL_MSG( "wxChoice: string not found" );
|
|
|
|
|
1998-05-20 14:01:55 +00:00
|
|
|
return "";
|
1998-08-14 10:07:38 +00:00
|
|
|
}
|
1998-05-20 14:01:55 +00:00
|
|
|
|
|
|
|
wxString wxChoice::GetStringSelection(void) const
|
|
|
|
{
|
|
|
|
GtkLabel *label = GTK_LABEL( GTK_BUTTON(m_widget)->child );
|
1998-08-08 13:11:54 +00:00
|
|
|
|
|
|
|
wxASSERT_MSG( label != NULL , "wxChoice: invalid label" );
|
|
|
|
|
1998-05-20 14:01:55 +00:00
|
|
|
return label->label;
|
1998-08-14 10:07:38 +00:00
|
|
|
}
|
1998-05-20 14:01:55 +00:00
|
|
|
|
|
|
|
int wxChoice::Number(void) const
|
|
|
|
{
|
1998-08-04 17:49:26 +00:00
|
|
|
GtkMenuShell *menu_shell = GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) ) );
|
1998-05-20 14:01:55 +00:00
|
|
|
int count = 0;
|
1998-08-04 17:49:26 +00:00
|
|
|
GList *child = menu_shell->children;
|
1998-05-20 14:01:55 +00:00
|
|
|
while (child)
|
|
|
|
{
|
|
|
|
count++;
|
|
|
|
child = child->next;
|
1998-08-14 10:07:38 +00:00
|
|
|
}
|
1998-05-20 14:01:55 +00:00
|
|
|
return count;
|
1998-08-14 10:07:38 +00:00
|
|
|
}
|
1998-05-20 14:01:55 +00:00
|
|
|
|
1998-07-04 15:17:59 +00:00
|
|
|
void wxChoice::SetColumns( int WXUNUSED(n) )
|
1998-05-20 14:01:55 +00:00
|
|
|
{
|
1998-08-14 10:07:38 +00:00
|
|
|
}
|
1998-05-20 14:01:55 +00:00
|
|
|
|
1998-07-04 15:17:59 +00:00
|
|
|
void wxChoice::SetSelection( int n )
|
1998-05-20 14:01:55 +00:00
|
|
|
{
|
|
|
|
int tmp = n;
|
|
|
|
gtk_option_menu_set_history( GTK_OPTION_MENU(m_widget), (gint)tmp );
|
1998-07-31 20:04:04 +00:00
|
|
|
|
1998-08-23 03:22:56 +00:00
|
|
|
gtk_choice_clicked_callback( (GtkWidget *) NULL, this );
|
1998-08-14 10:07:38 +00:00
|
|
|
}
|
1998-05-20 14:01:55 +00:00
|
|
|
|
|
|
|
void wxChoice::SetStringSelection( const wxString &string )
|
|
|
|
{
|
|
|
|
int n = FindString( string );
|
|
|
|
if (n != -1) SetSelection( n );
|
1998-08-14 10:07:38 +00:00
|
|
|
}
|
1998-05-20 14:01:55 +00:00
|
|
|
|