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