* Sync up to trunk head (r65394).
[reactos.git] / dll / cpl / intl / misc.c
1 #include "intl.h"
2
3 #define NUM_SHEETS 4
4
5 /* Insert the space */
6 LPTSTR
7 InsSpacePos(LPCTSTR szInsStr, const int nPos)
8 {
9 LPTSTR pszDestStr;
10 INT nDestStrCnt = 0;
11 INT nStrCnt;
12 INT nStrSize;
13
14 pszDestStr = (LPTSTR)malloc(MAX_SAMPLES_STR_SIZE * sizeof(TCHAR));
15
16 _tcscpy(pszDestStr, szInsStr);
17
18 nStrSize = _tcslen(szInsStr);
19
20 for (nStrCnt = 0; nStrCnt < nStrSize; nStrCnt++)
21 {
22 if (nStrCnt == nStrSize - nPos)
23 {
24 pszDestStr[nDestStrCnt] = _T(' ');
25 nDestStrCnt++;
26 }
27
28 pszDestStr[nDestStrCnt] = szInsStr[nStrCnt];
29 nDestStrCnt++;
30 }
31
32 pszDestStr[nDestStrCnt] = _T('\0');
33
34 return pszDestStr;
35 }
36
37 /* Insert the spaces by format string separated by ';' */
38 LPTSTR
39 InsSpacesFmt(LPCTSTR szSourceStr, LPCTSTR szFmtStr)
40 {
41 LPTSTR pszDestStr;
42 LPTSTR pszTempStr;
43 TCHAR szFmtVal[255];
44 UINT nFmtCount = 0;
45 INT nValCount = 0;
46 INT nLastVal = 0;
47 INT nSpaceOffset = 0;
48 BOOL wasNul=FALSE;
49
50 pszDestStr = (LPTSTR)malloc(255 * sizeof(TCHAR));
51
52 _tcscpy(pszDestStr, szSourceStr);
53
54 /* If format is clean return source string */
55 if (!*szFmtStr)
56 return pszDestStr;
57
58 /* Search for all format values */
59 for (nFmtCount = 0; nFmtCount <= _tcslen(szFmtStr); nFmtCount++)
60 {
61 if (szFmtStr[nFmtCount] == _T(';') || szFmtStr[nFmtCount] == _T('\0'))
62 {
63 if (_ttoi(szFmtVal) == 0 && !wasNul)
64 {
65 wasNul=TRUE;
66 break;
67 }
68
69 /* If was 0, repeat spaces */
70 if (wasNul)
71 {
72 nSpaceOffset += nLastVal;
73 }
74 else
75 {
76 nSpaceOffset += _ttoi(szFmtVal);
77 }
78
79 szFmtVal[nValCount] = _T('\0');
80 nValCount=0;
81
82 /* Insert space to finded position plus all pos before */
83 pszTempStr = InsSpacePos(pszDestStr, nSpaceOffset);
84 _tcscpy(pszDestStr,pszTempStr);
85 free(pszTempStr);
86
87 /* Num of spaces total increment */
88 if (!wasNul)
89 {
90 nSpaceOffset++;
91 nLastVal = _ttoi(szFmtVal);
92 }
93 }
94 else
95 {
96 szFmtVal[nValCount++] = szFmtStr[nFmtCount];
97 }
98 }
99
100 /* Create spaces for rest part of string */
101 if (wasNul && nLastVal != 0)
102 {
103 for (nFmtCount = nSpaceOffset + nLastVal; nFmtCount < _tcslen(pszDestStr); nFmtCount += nLastVal + 1)
104 {
105 pszTempStr = InsSpacePos(pszDestStr, nFmtCount);
106 _tcscpy(pszDestStr, pszTempStr);
107 free(pszTempStr);
108 }
109 }
110
111 return pszDestStr;
112 }
113
114 /* Replace given template in source string with string to replace and return received string */
115 LPTSTR
116 ReplaceSubStr(LPCTSTR szSourceStr,
117 LPCTSTR szStrToReplace,
118 LPCTSTR szTempl)
119 {
120 LPTSTR szDestStr;
121 UINT nCharCnt;
122 UINT nSubStrCnt;
123 UINT nDestStrCnt;
124 UINT nFirstCharCnt;
125
126 szDestStr = (LPTSTR)malloc(MAX_SAMPLES_STR_SIZE * sizeof(TCHAR));
127
128 nDestStrCnt = 0;
129 nFirstCharCnt = 0;
130
131 _tcscpy(szDestStr, _T(""));
132
133 while (nFirstCharCnt < _tcslen(szSourceStr))
134 {
135 if (szSourceStr[nFirstCharCnt] == szTempl[0])
136 {
137 nSubStrCnt = 0;
138 for (nCharCnt = nFirstCharCnt; nCharCnt < nFirstCharCnt + _tcslen(szTempl); nCharCnt++)
139 {
140 if (szSourceStr[nCharCnt] == szTempl[nSubStrCnt])
141 {
142 nSubStrCnt++;
143 }
144 else
145 {
146 break;
147 }
148
149 if (_tcslen(szTempl) == nSubStrCnt)
150 {
151 _tcscat(szDestStr, szStrToReplace);
152 nDestStrCnt = _tcslen(szDestStr);
153 nFirstCharCnt += _tcslen(szTempl) - 1;
154 break;
155 }
156 }
157 }
158 else
159 {
160 szDestStr[nDestStrCnt++] = szSourceStr[nFirstCharCnt];
161 szDestStr[nDestStrCnt] = _T('\0');
162 }
163
164 nFirstCharCnt++;
165 }
166
167 return szDestStr;
168 }
169
170
171 static VOID
172 InitPropSheetPage(PROPSHEETPAGE *psp, WORD idDlg, DLGPROC DlgProc, PGLOBALDATA pGlobalData)
173 {
174 ZeroMemory(psp, sizeof(PROPSHEETPAGE));
175 psp->dwSize = sizeof(PROPSHEETPAGE);
176 psp->dwFlags = PSP_DEFAULT;
177 psp->hInstance = hApplet;
178 psp->pszTemplate = MAKEINTRESOURCE(idDlg);
179 psp->pfnDlgProc = DlgProc;
180 psp->lParam = (LPARAM)pGlobalData;
181 }
182
183
184 /* Create applets */
185 LONG
186 APIENTRY
187 SetupApplet(HWND hwndDlg, LCID lcid)
188 {
189 PROPSHEETPAGE PsPage[NUM_SHEETS + 1];
190 PROPSHEETHEADER psh;
191 PGLOBALDATA pGlobalData;
192 TCHAR Caption[MAX_STR_SIZE];
193 INT ret;
194
195 LoadString(hApplet, IDS_CUSTOMIZE_TITLE, Caption, sizeof(Caption) / sizeof(TCHAR));
196
197 pGlobalData = (PGLOBALDATA)malloc(sizeof(GLOBALDATA));
198
199 pGlobalData->lcid = lcid;
200
201 ZeroMemory(&psh, sizeof(PROPSHEETHEADER));
202 psh.dwSize = sizeof(PROPSHEETHEADER);
203 psh.dwFlags = PSH_PROPSHEETPAGE | PSH_USECALLBACK | PSH_PROPTITLE;
204 psh.hwndParent = hwndDlg;
205 psh.hInstance = hApplet;
206 psh.hIcon = LoadIcon(hApplet, MAKEINTRESOURCE(IDC_CPLICON));
207 psh.pszCaption = Caption;
208 psh.nPages = (sizeof(PsPage) / sizeof(PROPSHEETPAGE)) - 1;
209 psh.nStartPage = 0;
210 psh.ppsp = PsPage;
211
212 InitPropSheetPage(&PsPage[0], IDD_NUMBERSPAGE, NumbersPageProc, pGlobalData);
213 InitPropSheetPage(&PsPage[1], IDD_CURRENCYPAGE, CurrencyPageProc, pGlobalData);
214 InitPropSheetPage(&PsPage[2], IDD_TIMEPAGE, TimePageProc, pGlobalData);
215 InitPropSheetPage(&PsPage[3], IDD_DATEPAGE, DatePageProc, pGlobalData);
216
217 if (IsSortPageNeeded(lcid))
218 {
219 psh.nPages++;
220 InitPropSheetPage(&PsPage[4], IDD_SORTPAGE, SortPageProc, pGlobalData);
221 }
222
223 ret = PropertySheet(&psh);
224
225 free(pGlobalData);
226
227 return (LONG)(ret != -1);
228 }
229
230 /* EOF */