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