X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/1e6feb95a79834836e88143b15d9f424ebe79621..ef41d80cdfa98391d8530c4b142c9816e0519e68:/src/msw/menu.cpp?ds=inline diff --git a/src/msw/menu.cpp b/src/msw/menu.cpp index e15df2c855..eb78604c7d 100644 --- a/src/msw/menu.cpp +++ b/src/msw/menu.cpp @@ -61,16 +61,31 @@ extern wxMenu *wxCurrentPopupMenu; static const int idMenuTitle = -2; // ---------------------------------------------------------------------------- -// macros +// private functions // ---------------------------------------------------------------------------- -IMPLEMENT_DYNAMIC_CLASS(wxMenu, wxEvtHandler) -IMPLEMENT_DYNAMIC_CLASS(wxMenuBar, wxWindow) +// make the given menu item default +static void SetDefaultMenuItem(HMENU hmenu, UINT id) +{ + MENUITEMINFO mii; + wxZeroMemory(mii); + mii.cbSize = sizeof(MENUITEMINFO); + mii.fMask = MIIM_STATE; + mii.fState = MFS_DEFAULT; + + if ( !::SetMenuItemInfo(hmenu, id, FALSE, &mii) ) + { + wxLogLastError(wxT("SetMenuItemInfo")); + } +} // ============================================================================ // implementation // ============================================================================ +IMPLEMENT_DYNAMIC_CLASS(wxMenu, wxEvtHandler) +IMPLEMENT_DYNAMIC_CLASS(wxMenuBar, wxWindow) + // --------------------------------------------------------------------------- // wxMenu construction, adding and removing menu items // --------------------------------------------------------------------------- @@ -79,6 +94,7 @@ IMPLEMENT_DYNAMIC_CLASS(wxMenuBar, wxWindow) void wxMenu::Init() { m_doBreak = FALSE; + m_startRadioGroup = -1; // create the menu m_hMenu = (WXHMENU)CreatePopupMenu(); @@ -121,6 +137,13 @@ void wxMenu::Break() m_doBreak = TRUE; } +void wxMenu::Attach(wxMenuBarBase *menubar) +{ + wxMenuBase::Attach(menubar); + + EndRadioGroup(); +} + #if wxUSE_ACCEL int wxMenu::FindAccel(int id) const @@ -172,7 +195,7 @@ void wxMenu::UpdateAccel(wxMenuItem *item) if ( accel ) m_accels[n] = accel; else - m_accels.Remove(n); + m_accels.RemoveAt(n); } if ( IsAttached() ) @@ -255,38 +278,86 @@ bool wxMenu::DoInsertOrAppend(wxMenuItem *pItem, size_t pos) return FALSE; } - else - { - // if we just appended the title, highlight it + + // if we just appended the title, highlight it #ifdef __WIN32__ - if ( (int)id == idMenuTitle ) + if ( (int)id == idMenuTitle ) + { + // visually select the menu title + SetDefaultMenuItem(GetHmenu(), id); + } +#endif // __WIN32__ + + // if we're already attached to the menubar, we must update it + if ( IsAttached() && m_menuBar->IsAttached() ) + { + m_menuBar->Refresh(); + } + + return TRUE; +} + +void wxMenu::EndRadioGroup() +{ + // we're not inside a radio group any longer + m_startRadioGroup = -1; +} + +bool wxMenu::DoAppend(wxMenuItem *item) +{ + wxCHECK_MSG( item, FALSE, _T("NULL item in wxMenu::DoAppend") ); + + bool check = FALSE; + + if ( item->GetKind() == wxITEM_RADIO ) + { + int count = GetMenuItemCount(); + + if ( m_startRadioGroup == -1 ) { - // visually select the menu title - MENUITEMINFO mii; - mii.cbSize = sizeof(mii); - mii.fMask = MIIM_STATE; - mii.fState = MFS_DEFAULT; + // start a new radio group + m_startRadioGroup = count; - if ( !SetMenuItemInfo(GetHmenu(), (unsigned)id, FALSE, &mii) ) + // 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::Node *node = GetMenuItems().Item(m_startRadioGroup); + + if ( node ) { - wxLogLastError(wxT("SetMenuItemInfo")); + node->GetData()->SetRadioGroupEnd(count); + } + else + { + wxFAIL_MSG( _T("where is the radio group start item?") ); } } -#endif // __WIN32__ + } + else // not a radio item + { + EndRadioGroup(); + } - // if we're already attached to the menubar, we must update it - if ( IsAttached() && m_menuBar->IsAttached() ) - { - m_menuBar->Refresh(); - } + if ( !wxMenuBase::DoAppend(item) || !DoInsertOrAppend(item) ) + { + return FALSE; + } - return TRUE; + if ( check ) + { + // check the item initially + item->Check(TRUE); } -} -bool wxMenu::DoAppend(wxMenuItem *item) -{ - return wxMenuBase::DoAppend(item) && DoInsertOrAppend(item); + return TRUE; } bool wxMenu::DoInsert(size_t pos, wxMenuItem *item) @@ -317,7 +388,7 @@ wxMenuItem *wxMenu::DoRemove(wxMenuItem *item) { delete m_accels[n]; - m_accels.Remove(n); + m_accels.RemoveAt(n); } //else: this item doesn't have an accel, nothing to do #endif // wxUSE_ACCEL @@ -409,15 +480,7 @@ void wxMenu::SetTitle(const wxString& label) // put the title string in bold face if ( !m_title.IsEmpty() ) { - MENUITEMINFO mii; - mii.cbSize = sizeof(mii); - mii.fMask = MIIM_STATE; - mii.fState = MFS_DEFAULT; - - if ( !SetMenuItemInfo(hMenu, (unsigned)idMenuTitle, FALSE, &mii) ) - { - wxLogLastError(wxT("SetMenuItemInfo")); - } + SetDefaultMenuItem(GetHmenu(), (UINT)idMenuTitle); } #endif // Win32 } @@ -610,7 +673,8 @@ wxMenu *wxMenuBar::Replace(size_t pos, wxMenu *menu, const wxString& title) { wxMenu *menuOld = wxMenuBarBase::Replace(pos, menu, title); if ( !menuOld ) - return FALSE; + return NULL; + m_titles[pos] = title; if ( IsAttached() ) @@ -649,8 +713,6 @@ bool wxMenuBar::Insert(size_t pos, wxMenu *menu, const wxString& title) m_titles.Insert(title, pos); - menu->Attach(this); - if ( IsAttached() ) { if ( !::InsertMenu(GetHmenu(), pos, @@ -682,8 +744,6 @@ bool wxMenuBar::Append(wxMenu *menu, const wxString& title) if ( !wxMenuBarBase::Append(menu, title) ) return FALSE; - menu->Attach(this); - m_titles.Add(title); if ( IsAttached() ) @@ -721,8 +781,6 @@ wxMenu *wxMenuBar::Remove(size_t pos) wxLogLastError(wxT("RemoveMenu")); } - menu->Detach(); - #if wxUSE_ACCEL if ( menu->HasAccels() ) { @@ -773,8 +831,6 @@ void wxMenuBar::Attach(wxFrame *frame) { wxMenuBarBase::Attach(frame); - m_menuBarFrame = frame; - #if wxUSE_ACCEL RebuildAccelTable(); #endif // wxUSE_ACCEL