[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 LPTSTR lpCmdLine;
95 LPTSTR lpSwitch;
96 size_t len;
97
98 lpCmdLine = GetCommandLine();
99
100 lpSwitch = _tcsstr(lpCmdLine, _T("/f:\""));
101 if (!lpSwitch)
102 return FALSE;
103
104 len = _tcslen(lpSwitch);
105 if (len < 5 || lpSwitch[len-1] != _T('\"'))
106 {
107 DPRINT1("Invalid switch: %ls\n", lpSwitch);
108 return FALSE;
109 }
110
111 lpSwitch[len-1] = _T('\0');
112
113 hSetupInf = SetupOpenInfFile(&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 TCHAR szBuffer[30];
128
129 if (!SetupFindFirstLine(hSetupInf,
130 _T("Unattend"),
131 _T("LocaleID"),
132 &InfContext))
133 {
134 SetupCloseInfFile(hSetupInf);
135 DPRINT1("SetupFindFirstLine failed\n");
136 return;
137 }
138
139 if (!SetupGetStringField(&InfContext, 1, szBuffer,
140 sizeof(szBuffer) / sizeof(TCHAR), NULL))
141 {
142 SetupCloseInfFile(hSetupInf);
143 DPRINT1("SetupGetStringField failed\n");
144 return;
145 }
146
147 UnattendLCID = _tcstoul(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
168 LoadString(hApplet, IDS_CPLNAME, Caption, sizeof(Caption) / sizeof(TCHAR));
169
170 ZeroMemory(&psh, sizeof(PROPSHEETHEADER));
171 psh.dwSize = sizeof(PROPSHEETHEADER);
172 psh.dwFlags = PSH_PROPSHEETPAGE | PSH_PROPTITLE;
173 psh.hwndParent = hCPLWindow;
174 psh.hInstance = hApplet;
175 psh.hIcon = LoadIcon(hApplet, MAKEINTRESOURCE(IDC_CPLICON));
176 psh.pszCaption = Caption;
177 psh.nPages = sizeof(psp) / sizeof(PROPSHEETPAGE);
178 psh.nStartPage = 0;
179 psh.ppsp = psp;
180
181 InitPropSheetPage(&psp[0], IDD_GENERALPAGE, GeneralPageProc, (LPARAM)pGlobalData);
182 InitPropSheetPage(&psp[1], IDD_LANGUAGESPAGE, LanguagesPageProc, (LPARAM)pGlobalData);
183 InitPropSheetPage(&psp[2], IDD_ADVANCEDPAGE, AdvancedPageProc, (LPARAM)pGlobalData);
184
185 ret = (LONG)(PropertySheet(&psh) != -1);
186
187 HeapFree(GetProcessHeap(), 0, pGlobalData);
188
189 return ret;
190 }
191
192
193 /* Control Panel Callback */
194 LONG APIENTRY
195 CPlApplet(HWND hwndCpl,
196 UINT uMsg,
197 LPARAM lParam1,
198 LPARAM lParam2)
199 {
200 switch(uMsg)
201 {
202 case CPL_INIT:
203 return TRUE;
204
205 case CPL_GETCOUNT:
206 return NUM_APPLETS;
207
208 case CPL_INQUIRE:
209 {
210 CPLINFO *CPlInfo = (CPLINFO*)lParam2;
211 UINT uAppIndex = (UINT)lParam1;
212
213 CPlInfo->lData = 0;
214 CPlInfo->idIcon = Applets[uAppIndex].idIcon;
215 CPlInfo->idName = Applets[uAppIndex].idName;
216 CPlInfo->idInfo = Applets[uAppIndex].idDescription;
217 break;
218 }
219
220 case CPL_DBLCLK:
221 {
222 UINT uAppIndex = (UINT)lParam1;
223 hCPLWindow = hwndCpl;
224 Applets[uAppIndex].AppletProc(hwndCpl, uMsg, lParam1, lParam2);
225 break;
226 }
227 }
228
229 return FALSE;
230 }
231
232
233 BOOL WINAPI
234 DllMain(HINSTANCE hinstDLL,
235 DWORD dwReason,
236 LPVOID lpReserved)
237 {
238 switch(dwReason)
239 {
240 case DLL_PROCESS_ATTACH:
241 case DLL_THREAD_ATTACH:
242 hApplet = hinstDLL;
243 break;
244 }
245
246 return TRUE;
247 }