#ifndef UI_CONTROL_OPTION_H_ #define UI_CONTROL_OPTION_H_ #pragma once namespace ui { template class UILIB_API OptionTemplate : public CheckBoxTemplate { public: OptionTemplate(); ~OptionTemplate(); /// 重写父类方法,提供个性化功能,请参考父类声明 virtual void SetWindow(Window* pManager, Box* pParent, bool bInit = true) override; virtual void SetAttribute(const std::wstring& strName, const std::wstring& strValue) override; virtual void Selected(bool bSelected, bool bTriggerEvent = false) override; virtual void Activate() override; /** * @brief 获取所属组名称 * @return 返回组名称 */ virtual std::wstring GetGroup() const; /** * @brief 设置所属组 * @param[in] strGroupName 组名称 * @return 无 */ virtual void SetGroup(const std::wstring& strGroupName); protected: std::wstring m_sGroupName; }; template OptionTemplate::OptionTemplate() : m_sGroupName() { } template OptionTemplate::~OptionTemplate() { if (!m_sGroupName.empty() && this->m_pWindow) this->m_pWindow->RemoveOptionGroup(m_sGroupName, this); } template void OptionTemplate::SetWindow(Window* pManager, Box* pParent, bool bInit) { __super::SetWindow(pManager, pParent, bInit); if (bInit && !m_sGroupName.empty()) { if (this->m_pWindow) this->m_pWindow->AddOptionGroup(m_sGroupName, this); } } template void OptionTemplate::SetAttribute(const std::wstring& strName, const std::wstring& strValue) { if (strName == _T("group")) SetGroup(strValue); else __super::SetAttribute(strName, strValue); } template void OptionTemplate::Selected(bool bSelected, bool bTriggerEvent) { //if( m_bSelected == bSelected ) return; this->m_bSelected = bSelected; if (this->m_pWindow != NULL) { if (this->m_bSelected) { if (!m_sGroupName.empty()) { std::vector* aOptionGroup = this->m_pWindow->GetOptionGroup(m_sGroupName); ASSERT(aOptionGroup); if (aOptionGroup) { for (auto it = aOptionGroup->begin(); it != aOptionGroup->end(); it++) { auto pControl = static_cast*>(*it); if (pControl != this) { pControl->Selected(false, bTriggerEvent); } } } } if (bTriggerEvent) { this->m_pWindow->SendNotify(this, kEventSelect); } } else { this->m_pWindow->SendNotify(this, kEventUnSelect); } } this->Invalidate(); } template void OptionTemplate::Activate() { ButtonTemplate::Activate(); if (!this->IsActivatable()) return; Selected(true, true); } template std::wstring OptionTemplate::GetGroup() const { return m_sGroupName; } template void OptionTemplate::SetGroup(const std::wstring& strGroupName) { if (strGroupName.empty()) { if (m_sGroupName.empty()) return; m_sGroupName.clear(); } else { if (m_sGroupName == strGroupName) return; if (!m_sGroupName.empty() && this->m_pWindow) this->m_pWindow->RemoveOptionGroup(m_sGroupName, this); m_sGroupName = strGroupName; } if (!m_sGroupName.empty()) { if (this->m_pWindow) this->m_pWindow->AddOptionGroup(m_sGroupName, this); } else { if (this->m_pWindow) this->m_pWindow->RemoveOptionGroup(m_sGroupName, this); } Selected(this->m_bSelected, true); } typedef OptionTemplate Option; typedef OptionTemplate OptionBox; } #endif // UI_CONTROL_OPTION_H_