summaryrefslogtreecommitdiff
path: root/plugins/UserInfoEx/src/ctrl_combo.cpp
blob: b59f73e03f39fc98a9bc97afa6d54c9fb5720e93 (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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
/*
UserinfoEx plugin for Miranda IM

Copyright:
© 2006-2010 DeathAxe, Yasnovidyashii, Merlin, K. Romanov, Kreol

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 "commonheaders.h"
#include "ctrl_combo.h"

/**
 * This static method creates an object for the CCombo class and returns its pointer.
 * If combobox is filled with items from pList.
 *
 * @param		hDlg			- HWND of the owning propertysheet page
 * @param		idCtrl			- the ID of the control to associate with this class's instance
 * @param		pszSetting		- the database setting to be handled by this class
 * @param		bDBDataType		- datatype of of the associated database setting (WORD, DWORD, ...)
 * @param		pList			- pointer to a LPIDSTRLIST list, which holds the items to insert into the combobox.
 * @param		nListCount		- number of items in the list
 *
 * @return	pointer of the newly created CCombo object.
 **/
CBaseCtrl* CCombo::CreateObj(HWND hDlg, WORD idCtrl, LPCSTR pszSetting, BYTE bDBDataType, LPIDSTRLIST pList, INT nListCount)
{
	return new CCombo(hDlg, idCtrl, pszSetting, bDBDataType, pList, nListCount);
}

/**
 * This is the constructor. If combobox is filled with items from pList.
 *
 * @param		hDlg			- HWND of the owning propertysheet page
 * @param		idCtrl			- the ID of the control to associate with this class's instance
 * @param		pszSetting		- the database setting to be handled by this class
 * @param		bDBDataType		- datatype of of the associated database setting (WORD, DWORD, ...)
 * @param		pList			- pointer to a LPIDSTRLIST list, which holds the items to insert into the combobox.
 * @param		nListCount		- number of items in the list
 *
 * @return	nothing
 **/
CCombo::CCombo(HWND hDlg, WORD idCtrl, LPCSTR pszSetting, BYTE bDBDataType, LPIDSTRLIST pList, INT nListCount)
: CBaseCtrl(hDlg, idCtrl, pszSetting)
{
	_curSel = CB_ERR;
	_pList = pList;
	_nList = nListCount;
	_bDataType = bDBDataType;

	// fill in data
	if (_pList && (_nList > 0))
	{
		for (INT i = 0; i < _nList; i++)
		{
			AddItem(_pList[i].ptszTranslated, (LPARAM)&_pList[i]);
		}
	}
}

/**
 * This method searches for an item which matches the given nID.
 *
 * @param		nID				- the id of the desired object.
 *
 * @retval	CB_ERR	- item not found
 * @retval	0...n		- index of the combobox item
 **/
INT CCombo::Find(INT nID) const
{
	INT i;
	LPIDSTRLIST pd;

	for (i = ComboBox_GetCount(_hwnd) - 1; i >= 0; i--)
	{
		pd = (LPIDSTRLIST)ComboBox_GetItemData(_hwnd, i);
		if (PtrIsValid(pd) && (pd->nID == nID))
		{
			break;
		}
	}
	return i;
}

/**
 * This method searches for an item which matches the given label.
 *
 * @param		ptszItemLabel	- The translated label of the desired item.
 *
 * @retval	CB_ERR	- item not found
 * @retval	0...n		- index of the combobox item
 **/
INT CCombo::Find(LPTSTR ptszItemLabel) const
{
	return ComboBox_FindStringExact(_hwnd, 0, ptszItemLabel);
}

/**
 * Adds a string and associated item data to a combobox.
 *
 * @param	 pszText			- the text to add to the combobox
 * @param	 lParam				- item data to accociate with the new item
 *
 * @return	zero-based index to new item or CB_ERR on failure
 **/
INT_PTR CCombo::AddItem(LPCTSTR pszText, LPARAM lParam)
{
	INT_PTR added = ComboBox_AddString(_hwnd, pszText);
	if (SUCCEEDED(added)) 
	{
		if (PtrIsValid(lParam) && FAILED(ComboBox_SetItemData(_hwnd, added, lParam)))
		{
			ComboBox_DeleteString(_hwnd, added);
			added = CB_ERR;
		}
	}
	return added;
}

/**
 * This functions removes the user data from a combobox.
 *
 * @param		hCtrl			- HWND of the combobox
 *
 * @return	nothing
 **/
VOID CCombo::Release()
{
	delete this;
}

/**
 * This method selects the combobox item which matches the contact's setting, associated with.
 *
 * @param		hContact	- HANDLE of the contact
 * @param		pszProto	- the contact's protocol
 *
 * @return	nothing
 **/
BOOL CCombo::OnInfoChanged(HANDLE hContact, LPCSTR pszProto)
{
	if (!_Flags.B.hasChanged)
	{
		DBVARIANT dbv;
		LPIDSTRLIST pItem = NULL;
		INT iVal = CB_ERR;

		_Flags.B.hasCustom = _Flags.B.hasProto = _Flags.B.hasMeta = 0;
		_Flags.W |= DB::Setting::GetTStringCtrl(hContact, USERINFO, USERINFO, pszProto, _pszSetting, &dbv);
		EnableWindow(_hwnd, !hContact || _Flags.B.hasCustom || !DB::Setting::GetByte(SET_PROPSHEET_PCBIREADONLY, 0));	

		if (_Flags.B.hasCustom || _Flags.B.hasProto || _Flags.B.hasMeta)
		{
			switch (dbv.type) 
			{
			case DBVT_BYTE:		iVal = Find((INT)dbv.bVal);		break;
			case DBVT_WORD:		iVal = Find((INT)dbv.wVal);		break;
			case DBVT_DWORD:	iVal = Find((INT)dbv.dVal);		break;
			case DBVT_TCHAR:
				iVal = Find(TranslateTS(dbv.ptszVal));
				if (iVal == CB_ERR) {
					// other
					iVal = Find(_pList[_nList - 1].nID);
				}
			}
		}
		if (iVal == CB_ERR)
		{
			// unspecified
			iVal = Find(_pList[0].nID);
		}
		DB::Variant::Free(&dbv);
		ComboBox_SetCurSel(_hwnd, iVal);
		_curSel = ComboBox_GetCurSel(_hwnd);
		SendMessage(GetParent(_hwnd), WM_COMMAND, MAKEWPARAM( (WORD)this->_idCtrl, (WORD)CBN_SELCHANGE), (LPARAM)_hwnd);
	}
	return _Flags.B.hasChanged;
}

/**
 * This method writes the combobox's item
 *
 * @param		hContact		- HANDLE of the contact
 * @param		pszProto		- the contact's protocol
 *
 * @return	nothing
 **/
VOID CCombo::OnApply(HANDLE hContact, LPCSTR pszProto)
{
	if (_Flags.B.hasChanged)
	{
		LPCSTR pszModule = hContact ? USERINFO : pszProto;

		if ((_Flags.B.hasCustom || !hContact) && (_curSel != CB_ERR))
		{
			LPIDSTRLIST pd;

			pd = (LPIDSTRLIST)SendMessage(_hwnd, CB_GETITEMDATA, _curSel, 0);
			if (pd != NULL)
			{
				switch (_bDataType)
				{
				case DBVT_BYTE:
					DB::Setting::WriteByte(hContact, pszModule, _pszSetting, pd->nID);
					break;
				case DBVT_WORD:
					DB::Setting::WriteWord(hContact, pszModule, _pszSetting, pd->nID);
					break;
				case DBVT_DWORD:
					DB::Setting::WriteDWord(hContact, pszModule, _pszSetting, pd->nID);
					break;
				case DBVT_ASCIIZ:
				case DBVT_WCHAR:
					DB::Setting::WriteAString(hContact, pszModule, _pszSetting, (LPSTR)pd->pszText);
				}
				if (!hContact)
				{
					_Flags.B.hasCustom = 0;
					_Flags.B.hasProto = 1;
				}
				_Flags.B.hasChanged = 0;
			}
		}
		if (_Flags.B.hasChanged)
		{
			DB::Setting::Delete(hContact, pszModule, _pszSetting);
			_Flags.B.hasChanged = 0;
			OnInfoChanged(hContact, pszProto);
		}
		InvalidateRect(_hwnd, NULL, TRUE);
	}
}

/**
 * The user changed combobox selection, so mark it changed.
 *
 * @return	nothing
 **/
VOID CCombo::OnChangedByUser(WORD wChangedMsg)
{
	if (wChangedMsg == CBN_SELCHANGE)
	{
		INT c = ComboBox_GetCurSel(_hwnd);

		if (_curSel != c)
		{
			if (!_Flags.B.hasChanged)
			{
				_Flags.B.hasChanged = 1;
				_Flags.B.hasCustom = 1;
				SendMessage(GetParent(GetParent(_hwnd)), PSM_CHANGED, 0, 0);
			}
			_curSel = c;
		}
	}
}