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