simplify menu item callbacks by passing in wxMenuItem instead of wxMenu

git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@54123 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
This commit is contained in:
Paul Cornett
2008-06-11 17:07:07 +00:00
parent a1b0c48fcc
commit e2147e6d3c
2 changed files with 18 additions and 48 deletions

View File

@@ -84,9 +84,6 @@ public:
// TODO: virtual void SetTitle(const wxString& title); // TODO: virtual void SetTitle(const wxString& title);
// implementation
int FindMenuIdByMenuItem( GtkWidget *menuItem ) const;
// implementation GTK only // implementation GTK only
GtkWidget *m_menu; // GtkMenu GtkWidget *m_menu; // GtkMenu
GtkWidget *m_owner; GtkWidget *m_owner;

View File

@@ -33,7 +33,7 @@ static const int wxGTK_TITLE_ID = -3;
// forward declare it as it's used by wxMenuBar too when using Hildon // forward declare it as it's used by wxMenuBar too when using Hildon
extern "C" extern "C"
{ {
static void gtk_menu_clicked_callback(GtkWidget *widget, wxMenu *menu); static void menuitem_activate(GtkWidget*, wxMenuItem* item);
} }
#if wxUSE_ACCEL #if wxUSE_ACCEL
@@ -263,7 +263,7 @@ bool wxMenuBar::GtkAppend(wxMenu *menu, const wxString& title, int pos)
menu->m_owner = gtk_menu_item_new_with_mnemonic( wxGTK_CONV( str ) ); menu->m_owner = gtk_menu_item_new_with_mnemonic( wxGTK_CONV( str ) );
g_signal_connect(menu->m_owner, "activate", g_signal_connect(menu->m_owner, "activate",
G_CALLBACK(gtk_menu_clicked_callback), menu); G_CALLBACK(menuitem_activate), item);
item->SetMenuItem(menu->m_owner); item->SetMenuItem(menu->m_owner);
} }
else else
@@ -458,21 +458,13 @@ void wxMenuBar::SetMenuLabel( size_t pos, const wxString& label )
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
extern "C" { extern "C" {
static void gtk_menu_clicked_callback( GtkWidget *widget, wxMenu *menu ) static void menuitem_activate(GtkWidget*, wxMenuItem* item)
{ {
int id = menu->FindMenuIdByMenuItem(widget); if (!item->IsEnabled())
/* should find it for normal (not popup) menu */
wxASSERT_MSG( (id != -1) || (menu->GetInvokingWindow() != NULL),
_T("menu item not found in gtk_menu_clicked_callback") );
if (!menu->IsEnabled(id))
return; return;
wxMenuItem* item = menu->FindChildItem( id ); int id = item->GetId();
wxCHECK_RET( item, wxT("error in menu item callback") ); if (id == wxGTK_TITLE_ID)
if ( item->GetId() == wxGTK_TITLE_ID )
{ {
// ignore events from the menu title // ignore events from the menu title
return; return;
@@ -498,6 +490,7 @@ static void gtk_menu_clicked_callback( GtkWidget *widget, wxMenu *menu )
// Is this menu on a menubar? (possibly nested) // Is this menu on a menubar? (possibly nested)
wxMenu* menu = item->GetMenu();
wxFrame* frame = NULL; wxFrame* frame = NULL;
if(menu->IsAttached()) if(menu->IsAttached())
frame = menu->GetMenuBar()->GetFrame(); frame = menu->GetMenuBar()->GetFrame();
@@ -531,16 +524,13 @@ static void gtk_menu_clicked_callback( GtkWidget *widget, wxMenu *menu )
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
extern "C" { extern "C" {
static void gtk_menu_hilight_callback( GtkWidget *widget, wxMenu *menu ) static void menuitem_select(GtkWidget*, wxMenuItem* item)
{ {
int id = menu->FindMenuIdByMenuItem(widget); if (!item->IsEnabled())
wxASSERT( id != -1 ); // should find it!
if (!menu->IsEnabled(id))
return; return;
wxMenuEvent event( wxEVT_MENU_HIGHLIGHT, id ); wxMenu* menu = item->GetMenu();
wxMenuEvent event(wxEVT_MENU_HIGHLIGHT, item->GetId());
event.SetEventObject( menu ); event.SetEventObject( menu );
wxEvtHandler* handler = menu->GetEventHandler(); wxEvtHandler* handler = menu->GetEventHandler();
@@ -557,15 +547,12 @@ static void gtk_menu_hilight_callback( GtkWidget *widget, wxMenu *menu )
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
extern "C" { extern "C" {
static void gtk_menu_nolight_callback( GtkWidget *widget, wxMenu *menu ) static void menuitem_deselect(GtkWidget*, wxMenuItem* item)
{ {
int id = menu->FindMenuIdByMenuItem(widget); if (!item->IsEnabled())
wxASSERT( id != -1 ); // should find it!
if (!menu->IsEnabled(id))
return; return;
wxMenu* menu = item->GetMenu();
wxMenuEvent event( wxEVT_MENU_HIGHLIGHT, -1 ); wxMenuEvent event( wxEVT_MENU_HIGHLIGHT, -1 );
event.SetEventObject( menu ); event.SetEventObject( menu );
@@ -858,9 +845,9 @@ bool wxMenu::GtkAppend(wxMenuItem *mitem, int pos)
{ {
mitem->SetGtkLabel(); mitem->SetGtkLabel();
g_signal_connect (menuItem, "select", g_signal_connect (menuItem, "select",
G_CALLBACK (gtk_menu_hilight_callback), this); G_CALLBACK(menuitem_select), mitem);
g_signal_connect (menuItem, "deselect", g_signal_connect (menuItem, "deselect",
G_CALLBACK (gtk_menu_nolight_callback), this); G_CALLBACK(menuitem_deselect), mitem);
if ( mitem->IsSubMenu() && mitem->GetKind() != wxITEM_RADIO && mitem->GetKind() != wxITEM_CHECK ) if ( mitem->IsSubMenu() && mitem->GetKind() != wxITEM_RADIO && mitem->GetKind() != wxITEM_CHECK )
{ {
@@ -877,8 +864,8 @@ bool wxMenu::GtkAppend(wxMenuItem *mitem, int pos)
else else
{ {
g_signal_connect (menuItem, "activate", g_signal_connect (menuItem, "activate",
G_CALLBACK (gtk_menu_clicked_callback), G_CALLBACK(menuitem_activate),
this); mitem);
} }
} }
@@ -925,20 +912,6 @@ wxMenuItem *wxMenu::DoRemove(wxMenuItem *item)
return item; return item;
} }
int wxMenu::FindMenuIdByMenuItem( GtkWidget *menuItem ) const
{
wxMenuItemList::compatibility_iterator node = m_items.GetFirst();
while (node)
{
wxMenuItem *item = node->GetData();
if (item->GetMenuItem() == menuItem)
return item->GetId();
node = node->GetNext();
}
return wxNOT_FOUND;
}
void wxMenu::Attach(wxMenuBarBase *menubar) void wxMenu::Attach(wxMenuBarBase *menubar)
{ {
wxMenuBase::Attach(menubar); wxMenuBase::Attach(menubar);