Release 1.4.1.
[wine] / programs / winecfg / appdefaults.c
1 /*
2  * WineCfg app settings tabsheet
3  *
4  * Copyright 2004 Robert van Herk
5  * Copyright 2004 Chris Morgan
6  * Copyright 2004 Mike Hearn
7  *
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.
12  *
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.
17  *
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
21  *
22  */
23
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"
35
36 WINE_DEFAULT_DEBUG_CHANNEL(winecfg);
37
38 static const struct
39 {
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[] =
51 {
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", 2, 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.51",   3, 51, 0x421, VER_PLATFORM_WIN32_NT, "Service Pack 5", 5, 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, ""}
67 };
68
69 #define NB_VERSIONS (sizeof(win_versions)/sizeof(win_versions[0]))
70
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";
74
75 static int get_registry_version(void)
76 {
77     int i, best = -1, platform, major, minor = 0, build = 0;
78     char *p, *ver, *type = NULL;
79
80     if ((ver = get_reg_key( HKEY_LOCAL_MACHINE, szKeyNT, "CurrentVersion", NULL )))
81     {
82         char *build_str;
83
84         platform = VER_PLATFORM_WIN32_NT;
85
86         build_str = get_reg_key( HKEY_LOCAL_MACHINE, szKeyNT, "CurrentBuildNumber", NULL );
87         build = atoi(build_str);
88
89         type = get_reg_key( HKEY_LOCAL_MACHINE, szKeyProdNT, "ProductType", NULL );
90     }
91     else if ((ver = get_reg_key( HKEY_LOCAL_MACHINE, szKey9x, "VersionNumber", NULL )))
92         platform = VER_PLATFORM_WIN32_WINDOWS;
93     else
94         return -1;
95
96     if ((p = strchr( ver, '.' )))
97     {
98         char *minor_str = p;
99         *minor_str++ = 0;
100         if ((p = strchr( minor_str, '.' )))
101         {
102             char *build_str = p;
103             *build_str++ = 0;
104             build = atoi(build_str);
105         }
106         minor = atoi(minor_str);
107     }
108     major = atoi(ver);
109
110     for (i = 0; i < NB_VERSIONS; i++)
111     {
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;
115         best = i;
116         if ((win_versions[i].dwMinorVersion == minor) &&
117             (win_versions[i].dwBuildNumber == build))
118             return i;
119     }
120     return best;
121 }
122
123 static void update_comboboxes(HWND dialog)
124 {
125     int i, ver;
126     char *winver;
127
128     /* retrieve the registry values */
129     winver = get_reg_key(config_key, keypath(""), "Version", "");
130     ver = get_registry_version();
131
132     if (!winver || !winver[0])
133     {
134         HeapFree(GetProcessHeap(), 0, winver);
135
136         if (current_app) /* no explicit setting */
137         {
138             WINE_TRACE("setting winver combobox to default\n");
139             SendDlgItemMessageW(dialog, IDC_WINVER, CB_SETCURSEL, 0, 0);
140             return;
141         }
142         if (ver != -1) winver = strdupA( win_versions[ver].szVersion );
143         else winver = strdupA("winxp");
144     }
145     WINE_TRACE("winver is %s\n", winver);
146
147     /* normalize the version strings */
148     for (i = 0; i < NB_VERSIONS; i++)
149     {
150         if (!strcasecmp (win_versions[i].szVersion, winver))
151         {
152             SendDlgItemMessageW(dialog, IDC_WINVER, CB_SETCURSEL,
153                                 i + (current_app?1:0), 0);
154             WINE_TRACE("match with %s\n", win_versions[i].szVersion);
155             break;
156         }
157     }
158
159     HeapFree(GetProcessHeap(), 0, winver);
160 }
161
162 static void
163 init_comboboxes (HWND dialog)
164 {
165     int i;
166
167     SendDlgItemMessageW(dialog, IDC_WINVER, CB_RESETCONTENT, 0, 0);
168
169     /* add the default entries (automatic) which correspond to no setting  */
170     if (current_app)
171     {
172         WCHAR str[256];
173         LoadStringW (GetModuleHandleW(NULL), IDS_USE_GLOBAL_SETTINGS, str,
174             sizeof(str)/sizeof(str[0]));
175         SendDlgItemMessageW (dialog, IDC_WINVER, CB_ADDSTRING, 0, (LPARAM)str);
176     }
177
178     for (i = 0; i < NB_VERSIONS; i++)
179     {
180       SendDlgItemMessageA(dialog, IDC_WINVER, CB_ADDSTRING,
181                           0, (LPARAM) win_versions[i].szDescription);
182     }
183 }
184
185 static void add_listview_item(HWND listview, WCHAR *text, void *association)
186 {
187   LVITEMW item;
188
189   item.mask = LVIF_TEXT | LVIF_PARAM;
190   item.pszText = text;
191   item.cchTextMax = lstrlenW(text);
192   item.lParam = (LPARAM) association;
193   item.iItem = SendMessageW( listview, LVM_GETITEMCOUNT, 0, 0 );
194   item.iSubItem = 0;
195
196   SendMessageW(listview, LVM_INSERTITEMW, 0, (LPARAM) &item);
197 }
198
199 /* Called when the application is initialized (cannot reinit!)  */
200 static void init_appsheet(HWND dialog)
201 {
202   HWND listview;
203   LVITEMW item;
204   HKEY key;
205   int i;
206   DWORD size;
207   WCHAR appname[1024];
208
209   WINE_TRACE("()\n");
210
211   listview = GetDlgItem(dialog, IDC_APP_LISTVIEW);
212
213   /* we use the lparam field of the item so we can alter the presentation later and not change code
214    * for instance, to use the tile view or to display the EXEs embedded 'display name' */
215   LoadStringW (GetModuleHandleW(NULL), IDS_DEFAULT_SETTINGS, appname,
216       sizeof(appname)/sizeof(appname[0]));
217   add_listview_item(listview, appname, NULL);
218
219   /* because this list is only populated once, it's safe to bypass the settings list here  */
220   if (RegOpenKeyA(config_key, "AppDefaults", &key) == ERROR_SUCCESS)
221   {
222       i = 0;
223       size = sizeof(appname)/sizeof(appname[0]);
224       while (RegEnumKeyExW (key, i, appname, &size, NULL, NULL, NULL, NULL) == ERROR_SUCCESS)
225       {
226           add_listview_item(listview, appname, strdupW(appname));
227
228           i++;
229           size = sizeof(appname)/sizeof(appname[0]);
230       }
231
232       RegCloseKey(key);
233   }
234
235   init_comboboxes(dialog);
236   
237   /* Select the default settings listview item  */
238   item.iItem = 0;
239   item.iSubItem = 0;
240   item.mask = LVIF_STATE;
241   item.state = LVIS_SELECTED | LVIS_FOCUSED;
242   item.stateMask = LVIS_SELECTED | LVIS_FOCUSED;
243
244   SendMessageW(listview, LVM_SETITEMW, 0, (LPARAM) &item);
245 }
246
247 /* there has to be an easier way than this  */
248 static int get_listview_selection(HWND listview)
249 {
250   int count = SendMessageW(listview, LVM_GETITEMCOUNT, 0, 0);
251   int i;
252
253   for (i = 0; i < count; i++)
254   {
255       if (SendMessageW( listview, LVM_GETITEMSTATE, i, LVIS_SELECTED )) return i;
256   }
257
258   return -1;
259 }
260
261
262 /* called when the user selects a different application */
263 static void on_selection_change(HWND dialog, HWND listview)
264 {
265   LVITEMW item;
266   WCHAR* oldapp = current_app;
267
268   WINE_TRACE("()\n");
269
270   item.iItem = get_listview_selection(listview);
271   item.iSubItem = 0;
272   item.mask = LVIF_PARAM;
273
274   WINE_TRACE("item.iItem=%d\n", item.iItem);
275   
276   if (item.iItem == -1) return;
277
278   SendMessageW(listview, LVM_GETITEMW, 0, (LPARAM) &item);
279
280   current_app = (WCHAR*) item.lParam;
281
282   if (current_app)
283   {
284       WINE_TRACE("current_app is now %s\n", wine_dbgstr_w (current_app));
285       enable(IDC_APP_REMOVEAPP);
286   }
287   else
288   {
289       WINE_TRACE("current_app=NULL, editing global settings\n");
290       /* focus will never be on the button in this callback so it's safe  */
291       disable(IDC_APP_REMOVEAPP);
292   }
293
294   /* reset the combo boxes if we changed from/to global/app-specific  */
295
296   if ((oldapp && !current_app) || (!oldapp && current_app))
297       init_comboboxes(dialog);
298   
299   update_comboboxes(dialog);
300
301   set_window_title(dialog);
302 }
303
304 static BOOL list_contains_file(HWND listview, WCHAR *filename)
305 {
306   LVFINDINFOW find_info = { LVFI_STRING, filename, 0, {0, 0}, 0 };
307   int index;
308
309   index = ListView_FindItemW(listview, -1, &find_info);
310
311   return (index != -1);
312 }
313
314 static void on_add_app_click(HWND dialog)
315 {
316   static const WCHAR filterW[] = {'%','s','%','c','*','.','e','x','e',';','*','.','e','x','e','.','s','o','%','c',0};
317   WCHAR filetitle[MAX_PATH];
318   WCHAR file[MAX_PATH];
319   WCHAR programsFilter[100], filter[MAX_PATH];
320   WCHAR selectExecutableStr[100];
321   static const WCHAR pathC[] = { 'c',':','\\',0 };
322
323   OPENFILENAMEW ofn = { sizeof(OPENFILENAMEW),
324                        0, /*hInst*/0, 0, NULL, 0, 0, NULL,
325                        0, NULL, 0, pathC, 0,
326                        OFN_SHOWHELP | OFN_HIDEREADONLY | OFN_ENABLESIZING,
327                        0, 0, NULL, 0, NULL };
328
329   LoadStringW (GetModuleHandleW(NULL), IDS_SELECT_EXECUTABLE, selectExecutableStr,
330       sizeof(selectExecutableStr)/sizeof(selectExecutableStr[0]));
331   LoadStringW (GetModuleHandleW(NULL), IDS_EXECUTABLE_FILTER, programsFilter,
332       sizeof(programsFilter)/sizeof(programsFilter[0]));
333   snprintfW( filter, MAX_PATH, filterW, programsFilter, 0, 0 );
334
335   ofn.lpstrTitle = selectExecutableStr;
336   ofn.lpstrFilter = filter;
337   ofn.lpstrFileTitle = filetitle;
338   ofn.lpstrFileTitle[0] = '\0';
339   ofn.nMaxFileTitle = sizeof(filetitle)/sizeof(filetitle[0]);
340   ofn.lpstrFile = file;
341   ofn.lpstrFile[0] = '\0';
342   ofn.nMaxFile = sizeof(file)/sizeof(file[0]);
343
344   if (GetOpenFileNameW (&ofn))
345   {
346       HWND listview = GetDlgItem(dialog, IDC_APP_LISTVIEW);
347       int count = SendMessageW(listview, LVM_GETITEMCOUNT, 0, 0);
348       WCHAR* new_app;
349       LVITEMW item;
350
351       if (list_contains_file(listview, filetitle))
352           return;
353
354       new_app = strdupW(filetitle);
355
356       WINE_TRACE("adding %s\n", wine_dbgstr_w (new_app));
357
358       add_listview_item(listview, new_app, new_app);
359
360       item.mask = LVIF_STATE;
361       item.state = LVIS_SELECTED | LVIS_FOCUSED;
362       item.stateMask = LVIS_SELECTED | LVIS_FOCUSED;
363       SendMessageW(listview, LVM_SETITEMSTATE, count, (LPARAM)&item );
364
365       SetFocus(listview);
366   }
367   else WINE_TRACE("user cancelled\n");
368 }
369
370 static void on_remove_app_click(HWND dialog)
371 {
372     HWND listview = GetDlgItem(dialog, IDC_APP_LISTVIEW);
373     int selection = get_listview_selection(listview);
374     char *section = keypath(""); /* AppDefaults\\whatever.exe\\ */
375     LVITEMW item;
376
377     item.iItem = selection;
378     item.iSubItem = 0;
379     item.mask = LVIF_PARAM;
380
381     WINE_TRACE("selection=%d, section=%s\n", selection, section);
382     
383     assert( selection != 0 ); /* user cannot click this button when "default settings" is selected  */
384
385     section[strlen(section)] = '\0'; /* remove last backslash  */
386     set_reg_key(config_key, section, NULL, NULL); /* delete the section  */
387     SendMessageW(listview, LVM_GETITEMW, 0, (LPARAM) &item);
388     HeapFree (GetProcessHeap(), 0, (void*)item.lParam);
389     SendMessageW(listview, LVM_DELETEITEM, selection, 0);
390     item.mask = LVIF_STATE;
391     item.state = LVIS_SELECTED | LVIS_FOCUSED;
392     item.stateMask = LVIS_SELECTED | LVIS_FOCUSED;
393     SendMessageW(listview, LVM_SETITEMSTATE, -1, (LPARAM)&item);
394
395     SetFocus(listview);
396     SendMessageW(GetParent(dialog), PSM_CHANGED, (WPARAM) dialog, 0);
397 }
398
399 static void on_winver_change(HWND dialog)
400 {
401     int selection = SendDlgItemMessageW(dialog, IDC_WINVER, CB_GETCURSEL, 0, 0);
402
403     if (current_app)
404     {
405         if (!selection)
406         {
407             WINE_TRACE("default selected so removing current setting\n");
408             set_reg_key(config_key, keypath(""), "Version", NULL);
409         }
410         else
411         {
412             WINE_TRACE("setting Version key to value '%s'\n", win_versions[selection-1].szVersion);
413             set_reg_key(config_key, keypath(""), "Version", win_versions[selection-1].szVersion);
414         }
415     }
416     else /* global version only */
417     {
418         static const char szKeyWindNT[] = "System\\CurrentControlSet\\Control\\Windows";
419         static const char szKeyEnvNT[]  = "System\\CurrentControlSet\\Control\\Session Manager\\Environment";
420         char Buffer[40];
421
422         switch (win_versions[selection].dwPlatformId)
423         {
424         case VER_PLATFORM_WIN32_WINDOWS:
425             snprintf(Buffer, sizeof(Buffer), "%d.%d.%d", win_versions[selection].dwMajorVersion,
426                      win_versions[selection].dwMinorVersion, win_versions[selection].dwBuildNumber);
427             set_reg_key(HKEY_LOCAL_MACHINE, szKey9x, "VersionNumber", Buffer);
428             set_reg_key(HKEY_LOCAL_MACHINE, szKey9x, "SubVersionNumber", win_versions[selection].szCSDVersion);
429             snprintf(Buffer, sizeof(Buffer), "Microsoft %s", win_versions[selection].szDescription);
430             set_reg_key(HKEY_LOCAL_MACHINE, szKey9x, "ProductName", Buffer);
431
432             set_reg_key(HKEY_LOCAL_MACHINE, szKeyNT, "CSDVersion", NULL);
433             set_reg_key(HKEY_LOCAL_MACHINE, szKeyNT, "CurrentVersion", NULL);
434             set_reg_key(HKEY_LOCAL_MACHINE, szKeyNT, "CurrentBuildNumber", NULL);
435             set_reg_key(HKEY_LOCAL_MACHINE, szKeyNT, "ProductName", NULL);
436             set_reg_key(HKEY_LOCAL_MACHINE, szKeyProdNT, "ProductType", NULL);
437             set_reg_key(HKEY_LOCAL_MACHINE, szKeyWindNT, "CSDVersion", NULL);
438             set_reg_key(HKEY_LOCAL_MACHINE, szKeyEnvNT, "OS", NULL);
439             set_reg_key(config_key, keypath(""), "Version", NULL);
440             break;
441
442         case VER_PLATFORM_WIN32_NT:
443             snprintf(Buffer, sizeof(Buffer), "%d.%d", win_versions[selection].dwMajorVersion,
444                      win_versions[selection].dwMinorVersion);
445             set_reg_key(HKEY_LOCAL_MACHINE, szKeyNT, "CurrentVersion", Buffer);
446             set_reg_key(HKEY_LOCAL_MACHINE, szKeyNT, "CSDVersion", win_versions[selection].szCSDVersion);
447             snprintf(Buffer, sizeof(Buffer), "%d", win_versions[selection].dwBuildNumber);
448             set_reg_key(HKEY_LOCAL_MACHINE, szKeyNT, "CurrentBuildNumber", Buffer);
449             snprintf(Buffer, sizeof(Buffer), "Microsoft %s", win_versions[selection].szDescription);
450             set_reg_key(HKEY_LOCAL_MACHINE, szKeyNT, "ProductName", Buffer);
451             set_reg_key(HKEY_LOCAL_MACHINE, szKeyProdNT, "ProductType", win_versions[selection].szProductType);
452             set_reg_key_dword(HKEY_LOCAL_MACHINE, szKeyWindNT, "CSDVersion",
453                               MAKEWORD( win_versions[selection].wServicePackMinor,
454                                         win_versions[selection].wServicePackMajor ));
455             set_reg_key(HKEY_LOCAL_MACHINE, szKeyEnvNT, "OS", "Windows_NT");
456
457             set_reg_key(HKEY_LOCAL_MACHINE, szKey9x, "VersionNumber", NULL);
458             set_reg_key(HKEY_LOCAL_MACHINE, szKey9x, "SubVersionNumber", NULL);
459             set_reg_key(HKEY_LOCAL_MACHINE, szKey9x, "ProductName", NULL);
460             set_reg_key(config_key, keypath(""), "Version", NULL);
461             break;
462
463         case VER_PLATFORM_WIN32s:
464             set_reg_key(HKEY_LOCAL_MACHINE, szKeyNT, "CSDVersion", NULL);
465             set_reg_key(HKEY_LOCAL_MACHINE, szKeyNT, "CurrentVersion", NULL);
466             set_reg_key(HKEY_LOCAL_MACHINE, szKeyNT, "CurrentBuildNumber", NULL);
467             set_reg_key(HKEY_LOCAL_MACHINE, szKeyNT, "ProductName", NULL);
468             set_reg_key(HKEY_LOCAL_MACHINE, szKeyProdNT, "ProductType", NULL);
469             set_reg_key(HKEY_LOCAL_MACHINE, szKeyWindNT, "CSDVersion", NULL);
470             set_reg_key(HKEY_LOCAL_MACHINE, szKeyEnvNT, "OS", NULL);
471             set_reg_key(HKEY_LOCAL_MACHINE, szKey9x, "VersionNumber", NULL);
472             set_reg_key(HKEY_LOCAL_MACHINE, szKey9x, "SubVersionNumber", NULL);
473             set_reg_key(HKEY_LOCAL_MACHINE, szKey9x, "ProductName", NULL);
474             set_reg_key(config_key, keypath(""), "Version", win_versions[selection].szVersion);
475             break;
476         }
477     }
478
479     /* enable the apply button  */
480     SendMessageW(GetParent(dialog), PSM_CHANGED, (WPARAM) dialog, 0);
481 }
482
483 INT_PTR CALLBACK
484 AppDlgProc (HWND hDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
485 {
486   switch (uMsg)
487   {
488     case WM_INITDIALOG:
489         init_appsheet(hDlg);
490         break;
491
492     case WM_SHOWWINDOW:
493         set_window_title(hDlg);
494         break;
495
496     case WM_NOTIFY:
497       switch (((LPNMHDR)lParam)->code)
498       {
499         case LVN_ITEMCHANGED:
500             on_selection_change(hDlg, GetDlgItem(hDlg, IDC_APP_LISTVIEW));
501             break;
502         case PSN_APPLY:
503             apply();
504             SetWindowLongPtrW(hDlg, DWLP_MSGRESULT, PSNRET_NOERROR);
505             break;
506       }
507       
508       break;
509     
510     case WM_COMMAND:
511       switch(HIWORD(wParam))
512       {
513         case CBN_SELCHANGE:
514           switch(LOWORD(wParam))
515           {
516             case IDC_WINVER:
517               on_winver_change(hDlg);
518               break;
519           }
520         case BN_CLICKED:
521           switch(LOWORD(wParam))
522           {
523             case IDC_APP_ADDAPP:
524               on_add_app_click(hDlg);
525               break;
526             case IDC_APP_REMOVEAPP:
527               on_remove_app_click(hDlg);
528               break;
529           }
530           break;
531       }
532
533       break;
534   }
535   
536   return 0;
537 }