winecfg: Make the open file dialogs resizable.
[wine/multimedia.git] / programs / winecfg / appdefaults.c
blob8e48949d036f480366ba68ffbd40bf360fc9ab71
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 { "win2008", "Windows 2008", 6, 0, 0x1771,VER_PLATFORM_WIN32_NT, "Service Pack 1", 0, 0, "ServerNT"},
52 { "vista", "Windows Vista", 6, 0, 0x1770,VER_PLATFORM_WIN32_NT, " ", 0, 0, "WinNT"},
53 { "win2003", "Windows 2003", 5, 2, 0xECE, VER_PLATFORM_WIN32_NT, "Service Pack 1", 1, 0, "ServerNT"},
54 { "winxp", "Windows XP", 5, 1, 0xA28, VER_PLATFORM_WIN32_NT, "Service Pack 2", 2, 0, "WinNT"},
55 { "win2k", "Windows 2000", 5, 0, 0x893, VER_PLATFORM_WIN32_NT, "Service Pack 4", 4, 0, "WinNT"},
56 { "winme", "Windows ME", 4, 90, 0xBB8, VER_PLATFORM_WIN32_WINDOWS, " ", 0, 0, ""},
57 { "win98", "Windows 98", 4, 10, 0x8AE, VER_PLATFORM_WIN32_WINDOWS, " A ", 0, 0, ""},
58 { "win95", "Windows 95", 4, 0, 0x3B6, VER_PLATFORM_WIN32_WINDOWS, "", 0, 0, ""},
59 { "nt40", "Windows NT 4.0", 4, 0, 0x565, VER_PLATFORM_WIN32_NT, "Service Pack 6a", 6, 0, "WinNT"},
60 { "nt351", "Windows NT 3.5", 3, 51, 0x421, VER_PLATFORM_WIN32_NT, "Service Pack 2", 0, 0, "WinNT"},
61 { "win31", "Windows 3.1", 2, 10, 0, VER_PLATFORM_WIN32s, "Win32s 1.3", 0, 0, ""},
62 { "win30", "Windows 3.0", 3, 0, 0, VER_PLATFORM_WIN32s, "Win32s 1.3", 0, 0, ""},
63 { "win20", "Windows 2.0", 2, 0, 0, VER_PLATFORM_WIN32s, "Win32s 1.3", 0, 0, ""}
66 #define NB_VERSIONS (sizeof(win_versions)/sizeof(win_versions[0]))
68 static const char szKey9x[] = "Software\\Microsoft\\Windows\\CurrentVersion";
69 static const char szKeyNT[] = "Software\\Microsoft\\Windows NT\\CurrentVersion";
71 static int get_registry_version(void)
73 int i, best = -1, platform, major, minor = 0, build = 0;
74 char *p, *ver;
76 if ((ver = get_reg_key( HKEY_LOCAL_MACHINE, szKeyNT, "CurrentVersion", NULL )))
78 char *build_str;
80 platform = VER_PLATFORM_WIN32_NT;
82 build_str = get_reg_key( HKEY_LOCAL_MACHINE, szKeyNT, "CurrentBuildNumber", NULL );
83 build = atoi(build_str);
85 else if ((ver = get_reg_key( HKEY_LOCAL_MACHINE, szKey9x, "VersionNumber", NULL )))
86 platform = VER_PLATFORM_WIN32_WINDOWS;
87 else
88 return -1;
90 if ((p = strchr( ver, '.' )))
92 char *minor_str = p;
93 *minor_str++ = 0;
94 if ((p = strchr( minor_str, '.' )))
96 char *build_str = p;
97 *build_str++ = 0;
98 build = atoi(build_str);
100 minor = atoi(minor_str);
102 major = atoi(ver);
104 for (i = 0; i < NB_VERSIONS; i++)
106 if (win_versions[i].dwPlatformId != platform) continue;
107 if (win_versions[i].dwMajorVersion != major) continue;
108 best = i;
109 if ((win_versions[i].dwMinorVersion == minor) &&
110 (win_versions[i].dwBuildNumber == build))
111 return i;
113 return best;
116 static void update_comboboxes(HWND dialog)
118 int i, ver;
119 char *winver;
121 /* retrieve the registry values */
122 winver = get_reg_key(config_key, keypath(""), "Version", "");
123 ver = get_registry_version();
125 if (!winver || !winver[0])
127 HeapFree(GetProcessHeap(), 0, winver);
129 if (current_app) /* no explicit setting */
131 WINE_TRACE("setting winver combobox to default\n");
132 SendDlgItemMessage (dialog, IDC_WINVER, CB_SETCURSEL, 0, 0);
133 return;
135 if (ver != -1) winver = strdupA( win_versions[ver].szVersion );
136 else winver = strdupA("winxp");
138 WINE_TRACE("winver is %s\n", winver);
140 /* normalize the version strings */
141 for (i = 0; i < NB_VERSIONS; i++)
143 if (!strcasecmp (win_versions[i].szVersion, winver))
145 SendDlgItemMessage (dialog, IDC_WINVER, CB_SETCURSEL,
146 (WPARAM) i + (current_app?1:0), 0);
147 WINE_TRACE("match with %s\n", win_versions[i].szVersion);
148 break;
152 HeapFree(GetProcessHeap(), 0, winver);
155 static void
156 init_comboboxes (HWND dialog)
158 int i;
160 SendDlgItemMessage(dialog, IDC_WINVER, CB_RESETCONTENT, 0, 0);
162 /* add the default entries (automatic) which correspond to no setting */
163 if (current_app)
165 WCHAR str[256];
166 LoadStringW (GetModuleHandle (NULL), IDS_USE_GLOBAL_SETTINGS, str,
167 sizeof(str)/sizeof(str[0]));
168 SendDlgItemMessageW (dialog, IDC_WINVER, CB_ADDSTRING, 0, (LPARAM)str);
171 for (i = 0; i < NB_VERSIONS; i++)
173 SendDlgItemMessage (dialog, IDC_WINVER, CB_ADDSTRING,
174 0, (LPARAM) win_versions[i].szDescription);
178 static void add_listview_item(HWND listview, WCHAR *text, void *association)
180 LVITEMW item;
182 item.mask = LVIF_TEXT | LVIF_PARAM;
183 item.pszText = text;
184 item.cchTextMax = lstrlenW(text);
185 item.lParam = (LPARAM) association;
186 item.iItem = ListView_GetItemCount(listview);
187 item.iSubItem = 0;
189 SendMessage(listview, LVM_INSERTITEMW, 0, (LPARAM) &item);
192 /* Called when the application is initialized (cannot reinit!) */
193 static void init_appsheet(HWND dialog)
195 HWND listview;
196 HKEY key;
197 int i;
198 DWORD size;
199 WCHAR appname[1024];
201 WINE_TRACE("()\n");
203 listview = GetDlgItem(dialog, IDC_APP_LISTVIEW);
205 /* we use the lparam field of the item so we can alter the presentation later and not change code
206 * for instance, to use the tile view or to display the EXEs embedded 'display name' */
207 LoadStringW (GetModuleHandle (NULL), IDS_DEFAULT_SETTINGS, appname,
208 sizeof(appname)/sizeof(appname[0]));
209 add_listview_item(listview, appname, NULL);
211 /* because this list is only populated once, it's safe to bypass the settings list here */
212 if (RegOpenKey(config_key, "AppDefaults", &key) == ERROR_SUCCESS)
214 i = 0;
215 size = sizeof(appname)/sizeof(appname[0]);
216 while (RegEnumKeyExW (key, i, appname, &size, NULL, NULL, NULL, NULL) == ERROR_SUCCESS)
218 add_listview_item(listview, appname, strdupW(appname));
220 i++;
221 size = sizeof(appname)/sizeof(appname[0]);
224 RegCloseKey(key);
227 init_comboboxes(dialog);
229 /* Select the default settings listview item */
231 LVITEM item;
233 item.iItem = 0;
234 item.iSubItem = 0;
235 item.mask = LVIF_STATE;
236 item.state = LVIS_SELECTED | LVIS_FOCUSED;
237 item.stateMask = LVIS_SELECTED | LVIS_FOCUSED;
239 SendMessage(listview, LVM_SETITEM, 0, (LPARAM) &item);
244 /* there has to be an easier way than this */
245 static int get_listview_selection(HWND listview)
247 int count = ListView_GetItemCount(listview);
248 int i;
250 for (i = 0; i < count; i++)
252 if (ListView_GetItemState(listview, i, LVIS_SELECTED)) return i;
255 return -1;
259 /* called when the user selects a different application */
260 static void on_selection_change(HWND dialog, HWND listview)
262 LVITEM item;
263 WCHAR* oldapp = current_app;
265 WINE_TRACE("()\n");
267 item.iItem = get_listview_selection(listview);
268 item.iSubItem = 0;
269 item.mask = LVIF_PARAM;
271 WINE_TRACE("item.iItem=%d\n", item.iItem);
273 if (item.iItem == -1) return;
275 SendMessage(listview, LVM_GETITEM, 0, (LPARAM) &item);
277 current_app = (WCHAR*) item.lParam;
279 if (current_app)
281 WINE_TRACE("current_app is now %s\n", wine_dbgstr_w (current_app));
282 enable(IDC_APP_REMOVEAPP);
284 else
286 WINE_TRACE("current_app=NULL, editing global settings\n");
287 /* focus will never be on the button in this callback so it's safe */
288 disable(IDC_APP_REMOVEAPP);
291 /* reset the combo boxes if we changed from/to global/app-specific */
293 if ((oldapp && !current_app) || (!oldapp && current_app))
294 init_comboboxes(dialog);
296 update_comboboxes(dialog);
298 set_window_title(dialog);
301 static BOOL list_contains_file(HWND listview, WCHAR *filename)
303 LVFINDINFOW find_info = { LVFI_STRING, filename, 0, {0, 0}, 0 };
304 int index;
306 index = ListView_FindItemW(listview, -1, &find_info);
308 return (index != -1);
311 static void on_add_app_click(HWND dialog)
313 WCHAR filetitle[MAX_PATH];
314 WCHAR file[MAX_PATH];
315 WCHAR programsFilter[100];
316 WCHAR selectExecutableStr[100];
317 static const WCHAR pathC[] = { 'c',':','\\',0 };
319 OPENFILENAMEW ofn = { sizeof(OPENFILENAMEW),
320 0, /*hInst*/0, 0, NULL, 0, 0, NULL,
321 0, NULL, 0, pathC, 0,
322 OFN_SHOWHELP | OFN_HIDEREADONLY | OFN_ENABLESIZING,
323 0, 0, NULL, 0, NULL };
325 LoadStringW (GetModuleHandle (NULL), IDS_SELECT_EXECUTABLE, selectExecutableStr,
326 sizeof(selectExecutableStr)/sizeof(selectExecutableStr[0]));
327 LoadStringW (GetModuleHandle (NULL), IDS_EXECUTABLE_FILTER, programsFilter,
328 sizeof(programsFilter)/sizeof(programsFilter[0]));
330 ofn.lpstrTitle = selectExecutableStr;
331 ofn.lpstrFilter = programsFilter;
332 ofn.lpstrFileTitle = filetitle;
333 ofn.lpstrFileTitle[0] = '\0';
334 ofn.nMaxFileTitle = sizeof(filetitle)/sizeof(filetitle[0]);
335 ofn.lpstrFile = file;
336 ofn.lpstrFile[0] = '\0';
337 ofn.nMaxFile = sizeof(file)/sizeof(file[0]);
339 if (GetOpenFileNameW (&ofn))
341 HWND listview = GetDlgItem(dialog, IDC_APP_LISTVIEW);
342 int count = ListView_GetItemCount(listview);
343 WCHAR* new_app;
345 if (list_contains_file(listview, filetitle))
346 return;
348 new_app = strdupW(filetitle);
350 WINE_TRACE("adding %s\n", wine_dbgstr_w (new_app));
352 add_listview_item(listview, new_app, new_app);
354 ListView_SetItemState(listview, count, LVIS_SELECTED | LVIS_FOCUSED, LVIS_SELECTED | LVIS_FOCUSED);
356 SetFocus(listview);
358 else WINE_TRACE("user cancelled\n");
361 static void on_remove_app_click(HWND dialog)
363 HWND listview = GetDlgItem(dialog, IDC_APP_LISTVIEW);
364 int selection = get_listview_selection(listview);
365 char *section = keypath(""); /* AppDefaults\\whatever.exe\\ */
366 LVITEMW item;
368 item.iItem = selection;
369 item.iSubItem = 0;
370 item.mask = LVIF_PARAM;
372 WINE_TRACE("selection=%d, section=%s\n", selection, section);
374 assert( selection != 0 ); /* user cannot click this button when "default settings" is selected */
376 section[strlen(section)] = '\0'; /* remove last backslash */
377 set_reg_key(config_key, section, NULL, NULL); /* delete the section */
378 SendMessage(listview, LVM_GETITEMW, 0, (LPARAM) &item);
379 HeapFree (GetProcessHeap(), 0, (void*)item.lParam);
380 SendMessage(listview, LVM_DELETEITEM, selection, 0);
381 ListView_SetItemState(listview, selection - 1, LVIS_SELECTED | LVIS_FOCUSED, LVIS_SELECTED | LVIS_FOCUSED);
383 SetFocus(listview);
385 SendMessage(GetParent(dialog), PSM_CHANGED, (WPARAM) dialog, 0);
388 static void on_winver_change(HWND dialog)
390 int selection = SendDlgItemMessage(dialog, IDC_WINVER, CB_GETCURSEL, 0, 0);
392 if (current_app)
394 if (!selection)
396 WINE_TRACE("default selected so removing current setting\n");
397 set_reg_key(config_key, keypath(""), "Version", NULL);
399 else
401 WINE_TRACE("setting Version key to value '%s'\n", win_versions[selection-1].szVersion);
402 set_reg_key(config_key, keypath(""), "Version", win_versions[selection-1].szVersion);
405 else /* global version only */
407 static const char szKeyProdNT[] = "System\\CurrentControlSet\\Control\\ProductOptions";
408 static const char szKeyWindNT[] = "System\\CurrentControlSet\\Control\\Windows";
409 static const char szKeyEnvNT[] = "System\\CurrentControlSet\\Control\\Session Manager\\Environment";
410 char Buffer[40];
412 switch (win_versions[selection].dwPlatformId)
414 case VER_PLATFORM_WIN32_WINDOWS:
415 snprintf(Buffer, sizeof(Buffer), "%d.%d.%d", win_versions[selection].dwMajorVersion,
416 win_versions[selection].dwMinorVersion, win_versions[selection].dwBuildNumber);
417 set_reg_key(HKEY_LOCAL_MACHINE, szKey9x, "VersionNumber", Buffer);
418 set_reg_key(HKEY_LOCAL_MACHINE, szKey9x, "SubVersionNumber", win_versions[selection].szCSDVersion);
420 set_reg_key(HKEY_LOCAL_MACHINE, szKeyNT, "CSDVersion", NULL);
421 set_reg_key(HKEY_LOCAL_MACHINE, szKeyNT, "CurrentVersion", NULL);
422 set_reg_key(HKEY_LOCAL_MACHINE, szKeyNT, "CurrentBuildNumber", NULL);
423 set_reg_key(HKEY_LOCAL_MACHINE, szKeyProdNT, "ProductType", NULL);
424 set_reg_key(HKEY_LOCAL_MACHINE, szKeyWindNT, "CSDVersion", NULL);
425 set_reg_key(HKEY_LOCAL_MACHINE, szKeyEnvNT, "OS", NULL);
426 set_reg_key(config_key, keypath(""), "Version", NULL);
427 break;
429 case VER_PLATFORM_WIN32_NT:
430 snprintf(Buffer, sizeof(Buffer), "%d.%d", win_versions[selection].dwMajorVersion,
431 win_versions[selection].dwMinorVersion);
432 set_reg_key(HKEY_LOCAL_MACHINE, szKeyNT, "CurrentVersion", Buffer);
433 set_reg_key(HKEY_LOCAL_MACHINE, szKeyNT, "CSDVersion", win_versions[selection].szCSDVersion);
434 snprintf(Buffer, sizeof(Buffer), "%d", win_versions[selection].dwBuildNumber);
435 set_reg_key(HKEY_LOCAL_MACHINE, szKeyNT, "CurrentBuildNumber", Buffer);
436 set_reg_key(HKEY_LOCAL_MACHINE, szKeyProdNT, "ProductType", win_versions[selection].szProductType);
437 set_reg_key_dword(HKEY_LOCAL_MACHINE, szKeyWindNT, "CSDVersion",
438 MAKEWORD( win_versions[selection].wServicePackMinor,
439 win_versions[selection].wServicePackMajor ));
440 set_reg_key(HKEY_LOCAL_MACHINE, szKeyEnvNT, "OS", "Windows_NT");
442 set_reg_key(HKEY_LOCAL_MACHINE, szKey9x, "VersionNumber", NULL);
443 set_reg_key(HKEY_LOCAL_MACHINE, szKey9x, "SubVersionNumber", NULL);
444 set_reg_key(config_key, keypath(""), "Version", NULL);
445 break;
447 case VER_PLATFORM_WIN32s:
448 set_reg_key(HKEY_LOCAL_MACHINE, szKeyNT, "CSDVersion", NULL);
449 set_reg_key(HKEY_LOCAL_MACHINE, szKeyNT, "CurrentVersion", NULL);
450 set_reg_key(HKEY_LOCAL_MACHINE, szKeyNT, "CurrentBuildNumber", NULL);
451 set_reg_key(HKEY_LOCAL_MACHINE, szKeyProdNT, "ProductType", NULL);
452 set_reg_key(HKEY_LOCAL_MACHINE, szKeyWindNT, "CSDVersion", NULL);
453 set_reg_key(HKEY_LOCAL_MACHINE, szKeyEnvNT, "OS", NULL);
454 set_reg_key(HKEY_LOCAL_MACHINE, szKey9x, "VersionNumber", NULL);
455 set_reg_key(HKEY_LOCAL_MACHINE, szKey9x, "SubVersionNumber", NULL);
456 set_reg_key(config_key, keypath(""), "Version", win_versions[selection].szVersion);
457 break;
461 /* enable the apply button */
462 SendMessage(GetParent(dialog), PSM_CHANGED, (WPARAM) dialog, 0);
465 INT_PTR CALLBACK
466 AppDlgProc (HWND hDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
468 switch (uMsg)
470 case WM_INITDIALOG:
471 init_appsheet(hDlg);
472 break;
474 case WM_SHOWWINDOW:
475 set_window_title(hDlg);
476 break;
478 case WM_NOTIFY:
479 switch (((LPNMHDR)lParam)->code)
481 case LVN_ITEMCHANGED:
482 on_selection_change(hDlg, GetDlgItem(hDlg, IDC_APP_LISTVIEW));
483 break;
484 case PSN_APPLY:
485 apply();
486 SetWindowLongPtr(hDlg, DWLP_MSGRESULT, PSNRET_NOERROR);
487 break;
490 break;
492 case WM_COMMAND:
493 switch(HIWORD(wParam))
495 case CBN_SELCHANGE:
496 switch(LOWORD(wParam))
498 case IDC_WINVER:
499 on_winver_change(hDlg);
500 break;
502 case BN_CLICKED:
503 switch(LOWORD(wParam))
505 case IDC_APP_ADDAPP:
506 on_add_app_click(hDlg);
507 break;
508 case IDC_APP_REMOVEAPP:
509 on_remove_app_click(hDlg);
510 break;
512 break;
515 break;
518 return 0;