Add ShowDirSymLink and ShowDotFiles to the drive ui.
[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 void update_comboboxes(HWND dialog)
36 {
37   int i;
38   const VERSION_DESC *pVer = NULL;
39
40   char *winver;
41   
42   /* retrieve the registry values */
43   winver = get(keypath("Version"), "Windows", "");
44
45   /* empty winver means use automatic mode (ie the builtin dll linkage heuristics)  */
46   WINE_TRACE("winver is %s\n", *winver != '\0' ? winver : "null (automatic mode)");
47
48   /* normalize the version strings */
49   if (*winver != '\0')
50   {
51     if ((pVer = getWinVersions ()))
52     {
53       for (i = 0; *pVer->szVersion || *pVer->szDescription; i++, pVer++)
54       {
55         if (!strcasecmp (pVer->szVersion, winver))
56         {
57           SendDlgItemMessage (dialog, IDC_WINVER, CB_SETCURSEL, (WPARAM) (i + 1), 0);
58           WINE_TRACE("match with %s\n", pVer->szVersion);
59         }
60       }
61     }
62   }
63   else /* no explicit setting */
64   {
65     WINE_TRACE("setting winver combobox to automatic/default\n");
66     SendDlgItemMessage (dialog, IDC_WINVER, CB_SETCURSEL, 0, 0);
67   }
68
69   HeapFree(GetProcessHeap(), 0, winver);
70 }
71
72 void
73 init_comboboxes (HWND dialog)
74 {
75   int i;
76   const VERSION_DESC *ver = NULL;
77
78   SendDlgItemMessage(dialog, IDC_WINVER, CB_RESETCONTENT, 0, 0);
79
80   /* add the default entries (automatic) which correspond to no setting  */
81   if (current_app)
82   {
83       SendDlgItemMessage(dialog, IDC_WINVER, CB_ADDSTRING, 0, (LPARAM) "Use global settings");
84   }
85   else
86   {
87       SendDlgItemMessage(dialog, IDC_WINVER, CB_ADDSTRING, 0, (LPARAM) "Automatically detect required version");
88   }
89
90   if ((ver = getWinVersions ()))
91   {
92     for (i = 0; *ver->szVersion || *ver->szDescription; i++, ver++)
93     {
94       SendDlgItemMessage (dialog, IDC_WINVER, CB_ADDSTRING,
95                           0, (LPARAM) ver->szDescription);
96     }
97   }
98 }
99
100 static void add_listview_item(HWND listview, char *text, void *association)
101 {
102   LVITEM item;
103
104   ZeroMemory(&item, sizeof(LVITEM));
105
106   item.mask = LVIF_TEXT | LVIF_PARAM;
107   item.pszText = text;
108   item.cchTextMax = strlen(text);
109   item.lParam = (LPARAM) association;
110   item.iItem = ListView_GetItemCount(listview);
111
112   ListView_InsertItem(listview, &item);
113 }
114
115 /* Called when the application is initialized (cannot reinit!)  */
116 static void init_appsheet(HWND dialog)
117 {
118   HWND listview;
119   HKEY key;
120   int i;
121   DWORD size;
122   char appname[1024];
123
124   WINE_TRACE("()\n");
125
126   listview = GetDlgItem(dialog, IDC_APP_LISTVIEW);
127
128   /* we use the lparam field of the item so we can alter the presentation later and not change code
129    * for instance, to use the tile view or to display the EXEs embedded 'display name' */
130   add_listview_item(listview, "Default Settings", NULL);
131
132   /* because this list is only populated once, it's safe to bypass the settings list here  */
133   if (RegOpenKey(config_key, "AppDefaults", &key) == ERROR_SUCCESS)
134   {
135       i = 0;
136       size = sizeof(appname);
137       while (RegEnumKeyEx(key, i, appname, &size, NULL, NULL, NULL, NULL) == ERROR_SUCCESS)
138       {
139           add_listview_item(listview, appname, strdupA(appname));
140
141           i++;
142           size = sizeof(appname);
143       }
144
145       RegCloseKey(key);
146   }
147
148   init_comboboxes(dialog);
149   
150   /* Select the default settings listview item  */
151   {
152       LVITEM item;
153       
154       ZeroMemory(&item, sizeof(item));
155       
156       item.mask = LVIF_STATE;
157       item.iItem = 0;
158       item.state = LVIS_SELECTED | LVIS_FOCUSED;
159       item.stateMask = LVIS_SELECTED | LVIS_FOCUSED;
160
161       ListView_SetItem(listview, &item);
162   }
163   
164 }
165
166 /* there has to be an easier way than this  */
167 static int get_listview_selection(HWND listview)
168 {
169   int count = ListView_GetItemCount(listview);
170   int i;
171   
172   for (i = 0; i < count; i++)
173   {
174     if (ListView_GetItemState(listview, i, LVIS_SELECTED)) return i;
175   }
176
177   return -1;
178 }
179
180
181 /* called when the user selects a different application */
182 static void on_selection_change(HWND dialog, HWND listview)
183 {
184   LVITEM item;
185   char *oldapp = current_app;
186
187   WINE_TRACE("()\n");
188
189   item.iItem = get_listview_selection(listview);
190   item.mask = LVIF_PARAM;
191
192   WINE_TRACE("item.iItem=%d\n", item.iItem);
193   
194   if (item.iItem == -1) return;
195   
196   ListView_GetItem(listview, &item);
197
198   current_app = (char *) item.lParam;
199
200   if (current_app)
201   {
202       WINE_TRACE("current_app is now %s\n", current_app);
203       enable(IDC_APP_REMOVEAPP);
204   }
205   else
206   {
207       WINE_TRACE("current_app=NULL, editing global settings\n");
208       /* focus will never be on the button in this callback so it's safe  */
209       disable(IDC_APP_REMOVEAPP);
210   }
211
212   /* reset the combo boxes if we changed from/to global/app-specific  */
213
214   if ((oldapp && !current_app) || (!oldapp && current_app))
215       init_comboboxes(dialog);
216   
217   update_comboboxes(dialog);
218
219   set_window_title(dialog);
220 }
221
222 static BOOL list_contains_file(HWND listview, char *filename)
223 {
224   LVFINDINFO find_info = { LVFI_STRING, filename, 0, {0, 0}, 0 };
225   int index;
226
227   index = ListView_FindItem(listview, -1, &find_info);
228
229   return (index != -1);
230 }
231
232 static void on_add_app_click(HWND dialog)
233 {
234   char filetitle[MAX_PATH];
235   char file[MAX_PATH];
236
237   OPENFILENAME ofn = { sizeof(OPENFILENAME),
238                        0, /*hInst*/0, "Wine Programs (*.exe,*.exe.so)\0*.exe;*.exe.so\0", NULL, 0, 0, NULL,
239                        0, NULL, 0, "c:\\", "Select a Windows executable file",
240                        OFN_SHOWHELP | OFN_HIDEREADONLY, 0, 0, NULL, 0, NULL };
241
242   ofn.lpstrFileTitle = filetitle;
243   ofn.lpstrFileTitle[0] = '\0';
244   ofn.nMaxFileTitle = sizeof(filetitle);
245   ofn.lpstrFile = file;
246   ofn.lpstrFile[0] = '\0';
247   ofn.nMaxFile = sizeof(file);
248
249   if (GetOpenFileName(&ofn))
250   {
251       HWND listview = GetDlgItem(dialog, IDC_APP_LISTVIEW);
252       int count = ListView_GetItemCount(listview);
253       char* new_app;
254       
255       new_app = strdupA(filetitle);
256
257       if (list_contains_file(listview, new_app))
258           return;
259       
260       WINE_TRACE("adding %s\n", new_app);
261       
262       add_listview_item(listview, new_app, new_app);
263
264       ListView_SetItemState(listview, count, LVIS_SELECTED | LVIS_FOCUSED, LVIS_SELECTED | LVIS_FOCUSED);
265
266       SetFocus(listview);
267   }
268   else WINE_TRACE("user cancelled\n");
269 }
270
271 static void on_remove_app_click(HWND dialog)
272 {
273     HWND listview = GetDlgItem(dialog, IDC_APP_LISTVIEW);
274     int selection = get_listview_selection(listview);
275     char *section = keypath(""); /* AppDefaults\\whatever.exe\\ */
276
277     WINE_TRACE("selection=%d, section=%s\n", selection, section);
278     
279     assert( selection != 0 ); /* user cannot click this button when "default settings" is selected  */
280
281     section[strlen(section)] = '\0'; /* remove last backslash  */
282     set(section, NULL, NULL); /* delete the section  */
283     ListView_DeleteItem(listview, selection);
284     ListView_SetItemState(listview, selection - 1, LVIS_SELECTED | LVIS_FOCUSED, LVIS_SELECTED | LVIS_FOCUSED);
285
286     SetFocus(listview);
287     
288     SendMessage(GetParent(dialog), PSM_CHANGED, (WPARAM) dialog, 0);        
289 }
290
291 static void on_winver_change(HWND dialog)
292 {
293     int selection = SendDlgItemMessage(dialog, IDC_WINVER, CB_GETCURSEL, 0, 0);
294     VERSION_DESC *ver = getWinVersions();
295
296     if (selection == 0)
297     {
298         WINE_TRACE("automatic/default selected so removing current setting\n");
299         set(keypath("Version"), "Windows", NULL);
300     }
301     else
302     {
303         WINE_TRACE("setting Version\\Windows key to value '%s'\n", ver[selection - 1].szVersion);
304         set(keypath("Version"), "Windows", ver[selection - 1].szVersion);
305     }
306
307     /* enable the apply button  */
308     SendMessage(GetParent(dialog), PSM_CHANGED, (WPARAM) dialog, 0);
309 }
310
311 INT_PTR CALLBACK
312 AppDlgProc (HWND hDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
313 {
314   switch (uMsg)
315   {
316     case WM_INITDIALOG:
317         init_appsheet(hDlg);
318         break;
319
320     case WM_SHOWWINDOW:
321         set_window_title(hDlg);
322         break;
323
324     case WM_NOTIFY:
325       switch (((LPNMHDR)lParam)->code)
326       {
327         case LVN_ITEMCHANGED:
328             on_selection_change(hDlg, GetDlgItem(hDlg, IDC_APP_LISTVIEW));
329             break;
330         case PSN_APPLY:
331             apply();
332             SetWindowLongPtr(hDlg, DWLP_MSGRESULT, PSNRET_NOERROR);
333             break;
334       }
335       
336       break;
337     
338     case WM_COMMAND:
339       switch(HIWORD(wParam))
340       {
341         case CBN_SELCHANGE:
342           switch(LOWORD(wParam))
343           {
344             case IDC_WINVER:
345               on_winver_change(hDlg);
346               break;
347           }
348         case BN_CLICKED:
349           switch(LOWORD(wParam))
350           {
351             case IDC_APP_ADDAPP:
352               on_add_app_click(hDlg);
353               break;
354             case IDC_APP_REMOVEAPP:
355               on_remove_app_click(hDlg);
356               break;
357           }
358           break;
359       }
360
361       break;
362   }
363   
364   return 0;
365 }