📄 menu.cpp
字号:
if ( item->IsSeparator() )
{
if ( mh )
MacAppendMenu(mh, "\p-" );
}
else
{
wxAcceleratorEntry* entry = wxGetAccelFromString( item->GetText() ) ;
if ( item->GetId() == wxApp::s_macAboutMenuItemId )
{
// this will be taken care of below
}
else
{
if ( mh )
{
UMAAppendMenuItem(mh, wxStripMenuCodes(item->GetText()) , wxFont::GetDefaultEncoding(), entry);
SetMenuItemCommandID( mh , CountMenuItems(mh) , wxIdToMacCommand ( item->GetId() ) ) ;
SetMenuItemRefCon( mh , CountMenuItems(mh) , (UInt32)item ) ;
}
}
delete entry ;
}
}
}
}
else
{
UMASetMenuTitle( MAC_WXHMENU(menu->GetHMenu()) , m_titles[i], m_font.GetEncoding() ) ;
menu->MacBeforeDisplay(false) ;
::InsertMenu(MAC_WXHMENU(_wxMenuAt(m_menus, i)->GetHMenu()), 0);
}
}
// take care of the about menu item wherever it is
{
wxMenu* aboutMenu ;
wxMenuItem *aboutMenuItem = FindItem(wxApp::s_macAboutMenuItemId , &aboutMenu) ;
if ( aboutMenuItem )
{
wxAcceleratorEntry* entry = wxGetAccelFromString( aboutMenuItem->GetText() ) ;
UMASetMenuItemText( GetMenuHandle( kwxMacAppleMenuId ) , 1 , wxStripMenuCodes ( aboutMenuItem->GetText() ) , wxFont::GetDefaultEncoding() );
UMAEnableMenuItem( GetMenuHandle( kwxMacAppleMenuId ) , 1 , true );
SetMenuItemCommandID( GetMenuHandle( kwxMacAppleMenuId ) , 1 , kHICommandAbout ) ;
SetMenuItemRefCon(GetMenuHandle( kwxMacAppleMenuId ) , 1 , (UInt32)aboutMenuItem ) ;
UMASetMenuItemShortcut( GetMenuHandle( kwxMacAppleMenuId ) , 1 , entry ) ;
}
}
if ( GetAutoWindowMenu() )
{
if ( MacGetWindowMenuHMenu() == NULL )
CreateStandardWindowMenu( 0 , (MenuHandle*) &s_macWindowMenuHandle ) ;
InsertMenu( (MenuHandle) MacGetWindowMenuHMenu() , 0 ) ;
}
::DrawMenuBar() ;
s_macInstalledMenuBar = this;
}
void wxMenuBar::EnableTop(size_t pos, bool enable)
{
wxCHECK_RET( IsAttached(), wxT("doesn't work with unattached menubars") );
_wxMenuAt(m_menus, pos)->MacEnableMenu( enable ) ;
Refresh();
}
bool wxMenuBar::Enable(bool enable)
{
wxCHECK_MSG( IsAttached(), false, wxT("doesn't work with unattached menubars") );
size_t i;
for (i = 0; i < GetMenuCount(); i++)
EnableTop(i, enable);
return true;
}
void wxMenuBar::SetLabelTop(size_t pos, const wxString& label)
{
wxCHECK_RET( pos < GetMenuCount(), wxT("invalid menu index") );
m_titles[pos] = label;
if ( !IsAttached() )
return;
_wxMenuAt(m_menus, pos)->SetTitle( label ) ;
if (wxMenuBar::s_macInstalledMenuBar == this) // are we currently installed ?
{
::SetMenuBar( GetMenuBar() ) ;
::InvalMenuBar() ;
}
}
wxString wxMenuBar::GetLabelTop(size_t pos) const
{
wxCHECK_MSG( pos < GetMenuCount(), wxEmptyString,
wxT("invalid menu index in wxMenuBar::GetLabelTop") );
return m_titles[pos];
}
int wxMenuBar::FindMenu(const wxString& title)
{
wxString menuTitle = wxStripMenuCodes(title);
size_t count = GetMenuCount();
for ( size_t i = 0; i < count; i++ )
{
wxString title = wxStripMenuCodes(m_titles[i]);
if ( menuTitle == title )
return i;
}
return wxNOT_FOUND;
}
// ---------------------------------------------------------------------------
// wxMenuBar construction
// ---------------------------------------------------------------------------
wxMenu *wxMenuBar::Replace(size_t pos, wxMenu *menu, const wxString& title)
{
wxMenu *menuOld = wxMenuBarBase::Replace(pos, menu, title);
if ( !menuOld )
return NULL;
m_titles[pos] = title;
if ( IsAttached() )
{
if (s_macInstalledMenuBar == this)
{
menuOld->MacAfterDisplay( false ) ;
::DeleteMenu( menuOld->MacGetMenuId() /* m_menus[pos]->MacGetMenuId() */ ) ;
menu->MacBeforeDisplay( false ) ;
UMASetMenuTitle( MAC_WXHMENU(menu->GetHMenu()) , title , m_font.GetEncoding() ) ;
if ( pos == m_menus.GetCount() - 1)
::InsertMenu( MAC_WXHMENU(menu->GetHMenu()) , 0 ) ;
else
::InsertMenu( MAC_WXHMENU(menu->GetHMenu()) , _wxMenuAt(m_menus, pos + 1)->MacGetMenuId() ) ;
}
Refresh();
}
if (m_invokingWindow)
wxMenubarSetInvokingWindow( menu, m_invokingWindow );
return menuOld;
}
bool wxMenuBar::Insert(size_t pos, wxMenu *menu, const wxString& title)
{
if ( !wxMenuBarBase::Insert(pos, menu, title) )
return false;
m_titles.Insert(title, pos);
UMASetMenuTitle( MAC_WXHMENU(menu->GetHMenu()) , title , m_font.GetEncoding() ) ;
if ( IsAttached() && s_macInstalledMenuBar == this )
{
if (s_macInstalledMenuBar == this)
{
menu->MacBeforeDisplay( false ) ;
if ( pos == (size_t) -1 || pos + 1 == m_menus.GetCount() )
::InsertMenu( MAC_WXHMENU(menu->GetHMenu()) , 0 ) ;
else
::InsertMenu( MAC_WXHMENU(menu->GetHMenu()) , _wxMenuAt(m_menus, pos+1)->MacGetMenuId() ) ;
}
Refresh();
}
if (m_invokingWindow)
wxMenubarSetInvokingWindow( menu, m_invokingWindow );
return true;
}
wxMenu *wxMenuBar::Remove(size_t pos)
{
wxMenu *menu = wxMenuBarBase::Remove(pos);
if ( !menu )
return NULL;
if ( IsAttached() )
{
if (s_macInstalledMenuBar == this)
::DeleteMenu( menu->MacGetMenuId() /* m_menus[pos]->MacGetMenuId() */ ) ;
Refresh();
}
m_titles.RemoveAt(pos);
return menu;
}
bool wxMenuBar::Append(wxMenu *menu, const wxString& title)
{
WXHMENU submenu = menu ? menu->GetHMenu() : 0;
wxCHECK_MSG( submenu, false, wxT("can't append invalid menu to menubar") );
if ( !wxMenuBarBase::Append(menu, title) )
return false;
m_titles.Add(title);
UMASetMenuTitle( MAC_WXHMENU(menu->GetHMenu()) , title , m_font.GetEncoding() ) ;
if ( IsAttached() )
{
if (s_macInstalledMenuBar == this)
{
menu->MacBeforeDisplay( false ) ;
::InsertMenu( MAC_WXHMENU(menu->GetHMenu()) , 0 ) ;
}
Refresh();
}
// m_invokingWindow is set after wxFrame::SetMenuBar(). This call enables
// adding menu later on.
if (m_invokingWindow)
wxMenubarSetInvokingWindow( menu, m_invokingWindow );
return true;
}
static void wxMenubarUnsetInvokingWindow( wxMenu *menu )
{
menu->SetInvokingWindow( (wxWindow*) NULL );
wxMenuItemList::compatibility_iterator node = menu->GetMenuItems().GetFirst();
while (node)
{
wxMenuItem *menuitem = node->GetData();
if (menuitem->IsSubMenu())
wxMenubarUnsetInvokingWindow( menuitem->GetSubMenu() );
node = node->GetNext();
}
}
static void wxMenubarSetInvokingWindow( wxMenu *menu, wxWindow *win )
{
menu->SetInvokingWindow( win );
wxMenuItem *menuitem;
wxMenuItemList::compatibility_iterator node = menu->GetMenuItems().GetFirst();
while (node)
{
menuitem = node->GetData();
if (menuitem->IsSubMenu())
wxMenubarSetInvokingWindow( menuitem->GetSubMenu() , win );
node = node->GetNext();
}
}
void wxMenuBar::UnsetInvokingWindow()
{
m_invokingWindow = (wxWindow*) NULL;
wxMenu *menu;
wxMenuList::compatibility_iterator node = m_menus.GetFirst();
while (node)
{
menu = node->GetData();
wxMenubarUnsetInvokingWindow( menu );
node = node->GetNext();
}
}
void wxMenuBar::SetInvokingWindow(wxFrame *frame)
{
m_invokingWindow = frame;
wxMenu *menu;
wxMenuList::compatibility_iterator node = m_menus.GetFirst();
while (node)
{
menu = node->GetData();
wxMenubarSetInvokingWindow( menu, frame );
node = node->GetNext();
}
}
void wxMenuBar::Detach()
{
wxMenuBarBase::Detach() ;
}
void wxMenuBar::Attach(wxFrame *frame)
{
wxMenuBarBase::Attach( frame ) ;
}
// ---------------------------------------------------------------------------
// wxMenuBar searching for menu items
// ---------------------------------------------------------------------------
// Find the itemString in menuString, and return the item id or wxNOT_FOUND
int wxMenuBar::FindMenuItem(const wxString& menuString,
const wxString& itemString) const
{
wxString menuLabel = wxStripMenuCodes(menuString);
size_t count = GetMenuCount();
for ( size_t i = 0; i < count; i++ )
{
wxString title = wxStripMenuCodes(m_titles[i]);
if ( menuLabel == title )
return _wxMenuAt(m_menus, i)->FindItem(itemString);
}
return wxNOT_FOUND;
}
wxMenuItem *wxMenuBar::FindItem(int id, wxMenu **itemMenu) const
{
if ( itemMenu )
*itemMenu = NULL;
wxMenuItem *item = NULL;
size_t count = GetMenuCount();
for ( size_t i = 0; !item && (i < count); i++ )
item = _wxMenuAt(m_menus, i)->FindItem(id, itemMenu);
return item;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -