winecfg: No need to cut a backslash if it does not exist.
[wine.git] / programs / winecfg / appdefaults.c
blob7dd39004a028546782ed1a6d03795bd627d1bdee
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 <stdlib.h>
31 #include <assert.h>
32 #include "wine/unicode.h"
33 #include "winecfg.h"
34 #include "resource.h"
36 WINE_DEFAULT_DEBUG_CHANNEL(winecfg);
38 static const struct
40 const char *szVersion;
41 const char *szDescription;
42 DWORD dwMajorVersion;
43 DWORD dwMinorVersion;
44 DWORD dwBuildNumber;
45 DWORD dwPlatformId;
46 const char *szCSDVersion;
47 WORD wServicePackMajor;
48 WORD wServicePackMinor;
49 const char *szProductType;
50 } win_versions[] =
52 { "win81", "Windows 8.1", 6, 3, 0x2580,VER_PLATFORM_WIN32_NT, " ", 0, 0, "WinNT"},
53 { "win8", "Windows 8", 6, 2, 0x23F0,VER_PLATFORM_WIN32_NT, " ", 0, 0, "WinNT"},
54 { "win2008r2", "Windows 2008 R2", 6, 1, 0x1DB1,VER_PLATFORM_WIN32_NT, "Service Pack 1", 1, 0, "ServerNT"},
55 { "win7", "Windows 7", 6, 1, 0x1DB1,VER_PLATFORM_WIN32_NT, "Service Pack 1", 1, 0, "WinNT"},
56 { "win2008", "Windows 2008", 6, 0, 0x1772,VER_PLATFORM_WIN32_NT, "Service Pack 2", 2, 0, "ServerNT"},
57 { "vista", "Windows Vista", 6, 0, 0x1772,VER_PLATFORM_WIN32_NT, "Service Pack 2", 2, 0, "WinNT"},
58 { "win2003", "Windows 2003", 5, 2, 0xECE, VER_PLATFORM_WIN32_NT, "Service Pack 2", 2, 0, "ServerNT"},
59 #ifdef _WIN64
60 { "winxp64", "Windows XP", 5, 2, 0xECE, VER_PLATFORM_WIN32_NT, "Service Pack 2", 2, 0, "WinNT"},
61 #else
62 { "winxp", "Windows XP", 5, 1, 0xA28, VER_PLATFORM_WIN32_NT, "Service Pack 3", 3, 0, "WinNT"},
63 { "win2k", "Windows 2000", 5, 0, 0x893, VER_PLATFORM_WIN32_NT, "Service Pack 4", 4, 0, "WinNT"},
64 { "winme", "Windows ME", 4, 90, 0xBB8, VER_PLATFORM_WIN32_WINDOWS, " ", 0, 0, ""},
65 { "win98", "Windows 98", 4, 10, 0x8AE, VER_PLATFORM_WIN32_WINDOWS, " A ", 0, 0, ""},
66 { "win95", "Windows 95", 4, 0, 0x3B6, VER_PLATFORM_WIN32_WINDOWS, "", 0, 0, ""},
67 { "nt40", "Windows NT 4.0", 4, 0, 0x565, VER_PLATFORM_WIN32_NT, "Service Pack 6a", 6, 0, "WinNT"},
68 { "nt351", "Windows NT 3.51", 3, 51, 0x421, VER_PLATFORM_WIN32_NT, "Service Pack 5", 5, 0, "WinNT"},
69 { "win31", "Windows 3.1", 3, 10, 0, VER_PLATFORM_WIN32s, "Win32s 1.3", 0, 0, ""},
70 { "win30", "Windows 3.0", 3, 0, 0, VER_PLATFORM_WIN32s, "Win32s 1.3", 0, 0, ""},
71 { "win20", "Windows 2.0", 2, 0, 0, VER_PLATFORM_WIN32s, "Win32s 1.3", 0, 0, ""}
72 #endif
75 #define NB_VERSIONS (sizeof(win_versions)/sizeof(win_versions[0]))
77 static const char szKey9x[] = "Software\\Microsoft\\Windows\\CurrentVersion";
78 static const char szKeyNT[] = "Software\\Microsoft\\Windows NT\\CurrentVersion";
79 static const char szKeyProdNT[] = "System\\CurrentControlSet\\Control\\ProductOptions";
81 static int get_registry_version(void)
83 int i, best = -1, platform, major, minor = 0, build = 0;
84 char *p, *ver, *type = NULL;
86 if ((ver = get_reg_key( HKEY_LOCAL_MACHINE, szKeyNT, "CurrentVersion", NULL )))
88 char *build_str;
90 platform = VER_PLATFORM_WIN32_NT;
92 build_str = get_reg_key( HKEY_LOCAL_MACHINE, szKeyNT, "CurrentBuildNumber", NULL );
93 build = atoi(build_str);
95 type = get_reg_key( HKEY_LOCAL_MACHINE, szKeyProdNT, "ProductType", NULL );
97 else if ((ver = get_reg_key( HKEY_LOCAL_MACHINE, szKey9x, "VersionNumber", NULL )))
98 platform = VER_PLATFORM_WIN32_WINDOWS;
99 else
100 return -1;
102 if ((p = strchr( ver, '.' )))
104 char *minor_str = p;
105 *minor_str++ = 0;
106 if ((p = strchr( minor_str, '.' )))
108 char *build_str = p;
109 *build_str++ = 0;
110 build = atoi(build_str);
112 minor = atoi(minor_str);
114 major = atoi(ver);
116 for (i = 0; i < NB_VERSIONS; i++)
118 if (win_versions[i].dwPlatformId != platform) continue;
119 if (win_versions[i].dwMajorVersion != major) continue;
120 if (type && strcasecmp(win_versions[i].szProductType, type)) continue;
121 best = i;
122 if ((win_versions[i].dwMinorVersion == minor) &&
123 (win_versions[i].dwBuildNumber == build))
124 return i;
126 return best;
129 static void update_comboboxes(HWND dialog)
131 int i, ver;
132 char *winver;
134 /* retrieve the registry values */
135 winver = get_reg_key(config_key, keypath(""), "Version", "");
136 ver = get_registry_version();
138 if (!winver || !winver[0])
140 HeapFree(GetProcessHeap(), 0, winver);
142 if (current_app) /* no explicit setting */
144 WINE_TRACE("setting winver combobox to default\n");
145 SendDlgItemMessageW(dialog, IDC_WINVER, CB_SETCURSEL, 0, 0);
146 return;
148 if (ver != -1) winver = strdupA( win_versions[ver].szVersion );
149 else winver = strdupA("winxp");
151 WINE_TRACE("winver is %s\n", winver);
153 /* normalize the version strings */
154 for (i = 0; i < NB_VERSIONS; i++)
156 if (!strcasecmp (win_versions[i].szVersion, winver))
158 SendDlgItemMessageW(dialog, IDC_WINVER, CB_SETCURSEL,
159 i + (current_app?1:0), 0);
160 WINE_TRACE("match with %s\n", win_versions[i].szVersion);
161 break;
165 HeapFree(GetProcessHeap(), 0, winver);
168 static void
169 init_comboboxes (HWND dialog)
171 int i;
173 SendDlgItemMessageW(dialog, IDC_WINVER, CB_RESETCONTENT, 0, 0);
175 /* add the default entries (automatic) which correspond to no setting */
176 if (current_app)
178 WCHAR str[256];
179 LoadStringW (GetModuleHandleW(NULL), IDS_USE_GLOBAL_SETTINGS, str,
180 sizeof(str)/sizeof(str[0]));
181 SendDlgItemMessageW (dialog, IDC_WINVER, CB_ADDSTRING, 0, (LPARAM)str);
184 for (i = 0; i < NB_VERSIONS; i++)
186 SendDlgItemMessageA(dialog, IDC_WINVER, CB_ADDSTRING,
187 0, (LPARAM) win_versions[i].szDescription);
191 static void add_listview_item(HWND listview, WCHAR *text, void *association)
193 LVITEMW item;
195 item.mask = LVIF_TEXT | LVIF_PARAM;
196 item.pszText = text;
197 item.cchTextMax = lstrlenW(text);
198 item.lParam = (LPARAM) association;
199 item.iItem = SendMessageW( listview, LVM_GETITEMCOUNT, 0, 0 );
200 item.iSubItem = 0;
202 SendMessageW(listview, LVM_INSERTITEMW, 0, (LPARAM) &item);
205 /* Called when the application is initialized (cannot reinit!) */
206 static void init_appsheet(HWND dialog)
208 HWND listview;
209 LVITEMW item;
210 HKEY key;
211 int i;
212 DWORD size;
213 WCHAR appname[1024];
215 WINE_TRACE("()\n");
217 listview = GetDlgItem(dialog, IDC_APP_LISTVIEW);
219 /* we use the lparam field of the item so we can alter the presentation later and not change code
220 * for instance, to use the tile view or to display the EXEs embedded 'display name' */
221 LoadStringW (GetModuleHandleW(NULL), IDS_DEFAULT_SETTINGS, appname,
222 sizeof(appname)/sizeof(appname[0]));
223 add_listview_item(listview, appname, NULL);
225 /* because this list is only populated once, it's safe to bypass the settings list here */
226 if (RegOpenKeyA(config_key, "AppDefaults", &key) == ERROR_SUCCESS)
228 i = 0;
229 size = sizeof(appname)/sizeof(appname[0]);
230 while (RegEnumKeyExW (key, i, appname, &size, NULL, NULL, NULL, NULL) == ERROR_SUCCESS)
232 add_listview_item(listview, appname, strdupW(appname));
234 i++;
235 size = sizeof(appname)/sizeof(appname[0]);
238 RegCloseKey(key);
241 init_comboboxes(dialog);
243 /* Select the default settings listview item */
244 item.iItem = 0;
245 item.iSubItem = 0;
246 item.mask = LVIF_STATE;
247 item.state = LVIS_SELECTED | LVIS_FOCUSED;
248 item.stateMask = LVIS_SELECTED | LVIS_FOCUSED;
250 SendMessageW(listview, LVM_SETITEMW, 0, (LPARAM) &item);
253 /* there has to be an easier way than this */
254 static int get_listview_selection(HWND listview)
256 int count = SendMessageW(listview, LVM_GETITEMCOUNT, 0, 0);
257 int i;
259 for (i = 0; i < count; i++)
261 if (SendMessageW( listview, LVM_GETITEMSTATE, i, LVIS_SELECTED )) return i;
264 return -1;
268 /* called when the user selects a different application */
269 static void on_selection_change(HWND dialog, HWND listview)
271 LVITEMW item;
272 WCHAR* oldapp = current_app;
274 WINE_TRACE("()\n");
276 item.iItem = get_listview_selection(listview);
277 item.iSubItem = 0;
278 item.mask = LVIF_PARAM;
280 WINE_TRACE("item.iItem=%d\n", item.iItem);
282 if (item.iItem == -1) return;
284 SendMessageW(listview, LVM_GETITEMW, 0, (LPARAM) &item);
286 current_app = (WCHAR*) item.lParam;
288 if (current_app)
290 WINE_TRACE("current_app is now %s\n", wine_dbgstr_w (current_app));
291 enable(IDC_APP_REMOVEAPP);
293 else
295 WINE_TRACE("current_app=NULL, editing global settings\n");
296 /* focus will never be on the button in this callback so it's safe */
297 disable(IDC_APP_REMOVEAPP);
300 /* reset the combo boxes if we changed from/to global/app-specific */
302 if ((oldapp && !current_app) || (!oldapp && current_app))
303 init_comboboxes(dialog);
305 update_comboboxes(dialog);
307 set_window_title(dialog);
310 static BOOL list_contains_file(HWND listview, WCHAR *filename)
312 LVFINDINFOW find_info = { LVFI_STRING, filename, 0, {0, 0}, 0 };
313 int index;
315 index = ListView_FindItemW(listview, -1, &find_info);
317 return (index != -1);
320 static void on_add_app_click(HWND dialog)
322 static const WCHAR filterW[] = {'%','s','%','c','*','.','e','x','e',';','*','.','e','x','e','.','s','o','%','c',0};
323 WCHAR filetitle[MAX_PATH];
324 WCHAR file[MAX_PATH];
325 WCHAR programsFilter[100], filter[MAX_PATH];
326 WCHAR selectExecutableStr[100];
327 static const WCHAR pathC[] = { 'c',':','\\',0 };
329 OPENFILENAMEW ofn = { sizeof(OPENFILENAMEW),
330 0, /*hInst*/0, 0, NULL, 0, 0, NULL,
331 0, NULL, 0, pathC, 0,
332 OFN_SHOWHELP | OFN_HIDEREADONLY | OFN_ENABLESIZING,
333 0, 0, NULL, 0, NULL };
335 LoadStringW (GetModuleHandleW(NULL), IDS_SELECT_EXECUTABLE, selectExecutableStr,
336 sizeof(selectExecutableStr)/sizeof(selectExecutableStr[0]));
337 LoadStringW (GetModuleHandleW(NULL), IDS_EXECUTABLE_FILTER, programsFilter,
338 sizeof(programsFilter)/sizeof(programsFilter[0]));
339 snprintfW( filter, MAX_PATH, filterW, programsFilter, 0, 0 );
341 ofn.lpstrTitle = selectExecutableStr;
342 ofn.lpstrFilter = filter;
343 ofn.lpstrFileTitle = filetitle;
344 ofn.lpstrFileTitle[0] = '\0';
345 ofn.nMaxFileTitle = sizeof(filetitle)/sizeof(filetitle[0]);
346 ofn.lpstrFile = file;
347 ofn.lpstrFile[0] = '\0';
348 ofn.nMaxFile = sizeof(file)/sizeof(file[0]);
350 if (GetOpenFileNameW (&ofn))
352 HWND listview = GetDlgItem(dialog, IDC_APP_LISTVIEW);
353 int count = SendMessageW(listview, LVM_GETITEMCOUNT, 0, 0);
354 WCHAR* new_app;
355 LVITEMW item;
357 if (list_contains_file(listview, filetitle))
358 return;
360 new_app = strdupW(filetitle);
362 WINE_TRACE("adding %s\n", wine_dbgstr_w (new_app));
364 add_listview_item(listview, new_app, new_app);
366 item.mask = LVIF_STATE;
367 item.state = LVIS_SELECTED | LVIS_FOCUSED;
368 item.stateMask = LVIS_SELECTED | LVIS_FOCUSED;
369 SendMessageW(listview, LVM_SETITEMSTATE, count, (LPARAM)&item );
371 SetFocus(listview);
373 else WINE_TRACE("user cancelled\n");
376 static void on_remove_app_click(HWND dialog)
378 HWND listview = GetDlgItem(dialog, IDC_APP_LISTVIEW);
379 int selection = get_listview_selection(listview);
380 LVITEMW item;
382 item.iItem = selection;
383 item.iSubItem = 0;
384 item.mask = LVIF_PARAM;
386 WINE_TRACE("selection=%d\n", selection);
388 assert( selection != 0 ); /* user cannot click this button when "default settings" is selected */
390 set_reg_key(config_key, keypath(""), NULL, NULL); /* delete the section */
391 SendMessageW(listview, LVM_GETITEMW, 0, (LPARAM) &item);
392 HeapFree (GetProcessHeap(), 0, (void*)item.lParam);
393 SendMessageW(listview, LVM_DELETEITEM, selection, 0);
394 item.mask = LVIF_STATE;
395 item.state = LVIS_SELECTED | LVIS_FOCUSED;
396 item.stateMask = LVIS_SELECTED | LVIS_FOCUSED;
397 SendMessageW(listview, LVM_SETITEMSTATE, -1, (LPARAM)&item);
399 SetFocus(listview);
400 SendMessageW(GetParent(dialog), PSM_CHANGED, (WPARAM) dialog, 0);
403 static void on_winver_change(HWND dialog)
405 int selection = SendDlgItemMessageW(dialog, IDC_WINVER, CB_GETCURSEL, 0, 0);
407 if (current_app)
409 if (!selection)
411 WINE_TRACE("default selected so removing current setting\n");
412 set_reg_key(config_key, keypath(""), "Version", NULL);
414 else
416 WINE_TRACE("setting Version key to value '%s'\n", win_versions[selection-1].szVersion);
417 set_reg_key(config_key, keypath(""), "Version", win_versions[selection-1].szVersion);
420 else /* global version only */
422 static const char szKeyWindNT[] = "System\\CurrentControlSet\\Control\\Windows";
423 static const char szKeyEnvNT[] = "System\\CurrentControlSet\\Control\\Session Manager\\Environment";
424 char Buffer[40];
426 switch (win_versions[selection].dwPlatformId)
428 case VER_PLATFORM_WIN32_WINDOWS:
429 snprintf(Buffer, sizeof(Buffer), "%d.%d.%d", win_versions[selection].dwMajorVersion,
430 win_versions[selection].dwMinorVersion, win_versions[selection].dwBuildNumber);
431 set_reg_key(HKEY_LOCAL_MACHINE, szKey9x, "VersionNumber", Buffer);
432 set_reg_key(HKEY_LOCAL_MACHINE, szKey9x, "SubVersionNumber", win_versions[selection].szCSDVersion);
433 snprintf(Buffer, sizeof(Buffer), "Microsoft %s", win_versions[selection].szDescription);
434 set_reg_key(HKEY_LOCAL_MACHINE, szKey9x, "ProductName", Buffer);
436 set_reg_key(HKEY_LOCAL_MACHINE, szKeyNT, "CSDVersion", NULL);
437 set_reg_key(HKEY_LOCAL_MACHINE, szKeyNT, "CurrentVersion", NULL);
438 set_reg_key(HKEY_LOCAL_MACHINE, szKeyNT, "CurrentBuildNumber", NULL);
439 set_reg_key(HKEY_LOCAL_MACHINE, szKeyNT, "ProductName", NULL);
440 set_reg_key(HKEY_LOCAL_MACHINE, szKeyProdNT, "ProductType", NULL);
441 set_reg_key(HKEY_LOCAL_MACHINE, szKeyWindNT, "CSDVersion", NULL);
442 set_reg_key(HKEY_LOCAL_MACHINE, szKeyEnvNT, "OS", NULL);
443 set_reg_key(config_key, keypath(""), "Version", NULL);
444 break;
446 case VER_PLATFORM_WIN32_NT:
447 snprintf(Buffer, sizeof(Buffer), "%d.%d", win_versions[selection].dwMajorVersion,
448 win_versions[selection].dwMinorVersion);
449 set_reg_key(HKEY_LOCAL_MACHINE, szKeyNT, "CurrentVersion", Buffer);
450 set_reg_key(HKEY_LOCAL_MACHINE, szKeyNT, "CSDVersion", win_versions[selection].szCSDVersion);
451 snprintf(Buffer, sizeof(Buffer), "%d", win_versions[selection].dwBuildNumber);
452 set_reg_key(HKEY_LOCAL_MACHINE, szKeyNT, "CurrentBuildNumber", Buffer);
453 snprintf(Buffer, sizeof(Buffer), "Microsoft %s", win_versions[selection].szDescription);
454 set_reg_key(HKEY_LOCAL_MACHINE, szKeyNT, "ProductName", Buffer);
455 set_reg_key(HKEY_LOCAL_MACHINE, szKeyProdNT, "ProductType", win_versions[selection].szProductType);
456 set_reg_key_dword(HKEY_LOCAL_MACHINE, szKeyWindNT, "CSDVersion",
457 MAKEWORD( win_versions[selection].wServicePackMinor,
458 win_versions[selection].wServicePackMajor ));
459 set_reg_key(HKEY_LOCAL_MACHINE, szKeyEnvNT, "OS", "Windows_NT");
461 set_reg_key(HKEY_LOCAL_MACHINE, szKey9x, "VersionNumber", NULL);
462 set_reg_key(HKEY_LOCAL_MACHINE, szKey9x, "SubVersionNumber", NULL);
463 set_reg_key(HKEY_LOCAL_MACHINE, szKey9x, "ProductName", NULL);
464 set_reg_key(config_key, keypath(""), "Version", NULL);
465 break;
467 case VER_PLATFORM_WIN32s:
468 set_reg_key(HKEY_LOCAL_MACHINE, szKeyNT, "CSDVersion", NULL);
469 set_reg_key(HKEY_LOCAL_MACHINE, szKeyNT, "CurrentVersion", NULL);
470 set_reg_key(HKEY_LOCAL_MACHINE, szKeyNT, "CurrentBuildNumber", NULL);
471 set_reg_key(HKEY_LOCAL_MACHINE, szKeyNT, "ProductName", NULL);
472 set_reg_key(HKEY_LOCAL_MACHINE, szKeyProdNT, "ProductType", NULL);
473 set_reg_key(HKEY_LOCAL_MACHINE, szKeyWindNT, "CSDVersion", NULL);
474 set_reg_key(HKEY_LOCAL_MACHINE, szKeyEnvNT, "OS", NULL);
475 set_reg_key(HKEY_LOCAL_MACHINE, szKey9x, "VersionNumber", NULL);
476 set_reg_key(HKEY_LOCAL_MACHINE, szKey9x, "SubVersionNumber", NULL);
477 set_reg_key(HKEY_LOCAL_MACHINE, szKey9x, "ProductName", NULL);
478 set_reg_key(config_key, keypath(""), "Version", win_versions[selection].szVersion);
479 break;
483 /* enable the apply button */
484 SendMessageW(GetParent(dialog), PSM_CHANGED, (WPARAM) dialog, 0);
487 INT_PTR CALLBACK
488 AppDlgProc (HWND hDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
490 switch (uMsg)
492 case WM_INITDIALOG:
493 init_appsheet(hDlg);
494 break;
496 case WM_SHOWWINDOW:
497 set_window_title(hDlg);
498 break;
500 case WM_NOTIFY:
501 switch (((LPNMHDR)lParam)->code)
503 case LVN_ITEMCHANGED:
504 on_selection_change(hDlg, GetDlgItem(hDlg, IDC_APP_LISTVIEW));
505 break;
506 case PSN_APPLY:
507 apply();
508 SetWindowLongPtrW(hDlg, DWLP_MSGRESULT, PSNRET_NOERROR);
509 break;
512 break;
514 case WM_COMMAND:
515 switch(HIWORD(wParam))
517 case CBN_SELCHANGE:
518 switch(LOWORD(wParam))
520 case IDC_WINVER:
521 on_winver_change(hDlg);
522 break;
524 case BN_CLICKED:
525 switch(LOWORD(wParam))
527 case IDC_APP_ADDAPP:
528 on_add_app_click(hDlg);
529 break;
530 case IDC_APP_REMOVEAPP:
531 on_remove_app_click(hDlg);
532 break;
534 break;
537 break;
540 return 0;