summaryrefslogtreecommitdiff
path: root/plugins/HistoryStats/src/optionsctrlimpl_radio.cpp
blob: 5909a6e907d0a2a79e8c7ed4c65c6c614193a96b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#include "stdafx.h"
#include "optionsctrlimpl.h"

/*
 * OptionsCtrlImpl::RadioSiblings
 */

int OptionsCtrlImpl::RadioSiblings::join(Radio* pRadio)
{
	m_Radios.push_back(pRadio);
	++m_nRadios;

	return m_nRadios - 1;
}

bool OptionsCtrlImpl::RadioSiblings::leave(int nRadio)
{
	assert(nRadio >= 0 && nRadio < m_Radios.size() && m_Radios[nRadio]);

	m_Radios[nRadio] = NULL;
	--m_nRadios;

	return (m_nRadios == 0);
}

void OptionsCtrlImpl::RadioSiblings::setChecked(int nRadio)
{
	if (nRadio == m_nChecked)
	{
		return;
	}

	if (nRadio < 0 || nRadio >= m_Radios.size())
	{
		return;
	}

	if (m_nChecked != -1)
	{
		assert(m_Radios[m_nChecked]);

		int nOldChecked = m_nChecked;

		m_nChecked = -1;
		m_Radios[nOldChecked]->updateItem();
	}

	assert(m_Radios[nRadio]);

	m_nChecked = nRadio;
	m_Radios[m_nChecked]->updateItem();
}

/*
 * OptionsCtrlImpl::Radio
 */

bool OptionsCtrlImpl::Radio::getChildEnable(bool bChecked)
{
	return
		!m_bDisableChildsOnUncheck && m_bDisableChilds && m_bEnabled ||
		m_bDisableChildsOnUncheck && bChecked && (!m_bDisableChilds || m_bEnabled);
}

void OptionsCtrlImpl::Radio::updateItem()
{
	bool bChecked = isChecked();

	m_pCtrl->setStateImage(m_hItem, m_bEnabled ? (bChecked ? siRadioC : siRadioU) : (bChecked ? siRadioCG : siRadioUG));

	if (m_bDisableChilds || m_bDisableChildsOnUncheck)
	{
		enableChilds(getChildEnable(bChecked));
	}
}

OptionsCtrlImpl::Radio::Radio(OptionsCtrlImpl* pCtrl, Item* pParent, Radio* pSibling, const wchar_t* szLabel, uint32_t dwFlags, INT_PTR dwData)
	: Item(pCtrl, itRadio, szLabel, dwFlags, dwData)
{
	m_bDisableChildsOnUncheck = bool_(dwFlags & OCF_DISABLECHILDSONUNCHECK);

	pCtrl->insertItem(pParent, this, szLabel, dwFlags, siRadioU);

	m_pSiblings = pSibling ? pSibling->m_pSiblings : new RadioSiblings();
	m_nIndex = m_pSiblings->join(this);

	if (dwFlags & OCF_CHECKED)
	{
		m_pSiblings->setChecked(m_nIndex);
	}
	else
	{
		updateItem();
	}

	if (pParent)
	{
		pParent->childAdded(this);
	}
}

OptionsCtrlImpl::Radio::~Radio()
{
	if (m_pSiblings->leave(m_nIndex))
	{
		delete m_pSiblings;
	}
}

void OptionsCtrlImpl::Radio::onToggle()
{
	if (m_bEnabled && !isChecked())
	{
		setChecked();
		m_pCtrl->setModified(this);
	}
}

void OptionsCtrlImpl::Radio::childAdded(Item* pChild)
{
	if (m_bDisableChilds || m_bDisableChildsOnUncheck)
	{
		pChild->setEnabled(getChildEnable(isChecked()));
	}
}