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