2 ===========================================================================
5 Copyright (C) 1999-2011 id Software LLC, a ZeniMax Media company.
7 This file is part of the Doom 3 GPL Source Code (?Doom 3 Source Code?).
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.
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.
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/>.
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.
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.
26 ===========================================================================
29 #include "../../idlib/precompiled.h"
32 #include "RegistryOptions.h"
36 rvRegistryOptions::rvRegistryOptions
41 rvRegistryOptions::rvRegistryOptions( void ) {
46 rvRegistryOptions::Init
49 void rvRegistryOptions::Init( const char *key ) {
55 rvRegistryOptions::Save
57 Write the options to the registry
60 bool rvRegistryOptions::Save ( void )
65 // Create the top level key
66 if ( ERROR_SUCCESS != RegCreateKeyEx ( HKEY_LOCAL_MACHINE, mBaseKey, 0, NULL, 0, KEY_ALL_ACCESS, NULL, &hKey, NULL ) )
71 // Write out the values
72 for ( i = 0; i < mValues.GetNumKeyVals(); i ++ )
74 const idKeyValue* key = mValues.GetKeyVal ( i );
76 RegSetValueEx ( hKey, key->GetKey().c_str(), 0, REG_SZ, (BYTE*)key->GetValue().c_str(), key->GetValue().Length() );
80 for ( i = 0; i < mRecentFiles.Num(); i ++ )
82 RegSetValueEx ( hKey, va("mru%d",i), 0, REG_SZ, (BYTE*)mRecentFiles[i].c_str(), mRecentFiles[i].Length() );
90 rvRegistryOptions::Load
92 Read the options from the registry
95 bool rvRegistryOptions::Load ( void )
99 TCHAR keyname[MAX_PATH];
105 mRecentFiles.Clear ( );
107 if ( ERROR_SUCCESS != RegOpenKeyEx ( HKEY_LOCAL_MACHINE, mBaseKey, 0, KEY_READ, &hKey ) )
112 // Read in the values and recent files
115 for ( i = 0; RegEnumValue ( hKey, i, keyname, &dwSize, NULL, NULL, NULL, NULL ) == ERROR_SUCCESS; i ++ )
120 if ( ERROR_SUCCESS != RegQueryValueEx ( hKey, keyname, NULL, &dwType, (LPBYTE)temp, &dwSize ) )
127 // Skip the mru values
128 if( !idStr(keyname).IcmpPrefix ( "mru" ) )
133 mValues.Set ( keyname, temp );
137 for ( i = 0; i < MAX_MRU_SIZE; i ++ )
140 if ( ERROR_SUCCESS != RegQueryValueEx ( hKey, va("mru%d", i ), NULL, &dwType, (LPBYTE)temp, &dwSize ) )
145 AddRecentFile ( temp );
153 rvRegistryOptions::SetWindowPlacement
155 Set a window placement in the options
158 void rvRegistryOptions::SetWindowPlacement ( const char* name, HWND hwnd )
162 wp.length = sizeof(wp);
163 ::GetWindowPlacement ( hwnd, &wp );
167 out = va("%d %d %d %d %d %d %d %d %d %d",
173 wp.rcNormalPosition.left,
174 wp.rcNormalPosition.top,
175 wp.rcNormalPosition.right,
176 wp.rcNormalPosition.bottom,
179 mValues.Set ( name, out );
184 rvRegistryOptions::GetWindowPlacement
186 Retrieve a window placement from the options
189 bool rvRegistryOptions::GetWindowPlacement ( const char* name, HWND hwnd )
192 wp.length = sizeof(wp);
194 const idKeyValue* key = mValues.FindKey ( name );
200 sscanf ( key->GetValue().c_str(), "%d %d %d %d %d %d %d %d %d %d",
206 &wp.rcNormalPosition.left,
207 &wp.rcNormalPosition.top,
208 &wp.rcNormalPosition.right,
209 &wp.rcNormalPosition.bottom,
212 ::SetWindowPlacement ( hwnd, &wp );
219 rvRegistryOptions::AddRecentFile
221 Adds the given filename to the MRU list
224 void rvRegistryOptions::AddRecentFile ( const char* filename )
228 idStr path = filename;
230 // Remove duplicates first
231 for ( i = mRecentFiles.Num() - 1; i >= 0; i -- )
233 if ( !mRecentFiles[i].Icmp ( filename ) )
235 mRecentFiles.RemoveIndex ( i );
240 // Alwasy trip to the max MRU size
241 while ( mRecentFiles.Num ( ) >= MAX_MRU_SIZE )
243 mRecentFiles.RemoveIndex ( 0 );
246 mRecentFiles.Append ( path );
251 rvRegistryOptions::SetColumnWidths
253 Set a group of column widths in the options
256 void rvRegistryOptions::SetColumnWidths ( const char* name, HWND list )
262 col.mask = LVCF_WIDTH;
264 for ( index = 0; ListView_GetColumn ( list, index, &col ); index ++ )
266 widths += va("%d ", col.cx );
269 mValues.Set ( name, widths );
274 rvRegistryOptions::GetColumnWidths
276 Retrieve a group of column widths from the options
279 void rvRegistryOptions::GetColumnWidths ( const char* name, HWND list )
286 widths = mValues.GetString ( name );
290 while ( NULL != (next = strchr ( parse, ' ' ) ) )
294 sscanf ( parse, "%d", &width );
297 ListView_SetColumnWidth ( list, index++, width );
303 rvRegistryOptions::SetBinary
305 Set binary data for the given key
308 void rvRegistryOptions::SetBinary ( const char* name, const unsigned char* data, int size )
311 for ( size --; size >= 0; size --, data++ )
313 binary += va("%02x", *data );
316 mValues.Set ( name, binary );
321 rvRegistryOptions::GetBinary
323 Get the binary data for a given key
326 void rvRegistryOptions::GetBinary ( const char* name, unsigned char* data, int size )
329 parse = mValues.GetString ( name );
330 for ( size --; size >= 0 && *parse && *(parse+1); size --, parse += 2, data ++ )
333 sscanf ( parse, "%02x", &value );
334 *data = (unsigned char)value;