91a88cf57c5fccf00424f6fcb0dba26fa28ac78d
[reactos.git] / reactos / dll / cpl / powercfg / powercfg.c
1 /* $Id$
2 *
3 * PROJECT: ReactOS Power Configuration Applet
4 * LICENSE: GPL - See COPYING in the top level directory
5 * FILE: dll/cpl/powercfg/powershemes.c
6 * PURPOSE: initialization of applet
7 * PROGRAMMERS: Alexander Wurzinger (Lohnegrim at gmx dot net)
8 * Johannes Anderwald (johannes.anderwald@student.tugraz.at)
9 * Martin Rottensteiner
10 * Dmitry Chapyshev (lentind@yandex.ru)
11 */
12
13 #include <windows.h>
14 #include <commctrl.h>
15 #include <shlobj.h>
16 #include <regstr.h>
17 #include <cpl.h>
18
19 #include "resource.h"
20 #include "powercfg.h"
21
22 #define NUM_APPLETS (1)
23
24 static LONG APIENTRY Applet1(HWND hwnd, UINT uMsg, LPARAM wParam, LPARAM lParam);
25 INT_PTR CALLBACK powershemesProc(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam);
26 INT_PTR CALLBACK alarmsProc(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam);
27 INT_PTR CALLBACK advancedProc(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam);
28 INT_PTR CALLBACK hibernateProc(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam);
29
30 HINSTANCE hApplet = 0;
31 GLOBAL_POWER_POLICY gGPP;
32 TCHAR langSel[255];
33
34 /* Applets */
35 APPLET Applets[NUM_APPLETS] =
36 {
37 {IDC_CPLICON_1, IDS_CPLNAME_1, IDS_CPLDESCRIPTION_1, Applet1}
38 };
39
40 static BOOL CALLBACK
41 PropSheetAddPage(HPROPSHEETPAGE hpage, LPARAM lParam)
42 {
43 PROPSHEETHEADER *ppsh = (PROPSHEETHEADER *)lParam;
44 if (ppsh != NULL && ppsh->nPages < MAX_POWER_PAGES)
45 {
46 ppsh->phpage[ppsh->nPages++] = hpage;
47 return TRUE;
48 }
49
50 return FALSE;
51 }
52
53 static BOOL
54 InitPropSheetPage(PROPSHEETHEADER *ppsh, WORD idDlg, DLGPROC DlgProc)
55 {
56 HPROPSHEETPAGE hPage;
57 PROPSHEETPAGE psp;
58
59 if (ppsh->nPages < MAX_POWER_PAGES)
60 {
61 ZeroMemory(&psp, sizeof(psp));
62 psp.dwSize = sizeof(psp);
63 psp.dwFlags = PSP_DEFAULT;
64 psp.hInstance = hApplet;
65 psp.pszTemplate = MAKEINTRESOURCE(idDlg);
66 psp.pfnDlgProc = DlgProc;
67
68 hPage = CreatePropertySheetPage(&psp);
69 if (hPage != NULL)
70 {
71 return PropSheetAddPage(hPage, (LPARAM)ppsh);
72 }
73 }
74
75 return FALSE;
76 }
77
78
79 /* Property Sheet Callback */
80 int CALLBACK
81 PropSheetProc(
82 HWND hwndDlg,
83 UINT uMsg,
84 LPARAM lParam
85 )
86 {
87 UNREFERENCED_PARAMETER(hwndDlg);
88 switch(uMsg)
89 {
90 case PSCB_BUTTONPRESSED:
91 switch(lParam)
92 {
93 case PSBTN_OK: /* OK */
94 break;
95 case PSBTN_CANCEL: /* Cancel */
96 break;
97 case PSBTN_APPLYNOW: /* Apply now */
98 break;
99 case PSBTN_FINISH: /* Close */
100 break;
101 default:
102 return FALSE;
103 }
104 break;
105
106 case PSCB_INITIALIZED:
107 break;
108 }
109 return TRUE;
110 }
111
112 /* First Applet */
113 static LONG APIENTRY
114 Applet1(HWND hwnd, UINT uMsg, LPARAM wParam, LPARAM lParam)
115 {
116 HPROPSHEETPAGE hpsp[MAX_POWER_PAGES];
117 PROPSHEETHEADER psh;
118 HPSXA hpsxa = NULL;
119 TCHAR Caption[1024];
120 SYSTEM_POWER_CAPABILITIES spc;
121 LONG ret;
122
123 UNREFERENCED_PARAMETER(hwnd);
124 UNREFERENCED_PARAMETER(uMsg);
125 UNREFERENCED_PARAMETER(wParam);
126 UNREFERENCED_PARAMETER(lParam);
127
128 memset(Caption, 0x0, sizeof(Caption));
129 LoadString(hApplet, IDS_CPLNAME_1, Caption, sizeof(Caption) / sizeof(TCHAR));
130
131 ZeroMemory(&psh, sizeof(PROPSHEETHEADER));
132 psh.dwSize = sizeof(PROPSHEETHEADER);
133 psh.dwFlags = PSH_USECALLBACK | PSH_PROPTITLE;
134 psh.hwndParent = NULL;
135 psh.hInstance = hApplet;
136 psh.hIcon = LoadIcon(hApplet, MAKEINTRESOURCE(IDC_CPLICON_1));
137 psh.pszCaption = Caption;
138 psh.nPages = 0;
139 psh.nStartPage = 0;
140 psh.phpage = hpsp;
141 psh.pfnCallback = PropSheetProc;
142
143 InitPropSheetPage(&psh, IDD_PROPPAGEPOWERSHEMES, (DLGPROC) powershemesProc);
144 if (GetPwrCapabilities(&spc))
145 {
146 if (spc.SystemBatteriesPresent)
147 {
148 InitPropSheetPage(&psh, IDD_PROPPAGEALARMS, (DLGPROC) alarmsProc);
149 }
150 }
151 InitPropSheetPage(&psh, IDD_PROPPAGEADVANCED, (DLGPROC) advancedProc);
152 InitPropSheetPage(&psh, IDD_PROPPAGEHIBERNATE, (DLGPROC) hibernateProc);
153
154 /* Load additional pages provided by shell extensions */
155 hpsxa = SHCreatePropSheetExtArray(HKEY_LOCAL_MACHINE, REGSTR_PATH_CONTROLSFOLDER TEXT("\\Power"), MAX_POWER_PAGES - psh.nPages);
156 if (hpsxa != NULL)
157 SHAddFromPropSheetExtArray(hpsxa, PropSheetAddPage, (LPARAM)&psh);
158
159 ret = (LONG)(PropertySheet(&psh) != -1);
160
161 if (hpsxa != NULL)
162 SHDestroyPropSheetExtArray(hpsxa);
163
164 return ret;
165 }
166
167 /* Control Panel Callback */
168 LONG CALLBACK
169 CPlApplet(
170 HWND hwndCPl,
171 UINT uMsg,
172 LPARAM lParam1,
173 LPARAM lParam2)
174 {
175 int i = (int)lParam1;
176
177 switch(uMsg)
178 {
179 case CPL_INIT:
180 {
181 return TRUE;
182 }
183 case CPL_GETCOUNT:
184 {
185 return NUM_APPLETS;
186 }
187 case CPL_INQUIRE:
188 {
189 CPLINFO *CPlInfo = (CPLINFO*)lParam2;
190 CPlInfo->lData = 0;
191 CPlInfo->idIcon = Applets[i].idIcon;
192 CPlInfo->idName = Applets[i].idName;
193 CPlInfo->idInfo = Applets[i].idDescription;
194 break;
195 }
196 case CPL_DBLCLK:
197 {
198 Applets[i].AppletProc(hwndCPl, uMsg, lParam1, lParam2);
199 break;
200 }
201 }
202 return FALSE;
203 }
204
205
206 BOOLEAN
207 WINAPI
208 DllMain(
209 HINSTANCE hinstDLL,
210 DWORD dwReason,
211 LPVOID lpvReserved)
212 {
213 UNREFERENCED_PARAMETER(lpvReserved);
214 switch(dwReason)
215 {
216 case DLL_PROCESS_ATTACH:
217 case DLL_THREAD_ATTACH:
218 hApplet = hinstDLL;
219 break;
220 }
221 return TRUE;
222 }
223
224