[INTL]
[reactos.git] / reactos / dll / cpl / intl / intl.c
1 /*
2 * ReactOS
3 * Copyright (C) 2004 ReactOS Team
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19 /*
20 * PROJECT: ReactOS International Control Panel
21 * FILE: dll/cpl/intl/intl.c
22 * PURPOSE: Property sheet code
23 * PROGRAMMER: Eric Kohl
24 */
25
26 #include "intl.h"
27
28 #include <debug.h>
29
30 #define NUM_APPLETS (1)
31
32 #define BUFFERSIZE 512
33
34 static LONG APIENTRY
35 Applet(HWND hwnd, UINT uMsg, LPARAM wParam, LPARAM lParam);
36
37
38 HINSTANCE hApplet = 0;
39 HWND hCPLWindow;
40 HINF hSetupInf = INVALID_HANDLE_VALUE;
41 DWORD IsUnattendedSetupEnabled = 0;
42 DWORD UnattendLCID = 0;
43
44
45 /* Applets */
46 APPLET Applets[NUM_APPLETS] =
47 {
48 {IDC_CPLICON, IDS_CPLNAME, IDS_CPLDESCRIPTION, Applet}
49 };
50
51 VOID
52 PrintErrorMsgBox(UINT msg)
53 {
54 TCHAR szErrorText[BUFFERSIZE];
55 TCHAR szErrorCaption[BUFFERSIZE];
56
57 LoadString(hApplet, msg, szErrorText, sizeof(szErrorText)/sizeof(TCHAR));
58 LoadString(hApplet, IDS_ERROR, szErrorCaption, sizeof(szErrorCaption)/sizeof(TCHAR));
59
60 MessageBox(NULL, szErrorText, szErrorCaption, MB_OK | MB_ICONERROR);
61 }
62
63 VOID
64 ResourceMessageBox(
65 HWND hwnd,
66 UINT uType,
67 UINT uCaptionId,
68 UINT uMessageId)
69 {
70 WCHAR szErrorText[BUFFERSIZE];
71 WCHAR szErrorCaption[BUFFERSIZE];
72
73 LoadStringW(hApplet, uMessageId, szErrorText, sizeof(szErrorText) / sizeof(WCHAR));
74 LoadStringW(hApplet, uCaptionId, szErrorCaption, sizeof(szErrorCaption) / sizeof(WCHAR));
75
76 MessageBoxW(hwnd, szErrorText, szErrorCaption, uType);
77 }
78
79 static VOID
80 InitPropSheetPage(PROPSHEETPAGE *psp, WORD idDlg, DLGPROC DlgProc, LPARAM lParam)
81 {
82 ZeroMemory(psp, sizeof(PROPSHEETPAGE));
83 psp->dwSize = sizeof(PROPSHEETPAGE);
84 psp->dwFlags = PSP_DEFAULT;
85 psp->hInstance = hApplet;
86 psp->pszTemplate = MAKEINTRESOURCE(idDlg);
87 psp->pfnDlgProc = DlgProc;
88 psp->lParam = lParam;
89 }
90
91 BOOL
92 OpenSetupInf(VOID)
93 {
94 PWSTR lpCmdLine;
95 PWSTR lpSwitch;
96 size_t len;
97
98 lpCmdLine = GetCommandLineW();
99
100 lpSwitch = wcsstr(lpCmdLine, L"/f:\"");
101 if (!lpSwitch)
102 return FALSE;
103
104 len = wcslen(lpSwitch);
105 if (len < 5 || lpSwitch[len-1] != L'\"')
106 {
107 DPRINT1("Invalid switch: %ls\n", lpSwitch);
108 return FALSE;
109 }
110
111 lpSwitch[len-1] = L'\0';
112
113 hSetupInf = SetupOpenInfFileW(&lpSwitch[4], NULL, INF_STYLE_OLDNT, NULL);
114 if (hSetupInf == INVALID_HANDLE_VALUE)
115 {
116 DPRINT1("Failed to open INF file: %ls\n", &lpSwitch[4]);
117 return FALSE;
118 }
119
120 return TRUE;
121 }
122
123 VOID
124 ParseSetupInf(VOID)
125 {
126 INFCONTEXT InfContext;
127 WCHAR szBuffer[30];
128
129 if (!SetupFindFirstLineW(hSetupInf,
130 L"Unattend",
131 L"LocaleID",
132 &InfContext))
133 {
134 SetupCloseInfFile(hSetupInf);
135 DPRINT1("SetupFindFirstLine failed\n");
136 return;
137 }
138
139 if (!SetupGetStringFieldW(&InfContext, 1, szBuffer,
140 sizeof(szBuffer) / sizeof(WCHAR), NULL))
141 {
142 SetupCloseInfFile(hSetupInf);
143 DPRINT1("SetupGetStringField failed\n");
144 return;
145 }
146
147 UnattendLCID = wcstoul(szBuffer, NULL, 16);
148 IsUnattendedSetupEnabled = 1;
149 SetupCloseInfFile(hSetupInf);
150 }
151
152 static LONG APIENTRY
153 Applet(HWND hwnd, UINT uMsg, LPARAM wParam, LPARAM lParam)
154 {
155 TCHAR Caption[BUFFERSIZE];
156 PROPSHEETPAGE psp[3];
157 PROPSHEETHEADER psh;
158 PGLOBALDATA pGlobalData;
159 LONG ret;
160
161 if (OpenSetupInf())
162 {
163 ParseSetupInf();
164 }
165
166 pGlobalData = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(GLOBALDATA));
167 if (pGlobalData == NULL)
168 return FALSE;
169
170 pGlobalData->SystemLCID = GetSystemDefaultLCID();
171
172 LoadString(hApplet, IDS_CPLNAME, Caption, sizeof(Caption) / sizeof(TCHAR));
173
174 ZeroMemory(&psh, sizeof(PROPSHEETHEADER));
175 psh.dwSize = sizeof(PROPSHEETHEADER);
176 psh.dwFlags = PSH_PROPSHEETPAGE;
177 psh.hwndParent = hCPLWindow;
178 psh.hInstance = hApplet;
179 psh.hIcon = LoadIcon(hApplet, MAKEINTRESOURCE(IDC_CPLICON));
180 psh.pszCaption = Caption;
181 psh.nPages = sizeof(psp) / sizeof(PROPSHEETPAGE);
182 psh.nStartPage = 0;
183 psh.ppsp = psp;
184
185 InitPropSheetPage(&psp[0], IDD_GENERALPAGE, GeneralPageProc, (LPARAM)pGlobalData);
186 InitPropSheetPage(&psp[1], IDD_LANGUAGESPAGE, LanguagesPageProc, (LPARAM)pGlobalData);
187 InitPropSheetPage(&psp[2], IDD_ADVANCEDPAGE, AdvancedPageProc, (LPARAM)pGlobalData);
188
189 ret = (LONG)(PropertySheet(&psh) != -1);
190
191 HeapFree(GetProcessHeap(), 0, pGlobalData);
192
193 return ret;
194 }
195
196
197 /* Control Panel Callback */
198 LONG APIENTRY
199 CPlApplet(HWND hwndCpl,
200 UINT uMsg,
201 LPARAM lParam1,
202 LPARAM lParam2)
203 {
204 switch(uMsg)
205 {
206 case CPL_INIT:
207 return TRUE;
208
209 case CPL_GETCOUNT:
210 return NUM_APPLETS;
211
212 case CPL_INQUIRE:
213 {
214 CPLINFO *CPlInfo = (CPLINFO*)lParam2;
215 UINT uAppIndex = (UINT)lParam1;
216
217 CPlInfo->lData = 0;
218 CPlInfo->idIcon = Applets[uAppIndex].idIcon;
219 CPlInfo->idName = Applets[uAppIndex].idName;
220 CPlInfo->idInfo = Applets[uAppIndex].idDescription;
221 break;
222 }
223
224 case CPL_DBLCLK:
225 {
226 UINT uAppIndex = (UINT)lParam1;
227 hCPLWindow = hwndCpl;
228 Applets[uAppIndex].AppletProc(hwndCpl, uMsg, lParam1, lParam2);
229 break;
230 }
231 }
232
233 return FALSE;
234 }
235
236
237 BOOL WINAPI
238 DllMain(HINSTANCE hinstDLL,
239 DWORD dwReason,
240 LPVOID lpReserved)
241 {
242 switch(dwReason)
243 {
244 case DLL_PROCESS_ATTACH:
245 case DLL_THREAD_ATTACH:
246 hApplet = hinstDLL;
247 break;
248 }
249
250 return TRUE;
251 }