📄 menu.cpp
字号:
*/
wxMenuBar* wxMenuBar::s_macInstalledMenuBar = NULL ;
wxMenuBar* wxMenuBar::s_macCommonMenuBar = NULL ;
void wxMenuBar::Init()
{
m_eventHandler = this;
m_menuBarFrame = NULL;
m_invokingWindow = (wxWindow*) NULL;
}
wxMenuBar::wxMenuBar()
{
Init();
}
wxMenuBar::wxMenuBar( long WXUNUSED(style) )
{
Init();
}
wxMenuBar::wxMenuBar(size_t count, wxMenu *menus[], const wxString titles[], long WXUNUSED(style))
{
Init();
m_titles.Alloc(count);
for ( size_t i = 0; i < count; i++ )
{
m_menus.Append(menus[i]);
m_titles.Add(titles[i]);
menus[i]->Attach(this);
}
}
wxMenuBar::~wxMenuBar()
{
if (s_macCommonMenuBar == this)
s_macCommonMenuBar = NULL;
if (s_macInstalledMenuBar == this)
{
::ClearMenuBar();
s_macInstalledMenuBar = NULL;
}
}
void wxMenuBar::Refresh(bool WXUNUSED(eraseBackground), const wxRect *WXUNUSED(rect))
{
wxCHECK_RET( IsAttached(), wxT("can't refresh unatteched menubar") );
DrawMenuBar();
}
void wxMenuBar::MacInstallMenuBar()
{
if ( s_macInstalledMenuBar == this )
return ;
wxStAppResource resload ;
Handle menubar = ::GetNewMBar( kwxMacMenuBarResource ) ;
wxString message ;
wxCHECK_RET( menubar != NULL, wxT("can't read MBAR resource") );
::SetMenuBar( menubar ) ;
#if TARGET_API_MAC_CARBON
::DisposeMenuBar( menubar ) ;
#else
::DisposeHandle( menubar ) ;
#endif
#if TARGET_API_MAC_OS8
MenuHandle menu = ::GetMenuHandle( kwxMacAppleMenuId ) ;
if ( CountMenuItems( menu ) == 2 )
{
::AppendResMenu(menu, 'DRVR');
}
#endif
// clean-up the help menu before adding new items
MenuHandle mh = NULL ;
if ( UMAGetHelpMenu( &mh , &firstUserHelpMenuItem) == noErr )
{
for ( int i = CountMenuItems( mh ) ; i >= firstUserHelpMenuItem ; --i )
{
DeleteMenuItem( mh , i ) ;
}
}
else
{
mh = NULL ;
}
#if TARGET_CARBON
if ( UMAGetSystemVersion() >= 0x1000 && wxApp::s_macPreferencesMenuItemId)
{
wxMenuItem *item = FindItem( wxApp::s_macPreferencesMenuItemId , NULL ) ;
if ( item == NULL || !(item->IsEnabled()) )
DisableMenuCommand( NULL , kHICommandPreferences ) ;
else
EnableMenuCommand( NULL , kHICommandPreferences ) ;
}
#endif
for (size_t i = 0; i < m_menus.GetCount(); i++)
{
wxMenuItemList::Node *node;
wxMenuItem *item;
int pos ;
wxMenu* menu = m_menus[i] , *subMenu = NULL ;
if( m_titles[i] == wxT("?") || m_titles[i] == wxT("&?") || m_titles[i] == wxApp::s_macHelpMenuTitleName )
{
if ( mh == NULL )
{
continue ;
}
for (pos = 0 , node = menu->GetMenuItems().GetFirst(); node; node = node->GetNext(), pos++)
{
item = (wxMenuItem *)node->GetData();
subMenu = item->GetSubMenu() ;
if (subMenu)
{
// we don't support hierarchical menus in the help menu yet
}
else
{
if ( item->IsSeparator() )
{
if ( mh )
MacAppendMenu(mh, "\p-" );
}
else
{
wxAcceleratorEntry* entry = wxGetAccelFromString( item->GetText() ) ;
if ( item->GetId() == wxApp::s_macAboutMenuItemId )
{
UMASetMenuItemText( GetMenuHandle( kwxMacAppleMenuId ) , 1 , item->GetText() , wxFont::GetDefaultEncoding() );
UMAEnableMenuItem( GetMenuHandle( kwxMacAppleMenuId ) , 1 , true );
SetMenuItemCommandID( GetMenuHandle( kwxMacAppleMenuId ) , 1 , item->GetId() ) ;
UMASetMenuItemShortcut( GetMenuHandle( kwxMacAppleMenuId ) , 1 , entry ) ;
}
else
{
if ( mh )
{
UMAAppendMenuItem(mh, item->GetText() , wxFont::GetDefaultEncoding(), entry);
SetMenuItemCommandID( mh , CountMenuItems(mh) , item->GetId() ) ;
}
}
delete entry ;
}
}
}
}
else
{
UMASetMenuTitle( MAC_WXHMENU(menu->GetHMenu()) , m_titles[i], m_font.GetEncoding() ) ;
m_menus[i]->MacBeforeDisplay(false) ;
::InsertMenu(MAC_WXHMENU(m_menus[i]->GetHMenu()), 0);
}
}
::DrawMenuBar() ;
s_macInstalledMenuBar = this;
}
void wxMenuBar::EnableTop(size_t pos, bool enable)
{
wxCHECK_RET( IsAttached(), wxT("doesn't work with unattached menubars") );
m_menus[pos]->MacEnableMenu( enable ) ;
Refresh();
}
void wxMenuBar::SetLabelTop(size_t pos, const wxString& label)
{
wxCHECK_RET( pos < GetMenuCount(), wxT("invalid menu index") );
m_titles[pos] = label;
if ( !IsAttached() )
{
return;
}
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
// ---------------------------------------------------------------------------
// ---------------------------------------------------------------------------
// 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()) , m_menus[pos+1]->MacGetMenuId() ) ;
}
}
}
Refresh();
}
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()) , m_menus[pos+1]->MacGetMenuId() ) ;
}
}
Refresh();
}
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)
{
::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::Node *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 );
wxMenuItemList::Node *node = menu->GetMenuItems().GetFirst();
while (node)
{
wxMenuItem *menuitem = node->GetData();
if (menuitem->IsSubMenu())
wxMenubarSetInvokingWindow( menuitem->GetSubMenu() , win );
node = node->GetNext();
}
}
void wxMenuBar::UnsetInvokingWindow()
{
m_invokingWindow = (wxWindow*) NULL;
wxMenuList::Node *node = m_menus.GetFirst();
while (node)
{
wxMenu *menu = node->GetData();
wxMenubarUnsetInvokingWindow( menu );
node = node->GetNext();
}
}
void wxMenuBar::SetInvokingWindow(wxFrame *frame)
{
m_invokingWindow = frame;
wxMenuList::Node *node = m_menus.GetFirst();
while (node)
{
wxMenu *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 ( menuString == title )
return 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 = m_menus[i]->FindItem(id, itemMenu);
}
return item;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -