Fix value renaming. Cleanup code, fix a few leaks.
[wine/multimedia.git] / programs / winecfg / appdefaults.c
blob8093b9c7584f5d3a28388893e34ebe4ede597557
1 /*
2 * Application defaults page
4 * Copyright 2003 Mike Hearn
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #include <stdio.h>
24 #include "winecfg.h"
25 #include <windef.h>
26 #include <winbase.h>
27 #include <winreg.h>
28 #include <wine/debug.h>
30 #include "resource.h"
32 WINE_DEFAULT_DEBUG_CHANNEL(winecfg);
34 int appSettings = EDITING_GLOBAL; /* start by editing global */
35 char *currentApp; /* the app we are currently editing, or NULL if editing global */
37 static int needToRefresh = 1;
39 char *getSectionForApp(char *section) {
40 static char *lastResult = NULL;
41 if (lastResult) HeapFree(GetProcessHeap(), 0, lastResult);
42 lastResult = HeapAlloc(GetProcessHeap(), 0, strlen("AppDefaults\\") + strlen(currentApp) + 2 /* \\ */ + strlen(section) + 1 /* terminator */);
43 sprintf(lastResult, "AppDefaults\\%s\\%s", currentApp, section);
44 return lastResult;
47 static void configureFor(HWND dialog, int mode) {
48 CheckRadioButton(dialog, IDC_EDITING_GLOBAL, IDC_EDITING_APP, mode == EDITING_APP ? IDC_EDITING_APP : IDC_EDITING_GLOBAL);
49 if (mode == EDITING_GLOBAL) {
50 disable(IDC_LIST_APPS);
51 disable(IDC_ADD_APPDEFAULT);
52 disable(IDC_REMOVE_APPDEFAULT);
53 } else {
54 enable(IDC_LIST_APPS);
55 enable(IDC_ADD_APPDEFAULT);
56 enable(IDC_REMOVE_APPDEFAULT);
58 appSettings = mode;
61 /* fill the dialog with the current appdefault entries */
62 static void refreshDialog(HWND dialog) {
63 HKEY key;
64 char *subKeyName = HeapAlloc(GetProcessHeap(), 0, MAX_NAME_LENGTH);
65 DWORD sizeOfSubKeyName = MAX_NAME_LENGTH;
66 int i, itemIndex;
68 WINE_TRACE("\n");
70 /* Clear the listbox */
71 SendMessageA(GetDlgItem(dialog, IDC_LIST_APPS), LB_RESETCONTENT, 0, 0);
73 return_if_fail(
74 RegCreateKey(HKEY_LOCAL_MACHINE, WINE_KEY_ROOT "\\AppDefaults", &key) == ERROR_SUCCESS
77 /* Iterate over each subkey in the AppDefaults tree */
78 for (i = 0;
79 RegEnumKeyEx(key, i, subKeyName, &sizeOfSubKeyName, NULL, NULL, NULL, NULL ) != ERROR_NO_MORE_ITEMS;
80 ++i, sizeOfSubKeyName = MAX_NAME_LENGTH) {
82 WINE_TRACE("appdefault entry=%s\n", subKeyName);
83 itemIndex = SendMessageA(GetDlgItem(dialog, IDC_LIST_APPS), LB_ADDSTRING ,(WPARAM) -1, (LPARAM) subKeyName);
86 configureFor(dialog, appSettings);
88 WINE_TRACE("done\n");
89 RegCloseKey(key);
90 HeapFree(GetProcessHeap(), 0, subKeyName);
94 static void onAppsListSelChange(HWND dialog) {
95 int newPos = SendDlgItemMessage(dialog, IDC_LIST_APPS, LB_GETCURSEL, 0, 0);
96 int appLen = SendDlgItemMessage(dialog, IDC_LIST_APPS, LB_GETTEXTLEN, newPos, 0);
97 if (currentApp) HeapFree(GetProcessHeap(), 0, currentApp);
98 currentApp = HeapAlloc(GetProcessHeap(), 0, appLen+1);
99 return_if_fail(
100 SendDlgItemMessage(dialog, IDC_LIST_APPS, LB_GETTEXT, newPos, (LPARAM) currentApp) != LB_ERR
102 WINE_TRACE("new selection is %s\n", currentApp);
105 INT_PTR CALLBACK
106 AppDlgProc (HWND hDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
108 switch (uMsg)
110 case WM_COMMAND: switch (LOWORD(wParam)) {
111 case IDC_EDITING_APP:
112 if (SendDlgItemMessage(hDlg, IDC_LIST_APPS, LB_GETCURSEL, 0, 0) == LB_ERR) {
113 /* no selection, so select the first one */
114 SendDlgItemMessage(hDlg, IDC_LIST_APPS, LB_SETCURSEL, 0, 0);
115 onAppsListSelChange(hDlg);
117 configureFor(hDlg, EDITING_APP);
118 break;
119 case IDC_EDITING_GLOBAL:
120 configureFor(hDlg, EDITING_GLOBAL);
121 break;
122 case IDC_ADD_APPDEFAULT:
123 WRITEME(hDlg);
124 refreshDialog(hDlg);
125 break;
126 case IDC_REMOVE_APPDEFAULT:
127 WRITEME(hDlg);
128 refreshDialog(hDlg);
129 break;
130 case IDC_LIST_APPS:
131 if (HIWORD(wParam) == LBN_SELCHANGE) onAppsListSelChange(hDlg);
132 break;
134 break;
136 case WM_NOTIFY: switch(((LPNMHDR)lParam)->code) {
137 case PSN_KILLACTIVE:
138 SetWindowLong(hDlg, DWL_MSGRESULT, FALSE);
139 break;
140 case PSN_APPLY:
141 SetWindowLong(hDlg, DWL_MSGRESULT, PSNRET_NOERROR);
142 break;
143 case PSN_SETACTIVE:
144 if (needToRefresh) {
145 refreshDialog(hDlg);
146 needToRefresh = 0;
148 break;
150 break;
152 case WM_INITDIALOG:
153 WINE_TRACE("Init appdefaults\n");
154 break;
157 return FALSE;