aboutsummaryrefslogtreecommitdiff
path: root/Src/Winamp/setup/spage_lang.cpp
blob: 832823119d45e2f7ba5ed85d10604cb08bed5ce9 (plain) (blame)
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
#define APSTUDIO_READONLY_SYMBOLS
#include "main.h"
#include "./spage_lang.h"
#include "./setup_resource.h"
#include "../nu/ns_wc.h"
#include "./langutil.h"

typedef struct _LANGREC
{
	LPWSTR	pszFileName;
	INT		nType;
} LANGREC;


static BOOL CALLBACK AddLangToListBox(ENUMLANG *pel, LPVOID pUser);
static INT ListBox_FindLangFileIndex(HWND hwndLB, LPCWSTR pszLangPath);
static LPCWSTR ListBox_GetSelectedLang(HWND hwndLB, LPWSTR pszLangPath, INT cchLen);


setup_page_lang::setup_page_lang() : ref(1), hwnd(NULL)
{
	*szSelectionPath = 0x00;
}
setup_page_lang::~setup_page_lang()
{
}

size_t setup_page_lang::AddRef()
{
	return ++ref;
}

size_t setup_page_lang::Release()
{
	if (1 == ref) 
	{
		delete(this);
		return 0;
	}
	return --ref;
}

HRESULT setup_page_lang::GetName(bool bShort, const wchar_t **pszName)
{
	static wchar_t szName[64] = {0};
	*pszName = (*szName) ? szName : getStringW(IDS_PAGE_LANGUAGE, szName, sizeof(szName)/sizeof(wchar_t));
	return S_OK;
}

HRESULT setup_page_lang::Save(HWND hwndText)
{
	if (S_FALSE == IsDirty()) return S_OK;

	if (!*szSelectionPath) *config_langpack = 0x00;
	else
	{
		StringCbCopyW(config_langpack, sizeof(config_langpack), szSelectionPath);
	}
	config_save_langpack_var();
	return S_OK;
}

HRESULT setup_page_lang::Revert(void)
{
	HRESULT hr(S_OK);

	if (*config_langpack)
	{
		StringCbCopyW(config_langpack, sizeof(szSelectionPath), config_langpack);
	}
	else szSelectionPath[0] = 0x00;
	
	if (hwnd) 
	{	
		HWND hwndLB = GetDlgItem(hwnd, IDC_LB_LANG);
		INT index = ListBox_FindLangFileIndex(hwndLB, szSelectionPath);
		if (LB_ERR == index && *szSelectionPath) index = ListBox_FindLangFileIndex(hwndLB, NULL); // find default embeded lang
		SendMessageW(hwndLB, LB_SETCURSEL, (LB_ERR != index) ? index : 0, 0L);
	}
	return hr;
}

HRESULT setup_page_lang::IsDirty(void)
{
	INT cr;

	cr = ComparePath(config_langpack, szSelectionPath, LANGDIR);
	if (!cr) return E_UNEXPECTED;
	 
	return (CSTR_EQUAL != cr) ? S_OK : S_FALSE;
}

HRESULT setup_page_lang::Validate(void)
{
	return S_OK;
}

HRESULT setup_page_lang::CreateView(HWND hwndParent, HWND *phwnd)
{
	*phwnd = WACreateDialogParam(MAKEINTRESOURCEW(IDD_SETUP_PAGE_LANG), hwndParent, ::DialogProc, (LPARAM)this);
	return S_OK;
}

void setup_page_lang::ListBox_OnSelChange(HWND hwndCtrl)
{
	ListBox_GetSelectedLang(hwndCtrl, szSelectionPath, sizeof(szSelectionPath)/sizeof(wchar_t));
}

void setup_page_lang::ListBox_OnItemDelete(DELETEITEMSTRUCT *pdis)
{
	LANGREC *pr = (LANGREC*)pdis->itemData;
	if (pr)
	{
		if (pr->pszFileName) free(pr->pszFileName);
		free(pr);
	}
}

INT_PTR setup_page_lang::OnInitDialog(HWND hwndFocus, LPARAM lParam)
{
	INT index;
	HWND hwndLB;
	hwndLB = GetDlgItem(hwnd, IDC_LB_LANG);
	EnumerateLanguages(AddLangToListBox, hwndLB);
	index = ListBox_FindLangFileIndex(hwndLB, szSelectionPath);
	if (LB_ERR == index && *szSelectionPath) index = ListBox_FindLangFileIndex(hwndLB, NULL); // find default embeded lang
	SendMessageW(hwndLB, LB_SETCURSEL, (LB_ERR != index) ? index : 0, 0L);

	PostMessageW(hwnd, WM_COMMAND, MAKEWPARAM(IDC_LB_LANG, LBN_SELCHANGE), (LPARAM)hwndLB);
	return 0;
}

void setup_page_lang::OnSize(UINT nType, INT cx, INT cy)
{
	HWND hwndCtrl;
	RECT rw;

	hwndCtrl = GetDlgItem(hwnd, IDC_LBL_HEADER);
	if (hwndCtrl)
	{
		GetWindowRect(hwndCtrl, &rw);
		MapWindowPoints(HWND_DESKTOP, hwnd, (POINT*)&rw, 2);
		SetWindowPos(hwndCtrl, NULL, 0, 0, cx - rw.left*2, rw.bottom  - rw.top, SWP_NOACTIVATE | SWP_NOZORDER | SWP_NOMOVE);
	}
	hwndCtrl = GetDlgItem(hwnd, IDC_LB_LANG);
	if (hwndCtrl)
	{
		
		GetWindowRect(hwndCtrl, &rw);
		MapWindowPoints(HWND_DESKTOP, hwnd, (POINT*)&rw, 2);
		SetWindowPos(hwndCtrl, NULL, max(0, (cx - (rw.right - rw.left))/2), rw.top, rw.right - rw.left, cy - rw.top - 16, SWP_NOACTIVATE | SWP_NOZORDER);
	}
}

void setup_page_lang::OnCommand(INT nCtrlID, INT nEvntID, HWND hwndCtrl)
{
	switch(nCtrlID)
	{		
		case IDC_LB_LANG:
			switch(nEvntID)
			{
				case LBN_SELCHANGE: ListBox_OnSelChange(hwndCtrl); break;
			}
			break;
	}
}

INT_PTR setup_page_lang::OnDeleteItem(INT nCtrlID, DELETEITEMSTRUCT *pdis)
{
	switch(nCtrlID)
	{		
		case IDC_LB_LANG:  ListBox_OnItemDelete(pdis); return TRUE;
	}
	return 0;
}

INT_PTR setup_page_lang::PageDlgProc(UINT uMsg, WPARAM wParam, LPARAM lParam)
{
	switch(uMsg)
	{
		case WM_INITDIALOG: return OnInitDialog((HWND)wParam, lParam);
		case WM_DESTROY:		break;
		case WM_SIZE:		OnSize((UINT)wParam, LOWORD(lParam), HIWORD(lParam)); break;
		case WM_COMMAND:		OnCommand(LOWORD(wParam), HIWORD(wParam), (HWND)lParam); break;
	}
	return 0;
}

static INT_PTR WINAPI DialogProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
	setup_page_lang *pInst = (setup_page_lang*)GetPropW(hwnd, L"SETUPPAGE");

	switch(uMsg)
	{
		case WM_INITDIALOG:
			pInst = (setup_page_lang*)lParam;
			if (pInst)
			{				
				pInst->hwnd = hwnd;
				SetPropW(hwnd, L"SETUPPAGE", pInst);
			}
			break;
		case WM_DESTROY:
			if (pInst) 
			{	
				pInst->PageDlgProc(uMsg, wParam, lParam);
				RemovePropW(hwnd,  L"SETUPPAGE");
				pInst = NULL;
			}
			break;
	}
	
	return (pInst) ? pInst->PageDlgProc(uMsg, wParam, lParam) : 0;
}

static BOOL CALLBACK AddLangToListBox(ENUMLANG *pel, LPVOID pUser)
{
	INT index;	
	LANGREC  *pr;
	HWND hwndLB = (HWND)pUser;
	if (!hwndLB) return FALSE;

	pr = (LANGREC*)calloc(1, sizeof(LANGREC));
	if (!pr) return FALSE;
	pr->nType = pel->nType;
	if (pel->pszFileName) pr->pszFileName = _wcsdup(pel->pszFileName);
	
	index = (INT)SendMessageW(hwndLB, LB_ADDSTRING, 0, (LPARAM)pel->pszName);
	if (LB_ERR != index) SendMessageW(hwndLB, LB_SETITEMDATA, index,  (LPARAM)pr);
	else 
	{
		if (pr->pszFileName) free(pr->pszFileName);
		free(pr);
	}
	return TRUE;
}

static INT ListBox_FindLangFileIndex(HWND hwndLB, LPCWSTR pszLangPath)
{
	int index, count;

	if (!hwndLB) return LB_ERR;

	index = LB_ERR;

	count = (INT)SendMessageW(hwndLB, LB_GETCOUNT, 0, 0L);
	for (index = 0; index < count; index++)
	{
		LANGREC *pr = (LANGREC*) SendMessageW(hwndLB, LB_GETITEMDATA, index, 0L);
		if (!pr || LB_ERR == (INT)(INT_PTR)pr) continue;
		if (!pszLangPath || !*pszLangPath)
		{
			if (LANG_FILETYPE_EMBED == pr->nType && (!pr->pszFileName || !*pr->pszFileName)) break;
		}
		else
		{
			if (CSTR_EQUAL == ComparePath(pszLangPath, pr->pszFileName, LANGDIR)) break;
		}
	}
	return (count == index) ? LB_ERR : index;
}

static LPCWSTR ListBox_GetSelectedLang(HWND hwndLB, LPWSTR pszLangPath, INT cchLen)
{
	INT index;
	LANGREC *pr;
	if (!hwndLB || !pszLangPath) return NULL;
	index = (INT)SendMessageW(hwndLB, LB_GETCURSEL, 0, 0L);
	if (LB_ERR == index) return NULL;
	pr = (LANGREC*)SendMessageW(hwndLB, LB_GETITEMDATA, index, 0L);
	if (!pr || LB_ERR == (INT)(INT_PTR)pr) return NULL;

	if (!pr->pszFileName || !*pr->pszFileName) pszLangPath[0] = 0x00;
	else if (S_OK != StringCchCopyW(pszLangPath, cchLen, pr->pszFileName)) return NULL;
	return pszLangPath;
}

#ifdef CBCLASS
#undef CBCLASS
#endif

#define CBCLASS setup_page_lang
START_DISPATCH
CB(ADDREF, AddRef)
CB(RELEASE, Release)
CB(API_SETUPPAGE_GET_NAME, GetName)
CB(API_SETUPPAGE_CREATEVIEW, CreateView)
CB(API_SETUPPAGE_SAVE, Save)
CB(API_SETUPPAGE_REVERT, Revert)
CB(API_SETUPPAGE_ISDIRTY, IsDirty)
CB(API_SETUPPAGE_VALIDATE, Validate)
END_DISPATCH
#undef CBCLASS