From 66ed21d69b7904c61d60eb3c9cc6bf04c78658a5 Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Tue, 23 Sep 2014 17:40:46 +0000 Subject: [PATCH] Fix the position calculating of insert a radio menu item git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@77795 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- include/wx/univ/menuitem.h | 2 ++ src/univ/menu.cpp | 67 ++++++++++++++++++++++++++++++++++++-- 2 files changed, 67 insertions(+), 2 deletions(-) diff --git a/include/wx/univ/menuitem.h b/include/wx/univ/menuitem.h index 889c1bf4a0..cee4897be9 100644 --- a/include/wx/univ/menuitem.h +++ b/include/wx/univ/menuitem.h @@ -52,6 +52,8 @@ public: void SetAsRadioGroupStart(); void SetRadioGroupStart(int start); void SetRadioGroupEnd(int end); + int GetRadioGroupStart(); + int GetRadioGroupEnd(); // wxUniv-specific methods for implementation only starting from here diff --git a/src/univ/menu.cpp b/src/univ/menu.cpp index 5142f45fab..c764af3cef 100644 --- a/src/univ/menu.cpp +++ b/src/univ/menu.cpp @@ -1160,8 +1160,61 @@ wxMenuItem* wxMenu::DoAppend(wxMenuItem *item) wxMenuItem* wxMenu::DoInsert(size_t pos, wxMenuItem *item) { - if ( !wxMenuBase::DoInsert(pos, item) ) - return NULL; + if ( item->GetKind() == wxITEM_RADIO ) + { + unsigned int start, end; + wxMenuItemIter firstRadio; + + if ( m_startRadioGroup == -1 ) + { + // start a new radio group + m_startRadioGroup = pos; + + // set this element as the first of radio group + item->SetAsRadioGroupStart(); + item->SetRadioGroupEnd(m_startRadioGroup); + wxMenuBase::DoInsert(pos, item); + item->Check(true); + } + else // extend the current radio group + { + // get current first radio item in radio group + firstRadio = GetMenuItems().Item(m_startRadioGroup); + + // get current radio group range + start = firstRadio->GetData()->GetRadioGroupStart(); + end = firstRadio->GetData()->GetRadioGroupEnd(); + + if ( pos <= start ) + { + // Item is inserted in the begining of the range + // we need to update its end item + m_startRadioGroup = pos; + item->SetAsRadioGroupStart(); + item->SetRadioGroupEnd(end + 1); + } + else if ( (pos >= start) && (pos <= end) ) + { + // we need to update its end item + item->SetRadioGroupStart(m_startRadioGroup); + + // Item is inserted in the middle of this range or immediately + // after it in which case it extends this range so make it span + // one more item in any case. + if ( firstRadio ) + { + firstRadio->GetData()->SetRadioGroupEnd(end + 1); + } + else + { + wxFAIL_MSG( wxT("where is the radio group start item?") ); + } + } + wxMenuBase::DoInsert(pos, item); + } + } + else + wxMenuBase::DoInsert(pos, item); OnItemAdded(item); @@ -1605,6 +1658,16 @@ void wxMenuItem::SetRadioGroupEnd(int end) m_radioGroup.end = end; } +int wxMenuItem::GetRadioGroupStart() +{ + return m_radioGroup.start; +} + +int wxMenuItem::GetRadioGroupEnd() +{ + return m_radioGroup.end; +} + // ---------------------------------------------------------------------------- // wxMenuBar creation // ----------------------------------------------------------------------------