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