summaryrefslogtreecommitdiff
path: root/protocols/Quotes/src/OptionDukasCopy.cpp
blob: 6fcc542b09e0b60c01950b90f0af91189bdce94f (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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
#include "StdAfx.h"
#include "OptionDukasCopy.h"
#include "IconLib.h"
#include "ModuleInfo.h"
#include "QuotesProviders.h"
#include "QuotesProviderDukasCopy.h"
#include "resource.h"
#include "EconomicRateInfo.h"
#include "CommonOptionDlg.h"

// extern HANDLE g_hEventSettingsCnanged;

namespace
{
	enum ETreeCheckBoxState
	{
		// tree check box state
		TCBS_NOSTATEBOX = 0,
		TCBS_UNCHECKED = 1,
		TCBS_CHECKED = 2,
	};

	enum
	{
		TREE_VIEW_CHECK_STATE_CHANGE = WM_USER + 100,
		IMAGE_INDEX_SECTION = 0,
		IMAGE_INDEX_QUOTE = 1
	};

	// 	typedef CQuotesProviders::TQuotesProviders TQuotesProviders;
	// 	typedef CQuotesProviders::TQuotesProviderPtr TQuotesProviderPtr;

	HTREEITEM tree_insert_item(HWND hwndTree,
		const tstring& rsName,
		HTREEITEM htiParent,
		int nImage,
		LPARAM lp = 0)
	{
// 		USES_CONVERSION;
		TVINSERTSTRUCT tvi;
		ZeroMemory(&tvi,sizeof(tvi));

		tvi.hParent = htiParent;
		tvi.hInsertAfter = TVI_LAST;
		tvi.item.mask = TVIF_TEXT|TVIF_PARAM|TVIF_IMAGE|TVIF_SELECTEDIMAGE;

// 		CA2T name(rsName.c_str());
		
		tvi.item.pszText = const_cast<LPTSTR>(rsName.c_str());//name;
		tvi.item.lParam = lp;
		tvi.item.iImage = nImage;
		tvi.item.iSelectedImage = nImage;
		return TreeView_InsertItem(hwndTree,&tvi);
	}

	bool add_quote_to_tree(const CQuotesProviderDukasCopy::CQuote& q,HWND hwndTree,HTREEITEM htiParent,const CQuotesProviderDukasCopy* pQuotesProvier)
	{
		bool bChecked = pQuotesProvier->IsQuoteWatched(q);
		HTREEITEM hti = tree_insert_item(hwndTree,((false == q.GetName().empty()) ? q.GetName() : q.GetSymbol()),htiParent,IMAGE_INDEX_QUOTE);
		if(hti && bChecked)
		{
			HWND hDlg = ::GetParent(hwndTree);
			assert(::IsWindow(hDlg));
			::PostMessage(hDlg,TREE_VIEW_CHECK_STATE_CHANGE,MAKEWPARAM(0,TCBS_CHECKED),reinterpret_cast<LPARAM>(hti));
		}

		return (NULL != hti && bChecked);
	}

	void add_section_to_tree(const CQuotesProviderDukasCopy::CQuoteSection& qs,
		HWND hwndTree,
		HTREEITEM htiParent,
		const CQuotesProviderDukasCopy* pQuotesProvier,
		bool& rbIsChecked,
		bool& rbIsExpended,
		bool bExpand = false)
	{
		rbIsChecked = false;
		rbIsExpended = false;
		HTREEITEM hti = tree_insert_item(hwndTree,qs.GetName(),htiParent,IMAGE_INDEX_SECTION);

		size_t cCheckedItems = 0;
		size_t cSection = qs.GetSectionCount();
		for(size_t i = 0;i < cSection;++i)
		{
			bool bIsChecked = false;
			bool bIsExpanded = false;
			CQuotesProviderDukasCopy::CQuoteSection other = qs.GetSection(i);
			add_section_to_tree(other,hwndTree,hti,pQuotesProvier,bIsChecked,bIsExpanded);

			if(bIsChecked)
			{
				++cCheckedItems;
			}

			if(bIsExpanded)
			{
				bExpand = true;
			}
		}

		size_t cQuotes = qs.GetQuoteCount();
		for(size_t i = 0;i < cQuotes;++i)
		{
			CQuotesProviderDukasCopy::CQuote q = qs.GetQuote(i);
			if(true == add_quote_to_tree(q,hwndTree,hti,pQuotesProvier))
			{
				++ cCheckedItems;
			}
		}

		if(bExpand || cCheckedItems > 0)
		{
			rbIsExpended = true;
			TreeView_Expand(hwndTree,hti,TVE_EXPAND);
		}

		if(cCheckedItems == (cSection+cQuotes))
		{
			rbIsChecked = true;
			HWND hDlg = ::GetParent(hwndTree);
			assert(::IsWindow(hDlg));
			::PostMessage(hDlg,TREE_VIEW_CHECK_STATE_CHANGE,MAKEWPARAM(0,TCBS_CHECKED),reinterpret_cast<LPARAM>(hti));
		}
	}

	void add_provider_to_tree(const CQuotesProviderDukasCopy* pQuotesProvier,HWND hwndTree)
	{
		CQuotesProviderDukasCopy::CQuoteSection qs = pQuotesProvier->GetQuotes();
		bool bIsChecked = false;
		bool bIsExpanded = false;
		add_section_to_tree(qs,hwndTree,TVI_ROOT,pQuotesProvier,bIsChecked,bIsExpanded,true);
	}

	inline HTREEITEM tree_get_child_item(HWND hwndTree,HTREEITEM hti)
	{
		return reinterpret_cast<HTREEITEM>(::SendMessage(hwndTree, TVM_GETNEXTITEM, TVGN_CHILD,reinterpret_cast<LPARAM>(hti)));
	}

	inline HTREEITEM tree_get_next_sibling_item(HWND hwndTree,HTREEITEM hti)
	{
		return reinterpret_cast<HTREEITEM>(::SendMessage(hwndTree, TVM_GETNEXTITEM, TVGN_NEXT,reinterpret_cast<LPARAM>(hti)));
	}

	inline ETreeCheckBoxState tree_get_state_image(HWND hwndTree,HTREEITEM hti)
	{
		TVITEM   tvi;
		tvi.hItem = hti;
		tvi.mask = TVIF_STATE|TVIF_HANDLE;
		tvi.stateMask = TVIS_STATEIMAGEMASK;
		if(TRUE == ::SendMessage(hwndTree,TVM_GETITEM,0,reinterpret_cast<LPARAM>(&tvi)))
		{
			UINT nState = (tvi.state >> 12);
			return static_cast<ETreeCheckBoxState>(nState);
		}
		else
		{
			return TCBS_UNCHECKED;
		}
	}

	void tree_do_set_item_state(HWND hwndTree,HTREEITEM hti,ETreeCheckBoxState nState)
	{
		TVITEM tvi;
		ZeroMemory(&tvi,sizeof(tvi));

		tvi.mask = TVIF_STATE|TVIF_HANDLE;
		tvi.hItem = hti;

		tvi.stateMask = TVIS_STATEIMAGEMASK;
		tvi.state = INDEXTOSTATEIMAGEMASK(nState);

		::SendMessage(hwndTree,TVM_SETITEM,0,reinterpret_cast<LPARAM>(&tvi));
	}

	void tree_set_item_state(HWND hwndTree,HTREEITEM hti,ETreeCheckBoxState nState,bool bRecursively)
	{
		if(true == bRecursively)
		{
			for(hti = tree_get_child_item(hwndTree,hti);hti;hti = tree_get_next_sibling_item(hwndTree,hti))
			{
				tree_do_set_item_state(hwndTree,hti,nState);
				tree_set_item_state(hwndTree,hti,nState,bRecursively);
			}
		}
		else
		{
			tree_do_set_item_state(hwndTree,hti,nState);
		}
	}

	void save_quote_selection(HWND hwndTree,HTREEITEM h,const CQuotesProviderDukasCopy::CQuote& q,CQuotesProviderDukasCopy* pQuotesProvier)
	{
		ETreeCheckBoxState nState = tree_get_state_image(hwndTree,h);
		pQuotesProvier->WatchForQuote(q,(TCBS_CHECKED == nState));
	}

	void recursive_save_quote_section_selection(HWND hwndTree,HTREEITEM h,const CQuotesProviderDukasCopy::CQuoteSection& qs,CQuotesProviderDukasCopy* pQuotesProvier)
	{
		size_t cSection = qs.GetSectionCount();
		h = tree_get_child_item(hwndTree,h);
		for(size_t i = 0;h && (i < cSection);++i,h = tree_get_next_sibling_item(hwndTree,h))
		{
			CQuotesProviderDukasCopy::CQuoteSection other = qs.GetSection(i);
			recursive_save_quote_section_selection(hwndTree,h,other,pQuotesProvier);
		}

		size_t cQuotes = qs.GetQuoteCount();
		for(size_t i = 0;h && (i < cQuotes);++i,h = tree_get_next_sibling_item(hwndTree,h))
		{
			CQuotesProviderDukasCopy::CQuote q = qs.GetQuote(i);
			save_quote_selection(hwndTree,h,q,pQuotesProvier);
		}
	}

	void recursive_save_selection(HWND hwndTree,CQuotesProviderDukasCopy* pQuotesProvider)
	{
		// 		CModuleInfo::TQuotesProvidersPtr& pProviders = CModuleInfo::GetQuoteProvidersPtr();
		// 		const TQuotesProviders& rapQuotesProviders = pProviders->GetProviders();
		// 
		// 		TQuotesProviders::const_iterator i = rapQuotesProviders.begin();
		// 		TQuotesProviders::const_iterator iE = rapQuotesProviders.end();
		// 		for(HTREEITEM h = tree_get_child_item(hwndTree,TVI_ROOT);h && (i!=iE);++i,h = tree_get_next_sibling_item(hwndTree,h))
		// 		{
		// 			const TQuotesProviderPtr& pQuotesProvier = *i;
		CQuotesProviderDukasCopy::CQuoteSection qs = pQuotesProvider->GetQuotes();
		recursive_save_quote_section_selection(hwndTree,tree_get_child_item(hwndTree,TVI_ROOT),qs,pQuotesProvider);
		// 		}
	}

	class CImageListWrapper
	{
	public:
		CImageListWrapper()
			: m_hImageList(ImageList_Create(::GetSystemMetrics(SM_CXSMICON),
			::GetSystemMetrics(SM_CYSMICON),
			ILC_COLOR24|ILC_MASK,2,0))
		{
			if(m_hImageList)
			{
				ImageList_AddIcon(m_hImageList,Quotes_LoadIconEx(ICON_STR_SECTION));
				ImageList_AddIcon(m_hImageList,Quotes_LoadIconEx(ICON_STR_QUOTE));
			}
		}

		~CImageListWrapper()
		{
			if(m_hImageList)
			{
				ImageList_Destroy(m_hImageList);
			}
		}

		operator HIMAGELIST()const
		{
			return m_hImageList;
		}

	private:
		HIMAGELIST m_hImageList;
	};

	HIMAGELIST get_image_list()
	{
		static CImageListWrapper wrapper;
		return wrapper;
	}

	CQuotesProviderDukasCopy* get_dukas_copy_provider()
	{
		CModuleInfo::TQuotesProvidersPtr& pProviders = CModuleInfo::GetQuoteProvidersPtr();
		const CQuotesProviders::TQuotesProviders& rapQuotesProviders = pProviders->GetProviders();
		for(CQuotesProviders::TQuotesProviders::const_iterator i = rapQuotesProviders.begin();i != rapQuotesProviders.end();++i)
		{
			const CQuotesProviders::TQuotesProviderPtr& pProvider = *i;
			CQuotesProviderDukasCopy* pDukas = dynamic_cast<CQuotesProviderDukasCopy*>(pProvider.get());
			if(pDukas)
			{
				return pDukas;
			}
		}

		assert(!"We should never get here!");
		return NULL;
	}

	INT_PTR CALLBACK EconomicRatesDlgProc(HWND hdlg,UINT message,WPARAM wParam,LPARAM lParam)
	{
		CCommonDlgProcData d(get_dukas_copy_provider());
		CommonOptionDlgProc(hdlg,message,wParam,lParam,d);

		switch(message)
		{
		case WM_INITDIALOG:
			{
				TranslateDialogDefault(hdlg);

				HWND hwndTree = ::GetDlgItem(hdlg,IDC_TREE_ECONOMIC_RATES);
				HIMAGELIST hImage = get_image_list();
				if(hImage)
				{
					TreeView_SetImageList(hwndTree,hImage,TVSIL_NORMAL);
				}

				const CQuotesProviderDukasCopy* pDukasProvider = get_dukas_copy_provider();
				if(pDukasProvider)
				{
					add_provider_to_tree(pDukasProvider,hwndTree);
				}
				// 			Window_SetIcon_IcoLib(hdlg, SKINICON_OTHER_MIRANDA);
			}
			return TRUE;

		case WM_NOTIFY:
			{
				LPNMHDR pNMHDR = reinterpret_cast<LPNMHDR>(lParam);
				switch(pNMHDR->code)
				{
				case TVN_KEYDOWN:
					if(IDC_TREE_ECONOMIC_RATES == wParam)
					{
						LPNMTVKEYDOWN pKeyDown = reinterpret_cast<LPNMTVKEYDOWN>(lParam);
						if(VK_SPACE == pKeyDown->wVKey)
						{
							HTREEITEM hti = TreeView_GetSelection(::GetDlgItem(hdlg,IDC_TREE_ECONOMIC_RATES));
							::PostMessage(hdlg,TREE_VIEW_CHECK_STATE_CHANGE,MAKEWPARAM(1,0),reinterpret_cast<LPARAM>(hti));
							PropSheet_Changed(::GetParent(hdlg),hdlg);
						}
					}
					break;
				case NM_CLICK:
					if(IDC_TREE_ECONOMIC_RATES == wParam)
					{
						DWORD pos = ::GetMessagePos();

						HWND hwndTree = ::GetDlgItem(hdlg,IDC_TREE_ECONOMIC_RATES);

						TVHITTESTINFO tvhti;
						tvhti.pt.x = LOWORD(pos);
						tvhti.pt.y = HIWORD(pos);
						::ScreenToClient(hwndTree,&(tvhti.pt));

						HTREEITEM hti = reinterpret_cast<HTREEITEM>(::SendMessage(hwndTree,TVM_HITTEST,0,reinterpret_cast<LPARAM>(&tvhti)));
						if(hti && (tvhti.flags&TVHT_ONITEMSTATEICON))
						{
							::PostMessage(hdlg,TREE_VIEW_CHECK_STATE_CHANGE,MAKEWPARAM(1,0),reinterpret_cast<LPARAM>(hti));
							PropSheet_Changed(::GetParent(hdlg),hdlg);
						}
					}
					break;
				case PSN_APPLY:
					{
						CQuotesProviderDukasCopy* pDukasProvider = get_dukas_copy_provider();
						if(pDukasProvider)
						{
							recursive_save_selection(::GetDlgItem(hdlg,IDC_TREE_ECONOMIC_RATES),pDukasProvider);
							pDukasProvider->RefreshAll();
						}
					}
					break;
				}
			}
			return TRUE;

		case TREE_VIEW_CHECK_STATE_CHANGE:
			{
				HWND hwndTree = ::GetDlgItem(hdlg,IDC_TREE_ECONOMIC_RATES);
				HTREEITEM hti = reinterpret_cast<HTREEITEM>(lParam);

				ETreeCheckBoxState nState;

				bool bRecursively = 1 == LOWORD(wParam);
				if(bRecursively)
				{
					nState = tree_get_state_image(hwndTree,hti);
				}
				else
				{
					nState = static_cast<ETreeCheckBoxState>(HIWORD(wParam));
				}

				tree_set_item_state(hwndTree,hti,nState,bRecursively);
			}
			break;
			// 		case WM_CLOSE:
			// 			DestroyWindow(hdlg);
			// 			break;
			// 		case WM_DESTROY:
			// 			g_hwndEconomicRates = NULL;
			// 			break;
		}

		return FALSE;
	}
}

void ShowDukasCopyPropPage(CQuotesProviderDukasCopy* pProvider,WPARAM wp,OPTIONSDIALOGPAGE& odp)
{
	const IQuotesProvider::CProviderInfo& pi = pProvider->GetInfo();

	odp.pszTemplate = MAKEINTRESOURCEA(IDD_DIALOG_ECONOMIC_RATES);
	odp.pfnDlgProc = EconomicRatesDlgProc;
// #if MIRANDA_VER >= 0x0600
	//odp.ptszTab = TranslateTS(const_cast<LPTSTR>(pi.m_sName.c_str()));
	odp.ptszTab = const_cast<LPTSTR>(pi.m_sName.c_str());
// #else
// 	tostringstream o;
// 	o << TranslateTS(QUOTES_PROTOCOL_NAME) << _T(" - ") << TranslateTS(pi.m_sName.c_str());
// 	tstring sTitle = o.str();
// 	odp.ptszTitle = TranslateTS(const_cast<LPTSTR>(sTitle.c_str()));
// #endif

	Options_AddPage(wp, &odp);
}