1998-05-20 14:01:55 +00:00
|
|
|
/////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Name: choice.cpp
|
|
|
|
// Purpose:
|
|
|
|
// Author: Robert Roebling
|
1998-10-26 10:56:58 +00:00
|
|
|
// Id: $Id$
|
1998-10-24 17:12:05 +00:00
|
|
|
// Copyright: (c) 1998 Robert Roebling
|
1999-03-16 18:54:24 +00:00
|
|
|
// Licence: wxWindows licence
|
1998-05-20 14:01:55 +00:00
|
|
|
/////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
|
2003-08-09 12:46:53 +00:00
|
|
|
#if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
|
1998-05-20 14:01:55 +00:00
|
|
|
#pragma implementation "choice.h"
|
|
|
|
#endif
|
|
|
|
|
2001-06-26 20:59:19 +00:00
|
|
|
#include "wx/defs.h"
|
1998-05-20 14:01:55 +00:00
|
|
|
|
1999-06-15 20:21:59 +00:00
|
|
|
#if wxUSE_CHOICE
|
|
|
|
|
2001-06-26 20:59:19 +00:00
|
|
|
#include "wx/choice.h"
|
2003-07-19 22:01:14 +00:00
|
|
|
#include "wx/arrstr.h"
|
2001-06-26 20:59:19 +00:00
|
|
|
|
2002-03-12 19:24:30 +00:00
|
|
|
#include "wx/gtk/private.h"
|
1999-01-02 19:13:25 +00:00
|
|
|
|
1999-04-27 19:32:19 +00:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// idle system
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
extern void wxapp_install_idle_handler();
|
|
|
|
extern bool g_isIdle;
|
|
|
|
|
1998-08-02 20:38:05 +00:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// data
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
extern bool g_blockEventsOnDrag;
|
|
|
|
|
1998-05-20 14:01:55 +00:00
|
|
|
//-----------------------------------------------------------------------------
|
1998-09-05 18:26:06 +00:00
|
|
|
// "activate"
|
1998-05-20 14:01:55 +00:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
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
|
|
|
{
|
1999-10-22 13:12:04 +00:00
|
|
|
if (g_isIdle)
|
1999-07-27 20:23:13 +00:00
|
|
|
wxapp_install_idle_handler();
|
1999-04-27 19:32:19 +00:00
|
|
|
|
1999-05-10 11:02:43 +00:00
|
|
|
if (!choice->m_hasVMT) return;
|
1999-03-16 18:54:24 +00:00
|
|
|
|
1999-04-27 19:32:19 +00:00
|
|
|
if (g_blockEventsOnDrag) return;
|
1999-03-16 18:54:24 +00:00
|
|
|
|
1999-04-27 19:32:19 +00:00
|
|
|
wxCommandEvent event(wxEVT_COMMAND_CHOICE_SELECTED, choice->GetId() );
|
1999-10-22 18:00:39 +00:00
|
|
|
int n = choice->GetSelection();
|
|
|
|
|
|
|
|
event.SetInt( n );
|
1999-04-27 19:32:19 +00:00
|
|
|
event.SetString( choice->GetStringSelection() );
|
|
|
|
event.SetEventObject(choice);
|
1999-10-22 18:00:39 +00:00
|
|
|
|
|
|
|
if ( choice->HasClientObjectData() )
|
|
|
|
event.SetClientObject( choice->GetClientObject(n) );
|
|
|
|
else if ( choice->HasClientUntypedData() )
|
|
|
|
event.SetClientData( choice->GetClientData(n) );
|
|
|
|
|
1999-04-27 19:32:19 +00:00
|
|
|
choice->GetEventHandler()->ProcessEvent(event);
|
1998-08-14 10:07:38 +00:00
|
|
|
}
|
1998-05-20 14:01:55 +00:00
|
|
|
|
1998-09-05 18:26:06 +00:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// wxChoice
|
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
|
|
|
|
1998-11-06 08:50:52 +00:00
|
|
|
wxChoice::wxChoice()
|
1998-05-20 14:01:55 +00:00
|
|
|
{
|
1999-10-22 13:12:04 +00:00
|
|
|
m_strings = (wxSortedArrayString *)NULL;
|
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,
|
1998-11-06 08:50:52 +00:00
|
|
|
const wxPoint &pos, const wxSize &size,
|
|
|
|
int n, const wxString choices[],
|
|
|
|
long style, const wxValidator& validator, const wxString &name )
|
1998-05-20 14:01:55 +00:00
|
|
|
{
|
1998-11-06 08:50:52 +00:00
|
|
|
m_needParent = TRUE;
|
1999-04-09 18:01:17 +00:00
|
|
|
#if (GTK_MINOR_VERSION > 0)
|
|
|
|
m_acceptsFocus = TRUE;
|
|
|
|
#endif
|
1999-03-16 18:54:24 +00:00
|
|
|
|
1999-07-27 20:23:13 +00:00
|
|
|
if (!PreCreation( parent, pos, size ) ||
|
|
|
|
!CreateBase( parent, id, pos, size, style, validator, name ))
|
|
|
|
{
|
1999-10-08 14:35:56 +00:00
|
|
|
wxFAIL_MSG( wxT("wxChoice creation failed") );
|
1999-10-22 13:12:04 +00:00
|
|
|
return FALSE;
|
1999-07-27 20:23:13 +00:00
|
|
|
}
|
1998-08-14 10:07:38 +00:00
|
|
|
|
1998-11-06 08:50:52 +00:00
|
|
|
m_widget = gtk_option_menu_new();
|
1999-03-16 18:54:24 +00:00
|
|
|
|
1999-10-22 13:12:04 +00:00
|
|
|
if ( style & wxCB_SORT )
|
|
|
|
{
|
|
|
|
// if our m_strings != NULL, DoAppend() will check for it and insert
|
|
|
|
// items in the correct order
|
|
|
|
m_strings = new wxSortedArrayString;
|
|
|
|
}
|
|
|
|
|
1998-11-06 08:50:52 +00:00
|
|
|
GtkWidget *menu = gtk_menu_new();
|
1999-03-16 18:54:24 +00:00
|
|
|
|
1998-11-06 08:50:52 +00:00
|
|
|
for (int i = 0; i < n; i++)
|
|
|
|
{
|
2003-05-09 12:58:28 +00:00
|
|
|
GtkAddHelper(menu, i, choices[i]);
|
1998-11-06 08:50:52 +00:00
|
|
|
}
|
1999-10-22 13:12:04 +00:00
|
|
|
|
1998-11-06 08:50:52 +00:00
|
|
|
gtk_option_menu_set_menu( GTK_OPTION_MENU(m_widget), menu );
|
1999-03-16 18:54:24 +00:00
|
|
|
|
1999-05-09 22:17:03 +00:00
|
|
|
m_parent->DoAddChild( this );
|
1999-03-16 18:54:24 +00:00
|
|
|
|
1998-11-06 08:50:52 +00:00
|
|
|
PostCreation();
|
2003-12-03 03:24:47 +00:00
|
|
|
InheritAttributes();
|
2000-01-04 13:02:27 +00:00
|
|
|
|
2001-11-27 16:15:25 +00:00
|
|
|
SetBestSize(size);
|
2000-01-04 13:02:27 +00:00
|
|
|
|
1998-11-06 08:50:52 +00:00
|
|
|
Show( TRUE );
|
1999-03-16 18:54:24 +00:00
|
|
|
|
1998-11-06 08:50:52 +00:00
|
|
|
return TRUE;
|
1998-08-14 10:07:38 +00:00
|
|
|
}
|
1999-03-16 18:54:24 +00:00
|
|
|
|
1998-11-06 08:50:52 +00:00
|
|
|
wxChoice::~wxChoice()
|
|
|
|
{
|
1998-11-06 13:13:43 +00:00
|
|
|
Clear();
|
1999-10-22 13:12:04 +00:00
|
|
|
|
|
|
|
delete m_strings;
|
1998-11-06 08:50:52 +00:00
|
|
|
}
|
|
|
|
|
1999-10-18 15:45:28 +00:00
|
|
|
int wxChoice::DoAppend( const wxString &item )
|
1998-11-06 08:50:52 +00:00
|
|
|
{
|
1999-10-22 15:55:27 +00:00
|
|
|
wxCHECK_MSG( m_widget != NULL, -1, wxT("invalid choice control") );
|
1999-03-16 18:54:24 +00:00
|
|
|
|
1998-11-06 08:50:52 +00:00
|
|
|
GtkWidget *menu = gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) );
|
1999-03-16 18:54:24 +00:00
|
|
|
|
2003-05-09 12:58:28 +00:00
|
|
|
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);
|
1998-11-06 08:50:52 +00:00
|
|
|
}
|
1998-10-26 00:19:25 +00:00
|
|
|
|
1999-10-22 18:00:39 +00:00
|
|
|
void wxChoice::DoSetItemClientData( int n, void* clientData )
|
1998-11-06 08:50:52 +00:00
|
|
|
{
|
1999-10-22 15:55:27 +00:00
|
|
|
wxCHECK_RET( m_widget != NULL, wxT("invalid choice control") );
|
1999-03-16 18:54:24 +00:00
|
|
|
|
2003-07-10 19:55:12 +00:00
|
|
|
wxList::compatibility_iterator node = m_clientList.Item( n );
|
1999-10-22 18:00:39 +00:00
|
|
|
wxCHECK_RET( node, wxT("invalid index in wxChoice::DoSetItemClientData") );
|
1999-03-16 18:54:24 +00:00
|
|
|
|
1998-11-06 13:13:43 +00:00
|
|
|
node->SetData( (wxObject*) clientData );
|
1998-11-06 08:50:52 +00:00
|
|
|
}
|
|
|
|
|
1999-10-22 18:00:39 +00:00
|
|
|
void* wxChoice::DoGetItemClientData( int n ) const
|
1998-11-06 08:50:52 +00:00
|
|
|
{
|
1999-10-22 15:55:27 +00:00
|
|
|
wxCHECK_MSG( m_widget != NULL, NULL, wxT("invalid choice control") );
|
1999-03-16 18:54:24 +00:00
|
|
|
|
2003-07-10 19:55:12 +00:00
|
|
|
wxList::compatibility_iterator node = m_clientList.Item( n );
|
1999-10-22 18:00:39 +00:00
|
|
|
wxCHECK_MSG( node, NULL, wxT("invalid index in wxChoice::DoGetItemClientData") );
|
1999-03-16 18:54:24 +00:00
|
|
|
|
2003-01-13 05:17:41 +00:00
|
|
|
return node->GetData();
|
1998-08-14 10:07:38 +00:00
|
|
|
}
|
1998-11-06 08:50:52 +00:00
|
|
|
|
1999-10-22 18:00:39 +00:00
|
|
|
void wxChoice::DoSetItemClientObject( int n, wxClientData* clientData )
|
1998-11-06 08:50:52 +00:00
|
|
|
{
|
1999-10-22 15:55:27 +00:00
|
|
|
wxCHECK_RET( m_widget != NULL, wxT("invalid choice control") );
|
1999-03-16 18:54:24 +00:00
|
|
|
|
2003-07-10 19:55:12 +00:00
|
|
|
wxList::compatibility_iterator node = m_clientList.Item( n );
|
1999-10-22 18:00:39 +00:00
|
|
|
wxCHECK_RET( node, wxT("invalid index in wxChoice::DoSetItemClientObject") );
|
1999-03-16 18:54:24 +00:00
|
|
|
|
2002-08-27 20:28:48 +00:00
|
|
|
// wxItemContainer already deletes data for us
|
1999-03-16 18:54:24 +00:00
|
|
|
|
1998-11-06 08:50:52 +00:00
|
|
|
node->SetData( (wxObject*) clientData );
|
|
|
|
}
|
|
|
|
|
1999-10-22 18:00:39 +00:00
|
|
|
wxClientData* wxChoice::DoGetItemClientObject( int n ) const
|
1998-11-06 08:50:52 +00:00
|
|
|
{
|
1999-10-22 15:55:27 +00:00
|
|
|
wxCHECK_MSG( m_widget != NULL, (wxClientData*) NULL, wxT("invalid choice control") );
|
1999-03-16 18:54:24 +00:00
|
|
|
|
2003-07-10 19:55:12 +00:00
|
|
|
wxList::compatibility_iterator node = m_clientList.Item( n );
|
1999-10-18 15:45:28 +00:00
|
|
|
wxCHECK_MSG( node, (wxClientData *)NULL,
|
1999-10-22 18:00:39 +00:00
|
|
|
wxT("invalid index in wxChoice::DoGetItemClientObject") );
|
1999-03-16 18:54:24 +00:00
|
|
|
|
2003-01-13 05:17:41 +00:00
|
|
|
return (wxClientData*) node->GetData();
|
1998-11-06 08:50:52 +00:00
|
|
|
}
|
1999-03-16 18:54:24 +00:00
|
|
|
|
1998-11-06 08:50:52 +00:00
|
|
|
void wxChoice::Clear()
|
1998-05-20 14:01:55 +00:00
|
|
|
{
|
1999-10-08 14:35:56 +00:00
|
|
|
wxCHECK_RET( m_widget != NULL, wxT("invalid choice") );
|
1998-10-26 00:19:25 +00:00
|
|
|
|
1998-11-06 08:50:52 +00:00
|
|
|
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 );
|
1999-03-16 18:54:24 +00:00
|
|
|
|
1999-10-22 18:00:39 +00:00
|
|
|
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!
|
2003-07-10 19:55:12 +00:00
|
|
|
wxList::compatibility_iterator node = m_clientList.GetFirst();
|
1999-10-22 18:00:39 +00:00
|
|
|
while ( node )
|
|
|
|
{
|
2003-01-13 05:17:41 +00:00
|
|
|
delete (wxClientData *)node->GetData();
|
|
|
|
node = node->GetNext();
|
1999-10-22 18:00:39 +00:00
|
|
|
}
|
|
|
|
}
|
1999-10-19 10:51:48 +00:00
|
|
|
m_clientList.Clear();
|
1999-10-22 15:55:27 +00:00
|
|
|
|
|
|
|
if ( m_strings )
|
|
|
|
m_strings->Clear();
|
1998-08-14 10:07:38 +00:00
|
|
|
}
|
1998-05-20 14:01:55 +00:00
|
|
|
|
2002-04-12 09:31:12 +00:00
|
|
|
void wxChoice::Delete( int n )
|
1998-09-01 15:41:45 +00:00
|
|
|
{
|
2002-04-12 09:31:12 +00:00
|
|
|
wxCHECK_RET( m_widget != NULL, wxT("invalid choice") );
|
|
|
|
|
|
|
|
// VZ: apparently GTK+ doesn't have a built-in function to do it (not even
|
2003-09-30 22:24:06 +00:00
|
|
|
// in 2.0), hence this dumb implementation -- still better than nothing
|
2002-04-12 09:31:12 +00:00
|
|
|
int i,
|
|
|
|
count = GetCount();
|
|
|
|
|
|
|
|
wxCHECK_RET( n >= 0 && n < count, _T("invalid index in wxChoice::Delete") );
|
|
|
|
|
2003-09-30 22:24:06 +00:00
|
|
|
const bool hasClientData = m_clientDataItemsType != wxClientData_None;
|
|
|
|
const bool hasObjectData = m_clientDataItemsType == wxClientData_Object;
|
|
|
|
|
|
|
|
wxList::compatibility_iterator node = m_clientList.GetFirst();
|
|
|
|
|
2002-04-12 09:31:12 +00:00
|
|
|
wxArrayString items;
|
2003-09-30 22:24:06 +00:00
|
|
|
wxArrayPtrVoid itemsData;
|
2002-04-12 09:31:12 +00:00
|
|
|
items.Alloc(count);
|
|
|
|
for ( i = 0; i < count; i++ )
|
|
|
|
{
|
|
|
|
if ( i != n )
|
2003-09-30 22:24:06 +00:00
|
|
|
{
|
2002-04-12 09:31:12 +00:00
|
|
|
items.Add(GetString(i));
|
2003-09-30 22:24:06 +00:00
|
|
|
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;
|
2002-04-12 09:31:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Clear();
|
|
|
|
|
|
|
|
for ( i = 0; i < count - 1; i++ )
|
|
|
|
{
|
|
|
|
Append(items[i]);
|
2003-09-30 22:24:06 +00:00
|
|
|
|
|
|
|
if ( hasObjectData )
|
|
|
|
SetClientObject(i, (wxClientData *)itemsData[i]);
|
|
|
|
else if ( hasClientData )
|
2003-09-30 22:26:47 +00:00
|
|
|
SetClientData(i, itemsData[i]);
|
2002-04-12 09:31:12 +00:00
|
|
|
}
|
1998-09-01 15:41:45 +00:00
|
|
|
}
|
|
|
|
|
1998-05-20 14:01:55 +00:00
|
|
|
int wxChoice::FindString( const wxString &string ) const
|
|
|
|
{
|
1999-10-08 14:35:56 +00:00
|
|
|
wxCHECK_MSG( m_widget != NULL, -1, wxT("invalid choice") );
|
1998-11-06 08:50:52 +00:00
|
|
|
|
|
|
|
// If you read this code once and you think you understand
|
|
|
|
// it, then you are very wrong. Robert Roebling.
|
1999-03-16 18:54:24 +00:00
|
|
|
|
1998-11-06 08:50:52 +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 );
|
|
|
|
GtkLabel *label = (GtkLabel *) NULL;
|
2002-03-12 19:24:30 +00:00
|
|
|
if (bin->child)
|
|
|
|
label = GTK_LABEL(bin->child);
|
|
|
|
if (!label)
|
|
|
|
label = GTK_LABEL( BUTTON_CHILD(m_widget) );
|
1999-03-16 18:54:24 +00:00
|
|
|
|
1999-10-08 14:35:56 +00:00
|
|
|
wxASSERT_MSG( label != NULL , wxT("wxChoice: invalid label") );
|
2003-09-30 22:24:06 +00:00
|
|
|
|
2002-08-15 20:45:58 +00:00
|
|
|
#ifdef __WXGTK20__
|
|
|
|
wxString tmp( wxGTK_CONV_BACK( gtk_label_get_text( label) ) );
|
|
|
|
#else
|
|
|
|
wxString tmp( label->label );
|
|
|
|
#endif
|
2002-08-05 17:59:20 +00:00
|
|
|
if (string == tmp)
|
2002-03-12 19:24:30 +00:00
|
|
|
return count;
|
1999-03-16 18:54:24 +00:00
|
|
|
|
2002-03-12 19:24:30 +00:00
|
|
|
child = child->next;
|
|
|
|
count++;
|
1998-11-06 08:50:52 +00:00
|
|
|
}
|
1999-03-16 18:54:24 +00:00
|
|
|
|
1998-11-06 08:50:52 +00:00
|
|
|
return -1;
|
1998-08-14 10:07:38 +00:00
|
|
|
}
|
1998-05-20 14:01:55 +00:00
|
|
|
|
1999-10-18 15:45:28 +00:00
|
|
|
int wxChoice::GetSelection() const
|
1998-05-20 14:01:55 +00:00
|
|
|
{
|
1999-10-08 14:35:56 +00:00
|
|
|
wxCHECK_MSG( m_widget != NULL, -1, wxT("invalid choice") );
|
2003-09-30 22:24:06 +00:00
|
|
|
|
2002-08-15 20:45:58 +00:00
|
|
|
#ifdef __WXGTK20__
|
1998-11-06 08:50:52 +00:00
|
|
|
|
2002-08-15 20:45:58 +00:00
|
|
|
return gtk_option_menu_get_history( GTK_OPTION_MENU(m_widget) );
|
2003-09-30 22:24:06 +00:00
|
|
|
|
2002-08-15 20:45:58 +00:00
|
|
|
#else
|
1998-11-06 08:50:52 +00:00
|
|
|
GtkMenuShell *menu_shell = GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) ) );
|
|
|
|
int count = 0;
|
2003-09-30 22:24:06 +00:00
|
|
|
|
1998-11-06 08:50:52 +00:00
|
|
|
GList *child = menu_shell->children;
|
|
|
|
while (child)
|
|
|
|
{
|
|
|
|
GtkBin *bin = GTK_BIN( child->data );
|
|
|
|
if (!bin->child) return count;
|
|
|
|
child = child->next;
|
|
|
|
count++;
|
|
|
|
}
|
1999-03-16 18:54:24 +00:00
|
|
|
|
1998-11-06 08:50:52 +00:00
|
|
|
return -1;
|
2002-08-15 20:45:58 +00:00
|
|
|
#endif
|
1998-08-14 10:07:38 +00:00
|
|
|
}
|
1998-05-20 14:01:55 +00:00
|
|
|
|
2003-10-06 18:43:45 +00:00
|
|
|
void wxChoice::SetString( int n, const wxString& str )
|
1999-10-22 18:00:39 +00:00
|
|
|
{
|
|
|
|
wxCHECK_RET( m_widget != NULL, wxT("invalid choice") );
|
|
|
|
|
2003-10-06 18:09:20 +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)
|
|
|
|
{
|
|
|
|
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++;
|
|
|
|
}
|
1999-10-22 18:00:39 +00:00
|
|
|
}
|
|
|
|
|
1998-07-04 15:17:59 +00:00
|
|
|
wxString wxChoice::GetString( int n ) const
|
1998-05-20 14:01:55 +00:00
|
|
|
{
|
1999-10-08 14:35:56 +00:00
|
|
|
wxCHECK_MSG( m_widget != NULL, wxT(""), wxT("invalid choice") );
|
1998-11-06 08:50:52 +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)
|
1998-05-20 14:01:55 +00:00
|
|
|
{
|
1998-11-06 08:50:52 +00:00
|
|
|
GtkBin *bin = GTK_BIN( child->data );
|
|
|
|
if (count == n)
|
|
|
|
{
|
|
|
|
GtkLabel *label = (GtkLabel *) NULL;
|
2002-03-12 19:24:30 +00:00
|
|
|
if (bin->child)
|
|
|
|
label = GTK_LABEL(bin->child);
|
|
|
|
if (!label)
|
|
|
|
label = GTK_LABEL( BUTTON_CHILD(m_widget) );
|
1999-03-16 18:54:24 +00:00
|
|
|
|
1999-10-08 14:35:56 +00:00
|
|
|
wxASSERT_MSG( label != NULL , wxT("wxChoice: invalid label") );
|
1999-03-16 18:54:24 +00:00
|
|
|
|
2002-08-15 20:45:58 +00:00
|
|
|
#ifdef __WXGTK20__
|
|
|
|
return wxString( wxGTK_CONV_BACK( gtk_label_get_text( label) ) );
|
|
|
|
#else
|
|
|
|
return wxString( label->label );
|
|
|
|
#endif
|
1998-11-06 08:50:52 +00:00
|
|
|
}
|
|
|
|
child = child->next;
|
|
|
|
count++;
|
1998-08-14 10:07:38 +00:00
|
|
|
}
|
1999-03-16 18:54:24 +00:00
|
|
|
|
1999-10-08 14:35:56 +00:00
|
|
|
wxFAIL_MSG( wxT("wxChoice: invalid index in GetString()") );
|
1999-03-16 18:54:24 +00:00
|
|
|
|
1999-10-08 14:35:56 +00:00
|
|
|
return wxT("");
|
1998-08-14 10:07:38 +00:00
|
|
|
}
|
1998-05-20 14:01:55 +00:00
|
|
|
|
1999-10-18 15:45:28 +00:00
|
|
|
int wxChoice::GetCount() const
|
1998-05-20 14:01:55 +00:00
|
|
|
{
|
1999-10-08 14:35:56 +00:00
|
|
|
wxCHECK_MSG( m_widget != NULL, 0, wxT("invalid choice") );
|
1998-11-06 08:50:52 +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)
|
|
|
|
{
|
|
|
|
count++;
|
|
|
|
child = child->next;
|
|
|
|
}
|
|
|
|
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::SetSelection( int n )
|
1998-05-20 14:01:55 +00:00
|
|
|
{
|
1999-10-08 14:35:56 +00:00
|
|
|
wxCHECK_RET( m_widget != NULL, wxT("invalid choice") );
|
1998-10-26 00:19:25 +00:00
|
|
|
|
1998-11-06 08:50:52 +00:00
|
|
|
int tmp = n;
|
|
|
|
gtk_option_menu_set_history( GTK_OPTION_MENU(m_widget), (gint)tmp );
|
1998-08-14 10:07:38 +00:00
|
|
|
}
|
1998-05-20 14:01:55 +00:00
|
|
|
|
1998-10-29 18:03:18 +00:00
|
|
|
void wxChoice::ApplyWidgetStyle()
|
1998-09-06 13:46:50 +00:00
|
|
|
{
|
1998-11-06 08:50:52 +00:00
|
|
|
SetWidgetStyle();
|
1999-03-16 18:54:24 +00:00
|
|
|
|
1998-11-06 08:50:52 +00:00
|
|
|
GtkMenuShell *menu_shell = GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) ) );
|
1999-03-16 18:54:24 +00:00
|
|
|
|
1998-11-06 08:50:52 +00:00
|
|
|
gtk_widget_set_style( m_widget, m_widgetStyle );
|
|
|
|
gtk_widget_set_style( GTK_WIDGET( menu_shell ), m_widgetStyle );
|
1999-03-16 18:54:24 +00:00
|
|
|
|
1998-11-06 08:50:52 +00:00
|
|
|
GList *child = menu_shell->children;
|
|
|
|
while (child)
|
|
|
|
{
|
|
|
|
gtk_widget_set_style( GTK_WIDGET( child->data ), m_widgetStyle );
|
1999-03-16 18:54:24 +00:00
|
|
|
|
1998-11-06 08:50:52 +00:00
|
|
|
GtkBin *bin = GTK_BIN( child->data );
|
|
|
|
GtkWidget *label = (GtkWidget *) NULL;
|
2002-03-12 19:24:30 +00:00
|
|
|
if (bin->child)
|
|
|
|
label = bin->child;
|
|
|
|
if (!label)
|
|
|
|
label = BUTTON_CHILD(m_widget);
|
1999-03-16 18:54:24 +00:00
|
|
|
|
1998-11-06 08:50:52 +00:00
|
|
|
gtk_widget_set_style( label, m_widgetStyle );
|
1999-03-16 18:54:24 +00:00
|
|
|
|
1998-11-06 08:50:52 +00:00
|
|
|
child = child->next;
|
|
|
|
}
|
1998-10-26 00:19:25 +00:00
|
|
|
}
|
|
|
|
|
2003-05-09 12:58:28 +00:00
|
|
|
int wxChoice::GtkAddHelper(GtkWidget *menu, int pos, const wxString& item)
|
1999-10-22 13:12:04 +00:00
|
|
|
{
|
2003-05-09 12:58:28 +00:00
|
|
|
wxCHECK_MSG((pos>=0) && (pos<=(int)m_clientList.GetCount()), -1, wxT("invalid index"));
|
|
|
|
|
2002-08-05 17:59:20 +00:00
|
|
|
GtkWidget *menu_item = gtk_menu_item_new_with_label( wxGTK_CONV( item ) );
|
1999-10-22 13:12:04 +00:00
|
|
|
|
|
|
|
size_t index;
|
|
|
|
if ( m_strings )
|
|
|
|
{
|
|
|
|
// sorted control, need to insert at the correct index
|
|
|
|
index = m_strings->Add(item);
|
|
|
|
|
|
|
|
gtk_menu_insert( GTK_MENU(menu), menu_item, index );
|
|
|
|
|
|
|
|
if ( index )
|
|
|
|
{
|
|
|
|
m_clientList.Insert( m_clientList.Item(index - 1),
|
|
|
|
(wxObject*) NULL );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
1999-10-22 18:00:39 +00:00
|
|
|
m_clientList.Insert( (wxObject*) NULL );
|
1999-10-22 13:12:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2003-05-09 12:58:28 +00:00
|
|
|
// don't call wxChoice::GetCount() from here because it doesn't work
|
|
|
|
// if we're called from ctor (and GtkMenuShell is still NULL)
|
|
|
|
|
1999-10-22 13:12:04 +00:00
|
|
|
// normal control, just append
|
2003-05-09 12:58:28 +00:00
|
|
|
if (pos == (int)m_clientList.GetCount())
|
|
|
|
{
|
1999-10-22 13:12:04 +00:00
|
|
|
gtk_menu_append( GTK_MENU(menu), menu_item );
|
|
|
|
m_clientList.Append( (wxObject*) NULL );
|
1999-10-24 16:45:01 +00:00
|
|
|
index = m_clientList.GetCount() - 1;
|
2003-05-09 12:58:28 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
gtk_menu_insert( GTK_MENU(menu), menu_item, pos );
|
|
|
|
m_clientList.Insert( pos, (wxObject*) NULL );
|
|
|
|
index = pos;
|
|
|
|
}
|
1999-10-22 13:12:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (GTK_WIDGET_REALIZED(m_widget))
|
|
|
|
{
|
|
|
|
gtk_widget_realize( menu_item );
|
|
|
|
gtk_widget_realize( GTK_BIN(menu_item)->child );
|
|
|
|
|
|
|
|
if (m_widgetStyle) ApplyWidgetStyle();
|
|
|
|
}
|
|
|
|
|
|
|
|
gtk_signal_connect( GTK_OBJECT( menu_item ), "activate",
|
|
|
|
GTK_SIGNAL_FUNC(gtk_choice_clicked_callback), (gpointer*)this );
|
|
|
|
|
|
|
|
gtk_widget_show( menu_item );
|
|
|
|
|
|
|
|
// return the index of the item in the control
|
|
|
|
return index;
|
|
|
|
}
|
|
|
|
|
1999-11-19 21:01:20 +00:00
|
|
|
wxSize wxChoice::DoGetBestSize() const
|
|
|
|
{
|
2000-01-04 13:02:27 +00:00
|
|
|
wxSize ret( wxControl::DoGetBestSize() );
|
2001-11-23 19:43:02 +00:00
|
|
|
|
|
|
|
// we know better our horizontal extent: it depends on the longest string
|
|
|
|
// we have
|
|
|
|
ret.x = 0;
|
|
|
|
if ( m_widget )
|
|
|
|
{
|
2003-01-05 22:57:45 +00:00
|
|
|
int width;
|
2001-11-23 19:43:02 +00:00
|
|
|
size_t count = GetCount();
|
|
|
|
for ( size_t n = 0; n < count; n++ )
|
|
|
|
{
|
2003-01-05 22:57:45 +00:00
|
|
|
GetTextExtent( GetString(n), &width, NULL, NULL, NULL, &m_font );
|
2001-11-23 19:43:02 +00:00
|
|
|
if ( width > ret.x )
|
|
|
|
ret.x = width;
|
|
|
|
}
|
|
|
|
|
2001-11-27 16:15:25 +00:00
|
|
|
// 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;
|
2001-11-23 19:43:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// but not less than the minimal width
|
|
|
|
if ( ret.x < 80 )
|
|
|
|
ret.x = 80;
|
|
|
|
|
2003-01-31 10:31:05 +00:00
|
|
|
ret.y = 16 + GetCharHeight();
|
2002-03-12 19:24:30 +00:00
|
|
|
|
2000-01-04 13:02:27 +00:00
|
|
|
return ret;
|
1999-11-19 21:01:20 +00:00
|
|
|
}
|
|
|
|
|
2003-07-08 11:26:04 +00:00
|
|
|
bool wxChoice::IsOwnGtkWindow( GdkWindow *window )
|
|
|
|
{
|
|
|
|
#ifdef __WXGTK20__
|
|
|
|
return GTK_BUTTON(m_widget)->event_window;
|
|
|
|
#else
|
|
|
|
return (window == m_widget->window);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-11-27 16:15:25 +00:00
|
|
|
#endif // wxUSE_CHOICE
|
|
|
|
|