//**********************************************************************
void CBCGPRibbonCustomizeRibbonPage::OnToolbarReset() 
{
#ifndef BCGP_EXCLUDE_GRID_CTRL
	ASSERT_VALID (m_pRibbonBar);

	CBCGPLocalResource locaRes;
	if (AfxMessageBox (IDS_BCGBARRES_RESET_RIBBON, MB_YESNO | MB_ICONWARNING) != IDYES)
	{
		return;
	}

	// Mark all custom data as "to be deleted":
	for (int i = 0; i < m_wndRibbonTreeDest.GetRowCount(); i++)
	{
		CBCGPGridRow* pRow = m_wndRibbonTreeDest.GetRow(i);
		if (pRow == NULL)
		{
			continue;
		}

		CObject* lpData = (CObject*)pRow->GetData();
		if (lpData == NULL)
		{
			continue;
		}

		CBCGPRibbonCategory* pCategory = DYNAMIC_DOWNCAST(CBCGPRibbonCategory, lpData);
		if (pCategory != NULL && pCategory->IsCustom())
		{
			pCategory->m_bToBeDeleted = TRUE;
		}
		else
		{
			CBCGPRibbonPanel* pPanel = DYNAMIC_DOWNCAST(CBCGPRibbonPanel, lpData);
			if (pPanel != NULL && pPanel->IsCustom())
			{
				pPanel->m_bToBeDeleted = TRUE;
			}
		}
	}

	m_CustomizationData.ResetAll(m_pRibbonBar);
	RebuildDestTree();
#endif
}
//**********************************************************************
LRESULT CBCGPRibbonCustomizeRibbonPage::OnSelChangeRibbonTree(WPARAM, LPARAM lp)
{
#ifndef BCGP_EXCLUDE_GRID_CTRL
	if (m_bDontProcessSelChanged)
	{
		return 0;
	}

	m_wndAdd.EnableWindow(FALSE);

	CBCGPGridCtrl* pGrid = (CBCGPGridCtrl*)lp;

	if (pGrid->GetSafeHwnd() == m_wndRibbonTreeDest.GetSafeHwnd())
	{
		m_wndRename.EnableWindow(FALSE);
		m_wndNewTab.EnableWindow(FALSE);
		m_wndNewGroup.EnableWindow(FALSE);
		m_wndRemove.EnableWindow(FALSE);
		m_wndUp.EnableWindow(FALSE);
		m_wndDown.EnableWindow(FALSE);

		m_pSelCategoryDest = NULL;
		m_pSelPanelDest = NULL;
		m_pSelElemDest = NULL;

		CBCGPGridRow* pRow = m_wndRibbonTreeDest.GetCurSel();
		if (pRow == NULL)
		{
			return 0;
		}

		ASSERT_VALID(pRow);

		CObject* lpData = (CObject*)pRow->GetData();
		if (lpData != NULL)
		{
			m_wndNewTab.EnableWindow();

			m_pSelCategoryDest = DYNAMIC_DOWNCAST(CBCGPRibbonCategory, lpData);
			if (m_pSelCategoryDest != NULL)
			{
				m_wndRename.EnableWindow();
				m_wndNewGroup.EnableWindow();

				if (m_pSelCategoryDest->IsCustom())
				{
					m_wndRemove.EnableWindow();
				}

				m_wndUp.EnableWindow(!m_wndRibbonTreeDest.IsSelFirstInGroup());
				m_wndDown.EnableWindow(!m_wndRibbonTreeDest.IsSelLastInGroup());
			}
			else
			{
				m_pSelPanelDest = DYNAMIC_DOWNCAST(CBCGPRibbonPanel, lpData);
				if (m_pSelPanelDest != NULL)
				{
					m_wndRename.EnableWindow();
					m_wndNewGroup.EnableWindow();
					m_wndRemove.EnableWindow();

					m_wndUp.EnableWindow(!m_wndRibbonTreeDest.IsSelFirstInGroup());
					m_wndDown.EnableWindow(!m_wndRibbonTreeDest.IsSelLastInGroup());
				}
				else
				{
					m_pSelElemDest = DYNAMIC_DOWNCAST(CBCGPBaseRibbonElement, lpData);
					if (m_pSelElemDest != NULL)
					{
						m_wndNewGroup.EnableWindow();
						
						if (m_pSelElemDest->IsCustom() && m_pSelElemDest->GetParentPanel() != NULL)
						{
							m_wndRename.EnableWindow();
							m_wndRemove.EnableWindow();

							m_wndUp.EnableWindow(!m_wndRibbonTreeDest.IsSelFirstInGroup());
							m_wndDown.EnableWindow(!m_wndRibbonTreeDest.IsSelLastInGroup());
						}
					}
				}
			}
		}
	}
	else if (pGrid->GetSafeHwnd() == m_wndRibbonTreeSrc.GetSafeHwnd())
	{
		m_pSelCategorySrc = NULL;
		m_pSelPanelSrc = NULL;
		m_pSelElemSrc = NULL;

		CBCGPGridRow* pRow = m_wndRibbonTreeSrc.GetCurSel();
		if (pRow == NULL)
		{
			return 0;
		}

		ASSERT_VALID(pRow);

		CObject* lpData = (CObject*)pRow->GetData();
		if (lpData != NULL)
		{
			m_pSelCategorySrc = DYNAMIC_DOWNCAST(CBCGPRibbonCategory, lpData);
			m_pSelPanelSrc = DYNAMIC_DOWNCAST(CBCGPRibbonPanel, lpData);
			m_pSelElemSrc = DYNAMIC_DOWNCAST(CBCGPBaseRibbonElement, lpData);
		}
	}

	if (m_pSelCategorySrc != NULL)
	{
		m_wndAdd.EnableWindow();
	}
	else if (m_pSelPanelSrc != NULL)
	{
		ASSERT_VALID(m_pSelPanelSrc);

		CBCGPRibbonCategory* pCategory = m_pSelCategoryDest;
		if (pCategory == NULL)
		{
			if (m_pSelPanelDest != NULL)
			{
				pCategory = m_pSelPanelDest->GetParentCategory();
			}
			else if (m_pSelElemDest != NULL)
			{
				pCategory = m_pSelElemDest->GetParentCategory();
			}
		}

		if (pCategory != NULL)
		{
			m_wndAdd.EnableWindow(pCategory != NULL && pCategory->FindPanelByOriginal(m_pSelPanelSrc) == NULL);
		}
	}
	else if (m_pSelElemSrc != NULL)
	{
		ASSERT_VALID(m_pSelElemSrc);

		CBCGPRibbonPanel* pPanel = m_pSelPanelDest;
		if (pPanel == NULL && m_pSelElemDest != NULL)
		{
			pPanel = m_pSelElemDest->GetParentPanel();
		}

		BOOL bCanAdd = FALSE;

		if (pPanel != NULL && pPanel->IsCustom() && pPanel->m_pOriginal == NULL)
		{
			CBCGPRibbonCustomPanel* pCustomPanel = m_CustomizationData.FindCustomPanel(pPanel);
			if (pCustomPanel != NULL)
			{
				ASSERT_VALID(pCustomPanel);

				if (pCustomPanel->FindByID(m_pSelElemSrc->GetID()) == NULL &&
					m_wndRibbonTreeDest.FindElementInPanel(pPanel, m_pSelElemSrc->GetID()) == NULL)
				{
					bCanAdd = TRUE;
				}
			}
		}

		m_wndAdd.EnableWindow(bCanAdd);
	}
#endif
	return 0;
}