summaryrefslogtreecommitdiff
path: root/src/mir_core/src/CCtrlBase.cpp
blob: f6013a59ab599f6997d0792f37ae742d6d18d08a (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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
/*

Object UI extensions
Copyright (c) 2008  Victor Pavlychko, George Hazan
Copyright (c) 2012-17 Miranda NG project

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.

*/

#include "stdafx.h"

static mir_cs csCtrl;

static int CompareControls(const CCtrlBase *p1, const CCtrlBase *p2)
{
	return (INT_PTR)p1->GetHwnd() - (INT_PTR)p2->GetHwnd();
}
static LIST<CCtrlBase> arControls(10, CompareControls);

/////////////////////////////////////////////////////////////////////////////////////////
// CCtrlBase

CCtrlBase::CCtrlBase(CDlgBase *wnd, int idCtrl)
	: m_parentWnd(wnd),
	m_idCtrl(idCtrl),
	m_hwnd(nullptr),
	m_bChanged(false),
	m_bSilent(false),
	m_bUseSystemColors(false)
{
	if (wnd)
		wnd->AddControl(this);
}

CCtrlBase::~CCtrlBase()
{
}

void CCtrlBase::OnInit()
{
	m_hwnd = (m_idCtrl && m_parentWnd && m_parentWnd->GetHwnd()) ? GetDlgItem(m_parentWnd->GetHwnd(), m_idCtrl) : nullptr;
}

void CCtrlBase::OnDestroy()
{
	PVOID bullshit[2];  // vfptr + hwnd
	bullshit[1] = m_hwnd;
	CCtrlBase *pCtrl = arControls.find((CCtrlBase*)&bullshit);
	if (pCtrl) {
		pCtrl->Unsubclass();
		arControls.remove(pCtrl);
	}

	m_hwnd = nullptr;
}

void CCtrlBase::OnApply()
{
	m_bChanged = false;
}

void CCtrlBase::OnReset()
{}

void CCtrlBase::Show(bool bShow)
{
	::ShowWindow(m_hwnd, bShow ? SW_SHOW : SW_HIDE);
}

void CCtrlBase::Enable(bool bIsEnable)
{
	::EnableWindow(m_hwnd, bIsEnable);
}

bool CCtrlBase::Enabled() const
{
	return (m_hwnd) ? IsWindowEnabled(m_hwnd) != 0 : false;
}

void CCtrlBase::NotifyChange()
{
	if (!m_parentWnd || m_parentWnd->IsInitialized())
		m_bChanged = true;

	if (m_parentWnd && !m_bSilent) {
		m_parentWnd->OnChange(this);
		if (m_parentWnd->IsInitialized())
			::SendMessage(::GetParent(m_parentWnd->GetHwnd()), PSM_CHANGED, 0, 0);
	}

	OnChange(this);
}

LRESULT CCtrlBase::SendMsg(UINT Msg, WPARAM wParam, LPARAM lParam) const
{
	return ::SendMessage(m_hwnd, Msg, wParam, lParam);
}

void CCtrlBase::SetText(const wchar_t *text)
{
	::SetWindowText(m_hwnd, text);
}

void CCtrlBase::SetTextA(const char *text)
{
	::SetWindowTextA(m_hwnd, text);
}

void CCtrlBase::SetInt(int value)
{
	wchar_t buf[32] = { 0 };
	mir_snwprintf(buf, L"%d", value);
	SetWindowText(m_hwnd, buf);
}

wchar_t* CCtrlBase::GetText()
{
	int length = GetWindowTextLength(m_hwnd) + 1;
	wchar_t *result = (wchar_t *)mir_alloc(length * sizeof(wchar_t));
	GetWindowText(m_hwnd, result, length);
	return result;
}

char* CCtrlBase::GetTextA()
{
	int length = GetWindowTextLength(m_hwnd) + 1;
	char *result = (char *)mir_alloc(length * sizeof(char));
	GetWindowTextA(m_hwnd, result, length);
	return result;
}

wchar_t* CCtrlBase::GetText(wchar_t *buf, int size)
{
	GetWindowText(m_hwnd, buf, size);
	buf[size - 1] = 0;
	return buf;
}

char* CCtrlBase::GetTextA(char *buf, int size)
{
	GetWindowTextA(m_hwnd, buf, size);
	buf[size - 1] = 0;
	return buf;
}

int CCtrlBase::GetInt()
{
	int length = GetWindowTextLength(m_hwnd) + 1;
	wchar_t *result = (wchar_t *)_alloca(length * sizeof(wchar_t));
	GetWindowText(m_hwnd, result, length);
	return _wtoi(result);
}

LRESULT CCtrlBase::CustomWndProc(UINT, WPARAM, LPARAM)
{
	return FALSE;
}

LRESULT CALLBACK CCtrlBase::GlobalSubclassWndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
	PVOID bullshit[2];  // vfptr + hwnd
	bullshit[1] = hwnd;
	CCtrlBase *pCtrl = arControls.find((CCtrlBase*)&bullshit);
	if (pCtrl) {
		LRESULT res = pCtrl->CustomWndProc(msg, wParam, lParam);
		if (res != 0)
			return res;
	}

	return mir_callNextSubclass(hwnd, GlobalSubclassWndProc, msg, wParam, lParam);
}

void CCtrlBase::Subclass()
{
	mir_subclassWindow(m_hwnd, GlobalSubclassWndProc);

	mir_cslock lck(csCtrl);
	arControls.insert(this);
}

void CCtrlBase::Unsubclass()
{
	mir_unsubclassWindow(m_hwnd, GlobalSubclassWndProc);
}