summaryrefslogtreecommitdiff
path: root/plugins/TopToolBar/buttonopt.cpp
blob: 99f7c0f9deca90facb310f6b3476761d3ccc4cfb (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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
#include "common.h"

#define OFFSET_PROTOPOS 200
#define OFFSET_VISIBLE 400

extern HINSTANCE hInst;

struct OrderData {
	int dragging;
	HTREEITEM hDragItem;
};

char *MyDbGetString(HANDLE hContact, const char *szModule, const char *szSetting)
{
	char *str = NULL;
	DBVARIANT dbv;
	DBGetContactSetting(hContact, szModule, szSetting, &dbv);
	if (dbv.type == DBVT_ASCIIZ)
		str = strdup(dbv.pszVal);
	DBFreeVariant(&dbv);
	return str;
}

int CheckButtonOrder()
{
	bool protochanged = FALSE;
	char buf[10];
	char buf2[10];
	
	int StoredButCount = DBGetContactSettingByte(0, TTB_OPTDIR, "ButCount", -1);
	if (StoredButCount == -1)
		protochanged = TRUE;
	if (protochanged) {
		//reseting all settings;
		PROTOCOLDESCRIPTOR **protos;
		int count;
		CallService(MS_PROTO_ENUMPROTOCOLS, (WPARAM)&count, (LPARAM)&protos);

		int v = 0;
		for (int i = 0;i<count;i++) {
			if (protos[i]->type != PROTOTYPE_PROTOCOL || CallProtoService(protos[i]->szName, PS_GETCAPS, PFLAGNUM_2, 0) == 0) continue;
			itoa(v, buf, 10);			
			DBWriteContactSettingString(0, "Protocols", buf, protos[i]->szName);		

			itoa(OFFSET_VISIBLE+v, buf, 10);//save default visible status
			DBWriteContactSettingByte(0, "Protocols", buf, 1);
			v++;
		}

		DBWriteContactSettingByte(0, TTB_OPTDIR, "ButCount", v);
		return 1;
	}

	return 0;
}

static BOOL CALLBACK ProtocolOrderOpts(HWND hwndDlg,  UINT msg,  WPARAM wParam,  LPARAM lParam)
{
	ProtocolOrderData *dat = (ProtocolOrderData*)GetWindowLongPtr(GetDlgItem(hwndDlg, IDC_PROTOCOLORDER), GWL_USERDATA);

	switch (msg) {
	case WM_INITDIALOG: {
		PROTOCOLDESCRIPTOR **protos;
		TVINSERTSTRUCT tvis;
		ProtocolData *PD;
		char szName[64];
		char *szSTName;
		char buf[10];
		int i, count;

		dat = (struct ProtocolOrderData*)malloc(sizeof(struct ProtocolOrderData));
		SetWindowLong(GetDlgItem(hwndDlg, IDC_PROTOCOLORDER), GWL_USERDATA, (LONG)dat);
		dat->dragging = 0;

		SetWindowLong(GetDlgItem(hwndDlg, IDC_PROTOCOLVISIBILITY), GWL_STYLE, GetWindowLongPtr(GetDlgItem(hwndDlg, IDC_PROTOCOLVISIBILITY), GWL_STYLE)|TVS_NOHSCROLL);
		SetWindowLong(GetDlgItem(hwndDlg, IDC_PROTOCOLORDER), GWL_STYLE, GetWindowLongPtr(GetDlgItem(hwndDlg, IDC_PROTOCOLORDER), GWL_STYLE)|TVS_NOHSCROLL);

		{	HIMAGELIST himlCheckBoxes;
			himlCheckBoxes = ImageList_Create(GetSystemMetrics(SM_CXSMICON), GetSystemMetrics(SM_CYSMICON), ILC_COLOR4|ILC_MASK, 2, 2);
			ImageList_AddIcon(himlCheckBoxes, LoadIcon(GetModuleHandle(NULL), MAKEINTRESOURCE(IDI_NOTICK)));
			ImageList_AddIcon(himlCheckBoxes, LoadIcon(GetModuleHandle(NULL), MAKEINTRESOURCE(IDI_TICK)));
			TreeView_SetImageList(GetDlgItem(hwndDlg, IDC_PROTOCOLVISIBILITY), himlCheckBoxes, TVSIL_NORMAL);
			//
			TreeView_SetImageList(GetDlgItem(hwndDlg, IDC_PROTOCOLORDER), himlCheckBoxes, TVSIL_NORMAL);

		}

		tvis.hParent = NULL;
		tvis.hInsertAfter = TVI_LAST;
		tvis.item.mask = TVIF_PARAM|TVIF_TEXT|TVIF_IMAGE|TVIF_SELECTEDIMAGE;	
			
		CheckProtocolOrder();

		count = DBGetContactSettingByte(0, "Protocols", "ProtoCount", -1);
		if (count == -1){return(FALSE);}

		for (i = 0;i<count;i++) {
			//if (protos[i]->type != PROTOTYPE_PROTOCOL || CallProtoService(protos[i]->szName, PS_GETCAPS, PFLAGNUM_2, 0) == 0) continue;
			itoa(i, &buf, 10);
			szSTName = MyDbGetString(0, "Protocols", &buf);
			if (szSTName == NULL){continue;}
				
			CallProtoService(szSTName, PS_GETNAME, sizeof(szName), (LPARAM)szName);
			PD = (ProtocolData*)malloc(sizeof(ProtocolData));
				
				
			PD->RealName = szSTName;
				
			itoa(OFFSET_VISIBLE+i, buf, 10);
			PD->show = DBGetContactSettingByte(0, "Protocols", buf, 1);

			itoa(OFFSET_PROTOPOS+i, buf, 10);
			PD->protopos = DBGetContactSettingByte(0, "Protocols", buf, -1);
					
			tvis.item.lParam = (LPARAM)PD;
			tvis.item.pszText = Translate(szName);
			tvis.item.iImage = tvis.item.iSelectedImage = PD->show;
			TreeView_InsertItem(GetDlgItem(hwndDlg, IDC_PROTOCOLORDER), &tvis);
			tvis.item.iImage = tvis.item.iSelectedImage = PD->show;
			TreeView_InsertItem(GetDlgItem(hwndDlg, IDC_PROTOCOLVISIBILITY), &tvis);
				
			//free(szSTName);
		}
		return TRUE;
	}
	case WM_NOTIFY:
		switch(((LPNMHDR)lParam)->idFrom) {
			case 0: 
				switch (((LPNMHDR)lParam)->code)
				{
					case PSN_APPLY:
					{	
						TVITEM tvi;
						PROTOCOLDESCRIPTOR **protos;
						int count;
						char idstr[33];
						char buf[10];

						/*
						tvi.hItem = TreeView_GetRoot(GetDlgItem(hwndDlg, IDC_PROTOCOLVISIBILITY));
						tvi.cchTextMax = 32;
						tvi.mask = TVIF_TEXT|TVIF_IMAGE|TVIF_PARAM|TVIF_HANDLE;
						tvi.pszText = &idstr;
						//count = 0;
						while(tvi.hItem != NULL) {
							itoa(OFFSET_VISIBLE+count, &buf, 10);
							TreeView_GetItem(GetDlgItem(hwndDlg, IDC_PROTOCOLVISIBILITY), &tvi);
							DBWriteContactSettingByte(NULL, "Protocols", &buf, (byte)tvi.iImage);
							tvi.hItem = TreeView_GetNextSibling(GetDlgItem(hwndDlg, IDC_PROTOCOLVISIBILITY), tvi.hItem);
							//count++;
						}
						*/
						tvi.hItem = TreeView_GetRoot(GetDlgItem(hwndDlg, IDC_PROTOCOLORDER));
						tvi.cchTextMax = 32;
						tvi.mask = TVIF_TEXT|TVIF_PARAM|TVIF_HANDLE;
						tvi.pszText = &idstr;
						//CallService(MS_PROTO_ENUMPROTOCOLS, (WPARAM)&count, (LPARAM)&protos);
						//count--;
						count = 0;

						while(tvi.hItem != NULL) {
							itoa(count, buf, 10);
							TreeView_GetItem(GetDlgItem(hwndDlg, IDC_PROTOCOLORDER), &tvi);
							DBWriteContactSettingString(NULL, "Protocols", &buf, ((ProtocolData *)tvi.lParam)->RealName);
								
							itoa(OFFSET_PROTOPOS+count, &buf, 10);//save pos in protos
							DBWriteContactSettingByte(0, "Protocols", &buf, ((ProtocolData *)tvi.lParam)->protopos);

							itoa(OFFSET_VISIBLE+count, &buf, 10);//save pos in protos
							DBWriteContactSettingByte(0, "Protocols", &buf, ((ProtocolData *)tvi.lParam)->show);
								
							tvi.hItem = TreeView_GetNextSibling(GetDlgItem(hwndDlg, IDC_PROTOCOLORDER), tvi.hItem);
								
							count++;
						}
						CluiProtocolStatusChanged(0, 0);
					}
				}
				break;
				/*
				case IDC_PROTOCOLORDER: //IDC_PROTOCOLVISIBILITY:
				if (((LPNMHDR)lParam)->code == NM_CLICK) {
					TVHITTESTINFO hti;
					hti.pt.x = (short)LOWORD(GetMessagePos());
					hti.pt.y = (short)HIWORD(GetMessagePos());
					ScreenToClient(((LPNMHDR)lParam)->hwndFrom, &hti.pt);
					if (TreeView_HitTest(((LPNMHDR)lParam)->hwndFrom, &hti))
						if (hti.flags&TVHT_ONITEMICON) {
							TVITEM tvi;
							tvi.mask = TVIF_HANDLE|TVIF_IMAGE|TVIF_SELECTEDIMAGE;
							tvi.hItem = hti.hItem;
							TreeView_GetItem(((LPNMHDR)lParam)->hwndFrom, &tvi);
							tvi.iImage = tvi.iSelectedImage = !tvi.iImage;
							((ProtocolData *)tvi.lParam)->show = tvi.iImage;
							TreeView_SetItem(((LPNMHDR)lParam)->hwndFrom, &tvi);
							SendMessage(GetParent(hwndDlg),  PSM_CHANGED,  0,  0);
							ShowWindow(GetDlgItem(hwndDlg, IDC_PROTOCOLORDERWARNING), SW_SHOW);
						}
				}
				break;
				*/
			case IDC_PROTOCOLORDER:
				switch (((LPNMHDR)lParam)->code) {
					case TVN_BEGINDRAG:
						SetCapture(hwndDlg);
						dat->dragging = 1;
						dat->hDragItem = ((LPNMTREEVIEW)lParam)->itemNew.hItem;
						TreeView_SelectItem(GetDlgItem(hwndDlg, IDC_PROTOCOLORDER), dat->hDragItem);
						//ShowWindow(GetDlgItem(hwndDlg, IDC_PROTOCOLORDERWARNING), SW_SHOW);
						break;
					case NM_CLICK:
						{
							
					TVHITTESTINFO hti;
					hti.pt.x = (short)LOWORD(GetMessagePos());
					hti.pt.y = (short)HIWORD(GetMessagePos());
					ScreenToClient(((LPNMHDR)lParam)->hwndFrom, &hti.pt);
					if (TreeView_HitTest(((LPNMHDR)lParam)->hwndFrom, &hti))
						if (hti.flags&TVHT_ONITEMICON) {
							TVITEM tvi;
							tvi.mask = TVIF_HANDLE|TVIF_IMAGE|TVIF_SELECTEDIMAGE;
							tvi.hItem = hti.hItem;
							TreeView_GetItem(((LPNMHDR)lParam)->hwndFrom, &tvi);
							tvi.iImage = tvi.iSelectedImage = !tvi.iImage;
							((ProtocolData *)tvi.lParam)->show = tvi.iImage;
							TreeView_SetItem(((LPNMHDR)lParam)->hwndFrom, &tvi);
							SendMessage(GetParent(hwndDlg),  PSM_CHANGED,  0,  0);
								
							//all changes take effect in runtime
							//ShowWindow(GetDlgItem(hwndDlg, IDC_PROTOCOLORDERWARNING), SW_SHOW);
						}
							
							
							
						}
					}
				break;
		}
		break;
	case WM_MOUSEMOVE:
		if (!dat->dragging) break;
		{	TVHITTESTINFO hti;
			hti.pt.x = (short)LOWORD(lParam);
			hti.pt.y = (short)HIWORD(lParam);
			ClientToScreen(hwndDlg, &hti.pt);
			ScreenToClient(GetDlgItem(hwndDlg, IDC_PROTOCOLORDER), &hti.pt);
			TreeView_HitTest(GetDlgItem(hwndDlg, IDC_PROTOCOLORDER), &hti);
			if (hti.flags&(TVHT_ONITEM|TVHT_ONITEMRIGHT)) {
				hti.pt.y -= TreeView_GetItemHeight(GetDlgItem(hwndDlg, IDC_PROTOCOLORDER))/2;
				TreeView_HitTest(GetDlgItem(hwndDlg, IDC_PROTOCOLORDER), &hti);
				TreeView_SetInsertMark(GetDlgItem(hwndDlg, IDC_PROTOCOLORDER), hti.hItem, 1);
			}
			else {
				if (hti.flags&TVHT_ABOVE) SendDlgItemMessage(hwndDlg, IDC_PROTOCOLORDER, WM_VSCROLL, MAKEWPARAM(SB_LINEUP, 0), 0);
				if (hti.flags&TVHT_BELOW) SendDlgItemMessage(hwndDlg, IDC_PROTOCOLORDER, WM_VSCROLL, MAKEWPARAM(SB_LINEDOWN, 0), 0);
				TreeView_SetInsertMark(GetDlgItem(hwndDlg, IDC_PROTOCOLORDER), NULL, 0);
			}
		}
		break;
	case WM_LBUTTONUP:
		if (!dat->dragging) break;
		TreeView_SetInsertMark(GetDlgItem(hwndDlg, IDC_PROTOCOLORDER), NULL, 0);
		dat->dragging = 0;
		ReleaseCapture();
		{	TVHITTESTINFO hti;
			TVITEM tvi;
			hti.pt.x = (short)LOWORD(lParam);
			hti.pt.y = (short)HIWORD(lParam);
			ClientToScreen(hwndDlg, &hti.pt);
			ScreenToClient(GetDlgItem(hwndDlg, IDC_PROTOCOLORDER), &hti.pt);
			hti.pt.y -= TreeView_GetItemHeight(GetDlgItem(hwndDlg, IDC_PROTOCOLORDER))/2;
			TreeView_HitTest(GetDlgItem(hwndDlg, IDC_PROTOCOLORDER), &hti);
			if (dat->hDragItem == hti.hItem) break;
			tvi.mask = TVIF_HANDLE|TVIF_PARAM;
			tvi.hItem = hti.hItem;
			TreeView_GetItem(GetDlgItem(hwndDlg, IDC_PROTOCOLORDER), &tvi);
			if (hti.flags&(TVHT_ONITEM|TVHT_ONITEMRIGHT)) {
				TVINSERTSTRUCT tvis;
				char name[128];
				tvis.item.mask = TVIF_HANDLE|TVIF_PARAM|TVIF_TEXT|TVIF_IMAGE|TVIF_SELECTEDIMAGE;
				tvis.item.stateMask = 0xFFFFFFFF;
				tvis.item.pszText = name;
				tvis.item.cchTextMax = sizeof(name);
				tvis.item.hItem = dat->hDragItem;
				//
				tvis.item.iImage = tvis.item.iSelectedImage = ((ProtocolData *)tvi.lParam)->show;
					
				TreeView_GetItem(GetDlgItem(hwndDlg, IDC_PROTOCOLORDER), &tvis.item);
					

				TreeView_DeleteItem(GetDlgItem(hwndDlg, IDC_PROTOCOLORDER), dat->hDragItem);
				tvis.hParent = NULL;
				tvis.hInsertAfter = hti.hItem;
				TreeView_SelectItem(GetDlgItem(hwndDlg, IDC_PROTOCOLORDER), TreeView_InsertItem(GetDlgItem(hwndDlg, IDC_PROTOCOLORDER), &tvis));
				SendMessage(GetParent(hwndDlg),  PSM_CHANGED,  0,  0);
			}
		}
		break; 
	}
	return FALSE;
}

static int ProtocolOrderInit(WPARAM wParam, LPARAM lParam) {
	OPTIONSDIALOGPAGE odp;

	ZeroMemory(&odp, sizeof(odp));
	odp.cbSize = sizeof(odp);
	odp.position = -1000000000;
	odp.hInstance = hInst;//GetModuleHandle(NULL);
	odp.pszTemplate = MAKEINTRESOURCE(IDD_OPT_PROTOCOLORDER);
	odp.pszGroup = Translate("Contact List");
	odp.pszTitle = Translate("Protocols");
	odp.pfnDlgProc = ProtocolOrderOpts;
	odp.flags = ODPF_BOLDGROUPS|ODPF_EXPERTONLY;
	CallService(MS_OPT_ADDPAGE, wParam, (LPARAM)&odp);

	return 0;
}

int LoadProtocolOrderModule(void) {
	HookEvent(ME_OPT_INITIALISE, ProtocolOrderInit);
	return 0;
}