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
28 #include <wine/debug.h>
32 #include "wine/unicode.h"
36 WINE_DEFAULT_DEBUG_CHANNEL(winecfg);
40 const char *szVersion;
41 const char *szDescription;
46 const char *szCSDVersion;
47 WORD wServicePackMajor;
48 WORD wServicePackMinor;
49 const char *szProductType;
52 { "win2008r2", "Windows 2008 R2", 6, 1, 0x1DB1,VER_PLATFORM_WIN32_NT, "Service Pack 1", 1, 0, "ServerNT"},
53 { "win7", "Windows 7", 6, 1, 0x1DB1,VER_PLATFORM_WIN32_NT, "Service Pack 1", 1, 0, "WinNT"},
54 { "win2008", "Windows 2008", 6, 0, 0x1772,VER_PLATFORM_WIN32_NT, "Service Pack 2", 0, 0, "ServerNT"},
55 { "vista", "Windows Vista", 6, 0, 0x1772,VER_PLATFORM_WIN32_NT, "Service Pack 2", 2, 0, "WinNT"},
56 { "win2003", "Windows 2003", 5, 2, 0xECE, VER_PLATFORM_WIN32_NT, "Service Pack 2", 2, 0, "ServerNT"},
57 { "winxp", "Windows XP", 5, 1, 0xA28, VER_PLATFORM_WIN32_NT, "Service Pack 3", 3, 0, "WinNT"},
58 { "win2k", "Windows 2000", 5, 0, 0x893, VER_PLATFORM_WIN32_NT, "Service Pack 4", 4, 0, "WinNT"},
59 { "winme", "Windows ME", 4, 90, 0xBB8, VER_PLATFORM_WIN32_WINDOWS, " ", 0, 0, ""},
60 { "win98", "Windows 98", 4, 10, 0x8AE, VER_PLATFORM_WIN32_WINDOWS, " A ", 0, 0, ""},
61 { "win95", "Windows 95", 4, 0, 0x3B6, VER_PLATFORM_WIN32_WINDOWS, "", 0, 0, ""},
62 { "nt40", "Windows NT 4.0", 4, 0, 0x565, VER_PLATFORM_WIN32_NT, "Service Pack 6a", 6, 0, "WinNT"},
63 { "nt351", "Windows NT 3.5", 3, 51, 0x421, VER_PLATFORM_WIN32_NT, "Service Pack 2", 0, 0, "WinNT"},
64 { "win31", "Windows 3.1", 3, 10, 0, VER_PLATFORM_WIN32s, "Win32s 1.3", 0, 0, ""},
65 { "win30", "Windows 3.0", 3, 0, 0, VER_PLATFORM_WIN32s, "Win32s 1.3", 0, 0, ""},
66 { "win20", "Windows 2.0", 2, 0, 0, VER_PLATFORM_WIN32s, "Win32s 1.3", 0, 0, ""}
69 #define NB_VERSIONS (sizeof(win_versions)/sizeof(win_versions[0]))
71 static const char szKey9x[] = "Software\\Microsoft\\Windows\\CurrentVersion";
72 static const char szKeyNT[] = "Software\\Microsoft\\Windows NT\\CurrentVersion";
73 static const char szKeyProdNT[] = "System\\CurrentControlSet\\Control\\ProductOptions";
75 static int get_registry_version(void)
77 int i, best = -1, platform, major, minor = 0, build = 0;
78 char *p, *ver, *type = NULL;
80 if ((ver = get_reg_key( HKEY_LOCAL_MACHINE, szKeyNT, "CurrentVersion", NULL )))
84 platform = VER_PLATFORM_WIN32_NT;
86 build_str = get_reg_key( HKEY_LOCAL_MACHINE, szKeyNT, "CurrentBuildNumber", NULL );
87 build = atoi(build_str);
89 type = get_reg_key( HKEY_LOCAL_MACHINE, szKeyProdNT, "ProductType", NULL );
91 else if ((ver = get_reg_key( HKEY_LOCAL_MACHINE, szKey9x, "VersionNumber", NULL )))
92 platform = VER_PLATFORM_WIN32_WINDOWS;
96 if ((p = strchr( ver, '.' )))
100 if ((p = strchr( minor_str, '.' )))
104 build = atoi(build_str);
106 minor = atoi(minor_str);
110 for (i = 0; i < NB_VERSIONS; i++)
112 if (win_versions[i].dwPlatformId != platform) continue;
113 if (win_versions[i].dwMajorVersion != major) continue;
114 if (type && strcasecmp(win_versions[i].szProductType, type)) continue;
116 if ((win_versions[i].dwMinorVersion == minor) &&
117 (win_versions[i].dwBuildNumber == build))
123 static void update_comboboxes(HWND dialog)
128 /* retrieve the registry values */
129 winver = get_reg_key(config_key, keypath(""), "Version", "");
130 ver = get_registry_version();
132 if (!winver || !winver[0])
134 HeapFree(GetProcessHeap(), 0, winver);
136 if (current_app) /* no explicit setting */
138 WINE_TRACE("setting winver combobox to default\n");
139 SendDlgItemMessage (dialog, IDC_WINVER, CB_SETCURSEL, 0, 0);
142 if (ver != -1) winver = strdupA( win_versions[ver].szVersion );
143 else winver = strdupA("winxp");
145 WINE_TRACE("winver is %s\n", winver);
147 /* normalize the version strings */
148 for (i = 0; i < NB_VERSIONS; i++)
150 if (!strcasecmp (win_versions[i].szVersion, winver))
152 SendDlgItemMessage (dialog, IDC_WINVER, CB_SETCURSEL,
153 i + (current_app?1:0), 0);
154 WINE_TRACE("match with %s\n", win_versions[i].szVersion);
159 HeapFree(GetProcessHeap(), 0, winver);
163 init_comboboxes (HWND dialog)
167 SendDlgItemMessage(dialog, IDC_WINVER, CB_RESETCONTENT, 0, 0);
169 /* add the default entries (automatic) which correspond to no setting */
173 LoadStringW (GetModuleHandle (NULL), IDS_USE_GLOBAL_SETTINGS, str,
174 sizeof(str)/sizeof(str[0]));
175 SendDlgItemMessageW (dialog, IDC_WINVER, CB_ADDSTRING, 0, (LPARAM)str);
178 for (i = 0; i < NB_VERSIONS; i++)
180 SendDlgItemMessage (dialog, IDC_WINVER, CB_ADDSTRING,
181 0, (LPARAM) win_versions[i].szDescription);
185 static void add_listview_item(HWND listview, WCHAR *text, void *association)
189 item.mask = LVIF_TEXT | LVIF_PARAM;
191 item.cchTextMax = lstrlenW(text);
192 item.lParam = (LPARAM) association;
193 item.iItem = ListView_GetItemCount(listview);
196 SendMessage(listview, LVM_INSERTITEMW, 0, (LPARAM) &item);
199 /* Called when the application is initialized (cannot reinit!) */
200 static void init_appsheet(HWND dialog)
210 listview = GetDlgItem(dialog, IDC_APP_LISTVIEW);
212 /* we use the lparam field of the item so we can alter the presentation later and not change code
213 * for instance, to use the tile view or to display the EXEs embedded 'display name' */
214 LoadStringW (GetModuleHandle (NULL), IDS_DEFAULT_SETTINGS, appname,
215 sizeof(appname)/sizeof(appname[0]));
216 add_listview_item(listview, appname, NULL);
218 /* because this list is only populated once, it's safe to bypass the settings list here */
219 if (RegOpenKey(config_key, "AppDefaults", &key) == ERROR_SUCCESS)
222 size = sizeof(appname)/sizeof(appname[0]);
223 while (RegEnumKeyExW (key, i, appname, &size, NULL, NULL, NULL, NULL) == ERROR_SUCCESS)
225 add_listview_item(listview, appname, strdupW(appname));
228 size = sizeof(appname)/sizeof(appname[0]);
234 init_comboboxes(dialog);
236 /* Select the default settings listview item */
242 item.mask = LVIF_STATE;
243 item.state = LVIS_SELECTED | LVIS_FOCUSED;
244 item.stateMask = LVIS_SELECTED | LVIS_FOCUSED;
246 SendMessage(listview, LVM_SETITEM, 0, (LPARAM) &item);
251 /* there has to be an easier way than this */
252 static int get_listview_selection(HWND listview)
254 int count = ListView_GetItemCount(listview);
257 for (i = 0; i < count; i++)
259 if (ListView_GetItemState(listview, i, LVIS_SELECTED)) return i;
266 /* called when the user selects a different application */
267 static void on_selection_change(HWND dialog, HWND listview)
270 WCHAR* oldapp = current_app;
274 item.iItem = get_listview_selection(listview);
276 item.mask = LVIF_PARAM;
278 WINE_TRACE("item.iItem=%d\n", item.iItem);
280 if (item.iItem == -1) return;
282 SendMessage(listview, LVM_GETITEM, 0, (LPARAM) &item);
284 current_app = (WCHAR*) item.lParam;
288 WINE_TRACE("current_app is now %s\n", wine_dbgstr_w (current_app));
289 enable(IDC_APP_REMOVEAPP);
293 WINE_TRACE("current_app=NULL, editing global settings\n");
294 /* focus will never be on the button in this callback so it's safe */
295 disable(IDC_APP_REMOVEAPP);
298 /* reset the combo boxes if we changed from/to global/app-specific */
300 if ((oldapp && !current_app) || (!oldapp && current_app))
301 init_comboboxes(dialog);
303 update_comboboxes(dialog);
305 set_window_title(dialog);
308 static BOOL list_contains_file(HWND listview, WCHAR *filename)
310 LVFINDINFOW find_info = { LVFI_STRING, filename, 0, {0, 0}, 0 };
313 index = ListView_FindItemW(listview, -1, &find_info);
315 return (index != -1);
318 static void on_add_app_click(HWND dialog)
320 static const WCHAR filterW[] = {'%','s','%','c','*','.','e','x','e',';','*','.','e','x','e','.','s','o','%','c',0};
321 WCHAR filetitle[MAX_PATH];
322 WCHAR file[MAX_PATH];
323 WCHAR programsFilter[100], filter[MAX_PATH];
324 WCHAR selectExecutableStr[100];
325 static const WCHAR pathC[] = { 'c',':','\\',0 };
327 OPENFILENAMEW ofn = { sizeof(OPENFILENAMEW),
328 0, /*hInst*/0, 0, NULL, 0, 0, NULL,
329 0, NULL, 0, pathC, 0,
330 OFN_SHOWHELP | OFN_HIDEREADONLY | OFN_ENABLESIZING,
331 0, 0, NULL, 0, NULL };
333 LoadStringW (GetModuleHandle (NULL), IDS_SELECT_EXECUTABLE, selectExecutableStr,
334 sizeof(selectExecutableStr)/sizeof(selectExecutableStr[0]));
335 LoadStringW (GetModuleHandle (NULL), IDS_EXECUTABLE_FILTER, programsFilter,
336 sizeof(programsFilter)/sizeof(programsFilter[0]));
337 snprintfW( filter, MAX_PATH, filterW, programsFilter, 0, 0 );
339 ofn.lpstrTitle = selectExecutableStr;
340 ofn.lpstrFilter = filter;
341 ofn.lpstrFileTitle = filetitle;
342 ofn.lpstrFileTitle[0] = '\0';
343 ofn.nMaxFileTitle = sizeof(filetitle)/sizeof(filetitle[0]);
344 ofn.lpstrFile = file;
345 ofn.lpstrFile[0] = '\0';
346 ofn.nMaxFile = sizeof(file)/sizeof(file[0]);
348 if (GetOpenFileNameW (&ofn))
350 HWND listview = GetDlgItem(dialog, IDC_APP_LISTVIEW);
351 int count = ListView_GetItemCount(listview);
354 if (list_contains_file(listview, filetitle))
357 new_app = strdupW(filetitle);
359 WINE_TRACE("adding %s\n", wine_dbgstr_w (new_app));
361 add_listview_item(listview, new_app, new_app);
363 ListView_SetItemState(listview, count, LVIS_SELECTED | LVIS_FOCUSED, LVIS_SELECTED | LVIS_FOCUSED);
367 else WINE_TRACE("user cancelled\n");
370 static void on_remove_app_click(HWND dialog)
372 HWND listview = GetDlgItem(dialog, IDC_APP_LISTVIEW);
373 int selection = get_listview_selection(listview);
374 char *section = keypath(""); /* AppDefaults\\whatever.exe\\ */
377 item.iItem = selection;
379 item.mask = LVIF_PARAM;
381 WINE_TRACE("selection=%d, section=%s\n", selection, section);
383 assert( selection != 0 ); /* user cannot click this button when "default settings" is selected */
385 section[strlen(section)] = '\0'; /* remove last backslash */
386 set_reg_key(config_key, section, NULL, NULL); /* delete the section */
387 SendMessage(listview, LVM_GETITEMW, 0, (LPARAM) &item);
388 HeapFree (GetProcessHeap(), 0, (void*)item.lParam);
389 SendMessage(listview, LVM_DELETEITEM, selection, 0);
390 ListView_SetItemState(listview, selection - 1, LVIS_SELECTED | LVIS_FOCUSED, LVIS_SELECTED | LVIS_FOCUSED);
394 SendMessage(GetParent(dialog), PSM_CHANGED, (WPARAM) dialog, 0);
397 static void on_winver_change(HWND dialog)
399 int selection = SendDlgItemMessage(dialog, IDC_WINVER, CB_GETCURSEL, 0, 0);
405 WINE_TRACE("default selected so removing current setting\n");
406 set_reg_key(config_key, keypath(""), "Version", NULL);
410 WINE_TRACE("setting Version key to value '%s'\n", win_versions[selection-1].szVersion);
411 set_reg_key(config_key, keypath(""), "Version", win_versions[selection-1].szVersion);
414 else /* global version only */
416 static const char szKeyWindNT[] = "System\\CurrentControlSet\\Control\\Windows";
417 static const char szKeyEnvNT[] = "System\\CurrentControlSet\\Control\\Session Manager\\Environment";
420 switch (win_versions[selection].dwPlatformId)
422 case VER_PLATFORM_WIN32_WINDOWS:
423 snprintf(Buffer, sizeof(Buffer), "%d.%d.%d", win_versions[selection].dwMajorVersion,
424 win_versions[selection].dwMinorVersion, win_versions[selection].dwBuildNumber);
425 set_reg_key(HKEY_LOCAL_MACHINE, szKey9x, "VersionNumber", Buffer);
426 set_reg_key(HKEY_LOCAL_MACHINE, szKey9x, "SubVersionNumber", win_versions[selection].szCSDVersion);
427 snprintf(Buffer, sizeof(Buffer), "Microsoft %s", win_versions[selection].szDescription);
428 set_reg_key(HKEY_LOCAL_MACHINE, szKey9x, "ProductName", Buffer);
430 set_reg_key(HKEY_LOCAL_MACHINE, szKeyNT, "CSDVersion", NULL);
431 set_reg_key(HKEY_LOCAL_MACHINE, szKeyNT, "CurrentVersion", NULL);
432 set_reg_key(HKEY_LOCAL_MACHINE, szKeyNT, "CurrentBuildNumber", NULL);
433 set_reg_key(HKEY_LOCAL_MACHINE, szKeyNT, "ProductName", NULL);
434 set_reg_key(HKEY_LOCAL_MACHINE, szKeyProdNT, "ProductType", NULL);
435 set_reg_key(HKEY_LOCAL_MACHINE, szKeyWindNT, "CSDVersion", NULL);
436 set_reg_key(HKEY_LOCAL_MACHINE, szKeyEnvNT, "OS", NULL);
437 set_reg_key(config_key, keypath(""), "Version", NULL);
440 case VER_PLATFORM_WIN32_NT:
441 snprintf(Buffer, sizeof(Buffer), "%d.%d", win_versions[selection].dwMajorVersion,
442 win_versions[selection].dwMinorVersion);
443 set_reg_key(HKEY_LOCAL_MACHINE, szKeyNT, "CurrentVersion", Buffer);
444 set_reg_key(HKEY_LOCAL_MACHINE, szKeyNT, "CSDVersion", win_versions[selection].szCSDVersion);
445 snprintf(Buffer, sizeof(Buffer), "%d", win_versions[selection].dwBuildNumber);
446 set_reg_key(HKEY_LOCAL_MACHINE, szKeyNT, "CurrentBuildNumber", Buffer);
447 snprintf(Buffer, sizeof(Buffer), "Microsoft %s", win_versions[selection].szDescription);
448 set_reg_key(HKEY_LOCAL_MACHINE, szKeyNT, "ProductName", Buffer);
449 set_reg_key(HKEY_LOCAL_MACHINE, szKeyProdNT, "ProductType", win_versions[selection].szProductType);
450 set_reg_key_dword(HKEY_LOCAL_MACHINE, szKeyWindNT, "CSDVersion",
451 MAKEWORD( win_versions[selection].wServicePackMinor,
452 win_versions[selection].wServicePackMajor ));
453 set_reg_key(HKEY_LOCAL_MACHINE, szKeyEnvNT, "OS", "Windows_NT");
455 set_reg_key(HKEY_LOCAL_MACHINE, szKey9x, "VersionNumber", NULL);
456 set_reg_key(HKEY_LOCAL_MACHINE, szKey9x, "SubVersionNumber", NULL);
457 set_reg_key(HKEY_LOCAL_MACHINE, szKey9x, "ProductName", NULL);
458 set_reg_key(config_key, keypath(""), "Version", NULL);
461 case VER_PLATFORM_WIN32s:
462 set_reg_key(HKEY_LOCAL_MACHINE, szKeyNT, "CSDVersion", NULL);
463 set_reg_key(HKEY_LOCAL_MACHINE, szKeyNT, "CurrentVersion", NULL);
464 set_reg_key(HKEY_LOCAL_MACHINE, szKeyNT, "CurrentBuildNumber", NULL);
465 set_reg_key(HKEY_LOCAL_MACHINE, szKeyNT, "ProductName", NULL);
466 set_reg_key(HKEY_LOCAL_MACHINE, szKeyProdNT, "ProductType", NULL);
467 set_reg_key(HKEY_LOCAL_MACHINE, szKeyWindNT, "CSDVersion", NULL);
468 set_reg_key(HKEY_LOCAL_MACHINE, szKeyEnvNT, "OS", NULL);
469 set_reg_key(HKEY_LOCAL_MACHINE, szKey9x, "VersionNumber", NULL);
470 set_reg_key(HKEY_LOCAL_MACHINE, szKey9x, "SubVersionNumber", NULL);
471 set_reg_key(HKEY_LOCAL_MACHINE, szKey9x, "ProductName", NULL);
472 set_reg_key(config_key, keypath(""), "Version", win_versions[selection].szVersion);
477 /* enable the apply button */
478 SendMessage(GetParent(dialog), PSM_CHANGED, (WPARAM) dialog, 0);
482 AppDlgProc (HWND hDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
491 set_window_title(hDlg);
495 switch (((LPNMHDR)lParam)->code)
497 case LVN_ITEMCHANGED:
498 on_selection_change(hDlg, GetDlgItem(hDlg, IDC_APP_LISTVIEW));
502 SetWindowLongPtr(hDlg, DWLP_MSGRESULT, PSNRET_NOERROR);
509 switch(HIWORD(wParam))
512 switch(LOWORD(wParam))
515 on_winver_change(hDlg);
519 switch(LOWORD(wParam))
522 on_add_app_click(hDlg);
524 case IDC_APP_REMOVEAPP:
525 on_remove_app_click(hDlg);