msvcrt: Added _mbstok_s_l implementation.
[wine.git] / programs / winecfg / appdefaults.c
blobc8940a651398e8837d130ca67d990c2561a54a57
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 { "win8", "Windows 8", 6, 2, 0x23F0,VER_PLATFORM_WIN32_NT, " ", 0, 0, "WinNT"},
53 { "win2008r2", "Windows 2008 R2", 6, 1, 0x1DB1,VER_PLATFORM_WIN32_NT, "Service Pack 1", 1, 0, "ServerNT"},
54 { "win7", "Windows 7", 6, 1, 0x1DB1,VER_PLATFORM_WIN32_NT, "Service Pack 1", 1, 0, "WinNT"},
55 { "win2008", "Windows 2008", 6, 0, 0x1772,VER_PLATFORM_WIN32_NT, "Service Pack 2", 2, 0, "ServerNT"},
56 { "vista", "Windows Vista", 6, 0, 0x1772,VER_PLATFORM_WIN32_NT, "Service Pack 2", 2, 0, "WinNT"},
57 { "win2003", "Windows 2003", 5, 2, 0xECE, VER_PLATFORM_WIN32_NT, "Service Pack 2", 2, 0, "ServerNT"},
58 { "winxp", "Windows XP", 5, 1, 0xA28, VER_PLATFORM_WIN32_NT, "Service Pack 3", 3, 0, "WinNT"},
59 { "win2k", "Windows 2000", 5, 0, 0x893, VER_PLATFORM_WIN32_NT, "Service Pack 4", 4, 0, "WinNT"},
60 { "winme", "Windows ME", 4, 90, 0xBB8, VER_PLATFORM_WIN32_WINDOWS, " ", 0, 0, ""},
61 { "win98", "Windows 98", 4, 10, 0x8AE, VER_PLATFORM_WIN32_WINDOWS, " A ", 0, 0, ""},
62 { "win95", "Windows 95", 4, 0, 0x3B6, VER_PLATFORM_WIN32_WINDOWS, "", 0, 0, ""},
63 { "nt40", "Windows NT 4.0", 4, 0, 0x565, VER_PLATFORM_WIN32_NT, "Service Pack 6a", 6, 0, "WinNT"},
64 { "nt351", "Windows NT 3.51", 3, 51, 0x421, VER_PLATFORM_WIN32_NT, "Service Pack 5", 5, 0, "WinNT"},
65 { "win31", "Windows 3.1", 3, 10, 0, VER_PLATFORM_WIN32s, "Win32s 1.3", 0, 0, ""},
66 { "win30", "Windows 3.0", 3, 0, 0, VER_PLATFORM_WIN32s, "Win32s 1.3", 0, 0, ""},
67 { "win20", "Windows 2.0", 2, 0, 0, VER_PLATFORM_WIN32s, "Win32s 1.3", 0, 0, ""}
70 #define NB_VERSIONS (sizeof(win_versions)/sizeof(win_versions[0]))
72 static const char szKey9x[] = "Software\\Microsoft\\Windows\\CurrentVersion";
73 static const char szKeyNT[] = "Software\\Microsoft\\Windows NT\\CurrentVersion";
74 static const char szKeyProdNT[] = "System\\CurrentControlSet\\Control\\ProductOptions";
76 static int get_registry_version(void)
78 int i, best = -1, platform, major, minor = 0, build = 0;
79 char *p, *ver, *type = NULL;
81 if ((ver = get_reg_key( HKEY_LOCAL_MACHINE, szKeyNT, "CurrentVersion", NULL )))
83 char *build_str;
85 platform = VER_PLATFORM_WIN32_NT;
87 build_str = get_reg_key( HKEY_LOCAL_MACHINE, szKeyNT, "CurrentBuildNumber", NULL );
88 build = atoi(build_str);
90 type = get_reg_key( HKEY_LOCAL_MACHINE, szKeyProdNT, "ProductType", NULL );
92 else if ((ver = get_reg_key( HKEY_LOCAL_MACHINE, szKey9x, "VersionNumber", NULL )))
93 platform = VER_PLATFORM_WIN32_WINDOWS;
94 else
95 return -1;
97 if ((p = strchr( ver, '.' )))
99 char *minor_str = p;
100 *minor_str++ = 0;
101 if ((p = strchr( minor_str, '.' )))
103 char *build_str = p;
104 *build_str++ = 0;
105 build = atoi(build_str);
107 minor = atoi(minor_str);
109 major = atoi(ver);
111 for (i = 0; i < NB_VERSIONS; i++)
113 if (win_versions[i].dwPlatformId != platform) continue;
114 if (win_versions[i].dwMajorVersion != major) continue;
115 if (type && strcasecmp(win_versions[i].szProductType, type)) continue;
116 best = i;
117 if ((win_versions[i].dwMinorVersion == minor) &&
118 (win_versions[i].dwBuildNumber == build))
119 return i;
121 return best;
124 static void update_comboboxes(HWND dialog)
126 int i, ver;
127 char *winver;
129 /* retrieve the registry values */
130 winver = get_reg_key(config_key, keypath(""), "Version", "");
131 ver = get_registry_version();
133 if (!winver || !winver[0])
135 HeapFree(GetProcessHeap(), 0, winver);
137 if (current_app) /* no explicit setting */
139 WINE_TRACE("setting winver combobox to default\n");
140 SendDlgItemMessageW(dialog, IDC_WINVER, CB_SETCURSEL, 0, 0);
141 return;
143 if (ver != -1) winver = strdupA( win_versions[ver].szVersion );
144 else winver = strdupA("winxp");
146 WINE_TRACE("winver is %s\n", winver);
148 /* normalize the version strings */
149 for (i = 0; i < NB_VERSIONS; i++)
151 if (!strcasecmp (win_versions[i].szVersion, winver))
153 SendDlgItemMessageW(dialog, IDC_WINVER, CB_SETCURSEL,
154 i + (current_app?1:0), 0);
155 WINE_TRACE("match with %s\n", win_versions[i].szVersion);
156 break;
160 HeapFree(GetProcessHeap(), 0, winver);
163 static void
164 init_comboboxes (HWND dialog)
166 int i;
168 SendDlgItemMessageW(dialog, IDC_WINVER, CB_RESETCONTENT, 0, 0);
170 /* add the default entries (automatic) which correspond to no setting */
171 if (current_app)
173 WCHAR str[256];
174 LoadStringW (GetModuleHandleW(NULL), IDS_USE_GLOBAL_SETTINGS, str,
175 sizeof(str)/sizeof(str[0]));
176 SendDlgItemMessageW (dialog, IDC_WINVER, CB_ADDSTRING, 0, (LPARAM)str);
179 for (i = 0; i < NB_VERSIONS; i++)
181 SendDlgItemMessageA(dialog, IDC_WINVER, CB_ADDSTRING,
182 0, (LPARAM) win_versions[i].szDescription);
186 static void add_listview_item(HWND listview, WCHAR *text, void *association)
188 LVITEMW item;
190 item.mask = LVIF_TEXT | LVIF_PARAM;
191 item.pszText = text;
192 item.cchTextMax = lstrlenW(text);
193 item.lParam = (LPARAM) association;
194 item.iItem = SendMessageW( listview, LVM_GETITEMCOUNT, 0, 0 );
195 item.iSubItem = 0;
197 SendMessageW(listview, LVM_INSERTITEMW, 0, (LPARAM) &item);
200 /* Called when the application is initialized (cannot reinit!) */
201 static void init_appsheet(HWND dialog)
203 HWND listview;
204 LVITEMW item;
205 HKEY key;
206 int i;
207 DWORD size;
208 WCHAR appname[1024];
210 WINE_TRACE("()\n");
212 listview = GetDlgItem(dialog, IDC_APP_LISTVIEW);
214 /* we use the lparam field of the item so we can alter the presentation later and not change code
215 * for instance, to use the tile view or to display the EXEs embedded 'display name' */
216 LoadStringW (GetModuleHandleW(NULL), IDS_DEFAULT_SETTINGS, appname,
217 sizeof(appname)/sizeof(appname[0]));
218 add_listview_item(listview, appname, NULL);
220 /* because this list is only populated once, it's safe to bypass the settings list here */
221 if (RegOpenKeyA(config_key, "AppDefaults", &key) == ERROR_SUCCESS)
223 i = 0;
224 size = sizeof(appname)/sizeof(appname[0]);
225 while (RegEnumKeyExW (key, i, appname, &size, NULL, NULL, NULL, NULL) == ERROR_SUCCESS)
227 add_listview_item(listview, appname, strdupW(appname));
229 i++;
230 size = sizeof(appname)/sizeof(appname[0]);
233 RegCloseKey(key);
236 init_comboboxes(dialog);
238 /* Select the default settings listview item */
239 item.iItem = 0;
240 item.iSubItem = 0;
241 item.mask = LVIF_STATE;
242 item.state = LVIS_SELECTED | LVIS_FOCUSED;
243 item.stateMask = LVIS_SELECTED | LVIS_FOCUSED;
245 SendMessageW(listview, LVM_SETITEMW, 0, (LPARAM) &item);
248 /* there has to be an easier way than this */
249 static int get_listview_selection(HWND listview)
251 int count = SendMessageW(listview, LVM_GETITEMCOUNT, 0, 0);
252 int i;
254 for (i = 0; i < count; i++)
256 if (SendMessageW( listview, LVM_GETITEMSTATE, i, LVIS_SELECTED )) return i;
259 return -1;
263 /* called when the user selects a different application */
264 static void on_selection_change(HWND dialog, HWND listview)
266 LVITEMW item;
267 WCHAR* oldapp = current_app;
269 WINE_TRACE("()\n");
271 item.iItem = get_listview_selection(listview);
272 item.iSubItem = 0;
273 item.mask = LVIF_PARAM;
275 WINE_TRACE("item.iItem=%d\n", item.iItem);
277 if (item.iItem == -1) return;
279 SendMessageW(listview, LVM_GETITEMW, 0, (LPARAM) &item);
281 current_app = (WCHAR*) item.lParam;
283 if (current_app)
285 WINE_TRACE("current_app is now %s\n", wine_dbgstr_w (current_app));
286 enable(IDC_APP_REMOVEAPP);
288 else
290 WINE_TRACE("current_app=NULL, editing global settings\n");
291 /* focus will never be on the button in this callback so it's safe */
292 disable(IDC_APP_REMOVEAPP);
295 /* reset the combo boxes if we changed from/to global/app-specific */
297 if ((oldapp && !current_app) || (!oldapp && current_app))
298 init_comboboxes(dialog);
300 update_comboboxes(dialog);
302 set_window_title(dialog);
305 static BOOL list_contains_file(HWND listview, WCHAR *filename)
307 LVFINDINFOW find_info = { LVFI_STRING, filename, 0, {0, 0}, 0 };
308 int index;
310 index = ListView_FindItemW(listview, -1, &find_info);
312 return (index != -1);
315 static void on_add_app_click(HWND dialog)
317 static const WCHAR filterW[] = {'%','s','%','c','*','.','e','x','e',';','*','.','e','x','e','.','s','o','%','c',0};
318 WCHAR filetitle[MAX_PATH];
319 WCHAR file[MAX_PATH];
320 WCHAR programsFilter[100], filter[MAX_PATH];
321 WCHAR selectExecutableStr[100];
322 static const WCHAR pathC[] = { 'c',':','\\',0 };
324 OPENFILENAMEW ofn = { sizeof(OPENFILENAMEW),
325 0, /*hInst*/0, 0, NULL, 0, 0, NULL,
326 0, NULL, 0, pathC, 0,
327 OFN_SHOWHELP | OFN_HIDEREADONLY | OFN_ENABLESIZING,
328 0, 0, NULL, 0, NULL };
330 LoadStringW (GetModuleHandleW(NULL), IDS_SELECT_EXECUTABLE, selectExecutableStr,
331 sizeof(selectExecutableStr)/sizeof(selectExecutableStr[0]));
332 LoadStringW (GetModuleHandleW(NULL), IDS_EXECUTABLE_FILTER, programsFilter,
333 sizeof(programsFilter)/sizeof(programsFilter[0]));
334 snprintfW( filter, MAX_PATH, filterW, programsFilter, 0, 0 );
336 ofn.lpstrTitle = selectExecutableStr;
337 ofn.lpstrFilter = filter;
338 ofn.lpstrFileTitle = filetitle;
339 ofn.lpstrFileTitle[0] = '\0';
340 ofn.nMaxFileTitle = sizeof(filetitle)/sizeof(filetitle[0]);
341 ofn.lpstrFile = file;
342 ofn.lpstrFile[0] = '\0';
343 ofn.nMaxFile = sizeof(file)/sizeof(file[0]);
345 if (GetOpenFileNameW (&ofn))
347 HWND listview = GetDlgItem(dialog, IDC_APP_LISTVIEW);
348 int count = SendMessageW(listview, LVM_GETITEMCOUNT, 0, 0);
349 WCHAR* new_app;
350 LVITEMW item;
352 if (list_contains_file(listview, filetitle))
353 return;
355 new_app = strdupW(filetitle);
357 WINE_TRACE("adding %s\n", wine_dbgstr_w (new_app));
359 add_listview_item(listview, new_app, new_app);
361 item.mask = LVIF_STATE;
362 item.state = LVIS_SELECTED | LVIS_FOCUSED;
363 item.stateMask = LVIS_SELECTED | LVIS_FOCUSED;
364 SendMessageW(listview, LVM_SETITEMSTATE, count, (LPARAM)&item );
366 SetFocus(listview);
368 else WINE_TRACE("user cancelled\n");
371 static void on_remove_app_click(HWND dialog)
373 HWND listview = GetDlgItem(dialog, IDC_APP_LISTVIEW);
374 int selection = get_listview_selection(listview);
375 char *section = keypath(""); /* AppDefaults\\whatever.exe\\ */
376 LVITEMW item;
378 item.iItem = selection;
379 item.iSubItem = 0;
380 item.mask = LVIF_PARAM;
382 WINE_TRACE("selection=%d, section=%s\n", selection, section);
384 assert( selection != 0 ); /* user cannot click this button when "default settings" is selected */
386 section[strlen(section)] = '\0'; /* remove last backslash */
387 set_reg_key(config_key, section, NULL, NULL); /* delete the section */
388 SendMessageW(listview, LVM_GETITEMW, 0, (LPARAM) &item);
389 HeapFree (GetProcessHeap(), 0, (void*)item.lParam);
390 SendMessageW(listview, LVM_DELETEITEM, selection, 0);
391 item.mask = LVIF_STATE;
392 item.state = LVIS_SELECTED | LVIS_FOCUSED;
393 item.stateMask = LVIS_SELECTED | LVIS_FOCUSED;
394 SendMessageW(listview, LVM_SETITEMSTATE, -1, (LPARAM)&item);
396 SetFocus(listview);
397 SendMessageW(GetParent(dialog), PSM_CHANGED, (WPARAM) dialog, 0);
400 static void on_winver_change(HWND dialog)
402 int selection = SendDlgItemMessageW(dialog, IDC_WINVER, CB_GETCURSEL, 0, 0);
404 if (current_app)
406 if (!selection)
408 WINE_TRACE("default selected so removing current setting\n");
409 set_reg_key(config_key, keypath(""), "Version", NULL);
411 else
413 WINE_TRACE("setting Version key to value '%s'\n", win_versions[selection-1].szVersion);
414 set_reg_key(config_key, keypath(""), "Version", win_versions[selection-1].szVersion);
417 else /* global version only */
419 static const char szKeyWindNT[] = "System\\CurrentControlSet\\Control\\Windows";
420 static const char szKeyEnvNT[] = "System\\CurrentControlSet\\Control\\Session Manager\\Environment";
421 char Buffer[40];
423 switch (win_versions[selection].dwPlatformId)
425 case VER_PLATFORM_WIN32_WINDOWS:
426 snprintf(Buffer, sizeof(Buffer), "%d.%d.%d", win_versions[selection].dwMajorVersion,
427 win_versions[selection].dwMinorVersion, win_versions[selection].dwBuildNumber);
428 set_reg_key(HKEY_LOCAL_MACHINE, szKey9x, "VersionNumber", Buffer);
429 set_reg_key(HKEY_LOCAL_MACHINE, szKey9x, "SubVersionNumber", win_versions[selection].szCSDVersion);
430 snprintf(Buffer, sizeof(Buffer), "Microsoft %s", win_versions[selection].szDescription);
431 set_reg_key(HKEY_LOCAL_MACHINE, szKey9x, "ProductName", Buffer);
433 set_reg_key(HKEY_LOCAL_MACHINE, szKeyNT, "CSDVersion", NULL);
434 set_reg_key(HKEY_LOCAL_MACHINE, szKeyNT, "CurrentVersion", NULL);
435 set_reg_key(HKEY_LOCAL_MACHINE, szKeyNT, "CurrentBuildNumber", NULL);
436 set_reg_key(HKEY_LOCAL_MACHINE, szKeyNT, "ProductName", NULL);
437 set_reg_key(HKEY_LOCAL_MACHINE, szKeyProdNT, "ProductType", NULL);
438 set_reg_key(HKEY_LOCAL_MACHINE, szKeyWindNT, "CSDVersion", NULL);
439 set_reg_key(HKEY_LOCAL_MACHINE, szKeyEnvNT, "OS", NULL);
440 set_reg_key(config_key, keypath(""), "Version", NULL);
441 break;
443 case VER_PLATFORM_WIN32_NT:
444 snprintf(Buffer, sizeof(Buffer), "%d.%d", win_versions[selection].dwMajorVersion,
445 win_versions[selection].dwMinorVersion);
446 set_reg_key(HKEY_LOCAL_MACHINE, szKeyNT, "CurrentVersion", Buffer);
447 set_reg_key(HKEY_LOCAL_MACHINE, szKeyNT, "CSDVersion", win_versions[selection].szCSDVersion);
448 snprintf(Buffer, sizeof(Buffer), "%d", win_versions[selection].dwBuildNumber);
449 set_reg_key(HKEY_LOCAL_MACHINE, szKeyNT, "CurrentBuildNumber", Buffer);
450 snprintf(Buffer, sizeof(Buffer), "Microsoft %s", win_versions[selection].szDescription);
451 set_reg_key(HKEY_LOCAL_MACHINE, szKeyNT, "ProductName", Buffer);
452 set_reg_key(HKEY_LOCAL_MACHINE, szKeyProdNT, "ProductType", win_versions[selection].szProductType);
453 set_reg_key_dword(HKEY_LOCAL_MACHINE, szKeyWindNT, "CSDVersion",
454 MAKEWORD( win_versions[selection].wServicePackMinor,
455 win_versions[selection].wServicePackMajor ));
456 set_reg_key(HKEY_LOCAL_MACHINE, szKeyEnvNT, "OS", "Windows_NT");
458 set_reg_key(HKEY_LOCAL_MACHINE, szKey9x, "VersionNumber", NULL);
459 set_reg_key(HKEY_LOCAL_MACHINE, szKey9x, "SubVersionNumber", NULL);
460 set_reg_key(HKEY_LOCAL_MACHINE, szKey9x, "ProductName", NULL);
461 set_reg_key(config_key, keypath(""), "Version", NULL);
462 break;
464 case VER_PLATFORM_WIN32s:
465 set_reg_key(HKEY_LOCAL_MACHINE, szKeyNT, "CSDVersion", NULL);
466 set_reg_key(HKEY_LOCAL_MACHINE, szKeyNT, "CurrentVersion", NULL);
467 set_reg_key(HKEY_LOCAL_MACHINE, szKeyNT, "CurrentBuildNumber", NULL);
468 set_reg_key(HKEY_LOCAL_MACHINE, szKeyNT, "ProductName", NULL);
469 set_reg_key(HKEY_LOCAL_MACHINE, szKeyProdNT, "ProductType", NULL);
470 set_reg_key(HKEY_LOCAL_MACHINE, szKeyWindNT, "CSDVersion", NULL);
471 set_reg_key(HKEY_LOCAL_MACHINE, szKeyEnvNT, "OS", NULL);
472 set_reg_key(HKEY_LOCAL_MACHINE, szKey9x, "VersionNumber", NULL);
473 set_reg_key(HKEY_LOCAL_MACHINE, szKey9x, "SubVersionNumber", NULL);
474 set_reg_key(HKEY_LOCAL_MACHINE, szKey9x, "ProductName", NULL);
475 set_reg_key(config_key, keypath(""), "Version", win_versions[selection].szVersion);
476 break;
480 /* enable the apply button */
481 SendMessageW(GetParent(dialog), PSM_CHANGED, (WPARAM) dialog, 0);
484 INT_PTR CALLBACK
485 AppDlgProc (HWND hDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
487 switch (uMsg)
489 case WM_INITDIALOG:
490 init_appsheet(hDlg);
491 break;
493 case WM_SHOWWINDOW:
494 set_window_title(hDlg);
495 break;
497 case WM_NOTIFY:
498 switch (((LPNMHDR)lParam)->code)
500 case LVN_ITEMCHANGED:
501 on_selection_change(hDlg, GetDlgItem(hDlg, IDC_APP_LISTVIEW));
502 break;
503 case PSN_APPLY:
504 apply();
505 SetWindowLongPtrW(hDlg, DWLP_MSGRESULT, PSNRET_NOERROR);
506 break;
509 break;
511 case WM_COMMAND:
512 switch(HIWORD(wParam))
514 case CBN_SELCHANGE:
515 switch(LOWORD(wParam))
517 case IDC_WINVER:
518 on_winver_change(hDlg);
519 break;
521 case BN_CLICKED:
522 switch(LOWORD(wParam))
524 case IDC_APP_ADDAPP:
525 on_add_app_click(hDlg);
526 break;
527 case IDC_APP_REMOVEAPP:
528 on_remove_app_click(hDlg);
529 break;
531 break;
534 break;
537 return 0;