ShellExecuteEx, ExtractIconEx, SHFileOperation, SHGetFileInfo,
[wine/wine-gecko.git] / programs / winecfg / appdefaults.c
blob4a3666598d17da7cf051a57946a8324c84a7307b
1 /*
2 * WineCfg app settings tabsheet
4 * Copyright 2004 Robert van Herk
5 * Copyright 2004 Chris Morgan
6 * Copyright 2004 Mike Hearn
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.
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.
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
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"
33 WINE_DEFAULT_DEBUG_CHANNEL(winecfg);
35 static void update_comboboxes(HWND dialog)
37 int i;
38 const VERSION_DESC *pVer = NULL;
40 char *winver, *dosver;
42 /* retrieve the registry values */
43 winver = get(keypath("Version"), "Windows", "");
44 dosver = get(keypath("Version"), "DOS", "");
46 /* empty winver/dosver means use automatic mode (ie the builtin dll linkage heuristics) */
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)");
51 /* normalize the version strings */
52 if (*winver != '\0')
54 if ((pVer = getWinVersions ()))
56 for (i = 0; *pVer->szVersion || *pVer->szDescription; i++, pVer++)
58 if (!strcasecmp (pVer->szVersion, winver))
60 SendDlgItemMessage (dialog, IDC_WINVER, CB_SETCURSEL, (WPARAM) (i + 1), 0);
61 WINE_TRACE("match with %s\n", pVer->szVersion);
66 else /* no explicit setting */
68 WINE_TRACE("setting winver combobox to automatic/default\n");
69 SendDlgItemMessage (dialog, IDC_WINVER, CB_SETCURSEL, 0, 0);
72 if (*dosver != '\0')
74 if ((pVer = getDOSVersions ()))
76 for (i = 0; *pVer->szVersion || *pVer->szDescription; i++, pVer++)
78 if (!strcasecmp (pVer->szVersion, dosver))
80 SendDlgItemMessage (dialog, IDC_DOSVER, CB_SETCURSEL,
81 (WPARAM) (i + 1), 0);
82 WINE_TRACE("match with %s\n", pVer->szVersion);
87 else
89 WINE_TRACE("setting dosver combobox to automatic/default\n");
90 SendDlgItemMessage (dialog, IDC_DOSVER, CB_SETCURSEL, 0, 0);
93 HeapFree(GetProcessHeap(), 0, winver);
94 HeapFree(GetProcessHeap(), 0, dosver);
97 void
98 init_comboboxes (HWND dialog)
100 int i;
101 const VERSION_DESC *ver = NULL;
104 SendDlgItemMessage(dialog, IDC_WINVER, CB_RESETCONTENT, 0, 0);
105 SendDlgItemMessage(dialog, IDC_DOSVER, CB_RESETCONTENT, 0, 0);
107 /* add the default entries (automatic) which correspond to no setting */
108 if (current_app)
110 SendDlgItemMessage(dialog, IDC_WINVER, CB_ADDSTRING, 0, (LPARAM) "Use global settings");
111 SendDlgItemMessage(dialog, IDC_DOSVER, CB_ADDSTRING, 0, (LPARAM) "Use global settings");
113 else
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");
119 if ((ver = getWinVersions ()))
121 for (i = 0; *ver->szVersion || *ver->szDescription; i++, ver++)
123 SendDlgItemMessage (dialog, IDC_WINVER, CB_ADDSTRING,
124 0, (LPARAM) ver->szDescription);
127 if ((ver = getDOSVersions ()))
129 for (i = 0; *ver->szVersion || *ver->szDescription ; i++, ver++)
131 SendDlgItemMessage (dialog, IDC_DOSVER, CB_ADDSTRING,
132 0, (LPARAM) ver->szDescription);
137 static void add_listview_item(HWND listview, char *text, void *association)
139 LVITEM item;
141 ZeroMemory(&item, sizeof(LVITEM));
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);
149 ListView_InsertItem(listview, &item);
152 /* Called when the application is initialized (cannot reinit!) */
153 static void init_appsheet(HWND dialog)
155 HWND listview;
156 HKEY key;
157 int i;
158 DWORD size;
159 char appname[1024];
161 WINE_TRACE("()\n");
163 listview = GetDlgItem(dialog, IDC_APP_LISTVIEW);
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);
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)
172 i = 0;
173 size = sizeof(appname);
174 while (RegEnumKeyEx(key, i, appname, &size, NULL, NULL, NULL, NULL) == ERROR_SUCCESS)
176 add_listview_item(listview, appname, strdupA(appname));
178 i++;
179 size = sizeof(appname);
182 RegCloseKey(key);
185 init_comboboxes(dialog);
187 /* Select the default settings listview item */
189 LVITEM item;
191 ZeroMemory(&item, sizeof(item));
193 item.mask = LVIF_STATE;
194 item.iItem = 0;
195 item.state = LVIS_SELECTED | LVIS_FOCUSED;
196 item.stateMask = LVIS_SELECTED | LVIS_FOCUSED;
198 ListView_SetItem(listview, &item);
203 /* there has to be an easier way than this */
204 static int get_listview_selection(HWND listview)
206 int count = ListView_GetItemCount(listview);
207 int i;
209 for (i = 0; i < count; i++)
211 if (ListView_GetItemState(listview, i, LVIS_SELECTED)) return i;
214 return -1;
218 /* called when the user selects a different application */
219 static void on_selection_change(HWND dialog, HWND listview)
221 LVITEM item;
222 char *oldapp = current_app;
224 WINE_TRACE("()\n");
226 item.iItem = get_listview_selection(listview);
227 item.mask = LVIF_PARAM;
229 if (item.iItem == -1) return;
231 ListView_GetItem(listview, &item);
233 current_app = (char *) item.lParam;
235 if (current_app)
237 WINE_TRACE("current_app is now %s\n", current_app);
238 enable(IDC_APP_REMOVEAPP);
240 else
242 WINE_TRACE("current_app=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);
247 /* reset the combo boxes if we changed from/to global/app-specific */
249 if ((oldapp && !current_app) || (!oldapp && current_app))
250 init_comboboxes(dialog);
252 update_comboboxes(dialog);
254 set_window_title(dialog);
257 static void on_add_app_click(HWND dialog)
259 char filetitle[MAX_PATH];
260 char file[MAX_PATH];
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 };
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);
274 if (GetOpenFileName(&ofn))
276 HWND listview = GetDlgItem(dialog, IDC_APP_LISTVIEW);
277 int count = ListView_GetItemCount(listview);
279 if (current_app) HeapFree(GetProcessHeap(), 0, current_app);
280 current_app = strdupA(filetitle);
282 WINE_TRACE("adding %s\n", current_app);
284 add_listview_item(listview, current_app, current_app);
286 ListView_SetItemState(listview, count, LVIS_SELECTED | LVIS_FOCUSED, LVIS_SELECTED | LVIS_FOCUSED);
288 SetFocus(listview);
290 else WINE_TRACE("user cancelled\n");
293 static void on_remove_app_click(HWND dialog)
295 HWND listview = GetDlgItem(dialog, IDC_APP_LISTVIEW);
296 int selection = get_listview_selection(listview);
297 char *section = keypath(""); /* AppDefaults\\whatever.exe\\ */
299 WINE_TRACE("selection=%d, section=%s\n", selection, section);
301 assert( selection != 0 ); /* user cannot click this button when "default settings" is selected */
303 section[strlen(section)] = '\0'; /* remove last backslash */
304 set(section, NULL, NULL); /* delete the section */
305 ListView_DeleteItem(listview, selection);
307 SetFocus(listview);
310 static void on_winver_change(HWND dialog)
312 int selection = SendDlgItemMessage(dialog, IDC_WINVER, CB_GETCURSEL, 0, 0);
313 VERSION_DESC *ver = getWinVersions();
315 if (selection == 0)
317 WINE_TRACE("automatic/default selected so removing current setting\n");
318 set(keypath("Version"), "Windows", NULL);
320 else
322 WINE_TRACE("setting Version\\Windows key to value '%s'\n", ver[selection - 1].szVersion);
323 set(keypath("Version"), "Windows", ver[selection - 1].szVersion);
326 /* enable the apply button */
327 SendMessage(GetParent(dialog), PSM_CHANGED, (WPARAM) dialog, 0);
330 static void on_dosver_change(HWND dialog)
332 int selection = SendDlgItemMessage(dialog, IDC_DOSVER, CB_GETCURSEL, 0, 0);
333 VERSION_DESC *ver = getDOSVersions();
335 if (selection == 0)
337 WINE_TRACE("automatic/default selected so removing current setting\n");
338 set(keypath("Version"), "DOS", NULL);
340 else
342 WINE_TRACE("setting Version\\DOS key to value '%s'\n", ver[selection - 1].szVersion);
343 set(keypath("Version"), "DOS", ver[selection - 1].szVersion);
346 /* enable the apply button */
347 SendMessage(GetParent(dialog), PSM_CHANGED, (WPARAM) dialog, 0);
350 INT_PTR CALLBACK
351 AppDlgProc (HWND hDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
353 switch (uMsg)
355 case WM_INITDIALOG:
356 init_appsheet(hDlg);
357 break;
359 case WM_SHOWWINDOW:
360 set_window_title(hDlg);
361 break;
363 case WM_NOTIFY:
364 switch (((LPNMHDR)lParam)->code)
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;
375 break;
377 case WM_COMMAND:
378 switch(HIWORD(wParam))
380 case CBN_SELCHANGE:
381 switch(LOWORD(wParam))
383 case IDC_WINVER:
384 on_winver_change(hDlg);
385 break;
386 case IDC_DOSVER:
387 on_dosver_change(hDlg);
388 break;
390 case BN_CLICKED:
391 switch(LOWORD(wParam))
393 case IDC_APP_ADDAPP:
394 on_add_app_click(hDlg);
395 break;
396 case IDC_APP_REMOVEAPP:
397 on_remove_app_click(hDlg);
398 break;
400 break;
403 break;
406 return 0;