Fixed menu event propogation to be consistent across platforms, and

with the docs.


git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@19967 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
This commit is contained in:
Robin Dunn
2003-04-03 18:39:20 +00:00
parent 91ce04cf9e
commit a01fe3d6d2
5 changed files with 106 additions and 99 deletions

View File

@@ -199,7 +199,8 @@ bool wxFrameBase::ProcessCommand(int id)
} }
} }
return GetEventHandler()->ProcessEvent(commandEvent); GetEventHandler()->ProcessEvent(commandEvent);
return TRUE;
#else // !wxUSE_MENUS #else // !wxUSE_MENUS
return FALSE; return FALSE;
#endif // wxUSE_MENUS/!wxUSE_MENUS #endif // wxUSE_MENUS/!wxUSE_MENUS

View File

@@ -172,7 +172,8 @@ static void gtk_menu_open_callback( GtkWidget *widget, wxMenu *menu )
wxMenuEvent event( wxEVT_MENU_OPEN, -1 ); wxMenuEvent event( wxEVT_MENU_OPEN, -1 );
event.SetEventObject( menu ); event.SetEventObject( menu );
if (menu->GetEventHandler()->ProcessEvent(event)) wxEvtHandler* handler = menu->GetEventHandler();
if (handler && handler->ProcessEvent(event))
return; return;
wxWindow *win = menu->GetInvokingWindow(); wxWindow *win = menu->GetInvokingWindow();
@@ -375,7 +376,7 @@ bool wxMenuBar::GtkAppend(wxMenu *menu, const wxString& title)
buf << wxT('/') << str.c_str(); buf << wxT('/') << str.c_str();
// local buffer in multibyte form // local buffer in multibyte form
char cbuf[400]; char cbuf[400];
strcpy(cbuf, wxGTK_CONV(buf) ); strcpy(cbuf, wxGTK_CONV(buf) );
GtkItemFactoryEntry entry; GtkItemFactoryEntry entry;
@@ -386,7 +387,7 @@ bool wxMenuBar::GtkAppend(wxMenu *menu, const wxString& title)
entry.item_type = (char *)"<Branch>"; entry.item_type = (char *)"<Branch>";
gtk_item_factory_create_item( m_factory, &entry, (gpointer) this, 2 ); // what is 2 ? gtk_item_factory_create_item( m_factory, &entry, (gpointer) this, 2 ); // what is 2 ?
// in order to get the pointer to the item we need the item text _without_ underscores // in order to get the pointer to the item we need the item text _without_ underscores
wxString tmp = wxT("<main>/"); wxString tmp = wxT("<main>/");
const wxChar *pc; const wxChar *pc;
for ( pc = str; *pc != wxT('\0'); pc++ ) for ( pc = str; *pc != wxT('\0'); pc++ )
@@ -493,10 +494,10 @@ static wxMenu *CopyMenu (wxMenu *menu)
else else
menucopy->Append (itemid, text, CopyMenu(submenu), menucopy->Append (itemid, text, CopyMenu(submenu),
menu->GetHelpString(itemid)); menu->GetHelpString(itemid));
node = node->GetNext(); node = node->GetNext();
} }
return menucopy; return menucopy;
} }
@@ -700,30 +701,38 @@ static void gtk_menu_clicked_callback( GtkWidget *widget, wxMenu *menu )
if (!menu->IsEnabled(id)) if (!menu->IsEnabled(id))
return; return;
wxMenuItem* item = menu->FindChildItem( id ); if ( menu->IsAttached() ) // is this menu on a menubar?
wxCHECK_RET( item, wxT("error in menu item callback") );
if (item->IsCheckable())
{ {
bool isReallyChecked = item->IsChecked(), wxFrame* frame = menu->GetMenuBar()->GetFrame();
isInternallyChecked = item->wxMenuItemBase::IsChecked(); frame->ProcessCommand(id);
}
else
{
wxMenuItem* item = menu->FindChildItem( id );
wxCHECK_RET( item, wxT("error in menu item callback") );
// ensure that the internal state is always consistent with what is if (item->IsCheckable())
// shown on the screen
item->wxMenuItemBase::Check(isReallyChecked);
// we must not report the events for the radio button going up nor the
// events resulting from the calls to wxMenuItem::Check()
if ( (item->GetKind() == wxITEM_RADIO && !isReallyChecked) ||
(isInternallyChecked == isReallyChecked) )
{ {
return; bool isReallyChecked = item->IsChecked(),
isInternallyChecked = item->wxMenuItemBase::IsChecked();
// ensure that the internal state is always consistent with what is
// shown on the screen
item->wxMenuItemBase::Check(isReallyChecked);
// we must not report the events for the radio button going up nor the
// events resulting from the calls to wxMenuItem::Check()
if ( (item->GetKind() == wxITEM_RADIO && !isReallyChecked) ||
(isInternallyChecked == isReallyChecked) )
{
return;
}
// the user pressed on the menu item: report the event below
} }
// the user pressed on the menu item: report the event below menu->SendEvent(id, item->IsCheckable() ? item->IsChecked() : -1);
} }
menu->SendEvent(id, item->IsCheckable() ? item->IsChecked() : -1);
} }
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
@@ -744,7 +753,8 @@ static void gtk_menu_hilight_callback( GtkWidget *widget, wxMenu *menu )
wxMenuEvent event( wxEVT_MENU_HIGHLIGHT, id ); wxMenuEvent event( wxEVT_MENU_HIGHLIGHT, id );
event.SetEventObject( menu ); event.SetEventObject( menu );
if (menu->GetEventHandler()->ProcessEvent(event)) wxEvtHandler* handler = menu->GetEventHandler();
if (handler && handler->ProcessEvent(event))
return; return;
wxWindow *win = menu->GetInvokingWindow(); wxWindow *win = menu->GetInvokingWindow();
@@ -769,7 +779,8 @@ static void gtk_menu_nolight_callback( GtkWidget *widget, wxMenu *menu )
wxMenuEvent event( wxEVT_MENU_HIGHLIGHT, -1 ); wxMenuEvent event( wxEVT_MENU_HIGHLIGHT, -1 );
event.SetEventObject( menu ); event.SetEventObject( menu );
if (menu->GetEventHandler()->ProcessEvent(event)) wxEvtHandler* handler = menu->GetEventHandler();
if (handler && handler->ProcessEvent(event))
return; return;
wxWindow *win = menu->GetInvokingWindow(); wxWindow *win = menu->GetInvokingWindow();
@@ -861,10 +872,10 @@ wxString wxMenuItemBase::GetLabelFromText(const wxString& text)
// "&" is doubled to indicate "&" instead of accelerator // "&" is doubled to indicate "&" instead of accelerator
continue; continue;
} }
label += *pc; label += *pc;
} }
// wxPrintf( L"text %s label %s\n", text.c_str(), label.c_str() ); // wxPrintf( L"text %s label %s\n", text.c_str(), label.c_str() );
return label; return label;
@@ -879,7 +890,7 @@ void wxMenuItem::SetText( const wxString& str )
wxString label1 = wxStripMenuCodes(str.BeforeFirst('\t')); wxString label1 = wxStripMenuCodes(str.BeforeFirst('\t'));
if (oldLabel == label1) if (oldLabel == label1)
return; return;
DoSetText(str); DoSetText(str);
if (m_menuItem) if (m_menuItem)
@@ -898,7 +909,7 @@ void wxMenuItem::SetText( const wxString& str )
if (m_text[n] != wxT('\\')) if (m_text[n] != wxT('\\'))
tmp += m_text[n]; tmp += m_text[n];
} }
gtk_label_set_text_with_mnemonic( GTK_LABEL(label), wxGTK_CONV(tmp) ); gtk_label_set_text_with_mnemonic( GTK_LABEL(label), wxGTK_CONV(tmp) );
#else #else
// set new text // set new text
@@ -958,9 +969,9 @@ void wxMenuItem::DoSetText( const wxString& str )
} }
++pc; ++pc;
} }
// wxPrintf( L"str %s m_text %s\n", str.c_str(), m_text.c_str() ); // wxPrintf( L"str %s m_text %s\n", str.c_str(), m_text.c_str() );
m_hotKey = wxT(""); m_hotKey = wxT("");
if(*pc == wxT('\t')) if(*pc == wxT('\t'))
@@ -1190,7 +1201,7 @@ bool wxMenu::GtkAppend(wxMenuItem *mitem)
gtk_signal_connect( GTK_OBJECT(menuItem), "activate", gtk_signal_connect( GTK_OBJECT(menuItem), "activate",
GTK_SIGNAL_FUNC(gtk_menu_clicked_callback), GTK_SIGNAL_FUNC(gtk_menu_clicked_callback),
(gpointer)this ); (gpointer)this );
gtk_menu_append( GTK_MENU(m_menu), menuItem ); gtk_menu_append( GTK_MENU(m_menu), menuItem );
gtk_widget_show( menuItem ); gtk_widget_show( menuItem );
@@ -1276,7 +1287,7 @@ bool wxMenu::GtkAppend(wxMenuItem *mitem)
wxString path( mitem->GetFactoryPath() ); wxString path( mitem->GetFactoryPath() );
menuItem = gtk_item_factory_get_widget( m_factory, wxGTK_CONV( path ) ); menuItem = gtk_item_factory_get_widget( m_factory, wxGTK_CONV( path ) );
if (!menuItem) if (!menuItem)
wxLogError( wxT("Wrong menu path: %s\n"), path.c_str() ); wxLogError( wxT("Wrong menu path: %s\n"), path.c_str() );
} }
@@ -1284,7 +1295,7 @@ bool wxMenu::GtkAppend(wxMenuItem *mitem)
if ( !mitem->IsSeparator() ) if ( !mitem->IsSeparator() )
{ {
wxASSERT_MSG( menuItem, wxT("invalid menuitem") ); wxASSERT_MSG( menuItem, wxT("invalid menuitem") );
gtk_signal_connect( GTK_OBJECT(menuItem), "select", gtk_signal_connect( GTK_OBJECT(menuItem), "select",
GTK_SIGNAL_FUNC(gtk_menu_hilight_callback), GTK_SIGNAL_FUNC(gtk_menu_hilight_callback),
(gpointer)this ); (gpointer)this );

View File

@@ -172,7 +172,8 @@ static void gtk_menu_open_callback( GtkWidget *widget, wxMenu *menu )
wxMenuEvent event( wxEVT_MENU_OPEN, -1 ); wxMenuEvent event( wxEVT_MENU_OPEN, -1 );
event.SetEventObject( menu ); event.SetEventObject( menu );
if (menu->GetEventHandler()->ProcessEvent(event)) wxEvtHandler* handler = menu->GetEventHandler();
if (handler && handler->ProcessEvent(event))
return; return;
wxWindow *win = menu->GetInvokingWindow(); wxWindow *win = menu->GetInvokingWindow();
@@ -375,7 +376,7 @@ bool wxMenuBar::GtkAppend(wxMenu *menu, const wxString& title)
buf << wxT('/') << str.c_str(); buf << wxT('/') << str.c_str();
// local buffer in multibyte form // local buffer in multibyte form
char cbuf[400]; char cbuf[400];
strcpy(cbuf, wxGTK_CONV(buf) ); strcpy(cbuf, wxGTK_CONV(buf) );
GtkItemFactoryEntry entry; GtkItemFactoryEntry entry;
@@ -386,7 +387,7 @@ bool wxMenuBar::GtkAppend(wxMenu *menu, const wxString& title)
entry.item_type = (char *)"<Branch>"; entry.item_type = (char *)"<Branch>";
gtk_item_factory_create_item( m_factory, &entry, (gpointer) this, 2 ); // what is 2 ? gtk_item_factory_create_item( m_factory, &entry, (gpointer) this, 2 ); // what is 2 ?
// in order to get the pointer to the item we need the item text _without_ underscores // in order to get the pointer to the item we need the item text _without_ underscores
wxString tmp = wxT("<main>/"); wxString tmp = wxT("<main>/");
const wxChar *pc; const wxChar *pc;
for ( pc = str; *pc != wxT('\0'); pc++ ) for ( pc = str; *pc != wxT('\0'); pc++ )
@@ -493,10 +494,10 @@ static wxMenu *CopyMenu (wxMenu *menu)
else else
menucopy->Append (itemid, text, CopyMenu(submenu), menucopy->Append (itemid, text, CopyMenu(submenu),
menu->GetHelpString(itemid)); menu->GetHelpString(itemid));
node = node->GetNext(); node = node->GetNext();
} }
return menucopy; return menucopy;
} }
@@ -700,30 +701,38 @@ static void gtk_menu_clicked_callback( GtkWidget *widget, wxMenu *menu )
if (!menu->IsEnabled(id)) if (!menu->IsEnabled(id))
return; return;
wxMenuItem* item = menu->FindChildItem( id ); if ( menu->IsAttached() ) // is this menu on a menubar?
wxCHECK_RET( item, wxT("error in menu item callback") );
if (item->IsCheckable())
{ {
bool isReallyChecked = item->IsChecked(), wxFrame* frame = menu->GetMenuBar()->GetFrame();
isInternallyChecked = item->wxMenuItemBase::IsChecked(); frame->ProcessCommand(id);
}
else
{
wxMenuItem* item = menu->FindChildItem( id );
wxCHECK_RET( item, wxT("error in menu item callback") );
// ensure that the internal state is always consistent with what is if (item->IsCheckable())
// shown on the screen
item->wxMenuItemBase::Check(isReallyChecked);
// we must not report the events for the radio button going up nor the
// events resulting from the calls to wxMenuItem::Check()
if ( (item->GetKind() == wxITEM_RADIO && !isReallyChecked) ||
(isInternallyChecked == isReallyChecked) )
{ {
return; bool isReallyChecked = item->IsChecked(),
isInternallyChecked = item->wxMenuItemBase::IsChecked();
// ensure that the internal state is always consistent with what is
// shown on the screen
item->wxMenuItemBase::Check(isReallyChecked);
// we must not report the events for the radio button going up nor the
// events resulting from the calls to wxMenuItem::Check()
if ( (item->GetKind() == wxITEM_RADIO && !isReallyChecked) ||
(isInternallyChecked == isReallyChecked) )
{
return;
}
// the user pressed on the menu item: report the event below
} }
// the user pressed on the menu item: report the event below menu->SendEvent(id, item->IsCheckable() ? item->IsChecked() : -1);
} }
menu->SendEvent(id, item->IsCheckable() ? item->IsChecked() : -1);
} }
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
@@ -744,7 +753,8 @@ static void gtk_menu_hilight_callback( GtkWidget *widget, wxMenu *menu )
wxMenuEvent event( wxEVT_MENU_HIGHLIGHT, id ); wxMenuEvent event( wxEVT_MENU_HIGHLIGHT, id );
event.SetEventObject( menu ); event.SetEventObject( menu );
if (menu->GetEventHandler()->ProcessEvent(event)) wxEvtHandler* handler = menu->GetEventHandler();
if (handler && handler->ProcessEvent(event))
return; return;
wxWindow *win = menu->GetInvokingWindow(); wxWindow *win = menu->GetInvokingWindow();
@@ -769,7 +779,8 @@ static void gtk_menu_nolight_callback( GtkWidget *widget, wxMenu *menu )
wxMenuEvent event( wxEVT_MENU_HIGHLIGHT, -1 ); wxMenuEvent event( wxEVT_MENU_HIGHLIGHT, -1 );
event.SetEventObject( menu ); event.SetEventObject( menu );
if (menu->GetEventHandler()->ProcessEvent(event)) wxEvtHandler* handler = menu->GetEventHandler();
if (handler && handler->ProcessEvent(event))
return; return;
wxWindow *win = menu->GetInvokingWindow(); wxWindow *win = menu->GetInvokingWindow();
@@ -861,10 +872,10 @@ wxString wxMenuItemBase::GetLabelFromText(const wxString& text)
// "&" is doubled to indicate "&" instead of accelerator // "&" is doubled to indicate "&" instead of accelerator
continue; continue;
} }
label += *pc; label += *pc;
} }
// wxPrintf( L"text %s label %s\n", text.c_str(), label.c_str() ); // wxPrintf( L"text %s label %s\n", text.c_str(), label.c_str() );
return label; return label;
@@ -879,7 +890,7 @@ void wxMenuItem::SetText( const wxString& str )
wxString label1 = wxStripMenuCodes(str.BeforeFirst('\t')); wxString label1 = wxStripMenuCodes(str.BeforeFirst('\t'));
if (oldLabel == label1) if (oldLabel == label1)
return; return;
DoSetText(str); DoSetText(str);
if (m_menuItem) if (m_menuItem)
@@ -898,7 +909,7 @@ void wxMenuItem::SetText( const wxString& str )
if (m_text[n] != wxT('\\')) if (m_text[n] != wxT('\\'))
tmp += m_text[n]; tmp += m_text[n];
} }
gtk_label_set_text_with_mnemonic( GTK_LABEL(label), wxGTK_CONV(tmp) ); gtk_label_set_text_with_mnemonic( GTK_LABEL(label), wxGTK_CONV(tmp) );
#else #else
// set new text // set new text
@@ -958,9 +969,9 @@ void wxMenuItem::DoSetText( const wxString& str )
} }
++pc; ++pc;
} }
// wxPrintf( L"str %s m_text %s\n", str.c_str(), m_text.c_str() ); // wxPrintf( L"str %s m_text %s\n", str.c_str(), m_text.c_str() );
m_hotKey = wxT(""); m_hotKey = wxT("");
if(*pc == wxT('\t')) if(*pc == wxT('\t'))
@@ -1190,7 +1201,7 @@ bool wxMenu::GtkAppend(wxMenuItem *mitem)
gtk_signal_connect( GTK_OBJECT(menuItem), "activate", gtk_signal_connect( GTK_OBJECT(menuItem), "activate",
GTK_SIGNAL_FUNC(gtk_menu_clicked_callback), GTK_SIGNAL_FUNC(gtk_menu_clicked_callback),
(gpointer)this ); (gpointer)this );
gtk_menu_append( GTK_MENU(m_menu), menuItem ); gtk_menu_append( GTK_MENU(m_menu), menuItem );
gtk_widget_show( menuItem ); gtk_widget_show( menuItem );
@@ -1276,7 +1287,7 @@ bool wxMenu::GtkAppend(wxMenuItem *mitem)
wxString path( mitem->GetFactoryPath() ); wxString path( mitem->GetFactoryPath() );
menuItem = gtk_item_factory_get_widget( m_factory, wxGTK_CONV( path ) ); menuItem = gtk_item_factory_get_widget( m_factory, wxGTK_CONV( path ) );
if (!menuItem) if (!menuItem)
wxLogError( wxT("Wrong menu path: %s\n"), path.c_str() ); wxLogError( wxT("Wrong menu path: %s\n"), path.c_str() );
} }
@@ -1284,7 +1295,7 @@ bool wxMenu::GtkAppend(wxMenuItem *mitem)
if ( !mitem->IsSeparator() ) if ( !mitem->IsSeparator() )
{ {
wxASSERT_MSG( menuItem, wxT("invalid menuitem") ); wxASSERT_MSG( menuItem, wxT("invalid menuitem") );
gtk_signal_connect( GTK_OBJECT(menuItem), "select", gtk_signal_connect( GTK_OBJECT(menuItem), "select",
GTK_SIGNAL_FUNC(gtk_menu_hilight_callback), GTK_SIGNAL_FUNC(gtk_menu_hilight_callback),
(gpointer)this ); (gpointer)this );

View File

@@ -2348,21 +2348,13 @@ void wxApp::MacHandleOSEvent( WXEVENTREF evr )
void wxApp::MacHandleMenuCommand( wxUint32 id ) void wxApp::MacHandleMenuCommand( wxUint32 id )
{ {
wxMenuBar* mbar = wxMenuBar::MacGetInstalledMenuBar() ; wxMenuBar* mbar = wxMenuBar::MacGetInstalledMenuBar() ;
wxMenu* menu = NULL ; wxFrame* frame = mbar->GetFrame();
wxMenuItem* item = NULL ; wxCHECK_RET( mbar != NULL && frame != NULL, wxT("error in menu item callback") );
if ( mbar ) if ( frame )
{ {
item = mbar->FindItem( id , &menu ) ; frame->ProcessCommand(id);
} }
wxCHECK_RET( item != NULL && menu != NULL && mbar != NULL, wxT("error in menu item callback") );
if (item->IsCheckable())
{
item->Check( !item->IsChecked() ) ;
}
menu->SendEvent( id , item->IsCheckable() ? item->IsChecked() : -1 ) ;
} }
#if !TARGET_CARBON #if !TARGET_CARBON

View File

@@ -2348,21 +2348,13 @@ void wxApp::MacHandleOSEvent( WXEVENTREF evr )
void wxApp::MacHandleMenuCommand( wxUint32 id ) void wxApp::MacHandleMenuCommand( wxUint32 id )
{ {
wxMenuBar* mbar = wxMenuBar::MacGetInstalledMenuBar() ; wxMenuBar* mbar = wxMenuBar::MacGetInstalledMenuBar() ;
wxMenu* menu = NULL ; wxFrame* frame = mbar->GetFrame();
wxMenuItem* item = NULL ; wxCHECK_RET( mbar != NULL && frame != NULL, wxT("error in menu item callback") );
if ( mbar ) if ( frame )
{ {
item = mbar->FindItem( id , &menu ) ; frame->ProcessCommand(id);
} }
wxCHECK_RET( item != NULL && menu != NULL && mbar != NULL, wxT("error in menu item callback") );
if (item->IsCheckable())
{
item->Check( !item->IsChecked() ) ;
}
menu->SendEvent( id , item->IsCheckable() ? item->IsChecked() : -1 ) ;
} }
#if !TARGET_CARBON #if !TARGET_CARBON