menu.cpp
来自「A*算法 A*算法 A*算法 A*算法A*算法A*算法」· C++ 代码 · 共 1,349 行 · 第 1/3 页
CPP
1,349 行
mii.hbmpItem = HBMMENU_CALLBACK;
ok = ::InsertMenuItem(GetHmenu(), pos, TRUE /* by pos */, &mii);
if ( !ok )
{
wxLogLastError(wxT("InsertMenuItem()"));
}
else // InsertMenuItem() ok
{
// we need to remove the extra indent which is reserved for
// the checkboxes by default as it looks ugly unless check
// boxes are used together with bitmaps and this is not the
// case in wx API
WinStruct<MENUINFO> mi;
// don't call SetMenuInfo() directly, this would prevent
// the app from starting up under Windows 95/NT 4
typedef BOOL (WINAPI *SetMenuInfo_t)(HMENU, MENUINFO *);
wxDynamicLibrary dllUser(_T("user32"));
wxDYNLIB_FUNCTION(SetMenuInfo_t, SetMenuInfo, dllUser);
if ( pfnSetMenuInfo )
{
mi.fMask = MIM_STYLE;
mi.dwStyle = MNS_CHECKORBMP;
if ( !(*pfnSetMenuInfo)(GetHmenu(), &mi) )
wxLogLastError(_T("SetMenuInfo(MNS_NOCHECK)"));
}
// tell the item that it's not really owner-drawn but only
// needs to draw its bitmap, the rest is done by Windows
pItem->ResetOwnerDrawn();
}
}
#endif // MIIM_BITMAP
}
if ( !ok )
{
// item draws itself, pass pointer to it in data parameter
flags |= MF_OWNERDRAW;
pData = (LPCTSTR)pItem;
}
}
else
#endif // wxUSE_OWNER_DRAWN
{
// menu is just a normal string (passed in data parameter)
flags |= MF_STRING;
#ifdef __WXWINCE__
itemText = wxMenuItem::GetLabelFromText(itemText);
#endif
pData = (wxChar*)itemText.c_str();
}
// item might have been already inserted by InsertMenuItem() above
if ( !ok )
{
if ( !::InsertMenu(GetHmenu(), pos, flags | MF_BYPOSITION, id, pData) )
{
wxLogLastError(wxT("InsertMenu[Item]()"));
return false;
}
}
// if we just appended the title, highlight it
if ( (int)id == idMenuTitle )
{
// visually select the menu title
SetDefaultMenuItem(GetHmenu(), id);
}
// if we're already attached to the menubar, we must update it
if ( IsAttached() && GetMenuBar()->IsAttached() )
{
GetMenuBar()->Refresh();
}
return true;
}
void wxMenu::EndRadioGroup()
{
// we're not inside a radio group any longer
m_startRadioGroup = -1;
}
wxMenuItem* wxMenu::DoAppend(wxMenuItem *item)
{
wxCHECK_MSG( item, NULL, _T("NULL item in wxMenu::DoAppend") );
bool check = false;
if ( item->GetKind() == wxITEM_RADIO )
{
int count = GetMenuItemCount();
if ( m_startRadioGroup == -1 )
{
// start a new radio group
m_startRadioGroup = count;
// for now it has just one element
item->SetAsRadioGroupStart();
item->SetRadioGroupEnd(m_startRadioGroup);
// ensure that we have a checked item in the radio group
check = true;
}
else // extend the current radio group
{
// we need to update its end item
item->SetRadioGroupStart(m_startRadioGroup);
wxMenuItemList::compatibility_iterator node = GetMenuItems().Item(m_startRadioGroup);
if ( node )
{
node->GetData()->SetRadioGroupEnd(count);
}
else
{
wxFAIL_MSG( _T("where is the radio group start item?") );
}
}
}
else // not a radio item
{
EndRadioGroup();
}
if ( !wxMenuBase::DoAppend(item) || !DoInsertOrAppend(item) )
{
return NULL;
}
if ( check )
{
// check the item initially
item->Check(true);
}
return item;
}
wxMenuItem* wxMenu::DoInsert(size_t pos, wxMenuItem *item)
{
if (wxMenuBase::DoInsert(pos, item) && DoInsertOrAppend(item, pos))
return item;
else
return NULL;
}
wxMenuItem *wxMenu::DoRemove(wxMenuItem *item)
{
// we need to find the items position in the child list
size_t pos;
wxMenuItemList::compatibility_iterator node = GetMenuItems().GetFirst();
for ( pos = 0; node; pos++ )
{
if ( node->GetData() == item )
break;
node = node->GetNext();
}
// DoRemove() (unlike Remove) can only be called for existing item!
wxCHECK_MSG( node, NULL, wxT("bug in wxMenu::Remove logic") );
#if wxUSE_ACCEL
// remove the corresponding accel from the accel table
int n = FindAccel(item->GetId());
if ( n != wxNOT_FOUND )
{
delete m_accels[n];
m_accels.RemoveAt(n);
}
//else: this item doesn't have an accel, nothing to do
#endif // wxUSE_ACCEL
// remove the item from the menu
if ( !::RemoveMenu(GetHmenu(), (UINT)pos, MF_BYPOSITION) )
{
wxLogLastError(wxT("RemoveMenu"));
}
if ( IsAttached() && GetMenuBar()->IsAttached() )
{
// otherwise, the chane won't be visible
GetMenuBar()->Refresh();
}
// and from internal data structures
return wxMenuBase::DoRemove(item);
}
// ---------------------------------------------------------------------------
// accelerator helpers
// ---------------------------------------------------------------------------
#if wxUSE_ACCEL
// create the wxAcceleratorEntries for our accels and put them into provided
// array - return the number of accels we have
size_t wxMenu::CopyAccels(wxAcceleratorEntry *accels) const
{
size_t count = GetAccelCount();
for ( size_t n = 0; n < count; n++ )
{
*accels++ = *m_accels[n];
}
return count;
}
#endif // wxUSE_ACCEL
// ---------------------------------------------------------------------------
// set wxMenu title
// ---------------------------------------------------------------------------
void wxMenu::SetTitle(const wxString& label)
{
bool hasNoTitle = m_title.empty();
m_title = label;
HMENU hMenu = GetHmenu();
if ( hasNoTitle )
{
if ( !label.empty() )
{
if ( !::InsertMenu(hMenu, 0u, MF_BYPOSITION | MF_STRING,
(unsigned)idMenuTitle, m_title) ||
!::InsertMenu(hMenu, 1u, MF_BYPOSITION, (unsigned)-1, NULL) )
{
wxLogLastError(wxT("InsertMenu"));
}
}
}
else
{
if ( label.empty() )
{
// remove the title and the separator after it
if ( !RemoveMenu(hMenu, 0, MF_BYPOSITION) ||
!RemoveMenu(hMenu, 0, MF_BYPOSITION) )
{
wxLogLastError(wxT("RemoveMenu"));
}
}
else
{
// modify the title
#ifdef __WXWINCE__
MENUITEMINFO info;
wxZeroMemory(info);
info.cbSize = sizeof(info);
info.fMask = MIIM_TYPE;
info.fType = MFT_STRING;
info.cch = m_title.Length();
info.dwTypeData = (LPTSTR) m_title.c_str();
if ( !SetMenuItemInfo(hMenu, 0, TRUE, & info) )
{
wxLogLastError(wxT("SetMenuItemInfo"));
}
#else
if ( !ModifyMenu(hMenu, 0u,
MF_BYPOSITION | MF_STRING,
(unsigned)idMenuTitle, m_title) )
{
wxLogLastError(wxT("ModifyMenu"));
}
#endif
}
}
#ifdef __WIN32__
// put the title string in bold face
if ( !m_title.empty() )
{
SetDefaultMenuItem(GetHmenu(), (UINT)idMenuTitle);
}
#endif // Win32
}
// ---------------------------------------------------------------------------
// event processing
// ---------------------------------------------------------------------------
bool wxMenu::MSWCommand(WXUINT WXUNUSED(param), WXWORD id)
{
// ignore commands from the menu title
if ( id != (WXWORD)idMenuTitle )
{
// get the checked status of the command: notice that menuState is the
// old state of the menu, so the test for MF_CHECKED must be inverted
UINT menuState = ::GetMenuState(GetHmenu(), id, MF_BYCOMMAND);
SendEvent(id, !(menuState & MF_CHECKED));
}
return true;
}
// ---------------------------------------------------------------------------
// other
// ---------------------------------------------------------------------------
wxWindow *wxMenu::GetWindow() const
{
if ( m_invokingWindow != NULL )
return m_invokingWindow;
else if ( GetMenuBar() != NULL)
return GetMenuBar()->GetFrame();
return NULL;
}
// ---------------------------------------------------------------------------
// Menu Bar
// ---------------------------------------------------------------------------
void wxMenuBar::Init()
{
m_eventHandler = this;
m_hMenu = 0;
#if wxUSE_TOOLBAR && defined(__WXWINCE__)
m_toolBar = NULL;
#endif
// Not using a combined wxToolBar/wxMenuBar? then use
// a commandbar in WinCE .NET just to implement the
// menubar.
#if defined(WINCE_WITH_COMMANDBAR)
m_commandBar = NULL;
m_adornmentsAdded = false;
#endif
}
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()
{
// In Windows CE (not .NET), the menubar is always associated
// with a toolbar, which destroys the menu implicitly.
#if defined(WINCE_WITHOUT_COMMANDBAR) && defined(__POCKETPC__)
if (GetToolBar())
{
wxToolMenuBar* toolMenuBar = wxDynamicCast(GetToolBar(), wxToolMenuBar);
if (toolMenuBar)
toolMenuBar->SetMenuBar(NULL);
}
#else
// we should free Windows resources only if Windows doesn't do it for us
// which happens if we're attached to a frame
if (m_hMenu && !IsAttached())
{
#if defined(WINCE_WITH_COMMANDBAR)
::DestroyWindow((HWND) m_commandBar);
m_commandBar = (WXHWND) NULL;
#else
::DestroyMenu((HMENU)m_hMenu);
#endif
m_hMenu = (WXHMENU)NULL;
}
#endif
}
// ---------------------------------------------------------------------------
// wxMenuBar helpers
// ---------------------------------------------------------------------------
void wxMenuBar::Refresh()
{
wxCHECK_RET( IsAttached(), wxT("can't refresh unattached menubar") );
#if defined(WINCE_WITHOUT_COMMANDBAR)
if (GetToolBar())
{
CommandBar_DrawMenuBar((HWND) GetToolBar()->GetHWND(), 0);
}
#elif defined(WINCE_WITH_COMMANDBAR)
if (m_commandBar)
DrawMenuBar((HWND) m_commandBar);
#else
DrawMenuBar(GetHwndOf(GetFrame()));
#endif
}
WXHMENU wxMenuBar::Create()
{
// Note: this totally doesn't work on Smartphone,
// since you have to use resources.
// We'll have to find another way to add a menu
// by changing/adding menu items to an existing menu.
#if defined(WINCE_WITHOUT_COMMANDBAR)
if ( m_hMenu != 0 )
return m_hMenu;
if (!GetToolBar())
return 0;
HWND hCommandBar = (HWND) GetToolBar()->GetHWND();
HMENU hMenu = (HMENU)::SendMessage(hCommandBar, SHCMBM_GETMENU, (WPARAM)0, (LPARAM)0);
// hMenu may be zero on Windows Mobile 5. So add the menus anyway.
if (1) // (hMenu)
{
TBBUTTON tbButton;
memset(&tbButton, 0, sizeof(TBBUTTON));
tbButton.iBitmap = I_IMAGENONE;
tbButton.fsState = TBSTATE_ENABLED;
tbButton.fsStyle = TBSTYLE_DROPDOWN | TBSTYLE_NO_DROPDOWN_ARROW | TBSTYLE_AUTOSIZE;
size_t i;
for (i = 0; i < GetMenuCount(); i++)
{
HMENU hPopupMenu = (HMENU) GetMenu(i)->GetHMenu() ;
tbButton.dwData = (DWORD)hPopupMenu;
wxString label = wxStripMenuCodes(GetLabelTop(i));
tbButton.iString = (int) label.c_str();
int position = i;
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?