[CPL] Do not enable the "Apply" button unconditionally (#1876)
[reactos.git] / dll / cpl / openglcfg / general.c
1 #include "openglcfg.h"
2
3 #include <winreg.h>
4 #include <debug.h>
5
6 static PWCHAR *pOglDrivers = NULL;
7 static DWORD dwNumDrivers = 0;
8
9 static VOID InitSettings(HWND hWndDlg)
10 {
11 HKEY hKeyRenderer;
12 HKEY hKeyDrivers;
13 WCHAR szBuffer[MAX_KEY_LENGTH];
14 WCHAR szBultin[MAX_KEY_LENGTH];
15 WCHAR szDriver[MAX_KEY_LENGTH];
16 DWORD dwType = 0;
17 DWORD dwSize = MAX_KEY_LENGTH;
18
19 if (RegOpenKeyExW(HKEY_LOCAL_MACHINE, KEY_DRIVERS, 0, KEY_READ, &hKeyDrivers) != ERROR_SUCCESS)
20 return;
21
22 if (RegCreateKeyExW(HKEY_CURRENT_USER, KEY_RENDERER, 0, NULL, 0, MAXIMUM_ALLOWED, NULL, &hKeyRenderer, NULL) != ERROR_SUCCESS)
23 {
24 RegCloseKey(hKeyDrivers);
25 return;
26 }
27
28 LoadString(hApplet, IDS_DEBUG_DNM, (LPTSTR)szBultin, 127);
29 SendDlgItemMessageW(hWndDlg, IDC_DEBUG_OUTPUT, CB_ADDSTRING, 0, (LPARAM)szBultin);
30
31 LoadString(hApplet, IDS_DEBUG_SET, (LPTSTR)szBultin, 127);
32 SendDlgItemMessageW(hWndDlg, IDC_DEBUG_OUTPUT, CB_ADDSTRING, 0, (LPARAM)szBultin);
33
34 LoadString(hApplet, IDS_DEBUG_CLEAR, (LPTSTR)szBultin, 127);
35 SendDlgItemMessageW(hWndDlg, IDC_DEBUG_OUTPUT, CB_ADDSTRING, 0, (LPARAM)szBultin);
36
37 SendDlgItemMessageW(hWndDlg, IDC_DEBUG_OUTPUT, CB_SETCURSEL, 0, 0);
38
39 LoadString(hApplet, IDS_RENDERER_DEFAULT, (LPTSTR)szBultin, 127);
40 SendDlgItemMessageW(hWndDlg, IDC_RENDERER, CB_ADDSTRING, 0, (LPARAM)szBultin);
41
42 LoadString(hApplet, IDS_RENDERER_RSWR, (LPTSTR)szBultin, 127);
43 SendDlgItemMessageW(hWndDlg, IDC_RENDERER, CB_ADDSTRING, 0, (LPARAM)szBultin);
44
45 if (RegQueryValueExW(hKeyRenderer, NULL, NULL, &dwType, (LPBYTE)szDriver, &dwSize) != ERROR_SUCCESS || dwSize == sizeof(WCHAR))
46 SendDlgItemMessageW(hWndDlg, IDC_RENDERER, CB_SETCURSEL, RENDERER_DEFAULT, 0);
47
48 if (dwType == REG_SZ)
49 {
50 DWORD ret;
51 INT iKey;
52
53 if (wcsncmp(szBultin, szDriver, MAX_KEY_LENGTH) == 0)
54 SendDlgItemMessageW(hWndDlg, IDC_RENDERER, CB_SETCURSEL, RENDERER_RSWR, 0);
55
56 ret = RegQueryInfoKeyW(hKeyDrivers, NULL, NULL, NULL, &dwNumDrivers, NULL, NULL, NULL, NULL, NULL, NULL, NULL);
57
58 if (ret != ERROR_SUCCESS || dwNumDrivers <= 0)
59 {
60 RegCloseKey(hKeyDrivers);
61 RegCloseKey(hKeyRenderer);
62 return;
63 }
64
65 pOglDrivers = HeapAlloc(GetProcessHeap(), 0, dwNumDrivers * sizeof(PWCHAR));
66
67 if (!pOglDrivers)
68 dwNumDrivers = 0;
69
70 for (iKey = 0; iKey < dwNumDrivers; iKey++)
71 {
72 dwSize = MAX_KEY_LENGTH;
73 ret = RegEnumKeyEx(hKeyDrivers, iKey, szBuffer, &dwSize, NULL, NULL, NULL, NULL);
74
75 if (ret != ERROR_SUCCESS)
76 break;
77
78 /* Mind the null terminator */
79 dwSize++;
80
81 pOglDrivers[iKey] = HeapAlloc(GetProcessHeap(), 0, dwSize * sizeof(WCHAR));
82
83 if (!pOglDrivers[iKey])
84 break;
85
86 SendDlgItemMessageW(hWndDlg, IDC_RENDERER, CB_ADDSTRING, 0, (LPARAM)szBuffer);
87
88 StringCchCopy(pOglDrivers[iKey], dwSize, szBuffer);
89
90 if (wcsncmp(szBuffer, szDriver, MAX_KEY_LENGTH) == 0)
91 SendDlgItemMessageW(hWndDlg, IDC_RENDERER, CB_SETCURSEL, iKey + 2, 0);
92 }
93 }
94
95 RegCloseKey(hKeyDrivers);
96 RegCloseKey(hKeyRenderer);
97
98 return;
99 }
100
101 static VOID SaveSettings(HWND hWndDlg)
102 {
103 HKEY hKeyRenderer;
104 HKEY hKeyDebug;
105 INT iSel = 0;
106
107 if (RegOpenKeyExW(HKEY_CURRENT_USER, KEY_RENDERER, 0, KEY_WRITE, &hKeyRenderer) != ERROR_SUCCESS)
108 return;
109
110 if (RegOpenKeyExW(HKEY_LOCAL_MACHINE, KEY_DEBUG_CHANNEL, 0, KEY_WRITE, &hKeyDebug) == ERROR_SUCCESS)
111 {
112 iSel = (INT)SendDlgItemMessageW(hWndDlg, IDC_DEBUG_OUTPUT, CB_GETCURSEL, 0, 0);
113
114 switch (iSel)
115 {
116 case DEBUG_SET:
117 RegSetValueExW(hKeyDebug, L"DEBUGCHANNEL", 0, REG_SZ, (PBYTE)L"+opengl,+wgl", 13 * sizeof(WCHAR));
118 break;
119
120 case DEBUG_CLEAR:
121 RegSetValueExW(hKeyDebug, L"DEBUGCHANNEL", 0, REG_SZ, (PBYTE)L"", sizeof(WCHAR));
122 break;
123 }
124 RegCloseKey(hKeyDebug);
125 }
126
127 iSel = (INT)SendDlgItemMessageW(hWndDlg, IDC_RENDERER, CB_GETCURSEL, 0, 0);
128
129 switch (iSel)
130 {
131 case CB_ERR:
132 break;
133
134 case RENDERER_DEFAULT:
135 RegSetValueExW(hKeyRenderer, L"", 0, REG_SZ, (PBYTE)L"", sizeof(WCHAR));
136 break;
137
138 case RENDERER_RSWR:
139 {
140 WCHAR szBuffer[MAX_KEY_LENGTH];
141 LoadString(hApplet, IDS_RENDERER_RSWR, (LPTSTR)szBuffer, 127);
142 RegSetValueExW(hKeyRenderer, L"", 0, REG_SZ, (PBYTE)szBuffer, (wcslen(szBuffer) + 1) * sizeof(WCHAR));
143 break;
144 }
145
146 default:
147 {
148 /* Adjustment for DEFAULT and RSWR renderers */
149 iSel -= 2;
150
151 if (iSel >= 0 && iSel <= dwNumDrivers)
152 RegSetValueExW(hKeyRenderer, L"", 0, REG_SZ, (PBYTE)pOglDrivers[iSel], (wcslen(pOglDrivers[iSel]) + 1) * sizeof(WCHAR));
153
154 break;
155 }
156 }
157
158 RegCloseKey(hKeyRenderer);
159 }
160
161
162 INT_PTR CALLBACK GeneralPageProc(HWND hWndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
163 {
164 LPPSHNOTIFY lppsn;
165
166 switch (uMsg)
167 {
168 case WM_INITDIALOG:
169 InitSettings(hWndDlg);
170 return TRUE;
171
172 case WM_COMMAND:
173 if (LOWORD(wParam) == IDC_RENDERER ||
174 LOWORD(wParam) == IDC_DEBUG_OUTPUT)
175 {
176 if (HIWORD(wParam) == CBN_SELCHANGE)
177 {
178 PropSheet_Changed(GetParent(hWndDlg), hWndDlg);
179 }
180 }
181 break;
182
183 case WM_NOTIFY:
184 lppsn = (LPPSHNOTIFY)lParam;
185 if (lppsn->hdr.code == PSN_APPLY)
186 {
187 SaveSettings(hWndDlg);
188 return TRUE;
189 }
190 break;
191
192 case WM_DESTROY:
193 {
194 INT iKey;
195 for (iKey = 0; iKey <= dwNumDrivers; iKey++)
196 HeapFree(GetProcessHeap(), 0, pOglDrivers[iKey]);
197
198 HeapFree(GetProcessHeap(), 0, pOglDrivers);
199 }
200 }
201
202 return FALSE;
203 }