d8333762d5dcd132c34e9a00cc296a86903873b4
[reactos.git] / reactos / dll / cpl / intl / sort.c
1 /*
2 * PROJECT: ReactOS International Control Panel
3 * FILE: dll/cpl/intl/sort.c
4 * PURPOSE: Sorting property page
5 * PROGRAMMER: Eric Kohl
6 */
7
8 #include "intl.h"
9
10 static BOOL bSortPage = FALSE;
11 static LCID userLcid;
12
13 static HWND hWndSortList = NULL;
14
15
16 static BOOL CALLBACK
17 SortTestEnumProc(PWSTR lpLocale)
18 {
19 LCID lcid;
20
21 lcid = wcstoul(lpLocale, NULL, 16);
22
23 if ((LANGIDFROMLCID(lcid) == LANGIDFROMLCID(userLcid)) &&
24 (SORTIDFROMLCID(lcid) != SORTIDFROMLCID(userLcid)))
25 bSortPage = TRUE;
26
27 return TRUE;
28 }
29
30
31 BOOL
32 IsSortPageNeeded(LCID lcid)
33 {
34 /* Handle special case for Spanish (Spain) */
35 if (lcid == MAKELCID(MAKELANGID(LANG_SPANISH, SUBLANG_SPANISH), SORT_DEFAULT) ||
36 lcid == MAKELCID(MAKELANGID(LANG_SPANISH, SUBLANG_SPANISH_MODERN), SORT_DEFAULT))
37 return TRUE;
38
39 userLcid = lcid;
40 bSortPage = FALSE;
41
42 EnumSystemLocalesW(SortTestEnumProc, LCID_ALTERNATE_SORTS);
43
44 return bSortPage;
45 }
46
47
48 static BOOL CALLBACK
49 SortEnumProc(LPTSTR lpLocale)
50 {
51 LCID lcid;
52 WCHAR lang[255];
53 INT index;
54
55 lcid = wcstoul(lpLocale, NULL, 16);
56
57 if ((LANGIDFROMLCID(lcid) == LANGIDFROMLCID(userLcid)) &&
58 (SORTIDFROMLCID(lcid) != SORTIDFROMLCID(userLcid)))
59 {
60 GetLocaleInfoW(lcid, LOCALE_SSORTNAME, lang, sizeof(lang)/sizeof(WCHAR));
61
62 index = SendMessageW(hWndSortList,
63 CB_ADDSTRING,
64 0,
65 (LPARAM)lang);
66
67 SendMessageW(hWndSortList,
68 CB_SETITEMDATA,
69 index,
70 (LPARAM)lcid);
71 }
72
73 return TRUE;
74 }
75
76 static VOID
77 CreateSortList(HWND hwnd, LCID lcid)
78 {
79 WCHAR lang[255];
80 INT index;
81
82 hWndSortList = hwnd;
83
84 /* Handle special case for Spanish (Spain) */
85 if (lcid == MAKELCID(MAKELANGID(LANG_SPANISH, SUBLANG_SPANISH), SORT_DEFAULT) ||
86 lcid == MAKELCID(MAKELANGID(LANG_SPANISH, SUBLANG_SPANISH_MODERN), SORT_DEFAULT))
87 {
88 /* Add traditional sorting */
89 GetLocaleInfoW(MAKELANGID(LANG_SPANISH, SUBLANG_SPANISH),
90 LOCALE_SSORTNAME, lang, sizeof(lang)/sizeof(TCHAR));
91
92 index = SendMessageW(hwnd,
93 CB_ADDSTRING,
94 0,
95 (LPARAM)lang);
96
97 SendMessageW(hwnd,
98 CB_SETITEMDATA,
99 index,
100 (LPARAM)MAKELANGID(LANG_SPANISH, SUBLANG_SPANISH));
101
102 /* Add modern sorting */
103 GetLocaleInfoW(MAKELANGID(LANG_SPANISH, SUBLANG_SPANISH_MODERN),
104 LOCALE_SSORTNAME, lang, sizeof(lang)/sizeof(TCHAR));
105
106 index = SendMessageW(hwnd,
107 CB_ADDSTRING,
108 0,
109 (LPARAM)lang);
110
111 SendMessageW(hwnd,
112 CB_SETITEMDATA,
113 index,
114 (LPARAM)MAKELANGID(LANG_SPANISH, SUBLANG_SPANISH_MODERN));
115 }
116 else
117 {
118 userLcid = lcid;
119
120 GetLocaleInfoW(lcid & 0xFFFF, LOCALE_SSORTNAME, lang, sizeof(lang)/sizeof(WCHAR));
121
122 index = SendMessageW(hWndSortList,
123 CB_ADDSTRING,
124 0,
125 (LPARAM)lang);
126
127 SendMessageW(hWndSortList,
128 CB_SETITEMDATA,
129 index,
130 (LPARAM)lcid & 0xFFFF);
131
132 EnumSystemLocalesW(SortEnumProc, LCID_ALTERNATE_SORTS);
133 }
134
135 /* Select current locale */
136 /* or should it be System and not user? */
137 GetLocaleInfoW(lcid, LOCALE_SSORTNAME, lang, sizeof(lang)/sizeof(WCHAR));
138
139 SendMessageW(hwnd,
140 CB_SELECTSTRING,
141 -1,
142 (LPARAM)lang);
143 }
144
145 /* Property page dialog callback */
146 INT_PTR CALLBACK
147 SortPageProc(HWND hwndDlg,
148 UINT uMsg,
149 WPARAM wParam,
150 LPARAM lParam)
151 {
152 PGLOBALDATA pGlobalData;
153
154 pGlobalData = (PGLOBALDATA)GetWindowLongPtr(hwndDlg, DWLP_USER);
155
156 switch (uMsg)
157 {
158 case WM_INITDIALOG:
159 pGlobalData = (PGLOBALDATA)((LPPROPSHEETPAGE)lParam)->lParam;
160 SetWindowLongPtr(hwndDlg, DWLP_USER, (LONG_PTR)pGlobalData);
161
162 CreateSortList(GetDlgItem(hwndDlg, IDC_SORTLIST_COMBO), pGlobalData->UserLCID);
163 break;
164
165 case WM_COMMAND:
166 switch (LOWORD(wParam))
167 {
168 case IDC_SORTLIST_COMBO:
169 if (HIWORD(wParam) == CBN_SELCHANGE)
170 {
171 PropSheet_Changed(GetParent(hwndDlg), hwndDlg);
172 }
173 break;
174 }
175 break;
176
177 case WM_NOTIFY:
178 if (((LPNMHDR)lParam)->code == (UINT)PSN_APPLY)
179 {
180 LCID NewLcid;
181 INT iCurSel;
182
183 iCurSel = SendDlgItemMessage(hwndDlg,
184 IDC_SORTLIST_COMBO,
185 CB_GETCURSEL,
186 0,
187 0);
188 if (iCurSel == CB_ERR)
189 break;
190
191 NewLcid = SendDlgItemMessage(hwndDlg,
192 IDC_SORTLIST_COMBO,
193 CB_GETITEMDATA,
194 iCurSel,
195 0);
196 if (NewLcid == (LCID)CB_ERR)
197 break;
198
199 /* Save the new LCID */
200 pGlobalData->UserLCID = NewLcid;
201 pGlobalData->fUserLocaleChanged = TRUE;
202 }
203 break;
204 }
205
206 return FALSE;
207 }
208
209 /* EOF */