void CXTPRibbonGroup::OnGroupAdded()
{
    CXTPControl* pGroupOption = GetControlGroupOption();

    m_pParent->GetControls()->Add(pGroupOption);
    pGroupOption->InternalAddRef();

    pGroupOption->SetHideFlag(xtpHideRibbonTab, !IsVisible());


    pGroupOption->SetID(GetID());

    CXTPControl* pGroupPopup = (CXTPControl*)GetControlGroupPopup();
    m_pParent->GetControls()->Add(pGroupPopup);
    pGroupPopup->SetCaption(GetCaption());
    pGroupPopup->SetDescription(NULL);

    pGroupPopup->InternalAddRef();

    pGroupPopup->SetHideFlag(xtpHideRibbonTab, !IsVisible());

    CXTPCommandBars* pCommandBars = m_pRibbonBar->GetCommandBars();
    ASSERT(pCommandBars);

    if (pCommandBars && pCommandBars->IsActionsEnabled())
    {
        CXTPControlAction* pAction = pCommandBars->CreateAction(GetID());
        if (pAction->GetCaption().IsEmpty()) pAction->SetCaption(GetCaption());

        pGroupPopup->SetAction(pAction);
        pGroupOption->SetAction(pAction);
    }
}
CXTPControl* CXTPRibbonGroup::AddClone(CXTPControl* pClone, int nBefore, BOOL bRecursive)
{
    if (nBefore < 0 || nBefore >= GetCount())
        nBefore = (int)GetCount();

    CXTPControl* pControl = (CXTPControl*)pClone->GetRuntimeClass()->CreateObject();
    ASSERT(pControl);
    if (!pControl)
        return NULL;

    int nControlPos = m_pControlGroupOption->GetIndex() - GetCount() + nBefore;

    GetRibbonBar()->GetControls()->InsertAt(pControl, nControlPos);

    m_arrControls.InsertAt(nBefore, pControl);
    pControl->InternalAddRef();

    pControl->Copy(pClone, bRecursive);


    pControl->m_pRibbonGroup = this;
    pControl->SetHideFlag(xtpHideRibbonTab, !IsVisible());

    return pControl;
}
void CXTPRibbonGroup::Copy(CXTPRibbonGroup* pGroup)
{
    m_nId = pGroup->m_nId;
    m_nIconId = pGroup->m_nIconId;
    m_bShowOptionButton = pGroup->m_bShowOptionButton;
    m_strCaption = pGroup->m_strCaption;
    m_pGroups = pGroup->m_pGroups;
    m_bControlsGrouping = pGroup->m_bControlsGrouping;
    m_bControlsCentering = pGroup->m_bControlsCentering;
    m_bVisible = pGroup->m_bVisible;

    int nControlPos = m_pControlGroupOption->GetIndex();

    for (int i = 0; i < pGroup->GetCount(); i++)
    {
        CXTPControl* pControl = pGroup->GetAt(i);

        CXTPControl* pClone = m_pParent->GetControls()->AddClone(pControl, nControlPos + i);
        pClone->m_pRibbonGroup = this;
        pClone->SetHideFlag(xtpHideRibbonTab, FALSE);
        m_arrControls.Add(pClone);
        pClone->InternalAddRef();
    }

    m_pControlGroupOption->Copy(pGroup->m_pControlGroupOption, FALSE);
    m_pControlGroupOption->SetHideFlag(xtpHideRibbonTab, FALSE);

    m_pControlGroupPopup->Copy(pGroup->m_pControlGroupPopup, FALSE);
    m_pControlGroupPopup->SetHideFlag(xtpHideRibbonTab, FALSE);
}
CXTPControl* CXTPRibbonGroup::Add(XTPControlType controlType, int nId, LPCTSTR lpszParameter, int nBefore, BOOL bTemporary)
{
    if (nBefore < 0 || nBefore >= GetCount())
        nBefore = (int)GetCount();

    int nControlPos = m_pControlGroupOption->GetIndex() - GetCount() + nBefore;

    CXTPControl* pControl = GetRibbonBar()->GetControls()->Add(controlType, nId, lpszParameter, nControlPos, bTemporary);
    ASSERT(pControl);
    if (!pControl)
        return NULL;

    m_arrControls.InsertAt(nBefore, pControl);
    pControl->InternalAddRef();

    pControl->m_pRibbonGroup = this;
    pControl->SetHideFlag(xtpHideRibbonTab, !IsVisible());

    return pControl;
}
BOOL CXTPRibbonGroup::SetButtons(UINT* pButtons, int nCount)
{
    BOOL bSeparator = FALSE;

    CXTPRibbonBar* pRibbonBar = GetRibbonBar();
    CWnd* pSite = pRibbonBar->GetSite();

    for (int i = 0; i < nCount; i++)
    {
        if (pButtons[i] == 0)
            bSeparator = TRUE;
        else
        {
            XTPControlType controlType = xtpControlButton;
            XTPButtonStyle buttonStyle = xtpButtonAutomatic;
            CXTPControl* pControl = NULL;
            UINT nID = pButtons[i];

            XTP_COMMANDBARS_CREATECONTROL cs;

            if (pSite)
            {
                cs.nID = nID;
                cs.pControl = NULL;
                cs.bToolBar = TRUE;
                cs.pMenu = NULL;
                cs.nIndex = i;
                cs.strCaption = pRibbonBar->GetTitle();
                cs.controlType = controlType;
                cs.pCommandBar = pRibbonBar;
                cs.buttonStyle = buttonStyle;

                if (pSite->SendMessage(WM_XTP_BEFORECREATECONTROL, 0, (LPARAM)&cs) != 0)
                {
                    pControl = cs.pControl;
                    controlType = cs.controlType;
                    buttonStyle = cs.buttonStyle;
                    nID = cs.nID;
                }
            }

            int nControlPos = m_pControlGroupOption->GetIndex();

            if (pControl == NULL)
            {
                pControl = pRibbonBar->GetControls()->Add(controlType, nID, NULL, nControlPos);
                if (pControl)
                {
                    pControl->SetStyle(buttonStyle);
                    if (controlType == xtpControlPopup) pControl->SetIconId(nID);
                }
            }
            else pRibbonBar->GetControls()->Add(pControl, nID, NULL, nControlPos);

            if (!pControl)
                continue;

            if (bSeparator)
            {
                pControl->SetBeginGroup(TRUE);
                bSeparator = FALSE;
            }

            m_arrControls.InsertAt(GetCount(), pControl);
            pControl->InternalAddRef();

            pControl->m_pRibbonGroup = this;
            pControl->SetHideFlag(xtpHideRibbonTab, !IsVisible());

            if (pSite)
            {
                cs.pControl = pControl;
                pSite->SendMessage(WM_XTP_AFTERCREATECONTROL, 0, (LPARAM)&cs);
            }
        }
    }
    return TRUE;
}