jscript: Add the Romanian translation.
[wine/hramrach.git] / programs / winecfg / appdefaults.c
blobcf07fe8c63ef21308fce85259b32d11143a8213e
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 "winecfg.h"
33 #include "resource.h"
35 WINE_DEFAULT_DEBUG_CHANNEL(winecfg);
37 static const struct
39 const char *szVersion;
40 const char *szDescription;
41 DWORD dwMajorVersion;
42 DWORD dwMinorVersion;
43 DWORD dwBuildNumber;
44 DWORD dwPlatformId;
45 const char *szCSDVersion;
46 WORD wServicePackMajor;
47 WORD wServicePackMinor;
48 const char *szProductType;
49 } win_versions[] =
51 { "win7", "Windows 7", 6, 1, 0x1DB0,VER_PLATFORM_WIN32_NT, " ", 0, 0, "WinNT"},
52 { "win2008", "Windows 2008", 6, 0, 0x1771,VER_PLATFORM_WIN32_NT, "Service Pack 1", 0, 0, "ServerNT"},
53 { "vista", "Windows Vista", 6, 0, 0x1772,VER_PLATFORM_WIN32_NT, "Service Pack 2", 2, 0, "WinNT"},
54 { "win2003", "Windows 2003", 5, 2, 0xECE, VER_PLATFORM_WIN32_NT, "Service Pack 1", 1, 0, "ServerNT"},
55 { "winxp", "Windows XP", 5, 1, 0xA28, VER_PLATFORM_WIN32_NT, "Service Pack 3", 3, 0, "WinNT"},
56 { "win2k", "Windows 2000", 5, 0, 0x893, VER_PLATFORM_WIN32_NT, "Service Pack 4", 4, 0, "WinNT"},
57 { "winme", "Windows ME", 4, 90, 0xBB8, VER_PLATFORM_WIN32_WINDOWS, " ", 0, 0, ""},
58 { "win98", "Windows 98", 4, 10, 0x8AE, VER_PLATFORM_WIN32_WINDOWS, " A ", 0, 0, ""},
59 { "win95", "Windows 95", 4, 0, 0x3B6, VER_PLATFORM_WIN32_WINDOWS, "", 0, 0, ""},
60 { "nt40", "Windows NT 4.0", 4, 0, 0x565, VER_PLATFORM_WIN32_NT, "Service Pack 6a", 6, 0, "WinNT"},
61 { "nt351", "Windows NT 3.5", 3, 51, 0x421, VER_PLATFORM_WIN32_NT, "Service Pack 2", 0, 0, "WinNT"},
62 { "win31", "Windows 3.1", 2, 10, 0, VER_PLATFORM_WIN32s, "Win32s 1.3", 0, 0, ""},
63 { "win30", "Windows 3.0", 3, 0, 0, VER_PLATFORM_WIN32s, "Win32s 1.3", 0, 0, ""},
64 { "win20", "Windows 2.0", 2, 0, 0, VER_PLATFORM_WIN32s, "Win32s 1.3", 0, 0, ""}
67 #define NB_VERSIONS (sizeof(win_versions)/sizeof(win_versions[0]))
69 static const char szKey9x[] = "Software\\Microsoft\\Windows\\CurrentVersion";
70 static const char szKeyNT[] = "Software\\Microsoft\\Windows NT\\CurrentVersion";
72 static int get_registry_version(void)
74 int i, best = -1, platform, major, minor = 0, build = 0;
75 char *p, *ver;
77 if ((ver = get_reg_key( HKEY_LOCAL_MACHINE, szKeyNT, "CurrentVersion", NULL )))
79 char *build_str;
81 platform = VER_PLATFORM_WIN32_NT;
83 build_str = get_reg_key( HKEY_LOCAL_MACHINE, szKeyNT, "CurrentBuildNumber", NULL );
84 build = atoi(build_str);
86 else if ((ver = get_reg_key( HKEY_LOCAL_MACHINE, szKey9x, "VersionNumber", NULL )))
87 platform = VER_PLATFORM_WIN32_WINDOWS;
88 else
89 return -1;
91 if ((p = strchr( ver, '.' )))
93 char *minor_str = p;
94 *minor_str++ = 0;
95 if ((p = strchr( minor_str, '.' )))
97 char *build_str = p;
98 *build_str++ = 0;
99 build = atoi(build_str);
101 minor = atoi(minor_str);
103 major = atoi(ver);
105 for (i = 0; i < NB_VERSIONS; i++)
107 if (win_versions[i].dwPlatformId != platform) continue;
108 if (win_versions[i].dwMajorVersion != major) continue;
109 best = i;
110 if ((win_versions[i].dwMinorVersion == minor) &&
111 (win_versions[i].dwBuildNumber == build))
112 return i;
114 return best;
117 static void update_comboboxes(HWND dialog)
119 int i, ver;
120 char *winver;
122 /* retrieve the registry values */
123 winver = get_reg_key(config_key, keypath(""), "Version", "");
124 ver = get_registry_version();
126 if (!winver || !winver[0])
128 HeapFree(GetProcessHeap(), 0, winver);
130 if (current_app) /* no explicit setting */
132 WINE_TRACE("setting winver combobox to default\n");
133 SendDlgItemMessage (dialog, IDC_WINVER, CB_SETCURSEL, 0, 0);
134 return;
136 if (ver != -1) winver = strdupA( win_versions[ver].szVersion );
137 else winver = strdupA("winxp");
139 WINE_TRACE("winver is %s\n", winver);
141 /* normalize the version strings */
142 for (i = 0; i < NB_VERSIONS; i++)
144 if (!strcasecmp (win_versions[i].szVersion, winver))
146 SendDlgItemMessage (dialog, IDC_WINVER, CB_SETCURSEL,
147 i + (current_app?1:0), 0);
148 WINE_TRACE("match with %s\n", win_versions[i].szVersion);
149 break;
153 HeapFree(GetProcessHeap(), 0, winver);
156 static void
157 init_comboboxes (HWND dialog)
159 int i;
161 SendDlgItemMessage(dialog, IDC_WINVER, CB_RESETCONTENT, 0, 0);
163 /* add the default entries (automatic) which correspond to no setting */
164 if (current_app)
166 WCHAR str[256];
167 LoadStringW (GetModuleHandle (NULL), IDS_USE_GLOBAL_SETTINGS, str,
168 sizeof(str)/sizeof(str[0]));
169 SendDlgItemMessageW (dialog, IDC_WINVER, CB_ADDSTRING, 0, (LPARAM)str);
172 for (i = 0; i < NB_VERSIONS; i++)
174 SendDlgItemMessage (dialog, IDC_WINVER, CB_ADDSTRING,
175 0, (LPARAM) win_versions[i].szDescription);
179 static void add_listview_item(HWND listview, WCHAR *text, void *association)
181 LVITEMW item;
183 item.mask = LVIF_TEXT | LVIF_PARAM;
184 item.pszText = text;
185 item.cchTextMax = lstrlenW(text);
186 item.lParam = (LPARAM) association;
187 item.iItem = ListView_GetItemCount(listview);
188 item.iSubItem = 0;
190 SendMessage(listview, LVM_INSERTITEMW, 0, (LPARAM) &item);
193 /* Called when the application is initialized (cannot reinit!) */
194 static void init_appsheet(HWND dialog)
196 HWND listview;
197 HKEY key;
198 int i;
199 DWORD size;
200 WCHAR appname[1024];
202 WINE_TRACE("()\n");
204 listview = GetDlgItem(dialog, IDC_APP_LISTVIEW);
206 /* we use the lparam field of the item so we can alter the presentation later and not change code
207 * for instance, to use the tile view or to display the EXEs embedded 'display name' */
208 LoadStringW (GetModuleHandle (NULL), IDS_DEFAULT_SETTINGS, appname,
209 sizeof(appname)/sizeof(appname[0]));
210 add_listview_item(listview, appname, NULL);
212 /* because this list is only populated once, it's safe to bypass the settings list here */
213 if (RegOpenKey(config_key, "AppDefaults", &key) == ERROR_SUCCESS)
215 i = 0;
216 size = sizeof(appname)/sizeof(appname[0]);
217 while (RegEnumKeyExW (key, i, appname, &size, NULL, NULL, NULL, NULL) == ERROR_SUCCESS)
219 add_listview_item(listview, appname, strdupW(appname));
221 i++;
222 size = sizeof(appname)/sizeof(appname[0]);
225 RegCloseKey(key);
228 init_comboboxes(dialog);
230 /* Select the default settings listview item */
232 LVITEM item;
234 item.iItem = 0;
235 item.iSubItem = 0;
236 item.mask = LVIF_STATE;
237 item.state = LVIS_SELECTED | LVIS_FOCUSED;
238 item.stateMask = LVIS_SELECTED | LVIS_FOCUSED;
240 SendMessage(listview, LVM_SETITEM, 0, (LPARAM) &item);
245 /* there has to be an easier way than this */
246 static int get_listview_selection(HWND listview)
248 int count = ListView_GetItemCount(listview);
249 int i;
251 for (i = 0; i < count; i++)
253 if (ListView_GetItemState(listview, i, LVIS_SELECTED)) return i;
256 return -1;
260 /* called when the user selects a different application */
261 static void on_selection_change(HWND dialog, HWND listview)
263 LVITEM item;
264 WCHAR* oldapp = current_app;
266 WINE_TRACE("()\n");
268 item.iItem = get_listview_selection(listview);
269 item.iSubItem = 0;
270 item.mask = LVIF_PARAM;
272 WINE_TRACE("item.iItem=%d\n", item.iItem);
274 if (item.iItem == -1) return;
276 SendMessage(listview, LVM_GETITEM, 0, (LPARAM) &item);
278 current_app = (WCHAR*) item.lParam;
280 if (current_app)
282 WINE_TRACE("current_app is now %s\n", wine_dbgstr_w (current_app));
283 enable(IDC_APP_REMOVEAPP);
285 else
287 WINE_TRACE("current_app=NULL, editing global settings\n");
288 /* focus will never be on the button in this callback so it's safe */
289 disable(IDC_APP_REMOVEAPP);
292 /* reset the combo boxes if we changed from/to global/app-specific */
294 if ((oldapp && !current_app) || (!oldapp && current_app))
295 init_comboboxes(dialog);
297 update_comboboxes(dialog);
299 set_window_title(dialog);
302 static BOOL list_contains_file(HWND listview, WCHAR *filename)
304 LVFINDINFOW find_info = { LVFI_STRING, filename, 0, {0, 0}, 0 };
305 int index;
307 index = ListView_FindItemW(listview, -1, &find_info);
309 return (index != -1);
312 static void on_add_app_click(HWND dialog)
314 WCHAR filetitle[MAX_PATH];
315 WCHAR file[MAX_PATH];
316 WCHAR programsFilter[100];
317 WCHAR selectExecutableStr[100];
318 static const WCHAR pathC[] = { 'c',':','\\',0 };
320 OPENFILENAMEW ofn = { sizeof(OPENFILENAMEW),
321 0, /*hInst*/0, 0, NULL, 0, 0, NULL,
322 0, NULL, 0, pathC, 0,
323 OFN_SHOWHELP | OFN_HIDEREADONLY | OFN_ENABLESIZING,
324 0, 0, NULL, 0, NULL };
326 LoadStringW (GetModuleHandle (NULL), IDS_SELECT_EXECUTABLE, selectExecutableStr,
327 sizeof(selectExecutableStr)/sizeof(selectExecutableStr[0]));
328 LoadStringW (GetModuleHandle (NULL), IDS_EXECUTABLE_FILTER, programsFilter,
329 sizeof(programsFilter)/sizeof(programsFilter[0]));
331 ofn.lpstrTitle = selectExecutableStr;
332 ofn.lpstrFilter = programsFilter;
333 ofn.lpstrFileTitle = filetitle;
334 ofn.lpstrFileTitle[0] = '\0';
335 ofn.nMaxFileTitle = sizeof(filetitle)/sizeof(filetitle[0]);
336 ofn.lpstrFile = file;
337 ofn.lpstrFile[0] = '\0';
338 ofn.nMaxFile = sizeof(file)/sizeof(file[0]);
340 if (GetOpenFileNameW (&ofn))
342 HWND listview = GetDlgItem(dialog, IDC_APP_LISTVIEW);
343 int count = ListView_GetItemCount(listview);
344 WCHAR* new_app;
346 if (list_contains_file(listview, filetitle))
347 return;
349 new_app = strdupW(filetitle);
351 WINE_TRACE("adding %s\n", wine_dbgstr_w (new_app));
353 add_listview_item(listview, new_app, new_app);
355 ListView_SetItemState(listview, count, LVIS_SELECTED | LVIS_FOCUSED, LVIS_SELECTED | LVIS_FOCUSED);
357 SetFocus(listview);
359 else WINE_TRACE("user cancelled\n");
362 static void on_remove_app_click(HWND dialog)
364 HWND listview = GetDlgItem(dialog, IDC_APP_LISTVIEW);
365 int selection = get_listview_selection(listview);
366 char *section = keypath(""); /* AppDefaults\\whatever.exe\\ */
367 LVITEMW item;
369 item.iItem = selection;
370 item.iSubItem = 0;
371 item.mask = LVIF_PARAM;
373 WINE_TRACE("selection=%d, section=%s\n", selection, section);
375 assert( selection != 0 ); /* user cannot click this button when "default settings" is selected */
377 section[strlen(section)] = '\0'; /* remove last backslash */
378 set_reg_key(config_key, section, NULL, NULL); /* delete the section */
379 SendMessage(listview, LVM_GETITEMW, 0, (LPARAM) &item);
380 HeapFree (GetProcessHeap(), 0, (void*)item.lParam);
381 SendMessage(listview, LVM_DELETEITEM, selection, 0);
382 ListView_SetItemState(listview, selection - 1, LVIS_SELECTED | LVIS_FOCUSED, LVIS_SELECTED | LVIS_FOCUSED);
384 SetFocus(listview);
386 SendMessage(GetParent(dialog), PSM_CHANGED, (WPARAM) dialog, 0);
389 static void on_winver_change(HWND dialog)
391 int selection = SendDlgItemMessage(dialog, IDC_WINVER, CB_GETCURSEL, 0, 0);
393 if (current_app)
395 if (!selection)
397 WINE_TRACE("default selected so removing current setting\n");
398 set_reg_key(config_key, keypath(""), "Version", NULL);
400 else
402 WINE_TRACE("setting Version key to value '%s'\n", win_versions[selection-1].szVersion);
403 set_reg_key(config_key, keypath(""), "Version", win_versions[selection-1].szVersion);
406 else /* global version only */
408 static const char szKeyProdNT[] = "System\\CurrentControlSet\\Control\\ProductOptions";
409 static const char szKeyWindNT[] = "System\\CurrentControlSet\\Control\\Windows";
410 static const char szKeyEnvNT[] = "System\\CurrentControlSet\\Control\\Session Manager\\Environment";
411 char Buffer[40];
413 switch (win_versions[selection].dwPlatformId)
415 case VER_PLATFORM_WIN32_WINDOWS:
416 snprintf(Buffer, sizeof(Buffer), "%d.%d.%d", win_versions[selection].dwMajorVersion,
417 win_versions[selection].dwMinorVersion, win_versions[selection].dwBuildNumber);
418 set_reg_key(HKEY_LOCAL_MACHINE, szKey9x, "VersionNumber", Buffer);
419 set_reg_key(HKEY_LOCAL_MACHINE, szKey9x, "SubVersionNumber", win_versions[selection].szCSDVersion);
421 set_reg_key(HKEY_LOCAL_MACHINE, szKeyNT, "CSDVersion", NULL);
422 set_reg_key(HKEY_LOCAL_MACHINE, szKeyNT, "CurrentVersion", NULL);
423 set_reg_key(HKEY_LOCAL_MACHINE, szKeyNT, "CurrentBuildNumber", NULL);
424 set_reg_key(HKEY_LOCAL_MACHINE, szKeyProdNT, "ProductType", NULL);
425 set_reg_key(HKEY_LOCAL_MACHINE, szKeyWindNT, "CSDVersion", NULL);
426 set_reg_key(HKEY_LOCAL_MACHINE, szKeyEnvNT, "OS", NULL);
427 set_reg_key(config_key, keypath(""), "Version", NULL);
428 break;
430 case VER_PLATFORM_WIN32_NT:
431 snprintf(Buffer, sizeof(Buffer), "%d.%d", win_versions[selection].dwMajorVersion,
432 win_versions[selection].dwMinorVersion);
433 set_reg_key(HKEY_LOCAL_MACHINE, szKeyNT, "CurrentVersion", Buffer);
434 set_reg_key(HKEY_LOCAL_MACHINE, szKeyNT, "CSDVersion", win_versions[selection].szCSDVersion);
435 snprintf(Buffer, sizeof(Buffer), "%d", win_versions[selection].dwBuildNumber);
436 set_reg_key(HKEY_LOCAL_MACHINE, szKeyNT, "CurrentBuildNumber", Buffer);
437 set_reg_key(HKEY_LOCAL_MACHINE, szKeyProdNT, "ProductType", win_versions[selection].szProductType);
438 set_reg_key_dword(HKEY_LOCAL_MACHINE, szKeyWindNT, "CSDVersion",
439 MAKEWORD( win_versions[selection].wServicePackMinor,
440 win_versions[selection].wServicePackMajor ));
441 set_reg_key(HKEY_LOCAL_MACHINE, szKeyEnvNT, "OS", "Windows_NT");
443 set_reg_key(HKEY_LOCAL_MACHINE, szKey9x, "VersionNumber", NULL);
444 set_reg_key(HKEY_LOCAL_MACHINE, szKey9x, "SubVersionNumber", NULL);
445 set_reg_key(config_key, keypath(""), "Version", NULL);
446 break;
448 case VER_PLATFORM_WIN32s:
449 set_reg_key(HKEY_LOCAL_MACHINE, szKeyNT, "CSDVersion", NULL);
450 set_reg_key(HKEY_LOCAL_MACHINE, szKeyNT, "CurrentVersion", NULL);
451 set_reg_key(HKEY_LOCAL_MACHINE, szKeyNT, "CurrentBuildNumber", NULL);
452 set_reg_key(HKEY_LOCAL_MACHINE, szKeyProdNT, "ProductType", NULL);
453 set_reg_key(HKEY_LOCAL_MACHINE, szKeyWindNT, "CSDVersion", NULL);
454 set_reg_key(HKEY_LOCAL_MACHINE, szKeyEnvNT, "OS", NULL);
455 set_reg_key(HKEY_LOCAL_MACHINE, szKey9x, "VersionNumber", NULL);
456 set_reg_key(HKEY_LOCAL_MACHINE, szKey9x, "SubVersionNumber", NULL);
457 set_reg_key(config_key, keypath(""), "Version", win_versions[selection].szVersion);
458 break;
462 /* enable the apply button */
463 SendMessage(GetParent(dialog), PSM_CHANGED, (WPARAM) dialog, 0);
466 INT_PTR CALLBACK
467 AppDlgProc (HWND hDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
469 switch (uMsg)
471 case WM_INITDIALOG:
472 init_appsheet(hDlg);
473 break;
475 case WM_SHOWWINDOW:
476 set_window_title(hDlg);
477 break;
479 case WM_NOTIFY:
480 switch (((LPNMHDR)lParam)->code)
482 case LVN_ITEMCHANGED:
483 on_selection_change(hDlg, GetDlgItem(hDlg, IDC_APP_LISTVIEW));
484 break;
485 case PSN_APPLY:
486 apply();
487 SetWindowLongPtr(hDlg, DWLP_MSGRESULT, PSNRET_NOERROR);
488 break;
491 break;
493 case WM_COMMAND:
494 switch(HIWORD(wParam))
496 case CBN_SELCHANGE:
497 switch(LOWORD(wParam))
499 case IDC_WINVER:
500 on_winver_change(hDlg);
501 break;
503 case BN_CLICKED:
504 switch(LOWORD(wParam))
506 case IDC_APP_ADDAPP:
507 on_add_app_click(hDlg);
508 break;
509 case IDC_APP_REMOVEAPP:
510 on_remove_app_click(hDlg);
511 break;
513 break;
516 break;
519 return 0;