winecfg: Fix issues with LVM_GETITEMW by removing superfluous flag.
[wine.git] / programs / winecfg / appdefaults.c
blobad6b7ccdf195685f84637f019a2019569dddd70d
1 /*
2 * WineCfg app settings tabsheet
4 * Copyright 2004 Robert van Herk
5 * Copyright 2004 Chris Morgan
6 * Copyright 2004 Mike Hearn
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
24 #define WIN32_LEAN_AND_MEAN
25 #define NONAMELESSUNION
26 #include <windows.h>
27 #include <commdlg.h>
28 #include <wine/debug.h>
29 #include <stdio.h>
30 #include <assert.h>
31 #include "winecfg.h"
32 #include "resource.h"
34 WINE_DEFAULT_DEBUG_CHANNEL(winecfg);
36 static const struct
38 const char *szVersion;
39 const char *szDescription;
40 DWORD dwMajorVersion;
41 DWORD dwMinorVersion;
42 DWORD dwBuildNumber;
43 DWORD dwPlatformId;
44 const char *szCSDVersion;
45 WORD wServicePackMajor;
46 WORD wServicePackMinor;
47 const char *szProductType;
48 } win_versions[] =
50 { "win2003", "Windows 2003", 5, 2, 0xECE, VER_PLATFORM_WIN32_NT, "Service Pack 1", 1, 0, "ServerNT"},
51 { "winxp", "Windows XP", 5, 1, 0xA28, VER_PLATFORM_WIN32_NT, "Service Pack 2", 2, 0, "WinNT"},
52 { "win2k", "Windows 2000", 5, 0, 0x893, VER_PLATFORM_WIN32_NT, "Service Pack 4", 4, 0, "WinNT"},
53 { "winme", "Windows ME", 4, 90, 0xBB8, VER_PLATFORM_WIN32_WINDOWS, " ", 0, 0, ""},
54 { "win98", "Windows 98", 4, 10, 0x8AE, VER_PLATFORM_WIN32_WINDOWS, " A ", 0, 0, ""},
55 { "win95", "Windows 95", 4, 0, 0x3B6, VER_PLATFORM_WIN32_WINDOWS, "", 0, 0, ""},
56 { "nt40", "Windows NT 4.0", 4, 0, 0x565, VER_PLATFORM_WIN32_NT, "Service Pack 6a", 6, 0, "WinNT"},
57 { "nt351", "Windows NT 3.5", 3, 51, 0x421, VER_PLATFORM_WIN32_NT, "Service Pack 2", 0, 0, "WinNT"},
58 { "win31", "Windows 3.1", 2, 10, 0, VER_PLATFORM_WIN32s, "Win32s 1.3", 0, 0, ""},
59 { "win30", "Windows 3.0", 3, 0, 0, VER_PLATFORM_WIN32s, "Win32s 1.3", 0, 0, ""},
60 { "win20", "Windows 2.0", 2, 0, 0, VER_PLATFORM_WIN32s, "Win32s 1.3", 0, 0, ""}
63 #define NB_VERSIONS (sizeof(win_versions)/sizeof(win_versions[0]))
65 static void update_comboboxes(HWND dialog)
67 int i;
69 char *winver;
71 /* retrieve the registry values */
72 winver = get_reg_key(config_key, keypath(""), "Version", "");
74 if (*winver == '\0')
76 HeapFree(GetProcessHeap(), 0, winver);
78 if (current_app) /* no explicit setting */
80 WINE_TRACE("setting winver combobox to default\n");
81 SendDlgItemMessage (dialog, IDC_WINVER, CB_SETCURSEL, 0, 0);
82 return;
84 winver = strdupA("win2k");
86 WINE_TRACE("winver is %s\n", winver);
88 /* normalize the version strings */
89 for (i = 0; i < NB_VERSIONS; i++)
91 if (!strcasecmp (win_versions[i].szVersion, winver))
93 SendDlgItemMessage (dialog, IDC_WINVER, CB_SETCURSEL,
94 (WPARAM) i + (current_app?1:0), 0);
95 WINE_TRACE("match with %s\n", win_versions[i].szVersion);
96 break;
100 HeapFree(GetProcessHeap(), 0, winver);
103 static void
104 init_comboboxes (HWND dialog)
106 int i;
108 SendDlgItemMessage(dialog, IDC_WINVER, CB_RESETCONTENT, 0, 0);
110 /* add the default entries (automatic) which correspond to no setting */
111 if (current_app)
113 WCHAR str[256];
114 LoadStringW (GetModuleHandle (NULL), IDS_USE_GLOBAL_SETTINGS, str,
115 sizeof(str)/sizeof(str[0]));
116 SendDlgItemMessageW (dialog, IDC_WINVER, CB_ADDSTRING, 0, (LPARAM)str);
119 for (i = 0; i < NB_VERSIONS; i++)
121 SendDlgItemMessage (dialog, IDC_WINVER, CB_ADDSTRING,
122 0, (LPARAM) win_versions[i].szDescription);
126 static void add_listview_item(HWND listview, const WCHAR *text, void *association)
128 LVITEMW item;
130 ZeroMemory(&item, sizeof(LVITEM));
132 item.mask = LVIF_TEXT | LVIF_PARAM;
133 item.pszText = (WCHAR*) text;
134 item.cchTextMax = lstrlenW(text);
135 item.lParam = (LPARAM) association;
136 item.iItem = ListView_GetItemCount(listview);
138 SendMessage(listview, LVM_INSERTITEMW, 0, (LPARAM) &item);
141 /* Called when the application is initialized (cannot reinit!) */
142 static void init_appsheet(HWND dialog)
144 HWND listview;
145 HKEY key;
146 int i;
147 DWORD size;
148 WCHAR appname[1024];
150 WINE_TRACE("()\n");
152 listview = GetDlgItem(dialog, IDC_APP_LISTVIEW);
154 /* we use the lparam field of the item so we can alter the presentation later and not change code
155 * for instance, to use the tile view or to display the EXEs embedded 'display name' */
156 LoadStringW (GetModuleHandle (NULL), IDS_DEFAULT_SETTINGS, appname,
157 sizeof(appname)/sizeof(appname[0]));
158 add_listview_item(listview, appname, NULL);
160 /* because this list is only populated once, it's safe to bypass the settings list here */
161 if (RegOpenKey(config_key, "AppDefaults", &key) == ERROR_SUCCESS)
163 i = 0;
164 size = sizeof(appname)/sizeof(appname[0]);
165 while (RegEnumKeyExW (key, i, appname, &size, NULL, NULL, NULL, NULL) == ERROR_SUCCESS)
167 add_listview_item(listview, appname, strdupW(appname));
169 i++;
170 size = sizeof(appname)/sizeof(appname[0]);
173 RegCloseKey(key);
176 init_comboboxes(dialog);
178 /* Select the default settings listview item */
180 LVITEM item;
182 ZeroMemory(&item, sizeof(item));
184 item.mask = LVIF_STATE;
185 item.iItem = 0;
186 item.state = LVIS_SELECTED | LVIS_FOCUSED;
187 item.stateMask = LVIS_SELECTED | LVIS_FOCUSED;
189 SendMessage(listview, LVM_SETITEM, 0, (LPARAM) &item);
194 /* there has to be an easier way than this */
195 static int get_listview_selection(HWND listview)
197 int count = ListView_GetItemCount(listview);
198 int i;
200 for (i = 0; i < count; i++)
202 if (ListView_GetItemState(listview, i, LVIS_SELECTED)) return i;
205 return -1;
209 /* called when the user selects a different application */
210 static void on_selection_change(HWND dialog, HWND listview)
212 LVITEM item;
213 WCHAR* oldapp = current_app;
215 WINE_TRACE("()\n");
217 item.iItem = get_listview_selection(listview);
218 item.mask = LVIF_PARAM;
220 WINE_TRACE("item.iItem=%d\n", item.iItem);
222 if (item.iItem == -1) return;
224 SendMessage(listview, LVM_GETITEM, 0, (LPARAM) &item);
226 current_app = (WCHAR*) item.lParam;
228 if (current_app)
230 WINE_TRACE("current_app is now %s\n", wine_dbgstr_w (current_app));
231 enable(IDC_APP_REMOVEAPP);
233 else
235 WINE_TRACE("current_app=NULL, editing global settings\n");
236 /* focus will never be on the button in this callback so it's safe */
237 disable(IDC_APP_REMOVEAPP);
240 /* reset the combo boxes if we changed from/to global/app-specific */
242 if ((oldapp && !current_app) || (!oldapp && current_app))
243 init_comboboxes(dialog);
245 update_comboboxes(dialog);
247 set_window_title(dialog);
250 static BOOL list_contains_file(HWND listview, WCHAR *filename)
252 LVFINDINFOW find_info = { LVFI_STRING, filename, 0, {0, 0}, 0 };
253 int index;
255 index = ListView_FindItemW(listview, -1, &find_info);
257 return (index != -1);
260 static void on_add_app_click(HWND dialog)
262 WCHAR filetitle[MAX_PATH];
263 WCHAR file[MAX_PATH];
264 WCHAR programsFilter[100];
265 WCHAR selectExecutableStr[100];
266 static const WCHAR pathC[] = { 'c',':','\\',0 };
268 OPENFILENAMEW ofn = { sizeof(OPENFILENAMEW),
269 0, /*hInst*/0, 0, NULL, 0, 0, NULL,
270 0, NULL, 0, pathC, 0,
271 OFN_SHOWHELP | OFN_HIDEREADONLY, 0, 0, NULL, 0, NULL };
273 LoadStringW (GetModuleHandle (NULL), IDS_SELECT_EXECUTABLE, selectExecutableStr,
274 sizeof(selectExecutableStr)/sizeof(selectExecutableStr[0]));
275 LoadStringW (GetModuleHandle (NULL), IDS_EXECUTABLE_FILTER, programsFilter,
276 sizeof(programsFilter)/sizeof(programsFilter[0]));
278 ofn.lpstrTitle = selectExecutableStr;
279 ofn.lpstrFilter = programsFilter;
280 ofn.lpstrFileTitle = filetitle;
281 ofn.lpstrFileTitle[0] = '\0';
282 ofn.nMaxFileTitle = sizeof(filetitle)/sizeof(filetitle[0]);
283 ofn.lpstrFile = file;
284 ofn.lpstrFile[0] = '\0';
285 ofn.nMaxFile = sizeof(file)/sizeof(file[0]);
287 if (GetOpenFileNameW (&ofn))
289 HWND listview = GetDlgItem(dialog, IDC_APP_LISTVIEW);
290 int count = ListView_GetItemCount(listview);
291 WCHAR* new_app;
293 if (list_contains_file(listview, filetitle))
294 return;
296 new_app = strdupW(filetitle);
298 WINE_TRACE("adding %s\n", wine_dbgstr_w (new_app));
300 add_listview_item(listview, new_app, new_app);
302 ListView_SetItemState(listview, count, LVIS_SELECTED | LVIS_FOCUSED, LVIS_SELECTED | LVIS_FOCUSED);
304 SetFocus(listview);
306 else WINE_TRACE("user cancelled\n");
309 static void on_remove_app_click(HWND dialog)
311 HWND listview = GetDlgItem(dialog, IDC_APP_LISTVIEW);
312 int selection = get_listview_selection(listview);
313 char *section = keypath(""); /* AppDefaults\\whatever.exe\\ */
314 LVITEMW item;
316 item.iItem = selection;
317 item.mask = LVIF_PARAM;
319 WINE_TRACE("selection=%d, section=%s\n", selection, section);
321 assert( selection != 0 ); /* user cannot click this button when "default settings" is selected */
323 section[strlen(section)] = '\0'; /* remove last backslash */
324 set_reg_key(config_key, section, NULL, NULL); /* delete the section */
325 SendMessage(listview, LVM_GETITEMW, 0, (LPARAM) &item);
326 HeapFree (GetProcessHeap(), 0, (void*)item.lParam);
327 SendMessage(listview, LVM_DELETEITEM, selection, 0);
328 ListView_SetItemState(listview, selection - 1, LVIS_SELECTED | LVIS_FOCUSED, LVIS_SELECTED | LVIS_FOCUSED);
330 SetFocus(listview);
332 SendMessage(GetParent(dialog), PSM_CHANGED, (WPARAM) dialog, 0);
335 static void on_winver_change(HWND dialog)
337 int selection = SendDlgItemMessage(dialog, IDC_WINVER, CB_GETCURSEL, 0, 0);
339 if (!selection && current_app)
341 WINE_TRACE("default selected so removing current setting\n");
342 set_reg_key(config_key, keypath(""), "Version", NULL);
344 else
346 if (current_app) selection--;
347 WINE_TRACE("setting Version key to value '%s'\n", win_versions[selection].szVersion);
348 set_reg_key(config_key, keypath(""), "Version", win_versions[selection].szVersion);
350 /* global version only */
351 if (!current_app)
353 static const char szKey9x[] = "Software\\Microsoft\\Windows\\CurrentVersion";
354 static const char szKeyNT[] = "Software\\Microsoft\\Windows NT\\CurrentVersion";
355 static const char szKeyProdNT[] = "System\\CurrentControlSet\\Control\\ProductOptions";
356 static const char szKeyWindNT[] = "System\\CurrentControlSet\\Control\\Windows";
357 static const char szKeyEnvNT[] = "System\\CurrentControlSet\\Control\\Session Manager\\Environment";
358 char Buffer[40];
360 switch (win_versions[selection].dwPlatformId)
362 case VER_PLATFORM_WIN32_WINDOWS:
363 snprintf(Buffer, sizeof(Buffer), "%ld.%ld.%ld", win_versions[selection].dwMajorVersion,
364 win_versions[selection].dwMinorVersion, win_versions[selection].dwBuildNumber);
365 set_reg_key(HKEY_LOCAL_MACHINE, szKey9x, "VersionNumber", Buffer);
366 set_reg_key(HKEY_LOCAL_MACHINE, szKey9x, "SubVersionNumber", win_versions[selection].szCSDVersion);
368 set_reg_key(HKEY_LOCAL_MACHINE, szKeyNT, "CSDVersion", NULL);
369 set_reg_key(HKEY_LOCAL_MACHINE, szKeyNT, "CurrentVersion", NULL);
370 set_reg_key(HKEY_LOCAL_MACHINE, szKeyNT, "CurrentBuildNumber", NULL);
371 set_reg_key(HKEY_LOCAL_MACHINE, szKeyProdNT, "ProductType", NULL);
372 set_reg_key(HKEY_LOCAL_MACHINE, szKeyWindNT, "CSDVersion", NULL);
373 set_reg_key(HKEY_LOCAL_MACHINE, szKeyEnvNT, "OS", NULL);
374 break;
376 case VER_PLATFORM_WIN32_NT:
377 snprintf(Buffer, sizeof(Buffer), "%ld.%ld", win_versions[selection].dwMajorVersion,
378 win_versions[selection].dwMinorVersion);
379 set_reg_key(HKEY_LOCAL_MACHINE, szKeyNT, "CurrentVersion", Buffer);
380 set_reg_key(HKEY_LOCAL_MACHINE, szKeyNT, "CSDVersion", win_versions[selection].szCSDVersion);
381 snprintf(Buffer, sizeof(Buffer), "%ld", win_versions[selection].dwBuildNumber);
382 set_reg_key(HKEY_LOCAL_MACHINE, szKeyNT, "CurrentBuildNumber", Buffer);
383 set_reg_key(HKEY_LOCAL_MACHINE, szKeyProdNT, "ProductType", win_versions[selection].szProductType);
384 set_reg_key_dword(HKEY_LOCAL_MACHINE, szKeyWindNT, "CSDVersion",
385 MAKEWORD( win_versions[selection].wServicePackMinor,
386 win_versions[selection].wServicePackMajor ));
387 set_reg_key(HKEY_LOCAL_MACHINE, szKeyEnvNT, "OS", "Windows_NT");
389 set_reg_key(HKEY_LOCAL_MACHINE, szKey9x, "VersionNumber", NULL);
390 set_reg_key(HKEY_LOCAL_MACHINE, szKey9x, "SubVersionNumber", NULL);
391 break;
393 case VER_PLATFORM_WIN32s:
394 set_reg_key(HKEY_LOCAL_MACHINE, szKeyNT, "CSDVersion", NULL);
395 set_reg_key(HKEY_LOCAL_MACHINE, szKeyNT, "CurrentVersion", NULL);
396 set_reg_key(HKEY_LOCAL_MACHINE, szKeyNT, "CurrentBuildNumber", NULL);
397 set_reg_key(HKEY_LOCAL_MACHINE, szKeyProdNT, "ProductType", NULL);
398 set_reg_key(HKEY_LOCAL_MACHINE, szKeyWindNT, "CSDVersion", NULL);
399 set_reg_key(HKEY_LOCAL_MACHINE, szKeyEnvNT, "OS", NULL);
400 set_reg_key(HKEY_LOCAL_MACHINE, szKey9x, "VersionNumber", NULL);
401 set_reg_key(HKEY_LOCAL_MACHINE, szKey9x, "SubVersionNumber", NULL);
402 break;
406 /* enable the apply button */
407 SendMessage(GetParent(dialog), PSM_CHANGED, (WPARAM) dialog, 0);
410 INT_PTR CALLBACK
411 AppDlgProc (HWND hDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
413 switch (uMsg)
415 case WM_INITDIALOG:
416 init_appsheet(hDlg);
417 break;
419 case WM_SHOWWINDOW:
420 set_window_title(hDlg);
421 break;
423 case WM_NOTIFY:
424 switch (((LPNMHDR)lParam)->code)
426 case LVN_ITEMCHANGED:
427 on_selection_change(hDlg, GetDlgItem(hDlg, IDC_APP_LISTVIEW));
428 break;
429 case PSN_APPLY:
430 apply();
431 SetWindowLongPtr(hDlg, DWLP_MSGRESULT, PSNRET_NOERROR);
432 break;
435 break;
437 case WM_COMMAND:
438 switch(HIWORD(wParam))
440 case CBN_SELCHANGE:
441 switch(LOWORD(wParam))
443 case IDC_WINVER:
444 on_winver_change(hDlg);
445 break;
447 case BN_CLICKED:
448 switch(LOWORD(wParam))
450 case IDC_APP_ADDAPP:
451 on_add_app_click(hDlg);
452 break;
453 case IDC_APP_REMOVEAPP:
454 on_remove_app_click(hDlg);
455 break;
457 break;
460 break;
463 return 0;