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