notepad: Fix a string resource so its format placeholders can be reordered.
[wine] / programs / wineconsole / registry.c
1 /*
2  * an application for displaying Win32 console
3  *      registry and init functions
4  *
5  * Copyright 2001 Eric Pouech
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20  */
21
22 #include <stdarg.h>
23
24 #include "windef.h"
25 #include "winbase.h"
26 #include "winreg.h"
27 #include "winecon_private.h"
28
29 #include "wine/debug.h"
30
31 WINE_DEFAULT_DEBUG_CHANNEL(wineconsole);
32
33 static const WCHAR wszConsole[]           = {'C','o','n','s','o','l','e',0};
34 static const WCHAR wszCursorSize[]        = {'C','u','r','s','o','r','S','i','z','e',0};
35 static const WCHAR wszCursorVisible[]     = {'C','u','r','s','o','r','V','i','s','i','b','l','e',0};
36 static const WCHAR wszEditionMode[]       = {'E','d','i','t','i','o','n','M','o','d','e',0};
37 static const WCHAR wszExitOnDie[]         = {'E','x','i','t','O','n','D','i','e',0};
38 static const WCHAR wszFaceName[]          = {'F','a','c','e','N','a','m','e',0};
39 static const WCHAR wszFontSize[]          = {'F','o','n','t','S','i','z','e',0};
40 static const WCHAR wszFontWeight[]        = {'F','o','n','t','W','e','i','g','h','t',0};
41 static const WCHAR wszHistoryBufferSize[] = {'H','i','s','t','o','r','y','B','u','f','f','e','r','S','i','z','e',0};
42 static const WCHAR wszHistoryNoDup[]      = {'H','i','s','t','o','r','y','N','o','D','u','p',0};
43 static const WCHAR wszMenuMask[]          = {'M','e','n','u','M','a','s','k',0};
44 static const WCHAR wszQuickEdit[]         = {'Q','u','i','c','k','E','d','i','t',0};
45 static const WCHAR wszScreenBufferSize[]  = {'S','c','r','e','e','n','B','u','f','f','e','r','S','i','z','e',0};
46 static const WCHAR wszScreenColors[]      = {'S','c','r','e','e','n','C','o','l','o','r','s',0};
47 static const WCHAR wszWindowSize[]        = {'W','i','n','d','o','w','S','i','z','e',0};
48
49 void WINECON_DumpConfig(const char* pfx, const struct config_data* cfg)
50 {
51     WINE_TRACE("%s cell=(%u,%u) cursor=(%d,%d) attr=%02x font=%s/%u hist=%u/%d flags=%c%c msk=%08x sb=(%u,%u) win=(%u,%u)x(%u,%u) edit=%u registry=%s\n",
52                pfx, cfg->cell_width, cfg->cell_height, cfg->cursor_size, cfg->cursor_visible, cfg->def_attr,
53                wine_dbgstr_w(cfg->face_name), cfg->font_weight, cfg->history_size, cfg->history_nodup ? 1 : 2,
54                cfg->quick_edit ? 'Q' : 'q', cfg->exit_on_die ? 'X' : 'x',
55                cfg->menu_mask, cfg->sb_width, cfg->sb_height, cfg->win_pos.X, cfg->win_pos.Y, cfg->win_width, cfg->win_height,
56                cfg->edition_mode,
57                wine_dbgstr_w(cfg->registry));
58 }
59
60 /******************************************************************
61  *              WINECON_CreateKeyName
62  *
63  * Get a proper key name from an appname (mainly convert '\\' to '_')
64  */
65 static LPWSTR   WINECON_CreateKeyName(LPCWSTR kn)
66 {
67     LPWSTR      ret = HeapAlloc(GetProcessHeap(), 0, (lstrlenW(kn) + 1) * sizeof(WCHAR));
68     LPWSTR      ptr = ret;
69
70     if (!ptr) WINECON_Fatal("OOM");
71
72     do
73     {
74         *ptr++ = *kn == '\\' ? '_' : *kn;
75     } while (*kn++ != 0);
76     return ret;
77 }
78
79 /******************************************************************
80  *              WINECON_RegLoadHelper
81  *
82  * Read the basic configuration from any console key or subkey
83  */
84 static void WINECON_RegLoadHelper(HKEY hConKey, struct config_data* cfg)
85 {
86     DWORD       type;
87     DWORD       count;
88     DWORD       val;
89
90     count = sizeof(val);
91     if (!RegQueryValueExW(hConKey, wszCursorSize, 0, &type, (LPBYTE)&val, &count))
92         cfg->cursor_size = val;
93
94     count = sizeof(val);
95     if (!RegQueryValueExW(hConKey, wszCursorVisible, 0, &type, (LPBYTE)&val, &count))
96         cfg->cursor_visible = val;
97
98     count = sizeof(val);
99     if (!RegQueryValueExW(hConKey, wszEditionMode, 0, &type, (LPBYTE)&val, &count))
100         cfg->edition_mode = val;
101
102     count = sizeof(val);
103     if (!RegQueryValueExW(hConKey, wszExitOnDie, 0, &type, (LPBYTE)&val, &count))
104         cfg->exit_on_die = val;
105
106     count = sizeof(cfg->face_name);
107     RegQueryValueExW(hConKey, wszFaceName, 0, &type, (LPBYTE)&cfg->face_name, &count);
108
109     count = sizeof(val);
110     if (!RegQueryValueExW(hConKey, wszFontSize, 0, &type, (LPBYTE)&val, &count))
111     {
112         cfg->cell_height = HIWORD(val);
113         cfg->cell_width  = LOWORD(val);
114     }
115
116     count = sizeof(val);
117     if (!RegQueryValueExW(hConKey, wszFontWeight, 0, &type, (LPBYTE)&val, &count))
118         cfg->font_weight = val;
119
120     count = sizeof(val);
121     if (!RegQueryValueExW(hConKey, wszHistoryBufferSize, 0, &type, (LPBYTE)&val, &count))
122         cfg->history_size = val;
123
124     count = sizeof(val);
125     if (!RegQueryValueExW(hConKey, wszHistoryNoDup, 0, &type, (LPBYTE)&val, &count))
126         cfg->history_nodup = val;
127
128     count = sizeof(val);
129     if (!RegQueryValueExW(hConKey, wszMenuMask, 0, &type, (LPBYTE)&val, &count))
130         cfg->menu_mask = val;
131
132     count = sizeof(val);
133     if (!RegQueryValueExW(hConKey, wszQuickEdit, 0, &type, (LPBYTE)&val, &count))
134         cfg->quick_edit = val;
135
136     count = sizeof(val);
137     if (!RegQueryValueExW(hConKey, wszScreenBufferSize, 0, &type, (LPBYTE)&val, &count))
138     {
139         cfg->sb_height = HIWORD(val);
140         cfg->sb_width  = LOWORD(val);
141     }
142
143     count = sizeof(val);
144     if (!RegQueryValueExW(hConKey, wszScreenColors, 0, &type, (LPBYTE)&val, &count))
145         cfg->def_attr = val;
146
147     count = sizeof(val);
148     if (!RegQueryValueExW(hConKey, wszWindowSize, 0, &type, (LPBYTE)&val, &count))
149     {
150         cfg->win_height = HIWORD(val);
151         cfg->win_width  = LOWORD(val);
152     }
153
154     /* win_pos isn't read from registry */
155 }
156
157 /******************************************************************
158  *              WINECON_RegLoad
159  *
160  *
161  */
162 void WINECON_RegLoad(const WCHAR* appname, struct config_data* cfg)
163 {
164     HKEY        hConKey;
165
166     WINE_TRACE("loading %s registry settings.\n", appname ? wine_dbgstr_w(appname) : "default");
167
168     /* first set default values */
169     cfg->cursor_size = 25;
170     cfg->cursor_visible = 1;
171     cfg->exit_on_die = 1;
172     memset(cfg->face_name, 0, sizeof(cfg->face_name));
173     cfg->cell_height = 12;
174     cfg->cell_width  = 8;
175     cfg->font_weight = 0;
176     cfg->history_size = 50;
177     cfg->history_nodup = 0;
178     cfg->menu_mask = 0;
179     cfg->quick_edit = 0;
180     cfg->sb_height = 25;
181     cfg->sb_width  = 80;
182     cfg->def_attr = 0x000F;
183     cfg->win_height = 25;
184     cfg->win_width  = 80;
185     cfg->win_pos.X = 0;
186     cfg->win_pos.Y = 0;
187     cfg->edition_mode = 0;
188     cfg->registry = NULL;
189
190     /* then read global settings */
191     if (!RegOpenKeyW(HKEY_CURRENT_USER, wszConsole, &hConKey))
192     {
193         WINECON_RegLoadHelper(hConKey, cfg);
194         /* if requested, load part related to console title */
195         if (appname)
196         {
197             HKEY        hAppKey;
198
199             cfg->registry = WINECON_CreateKeyName(appname);
200             if (!RegOpenKeyW(hConKey, cfg->registry, &hAppKey))
201             {
202                 WINECON_RegLoadHelper(hAppKey, cfg);
203                 RegCloseKey(hAppKey);
204             }
205         }
206         RegCloseKey(hConKey);
207     }
208     WINECON_DumpConfig("load", cfg);
209 }
210
211 /******************************************************************
212  *              WINECON_RegSaveHelper
213  *
214  *
215  */
216 static void WINECON_RegSaveHelper(HKEY hConKey, const struct config_data* cfg)
217 {
218     DWORD       val;
219
220     WINECON_DumpConfig("save", cfg);
221
222     val = cfg->cursor_size;
223     RegSetValueExW(hConKey, wszCursorSize, 0, REG_DWORD, (LPBYTE)&val, sizeof(val));
224
225     val = cfg->cursor_visible;
226     RegSetValueExW(hConKey, wszCursorVisible, 0, REG_DWORD, (LPBYTE)&val, sizeof(val));
227
228     val = cfg->edition_mode;
229     RegSetValueExW(hConKey, wszEditionMode, 0, REG_DWORD, (LPBYTE)&val, sizeof(val));
230
231     val = cfg->exit_on_die;
232     RegSetValueExW(hConKey, wszExitOnDie, 0, REG_DWORD, (LPBYTE)&val, sizeof(val));
233
234     RegSetValueExW(hConKey, wszFaceName, 0, REG_SZ, (LPBYTE)&cfg->face_name, sizeof(cfg->face_name));
235
236     val = MAKELONG(cfg->cell_width, cfg->cell_height);
237     RegSetValueExW(hConKey, wszFontSize, 0, REG_DWORD, (LPBYTE)&val, sizeof(val));
238
239     val = cfg->font_weight;
240     RegSetValueExW(hConKey, wszFontWeight, 0, REG_DWORD, (LPBYTE)&val, sizeof(val));
241
242     val = cfg->history_size;
243     RegSetValueExW(hConKey, wszHistoryBufferSize, 0, REG_DWORD, (LPBYTE)&val, sizeof(val));
244
245     val = cfg->history_nodup;
246     RegSetValueExW(hConKey, wszHistoryNoDup, 0, REG_DWORD, (LPBYTE)&val, sizeof(val));
247
248     val = cfg->menu_mask;
249     RegSetValueExW(hConKey, wszMenuMask, 0, REG_DWORD, (LPBYTE)&val, sizeof(val));
250
251     val = cfg->quick_edit;
252     RegSetValueExW(hConKey, wszQuickEdit, 0, REG_DWORD, (LPBYTE)&val, sizeof(val));
253
254     val = MAKELONG(cfg->sb_width, cfg->sb_height);
255     RegSetValueExW(hConKey, wszScreenBufferSize, 0, REG_DWORD, (LPBYTE)&val, sizeof(val));
256
257     val = cfg->def_attr;
258     RegSetValueExW(hConKey, wszScreenColors, 0, REG_DWORD, (LPBYTE)&val, sizeof(val));
259
260     val = MAKELONG(cfg->win_width, cfg->win_height);
261     RegSetValueExW(hConKey, wszWindowSize, 0, REG_DWORD, (LPBYTE)&val, sizeof(val));
262 }
263
264 /******************************************************************
265  *              WINECON_RegSave
266  *
267  *
268  */
269 void WINECON_RegSave(const struct config_data* cfg)
270 {
271     HKEY        hConKey;
272
273     WINE_TRACE("saving registry settings.\n");
274     if (RegCreateKeyW(HKEY_CURRENT_USER, wszConsole, &hConKey))
275     {
276         WINE_ERR("Can't open registry for saving\n");
277     }
278     else
279     {
280         if (cfg->registry)
281         {
282             HKEY    hAppKey;
283
284             if (RegCreateKeyW(hConKey, cfg->registry, &hAppKey))
285             {
286                 WINE_ERR("Can't open registry for saving\n");
287             }
288             else
289             {
290                 /* FIXME: maybe only save the values different from the default value ? */
291                 WINECON_RegSaveHelper(hAppKey, cfg);
292                 RegCloseKey(hAppKey);
293             }
294         }
295         else WINECON_RegSaveHelper(hConKey, cfg);
296         RegCloseKey(hConKey);
297     }
298 }