nim_duilib/tool_kits/duilib/Control/CheckBox.h

298 lines
9.3 KiB
C
Raw Normal View History

2019-04-19 17:19:57 +08:00
#ifndef UI_CONTROL_CHECKBOX_H_
#define UI_CONTROL_CHECKBOX_H_
#pragma once
namespace ui
{
template<typename InheritType = Control>
class UILIB_API CheckBoxTemplate : public ButtonTemplate<InheritType>
{
public:
CheckBoxTemplate();
/// <20><>д<EFBFBD><D0B4><EFBFBD><EFBFBD><E0B7BD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><E1B9A9><EFBFBD>Ի<EFBFBD><D4BB><EFBFBD><EFBFBD>ܣ<EFBFBD><DCA3><EFBFBD><EFBFBD>ο<EFBFBD><CEBF><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
virtual void Activate() override;
virtual Image* GetEstimateImage() override;
virtual void SetAttribute(const std::wstring& strName, const std::wstring& strValue) override;
virtual void PaintStatusColor(IRenderContext* pRender) override;
virtual void PaintStatusImage(IRenderContext* pRender) override;
virtual void PaintText(IRenderContext* pRender) override;
virtual bool HasHotState();
/**
* @brief <EFBFBD>жϵ<EFBFBD>ǰ<EFBFBD>Ƿ<EFBFBD><EFBFBD><EFBFBD>ѡ<EFBFBD><EFBFBD>״̬
* @return <EFBFBD><EFBFBD><EFBFBD><EFBFBD> true Ϊѡ<EFBFBD><EFBFBD>״̬<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϊ false
*/
bool IsSelected() const { return m_bSelected; }
/**
* @brief <EFBFBD><EFBFBD><EFBFBD>ÿؼ<EFBFBD><EFBFBD>Ƿ<EFBFBD>ѡ<EFBFBD><EFBFBD>״̬
* @param[in] bSelected Ϊ true ʱΪѡ<EFBFBD><EFBFBD>״̬<EFBFBD><EFBFBD>false ʱΪȡ<EFBFBD><EFBFBD>ѡ<EFBFBD><EFBFBD>״̬
* @param[in] bTriggerEvent <EFBFBD>Ƿ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>״̬<EFBFBD>ı<EFBFBD><EFBFBD>¼<EFBFBD><EFBFBD><EFBFBD>true Ϊ<EFBFBD><EFBFBD><EFBFBD>ͣ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϊ false<EFBFBD><EFBFBD>Ĭ<EFBFBD><EFBFBD>Ϊ false
* @return <EFBFBD><EFBFBD>
*/
virtual void Selected(bool bSelected, bool bTriggerEvent = false);
/**
* @brief <EFBFBD><EFBFBD>ȡ<EFBFBD><EFBFBD>ѡ<EFBFBD><EFBFBD>ʱ<EFBFBD><EFBFBD>ͼƬ
* @param[in] stateType Ҫ<EFBFBD><EFBFBD>ȡ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>״̬<EFBFBD>µ<EFBFBD>ͼƬ<EFBFBD><EFBFBD><EFBFBD>ο<EFBFBD> ControlStateType ö<EFBFBD><EFBFBD>
* @return <EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͼƬλ<EFBFBD><EFBFBD>
*/
std::wstring GetSelectedStateImage(ControlStateType stateType);
/**
* @brief <EFBFBD><EFBFBD><EFBFBD>ñ<EFBFBD>ѡ<EFBFBD><EFBFBD>ʱ<EFBFBD><EFBFBD>ͼƬ
* @param[in] stateType Ҫ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>״̬<EFBFBD>µ<EFBFBD>ͼƬ
* @param[in] strImage ͼƬ<EFBFBD><EFBFBD>ַ
* @return <EFBFBD><EFBFBD>
*/
void SetSelectedStateImage(ControlStateType stateType, const std::wstring& strImage);
/**
* @brief <EFBFBD><EFBFBD>ȡ<EFBFBD><EFBFBD>ѡ<EFBFBD><EFBFBD>ʱ<EFBFBD><EFBFBD><EFBFBD>ı<EFBFBD><EFBFBD><EFBFBD>ɫ
* @return <EFBFBD><EFBFBD><EFBFBD>ر<EFBFBD>ѡ<EFBFBD><EFBFBD>ʱ<EFBFBD><EFBFBD><EFBFBD>ı<EFBFBD><EFBFBD><EFBFBD>ɫ
*/
std::wstring GetSelectedTextColor();
/**
* @brief <EFBFBD><EFBFBD><EFBFBD>ñ<EFBFBD>ѡ<EFBFBD><EFBFBD>ʱ<EFBFBD><EFBFBD><EFBFBD>ı<EFBFBD><EFBFBD><EFBFBD>ɫ
* @param[in] dwTextColor Ҫ<EFBFBD><EFBFBD><EFBFBD>õ<EFBFBD><EFBFBD><EFBFBD>ɫ<EFBFBD>ַ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ɫ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> global.xml <EFBFBD>д<EFBFBD><EFBFBD><EFBFBD>
* @return <EFBFBD><EFBFBD>
*/
void SetSelectedTextColor(const std::wstring& dwTextColor);
/**
* @brief <EFBFBD><EFBFBD>ȡ<EFBFBD><EFBFBD>ѡ<EFBFBD><EFBFBD>ʱ<EFBFBD>Ŀؼ<EFBFBD><EFBFBD><EFBFBD>ɫ
* @param[in] stateType Ҫ<EFBFBD><EFBFBD>ȡ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>״̬<EFBFBD>µ<EFBFBD><EFBFBD><EFBFBD>ɫ
* @return <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ɫ<EFBFBD>ַ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֵ<EFBFBD><EFBFBD> global.xml <EFBFBD>ж<EFBFBD><EFBFBD><EFBFBD>
*/
std::wstring GetSelectStateColor(ControlStateType stateType);
/**
* @brief <EFBFBD><EFBFBD><EFBFBD>ñ<EFBFBD>ѡ<EFBFBD><EFBFBD>ʱ<EFBFBD>Ŀؼ<EFBFBD><EFBFBD><EFBFBD>ɫ
* @param[in] stateType Ҫ<EFBFBD><EFBFBD><EFBFBD>ú<EFBFBD><EFBFBD><EFBFBD>״̬<EFBFBD>µ<EFBFBD><EFBFBD><EFBFBD>ɫ
* @param[in] stateColor Ҫ<EFBFBD><EFBFBD><EFBFBD>õ<EFBFBD><EFBFBD><EFBFBD>ɫ
* @return <EFBFBD><EFBFBD>
*/
void SetSelectedStateColor(ControlStateType stateType, const std::wstring& stateColor);
/**
* @brief <EFBFBD><EFBFBD>ȡ<EFBFBD><EFBFBD>ѡ<EFBFBD><EFBFBD>ʱ<EFBFBD><EFBFBD>ǰ<EFBFBD><EFBFBD>ͼƬ
* @param[in] stateType Ҫ<EFBFBD><EFBFBD>ȡ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>״̬<EFBFBD>µ<EFBFBD>ǰ<EFBFBD><EFBFBD>ͼƬ
* @return <EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͼƬλ<EFBFBD><EFBFBD>
*/
std::wstring GetSelectedForeStateImage(ControlStateType stateType);
/**
* @brief <EFBFBD><EFBFBD><EFBFBD>ñ<EFBFBD>ѡ<EFBFBD><EFBFBD>ʱ<EFBFBD><EFBFBD>ǰ<EFBFBD><EFBFBD>ͼƬ
* @param[in] stateType Ҫ<EFBFBD><EFBFBD><EFBFBD>ú<EFBFBD><EFBFBD><EFBFBD>״̬<EFBFBD>µ<EFBFBD>ǰ<EFBFBD><EFBFBD>ͼƬ
* @param[in] pStrImage ͼƬλ<EFBFBD><EFBFBD>
* @return <EFBFBD><EFBFBD>
*/
void SetSelectedForeStateImage(ControlStateType stateType, const std::wstring& pStrImage);
/**
* @brief <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ѡ<EFBFBD><EFBFBD>ʱ<EFBFBD><EFBFBD><EFBFBD>¼<EFBFBD>
* @param[in] callback <EFBFBD><EFBFBD>ѡ<EFBFBD><EFBFBD>ʱ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ļص<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* @return <EFBFBD><EFBFBD>
*/
void AttachSelect(const EventCallback& callback) { this->OnEvent[kEventSelect] += callback; }
/**
* @brief <EFBFBD><EFBFBD><EFBFBD><EFBFBD>ȡ<EFBFBD><EFBFBD>ѡ<EFBFBD><EFBFBD>ʱ<EFBFBD><EFBFBD><EFBFBD>¼<EFBFBD>
* @param[in] callback ȡ<EFBFBD><EFBFBD>ѡ<EFBFBD><EFBFBD>ʱ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ļص<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* @return <EFBFBD><EFBFBD>
*/
void AttachUnSelect(const EventCallback& callback) { this->OnEvent[kEventUnSelect] += callback; }
protected:
bool m_bSelected;
std::wstring m_dwSelectedTextColor;
StateColorMap m_selectedColorMap;
};
template<typename InheritType>
CheckBoxTemplate<InheritType>::CheckBoxTemplate() : m_bSelected(false), m_dwSelectedTextColor(), m_selectedColorMap()
{
m_selectedColorMap.SetControl(this);
}
template<typename InheritType>
void CheckBoxTemplate<InheritType>::Activate()
{
if (!this->IsActivatable())
return;
Selected(!m_bSelected, true);
}
template<typename InheritType>
void CheckBoxTemplate<InheritType>::Selected(bool bSelected, bool bTriggerEvent)
{
if (m_bSelected == bSelected) return;
m_bSelected = bSelected;
if (this->m_pWindow != NULL) {
if (bTriggerEvent) {
if (m_bSelected) {
this->m_pWindow->SendNotify(this, kEventSelect);
}
else {
this->m_pWindow->SendNotify(this, kEventUnSelect);
}
}
}
this->Invalidate();
}
template<typename InheritType>
bool CheckBoxTemplate<InheritType>::HasHotState()
{
return m_selectedColorMap.HasHotColor() || __super::HasHotState();
}
template<typename InheritType>
Image* CheckBoxTemplate<InheritType>::GetEstimateImage()
{
Image* estimateImage = __super::GetEstimateImage();
if (!estimateImage) {
estimateImage = this->m_imageMap.GetEstimateImage(kStateImageSelectedBk);
}
return estimateImage;
}
template<typename InheritType>
void CheckBoxTemplate<InheritType>::SetAttribute(const std::wstring& strName, const std::wstring& strValue)
{
if (strName == _T("selected")) Selected(strValue == _T("true"), true);
else if (strName == _T("selectednormalimage")) SetSelectedStateImage(kControlStateNormal, strValue);
else if (strName == _T("selectedhotimage")) SetSelectedStateImage(kControlStateHot, strValue);
else if (strName == _T("selectedpushedimage")) SetSelectedStateImage(kControlStatePushed, strValue);
else if (strName == _T("selecteddisabledimage")) SetSelectedStateImage(kControlStateDisabled, strValue);
else if (strName == _T("selectedtextcolor")) SetSelectedTextColor(strValue);
else if (strName == _T("selectednormalcolor")) SetSelectedStateColor(kControlStateNormal, strValue);
else if (strName == _T("selectedhotcolor")) SetSelectedStateColor(kControlStateHot, strValue);
else if (strName == _T("selectedpushedcolor")) SetSelectedStateColor(kControlStatePushed, strValue);
else if (strName == _T("selecteddisabledcolor")) SetSelectedStateColor(kControlStateDisabled, strValue);
else if (strName == _T("selectedforenormalimage")) SetSelectedForeStateImage(kControlStateNormal, strValue);
else if (strName == _T("selectedforehotimage")) SetSelectedForeStateImage(kControlStateHot, strValue);
else if (strName == _T("selectedforepushedimage")) SetSelectedForeStateImage(kControlStatePushed, strValue);
else if (strName == _T("selectedforedisabledimage")) SetSelectedForeStateImage(kControlStateDisabled, strValue);
else __super::SetAttribute(strName, strValue);
}
template<typename InheritType>
void CheckBoxTemplate<InheritType>::PaintStatusColor(IRenderContext* pRender)
{
if (!IsSelected()) {
__super::PaintStatusColor(pRender);
return;
}
m_selectedColorMap.PaintStatusColor(pRender, this->m_rcPaint, this->m_uButtonState);
}
template<typename InheritType>
void CheckBoxTemplate<InheritType>::PaintStatusImage(IRenderContext* pRender)
{
if (!IsSelected()) {
__super::PaintStatusImage(pRender);
return;
}
this->m_imageMap.PaintStatusImage(pRender, kStateImageSelectedBk, this->m_uButtonState);
this->m_imageMap.PaintStatusImage(pRender, kStateImageSelectedFore, this->m_uButtonState);
}
template<typename InheritType>
void CheckBoxTemplate<InheritType>::PaintText(IRenderContext* pRender)
{
if (!IsSelected()) {
__super::PaintText(pRender);
return;
}
if (this->GetText().empty()) return;
UiRect rc = this->m_rcItem;
rc.left += this->m_rcTextPadding.left;
rc.right -= this->m_rcTextPadding.right;
rc.top += this->m_rcTextPadding.top;
rc.bottom -= this->m_rcTextPadding.bottom;
std::wstring newTextColor = m_dwSelectedTextColor.empty() ? this->m_textColorMap[kControlStateNormal] : m_dwSelectedTextColor;
DWORD dwTextColor = GlobalManager::GetTextColor(newTextColor);
DWORD dwDisabledTextColor = GlobalManager::GetTextColor(this->m_textColorMap[kControlStateDisabled]);
pRender->DrawText(rc, this->GetText(), this->IsEnabled() ? dwTextColor : dwDisabledTextColor, this->m_iFont, this->m_uTextStyle);
}
template<typename InheritType>
std::wstring CheckBoxTemplate<InheritType>::GetSelectedStateImage(ControlStateType stateType)
{
return this->m_imageMap.GetImagePath(kStateImageSelectedBk, stateType);
}
template<typename InheritType>
void CheckBoxTemplate<InheritType>::SetSelectedStateImage(ControlStateType stateType, const std::wstring& pStrImage)
{
this->m_imageMap.SetImage(kStateImageSelectedBk, stateType, pStrImage);
if (this->GetFixedWidth() == DUI_LENGTH_AUTO || this->GetFixedHeight() == DUI_LENGTH_AUTO) {
this->ArrangeAncestor();
}
else {
this->Invalidate();
}
}
template<typename InheritType>
std::wstring CheckBoxTemplate<InheritType>::GetSelectedTextColor()
{
return m_dwSelectedTextColor;
}
template<typename InheritType>
void CheckBoxTemplate<InheritType>::SetSelectedTextColor(const std::wstring& dwTextColor)
{
m_dwSelectedTextColor = dwTextColor;
this->Invalidate();
}
template<typename InheritType>
std::wstring CheckBoxTemplate<InheritType>::GetSelectStateColor(ControlStateType stateType)
{
return m_selectedColorMap[stateType];
}
template<typename InheritType>
void CheckBoxTemplate<InheritType>::SetSelectedStateColor(ControlStateType stateType, const std::wstring& stateColor)
{
m_selectedColorMap[stateType] = stateColor;
this->Invalidate();
}
template<typename InheritType>
std::wstring CheckBoxTemplate<InheritType>::GetSelectedForeStateImage(ControlStateType stateType)
{
return this->m_imageMap.GetImagePath(kStateImageSelectedFore, stateType);
}
template<typename InheritType>
void CheckBoxTemplate<InheritType>::SetSelectedForeStateImage(ControlStateType stateType, const std::wstring& pStrImage)
{
this->m_imageMap.SetImage(kStateImageSelectedFore, stateType, pStrImage);
if (this->GetFixedWidth() == DUI_LENGTH_AUTO || this->GetFixedHeight() == DUI_LENGTH_AUTO) {
this->ArrangeAncestor();
}
else {
this->Invalidate();
}
}
typedef CheckBoxTemplate<Control> CheckBox;
typedef CheckBoxTemplate<Box> CheckBoxBox;
} // namespace ui
#endif // UI_CONTROL_CHECKBOX_H_#ifndef UI_CONTROL_CHECKBOX_H_