]> icculus.org git repositories - icculus/iodoom3.git/blob - neo/tools/common/RegistryOptions.cpp
hello world
[icculus/iodoom3.git] / neo / tools / common / RegistryOptions.cpp
1 /*
2 ===========================================================================
3
4 Doom 3 GPL Source Code
5 Copyright (C) 1999-2011 id Software LLC, a ZeniMax Media company. 
6
7 This file is part of the Doom 3 GPL Source Code (?Doom 3 Source Code?).  
8
9 Doom 3 Source Code is free software: you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation, either version 3 of the License, or
12 (at your option) any later version.
13
14 Doom 3 Source Code is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with Doom 3 Source Code.  If not, see <http://www.gnu.org/licenses/>.
21
22 In addition, the Doom 3 Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the Doom 3 Source Code.  If not, please request a copy in writing from id Software at the address below.
23
24 If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA.
25
26 ===========================================================================
27 */
28
29 #include "../../idlib/precompiled.h"
30 #pragma hdrstop
31
32 #include "RegistryOptions.h"
33
34 /*
35 ================
36 rvRegistryOptions::rvRegistryOptions
37
38 Constructor
39 ================
40 */
41 rvRegistryOptions::rvRegistryOptions( void ) {
42 }
43
44 /*
45 ================
46 rvRegistryOptions::Init
47 ================
48 */
49 void rvRegistryOptions::Init( const char *key ) {
50         mBaseKey = key;
51 }
52
53 /*
54 ================
55 rvRegistryOptions::Save
56
57 Write the options to the registry 
58 ================
59 */
60 bool rvRegistryOptions::Save ( void )
61 {
62         HKEY    hKey;
63         int             i;
64
65         // Create the top level key
66         if ( ERROR_SUCCESS != RegCreateKeyEx ( HKEY_LOCAL_MACHINE, mBaseKey, 0, NULL, 0, KEY_ALL_ACCESS, NULL, &hKey, NULL ) )
67         {
68                 return false;
69         }
70
71         // Write out the values
72         for ( i = 0; i < mValues.GetNumKeyVals(); i ++ )
73         {
74                 const idKeyValue* key = mValues.GetKeyVal ( i );
75                 assert ( key ); 
76                 RegSetValueEx ( hKey, key->GetKey().c_str(), 0, REG_SZ, (BYTE*)key->GetValue().c_str(), key->GetValue().Length() );
77         }
78
79         // Write Recent Files
80         for ( i = 0; i < mRecentFiles.Num(); i ++ )     
81         {
82                 RegSetValueEx ( hKey, va("mru%d",i), 0, REG_SZ, (BYTE*)mRecentFiles[i].c_str(), mRecentFiles[i].Length() );     
83         }
84
85         return true;
86 }
87
88 /*
89 ================
90 rvRegistryOptions::Load
91
92 Read the options from the registry
93 ================
94 */
95 bool rvRegistryOptions::Load ( void )
96 {
97         HKEY    hKey;
98         char    temp[MAX_PATH];
99         TCHAR   keyname[MAX_PATH];
100         DWORD   dwType;
101         DWORD   dwSize;
102         int             i;
103
104         mValues.Clear ( );
105         mRecentFiles.Clear ( );
106
107         if ( ERROR_SUCCESS != RegOpenKeyEx ( HKEY_LOCAL_MACHINE, mBaseKey, 0, KEY_READ, &hKey ) )
108         {
109                 return false;
110         }
111
112         // Read in the values and recent files
113         keyname[0] = 0;
114         dwSize = MAX_PATH;
115         for ( i = 0; RegEnumValue ( hKey, i, keyname, &dwSize, NULL, NULL, NULL, NULL ) == ERROR_SUCCESS; i ++ )
116         {
117                 temp[0] = '\0'; 
118                 dwSize = MAX_PATH;
119                 
120                 if ( ERROR_SUCCESS != RegQueryValueEx ( hKey, keyname, NULL, &dwType, (LPBYTE)temp, &dwSize ) )
121                 {
122                         continue;
123                 }
124
125                 dwSize = MAX_PATH;
126
127                 // Skip the mru values
128                 if( !idStr(keyname).IcmpPrefix ( "mru" ) )
129                 {
130                         continue;
131                 }
132                 
133                 mValues.Set ( keyname, temp );
134         }                       
135
136         // Read Recent Files
137         for ( i = 0; i < MAX_MRU_SIZE; i ++ )
138         {
139                 dwSize = MAX_PATH;
140                 if ( ERROR_SUCCESS != RegQueryValueEx ( hKey, va("mru%d", i ), NULL, &dwType, (LPBYTE)temp, &dwSize ) )
141                 {       
142                         continue;
143                 }
144                 
145                 AddRecentFile ( temp );
146         }
147
148         return true;
149 }
150
151 /*
152 ================
153 rvRegistryOptions::SetWindowPlacement
154
155 Set a window placement in the options
156 ================
157 */
158 void rvRegistryOptions::SetWindowPlacement ( const char* name, HWND hwnd )
159 {
160         WINDOWPLACEMENT wp;
161         
162         wp.length = sizeof(wp);
163         ::GetWindowPlacement ( hwnd, &wp );
164         
165         idStr out;
166         
167         out = va("%d %d %d %d %d %d %d %d %d %d",
168                          wp.flags,
169                          wp.ptMaxPosition.x,
170                          wp.ptMaxPosition.y,
171                          wp.ptMinPosition.x,
172                          wp.ptMinPosition.y,
173                          wp.rcNormalPosition.left,
174                          wp.rcNormalPosition.top,
175                          wp.rcNormalPosition.right,
176                          wp.rcNormalPosition.bottom,
177                          wp.showCmd );
178                          
179         mValues.Set ( name, out );       
180 }
181
182 /*
183 ================
184 rvRegistryOptions::GetWindowPlacement
185
186 Retrieve a window placement from the options
187 ================
188 */
189 bool rvRegistryOptions::GetWindowPlacement ( const char* name, HWND hwnd )
190 {
191         WINDOWPLACEMENT wp;
192         wp.length = sizeof(wp);
193
194         const idKeyValue* key = mValues.FindKey ( name );
195         if ( !key )
196         {
197                 return false;
198         }
199
200         sscanf ( key->GetValue().c_str(), "%d %d %d %d %d %d %d %d %d %d",
201                          &wp.flags,
202                          &wp.ptMaxPosition.x,
203                          &wp.ptMaxPosition.y,
204                          &wp.ptMinPosition.x,
205                          &wp.ptMinPosition.y,
206                          &wp.rcNormalPosition.left,
207                          &wp.rcNormalPosition.top,
208                          &wp.rcNormalPosition.right,
209                          &wp.rcNormalPosition.bottom,
210                          &wp.showCmd );
211                          
212         ::SetWindowPlacement ( hwnd, &wp );
213         
214         return true;
215 }
216
217 /*
218 ================
219 rvRegistryOptions::AddRecentFile
220
221 Adds the given filename to the MRU list
222 ================
223 */
224 void rvRegistryOptions::AddRecentFile ( const char* filename )
225 {
226         int i;
227         
228         idStr path = filename;
229
230         // Remove duplicates first
231         for ( i = mRecentFiles.Num() - 1; i >= 0; i -- )
232         {
233                 if ( !mRecentFiles[i].Icmp ( filename ) )
234                 {
235                         mRecentFiles.RemoveIndex ( i );
236                         break;
237                 }
238         }
239         
240         // Alwasy trip to the max MRU size
241         while ( mRecentFiles.Num ( ) >= MAX_MRU_SIZE )
242         {
243                 mRecentFiles.RemoveIndex ( 0 );
244         }
245                 
246         mRecentFiles.Append ( path );   
247 }
248
249 /*
250 ================
251 rvRegistryOptions::SetColumnWidths
252
253 Set a group of column widths in the options
254 ================
255 */
256 void rvRegistryOptions::SetColumnWidths ( const char* name, HWND list )
257 {
258         LVCOLUMN col;
259         int              index;
260         idStr    widths;
261         
262         col.mask = LVCF_WIDTH;  
263         
264         for ( index = 0; ListView_GetColumn ( list, index, &col ); index ++ )
265         {
266                 widths += va("%d ", col.cx );
267         }
268         
269         mValues.Set ( name, widths );
270 }
271
272 /*
273 ================
274 rvRegistryOptions::GetColumnWidths
275
276 Retrieve a group of column widths from the options
277 ================
278 */
279 void rvRegistryOptions::GetColumnWidths ( const char* name, HWND list )
280 {
281         idStr           widths;
282         const char* parse;
283         const char* next;
284         int                     index;
285         
286         widths = mValues.GetString ( name );
287         parse = widths;
288         index = 0;
289         
290         while ( NULL != (next = strchr ( parse, ' ' ) ) )
291         {
292                 int width;
293                 
294                 sscanf ( parse, "%d", &width );
295                 parse = next + 1;
296                 
297                 ListView_SetColumnWidth ( list, index++, width );
298         }
299 }
300
301 /*
302 ================
303 rvRegistryOptions::SetBinary
304
305 Set binary data for the given key
306 ================
307 */
308 void rvRegistryOptions::SetBinary ( const char* name, const unsigned char* data, int size )
309 {
310         idStr binary;
311         for ( size --; size >= 0; size --, data++ )
312         {
313                 binary += va("%02x", *data );
314         }
315         
316         mValues.Set ( name, binary );
317 }
318
319 /*
320 ================
321 rvRegistryOptions::GetBinary
322
323 Get the binary data for a given key
324 ================
325 */
326 void rvRegistryOptions::GetBinary ( const char* name, unsigned char* data, int size )
327 {
328         const char* parse;
329         parse = mValues.GetString ( name );
330         for ( size --; size >= 0 && *parse && *(parse+1); size --, parse += 2, data ++  )
331         {
332                 int value;
333                 sscanf ( parse, "%02x", &value );
334                 *data = (unsigned char)value;
335         }       
336 }