Use a better location than HKCU\Wine for saving the temporary
[wine] / programs / winecfg / libraries.c
1 /*
2  * WineCfg libraries tabsheet
3  *
4  * Copyright 2004 Robert van Herk
5  * Copyright 2004 Mike Hearn
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20  *
21  */
22
23 #define NONAMELESSUNION
24 #include <windows.h>
25 #include <commdlg.h>
26 #include <wine/debug.h>
27 #include <stdio.h>
28 #include <assert.h>
29 #include "winecfg.h"
30 #include "resource.h"
31
32 WINE_DEFAULT_DEBUG_CHANNEL(winecfg);
33
34 enum dllmode
35 {
36         BUILTIN_NATIVE,
37         NATIVE_BUILTIN,
38         BUILTIN,
39         NATIVE,
40         DISABLE,
41         UNKNOWN /* Special value indicating an erronous DLL override mode */
42 };
43
44 struct dll
45 {
46         char *name;
47         enum dllmode mode;
48 };
49
50 /* Convert a registry string to a dllmode */
51 static enum dllmode string_to_mode(char *in)
52 {
53     int i, j, len;
54     char *out;
55     enum dllmode res;
56
57     len = strlen(in);
58     out = HeapAlloc(GetProcessHeap(), 0, len);
59
60     /* remove the spaces */
61     for (i = j = 0; i <= len; ++i) {
62         if (in[i] != ' ') {
63             out[j++] = in[i];
64         }
65     }
66
67     /* parse the string */
68     res = UNKNOWN;
69     if (strcmp(out, "builtin,native") == 0) res = BUILTIN_NATIVE;
70     if (strcmp(out, "native,builtin") == 0) res = NATIVE_BUILTIN;
71     if (strcmp(out, "builtin") == 0) res = BUILTIN;
72     if (strcmp(out, "native") == 0) res = NATIVE;
73     if (strcmp(out, "") == 0) res = DISABLE;
74
75     HeapFree(GetProcessHeap(), 0, out);
76     return res;
77 }
78
79 /* Convert a dllmode to a registry string. */
80 static const char* mode_to_string(enum dllmode mode)
81 {
82     switch( mode )
83     {
84         case NATIVE: return "native";
85         case BUILTIN: return "builtin";
86         case NATIVE_BUILTIN: return "native,builtin";
87         case BUILTIN_NATIVE: return "builtin,native";
88         case DISABLE: return "";
89         default: assert(FALSE); return "";
90     }
91 }
92
93 /* Convert a dllmode to a pretty string for display. TODO: use translations. */
94 static const char* mode_to_label(enum dllmode mode)
95 {
96     WINE_FIXME("translate me");
97     return mode_to_string(mode);
98 }
99
100 /* Convert a control id (IDC_ constant) to a dllmode */
101 static enum dllmode id_to_mode(DWORD id)
102 {
103     switch( id )
104     {
105         case IDC_RAD_BUILTIN: return BUILTIN;
106         case IDC_RAD_NATIVE: return NATIVE;
107         case IDC_RAD_NATIVE_BUILTIN: return NATIVE_BUILTIN;
108         case IDC_RAD_BUILTIN_NATIVE: return BUILTIN_NATIVE;
109         case IDC_RAD_DISABLE: return DISABLE;
110         default: assert( FALSE ); return 0; /* should not be reached  */
111     }
112 }
113
114 /* Convert a dllmode to a control id (IDC_ constant) */
115 static DWORD mode_to_id(enum dllmode mode)
116 {
117     switch( mode )
118     {
119         case BUILTIN: return IDC_RAD_BUILTIN;
120         case NATIVE: return IDC_RAD_NATIVE;
121         case NATIVE_BUILTIN: return IDC_RAD_NATIVE_BUILTIN;
122         case BUILTIN_NATIVE: return IDC_RAD_BUILTIN_NATIVE;
123         case DISABLE: return IDC_RAD_DISABLE;
124         default: assert( FALSE ); return 0; /* should not be reached  */
125     }
126 }
127
128 static void set_controls_from_selection(HWND dialog)
129 {
130     int index = SendDlgItemMessage(dialog, IDC_DLLS_LIST, LB_GETCURSEL, 0, 0);
131     struct dll *dll;
132     DWORD id;
133     int i;
134     
135     if (index == -1) /* no selection  */
136     {
137         for (i = IDC_RAD_BUILTIN; i <= IDC_RAD_DISABLE; i++)
138             disable(i);
139
140         CheckRadioButton(dialog, IDC_RAD_BUILTIN, IDC_RAD_DISABLE, -1);
141         
142         return;
143     }
144
145     /* enable the controls  */
146     for (i = IDC_RAD_BUILTIN; i <= IDC_RAD_DISABLE; i++)
147         enable(i);
148
149     dll = (struct dll *) SendDlgItemMessage(dialog, IDC_DLLS_LIST, LB_GETITEMDATA, index, 0);
150    
151     id = mode_to_id(dll->mode);
152
153     CheckRadioButton(dialog, IDC_RAD_BUILTIN, IDC_RAD_DISABLE, id);
154 }
155
156
157 static void clear_settings(HWND dialog)
158 {
159     int count = SendDlgItemMessage(dialog, IDC_DLLS_LIST, LB_GETCOUNT, 0, 0);
160     int i;
161
162     WINE_TRACE("count=%d\n", count);
163     
164     for (i = 0; i < count; i++)
165     {
166         struct dll *dll = (struct dll *) SendDlgItemMessage(dialog, IDC_DLLS_LIST, LB_GETITEMDATA, 0, 0);
167         
168         SendDlgItemMessage(dialog, IDC_DLLS_LIST, LB_DELETESTRING, 0, 0);
169         
170         HeapFree(GetProcessHeap(), 0, dll->name);
171         HeapFree(GetProcessHeap(), 0, dll);
172     }
173 }
174
175 static void load_library_settings(HWND dialog)
176 {
177     char **overrides = enumerate_values(keypath("DllOverrides"));
178     char **p;
179     int sel, count = 0;
180
181     sel = SendDlgItemMessage(dialog, IDC_DLLS_LIST, LB_GETCURSEL, 0, 0);
182
183     WINE_TRACE("sel=%d\n", sel);
184
185     clear_settings(dialog);
186     
187     if (!overrides || *overrides == NULL)
188     {
189         set_controls_from_selection(dialog);
190         disable(IDC_DLLS_REMOVEDLL);
191         HeapFree(GetProcessHeap(), 0, overrides);
192         return;
193     }
194
195     enable(IDC_DLLS_REMOVEDLL);
196     
197     for (p = overrides; *p != NULL; p++)
198     {
199         int index;
200         char *str, *value;
201         const char *label;
202         struct dll *dll;
203
204         value = get_reg_key(keypath("DllOverrides"), *p, NULL);
205
206         label = mode_to_label(string_to_mode(value));
207         
208         str = HeapAlloc(GetProcessHeap(), 0, strlen(*p) + 2 + strlen(label) + 2);
209         strcpy(str, *p);
210         strcat(str, " (");
211         strcat(str, label);
212         strcat(str, ")");
213
214         dll = HeapAlloc(GetProcessHeap(), 0, sizeof(struct dll));
215         dll->name = *p;
216         dll->mode = string_to_mode(value);
217
218         index = SendDlgItemMessage(dialog, IDC_DLLS_LIST, LB_ADDSTRING, (WPARAM) -1, (LPARAM) str);
219         SendDlgItemMessage(dialog, IDC_DLLS_LIST, LB_SETITEMDATA, index, (LPARAM) dll);
220
221         HeapFree(GetProcessHeap(), 0, str);
222
223         count++;
224     }
225
226     HeapFree(GetProcessHeap(), 0, overrides);
227
228     /* restore the previous selection, if possible  */
229     if (sel >= count - 1) sel = count - 1;
230     else if (sel == -1) sel = 0;
231     
232     SendDlgItemMessage(dialog, IDC_DLLS_LIST, LB_SETCURSEL, sel, 0);
233
234     set_controls_from_selection(dialog);
235 }
236
237 /* Called when the application is initialized (cannot reinit!)  */
238 static void init_libsheet(HWND dialog)
239 {
240     /* clear the add dll controls  */
241     SendDlgItemMessage(dialog, IDC_DLLCOMBO, WM_SETTEXT, 1, (LPARAM) "");
242     disable(IDC_DLLS_ADDDLL);
243 }
244
245
246 static void on_add_combo_change(HWND dialog)
247 {
248     char buffer[1024];
249
250     SendDlgItemMessage(dialog, IDC_DLLCOMBO, WM_GETTEXT, sizeof(buffer), (LPARAM) buffer);
251
252     if (strlen(buffer))
253         enable(IDC_DLLS_ADDDLL)
254     else
255         disable(IDC_DLLS_ADDDLL);
256 }
257
258 static void set_dllmode(HWND dialog, DWORD id)
259 {
260     enum dllmode mode;
261     struct dll *dll;
262     int sel;
263     const char *str;
264
265     mode = id_to_mode(id);
266
267     sel = SendDlgItemMessage(dialog, IDC_DLLS_LIST, LB_GETCURSEL, 0, 0);
268     if (sel == -1) return;
269     
270     dll = (struct dll *) SendDlgItemMessage(dialog, IDC_DLLS_LIST, LB_GETITEMDATA, sel, 0);
271
272     str = mode_to_string(mode);
273     WINE_TRACE("Setting %s to %s\n", dll->name, str);
274     
275     SendMessage(GetParent(dialog), PSM_CHANGED, 0, 0);
276     set_reg_key(keypath("DllOverrides"), dll->name, str);
277
278     load_library_settings(dialog);  /* ... and refresh  */
279 }
280
281 static void on_add_click(HWND dialog)
282 {
283     char buffer[1024];
284
285     ZeroMemory(buffer, sizeof(buffer));
286
287     SendDlgItemMessage(dialog, IDC_DLLCOMBO, WM_GETTEXT, sizeof(buffer), (LPARAM) buffer);
288
289     SendDlgItemMessage(dialog, IDC_DLLCOMBO, WM_SETTEXT, 0, (LPARAM) "");
290     disable(IDC_DLLS_ADDDLL);
291     
292     WINE_TRACE("Adding %s as native, builtin", buffer);
293     
294     SendMessage(GetParent(dialog), PSM_CHANGED, 0, 0);
295     set_reg_key(keypath("DllOverrides"), buffer, "native,builtin");
296
297     load_library_settings(dialog);
298
299     SendDlgItemMessage(dialog, IDC_DLLS_LIST, LB_SELECTSTRING, (WPARAM) 0, (LPARAM) buffer);
300
301     set_controls_from_selection(dialog);
302 }
303
304 static void on_remove_click(HWND dialog)
305 {
306     int sel = SendDlgItemMessage(dialog, IDC_DLLS_LIST, LB_GETCURSEL, 0, 0);
307     struct dll *dll;
308
309     if (sel == LB_ERR) return;
310     
311     dll = (struct dll *) SendDlgItemMessage(dialog, IDC_DLLS_LIST, LB_GETITEMDATA, sel, 0);
312     
313     SendDlgItemMessage(dialog, IDC_DLLS_LIST, LB_DELETESTRING, sel, 0);
314
315     SendMessage(GetParent(dialog), PSM_CHANGED, 0, 0);
316     set_reg_key(keypath("DllOverrides"), dll->name, NULL);
317
318     HeapFree(GetProcessHeap(), 0, dll->name);
319     HeapFree(GetProcessHeap(), 0, dll);
320
321     if (SendDlgItemMessage(dialog, IDC_DLLS_LIST, LB_GETCOUNT, 0, 0) > 0)
322         SendDlgItemMessage(dialog, IDC_DLLS_LIST, LB_SETCURSEL, max(sel - 1, 0), 0);
323     else
324         disable(IDC_DLLS_REMOVEDLL);
325
326     set_controls_from_selection(dialog);
327 }
328
329 INT_PTR CALLBACK
330 LibrariesDlgProc (HWND hDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
331 {
332         switch (uMsg)
333         {
334         case WM_INITDIALOG:
335                 init_libsheet(hDlg);
336                 break;
337         case WM_SHOWWINDOW:
338                 set_window_title(hDlg);
339                 break;
340         case WM_NOTIFY:
341                 switch (((LPNMHDR)lParam)->code) {
342                 case PSN_SETACTIVE:
343                     load_library_settings(hDlg);
344                     break;
345                 }
346                 break;
347         case WM_COMMAND:
348                 switch(HIWORD(wParam)) {
349
350                     /* FIXME: when the user hits enter in the DLL combo box we should invoke the add
351                      * add button, rather than the propsheet OK button. But I don't know how to do that!
352                      */
353                     
354                 case CBN_EDITCHANGE:
355                         if(LOWORD(wParam) == IDC_DLLCOMBO)
356                         {
357                             on_add_combo_change(hDlg);
358                             break;
359                         }
360                     
361                 case BN_CLICKED:
362                         switch(LOWORD(wParam)) {
363                         case IDC_RAD_BUILTIN:
364                         case IDC_RAD_NATIVE:
365                         case IDC_RAD_BUILTIN_NATIVE:
366                         case IDC_RAD_NATIVE_BUILTIN:
367                         case IDC_RAD_DISABLE:
368                             set_dllmode(hDlg, LOWORD(wParam));
369                             break;
370                             
371                         case IDC_DLLS_ADDDLL:
372                             on_add_click(hDlg);
373                             break;
374                         case IDC_DLLS_REMOVEDLL:
375                             on_remove_click(hDlg);
376                             break;
377                         }
378                         break;
379                 case LBN_SELCHANGE:
380                         set_controls_from_selection(hDlg);
381                         break;
382                 }
383                 break;
384         }
385
386         return 0;
387 }