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